Drive Coroutine Desk from your own code
The page reads the pasted Kotlin behind one feature in the browser for free — one or more files
joined into one paste, each introduced by a // file: <path> line, plus
build.gradle.kts (or a libs.versions.toml) for the Kotlin and
kotlinx.coroutines versions — with its line numbers. It lexes strings, string templates, raw
strings, char literals, backtick names and comments out first, rebuilds the brace structure (classes,
funs, lambdas, loops, try/catch), matches every trailing lambda to its call, works out the scope and
dispatcher of every line of coroutine code, and inventories every fun and whether it is
suspend, every coroutine builder (launch, async,
runBlocking, withContext, coroutineScope, flow,
callbackFlow ...) with the scope and dispatcher it runs on, every call coroutine code makes
that blocks its thread, every catch around suspending calls and whether it lets
CancellationException through, every StateFlow, SharedFlow and
Channel property and what is done to it, and every collect and whether code
after it can run. Then it runs 17 deterministic checks drawn from coroutine practice (catalogue ids
KC-01..KC-17, plus KC-18 for context): launch or async on
GlobalScope or a throwaway CoroutineScope(...), runBlocking in
coroutine code or on the main thread, Thread.sleep in coroutine code, blocking network,
database and wait calls (OkHttp execute(), URL.readText(), JDBC,
Future.get(), CountDownLatch.await(), BlockingQueue.take()) and
blocking file I/O off Dispatchers.IO, catches and runCatching that swallow
CancellationException, loops that never suspend or check for cancellation,
async whose Deferred is never awaited, Job() or
SupervisorJob() passed to a builder, callbackFlow without
awaitClose, a MutableStateFlow holding a mutable collection or changed in
place, value = value... instead of update { }, mutable flows exposed as public
properties, code after collecting a flow that never completes, suspendCoroutine instead of
suspendCancellableCoroutine, credential literals, and async { }.await() at
once. Nothing is compiled or run: every flag is a reading of the text. Its two paid steps are available
over HTTP. You send one JSON object with task set to "audit" or
"fix", the paste as code and the page’s check as a
facts string, and the model replies with one JSON object: an audit that rates the four
areas, answers every flag and gives exact before/after edits per finding, or the whole fixed file(s)
with a list of every change tied to the page’s flags.
Base URL: https://api.skillsafe.ai/v1/app-api. Every call sends
Content-Type: application/json and, once you have a token,
Authorization: Bearer <token>. The paths below (/guest,
/me, /estimate, /run, /jobs/{job_id},
/run-stream) are relative to it, exactly as sdk.js calls them. A
plain-text summary of the app for agents is at /llms.txt. Both lanes run on
the gpt-terra model with a 10% publisher markup. A run needs a signed-in (user) token and
is metered: the platform places a hold on your credits when the run starts and then
charges only what the run used. /estimate is free.
The response envelope
Responses are JSON of the form {"ok": ..., "data": ..., "error": {...}}. The page’s
SDK (sdk.js) decides success from the HTTP status, not from ok: on a
2xx it returns data; otherwise it raises an error whose message is
error.message (or the HTTP status text when the body carries none), with
status, code (error.code) and details
(error.details) attached. A body that is not JSON is treated as {}.
{ "ok": true, "data": { ... } }
{ "ok": false, "error": { "code": "...", "message": "...", "details": { ... } } }
Error codes
Branch on the HTTP status and error.code, show error.message. The page
handles 401 and 402 by name; the rest reach it as a generic failure (“The run failed: ...”).
| HTTP | error.code | What it means / what to do |
|---|---|---|
400 | VALIDATION_ERROR | The body is not a JSON object, a required field (task, code, facts) is missing, or a declared field has the wrong type. Every field is a string; sending facts or audit_handoff as an object is the usual cause. |
401 | UNAUTHORIZED | No token, or one that expired or was revoked. The page forgets the signed-in user and says “Your session expired - sign in again.” Get a new token. |
402 | INSUFFICIENT_CREDITS | The balance cannot cover the run. The page says “Not enough credits for this run.” with a top-up link, and tries never to submit into a 402: it disables the run button when the /me balance is below the estimate’s min_credits. |
404 | NOT_FOUND | A wrong path, or a job id that does not belong to this token. |
409 | CONFLICT | An Idempotency-Key replayed with a different body. Send the original body, or use a new key (the page bumps its :a<attempt> suffix for a retry). |
429 | RATE_LIMITED | Too many requests. Back off with a delay; do not tight-loop, and never resend a metered run without its original Idempotency-Key. |
Failures that do not arrive as an HTTP status:
| What | What happens |
|---|---|
event: error on /run-stream | The SDK raises an error carrying the event’s message, code and job_id. |
Job status: "failed" | Polling stops; the job’s error field says why. |
| Polling past 180 s | The SDK’s own client-side limit (waitForJob, 1 s interval, 180 000 ms default): it throws “job timed out”. The job itself is not cancelled. |
The task field: two lanes
task routes the run, and it takes exactly two values: "audit" and
"fix". Each lane has its own reply contract, and a reply never mixes them. The
skill’s instructions tell the model to do the audit lane when task is missing or
anything else (and the page’s own Kotkit.buildInput turns any lane that is not
"fix" into "audit"), so a typo silently gets you an audit: always send one of
the two values.
task | What it does | verdict | Use it when |
|---|---|---|---|
"audit" | Reviews the code the way a senior Kotlin engineer reviews coroutine code before it ships: says what the feature does and what runs concurrently in it, rates the four areas (structure, threads, cancellation, flows) as sound, fragile or broken, confirms or dismisses every page flag, adds what text rules cannot see, and gives the verbatim evidence and exact before/after edits for each finding, listed in the order to apply them — what freezes, deadlocks or crashes first (runBlocking, Thread.sleep, loops that never suspend, callbackFlow without awaitClose), then blocking calls off Dispatchers.IO, then lost cancellation, failures and scopes, then flow state and collection, then hygiene — with a plan that ends in a test under runTest (kotlinx-coroutines-test) and a cancellation test. | blocked | needs_work | ready | Before shipping: does every coroutine have a scope that cancels it, does anything block its thread, does cancellation get through every catch and loop, does state reach the collectors, and what should change first. |
"fix" | Writes the fixed version of the pasted code: the whole of every file it changes (at most one new file next to them), with coroutines moved onto a scope that owns them, blocking calls inside withContext(Dispatchers.IO) or replaced by delay, CancellationException rethrown, loops that suspend or check ensureActive(), awaitClose in every callbackFlow, and state held immutably and changed with update { } — plus a changes list tied to the page flags, the verify steps (the build, e.g. ./gradlew build, and the runTest test to write), and what it left open. Every fun keeps its name, parameters and suspend or not, and no public signature changes unless the note asks for it. | fixed | partial | When you want the code changed, not a list of findings: a feature you already know is broken, or what is left after an audit. |
The page chains the two: after an audit, “Apply every edit and fix the rest” puts
every audit edit into the paste, switches to the fix lane on that edited paste and adds an
audit_handoff string (below) with the audit’s verdict, headline and findings. Over
the API that is a second run with task: "fix", the edited paste as code,
facts recomputed from the edited paste, and that string. The page drops the handoff as
soon as the paste is edited again.
Guards the page applies before a run and the API does not: it refuses to price or run a paste it cannot read as Kotlin (“Nothing was pasted.”, “No Kotlin file was found. Paste .kt source, with a // file: <path>.kt line above each file.”, or “This does not read as Kotlin source: no fun, class, object, import or package was found.”), and it warns before a run on a paste with no coroutine code (“No suspend fun and no coroutine was found: there may be little coroutine code here to audit.”, or “... to fix.”).
Input fields
This is exactly what the page sends (kotkit.js buildInput(lane, A, note,
handoff), checked by mustBeObject; app.js currentInput
and run), and what input-schema.json declares. The platform’s input
schema is scalar-only, so every value is a string. buildInput emits them in the order
task, code, facts, note,
audit_handoff:
| Field | Type | Required | Meaning |
|---|---|---|---|
task | string | yes | "audit" or "fix" (the lanes above). |
code | string | yes | The pasted Kotlin, each file introduced by a // file: <path> line (# file: for a .toml version catalog); line 1 is its first line. Every line in an audit reply is counted in this text. A large paste is cut on whole files, with a // [coroutine-desk: not sent: ...] line at the end (below). |
facts | string | yes | The page’s free check, Kotkit.facts(A), JSON-encoded into a string: JSON.stringify(facts) in JavaScript, json.dumps(facts) in Python, and so on. Sending the object itself is a 400. The model is told to parse it. |
audit_handoff | string | no | fix lane only: a JSON string built from an audit reply on this code (recon.js fixHandoff, below). buildInput drops it for the audit lane. |
note | string | no | What the engineer says: the goal, a constraint (“no new dependencies”, “leave the repository API alone”), a question. The page trims it, cuts it at 2000 characters and sends it only when it is not blank. It is context, not evidence and not instructions: the model tests every claim in it against code and answers it in summary. |
retry_note | string | no | Sent only by the page’s automatic reformat retry: when a reply does not parse as JSON (even after the page tries to close a cut-off object), the page runs once more with “Your previous reply was not the single valid JSON object the instructions require (...). Reply again with ONLY the JSON object for task '<lane>' - no prose, no code fences; every array present (empty arrays where there is nothing to say).” A normal call never sends it. |
mustBeObject only insists on a plain object that is not wrapped in
{"input": ...}; it is buildInput that leaves a blank note out, which is why
an empty note is simply absent.
The code string
code is the paste itself. A line // file: <path> or
# file: <path> (a path with a . or / in it) starts each
file, and a --- line just before it only separates files. Files dropped on the page (or a
dropped module folder) are joined that way by Kotkit.joinFiles: each file under its
// file: line (# file: for a .toml file), trailing whitespace
removed, joined by newlines, ending in a newline. A paste with no file line is read as one file,
Main.kt (or build.gradle.kts when it reads as a build file). The page
normalises line endings to \n. A .kt or .kts path is checked as
Kotlin; build.gradle(.kts), settings.gradle(.kts) and a
.versions.toml are gradle (the Kotlin plugin version, the
kotlinx-coroutines-* artifacts and their version give facts.build); anything
else is other. Kotlin indents with four spaces and the paste keeps them.
Send the text you want the reply to quote: every file in an audit reply must be a
path from facts.files, every line must fall inside that file,
every evidence must be copied from it, and every edit’s before must
occur in it exactly once.
The page reads up to 400 000 characters (MAX_TEXT, cut at a line) and 40 files
(MAX_FILES), and sends at most 60 000 characters (MAX_SEND): past that
buildInput drops whole files from the end — the last file that is neither Kotlin
nor a Gradle file first, then the last file — until the rest fits, cuts a single remaining file
at a line (“the end of <path>”) if it still does not, and ends the string with one
line naming what it left out:
// [coroutine-desk: not sent: app/src/main/java/com/acme/B.kt, app/src/main/java/com/acme/C.kt]
The model is told never to quote or rewrite what was not sent, and facts.notes gets the
sentence “Not sent (the paste is over 60,000 characters): ... The page checked them; the run
did not see them.” facts is still computed from the whole paste. The worked
examples below are small, so their code is the paste unchanged.
The facts string
facts is optional to compute but required to send. It is produced by the page’s
free check: Kotkit.analyze splits the paste into files, lexes them, rebuilds their
brace structure, works out the scope and dispatcher of the coroutine code, inventories it and runs the
rules, and Kotkit.facts(A) turns that analysis into the object below (it does not depend
on the lane: an audit and a fix run of the same paste carry the same facts). It is how the model knows
which flags to answer, which file paths and line ranges to name, and which functions must survive. Its
top-level keys, in the order built:
| Key | Contents |
|---|---|
build | kotlin and coroutines (the versions a pasted build file declares, or null), coroutines_artifacts (core, android, test ...), gradle_pasted, android (the build or the code is Android: the Android Gradle plugin, androidx. or android. imports, viewModelScope, lifecycleScope), and counts: kotlin_files, functions, suspend_funs, builders, flows. |
files | Every file: path (use it verbatim), kind (kotlin, gradle, other), line and end_line (the paste lines of the file’s content, without its // file: line). |
functions | Every fun (up to 200), interface members included: name (Class.fun for members), file, line, suspend, private. |
coroutines | Every coroutine builder (up to 120): file, line, func, builder (launch, async, runBlocking, withContext, callbackFlow, stateIn ...), scope (the receiver it is called on, or null), dispatcher (io, default, main, unconfined, injected — a dispatcher passed in — or null when the code does not say), in_loop, kept (for async: whether its Deferred is used; null otherwise). |
blocking_calls | Every call coroutine code makes that blocks its thread (up to 120): file, line, func, call, id (the catalogue id it falls under), dispatcher (the one it runs on; caller for a suspend fun with no withContext of its own: it runs on whatever dispatcher its caller uses). |
catches | Every catch around a suspending call in coroutine code (up to 60): file, line, func, type, suspends (the suspending calls it wraps), rethrows (it throws, checks for CancellationException or calls ensureActive(), or a CancellationException catch comes first), in_loop. |
state_flows | Every MutableStateFlow, MutableSharedFlow, StateFlow, SharedFlow and Channel property (up to 60): name, kind, file, line, visibility, holds_mutable, changed_in_place and read_modify_write (how many places do each). |
collects | Every collect (up to 60): file, line, func, flow, hot (a StateFlow or SharedFlow, which never completes), code_after (the first line after it in the same coroutine, or null). |
flags | Every flag the page raised: uid (F1, F2, ... in severity, file, then line order), id (KC-01..KC-18), sev (high | medium | low | info), file, line, text, detail, and lines (every line an aggregated flag covers, only when it covers more than one). An audit must answer every non-info flag by its uid; a fix must close every high and medium flag or name it in open. |
verdict_candidate | The audit verdict the flags point at (the page’s own reading): blocked with any high flag, needs_work with any medium, otherwise ready. |
notes | Plain sentences about what was not read or not sent (empty for a paste that fits). |
The simplest source is kotkit.js itself: the page loads it from
/kotkit.js, and it also runs in Node (it sets module.exports).
This script prints what the page would send as code and facts (with the
60 000-character cut applied), with the facts decoded so you can read them; the step 4 samples compact
them back into the page’s string byte for byte. Save the paste as
orders-paste.txt and redirect the output into orders-check.json:
// build-check.js -- run: node build-check.js orders-paste.txt > orders-check.json (kotkit.js saved next to it)
const fs = require("fs");
const Kotkit = require("./kotkit.js");
const A = Kotkit.analyze(fs.readFileSync(process.argv[2], "utf8"));
if (!A.ok) { console.error(A.errors.join("\n")); process.exit(1); }
const inp = Kotkit.buildInput("audit", A, "", null); // code and facts do not depend on the lane
console.log(JSON.stringify({ code: inp.code, facts: JSON.parse(inp.facts) }, null, 2));
const real = A.flags.filter((f) => f.sev !== "info");
console.error(A.files.filter((f) => f.kind === "kotlin").length + " Kotlin file(s), " + A.suspend_funs + " suspend fun(s) of " + A.functions.length + " fun(s), " + A.builders.length + " coroutine builder(s), " + A.flows.length + " flow propert(ies); " +
real.length + " flags (" + A.counts.high + " high, " + A.counts.medium + " medium, " + A.counts.low + " low); verdict_candidate " + A.candidate);
For the orders example it reports 2 Kotlin file(s), 4 suspend fun(s) of 9 fun(s), 3 coroutine builder(s), 2 flow propert(ies); 6 flags (2 high, 3 medium, 1 low); verdict_candidate blocked; for the tracker example,
1 Kotlin file(s), 3 suspend fun(s) of 7 fun(s), 2 coroutine builder(s), 0 flow propert(ies); 3 flags (2 high, 1 medium, 0 low); verdict_candidate blocked. For both worked examples its code and facts are
exactly what the page sends.
Without the page’s check. The platform only checks that facts
is a string, so a minimal object works — "{}", or a hand-built one such as
{"flags": [], "notes": ["No page check was run; read the code directly."]}
— and the model still reads code. But you lose what the check adds: with no
flags there is nothing to confirm, dismiss or close, with no files there are
no paths and line ranges to name, with no build the model does not know the
kotlinx.coroutines version or whether the code is Android, and with no functions nothing
says what must survive a fix and stay suspend or not. Build it with kotkit.js
as above when you can.
The audit_handoff string
Only for task: "fix", and only for the code the audit was about with every audit edit
applied. The page builds both from the normalised audit reply res
(recon.js normalize, below) and the analysis A of the audited
paste, in recon.js fixHandoff:
const run = Recon.applyFindings(A, res.findings, Kotkit); // every finding's edits, in order, each on top of the last
JSON.stringify({ verdict: res.verdict, headline: res.headline,
findings: res.findings.map(f => ({ id: f.id, severity: f.severity, file: f.file, line: f.line, title: f.title, why: f.why, edits: f.edits.length })),
applied: /* edits that applied */, failed: /* edits that did not */ })
// run.A.text is the edited paste: it becomes code, and facts is recomputed from it
That is {"verdict", "headline", "findings": [{id, severity, file, line, title, why, edits}],
"applied", "failed"}, where each finding’s edits is the number of
edits it carried. applyFindings applies every finding’s edits with
Kotkit.applyEdits, in the order listed: each before must occur exactly once
in its file as the earlier edits left it (leading and trailing blank lines ignored), or that edit is
reported as failed and skipped — it is never guessed. The handoff is a previous reply, not
evidence: the model checks it against code and fixes what is left. This script does the
whole step — the edited paste, its recomputed facts and the handoff string, in the same shape as
build-check.js prints, so the samples read it the same way:
// handoff.js -- run: node handoff.js orders-paste.txt orders-reply.txt > orders-fix-check.json (kotkit.js, recon.js next to it)
const fs = require("fs");
const Kotkit = require("./kotkit.js");
const Recon = require("./recon.js");
const A = Kotkit.analyze(fs.readFileSync(process.argv[2], "utf8")); // the audited paste
const res = Recon.normalize(Recon.parseResult(fs.readFileSync(process.argv[3], "utf8")), "audit");
const ho = Recon.fixHandoff(A, res); // every edit, in order
const B = Kotkit.analyze(ho.text); // facts recomputed from the edited paste
const inp = Kotkit.buildInput("fix", B, "", ho.handoff);
console.log(JSON.stringify({ code: inp.code, facts: JSON.parse(inp.facts), audit_handoff: inp.audit_handoff }, null, 2));
console.error(ho.applied + " edit(s) applied, " + ho.failed + " did not apply");
Save the audit’s output.output as orders-reply.txt; for the orders
audit below it reports 14 edit(s) applied, 0 did not apply. The handoff string it builds, decoded, with each why left out:
{
"verdict": "blocked",
"headline": "Thread.sleep in the retry loop and runBlocking in authHeader can freeze the UI thread, and a cancellation-swallowing catch is why polling keeps hitting the API after the screen closes.",
"findings": [
{
"id": "KC-03",
"severity": "high",
"file": "app/src/main/java/com/example/orders/OrderRepository.kt",
"line": 42,
"title": "Thread.sleep blocks the retry loop in fetchOrders",
"why": ...,
"edits": 2
},
{
"id": "KC-02",
"severity": "high",
"file": "app/src/main/java/com/example/orders/OrderSyncViewModel.kt",
"line": 105,
"title": "runBlocking in authHeader blocks the calling thread",
"why": ...,
"edits": 3
},
{
"id": "KC-04",
"severity": "medium",
"file": "app/src/main/java/com/example/orders/OrderRepository.kt",
"line": 37,
"title": "OkHttp execute() runs uncontrolled on the caller's thread",
"why": ...,
"edits": 2
},
{
"id": "KC-01",
"severity": "medium",
"file": "app/src/main/java/com/example/orders/OrderRepository.kt",
"line": 48,
"title": "refreshInBackground launches on GlobalScope",
"why": ...,
"edits": 3
},
{
"id": "KC-06",
"severity": "medium",
"file": "app/src/main/java/com/example/orders/OrderSyncViewModel.kt",
"line": 94,
"title": "Polling's catch swallows CancellationException",
"why": ...,
"edits": 2
},
{
"id": "KC-12",
"severity": "low",
"file": "app/src/main/java/com/example/orders/OrderSyncViewModel.kt",
"line": 95,
"title": "Error state is set with a read-modify-write instead of update { }",
"why": ...,
"edits": 2
}
],
"applied": 14,
"failed": 0
}
The page sends that fix run with the audit’s note and the key
coroutine-desk:fix:f7f6c1dc-3319:a1; for the shipped example it first looks for a saved
reply to replay under orders:fix, the handoff run of the orders audit.
The output contracts
The model is instructed (SKILL.md) to reply with one JSON object and
nothing else — no prose, no code fences — with every array of its lane
present ([] when empty). The run’s output text is that object. In both lanes it
must ground everything in code: a caller, a DI module, a dispatcher, a scope’s
owner, a lifecycle, a test or a library’s behaviour that is not in the paste is never claimed to
exist — it goes into questions; whether a function from another file suspends,
blocks or is thread safe is unknown unless the paste shows it (“if OrderDao.replaceAll is a Room
suspend DAO ...”, never “it is”). It must never write a secret value: a line that
holds a literal credential is never copied into the reply (not in evidence, not in an edit, not in a
file), and no credential, real hostname or endpoint is invented. It must never change what the code
offers its callers: the same public classes, funs and properties with the same names, parameters,
return types and types, and the same suspend-or-not nature of every fun that is not
private, unless the note asks for it. It uses an API only when it is sure the API exists with that name
in kotlinx.coroutines (or AndroidX lifecycle, when the paste uses it), and never adds a dependency the
paste does not already use unless the note allows it. Every page flag whose sev is not
info is answered exactly once (audit lane: in flag_answers), and an info
flag is never reported as a finding.
task: "audit"
task:"audit".verdict:blocked(any finding is high) |needs_work(the worst is medium) |ready(nothing above low; low findings and questions do not stop a release).headline: one sentence.feature: one or two sentences — what the code does and what runs concurrently in it.areas[]: exactly four entries, in the order structure, threads, cancellation, flows:{area, status, note}withstatussound|fragile|broken(brokenwhen a high finding sits in that area,fragilefor a medium one).structure: every coroutine has an owning scope that cancels it, parent-child links intact,asyncawaited, failures reach someone;threads: dispatchers, blocking calls,runBlocking, loops that never suspend;cancellation:CancellationExceptionpropagates, loops and callbacks can be cancelled, cleanup,awaitCloseandinvokeOnCancellation;flows:StateFlowandSharedFlowstate held immutably, updated atomically and exposed read-only, collection that ends,flowOnandcatchplacement.findings[]:{id, flags[], file, line, severity, area, title, evidence, why, edits[]}.idis a catalogue id (KC-01..KC-17) orother;flagslists the pageuids it confirms ([]for something the page did not flag);fileis apathfromfacts.files, copied exactly;lineis the line ofcodewith the offending text, inside that file’sline..end_line;severityishigh(the code freezes a thread, deadlocks, crashes or silently never runs:runBlockingin coroutine code or on the main thread,Thread.sleepin coroutine code, a loop in coroutine code that never suspends, acallbackFlowwith noawaitClose,withContextaroundemitin aflow { }) |medium(work, errors, cancellation or state changes are lost: a blocking network, database or wait call offDispatchers.IO, a catch that swallowsCancellationException,GlobalScopeor a throwaway scope, a rootasyncnobody awaits, aJobpassed to a builder, aMutableStateFlowof a mutable collection or changed in place, code after collecting a flow that never completes, a credential in the source) |low(bounded risk or hygiene: file I/O offDispatchers.IO,value = value.copy(...)instead ofupdate { }, a public mutable flow,suspendCoroutine,async { }.await()at once, an unawaitedasyncinsidecoroutineScope);areaisstructure|threads|cancellation|flows;evidenceis one to three whole lines copied verbatim fromcodeas pasted (indentation may be trimmed).edits[]:{before, after}on that one file. Eachbeforeis a contiguous, exact slice of the file — whole lines with their indentation, as pasted — that occurs exactly once in the file as it stands when the edit is applied;afteris its replacement, with the same indentation (four spaces, never tabs). Findings are applied in the order listed and their edits in order, each on top of the ones before, so a later finding that touches the same lines writes itsbeforeas the earlier one left it. A new import goes into the edit of the first finding that needs it, as an edit of an existingimportline. Every line an edit adds follows the house rules itself, so the page’s re-check raises nothing new. An edit keeps a suspend fun suspend and a plain fun plain — except a private fun, which may become suspend when the same finding changes every caller of it — and never changes a public signature.editsmay be[]only for a low finding or a change outside the paste.flag_answers[]:{flag, status, reason}, exactly one per non-info page flag,statusconfirmed(a finding lists that uid) |dismissed(with the reason).plan[]: the findings in the order to apply them, ending with a test underrunTest(kotlinx-coroutines-test) that would have caught the worst problem and a cancellation test that cancels the scope or job and asserts the work stops.questions[](only what the paste cannot answer) andsummary(two to four sentences, answeringnoteif one was sent).
task: "fix"
task:"fix".verdict:fixed(openis empty) |partial.headline: one sentence.files[]:{path, content}— every pasted file it changes, whole (every line, without the// file:line, indented as the file already is: four spaces per level, never tabs), under itspathfromfacts.files. At most one new file next to them. Only the standard library, kotlinx.coroutines and what the paste already depends on.changes[]:{file, function, flags[], change, why}, one per change.functionis the fun it is in, asfacts.functionsnames it (Class.fun), or(module)for a change outside any fun (an import, a property, a class header) or(gradle)for the build file;flagsare the page flags the change closes. Every high and medium flag appears in theflagsof a change or of anopenitem.verify[]: always the build (./gradlew build, or the module’s test task), plus the test that would have caught the worst problem (arunTesttest with virtual time —advanceTimeBy,advanceUntilIdle— one that cancels the job and asserts the loop or the retry stops, one that collects theStateFlowand asserts the new value arrives, or one that closes the collector of acallbackFlowand asserts the listener is unregistered).open[]:{item, flags[], reason}for what it left unfixed — a decision for the engineer, or code outside the paste.questions[]andsummary, as in the audit lane; with anaudit_handoff, it fixes what the audit left.
How the page reads a reply (recon.js parseResult and
normalize): it strips a code fence and parses from the first { to the
last }. A reply is read as the lane that was asked for, unless it names the other lane
and carries that lane’s body (a files array for fix, a
findings array for audit). An unknown verdict shows as
needs_work or partial and counts as a disagreement; an unknown
severity becomes medium; ids like KC7 or kc 7
become KC-07; flags keeps only F<n> values; findings
with no file, evidence and title are dropped; feature may also arrive as
service or system, file as path, a
change’s function as func and its change as
what, and an open item as path; questions is
capped at 10, plan at 30 and verify at 20. An audit reply carries 9 sections
(verdict, headline, feature, areas, findings, flag_answers, plan, questions, summary) and a fix reply 8
(verdict, headline, files, changes, verify, open, questions, summary), which is what the page counts
when it recovers a partial reply.
Worked examples
Both are examples the page ships (the example buttons on the app page; the full
pastes are in /example.js, ids orders and
tracker), built exactly as the page builds them:
Kotkit.buildInput(lane, Kotkit.analyze(text), note, null), with the real model reply
the page replays for each (/example-result.js). The pastes are shown
whole; the request bodies cut the long code and facts strings with a visible
..., and the replies are pretty-printed excerpts, with ... where entries
were left out. Every value shown is copied from the input or the reply. Neither paste holds a
credential: the orders token comes from a TokenStore at run time, and the companies are
invented.
Audit: an order sync that sleeps and calls OkHttp’s blocking execute() in a suspend fun, runs runBlocking on the main thread, launches on GlobalScope and swallows cancellation in its polling loop
Three files: build.gradle.kts (the Android application plugin, Kotlin
2.0.21, kotlinx-coroutines-android:1.8.1,
lifecycle-viewmodel-ktx:2.8.6 and okhttp:4.12.0),
OrderRepository.kt and OrderSyncViewModel.kt: an Android order sync.
OrderRepository.fetchOrders is a suspend fun with no withContext of its own:
a while (true) retry loop that calls OkHttp’s blocking
client.newCall(request).execute(), catches IOException and waits with
Thread.sleep(500L * attempt); refreshInBackground starts
GlobalScope.launch to fetch and dao.replaceAll(orders).
OrderSyncViewModel.startPolling launches a while (true) loop in
viewModelScope that fetches, publishes a new SyncState and
delay(30_000)s, inside a catch (e: Exception) that writes the error with
_state.value = _state.value.copy(...); refreshNow calls
refreshInBackground(authHeader()), and the private authHeader is
runBlocking { "Bearer " + tokens.currentToken() }. The page’s free check raises
6 flags (2 high, 3 medium, 1 low) plus the KC-18 context flag; its own reading of the verdict is
blocked. The engineer’s note: “Polling keeps hitting the orders API after the screen is closed, and the order list freezes the UI now and then. What do we fix first? Changing OrderRepository's constructor is fine - the ViewModel factory is its only caller.”
The code string, whole: 3,284 characters, 112 lines ending in a newline. Save it as
orders-paste.txt and run
node build-check.js orders-paste.txt > orders-check.json:
// file: build.gradle.kts
plugins {
id("com.android.application")
kotlin("android") version "2.0.21"
}
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.8.1")
implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.8.6")
implementation("com.squareup.okhttp3:okhttp:4.12.0")
}
---
// file: app/src/main/java/com/example/orders/OrderRepository.kt
package com.example.orders
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import okhttp3.OkHttpClient
import okhttp3.Request
import java.io.IOException
data class Order(val id: String, val status: String)
class OrderRepository(
private val client: OkHttpClient,
private val baseUrl: String,
private val dao: OrderDao,
) {
suspend fun fetchOrders(auth: String): List<Order> {
var attempt = 0
while (true) {
try {
val request = Request.Builder()
.url("$baseUrl/v2/orders")
.header("Authorization", auth)
.build()
val body = client.newCall(request).execute().use { it.body!!.string() }
return parseOrders(body)
} catch (e: IOException) {
attempt++
if (attempt >= 3) throw e
Thread.sleep(500L * attempt)
}
}
}
fun refreshInBackground(auth: String) {
GlobalScope.launch {
val orders = fetchOrders(auth)
dao.replaceAll(orders)
}
}
private fun parseOrders(body: String): List<Order> =
body.lines().filter { it.isNotBlank() }.map { line ->
val (id, status) = line.split(",")
Order(id, status)
}
}
interface OrderDao {
suspend fun replaceAll(orders: List<Order>)
suspend fun count(): Int
}
---
// file: app/src/main/java/com/example/orders/OrderSyncViewModel.kt
package com.example.orders
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
data class SyncState(val orders: List<Order> = emptyList(), val error: String? = null)
class OrderSyncViewModel(
private val repository: OrderRepository,
private val tokens: TokenStore,
) : ViewModel() {
private val _state = MutableStateFlow(SyncState())
val state: StateFlow<SyncState> = _state.asStateFlow()
fun startPolling() {
viewModelScope.launch {
while (true) {
try {
val orders = repository.fetchOrders(authHeader())
_state.value = SyncState(orders = orders)
delay(30_000)
} catch (e: Exception) {
_state.value = _state.value.copy(error = e.message)
}
}
}
}
fun refreshNow() {
repository.refreshInBackground(authHeader())
}
private fun authHeader(): String = runBlocking {
"Bearer " + tokens.currentToken()
}
}
interface TokenStore {
suspend fun currentToken(): String
}
Its facts, decoded (the request carries this object as one compact JSON string):
{
"build": {
"kotlin": "2.0.21",
"coroutines": "1.8.1",
"coroutines_artifacts": [
"android"
],
"gradle_pasted": true,
"android": true,
"kotlin_files": 2,
"functions": 9,
"suspend_funs": 4,
"builders": 3,
"flows": 2
},
"files": [
{
"path": "build.gradle.kts",
"kind": "gradle",
"line": 2,
"end_line": 11
},
{
"path": "app/src/main/java/com/example/orders/OrderRepository.kt",
"kind": "kotlin",
"line": 14,
"end_line": 64
},
{
"path": "app/src/main/java/com/example/orders/OrderSyncViewModel.kt",
"kind": "kotlin",
"line": 67,
"end_line": 113
}
],
"functions": [
{
"name": "OrderRepository.fetchOrders",
"file": "app/src/main/java/com/example/orders/OrderRepository.kt",
"line": 29,
"suspend": true,
"private": false
},
{
"name": "OrderRepository.refreshInBackground",
"file": "app/src/main/java/com/example/orders/OrderRepository.kt",
"line": 47,
"suspend": false,
"private": false
},
{
"name": "OrderRepository.parseOrders",
"file": "app/src/main/java/com/example/orders/OrderRepository.kt",
"line": 54,
"suspend": false,
"private": true
},
{
"name": "OrderDao.replaceAll",
"file": "app/src/main/java/com/example/orders/OrderRepository.kt",
"line": 62,
"suspend": true,
"private": false
},
{
"name": "OrderDao.count",
"file": "app/src/main/java/com/example/orders/OrderRepository.kt",
"line": 63,
"suspend": true,
"private": false
},
{
"name": "OrderSyncViewModel.startPolling",
"file": "app/src/main/java/com/example/orders/OrderSyncViewModel.kt",
"line": 87,
"suspend": false,
"private": false
},
{
"name": "OrderSyncViewModel.refreshNow",
"file": "app/src/main/java/com/example/orders/OrderSyncViewModel.kt",
"line": 101,
"suspend": false,
"private": false
},
{
"name": "OrderSyncViewModel.authHeader",
"file": "app/src/main/java/com/example/orders/OrderSyncViewModel.kt",
"line": 105,
"suspend": false,
"private": true
},
{
"name": "TokenStore.currentToken",
"file": "app/src/main/java/com/example/orders/OrderSyncViewModel.kt",
"line": 111,
"suspend": true,
"private": false
}
],
"coroutines": [
{
"file": "app/src/main/java/com/example/orders/OrderRepository.kt",
"line": 48,
"func": "OrderRepository.refreshInBackground",
"builder": "launch",
"scope": "GlobalScope",
"dispatcher": "default",
"in_loop": false,
"kept": null
},
{
"file": "app/src/main/java/com/example/orders/OrderSyncViewModel.kt",
"line": 88,
"func": "OrderSyncViewModel.startPolling",
"builder": "launch",
"scope": "viewModelScope",
"dispatcher": "main",
"in_loop": false,
"kept": null
},
{
"file": "app/src/main/java/com/example/orders/OrderSyncViewModel.kt",
"line": 105,
"func": "OrderSyncViewModel.authHeader",
"builder": "runBlocking",
"scope": null,
"dispatcher": null,
"in_loop": false,
"kept": null
}
],
"blocking_calls": [
{
"file": "app/src/main/java/com/example/orders/OrderRepository.kt",
"line": 37,
"func": "OrderRepository.fetchOrders",
"call": "client.newCall(request).execute",
"id": "KC-04",
"dispatcher": "caller"
},
{
"file": "app/src/main/java/com/example/orders/OrderRepository.kt",
"line": 42,
"func": "OrderRepository.fetchOrders",
"call": "Thread.sleep",
"id": "KC-03",
"dispatcher": "caller"
}
],
"catches": [
{
"file": "app/src/main/java/com/example/orders/OrderSyncViewModel.kt",
"line": 94,
"func": "OrderSyncViewModel.startPolling",
"type": "Exception",
"suspends": [
"fetchOrders",
"delay"
],
"rethrows": false,
"in_loop": true
}
],
"state_flows": [
{
"name": "_state",
"kind": "MutableStateFlow",
"file": "app/src/main/java/com/example/orders/OrderSyncViewModel.kt",
"line": 84,
"visibility": "private",
"holds_mutable": false,
"changed_in_place": 0,
"read_modify_write": 1
},
{
"name": "state",
"kind": "StateFlow",
"file": "app/src/main/java/com/example/orders/OrderSyncViewModel.kt",
"line": 85,
"visibility": "public",
"holds_mutable": false,
"changed_in_place": 0,
"read_modify_write": 0
}
],
"collects": [],
"flags": [
{
"uid": "F1",
"id": "KC-03",
"sev": "high",
"file": "app/src/main/java/com/example/orders/OrderRepository.kt",
"line": 42,
"text": "suspend fun OrderRepository.fetchOrders calls Thread.sleep(): Thread.sleep parks the thread and ignores cancellation",
"detail": "Thread.sleep blocks the thread the coroutine runs on - on the main dispatcher the UI freezes (an ANR after five seconds), on Dispatchers.Default or IO one of a small pool of threads stops serving every other coroutine - and it cannot be cancelled, so a cancelled scope still waits it out. delay(ms) suspends instead: the thread is free while it waits and cancellation ends the wait at once."
},
{
"uid": "F2",
"id": "KC-02",
"sev": "high",
"file": "app/src/main/java/com/example/orders/OrderSyncViewModel.kt",
"line": 105,
"text": "runBlocking is called on the main thread in OrderSyncViewModel.authHeader (OrderSyncViewModel)",
"detail": "Every method of a ViewModel, an Activity, a Fragment or a composable is called on the main thread: runBlocking stops that thread until the block finishes - the UI freezes, a slow network turns into an ANR, and a block that needs Dispatchers.Main to resume never does, which deadlocks. Launch it in viewModelScope or lifecycleScope and publish the result through state instead."
},
{
"uid": "F3",
"id": "KC-04",
"sev": "medium",
"file": "app/src/main/java/com/example/orders/OrderRepository.kt",
"line": 37,
"text": "suspend fun OrderRepository.fetchOrders calls client.newCall(request).execute() on whatever dispatcher calls it - on Android usually the main thread: a blocking network, database or wait call runs on a coroutine's thread",
"detail": "Blocking calls (OkHttp Call.execute(), URL.readText(), JDBC executeQuery, Future.get(), CountDownLatch.await(), BlockingQueue.take()) hold the thread until they return. Coroutine code that is not inside withContext(Dispatchers.IO) runs on whatever dispatcher the caller uses - on Android usually the main thread, which throws NetworkOnMainThreadException or freezes the UI - and nothing suspends, so cancellation cannot interrupt it. Wrap the call in withContext(Dispatchers.IO) { ... } (the suspend fun is then main-safe), or use a suspending API (OkHttp's await via an adapter, a suspending Retrofit fun, CompletableFuture.await() from kotlinx-coroutines-jdk8)."
},
{
"uid": "F4",
"id": "KC-01",
"sev": "medium",
"file": "app/src/main/java/com/example/orders/OrderRepository.kt",
"line": 48,
"text": "GlobalScope.launch in OrderRepository.refreshInBackground starts a coroutine that nothing owns",
"detail": "A GlobalScope coroutine lives as long as the process: closing the screen, stopping the service or a failing caller never cancels it, so it keeps working (and holding whatever it captured - a ViewModel, an Activity) after nobody wants the result, and a crash in it is reported nowhere near the code that started it. Launch it in the scope that owns the work - viewModelScope, lifecycleScope, an injected application scope - or make the function suspend and let the caller decide."
},
{
"uid": "F5",
"id": "KC-06",
"sev": "medium",
"file": "app/src/main/java/com/example/orders/OrderSyncViewModel.kt",
"line": 94,
"text": "catch (e: Exception) in OrderSyncViewModel.startPolling wraps fetchOrders() and delay(), and also catches CancellationException, so the loop around it keeps running after the coroutine is cancelled",
"detail": "Cancellation reaches a suspended coroutine as a CancellationException thrown from the suspension point. Exception includes it, and this catch neither rethrows it nor checks for it: the coroutine logs or retries and goes on as if nothing happened - after the screen closed, after withTimeout gave up, after a sibling failed - and structured concurrency waits for it. Rethrow it first (catch (e: CancellationException) { throw e } before this catch, or if (e is CancellationException) throw e), or call coroutineContext.ensureActive() in the catch."
},
{
"uid": "F6",
"id": "KC-12",
"sev": "low",
"file": "app/src/main/java/com/example/orders/OrderSyncViewModel.kt",
"line": 95,
"text": "_state.value is read, changed and written back in 1 place instead of _state.update { }",
"detail": "value = value.copy(...) reads and writes in two steps: when two coroutines (or a coroutine and the main thread) do it at the same time, one of the two changes is lost. update { it.copy(...) } (kotlinx.coroutines.flow.update) retries with compare-and-set until the change lands on the latest value."
},
{
"uid": "F7",
"id": "KC-18",
"sev": "info",
"file": "app/src/main/java/com/example/orders/OrderRepository.kt",
"line": 14,
"text": "context: 2 Kotlin files, 4 suspend fun(s) of 9 fun(s), 3 coroutine builder(s) (2 launch, 0 async), 2 flow or channel properties, kotlinx.coroutines 1.8.1 [android]",
"detail": "Also read: build.gradle.kts (gradle). Only what is pasted is checked: which dispatcher a caller outside the paste uses, whether a function from another file suspends or blocks, and how the scopes are cancelled outside the paste, is outside the check."
}
],
"verdict_candidate": "blocked",
"notes": []
}
The request body (POST /estimate, then POST /run or
/run-stream with Idempotency-Key: coroutine-desk:audit:b94f0c30-320c:a1 as the page sends it):
{
"task": "audit",
"code": "// file: build.gradle.kts\nplugins {\n id(\"com.android.application\")\n kotlin(\"android\") version \"2.0.21\"\n}\n\ndependencies {\n implementation(\"org.jetbrains.kotlinx:kotlinx-coroutines-android:1.8.1\")\n implementation(\"androidx.lifecycle:lifecycle-viewmodel-ktx:2.8.6\")\n implementation(\"com.squareup.okhttp3:okhttp:4.12.0\")\n}\n---\n// file: app/src/main/java/com/example/orders/OrderRepository.kt\npackage com.example.orders\n...",
"facts": "{\"build\":{\"kotlin\":\"2.0.21\",\"coroutines\":\"1.8.1\",\"coroutines_artifacts\":[\"android\"],\"gradle_pasted\":true,\"android\":true,\"kotlin_files\":2,\"functions\":9,\"suspend_funs\":4,\"builders\":3,\"flows\":2},\"files\":...",
"note": "Polling keeps hitting the orders API after the screen is closed, and the order list freezes the UI now and then. What do we fix first? Changing OrderRepository's constructor is fine - the ViewModel factory is its only caller."
}
The reply (output.output), an excerpt: the verdict, the headline, the feature, each
area’s status, and the finding that answers “polling keeps hitting the orders API after
the screen is closed”, with its edits and its flag answer:
{
"task": "audit",
"verdict": "blocked",
"headline": "Thread.sleep in the retry loop and runBlocking in authHeader can freeze the UI thread, and a cancellation-swallowing catch is why polling keeps hitting the API after the screen closes.",
"feature": "OrderRepository fetches orders from a REST endpoint over OkHttp (retrying IOException up to three times) and can push a fire-and-forget refresh into a DAO; OrderSyncViewModel polls fetchOrders every 30 seconds inside viewModelScope, publishing the result through a StateFlow, and fetches a bearer token synchronously before each call.",
"areas": [
{
"area": "structure",
"status": "fragile"
},
{
"area": "threads",
"status": "broken"
},
{
"area": "cancellation",
"status": "fragile"
},
{
"area": "flows",
"status": "sound"
}
],
"findings": [
...,
{
"id": "KC-06",
"flags": [
"F5"
],
"file": "app/src/main/java/com/example/orders/OrderSyncViewModel.kt",
"line": 94,
"severity": "medium",
"area": "cancellation",
"title": "Polling's catch swallows CancellationException",
"evidence": "} catch (e: Exception) {\n_state.value = _state.value.copy(error = e.message)",
"why": "This catch wraps repository.fetchOrders(authHeader()) and delay(30_000) and neither rethrows CancellationException nor checks isActive/ensureActive. When viewModelScope is cancelled (the ViewModel is cleared, e.g. the screen closes) the CancellationException thrown at that suspension point is caught here, _state is written, and the surrounding while(true) loop immediately calls fetchOrders() again - so the poller keeps hitting the orders API after the screen that started it is gone.",
"edits": [
{
"before": "import androidx.lifecycle.viewModelScope\nimport kotlinx.coroutines.delay",
"after": "import androidx.lifecycle.viewModelScope\nimport kotlinx.coroutines.CancellationException\nimport kotlinx.coroutines.delay"
},
{
"before": " } catch (e: Exception) {\n _state.value = _state.value.copy(error = e.message)\n }",
"after": " } catch (e: CancellationException) {\n throw e\n } catch (e: Exception) {\n _state.value = _state.value.copy(error = e.message)\n }"
}
]
},
...
],
"flag_answers": [
...,
{
"flag": "F5",
"status": "confirmed",
"reason": "catch (e: Exception) in startPolling neither rethrows CancellationException nor checks isActive, so cancellation is swallowed."
},
...
],
"plan": [
...,
"Add a kotlinx-coroutines-test runTest that starts polling, advances virtual time past 30_000ms to confirm a second fetch happens, then cancels the job and asserts fetchOrders is not called again - this would have caught F5 letting the loop survive cancellation - plus a test that makes fetchOrders throw and asserts state.value.error is set."
],
"questions": [
...
],
"summary": ...
}
The reply has 6 findings, one per flag, and confirms all 6 flags; they carry 14 edits in all. In
order: Thread.sleep(500L * attempt) becomes delay(500L * attempt), with the
delay import (F1, KC-03); authHeader becomes a private
suspend fun without runBlocking, and refreshNow calls it from
viewModelScope.launch { } (F2, KC-02); the OkHttp call moves into
withContext(Dispatchers.IO) { } (F3, KC-04); OrderRepository gets a
private val externalScope: CoroutineScope constructor parameter, which the note allows,
and refreshInBackground launches on it instead of GlobalScope (F4, KC-01);
the finding above rethrows CancellationException before the general catch (F5, KC-06);
and the error branch uses _state.update { it.copy(error = e.message) } (F6, KC-12). Every
finding writes its import edit on top of the import lines the earlier findings left, which is why all
14 apply in order and the re-check below raises nothing.
Fix: a route recorder whose callbackFlow has no awaitClose and whose finish() spins on a volatile flag, with a launch(SupervisorJob()) left in place on purpose
One file, RouteRecorder.kt, and no build file. RouteRecorder.fixes is a
callbackFlow { } that registers a LocationSource listener sending each fix
with trySendBlocking(fix) and then ends without awaitClose, so the flow
fails as soon as it is collected and the listener is never unregistered;
record calls scope.launch(SupervisorJob()) to collect it and sets a
@Volatile uploading flag around store.append(trackId, fix);
and the suspend fun finish waits with an empty while (uploading) { } loop
before store.close(trackId). The free check raises 3 flags (2 high, 1 medium, 0 low); its reading of the
verdict is blocked. This example has no audit before it, so it carries no
audit_handoff. The engineer’s note: “Recording crashes as soon as it starts and finish() pins a CPU core. Fix those two. Leave the launch(SupervisorJob()) in record() exactly as it is for now - the platform team moves the recorder onto its own supervised scope next sprint.”
The code string, whole: 1,381 characters, 51 lines ending in a newline. Save it as
tracker-paste.txt and run
node build-check.js tracker-paste.txt > tracker-check.json:
// file: app/src/main/java/com/example/tracks/RouteRecorder.kt
package com.example.tracks
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.channels.trySendBlocking
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.launch
data class Fix(val lat: Double, val lon: Double, val accuracyM: Float)
interface LocationSource {
fun register(listener: (Fix) -> Unit): Int
fun unregister(handle: Int)
}
interface TrackStore {
suspend fun append(trackId: String, fix: Fix)
suspend fun close(trackId: String)
}
class RouteRecorder(
private val source: LocationSource,
private val store: TrackStore,
private val scope: CoroutineScope,
) {
@Volatile private var uploading = false
fun fixes(): Flow<Fix> = callbackFlow {
val handle = source.register { fix -> trySendBlocking(fix) }
}
fun record(trackId: String) {
scope.launch(SupervisorJob()) {
fixes().collect { fix ->
if (fix.accuracyM < 50f) {
uploading = true
store.append(trackId, fix)
uploading = false
}
}
}
}
suspend fun finish(trackId: String) {
while (uploading) {
}
store.close(trackId)
}
}
Its facts, decoded, cut to files, flags and
verdict_candidate (the full object also carries the build block — no
build file pasted, so kotlin and coroutines are null —
the 7 functions, 3 of them suspend, the 2 coroutine builders (callbackFlow in
RouteRecorder.fixes and scope.launch in RouteRecorder.record),
the one collect of fixes(), which is not hot, and empty
blocking_calls, catches and state_flows):
{
"files": [
{
"path": "app/src/main/java/com/example/tracks/RouteRecorder.kt",
"kind": "kotlin",
"line": 2,
"end_line": 52
}
],
"flags": [
{
"uid": "F1",
"id": "KC-10",
"sev": "high",
"file": "app/src/main/java/com/example/tracks/RouteRecorder.kt",
"line": 30,
"text": "callbackFlow { } in RouteRecorder.fixes never calls awaitClose",
"detail": "A callbackFlow's block must end in awaitClose { unregister(...) }: without it the block returns right after registering the listener, the flow fails with IllegalStateException (\"'awaitClose { yourCallbackOrListener.cancel() }' should be used in the end of callbackFlow block\"), and the listener is never removed - it keeps calling trySend on a closed channel and leaks whatever it captured. End the block with awaitClose { ... } that unregisters the callback."
},
{
"uid": "F2",
"id": "KC-07",
"sev": "high",
"file": "app/src/main/java/com/example/tracks/RouteRecorder.kt",
"line": 47,
"text": "while (uploading) in RouteRecorder.finish spins without suspending or checking for cancellation",
"detail": "The loop waits for a condition by spinning: it never suspends, so it burns its thread at 100% until the condition changes - and if the code that would change it needs the same thread (the main dispatcher, a single-thread context), it never does. Cancelling the scope does not stop it either. Wait with a suspending primitive (a StateFlow's first { it }, a Channel, a CompletableDeferred, delay between checks) and check ensureActive()."
},
{
"uid": "F3",
"id": "KC-09",
"sev": "medium",
"file": "app/src/main/java/com/example/tracks/RouteRecorder.kt",
"line": 35,
"text": "launch(SupervisorJob()) in RouteRecorder.record cuts the coroutine off from its parent",
"detail": "A Job passed to a builder replaces the parent's job in the new coroutine's context, so the new coroutine is no longer a child of the scope it was launched from: cancelling that scope (viewModelScope when the screen closes, a coroutineScope when a sibling fails) does not cancel it, and its failure does not reach the parent. For supervision, use supervisorScope { } or a scope built as CoroutineScope(SupervisorJob() + dispatcher); to stop cancellation of cleanup, use withContext(NonCancellable)."
},
{
"uid": "F4",
"id": "KC-18",
"sev": "info",
"file": "app/src/main/java/com/example/tracks/RouteRecorder.kt",
"line": 2,
"text": "context: 1 Kotlin file, 3 suspend fun(s) of 7 fun(s), 2 coroutine builder(s) (1 launch, 0 async), 0 flow or channel properties, no build file pasted",
"detail": "Only what is pasted is checked: which dispatcher a caller outside the paste uses, whether a function from another file suspends or blocks, and how the scopes are cancelled outside the paste, is outside the check."
}
],
"verdict_candidate": "blocked"
}
The request body (Idempotency-Key: coroutine-desk:fix:e99d934e-1968:a1):
{
"task": "fix",
"code": "// file: app/src/main/java/com/example/tracks/RouteRecorder.kt\npackage com.example.tracks\n\nimport kotlinx.coroutines.CoroutineScope\nimport kotlinx.coroutines.SupervisorJob\nimport kotlinx.coroutines.channels.trySendBlocking\nimport kotlinx.coroutines.flow.Flow\nimport kotlinx.coroutines.flow.callbackFlow\nimport kotlinx.coroutines.launch\n...",
"facts": "{\"build\":{\"kotlin\":null,\"coroutines\":null,\"coroutines_artifacts\":[],\"gradle_pasted\":false,\"android\":false,\"kotlin_files\":1,\"functions\":7,\"suspend_funs\":3,\"builders\":2,\"flows\":0},\"files\":[{\"path\":\"app/...",
"note": "Recording crashes as soon as it starts and finish() pins a CPU core. Fix those two. Leave the launch(SupervisorJob()) in record() exactly as it is for now - the platform team moves the recorder onto its own supervised scope next sprint."
}
The reply (output.output), an excerpt: the verdict, the headline, the returned file from
the new Mutex on, the change that stops the spin, the last verification step and the open
item:
{
"task": "fix",
"verdict": "partial",
"headline": "Added awaitClose to fixes() and replaced the busy-spin in finish() with a Mutex wait; left launch(SupervisorJob()) in record() exactly as the note asked.",
"files": [
{
"path": "app/src/main/java/com/example/tracks/RouteRecorder.kt",
"content": "package com.example.tracks\n...\n private val uploadLock = Mutex()\n\n fun fixes(): Flow<Fix> = callbackFlow {\n val handle = source.register { fix -> trySendBlocking(fix) }\n awaitClose { source.unregister(handle) }\n }\n\n fun record(trackId: String) {\n scope.launch(SupervisorJob()) {\n fixes().collect { fix ->\n if (fix.accuracyM < 50f) {\n uploadLock.withLock {\n store.append(trackId, fix)\n }\n }\n }\n }\n }\n\n suspend fun finish(trackId: String) {\n uploadLock.withLock { }\n store.close(trackId)\n }\n}\n"
}
],
"changes": [
...,
{
"file": "app/src/main/java/com/example/tracks/RouteRecorder.kt",
"function": "RouteRecorder.finish",
"flags": [
"F2"
],
"change": "Replaced while (uploading) { } with uploadLock.withLock { } before store.close(trackId).",
"why": "Suspends at a real cancellation point until any in-flight append releases the lock, instead of spinning the thread at 100% and never suspending or reacting to cancellation."
}
],
"verify": [
...,
"A runTest with a fake TrackStore.append that suspends on a CompletableDeferred until released: call record(trackId), then launch finish(trackId) while append is still suspended, advanceUntilIdle(), and assert store.close is only reached after the deferred completes and append returns - this would have caught the busy-spin (F2), which otherwise burns real CPU instead of suspending."
],
"open": [
{
"item": "launch(SupervisorJob()) in RouteRecorder.record still replaces the parent job, so cancelling scope does not cancel the recording coroutine and a failure inside it never reaches scope's owner.",
"flags": [
"F3"
],
"reason": "The note asks to leave this exactly as it is for now, since the platform team is moving the recorder onto its own supervised scope next sprint."
}
],
"questions": [
...
],
"summary": ...
}
The other three changes add the awaitClose, Mutex and withLock
imports ((module), no flag), end the callbackFlow with
awaitClose { source.unregister(handle) } (F1), and replace the @Volatile flag
in RouteRecorder.record with a private Mutex held around
store.append (no flag of its own; it is what finish now waits on). The
verdict is partial because the note asked to leave launch(SupervisorJob()) as
it is, so the medium F3 is named in open rather than fixed; every fun keeps its name and
its suspend or not.
The page checks the reply against the code; the API does not
What comes back from the API is the model’s raw text in output.output. The page
holds it to account in the browser before showing it (recon.js
reconcile, with the facts, the code and the handoff it sent): it applies every edit or
returned file to the paste and re-runs its own checks on the result. None of that happens in the API.
A direct API caller gets the reply unchecked and must do the applying and re-checking itself.
- Both lanes: the reply is the lane you asked for (one written for the other task is a disagreement), it carries a valid verdict for that lane, and no credential from the paste is repeated anywhere in it (every credential value the free check found in the paste).
- Audit: the areas table. Exactly four rows, covering structure, threads, cancellation and flows once each, each with a valid status.
- Audit: every finding is anchored. Its
idis in the catalogue (KC-01..KC-17, orother; KC-18 is context and never a finding); itsfileis a pasted file; itslineis inside that file; itsevidenceis found line by line in that file ofcode(indentation trimmed) and on or within three lines ofline; every citeduidis a non-info flag the page raised. A finding that carries a high flag at a lower severity, or a medium or high finding with no edit, is pointed out. - Audit: every flag is answered once, consistently. Every non-info flag answered exactly once and no other; a
confirmedanswer needs a finding carrying that uid; adismissedone must not be carried by a finding and should give a reason. The verdict must fit the worst severity (blockedwith a high finding,needs_workwith a medium, otherwiseready). - Audit: every edit is put in place and re-checked. The findings’ edits are applied in the listed order, each on top of the ones before (
Kotkit.applyEdits): abeforethat is not found, is found more than once, or changes nothing is a disagreement. After each finding the free check runs again: the code must still read and no new high flag may appear. With every edit applied, every confirmed flag a finding with edits carries must be gone, every function of the paste must still exist, and a new medium flag is pointed out. - Fix: the files. At least one file, at most one new one; none empty, none with a
// file:(or# file:) line, every path a.ktor.ktsfile or a.versions.toml; a Kotlin file indented with tabs is pointed out, and one sent back with every line indented is a disagreement. - Fix: put into the paste and re-checked. The files replace the pasted files of the same path (or are added) (
Kotkit.replaceFiles) and the free check runs again: the code must read, no high or medium flag of the paste may remain unless anopenitem names it, no new high or medium flag may appear, every function must still exist, and every fun that is not private must keep itsfunorsuspend fun. - Fix: the changes list matches the code. Every change names a returned file, a function in the fixed code (or
(module),(gradle)or(top level)) and real page flags; a high or medium flag no change and no open item names is pointed out. The verdict must fit theopenlist, and an emptyverify, or one with no test in it, is pointed out.
recon.js also runs in Node, so you can hold a reply to exactly the page’s standard.
Save /kotkit.js and /recon.js next to this script,
pass the file you built the body from (build-check.js or handoff.js output),
the reply text and the lane; it prints every statement, the count, and the paste with every edit (or
file) put back:
// recheck.js -- run: node recheck.js orders-check.json orders-reply.txt audit > orders-edited.txt (kotkit.js, recon.js next to it)
const fs = require("fs");
const Recon = require("./recon.js"); // loads ./kotkit.js itself
const sent = JSON.parse(fs.readFileSync(process.argv[2], "utf8")); // code, facts (and audit_handoff) you sent
const lane = process.argv[4] || "audit";
const res = Recon.normalize(Recon.parseResult(fs.readFileSync(process.argv[3], "utf8")), lane); // output.output
const r = Recon.reconcile(res, { facts: sent.facts, code: sent.code, handoff: sent.audit_handoff || null });
r.notes.forEach((n) => console.error(n.kind + ": " + n.text));
console.error(r.disagreements + " disagreement(s) in " + r.statements + " statements checked");
if (r.after) (r.after.flags || []).forEach((f) => console.error("still raised: " + f.id + " " + f.sev + " " + f.file + ":" + f.line + " " + f.text));
if (r.after && r.after.text) console.log(r.after.text.replace(/\n$/, "")); // the paste with every edit (or file) put back
For the orders audit reply it reports:
ok: The areas table rates structure, threads, cancellation and flows once each.
ok: Every one of the 6 findings names a pasted file, a line inside it and evidence copied from it.
ok: The verdict (blocked) fits the worst finding.
ok: Every one of the page's 6 flags is answered once.
ok: All 14 edits applied in order, each before text found exactly once.
ok: Every confirmed flag a finding carries is gone once all edits are applied.
ok: Every function of the paste is still there after the edits.
ok: The reply repeats no credential from the paste.
0 disagreement(s) in 8 statements checked
and for the tracker fix reply (node recheck.js tracker-check.json tracker-reply.txt fix):
ok: 1 file returned whole.
ok: With the fixed files in place the free check raises no high or medium flag beyond the ones left open (0 low).
info: F3 left open by the reply.
ok: Every one of the 7 functions is still there.
ok: Every one of the 4 changes names a returned file, a function in the fixed code and real page flags.
ok: The verdict (partial) fits the open list.
ok: The reply repeats no credential from the paste.
0 disagreement(s) in 6 statements checked
still raised: KC-09 medium app/src/main/java/com/example/tracks/RouteRecorder.kt:39 launch(SupervisorJob()) in RouteRecorder.record cuts the coroutine off from its parent
The page does not compile or run the code. After that, build it (./gradlew build, or the
module’s test task), run the tests the reply’s plan or verify
names under runTest (kotlinx-coroutines-test) with virtual time and a cancellation test
that cancels the scope and asserts the work stops, and on Android watch a run with a profiler and
StrictMode on for work that blocks the main thread, before you ship anything.
Step 1 — a tiny client helper
One function per language that does what sdk.js does for every call: JSON in,
bearer token if you have one, data out on a 2xx, an error carrying
status, error.code and error.message otherwise. The later
steps assume this helper is in the same file (for Go, Java and C#, the later snippets go inside
main / the top-level statements; Java’s makeInput from step 4 goes
in the class).
BASE="https://api.skillsafe.ai/v1/app-api"
TOKEN="${SKILLSAFE_TOKEN:-YOUR_TOKEN}" # step 2
# api METHOD PATH [extra curl args...] -- prints the whole envelope; pipe to jq
api() {
method="$1"; path="$2"; shift 2
curl -sS -X "$method" "$BASE$path" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $TOKEN" "$@"
}
# success: jq '.data' failure: jq '.error | {code, message}'
import hashlib, json, os, time, urllib.parse
import requests # pip install requests
BASE = "https://api.skillsafe.ai/v1/app-api"
TOKEN = os.environ.get("SKILLSAFE_TOKEN", "YOUR_TOKEN") # step 2
class ApiError(Exception):
def __init__(self, status, code, message, details=None):
super().__init__(f"{status} {code}: {message}")
self.status, self.code, self.details = status, code, details
def api(method, path, body=None, headers=None):
h = {"Content-Type": "application/json"}
if TOKEN:
h["Authorization"] = "Bearer " + TOKEN
h.update(headers or {})
r = requests.request(method, BASE + path, headers=h,
data=None if body is None else json.dumps(body))
try:
env = r.json()
except ValueError:
env = {}
if not r.ok:
err = env.get("error") or {}
raise ApiError(r.status_code, err.get("code"), err.get("message") or r.reason, err.get("details"))
return env.get("data")
// coroutine-desk.mjs -- Node 18+ (global fetch), run with: node coroutine-desk.mjs
import { readFileSync } from "node:fs";
import { createHash } from "node:crypto";
const BASE = "https://api.skillsafe.ai/v1/app-api";
let TOKEN = "YOUR_TOKEN"; // step 2
async function api(method, path, body, headers = {}) {
const h = { "Content-Type": "application/json", ...headers };
if (TOKEN) h.Authorization = "Bearer " + TOKEN;
const res = await fetch(BASE + path, {
method,
headers: h,
body: body === undefined ? undefined : JSON.stringify(body),
});
const json = await res.json().catch(() => ({}));
if (!res.ok) {
const err = new Error((json.error && json.error.message) || res.statusText);
err.status = res.status;
err.code = json.error && json.error.code;
err.details = json.error && json.error.details;
throw err;
}
return json.data;
}
package main
import (
"bufio"
"bytes"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"net/http"
"net/url"
"os"
"strings"
"time"
)
const base = "https://api.skillsafe.ai/v1/app-api"
var token = os.Getenv("SKILLSAFE_TOKEN") // step 2
type apiError struct {
Status int
Code string
Message string
}
func (e *apiError) Error() string { return fmt.Sprintf("%d %s: %s", e.Status, e.Code, e.Message) }
type envelope struct {
OK bool `json:"ok"`
Data json.RawMessage `json:"data"`
Error *struct {
Code string `json:"code"`
Message string `json:"message"`
} `json:"error"`
}
func newRequest(method, path string, body any, headers map[string]string) (*http.Request, error) {
var buf []byte
if body != nil {
b, err := json.Marshal(body)
if err != nil {
return nil, err
}
buf = b
}
req, err := http.NewRequest(method, base+path, bytes.NewReader(buf))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
if token != "" {
req.Header.Set("Authorization", "Bearer "+token)
}
for k, v := range headers {
req.Header.Set(k, v)
}
return req, nil
}
func errorFrom(res *http.Response, env envelope) error {
e := &apiError{Status: res.StatusCode, Message: http.StatusText(res.StatusCode)}
if env.Error != nil {
e.Code = env.Error.Code
if env.Error.Message != "" {
e.Message = env.Error.Message
}
}
return e
}
// api sends body as JSON and decodes the envelope's data into out (out may be nil).
func api(method, path string, body any, headers map[string]string, out any) error {
req, err := newRequest(method, path, body, headers)
if err != nil {
return err
}
res, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
var env envelope
_ = json.NewDecoder(res.Body).Decode(&env)
if res.StatusCode < 200 || res.StatusCode > 299 {
return errorFrom(res, env)
}
if out != nil && len(env.Data) > 0 {
return json.Unmarshal(env.Data, out)
}
return nil
}
func main() {
// steps 2-6 go here; remove any import a step you skip no longer uses
_, _, _, _, _ = bufio.NewScanner, sha256.Sum256, hex.EncodeToString, url.PathEscape, strings.Index
_ = time.Second
}
// CoroutineDesk.java -- Java 17+, needs com.fasterxml.jackson.core:jackson-databind on the classpath
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.net.URI;
import java.net.URLEncoder;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.MessageDigest;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class CoroutineDesk {
static final String BASE = "https://api.skillsafe.ai/v1/app-api";
static String token = System.getenv().getOrDefault("SKILLSAFE_TOKEN", "YOUR_TOKEN"); // step 2
static final HttpClient HTTP = HttpClient.newHttpClient();
static final ObjectMapper JSON = new ObjectMapper();
static class ApiException extends Exception {
final int status;
final String code;
ApiException(int status, String code, String message) {
super(status + " " + code + ": " + message);
this.status = status;
this.code = code;
}
}
static HttpRequest.Builder request(String method, String path, Object body, Map<String, String> headers) throws Exception {
HttpRequest.Builder b = HttpRequest.newBuilder(URI.create(BASE + path))
.header("Content-Type", "application/json")
.method(method, body == null ? HttpRequest.BodyPublishers.noBody()
: HttpRequest.BodyPublishers.ofString(JSON.writeValueAsString(body)));
if (token != null && !token.isEmpty()) b.header("Authorization", "Bearer " + token);
if (headers != null) headers.forEach(b::header);
return b;
}
static ApiException errorFrom(int status, JsonNode env) {
JsonNode err = env.path("error");
return new ApiException(status, err.path("code").asText(null), err.path("message").asText("HTTP " + status));
}
static JsonNode api(String method, String path, Object body, Map<String, String> headers) throws Exception {
HttpResponse<String> res = HTTP.send(request(method, path, body, headers).build(), HttpResponse.BodyHandlers.ofString());
JsonNode env;
try { env = JSON.readTree(res.body()); } catch (Exception e) { env = JSON.createObjectNode(); }
if (env == null) env = JSON.createObjectNode();
if (res.statusCode() / 100 != 2) throw errorFrom(res.statusCode(), env);
return env.path("data");
}
public static void main(String[] args) throws Exception {
// steps 2-6 go here (makeInput from step 4 goes next to api())
}
}
require "digest"
require "json"
require "net/http"
require "uri"
BASE = "https://api.skillsafe.ai/v1/app-api"
$token = ENV.fetch("SKILLSAFE_TOKEN", "YOUR_TOKEN") # step 2
class ApiError < StandardError
attr_reader :status, :code
def initialize(status, code, message)
super("#{status} #{code}: #{message}")
@status = status
@code = code
end
end
def build_request(method, path, body = nil, headers = {})
uri = URI(BASE + path)
req = Net::HTTPGenericRequest.new(method, !body.nil?, true, uri.request_uri)
req["Content-Type"] = "application/json"
req["Authorization"] = "Bearer #{$token}" if $token && !$token.empty?
headers.each { |k, v| req[k] = v }
req.body = JSON.generate(body) unless body.nil?
[uri, req]
end
def api(method, path, body = nil, headers = {})
uri, req = build_request(method, path, body, headers)
res = Net::HTTP.start(uri.host, uri.port, use_ssl: true) { |h| h.request(req) }
env = (JSON.parse(res.body) rescue {})
env = {} unless env.is_a?(Hash)
unless res.is_a?(Net::HTTPSuccess)
err = env["error"] || {}
raise ApiError.new(res.code.to_i, err["code"], err["message"] || res.message)
end
env["data"]
end
<?php
// needs the curl extension
const BASE = "https://api.skillsafe.ai/v1/app-api";
$TOKEN = getenv("SKILLSAFE_TOKEN") ?: "YOUR_TOKEN"; // step 2
class ApiError extends Exception {
public $status;
public $apiCode;
public function __construct($status, $code, $message) {
parent::__construct("$status $code: $message");
$this->status = $status;
$this->apiCode = $code;
}
}
function headers_for(array $extra = []): array {
global $TOKEN;
$h = ["Content-Type: application/json"];
if ($TOKEN) $h[] = "Authorization: Bearer $TOKEN";
foreach ($extra as $k => $v) $h[] = "$k: $v";
return $h;
}
function api(string $method, string $path, $body = null, array $headers = []) {
$ch = curl_init(BASE . $path);
curl_setopt_array($ch, [
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_HTTPHEADER => headers_for($headers),
CURLOPT_RETURNTRANSFER => true,
]);
if ($body !== null) curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
$raw = curl_exec($ch);
if ($raw === false) throw new RuntimeException(curl_error($ch));
$status = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
curl_close($ch);
$env = json_decode($raw);
if (!is_object($env)) $env = new stdClass();
if ($status < 200 || $status > 299) {
$e = $env->error ?? new stdClass();
throw new ApiError($status, $e->code ?? null, $e->message ?? "HTTP $status");
}
return $env->data ?? null;
}
// Program.cs -- .NET 6+ console app (implicit usings on) with top-level statements; steps 2-6 (with the MakeInput local function) go ABOVE this class
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
static class Api
{
public const string Base = "https://api.skillsafe.ai/v1/app-api";
public static string Token = Environment.GetEnvironmentVariable("SKILLSAFE_TOKEN") ?? "YOUR_TOKEN"; // step 2
public static readonly HttpClient Http = new HttpClient();
public class ApiException : Exception
{
public int Status;
public string? Code;
public ApiException(int status, string? code, string message) : base($"{status} {code}: {message}")
{
Status = status;
Code = code;
}
}
public static HttpRequestMessage Request(string method, string path, object? body = null, IDictionary<string, string>? headers = null)
{
var req = new HttpRequestMessage(new HttpMethod(method), Base + path);
if (body != null) req.Content = new StringContent(JsonSerializer.Serialize(body), Encoding.UTF8, "application/json");
if (!string.IsNullOrEmpty(Token)) req.Headers.TryAddWithoutValidation("Authorization", "Bearer " + Token);
if (headers != null) foreach (var kv in headers) req.Headers.TryAddWithoutValidation(kv.Key, kv.Value);
return req;
}
public static ApiException ErrorFrom(HttpResponseMessage res, JsonNode? env)
{
var err = env?["error"];
return new ApiException((int)res.StatusCode, err?["code"]?.GetValue<string>(),
err?["message"]?.GetValue<string>() ?? res.ReasonPhrase ?? "error");
}
public static async Task<JsonNode?> Call(string method, string path, object? body = null, IDictionary<string, string>? headers = null)
{
using var req = Request(method, path, body, headers);
using var res = await Http.SendAsync(req);
var text = await res.Content.ReadAsStringAsync();
JsonNode? env = null;
try { env = JsonNode.Parse(text); } catch (JsonException) { }
if (!res.IsSuccessStatusCode) throw ErrorFrom(res, env);
return env?["data"];
}
}
Step 2 — a token
Two kinds work:
-
Personal — needed for a run. Both lanes are metered in credits on
gpt-terra, so a run needs a signed-in (user) token (the page says “The audit run is metered, so it needs an account.”, or the same of the fix run) unless the estimate sayssponsor_enabled. Sign in and copy your token from /tokens.html, then export it asSKILLSAFE_TOKEN(the samples fall back to the placeholderYOUR_TOKEN). -
Guest — enough for
/meand/estimate.POST /guestwith{"slug": "coroutine-desk"}and noAuthorizationheader, exactly assdk.jsguest()does. The response’sdatacarriestokenandguest_id. Reuse a token you already hold rather than minting a new one each time.
# personal token (needed for a run): copy it from /tokens.html, then
# export SKILLSAFE_TOKEN=... and skip the rest of this block
# guest token (enough for /me and /estimate): no Authorization header on this one call
TOKEN=$(curl -sS -X POST "$BASE/guest" \
-H "Content-Type: application/json" \
-d '{"slug":"coroutine-desk"}' | jq -r '.data.token')
echo "guest token: ${TOKEN:0:12}..."
# personal token (needed for a run): set SKILLSAFE_TOKEN (from /tokens.html) and skip this
if TOKEN in ("", "YOUR_TOKEN"):
TOKEN = "" # no Authorization header on /guest
guest = api("POST", "/guest", {"slug": "coroutine-desk"})
TOKEN = guest["token"]
print("guest", guest.get("guest_id"))
// personal token (needed for a run): paste it from /tokens.html into TOKEN and skip this
if (!TOKEN || TOKEN === "YOUR_TOKEN") {
TOKEN = ""; // no Authorization header on /guest
const guest = await api("POST", "/guest", { slug: "coroutine-desk" });
TOKEN = guest.token;
console.log("guest", guest.guest_id);
}
// inside main -- personal token (needed for a run): set SKILLSAFE_TOKEN (from /tokens.html) and skip this
if token == "" {
var guest struct {
Token string `json:"token"`
GuestID string `json:"guest_id"`
}
if err := api("POST", "/guest", map[string]string{"slug": "coroutine-desk"}, nil, &guest); err != nil {
panic(err)
}
token = guest.Token
fmt.Println("guest", guest.GuestID)
}
// inside main -- personal token (needed for a run): set SKILLSAFE_TOKEN (from /tokens.html) and skip this
if (token.isEmpty() || token.equals("YOUR_TOKEN")) {
token = ""; // no Authorization header on /guest
JsonNode guest = api("POST", "/guest", Map.of("slug", "coroutine-desk"), null);
token = guest.path("token").asText();
System.out.println("guest " + guest.path("guest_id").asText());
}
# personal token (needed for a run): set SKILLSAFE_TOKEN (from /tokens.html) and skip this
if $token.empty? || $token == "YOUR_TOKEN"
$token = "" # no Authorization header on /guest
guest = api("POST", "/guest", { slug: "coroutine-desk" })
$token = guest["token"]
puts "guest #{guest["guest_id"]}"
end
// personal token (needed for a run): set SKILLSAFE_TOKEN (from /tokens.html) and skip this
if ($TOKEN === "" || $TOKEN === "YOUR_TOKEN") {
$TOKEN = ""; // no Authorization header on /guest
$guest = api("POST", "/guest", ["slug" => "coroutine-desk"]);
$TOKEN = $guest->token;
echo "guest {$guest->guest_id}\n";
}
// top-level statements (above the Api class) -- personal token (needed for a run): set SKILLSAFE_TOKEN (from /tokens.html) and skip this
if (Api.Token == "" || Api.Token == "YOUR_TOKEN")
{
Api.Token = ""; // no Authorization header on /guest
var guest = await Api.Call("POST", "/guest", new { slug = "coroutine-desk" });
Api.Token = guest!["token"]!.GetValue<string>();
Console.WriteLine($"guest {guest["guest_id"]}");
}
Step 3 — GET /me
Who the token belongs to and what it can spend. sdk.js documents the shape as
{subject_type, subject_id, credits, profile?}. The page treats the caller as signed
in only when subject_type is "user", shows credits as the
balance (1 credit = $0.0001, so it divides by 10 000 for the dollar figure), and compares it
with the estimate in step 4 before it lets you run.
api GET /me | jq '.data // .error'
me = api("GET", "/me")
print(me["subject_type"], me.get("subject_id"), me.get("credits"))
signed_in = me["subject_type"] == "user"
const me = await api("GET", "/me");
console.log(me.subject_type, me.subject_id, me.credits);
const signedIn = me.subject_type === "user";
var me struct {
SubjectType string `json:"subject_type"`
SubjectID any `json:"subject_id"`
Credits any `json:"credits"`
}
if err := api("GET", "/me", nil, nil, &me); err != nil {
panic(err)
}
fmt.Println(me.SubjectType, me.SubjectID, me.Credits)
signedIn := me.SubjectType == "user"
_ = signedIn
JsonNode me = api("GET", "/me", null, null);
System.out.println(me.path("subject_type").asText() + " " + me.path("subject_id").asText() + " " + me.path("credits"));
boolean signedIn = me.path("subject_type").asText().equals("user");
me = api("GET", "/me")
puts [me["subject_type"], me["subject_id"], me["credits"]].join(" ")
signed_in = me["subject_type"] == "user"
$me = api("GET", "/me");
echo $me->subject_type, " ", $me->subject_id ?? "", " ", $me->credits ?? "", "\n";
$signedIn = $me->subject_type === "user";
var me = await Api.Call("GET", "/me");
Console.WriteLine($"{me?["subject_type"]} {me?["subject_id"]} {me?["credits"]}");
var signedIn = me?["subject_type"]?.GetValue<string>() == "user";
Step 4 — build the input, then POST /estimate
First a small makeInput that does what buildInput does: task,
then code as the paste the page would send, facts as a compact JSON string,
the note when it is not blank, and — for the fix lane only — the
audit_handoff string when the file carries one. It reads the file
build-check.js (or handoff.js) printed. The samples build the
audit input of the orders example from orders-check.json; step 6 reuses
makeInput for the tracker (fix) example. Then price it. The body of
/estimate is the same input you will send to /run; no job is created and
nothing is charged — /estimate is free. From the response the page reads:
| Field | Meaning |
|---|---|
hold_credits | A reservation, not a price: the worst case the platform holds while the run is in flight. The page shows it as “reserves N credits ($...) for the audit run” and adds that you are charged only for what the run actually uses, usually far less. What you pay is the job’s charged_credits after settlement (step 5). A balance below the hold is allowed, but the page warns that the reply may be cut short (truncated: true). |
min_credits | The smallest balance that can start a run. Below it the page disables the run button and names the shortfall, because the call would be a 402. |
sponsor_enabled | When true, a caller who is not signed in may run; otherwise the page asks them to sign in. |
warnings | Input warnings. They do not refuse the call; the page shows them next to the price (“input warnings: ...”), so check them before you run. |
The page re-prices whenever the paste, the note or the lane change. The amounts grow with the size
of code and facts, so read them from the call rather than hard-coding them.
# orders-check.json: printed by build-check.js (see "The facts string").
# (.facts | tojson) turns the facts into ONE compact string, as JSON.stringify does.
BODY=$(jq -c --arg note "Polling keeps hitting the orders API after the screen is closed, and the order list freezes the UI now and then. What do we fix first? Changing OrderRepository's constructor is fine - the ViewModel factory is its only caller." \
'{task: "audit", code, facts: (.facts | tojson), note: $note}' orders-check.json)
# No page check at hand? facts: "{}" is accepted as a string, but the reply then has no page flags to answer.
# Fix lane after an audit: read handoff.js's output instead and add its string:
# '{task: "fix", code, facts: (.facts | tojson), note: $note} + (if .audit_handoff then {audit_handoff} else {} end)'
api POST /estimate -d "$BODY" \
| jq '.error // (.data | {hold_credits, min_credits, sponsor_enabled, warnings})'
def make_input(task, check_path, note=None):
"""The body the page sends: task, code, facts as a JSON STRING, an optional note, and
(fix lane only) the audit_handoff string -- every value a string."""
with open(check_path, encoding="utf-8") as f:
check = json.load(f) # build-check.js (or handoff.js) output
inp = {
"task": task,
"code": check["code"], # the paste as the page sends it
"facts": json.dumps(check["facts"], separators=(",", ":"), ensure_ascii=False), # a STRING, like JSON.stringify
}
n = (note or "").strip()
if n:
inp["note"] = n[:2000] # optional; the page caps it at 2000 chars
if task == "fix" and check.get("audit_handoff"):
inp["audit_handoff"] = check["audit_handoff"] # optional; already a JSON string
return inp
inp = make_input("audit", "orders-check.json",
"Polling keeps hitting the orders API after the screen is closed, and the order list freezes the UI now and then. What do we fix first? Changing OrderRepository's constructor is fine - the ViewModel factory is its only caller.")
est = api("POST", "/estimate", inp)
print("hold", est.get("hold_credits"), "min", est.get("min_credits"),
"sponsored", est.get("sponsor_enabled"), "warnings", est.get("warnings") or [])
// The body the page sends: task, code, facts as a JSON STRING, an optional note,
// and (fix lane only) the audit_handoff string -- every value a string.
function makeInput(task, checkPath, note) {
const check = JSON.parse(readFileSync(checkPath, "utf8")); // build-check.js (or handoff.js) output
const input = {
task,
code: check.code, // the paste as the page sends it
facts: JSON.stringify(check.facts), // a STRING: the page's free check
};
const n = (note || "").trim();
if (n) input.note = n.length > 2000 ? n.slice(0, 2000) : n; // optional; max 2000 chars
if (task === "fix" && check.audit_handoff) input.audit_handoff = check.audit_handoff; // optional
return input;
}
const input = makeInput("audit", "orders-check.json",
"Polling keeps hitting the orders API after the screen is closed, and the order list freezes the UI now and then. What do we fix first? Changing OrderRepository's constructor is fine - the ViewModel factory is its only caller.");
const est = await api("POST", "/estimate", input);
console.log("hold", est.hold_credits, "min", est.min_credits,
"sponsored", est.sponsor_enabled, "warnings", est.warnings || []);
// makeInput builds the body the page sends: task, code, facts as a JSON STRING,
// an optional note and (fix lane only) the audit_handoff string.
makeInput := func(task, checkPath, note string) map[string]any {
raw, err := os.ReadFile(checkPath) // build-check.js (or handoff.js) output
if err != nil {
panic(err)
}
var check struct {
Code string `json:"code"`
Facts json.RawMessage `json:"facts"`
AuditHandoff string `json:"audit_handoff"`
}
if err := json.Unmarshal(raw, &check); err != nil {
panic(err)
}
var facts bytes.Buffer
if err := json.Compact(&facts, check.Facts); err != nil { // keeps key order; travels as a STRING
panic(err)
}
in := map[string]any{"task": task, "code": check.Code, "facts": facts.String()}
if n := []rune(strings.TrimSpace(note)); len(n) > 0 {
if len(n) > 2000 {
n = n[:2000] // optional; the page caps it at 2000 chars
}
in["note"] = string(n)
}
if task == "fix" && check.AuditHandoff != "" {
in["audit_handoff"] = check.AuditHandoff // optional; already a JSON string
}
return in
}
input := makeInput("audit", "orders-check.json",
"Polling keeps hitting the orders API after the screen is closed, and the order list freezes the UI now and then. What do we fix first? Changing OrderRepository's constructor is fine - the ViewModel factory is its only caller.")
var est struct {
HoldCredits any `json:"hold_credits"`
MinCredits any `json:"min_credits"`
SponsorEnabled bool `json:"sponsor_enabled"`
Warnings []any `json:"warnings"`
}
if err := api("POST", "/estimate", input, nil, &est); err != nil {
panic(err)
}
fmt.Printf("hold %v min %v sponsored %v warnings %v\n", est.HoldCredits, est.MinCredits, est.SponsorEnabled, est.Warnings)
// ---- add to the CoroutineDesk class, next to api(): the body the page sends
static Map<String, Object> makeInput(String task, String checkPath, String note) throws Exception {
JsonNode check = JSON.readTree(Files.readString(Path.of(checkPath))); // build-check.js (or handoff.js) output
Map<String, Object> in = new LinkedHashMap<>();
in.put("task", task);
in.put("code", check.path("code").asText()); // the paste as the page sends it
in.put("facts", JSON.writeValueAsString(check.path("facts"))); // a STRING, compact
String n = note == null ? "" : note.strip();
if (!n.isEmpty()) in.put("note", n.length() > 2000 ? n.substring(0, 2000) : n); // optional; max 2000 chars
if (task.equals("fix") && check.hasNonNull("audit_handoff")) in.put("audit_handoff", check.get("audit_handoff").asText());
return in;
}
// ---- inside main
Map<String, Object> input = makeInput("audit", "orders-check.json",
"Polling keeps hitting the orders API after the screen is closed, and the order list freezes the UI now and then. What do we fix first? Changing OrderRepository's constructor is fine - the ViewModel factory is its only caller.");
JsonNode est = api("POST", "/estimate", input, null);
System.out.println("hold " + est.path("hold_credits") + " min " + est.path("min_credits")
+ " sponsored " + est.path("sponsor_enabled").asBoolean(false) + " warnings " + est.path("warnings"));
# The body the page sends: task, code, facts as a JSON STRING, an optional note,
# and (fix lane only) the audit_handoff string -- every value a string.
def make_input(task, check_path, note = nil)
check = JSON.parse(File.read(check_path, encoding: "UTF-8")) # build-check.js (or handoff.js) output
input = {
task: task,
code: check["code"], # the paste as the page sends it
facts: JSON.generate(check["facts"]) # a STRING, compact
}
n = note.to_s.strip
input[:note] = n[0, 2000] unless n.empty? # optional; max 2000 chars
input[:audit_handoff] = check["audit_handoff"] if task == "fix" && !check["audit_handoff"].to_s.empty?
input
end
input = make_input("audit", "orders-check.json",
"Polling keeps hitting the orders API after the screen is closed, and the order list freezes the UI now and then. What do we fix first? Changing OrderRepository's constructor is fine - the ViewModel factory is its only caller.")
est = api("POST", "/estimate", input)
puts "hold #{est["hold_credits"]} min #{est["min_credits"]} " \
"sponsored #{est["sponsor_enabled"]} warnings #{(est["warnings"] || []).inspect}"
// The body the page sends: task, code, facts as a JSON STRING, an optional note,
// and (fix lane only) the audit_handoff string -- every value a string.
function make_input(string $task, string $checkPath, string $note = ""): array {
// decode to objects (not arrays) so an empty {} stays {} when re-encoded
$check = json_decode(file_get_contents($checkPath)); // build-check.js (or handoff.js) output
$input = [
"task" => $task,
"code" => $check->code, // the paste as the page sends it
"facts" => json_encode($check->facts, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), // a STRING
];
$n = trim($note);
if ($n !== "") $input["note"] = mb_substr($n, 0, 2000); // optional; max 2000 chars
if ($task === "fix" && !empty($check->audit_handoff)) $input["audit_handoff"] = $check->audit_handoff;
return $input;
}
$input = make_input("audit", "orders-check.json",
"Polling keeps hitting the orders API after the screen is closed, and the order list freezes the UI now and then. What do we fix first? Changing OrderRepository's constructor is fine - the ViewModel factory is its only caller.");
$est = api("POST", "/estimate", $input);
echo "hold ", $est->hold_credits ?? "-", " min ", $est->min_credits ?? "-",
" sponsored ", json_encode($est->sponsor_enabled ?? false),
" warnings ", json_encode($est->warnings ?? []), "\n";
// The body the page sends: task, code, facts as a JSON STRING, an optional note,
// and (fix lane only) the audit_handoff string -- every value a string.
static Dictionary<string, string> MakeInput(string task, string checkPath, string? note = null)
{
var compact = new JsonSerializerOptions { Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping };
var check = JsonNode.Parse(File.ReadAllText(checkPath))!; // build-check.js (or handoff.js) output
var input = new Dictionary<string, string>
{
["task"] = task,
["code"] = check["code"]!.GetValue<string>(), // the paste as the page sends it
["facts"] = check["facts"]!.ToJsonString(compact), // a STRING, compact
};
var n = (note ?? "").Trim();
if (n.Length > 0) input["note"] = n.Length > 2000 ? n[..2000] : n; // optional; max 2000 chars
var handoff = check["audit_handoff"]?.GetValue<string>();
if (task == "fix" && !string.IsNullOrEmpty(handoff)) input["audit_handoff"] = handoff;
return input;
}
var input = MakeInput("audit", "orders-check.json",
"Polling keeps hitting the orders API after the screen is closed, and the order list freezes the UI now and then. What do we fix first? Changing OrderRepository's constructor is fine - the ViewModel factory is its only caller.");
var est = await Api.Call("POST", "/estimate", input);
Console.WriteLine($"hold {est?["hold_credits"]} min {est?["min_credits"]} " +
$"sponsored {est?["sponsor_enabled"]} warnings {est?["warnings"]?.ToJsonString() ?? "[]"}");
Step 5 — POST /run, then poll the job (audit)
Send the same input to /run with an Idempotency-Key header, using a
personal token. Its data carries a job_id. Poll
GET /jobs/{job_id} the way sdk.js waitForJob does: every
second until status is succeeded or failed, giving up on
the client side after 180 seconds. On the finished job, read:
output.output— the model’s reply as a string: the JSON object from the output contract of the lane you asked for. Parse it yourself (from the first{to the last}). The samples print the verdict and headline, each area’s status, each finding’sid,file,line,severityand number of edits, then each flag answer. Keep the text:recheck.jsreads it.truncated—truewhen the reply was cut short because the balance ran out. The page then closes the cut-off JSON, shows what arrived as a partial result (“N of 9 sections recovered” for an audit, of 8 for a fix run), and says it was cut short by the available balance.charged_credits— the real cost of the run, as opposed to the estimate’shold_creditsreservation. The page re-reads/meafterwards for the new balance.error— on afailedjob.
Idempotency key. Resending a request with the same key must not start a second
paid run, so derive the key from the input, not from the clock — and include the lane, so an
audit and a fix run of the same paste never share a key. The page uses
coroutine-desk:<lane>:<inputHash>:a<attempt> (app.js
run), where <lane> is audit or fix,
<inputHash> is a 32-bit djb2 hash of JSON.stringify(input) in hex, a
dash, and the string’s length in hex (app.js inputHash, over the
input without retry_note), and <attempt> is 1
for a normal run and 2 for the automatic reformat retry (which also adds
retry_note). For the two worked examples that is
coroutine-desk:audit:b94f0c30-320c:a1 (orders audit) and coroutine-desk:fix:e99d934e-1968:a1
(tracker fix); the fix run of the orders audit handed on (the example key orders:fix, above) is coroutine-desk:fix:f7f6c1dc-3319:a1. The format is the page’s
convention, not a requirement: any stable, unique string does. The samples below use the lane and a
SHA-256 prefix.
The page itself uses /run-stream (step 6), not /run; both take the
same body and header.
KEY="coroutine-desk:audit:$(printf '%s' "$BODY" | shasum -a 256 | cut -c1-16):a1"
JOB=$(api POST /run -H "Idempotency-Key: $KEY" -d "$BODY" | jq -r '.data.job_id')
for i in $(seq 180); do # 1 s interval, 180 s client-side limit
J=$(api GET "/jobs/$JOB")
S=$(printf '%s' "$J" | jq -r '.data.status')
if [ "$S" = "succeeded" ] || [ "$S" = "failed" ]; then break; fi
sleep 1
done
printf '%s' "$J" | jq '.data | {status, truncated, charged_credits, error}'
printf '%s' "$J" | jq -r '.data.output.output' \
| jq '{verdict, headline, areas: [(.areas // [])[] | {area, status}],
findings: [(.findings // [])[] | {id, file, line, severity, edits: ((.edits // []) | length)}],
flag_answers: [(.flag_answers // [])[] | {flag, status}]}'
body = json.dumps(inp)
key = "coroutine-desk:audit:" + hashlib.sha256(body.encode()).hexdigest()[:16] + ":a1"
job_id = api("POST", "/run", inp, {"Idempotency-Key": key})["job_id"]
deadline = time.time() + 180 # 1 s interval, 180 s client-side limit
while True:
job = api("GET", "/jobs/" + urllib.parse.quote(job_id, safe=""))
if job["status"] in ("succeeded", "failed"):
break
if time.time() > deadline:
raise TimeoutError("job timed out")
time.sleep(1)
if job["status"] == "failed":
raise SystemExit(f"run failed: {job.get('error')}")
print("truncated:", job.get("truncated") is True, "charged:", job.get("charged_credits"))
text = (job.get("output") or {}).get("output") or ""
reply = json.loads(text[text.index("{"): text.rindex("}") + 1])
print(reply["verdict"], "-", reply["headline"])
for s in reply.get("areas", []):
print("area", s["area"], s["status"])
for f in reply.get("findings", []):
print(f["id"], f["file"], f.get("line"), f["severity"], len(f.get("edits") or []), "edit(s)")
for a in reply.get("flag_answers", []):
print("answer", a["flag"], a["status"])
const key = "coroutine-desk:audit:" +
createHash("sha256").update(JSON.stringify(input)).digest("hex").slice(0, 16) + ":a1";
const { job_id } = await api("POST", "/run", input, { "Idempotency-Key": key });
let job;
const deadline = Date.now() + 180000; // 1 s interval, 180 s client-side limit
for (;;) {
job = await api("GET", "/jobs/" + encodeURIComponent(job_id));
if (job.status === "succeeded" || job.status === "failed") break;
if (Date.now() > deadline) throw new Error("job timed out");
await new Promise((r) => setTimeout(r, 1000));
}
if (job.status === "failed") throw new Error("run failed: " + JSON.stringify(job.error));
console.log("truncated:", job.truncated === true, "charged:", job.charged_credits);
const text = (job.output && job.output.output) || "";
const reply = JSON.parse(text.slice(text.indexOf("{"), text.lastIndexOf("}") + 1));
console.log(reply.verdict, "-", reply.headline);
for (const s of reply.areas || []) console.log("area", s.area, s.status);
for (const f of reply.findings || []) console.log(f.id, f.file, f.line, f.severity, (f.edits || []).length, "edit(s)");
for (const a of reply.flag_answers || []) console.log("answer", a.flag, a.status);
inputJSON, _ := json.Marshal(input)
sum := sha256.Sum256(inputJSON)
key := "coroutine-desk:audit:" + hex.EncodeToString(sum[:8]) + ":a1"
var started struct {
JobID string `json:"job_id"`
}
if err := api("POST", "/run", input, map[string]string{"Idempotency-Key": key}, &started); err != nil {
panic(err)
}
type jobT struct {
Status string `json:"status"`
Output *struct {
Output string `json:"output"`
} `json:"output"`
Truncated bool `json:"truncated"`
ChargedCredits any `json:"charged_credits"`
Error any `json:"error"`
}
var job jobT
deadline := time.Now().Add(180 * time.Second) // 1 s interval, 180 s client-side limit
for {
job = jobT{}
if err := api("GET", "/jobs/"+url.PathEscape(started.JobID), nil, nil, &job); err != nil {
panic(err)
}
if job.Status == "succeeded" || job.Status == "failed" {
break
}
if time.Now().After(deadline) {
panic("job timed out")
}
time.Sleep(time.Second)
}
if job.Status == "failed" {
panic(fmt.Sprintf("run failed: %v", job.Error))
}
fmt.Println("truncated:", job.Truncated, "charged:", job.ChargedCredits)
text := ""
if job.Output != nil {
text = job.Output.Output
}
var reply map[string]any
if a, b := strings.Index(text, "{"), strings.LastIndex(text, "}"); a >= 0 && b > a {
if err := json.Unmarshal([]byte(text[a:b+1]), &reply); err != nil {
panic(err)
}
}
fmt.Println(reply["verdict"], "-", reply["headline"])
areas, _ := reply["areas"].([]any)
for _, e := range areas {
s, _ := e.(map[string]any)
fmt.Println("area", s["area"], s["status"])
}
findings, _ := reply["findings"].([]any)
for _, e := range findings {
f, _ := e.(map[string]any)
edits, _ := f["edits"].([]any)
fmt.Println(f["id"], f["file"], f["line"], f["severity"], len(edits), "edit(s)")
}
answers, _ := reply["flag_answers"].([]any)
for _, e := range answers {
a, _ := e.(map[string]any)
fmt.Println("answer", a["flag"], a["status"])
}
byte[] digest = MessageDigest.getInstance("SHA-256").digest(JSON.writeValueAsBytes(input));
String key = "coroutine-desk:audit:" + HexFormat.of().formatHex(digest, 0, 8) + ":a1";
String jobId = api("POST", "/run", input, Map.of("Idempotency-Key", key)).path("job_id").asText();
JsonNode job;
long deadline = System.currentTimeMillis() + 180_000; // 1 s interval, 180 s client-side limit
while (true) {
job = api("GET", "/jobs/" + URLEncoder.encode(jobId, StandardCharsets.UTF_8), null, null);
String s = job.path("status").asText();
if (s.equals("succeeded") || s.equals("failed")) break;
if (System.currentTimeMillis() > deadline) throw new RuntimeException("job timed out");
Thread.sleep(1000);
}
if (job.path("status").asText().equals("failed")) throw new RuntimeException("run failed: " + job.path("error"));
System.out.println("truncated: " + job.path("truncated").asBoolean(false) + ", charged: " + job.path("charged_credits"));
String text = job.path("output").path("output").asText("");
JsonNode reply = JSON.readTree(text.substring(text.indexOf('{'), text.lastIndexOf('}') + 1));
System.out.println(reply.path("verdict").asText() + " - " + reply.path("headline").asText());
for (JsonNode s : reply.path("areas")) {
System.out.println("area " + s.path("area").asText() + " " + s.path("status").asText());
}
for (JsonNode f : reply.path("findings")) {
System.out.println(f.path("id").asText() + " " + f.path("file").asText() + " " + f.path("line") + " "
+ f.path("severity").asText() + " " + f.path("edits").size() + " edit(s)");
}
for (JsonNode a : reply.path("flag_answers")) {
System.out.println("answer " + a.path("flag").asText() + " " + a.path("status").asText());
}
key = "coroutine-desk:audit:#{Digest::SHA256.hexdigest(JSON.generate(input))[0, 16]}:a1"
job_id = api("POST", "/run", input, { "Idempotency-Key" => key })["job_id"]
deadline = Time.now + 180 # 1 s interval, 180 s client-side limit
job = nil
loop do
job = api("GET", "/jobs/#{URI.encode_www_form_component(job_id)}")
break if %w[succeeded failed].include?(job["status"])
raise "job timed out" if Time.now > deadline
sleep 1
end
raise "run failed: #{job["error"].inspect}" if job["status"] == "failed"
puts "truncated: #{job["truncated"] == true}, charged: #{job["charged_credits"]}"
text = (job["output"] || {})["output"].to_s
reply = JSON.parse(text[text.index("{")..text.rindex("}")])
puts "#{reply["verdict"]} - #{reply["headline"]}"
(reply["areas"] || []).each { |s| puts "area #{s["area"]} #{s["status"]}" }
(reply["findings"] || []).each { |f| puts "#{f["id"]} #{f["file"]} #{f["line"].inspect} #{f["severity"]} #{(f["edits"] || []).size} edit(s)" }
(reply["flag_answers"] || []).each { |a| puts "answer #{a["flag"]} #{a["status"]}" }
$key = "coroutine-desk:audit:" . substr(hash("sha256", json_encode($input)), 0, 16) . ":a1";
$jobId = api("POST", "/run", $input, ["Idempotency-Key" => $key])->job_id;
$deadline = time() + 180; // 1 s interval, 180 s client-side limit
while (true) {
$job = api("GET", "/jobs/" . rawurlencode($jobId));
if (in_array($job->status, ["succeeded", "failed"], true)) break;
if (time() > $deadline) throw new RuntimeException("job timed out");
sleep(1);
}
if ($job->status === "failed") throw new RuntimeException("run failed: " . json_encode($job->error ?? null));
echo "truncated: ", json_encode(($job->truncated ?? false) === true), ", charged: ", $job->charged_credits ?? "-", "\n";
$text = $job->output->output ?? "";
$reply = json_decode(substr($text, strpos($text, "{"), strrpos($text, "}") - strpos($text, "{") + 1));
echo $reply->verdict, " - ", $reply->headline, "\n";
foreach ($reply->areas ?? [] as $s) echo "area ", $s->area, " ", $s->status, "\n";
foreach ($reply->findings ?? [] as $f) echo $f->id, " ", $f->file, " ", json_encode($f->line ?? null), " ", $f->severity, " ", count($f->edits ?? []), " edit(s)\n";
foreach ($reply->flag_answers ?? [] as $a) echo "answer ", $a->flag, " ", $a->status, "\n";
var digest = System.Security.Cryptography.SHA256.HashData(Encoding.UTF8.GetBytes(JsonSerializer.Serialize(input)));
var key = "coroutine-desk:audit:" + Convert.ToHexString(digest, 0, 8).ToLowerInvariant() + ":a1";
var started = await Api.Call("POST", "/run", input, new Dictionary<string, string> { ["Idempotency-Key"] = key });
var jobId = started!["job_id"]!.GetValue<string>();
JsonNode job;
var deadline = DateTime.UtcNow.AddSeconds(180); // 1 s interval, 180 s client-side limit
while (true)
{
job = (await Api.Call("GET", "/jobs/" + Uri.EscapeDataString(jobId)))!;
var s = job["status"]?.GetValue<string>();
if (s == "succeeded" || s == "failed") break;
if (DateTime.UtcNow > deadline) throw new TimeoutException("job timed out");
await Task.Delay(1000);
}
if (job["status"]?.GetValue<string>() == "failed") throw new Exception($"run failed: {job["error"]?.ToJsonString()}");
var truncated = job["truncated"] is JsonValue t && t.TryGetValue(out bool tb) && tb;
Console.WriteLine($"truncated: {truncated}, charged: {job["charged_credits"]}");
var text = job["output"]?["output"]?.GetValue<string>() ?? "";
var reply = JsonNode.Parse(text[text.IndexOf('{')..(text.LastIndexOf('}') + 1)])!;
Console.WriteLine($"{reply["verdict"]} - {reply["headline"]}");
foreach (var s in reply["areas"]?.AsArray() ?? new JsonArray())
Console.WriteLine($"area {s?["area"]} {s?["status"]}");
foreach (var f in reply["findings"]?.AsArray() ?? new JsonArray())
Console.WriteLine($"{f?["id"]} {f?["file"]} {f?["line"]?.ToJsonString() ?? "null"} {f?["severity"]} {f?["edits"]?.AsArray().Count ?? 0} edit(s)");
foreach (var a in reply["flag_answers"]?.AsArray() ?? new JsonArray())
Console.WriteLine($"answer {a?["flag"]} {a?["status"]}");
Step 6 — POST /run-stream (Server-Sent Events, fix)
The alternative to step 5, and what the page uses: same kind of body, same
Idempotency-Key header, one request. Run one or the other for a given input, not
both. To show the second lane, the samples build the fix input of the
tracker example (with its own key) and stream it; to stream the audit input instead, pass the
step 4 input and key. The response is text/event-stream. sdk.js splits
it into frames on a blank line, takes the event name from an event: line (default
message) and concatenates the data: lines, skips frames with no data or
with data that is not JSON, and handles these events:
| Event | Data | What the SDK does |
|---|---|---|
delta | {text} | Passes text to onDelta. |
job | job info | Passes it to onJob (the page uses it to advance its progress display). |
done | the final payload: {job_id, status, charged_credits, output} | Keeps it as the result. |
pending | a payload | Treated exactly like done. If it carries a job_id and no output, poll that job as in step 5. |
error | {message, code, job_id} | After the stream ends, raises an error with those fields. |
If the response is not text/event-stream, it is an ordinary JSON envelope:
the SDK returns its data (an idempotent replay returns this way) or raises the
error on a non-2xx status. Handle both shapes.
From the final payload the page reads output.output (falling back to the
concatenated delta text), truncated and charged_credits.
Browsers receive progress ticks rather than deltas from this endpoint, so the page relies on the
final payload; if the stream fails part-way, it tries to recover a partial reply from whatever
delta text arrived. The samples print the verdict and headline, each returned file’s path and
size, each change (function, flags and change), each
verification step, each open item and each question. To put the files into the paste and re-check
them the way the page’s “Put every file into the paste” does, keep the reply text
and run recheck.js with the lane fix. To fix code you audited, read handoff.js’s output instead of
tracker-check.json: makeInput then carries its audit_handoff.
# the fix worked example: tracker-check.json from build-check.js
# (for audited code, read handoff.js's output instead; the + (...) adds its audit_handoff)
FBODY=$(jq -c --arg note "Recording crashes as soon as it starts and finish() pins a CPU core. Fix those two. Leave the launch(SupervisorJob()) in record() exactly as it is for now - the platform team moves the recorder onto its own supervised scope next sprint." \
'{task: "fix", code, facts: (.facts | tojson), note: $note} + (if .audit_handoff then {audit_handoff} else {} end)' \
tracker-check.json)
FKEY="coroutine-desk:fix:$(printf '%s' "$FBODY" | shasum -a 256 | cut -c1-16):a1"
# -N turns off buffering; the whole stream lands in $STREAM
STREAM=$(api POST /run-stream -N -H "Idempotency-Key: $FKEY" -d "$FBODY")
# frames look like:
# event: job
# data: {...}
#
# event: done
# data: {"job_id":"...","status":"...","charged_credits":...,"output":{"output":"{\"task\":\"fix\",...}"}}
# the final reply text, from the done (or pending) frame:
REPLY=$(printf '%s\n' "$STREAM" \
| awk '/^event:/{ev=$2} /^data:/ && (ev=="done" || ev=="pending"){sub(/^data: */,""); print}' \
| jq -r '.output.output')
# an idempotent replay answers with a plain JSON envelope instead: REPLY=$(printf '%s' "$STREAM" | jq -r '.data.output.output')
printf '%s' "$REPLY" | jq '{verdict, headline,
files: [(.files // [])[] | {path, characters: (.content | length)}],
changes: [(.changes // [])[] | {function, flags, change}], verify, open, questions}'
# the fix worked example: tracker-check.json from build-check.js
# (for audited code, pass handoff.js's output instead: make_input adds its audit_handoff)
fix_inp = make_input("fix", "tracker-check.json",
"Recording crashes as soon as it starts and finish() pins a CPU core. Fix those two. Leave the launch(SupervisorJob()) in record() exactly as it is for now - the platform team moves the recorder onto its own supervised scope next sprint.")
fix_key = "coroutine-desk:fix:" + hashlib.sha256(json.dumps(fix_inp).encode()).hexdigest()[:16] + ":a1"
h = {"Content-Type": "application/json", "Authorization": "Bearer " + TOKEN, "Idempotency-Key": fix_key}
done, failure, streamed = None, None, ""
with requests.post(BASE + "/run-stream", headers=h, data=json.dumps(fix_inp), stream=True) as r:
if "text/event-stream" not in r.headers.get("content-type", ""):
try:
env = r.json()
except ValueError:
env = {}
if not r.ok:
err = env.get("error") or {}
raise ApiError(r.status_code, err.get("code"), err.get("message") or r.reason)
done = env.get("data")
else:
event, data = "message", ""
for line in r.iter_lines(decode_unicode=True):
if line == "": # a blank line ends a frame
if data:
try:
payload = json.loads(data)
except ValueError:
payload = None
if event == "delta" and payload is not None:
streamed += payload.get("text") or ""
elif event == "job":
print("job", payload)
elif event in ("done", "pending"):
done = payload
elif event == "error":
failure = payload
event, data = "message", ""
elif line.startswith("event:"):
event = line[6:].strip()
elif line.startswith("data:"):
data += line[5:].strip()
if failure:
raise SystemExit(f"{failure.get('code')}: {failure.get('message')} (job {failure.get('job_id')})")
done = done or {}
full = (done.get("output") or {}).get("output") or streamed
print("truncated:", done.get("truncated") is True, "charged:", done.get("charged_credits"))
result = json.loads(full[full.index("{"): full.rindex("}") + 1])
print(result["verdict"], "-", result["headline"])
for f in result.get("files", []):
print("file", f["path"], len(f.get("content") or ""), "characters")
for c in result.get("changes", []):
print("change", c.get("function"), ",".join(c.get("flags") or []) or "-", "-", c.get("change"))
for step in result.get("verify", []):
print("verify", step)
for o in result.get("open", []):
print("open", o["item"], "-", o.get("reason"))
for q in result.get("questions", []):
print("question", q)
// the fix worked example: tracker-check.json from build-check.js
// (for audited code, pass handoff.js's output instead: makeInput adds its audit_handoff)
const fixInput = makeInput("fix", "tracker-check.json",
"Recording crashes as soon as it starts and finish() pins a CPU core. Fix those two. Leave the launch(SupervisorJob()) in record() exactly as it is for now - the platform team moves the recorder onto its own supervised scope next sprint.");
const fixKey = "coroutine-desk:fix:" +
createHash("sha256").update(JSON.stringify(fixInput)).digest("hex").slice(0, 16) + ":a1";
const res = await fetch(BASE + "/run-stream", {
method: "POST",
headers: { "Content-Type": "application/json", Authorization: "Bearer " + TOKEN, "Idempotency-Key": fixKey },
body: JSON.stringify(fixInput),
});
let done = null, failure = null, streamed = "";
if (!(res.headers.get("content-type") || "").includes("text/event-stream")) {
const json = await res.json().catch(() => ({}));
if (!res.ok) throw new Error((json.error && json.error.message) || res.statusText);
done = json.data;
} else {
const reader = res.body.getReader();
const decoder = new TextDecoder();
let buffer = "";
for (;;) {
const chunk = await reader.read();
if (chunk.done) break;
buffer += decoder.decode(chunk.value, { stream: true });
let idx;
while ((idx = buffer.indexOf("\n\n")) >= 0) {
const frame = buffer.slice(0, idx);
buffer = buffer.slice(idx + 2);
let event = "message", dataStr = "";
for (const line of frame.split("\n")) {
if (line.startsWith("event:")) event = line.slice(6).trim();
else if (line.startsWith("data:")) dataStr += line.slice(5).trim();
}
if (!dataStr) continue;
let data;
try { data = JSON.parse(dataStr); } catch { continue; }
if (event === "delta") streamed += data.text || "";
else if (event === "job") console.log("job", data);
else if (event === "done" || event === "pending") done = data;
else if (event === "error") failure = data;
}
}
}
if (failure) throw new Error(`${failure.code}: ${failure.message} (job ${failure.job_id})`);
const full = (done && done.output && done.output.output) || streamed;
console.log("truncated:", !!(done && done.truncated), "charged:", done && done.charged_credits);
const result = JSON.parse(full.slice(full.indexOf("{"), full.lastIndexOf("}") + 1));
console.log(result.verdict, "-", result.headline);
for (const f of result.files || []) console.log("file", f.path, (f.content || "").length, "characters");
for (const c of result.changes || []) console.log("change", c.function, (c.flags || []).join(",") || "-", "-", c.change);
for (const step of result.verify || []) console.log("verify", step);
for (const o of result.open || []) console.log("open", o.item, "-", o.reason);
for (const q of result.questions || []) console.log("question", q);
// the fix worked example: tracker-check.json from build-check.js
// (for audited code, pass handoff.js's output instead: makeInput adds its audit_handoff)
fixInput := makeInput("fix", "tracker-check.json",
"Recording crashes as soon as it starts and finish() pins a CPU core. Fix those two. Leave the launch(SupervisorJob()) in record() exactly as it is for now - the platform team moves the recorder onto its own supervised scope next sprint.")
fixJSON, _ := json.Marshal(fixInput)
fixSum := sha256.Sum256(fixJSON)
fixKey := "coroutine-desk:fix:" + hex.EncodeToString(fixSum[:8]) + ":a1"
sreq, err := newRequest("POST", "/run-stream", fixInput, map[string]string{"Idempotency-Key": fixKey})
if err != nil {
panic(err)
}
sres, err := http.DefaultClient.Do(sreq)
if err != nil {
panic(err)
}
defer sres.Body.Close()
var done, failure map[string]any
var streamed strings.Builder
if !strings.Contains(sres.Header.Get("Content-Type"), "text/event-stream") {
var env envelope
_ = json.NewDecoder(sres.Body).Decode(&env)
if sres.StatusCode < 200 || sres.StatusCode > 299 {
panic(errorFrom(sres, env))
}
_ = json.Unmarshal(env.Data, &done)
} else {
sc := bufio.NewScanner(sres.Body)
sc.Buffer(make([]byte, 64*1024), 16*1024*1024) // the done frame can be long
event, data := "message", ""
for sc.Scan() {
line := sc.Text()
switch {
case line == "": // a blank line ends a frame
if data != "" {
var p map[string]any
if json.Unmarshal([]byte(data), &p) == nil {
switch event {
case "delta":
if t, ok := p["text"].(string); ok {
streamed.WriteString(t)
}
case "job":
fmt.Println("job", p)
case "done", "pending":
done = p
case "error":
failure = p
}
}
}
event, data = "message", ""
case strings.HasPrefix(line, "event:"):
event = strings.TrimSpace(line[6:])
case strings.HasPrefix(line, "data:"):
data += strings.TrimSpace(line[5:])
}
}
}
if failure != nil {
panic(fmt.Sprintf("%v: %v (job %v)", failure["code"], failure["message"], failure["job_id"]))
}
full := streamed.String()
if out, ok := done["output"].(map[string]any); ok {
if s, ok := out["output"].(string); ok && s != "" {
full = s
}
}
fmt.Println("truncated:", done["truncated"] == true, "charged:", done["charged_credits"])
var result map[string]any
if a, b := strings.Index(full, "{"), strings.LastIndex(full, "}"); a >= 0 && b > a {
if err := json.Unmarshal([]byte(full[a:b+1]), &result); err != nil {
panic(err)
}
}
fmt.Println(result["verdict"], "-", result["headline"])
files, _ := result["files"].([]any)
for _, e := range files {
f, _ := e.(map[string]any)
c, _ := f["content"].(string)
fmt.Println("file", f["path"], len([]rune(c)), "characters")
}
changes, _ := result["changes"].([]any)
for _, e := range changes {
c, _ := e.(map[string]any)
var flags []string
if fs, ok := c["flags"].([]any); ok {
for _, fl := range fs {
flags = append(flags, fmt.Sprint(fl))
}
}
fl := strings.Join(flags, ",")
if fl == "" {
fl = "-"
}
fmt.Println("change", c["function"], fl, "-", c["change"])
}
verify, _ := result["verify"].([]any)
for _, st := range verify {
fmt.Println("verify", st)
}
open, _ := result["open"].([]any)
for _, e := range open {
o, _ := e.(map[string]any)
fmt.Println("open", o["item"], "-", o["reason"])
}
questions, _ := result["questions"].([]any)
for _, q := range questions {
fmt.Println("question", q)
}
// the fix worked example: tracker-check.json from build-check.js
// (for audited code, pass handoff.js's output instead: makeInput adds its audit_handoff)
Map<String, Object> fixInput = makeInput("fix", "tracker-check.json",
"Recording crashes as soon as it starts and finish() pins a CPU core. Fix those two. Leave the launch(SupervisorJob()) in record() exactly as it is for now - the platform team moves the recorder onto its own supervised scope next sprint.");
byte[] fixDigest = MessageDigest.getInstance("SHA-256").digest(JSON.writeValueAsBytes(fixInput));
String fixKey = "coroutine-desk:fix:" + HexFormat.of().formatHex(fixDigest, 0, 8) + ":a1";
HttpRequest sreq = request("POST", "/run-stream", fixInput, Map.of("Idempotency-Key", fixKey)).build();
HttpResponse<Stream<String>> sres = HTTP.send(sreq, HttpResponse.BodyHandlers.ofLines());
JsonNode done = null, failure = null;
StringBuilder streamed = new StringBuilder();
if (!sres.headers().firstValue("content-type").orElse("").contains("text/event-stream")) {
JsonNode env = JSON.readTree(sres.body().collect(Collectors.joining("\n")));
if (env == null) env = JSON.createObjectNode();
if (sres.statusCode() / 100 != 2) throw errorFrom(sres.statusCode(), env);
done = env.path("data");
} else {
String event = "message";
StringBuilder data = new StringBuilder();
Iterator<String> it = sres.body().iterator();
while (it.hasNext()) {
String line = it.next();
if (line.isEmpty()) { // a blank line ends a frame
if (data.length() > 0) {
JsonNode p = null;
try { p = JSON.readTree(data.toString()); } catch (Exception ignored) { }
if (p != null) {
switch (event) {
case "delta": streamed.append(p.path("text").asText("")); break;
case "job": System.out.println("job " + p); break;
case "done": case "pending": done = p; break;
case "error": failure = p; break;
default: break;
}
}
}
event = "message";
data.setLength(0);
} else if (line.startsWith("event:")) event = line.substring(6).trim();
else if (line.startsWith("data:")) data.append(line.substring(5).trim());
}
}
if (failure != null) {
throw new RuntimeException(failure.path("code").asText() + ": " + failure.path("message").asText()
+ " (job " + failure.path("job_id").asText() + ")");
}
String full = done == null ? "" : done.path("output").path("output").asText("");
if (full.isEmpty()) full = streamed.toString();
System.out.println("truncated: " + (done != null && done.path("truncated").asBoolean(false))
+ ", charged: " + (done == null ? "-" : done.path("charged_credits").toString()));
JsonNode result = JSON.readTree(full.substring(full.indexOf('{'), full.lastIndexOf('}') + 1));
System.out.println(result.path("verdict").asText() + " - " + result.path("headline").asText());
for (JsonNode f : result.path("files")) {
System.out.println("file " + f.path("path").asText() + " " + f.path("content").asText("").codePointCount(0, f.path("content").asText("").length()) + " characters");
}
for (JsonNode c : result.path("changes")) {
List<String> flags = new ArrayList<>();
c.path("flags").forEach(fl -> flags.add(fl.asText()));
System.out.println("change " + c.path("function").asText() + " " + (flags.isEmpty() ? "-" : String.join(",", flags))
+ " - " + c.path("change").asText());
}
for (JsonNode step : result.path("verify")) System.out.println("verify " + step.asText());
for (JsonNode o : result.path("open")) System.out.println("open " + o.path("item").asText() + " - " + o.path("reason").asText());
for (JsonNode q : result.path("questions")) System.out.println("question " + q.asText());
# the fix worked example: tracker-check.json from build-check.js
# (for audited code, pass handoff.js's output instead: make_input adds its audit_handoff)
fix_input = make_input("fix", "tracker-check.json",
"Recording crashes as soon as it starts and finish() pins a CPU core. Fix those two. Leave the launch(SupervisorJob()) in record() exactly as it is for now - the platform team moves the recorder onto its own supervised scope next sprint.")
fix_key = "coroutine-desk:fix:#{Digest::SHA256.hexdigest(JSON.generate(fix_input))[0, 16]}:a1"
uri, req = build_request("POST", "/run-stream", fix_input, { "Idempotency-Key" => fix_key })
done = failure = nil
streamed = +""
Net::HTTP.start(uri.host, uri.port, use_ssl: true, read_timeout: 300) do |http|
http.request(req) do |res|
unless res["content-type"].to_s.include?("text/event-stream")
env = (JSON.parse(res.body) rescue {})
unless res.is_a?(Net::HTTPSuccess)
raise ApiError.new(res.code.to_i, env.dig("error", "code"), env.dig("error", "message") || res.message)
end
done = env["data"]
next
end
buf = +""
res.read_body do |chunk|
buf << chunk
while (i = buf.index("\n\n"))
frame = buf.slice!(0, i + 2)
event = "message"
data = +""
frame.split("\n").each do |line|
if line.start_with?("event:") then event = line[6..].strip
elsif line.start_with?("data:") then data << line[5..].strip
end
end
next if data.empty?
payload = (JSON.parse(data) rescue nil)
next if payload.nil?
case event
when "delta" then streamed << payload["text"].to_s
when "job" then puts "job #{payload.inspect}"
when "done", "pending" then done = payload
when "error" then failure = payload
end
end
end
end
end
raise "#{failure["code"]}: #{failure["message"]} (job #{failure["job_id"]})" if failure
done ||= {}
full = (done["output"] || {})["output"].to_s
full = streamed if full.empty?
puts "truncated: #{done["truncated"] == true}, charged: #{done["charged_credits"]}"
result = JSON.parse(full[full.index("{")..full.rindex("}")])
puts "#{result["verdict"]} - #{result["headline"]}"
(result["files"] || []).each { |f| puts "file #{f["path"]} #{f["content"].to_s.length} characters" }
(result["changes"] || []).each do |c|
flags = (c["flags"] || []).join(",")
puts "change #{c["function"]} #{flags.empty? ? "-" : flags} - #{c["change"]}"
end
(result["verify"] || []).each { |step| puts "verify #{step}" }
(result["open"] || []).each { |o| puts "open #{o["item"]} - #{o["reason"]}" }
(result["questions"] || []).each { |q| puts "question #{q}" }
// the fix worked example: tracker-check.json from build-check.js
// (for audited code, pass handoff.js's output instead: make_input adds its audit_handoff)
$fixInput = make_input("fix", "tracker-check.json",
"Recording crashes as soon as it starts and finish() pins a CPU core. Fix those two. Leave the launch(SupervisorJob()) in record() exactly as it is for now - the platform team moves the recorder onto its own supervised scope next sprint.");
$fixKey = "coroutine-desk:fix:" . substr(hash("sha256", json_encode($fixInput)), 0, 16) . ":a1";
$done = null; $failure = null; $streamed = ""; $buf = ""; $ctype = "";
$handle = function (string $event, string $data) use (&$done, &$failure, &$streamed) {
$p = json_decode($data);
if ($p === null) return; // not JSON: skipped, as sdk.js does
switch ($event) {
case "delta": $streamed .= $p->text ?? ""; break;
case "job": echo "job ", json_encode($p), "\n"; break;
case "done": case "pending": $done = $p; break;
case "error": $failure = $p; break;
}
};
$ch = curl_init(BASE . "/run-stream");
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => headers_for(["Idempotency-Key" => $fixKey]),
CURLOPT_POSTFIELDS => json_encode($fixInput, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE),
CURLOPT_HEADERFUNCTION => function ($ch, $line) use (&$ctype) {
if (stripos($line, "content-type:") === 0) $ctype = strtolower(trim(substr($line, 13)));
return strlen($line);
},
CURLOPT_WRITEFUNCTION => function ($ch, $chunk) use (&$buf, &$ctype, $handle) {
$buf .= $chunk; // collects the response in memory
if (strpos($ctype, "text/event-stream") === false) return strlen($chunk); // plain JSON: parse at the end
while (($i = strpos($buf, "\n\n")) !== false) {
$frame = substr($buf, 0, $i);
$buf = substr($buf, $i + 2);
$event = "message"; $data = "";
foreach (explode("\n", $frame) as $line) {
if (strpos($line, "event:") === 0) $event = trim(substr($line, 6));
elseif (strpos($line, "data:") === 0) $data .= trim(substr($line, 5));
}
if ($data !== "") $handle($event, $data);
}
return strlen($chunk);
},
]);
if (curl_exec($ch) === false) throw new RuntimeException(curl_error($ch));
$status = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
curl_close($ch);
if (strpos($ctype, "text/event-stream") === false) {
$env = json_decode($buf) ?? new stdClass();
if ($status < 200 || $status > 299) {
throw new ApiError($status, $env->error->code ?? null, $env->error->message ?? "HTTP $status");
}
$done = $env->data ?? null;
}
if ($failure) throw new RuntimeException(($failure->code ?? "") . ": " . ($failure->message ?? "") . " (job " . ($failure->job_id ?? "") . ")");
$full = $done->output->output ?? "";
if ($full === "") $full = $streamed;
echo "truncated: ", json_encode(($done->truncated ?? false) === true), ", charged: ", $done->charged_credits ?? "-", "\n";
$result = json_decode(substr($full, strpos($full, "{"), strrpos($full, "}") - strpos($full, "{") + 1));
echo $result->verdict, " - ", $result->headline, "\n";
foreach ($result->files ?? [] as $f) echo "file ", $f->path, " ", mb_strlen($f->content ?? ""), " characters\n";
foreach ($result->changes ?? [] as $c) {
$flags = implode(",", $c->flags ?? []);
echo "change ", $c->function ?? "", " ", $flags === "" ? "-" : $flags, " - ", $c->change ?? "", "\n";
}
foreach ($result->verify ?? [] as $step) echo "verify ", $step, "\n";
foreach ($result->open ?? [] as $o) echo "open ", $o->item, " - ", $o->reason ?? "", "\n";
foreach ($result->questions ?? [] as $q) echo "question ", $q, "\n";
// the fix worked example: tracker-check.json from build-check.js
// (for audited code, pass handoff.js's output instead: MakeInput adds its audit_handoff)
var fixInput = MakeInput("fix", "tracker-check.json",
"Recording crashes as soon as it starts and finish() pins a CPU core. Fix those two. Leave the launch(SupervisorJob()) in record() exactly as it is for now - the platform team moves the recorder onto its own supervised scope next sprint.");
var fixDigest = System.Security.Cryptography.SHA256.HashData(Encoding.UTF8.GetBytes(JsonSerializer.Serialize(fixInput)));
var fixKey = "coroutine-desk:fix:" + Convert.ToHexString(fixDigest, 0, 8).ToLowerInvariant() + ":a1";
using var sreq = Api.Request("POST", "/run-stream", fixInput, new Dictionary<string, string> { ["Idempotency-Key"] = fixKey });
using var sres = await Api.Http.SendAsync(sreq, HttpCompletionOption.ResponseHeadersRead);
JsonNode? done = null, failure = null;
var streamed = new StringBuilder();
if (sres.Content.Headers.ContentType?.MediaType != "text/event-stream")
{
JsonNode? env = null;
try { env = JsonNode.Parse(await sres.Content.ReadAsStringAsync()); } catch (JsonException) { }
if (!sres.IsSuccessStatusCode) throw Api.ErrorFrom(sres, env);
done = env?["data"];
}
else
{
using var reader = new StreamReader(await sres.Content.ReadAsStreamAsync());
var ev = "message";
var data = new StringBuilder();
string? line;
while ((line = await reader.ReadLineAsync()) != null)
{
if (line.Length == 0) // a blank line ends a frame
{
if (data.Length > 0)
{
JsonNode? p = null;
try { p = JsonNode.Parse(data.ToString()); } catch (JsonException) { }
if (p != null)
{
switch (ev)
{
case "delta": streamed.Append(p["text"]?.GetValue<string>() ?? ""); break;
case "job": Console.WriteLine($"job {p.ToJsonString()}"); break;
case "done": case "pending": done = p; break;
case "error": failure = p; break;
}
}
}
ev = "message";
data.Clear();
}
else if (line.StartsWith("event:")) ev = line[6..].Trim();
else if (line.StartsWith("data:")) data.Append(line[5..].Trim());
}
}
if (failure != null) throw new Exception($"{failure["code"]}: {failure["message"]} (job {failure["job_id"]})");
var full = done?["output"]?["output"]?.GetValue<string>();
if (string.IsNullOrEmpty(full)) full = streamed.ToString();
var cut = done?["truncated"] is JsonValue tv && tv.TryGetValue(out bool cutShort) && cutShort;
Console.WriteLine($"truncated: {cut}, charged: {done?["charged_credits"]}");
var result = JsonNode.Parse(full[full.IndexOf('{')..(full.LastIndexOf('}') + 1)])!;
Console.WriteLine($"{result["verdict"]} - {result["headline"]}");
foreach (var f in result["files"]?.AsArray() ?? new JsonArray())
Console.WriteLine($"file {f?["path"]} {(f?["content"]?.GetValue<string>() ?? "").Length} characters");
foreach (var c in result["changes"]?.AsArray() ?? new JsonArray())
{
var flags = string.Join(",", (c?["flags"]?.AsArray() ?? new JsonArray()).Select(fl => fl?.GetValue<string>()));
Console.WriteLine($"change {c?["function"]} {(flags == "" ? "-" : flags)} - {c?["change"]}");
}
foreach (var step in result["verify"]?.AsArray() ?? new JsonArray()) Console.WriteLine($"verify {step}");
foreach (var o in result["open"]?.AsArray() ?? new JsonArray()) Console.WriteLine($"open {o?["item"]} - {o?["reason"]}");
foreach (var q in result["questions"]?.AsArray() ?? new JsonArray()) Console.WriteLine($"question {q}");
What the page does that you now do
- Reads the paste in the browser and builds
codeandfactsfrom it (kotkit.js), joining dropped files under// file:lines (# file:for a.tomlversion catalog) and cutting a large paste on whole files with a// [coroutine-desk: not sent: ...]line; you send the same two strings. - Prices every input with
/estimateand refuses to run belowmin_credits, or on a paste with no Kotlin it can read. - Sends each attempt with an input- and lane-derived
Idempotency-Key, and retries once withretry_notewhen a reply does not parse. - Closes a cut-off JSON reply and shows what arrived when
truncatedis true or the stream ends early. - Hands an audit to the fix lane as
audit_handoff, with every edit applied to the paste first, and only while that edited paste is unchanged. - Checks the reply against the code (
recon.js), in the browser: files, lines, evidence, every edit applied in order and the free check re-run after each finding, flag answers and verdict; the fixed files put back, re-checked and matched against the changes list and the open items, with every fun still there and stillsuspendor not; and repeated credentials.recheck.jsabove runs the same checks for you.
Or skip all of this and use Coroutine Desk in the browser: the check, the flags, the coroutine-builder, blocking-call, catch, flow and collect inventories, putting an edit or the fixed files back into the paste, and the Markdown and CSV exports are free.