@quickchatai/quickreview 1.2.0 → 1.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (350) hide show
  1. package/README.md +116 -53
  2. package/bin/qcr.js +1 -1
  3. package/config.example.toml +25 -7
  4. package/deploy/ansible/README.md +20 -12
  5. package/deploy/ansible/inventory.example.ini +1 -1
  6. package/deploy/collect-secrets.sh +1 -1
  7. package/deploy/qcr-update.sh +1 -1
  8. package/dist/extensions/cli/home.js +14 -0
  9. package/dist/extensions/cli/home.js.map +1 -0
  10. package/dist/extensions/cli/selfimprove.js +18 -0
  11. package/dist/extensions/cli/selfimprove.js.map +1 -0
  12. package/dist/extensions/home.js +350 -0
  13. package/dist/extensions/home.js.map +1 -0
  14. package/dist/extensions/login-route.js +1 -1
  15. package/dist/extensions/login-route.js.map +1 -1
  16. package/dist/extensions/onboarding.js +8 -7
  17. package/dist/extensions/onboarding.js.map +1 -1
  18. package/dist/extensions/selfimprove.js +49 -0
  19. package/dist/extensions/selfimprove.js.map +1 -0
  20. package/dist/extensions/setup-scan.js +23 -592
  21. package/dist/extensions/setup-scan.js.map +1 -1
  22. package/dist/extensions/setup.js +2 -47
  23. package/dist/extensions/setup.js.map +1 -1
  24. package/dist/extensions/vendor/openviking-pi/index.js +4 -0
  25. package/dist/extensions/vendor/openviking-pi/index.js.map +1 -1
  26. package/dist/extensions/vendor/openviking-pi/qcr-hooks.js +3 -1
  27. package/dist/extensions/vendor/openviking-pi/qcr-hooks.js.map +1 -1
  28. package/dist/extensions/vendor/openviking-pi/sync.js +81 -0
  29. package/dist/extensions/vendor/openviking-pi/sync.js.map +1 -1
  30. package/dist/src/archive/restore.js +18 -15
  31. package/dist/src/archive/restore.js.map +1 -1
  32. package/dist/src/botworker.js +13 -2
  33. package/dist/src/botworker.js.map +1 -1
  34. package/dist/src/channelfs.js +3 -26
  35. package/dist/src/channelfs.js.map +1 -1
  36. package/dist/src/cli.js +14 -50
  37. package/dist/src/cli.js.map +1 -1
  38. package/dist/src/config.js +16 -0
  39. package/dist/src/config.js.map +1 -1
  40. package/dist/src/daemon.js +33 -4
  41. package/dist/src/daemon.js.map +1 -1
  42. package/dist/src/debuground.js +1 -0
  43. package/dist/src/debuground.js.map +1 -1
  44. package/dist/src/debugroundpi.js +1 -0
  45. package/dist/src/debugroundpi.js.map +1 -1
  46. package/dist/src/engine/caserunner-sdk.js +15 -251
  47. package/dist/src/engine/caserunner-sdk.js.map +1 -1
  48. package/dist/src/home/adapters.js +349 -0
  49. package/dist/src/home/adapters.js.map +1 -0
  50. package/dist/src/home/advanced.js +318 -0
  51. package/dist/src/home/advanced.js.map +1 -0
  52. package/dist/src/home/advancedtools.js +57 -0
  53. package/dist/src/home/advancedtools.js.map +1 -0
  54. package/dist/src/home/autoreview.js +325 -0
  55. package/dist/src/home/autoreview.js.map +1 -0
  56. package/dist/src/home/daemonstatus.js +32 -0
  57. package/dist/src/home/daemonstatus.js.map +1 -0
  58. package/dist/src/home/devicelogin.js +61 -0
  59. package/dist/src/home/devicelogin.js.map +1 -0
  60. package/dist/src/home/launch.js +202 -0
  61. package/dist/src/home/launch.js.map +1 -0
  62. package/dist/src/home/loginowner.js +205 -0
  63. package/dist/src/home/loginowner.js.map +1 -0
  64. package/dist/src/home/memory.js +122 -0
  65. package/dist/src/home/memory.js.map +1 -0
  66. package/dist/src/home/model.js +811 -0
  67. package/dist/src/home/model.js.map +1 -0
  68. package/dist/src/home/panel.js +520 -0
  69. package/dist/src/home/panel.js.map +1 -0
  70. package/dist/src/home/ports.js +19 -0
  71. package/dist/src/home/ports.js.map +1 -0
  72. package/dist/src/home/review.js +163 -0
  73. package/dist/src/home/review.js.map +1 -0
  74. package/dist/src/home/reviews.js +478 -0
  75. package/dist/src/home/reviews.js.map +1 -0
  76. package/dist/src/home/setup.js +507 -0
  77. package/dist/src/home/setup.js.map +1 -0
  78. package/dist/src/home/setupadapters.js +214 -0
  79. package/dist/src/home/setupadapters.js.map +1 -0
  80. package/dist/src/home/setupcard.js +199 -0
  81. package/dist/src/home/setupcard.js.map +1 -0
  82. package/dist/src/home/setuptools.js +45 -0
  83. package/dist/src/home/setuptools.js.map +1 -0
  84. package/dist/src/home/spend.js +160 -0
  85. package/dist/src/home/spend.js.map +1 -0
  86. package/dist/src/home/switches.js +209 -0
  87. package/dist/src/home/switches.js.map +1 -0
  88. package/dist/src/home/switchon.js +439 -0
  89. package/dist/src/home/switchon.js.map +1 -0
  90. package/dist/src/home/toolnames.js +41 -0
  91. package/dist/src/home/toolnames.js.map +1 -0
  92. package/dist/src/home/tune.js +341 -0
  93. package/dist/src/home/tune.js.map +1 -0
  94. package/dist/src/home/words.js +7 -0
  95. package/dist/src/home/words.js.map +1 -0
  96. package/dist/src/homes.js +14 -0
  97. package/dist/src/homes.js.map +1 -1
  98. package/dist/src/install.js +1 -1
  99. package/dist/src/install.js.map +1 -1
  100. package/dist/src/ipc/workerspec.js +8 -0
  101. package/dist/src/ipc/workerspec.js.map +1 -1
  102. package/dist/src/ledgercheck.js +39 -10
  103. package/dist/src/ledgercheck.js.map +1 -1
  104. package/dist/src/live/publish.js +1 -1
  105. package/dist/src/live/publish.js.map +1 -1
  106. package/dist/src/livecli.js +1 -1
  107. package/dist/src/livecli.js.map +1 -1
  108. package/dist/src/loginrefresh/handover.js +121 -0
  109. package/dist/src/loginrefresh/handover.js.map +1 -0
  110. package/dist/src/loginrefresh/lock.js +54 -3
  111. package/dist/src/loginrefresh/lock.js.map +1 -1
  112. package/dist/src/loginrefresh/refresher.js +33 -5
  113. package/dist/src/loginrefresh/refresher.js.map +1 -1
  114. package/dist/src/loginrefresh/supervisor.js +18 -1
  115. package/dist/src/loginrefresh/supervisor.js.map +1 -1
  116. package/dist/src/npmverbs.js +42 -12
  117. package/dist/src/npmverbs.js.map +1 -1
  118. package/dist/src/onboard/choices.js +6 -3
  119. package/dist/src/onboard/choices.js.map +1 -1
  120. package/dist/src/onboard/engine.js +33 -292
  121. package/dist/src/onboard/engine.js.map +1 -1
  122. package/dist/src/onboard/steps/bringup.js +3 -2
  123. package/dist/src/onboard/steps/bringup.js.map +1 -1
  124. package/dist/src/onboard/steps/configfill.js +18 -7
  125. package/dist/src/onboard/steps/configfill.js.map +1 -1
  126. package/dist/src/onboard/steps/loginshare.js +213 -0
  127. package/dist/src/onboard/steps/loginshare.js.map +1 -0
  128. package/dist/src/onboard/steps/memory.js +14 -2
  129. package/dist/src/onboard/steps/memory.js.map +1 -1
  130. package/dist/src/onboard/steps/repoconfig.js +72 -0
  131. package/dist/src/onboard/steps/repoconfig.js.map +1 -0
  132. package/dist/src/onboard/toolnames.js +2 -2
  133. package/dist/src/onboard/toolnames.js.map +1 -1
  134. package/dist/src/piagentdir.js +13 -0
  135. package/dist/src/piagentdir.js.map +1 -0
  136. package/dist/src/pilogin.js +31 -16
  137. package/dist/src/pilogin.js.map +1 -1
  138. package/dist/src/qcrbin.js +16 -11
  139. package/dist/src/qcrbin.js.map +1 -1
  140. package/dist/src/reviewargs.js +122 -0
  141. package/dist/src/reviewargs.js.map +1 -0
  142. package/dist/src/reviewcli.js +7 -161
  143. package/dist/src/reviewcli.js.map +1 -1
  144. package/dist/src/reviewdetach.js +76 -11
  145. package/dist/src/reviewdetach.js.map +1 -1
  146. package/dist/src/scancli.js +39 -691
  147. package/dist/src/scancli.js.map +1 -1
  148. package/dist/src/selfimprove.js +66 -0
  149. package/dist/src/selfimprove.js.map +1 -0
  150. package/dist/src/session-command-supervisor.js +38 -9
  151. package/dist/src/session-command-supervisor.js.map +1 -1
  152. package/dist/src/setup/adapters.js +5 -223
  153. package/dist/src/setup/adapters.js.map +1 -1
  154. package/dist/src/setup/caserunner.js +15 -249
  155. package/dist/src/setup/caserunner.js.map +1 -1
  156. package/dist/src/setup/engine.js +32 -341
  157. package/dist/src/setup/engine.js.map +1 -1
  158. package/dist/src/setup/facts.js +3 -592
  159. package/dist/src/setup/facts.js.map +1 -1
  160. package/dist/src/setup/findings.js +3 -947
  161. package/dist/src/setup/findings.js.map +1 -1
  162. package/dist/src/setup/presentation.js +3 -8
  163. package/dist/src/setup/presentation.js.map +1 -1
  164. package/dist/src/setup/scan.js +3 -724
  165. package/dist/src/setup/scan.js.map +1 -1
  166. package/dist/src/setup/scanbuild.js +9 -92
  167. package/dist/src/setup/scanbuild.js.map +1 -1
  168. package/dist/src/setup/scanlease.js +3 -250
  169. package/dist/src/setup/scanlease.js.map +1 -1
  170. package/dist/src/setup/scanreport.js +3 -288
  171. package/dist/src/setup/scanreport.js.map +1 -1
  172. package/dist/src/setup/scanrun.js +3 -1732
  173. package/dist/src/setup/scanrun.js.map +1 -1
  174. package/dist/src/setup/scantree.js +3 -256
  175. package/dist/src/setup/scantree.js.map +1 -1
  176. package/dist/src/setup/stage2request.js +3 -258
  177. package/dist/src/setup/stage2request.js.map +1 -1
  178. package/dist/src/setup/unitcut.js +3 -436
  179. package/dist/src/setup/unitcut.js.map +1 -1
  180. package/dist/src/state.js +75 -9
  181. package/dist/src/state.js.map +1 -1
  182. package/dist/src/takeover.js +1 -0
  183. package/dist/src/takeover.js.map +1 -1
  184. package/dist/src/takeoverpi.js +2 -1
  185. package/dist/src/takeoverpi.js.map +1 -1
  186. package/dist/src/tui/alerts.js +4 -1
  187. package/dist/src/tui/alerts.js.map +1 -1
  188. package/dist/src/tui/board.js +2 -2
  189. package/dist/src/tui/board.js.map +1 -1
  190. package/dist/src/tui/companion.js +2 -1
  191. package/dist/src/tui/companion.js.map +1 -1
  192. package/dist/src/tui/deploytab.js +2 -2
  193. package/dist/src/tui/deploytab.js.map +1 -1
  194. package/dist/src/tui/index.js +9 -6
  195. package/dist/src/tui/index.js.map +1 -1
  196. package/dist/src/tui/memory.js +3 -16
  197. package/dist/src/tui/memory.js.map +1 -1
  198. package/dist/src/tui/memoryurl.js +22 -0
  199. package/dist/src/tui/memoryurl.js.map +1 -0
  200. package/dist/src/tui/remote.js +1 -1
  201. package/dist/src/tui/remote.js.map +1 -1
  202. package/dist/src/tui/route.js +2 -2
  203. package/dist/src/tui/route.js.map +1 -1
  204. package/dist/src/tui/setupstate.js +8 -5
  205. package/dist/src/tui/setupstate.js.map +1 -1
  206. package/dist/src/tui/sshtriage.js +1 -1
  207. package/dist/src/tui/sshtriage.js.map +1 -1
  208. package/dist/src/verdicts.js +12 -0
  209. package/dist/src/verdicts.js.map +1 -1
  210. package/dist/src/wire.js +9 -4
  211. package/dist/src/wire.js.map +1 -1
  212. package/dist/src/workerbreaker.js +32 -5
  213. package/dist/src/workerbreaker.js.map +1 -1
  214. package/dist/src/workerworld.js +4 -0
  215. package/dist/src/workerworld.js.map +1 -1
  216. package/docker/README.md +1 -1
  217. package/docker/entrypoint.sh +40 -6
  218. package/docker-compose.yml +16 -1
  219. package/extensions/cli/home.ts +14 -0
  220. package/extensions/cli/selfimprove.ts +18 -0
  221. package/extensions/home.ts +484 -0
  222. package/extensions/login-route.ts +1 -1
  223. package/extensions/onboarding.ts +8 -7
  224. package/extensions/selfimprove.ts +65 -0
  225. package/extensions/setup-scan.ts +79 -766
  226. package/extensions/setup.ts +14 -70
  227. package/extensions/vendor/openviking-pi/NOTICE +5 -0
  228. package/extensions/vendor/openviking-pi/README.md +39 -5
  229. package/extensions/vendor/openviking-pi/index.ts +4 -0
  230. package/extensions/vendor/openviking-pi/patches/0014-one-turn-sync-in-flight.patch +239 -0
  231. package/extensions/vendor/openviking-pi/qcr-hooks.ts +3 -1
  232. package/extensions/vendor/openviking-pi/sync.ts +80 -0
  233. package/extensions/vendor/openviking-pi/upstream.json +2 -1
  234. package/package.json +6 -4
  235. package/prompts/onboarding.md +8 -7
  236. package/schemas/review-result.v1.json +60 -0
  237. package/scripts/deploy.sh +8 -6
  238. package/scripts/qcr-host.sh +28 -5
  239. package/scripts/vm-ghcr-login.sh +7 -4
  240. package/src/archive/restore.ts +18 -21
  241. package/src/botworker.ts +13 -2
  242. package/src/channelfs.ts +3 -26
  243. package/src/cli.ts +13 -54
  244. package/src/config.ts +38 -0
  245. package/src/daemon.ts +34 -4
  246. package/src/debuground.ts +3 -0
  247. package/src/debugroundpi.ts +1 -0
  248. package/src/engine/caserunner-sdk.ts +29 -314
  249. package/src/home/adapters.ts +418 -0
  250. package/src/home/advanced.ts +421 -0
  251. package/src/home/advancedtools.ts +96 -0
  252. package/src/home/autoreview.ts +385 -0
  253. package/src/home/daemonstatus.ts +55 -0
  254. package/src/home/devicelogin.ts +107 -0
  255. package/src/home/launch.ts +257 -0
  256. package/src/home/loginowner.ts +248 -0
  257. package/src/home/memory.ts +176 -0
  258. package/src/home/model.ts +965 -0
  259. package/src/home/panel.ts +666 -0
  260. package/src/home/ports.ts +338 -0
  261. package/src/home/review.ts +206 -0
  262. package/src/home/reviews.ts +565 -0
  263. package/src/home/setup.ts +689 -0
  264. package/src/home/setupadapters.ts +217 -0
  265. package/src/home/setupcard.ts +288 -0
  266. package/src/home/setuptools.ts +57 -0
  267. package/src/home/spend.ts +194 -0
  268. package/src/home/switches.ts +293 -0
  269. package/src/home/switchon.ts +554 -0
  270. package/src/home/toolnames.ts +46 -0
  271. package/src/home/tune.ts +405 -0
  272. package/src/home/words.ts +8 -0
  273. package/src/homes.ts +17 -0
  274. package/src/install.ts +1 -1
  275. package/src/ipc/workerspec.ts +15 -0
  276. package/src/ledgercheck.ts +45 -9
  277. package/src/live/publish.ts +4 -1
  278. package/src/livecli.ts +6 -1
  279. package/src/loginrefresh/handover.ts +138 -0
  280. package/src/loginrefresh/lock.ts +72 -3
  281. package/src/loginrefresh/refresher.ts +34 -5
  282. package/src/loginrefresh/supervisor.ts +25 -2
  283. package/src/npmverbs.ts +51 -11
  284. package/src/onboard/choices.ts +6 -3
  285. package/src/onboard/engine.ts +64 -434
  286. package/src/onboard/steps/bringup.ts +3 -2
  287. package/src/onboard/steps/configfill.ts +23 -7
  288. package/src/onboard/steps/loginshare.ts +258 -0
  289. package/src/onboard/steps/memory.ts +18 -2
  290. package/src/onboard/steps/repoconfig.ts +85 -0
  291. package/src/onboard/toolnames.ts +2 -2
  292. package/src/piagentdir.ts +12 -0
  293. package/src/pilogin.ts +32 -16
  294. package/src/qcrbin.ts +16 -11
  295. package/src/reviewargs.ts +119 -0
  296. package/src/reviewcli.ts +6 -167
  297. package/src/reviewdetach.ts +107 -13
  298. package/src/scancli.ts +75 -826
  299. package/src/selfimprove.ts +90 -0
  300. package/src/session-command-supervisor.ts +41 -8
  301. package/src/setup/adapters.ts +13 -221
  302. package/src/setup/caserunner.ts +34 -273
  303. package/src/setup/engine.ts +62 -496
  304. package/src/setup/facts.ts +3 -627
  305. package/src/setup/findings.ts +3 -1264
  306. package/src/setup/presentation.ts +3 -18
  307. package/src/setup/scan.ts +3 -1033
  308. package/src/setup/scanbuild.ts +20 -114
  309. package/src/setup/scanlease.ts +3 -297
  310. package/src/setup/scanreport.ts +3 -373
  311. package/src/setup/scanrun.ts +3 -2268
  312. package/src/setup/scantree.ts +3 -259
  313. package/src/setup/stage2request.ts +3 -347
  314. package/src/setup/unitcut.ts +3 -523
  315. package/src/state.ts +126 -11
  316. package/src/takeover.ts +5 -0
  317. package/src/takeoverpi.ts +3 -2
  318. package/src/tui/alerts.ts +9 -2
  319. package/src/tui/board.ts +2 -2
  320. package/src/tui/companion.ts +2 -1
  321. package/src/tui/deploytab.ts +2 -2
  322. package/src/tui/index.ts +9 -6
  323. package/src/tui/memory.ts +3 -15
  324. package/src/tui/memoryurl.ts +22 -0
  325. package/src/tui/remote.ts +1 -1
  326. package/src/tui/route.ts +2 -2
  327. package/src/tui/setupstate.ts +20 -16
  328. package/src/tui/sshtriage.ts +1 -1
  329. package/src/verdicts.ts +13 -0
  330. package/src/wire.ts +9 -4
  331. package/src/workerbreaker.ts +41 -5
  332. package/src/workerworld.ts +9 -1
  333. package/dist/src/live/projection.js +0 -450
  334. package/dist/src/live/projection.js.map +0 -1
  335. package/dist/src/live/read.js +0 -60
  336. package/dist/src/live/read.js.map +0 -1
  337. package/dist/src/onboard/launch.js +0 -75
  338. package/dist/src/onboard/launch.js.map +0 -1
  339. package/dist/src/setup/ledger.js +0 -207
  340. package/dist/src/setup/ledger.js.map +0 -1
  341. package/dist/src/setup/stage2.js +0 -617
  342. package/dist/src/setup/stage2.js.map +0 -1
  343. package/dist/src/setup/textsniff.js +0 -89
  344. package/dist/src/setup/textsniff.js.map +0 -1
  345. package/src/live/projection.ts +0 -491
  346. package/src/live/read.ts +0 -86
  347. package/src/onboard/launch.ts +0 -103
  348. package/src/setup/ledger.ts +0 -324
  349. package/src/setup/stage2.ts +0 -926
  350. package/src/setup/textsniff.ts +0 -95
@@ -0,0 +1,418 @@
1
+ // The home's real ports (src/home/ports.ts), and the one function that turns
2
+ // a process's environment into the home's options: the env shim
3
+ // (extensions/cli/home.ts) calls `homeOptionsFromEnv(process.env)`.
4
+ //
5
+ // pi login the auth file of the pi home the chat runs in: pi's own
6
+ // agent dir ($PI_CODING_AGENT_DIR, else ~/.pi/agent), read the
7
+ // way pi reads it, so the login line and the chat cannot
8
+ // disagree. Provider ids only, never a token.
9
+ // GitHub `gh`: the account, the repository's open PRs, the browser.
10
+ // repository `[github] repo` of the install's config.toml, read again on
11
+ // every refresh.
12
+ // clock Date.now.
13
+ // reviews the app's detached review runs, its review state and the
14
+ // daemon's status (src/home/reviews.ts), as the person: their
15
+ // environment, so the run uses their own pi login.
16
+ //
17
+ // auto-review, memory
18
+ // this machine's compose stack (src/home/autoreview.ts,
19
+ // src/home/memory.ts): the containers, the daemon's own
20
+ // status.json, and the switches over the wizard's steps.
21
+ // login owner
22
+ // the same pi agent dir: whether auto-review's refresher holds
23
+ // the login (its lock beside auth.json, shared into the stack)
24
+ // and whether its record says the login expired
25
+ // (src/home/loginowner.ts).
26
+ //
27
+ // spend the app's detached runs' costs and the daemon's recorded
28
+ // costs (src/home/spend.ts): unknown when either can't be read.
29
+ //
30
+ // The stack these drive is a compose project with the wizard's state under it
31
+ // (`homeStackRoot`): a checkout or the image is its own, an app installed by
32
+ // a package manager gets one in its state home, never inside the package.
33
+ import { execFile } from "node:child_process";
34
+ import { cpSync, existsSync, mkdirSync, readFileSync } from "node:fs";
35
+ import { join } from "node:path";
36
+ import { parse } from "smol-toml";
37
+ import { loginSummary } from "@quickchatai/pi-quickreview/src/engine/auth.js";
38
+ import { assetRoot } from "@quickchatai/pi-quickreview/src/assetroot.js";
39
+ import { postSavedReview } from "@quickchatai/pi-quickreview/src/savedpost.js";
40
+ import { HOME_DEFAULTS, sandboxRoot, type HomeToolSet } from "../config.js";
41
+ import { loadOneShotConfig, locateConfig, stackRoot, stateHome } from "../homes.js";
42
+ import type { OnboardAdapters, ProcessRunner } from "../onboard/engine.js";
43
+ import { openStepMemoryForward } from "../onboard/stepforward.js";
44
+ import { composeAutoReview, serviceState } from "./autoreview.js";
45
+ import { hostLoginOwner } from "./loginowner.js";
46
+ import { DAEMON_SERVICE } from "../onboard/steps/compose.js";
47
+ import { daemonSnapshot } from "./daemonstatus.js";
48
+ import { composeMemory } from "./memory.js";
49
+ import { realSpendPort } from "./spend.js";
50
+ import { composeAdvanced, type AdvancedPort } from "./advanced.js";
51
+ import { checkoutTune, type TunePort } from "./tune.js";
52
+ import { stepRunner } from "./switchon.js";
53
+ import { piAgentDir } from "../piagentdir.js";
54
+ import { qcrChild, realDetachSeams, reviewsPort } from "./reviews.js";
55
+ import {
56
+ known,
57
+ unknown,
58
+ type AutoReviewPort,
59
+ type GithubPort,
60
+ type HomeAdapters,
61
+ type Known,
62
+ type MemoryPort,
63
+ type OpenPull,
64
+ type PiLoginPort,
65
+ type RepoPort,
66
+ type ReviewsPort,
67
+ type SpendPort,
68
+ } from "./ports.js";
69
+
70
+ /** The launch hands the child pi its model and tool set in these. */
71
+ export const HOME_MODEL_ENV = "QCR_HOME_MODEL";
72
+ export const HOME_TOOLS_ENV = "QCR_HOME_TOOLS";
73
+
74
+ export interface RunResult {
75
+ code: number;
76
+ stdout: string;
77
+ stderr: string;
78
+ /** The command is not installed. */
79
+ missing?: boolean;
80
+ }
81
+
82
+ export interface Runner {
83
+ run(cmd: string, args: readonly string[]): Promise<RunResult>;
84
+ }
85
+
86
+ export const execRunner: Runner = {
87
+ run: (cmd, args) =>
88
+ new Promise((resolve) => {
89
+ execFile(cmd, [...args], { timeout: 30_000, maxBuffer: 16 << 20 }, (err, stdout, stderr) => {
90
+ const e = err as (NodeJS.ErrnoException & { code?: unknown }) | null;
91
+ resolve({
92
+ code: e === null ? 0 : typeof e.code === "number" ? e.code : 1,
93
+ stdout: String(stdout),
94
+ stderr: e && !stderr ? String(e.message) : String(stderr),
95
+ ...(e?.code === "ENOENT" ? { missing: true } : {}),
96
+ });
97
+ });
98
+ }),
99
+ };
100
+
101
+ const firstLine = (s: string): string => s.trim().split("\n")[0]?.trim() || "no output";
102
+ const isMissingFile = (err: unknown): boolean =>
103
+ (err as NodeJS.ErrnoException | null)?.code === "ENOENT";
104
+ const message = (err: unknown): string => (err instanceof Error ? err.message : String(err));
105
+
106
+ /** The providers pi holds a login for in `agentDir`. No auth file is no
107
+ * login; a file that cannot be read or parsed is unknown. */
108
+ export function piLoginPort(
109
+ agentDir: string,
110
+ read: (p: string) => string = (p) => readFileSync(p, "utf8"),
111
+ ): PiLoginPort {
112
+ return {
113
+ providers: async () => {
114
+ const path = join(agentDir, "auth.json");
115
+ let raw: string;
116
+ try {
117
+ raw = read(path);
118
+ } catch (err) {
119
+ return isMissingFile(err) ? known([]) : unknown(`can't read ${path}: ${message(err)}`);
120
+ }
121
+ try {
122
+ return known(Object.keys(loginSummary(raw)));
123
+ } catch (err) {
124
+ return unknown(`can't parse ${path}: ${message(err)}`);
125
+ }
126
+ },
127
+ };
128
+ }
129
+
130
+ /** gh's words for "you are not signed in". */
131
+ const SIGNED_OUT = /gh auth login|not logged in|authentication required|HTTP 401|bad credentials/i;
132
+
133
+ const PR_FIELDS =
134
+ "number,title,author,updatedAt,isDraft,additions,deletions,changedFiles,headRefName,headRefOid";
135
+
136
+ export function githubPort(runner: Runner): GithubPort {
137
+ const why = (r: RunResult): string =>
138
+ r.missing ? "gh is not installed" : `gh failed: ${firstLine(r.stderr)}`;
139
+ return {
140
+ user: async () => {
141
+ const r = await runner.run("gh", ["api", "user", "--jq", ".login"]);
142
+ if (r.code === 0 && r.stdout.trim() !== "") return known(r.stdout.trim());
143
+ if (!r.missing && SIGNED_OUT.test(r.stderr)) return known(null);
144
+ return unknown(why(r));
145
+ },
146
+ openPulls: async (repo) => {
147
+ const r = await runner.run("gh", [
148
+ "pr",
149
+ "list",
150
+ "-R",
151
+ repo,
152
+ "--state",
153
+ "open",
154
+ "--limit",
155
+ "200",
156
+ "--json",
157
+ PR_FIELDS,
158
+ ]);
159
+ if (r.code !== 0) return unknown(why(r));
160
+ try {
161
+ return known(parsePulls(r.stdout));
162
+ } catch (err) {
163
+ return unknown(`gh's answer is not a pull request list: ${message(err)}`);
164
+ }
165
+ },
166
+ openInBrowser: async (repo, pr) => {
167
+ const r = await runner.run("gh", ["pr", "view", String(pr), "-R", repo, "--web"]);
168
+ return r.code === 0 ? { ok: true } : { ok: false, why: why(r) };
169
+ },
170
+ };
171
+ }
172
+
173
+ /** `gh pr list --json` rows as OpenPulls; throws on a row of the wrong shape. */
174
+ export function parsePulls(json: string): OpenPull[] {
175
+ const rows = JSON.parse(json) as unknown;
176
+ if (!Array.isArray(rows)) throw new Error("not an array");
177
+ return rows.map((r: Record<string, unknown>) => {
178
+ if (typeof r["number"] !== "number" || typeof r["title"] !== "string")
179
+ throw new Error("a row without a number or title");
180
+ const author = r["author"] as { login?: unknown } | null | undefined;
181
+ const num = (k: string): number => (typeof r[k] === "number" ? r[k] : 0);
182
+ return {
183
+ number: r["number"],
184
+ title: r["title"],
185
+ author: typeof author?.login === "string" ? author.login : "ghost",
186
+ updatedAt: typeof r["updatedAt"] === "string" ? r["updatedAt"] : "",
187
+ isDraft: r["isDraft"] === true,
188
+ additions: num("additions"),
189
+ deletions: num("deletions"),
190
+ changedFiles: num("changedFiles"),
191
+ headRefName: typeof r["headRefName"] === "string" ? r["headRefName"] : "",
192
+ headSha: typeof r["headRefOid"] === "string" ? r["headRefOid"] : "",
193
+ };
194
+ });
195
+ }
196
+
197
+ /** `[github] repo` of the config at `configPath()`, located again on every
198
+ * read. No config file, or an empty repo, is no repository. */
199
+ export function repoPort(
200
+ configPath: () => string,
201
+ read: (p: string) => string = (p) => readFileSync(p, "utf8"),
202
+ ): RepoPort {
203
+ return {
204
+ repo: async () => {
205
+ let path: string;
206
+ let raw: string;
207
+ try {
208
+ path = configPath();
209
+ raw = read(path);
210
+ } catch (err) {
211
+ return isMissingFile(err) ? known(null) : unknown(message(err));
212
+ }
213
+ let repo: unknown;
214
+ try {
215
+ repo = (
216
+ (parse(raw) as Record<string, unknown>)["github"] as Record<string, unknown> | undefined
217
+ )?.["repo"];
218
+ } catch (err) {
219
+ return unknown(`${path} is not valid TOML: ${message(err)}`);
220
+ }
221
+ if (repo === undefined || repo === "") return known(null);
222
+ return typeof repo === "string" && /^[\w.-]+\/[\w.-]+$/.test(repo.trim())
223
+ ? known(repo.trim())
224
+ : unknown(`${path}: [github] repo is not "owner/name" (${JSON.stringify(repo)})`);
225
+ },
226
+ };
227
+ }
228
+
229
+ const NOT_YET = "the home cannot read it yet";
230
+ const notYet = async (): Promise<Known<never>> => unknown(NOT_YET);
231
+
232
+ /** The ports that have no adapter yet: each answers unknown. */
233
+ export const unbuiltPorts: {
234
+ autoReview: AutoReviewPort;
235
+ memory: MemoryPort;
236
+ spend: SpendPort;
237
+ } = {
238
+ autoReview: { status: notYet },
239
+ memory: { status: notYet },
240
+ spend: { totals: notYet },
241
+ };
242
+
243
+ /** The reviewer as the person: the app's own bin and registry, the install's
244
+ * state home and config, in the environment the home runs in; auto-review's
245
+ * rounds from the daemon of the compose stack at `stack.root`. */
246
+ export function realReviewsPort(
247
+ env: Readonly<Record<string, string | undefined>>,
248
+ stack: { root: string; runner: ProcessRunner },
249
+ ): ReviewsPort {
250
+ const state = stateHome(env);
251
+ const appRoot = sandboxRoot();
252
+ // the home's own working directory (src/home/launch.ts `homePaths`)
253
+ const cwd = join(state, "home");
254
+ const proc = { execPath: process.execPath, bin: join(appRoot, "bin", "qcr.js"), cwd, env };
255
+ const config = () => loadOneShotConfig({ root: assetRoot(), env }).cfg;
256
+ return reviewsPort({
257
+ registry: join(state, "review", "detached"),
258
+ reportsDir: join(cwd, "reviews"),
259
+ reviewHome: join(state, "review"),
260
+ snapshot: () => daemonSnapshot(stack.runner, stack.root),
261
+ qcr: qcrChild(proc),
262
+ detach: realDetachSeams(proc),
263
+ post: async (file) => {
264
+ const r = await postSavedReview(file, {
265
+ config: config(),
266
+ dryrunDir: join(state, "review", "dryrun"),
267
+ github: { env },
268
+ });
269
+ return { failed: r.failed, line: r.line };
270
+ },
271
+ dryRun: () => config().dry_run,
272
+ now: () => Date.now(),
273
+ });
274
+ }
275
+
276
+ /** The wizard's adapters for switch-on's steps, over the app root. */
277
+ export function stepAdapters(root: string, configPath: () => string) {
278
+ return (log: (line: string) => void): OnboardAdapters => ({
279
+ fetch: (input, init) => fetch(input, init),
280
+ runner: stepRunner,
281
+ clock: () => new Date(),
282
+ host: {
283
+ nodeVersion: process.version,
284
+ uid: typeof process.getuid === "function" ? process.getuid() : null,
285
+ inContainer: existsSync("/.dockerenv"),
286
+ },
287
+ paths: { stateDir: join(root, "state"), configPath: configPath() },
288
+ log,
289
+ memoryForward: (req) => openStepMemoryForward(req),
290
+ });
291
+ }
292
+
293
+ export function realHomeAdapters(o: {
294
+ agentDir: string;
295
+ configPath: () => string;
296
+ reviews: ReviewsPort;
297
+ runner?: Runner;
298
+ /** The app root, the compose project of this machine's stack: given, the
299
+ * auto-review and memory ports read and switch it. */
300
+ root?: string;
301
+ /** The spend line's port (src/home/spend.ts); absent, its stub. */
302
+ spend?: SpendPort;
303
+ }): HomeAdapters {
304
+ const repo = repoPort(o.configPath);
305
+ const stack =
306
+ o.root === undefined
307
+ ? null
308
+ : {
309
+ root: o.root,
310
+ configPath: o.configPath,
311
+ runner: stepRunner,
312
+ stepAdapters: stepAdapters(o.root, o.configPath),
313
+ };
314
+ const daemonRuns = stack
315
+ ? async (): Promise<boolean | null> => {
316
+ const s = await serviceState(stack.runner, stack.root, DAEMON_SERVICE);
317
+ return s.kind === "known" ? s.value !== "stopped" : null;
318
+ }
319
+ : undefined;
320
+ return {
321
+ login: piLoginPort(o.agentDir),
322
+ loginOwner: hostLoginOwner({
323
+ loginDir: o.agentDir,
324
+ now: () => Date.now(),
325
+ ...(daemonRuns ? { daemonRuns } : {}),
326
+ }),
327
+ github: githubPort(o.runner ?? execRunner),
328
+ repo,
329
+ clock: { now: () => Date.now() },
330
+ reviews: o.reviews,
331
+ ...unbuiltPorts,
332
+ ...(o.spend ? { spend: o.spend } : {}),
333
+ ...(stack
334
+ ? {
335
+ // switch-on shares the home's login with the stack (src/onboard/steps/loginshare.ts)
336
+ autoReview: composeAutoReview({ ...stack, loginDir: o.agentDir, now: () => Date.now() }),
337
+ memory: composeMemory({
338
+ ...stack,
339
+ repo: async () => {
340
+ const r = await repo.repo();
341
+ return r.kind === "known" ? r.value : null;
342
+ },
343
+ }),
344
+ }
345
+ : {}),
346
+ };
347
+ }
348
+
349
+ /** What a compose project needs from the app: the stack, the tunnel
350
+ * wrappers' build context, and the config template the steps fill from. */
351
+ export const STACK_FILES: readonly string[] = [
352
+ "docker-compose.yml",
353
+ join("docker", "tunnel"),
354
+ "config.example.toml",
355
+ ];
356
+
357
+ /** The compose project the home's switches drive: src/homes.ts `stackRoot`,
358
+ * the one rule every surface that reads the stack uses. */
359
+ export function homeStackRoot(
360
+ env: Readonly<Record<string, string | undefined>>,
361
+ pkgRoot: string = sandboxRoot(),
362
+ ): string {
363
+ return stackRoot(env, pkgRoot);
364
+ }
365
+
366
+ /** Give a stack outside the package the package's own STACK_FILES, again on
367
+ * every launch, so an update brings the stack that matches the app. The
368
+ * person's state there (`.env`, `state/`) is never touched. A copy that
369
+ * fails is left to the compose-file check, which says what is missing. */
370
+ export function seedStack(root: string, pkgRoot: string): void {
371
+ if (root === pkgRoot) return;
372
+ for (const f of STACK_FILES) {
373
+ const from = join(pkgRoot, f);
374
+ if (!existsSync(from)) continue;
375
+ try {
376
+ mkdirSync(root, { recursive: true });
377
+ cpSync(from, join(root, f), { recursive: true, force: true });
378
+ } catch {
379
+ // the compose-file check names it
380
+ }
381
+ }
382
+ }
383
+
384
+ /** The home's options from a process's environment: the env shim's one call. */
385
+ export function homeOptionsFromEnv(env: Readonly<Record<string, string | undefined>>): {
386
+ adapters: HomeAdapters;
387
+ model: string;
388
+ tools: HomeToolSet;
389
+ tune: TunePort;
390
+ advanced: AdvancedPort;
391
+ } {
392
+ const model = (env[HOME_MODEL_ENV] ?? "").trim() || HOME_DEFAULTS.model;
393
+ const tools: HomeToolSet = env[HOME_TOOLS_ENV] === "full" ? "full" : "reviewer";
394
+ const root = homeStackRoot(env);
395
+ seedStack(root, sandboxRoot());
396
+ const configPath = (): string => locateConfig({ env }).path;
397
+ return {
398
+ adapters: realHomeAdapters({
399
+ agentDir: piAgentDir(env),
400
+ configPath,
401
+ reviews: realReviewsPort(env, { root, runner: stepRunner }),
402
+ root,
403
+ spend: realSpendPort({
404
+ registry: join(stateHome(env), "review", "detached"),
405
+ root,
406
+ runner: stepRunner,
407
+ }),
408
+ }),
409
+ model,
410
+ tools,
411
+ tune: checkoutTune({ env, root: assetRoot, configPath, runner: stepRunner }),
412
+ advanced: composeAdvanced({
413
+ root,
414
+ env,
415
+ stepAdapters: stepAdapters(root, configPath),
416
+ }),
417
+ };
418
+ }