gitoria

kunibert

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit8d34ca338d34ca33records: repo rename (gitoria#53)mre8d34ca33/lib/run-job.hl

6.2 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) }
  26. a.push('--codex-bin')
  27. a.push(codexBin)
  28. if (workDir != '') { a.push('--work-dir') a.push(workDir) }
  29. if (cfg.vendored != null && cfg.vendored.length > 0) { a.push('--vendored') a.push(cfg.vendored.join(',')) }
  30. if (cfg.vendoredLink != null) { a.push('--vendored-link') a.push(cfg.vendoredLink) }
  31. if (job.force || job.reset || job.notes.length > 0) { a.push('--force') }
  32. if (job.reset) { a.push('--reset') }
  33. if (job.notes.length > 0) {
  34. mkDir(dataDir + '/notes')
  35. nf = dataDir + '/notes/' + encodeURIComponent(job.key) + '.json'
  36. writeFile(nf, JSON.stringify(job.notes))
  37. a.push('--note-file')
  38. a.push(nf)
  39. }
  40. a.push('' + job.number)
  41. return a
  42. }
  43. cut = (String t) => {
  44. if (t.length <= 300) { return t }
  45. return t.slice(0, 300) + '…'
  46. }
  47. \\ run a child to its end or its timeout; cb({ exit, killed, out, err })
  48. runChild = (Hybrid argv, Number seconds, cb) => {
  49. let outLines = []
  50. let errLines = []
  51. let killed = false
  52. child = spawnArgs(argv)
  53. timer = after(seconds)
  54. on child.line(e) { if (e.stream == 'stdout') { outLines.push(e.line) } else { errLines.push(e.line) } }
  55. on child.exit(e) {
  56. timer.stop()
  57. cb({ exit = e.exit, =killed, out = outLines, err = errLines })
  58. }
  59. on timer.tick() {
  60. killed = true
  61. child.kill()
  62. }
  63. return child.pid
  64. }
  65. \\ the JSON the CLI printed last (stdout), or null
  66. lastJson = (Hybrid lines) => {
  67. for (let i = lines.length - 1; i >= 0; i = i - 1) {
  68. if (lines[i].trim() != '') {
  69. if (!lines[i].trim().startsWith('{')) { return null }
  70. return JSON.parse(lines[i])
  71. }
  72. }
  73. return null
  74. }
  75. runJob = (Hybrid job, done) => {
  76. if (repos == null) { return legacy(job, done) }
  77. cfg = repos['' + job.repo.id]
  78. if (cfg == null) { return done({ ok = false, error = 'no config for repo ' + job.repo.id }) }
  79. argv = argvFor(job, cfg)
  80. console.log(timestamp() + ' runjob: ' + job.key + ' start timeout=' + timeoutSeconds + 's force=' + job.force + ' reset=' + job.reset + ' notes=' + job.notes.length)
  81. runChild(argv, timeoutSeconds, (r) => {
  82. if (r.killed) {
  83. \\ the killed CLI cannot tell gitoria: do it now (short child), then report the failure
  84. msg = 'Review timed out after ' + timeoutSeconds + 's'
  85. runChild([hybriel, dir() + '/../tools/runner.hl', 'status-error', '--api', cfg.api, '--key-file', cfg.keyFile, '--head', job.head, '--message', msg], 60, (s) => {
  86. console.log(timestamp() + ' runjob: ' + job.key + ' KILLED at timeout, error status exit=' + s.exit)
  87. done({ ok = false, error = msg })
  88. })
  89. return null
  90. }
  91. j = lastJson(r.out)
  92. if (r.exit == 0 && (j == null || j.error == null)) {
  93. done({ ok = true, out = j })
  94. return null
  95. }
  96. let err = 'review child exit ' + r.exit
  97. if (j != null && j.error != null) { err = '' + j.error }
  98. else if (r.err.length > 0) { err = err + ': ' + r.err[r.err.length - 1] }
  99. done({ ok = false, error = cut(err) })
  100. return null
  101. })
  102. return null
  103. }
  104. \\ closed/merged PR: remove its session (state) via the CLI; done({ ok })
  105. dropSession = (Hybrid mark, String number, done) => {
  106. let cfg = null
  107. if (repos != null) { cfg = repos['' + mark.repo.id] }
  108. if (cfg == null) { return done({ ok = false, error = 'no config for repo ' + mark.repo.id }) }
  109. runChild([hybriel, dir() + '/../tools/runner.hl', 'drop', runnerData, cfg.clone, number], 60, (r) => {
  110. done({ ok = r.exit == 0 && !r.killed, error = r.err.join(' ') })
  111. return null
  112. })
  113. return null
  114. }
  115. legacy = (Hybrid job, done) => {
  116. 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 }) + '
  117. ')
  118. slowFile = dataDir + '/slow-' + job.key.replace('#', '_')
  119. if (exists(slowFile)) { run('sleep 3') }
  120. failFile = dataDir + '/fail-' + job.key.replace('#', '_')
  121. if (exists(failFile)) { return done({ ok = false, error = 'test failure hook' }) }
  122. return done({ ok = true })
  123. }

Branches

Latest commits

  • 8d34ca33records: repo rename (gitoria#53)mre
  • 1e05cb3fMerge branch 'repo-rename' into mainKunibert
  • 6ee0d2e9|||PR intake: match a delivery by repo.id and signature, not URLs (gitoria#53 rename)mre
  • 900ffcd4status: routgermre
  • 59b0b793log: routger merge waitmre
  • e52f094alog: routger intake check (hycon#122)mre
  • f979027frecords: design rule livemre
  • 71efaf84Merge branch 'design-guard' into mainKunibert
  • c0656581|||PR Flag removal of a documented design as major unless his recorded yes is linked; rules version 5mre
  • 901cdd75records: L10 update 2 livemre
  • 4a68016bMerge branch 'hl-standard-l10b' into mainKunibert
  • 246d0f30|||PR HL-STANDARD L10 update (hybriel ab293e5a), rules version 4mre
  • 1aa4d64crecords: HL-STANDARD L10 update livemre
  • 2b04c240Merge branch 'hl-standard-l10' into mainKunibert
  • 88c7b09d|||PR HL-STANDARD L10 update (hybriel 5885d688), rules version 3mre
  • 00869ecbrecords: gitoria#50 step 3 livemre
  • f97a0e5fMerge branch 'hl-standard' into mainKunibert
  • 9710e39fType STANDARD as String (L10, Kunibert review)mre
  • 4140b664|||PR Review against HL-STANDARD and cite rule ids (gitoria#50 step 3)mre
  • 75c6888drecords: gitoria#50 step 1 connected and provenmre