@uniqbit/mate-core 0.18.5 → 1.0.0-canary.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 (390) hide show
  1. package/claude-capability-plugins/companion-mentions/.claude-plugin/marketplace.json +14 -0
  2. package/claude-capability-plugins/companion-mentions/.claude-plugin/plugin.json +8 -0
  3. package/claude-capability-plugins/companion-mentions/hooks/companion.ts +457 -0
  4. package/claude-capability-plugins/companion-mentions/hooks/hooks.json +1 -0
  5. package/claude-capability-plugins/companion-mentions/hooks/register.ts +403 -0
  6. package/claude-plugin/hooks/companion-instructions.mjs +9 -0
  7. package/claude-plugin/hooks/hooks.json +21 -2
  8. package/claude-plugin/hooks/tokensave-route.mjs +52 -0
  9. package/opencode-plugin/src/server.ts +4 -0
  10. package/opencode-plugin/src/tui.tsx +2 -0
  11. package/opencode-plugin/tui.tsx +2 -0
  12. package/package.json +52 -15
  13. package/src/brand/art.generated.ts +69 -0
  14. package/src/brand/index.ts +12 -0
  15. package/src/brand/palette.ts +19 -0
  16. package/src/brand/svg.generated.ts +8 -0
  17. package/src/brand/terminal.ts +88 -0
  18. package/src/brand/types.ts +33 -0
  19. package/src/cli/commands/artifact/apply-context.ts +275 -0
  20. package/src/cli/commands/artifact/artifact.ts +12 -0
  21. package/src/cli/commands/artifact/delivery/archive.ts +125 -0
  22. package/src/cli/commands/artifact/delivery/branch-flags.ts +55 -0
  23. package/src/cli/commands/artifact/delivery/environment.ts +127 -0
  24. package/src/cli/commands/artifact/delivery/link.ts +174 -0
  25. package/src/cli/commands/artifact/delivery/output.ts +89 -0
  26. package/src/cli/commands/artifact/finish/command.ts +83 -8
  27. package/src/cli/commands/artifact/finish/engine.ts +91 -14
  28. package/src/cli/commands/artifact/finish/finisher.ts +10 -0
  29. package/src/cli/commands/artifact/finish/git.ts +132 -19
  30. package/src/cli/commands/artifact/finish/openspec.ts +182 -138
  31. package/src/cli/commands/artifact/finish/secret-scan.ts +43 -0
  32. package/src/cli/commands/artifact/finish/worktree-cleanup.ts +92 -0
  33. package/src/cli/commands/artifact/pending/command.ts +31 -3
  34. package/src/cli/commands/artifact/pending/discovery.ts +21 -57
  35. package/src/cli/commands/cap/graphify.ts +52 -3
  36. package/src/cli/commands/cap/index-cmd.ts +71 -15
  37. package/src/cli/commands/cap/openspec.ts +13 -25
  38. package/src/cli/commands/cap/tokensave.ts +7 -5
  39. package/src/cli/commands/companion/link.ts +14 -1
  40. package/src/cli/commands/companion/open.ts +68 -46
  41. package/src/cli/commands/companion/sync.ts +10 -0
  42. package/src/cli/commands/companion/tui.ts +17 -7
  43. package/src/cli/commands/companion/update.ts +143 -48
  44. package/src/cli/commands/doctor.ts +195 -7
  45. package/src/cli/commands/factory/factory.ts +301 -0
  46. package/src/cli/commands/install.ts +133 -9
  47. package/src/cli/commands/launch/shared.ts +16 -11
  48. package/src/cli/commands/plugin/install.ts +9 -4
  49. package/src/cli/commands/plugin/plugin.ts +3 -2
  50. package/src/cli/commands/report/adapter.ts +11 -2
  51. package/src/cli/commands/report/contract.ts +21 -0
  52. package/src/cli/commands/report/highlight.ts +34 -4
  53. package/src/cli/commands/report/index.ts +20 -1
  54. package/src/cli/commands/report/markdown.ts +110 -0
  55. package/src/cli/commands/report/palette.ts +118 -0
  56. package/src/cli/commands/report/renderer.ts +456 -153
  57. package/src/cli/commands/report/types.ts +16 -0
  58. package/src/cli/commands/session/context.ts +102 -0
  59. package/src/cli/commands/setup.ts +50 -24
  60. package/src/cli/commands/studio/factory-credential.ts +49 -0
  61. package/src/cli/commands/studio/index.ts +29 -2
  62. package/src/cli/commands/studio/openspec-cli.ts +10 -58
  63. package/src/cli/commands/studio/payload.ts +249 -15
  64. package/src/cli/commands/studio/routes.ts +41 -6
  65. package/src/cli/commands/studio/selection.ts +161 -17
  66. package/src/cli/commands/studio/server.ts +404 -324
  67. package/src/cli/commands/studio/services.ts +60 -0
  68. package/src/cli/commands/studio/studio-app.ts +67 -0
  69. package/src/cli/commands/studio/terminal-assets.ts +21 -33
  70. package/src/cli/commands/studio/terminal.ts +85 -15
  71. package/src/cli/commands/studio/views/model.ts +18 -0
  72. package/src/cli/commands/studio/views/vault/markdown-tree.ts +267 -0
  73. package/src/cli/commands/studio/views/vault/view.ts +185 -0
  74. package/src/cli/commands/studio/workset-session.ts +131 -0
  75. package/src/cli/commands/unwrap.ts +8 -0
  76. package/src/cli/commands/update.ts +27 -1
  77. package/src/cli/commands/working/cleanup.ts +10 -1
  78. package/src/cli/commands/working/link.ts +76 -0
  79. package/src/cli/commands/working/working.ts +10 -0
  80. package/src/cli/commands/working/worktree.ts +356 -0
  81. package/src/cli/commands/wrap.ts +5 -13
  82. package/src/cli/companion-link-wizard.tsx +2 -0
  83. package/src/cli/companion-selector.tsx +2 -0
  84. package/src/cli/confirm.ts +2 -0
  85. package/src/cli/install-plan.tsx +127 -160
  86. package/src/cli/launch-selector.tsx +2 -0
  87. package/src/cli/main.ts +22 -1
  88. package/src/cli/parse-flags.ts +32 -6
  89. package/src/cli/repo-list-table.tsx +2 -0
  90. package/src/cli/setup-selector.tsx +86 -151
  91. package/src/cli/usage.ts +14 -0
  92. package/src/create-mate.ts +5 -4
  93. package/src/hooks/companion-instructions.ts +190 -0
  94. package/src/hooks/hook-io.ts +63 -0
  95. package/src/hooks/session-banner.ts +21 -29
  96. package/src/hooks/session-guidance.ts +9 -17
  97. package/src/hooks/session-prepare.ts +115 -28
  98. package/src/hooks/tokensave-route.ts +26 -0
  99. package/src/hooks/validate-artifact-path.ts +82 -24
  100. package/src/index.ts +4 -0
  101. package/src/lib/apply-context/fingerprint.ts +174 -0
  102. package/src/lib/apply-context/protocol.ts +193 -0
  103. package/src/lib/change-layout.ts +154 -0
  104. package/src/lib/components/confirm-prompt.tsx +21 -9
  105. package/src/lib/components/select-menu.tsx +10 -3
  106. package/src/lib/components/spinner.tsx +4 -2
  107. package/src/lib/components/startup-progress.tsx +227 -41
  108. package/src/lib/components/text-input-display.tsx +2 -0
  109. package/src/lib/components/wizard-footer.tsx +2 -0
  110. package/src/lib/components/wizard-header.tsx +16 -12
  111. package/src/lib/context-mode-package.ts +24 -11
  112. package/src/lib/delivery/archive-change.ts +166 -0
  113. package/src/lib/delivery/branches.ts +132 -0
  114. package/src/lib/delivery/environment.ts +35 -0
  115. package/src/lib/delivery/forge.ts +279 -0
  116. package/src/lib/delivery/merge-git.ts +261 -0
  117. package/src/lib/delivery/overlap.ts +209 -0
  118. package/src/lib/delivery/record-delivery.ts +283 -0
  119. package/src/lib/delivery/record.ts +403 -0
  120. package/src/lib/delivery/refresh.ts +268 -0
  121. package/src/lib/delivery/repository-access.ts +141 -0
  122. package/src/lib/delivery/resolve-branch.ts +162 -0
  123. package/src/lib/delivery/stages.ts +111 -0
  124. package/src/lib/delivery/token.ts +24 -0
  125. package/src/lib/factory/attach-client.ts +113 -0
  126. package/src/lib/factory/attach.ts +178 -0
  127. package/src/lib/factory/broker.ts +190 -0
  128. package/src/lib/factory/bun-commands.ts +39 -0
  129. package/src/lib/factory/client.ts +92 -0
  130. package/src/lib/factory/command.ts +61 -0
  131. package/src/lib/factory/credential-store.ts +153 -0
  132. package/src/lib/factory/frames.ts +77 -0
  133. package/src/lib/factory/ids.ts +58 -0
  134. package/src/lib/factory/keyed-serial.ts +27 -0
  135. package/src/lib/factory/outcomes/actions.ts +114 -0
  136. package/src/lib/factory/outcomes/index.ts +381 -0
  137. package/src/lib/factory/outcomes/lifecycle.ts +205 -0
  138. package/src/lib/factory/outcomes/runs.ts +81 -0
  139. package/src/lib/factory/outcomes/sessions.ts +38 -0
  140. package/src/lib/factory/prepare.ts +49 -0
  141. package/src/lib/factory/pty.ts +103 -0
  142. package/src/lib/factory/runner/companion-context.ts +178 -0
  143. package/src/lib/factory/runner/control.ts +92 -0
  144. package/src/lib/factory/runner/harness-agent.ts +250 -0
  145. package/src/lib/factory/runner/live.ts +159 -0
  146. package/src/lib/factory/runner/questions.ts +22 -0
  147. package/src/lib/factory/runner/rank.ts +26 -0
  148. package/src/lib/factory/runner/run-policy.ts +12 -0
  149. package/src/lib/factory/runner/run.ts +199 -0
  150. package/src/lib/factory/runner/scheduler.ts +416 -0
  151. package/src/lib/factory/runner/state.ts +233 -0
  152. package/src/lib/factory/runner/workflow.ts +542 -0
  153. package/src/lib/factory/runtime.ts +972 -0
  154. package/src/lib/factory/sandbox.ts +292 -0
  155. package/src/lib/factory/worktree-branch.ts +17 -0
  156. package/src/lib/frontmatter.ts +18 -0
  157. package/src/lib/install.ts +66 -14
  158. package/src/lib/live-progress.ts +36 -0
  159. package/src/lib/member-choices.ts +67 -0
  160. package/src/lib/opencode-plugin-package.ts +5 -5
  161. package/src/lib/openspec-cli.ts +116 -0
  162. package/src/lib/openspec-scopes.ts +86 -0
  163. package/src/lib/orchestrator/adapters/base.ts +55 -22
  164. package/src/lib/orchestrator/adapters/claude-args.ts +50 -0
  165. package/src/lib/orchestrator/adapters/claude.ts +54 -41
  166. package/src/lib/orchestrator/adapters/opencode.ts +2 -0
  167. package/src/lib/orchestrator/config-store.ts +3 -2
  168. package/src/lib/orchestrator/editor.ts +31 -25
  169. package/src/lib/orchestrator/openspec-store.ts +14 -54
  170. package/src/lib/orchestrator/projection-claude-commands.ts +36 -0
  171. package/src/lib/orchestrator/projection-claude-links.ts +159 -0
  172. package/src/lib/orchestrator/projection-claude-skills.ts +19 -137
  173. package/src/lib/orchestrator/projection-entries.ts +100 -34
  174. package/src/lib/orchestrator/projection-opencode-copies.ts +32 -15
  175. package/src/lib/orchestrator/projection-openspec-store.ts +54 -9
  176. package/src/lib/orchestrator/projection-runtime-documents.ts +15 -6
  177. package/src/lib/orchestrator/projection-types.ts +36 -1
  178. package/src/lib/orchestrator/refresh-working-claude-links.ts +87 -0
  179. package/src/lib/orchestrator/session-context-link.ts +67 -0
  180. package/src/lib/orchestrator/session-prepare.ts +45 -5
  181. package/src/lib/orchestrator/setup-compatibilities.ts +56 -70
  182. package/src/lib/orchestrator/types.ts +7 -2
  183. package/src/lib/orchestrator/working-repo-projection.ts +50 -7
  184. package/src/lib/orchestrator/workspace-document.ts +105 -0
  185. package/src/lib/orchestrator/wrap-pass.ts +22 -1
  186. package/src/lib/package-paths.ts +37 -0
  187. package/src/lib/runtime-directory-refresh.ts +17 -1
  188. package/src/lib/workset/record.ts +513 -0
  189. package/src/lib/workset/repositories.ts +55 -0
  190. package/src/lib/workset/stage.ts +148 -0
  191. package/src/lib/workset/stages.ts +113 -0
  192. package/src/lib/workset/worktree-index.ts +116 -0
  193. package/src/lib/workset/worktree.ts +830 -0
  194. package/src/opencode/companion-hooks.ts +109 -7
  195. package/src/opencode/companion-policy.ts +37 -18
  196. package/src/opencode/enter-worktree.ts +72 -0
  197. package/src/opencode/projected-guidance.ts +4 -1
  198. package/src/opencode/session-prepare.ts +2 -1
  199. package/src/opencode/tui.tsx +13 -6
  200. package/src/plugins.ts +1 -0
  201. package/src/runtime/artifact-refusal.ts +63 -0
  202. package/src/runtime/claude-delivery-plan.ts +107 -8
  203. package/src/runtime/commit-attribution-guard.ts +181 -0
  204. package/src/runtime/companion-git.ts +52 -10
  205. package/src/runtime/companion-guidance.ts +20 -8
  206. package/src/runtime/companion-instructions.ts +340 -0
  207. package/src/runtime/companion-lock.ts +30 -7
  208. package/src/runtime/env.ts +48 -3
  209. package/src/runtime/error-message.ts +4 -0
  210. package/src/runtime/git-layout.ts +219 -0
  211. package/src/runtime/index.ts +29 -0
  212. package/src/runtime/managed-block.ts +117 -0
  213. package/src/runtime/markdown-scan.ts +266 -0
  214. package/src/runtime/physical-path.ts +111 -0
  215. package/src/runtime/policy.ts +50 -2
  216. package/src/runtime/projected-flag.ts +7 -0
  217. package/src/runtime/projected-guidance.ts +1 -0
  218. package/src/runtime/runtime-directory.ts +5 -0
  219. package/src/runtime/runtime-document-manifest.ts +62 -11
  220. package/src/runtime/secret-guard.ts +162 -0
  221. package/src/runtime/secret-patterns.ts +397 -0
  222. package/src/runtime/secret-scan.ts +90 -0
  223. package/src/runtime/session-context.ts +98 -0
  224. package/src/runtime/session-kind.ts +13 -0
  225. package/src/runtime/shell-heredoc.ts +81 -0
  226. package/src/runtime/tokensave-route.ts +152 -0
  227. package/src/runtime/wrap-state.ts +44 -2
  228. package/src/runtime/write-atomic.ts +64 -0
  229. package/src/studio.ts +124 -0
  230. package/src/templates/capabilities/openspec-cap/mate-minimal/schema.yaml +14 -10
  231. package/src/templates/capabilities/openspec-cap/mate-minimal/templates/spec.md +3 -5
  232. package/src/templates/capabilities/openspec-cap/mate-v1/schema.yaml +18 -102
  233. package/src/templates/capabilities/openspec-cap/mate-v1/templates/spec.md +3 -5
  234. package/src/templates/capabilities/openspec-cap/openspec-conventions.yaml +20 -14
  235. package/src/templates/mate-skills/THIRD-PARTY-NOTICES.md +40 -0
  236. package/src/templates/mate-skills/agents/mate-apply-change/SKILL.md +134 -0
  237. package/src/templates/mate-skills/agents/mate-apply-change/references/mate-compact-apply.md +47 -0
  238. package/src/templates/mate-skills/agents/mate-apply-change/references/openspec-apply-change.md +200 -0
  239. package/src/templates/mate-skills/agents/mate-archive-change/SKILL.md +86 -0
  240. package/src/templates/mate-skills/{claude/mate-openspec-backfill → agents/mate-backfill-spec}/SKILL.md +11 -4
  241. package/src/templates/mate-skills/agents/mate-clarify-intent/SKILL.md +110 -0
  242. package/src/templates/mate-skills/agents/mate-explore-change/SKILL.md +66 -0
  243. package/src/templates/mate-skills/agents/mate-explore-change/references/openspec-explore.md +350 -0
  244. package/src/templates/mate-skills/agents/mate-grill-me/SKILL.md +11 -4
  245. package/src/templates/mate-skills/agents/mate-grill-with-docs/SKILL.md +2 -2
  246. package/src/templates/mate-skills/agents/mate-interview-me/SKILL.md +12 -5
  247. package/src/templates/mate-skills/agents/mate-propose-change/SKILL.md +108 -0
  248. package/src/templates/mate-skills/agents/mate-propose-change/references/mate-revise.md +47 -0
  249. package/src/templates/mate-skills/agents/mate-propose-change/references/openspec-propose.md +169 -0
  250. package/src/templates/mate-skills/agents/mate-propose-change/references/openspec-update-change.md +103 -0
  251. package/src/templates/mate-skills/agents/{mate-artifact-publish → mate-publish-artifact}/SKILL.md +111 -24
  252. package/src/templates/mate-skills/{claude/mate-artifact-publish → agents/mate-publish-artifact}/references/openspec.md +8 -5
  253. package/src/templates/mate-skills/agents/mate-show-me/SKILL.md +95 -16
  254. package/src/templates/mate-skills/agents/mate-simplify-code/SKILL.md +87 -68
  255. package/src/templates/mate-skills/claude/mate-apply-change/SKILL.md +134 -0
  256. package/src/templates/mate-skills/claude/mate-apply-change/references/mate-compact-apply.md +47 -0
  257. package/src/templates/mate-skills/claude/mate-apply-change/references/openspec-apply-change.md +200 -0
  258. package/src/templates/mate-skills/claude/mate-archive-change/SKILL.md +86 -0
  259. package/src/templates/mate-skills/{agents/mate-openspec-backfill → claude/mate-backfill-spec}/SKILL.md +11 -4
  260. package/src/templates/mate-skills/claude/mate-clarify-intent/SKILL.md +110 -0
  261. package/src/templates/mate-skills/claude/mate-explore-change/SKILL.md +66 -0
  262. package/src/templates/mate-skills/claude/mate-explore-change/references/openspec-explore.md +350 -0
  263. package/src/templates/mate-skills/claude/mate-grill-me/SKILL.md +11 -4
  264. package/src/templates/mate-skills/claude/mate-grill-with-docs/SKILL.md +2 -2
  265. package/src/templates/mate-skills/claude/mate-interview-me/SKILL.md +12 -5
  266. package/src/templates/mate-skills/claude/mate-propose-change/SKILL.md +108 -0
  267. package/src/templates/mate-skills/claude/mate-propose-change/references/mate-revise.md +47 -0
  268. package/src/templates/mate-skills/claude/mate-propose-change/references/openspec-propose.md +169 -0
  269. package/src/templates/mate-skills/claude/mate-propose-change/references/openspec-update-change.md +103 -0
  270. package/src/templates/mate-skills/claude/{mate-artifact-publish → mate-publish-artifact}/SKILL.md +111 -24
  271. package/src/templates/mate-skills/{agents/mate-artifact-publish → claude/mate-publish-artifact}/references/openspec.md +8 -5
  272. package/src/templates/mate-skills/claude/mate-show-me/SKILL.md +95 -16
  273. package/src/templates/mate-skills/claude/mate-simplify-code/SKILL.md +87 -68
  274. package/src/templates/mate-skills/openspec-references.ts +142 -0
  275. package/src/templates/root/TEMPLATE_AGENTS.md +2 -2
  276. package/src/tools/setup/__snapshots__/runtime-surface-golden.test.ts.snap +3938 -391
  277. package/src/tools/setup/capabilities/companion-mentions.ts +27 -0
  278. package/src/tools/setup/capabilities/context-mode.ts +56 -7
  279. package/src/tools/setup/capabilities/graphify-gate.ts +31 -0
  280. package/src/tools/setup/capabilities/graphify-secrets.ts +243 -0
  281. package/src/tools/setup/capabilities/graphify.ts +46 -7
  282. package/src/tools/setup/capabilities/openspec.ts +163 -272
  283. package/src/tools/setup/capabilities/react-doctor.ts +8 -1
  284. package/src/tools/setup/capabilities/rtk.ts +4 -0
  285. package/src/tools/setup/capabilities/tokensave.ts +113 -71
  286. package/src/tools/setup/context-services.ts +9 -17
  287. package/src/tools/setup/dynamic-plugins/install.ts +24 -28
  288. package/src/tools/setup/engine.ts +100 -23
  289. package/src/tools/setup/install-contract.ts +8 -0
  290. package/src/tools/setup/managed-output.ts +51 -0
  291. package/src/tools/setup/mate-skill-copies.ts +51 -0
  292. package/src/tools/setup/mate.ts +86 -11
  293. package/src/tools/setup/package-managers/uv.ts +73 -11
  294. package/src/tools/setup/plugin.ts +40 -0
  295. package/src/tools/setup/plugins/guidance.ts +7 -4
  296. package/src/tools/setup/providers/agent-file-sections.ts +4 -2
  297. package/src/tools/setup/providers/claude-attribution.ts +65 -0
  298. package/src/tools/setup/providers/claude-plugin-folders.ts +625 -0
  299. package/src/tools/setup/providers/claude-plugin-hooks.ts +51 -19
  300. package/src/tools/setup/providers/claude-worktree-plugins.ts +122 -0
  301. package/src/tools/setup/providers/claude.ts +290 -8
  302. package/src/tools/setup/providers/opencode.ts +180 -1
  303. package/src/tools/setup/utils.ts +18 -4
  304. package/src/tools/setup/working-repo-cleanup.ts +29 -1
  305. package/src/tools/setup/working-repo-local-state.ts +252 -75
  306. package/src/tools/setup.ts +183 -26
  307. package/studio-dist/client/assets/companion-view-Csva26Ir.js +1 -0
  308. package/studio-dist/client/assets/delivery-path-DJMpRmiY.js +1 -0
  309. package/studio-dist/client/assets/dist-Dm7MtyHc.js +5 -0
  310. package/studio-dist/client/assets/factory-B7l9ZQyx.js +4 -0
  311. package/studio-dist/client/assets/index-n6uo_92S.js +40 -0
  312. package/studio-dist/client/assets/preload-helper-BLdHbfdN.js +1 -0
  313. package/studio-dist/client/assets/prepaint-CD5OpmTM.js +24 -0
  314. package/studio-dist/client/assets/queries-CN3JwMBX.js +1 -0
  315. package/studio-dist/client/assets/react-CabXf2Ds.js +1 -0
  316. package/studio-dist/client/assets/report-time-Dc-KOqQu.js +1 -0
  317. package/studio-dist/client/assets/reports-C01RsnMA.js +1 -0
  318. package/studio-dist/client/assets/rolldown-runtime-WkwZ5CU-.js +1 -0
  319. package/studio-dist/client/assets/routes-4pbC-2Hj.js +1 -0
  320. package/studio-dist/client/assets/skills-CihQganl.js +1 -0
  321. package/studio-dist/client/assets/spec-areas-C2L48N4C.js +1 -0
  322. package/studio-dist/client/assets/specs-D24_35_K.js +1 -0
  323. package/studio-dist/client/assets/studio-DKxfi691.css +1 -0
  324. package/studio-dist/client/assets/studio-queries-rvbJupCy.js +3 -0
  325. package/studio-dist/client/assets/terminal-eU5fzQzv.css +1 -0
  326. package/studio-dist/client/assets/vault-Bd4CQiTK.js +1 -0
  327. package/studio-dist/client/assets/warnings-V_Ys51wJ.js +1 -0
  328. package/studio-dist/client/assets/wasm-inline-C2en61B5.js +1 -0
  329. package/studio-dist/client/assets/workflow-BJejeVwQ.js +1 -0
  330. package/studio-dist/server/assets/__23tanstack-start-server-fn-resolver-BQKuMdJW.js +3881 -0
  331. package/studio-dist/server/assets/_tanstack-start-manifest_v.js +85 -0
  332. package/studio-dist/server/assets/actions-Bvgj9eb8.js +54 -0
  333. package/studio-dist/server/assets/actions-D8erOphH.js +44 -0
  334. package/studio-dist/server/assets/companion-view-aBady57v.js +33 -0
  335. package/studio-dist/server/assets/createServerFn-F5sAOF8V.js +459 -0
  336. package/studio-dist/server/assets/createServerRpc-tK2lE9R2.js +11 -0
  337. package/studio-dist/server/assets/delivery-path-BkKNjgkf.js +778 -0
  338. package/studio-dist/server/assets/dist-DpJWKyCm.js +5582 -0
  339. package/studio-dist/server/assets/empty-plugin-adapters-Cipd4ZH4.js +4 -0
  340. package/studio-dist/server/assets/factory-D6xnjedJ.js +2385 -0
  341. package/studio-dist/server/assets/factory-DT3UzHlh.js +68 -0
  342. package/studio-dist/server/assets/model-PyPALjii.js +18 -0
  343. package/studio-dist/server/assets/prepaint-BMke0fo1.js +41 -0
  344. package/studio-dist/server/assets/queries-Ag2iL8q2.js +107 -0
  345. package/studio-dist/server/assets/react-COs0LHn-.js +51 -0
  346. package/studio-dist/server/assets/report-time-DAfzln-N.js +8 -0
  347. package/studio-dist/server/assets/reports-BnUNJVtx.js +126 -0
  348. package/studio-dist/server/assets/rolldown-runtime-DYf_mQCB.js +34 -0
  349. package/studio-dist/server/assets/router-BH--drIx.js +4298 -0
  350. package/studio-dist/server/assets/routes-khSDK1bV.js +361 -0
  351. package/studio-dist/server/assets/search-JAKurDNh.js +36 -0
  352. package/studio-dist/server/assets/shell-CboT2f66.js +51 -0
  353. package/studio-dist/server/assets/skills-MWYnWoKs.js +106 -0
  354. package/studio-dist/server/assets/spec-areas-DcnO6vW-.js +16 -0
  355. package/studio-dist/server/assets/specs-DE3w8hZP.js +155 -0
  356. package/studio-dist/server/assets/start-BW5ovN0k.js +33 -0
  357. package/studio-dist/server/assets/studio-BeltxxKt.js +216 -0
  358. package/studio-dist/server/assets/studio-queries-CLnP9kGu.js +3365 -0
  359. package/studio-dist/server/assets/utils-C9CX1kcy.js +221 -0
  360. package/studio-dist/server/assets/vault-BGulVoNh.js +70 -0
  361. package/studio-dist/server/assets/vault-CuyyhLyB.js +832 -0
  362. package/studio-dist/server/assets/views-Bd7cRr27.js +82 -0
  363. package/studio-dist/server/assets/warnings-BH05rOuG.js +15 -0
  364. package/studio-dist/server/assets/wasm-inline-CNeBKGwC.js +3 -0
  365. package/studio-dist/server/assets/workflow-BSuIKWqH.js +42 -0
  366. package/studio-dist/server/server.js +16220 -0
  367. package/wrappers/bin/graphify +52 -4
  368. package/wrappers/bin/openspec +33 -9
  369. package/wrappers/lib/graphify-gate.mjs +8 -0
  370. package/src/cli/commands/studio/views/client.ts +0 -654
  371. package/src/cli/commands/studio/views/companion-picker.tsx +0 -95
  372. package/src/cli/commands/studio/views/companion-selector.tsx +0 -94
  373. package/src/cli/commands/studio/views/dashboard/changes.tsx +0 -107
  374. package/src/cli/commands/studio/views/dashboard/index.tsx +0 -19
  375. package/src/cli/commands/studio/views/document.tsx +0 -842
  376. package/src/cli/commands/studio/views/error.tsx +0 -20
  377. package/src/cli/commands/studio/views/reports/index.tsx +0 -49
  378. package/src/cli/commands/studio/views/selection-fields.tsx +0 -14
  379. package/src/cli/commands/studio/views/skills/index.tsx +0 -87
  380. package/src/cli/commands/studio/views/specs/index.tsx +0 -157
  381. package/src/cli/commands/studio/views/styles.ts +0 -519
  382. package/src/cli/commands/studio/views/terminal.tsx +0 -484
  383. package/src/cli/commands/studio/views/vault/state.ts +0 -43
  384. package/src/cli/commands/studio/views/warnings.tsx +0 -21
  385. package/src/cli/commands/studio/views/workflow/index.tsx +0 -21
  386. package/src/cli/commands/studio/views/workflow/steps.ts +0 -337
  387. package/src/cli/commands/studio/views/workflow/transcript.tsx +0 -190
  388. package/src/lib/components/theme.ts +0 -2
  389. package/src/tools/setup/git-utils.ts +0 -30
  390. package/src/tools/setup/runtime-documents.ts +0 -291
@@ -0,0 +1,219 @@
1
+ import fs from "node:fs";
2
+ import path from "node:path";
3
+
4
+ /**
5
+ * A Checkout's Git layout, read from files alone: no `git` process, no
6
+ * inherited `GIT_*` variables, callable from synchronous code. Every path is
7
+ * real, so callers may compare them directly.
8
+ *
9
+ * Known limits: a worktree of a `--separate-git-dir` clone finds that clone's
10
+ * checkout only through `core.worktree`, since no other file records it; and
11
+ * only the shared `config` is read, not its `include`s or `config.worktree`.
12
+ */
13
+
14
+ /**
15
+ * `main`: `.git` is a folder. `linked`: a worktree whose Git folder names a
16
+ * shared one through `commondir`. `own`: `.git` points to a Git folder that
17
+ * names no shared one, as a submodule's or a `--separate-git-dir` clone's.
18
+ */
19
+ export type GitLayoutKind = "main" | "linked" | "own";
20
+
21
+ export interface GitLayout {
22
+ kind: GitLayoutKind;
23
+ /** The Checkout's own Git folder. */
24
+ gitDir: string;
25
+ /** The folder all Checkouts of the clone share; Git reads `info/exclude` only here. */
26
+ commonDir: string;
27
+ }
28
+
29
+ function realPath(candidate: string): string {
30
+ try {
31
+ return fs.realpathSync(candidate);
32
+ } catch {
33
+ return path.resolve(candidate);
34
+ }
35
+ }
36
+
37
+ function readText(filePath: string): string | null {
38
+ try {
39
+ return fs.readFileSync(filePath, "utf8");
40
+ } catch {
41
+ return null;
42
+ }
43
+ }
44
+
45
+ function isDirectory(candidate: string): boolean {
46
+ return fs.statSync(candidate, { throwIfNoEntry: false })?.isDirectory() ?? false;
47
+ }
48
+
49
+ /** The folder a `.git` file names, resolved against the file's own folder. */
50
+ function gitFileTarget(dotGit: string): string | null {
51
+ const match = readText(dotGit)?.match(/^gitdir:\s*(.+?)\s*$/m);
52
+ return match ? path.resolve(path.dirname(dotGit), match[1]) : null;
53
+ }
54
+
55
+ /** A one-line file whose path value is resolved against `base`. */
56
+ function readPathFile(filePath: string, base: string): string | null {
57
+ const value = readText(filePath)?.trim();
58
+ return value ? path.resolve(base, value) : null;
59
+ }
60
+
61
+ export function readGitLayout(checkoutPath: string): GitLayout | null {
62
+ /** Real first: a relative `gitdir:` is relative to where `.git` really is. */
63
+ const dotGit = path.join(realPath(checkoutPath), ".git");
64
+ const stat = fs.statSync(dotGit, { throwIfNoEntry: false });
65
+ if (stat?.isDirectory()) {
66
+ const gitDir = realPath(dotGit);
67
+ return { kind: "main", gitDir, commonDir: gitDir };
68
+ }
69
+ if (!stat?.isFile()) return null;
70
+ const target = gitFileTarget(dotGit);
71
+ if (!target || !isDirectory(target)) return null;
72
+ const gitDir = realPath(target);
73
+ const commonDir = readPathFile(path.join(gitDir, "commondir"), gitDir);
74
+ return commonDir && isDirectory(commonDir)
75
+ ? { kind: "linked", gitDir, commonDir: realPath(commonDir) }
76
+ : { kind: "own", gitDir, commonDir: gitDir };
77
+ }
78
+
79
+ /** The real path of the nearest folder at or above `directory` holding a `.git` entry. */
80
+ export function checkoutRootOf(directory: string): string | null {
81
+ let current = realPath(directory);
82
+ for (;;) {
83
+ if (fs.existsSync(path.join(current, ".git"))) return current;
84
+ const parent = path.dirname(current);
85
+ if (parent === current) return null;
86
+ current = parent;
87
+ }
88
+ }
89
+
90
+ /** True when both Checkouts belong to one clone: the same one, or worktrees of one Git folder. */
91
+ export function isSameClone(first: string, second: string): boolean {
92
+ const one = readGitLayout(first);
93
+ const other = readGitLayout(second);
94
+ return !!one && !!other && one.commonDir === other.commonDir;
95
+ }
96
+
97
+ /** True when `worktree` is a linked worktree of the clone `checkout` belongs to. */
98
+ export function isLinkedWorktreeOf(worktree: string, checkout: string): boolean {
99
+ const own = readGitLayout(worktree);
100
+ const main = readGitLayout(checkout);
101
+ return own?.kind === "linked" && !!main && own.commonDir === main.commonDir;
102
+ }
103
+
104
+ const CONFIG_ESCAPES: Record<string, string> = { n: "\n", t: "\t" };
105
+
106
+ /** A config value without its comment, quotes and escapes, as Git reads it. */
107
+ function configValue(raw: string): string {
108
+ let value = "";
109
+ let quoted = false;
110
+ for (let index = 0; index < raw.length; index += 1) {
111
+ const char = raw[index];
112
+ if (char === "\\") {
113
+ const next = raw[index + 1] ?? "";
114
+ value += CONFIG_ESCAPES[next] ?? next;
115
+ index += 1;
116
+ } else if (char === '"') {
117
+ quoted = !quoted;
118
+ } else if (!quoted && (char === "#" || char === ";")) {
119
+ break;
120
+ } else {
121
+ value += char;
122
+ }
123
+ }
124
+ return value.trim();
125
+ }
126
+
127
+ /** `[core]` values from a Git config file; enough for `bare` and `worktree`. */
128
+ function coreConfig(commonDir: string): Map<string, string> {
129
+ const values = new Map<string, string>();
130
+ let inCore = false;
131
+ for (const raw of (readText(path.join(commonDir, "config")) ?? "").split(/\r?\n/)) {
132
+ let line = raw.trim();
133
+ /** `[core]` only: `[core "x"]` is a subsection, and a key may follow the header. */
134
+ const section = /^\[\s*([A-Za-z0-9.-]+)\s*("(?:[^"\\]|\\.)*")?\s*\](.*)$/.exec(line);
135
+ if (section) {
136
+ inCore = section[1].toLowerCase() === "core" && section[2] === undefined;
137
+ line = section[3].trim();
138
+ }
139
+ const entry = inCore ? /^([A-Za-z][\w-]*)\s*(?:=(.*)|[#;].*)?$/.exec(line) : null;
140
+ if (!entry) continue;
141
+ /** A key with no `=` is a true boolean. */
142
+ values.set(entry[1].toLowerCase(), entry[2] === undefined ? "true" : configValue(entry[2]));
143
+ }
144
+ return values;
145
+ }
146
+
147
+ /** Git's spellings of true: `true`, `yes`, `on`, any non-zero number. */
148
+ function configTrue(value: string | undefined): boolean {
149
+ if (value === undefined) return false;
150
+ const lower = value.toLowerCase();
151
+ if (["true", "yes", "on"].includes(lower)) return true;
152
+ return /^-?\d+$/.test(lower) && Number(lower) !== 0;
153
+ }
154
+
155
+ /**
156
+ * The worktree whose `.git` is the shared folder itself, else `core.worktree`;
157
+ * none for a bare repository.
158
+ */
159
+ function mainWorktree(commonDir: string): string | null {
160
+ const core = coreConfig(commonDir);
161
+ if (configTrue(core.get("bare"))) return null;
162
+ const configured = core.get("worktree");
163
+ if (configured) {
164
+ const worktree = path.resolve(commonDir, configured);
165
+ return isDirectory(worktree) ? realPath(worktree) : null;
166
+ }
167
+ const parent = path.dirname(commonDir);
168
+ const layout = readGitLayout(parent);
169
+ return layout && layout.gitDir === commonDir ? realPath(parent) : null;
170
+ }
171
+
172
+ /**
173
+ * The exclude file Git reads for a Checkout: the shared Git folder's, never a
174
+ * linked worktree's private one. `null` outside Git.
175
+ */
176
+ export function gitExcludePath(checkoutPath: string): string | null {
177
+ const layout = readGitLayout(checkoutPath);
178
+ return layout ? path.join(layout.commonDir, "info", "exclude") : null;
179
+ }
180
+
181
+ /**
182
+ * Each worktree the shared folder lists whose `.git` file still points back to
183
+ * it, and each locked one: Git keeps a locked worktree registered while its
184
+ * drive is away, and so does this list.
185
+ */
186
+ function linkedWorktrees(commonDir: string): string[] {
187
+ const adminRoot = path.join(commonDir, "worktrees");
188
+ let names: string[];
189
+ try {
190
+ names = fs.readdirSync(adminRoot).toSorted();
191
+ } catch {
192
+ return [];
193
+ }
194
+ const worktrees: string[] = [];
195
+ for (const name of names) {
196
+ const adminDir = path.join(adminRoot, name);
197
+ const dotGit = readPathFile(path.join(adminDir, "gitdir"), adminDir);
198
+ if (!dotGit || path.basename(dotGit) !== ".git") continue;
199
+ const checkout = path.dirname(dotGit);
200
+ const live = readGitLayout(checkout)?.gitDir === realPath(adminDir);
201
+ if (live || fs.existsSync(path.join(adminDir, "locked"))) worktrees.push(realPath(checkout));
202
+ }
203
+ return worktrees;
204
+ }
205
+
206
+ /**
207
+ * The clone's other live Checkouts: its main worktree and each linked worktree
208
+ * that still points back or is locked. A locked one may be unreachable.
209
+ * Empty when the layout cannot be read.
210
+ */
211
+ export function otherCheckouts(checkoutPath: string): string[] {
212
+ const layout = readGitLayout(checkoutPath);
213
+ if (!layout) return [];
214
+ const self = realPath(checkoutPath);
215
+ const main = layout.kind === "linked" ? mainWorktree(layout.commonDir) : self;
216
+ return [...(main ? [main] : []), ...linkedWorktrees(layout.commonDir)].filter(
217
+ (checkout) => checkout !== self,
218
+ );
219
+ }
@@ -1,3 +1,8 @@
1
+ export {
2
+ ARTIFACT_REFUSAL_HEADLINE,
3
+ artifactRefusal,
4
+ type ArtifactRefusalInput,
5
+ } from "./artifact-refusal";
1
6
  export {
2
7
  GRAPHIFY_SHARED_COMPANION_PATH_CONTRACT,
3
8
  GRAPHIFY_COMPANION_PATH_CONTRACT,
@@ -19,6 +24,7 @@ export {
19
24
  gitEnvironment,
20
25
  GIT_QUERY_TIMEOUT_MS,
21
26
  isAuthenticationFailure,
27
+ isTransientRemoteFailure,
22
28
  outputLines,
23
29
  stripGitProgress,
24
30
  type CompanionForkState,
@@ -88,6 +94,7 @@ export {
88
94
  companionFrameworkConfigPath,
89
95
  emptyCompanionPolicy,
90
96
  isCapabilityEnabled,
97
+ policyDocument,
91
98
  readCompanionPolicy,
92
99
  type CompanionPolicy,
93
100
  } from "./policy";
@@ -121,3 +128,25 @@ export {
121
128
  repoLocalRegistryPath,
122
129
  type RepoLocalRegistryFile,
123
130
  } from "./repo-local";
131
+ export {
132
+ SESSION_CONTEXT_PROTOCOL,
133
+ resolveSessionContext,
134
+ sessionContextFailure,
135
+ sessionContextReply,
136
+ type SessionContext,
137
+ type SessionContextError,
138
+ type SessionContextErrorCode,
139
+ type SessionContextResult,
140
+ } from "./session-context";
141
+ export {
142
+ DEFAULT_SECRET_ALLOW_PATTERNS,
143
+ DEFAULT_SECRET_DENY_PATTERNS,
144
+ defaultSecretPolicy,
145
+ isSecretFile,
146
+ parseSecretsConfig,
147
+ secretRule,
148
+ secretRules,
149
+ type SecretPolicy,
150
+ type SecretRule,
151
+ type SecretRules,
152
+ } from "./secret-patterns";
@@ -0,0 +1,117 @@
1
+ /**
2
+ * Shared by the setup writers and the Companion Instructions reader. In
3
+ * `runtime/` because hooks may import nothing outside it.
4
+ */
5
+
6
+ import { fencedRanges, trimBlankLines } from "./markdown-scan";
7
+
8
+ export interface ManagedBlockMarkers {
9
+ start: string;
10
+ end: string;
11
+ }
12
+
13
+ export function managedBlockMarkers(frameworkName: string, blockKey: string): ManagedBlockMarkers {
14
+ return {
15
+ start: `<!-- ${frameworkName}:managed:${blockKey} start -->`,
16
+ end: `<!-- ${frameworkName}:managed:${blockKey} end -->`,
17
+ };
18
+ }
19
+
20
+ export function companionBlockMarkers(frameworkName: string): ManagedBlockMarkers {
21
+ const name = frameworkName.toUpperCase();
22
+ return {
23
+ start: `<!-- ${name}:COMPANION:START -->`,
24
+ end: `<!-- ${name}:COMPANION:END -->`,
25
+ };
26
+ }
27
+
28
+ /**
29
+ * The plugin owning a block whose key is `<pluginId>:<content hash>`, the
30
+ * shape every setup writer produces; a key without a hash is returned whole.
31
+ */
32
+ export function managedBlockPluginId(blockKey: string): string {
33
+ const separator = blockKey.lastIndexOf(":");
34
+ return separator === -1 ? blockKey : blockKey.slice(0, separator);
35
+ }
36
+
37
+ function escapeRegExp(value: string): string {
38
+ return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
39
+ }
40
+
41
+ /** Matches a start marker; group 1 is the key. Given `pluginId`, only that plugin's blocks match. */
42
+ export function managedBlockStartPattern(frameworkName: string, pluginId?: string): RegExp {
43
+ const keyPattern = pluginId === undefined ? "[^\\s]+" : `${escapeRegExp(pluginId)}:[0-9a-f]+`;
44
+ return new RegExp(`<!-- ${escapeRegExp(frameworkName)}:managed:(${keyPattern}) start -->`, "g");
45
+ }
46
+
47
+ export interface ManagedBlock {
48
+ key: string;
49
+ /** Offsets of the whole block, markers included. */
50
+ start: number;
51
+ end: number;
52
+ body: string;
53
+ }
54
+
55
+ /** A marker quoted in a code fence is an example, not a block. */
56
+ function inFence(fenced: readonly [number, number][], index: number): boolean {
57
+ return fenced.some(([from, to]) => index >= from && index < to);
58
+ }
59
+
60
+ /** The first `marker` at or after `from` outside a code fence, or -1. */
61
+ function indexOutsideFence(
62
+ text: string,
63
+ marker: string,
64
+ from: number,
65
+ fenced: readonly [number, number][],
66
+ ): number {
67
+ let index = text.indexOf(marker, from);
68
+ while (index !== -1 && inFence(fenced, index)) {
69
+ index = text.indexOf(marker, index + marker.length);
70
+ }
71
+ return index;
72
+ }
73
+
74
+ /** Offsets of the first `MATE:COMPANION` block, markers included; `null` without one. */
75
+ export function findCompanionBlock(
76
+ text: string,
77
+ frameworkName: string,
78
+ ): { start: number; end: number } | null {
79
+ const { start, end } = companionBlockMarkers(frameworkName);
80
+ const fenced = fencedRanges(text);
81
+ const startIndex = indexOutsideFence(text, start, 0, fenced);
82
+ if (startIndex === -1) return null;
83
+ const endIndex = indexOutsideFence(text, end, startIndex + start.length, fenced);
84
+ return endIndex === -1 ? null : { start: startIndex, end: endIndex + end.length };
85
+ }
86
+
87
+ export function findManagedBlocks(text: string, frameworkName: string): ManagedBlock[] {
88
+ const blocks: ManagedBlock[] = [];
89
+ const fenced = fencedRanges(text);
90
+ for (const match of text.matchAll(managedBlockStartPattern(frameworkName))) {
91
+ if (inFence(fenced, match.index)) continue;
92
+ const key = match[1]!;
93
+ const { start, end } = managedBlockMarkers(frameworkName, key);
94
+ const bodyStart = match.index + start.length;
95
+ const endIndex = indexOutsideFence(text, end, bodyStart, fenced);
96
+ if (endIndex === -1) continue;
97
+ const previous = blocks.at(-1);
98
+ if (previous && match.index < previous.end) continue;
99
+ blocks.push({
100
+ key,
101
+ start: match.index,
102
+ end: endIndex + end.length,
103
+ body: trimBlankLines(text.slice(bodyStart, endIndex)),
104
+ });
105
+ }
106
+ return blocks;
107
+ }
108
+
109
+ export function stripManagedBlocks(text: string, frameworkName: string): string {
110
+ let result = "";
111
+ let cursor = 0;
112
+ for (const block of findManagedBlocks(text, frameworkName)) {
113
+ result += text.slice(cursor, block.start);
114
+ cursor = block.end;
115
+ }
116
+ return result + text.slice(cursor);
117
+ }
@@ -0,0 +1,266 @@
1
+ /**
2
+ * A line scan of CommonMark, deep enough to find `##` sections, fenced code
3
+ * and block-level HTML comments as Claude Code's memory loading reads them.
4
+ */
5
+
6
+ /** A block quote, or a list item whose content starts `width` columns into its parent's. */
7
+ type Container = { kind: "quote" } | { kind: "item"; width: number };
8
+
9
+ interface Fence {
10
+ char: string;
11
+ length: number;
12
+ /** The containers the fence opened in, outermost first; a line that leaves one ends the fence. */
13
+ containers: readonly Container[];
14
+ }
15
+
16
+ const FENCE_OPEN = /^(`{3,}|~{3,})(.*)$/;
17
+ const FENCE_CLOSE = /^(`{3,}|~{3,})[ \t]*$/;
18
+ const LIST_MARKER = /^(?:[-+*]|\d{1,9}[.)])/;
19
+ const BLANK = /^[ \t]*$/;
20
+
21
+ function indentation(text: string, from: number): { column: number; rest: string } {
22
+ let column = from;
23
+ let index = 0;
24
+ for (; index < text.length; index++) {
25
+ if (text[index] === " ") column++;
26
+ else if (text[index] === "\t") column += 4 - (column % 4);
27
+ else break;
28
+ }
29
+ return { column, rest: text.slice(index) };
30
+ }
31
+
32
+ /** Where a line's text stands: `rest` starts at `column`; the innermost container's content starts at `base`. */
33
+ interface Position {
34
+ column: number;
35
+ rest: string;
36
+ base: number;
37
+ }
38
+
39
+ /** CommonMark: `>` after up to three spaces of indentation, and one optional space that belongs to the marker. */
40
+ function afterQuoteMarker({ column, rest, base }: Position): Position | null {
41
+ if (column - base > 3 || !rest.startsWith(">")) return null;
42
+ const marker = column + 1;
43
+ const after = indentation(rest.slice(1), marker);
44
+ return { ...after, base: after.column > marker ? marker + 1 : marker };
45
+ }
46
+
47
+ /** The open containers a line continues, outermost first. A blank line continues list items, never a quote. */
48
+ function continueContainers(
49
+ line: string,
50
+ containers: readonly Container[],
51
+ ): { matched: Container[]; at: Position } {
52
+ let at: Position = { ...indentation(line, 0), base: 0 };
53
+ const matched: Container[] = [];
54
+ for (const container of containers) {
55
+ if (container.kind === "quote") {
56
+ const next = afterQuoteMarker(at);
57
+ if (!next) break;
58
+ at = next;
59
+ } else {
60
+ if (at.rest !== "" && at.column < at.base + container.width) break;
61
+ at = { ...at, base: at.base + container.width };
62
+ }
63
+ matched.push(container);
64
+ }
65
+ return { matched, at };
66
+ }
67
+
68
+ /** The open containers carried from line to line. */
69
+ interface ContainerContext {
70
+ containers: readonly Container[];
71
+ /** A paragraph is open, so a line that leaves its containers may continue it lazily. */
72
+ paragraph: boolean;
73
+ /** The innermost item opened on a blank line, so a blank line next ends it. */
74
+ emptyItem: boolean;
75
+ }
76
+
77
+ const TOP_LEVEL: ContainerContext = { containers: [], paragraph: false, emptyItem: false };
78
+
79
+ /** CommonMark: a line that starts a block, so it ends a paragraph rather than continuing it. */
80
+ const INTERRUPTS = /^(?:`{3,}|~{3,}|#{1,6}(?:[ \t]|$)|>|<!--|(?:[-+*]|\d{1,9}[.)])(?:[ \t]|$))/;
81
+ const THEMATIC_BREAK = /^(?:(?:\*[ \t]*){3,}|(?:-[ \t]*){3,}|(?:_[ \t]*){3,})$/;
82
+
83
+ /**
84
+ * Tracks only as much of CommonMark's block quotes and list items as placing a
85
+ * fence needs. A backtick fence's info string may hold no backtick, so
86
+ * ```` ```a``` ```` is inline code.
87
+ */
88
+ function lineBlock(
89
+ line: string,
90
+ context: ContainerContext,
91
+ ): { fence: Fence | null; context: ContainerContext } {
92
+ const { matched, at } = continueContainers(line, context.containers);
93
+ const continues = matched.length === context.containers.length;
94
+ let { column, rest, base } = at;
95
+ if (rest === "") {
96
+ const containers = continues && context.emptyItem ? matched.slice(0, -1) : matched;
97
+ return { fence: null, context: { containers, paragraph: false, emptyItem: false } };
98
+ }
99
+ if (!continues) {
100
+ const starts = INTERRUPTS.test(rest) || THEMATIC_BREAK.test(rest);
101
+ const lazy = context.paragraph && !(column - base <= 3 && starts);
102
+ if (lazy) return { fence: null, context };
103
+ }
104
+ let containers = matched;
105
+ let paragraph = context.paragraph && continues;
106
+
107
+ while (column - base <= 3 && !THEMATIC_BREAK.test(rest)) {
108
+ const quoted = afterQuoteMarker({ column, rest, base });
109
+ if (quoted) {
110
+ containers = [...containers, { kind: "quote" }];
111
+ ({ column, rest, base } = quoted);
112
+ paragraph = false;
113
+ if (rest === "") return { fence: null, context: { containers, paragraph, emptyItem: false } };
114
+ continue;
115
+ }
116
+ const marker = LIST_MARKER.exec(rest);
117
+ if (!marker) break;
118
+ const markerEnd = column + marker[0].length;
119
+ const after = indentation(rest.slice(marker[0].length), markerEnd);
120
+ const gap = after.column - markerEnd;
121
+ if (gap < 1 && after.rest !== "") break;
122
+ const ordered = marker[0].length > 1;
123
+ if (paragraph && (after.rest === "" || (ordered && Number.parseInt(marker[0], 10) !== 1))) {
124
+ break;
125
+ }
126
+ const content = gap > 4 || after.rest === "" ? markerEnd + 1 : after.column;
127
+ containers = [...containers, { kind: "item", width: content - base }];
128
+ base = content;
129
+ paragraph = false;
130
+ if (after.rest === "") {
131
+ return { fence: null, context: { containers, paragraph, emptyItem: true } };
132
+ }
133
+ column = after.column;
134
+ rest = after.rest;
135
+ }
136
+
137
+ if (column - base > 3) {
138
+ return { fence: null, context: { containers, paragraph, emptyItem: false } };
139
+ }
140
+ const match = FENCE_OPEN.exec(rest);
141
+ const run = match?.[1];
142
+ if (run && !(run.startsWith("`") && match[2]!.includes("`"))) {
143
+ const fence = { char: run[0]!, length: run.length, containers };
144
+ return { fence, context: { containers, paragraph: false, emptyItem: false } };
145
+ }
146
+ paragraph = !THEMATIC_BREAK.test(rest) && !/^#{1,6}(?:[ \t]|$)/.test(rest);
147
+ return { fence: null, context: { containers, paragraph, emptyItem: false } };
148
+ }
149
+
150
+ /** `ends`: a line that leaves one of the fence's containers ends it; fenced code never continues lazily. */
151
+ function fenceLine(line: string, fence: Fence): "content" | "close" | "ends" {
152
+ const { matched, at } = continueContainers(line, fence.containers);
153
+ if (matched.length < fence.containers.length) return "ends";
154
+ if (at.rest === "") return "content";
155
+ const match = at.column - at.base <= 3 ? FENCE_CLOSE.exec(at.rest) : null;
156
+ return match !== null && match[1]!.startsWith(fence.char) && match[1]!.length >= fence.length
157
+ ? "close"
158
+ : "content";
159
+ }
160
+
161
+ /** CommonMark: an ATX heading may be indented by up to three spaces, and be empty. */
162
+ const HEADING = /^ {0,3}##(?:[ \t]+(.*))?$/;
163
+ /** CommonMark: a closing run of `#`s, after a space or tab, is no part of the heading. */
164
+ const CLOSING_SEQUENCE = /(?:^|[ \t]+)#+[ \t]*$/;
165
+
166
+ function headingText(line: string): string | null {
167
+ const match = HEADING.exec(line);
168
+ if (!match) return null;
169
+ return (match[1] ?? "").replace(CLOSING_SEQUENCE, "").trim();
170
+ }
171
+
172
+ export interface ScannedLine {
173
+ text: string;
174
+ /** The heading text, `""` when empty, when the line opens a top-level (`##`) section: outside fenced code, list items and block quotes, whose scope it would otherwise leave. */
175
+ heading: string | null;
176
+ /** Inside fenced code, where blank lines are content. */
177
+ fenced: boolean;
178
+ /** Where the source line starts in the scanned text. */
179
+ offset: number;
180
+ }
181
+
182
+ /** CommonMark: an HTML block opens on a line that begins, after up to three spaces, with `<!--`. */
183
+ const BLOCK_COMMENT_OPEN = /^ {0,3}<!--/;
184
+ /** Claude Code's own pattern for the comments it removes from such a block. */
185
+ const CLOSED_COMMENT = /<!--[\s\S]*?-->/g;
186
+
187
+ type ScanState = { in: "prose" } | { in: "fence"; fence: Fence } | { in: "comment" };
188
+
189
+ const PROSE: ScanState = { in: "prose" };
190
+ const COMMENT: ScanState = { in: "comment" };
191
+
192
+ /**
193
+ * Strips block-level HTML comments as Claude Code's memory loading does: only
194
+ * an HTML block that opens with `<!--` and holds a `-->`, so a `<!--` inside
195
+ * prose, inside fenced code, or never closed stays. The rest of a closing line
196
+ * is still part of the HTML block, so it opens no heading and no fence.
197
+ */
198
+ export function scanLinesWithoutComments(text: string): ScannedLine[] {
199
+ const lines: ScannedLine[] = [];
200
+ let state = PROSE;
201
+ let list = TOP_LEVEL;
202
+ let offset = 0;
203
+
204
+ const emit = (line: string): void => {
205
+ const next = lineBlock(line, list);
206
+ const heading = next.context.containers.length === 0 ? headingText(line) : null;
207
+ lines.push({ text: line, heading, fenced: false, offset });
208
+ list = next.context;
209
+ state = next.fence ? { in: "fence", fence: next.fence } : PROSE;
210
+ };
211
+ /** A comment's lines are one HTML block: it ends the containers it leaves. */
212
+ const enterComment = (line: string): void => {
213
+ list = { ...lineBlock(line, list).context, paragraph: false };
214
+ };
215
+ const emitAfterClose = (line: string, close: number): void => {
216
+ const rest = line.slice(close + 3).replace(CLOSED_COMMENT, "");
217
+ lines.push({ text: rest, heading: null, fenced: false, offset });
218
+ state = PROSE;
219
+ };
220
+
221
+ for (const raw of text.split("\n")) {
222
+ const inFence = state.in === "fence" ? fenceLine(raw, state.fence) : "ends";
223
+ if (inFence !== "ends") {
224
+ lines.push({ text: raw, heading: null, fenced: true, offset });
225
+ if (inFence === "close") state = PROSE;
226
+ } else if (state.in === "comment") {
227
+ const close = raw.indexOf("-->");
228
+ if (close !== -1) emitAfterClose(raw, close);
229
+ } else if (BLOCK_COMMENT_OPEN.test(raw)) {
230
+ const close = text.indexOf("-->", offset + raw.indexOf("<!--") + 4);
231
+ if (close === -1) emit(raw);
232
+ else {
233
+ enterComment(raw);
234
+ if (close < offset + raw.length) emitAfterClose(raw, close - offset);
235
+ else state = COMMENT;
236
+ }
237
+ } else {
238
+ emit(raw);
239
+ }
240
+ offset += raw.length + 1;
241
+ }
242
+ return lines;
243
+ }
244
+
245
+ /** Offsets `[start, end)` of the lines inside fenced code. */
246
+ export function fencedRanges(text: string): [number, number][] {
247
+ return scanLinesWithoutComments(text)
248
+ .filter((line) => line.fenced)
249
+ .map((line) => [line.offset, line.offset + line.text.length]);
250
+ }
251
+
252
+ export function joinLines(lines: readonly ScannedLine[]): string {
253
+ const kept: string[] = [];
254
+ let afterBlank = false;
255
+ for (const line of lines) {
256
+ const blank = !line.fenced && BLANK.test(line.text);
257
+ if (!(blank && afterBlank)) kept.push(blank ? "" : line.text);
258
+ afterBlank = blank;
259
+ }
260
+ return trimBlankLines(kept.join("\n"));
261
+ }
262
+
263
+ /** Drops the blank lines around a text but no indentation: an indented fence opener keeps its meaning. */
264
+ export function trimBlankLines(text: string): string {
265
+ return text.replace(/^(?:[ \t]*\n)+/, "").trimEnd();
266
+ }