gitoria

kunibert

All repositories: gitoria

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

8.8 KB

  1. $mode = 'static'
  2. import { run } from 'hl:proc'
  3. import { readFile } from 'hl:fs'
  4. import { fetch } from 'hl:fetch'
  5. import { sha256 } from 'hl:crypto'
  6. import { require } from './review-output.hl'
  7. // Linked tickets as the spec source (kunibert#8). gitoria lists the ids (`pull.tickets`, `project#n`); we read the content from
  8. // the tickets service ourselves with our own read token. GET only. The token lives only in this process (never in an argv, a file
  9. // we write, a log or an error text).
  10. maxTickets = 10
  11. requestTimeoutMs = 15000 // the ONE timeout: per request
  12. maxSpecBytes = 60000 // cap of .kunibert/spec.md
  13. nameChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-_'
  14. digits = '0123456789'
  15. // strict `project#n`: project of [A-Za-z0-9._-] (1..100, not starting with '.', no '..'), n a positive integer without leading zero (max 9 digits)
  16. validId = (String id) => {
  17. parts = id.split('#')
  18. if (parts.length != 2) { return false }
  19. project = parts[0]
  20. n = parts[1]
  21. if (project.length < 1 || project.length > 100 || project.startsWith('.') || project.includes('..')) { return false }
  22. if (n.length < 1 || n.length > 9 || n.startsWith('0')) { return false }
  23. for (ch of project.split('')) { if (!nameChars.includes(ch)) { return false } }
  24. for (ch of n.split('')) { if (!digits.includes(ch)) { return false } }
  25. return true
  26. }
  27. // The token from its file (mode 600 required, first line); null when no file is configured. A configured but unusable file is an error.
  28. readToken = (String file) => {
  29. if (file == null || file == '') { return null }
  30. mode = run(['stat', '-c', '%a', file], { timeout = 10 })
  31. require(mode.exit == 0, 'tickets token file not readable')
  32. require(mode.lines.join('').trim().endsWith('00'), 'tickets token file must have mode 600 (no group/other access)')
  33. token = readFile(file).split('\n')[0].trim()
  34. require(token != '', 'tickets token file is empty')
  35. return token
  36. }
  37. // One GET. { id, ok, status, text } ; 404/403/other failures are { ok = false, status } (the caller says "not readable"), never an error.
  38. fetchTicket = (String base, String token, String id) => {
  39. parts = id.split('#')
  40. url = base + '/api/projects/' + encodeURIComponent(parts[0]) + '/tickets/' + parts[1]
  41. r = fetch(url, { method = 'GET', headers = { authorization = 'Bearer ' + token, accept = 'text/markdown', 'user-agent' = 'kunibert-review' }, timeoutMs = requestTimeoutMs, maxRedirects = 0 })
  42. if (!r.ok) { return { =id, ok = false, status = r.status, text = '' } }
  43. return { =id, ok = true, status = r.status, text = r.text() }
  44. }
  45. NL = '
  46. '
  47. // "# project #n: Title" is the first line of the served markdown.
  48. titleOf = (String md) => {
  49. first = md.split(NL)[0]
  50. i = first.indexOf(': ')
  51. if (first.startsWith('# ') && i > 0) { return first.slice(i + 2).trim() }
  52. return '(no title)'
  53. }
  54. // One fetched ticket as { id, link, title, head, entries[] } : `head` is title..description (everything before "## History"),
  55. // entries are the history items ("### n · date · ..." with their text), oldest first as the service sends them.
  56. parseTicket = (String base, String id, String md) => {
  57. parts = id.split('#')
  58. let top = md
  59. let hist = ''
  60. k = md.indexOf(NL + '## History' + NL)
  61. if (k >= 0) { top = md.slice(0, k) hist = md.slice(k + (NL + '## History' + NL).length) }
  62. let entries = []
  63. let cur = null
  64. for (line of hist.split(NL)) {
  65. if (line.startsWith('### ')) {
  66. if (cur != null) { entries.push(cur) }
  67. cur = { heading = line, body = [] }
  68. } else if (cur != null) { cur.body.push(line) }
  69. }
  70. if (cur != null) { entries.push(cur) }
  71. let rest = top.split(NL)
  72. rest.shift()
  73. let items = []
  74. for (e of entries) { items.push({ heading = e.heading, body = e.body.join(NL).trim(), cut = false }) }
  75. return { =id, link = base + '/' + parts[0] + '/' + parts[1], title = titleOf(md), head = rest.join(NL).trim(), entries = items }
  76. }
  77. section = (Hybrid t) => {
  78. let lines = ['## ' + t.id + ': ' + t.title, 'link: ' + t.link, '', t.head, '', '### History (oldest first)']
  79. for (e of t.entries) {
  80. lines.push('')
  81. lines.push(e.heading)
  82. if (e.cut) { lines.push('[comment text cut to keep this file under the size limit]') }
  83. else if (e.body != '') { lines.push(e.body) }
  84. }
  85. return lines.join(NL)
  86. }
  87. header = 'Spec file of Kunibert (kunibert#8): the tickets this pull request links to, read from the tickets service just now, each with its description and its history (comments and state changes) OLDEST FIRST.
  88. A later comment overrules the description and every earlier comment: where they disagree, the newest comment is the requirement.'
  89. total = (Hybrid tickets, Hybrid notReadable, Hybrid notes) => {
  90. return toBytes(render(tickets, notReadable, notes)).length
  91. }
  92. render = (Hybrid tickets, Hybrid notReadable, Hybrid notes) => {
  93. // keep the order of the PR's list: readable ones carry their index
  94. let blocks = []
  95. for (let a = 1; a <= 200; a = a + 1) {
  96. for (t of tickets) { if (t.at == a) { blocks.push({ text = section(t) }) } }
  97. for (n of notReadable) { if (n.at == a) { blocks.push({ text = '## ' + n.id + ': not readable (' + n.why + ')' }) } }
  98. }
  99. let lines = [header]
  100. for (n of notes) { lines.push('') lines.push(n) }
  101. for (b of blocks) { lines.push('') lines.push('---') lines.push('') lines.push(b.text) }
  102. return lines.join(NL) + NL
  103. }
  104. // Cut to the cap: the oldest history entries first (round robin over the tickets: every ticket's oldest, then the next oldest), their text only, the
  105. // heading stays; then, only if still over, the description of the biggest ticket. Says what was cut.
  106. fit = (Hybrid tickets, Hybrid notReadable, Hybrid notes) => {
  107. limit = maxSpecBytes - 400 // room for the NOTE line added at the end
  108. let cutEntries = 0
  109. let cutDesc = 0
  110. let more = true
  111. let i = 0
  112. while (total(tickets, notReadable, notes) > limit && more) {
  113. more = false
  114. for (t of tickets) {
  115. if (i < t.entries.length) {
  116. more = true
  117. if (!t.entries[i].cut && total(tickets, notReadable, notes) > limit) { t.entries[i].cut = true cutEntries = cutEntries + 1 }
  118. }
  119. }
  120. i = i + 1
  121. }
  122. let guard = 0
  123. while (total(tickets, notReadable, notes) > limit && guard < 20) {
  124. guard = guard + 1
  125. let big = tickets[0]
  126. for (t of tickets) { if (t.head.length > big.head.length) { big = t } }
  127. excess = total(tickets, notReadable, notes) - limit
  128. let keep = big.head.length - excess - 200
  129. if (keep < 0) { keep = 0 }
  130. cutDesc = cutDesc + 1
  131. big.head = big.head.slice(0, keep) + NL + '[description cut to keep this file under the size limit]'
  132. }
  133. let parts = []
  134. if (cutEntries > 0) { parts.push('the text of the ' + cutEntries + ' oldest history entries') }
  135. if (cutDesc > 0) { parts.push('part of a description') }
  136. if (parts.length > 0) { notes.push('NOTE: this file was cut to stay under ' + maxSpecBytes + ' bytes: ' + parts.join(' and ') + ' (the newest comments and the descriptions are kept first). Read the full tickets at the links if a cut matters.') }
  137. return render(tickets, notReadable, notes)
  138. }
  139. // ids -> { text, hash, ids, readable, notReadable } ; text == null when there is nothing to show (no ids, or no token configured).
  140. // base/tokenFile null: no tickets are read at all (today's "no ticket linked").
  141. buildSpec = (String base, String tokenFile, Hybrid ids) => {
  142. if (base == null || tokenFile == null || ids == null || ids.length == 0) { return { text = null, hash = null, readable = 0, notReadable = 0 } }
  143. token = readToken(tokenFile)
  144. let root = base.endsWith('/') ? base.slice(0, base.length - 1) : base
  145. let tickets = []
  146. let bad = []
  147. let notes = []
  148. let seen = []
  149. let at = 0
  150. for (id of ids.slice(0, 100)) {
  151. if (seen.includes(id)) { continue }
  152. seen.push(id)
  153. at = at + 1
  154. if (at > 200) { break }
  155. if (!validId(id)) { bad.push({ id = id.slice(0, 60), why = 'not a valid project#n id, ignored', =at }) continue }
  156. if (seen.length > maxTickets) { notes.push('NOTE: the PR links more than ' + maxTickets + ' tickets; only the first ' + maxTickets + ' are read.') break }
  157. r = fetchTicket(root, token, id)
  158. if (!r.ok) {
  159. why = r.status == 404 ? 'HTTP 404, no such ticket' : (r.status == 403 || r.status == 401 ? 'HTTP ' + r.status + ', no access' : 'HTTP ' + r.status)
  160. bad.push({ =id, =why, =at })
  161. } else {
  162. t = parseTicket(root, id, r.text)
  163. t.at = at
  164. tickets.push(t)
  165. }
  166. }
  167. if (tickets.length == 0 && bad.length == 0) { return { text = null, hash = null, readable = 0, notReadable = 0 } }
  168. text = fit(tickets, bad, notes)
  169. return { =text, hash = sha256(text), readable = tickets.length, notReadable = bad.length }
  170. }

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