orkeon-code 0.0.0-stage → 1.0.0-rc.1

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 (423) hide show
  1. package/.orkeon-version +1 -0
  2. package/LICENSE.md +21 -0
  3. package/NOTICE.md +17 -0
  4. package/README.fr.md +169 -0
  5. package/README.md +162 -2
  6. package/config/appsettings.anthropic.example.json +77 -0
  7. package/config/appsettings.example.json +71 -0
  8. package/config/appsettings.jev.example.json +128 -0
  9. package/config/appsettings.kev.example.json +130 -0
  10. package/config/appsettings.liquid.example.json +129 -0
  11. package/config/appsettings.ollama-whisper.example.json +92 -0
  12. package/config/appsettings.ollama.example.json +70 -0
  13. package/config/appsettings.whisper.example.json +31 -0
  14. package/mcp/whisper/README.md +293 -0
  15. package/mcp/whisper/pyproject.toml +32 -0
  16. package/mcp/whisper/uv.lock +1641 -0
  17. package/mcp/whisper/whisper_mcp.py +1053 -0
  18. package/orkeon-code +897 -0
  19. package/orkeon-code.mjs +71 -0
  20. package/orkeon-code.ps1 +837 -0
  21. package/package.json +57 -4
  22. package/scripts/install.ps1 +156 -0
  23. package/scripts/install.sh +148 -0
  24. package/scripts/session-start.ork.ts +90 -0
  25. package/scripts/session-start.sh +74 -0
  26. package/scripts/setup-fixture.sh +88 -0
  27. package/src/commands/.orkeon/orkeon-cli.d.ts +562 -0
  28. package/src/commands/_lib/args.ts +193 -0
  29. package/src/commands/_lib/dev/dev-batches.ts +368 -0
  30. package/src/commands/_lib/dev/dev-implement.ts +188 -0
  31. package/src/commands/_lib/dev/dev-source.ts +65 -0
  32. package/src/commands/_lib/dev/dev.ts +344 -0
  33. package/src/commands/_lib/dev-step.ts +142 -0
  34. package/src/commands/_lib/dispatch.ts +231 -0
  35. package/src/commands/_lib/effort.ts +15 -0
  36. package/src/commands/_lib/git/branch.ts +95 -0
  37. package/src/commands/_lib/git/commit.ts +94 -0
  38. package/src/commands/_lib/git/diff.ts +97 -0
  39. package/src/commands/_lib/git/review.ts +109 -0
  40. package/src/commands/_lib/memory/memory-admit.ts +150 -0
  41. package/src/commands/_lib/memory/memory.ts +51 -0
  42. package/src/commands/_lib/pr-context.ts +32 -0
  43. package/src/commands/_lib/project/analyze.ts +24 -0
  44. package/src/commands/_lib/session/export.ts +104 -0
  45. package/src/commands/_lib/session/resume.ts +160 -0
  46. package/src/commands/_lib/session/tasks.ts +184 -0
  47. package/src/commands/_lib/settings/advisor.ts +80 -0
  48. package/src/commands/_lib/settings/agents.ts +147 -0
  49. package/src/commands/_lib/settings/config.ts +141 -0
  50. package/src/commands/_lib/settings/hooks.ts +58 -0
  51. package/src/commands/_lib/settings/mode.ts +91 -0
  52. package/src/commands/_lib/settings/model.ts +146 -0
  53. package/src/commands/_lib/settings/permissions.ts +309 -0
  54. package/src/commands/_lib/settings/session-config.ts +49 -0
  55. package/src/commands/_lib/settings/skills.ts +51 -0
  56. package/src/commands/_lib/speech/say.ts +96 -0
  57. package/src/commands/_lib/speech/transcribe.ts +130 -0
  58. package/src/commands/_lib/speech/voice.ts +146 -0
  59. package/src/commands/_lib/status/cache.ts +31 -0
  60. package/src/commands/_lib/status/context.ts +57 -0
  61. package/src/commands/_lib/status/doctor.ts +186 -0
  62. package/src/commands/_lib/status/meta.ts +45 -0
  63. package/src/commands/_lib/status/status.ts +129 -0
  64. package/src/commands/_lib/status/usage-tools.ts +49 -0
  65. package/src/commands/_lib/status/usage.ts +139 -0
  66. package/src/commands/_lib/view.ts +29 -0
  67. package/src/commands/dev/dev-batches.cmd.ts +108 -0
  68. package/src/commands/dev/dev-learn.cmd.ts +29 -0
  69. package/src/commands/dev/dev-plan.cmd.ts +52 -0
  70. package/src/commands/dev/dev-spec.cmd.ts +55 -0
  71. package/src/commands/dev/dev-status.cmd.ts +16 -0
  72. package/src/commands/git/branch.cmd.ts +27 -0
  73. package/src/commands/git/commit.cmd.ts +48 -0
  74. package/src/commands/git/diff.cmd.ts +26 -0
  75. package/src/commands/git/review.cmd.ts +36 -0
  76. package/src/commands/memory/memory.cmd.ts +48 -0
  77. package/src/commands/project/analyze.cmd.ts +32 -0
  78. package/src/commands/project/debug-tool-call.cmd.ts +67 -0
  79. package/src/commands/project/files.cmd.ts +22 -0
  80. package/src/commands/project/init-verifiers.cmd.ts +14 -0
  81. package/src/commands/project/init.cmd.ts +14 -0
  82. package/src/commands/project/verify.cmd.ts +27 -0
  83. package/src/commands/session/assistant.cmd.ts +40 -0
  84. package/src/commands/session/btw.cmd.ts +18 -0
  85. package/src/commands/session/clear.cmd.ts +19 -0
  86. package/src/commands/session/compact.cmd.ts +16 -0
  87. package/src/commands/session/copy.cmd.ts +44 -0
  88. package/src/commands/session/exit.cmd.ts +9 -0
  89. package/src/commands/session/export.cmd.ts +28 -0
  90. package/src/commands/session/rename.cmd.ts +17 -0
  91. package/src/commands/session/resume.cmd.ts +19 -0
  92. package/src/commands/session/rewind.cmd.ts +26 -0
  93. package/src/commands/session/summary.cmd.ts +27 -0
  94. package/src/commands/session/tasks.cmd.ts +44 -0
  95. package/src/commands/settings/advisor.cmd.ts +38 -0
  96. package/src/commands/settings/agents.cmd.ts +19 -0
  97. package/src/commands/settings/config.cmd.ts +17 -0
  98. package/src/commands/settings/effort.cmd.ts +40 -0
  99. package/src/commands/settings/fast.cmd.ts +17 -0
  100. package/src/commands/settings/hooks.cmd.ts +17 -0
  101. package/src/commands/settings/mode.cmd.ts +20 -0
  102. package/src/commands/settings/model.cmd.ts +20 -0
  103. package/src/commands/settings/output-style.cmd.ts +48 -0
  104. package/src/commands/settings/permissions.cmd.ts +25 -0
  105. package/src/commands/settings/proactive.cmd.ts +15 -0
  106. package/src/commands/settings/skills.cmd.ts +17 -0
  107. package/src/commands/speech/say.cmd.ts +24 -0
  108. package/src/commands/speech/transcribe.cmd.ts +29 -0
  109. package/src/commands/speech/voice.cmd.ts +30 -0
  110. package/src/commands/status/context.cmd.ts +18 -0
  111. package/src/commands/status/doctor.cmd.ts +27 -0
  112. package/src/commands/status/release-notes.cmd.ts +9 -0
  113. package/src/commands/status/status.cmd.ts +25 -0
  114. package/src/commands/status/usage.cmd.ts +21 -0
  115. package/src/crews/.orkeon/orkeon.d.ts +3946 -0
  116. package/src/crews/_probe-shared-memory/domain/probe-verdict.ts +183 -0
  117. package/src/crews/_probe-shared-memory/probe.ork.ts +98 -0
  118. package/src/crews/advisor/application/run-advisor.ts +40 -0
  119. package/src/crews/advisor/crew.ork.ts +43 -0
  120. package/src/crews/advisor/domain/advice.ts +135 -0
  121. package/src/crews/agent-run/application/run-agent-run.ts +42 -0
  122. package/src/crews/agent-run/crew.ork.ts +67 -0
  123. package/src/crews/analyze/application/run-analyze.ts +118 -0
  124. package/src/crews/analyze/crew.ork.ts +40 -0
  125. package/src/crews/analyze/domain/harvest.ts +301 -0
  126. package/src/crews/code-review/application/prompts.ts +133 -0
  127. package/src/crews/code-review/application/review.ts +71 -0
  128. package/src/crews/code-review/application/run-code-review.ts +123 -0
  129. package/src/crews/code-review/crew.ork.ts +51 -0
  130. package/src/crews/code-review/domain/lenses.ts +193 -0
  131. package/src/crews/code-review/domain/report.ts +59 -0
  132. package/src/crews/code-review/domain/scope.ts +112 -0
  133. package/src/crews/dev-batches/application/plan-close.ts +181 -0
  134. package/src/crews/dev-batches/application/run-dev-batches.ts +669 -0
  135. package/src/crews/dev-batches/application/run-wave.ts +450 -0
  136. package/src/crews/dev-batches/application/seed-harvest.ts +162 -0
  137. package/src/crews/dev-batches/crew.ork.ts +143 -0
  138. package/src/crews/dev-batches/domain/batch-waves.ts +70 -0
  139. package/src/crews/dev-batches/domain/batches-budget.ts +52 -0
  140. package/src/crews/dev-batches/domain/batches-log.ts +54 -0
  141. package/src/crews/dev-batches/domain/wave-order.ts +43 -0
  142. package/src/crews/dev-change/application/change-prompt.ts +104 -0
  143. package/src/crews/dev-change/application/evidence.ts +169 -0
  144. package/src/crews/dev-change/application/run-dev-change.ts +315 -0
  145. package/src/crews/dev-change/crew.ork.ts +112 -0
  146. package/src/crews/dev-change/domain/change-answer.ts +159 -0
  147. package/src/crews/dev-change/domain/change-render.ts +79 -0
  148. package/src/crews/dev-implement/application/close.ts +183 -0
  149. package/src/crews/dev-implement/application/count-open-steps.ts +24 -0
  150. package/src/crews/dev-implement/application/locate-sheet.ts +80 -0
  151. package/src/crews/dev-implement/application/observe.ts +50 -0
  152. package/src/crews/dev-implement/application/report-writes.ts +41 -0
  153. package/src/crews/dev-implement/application/run-dev-implement.ts +811 -0
  154. package/src/crews/dev-implement/application/run-role.ts +27 -0
  155. package/src/crews/dev-implement/application/suite-job.ts +118 -0
  156. package/src/crews/dev-implement/application/touched.ts +74 -0
  157. package/src/crews/dev-implement/crew.ork.ts +68 -0
  158. package/src/crews/dev-implement/domain/contracts.ts +256 -0
  159. package/src/crews/dev-implement/domain/correction.ts +102 -0
  160. package/src/crews/dev-implement/domain/cycles.ts +35 -0
  161. package/src/crews/dev-implement/domain/implement-budget.ts +29 -0
  162. package/src/crews/dev-implement/domain/roles.ts +53 -0
  163. package/src/crews/dev-implement/domain/sheet-edits.ts +75 -0
  164. package/src/crews/dev-implement/domain/sheet-ticks.ts +22 -0
  165. package/src/crews/dev-implement/domain/step-checks.ts +136 -0
  166. package/src/crews/dev-implement/domain/step-section.ts +127 -0
  167. package/src/crews/dev-learn/application/learn-backlog.ts +58 -0
  168. package/src/crews/dev-learn/application/learn-prompt.ts +140 -0
  169. package/src/crews/dev-learn/application/run-dev-learn.ts +553 -0
  170. package/src/crews/dev-learn/crew.ork.ts +52 -0
  171. package/src/crews/dev-learn/domain/learn-answer.ts +235 -0
  172. package/src/crews/dev-learn/domain/owner-lines.ts +86 -0
  173. package/src/crews/dev-learn/domain/proposals-render.ts +96 -0
  174. package/src/crews/dev-plan/application/anchor-paths.ts +76 -0
  175. package/src/crews/dev-plan/application/inventory.ts +170 -0
  176. package/src/crews/dev-plan/application/plan-prompt.ts +159 -0
  177. package/src/crews/dev-plan/application/run-dev-plan.ts +455 -0
  178. package/src/crews/dev-plan/crew.ork.ts +109 -0
  179. package/src/crews/dev-plan/domain/inventory-model.ts +95 -0
  180. package/src/crews/dev-plan/domain/plan-answer.ts +738 -0
  181. package/src/crews/dev-plan/domain/plan-render.ts +307 -0
  182. package/src/crews/dev-spec/application/run-dev-spec.ts +206 -0
  183. package/src/crews/dev-spec/application/spec-prompt.ts +98 -0
  184. package/src/crews/dev-spec/crew.ork.ts +108 -0
  185. package/src/crews/dev-spec/domain/spec-answer.ts +253 -0
  186. package/src/crews/dev-spec/domain/spec-render.ts +76 -0
  187. package/src/crews/dev-verify/application/audit.ts +259 -0
  188. package/src/crews/dev-verify/application/auditor-prompt.ts +323 -0
  189. package/src/crews/dev-verify/application/capture.ts +303 -0
  190. package/src/crews/dev-verify/application/run-dev-verify.ts +409 -0
  191. package/src/crews/dev-verify/application/run-gate.ts +202 -0
  192. package/src/crews/dev-verify/crew.ork.ts +103 -0
  193. package/src/crews/dev-verify/domain/audit-answer.ts +150 -0
  194. package/src/crews/dev-verify/domain/auditor-charter.ts +21 -0
  195. package/src/crews/dev-verify/domain/capture-sections.ts +137 -0
  196. package/src/crews/dev-verify/domain/commands.ts +103 -0
  197. package/src/crews/dev-verify/domain/gate.ts +126 -0
  198. package/src/crews/dev-verify/domain/mechanical-table.ts +163 -0
  199. package/src/crews/dev-verify/domain/runtime-cut.ts +13 -0
  200. package/src/crews/dev-verify/domain/sheet-facts.ts +4 -0
  201. package/src/crews/dev-verify/domain/text.ts +20 -0
  202. package/src/crews/dev-verify/domain/verdict.ts +127 -0
  203. package/src/crews/git-commit/application/draft.ts +154 -0
  204. package/src/crews/git-commit/application/run-git-commit.ts +116 -0
  205. package/src/crews/git-commit/crew.ork.ts +52 -0
  206. package/src/crews/git-commit/domain/commit.ts +220 -0
  207. package/src/crews/init-verifiers/application/authoring.ts +89 -0
  208. package/src/crews/init-verifiers/application/prompts.ts +103 -0
  209. package/src/crews/init-verifiers/application/run-init-verifiers.ts +25 -0
  210. package/src/crews/init-verifiers/crew.ork.ts +34 -0
  211. package/src/crews/init-verifiers/domain/commands-section.ts +36 -0
  212. package/src/crews/init-verifiers/domain/readiness.ts +23 -0
  213. package/src/crews/main-loop/application/auto-compact.ts +77 -0
  214. package/src/crews/main-loop/application/context-gather.ts +133 -0
  215. package/src/crews/main-loop/application/run-main-loop.ts +350 -0
  216. package/src/crews/main-loop/application/turn-prompt.ts +112 -0
  217. package/src/crews/main-loop/crew.ork.ts +119 -0
  218. package/src/crews/main-loop/domain/autocompact.ts +27 -0
  219. package/src/crews/main-loop/domain/prompt-assembly.ts +521 -0
  220. package/src/crews/main-loop/domain/turn-chains.ts +129 -0
  221. package/src/crews/project-init/application/detect.ts +122 -0
  222. package/src/crews/project-init/application/prompts.ts +101 -0
  223. package/src/crews/project-init/application/run-project-init.ts +31 -0
  224. package/src/crews/project-init/crew.ork.ts +35 -0
  225. package/src/crews/project-init/domain/conventions.ts +36 -0
  226. package/src/crews/project-init/domain/existing-files.ts +63 -0
  227. package/src/crews/project-init/domain/package-map.ts +73 -0
  228. package/src/crews/session-compact/crew.ork.ts +55 -0
  229. package/src/crews/shared-memory-admit/crew.ork.ts +48 -0
  230. package/src/crews/speech/application/run-speech.ts +25 -0
  231. package/src/crews/speech/crew.ork.ts +37 -0
  232. package/src/shared/application/agents/child-agent.ts +255 -0
  233. package/src/shared/application/agents/run-sub-agent.ts +96 -0
  234. package/src/shared/application/answer-language.ts +14 -0
  235. package/src/shared/application/attached-skills.ts +36 -0
  236. package/src/shared/application/compaction/compact-session.ts +237 -0
  237. package/src/shared/application/compaction/extract-memories.ts +38 -0
  238. package/src/shared/application/compaction/restore-context.ts +78 -0
  239. package/src/shared/application/config-store.ts +123 -0
  240. package/src/shared/application/dev/brief-inventory.ts +113 -0
  241. package/src/shared/application/dev/close-batch.ts +83 -0
  242. package/src/shared/application/dev/find-repo-pack.ts +77 -0
  243. package/src/shared/application/dev/learn-decision.ts +252 -0
  244. package/src/shared/application/dev/load-dev-context.ts +266 -0
  245. package/src/shared/application/dev/measure-access-cost.ts +68 -0
  246. package/src/shared/application/dev/plan-source.ts +112 -0
  247. package/src/shared/application/dev/preflight.ts +71 -0
  248. package/src/shared/application/dev/project-files.ts +17 -0
  249. package/src/shared/application/dev/question-round.ts +52 -0
  250. package/src/shared/application/dev/resolve-work-folder.ts +133 -0
  251. package/src/shared/application/dev/script-tool-slot.ts +27 -0
  252. package/src/shared/application/dev/settle-questions.ts +55 -0
  253. package/src/shared/application/dev/source-review.ts +63 -0
  254. package/src/shared/application/dev/source-step.ts +150 -0
  255. package/src/shared/application/dev/spend.ts +42 -0
  256. package/src/shared/application/dev/step-precedents.ts +89 -0
  257. package/src/shared/application/dev/tool-call-policy.ts +89 -0
  258. package/src/shared/application/dev/tool-callbacks.ts +215 -0
  259. package/src/shared/application/dev/tool-trace-recorder.ts +45 -0
  260. package/src/shared/application/dev/verdict-sources.ts +48 -0
  261. package/src/shared/application/dev/wrapped-tools.ts +145 -0
  262. package/src/shared/application/extra-tools.ts +70 -0
  263. package/src/shared/application/git/batch-worktree.ts +97 -0
  264. package/src/shared/application/git/commit-files.ts +186 -0
  265. package/src/shared/application/git/merge-batch.ts +63 -0
  266. package/src/shared/application/git/open-pull-request.ts +51 -0
  267. package/src/shared/application/instructions-file.ts +19 -0
  268. package/src/shared/application/json-file.ts +34 -0
  269. package/src/shared/application/llm-profile.ts +35 -0
  270. package/src/shared/application/permission-rules.ts +159 -0
  271. package/src/shared/application/ports/approvals.ts +20 -0
  272. package/src/shared/application/ports/clock.ts +5 -0
  273. package/src/shared/application/ports/code-index.ts +93 -0
  274. package/src/shared/application/ports/crew.ts +60 -0
  275. package/src/shared/application/ports/files.ts +21 -0
  276. package/src/shared/application/ports/human.ts +13 -0
  277. package/src/shared/application/ports/llm.ts +54 -0
  278. package/src/shared/application/ports/memory.ts +30 -0
  279. package/src/shared/application/ports/progress.ts +23 -0
  280. package/src/shared/application/ports/runtime.ts +15 -0
  281. package/src/shared/application/ports/session.ts +74 -0
  282. package/src/shared/application/ports/shared-memory.ts +26 -0
  283. package/src/shared/application/ports/shell.ts +40 -0
  284. package/src/shared/application/ports/telemetry.ts +45 -0
  285. package/src/shared/application/ports/tools.ts +46 -0
  286. package/src/shared/application/ports/usage.ts +45 -0
  287. package/src/shared/application/ports.ts +27 -0
  288. package/src/shared/application/project-manifests.ts +106 -0
  289. package/src/shared/application/session-io.ts +84 -0
  290. package/src/shared/application/session-state.ts +36 -0
  291. package/src/shared/application/shared-memory/admit-entries.ts +177 -0
  292. package/src/shared/application/shared-memory/harvest-after-gaps.ts +72 -0
  293. package/src/shared/application/shared-memory/harvest-mistakes.ts +44 -0
  294. package/src/shared/application/shared-memory/import-agent-memory.ts +117 -0
  295. package/src/shared/application/shared-memory/memory-files.ts +110 -0
  296. package/src/shared/application/shared-memory/memory-settings.ts +93 -0
  297. package/src/shared/application/shared-memory/push-candidate.ts +146 -0
  298. package/src/shared/application/shared-memory/recall-precedents.ts +69 -0
  299. package/src/shared/application/state-snapshot.ts +38 -0
  300. package/src/shared/application/workflow/workflow-state.ts +135 -0
  301. package/src/shared/domain/agent-policy/behavior-flags.ts +168 -0
  302. package/src/shared/domain/agent-policy/extra-tools.ts +69 -0
  303. package/src/shared/domain/agent-policy/memory-categories.ts +6 -0
  304. package/src/shared/domain/agent-policy/mode-policy.ts +51 -0
  305. package/src/shared/domain/agent-policy/output-styles.ts +11 -0
  306. package/src/shared/domain/agent-policy/permission.ts +623 -0
  307. package/src/shared/domain/agent-policy/rule-sets.ts +412 -0
  308. package/src/shared/domain/agent-policy/tool-access.ts +77 -0
  309. package/src/shared/domain/agent-registry.ts +142 -0
  310. package/src/shared/domain/agents/built-in-charters.ts +43 -0
  311. package/src/shared/domain/agents/chain.ts +30 -0
  312. package/src/shared/domain/agents/charter.ts +61 -0
  313. package/src/shared/domain/agents/exclusive.ts +24 -0
  314. package/src/shared/domain/agents/instances.ts +249 -0
  315. package/src/shared/domain/code-intel-contract.ts +356 -0
  316. package/src/shared/domain/code-intel-routing.ts +113 -0
  317. package/src/shared/domain/compact-outcome.ts +44 -0
  318. package/src/shared/domain/compaction-thresholds.ts +161 -0
  319. package/src/shared/domain/compaction.ts +379 -0
  320. package/src/shared/domain/config-schema.ts +421 -0
  321. package/src/shared/domain/dev/access-cost.ts +226 -0
  322. package/src/shared/domain/dev/artefacts.ts +50 -0
  323. package/src/shared/domain/dev/batches-concurrency.ts +36 -0
  324. package/src/shared/domain/dev/batches-state.ts +49 -0
  325. package/src/shared/domain/dev/build-commands.ts +44 -0
  326. package/src/shared/domain/dev/call-guard.ts +341 -0
  327. package/src/shared/domain/dev/carried-text.ts +49 -0
  328. package/src/shared/domain/dev/design-rules.ts +100 -0
  329. package/src/shared/domain/dev/learn-proposals.ts +175 -0
  330. package/src/shared/domain/dev/learn-state.ts +93 -0
  331. package/src/shared/domain/dev/path-args.ts +42 -0
  332. package/src/shared/domain/dev/path-rebase.ts +93 -0
  333. package/src/shared/domain/dev/plan-checks.ts +190 -0
  334. package/src/shared/domain/dev/plan-format.ts +180 -0
  335. package/src/shared/domain/dev/project-files.ts +43 -0
  336. package/src/shared/domain/dev/questions.ts +249 -0
  337. package/src/shared/domain/dev/reading-rule.ts +41 -0
  338. package/src/shared/domain/dev/repo-pack.ts +237 -0
  339. package/src/shared/domain/dev/review-answer.ts +150 -0
  340. package/src/shared/domain/dev/review-prompt.ts +221 -0
  341. package/src/shared/domain/dev/reviewer-charter.ts +31 -0
  342. package/src/shared/domain/dev/sheet-facts.ts +83 -0
  343. package/src/shared/domain/dev/spec-format.ts +183 -0
  344. package/src/shared/domain/dev/step-budget.ts +72 -0
  345. package/src/shared/domain/dev/step-rules.ts +110 -0
  346. package/src/shared/domain/dev/test-commands.ts +90 -0
  347. package/src/shared/domain/dev/test-output.ts +33 -0
  348. package/src/shared/domain/dev/test-paths.ts +40 -0
  349. package/src/shared/domain/dev/tool-guards.ts +64 -0
  350. package/src/shared/domain/dev/trait-pattern.ts +83 -0
  351. package/src/shared/domain/dev/verdict-format.ts +169 -0
  352. package/src/shared/domain/dev/whitespace.ts +34 -0
  353. package/src/shared/domain/dev/work-folder.ts +67 -0
  354. package/src/shared/domain/dev/yaml-subset.ts +403 -0
  355. package/src/shared/domain/git/close.ts +273 -0
  356. package/src/shared/domain/git/pull-request.ts +96 -0
  357. package/src/shared/domain/git/worktree.ts +46 -0
  358. package/src/shared/domain/index-languages.ts +16 -0
  359. package/src/shared/domain/instructions-file.ts +18 -0
  360. package/src/shared/domain/iso-day.ts +4 -0
  361. package/src/shared/domain/llm-failure.ts +106 -0
  362. package/src/shared/domain/llm-profile.ts +25 -0
  363. package/src/shared/domain/marked-message.ts +33 -0
  364. package/src/shared/domain/model-catalog.ts +86 -0
  365. package/src/shared/domain/permission-modes.ts +103 -0
  366. package/src/shared/domain/prelude.ts +92 -0
  367. package/src/shared/domain/project-manifests.ts +83 -0
  368. package/src/shared/domain/search-routing.ts +85 -0
  369. package/src/shared/domain/secret-patterns.ts +45 -0
  370. package/src/shared/domain/session-keys.ts +85 -0
  371. package/src/shared/domain/session-start.ts +60 -0
  372. package/src/shared/domain/session-summary.ts +13 -0
  373. package/src/shared/domain/severity.ts +72 -0
  374. package/src/shared/domain/sha1.ts +61 -0
  375. package/src/shared/domain/shared-memory/agent-memory-import.ts +143 -0
  376. package/src/shared/domain/shared-memory/candidates.ts +41 -0
  377. package/src/shared/domain/shared-memory/entry.ts +145 -0
  378. package/src/shared/domain/shared-memory/front-matter.ts +183 -0
  379. package/src/shared/domain/shared-memory/gate.ts +44 -0
  380. package/src/shared/domain/shared-memory/harvest.ts +61 -0
  381. package/src/shared/domain/shared-memory/layout.ts +187 -0
  382. package/src/shared/domain/shared-memory/precedents-block.ts +93 -0
  383. package/src/shared/domain/shared-memory/refused.ts +84 -0
  384. package/src/shared/domain/shared-memory/verdict-rows.ts +134 -0
  385. package/src/shared/domain/snip-turns.ts +45 -0
  386. package/src/shared/domain/speech.ts +20 -0
  387. package/src/shared/domain/strip.ts +88 -0
  388. package/src/shared/domain/tool-trace.ts +122 -0
  389. package/src/shared/domain/types.ts +67 -0
  390. package/src/shared/domain/verifiers.ts +37 -0
  391. package/src/shared/domain/vfs.ts +9 -0
  392. package/src/shared/domain/workflow/state-file.ts +140 -0
  393. package/src/shared/infrastructure/globals.ts +52 -0
  394. package/src/shared/infrastructure/orkeon/approvals.ts +24 -0
  395. package/src/shared/infrastructure/orkeon/child-agents.ts +127 -0
  396. package/src/shared/infrastructure/orkeon/code-index.ts +120 -0
  397. package/src/shared/infrastructure/orkeon/crew-host.ts +27 -0
  398. package/src/shared/infrastructure/orkeon/crew-memory.ts +12 -0
  399. package/src/shared/infrastructure/orkeon/dev-deps.ts +172 -0
  400. package/src/shared/infrastructure/orkeon/files.ts +40 -0
  401. package/src/shared/infrastructure/orkeon/github-pull-requests.ts +29 -0
  402. package/src/shared/infrastructure/orkeon/host-provider.ts +22 -0
  403. package/src/shared/infrastructure/orkeon/human-input.ts +59 -0
  404. package/src/shared/infrastructure/orkeon/llm-profile.ts +38 -0
  405. package/src/shared/infrastructure/orkeon/llm.ts +109 -0
  406. package/src/shared/infrastructure/orkeon/memory-store.ts +41 -0
  407. package/src/shared/infrastructure/orkeon/memory-tools.ts +131 -0
  408. package/src/shared/infrastructure/orkeon/permission-rules.ts +42 -0
  409. package/src/shared/infrastructure/orkeon/progress.ts +24 -0
  410. package/src/shared/infrastructure/orkeon/run-state.ts +17 -0
  411. package/src/shared/infrastructure/orkeon/runtime-info.ts +30 -0
  412. package/src/shared/infrastructure/orkeon/runtime-tools.ts +60 -0
  413. package/src/shared/infrastructure/orkeon/script-tools.ts +134 -0
  414. package/src/shared/infrastructure/orkeon/session-store.ts +138 -0
  415. package/src/shared/infrastructure/orkeon/session-switch.ts +23 -0
  416. package/src/shared/infrastructure/orkeon/shared-memory.ts +61 -0
  417. package/src/shared/infrastructure/orkeon/shell-jobs.ts +75 -0
  418. package/src/shared/infrastructure/orkeon/shell.ts +31 -0
  419. package/src/shared/infrastructure/orkeon/telemetry.ts +63 -0
  420. package/src/shared/infrastructure/orkeon/tool-catalogue.ts +75 -0
  421. package/src/shared/infrastructure/orkeon/tool-results.ts +120 -0
  422. package/src/shared/infrastructure/orkeon/usage.ts +74 -0
  423. package/src/shared/infrastructure/orkeon/wiring.ts +118 -0
package/orkeon-code ADDED
@@ -0,0 +1,897 @@
1
+ #!/usr/bin/env bash
2
+ # orkeon-code: start the Orkeon Code REPL on a project.
3
+ #
4
+ # ./orkeon-code # the current directory is the project
5
+ # ./orkeon-code --workspace ~/src/app # another project
6
+ # ./orkeon-code --demo # the throwaway fixture (scripts/setup-fixture.sh)
7
+ # ./orkeon-code --mode docker|tool|source [--settings FILE] [-- extra REPL args]
8
+ # ./orkeon-code --version # Code's version, where it runs from, the Orkeon expected and found
9
+ # ./orkeon-code --trust # this run only, without asking (nothing stored)
10
+ # ./orkeon-code -c | --continue # the last session of this project, its conversation back
11
+ # ./orkeon-code --resume [<n> | <id>] # list the saved sessions of this project and pick one
12
+ # ./orkeon-code trust list | add <path> | remove <path> | check <path> [--trust] # check: exit 0, 3 or 4
13
+ # ./orkeon-code setup # write ~/.orkeon-code/appsettings.json (never the key)
14
+ #
15
+ # Folder trust: the first start in a folder asks whether you trust it, before anything of it is read
16
+ # or mounted; `y` stores it in ~/.orkeon-code/trusted-folders.json, and every folder below it starts
17
+ # without asking. Without a terminal an untrusted folder is refused (exit 3) unless --trust is given,
18
+ # or ORKEON_CODE_TRUST=1 with CI=true; neither stores anything. --demo trusts its own fixture.
19
+ #
20
+ # What runs: the Orkeon scripted-commands REPL (orkeon-repl) loads the slash
21
+ # commands of src/commands and the crews of src/crews. The project is mounted on
22
+ # the VFS path /workspace (read-write): every file tool, the shell tool and git
23
+ # work there. /output and /tmp are scratch mounts under the state directory, and
24
+ # /user-config (read-only) holds the orkeonCode block of the settings file: the
25
+ # user's own settings, which the project's .orkeon/config.json cannot widen.
26
+ #
27
+ # A clone (a .git or tests/ beside this script) keeps its state in .crew-output and its demo in
28
+ # fixtures/. An install (scripts/install.sh, npm) never writes to its own folder: its demo goes
29
+ # under ORKEON_CODE_HOME (default ~/.orkeon-code) in demo/, the state of each project in
30
+ # projects/<slug>/ (<slug>: the folder's name and a hash of its path, the runtime's own).
31
+ #
32
+ # Sessions: every start is a fresh session. The runtime saves the conversation when the REPL ends,
33
+ # outside the project (<state>/sessions, or .crew-output/projects/<slug>/sessions in a clone); the
34
+ # next start moves it to history/ beside it, where the 50 newest are kept
35
+ # (ORKEON_CODE_SESSIONS_KEEP). --continue brings the last one back, --resume <n or id> an older one;
36
+ # /resume lists them inside the REPL. A bare --llm-log after -- goes to <state>/llm-logs.
37
+ #
38
+ # Modes (auto-detected in this order, or forced with --mode / ORKEON_CODE_MODE):
39
+ # tool orkeon-repl on PATH (from the orkeon Debian package, an Orkeon release archive, or the
40
+ # dotnet tool), or ORKEON_REPL="<command line>" (e.g. "dotnet /opt/orkeon/Orkeon.ConsoleApp.dll")
41
+ # docker ghcr.io/orkeon/orkeon-runners:latest (the last release, not the pin), nothing else to install
42
+ # source an Orkeon checkout named by ORKEON_SOURCE, built there with `dotnet build`, run from the project
43
+ #
44
+ # Settings: --settings, else ORKEON_CODE_SETTINGS, else config/appsettings.local.json (a clone),
45
+ # else ~/.orkeon-code/appsettings.json, else ${XDG_CONFIG_HOME:-~/.config}/orkeon-code/appsettings.json
46
+ # (the old place, read with a warning), else Orkeon's own ${XDG_CONFIG_HOME:-~/.config}/Orkeon/appsettings.json,
47
+ # which the REPL then reads itself. None and a terminal: `setup` runs. Start from
48
+ # config/appsettings.example.json. The API key stays in the environment: the
49
+ # variable named by Llm:ApiKeyEnvVar, or ORKEON_Llm__ApiKey which wins.
50
+ # ORKEON_CODE_MODEL overrides Llm:Model for this run.
51
+ # ORKEON_CODE_AUDIO_DIR=<folder> turns speech-to-text on (tool and source modes): the folder is
52
+ # mounted on /audio (read-only), <state>/transcripts on /transcripts (read-only), and the
53
+ # Whisper MCP server of mcp/whisper is declared to the REPL, run by uv (mcp/whisper/README.md).
54
+ # ORKEON_CODE_VOICE=1 also opens the microphone (/voice) and the speaker (/say, voice.reply).
55
+ set -euo pipefail
56
+ # A CDPATH from the caller would make cd print the folder it enters, into the paths read from it.
57
+ unset CDPATH
58
+
59
+ # Prints <path> with every symlink of its last component followed.
60
+ resolve_links() { # <path>
61
+ local path="$1" target
62
+ while [ -L "$path" ]; do
63
+ target="$(readlink "$path")"
64
+ case "$target" in /*) path="$target" ;; *) path="$(dirname "$path")/$target" ;; esac
65
+ done
66
+ printf '%s\n' "$path"
67
+ }
68
+
69
+ # Follow symlinks, so a link from ~/.local/bin still finds the checkout or the install's version folder.
70
+ SELF="$(resolve_links "${BASH_SOURCE[0]}")"
71
+ ROOT="$(cd "$(dirname "$SELF")" && pwd -P)"
72
+ # The pin is a commit of Orkeon's main (.orkeon-version, ADR 0013): the source mode runs it; the tool and docker
73
+ # modes run a release, which the pin never names.
74
+ ORKEON_REF="$(tr -d '[:space:]' < "$ROOT/.orkeon-version")"
75
+ ORKEON_PIN="${ORKEON_REF:0:7}"
76
+ CODE_VERSION="$(sed -n 's/.*"version": *"\([^"]*\)".*/\1/p' "$ROOT/package.json" | head -n 1)"
77
+ IMAGE="${ORKEON_CODE_IMAGE:-ghcr.io/orkeon/orkeon-runners:latest}"
78
+ # A clone (a .git, folder or worktree file, or the tests of an unpacked source archive) or an
79
+ # install, whose folder is never written to.
80
+ if [ -e "$ROOT/.git" ] || [ -d "$ROOT/tests" ]; then KIND=clone; else KIND=install; fi
81
+ CODE_HOME="${ORKEON_CODE_HOME:-$HOME/.orkeon-code}"
82
+
83
+ die() { echo "orkeon-code: $*" >&2; exit 1; }
84
+ warn() { echo "orkeon-code: warning: $*" >&2; }
85
+
86
+ # Moves the Orkeon checkout of ORKEON_SOURCE from <head> to the pin, when nothing of the user's is in the way: no
87
+ # change to a tracked file, on main or detached, <head> an ancestor of the pin (fetched from origin's main when
88
+ # missing). Fails, moving nothing, otherwise or with ORKEON_SOURCE_FOLLOW_PIN=0.
89
+ follow_pin() { # <head>
90
+ local branch
91
+ [ "${ORKEON_SOURCE_FOLLOW_PIN:-1}" != 0 ] || return 1
92
+ [ -z "$(git -C "$ORKEON_SOURCE" status --porcelain --untracked-files=no --ignore-submodules=all 2>/dev/null)" ] || return 1
93
+ branch="$(git -C "$ORKEON_SOURCE" symbolic-ref -q --short HEAD || true)"
94
+ case "$branch" in ""|main) ;; *) return 1 ;; esac
95
+ git -C "$ORKEON_SOURCE" cat-file -e "$ORKEON_REF^{commit}" 2>/dev/null \
96
+ || git -C "$ORKEON_SOURCE" fetch -q origin main 2>/dev/null || return 1
97
+ git -C "$ORKEON_SOURCE" merge-base --is-ancestor "$1" "$ORKEON_REF" 2>/dev/null || return 1
98
+ if [ -n "$branch" ]; then git -C "$ORKEON_SOURCE" merge -q --ff-only "$ORKEON_REF" >/dev/null 2>&1 || return 1
99
+ else git -C "$ORKEON_SOURCE" checkout -q --detach "$ORKEON_REF" 2>/dev/null || return 1
100
+ fi
101
+ echo "orkeon-code: ORKEON_SOURCE moved from ${1:0:7} to the pin $ORKEON_PIN." >&2
102
+ }
103
+ lowered() { printf '%s' "$1" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//' | tr '[:upper:]' '[:lower:]'; }
104
+
105
+ # The Orkeon found, said without starting anything: the REPL has no --version flag, and docker pulls nothing.
106
+ # A mode forced by --mode or ORKEON_CODE_MODE names its runtime; otherwise the auto-detection order speaks.
107
+ # The source checkout, with its commit when it is a git repository: the line compares it with the pin.
108
+ source_found() {
109
+ local head
110
+ [ -n "${ORKEON_SOURCE:-}" ] || { echo "source checkout (none: set ORKEON_SOURCE)"; return; }
111
+ if head="$(git -C "$ORKEON_SOURCE" rev-parse --short=7 HEAD 2>/dev/null)"; then echo "source checkout $ORKEON_SOURCE at $head"
112
+ else echo "source checkout $ORKEON_SOURCE"
113
+ fi
114
+ }
115
+
116
+ runtime_found() { # <forced mode or empty>
117
+ local repl repl_bin version_file v
118
+ if [ -n "${ORKEON_REPL:-}" ]; then read -r repl _ <<< "$ORKEON_REPL"; else repl=orkeon-repl; fi
119
+ case "$1" in
120
+ source) source_found; return ;;
121
+ docker) echo "image $IMAGE (docker mode, not pulled)"; return ;;
122
+ esac
123
+ if repl_bin="$(command -v "$repl" 2>/dev/null)"; then
124
+ repl_bin="$(resolve_links "$repl_bin")"
125
+ # An Orkeon release archive installs bin/orkeon-repl beside its VERSION file.
126
+ version_file="$(dirname "$(dirname "$repl_bin")")/VERSION"
127
+ if [ -f "$version_file" ]; then echo "$(tr -d '[:space:]' < "$version_file") ($repl, $version_file)"
128
+ elif v="$(dpkg-query -W -f='${Version}' orkeon 2>/dev/null)" && [ -n "$v" ]; then echo "$v ($repl, the orkeon package)"
129
+ else echo "unknown ($repl_bin)"
130
+ fi
131
+ elif command -v docker >/dev/null 2>&1; then echo "image $IMAGE (docker mode, not pulled)"
132
+ elif [ -n "${ORKEON_SOURCE:-}" ]; then source_found
133
+ else echo "none (see docs/getting-started.md)"
134
+ fi
135
+ }
136
+
137
+ # ── Folder trust ───────────────────────────────────────────────────────────
138
+ # The store holds what the user trusted, one folder per line in the layout write_trust_store gives it: read
139
+ # without node (docker mode needs none), any other layout is refused, never trusted and never rewritten.
140
+ TRUST_FILE="$CODE_HOME/trusted-folders.json"
141
+
142
+ # Prints the stored folders, `<path><TAB><trustedAt>` per line; exits 1, naming the file, on a store it cannot read.
143
+ trust_entries() {
144
+ [ -s "$TRUST_FILE" ] || return 0
145
+ awk '
146
+ function bad() { failed = 1; exit 1 }
147
+ # A JSON string from its first character after the opening quote; sets STR and REST, 0 when it is not one.
148
+ function string(s, i, c, n) {
149
+ STR = ""
150
+ for (i = 1; i <= length(s); i++) {
151
+ c = substr(s, i, 1)
152
+ if (c == "\"") { REST = substr(s, i + 1); return 1 }
153
+ if (c == "\\") {
154
+ n = substr(s, i + 1, 1)
155
+ if (n == "\\" || n == "\"" || n == "/") { STR = STR n; i++; continue }
156
+ return 0
157
+ }
158
+ if (c ~ /[[:cntrl:]]/) return 0
159
+ STR = STR c
160
+ }
161
+ return 0
162
+ }
163
+ { sub(/\r$/, "") }
164
+ /^[ \t]*$/ { next }
165
+ step == 0 { if ($0 !~ /^[ \t]*\{[ \t]*$/) bad(); step = 1; next }
166
+ step == 1 { if ($0 !~ /^[ \t]*"version"[ \t]*:[ \t]*1[ \t]*,[ \t]*$/) bad(); step = 2; next }
167
+ step == 2 {
168
+ if ($0 ~ /^[ \t]*"folders"[ \t]*:[ \t]*\[[ \t]*\][ \t]*$/) { step = 4; next }
169
+ if ($0 !~ /^[ \t]*"folders"[ \t]*:[ \t]*\[[ \t]*$/) bad(); step = 3; next
170
+ }
171
+ step == 3 && /^[ \t]*\][ \t]*$/ { if (count > 0 && comma) bad(); step = 4; next }
172
+ step == 3 {
173
+ if (count > 0 && !comma) bad()
174
+ line = $0
175
+ if (!sub(/^[ \t]*\{[ \t]*"path"[ \t]*:[ \t]*"/, "", line) || !string(line) || STR == "") bad()
176
+ path = STR; line = REST
177
+ if (!sub(/^[ \t]*,[ \t]*"trustedAt"[ \t]*:[ \t]*"/, "", line) || !string(line)) bad()
178
+ date = STR; line = REST
179
+ if (line ~ /^[ \t]*\}[ \t]*,[ \t]*$/) comma = 1
180
+ else if (line ~ /^[ \t]*\}[ \t]*$/) comma = 0
181
+ else bad()
182
+ count++; out[count] = path "\t" date; next
183
+ }
184
+ step == 4 { if ($0 !~ /^[ \t]*\}[ \t]*$/) bad(); step = 5; next }
185
+ { bad() }
186
+ END {
187
+ if (failed || step != 5) exit 1
188
+ for (i = 1; i <= count; i++) print out[i]
189
+ }
190
+ ' "$TRUST_FILE" || die "$TRUST_FILE is not a trust store this launcher wrote: nothing is trusted, and the file is kept as it is. Fix it, or move it away and trust your folders again (orkeon-code trust add <path>)."
191
+ }
192
+
193
+ # Writes the store from `<path><TAB><trustedAt>` lines on stdin: a temporary file, then a rename.
194
+ write_trust_store() {
195
+ local entries=() line path date first=1 tmp
196
+ while IFS= read -r line; do [ -n "$line" ] && entries+=("$line"); done
197
+ [ -d "$CODE_HOME" ] || { mkdir -p "$CODE_HOME" && chmod 700 "$CODE_HOME"; } || die "could not create $CODE_HOME."
198
+ tmp="$TRUST_FILE.tmp.$$"
199
+ (
200
+ umask 077
201
+ {
202
+ printf '{\n "version": 1,\n'
203
+ if [ ${#entries[@]} -eq 0 ]; then printf ' "folders": []\n'
204
+ else
205
+ printf ' "folders": [\n'
206
+ for line in "${entries[@]}"; do
207
+ path="${line%%$'\t'*}"; date="${line#*$'\t'}"
208
+ path="${path//\\/\\\\}"; path="${path//\"/\\\"}"
209
+ if [ "$first" = 1 ]; then first=0; else printf ',\n'; fi
210
+ printf ' { "path": "%s", "trustedAt": "%s" }' "$path" "$date"
211
+ done
212
+ printf '\n ]\n'
213
+ fi
214
+ printf '}\n'
215
+ } > "$tmp"
216
+ ) && mv -f "$tmp" "$TRUST_FILE" || { rm -f "$tmp"; die "could not write $TRUST_FILE."; }
217
+ }
218
+
219
+ # The folder, physical (every symlink resolved), or nothing when it is not a folder.
220
+ canonical_dir() { (cd "$1" 2>/dev/null && pwd -P); }
221
+
222
+ # The runtime's folder name for a project (ProjectKey.Slug, Orkeon.Infrastructure/Session/ProjectKey.cs): the last
223
+ # segment, each UTF-16 unit an ASCII letter or digit lowercased, else '-', then '-' and the first 12 hex of the
224
+ # SHA-256 of the whole path. Bytes under LC_ALL=C: a 4-byte UTF-8 sequence is two UTF-16 units, any other one.
225
+ project_slug() ( # <path>
226
+ LC_ALL=C
227
+ key="$1"; name="$1"; out=""; i=0
228
+ while case "$name" in */|*\\) true ;; *) false ;; esac; do name="${name%?}"; done
229
+ name="${name##*[/\\]}"
230
+ while [ "$i" -lt "${#name}" ]; do
231
+ case "${name:i:1}" in
232
+ [A-Za-z0-9]) out="$out${name:i:1}" ;;
233
+ [$'\x80'-$'\xbf']) ;;
234
+ [$'\xf0'-$'\xf7']) out="$out--" ;;
235
+ *) out="$out-" ;;
236
+ esac
237
+ i=$((i + 1))
238
+ done
239
+ if command -v sha256sum >/dev/null 2>&1; then hash="$(printf '%s' "$key" | sha256sum)"
240
+ else hash="$(printf '%s' "$key" | shasum -a 256)"; fi
241
+ printf '%s-%s\n' "$(printf '%s' "$out" | tr 'A-Z' 'a-z')" "${hash:0:12}"
242
+ )
243
+
244
+ # <string> as a JSON string, the trust store's escaping (\ and ").
245
+ json_string() { local s="${1//\\/\\\\}"; printf '"%s"' "${s//\"/\\\"}"; }
246
+
247
+ # A string field of a session file as stored (its escapes unread), or nothing.
248
+ session_field() { # <file> <field>
249
+ { sed -nE 's/.*"'"$2"'"[[:space:]]*:[[:space:]]*"(([^"\\]|\\.)*)".*/\1/p' "$1" 2>/dev/null || true; } | head -n 1
250
+ }
251
+
252
+ # The messages of a session file: one "Role" per message.
253
+ session_messages() { # <file>
254
+ { grep -oE '"Role"[[:space:]]*:' "$1" 2>/dev/null || true; } | wc -l | tr -d ' '
255
+ }
256
+
257
+ # 0 when a session file holds no message ("Messages":[]): a start and a quit.
258
+ session_empty() { # <file>
259
+ grep -qE '"Messages"[[:space:]]*:[[:space:]]*\[[[:space:]]*\]' "$1" 2>/dev/null
260
+ }
261
+
262
+ # Removes from history/ every file of the session <id> (as stored, never the file name), before its new file lands
263
+ # there: a session /resume loaded in place goes on under its id, so its new file replaces the one it was loaded
264
+ # from, under its own name (no collision suffix). Nothing for an empty id.
265
+ drop_history_duplicates() { # <raw id>
266
+ local other
267
+ [ -n "$1" ] || return 0
268
+ for other in "$HISTORY"/*.json; do
269
+ [ -f "$other" ] || continue
270
+ [ "$(session_field "$other" SessionId)" = "$1" ] && rm -f "$other"
271
+ done
272
+ return 0
273
+ }
274
+
275
+ # Moves every session file the runtime saved into history/, under <UTC time>-<session id>.json: the last.json of
276
+ # sessions/ and the archives /resume left beside it (<stamp>-archived.json, moved first so last.json is the newest),
277
+ # unless <side only> is 1, and always those of side/<pid>/ (a start beside an open one, now ended). A session without
278
+ # a message (a start and a quit) is dropped, so it never pushes a real one out of the history; a session that goes
279
+ # on under the id of one already in the history replaces it there (drop_history_duplicates).
280
+ archive_sessions() { # [<side only>]
281
+ local last raw id stamp target n found=() lasts=() folders=()
282
+ [ "${1:-0}" = 1 ] || folders=("$SESSIONS"/*/)
283
+ folders+=("$PROJECT_STATE"/side/*/*/)
284
+ for last in "${folders[@]}"; do
285
+ [ -d "$last" ] || continue
286
+ for last in "$last"*.json; do
287
+ [ -f "$last" ] || continue
288
+ case "$last" in */last.json) lasts+=("$last") ;; *) found+=("$last") ;; esac
289
+ done
290
+ done
291
+ found+=("${lasts[@]}")
292
+ for last in "${found[@]}"; do
293
+ [ -f "$last" ] || continue
294
+ if session_empty "$last"; then rm -f "$last"; continue; fi
295
+ raw="$(session_field "$last" SessionId)"; id="$(printf '%s' "$raw" | tr -c 'A-Za-z0-9._-' '_')"
296
+ drop_history_duplicates "$raw"
297
+ stamp="$(date -u +%Y%m%dT%H%M%SZ)"
298
+ target="$HISTORY/$stamp-${id:-session}.json"; n=2
299
+ while [ -e "$target" ]; do target="$HISTORY/$stamp-${id:-session}-$n.json"; n=$((n + 1)); done
300
+ mv -f "$last" "$target" || die "could not move $last to $HISTORY."
301
+ done
302
+ for last in "$PROJECT_STATE"/side/*/; do [ -d "$last" ] && rm -rf "$last"; done
303
+ return 0
304
+ }
305
+
306
+ # The history, newest first, one file per line.
307
+ history_files() { find "$HISTORY" -maxdepth 1 -type f -name '*.json' 2>/dev/null | LC_ALL=C sort -r; }
308
+
309
+ # Prints the stored folder that covers <folder> (itself or an ancestor); 1 when none does.
310
+ trusted_by() { # <canonical folder>
311
+ local entries path
312
+ entries="$(trust_entries)" || exit 1
313
+ while IFS= read -r path; do
314
+ path="${path%%$'\t'*}"
315
+ [ -n "$path" ] || continue
316
+ if [ "$path" = "$1" ] || [ "$path" = / ]; then printf '%s\n' "$path"; return 0; fi
317
+ case "$1" in "$path"/*) printf '%s\n' "$path"; return 0 ;; esac
318
+ done <<< "$entries"
319
+ return 1
320
+ }
321
+
322
+ trust_add() { # <canonical folder>
323
+ local entries
324
+ case "$1" in *[[:cntrl:]]*) die "a folder whose path holds a control character cannot be trusted: $1" ;; esac
325
+ entries="$(trust_entries)" || exit 1
326
+ { [ -n "$entries" ] && printf '%s\n' "$entries"; printf '%s\t%s\n' "$1" "$(date -u +%Y-%m-%dT%H:%M:%SZ)"; } | write_trust_store
327
+ }
328
+
329
+ # Returns when <folder> may start: trusted, --trust or CI, or trusted now in a terminal; exits otherwise
330
+ # (DECLINED on a decline, 0 for a start, 3 without a terminal). Nothing of the folder is read here.
331
+ DECLINED=0
332
+ ensure_trusted() { # <canonical folder> <1 when --trust was given>
333
+ local answer
334
+ if [ "$2" = 1 ] || { [ "${ORKEON_CODE_TRUST:-}" = 1 ] && [ "${CI:-}" = true ]; }; then return 0; fi
335
+ trusted_by "$1" >/dev/null && return 0
336
+ if [ ! -t 0 ]; then
337
+ echo "orkeon-code: $1 is not a trusted folder, and there is no terminal to ask in. Trust it once with" \
338
+ "'orkeon-code trust add $1', or pass --trust for this run only." >&2
339
+ exit 3
340
+ fi
341
+ {
342
+ echo "orkeon-code: do you trust the files in this folder?"
343
+ echo " $1"
344
+ echo " The agent reads its AGENTS.md, .orkeon/config.json and .orkeon/repo.yaml, may run the commands"
345
+ echo " .orkeon/repo.yaml names, and git runs the folder's hooks through the shell."
346
+ if [ "$1" = / ] || [ "$1" = "$(canonical_dir "$HOME")" ]; then echo " This trusts every folder below it."; fi
347
+ printf '[y] trust [n] quit: '
348
+ } >&2
349
+ read -r answer || answer=""
350
+ case "$(lowered "$answer")" in
351
+ y|yes) trust_add "$1"; echo "orkeon-code: trusted $1 (orkeon-code trust remove $1 takes it back)." >&2 ;;
352
+ *) echo "orkeon-code: not trusted, nothing started." >&2; exit "$DECLINED" ;;
353
+ esac
354
+ }
355
+
356
+ if [ "${1:-}" = trust ]; then
357
+ shift
358
+ case "${1:-}" in
359
+ list)
360
+ entries="$(trust_entries)" || exit 1
361
+ if [ -n "$entries" ]; then
362
+ while IFS=$'\t' read -r path date; do printf '%s (trusted %s)\n' "$path" "$date"; done <<< "$entries"
363
+ fi
364
+ exit 0 ;;
365
+ add)
366
+ entries="$(trust_entries)" || exit 1
367
+ folder="$(canonical_dir "${2:?trust add needs a folder}")" || die "not a folder: $2"
368
+ if by="$(trusted_by "$folder")"; then
369
+ if [ "$by" = "$folder" ]; then echo "orkeon-code: $folder is already trusted."; else echo "orkeon-code: $folder is already trusted, through $by."; fi
370
+ exit 0
371
+ fi
372
+ trust_add "$folder"; echo "orkeon-code: trusted $folder."; exit 0 ;;
373
+ remove)
374
+ target="${2:?trust remove needs a folder}"
375
+ target="$(canonical_dir "$target")" || { target="${2%/}"; [ -n "$target" ] || target=/; }
376
+ entries="$(trust_entries)" || exit 1
377
+ kept="$(printf '%s\n' "$entries" | TARGET="$target" awk -F '\t' '$0 != "" && $1 != ENVIRON["TARGET"]')" || true
378
+ if [ "$kept" = "$entries" ]; then
379
+ if by="$(trusted_by "$target")"; then die "$target is not in the store: it is trusted through $by."; fi
380
+ die "$target is not a trusted folder."
381
+ fi
382
+ printf '%s\n' "$kept" | write_trust_store; echo "orkeon-code: $target is no longer trusted."; exit 0 ;;
383
+ check) # 0: it may start; 3: refused, no terminal; 4: declined in the terminal (scripts/run-crew.sh).
384
+ DECLINED=4
385
+ folder="$(canonical_dir "${2:?trust check needs a folder}")" || die "not a folder: $2"
386
+ ensure_trusted "$folder" "$([ "${3:-}" = --trust ] && echo 1 || echo 0)"; exit 0 ;;
387
+ *) die "usage: orkeon-code trust list | add <path> | remove <path> | check <path> [--trust]" ;;
388
+ esac
389
+ fi
390
+
391
+ # ── Settings: where they are looked for, and setup ─────────────────────────
392
+ # The XDG base the runtime itself uses (an absolute XDG_CONFIG_HOME, else ~/.config).
393
+ case "${XDG_CONFIG_HOME:-}" in /*) CONFIG_BASE="$XDG_CONFIG_HOME" ;; *) CONFIG_BASE="$HOME/.config" ;; esac
394
+ HOME_SETTINGS="$CODE_HOME/appsettings.json"
395
+ LEGACY_SETTINGS="$CONFIG_BASE/orkeon-code/appsettings.json"
396
+ # What `orkeon init` writes and the REPL reads when no --settings is given (RunnerSettings.GetGlobalSettingsPath).
397
+ RUNTIME_SETTINGS="$CONFIG_BASE/Orkeon/appsettings.json"
398
+
399
+ # Asks one line on the terminal; the answer, trimmed, or <default> when it is empty; 1 on end of input.
400
+ ask() { # <question> <default>
401
+ local answer
402
+ printf '%s' "$1" >&2
403
+ read -r answer || return 1
404
+ answer="$(printf '%s' "$answer" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')"
405
+ printf '%s\n' "${answer:-$2}"
406
+ }
407
+
408
+ # Writes $HOME_SETTINGS from one of the examples, with the model and the name of the key's variable asked on
409
+ # the terminal; never the key. 1 when the user stops or keeps the existing file.
410
+ run_setup() {
411
+ local answer example model var default_model default_var tmp
412
+ [ -t 0 ] || die "setup asks its questions in a terminal: run 'orkeon-code setup' in one."
413
+ if [ -f "$HOME_SETTINGS" ]; then
414
+ answer="$(ask "orkeon-code: $HOME_SETTINGS exists. Replace it? [y/N] " n)" || answer=n
415
+ case "$(lowered "$answer")" in y|yes) ;; *) echo "orkeon-code: $HOME_SETTINGS kept." >&2; return 1 ;; esac
416
+ fi
417
+ {
418
+ echo "orkeon-code setup: the settings go to $HOME_SETTINGS. The API key never does: only the name of the"
419
+ echo "environment variable that holds it."
420
+ echo " [1] a hosted, OpenAI-compatible endpoint (config/appsettings.example.json: DeepSeek)"
421
+ echo " [2] Ollama on this machine, no key (config/appsettings.ollama.example.json)"
422
+ } >&2
423
+ while :; do
424
+ answer="$(ask "Provider [1]: " 1)" || return 1
425
+ case "$answer" in
426
+ 1) example="$ROOT/config/appsettings.example.json"; break ;;
427
+ 2) example="$ROOT/config/appsettings.ollama.example.json"; break ;;
428
+ *) echo "orkeon-code: answer 1 or 2." >&2 ;;
429
+ esac
430
+ done
431
+ [ -f "$example" ] || die "$example not found."
432
+ default_model="$(sed -n 's/^[[:space:]]*"Model"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' "$example" | head -n 1)"
433
+ default_var="$(sed -n 's/.*"ApiKeyEnvVar"[[:space:]]*:[[:space:]]*"\([A-Za-z_][A-Za-z0-9_]*\)".*/\1/p' "$example" | head -n 1)"
434
+ while :; do
435
+ model="$(ask "Model [$default_model]: " "$default_model")" || return 1
436
+ # Never said back, and refused when it looks like a key: a key pasted at the wrong question.
437
+ if [[ "$model" =~ ^[A-Za-z0-9._:/@+-]+$ ]] && ! [[ "$model" =~ ^sk- ]] && ! [[ "$model" =~ ^[A-Za-z0-9_-]{32,}$ ]]; then break; fi
438
+ echo "orkeon-code: that is not a model id (letters, digits and . _ : / @ + -, not a key)." >&2
439
+ done
440
+ var=""
441
+ if [ -n "$default_var" ]; then
442
+ while :; do
443
+ var="$(ask "Name of the environment variable that holds the API key [$default_var]: " "$default_var")" || return 1
444
+ if [[ "$var" =~ ^[A-Z_][A-Z0-9_]{0,63}$ ]]; then break; fi
445
+ # Never said back: what was typed may be the key itself.
446
+ echo "orkeon-code: that is not a variable name (capitals, digits and _). Type the name of the variable, never the key." >&2
447
+ done
448
+ fi
449
+ [ -d "$CODE_HOME" ] || { mkdir -p "$CODE_HOME" && chmod 700 "$CODE_HOME"; } || die "could not create $CODE_HOME."
450
+ tmp="$HOME_SETTINGS.tmp.$$"
451
+ (
452
+ umask 077
453
+ MODEL="$model" KEY_VAR="$var" awk '
454
+ !c && /^[ \t]*"_comment"[ \t]*:/ {
455
+ print " \"_comment\": \"Written by orkeon-code setup. The API key never goes in this file: ApiKeyEnvVar names the environment variable that holds it, and ORKEON_Llm__ApiKey, when set, wins over everything. Every key is explained in docs/configuration.md.\","
456
+ c = 1; next
457
+ }
458
+ !m && /^[ \t]*"Model"[ \t]*:/ { sub(/"Model"[ \t]*:[ \t]*"[^"]*"/, "\"Model\": \"" ENVIRON["MODEL"] "\""); m = 1 }
459
+ !k && ENVIRON["KEY_VAR"] != "" && /^[ \t]*"ApiKeyEnvVar"[ \t]*:/ {
460
+ sub(/"ApiKeyEnvVar"[ \t]*:[ \t]*"[^"]*"/, "\"ApiKeyEnvVar\": \"" ENVIRON["KEY_VAR"] "\""); k = 1
461
+ }
462
+ { print }
463
+ ' "$example" > "$tmp"
464
+ ) && chmod 600 "$tmp" && mv -f "$tmp" "$HOME_SETTINGS" || { rm -f "$tmp"; die "could not write $HOME_SETTINGS."; }
465
+ echo "orkeon-code: wrote $HOME_SETTINGS (model $model, mode 600)." >&2
466
+ if [ -n "$var" ]; then echo "orkeon-code: export the key before you start: export $var=<your key>" >&2; fi
467
+ return 0
468
+ }
469
+
470
+ if [ "${1:-}" = setup ]; then
471
+ run_setup || exit 1
472
+ exit 0
473
+ fi
474
+
475
+ MODE="${ORKEON_CODE_MODE:-}"
476
+ WORKSPACE=""
477
+ SETTINGS="${ORKEON_CODE_SETTINGS:-}"
478
+ DEMO=0
479
+ VERSION_ONLY=0
480
+ TRUST_FLAG=0
481
+ CONTINUE=0
482
+ RESUME=0
483
+ RESUME_CHOICE=""
484
+ EXTRA=()
485
+ while [ $# -gt 0 ]; do
486
+ case "$1" in
487
+ --mode) MODE="${2:?--mode needs docker|tool|source}"; shift 2 ;;
488
+ --mode=*) MODE="${1#*=}"; shift ;;
489
+ --workspace) WORKSPACE="${2:?--workspace needs a directory}"; shift 2 ;;
490
+ --workspace=*) WORKSPACE="${1#*=}"; shift ;;
491
+ --settings) SETTINGS="${2:?--settings needs a file}"; shift 2 ;;
492
+ --settings=*) SETTINGS="${1#*=}"; shift ;;
493
+ --demo) DEMO=1; shift ;;
494
+ --version) VERSION_ONLY=1; shift ;;
495
+ --trust) TRUST_FLAG=1; shift ;;
496
+ -c|--continue) CONTINUE=1; shift ;;
497
+ --resume=*) RESUME=1; RESUME_CHOICE="${1#*=}"; shift ;;
498
+ --resume)
499
+ RESUME=1; shift
500
+ case "${1:-}" in ''|-*) ;; *) RESUME_CHOICE="$1"; shift ;; esac ;;
501
+ -h|--help) awk 'NR > 1 && /^set -euo pipefail/ { exit } NR > 1' "$SELF" | sed 's/^# \{0,1\}//'; exit 0 ;;
502
+ --) shift; EXTRA=("$@"); break ;;
503
+ *) EXTRA+=("$1"); shift ;;
504
+ esac
505
+ done
506
+
507
+ if [ "$CONTINUE" = 1 ] && [ "$RESUME" = 1 ]; then die "--continue and --resume do not go together: one brings the last session back, the other the one you pick."; fi
508
+
509
+ if [ "$VERSION_ONLY" = 1 ]; then
510
+ echo "orkeon-code $CODE_VERSION ($KIND: $ROOT)"
511
+ echo "Orkeon $ORKEON_PIN expected, found: $(runtime_found "$MODE")"
512
+ exit 0
513
+ fi
514
+
515
+ # ── Project ────────────────────────────────────────────────────────────────
516
+ if [ "$DEMO" = 1 ]; then
517
+ if [ "$KIND" = clone ]; then WORKSPACE="$ROOT/fixtures/sample-repo"; else WORKSPACE="$CODE_HOME/demo/sample-repo"; fi
518
+ bash "$ROOT/scripts/setup-fixture.sh" "$WORKSPACE" >/dev/null
519
+ fi
520
+ # Physical: the folder checked, stored and mounted is the same one, whatever link led to it.
521
+ WORKSPACE="$(canonical_dir "${WORKSPACE:-$PWD}")" || die "workspace not found: ${WORKSPACE:-$PWD}"
522
+ # Before anything of the project is read or mounted; the demo fixture is the launcher's own.
523
+ if [ "$DEMO" = 0 ]; then ensure_trusted "$WORKSPACE" "$TRUST_FLAG"; fi
524
+
525
+ # ── Settings ───────────────────────────────────────────────────────────────
526
+ # RUNTIME_OWN=1: Orkeon's own file, which the REPL reads itself when no --settings is passed.
527
+ RUNTIME_OWN=0
528
+ if [ -z "$SETTINGS" ]; then
529
+ cands=("$HOME_SETTINGS" "$LEGACY_SETTINGS")
530
+ if [ "$KIND" = clone ]; then cands=("$ROOT/config/appsettings.local.json" "${cands[@]}"); fi
531
+ for cand in "${cands[@]}"; do
532
+ if [ -f "$cand" ]; then SETTINGS="$cand"; break; fi
533
+ done
534
+ if [ -z "$SETTINGS" ] && [ -f "$RUNTIME_SETTINGS" ]; then SETTINGS="$RUNTIME_SETTINGS"; RUNTIME_OWN=1; fi
535
+ if [ "$SETTINGS" = "$LEGACY_SETTINGS" ]; then
536
+ warn "$LEGACY_SETTINGS is the old place of the settings: move it to $HOME_SETTINGS."
537
+ fi
538
+ if [ -z "$SETTINGS" ] && [ -t 0 ]; then
539
+ echo "orkeon-code: no settings file yet." >&2
540
+ if run_setup; then SETTINGS="$HOME_SETTINGS"; fi
541
+ fi
542
+ fi
543
+ if [ -z "$SETTINGS" ] || [ ! -f "$SETTINGS" ]; then
544
+ if [ "$KIND" = clone ]; then
545
+ die "no settings file. Run 'orkeon-code setup' in a terminal, copy config/appsettings.example.json to config/appsettings.local.json, or pass --settings FILE."
546
+ fi
547
+ die "no settings file. Run 'orkeon-code setup' in a terminal, copy $ROOT/config/appsettings.example.json to $HOME_SETTINGS, or pass --settings FILE."
548
+ fi
549
+ SETTINGS="$(cd "$(dirname "$SETTINGS")" && pwd)/$(basename "$SETTINGS")"
550
+
551
+ # The host falls back to a keyless default provider when the Llm section is
552
+ # missing, and every /assistant turn then returns nothing: say so up front.
553
+ KEY_VAR="$(sed -n 's/.*"ApiKeyEnvVar"[[:space:]]*:[[:space:]]*"\([A-Za-z_][A-Za-z0-9_]*\)".*/\1/p' "$SETTINGS" | head -n 1)"
554
+ if ! grep -q '"Llm"' "$SETTINGS"; then
555
+ warn "$SETTINGS has no \"Llm\" section: the host would use a keyless default and every turn would be empty."
556
+ elif [ -z "${ORKEON_Llm__ApiKey:-}" ] && [ -n "$KEY_VAR" ] && [ -z "${!KEY_VAR:-}" ]; then
557
+ warn "no API key: export $KEY_VAR (named by Llm:ApiKeyEnvVar) or ORKEON_Llm__ApiKey."
558
+ fi
559
+ if [ -n "${ORKEON_CODE_MODEL:-}" ]; then export ORKEON_Llm__Model="$ORKEON_CODE_MODEL"; fi
560
+
561
+ # The host's permission gate checks every tool call against the session's rules
562
+ # (/permissions), then the mode, and asks you when the mode says so; off, no call
563
+ # is checked. On by default here; a settings file can still turn it off.
564
+ export Orkeon__Security__PermissionGate__Enabled="${Orkeon__Security__PermissionGate__Enabled:-true}"
565
+
566
+ # ── Mode ───────────────────────────────────────────────────────────────────
567
+ if [ -z "$MODE" ]; then
568
+ if [ -n "${ORKEON_REPL:-}" ] || command -v orkeon-repl >/dev/null 2>&1; then MODE=tool
569
+ elif command -v docker >/dev/null 2>&1; then MODE=docker
570
+ elif [ -n "${ORKEON_SOURCE:-}" ]; then MODE=source
571
+ else die "no Orkeon runtime found: install Docker, or the orkeon-repl tool, or set ORKEON_SOURCE (see docs/getting-started.md)."
572
+ fi
573
+ fi
574
+
575
+ # ── State and sessions ─────────────────────────────────────────────────────
576
+ # An install keeps one folder per project, ~/.orkeon-code/projects/<slug>; a clone's .crew-output and an
577
+ # ORKEON_CODE_STATE may serve several projects, so their per-project part goes under projects/<slug>.
578
+ # Either way PROJECT_STATE lies inside STATE, which docker mode mounts on /work/state.
579
+ SLUG="$(project_slug "$WORKSPACE")"
580
+ if [ -n "${ORKEON_CODE_STATE:-}" ]; then STATE="$ORKEON_CODE_STATE"; PROJECT_REL="projects/$SLUG"
581
+ elif [ "$KIND" = clone ]; then STATE="$ROOT/.crew-output"; PROJECT_REL="projects/$SLUG"
582
+ else STATE="$CODE_HOME/projects/$SLUG"; PROJECT_REL=""; fi
583
+ mkdir -p "$STATE/output" "$STATE/tmp"
584
+ STATE="$(cd "$STATE" && pwd)"
585
+ PROJECT_STATE="$STATE${PROJECT_REL:+/$PROJECT_REL}"
586
+ # The runtime saves the session in <SessionsDirectory>/<its slug of the project>/last.json when the REPL ends
587
+ # (FileSessionBufferService), and reloads it at the next start. The folder is the launcher's /sessions mount,
588
+ # outside the project; the launcher keeps its own files beside it: history/ and project.json.
589
+ SESSIONS="$PROJECT_STATE/sessions"
590
+ HISTORY="$PROJECT_STATE/history"
591
+ # The typed memory (Orkeon #286) is kept beside them, in <RootDirectory>/<its slug>/memory.json on the /memory mount,
592
+ # outside the project so that neither git nor the file tools see it.
593
+ MEMORY="$PROJECT_STATE/memory"
594
+ mkdir -p "$SESSIONS" "$HISTORY" "$MEMORY"
595
+ if [ "$MODE" = docker ]; then RUNTIME_SLUG="$(project_slug /work/project)"; else RUNTIME_SLUG="$SLUG"; fi
596
+ now="$(date -u +%Y-%m-%dT%H:%M:%SZ)"
597
+ first_seen="$(session_field "$PROJECT_STATE/project.json" firstSeen)"
598
+ {
599
+ printf '{\n "path": %s,\n "slug": "%s",\n "runtimeSlug": "%s",\n' "$(json_string "$WORKSPACE")" "$SLUG" "$RUNTIME_SLUG"
600
+ printf ' "firstSeen": "%s",\n "lastSeen": "%s"\n}\n' "${first_seen:-$now}" "$now"
601
+ } > "$PROJECT_STATE/project.json.tmp.$$" && mv -f "$PROJECT_STATE/project.json.tmp.$$" "$PROJECT_STATE/project.json" \
602
+ || die "could not write $PROJECT_STATE/project.json."
603
+
604
+ # The starts open on this project, one pid a line (a launcher's pid is its REPL's: it execs it). Another one still
605
+ # running: its session is left where it is, and this one is kept beside it, in side/<pid>/, archived at the next
606
+ # start that finds none open.
607
+ LOCK="$PROJECT_STATE/.lock"
608
+ OTHER=""
609
+ live_pids=""
610
+ if [ -f "$LOCK" ]; then
611
+ while IFS= read -r other_pid; do
612
+ case "$other_pid" in ''|*[!0-9]*) continue ;; esac
613
+ if [ "$other_pid" != $$ ] && kill -0 "$other_pid" 2>/dev/null; then
614
+ OTHER="${OTHER:-$other_pid}"; live_pids="$live_pids$other_pid"$'\n'
615
+ fi
616
+ done < "$LOCK"
617
+ fi
618
+ printf '%s%s\n' "$live_pids" $$ > "$LOCK"
619
+ SESSIONS_SUB=sessions
620
+
621
+ # Fresh unless --continue (the runtime reloads the last itself) or --resume (the chosen one is put back).
622
+ SESSION_NOTE="fresh; --continue brings the last back, --resume lists them"
623
+ if [ -n "$OTHER" ]; then
624
+ warn "another session is open on this project (pid $OTHER): it is left as it is, and this one is kept beside it, in the history from the next start."
625
+ SESSION_NOTE="beside the open one"
626
+ SESSIONS_SUB="side/$$"
627
+ mkdir -p "$PROJECT_STATE/$SESSIONS_SUB"
628
+ elif [ "$CONTINUE" = 1 ]; then SESSION_NOTE="continued"; archive_sessions 1
629
+ else archive_sessions
630
+ fi
631
+ keep="${ORKEON_CODE_SESSIONS_KEEP:-50}"
632
+ case "$keep" in
633
+ [0-9]|[0-9][0-9]|[0-9][0-9][0-9]|[0-9][0-9][0-9][0-9]) keep=$((10#$keep)) ;;
634
+ *) keep=0 ;;
635
+ esac
636
+ if [ "$keep" -lt 1 ]; then
637
+ warn "ORKEON_CODE_SESSIONS_KEEP=${ORKEON_CODE_SESSIONS_KEEP-} is not a whole number from 1 to 9999: 50 sessions are kept."; keep=50
638
+ fi
639
+ history_files | tail -n "+$((keep + 1))" | while IFS= read -r old; do rm -f "$old"; done
640
+
641
+ if [ "$RESUME" = 1 ]; then
642
+ files=()
643
+ while IFS= read -r f; do files+=("$f"); done < <(history_files)
644
+ [ ${#files[@]} -gt 0 ] || die "no saved session for $WORKSPACE yet (they are kept in $HISTORY)."
645
+ list_sessions() {
646
+ local i=1 f title
647
+ for f in "${files[@]}"; do
648
+ title="$(session_field "$f" Title)"
649
+ printf '%3d %s %s (%s messages) %s\n' "$i" "$(basename "$f" .json | cut -c1-16)" "${title:-(untitled)}" \
650
+ "$(session_messages "$f")" "$(basename "$f" .json | cut -c18-)" >&2
651
+ i=$((i + 1))
652
+ done
653
+ }
654
+ if [ -z "$RESUME_CHOICE" ]; then
655
+ echo "orkeon-code: sessions of $WORKSPACE, newest first:" >&2
656
+ list_sessions
657
+ if [ ! -t 0 ]; then echo "orkeon-code: no terminal to choose in: pass --resume <number or id>." >&2; exit 2; fi
658
+ printf 'Session to resume [1]: ' >&2
659
+ read -r RESUME_CHOICE || RESUME_CHOICE=""
660
+ RESUME_CHOICE="${RESUME_CHOICE:-1}"
661
+ fi
662
+ chosen=""
663
+ # A number of the list first (up to four digits), else the start of an id.
664
+ case "$RESUME_CHOICE" in
665
+ [1-9]|[1-9][0-9]|[1-9][0-9][0-9]|[1-9][0-9][0-9][0-9])
666
+ if [ "$RESUME_CHOICE" -le ${#files[@]} ]; then chosen="${files[$((RESUME_CHOICE - 1))]}"; fi ;;
667
+ esac
668
+ if [ -z "$chosen" ] && [ -n "$RESUME_CHOICE" ]; then
669
+ for f in "${files[@]}"; do
670
+ case "$(basename "$f" .json | cut -c18-)" in "$RESUME_CHOICE"*) [ -z "$chosen" ] || die "more than one session starts with '$RESUME_CHOICE': give more of its id."; chosen="$f" ;; esac
671
+ done
672
+ fi
673
+ [ -n "$chosen" ] || die "no saved session '$RESUME_CHOICE' (orkeon-code --resume lists them)."
674
+ [ -z "$OTHER" ] || die "another session is open on this project (pid $OTHER): end it before you resume one."
675
+ # The chosen session lives in one place: back in sessions/, the history losing it (the last one was archived above).
676
+ mkdir -p "$SESSIONS/$RUNTIME_SLUG"
677
+ mv -f "$chosen" "$SESSIONS/$RUNTIME_SLUG/last.json" || die "could not put $chosen back."
678
+ SESSION_NOTE="resumed $(basename "$chosen" .json | cut -c18-)"
679
+ fi
680
+
681
+ # The session's start script (scripts/session-start.ork.ts): what a new session should know of
682
+ # the dev work left in progress. Copied fresh at every start into the agent's /tmp; the runtime
683
+ # runs it once, at the REPL's start, through its script host (a `.ork.ts`, never `sh`), and puts
684
+ # what it prints before the first free-text turn.
685
+ mkdir -p "$STATE/tmp/orkeon-code"
686
+ cp "$ROOT/scripts/session-start.ork.ts" "$STATE/tmp/orkeon-code/session-start.ork.ts"
687
+ export Orkeon__Cli__Session__StartScript="${Orkeon__Cli__Session__StartScript-/tmp/orkeon-code/session-start.ork.ts}"
688
+ # The session persists (the runtime's default in the REPL, said here; a settings file may still turn it off), in
689
+ # /sessions: the prefixed form wins over the settings file, since the archiving above reads that folder alone.
690
+ export Orkeon__Cli__Session__Persist="${Orkeon__Cli__Session__Persist:-true}"
691
+ export ORKEON_Orkeon__Cli__Session__SessionsDirectory=/sessions
692
+ export ORKEON_Orkeon__Cli__Memory__RootDirectory=/memory
693
+
694
+ # ── Speech-to-text (optional) ──────────────────────────────────────────────
695
+ # ORKEON_CODE_AUDIO_DIR names the folder of audio and video files. The Whisper
696
+ # MCP server (mcp/whisper) runs on the host, started by the REPL over stdio: it
697
+ # reads that folder and writes into <state>/transcripts, which the agents see as
698
+ # /audio and /transcripts. The server is declared through the environment
699
+ # (ORKEON_MCP__Servers__whisper__*), so no settings file names this checkout; a
700
+ # settings file, or the environment, that declares a "whisper" server keeps it, and
701
+ # this block stays out.
702
+ AUDIO=""
703
+ AUDIO_ARGS=()
704
+ if [ -n "${ORKEON_CODE_AUDIO_DIR:-}" ]; then
705
+ if [ "$MODE" = docker ]; then
706
+ warn "ORKEON_CODE_AUDIO_DIR is ignored in docker mode: the Whisper server runs in tool and source modes (mcp/whisper/README.md)."
707
+ elif grep -qi '"whisper"[[:space:]]*:' "$SETTINGS"; then
708
+ warn "$SETTINGS declares a \"whisper\" MCP server: ORKEON_CODE_AUDIO_DIR is ignored, the settings file wins."
709
+ elif env | grep -q '^ORKEON_MCP__Servers__whisper__'; then
710
+ warn "the environment declares a \"whisper\" MCP server (ORKEON_MCP__Servers__whisper__*): ORKEON_CODE_AUDIO_DIR is ignored, that declaration wins."
711
+ elif ! UV="$(command -v uv)"; then
712
+ warn "ORKEON_CODE_AUDIO_DIR is set but uv is not on PATH: speech-to-text is off (mcp/whisper/README.md, 'Install')."
713
+ else
714
+ [ -d "$ORKEON_CODE_AUDIO_DIR" ] || die "ORKEON_CODE_AUDIO_DIR=$ORKEON_CODE_AUDIO_DIR is not a folder."
715
+ AUDIO="$(cd "$ORKEON_CODE_AUDIO_DIR" && pwd)"
716
+ mkdir -p "$STATE/transcripts"
717
+ export WHISPER_AUDIO_DIR="$AUDIO" WHISPER_OUT_DIR="$STATE/transcripts"
718
+ export ORKEON_MCP__Servers__whisper__Transport=Stdio
719
+ export ORKEON_MCP__Servers__whisper__Command="$UV"
720
+ # The gate reads a server's annotations (readOnlyHint on transcribe_audio, list_audio and whisper_info)
721
+ # only when the server is trusted (Orkeon #223); untrusted, every tool of the server would be a write.
722
+ export ORKEON_MCP__Servers__whisper__TrustAnnotations=true
723
+ # ORKEON_CODE_VOICE=1 opens the microphone to /voice and the speaker to /say and
724
+ # voice.reply: the server gets the `voice` extra (sounddevice), WHISPER_MICROPHONE=1
725
+ # and WHISPER_SPEAKER=1; without it, it refuses to record and only writes what it
726
+ # would say. Piper, the default speech backend, is the `tts` extra; a backend on
727
+ # OpenAI's API or a compatible server (WHISPER_BACKEND or TTS_BACKEND openai or http)
728
+ # needs the `openai` extra; WHISPER_DEVICE=cuda the cuBLAS and cuDNN wheels of the
729
+ # `cuda` extra, which the server preloads. The values are read as the server reads
730
+ # them: case and spaces aside.
731
+ tts_backend="$(lowered "${TTS_BACKEND:-}")"
732
+ stt_backend="$(lowered "${WHISPER_BACKEND:-}")"
733
+ whisper_args=(run --project "$ROOT/mcp/whisper")
734
+ # An install's folder is never written to: uv keeps the server's environment under ORKEON_CODE_HOME and
735
+ # reads uv.lock as shipped.
736
+ if [ "$KIND" = install ]; then
737
+ whisper_args+=(--frozen)
738
+ export UV_PROJECT_ENVIRONMENT="${UV_PROJECT_ENVIRONMENT:-$CODE_HOME/whisper/.venv}"
739
+ fi
740
+ if [ "${ORKEON_CODE_VOICE:-}" = 1 ]; then
741
+ whisper_args+=(--extra voice)
742
+ export WHISPER_MICROPHONE=1 WHISPER_SPEAKER=1
743
+ fi
744
+ if [ "${tts_backend:-piper}" = piper ]; then whisper_args+=(--extra tts); fi
745
+ case " $tts_backend $stt_backend " in *" openai "*|*" http "*) whisper_args+=(--extra openai) ;; esac
746
+ if [ "$(lowered "${WHISPER_DEVICE:-}")" = cuda ]; then whisper_args+=(--extra cuda); fi
747
+ whisper_args+=("$ROOT/mcp/whisper/whisper_mcp.py")
748
+ for i in "${!whisper_args[@]}"; do export "ORKEON_MCP__Servers__whisper__Args__$i=${whisper_args[$i]}"; done
749
+ AUDIO_ARGS=(--mount "$AUDIO:/audio:ro" --mount "$STATE/transcripts:/transcripts:ro")
750
+ fi
751
+ fi
752
+
753
+ if [ "$MODE" = source ]; then echo "orkeon-code $CODE_VERSION on Orkeon $ORKEON_PIN (source)"; else echo "orkeon-code $CODE_VERSION on Orkeon ($MODE: a release, the pin is $ORKEON_PIN)"; fi
754
+ echo " project : $WORKSPACE -> /workspace"
755
+ if [ "$RUNTIME_OWN" = 1 ]; then echo " settings : $SETTINGS (Orkeon's own, read by the REPL)"; else echo " settings : $SETTINGS"; fi
756
+ echo " sessions : $PROJECT_STATE/$SESSIONS_SUB ($SESSION_NOTE)"
757
+ if [ -n "$AUDIO" ]; then
758
+ model="${WHISPER_MODEL:-$([ "$stt_backend" = openai ] && echo whisper-1 || echo small)}"
759
+ echo " audio : $AUDIO -> /audio (transcripts: $STATE/transcripts, model $model$([ "$(lowered "${WHISPER_DEVICE:-}")" = cuda ] && echo ' on the GPU')$([ "${ORKEON_CODE_VOICE:-}" = 1 ] && echo ', microphone and speaker on'))"
760
+ fi
761
+
762
+ # The user's orkeonCode block, handed to the scripts as a read-only file: they read no settings and no
763
+ # environment (V-04). Written afresh at each start; no block, or no node to read it (said), leaves the
764
+ # folder empty, and no user setting applies. The key is matched case for case, as in the .ps1.
765
+ USER_CONFIG_DIR="$STATE/user-config"
766
+ mkdir -p "$USER_CONFIG_DIR"
767
+ rm -f "$USER_CONFIG_DIR/config.json"
768
+ if command -v node >/dev/null 2>&1; then
769
+ node -e 'const fs=require("fs");const s=JSON.parse(fs.readFileSync(process.argv[1],"utf8"));if(s&&typeof s.orkeonCode==="object"&&s.orkeonCode&&!Array.isArray(s.orkeonCode))fs.writeFileSync(process.argv[2],JSON.stringify(s.orkeonCode,null,2)+"\n")' "$SETTINGS" "$USER_CONFIG_DIR/config.json" \
770
+ || warn "the orkeonCode block of $SETTINGS could not be read: no user setting applies."
771
+ elif grep -q '"orkeonCode"' "$SETTINGS"; then
772
+ warn "node is not on PATH: the orkeonCode block of $SETTINGS is not read, no user setting applies."
773
+ fi
774
+
775
+ # Sets ARGS. A function filling an array rather than mapfile: macOS ships bash 3.2.
776
+ # An empty <settings> passes none: the REPL then reads Orkeon's own file.
777
+ # <project state> is PROJECT_STATE as the REPL sees it: its sessions/ (side/<pid>/ beside an open start) on /sessions, its history/ on /session-history
778
+ # (read-only, for /resume), its memory/ on /memory.
779
+ repl_args() { # <commands> <crews> <settings> <workspace> <output> <tmp> <user-config> <project state>
780
+ ARGS=(--runner=scripted-commands --commands-dir "$1" --crews-dir "$2")
781
+ if [ -n "$3" ]; then ARGS+=(--settings "$3"); fi
782
+ ARGS+=(--mount "$4:/workspace:rw" --mount "$5:/output:rw" --mount "$6:/tmp:rw" --mount "$7:/user-config:ro"
783
+ --mount "$8/$SESSIONS_SUB:/sessions:rw" --mount "$8/history:/session-history:ro" --mount "$8/memory:/memory:rw")
784
+ }
785
+
786
+ # The exchange log (--llm-log) would land in ./llm-logs, the project: a bare --llm-log among the REPL's
787
+ # arguments is sent to <project state>/llm-logs instead; an explicit --llm-log-path is passed as given (a bare
788
+ # --llm-log beside it is then dropped, since the runtime keeps the last path it reads).
789
+ llm_log_args() { # <project state as the REPL sees it>
790
+ local a out=() named=0
791
+ for a in ${EXTRA[@]+"${EXTRA[@]}"}; do case "$a" in --llm-log-path|--llm-log-path=*) named=1 ;; esac; done
792
+ for a in ${EXTRA[@]+"${EXTRA[@]}"}; do
793
+ if [ "$a" = --llm-log ]; then
794
+ [ "$named" = 1 ] || out+=(--llm-log-path "$1/llm-logs")
795
+ else out+=("$a"); fi
796
+ done
797
+ EXTRA=(${out[@]+"${out[@]}"})
798
+ }
799
+
800
+ case "$MODE" in
801
+ docker)
802
+ command -v docker >/dev/null 2>&1 || die "docker not found."
803
+ # Everything the REPL reads sits under its working directory /work. The
804
+ # image is multi-arch (linux/amd64, linux/arm64): docker pulls the host's.
805
+ env_args=(-e ORKEON_RUNNER=repl -e HOME=/tmp -e DOTNET_CLI_HOME=/tmp
806
+ -e "Orkeon__Security__PermissionGate__Enabled=$Orkeon__Security__PermissionGate__Enabled"
807
+ -e "Orkeon__Cli__Session__StartScript=$Orkeon__Cli__Session__StartScript"
808
+ -e "Orkeon__Cli__Session__Persist=$Orkeon__Cli__Session__Persist"
809
+ -e ORKEON_Orkeon__Cli__Session__SessionsDirectory=/sessions
810
+ -e ORKEON_Orkeon__Cli__Memory__RootDirectory=/memory)
811
+ # The github tool's token (Orkeon #222): GITHUB_TOKEN, GH_TOKEN, or the variable Orkeon__Tools__GitHub__TokenVariable
812
+ # names, each handed over only when set, with the setting itself so the runtime reads the same variable.
813
+ for v in ORKEON_Llm__ApiKey ORKEON_Llm__Model "$KEY_VAR" GITHUB_TOKEN GH_TOKEN \
814
+ Orkeon__Tools__GitHub__TokenVariable "${Orkeon__Tools__GitHub__TokenVariable:-}"; do
815
+ if [ -n "$v" ] && [ -n "${!v:-}" ]; then env_args+=(-e "$v"); fi
816
+ done
817
+ # Files the agent writes in the project belong to the caller, not to root.
818
+ user_args=()
819
+ if [ "$(uname -s)" = Linux ]; then user_args=(--user "$(id -u):$(id -g)"); fi
820
+ tty_args=(-i)
821
+ if [ -t 0 ] && [ -t 1 ]; then tty_args=(-it); fi
822
+ in_state="/work/state${PROJECT_REL:+/$PROJECT_REL}"
823
+ repl_args /work/agent/commands /work/agent/crews /work/settings.json \
824
+ /work/project /work/state/output /work/state/tmp /work/state/user-config "$in_state"
825
+ llm_log_args "$in_state"
826
+ exec docker run --rm "${tty_args[@]}" ${user_args[@]+"${user_args[@]}"} "${env_args[@]}" -w /work \
827
+ -v "$WORKSPACE:/work/project" \
828
+ -v "$ROOT/src:/work/agent:ro" \
829
+ -v "$SETTINGS:/work/settings.json:ro" \
830
+ -v "$STATE:/work/state" \
831
+ "$IMAGE" "${ARGS[@]}" ${EXTRA[@]+"${EXTRA[@]}"}
832
+ ;;
833
+ tool|source)
834
+ if [ "$MODE" = tool ]; then
835
+ if [ -n "${ORKEON_REPL:-}" ]; then read -r -a REPL <<< "$ORKEON_REPL"; else REPL=(orkeon-repl); fi
836
+ command -v "${REPL[0]}" >/dev/null 2>&1 || die "${REPL[0]} not found (see docs/getting-started.md, 'Tool mode')."
837
+ else
838
+ [ -n "${ORKEON_SOURCE:-}" ] && [ -d "$ORKEON_SOURCE/src/apps/Orkeon.ConsoleApp" ] \
839
+ || die "ORKEON_SOURCE must name an Orkeon checkout (src/apps/Orkeon.ConsoleApp not found)."
840
+ ORKEON_SOURCE="$(cd "$ORKEON_SOURCE" && pwd)"
841
+ # The checkout should sit at the pin: one behind it is moved there (follow_pin), another commit runs, with a
842
+ # warning, never a refusal.
843
+ restore=--no-restore
844
+ if head="$(git -C "$ORKEON_SOURCE" rev-parse HEAD 2>/dev/null)" && [ "$head" != "$ORKEON_REF" ]; then
845
+ if follow_pin "$head"; then restore=""
846
+ else warn "ORKEON_SOURCE is at ${head:0:7}, the pin of .orkeon-version is $ORKEON_PIN: the commands were checked against the pin."
847
+ fi
848
+ fi
849
+ # The SDK runs in the Orkeon checkout, under its global.json, never in the
850
+ # project, whose global.json may pin another SDK ("A compatible .NET SDK was
851
+ # not found"). The REPL then starts as the built assembly: `dotnet <dll>`
852
+ # is a host command, which reads no global.json. A checkout just moved is restored by its build.
853
+ # The build is quiet and takes a minute after a move: say it runs, or it looks stuck.
854
+ echo "orkeon-code: building Orkeon in $ORKEON_SOURCE..." >&2
855
+ (cd "$ORKEON_SOURCE" && dotnet build src/apps/Orkeon.ConsoleApp -c Debug ${restore:+"$restore"} -nologo -v quiet) >&2 \
856
+ || die "building src/apps/Orkeon.ConsoleApp in $ORKEON_SOURCE failed (restore it once: dotnet restore Orkeon.sln)."
857
+ dll="$ORKEON_SOURCE/src/apps/Orkeon.ConsoleApp/bin/Debug/net10.0/Orkeon.ConsoleApp.dll"
858
+ [ -f "$dll" ] || die "$dll not found after the build."
859
+ REPL=(dotnet "$dll")
860
+ fi
861
+ # The runtime bundles each command and crew with esbuild. The Debian package
862
+ # and a release-archive install carry their own: /usr/bin/orkeon-repl is a
863
+ # script that points the runtime at /usr/lib/orkeon/esbuild-bin, and an
864
+ # archive's wrapper at its libexec/esbuild-bin. The dotnet tool and a source
865
+ # build carry none: for them, use the one npm installed for this checkout (or, for
866
+ # the npm package, beside it).
867
+ bundled=""
868
+ if [ "$MODE" = tool ] && repl_bin="$(command -v "${REPL[0]}")"; then
869
+ repl_bin="$(resolve_links "$repl_bin")"
870
+ if [ -x "$(dirname "$(dirname "$repl_bin")")/libexec/esbuild-bin/esbuild" ]; then bundled=1
871
+ # A launcher script that sets ORKEON_ESBUILD_PATH itself (the Debian package's).
872
+ elif [ "$(head -c 2 "$repl_bin" 2>/dev/null)" = '#!' ] && grep -q ORKEON_ESBUILD_PATH "$repl_bin" 2>/dev/null; then
873
+ bundled=1
874
+ fi
875
+ fi
876
+ if [ -z "$bundled" ] && [ -z "${ORKEON_ESBUILD_PATH:-}" ] && ! command -v esbuild >/dev/null 2>&1; then
877
+ found="$(find "$ROOT/node_modules/@esbuild" -path '*/bin/esbuild' -type f 2>/dev/null | head -n 1 || true)"
878
+ if [ -z "$found" ] && [ "$KIND" = clone ]; then die "esbuild not found: run 'npm ci' in $ROOT, or set ORKEON_ESBUILD_PATH."; fi
879
+ [ -n "$found" ] \
880
+ || die "esbuild not found: install Orkeon from its package or archive (it carries esbuild), or set ORKEON_ESBUILD_PATH."
881
+ export ORKEON_ESBUILD_PATH="$found"
882
+ fi
883
+ # The REPL runs from the project. It reads its --commands-dir and --crews-dir
884
+ # wherever they are, and a --mount outside its working directory (here /output,
885
+ # /tmp, /user-config, /sessions, /session-history and /memory, in the state directory,
886
+ # and an --llm-log-path there) under ORKEON_ALLOW_EXTERNAL_MOUNTS (Orkeon GAP-49). The
887
+ # seven roots are distinct, and none is one the REPL keeps (/sandbox, /credentials,
888
+ # /llm-logs, /cli-commands..., /crews...).
889
+ export ORKEON_ALLOW_EXTERNAL_MOUNTS=1
890
+ repl_args "$ROOT/src/commands" "$ROOT/src/crews" "$([ "$RUNTIME_OWN" = 1 ] || echo "$SETTINGS")" \
891
+ "$WORKSPACE" "$STATE/output" "$STATE/tmp" "$USER_CONFIG_DIR" "$PROJECT_STATE"
892
+ llm_log_args "$PROJECT_STATE"
893
+ cd "$WORKSPACE"
894
+ exec "${REPL[@]}" "${ARGS[@]}" ${AUDIO_ARGS[@]+"${AUDIO_ARGS[@]}"} ${EXTRA[@]+"${EXTRA[@]}"}
895
+ ;;
896
+ *) die "unknown mode '$MODE' (expected docker, tool or source)" ;;
897
+ esac