gitoria

kunibert

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Main branchmainbd3591c7conductor: gates run on the live day44 binarymremain/lib/run-job.hl

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

Branches

Latest commits

  • bd3591c7conductor: gates run on the live day44 binarymre
  • 47f747a7records: jukebox #9 fix livemre
  • 0a3169fdMerge branch 'vendored-size' into mainKunibert
  • e447bb09Vendored size fix: keep renames with an app side whole; type the filter parameter (L10)mre
  • 99ccc742Vendored size fix: no renames in the app diff, filter the resumed delta toomre
  • 1a52e100|||PR Diff size limit counts only app files when vendored paths are setmre
  • 50a1867estatus: jukebox #9mre
  • c57484e6log: jukebox #9 diff sizemre
  • 795a20carecords: guard fix live (PR 8)mre
  • 6981d96fMerge branch 'fresh-on-nocmd' into mainKunibert
  • 37c47840runner: one shared prompt for both fresh-session fallbacks (L16)mre
  • f945d2e1|||PR runner: a resumed run with no command on a new head gets one fresh session instead of a tool errormre
  • 10b2a49estatus: PR 314, guard fix proposedmre
  • e73995a3log: hycon PR 314 approved after fresh reviewmre
  • f60427falog: hycon PR 314 false tool errormre
  • 481974f7log: Hermes accepts PR 259 findingmre
  • bb0976acrecords: hycon PR 259 design findingmre
  • 5c637c24records: hycon PR 259 false tool errormre
  • a344c406log: routger 132 reviewedmre
  • 56e70dcbrecords: review time limit 1800 s (routger 132)mre