gitoria

kunibert

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitcd4cb09ccd4cb09cNo review and status error when the model's tools did not run (kunibert#11)Kunibertcd4cb09c/lib/run-job.hl

6.0 KB

  1. import { appendFile, exists, writeFile, mkDir, tryReadFile } from 'hl:fs'
  2. import { timestamp } from 'hl:time'
  3. import { run, spawnArgs, dir } from 'hl:proc'
  4. import { after } from 'hl:time'
  5. \\ THE ONE FUNCTION the queue calls: runJob(job, done). It starts the review CLI (tools/runner.hl review-pr ...) as a CHILD with
  6. \\ spawnArgs and returns at once; `done(result)` is called later, from the child's exit event (or the timeout), with
  7. \\ { ok, error, out }. Nothing here blocks the web routes while the model runs.
  8. \\ Legacy test mode (no repos configured, K5 phases): the job is only recorded in <dataDir>/runjobs.jsonl; hooks
  9. \\ <dataDir>/fail-<key, # as _> (fails) and <dataDir>/slow-<key> (3 s, blocking) as before.
  10. String dataDir = ''
  11. Hybrid repos = null \\ repo.id -> { api, clone, keyFile }; null = legacy test mode
  12. String hybriel = 'hybriel' \\ the runtime binary
  13. Number timeoutSeconds = 900 \\ hard limit per job: past it the child is killed, the job failed
  14. String runnerData = '' \\ the CLI's own state dir (sessions, git cache)
  15. String ticketsBase = '' \\ tickets service base URL; '' = linked tickets are not read
  16. String reviewUser = '' \\ unix user the model runs as ('' = the service's own user)
  17. String codexBin = 'codex' \\ the codex binary (an absolute path when reviewUser is set)
  18. String workDir = '' \\ job worktrees ('' = the CLI default under runnerData)
  19. String ticketsTokenFile = '' \\ read token file of Kunibert for it (mode 600); only the PATH goes into the argv
  20. \\ the argv of the review child; the key file path is the only secret-related thing in it
  21. argvFor = (Hybrid job, Hybrid cfg) => {
  22. tool = dir() + '/../tools/runner.hl'
  23. let a = [hybriel, tool, 'review-pr', runnerData, '--api', cfg.api, '--key-file', cfg.keyFile, '--clone', cfg.clone]
  24. if (ticketsBase != '' && ticketsTokenFile != '') { a.push('--tickets-base') a.push(ticketsBase) a.push('--tickets-token-file') a.push(ticketsTokenFile) }
  25. if (reviewUser != '') { a.push('--review-user') a.push(reviewUser) a.push('--codex-bin') a.push(codexBin) }
  26. if (workDir != '') { a.push('--work-dir') a.push(workDir) }
  27. if (job.force || job.reset || job.notes.length > 0) { a.push('--force') }
  28. if (job.reset) { a.push('--reset') }
  29. if (job.notes.length > 0) {
  30. mkDir(dataDir + '/notes')
  31. nf = dataDir + '/notes/' + job.key.replace('#', '_') + '.json'
  32. writeFile(nf, JSON.stringify(job.notes))
  33. a.push('--note-file')
  34. a.push(nf)
  35. }
  36. a.push('' + job.number)
  37. return a
  38. }
  39. cut = (String t) => {
  40. if (t.length <= 300) { return t }
  41. return t.slice(0, 300) + '…'
  42. }
  43. \\ run a child to its end or its timeout; cb({ exit, killed, out, err })
  44. runChild = (Hybrid argv, Number seconds, cb) => {
  45. let outLines = []
  46. let errLines = []
  47. let killed = false
  48. child = spawnArgs(argv)
  49. timer = after(seconds)
  50. on child.line(e) { if (e.stream == 'stdout') { outLines.push(e.line) } else { errLines.push(e.line) } }
  51. on child.exit(e) {
  52. timer.stop()
  53. cb({ exit = e.exit, =killed, out = outLines, err = errLines })
  54. }
  55. on timer.tick() {
  56. killed = true
  57. child.kill()
  58. }
  59. return child.pid
  60. }
  61. \\ the JSON the CLI printed last (stdout), or null
  62. lastJson = (Hybrid lines) => {
  63. for (let i = lines.length - 1; i >= 0; i = i - 1) {
  64. if (lines[i].trim() != '') {
  65. if (!lines[i].trim().startsWith('{')) { return null }
  66. return JSON.parse(lines[i])
  67. }
  68. }
  69. return null
  70. }
  71. runJob = (Hybrid job, done) => {
  72. if (repos == null) { return legacy(job, done) }
  73. cfg = repos['' + job.repo.id]
  74. if (cfg == null) { return done({ ok = false, error = 'no config for repo ' + job.repo.id }) }
  75. argv = argvFor(job, cfg)
  76. console.log(timestamp() + ' runjob: ' + job.key + ' start timeout=' + timeoutSeconds + 's force=' + job.force + ' reset=' + job.reset + ' notes=' + job.notes.length)
  77. runChild(argv, timeoutSeconds, (r) => {
  78. if (r.killed) {
  79. \\ the killed CLI cannot tell gitoria: do it now (short child), then report the failure
  80. msg = 'Review timed out after ' + timeoutSeconds + 's'
  81. runChild([hybriel, dir() + '/../tools/runner.hl', 'status-error', '--api', cfg.api, '--key-file', cfg.keyFile, '--head', job.head, '--message', msg], 60, (s) => {
  82. console.log(timestamp() + ' runjob: ' + job.key + ' KILLED at timeout, error status exit=' + s.exit)
  83. done({ ok = false, error = msg })
  84. })
  85. return null
  86. }
  87. j = lastJson(r.out)
  88. if (r.exit == 0 && (j == null || j.error == null)) {
  89. done({ ok = true, out = j })
  90. return null
  91. }
  92. let err = 'review child exit ' + r.exit
  93. if (j != null && j.error != null) { err = '' + j.error }
  94. else if (r.err.length > 0) { err = err + ': ' + r.err[r.err.length - 1] }
  95. done({ ok = false, error = cut(err) })
  96. return null
  97. })
  98. return null
  99. }
  100. \\ closed/merged PR: remove its session (state) via the CLI; done({ ok })
  101. dropSession = (Hybrid mark, String number, done) => {
  102. let cfg = null
  103. if (repos != null) { cfg = repos['' + mark.repo.id] }
  104. if (cfg == null) { return done({ ok = false, error = 'no config for repo ' + mark.repo.id }) }
  105. runChild([hybriel, dir() + '/../tools/runner.hl', 'drop', runnerData, cfg.clone, number], 60, (r) => {
  106. done({ ok = r.exit == 0 && !r.killed, error = r.err.join(' ') })
  107. return null
  108. })
  109. return null
  110. }
  111. legacy = (Hybrid job, done) => {
  112. 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 }) + '
  113. ')
  114. slowFile = dataDir + '/slow-' + job.key.replace('#', '_')
  115. if (exists(slowFile)) { run('sleep 3') }
  116. failFile = dataDir + '/fail-' + job.key.replace('#', '_')
  117. if (exists(failFile)) { return done({ ok = false, error = 'test failure hook' }) }
  118. return done({ ok = true })
  119. }

Branches

Latest commits

  • 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
  • c9b48bfeVendor Hybriel plugins; deploy ships bin/ from zig-outKunibert
  • a41ba15fKunibert: event intake, git cache, review runner, posting, linked tickets, deploy filesKunibert