kunibert
All repositories: gitoria
6.0 KB
import { appendFile, exists, writeFile, mkDir, tryReadFile } from 'hl:fs'import { timestamp } from 'hl:time'import { run, spawnArgs, dir } from 'hl:proc'import { after } from 'hl:time'\\ THE ONE FUNCTION the queue calls: runJob(job, done). It starts the review CLI (tools/runner.hl review-pr ...) as a CHILD with\\ spawnArgs and returns at once; `done(result)` is called later, from the child's exit event (or the timeout), with\\ { ok, error, out }. Nothing here blocks the web routes while the model runs.\\ Legacy test mode (no repos configured, K5 phases): the job is only recorded in <dataDir>/runjobs.jsonl; hooks\\ <dataDir>/fail-<key, # as _> (fails) and <dataDir>/slow-<key> (3 s, blocking) as before.String dataDir = ''Hybrid repos = null \\ repo.id -> { api, clone, keyFile }; null = legacy test modeString hybriel = 'hybriel' \\ the runtime binaryNumber timeoutSeconds = 900 \\ hard limit per job: past it the child is killed, the job failedString runnerData = '' \\ the CLI's own state dir (sessions, git cache)String ticketsBase = '' \\ tickets service base URL; '' = linked tickets are not readString reviewUser = '' \\ unix user the model runs as ('' = the service's own user)String codexBin = 'codex' \\ the codex binary (an absolute path when reviewUser is set)String workDir = '' \\ job worktrees ('' = the CLI default under runnerData)String ticketsTokenFile = '' \\ read token file of Kunibert for it (mode 600); only the PATH goes into the argv\\ the argv of the review child; the key file path is the only secret-related thing in itargvFor = (Hybrid job, Hybrid cfg) => {tool = dir() + '/../tools/runner.hl'let a = [hybriel, tool, 'review-pr', runnerData, '--api', cfg.api, '--key-file', cfg.keyFile, '--clone', cfg.clone]if (ticketsBase != '' && ticketsTokenFile != '') { a.push('--tickets-base') a.push(ticketsBase) a.push('--tickets-token-file') a.push(ticketsTokenFile) }if (reviewUser != '') { a.push('--review-user') a.push(reviewUser) a.push('--codex-bin') a.push(codexBin) }if (workDir != '') { a.push('--work-dir') a.push(workDir) }if (job.force || job.reset || job.notes.length > 0) { a.push('--force') }if (job.reset) { a.push('--reset') }if (job.notes.length > 0) {mkDir(dataDir + '/notes')nf = dataDir + '/notes/' + job.key.replace('#', '_') + '.json'writeFile(nf, JSON.stringify(job.notes))a.push('--note-file')a.push(nf)}a.push('' + job.number)return a}cut = (String t) => {if (t.length <= 300) { return t }return t.slice(0, 300) + '…'}\\ run a child to its end or its timeout; cb({ exit, killed, out, err })runChild = (Hybrid argv, Number seconds, cb) => {let outLines = []let errLines = []let killed = falsechild = spawnArgs(argv)timer = after(seconds)on child.line(e) { if (e.stream == 'stdout') { outLines.push(e.line) } else { errLines.push(e.line) } }on child.exit(e) {timer.stop()cb({ exit = e.exit, =killed, out = outLines, err = errLines })}on timer.tick() {killed = truechild.kill()}return child.pid}\\ the JSON the CLI printed last (stdout), or nulllastJson = (Hybrid lines) => {for (let i = lines.length - 1; i >= 0; i = i - 1) {if (lines[i].trim() != '') {if (!lines[i].trim().startsWith('{')) { return null }return JSON.parse(lines[i])}}return null}runJob = (Hybrid job, done) => {if (repos == null) { return legacy(job, done) }cfg = repos['' + job.repo.id]if (cfg == null) { return done({ ok = false, error = 'no config for repo ' + job.repo.id }) }argv = argvFor(job, cfg)console.log(timestamp() + ' runjob: ' + job.key + ' start timeout=' + timeoutSeconds + 's force=' + job.force + ' reset=' + job.reset + ' notes=' + job.notes.length)runChild(argv, timeoutSeconds, (r) => {if (r.killed) {\\ the killed CLI cannot tell gitoria: do it now (short child), then report the failuremsg = 'Review timed out after ' + timeoutSeconds + 's'runChild([hybriel, dir() + '/../tools/runner.hl', 'status-error', '--api', cfg.api, '--key-file', cfg.keyFile, '--head', job.head, '--message', msg], 60, (s) => {console.log(timestamp() + ' runjob: ' + job.key + ' KILLED at timeout, error status exit=' + s.exit)done({ ok = false, error = msg })})return null}j = lastJson(r.out)if (r.exit == 0 && (j == null || j.error == null)) {done({ ok = true, out = j })return null}let err = 'review child exit ' + r.exitif (j != null && j.error != null) { err = '' + j.error }else if (r.err.length > 0) { err = err + ': ' + r.err[r.err.length - 1] }done({ ok = false, error = cut(err) })return null})return null}\\ closed/merged PR: remove its session (state) via the CLI; done({ ok })dropSession = (Hybrid mark, String number, done) => {let cfg = nullif (repos != null) { cfg = repos['' + mark.repo.id] }if (cfg == null) { return done({ ok = false, error = 'no config for repo ' + mark.repo.id }) }runChild([hybriel, dir() + '/../tools/runner.hl', 'drop', runnerData, cfg.clone, number], 60, (r) => {done({ ok = r.exit == 0 && !r.killed, error = r.err.join(' ') })return null})return null}legacy = (Hybrid job, done) => {appendFile(dataDir + '/runjobs.jsonl', JSON.stringify({ at = timestamp(), key = job.key, head = job.head, force = job.force, reset = job.reset, threadReplyOnly = job.threadReplyOnly, threads = job.threads.length, notes = job.notes, events = job.events, tries = job.tries }) + '')slowFile = dataDir + '/slow-' + job.key.replace('#', '_')if (exists(slowFile)) { run('sleep 3') }failFile = dataDir + '/fail-' + job.key.replace('#', '_')if (exists(failFile)) { return done({ ok = false, error = 'test failure hook' }) }return done({ ok = true })}
Branches
Latest commits
- 44af321bRecords: contract v1.4mre
- 310f3fd0Records: outdated semanticsmre
- 268ebfe6Records: #11 proven live and closedmre
- 88e8aad3Records: K11 verified and deployedmre
- cd4cb09cNo review and status error when the model's tools did not run (kunibert#11)Kunibert
- a6b54efaStatus updatemre
- 5a72268eRecords: #4-#10 done, #11 started, review package on #1mre
- 194c7da6Records: real proof greenmre
- cdfa94a9Records: AppArmor profile for the review sandboxmre
- c1bf02d8Status: sandbox blockermre
- 88aaacc5Records: real proof part 2 (posting works, sandbox blocked by AppArmor userns)mre
- 401df4e8Records: review user logged inmre
- 6ca681b3Records: login command, status cleanupmre
- 6b8d763cRecords: review user, device login startedKunibert
- 07025b99Open the job worktree read-only for the review user before each model runKunibert
- 2a44bd54Runner CLI: --review-user/--codex-bin/--work-dir work in every modeKunibert
- 2177f66fReview model runs as its own unix user (reviewUser, codexBin, workDir)Kunibert
- 35df8306Records: live service on byrodin, real proof part 1Kunibert
- 7480a98dkunibert.service: SIGTERM stop is a clean exitKunibert
- 3a8d93f9gitoria client: parse only JSON answers, accept a plain-text errorKunibert