← Routrel Audit / API
Tokens

Drive Routrel Audit from your own code

The page reads the Kotlin behind one Ktor service in the browser for free — the Application module and its plugins, the routes, the services, the Exposed tables, entities and repositories and the database setup (a build.gradle.kts or a migration .sql may be among them), one or more files joined into one paste, each introduced by a // file: <path> line — with its line numbers. It lexes Kotlin strings, string templates, raw strings, char literals, backtick names and nested comments out first, rebuilds the brace structure (classes, funs, lambdas, loops, try/catch), and inventories the service: every route with its full path and the authenticate block around it (following Route extension funs to where routing mounts them), every install(...) and what it is configured with, every JWT verifier, every Exposed transaction with the fun it is in, the thread it runs on and what it writes, and every table and entity. Then it runs 22 deterministic checks drawn from Ktor and Exposed practice: server ids KT-01..KT-10 (runBlocking or Thread.sleep in a handler, a state-changing route outside every authenticate { }, CORS anyHost(), a StatusPages handler that sends the exception’s message, a JWT verifier that checks neither audience nor issuer, !! on a request parameter or principal, a parameter parsed with nothing that turns a bad value into 400, call.respond in an if with no return, call.receive with no ContentNegotiation, no StatusPages), data ids EX-01..EX-10 (a blocking transaction { } run from coroutine code, SQL text built from values, a LIKE pattern built from unescaped input, one operation split across several transactions, a DAO entity returned out of its transaction, a query, insert or transaction inside a loop, Database.connect with no pool, SchemaUtils.create at startup, update { } with no where, single() or first() on a query that can find nothing), and KV-01 (a credential literal), plus KV-00 for context. Nothing is compiled or run: every flag is a reading of the text. Its three paid steps are available over HTTP. You send one JSON object with task set to "server", "data" 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 lane’s areas, answers every flag of the lane 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. All three 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 (the estimate’s hold_credits, a reservation) and then charges only what the run used (the job’s charged_credits). /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.

HTTPerror.codeWhat it means / what to do
400VALIDATION_ERRORThe 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.
401UNAUTHORIZEDNo 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.
402INSUFFICIENT_CREDITSThe 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.
404NOT_FOUNDA wrong path, or a job id that does not belong to this token.
409CONFLICTAn 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).
429RATE_LIMITEDToo 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:

WhatWhat happens
event: error on /run-streamThe 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 sThe 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: three lanes

task routes the run, and it takes exactly three values: "server", "data" and "fix". The two audits share one reply shape (with different areas and catalogue ids), the fix lane has its own, and a reply never mixes two contracts. The skill’s instructions tell the model to do the server lane when task is missing or anything else (and the page’s own Ktorkit.buildInput turns any lane it does not know into "server"), so a typo silently gets you a server audit: always send one of the three values.

taskWhat it doesverdictUse it when
"server"Reviews the Ktor server the way a senior engineer reviews it before it takes traffic: which routes anyone can call, what a malformed or hostile request does, what the client sees when something fails, and what blocks the threads serving calls. Rates four areas, in this order: auth (authenticate coverage, the JWT verifier and validate, the principal and ownership checks, CORS), handlers (early responses and returns, parameters and bodies, !!, ContentNegotiation), errors (StatusPages mapping, what reaches the client) and threads (runBlocking, Thread.sleep and blocking calls in handlers), each sound, fragile or broken. Confirms or dismisses every server flag (KT-01..KT-10, KV-01), adds what text rules cannot see (a route that lets one user act on another user’s resource, input used without validation, a public route that returns more than it should), and gives the verbatim evidence and exact before/after edits per finding, with a plan that ends in a testApplication { } test.blocked | needs_work | readyBefore the service takes traffic: auth, request handling, error responses and threads.
"data"Reviews the Exposed data layer the way a senior engineer reviews it before it holds production data: what runs in which transaction, what a concurrent request can do between two steps, what SQL the queries become, and how connections and the schema are managed. Rates three areas, in this order: transactions (blocking vs suspended transactions, one transaction per operation, locking, entities that escape), queries (values in SQL text, LIKE patterns, N+1, update without where, single/first) and setup (connection pool, migrations, SchemaUtils). Confirms or dismisses every data flag (EX-01..EX-10, KV-01), with the same findings, edits and plan as the server audit, ending in a repository test against a real Postgres.blocked | needs_work | readyWhen requests stall under load, balances or counts go wrong under concurrency, or the database shows more connections than expected.
"fix"Writes the fixed version of the pasted code: the whole of every file it changes (at most one new file next to them), plus a changes list tied to the page flags, the verify steps (./gradlew build, and the testApplication and repository tests to write) and what it left open. It closes every high and medium flag of both catalogues unless the note asks to leave something as it is, or the fix needs a change the signature rule forbids: every fun that is not private keeps its name, its parameters, its return type and whether it is suspend, unless the note allows more.fixed | partialWhen you want the code changed, not a list of findings: a service you already know is broken, or what is left after an audit.

The page chains the lanes: after either 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 lane, verdict, headline and findings; after a server audit, “Apply every edit and audit the data layer” puts the server edits into the paste and selects the data audit instead (no handoff string: it is a plain data run on the edited paste). Over the API a handoff is a second run with task: "fix", the edited paste as code, facts recomputed from the edited paste for the fix lane, 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 (“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 little to review in that lane (“No route and no install(...) were found: there may be little Ktor server code here to audit.”, “No Exposed transaction, table or Database.connect was found: there may be little data-layer code here to audit.”, or “No Ktor route and no Exposed code were found: there may be little here to fix.”).

Input fields

This is exactly what the page sends (ktorkit.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: {task, code, facts, note?, audit_handoff?, retry_note?}. buildInput emits them in the order task, code, facts, note, audit_handoff:

FieldTypeRequiredMeaning
taskstringyes"server", "data" or "fix" (the lanes above).
codestringyesThe paste, each file introduced by a // file: <path> line (# file: <path> for a .toml file); 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 // [routrel-audit: not sent: ...] line at the end (below).
factsstringyesThe page’s free check for this lane, Ktorkit.facts(A, lane), JSON-encoded into a string: JSON.stringify(facts) in JavaScript, json.dumps(facts) in Python, and so on. The input schema declares it required, so it must be sent; sending the object itself is a 400. The model is told to parse it.
audit_handoffstringnofix lane only: a JSON string built from a server or data audit reply on this code (recon.js fixHandoff, below). buildInput drops it for the two audits.
notestringnoWhat the engineer says: the goal, a constraint (“keep the repository’s API”, “no new dependency”), 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_notestringnoSent 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>) starts each file when the path holds a . or a /, and a --- line just before it only separates files. Files dropped on the page (or a dropped source folder) are joined that way by Ktorkit.joinFiles, the build file first, then migration .sql files, then Application.kt, then the rest by path: each file under its marker line (# file: for a .versions.toml, // file: otherwise), 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 script). The page normalises line endings to \n. A build.gradle(.kts), settings.gradle(.kts) or .versions.toml path is read as gradle (for the Ktor, Exposed and Kotlin versions and the artifacts), a .kt or .kts path as kotlin; anything else, a migration .sql included, is other: it is sent and the model may read it, but the checks do not parse it. 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 Gradle 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:

// [routrel-audit: not sent: src/main/resources/db/migration/V7__audit_log.sql, src/main/kotlin/com/acme/AdminRoutes.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 required by the input schema: every run carries it as a string. The browser computes it for free with the page’s check: Ktorkit.analyze splits the paste into files, lexes them, rebuilds their brace structure, resolves every route’s full path and authenticate block, reads the plugins, JWT verifiers, transactions, tables and entities and runs the rules, and Ktorkit.facts(A, lane) turns that analysis into the object below. It is cut per lane: the server audit gets the server flags and the shared ones (KV-01, KV-00), the data audit the data flags and the shared ones, the fix lane all of them, and lane and verdict_candidate follow the lane. The flag uids (F1, F2, ...) are numbered over the whole paste, so a lane’s list can skip numbers. It is how the model knows which flags to answer, which file paths and line ranges to name, and which funs must survive. Its top-level keys, in the order built:

KeyContents
laneThe lane these facts were cut for: server, data or fix. Send it with the same task.
buildWhat a pasted build file declares: kotlin, ktor, exposed (versions, or null), artifacts, gradle_pasted; and counts: kotlin_files, functions, suspend_funs, routes, transactions, and module_pasted (an Application module with its install(...) calls, and every configure fun it calls, is in the paste).
filesEvery file: path (use it verbatim), kind (kotlin, gradle, other), line and end_line (the paste lines of the file’s content, without its marker line).
functionsEvery fun (up to 200): name (Class.fun for members, Receiver.fun for extension funs such as Route.slotRoutes), file, line, suspend, private.
routesEvery route handler (up to 150): method, path (the full path, prefixes included), file, line, func, auth (the authenticate provider names around it, or null), mounted (false when its Route fun is never mounted in the paste: its prefix and its authenticate block are then unknown), and where they apply optional_auth, receives (the types it reads with call.receive) and via (where its Route fun is mounted).
pluginsEvery install(...) (up to 40): name, file, line, func, and for CORS any_host, allow_credentials and hosts.
jwtEvery jwt provider (up to 10): name, file, line, verifier, audience, issuer (true, false, or null when the verifier is built outside the paste), validate.
transactionsEvery Exposed transaction (up to 150): file, line, func, kind (transaction, newSuspendedTransaction ...), blocking, coroutine (it runs in coroutine code), dispatcher, writes, queries, and nested when it is.
tables, entitiesEvery Exposed table object (name, base, sql_name, file, line) and DAO entity class (name, base, file, line), up to 60 each.
flagsEvery flag of the lane: uid, id (KT-01..KT-10, EX-01..EX-10, KV-01, KV-00), 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_candidateThe verdict the lane’s flags alone point at: blocked with any high flag, needs_work with any medium, otherwise ready.
notesPlain sentences about what was not read or not sent (empty for a paste that fits).

The simplest source is ktorkit.js itself: the page loads it from /ktorkit.js, and it also runs in Node (it sets module.exports). This script prints what the page would send as task, code and facts for one lane (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 bookings-paste.txt and redirect the output into bookings-check.json:

// build-check.js -- run: node build-check.js server bookings-paste.txt > bookings-check.json   (ktorkit.js saved next to it)
const fs = require("fs");
const Ktorkit = require("./ktorkit.js");
const lane = process.argv[2];                                  // server | data | fix: facts are cut per lane
if (Ktorkit.LANES.indexOf(lane) === -1) { console.error("lane must be server, data or fix"); process.exit(1); }
const A = Ktorkit.analyze(fs.readFileSync(process.argv[3], "utf8"));
if (!A.ok) { console.error(A.errors.join("\n")); process.exit(1); }
const inp = Ktorkit.buildInput(lane, A, "", null);
console.log(JSON.stringify({ task: lane, code: inp.code, facts: JSON.parse(inp.facts) }, null, 2));
const c = A.laneCounts[lane], real = A.flags.filter((f) => Ktorkit.inLane(f, lane) && f.sev !== "info");
console.error(lane + ": " + A.files.length + " file(s), " + A.functions.length + " fun(s), " + A.routes.length + " route(s), " + A.transactions.length + " transaction(s); " +
  real.length + " flags (" + c.high + " high, " + c.medium + " medium, " + c.low + " low); verdict_candidate " + A.candidates[lane]);

For the three worked examples (node build-check.js server bookings-paste.txt, node build-check.js data bookings-paste.txt, node build-check.js fix wallet-paste.txt) the counts it reports are the ones in the facts below:

server: 6 file(s), 6 fun(s), 3 route(s), 3 transaction(s); 0 flags (0 high, 0 medium, 0 low); verdict_candidate ready
data: 6 file(s), 6 fun(s), 3 route(s), 3 transaction(s); 0 flags (0 high, 0 medium, 0 low); verdict_candidate ready
fix: 5 file(s), 7 fun(s), 2 route(s), 5 transaction(s); 4 flags (2 high, 2 medium, 0 low); verdict_candidate blocked

For all three its code and facts are exactly what the page sends. The same paste gives different facts per lane, so build the file for the lane you will run (a server file sent with task: "data" carries no data flags to answer).

Without the page’s check. A programmatic caller that does not run ktorkit.js must build facts itself: it is required, and the platform only checks that it is a string. A minimal string with the same keys works, and the model still reads code:

{"lane":"server","build":{"kotlin":null,"ktor":null,"exposed":null,"artifacts":[],"gradle_pasted":false,"kotlin_files":0,"functions":0,"suspend_funs":0,"routes":0,"transactions":0,"module_pasted":false},"files":[],"functions":[],"routes":[],"plugins":[],"jwt":[],"transactions":[],"tables":[],"entities":[],"flags":[],"verdict_candidate":null,"notes":["No page check was run; read the code directly."]}

Set lane to the task you send. But fill in the fields the model needs, or 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 routes the model does not know which authenticate block a route sits in once its Route fun is mounted elsewhere, with no transactions it does not know which thread each one runs on, and with no functions nothing says what must survive a fix and stay suspend or not. Build it with ktorkit.js as above when you can; it costs nothing.

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, Ktorkit);   // every finding's edits, in order, each on top of the last
JSON.stringify({ lane: res.task, 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 for the fix lane

That is {"lane", "verdict", "headline", "findings": [{id, severity, file, line, title, why, edits}], "applied", "failed"}, where lane is server or data and each finding’s edits is the number of edits it carried. applyFindings applies every finding’s edits with Ktorkit.applyEdits, in the order listed: each before must occur exactly once in its file as the earlier edits left it, 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 fix-lane 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 server > orders-fix-check.json   (ktorkit.js, recon.js next to it)
const fs = require("fs");
const Ktorkit = require("./ktorkit.js");
const Recon = require("./recon.js");
const A = Ktorkit.analyze(fs.readFileSync(process.argv[2], "utf8"));           // the audited paste
const lane = process.argv[4] || "server";                                      // the audit's lane: server or data
const res = Recon.normalize(Recon.parseResult(fs.readFileSync(process.argv[3], "utf8")), lane);
const ho = Recon.fixHandoff(A, res);                                           // every edit, in order
const B = Ktorkit.analyze(ho.text);                                            // facts recomputed from the edited paste
const inp = Ktorkit.buildInput("fix", B, "", ho.handoff);
console.log(JSON.stringify({ task: "fix", 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 the reply file. An audit with no findings (like the two bookings audits below) hands on no edit: its handoff carries "findings": [] and "applied": 0, and a fix run on it works from the paste and its facts alone. For the server-to-data step, take ho.text (the paste with every server edit applied) and run build-check.js data on it: the data audit gets no handoff string.

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 every lane it must ground everything in code: a caller, a plugin installed elsewhere, an application.conf value, a migration, an index, a database constraint, a Koin module or a library’s behaviour that is not in the paste is never claimed to exist — it goes into questions (“if DatabaseFactory.create builds a HikariDataSource ...”, never “it does”). It must never write a secret value: a line that holds a literal credential (a password, a JWT signing key, a token, a JDBC URL with a password) 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 (example hosts use example). It must never change what the code offers its callers: public classes, funs and properties keep their names, parameters, return types and whether they are suspend, unless the note allows more. When the only real fix changes a public signature and the note does not allow it, an audit keeps the finding with edits: [] and says in why what to change, and the fix lane names it in open. Before using an API it must be sure it exists in the Ktor and Exposed versions the paste uses (the build file, else the imports), and it never adds a dependency the paste does not already have unless the note allows it. Every flag of the lane whose sev is not info is answered exactly once (audit lanes: in flag_answers), and an info flag is never reported as a finding.

task: "server" and task: "data"

task: "fix"

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; one naming the fix lane when an audit was asked (or the other way round) is read as that lane only when it carries that lane’s body (a files array for fix, a findings array for an audit), and one naming the other audit is read as the audit asked for, with the mismatch counted as a disagreement. An unknown verdict shows as needs_work or partial and counts as a disagreement; an unknown severity becomes medium; ids like KT1 or ex 9 become KT-01 and EX-09; flags keeps only F<n> values; findings with no file, evidence and title are dropped; system may also arrive as feature or service, 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, system, 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

One per lane, from the examples the page ships (the example buttons on the app page; the full pastes are in /example.js, ids bookings and wallet), built exactly as the page builds them: Ktorkit.buildInput(lane, Ktorkit.analyze(text), note, null), with the real model reply the page replays for each (/example-result.js; the bookings data audit is the clinic’s second saved reply). The server and data examples are the same paste run in two lanes. The pastes are shown whole; the request bodies cut the long code and facts strings with a visible ..., and the replies are pretty-printed, with ... only where a long file content was cut (the files are then shown whole). Every value shown is copied from the input or the reply. No paste holds a credential, and the services are invented.

ExampletaskPage flags of the laneverdict_candidateReply verdictIdempotency-Key the page sends
Clinic bookings"server"none (KV-00 only)readyreadyroutrel-audit:server:e7ea62e2-3b88:a1
Clinic bookings"data"none (KV-00 only)readyreadyroutrel-audit:data:1ad72fe8-3b84:a1
Wallet"fix"4 (2 high, 2 medium) plus KV-00blockedpartialroutrel-audit:fix:5820c181-391b:a1

Server audit: a clinic booking service with its routes behind authenticate, a JWT verifier with audience and issuer, StatusPages that leak nothing and CORS limited to one host - ready

Six files: build.gradle.kts (Kotlin 1.9.24, Ktor 2.3.12, Exposed 0.52.0, HikariCP and Flyway), src/main/resources/db/migration/V1__slots.sql (a slots table with UNIQUE (practitioner_id, starts_at)), and under src/main/kotlin/com/varnwell/clinic/ Application.kt, SlotRoutes.kt, SlotRepository.kt and DatabaseFactory.kt. Application.module reads the JWT audience, issuer and secret from environment.config, installs ContentNegotiation with json(), CORS with allowHost("book.varnwell.example", schemes = listOf("https")), a jwt("patient") provider whose verifier calls withAudience and withIssuer, and StatusPages that answers IllegalArgumentException with 400 and any other Throwable with a fixed “Internal server error” after logging it; then routing { slotRoutes(slots) }. Route.slotRoutes has a public GET /practitioners/{pid}/slots and, inside authenticate("patient"), POST and DELETE /slots/{id}/booking, each parsing its parameters with toIntOrNull() and ?: return@... call.respond(...) and taking the patient from the JWT subject. SlotRepository runs every operation in newSuspendedTransaction(Dispatchers.IO, db), and books with one conditional update whose row count is checked. The page’s free check raises no server flag, only the KV-00 context flag; its own reading of the verdict is ready. The engineer’s note: “Release candidate for the clinic's booking API. Anything to fix before it goes out?”

The code string, whole: 9,144 characters, 223 lines ending in a newline. Save it as bookings-paste.txt and run node build-check.js server bookings-paste.txt > bookings-check.json:

// file: build.gradle.kts
plugins {
    kotlin("jvm") version "1.9.24"
    id("io.ktor.plugin") version "2.3.12"
    kotlin("plugin.serialization") version "1.9.24"
}

dependencies {
    implementation("io.ktor:ktor-server-core-jvm")
    implementation("io.ktor:ktor-server-netty-jvm")
    implementation("io.ktor:ktor-server-content-negotiation-jvm")
    implementation("io.ktor:ktor-serialization-kotlinx-json-jvm")
    implementation("io.ktor:ktor-server-auth-jvm")
    implementation("io.ktor:ktor-server-auth-jwt-jvm")
    implementation("io.ktor:ktor-server-cors-jvm")
    implementation("io.ktor:ktor-server-status-pages-jvm")
    implementation("org.jetbrains.exposed:exposed-core:0.52.0")
    implementation("org.jetbrains.exposed:exposed-jdbc:0.52.0")
    implementation("org.postgresql:postgresql:42.7.3")
    implementation("com.zaxxer:HikariCP:5.1.0")
    implementation("org.flywaydb:flyway-core:10.15.0")
    implementation("org.flywaydb:flyway-database-postgresql:10.15.0")
    implementation("ch.qos.logback:logback-classic:1.5.6")
}
// file: src/main/resources/db/migration/V1__slots.sql
CREATE TABLE slots (
    id SERIAL PRIMARY KEY,
    practitioner_id INTEGER NOT NULL,
    starts_at BIGINT NOT NULL,
    patient_id VARCHAR(64),
    CONSTRAINT ux_slots_practitioner_start UNIQUE (practitioner_id, starts_at)
);
// file: src/main/kotlin/com/varnwell/clinic/Application.kt
package com.varnwell.clinic

import com.auth0.jwt.JWT
import com.auth0.jwt.algorithms.Algorithm
import io.ktor.http.HttpHeaders
import io.ktor.http.HttpMethod
import io.ktor.http.HttpStatusCode
import io.ktor.serialization.kotlinx.json.json
import io.ktor.server.application.Application
import io.ktor.server.application.call
import io.ktor.server.application.install
import io.ktor.server.auth.Authentication
import io.ktor.server.auth.jwt.JWTPrincipal
import io.ktor.server.auth.jwt.jwt
import io.ktor.server.plugins.contentnegotiation.ContentNegotiation
import io.ktor.server.plugins.cors.routing.CORS
import io.ktor.server.plugins.statuspages.StatusPages
import io.ktor.server.response.respond
import io.ktor.server.routing.routing
import kotlinx.serialization.Serializable

@Serializable
data class ApiError(val error: String)

fun Application.module() {
    val cfg = environment.config
    val audience = cfg.property("jwt.audience").getString()
    val issuer = cfg.property("jwt.issuer").getString()
    val secret = cfg.property("jwt.secret").getString()
    val slots = SlotRepository(DatabaseFactory.create(cfg))

    install(ContentNegotiation) {
        json()
    }
    install(CORS) {
        allowHost("book.varnwell.example", schemes = listOf("https"))
        allowHeader(HttpHeaders.ContentType)
        allowHeader(HttpHeaders.Authorization)
        allowMethod(HttpMethod.Delete)
    }
    install(Authentication) {
        jwt("patient") {
            realm = "varnwell"
            verifier(
                JWT.require(Algorithm.HMAC256(secret))
                    .withAudience(audience)
                    .withIssuer(issuer)
                    .build()
            )
            validate { credential ->
                if (credential.payload.subject.isNullOrBlank()) null else JWTPrincipal(credential.payload)
            }
            challenge { _, _ ->
                call.respond(HttpStatusCode.Unauthorized, ApiError("Sign in again"))
            }
        }
    }
    install(StatusPages) {
        exception<IllegalArgumentException> { call, cause ->
            call.respond(HttpStatusCode.BadRequest, ApiError(cause.message ?: "Bad request"))
        }
        exception<Throwable> { call, cause ->
            call.application.environment.log.error("Unhandled error", cause)
            call.respond(HttpStatusCode.InternalServerError, ApiError("Internal server error"))
        }
    }
    routing {
        slotRoutes(slots)
    }
}
// file: src/main/kotlin/com/varnwell/clinic/SlotRoutes.kt
package com.varnwell.clinic

import io.ktor.http.HttpStatusCode
import io.ktor.server.application.call
import io.ktor.server.auth.authenticate
import io.ktor.server.auth.jwt.JWTPrincipal
import io.ktor.server.auth.principal
import io.ktor.server.response.respond
import io.ktor.server.routing.Route
import io.ktor.server.routing.delete
import io.ktor.server.routing.get
import io.ktor.server.routing.post
import io.ktor.server.routing.route

fun Route.slotRoutes(slots: SlotRepository) {
    route("/practitioners/{pid}/slots") {
        get {
            val pid = call.parameters["pid"]?.toIntOrNull()
                ?: return@get call.respond(HttpStatusCode.BadRequest, ApiError("pid must be a number"))
            val from = call.request.queryParameters["from"]?.toLongOrNull() ?: 0L
            call.respond(slots.freeSlots(pid, from, limit = 50))
        }
    }

    authenticate("patient") {
        route("/slots/{id}") {
            post("/booking") {
                val id = call.parameters["id"]?.toIntOrNull()
                    ?: return@post call.respond(HttpStatusCode.BadRequest, ApiError("id must be a number"))
                val patient = call.principal<JWTPrincipal>()?.subject
                    ?: return@post call.respond(HttpStatusCode.Unauthorized, ApiError("Sign in again"))
                if (slots.book(id, patient)) call.respond(HttpStatusCode.Created, BookingResult(id, patient))
                else call.respond(HttpStatusCode.Conflict, ApiError("This slot is no longer free"))
            }

            delete("/booking") {
                val id = call.parameters["id"]?.toIntOrNull()
                    ?: return@delete call.respond(HttpStatusCode.BadRequest, ApiError("id must be a number"))
                val patient = call.principal<JWTPrincipal>()?.subject
                    ?: return@delete call.respond(HttpStatusCode.Unauthorized, ApiError("Sign in again"))
                if (slots.cancel(id, patient)) call.respond(HttpStatusCode.NoContent)
                else call.respond(HttpStatusCode.NotFound, ApiError("No booking of yours on this slot"))
            }
        }
    }
}
// file: src/main/kotlin/com/varnwell/clinic/SlotRepository.kt
package com.varnwell.clinic

import kotlinx.coroutines.Dispatchers
import kotlinx.serialization.Serializable
import org.jetbrains.exposed.dao.id.IntIdTable
import org.jetbrains.exposed.sql.Database
import org.jetbrains.exposed.sql.SortOrder
import org.jetbrains.exposed.sql.and
import org.jetbrains.exposed.sql.selectAll
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction
import org.jetbrains.exposed.sql.update

object Slots : IntIdTable("slots") {
    val practitionerId = integer("practitioner_id")
    val startsAt = long("starts_at")
    val patientId = varchar("patient_id", 64).nullable()
}

@Serializable
data class Slot(val id: Int, val practitionerId: Int, val startsAt: Long)

@Serializable
data class BookingResult(val slotId: Int, val patient: String)

class SlotRepository(private val db: Database) {
    suspend fun freeSlots(practitionerId: Int, from: Long, limit: Int): List<Slot> = newSuspendedTransaction(Dispatchers.IO, db) {
        Slots.selectAll()
            .where { (Slots.practitionerId eq practitionerId) and (Slots.startsAt greaterEq from) and Slots.patientId.isNull() }
            .orderBy(Slots.startsAt, SortOrder.ASC)
            .limit(limit)
            .map { Slot(it[Slots.id].value, it[Slots.practitionerId], it[Slots.startsAt]) }
    }

    /* One conditional UPDATE: it books the slot only while it is still free, so two patients racing for
     * the same slot cannot both win - the second one updates no row. */
    suspend fun book(slotId: Int, patient: String): Boolean = newSuspendedTransaction(Dispatchers.IO, db) {
        Slots.update({ (Slots.id eq slotId) and Slots.patientId.isNull() }) {
            it[patientId] = patient
        } == 1
    }

    suspend fun cancel(slotId: Int, patient: String): Boolean = newSuspendedTransaction(Dispatchers.IO, db) {
        Slots.update({ (Slots.id eq slotId) and (Slots.patientId eq patient) }) {
            it[patientId] = null
        } == 1
    }
}
// file: src/main/kotlin/com/varnwell/clinic/DatabaseFactory.kt
package com.varnwell.clinic

import com.zaxxer.hikari.HikariConfig
import com.zaxxer.hikari.HikariDataSource
import io.ktor.server.config.ApplicationConfig
import org.flywaydb.core.Flyway
import org.jetbrains.exposed.sql.Database

object DatabaseFactory {
    fun create(cfg: ApplicationConfig): Database {
        val config = HikariConfig().apply {
            jdbcUrl = cfg.property("database.url").getString()
            username = cfg.property("database.user").getString()
            password = cfg.property("database.password").getString()
            maximumPoolSize = cfg.property("database.maxPoolSize").getString().toInt()
            isAutoCommit = false
            transactionIsolation = "TRANSACTION_READ_COMMITTED"
            validate()
        }
        val dataSource = HikariDataSource(config)
        Flyway.configure().dataSource(dataSource).locations("classpath:db/migration").load().migrate()
        return Database.connect(dataSource)
    }
}

Its server-lane facts, decoded (the request carries this object as one compact JSON string):

{
  "lane": "server",
  "build": {
    "kotlin": "1.9.24",
    "ktor": "2.3.12",
    "exposed": "0.52.0",
    "artifacts": [
      "ktor-server-core-jvm",
      "ktor-server-netty-jvm",
      "ktor-server-content-negotiation-jvm",
      "ktor-serialization-kotlinx-json-jvm",
      "ktor-server-auth-jvm",
      "ktor-server-auth-jwt-jvm",
      "ktor-server-cors-jvm",
      "ktor-server-status-pages-jvm",
      "exposed-core",
      "exposed-jdbc",
      "postgresql",
      "HikariCP",
      "flyway-core",
      "flyway-database-postgresql"
    ],
    "gradle_pasted": true,
    "kotlin_files": 4,
    "functions": 6,
    "suspend_funs": 3,
    "routes": 3,
    "transactions": 3,
    "module_pasted": true
  },
  "files": [
    {
      "path": "build.gradle.kts",
      "kind": "gradle",
      "line": 2,
      "end_line": 24
    },
    {
      "path": "src/main/resources/db/migration/V1__slots.sql",
      "kind": "other",
      "line": 26,
      "end_line": 32
    },
    {
      "path": "src/main/kotlin/com/varnwell/clinic/Application.kt",
      "kind": "kotlin",
      "line": 34,
      "end_line": 103
    },
    {
      "path": "src/main/kotlin/com/varnwell/clinic/SlotRoutes.kt",
      "kind": "kotlin",
      "line": 105,
      "end_line": 150
    },
    {
      "path": "src/main/kotlin/com/varnwell/clinic/SlotRepository.kt",
      "kind": "kotlin",
      "line": 152,
      "end_line": 198
    },
    {
      "path": "src/main/kotlin/com/varnwell/clinic/DatabaseFactory.kt",
      "kind": "kotlin",
      "line": 200,
      "end_line": 224
    }
  ],
  "functions": [
    {
      "name": "Application.module",
      "file": "src/main/kotlin/com/varnwell/clinic/Application.kt",
      "line": 58,
      "suspend": false,
      "private": false
    },
    {
      "name": "Route.slotRoutes",
      "file": "src/main/kotlin/com/varnwell/clinic/SlotRoutes.kt",
      "line": 119,
      "suspend": false,
      "private": false
    },
    {
      "name": "SlotRepository.freeSlots",
      "file": "src/main/kotlin/com/varnwell/clinic/SlotRepository.kt",
      "line": 177,
      "suspend": true,
      "private": false
    },
    {
      "name": "SlotRepository.book",
      "file": "src/main/kotlin/com/varnwell/clinic/SlotRepository.kt",
      "line": 187,
      "suspend": true,
      "private": false
    },
    {
      "name": "SlotRepository.cancel",
      "file": "src/main/kotlin/com/varnwell/clinic/SlotRepository.kt",
      "line": 193,
      "suspend": true,
      "private": false
    },
    {
      "name": "DatabaseFactory.create",
      "file": "src/main/kotlin/com/varnwell/clinic/DatabaseFactory.kt",
      "line": 209,
      "suspend": false,
      "private": false
    }
  ],
  "routes": [
    {
      "method": "GET",
      "path": "/practitioners/{pid}/slots",
      "file": "src/main/kotlin/com/varnwell/clinic/SlotRoutes.kt",
      "line": 121,
      "func": "Route.slotRoutes",
      "auth": null,
      "mounted": true,
      "via": [
        "Route.slotRoutes (mounted at src/main/kotlin/com/varnwell/clinic/Application.kt:101)"
      ]
    },
    {
      "method": "POST",
      "path": "/slots/{id}/booking",
      "file": "src/main/kotlin/com/varnwell/clinic/SlotRoutes.kt",
      "line": 131,
      "func": "Route.slotRoutes",
      "auth": "patient",
      "mounted": true,
      "via": [
        "Route.slotRoutes (mounted at src/main/kotlin/com/varnwell/clinic/Application.kt:101)"
      ]
    },
    {
      "method": "DELETE",
      "path": "/slots/{id}/booking",
      "file": "src/main/kotlin/com/varnwell/clinic/SlotRoutes.kt",
      "line": 140,
      "func": "Route.slotRoutes",
      "auth": "patient",
      "mounted": true,
      "via": [
        "Route.slotRoutes (mounted at src/main/kotlin/com/varnwell/clinic/Application.kt:101)"
      ]
    }
  ],
  "plugins": [
    {
      "name": "ContentNegotiation",
      "file": "src/main/kotlin/com/varnwell/clinic/Application.kt",
      "line": 65,
      "func": "Application.module"
    },
    {
      "name": "CORS",
      "file": "src/main/kotlin/com/varnwell/clinic/Application.kt",
      "line": 68,
      "func": "Application.module",
      "any_host": false,
      "allow_credentials": false,
      "hosts": [
        "book.varnwell.example"
      ]
    },
    {
      "name": "Authentication",
      "file": "src/main/kotlin/com/varnwell/clinic/Application.kt",
      "line": 74,
      "func": "Application.module"
    },
    {
      "name": "StatusPages",
      "file": "src/main/kotlin/com/varnwell/clinic/Application.kt",
      "line": 91,
      "func": "Application.module"
    }
  ],
  "jwt": [
    {
      "name": "patient",
      "file": "src/main/kotlin/com/varnwell/clinic/Application.kt",
      "line": 75,
      "verifier": true,
      "audience": true,
      "issuer": true,
      "validate": true
    }
  ],
  "transactions": [
    {
      "file": "src/main/kotlin/com/varnwell/clinic/SlotRepository.kt",
      "line": 177,
      "func": "SlotRepository.freeSlots",
      "kind": "newSuspendedTransaction",
      "blocking": false,
      "coroutine": true,
      "dispatcher": "io",
      "writes": [],
      "queries": [
        "Slots.selectAll"
      ]
    },
    {
      "file": "src/main/kotlin/com/varnwell/clinic/SlotRepository.kt",
      "line": 187,
      "func": "SlotRepository.book",
      "kind": "newSuspendedTransaction",
      "blocking": false,
      "coroutine": true,
      "dispatcher": "io",
      "writes": [
        "update"
      ],
      "queries": []
    },
    {
      "file": "src/main/kotlin/com/varnwell/clinic/SlotRepository.kt",
      "line": 193,
      "func": "SlotRepository.cancel",
      "kind": "newSuspendedTransaction",
      "blocking": false,
      "coroutine": true,
      "dispatcher": "io",
      "writes": [
        "update"
      ],
      "queries": []
    }
  ],
  "tables": [
    {
      "name": "Slots",
      "base": "IntIdTable",
      "sql_name": "slots",
      "file": "src/main/kotlin/com/varnwell/clinic/SlotRepository.kt",
      "line": 164
    }
  ],
  "entities": [],
  "flags": [
    {
      "uid": "F1",
      "id": "KV-00",
      "sev": "info",
      "file": "src/main/kotlin/com/varnwell/clinic/Application.kt",
      "line": 34,
      "text": "context: 4 Kotlin files, 6 fun(s) (3 suspend), 3 route(s), 4 plugin install(s), 3 Exposed transaction(s), 1 table(s), 0 entities; Ktor 2.3.12; Exposed 0.52.0",
      "detail": "Also read: src/main/resources/db/migration/V1__slots.sql. Only what is pasted is checked: routes in Route functions that are never mounted in the paste get their path and authenticate block from nowhere (their auth is not judged), what a fun in another file does is unknown, and the database's own indexes and constraints are outside the check unless the migrations are pasted."
    }
  ],
  "verdict_candidate": "ready",
  "notes": []
}

The request body (POST /estimate, then POST /run or /run-stream with Idempotency-Key: routrel-audit:server:e7ea62e2-3b88:a1 as the page sends it):

{
  "task": "server",
  "code": "// file: build.gradle.kts\nplugins {\n    kotlin(\"jvm\") version \"1.9.24\"\n    id(\"io.ktor.plugin\") version \"2.3.12\"\n...",
  "facts": "{\"lane\":\"server\",\"build\":{\"kotlin\":\"1.9.24\",\"ktor\":\"2.3.12\",\"exposed\":\"0.52.0\",\"artifacts\":[\"ktor-server-core-jvm\",\"ktor-server-netty-jvm\",\"ktor-server-content-negotiatio...",
  "note": "Release candidate for the clinic's booking API. Anything to fix before it goes out?"
}

The reply (output.output), whole:

{
  "task": "server",
  "verdict": "ready",
  "headline": "Auth, error handling and threading in this Ktor server are sound; nothing here blocks release.",
  "system": "This is a Ktor + Exposed booking API for a clinic: patients browse a practitioner's free slots and book or cancel their own slot by id; the audited part is the Application module, its plugins, and the slot routes.",
  "areas": [
    {
      "area": "auth",
      "status": "sound",
      "note": "The JWT verifier checks both audience and issuer, CORS is limited to book.varnwell.example with no allowCredentials, and both state-changing routes sit inside authenticate(\"patient\") with the booking/cancel identity taken only from the JWT subject, not from client input."
    },
    {
      "area": "handlers",
      "status": "sound",
      "note": "Every path and query parameter is parsed with toIntOrNull/toLongOrNull and answered with an early return@... 400 or a default, call.principal<JWTPrincipal>() is guarded with ?: return, and each handler's if/else covers both respond branches with no fall-through."
    },
    {
      "area": "errors",
      "status": "sound",
      "note": "StatusPages maps IllegalArgumentException to 400 with its message and every other Throwable to a fixed \"Internal server error\" after logging the cause, so no exception text or stack trace reaches the client."
    },
    {
      "area": "threads",
      "status": "sound",
      "note": "All three repository operations run inside newSuspendedTransaction(Dispatchers.IO, db) from suspend functions; no runBlocking or Thread.sleep appears in a route handler."
    }
  ],
  "findings": [],
  "flag_answers": [],
  "plan": [
    "Add a testApplication test that calls POST and DELETE /slots/{id}/booking with no Authorization header and asserts 401.",
    "Add a testApplication test that calls POST /slots/{id}/booking with a non-numeric id (e.g. /slots/abc/booking) and asserts 400.",
    "Add a testApplication test that calls POST /slots/{id}/booking for a slot that does not exist or is already booked and asserts 409, and DELETE /slots/{id}/booking for a slot the caller never booked and asserts 404."
  ],
  "questions": [],
  "summary": "Nothing in the server layer needs a fix before release: JWT verification, CORS, StatusPages and every route's parameter and principal handling follow the house rules, and no handler blocks a thread. The only outstanding gap is test coverage - add the testApplication cases in the plan to lock in the 401/400/409/404 behavior before shipping."
}

With no server flag to answer, findings and flag_answers are both [], which is what the contract asks for: an empty flag_answers is right only when the lane’s facts carry no non-info flag. The four areas come back in the lane’s order, all sound, so the verdict is ready, and with no finding the plan is just the testApplication tests. The data layer of the same paste is the next example.

Data audit: the same clinic paste, run in the data lane - suspended transactions on a Hikari pool, Flyway migrations and a conditional UPDATE that books a slot once

The code string is the same 9,144-character paste as the server example (the page’s example stores a second saved reply for this lane). The data lane’s facts are the server facts above with one key changed: "lane": "data". Every other key (build, files, functions, routes, plugins, jwt, transactions, tables, entities, the lone KV-00 flag F1, verdict_candidate: "ready" and notes) is identical, because the paste raises neither a server nor a data flag. Build it with node build-check.js data bookings-paste.txt > bookings-data-check.json. The three transactions it lists are the ones this lane reviews: SlotRepository.freeSlots (queries: ["Slots.selectAll"]), SlotRepository.book and SlotRepository.cancel (writes: ["update"]), each a newSuspendedTransaction with coroutine: true and dispatcher: "io".

The request body (Idempotency-Key: routrel-audit:data:1ad72fe8-3b84:a1):

{
  "task": "data",
  "code": "// file: build.gradle.kts\nplugins {\n    kotlin(\"jvm\") version \"1.9.24\"\n    id(\"io.ktor.plugin\") version \"2.3.12\"\n...",
  "facts": "{\"lane\":\"data\",\"build\":{\"kotlin\":\"1.9.24\",\"ktor\":\"2.3.12\",\"exposed\":\"0.52.0\",\"artifacts\":[\"ktor-server-core-jvm\",\"ktor-server-netty-jvm\",\"ktor-server-content-negotiation-...",
  "note": "Release candidate for the clinic's booking API. Anything to fix before it goes out?"
}

The reply (output.output), whole, pretty-printed (the model sent it on one line):

{
  "task": "data",
  "verdict": "ready",
  "headline": "The Exposed data layer books and cancels slots with single atomic conditional updates and a pooled, migrated setup, with nothing left to fix before release.",
  "system": "This Ktor service lets patients list a practitioner's free slots and book or cancel a booking on one; the audited part is the Exposed data layer - the Slots table, SlotRepository's transactions and queries, and DatabaseFactory's connection and migration setup.",
  "areas": [
    {
      "area": "transactions",
      "status": "sound",
      "note": "freeSlots, book and cancel each run one newSuspendedTransaction(Dispatchers.IO, db), and book/cancel decide and write in a single conditional update whose row count is checked, so no operation is split across transactions or races on the same row."
    },
    {
      "area": "queries",
      "status": "sound",
      "note": "No query builds SQL from values or a LIKE pattern, no query or update runs inside a loop, freeSlots takes a fixed limit and maps rows to a plain Slot inside its transaction, and neither update can find nothing since its row count is checked instead of calling single() or first()."
    },
    {
      "area": "setup",
      "status": "sound",
      "note": "DatabaseFactory builds a HikariDataSource with a pool size, isAutoCommit = false and an isolation level, and a versioned Flyway migration owns the schema; SchemaUtils.create is not used."
    }
  ],
  "findings": [],
  "flag_answers": [],
  "plan": [
    "Add a repository test against a real Postgres (Testcontainers) that fires two concurrent SlotRepository.book calls for the same slot id and asserts exactly one returns true and the other false.",
    "Add a repository test asserting SlotRepository.freeSlots for one practitioner runs in one query and returns no more than its limit."
  ],
  "questions": [],
  "summary": "Booking and cancelling each run inside one newSuspendedTransaction and decide-and-write in a single conditional UPDATE whose affected-row count is checked, so two patients racing for the same slot cannot both win. Queries build no SQL from values, use no LIKE pattern or loops, and DatabaseFactory pools connections through HikariCP behind a versioned Flyway migration. This part of the code has nothing to fix before release; add the two concurrency and query-shape tests in plan first."
}

Three areas, in the data lane’s order (transactions, queries, setup), all sound. The two plan steps are the repository tests the contract asks for when there is no finding: two concurrent book calls on one slot, exactly one of which wins, and a query-shape test for freeSlots.

Fix: a wallet data layer whose setBalance updates every row, whose statement query splices the owner into SQL and whose transfer runs in five transactions - with findByOwner’s entity return type kept on purpose

Five files: build.gradle.kts (Ktor 2.3.12, Exposed 0.52.0 core, DAO and JDBC, the Postgres driver) and under src/main/kotlin/com/quillbank/wallet/ Tables.kt (a Wallets UUIDTable, a Transfers LongIdTable and a WalletEntity), WalletRepository.kt, TransferService.kt and WalletRoutes.kt. Every repository fun opens its own newSuspendedTransaction(Dispatchers.IO, db): findByOwner returns the WalletEntity? it loaded, setBalance calls Wallets.update { } with no where, and statement builds its exec(...) SQL with '$owner' and '$month' templates. TransferService.transfer reads both balances, checks the source, and calls setBalance twice and recordTransfer once — five transactions for one transfer. Route.walletRoutes sits inside authenticate("customer") but is never mounted in the paste (no Application module was pasted), so its routes carry mounted: false. The page’s free check raises 4 flags for the fix lane (2 high, 2 medium) plus KV-00; the verdict its flags point at is blocked. The engineer’s note: “Transfers sometimes leave balances wrong, and last night every wallet ended up with the same balance. Fix the data layer. The mobile backend calls WalletRepository.findByOwner and reads the WalletEntity it returns - keep that function's signature exactly as it is this release.”

The code string, whole: 6,041 characters, 151 lines ending in a newline. Save it as wallet-paste.txt and run node build-check.js fix wallet-paste.txt > wallet-check.json:

// file: build.gradle.kts
plugins {
    kotlin("jvm") version "1.9.24"
    id("io.ktor.plugin") version "2.3.12"
    kotlin("plugin.serialization") version "1.9.24"
}

dependencies {
    implementation("io.ktor:ktor-server-core-jvm")
    implementation("io.ktor:ktor-server-auth-jvm")
    implementation("io.ktor:ktor-server-auth-jwt-jvm")
    implementation("org.jetbrains.exposed:exposed-core:0.52.0")
    implementation("org.jetbrains.exposed:exposed-dao:0.52.0")
    implementation("org.jetbrains.exposed:exposed-jdbc:0.52.0")
    implementation("org.postgresql:postgresql:42.7.3")
}
// file: src/main/kotlin/com/quillbank/wallet/Tables.kt
package com.quillbank.wallet

import org.jetbrains.exposed.dao.UUIDEntity
import org.jetbrains.exposed.dao.UUIDEntityClass
import org.jetbrains.exposed.dao.id.EntityID
import org.jetbrains.exposed.dao.id.LongIdTable
import org.jetbrains.exposed.dao.id.UUIDTable
import java.util.UUID

object Wallets : UUIDTable("wallets") {
    val owner = varchar("owner", 64).uniqueIndex()
    val balanceCents = long("balance_cents")
    val currency = varchar("currency", 3)
}

object Transfers : LongIdTable("transfers") {
    val fromWallet = reference("from_wallet", Wallets)
    val toWallet = reference("to_wallet", Wallets)
    val amountCents = long("amount_cents")
    val createdAt = long("created_at")
}

class WalletEntity(id: EntityID<UUID>) : UUIDEntity(id) {
    companion object : UUIDEntityClass<WalletEntity>(Wallets)

    var owner by Wallets.owner
    var balanceCents by Wallets.balanceCents
    var currency by Wallets.currency
}
// file: src/main/kotlin/com/quillbank/wallet/WalletRepository.kt
package com.quillbank.wallet

import kotlinx.coroutines.Dispatchers
import org.jetbrains.exposed.sql.Database
import org.jetbrains.exposed.sql.insert
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction
import org.jetbrains.exposed.sql.update
import java.util.UUID

class WalletRepository(private val db: Database) {
    suspend fun findByOwner(owner: String): WalletEntity? = newSuspendedTransaction(Dispatchers.IO, db) {
        WalletEntity.find { Wallets.owner eq owner }.firstOrNull()
    }

    suspend fun balance(id: UUID): Long? = newSuspendedTransaction(Dispatchers.IO, db) {
        WalletEntity.findById(id)?.balanceCents
    }

    suspend fun setBalance(id: UUID, cents: Long) {
        newSuspendedTransaction(Dispatchers.IO, db) {
            Wallets.update {
                it[balanceCents] = cents
            }
        }
    }

    suspend fun recordTransfer(from: UUID, to: UUID, cents: Long) {
        newSuspendedTransaction(Dispatchers.IO, db) {
            Transfers.insert {
                it[fromWallet] = from
                it[toWallet] = to
                it[amountCents] = cents
                it[createdAt] = System.currentTimeMillis()
            }
        }
    }

    suspend fun statement(owner: String, month: String): List<String> = newSuspendedTransaction(Dispatchers.IO, db) {
        val lines = mutableListOf<String>()
        exec(
            "SELECT t.amount_cents, t.created_at FROM transfers t JOIN wallets w ON w.id = t.from_wallet " +
                "WHERE w.owner = '$owner' AND to_char(to_timestamp(t.created_at / 1000), 'YYYY-MM') = '$month' ORDER BY t.created_at"
        ) { rs ->
            while (rs.next()) lines += "${rs.getLong(2)}: -${rs.getLong(1)}"
        }
        lines
    }
}
// file: src/main/kotlin/com/quillbank/wallet/TransferService.kt
package com.quillbank.wallet

import java.util.UUID

class InsufficientFunds(message: String) : IllegalArgumentException(message)

class TransferService(private val repo: WalletRepository) {
    suspend fun transfer(from: UUID, to: UUID, cents: Long) {
        require(cents > 0) { "amount must be positive" }
        require(from != to) { "cannot transfer to the same wallet" }
        val fromBalance = repo.balance(from) ?: throw IllegalArgumentException("no wallet $from")
        val toBalance = repo.balance(to) ?: throw IllegalArgumentException("no wallet $to")
        if (fromBalance < cents) throw InsufficientFunds("balance too low")
        repo.setBalance(from, fromBalance - cents)
        repo.setBalance(to, toBalance + cents)
        repo.recordTransfer(from, to, cents)
    }
}
// file: src/main/kotlin/com/quillbank/wallet/WalletRoutes.kt
package com.quillbank.wallet

import io.ktor.http.HttpStatusCode
import io.ktor.server.application.call
import io.ktor.server.auth.authenticate
import io.ktor.server.auth.jwt.JWTPrincipal
import io.ktor.server.auth.principal
import io.ktor.server.response.respond
import io.ktor.server.routing.Route
import io.ktor.server.routing.get
import io.ktor.server.routing.post
import java.util.UUID

fun Route.walletRoutes(repo: WalletRepository, transfers: TransferService) {
    authenticate("customer") {
        get("/wallet/statement") {
            val owner = call.principal<JWTPrincipal>()?.subject
                ?: return@get call.respond(HttpStatusCode.Unauthorized)
            val month = call.request.queryParameters["month"]
                ?: return@get call.respond(HttpStatusCode.BadRequest, "month is required, as YYYY-MM")
            call.respond(repo.statement(owner, month))
        }

        post("/wallet/transfers") {
            val owner = call.principal<JWTPrincipal>()?.subject
                ?: return@post call.respond(HttpStatusCode.Unauthorized)
            val wallet = repo.findByOwner(owner) ?: return@post call.respond(HttpStatusCode.NotFound)
            val to = call.request.queryParameters["to"]?.let { runCatching { UUID.fromString(it) }.getOrNull() }
                ?: return@post call.respond(HttpStatusCode.BadRequest, "to must be a wallet id")
            val cents = call.request.queryParameters["cents"]?.toLongOrNull()
                ?: return@post call.respond(HttpStatusCode.BadRequest, "cents must be a number")
            transfers.transfer(wallet.id.value, to, cents)
            call.respond(HttpStatusCode.NoContent)
        }
    }
}

Its fix-lane facts, decoded:

{
  "lane": "fix",
  "build": {
    "kotlin": "1.9.24",
    "ktor": "2.3.12",
    "exposed": "0.52.0",
    "artifacts": [
      "ktor-server-core-jvm",
      "ktor-server-auth-jvm",
      "ktor-server-auth-jwt-jvm",
      "exposed-core",
      "exposed-dao",
      "exposed-jdbc",
      "postgresql"
    ],
    "gradle_pasted": true,
    "kotlin_files": 4,
    "functions": 7,
    "suspend_funs": 6,
    "routes": 2,
    "transactions": 5,
    "module_pasted": false
  },
  "files": [
    {
      "path": "build.gradle.kts",
      "kind": "gradle",
      "line": 2,
      "end_line": 16
    },
    {
      "path": "src/main/kotlin/com/quillbank/wallet/Tables.kt",
      "kind": "kotlin",
      "line": 18,
      "end_line": 46
    },
    {
      "path": "src/main/kotlin/com/quillbank/wallet/WalletRepository.kt",
      "kind": "kotlin",
      "line": 48,
      "end_line": 95
    },
    {
      "path": "src/main/kotlin/com/quillbank/wallet/TransferService.kt",
      "kind": "kotlin",
      "line": 97,
      "end_line": 114
    },
    {
      "path": "src/main/kotlin/com/quillbank/wallet/WalletRoutes.kt",
      "kind": "kotlin",
      "line": 116,
      "end_line": 152
    }
  ],
  "functions": [
    {
      "name": "WalletRepository.findByOwner",
      "file": "src/main/kotlin/com/quillbank/wallet/WalletRepository.kt",
      "line": 58,
      "suspend": true,
      "private": false
    },
    {
      "name": "WalletRepository.balance",
      "file": "src/main/kotlin/com/quillbank/wallet/WalletRepository.kt",
      "line": 62,
      "suspend": true,
      "private": false
    },
    {
      "name": "WalletRepository.setBalance",
      "file": "src/main/kotlin/com/quillbank/wallet/WalletRepository.kt",
      "line": 66,
      "suspend": true,
      "private": false
    },
    {
      "name": "WalletRepository.recordTransfer",
      "file": "src/main/kotlin/com/quillbank/wallet/WalletRepository.kt",
      "line": 74,
      "suspend": true,
      "private": false
    },
    {
      "name": "WalletRepository.statement",
      "file": "src/main/kotlin/com/quillbank/wallet/WalletRepository.kt",
      "line": 85,
      "suspend": true,
      "private": false
    },
    {
      "name": "TransferService.transfer",
      "file": "src/main/kotlin/com/quillbank/wallet/TransferService.kt",
      "line": 104,
      "suspend": true,
      "private": false
    },
    {
      "name": "Route.walletRoutes",
      "file": "src/main/kotlin/com/quillbank/wallet/WalletRoutes.kt",
      "line": 129,
      "suspend": false,
      "private": false
    }
  ],
  "routes": [
    {
      "method": "GET",
      "path": "/wallet/statement",
      "file": "src/main/kotlin/com/quillbank/wallet/WalletRoutes.kt",
      "line": 131,
      "func": "Route.walletRoutes",
      "auth": "customer",
      "mounted": false
    },
    {
      "method": "POST",
      "path": "/wallet/transfers",
      "file": "src/main/kotlin/com/quillbank/wallet/WalletRoutes.kt",
      "line": 139,
      "func": "Route.walletRoutes",
      "auth": "customer",
      "mounted": false
    }
  ],
  "plugins": [],
  "jwt": [],
  "transactions": [
    {
      "file": "src/main/kotlin/com/quillbank/wallet/WalletRepository.kt",
      "line": 58,
      "func": "WalletRepository.findByOwner",
      "kind": "newSuspendedTransaction",
      "blocking": false,
      "coroutine": true,
      "dispatcher": "io",
      "writes": [],
      "queries": [
        "WalletEntity.find"
      ]
    },
    {
      "file": "src/main/kotlin/com/quillbank/wallet/WalletRepository.kt",
      "line": 62,
      "func": "WalletRepository.balance",
      "kind": "newSuspendedTransaction",
      "blocking": false,
      "coroutine": true,
      "dispatcher": "io",
      "writes": [],
      "queries": [
        "WalletEntity.findById"
      ]
    },
    {
      "file": "src/main/kotlin/com/quillbank/wallet/WalletRepository.kt",
      "line": 67,
      "func": "WalletRepository.setBalance",
      "kind": "newSuspendedTransaction",
      "blocking": false,
      "coroutine": true,
      "dispatcher": "io",
      "writes": [
        "update"
      ],
      "queries": []
    },
    {
      "file": "src/main/kotlin/com/quillbank/wallet/WalletRepository.kt",
      "line": 75,
      "func": "WalletRepository.recordTransfer",
      "kind": "newSuspendedTransaction",
      "blocking": false,
      "coroutine": true,
      "dispatcher": "io",
      "writes": [
        "insert"
      ],
      "queries": []
    },
    {
      "file": "src/main/kotlin/com/quillbank/wallet/WalletRepository.kt",
      "line": 85,
      "func": "WalletRepository.statement",
      "kind": "newSuspendedTransaction",
      "blocking": false,
      "coroutine": true,
      "dispatcher": "io",
      "writes": [],
      "queries": []
    }
  ],
  "tables": [
    {
      "name": "Wallets",
      "base": "UUIDTable",
      "sql_name": "wallets",
      "file": "src/main/kotlin/com/quillbank/wallet/Tables.kt",
      "line": 27
    },
    {
      "name": "Transfers",
      "base": "LongIdTable",
      "sql_name": "transfers",
      "file": "src/main/kotlin/com/quillbank/wallet/Tables.kt",
      "line": 33
    }
  ],
  "entities": [
    {
      "name": "WalletEntity",
      "base": "UUIDEntity",
      "file": "src/main/kotlin/com/quillbank/wallet/Tables.kt",
      "line": 40
    }
  ],
  "flags": [
    {
      "uid": "F1",
      "id": "EX-09",
      "sev": "high",
      "file": "src/main/kotlin/com/quillbank/wallet/WalletRepository.kt",
      "line": 68,
      "text": "the newSuspendedTransaction { } in WalletRepository.setBalance calls Wallets.update { } with no where condition, which changes every row of the table",
      "detail": "Table.update's where is optional, and without it Exposed writes UPDATE ... SET with no WHERE: every row of Wallets gets these values at once. Pass the condition first: Wallets.update({ Wallets.id eq id }) { ... } (or .update(where = { ... })), and check the returned row count."
    },
    {
      "uid": "F2",
      "id": "EX-02",
      "sev": "high",
      "file": "src/main/kotlin/com/quillbank/wallet/WalletRepository.kt",
      "line": 87,
      "text": "the newSuspendedTransaction { } in WalletRepository.statement builds the SQL for exec(...) from a string template ($owner and $month)",
      "detail": "Whatever is spliced into the SQL text is executed as SQL: a value such as ' OR '1'='1 or '; DROP TABLE ... turns into part of the statement (SQL injection), and even trusted values break on a quote. Build the query with Exposed's DSL (Table.selectAll().where { col eq value }), or keep raw SQL and pass the values as arguments - exec(sql, listOf(column.columnType to value)) in Exposed - never inside the text."
    },
    {
      "uid": "F3",
      "id": "EX-05",
      "sev": "medium",
      "file": "src/main/kotlin/com/quillbank/wallet/WalletRepository.kt",
      "line": 58,
      "text": "WalletRepository.findByOwner returns WalletEntity? - a DAO entity - out of the transaction that loaded it",
      "detail": "An Exposed entity stays tied to the transaction that read it: reading a reference (referencedOn, referrersOn) after the transaction closed throws IllegalStateException (no transaction in context), and a property changed after it closed is not written by that transaction. Callers that get the entity cannot tell which of its members are safe to touch. Map the entity to a plain data class inside the transaction (toModel()) and return that."
    },
    {
      "uid": "F4",
      "id": "EX-04",
      "sev": "medium",
      "file": "src/main/kotlin/com/quillbank/wallet/TransferService.kt",
      "line": 107,
      "text": "TransferService.transfer calls balance(), setBalance() and recordTransfer(), and each opens its own transaction",
      "detail": "Every call commits on its own, so TransferService.transfer is not atomic: a failure between the calls leaves the first one's write in place, and a concurrent request can act between them on what the first one read. Move the whole operation into one repository fun whose single newSuspendedTransaction { } does every read and write (repository funs that each open their own newSuspendedTransaction start separate transactions), and lock what it reads - forUpdate(), or a conditional UPDATE whose row count is checked - so a concurrent request cannot act in between.",
      "lines": [
        107,
        108,
        110,
        111,
        112
      ]
    },
    {
      "uid": "F5",
      "id": "KV-00",
      "sev": "info",
      "file": "src/main/kotlin/com/quillbank/wallet/Tables.kt",
      "line": 18,
      "text": "context: 4 Kotlin files, 7 fun(s) (6 suspend), 2 route(s) (2 in Route functions not mounted in the paste), 0 plugin install(s), 5 Exposed transaction(s), 2 table(s), 1 entity; Ktor 2.3.12; Exposed 0.52.0",
      "detail": "No Application module with install(...) calls was pasted, so the missing-plugin checks (KT-09, KT-10) did not run. Only what is pasted is checked: routes in Route functions that are never mounted in the paste get their path and authenticate block from nowhere (their auth is not judged), what a fun in another file does is unknown, and the database's own indexes and constraints are outside the check unless the migrations are pasted."
    }
  ],
  "verdict_candidate": "blocked",
  "notes": []
}

The request body (Idempotency-Key: routrel-audit:fix:5820c181-391b:a1):

{
  "task": "fix",
  "code": "// file: build.gradle.kts\nplugins {\n    kotlin(\"jvm\") version \"1.9.24\"\n    id(\"io.ktor.plugin\") version \"2.3.12\"\n...",
  "facts": "{\"lane\":\"fix\",\"build\":{\"kotlin\":\"1.9.24\",\"ktor\":\"2.3.12\",\"exposed\":\"0.52.0\",\"artifacts\":[\"ktor-server-core-jvm\",\"ktor-server-auth-jvm\",\"ktor-server-auth-jwt-jvm\",\"exposed...",
  "note": "Transfers sometimes leave balances wrong, and last night every wallet ended up with the same balance. Fix the data layer. The mobile backend calls WalletRepository.findByOwner and reads the WalletEntity it returns - keep that function's signature exactly as it is this release."
}

The reply (output.output), pretty-printed; each file’s content is cut after its first lines here and shown whole below:

{
  "task": "fix",
  "verdict": "partial",
  "headline": "Fixed the where-less update that wiped every wallet, parameterized the injectable statement query, and made transfers atomic; findByOwner's signature stays as the note requires, so its transaction-escaping entity remains open.",
  "files": [
    {
      "path": "src/main/kotlin/com/quillbank/wallet/WalletRepository.kt",
      "content": "package com.quillbank.wallet\n\nimport kotlinx.coroutines.Dispatchers\n...  (3,526 characters, shown whole below)"
    },
    {
      "path": "src/main/kotlin/com/quillbank/wallet/TransferService.kt",
      "content": "package com.quillbank.wallet\n\nimport java.util.UUID\n...  (433 characters, shown whole below)"
    }
  ],
  "changes": [
    {
      "file": "src/main/kotlin/com/quillbank/wallet/WalletRepository.kt",
      "function": "WalletRepository.setBalance",
      "flags": [
        "F1"
      ],
      "change": "Added a where clause (Wallets.id eq id) to the update, so it only touches the targeted wallet.",
      "why": "Prevents a single balance write from silently setting every wallet in the table to the same balance."
    },
    {
      "file": "src/main/kotlin/com/quillbank/wallet/WalletRepository.kt",
      "function": "WalletRepository.statement",
      "flags": [
        "F2"
      ],
      "change": "Replaced the string-templated owner and month values with ? placeholders and passed them as exec arguments.",
      "why": "Stops a wallet owner id or month value from being interpreted as part of the SQL statement."
    },
    {
      "file": "src/main/kotlin/com/quillbank/wallet/WalletRepository.kt",
      "function": "WalletRepository.transferBalance",
      "flags": [],
      "change": "Added a repository function that locks both wallets in a fixed id order, debits the source with a conditional update whose row count is checked, credits the destination, and inserts the transfer record, all inside one newSuspendedTransaction.",
      "why": "Gives the whole transfer a single atomic, row-locked unit so a concurrent transfer cannot act on a balance the first transfer is still writing, and two transfers between the same two wallets cannot deadlock."
    },
    {
      "file": "src/main/kotlin/com/quillbank/wallet/TransferService.kt",
      "function": "TransferService.transfer",
      "flags": [
        "F4"
      ],
      "change": "Replaced the separate balance(), balance(), setBalance(), setBalance() and recordTransfer() calls with one call to WalletRepository.transferBalance.",
      "why": "The old code committed each read and write as its own transaction, so a concurrent transfer or a mid-way failure could leave both wallets' balances wrong; one transaction makes the debit, credit and record all-or-nothing."
    }
  ],
  "verify": [
    "./gradlew build",
    "A repository test against a real Postgres (Testcontainers): create two wallets, run transferBalance concurrently twice from the same source wallet with just enough balance for one transfer - exactly one call must succeed and the other must throw InsufficientFunds, and the source wallet's final balance must reflect only the one debit.",
    "A repository test that calls setBalance on one wallet among several and asserts only that wallet's row changed.",
    "A repository test that calls statement() with an owner or month value containing a quote (e.g. \"' OR '1'='1\") and asserts it is treated as a literal value, not as part of the query."
  ],
  "open": [
    {
      "item": "WalletRepository.findByOwner still returns the WalletEntity itself after its transaction has closed.",
      "flags": [
        "F3"
      ],
      "reason": "The note requires this function's signature to stay exactly as it is because the mobile backend calls it and reads the returned WalletEntity; mapping it to a plain data class (toModel()) would change that return type, so the fix is left out this release. Callers must only read owner, balanceCents and currency - properties already loaded before the transaction closed - and must never touch a lazy relation after the call returns."
    }
  ],
  "questions": [
    "Does the mobile backend read anything from the WalletEntity that findByOwner returns besides owner, balanceCents and currency, or read it after the call has returned rather than immediately? Any such access throws IllegalStateException once the transaction is closed."
  ],
  "summary": "Fixed the two high findings: setBalance's where-less update, which is what wiped every wallet to the same balance, and the string-templated SQL in statement(), which let an owner id or month value run as part of the query. Added WalletRepository.transferBalance so a transfer's balance check, debit, credit and record happen as one atomic, row-locked operation instead of three separate transactions, which is what let concurrent transfers leave balances wrong; TransferService.transfer now calls it and keeps its own signature and exceptions unchanged. findByOwner's signature is left exactly as it was per the note, so the DAO entity it returns still outlives its transaction - that risk is listed in open with a note on what is safe to read from it."
}

The returned src/main/kotlin/com/quillbank/wallet/WalletRepository.kt, whole (3,526 characters):

package com.quillbank.wallet

import kotlinx.coroutines.Dispatchers
import org.jetbrains.exposed.sql.Database
import org.jetbrains.exposed.sql.VarCharColumnType
import org.jetbrains.exposed.sql.and
import org.jetbrains.exposed.sql.insert
import org.jetbrains.exposed.sql.selectAll
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction
import org.jetbrains.exposed.sql.update
import java.util.UUID

class WalletRepository(private val db: Database) {
    suspend fun findByOwner(owner: String): WalletEntity? = newSuspendedTransaction(Dispatchers.IO, db) {
        WalletEntity.find { Wallets.owner eq owner }.firstOrNull()
    }

    suspend fun balance(id: UUID): Long? = newSuspendedTransaction(Dispatchers.IO, db) {
        WalletEntity.findById(id)?.balanceCents
    }

    suspend fun setBalance(id: UUID, cents: Long) {
        newSuspendedTransaction(Dispatchers.IO, db) {
            Wallets.update({ Wallets.id eq id }) {
                it[balanceCents] = cents
            }
        }
    }

    suspend fun recordTransfer(from: UUID, to: UUID, cents: Long) {
        newSuspendedTransaction(Dispatchers.IO, db) {
            Transfers.insert {
                it[fromWallet] = from
                it[toWallet] = to
                it[amountCents] = cents
                it[createdAt] = System.currentTimeMillis()
            }
        }
    }

    suspend fun transferBalance(from: UUID, to: UUID, cents: Long) {
        newSuspendedTransaction(Dispatchers.IO, db) {
            val firstId = if (from.toString() <= to.toString()) from else to
            val secondId = if (firstId == from) to else from
            val firstRow = Wallets.selectAll().where { Wallets.id eq firstId }.forUpdate().singleOrNull()
            val secondRow = Wallets.selectAll().where { Wallets.id eq secondId }.forUpdate().singleOrNull()
            val rows = mapOf(firstId to firstRow, secondId to secondRow)
            val fromRow = rows[from] ?: throw IllegalArgumentException("no wallet $from")
            val toRow = rows[to] ?: throw IllegalArgumentException("no wallet $to")
            val fromBalance = fromRow[Wallets.balanceCents]
            val toBalance = toRow[Wallets.balanceCents]
            if (fromBalance < cents) throw InsufficientFunds("balance too low")
            val debited = Wallets.update({ (Wallets.id eq from) and (Wallets.balanceCents greaterEq cents) }) {
                it[balanceCents] = fromBalance - cents
            }
            if (debited != 1) throw InsufficientFunds("balance too low")
            Wallets.update({ Wallets.id eq to }) {
                it[balanceCents] = toBalance + cents
            }
            Transfers.insert {
                it[fromWallet] = from
                it[toWallet] = to
                it[amountCents] = cents
                it[createdAt] = System.currentTimeMillis()
            }
        }
    }

    suspend fun statement(owner: String, month: String): List<String> = newSuspendedTransaction(Dispatchers.IO, db) {
        val lines = mutableListOf<String>()
        exec(
            "SELECT t.amount_cents, t.created_at FROM transfers t JOIN wallets w ON w.id = t.from_wallet " +
                "WHERE w.owner = ? AND to_char(to_timestamp(t.created_at / 1000), 'YYYY-MM') = ? ORDER BY t.created_at",
            listOf(Wallets.owner.columnType to owner, VarCharColumnType() to month)
        ) { rs ->
            while (rs.next()) lines += "${rs.getLong(2)}: -${rs.getLong(1)}"
        }
        lines
    }
}

The returned src/main/kotlin/com/quillbank/wallet/TransferService.kt, whole (433 characters):

package com.quillbank.wallet

import java.util.UUID

class InsufficientFunds(message: String) : IllegalArgumentException(message)

class TransferService(private val repo: WalletRepository) {
    suspend fun transfer(from: UUID, to: UUID, cents: Long) {
        require(cents > 0) { "amount must be positive" }
        require(from != to) { "cannot transfer to the same wallet" }
        repo.transferBalance(from, to, cents)
    }
}

The verdict is partial because the note asked to keep WalletRepository.findByOwner’s signature this release, so the medium F3 (EX-05, the entity returned out of its transaction) is named in open rather than fixed. The other three flags are closed by changes: F1 by the where clause in setBalance, F2 by ? placeholders with the values passed as exec arguments in statement, and F4 by TransferService.transfer calling the new WalletRepository.transferBalance (itself listed as a change with flags: []), which does the reads, the conditional debit, the credit and the insert in one newSuspendedTransaction, locking both rows with forUpdate() in a fixed order. Every fun of the paste is still there with its suspend kept (balance, setBalance and recordTransfer are unchanged or only given a where), the new fun is an addition, and Tables.kt, WalletRoutes.kt and the build file are not returned because they did not change.

The page re-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 free check 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.

recon.js also runs in Node, so you can hold a reply to exactly the page’s standard. Save /ktorkit.js and /recon.js next to this script, pass the file you built the body from (build-check.js or handoff.js output, which carries the lane as task) and the reply text; it prints every statement, the count, and the paste with every edit (or file) put back:

// recheck.js -- run: node recheck.js wallet-check.json wallet-reply.txt > wallet-fixed.txt   (ktorkit.js, recon.js next to it)
const fs = require("fs");
const Recon = require("./recon.js");                                             // loads ./ktorkit.js itself
const sent = JSON.parse(fs.readFileSync(process.argv[2], "utf8"));              // task, code, facts (and audit_handoff) you sent
const res = Recon.normalize(Recon.parseResult(fs.readFileSync(process.argv[3], "utf8")), sent.task);  // 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

Each statement is printed as ok:, warn:, bad: (a disagreement) or info:. For the wallet fix reply, the one thing the reply itself says is left is F3: findByOwner keeps returning the WalletEntity, as the note asked, so expect it to be reported as left open by the reply rather than as a disagreement.

The page does not compile or run the code. After that, run the build (./gradlew build) and the tests the reply’s plan or verify names: testApplication { } tests for every route (no token gives 401, an unknown id 404, a bad id 400, no exception text in the error body) and repository tests against a real Postgres (Testcontainers, or a disposable database in CI) — two concurrent operations on the same row, one of which must fail or wait; an update that touches exactly one row — 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 (a personal token from /tokens.html, step 2), 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}'

Step 2 — a token

Two kinds work:

# 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":"routrel-audit"}' | jq -r '.data.token')
echo "guest token: ${TOKEN:0:12}..."

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'

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 (required), 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; that file’s facts.lane must be the task you pass, since the facts are cut per lane. The samples build the server input of the bookings example from bookings-check.json; step 5 runs it, and step 6 reuses makeInput for the wallet (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:

FieldMeaning
hold_creditsA 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 server 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_creditsThe 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_enabledWhen true, a caller who is not signed in may run; otherwise the page asks them to sign in.
warningsInput 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.

# bookings-check.json: printed by `node build-check.js server bookings-paste.txt` (see "The facts string").
# (.facts | tojson) turns the facts into ONE compact string, as JSON.stringify does; build the file for this lane.
BODY=$(jq -c --arg note "Release candidate for the clinic's booking API. Anything to fix before it goes out?" \
  '{task: "server", code, facts: (.facts | tojson), note: $note}' bookings-check.json)
# facts is required. No page check at hand? "{}" 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})'

Step 5 — POST /run, then poll the job (server 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:

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 a server audit, a data audit and a fix run of the same paste never share a key. The page uses routrel-audit:<lane>:<inputHash>:a<attempt> (app.js run), where <lane> is server, data 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 three worked inputs above that is routrel-audit:server:e7ea62e2-3b88:a1 (bookings server audit), routrel-audit:data:1ad72fe8-3b84:a1 (bookings data audit) and routrel-audit:fix:5820c181-391b:a1 (wallet fix). 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. For the data audit, run the same code with node build-check.js data bookings-paste.txt > bookings-data-check.json, makeInput("data", "bookings-data-check.json", note) and a key that starts routrel-audit:data:; the reply has the same shape, with the three data areas (transactions, queries, setup).

KEY="routrel-audit:server:$(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}]}'

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 another lane, the samples build the fix input of the wallet example (with its own key) and stream it; to stream the server (or data) 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:

EventDataWhat the SDK does
delta{text}Passes text to onDelta.
jobjob infoPasses it to onJob (the page uses it to advance its progress display).
donethe final payload: {job_id, status, charged_credits, output}Keeps it as the result.
pendinga payloadTreated 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 wallet-check.json: makeInput then carries its audit_handoff.

# the fix worked example: wallet-check.json from `node build-check.js fix wallet-paste.txt`
# (for audited code, read handoff.js's output instead; the + (...) adds its audit_handoff)
FBODY=$(jq -c --arg note "Transfers sometimes leave balances wrong, and last night every wallet ended up with the same balance. Fix the data layer. The mobile backend calls WalletRepository.findByOwner and reads the WalletEntity it returns - keep that function's signature exactly as it is this release." \
  '{task: "fix", code, facts: (.facts | tojson), note: $note} + (if .audit_handoff then {audit_handoff} else {} end)' \
  wallet-check.json)
FKEY="routrel-audit: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}'

What the page does that you now do

Or skip all of this and use Routrel Audit in the browser: the check, the flags, the route, plugin and transaction inventories, putting an edit or the fixed files back into the paste, and the Markdown and CSV exports are free.