@productbrain/cli 0.1.0-beta.15 → 0.1.0-beta.1522

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 (948) hide show
  1. package/README.md +76 -115
  2. package/dist/__tests__/adapters.test.d.ts +2 -0
  3. package/dist/__tests__/adapters.test.d.ts.map +1 -0
  4. package/dist/__tests__/adapters.test.js +417 -0
  5. package/dist/__tests__/adapters.test.js.map +1 -0
  6. package/dist/__tests__/audit.test.js +65 -44
  7. package/dist/__tests__/audit.test.js.map +1 -1
  8. package/dist/__tests__/authority-domains-queue.test.d.ts +2 -0
  9. package/dist/__tests__/authority-domains-queue.test.d.ts.map +1 -0
  10. package/dist/__tests__/authority-domains-queue.test.js +169 -0
  11. package/dist/__tests__/authority-domains-queue.test.js.map +1 -0
  12. package/dist/__tests__/authority-domains.test.d.ts +2 -0
  13. package/dist/__tests__/authority-domains.test.d.ts.map +1 -0
  14. package/dist/__tests__/authority-domains.test.js +48 -0
  15. package/dist/__tests__/authority-domains.test.js.map +1 -0
  16. package/dist/__tests__/batch-transformations.test.d.ts +2 -0
  17. package/dist/__tests__/batch-transformations.test.d.ts.map +1 -0
  18. package/dist/__tests__/batch-transformations.test.js +263 -0
  19. package/dist/__tests__/batch-transformations.test.js.map +1 -0
  20. package/dist/__tests__/canonicalRefs.vocab.test.d.ts +2 -0
  21. package/dist/__tests__/canonicalRefs.vocab.test.d.ts.map +1 -0
  22. package/dist/__tests__/canonicalRefs.vocab.test.js +251 -0
  23. package/dist/__tests__/canonicalRefs.vocab.test.js.map +1 -0
  24. package/dist/__tests__/capture-receipt-domain.test.d.ts +2 -0
  25. package/dist/__tests__/capture-receipt-domain.test.d.ts.map +1 -0
  26. package/dist/__tests__/capture-receipt-domain.test.js +29 -0
  27. package/dist/__tests__/capture-receipt-domain.test.js.map +1 -0
  28. package/dist/__tests__/capture.test.js +309 -18
  29. package/dist/__tests__/capture.test.js.map +1 -1
  30. package/dist/__tests__/codex-prep-parity.test.d.ts +2 -0
  31. package/dist/__tests__/codex-prep-parity.test.d.ts.map +1 -0
  32. package/dist/__tests__/codex-prep-parity.test.js +107 -0
  33. package/dist/__tests__/codex-prep-parity.test.js.map +1 -0
  34. package/dist/__tests__/config-prod-fallthrough.test.d.ts +2 -0
  35. package/dist/__tests__/config-prod-fallthrough.test.d.ts.map +1 -0
  36. package/dist/__tests__/config-prod-fallthrough.test.js +82 -0
  37. package/dist/__tests__/config-prod-fallthrough.test.js.map +1 -0
  38. package/dist/__tests__/config.test.d.ts +8 -0
  39. package/dist/__tests__/config.test.d.ts.map +1 -0
  40. package/dist/__tests__/config.test.js +566 -0
  41. package/dist/__tests__/config.test.js.map +1 -0
  42. package/dist/__tests__/constants.test.d.ts +2 -0
  43. package/dist/__tests__/constants.test.d.ts.map +1 -0
  44. package/dist/__tests__/constants.test.js +146 -0
  45. package/dist/__tests__/constants.test.js.map +1 -0
  46. package/dist/__tests__/constellation.test.js +16 -22
  47. package/dist/__tests__/constellation.test.js.map +1 -1
  48. package/dist/__tests__/context-strategy.test.js +8 -8
  49. package/dist/__tests__/context-strategy.test.js.map +1 -1
  50. package/dist/__tests__/envelope-contract.test.d.ts +15 -0
  51. package/dist/__tests__/envelope-contract.test.d.ts.map +1 -0
  52. package/dist/__tests__/envelope-contract.test.js +152 -0
  53. package/dist/__tests__/envelope-contract.test.js.map +1 -0
  54. package/dist/__tests__/errors.test.d.ts +2 -0
  55. package/dist/__tests__/errors.test.d.ts.map +1 -0
  56. package/dist/__tests__/errors.test.js +118 -0
  57. package/dist/__tests__/errors.test.js.map +1 -0
  58. package/dist/__tests__/experiment.test.d.ts +6 -0
  59. package/dist/__tests__/experiment.test.d.ts.map +1 -0
  60. package/dist/__tests__/experiment.test.js +69 -0
  61. package/dist/__tests__/experiment.test.js.map +1 -0
  62. package/dist/__tests__/fields.test.js +36 -36
  63. package/dist/__tests__/fields.test.js.map +1 -1
  64. package/dist/__tests__/glossary.test.d.ts +2 -0
  65. package/dist/__tests__/glossary.test.d.ts.map +1 -0
  66. package/dist/__tests__/glossary.test.js +32 -0
  67. package/dist/__tests__/glossary.test.js.map +1 -0
  68. package/dist/__tests__/handshake-augment.test.d.ts +2 -0
  69. package/dist/__tests__/handshake-augment.test.d.ts.map +1 -0
  70. package/dist/__tests__/handshake-augment.test.js +423 -0
  71. package/dist/__tests__/handshake-augment.test.js.map +1 -0
  72. package/dist/__tests__/handshake-dormancy.test.d.ts +2 -0
  73. package/dist/__tests__/handshake-dormancy.test.d.ts.map +1 -0
  74. package/dist/__tests__/handshake-dormancy.test.js +207 -0
  75. package/dist/__tests__/handshake-dormancy.test.js.map +1 -0
  76. package/dist/__tests__/handshake-formatter.test.d.ts +2 -0
  77. package/dist/__tests__/handshake-formatter.test.d.ts.map +1 -0
  78. package/dist/__tests__/handshake-formatter.test.js +67 -0
  79. package/dist/__tests__/handshake-formatter.test.js.map +1 -0
  80. package/dist/__tests__/handshake-preview.test.d.ts +2 -0
  81. package/dist/__tests__/handshake-preview.test.d.ts.map +1 -0
  82. package/dist/__tests__/handshake-preview.test.js +949 -0
  83. package/dist/__tests__/handshake-preview.test.js.map +1 -0
  84. package/dist/__tests__/handshake.e2e.test.d.ts +2 -0
  85. package/dist/__tests__/handshake.e2e.test.d.ts.map +1 -0
  86. package/dist/__tests__/handshake.e2e.test.js +1252 -0
  87. package/dist/__tests__/handshake.e2e.test.js.map +1 -0
  88. package/dist/__tests__/handshake.test.js +813 -19
  89. package/dist/__tests__/handshake.test.js.map +1 -1
  90. package/dist/__tests__/hook-intents.test.d.ts +2 -0
  91. package/dist/__tests__/hook-intents.test.d.ts.map +1 -0
  92. package/dist/__tests__/hook-intents.test.js +184 -0
  93. package/dist/__tests__/hook-intents.test.js.map +1 -0
  94. package/dist/__tests__/ingest.test.js +28 -28
  95. package/dist/__tests__/ingest.test.js.map +1 -1
  96. package/dist/__tests__/init.test.d.ts +7 -0
  97. package/dist/__tests__/init.test.d.ts.map +1 -0
  98. package/dist/__tests__/init.test.js +146 -0
  99. package/dist/__tests__/init.test.js.map +1 -0
  100. package/dist/__tests__/login.test.d.ts +2 -0
  101. package/dist/__tests__/login.test.d.ts.map +1 -0
  102. package/dist/__tests__/login.test.js +167 -0
  103. package/dist/__tests__/login.test.js.map +1 -0
  104. package/dist/__tests__/manifest.test.d.ts +6 -0
  105. package/dist/__tests__/manifest.test.d.ts.map +1 -0
  106. package/dist/__tests__/manifest.test.js +255 -0
  107. package/dist/__tests__/manifest.test.js.map +1 -0
  108. package/dist/__tests__/method-publish-coherency.test.d.ts +9 -0
  109. package/dist/__tests__/method-publish-coherency.test.d.ts.map +1 -0
  110. package/dist/__tests__/method-publish-coherency.test.js +71 -0
  111. package/dist/__tests__/method-publish-coherency.test.js.map +1 -0
  112. package/dist/__tests__/method-registry.integration.test.d.ts +6 -0
  113. package/dist/__tests__/method-registry.integration.test.d.ts.map +1 -0
  114. package/dist/__tests__/method-registry.integration.test.js +18 -0
  115. package/dist/__tests__/method-registry.integration.test.js.map +1 -0
  116. package/dist/__tests__/method-registry.test.d.ts +14 -0
  117. package/dist/__tests__/method-registry.test.d.ts.map +1 -0
  118. package/dist/__tests__/method-registry.test.js +134 -0
  119. package/dist/__tests__/method-registry.test.js.map +1 -0
  120. package/dist/__tests__/notice-marker.test.d.ts +2 -0
  121. package/dist/__tests__/notice-marker.test.d.ts.map +1 -0
  122. package/dist/__tests__/notice-marker.test.js +41 -0
  123. package/dist/__tests__/notice-marker.test.js.map +1 -0
  124. package/dist/__tests__/onboarding-path-b.test.d.ts +2 -0
  125. package/dist/__tests__/onboarding-path-b.test.d.ts.map +1 -0
  126. package/dist/__tests__/onboarding-path-b.test.js +46 -0
  127. package/dist/__tests__/onboarding-path-b.test.js.map +1 -0
  128. package/dist/__tests__/onboarding.test.d.ts +6 -0
  129. package/dist/__tests__/onboarding.test.d.ts.map +1 -0
  130. package/dist/__tests__/onboarding.test.js +347 -0
  131. package/dist/__tests__/onboarding.test.js.map +1 -0
  132. package/dist/__tests__/orient.test.d.ts +2 -0
  133. package/dist/__tests__/orient.test.d.ts.map +1 -0
  134. package/dist/__tests__/orient.test.js +424 -0
  135. package/dist/__tests__/orient.test.js.map +1 -0
  136. package/dist/__tests__/perimeter.test.d.ts +2 -0
  137. package/dist/__tests__/perimeter.test.d.ts.map +1 -0
  138. package/dist/__tests__/perimeter.test.js +165 -0
  139. package/dist/__tests__/perimeter.test.js.map +1 -0
  140. package/dist/__tests__/personal-layer.test.d.ts +11 -0
  141. package/dist/__tests__/personal-layer.test.d.ts.map +1 -0
  142. package/dist/__tests__/personal-layer.test.js +268 -0
  143. package/dist/__tests__/personal-layer.test.js.map +1 -0
  144. package/dist/__tests__/preview-key-refresh.test.d.ts +2 -0
  145. package/dist/__tests__/preview-key-refresh.test.d.ts.map +1 -0
  146. package/dist/__tests__/preview-key-refresh.test.js +126 -0
  147. package/dist/__tests__/preview-key-refresh.test.js.map +1 -0
  148. package/dist/__tests__/profiles.test.d.ts +2 -0
  149. package/dist/__tests__/profiles.test.d.ts.map +1 -0
  150. package/dist/__tests__/profiles.test.js +316 -0
  151. package/dist/__tests__/profiles.test.js.map +1 -0
  152. package/dist/__tests__/promote-batch.test.d.ts +2 -0
  153. package/dist/__tests__/promote-batch.test.d.ts.map +1 -0
  154. package/dist/__tests__/promote-batch.test.js +185 -0
  155. package/dist/__tests__/promote-batch.test.js.map +1 -0
  156. package/dist/__tests__/promote.test.js +213 -34
  157. package/dist/__tests__/promote.test.js.map +1 -1
  158. package/dist/__tests__/prompts.test.d.ts +6 -0
  159. package/dist/__tests__/prompts.test.d.ts.map +1 -0
  160. package/dist/__tests__/prompts.test.js +146 -0
  161. package/dist/__tests__/prompts.test.js.map +1 -0
  162. package/dist/__tests__/proof-run.test.d.ts +2 -0
  163. package/dist/__tests__/proof-run.test.d.ts.map +1 -0
  164. package/dist/__tests__/proof-run.test.js +255 -0
  165. package/dist/__tests__/proof-run.test.js.map +1 -0
  166. package/dist/__tests__/proposals.test.js +24 -47
  167. package/dist/__tests__/proposals.test.js.map +1 -1
  168. package/dist/__tests__/relate.test.js +34 -36
  169. package/dist/__tests__/relate.test.js.map +1 -1
  170. package/dist/__tests__/repo-detect.test.js +97 -1
  171. package/dist/__tests__/repo-detect.test.js.map +1 -1
  172. package/dist/__tests__/runner.test.js +19 -15
  173. package/dist/__tests__/runner.test.js.map +1 -1
  174. package/dist/__tests__/session-close-loop.test.d.ts +2 -0
  175. package/dist/__tests__/session-close-loop.test.d.ts.map +1 -0
  176. package/dist/__tests__/session-close-loop.test.js +231 -0
  177. package/dist/__tests__/session-close-loop.test.js.map +1 -0
  178. package/dist/__tests__/session-reset.test.d.ts +2 -0
  179. package/dist/__tests__/session-reset.test.d.ts.map +1 -0
  180. package/dist/__tests__/session-reset.test.js +122 -0
  181. package/dist/__tests__/session-reset.test.js.map +1 -0
  182. package/dist/__tests__/session-resume-backstop.test.d.ts +2 -0
  183. package/dist/__tests__/session-resume-backstop.test.d.ts.map +1 -0
  184. package/dist/__tests__/session-resume-backstop.test.js +97 -0
  185. package/dist/__tests__/session-resume-backstop.test.js.map +1 -0
  186. package/dist/__tests__/session-start-key-refresh.test.d.ts +2 -0
  187. package/dist/__tests__/session-start-key-refresh.test.d.ts.map +1 -0
  188. package/dist/__tests__/session-start-key-refresh.test.js +179 -0
  189. package/dist/__tests__/session-start-key-refresh.test.js.map +1 -0
  190. package/dist/__tests__/session-state-machine.test.d.ts +2 -0
  191. package/dist/__tests__/session-state-machine.test.d.ts.map +1 -0
  192. package/dist/__tests__/session-state-machine.test.js +198 -0
  193. package/dist/__tests__/session-state-machine.test.js.map +1 -0
  194. package/dist/__tests__/session-switch.test.d.ts +2 -0
  195. package/dist/__tests__/session-switch.test.d.ts.map +1 -0
  196. package/dist/__tests__/session-switch.test.js +130 -0
  197. package/dist/__tests__/session-switch.test.js.map +1 -0
  198. package/dist/__tests__/session-touch.test.js +11 -11
  199. package/dist/__tests__/session-touch.test.js.map +1 -1
  200. package/dist/__tests__/session.test.js +4 -10
  201. package/dist/__tests__/session.test.js.map +1 -1
  202. package/dist/__tests__/setup-ingest.test.d.ts +2 -0
  203. package/dist/__tests__/setup-ingest.test.d.ts.map +1 -0
  204. package/dist/__tests__/setup-ingest.test.js +71 -0
  205. package/dist/__tests__/setup-ingest.test.js.map +1 -0
  206. package/dist/__tests__/setup-resolver.test.d.ts +14 -0
  207. package/dist/__tests__/setup-resolver.test.d.ts.map +1 -0
  208. package/dist/__tests__/setup-resolver.test.js +228 -0
  209. package/dist/__tests__/setup-resolver.test.js.map +1 -0
  210. package/dist/__tests__/skill-vocabulary.test.d.ts +21 -0
  211. package/dist/__tests__/skill-vocabulary.test.d.ts.map +1 -0
  212. package/dist/__tests__/skill-vocabulary.test.js +192 -0
  213. package/dist/__tests__/skill-vocabulary.test.js.map +1 -0
  214. package/dist/__tests__/spinner-labels.test.d.ts +2 -0
  215. package/dist/__tests__/spinner-labels.test.d.ts.map +1 -0
  216. package/dist/__tests__/spinner-labels.test.js +23 -0
  217. package/dist/__tests__/spinner-labels.test.js.map +1 -0
  218. package/dist/__tests__/state.test.d.ts +6 -0
  219. package/dist/__tests__/state.test.d.ts.map +1 -0
  220. package/dist/__tests__/state.test.js +97 -0
  221. package/dist/__tests__/state.test.js.map +1 -0
  222. package/dist/__tests__/surface-profiles.test.d.ts +2 -0
  223. package/dist/__tests__/surface-profiles.test.d.ts.map +1 -0
  224. package/dist/__tests__/surface-profiles.test.js +233 -0
  225. package/dist/__tests__/surface-profiles.test.js.map +1 -0
  226. package/dist/__tests__/surfaces.test.d.ts +2 -0
  227. package/dist/__tests__/surfaces.test.d.ts.map +1 -0
  228. package/dist/__tests__/surfaces.test.js +46 -0
  229. package/dist/__tests__/surfaces.test.js.map +1 -0
  230. package/dist/__tests__/update-check.test.d.ts +2 -0
  231. package/dist/__tests__/update-check.test.d.ts.map +1 -0
  232. package/dist/__tests__/update-check.test.js +215 -0
  233. package/dist/__tests__/update-check.test.js.map +1 -0
  234. package/dist/__tests__/update.test.js +63 -72
  235. package/dist/__tests__/update.test.js.map +1 -1
  236. package/dist/__tests__/upgrade-runner.test.d.ts +2 -0
  237. package/dist/__tests__/upgrade-runner.test.d.ts.map +1 -0
  238. package/dist/__tests__/upgrade-runner.test.js +54 -0
  239. package/dist/__tests__/upgrade-runner.test.js.map +1 -0
  240. package/dist/__tests__/upgrade.test.d.ts +2 -0
  241. package/dist/__tests__/upgrade.test.d.ts.map +1 -0
  242. package/dist/__tests__/upgrade.test.js +56 -0
  243. package/dist/__tests__/upgrade.test.js.map +1 -0
  244. package/dist/__tests__/vocabulary-leak.test.d.ts +39 -0
  245. package/dist/__tests__/vocabulary-leak.test.d.ts.map +1 -0
  246. package/dist/__tests__/vocabulary-leak.test.js +534 -0
  247. package/dist/__tests__/vocabulary-leak.test.js.map +1 -0
  248. package/dist/__tests__/workspace.test.d.ts +2 -0
  249. package/dist/__tests__/workspace.test.d.ts.map +1 -0
  250. package/dist/__tests__/workspace.test.js +328 -0
  251. package/dist/__tests__/workspace.test.js.map +1 -0
  252. package/dist/commands/__tests__/connect-handoff.test.d.ts +11 -0
  253. package/dist/commands/__tests__/connect-handoff.test.d.ts.map +1 -0
  254. package/dist/commands/__tests__/connect-handoff.test.js +111 -0
  255. package/dist/commands/__tests__/connect-handoff.test.js.map +1 -0
  256. package/dist/commands/__tests__/setup-detect-surfaces.test.d.ts +15 -0
  257. package/dist/commands/__tests__/setup-detect-surfaces.test.d.ts.map +1 -0
  258. package/dist/commands/__tests__/setup-detect-surfaces.test.js +149 -0
  259. package/dist/commands/__tests__/setup-detect-surfaces.test.js.map +1 -0
  260. package/dist/commands/__tests__/setup-state.test.d.ts +2 -0
  261. package/dist/commands/__tests__/setup-state.test.d.ts.map +1 -0
  262. package/dist/commands/__tests__/setup-state.test.js +194 -0
  263. package/dist/commands/__tests__/setup-state.test.js.map +1 -0
  264. package/dist/commands/accept.d.ts.map +1 -1
  265. package/dist/commands/accept.js +10 -6
  266. package/dist/commands/accept.js.map +1 -1
  267. package/dist/commands/admin/cockpit.d.ts +90 -0
  268. package/dist/commands/admin/cockpit.d.ts.map +1 -0
  269. package/dist/commands/admin/cockpit.js +618 -0
  270. package/dist/commands/admin/cockpit.js.map +1 -0
  271. package/dist/commands/admin/index.d.ts +21 -0
  272. package/dist/commands/admin/index.d.ts.map +1 -0
  273. package/dist/commands/admin/index.js +256 -0
  274. package/dist/commands/admin/index.js.map +1 -0
  275. package/dist/commands/admin/inspect.d.ts +30 -0
  276. package/dist/commands/admin/inspect.d.ts.map +1 -0
  277. package/dist/commands/admin/inspect.js +555 -0
  278. package/dist/commands/admin/inspect.js.map +1 -0
  279. package/dist/commands/admin/inspect.test.d.ts +7 -0
  280. package/dist/commands/admin/inspect.test.d.ts.map +1 -0
  281. package/dist/commands/admin/inspect.test.js +90 -0
  282. package/dist/commands/admin/inspect.test.js.map +1 -0
  283. package/dist/commands/admin/manage.d.ts +8 -0
  284. package/dist/commands/admin/manage.d.ts.map +1 -0
  285. package/dist/commands/admin/manage.js +260 -0
  286. package/dist/commands/admin/manage.js.map +1 -0
  287. package/dist/commands/admin/manage.test.d.ts +16 -0
  288. package/dist/commands/admin/manage.test.d.ts.map +1 -0
  289. package/dist/commands/admin/manage.test.js +159 -0
  290. package/dist/commands/admin/manage.test.js.map +1 -0
  291. package/dist/commands/admin/seed.d.ts +75 -0
  292. package/dist/commands/admin/seed.d.ts.map +1 -0
  293. package/dist/commands/admin/seed.js +944 -0
  294. package/dist/commands/admin/seed.js.map +1 -0
  295. package/dist/commands/admin/seed.test.d.ts +11 -0
  296. package/dist/commands/admin/seed.test.d.ts.map +1 -0
  297. package/dist/commands/admin/seed.test.js +127 -0
  298. package/dist/commands/admin/seed.test.js.map +1 -0
  299. package/dist/commands/admin/seedRegistryEntries.generated.d.ts +14 -0
  300. package/dist/commands/admin/seedRegistryEntries.generated.d.ts.map +1 -0
  301. package/dist/commands/admin/seedRegistryEntries.generated.js +144 -0
  302. package/dist/commands/admin/seedRegistryEntries.generated.js.map +1 -0
  303. package/dist/commands/admin/seedRegistryEntries.test.d.ts +11 -0
  304. package/dist/commands/admin/seedRegistryEntries.test.d.ts.map +1 -0
  305. package/dist/commands/admin/seedRegistryEntries.test.js +70 -0
  306. package/dist/commands/admin/seedRegistryEntries.test.js.map +1 -0
  307. package/dist/commands/audit.d.ts.map +1 -1
  308. package/dist/commands/audit.js +42 -7
  309. package/dist/commands/audit.js.map +1 -1
  310. package/dist/commands/authority-domains.d.ts +157 -0
  311. package/dist/commands/authority-domains.d.ts.map +1 -0
  312. package/dist/commands/authority-domains.js +356 -0
  313. package/dist/commands/authority-domains.js.map +1 -0
  314. package/dist/commands/brief.d.ts.map +1 -1
  315. package/dist/commands/brief.js +10 -5
  316. package/dist/commands/brief.js.map +1 -1
  317. package/dist/commands/capture.d.ts +7 -0
  318. package/dist/commands/capture.d.ts.map +1 -1
  319. package/dist/commands/capture.js +266 -19
  320. package/dist/commands/capture.js.map +1 -1
  321. package/dist/commands/chain-walk.d.ts.map +1 -1
  322. package/dist/commands/chain-walk.js +8 -3
  323. package/dist/commands/chain-walk.js.map +1 -1
  324. package/dist/commands/changes.d.ts.map +1 -1
  325. package/dist/commands/changes.js +8 -3
  326. package/dist/commands/changes.js.map +1 -1
  327. package/dist/commands/codex-prep.d.ts +23 -0
  328. package/dist/commands/codex-prep.d.ts.map +1 -0
  329. package/dist/commands/codex-prep.js +56 -0
  330. package/dist/commands/codex-prep.js.map +1 -0
  331. package/dist/commands/collections.d.ts +22 -0
  332. package/dist/commands/collections.d.ts.map +1 -0
  333. package/dist/commands/collections.js +77 -0
  334. package/dist/commands/collections.js.map +1 -0
  335. package/dist/commands/connect-config.test.d.ts +2 -0
  336. package/dist/commands/connect-config.test.d.ts.map +1 -0
  337. package/dist/commands/connect-config.test.js +44 -0
  338. package/dist/commands/connect-config.test.js.map +1 -0
  339. package/dist/commands/connect-context.d.ts +45 -0
  340. package/dist/commands/connect-context.d.ts.map +1 -0
  341. package/dist/commands/connect-context.js +64 -0
  342. package/dist/commands/connect-context.js.map +1 -0
  343. package/dist/commands/connect-context.test.d.ts +2 -0
  344. package/dist/commands/connect-context.test.d.ts.map +1 -0
  345. package/dist/commands/connect-context.test.js +110 -0
  346. package/dist/commands/connect-context.test.js.map +1 -0
  347. package/dist/commands/connect-handoff.d.ts +51 -0
  348. package/dist/commands/connect-handoff.d.ts.map +1 -0
  349. package/dist/commands/connect-handoff.js +70 -0
  350. package/dist/commands/connect-handoff.js.map +1 -0
  351. package/dist/commands/connect-integration.test.d.ts +7 -0
  352. package/dist/commands/connect-integration.test.d.ts.map +1 -0
  353. package/dist/commands/connect-integration.test.js +212 -0
  354. package/dist/commands/connect-integration.test.js.map +1 -0
  355. package/dist/commands/connect-screens.d.ts +23 -0
  356. package/dist/commands/connect-screens.d.ts.map +1 -0
  357. package/dist/commands/connect-screens.js +90 -0
  358. package/dist/commands/connect-screens.js.map +1 -0
  359. package/dist/commands/connect.d.ts +36 -0
  360. package/dist/commands/connect.d.ts.map +1 -0
  361. package/dist/commands/connect.js +264 -0
  362. package/dist/commands/connect.js.map +1 -0
  363. package/dist/commands/connect.test.d.ts +6 -0
  364. package/dist/commands/connect.test.d.ts.map +1 -0
  365. package/dist/commands/connect.test.js +297 -0
  366. package/dist/commands/connect.test.js.map +1 -0
  367. package/dist/commands/constellation.d.ts.map +1 -1
  368. package/dist/commands/constellation.js +8 -3
  369. package/dist/commands/constellation.js.map +1 -1
  370. package/dist/commands/context.d.ts.map +1 -1
  371. package/dist/commands/context.js +8 -3
  372. package/dist/commands/context.js.map +1 -1
  373. package/dist/commands/cross-cut.js +2 -2
  374. package/dist/commands/cross-cut.js.map +1 -1
  375. package/dist/commands/doctor.d.ts +18 -0
  376. package/dist/commands/doctor.d.ts.map +1 -0
  377. package/dist/commands/doctor.js +297 -0
  378. package/dist/commands/doctor.js.map +1 -0
  379. package/dist/commands/doctor.test.d.ts +8 -0
  380. package/dist/commands/doctor.test.d.ts.map +1 -0
  381. package/dist/commands/doctor.test.js +461 -0
  382. package/dist/commands/doctor.test.js.map +1 -0
  383. package/dist/commands/fields.d.ts.map +1 -1
  384. package/dist/commands/fields.js +8 -4
  385. package/dist/commands/fields.js.map +1 -1
  386. package/dist/commands/get.d.ts.map +1 -1
  387. package/dist/commands/get.js +14 -6
  388. package/dist/commands/get.js.map +1 -1
  389. package/dist/commands/handshake.d.ts +233 -2
  390. package/dist/commands/handshake.d.ts.map +1 -1
  391. package/dist/commands/handshake.js +2338 -81
  392. package/dist/commands/handshake.js.map +1 -1
  393. package/dist/commands/ingest.d.ts.map +1 -1
  394. package/dist/commands/ingest.js +17 -9
  395. package/dist/commands/ingest.js.map +1 -1
  396. package/dist/commands/init.d.ts +14 -0
  397. package/dist/commands/init.d.ts.map +1 -0
  398. package/dist/commands/init.js +109 -0
  399. package/dist/commands/init.js.map +1 -0
  400. package/dist/commands/login.d.ts +4 -0
  401. package/dist/commands/login.d.ts.map +1 -1
  402. package/dist/commands/login.js +101 -38
  403. package/dist/commands/login.js.map +1 -1
  404. package/dist/commands/method.d.ts +99 -0
  405. package/dist/commands/method.d.ts.map +1 -0
  406. package/dist/commands/method.js +796 -0
  407. package/dist/commands/method.js.map +1 -0
  408. package/dist/commands/migrate-setup.d.ts +18 -0
  409. package/dist/commands/migrate-setup.d.ts.map +1 -0
  410. package/dist/commands/migrate-setup.js +148 -0
  411. package/dist/commands/migrate-setup.js.map +1 -0
  412. package/dist/commands/orient.d.ts +197 -3
  413. package/dist/commands/orient.d.ts.map +1 -1
  414. package/dist/commands/orient.js +129 -7
  415. package/dist/commands/orient.js.map +1 -1
  416. package/dist/commands/profile.d.ts +34 -0
  417. package/dist/commands/profile.d.ts.map +1 -0
  418. package/dist/commands/profile.js +165 -0
  419. package/dist/commands/profile.js.map +1 -0
  420. package/dist/commands/promote.d.ts +12 -1
  421. package/dist/commands/promote.d.ts.map +1 -1
  422. package/dist/commands/promote.js +197 -30
  423. package/dist/commands/promote.js.map +1 -1
  424. package/dist/commands/proof-run.d.ts +51 -0
  425. package/dist/commands/proof-run.d.ts.map +1 -0
  426. package/dist/commands/proof-run.js +209 -0
  427. package/dist/commands/proof-run.js.map +1 -0
  428. package/dist/commands/proposals.js +2 -2
  429. package/dist/commands/proposals.js.map +1 -1
  430. package/dist/commands/reject.d.ts.map +1 -1
  431. package/dist/commands/reject.js +14 -7
  432. package/dist/commands/reject.js.map +1 -1
  433. package/dist/commands/relate.d.ts.map +1 -1
  434. package/dist/commands/relate.js +49 -16
  435. package/dist/commands/relate.js.map +1 -1
  436. package/dist/commands/scoreboard.d.ts +28 -0
  437. package/dist/commands/scoreboard.d.ts.map +1 -0
  438. package/dist/commands/scoreboard.js +40 -0
  439. package/dist/commands/scoreboard.js.map +1 -0
  440. package/dist/commands/search.js +2 -2
  441. package/dist/commands/search.js.map +1 -1
  442. package/dist/commands/session.d.ts +26 -2
  443. package/dist/commands/session.d.ts.map +1 -1
  444. package/dist/commands/session.js +355 -41
  445. package/dist/commands/session.js.map +1 -1
  446. package/dist/commands/setup-audit.d.ts +59 -0
  447. package/dist/commands/setup-audit.d.ts.map +1 -0
  448. package/dist/commands/setup-audit.js +250 -0
  449. package/dist/commands/setup-audit.js.map +1 -0
  450. package/dist/commands/setup-detect-surfaces.d.ts +38 -0
  451. package/dist/commands/setup-detect-surfaces.d.ts.map +1 -0
  452. package/dist/commands/setup-detect-surfaces.js +76 -0
  453. package/dist/commands/setup-detect-surfaces.js.map +1 -0
  454. package/dist/commands/setup-ingest.d.ts +17 -0
  455. package/dist/commands/setup-ingest.d.ts.map +1 -0
  456. package/dist/commands/setup-ingest.js +174 -0
  457. package/dist/commands/setup-ingest.js.map +1 -0
  458. package/dist/commands/setup-resolver.d.ts +58 -0
  459. package/dist/commands/setup-resolver.d.ts.map +1 -0
  460. package/dist/commands/setup-resolver.js +150 -0
  461. package/dist/commands/setup-resolver.js.map +1 -0
  462. package/dist/commands/setup-state.d.ts +42 -0
  463. package/dist/commands/setup-state.d.ts.map +1 -0
  464. package/dist/commands/setup-state.js +93 -0
  465. package/dist/commands/setup-state.js.map +1 -0
  466. package/dist/commands/setup.d.ts +23 -0
  467. package/dist/commands/setup.d.ts.map +1 -0
  468. package/dist/commands/setup.js +69 -0
  469. package/dist/commands/setup.js.map +1 -0
  470. package/dist/commands/update.d.ts +9 -0
  471. package/dist/commands/update.d.ts.map +1 -1
  472. package/dist/commands/update.js +92 -26
  473. package/dist/commands/update.js.map +1 -1
  474. package/dist/commands/upgrade.d.ts +5 -0
  475. package/dist/commands/upgrade.d.ts.map +1 -0
  476. package/dist/commands/upgrade.js +110 -0
  477. package/dist/commands/upgrade.js.map +1 -0
  478. package/dist/commands/usage.d.ts +40 -0
  479. package/dist/commands/usage.d.ts.map +1 -0
  480. package/dist/commands/usage.js +232 -0
  481. package/dist/commands/usage.js.map +1 -0
  482. package/dist/commands/verify.d.ts.map +1 -1
  483. package/dist/commands/verify.js +14 -7
  484. package/dist/commands/verify.js.map +1 -1
  485. package/dist/commands/welcome.d.ts +21 -0
  486. package/dist/commands/welcome.d.ts.map +1 -0
  487. package/dist/commands/welcome.js +50 -0
  488. package/dist/commands/welcome.js.map +1 -0
  489. package/dist/commands/whoami.d.ts +12 -0
  490. package/dist/commands/whoami.d.ts.map +1 -0
  491. package/dist/commands/whoami.js +70 -0
  492. package/dist/commands/whoami.js.map +1 -0
  493. package/dist/commands/whoami.test.d.ts +2 -0
  494. package/dist/commands/whoami.test.d.ts.map +1 -0
  495. package/dist/commands/whoami.test.js +50 -0
  496. package/dist/commands/whoami.test.js.map +1 -0
  497. package/dist/commands/workspace.d.ts +113 -0
  498. package/dist/commands/workspace.d.ts.map +1 -0
  499. package/dist/commands/workspace.js +263 -0
  500. package/dist/commands/workspace.js.map +1 -0
  501. package/dist/formatters/__tests__/orient-provenance.test.d.ts +7 -0
  502. package/dist/formatters/__tests__/orient-provenance.test.d.ts.map +1 -0
  503. package/dist/formatters/__tests__/orient-provenance.test.js +454 -0
  504. package/dist/formatters/__tests__/orient-provenance.test.js.map +1 -0
  505. package/dist/formatters/__tests__/session.test.d.ts +2 -0
  506. package/dist/formatters/__tests__/session.test.d.ts.map +1 -0
  507. package/dist/formatters/__tests__/session.test.js +113 -0
  508. package/dist/formatters/__tests__/session.test.js.map +1 -0
  509. package/dist/formatters/audit.d.ts +6 -0
  510. package/dist/formatters/audit.d.ts.map +1 -1
  511. package/dist/formatters/audit.js.map +1 -1
  512. package/dist/formatters/capture.d.ts +26 -3
  513. package/dist/formatters/capture.d.ts.map +1 -1
  514. package/dist/formatters/capture.js +25 -1
  515. package/dist/formatters/capture.js.map +1 -1
  516. package/dist/formatters/collections.d.ts +40 -0
  517. package/dist/formatters/collections.d.ts.map +1 -0
  518. package/dist/formatters/collections.js +93 -0
  519. package/dist/formatters/collections.js.map +1 -0
  520. package/dist/formatters/entry.d.ts +30 -4
  521. package/dist/formatters/entry.d.ts.map +1 -1
  522. package/dist/formatters/entry.js +66 -17
  523. package/dist/formatters/entry.js.map +1 -1
  524. package/dist/formatters/handshake.d.ts +45 -0
  525. package/dist/formatters/handshake.d.ts.map +1 -1
  526. package/dist/formatters/handshake.js +150 -3
  527. package/dist/formatters/handshake.js.map +1 -1
  528. package/dist/formatters/orient.d.ts +268 -5
  529. package/dist/formatters/orient.d.ts.map +1 -1
  530. package/dist/formatters/orient.js +381 -34
  531. package/dist/formatters/orient.js.map +1 -1
  532. package/dist/formatters/promote.d.ts +6 -0
  533. package/dist/formatters/promote.d.ts.map +1 -1
  534. package/dist/formatters/promote.js +8 -0
  535. package/dist/formatters/promote.js.map +1 -1
  536. package/dist/formatters/relate.d.ts +2 -0
  537. package/dist/formatters/relate.d.ts.map +1 -1
  538. package/dist/formatters/relate.js +3 -0
  539. package/dist/formatters/relate.js.map +1 -1
  540. package/dist/formatters/scoreboard.d.ts +11 -0
  541. package/dist/formatters/scoreboard.d.ts.map +1 -0
  542. package/dist/formatters/scoreboard.js +48 -0
  543. package/dist/formatters/scoreboard.js.map +1 -0
  544. package/dist/formatters/search.d.ts +0 -4
  545. package/dist/formatters/search.d.ts.map +1 -1
  546. package/dist/formatters/search.js +4 -1
  547. package/dist/formatters/search.js.map +1 -1
  548. package/dist/formatters/session.d.ts +39 -2
  549. package/dist/formatters/session.d.ts.map +1 -1
  550. package/dist/formatters/session.js +130 -9
  551. package/dist/formatters/session.js.map +1 -1
  552. package/dist/formatters/update.d.ts.map +1 -1
  553. package/dist/formatters/update.js +2 -0
  554. package/dist/formatters/update.js.map +1 -1
  555. package/dist/generators/adapters.d.ts +37 -3
  556. package/dist/generators/adapters.d.ts.map +1 -1
  557. package/dist/generators/adapters.js +196 -8
  558. package/dist/generators/adapters.js.map +1 -1
  559. package/dist/generators/adapters.test.d.ts +2 -0
  560. package/dist/generators/adapters.test.d.ts.map +1 -0
  561. package/dist/generators/adapters.test.js +27 -0
  562. package/dist/generators/adapters.test.js.map +1 -0
  563. package/dist/generators/archetypes.d.ts +52 -0
  564. package/dist/generators/archetypes.d.ts.map +1 -0
  565. package/dist/generators/archetypes.js +190 -0
  566. package/dist/generators/archetypes.js.map +1 -0
  567. package/dist/generators/archetypes.test.d.ts +2 -0
  568. package/dist/generators/archetypes.test.d.ts.map +1 -0
  569. package/dist/generators/archetypes.test.js +272 -0
  570. package/dist/generators/archetypes.test.js.map +1 -0
  571. package/dist/generators/boundary-manifest.d.ts +29 -0
  572. package/dist/generators/boundary-manifest.d.ts.map +1 -0
  573. package/dist/generators/boundary-manifest.js +183 -0
  574. package/dist/generators/boundary-manifest.js.map +1 -0
  575. package/dist/generators/boundary-manifest.test.d.ts +2 -0
  576. package/dist/generators/boundary-manifest.test.d.ts.map +1 -0
  577. package/dist/generators/boundary-manifest.test.js +91 -0
  578. package/dist/generators/boundary-manifest.test.js.map +1 -0
  579. package/dist/generators/chain-classifier.d.ts +63 -0
  580. package/dist/generators/chain-classifier.d.ts.map +1 -0
  581. package/dist/generators/chain-classifier.js +131 -0
  582. package/dist/generators/chain-classifier.js.map +1 -0
  583. package/dist/generators/chain-classifier.test.d.ts +2 -0
  584. package/dist/generators/chain-classifier.test.d.ts.map +1 -0
  585. package/dist/generators/chain-classifier.test.js +371 -0
  586. package/dist/generators/chain-classifier.test.js.map +1 -0
  587. package/dist/generators/chain-rules.d.ts +42 -0
  588. package/dist/generators/chain-rules.d.ts.map +1 -0
  589. package/dist/generators/chain-rules.js +152 -0
  590. package/dist/generators/chain-rules.js.map +1 -0
  591. package/dist/generators/chain-rules.test.d.ts +2 -0
  592. package/dist/generators/chain-rules.test.d.ts.map +1 -0
  593. package/dist/generators/chain-rules.test.js +211 -0
  594. package/dist/generators/chain-rules.test.js.map +1 -0
  595. package/dist/generators/context-md.d.ts +1 -1
  596. package/dist/generators/context-md.d.ts.map +1 -1
  597. package/dist/generators/context-md.js +18 -7
  598. package/dist/generators/context-md.js.map +1 -1
  599. package/dist/generators/handshake-diff.d.ts +67 -0
  600. package/dist/generators/handshake-diff.d.ts.map +1 -0
  601. package/dist/generators/handshake-diff.js +183 -0
  602. package/dist/generators/handshake-diff.js.map +1 -0
  603. package/dist/generators/handshake-diff.test.d.ts +2 -0
  604. package/dist/generators/handshake-diff.test.d.ts.map +1 -0
  605. package/dist/generators/handshake-diff.test.js +264 -0
  606. package/dist/generators/handshake-diff.test.js.map +1 -0
  607. package/dist/generators/manifest.d.ts +115 -0
  608. package/dist/generators/manifest.d.ts.map +1 -0
  609. package/dist/generators/manifest.js +277 -0
  610. package/dist/generators/manifest.js.map +1 -0
  611. package/dist/generators/portable-knowledge.d.ts +96 -9
  612. package/dist/generators/portable-knowledge.d.ts.map +1 -1
  613. package/dist/generators/portable-knowledge.js +367 -17
  614. package/dist/generators/portable-knowledge.js.map +1 -1
  615. package/dist/generators/portable-knowledge.test.js +529 -1
  616. package/dist/generators/portable-knowledge.test.js.map +1 -1
  617. package/dist/generators/region-projections.d.ts +18 -0
  618. package/dist/generators/region-projections.d.ts.map +1 -0
  619. package/dist/generators/region-projections.js +49 -0
  620. package/dist/generators/region-projections.js.map +1 -0
  621. package/dist/generators/region-projections.test.d.ts +2 -0
  622. package/dist/generators/region-projections.test.d.ts.map +1 -0
  623. package/dist/generators/region-projections.test.js +63 -0
  624. package/dist/generators/region-projections.test.js.map +1 -0
  625. package/dist/generators/region.d.ts +24 -0
  626. package/dist/generators/region.d.ts.map +1 -0
  627. package/dist/generators/region.js +87 -0
  628. package/dist/generators/region.js.map +1 -0
  629. package/dist/generators/region.test.d.ts +2 -0
  630. package/dist/generators/region.test.d.ts.map +1 -0
  631. package/dist/generators/region.test.js +126 -0
  632. package/dist/generators/region.test.js.map +1 -0
  633. package/dist/generators/surface-profiles.d.ts +48 -0
  634. package/dist/generators/surface-profiles.d.ts.map +1 -0
  635. package/dist/generators/surface-profiles.js +98 -0
  636. package/dist/generators/surface-profiles.js.map +1 -0
  637. package/dist/index.js +800 -252
  638. package/dist/index.js.map +1 -1
  639. package/dist/lib/activation.d.ts +28 -0
  640. package/dist/lib/activation.d.ts.map +1 -0
  641. package/dist/lib/activation.js +57 -0
  642. package/dist/lib/activation.js.map +1 -0
  643. package/dist/lib/activation.test.d.ts +6 -0
  644. package/dist/lib/activation.test.d.ts.map +1 -0
  645. package/dist/lib/activation.test.js +121 -0
  646. package/dist/lib/activation.test.js.map +1 -0
  647. package/dist/lib/canonicalRefs.d.ts +141 -0
  648. package/dist/lib/canonicalRefs.d.ts.map +1 -0
  649. package/dist/lib/canonicalRefs.js +150 -0
  650. package/dist/lib/canonicalRefs.js.map +1 -0
  651. package/dist/lib/client.d.ts +33 -6
  652. package/dist/lib/client.d.ts.map +1 -1
  653. package/dist/lib/client.js +203 -42
  654. package/dist/lib/client.js.map +1 -1
  655. package/dist/lib/collectionRegistry.d.ts +40 -0
  656. package/dist/lib/collectionRegistry.d.ts.map +1 -0
  657. package/dist/lib/collectionRegistry.js +112 -0
  658. package/dist/lib/collectionRegistry.js.map +1 -0
  659. package/dist/lib/compose-wrapup-view.d.ts +48 -0
  660. package/dist/lib/compose-wrapup-view.d.ts.map +1 -0
  661. package/dist/lib/compose-wrapup-view.js +37 -0
  662. package/dist/lib/compose-wrapup-view.js.map +1 -0
  663. package/dist/lib/config.d.ts +204 -4
  664. package/dist/lib/config.d.ts.map +1 -1
  665. package/dist/lib/config.js +594 -42
  666. package/dist/lib/config.js.map +1 -1
  667. package/dist/lib/connectKeyLabel.d.ts +9 -0
  668. package/dist/lib/connectKeyLabel.d.ts.map +1 -0
  669. package/dist/lib/connectKeyLabel.js +12 -0
  670. package/dist/lib/connectKeyLabel.js.map +1 -0
  671. package/dist/lib/constants.d.ts +46 -0
  672. package/dist/lib/constants.d.ts.map +1 -0
  673. package/dist/lib/constants.js +83 -0
  674. package/dist/lib/constants.js.map +1 -0
  675. package/dist/lib/conversation-engine.d.ts +45 -0
  676. package/dist/lib/conversation-engine.d.ts.map +1 -0
  677. package/dist/lib/conversation-engine.js +112 -0
  678. package/dist/lib/conversation-engine.js.map +1 -0
  679. package/dist/lib/conversation-phases.d.ts +59 -0
  680. package/dist/lib/conversation-phases.d.ts.map +1 -0
  681. package/dist/lib/conversation-phases.js +11 -0
  682. package/dist/lib/conversation-phases.js.map +1 -0
  683. package/dist/lib/conversation-signals.d.ts +30 -0
  684. package/dist/lib/conversation-signals.d.ts.map +1 -0
  685. package/dist/lib/conversation-signals.js +64 -0
  686. package/dist/lib/conversation-signals.js.map +1 -0
  687. package/dist/lib/deployment.d.ts +23 -0
  688. package/dist/lib/deployment.d.ts.map +1 -0
  689. package/dist/lib/deployment.js +78 -0
  690. package/dist/lib/deployment.js.map +1 -0
  691. package/dist/lib/deployment.test.d.ts +5 -0
  692. package/dist/lib/deployment.test.d.ts.map +1 -0
  693. package/dist/lib/deployment.test.js +54 -0
  694. package/dist/lib/deployment.test.js.map +1 -0
  695. package/dist/lib/errors.d.ts +63 -0
  696. package/dist/lib/errors.d.ts.map +1 -0
  697. package/dist/lib/errors.js +72 -0
  698. package/dist/lib/errors.js.map +1 -0
  699. package/dist/lib/experiment.d.ts +18 -0
  700. package/dist/lib/experiment.d.ts.map +1 -0
  701. package/dist/lib/experiment.js +28 -0
  702. package/dist/lib/experiment.js.map +1 -0
  703. package/dist/lib/format.d.ts +10 -0
  704. package/dist/lib/format.d.ts.map +1 -0
  705. package/dist/lib/format.js +27 -0
  706. package/dist/lib/format.js.map +1 -0
  707. package/dist/lib/frontmatter.d.ts +55 -0
  708. package/dist/lib/frontmatter.d.ts.map +1 -0
  709. package/dist/lib/frontmatter.js +92 -0
  710. package/dist/lib/frontmatter.js.map +1 -0
  711. package/dist/lib/frontmatter.test.d.ts +15 -0
  712. package/dist/lib/frontmatter.test.d.ts.map +1 -0
  713. package/dist/lib/frontmatter.test.js +98 -0
  714. package/dist/lib/frontmatter.test.js.map +1 -0
  715. package/dist/lib/glossary.d.ts +19 -0
  716. package/dist/lib/glossary.d.ts.map +1 -0
  717. package/dist/lib/glossary.js +53 -0
  718. package/dist/lib/glossary.js.map +1 -0
  719. package/dist/lib/hook-intents.d.ts +51 -0
  720. package/dist/lib/hook-intents.d.ts.map +1 -0
  721. package/dist/lib/hook-intents.js +85 -0
  722. package/dist/lib/hook-intents.js.map +1 -0
  723. package/dist/lib/inferSourceDate.d.ts +12 -0
  724. package/dist/lib/inferSourceDate.d.ts.map +1 -0
  725. package/dist/lib/inferSourceDate.js +44 -0
  726. package/dist/lib/inferSourceDate.js.map +1 -0
  727. package/dist/lib/method-registry.d.ts +32 -0
  728. package/dist/lib/method-registry.d.ts.map +1 -0
  729. package/dist/lib/method-registry.js +53 -0
  730. package/dist/lib/method-registry.js.map +1 -0
  731. package/dist/lib/normalizeMaterializedFilename.d.ts +28 -0
  732. package/dist/lib/normalizeMaterializedFilename.d.ts.map +1 -0
  733. package/dist/lib/normalizeMaterializedFilename.js +56 -0
  734. package/dist/lib/normalizeMaterializedFilename.js.map +1 -0
  735. package/dist/lib/normalizeMaterializedFilename.test.d.ts +16 -0
  736. package/dist/lib/normalizeMaterializedFilename.test.d.ts.map +1 -0
  737. package/dist/lib/normalizeMaterializedFilename.test.js +90 -0
  738. package/dist/lib/normalizeMaterializedFilename.test.js.map +1 -0
  739. package/dist/lib/notice-marker.d.ts +3 -0
  740. package/dist/lib/notice-marker.d.ts.map +1 -0
  741. package/dist/lib/notice-marker.js +53 -0
  742. package/dist/lib/notice-marker.js.map +1 -0
  743. package/dist/lib/onboarding-path-b.d.ts +10 -0
  744. package/dist/lib/onboarding-path-b.d.ts.map +1 -0
  745. package/dist/lib/onboarding-path-b.js +213 -0
  746. package/dist/lib/onboarding-path-b.js.map +1 -0
  747. package/dist/lib/onboarding-shared.d.ts +80 -0
  748. package/dist/lib/onboarding-shared.d.ts.map +1 -0
  749. package/dist/lib/onboarding-shared.js +174 -0
  750. package/dist/lib/onboarding-shared.js.map +1 -0
  751. package/dist/lib/onboarding-topics.d.ts +27 -0
  752. package/dist/lib/onboarding-topics.d.ts.map +1 -0
  753. package/dist/lib/onboarding-topics.js +57 -0
  754. package/dist/lib/onboarding-topics.js.map +1 -0
  755. package/dist/lib/onboarding.d.ts +17 -0
  756. package/dist/lib/onboarding.d.ts.map +1 -0
  757. package/dist/lib/onboarding.js +350 -0
  758. package/dist/lib/onboarding.js.map +1 -0
  759. package/dist/lib/profiles.d.ts +41 -0
  760. package/dist/lib/profiles.d.ts.map +1 -0
  761. package/dist/lib/profiles.js +188 -0
  762. package/dist/lib/profiles.js.map +1 -0
  763. package/dist/lib/prompts.d.ts +65 -0
  764. package/dist/lib/prompts.d.ts.map +1 -0
  765. package/dist/lib/prompts.js +132 -0
  766. package/dist/lib/prompts.js.map +1 -0
  767. package/dist/lib/repo-detect.d.ts +19 -0
  768. package/dist/lib/repo-detect.d.ts.map +1 -1
  769. package/dist/lib/repo-detect.js +25 -0
  770. package/dist/lib/repo-detect.js.map +1 -1
  771. package/dist/lib/runner.d.ts +2 -0
  772. package/dist/lib/runner.d.ts.map +1 -1
  773. package/dist/lib/runner.js +21 -7
  774. package/dist/lib/runner.js.map +1 -1
  775. package/dist/lib/session.d.ts +35 -0
  776. package/dist/lib/session.d.ts.map +1 -1
  777. package/dist/lib/session.js +94 -3
  778. package/dist/lib/session.js.map +1 -1
  779. package/dist/lib/spinner.d.ts +27 -0
  780. package/dist/lib/spinner.d.ts.map +1 -0
  781. package/dist/lib/spinner.js +76 -0
  782. package/dist/lib/spinner.js.map +1 -0
  783. package/dist/lib/spinner.test.d.ts +2 -0
  784. package/dist/lib/spinner.test.d.ts.map +1 -0
  785. package/dist/lib/spinner.test.js +39 -0
  786. package/dist/lib/spinner.test.js.map +1 -0
  787. package/dist/lib/startup-resolution-signals.d.ts +34 -0
  788. package/dist/lib/startup-resolution-signals.d.ts.map +1 -0
  789. package/dist/lib/startup-resolution-signals.js +118 -0
  790. package/dist/lib/startup-resolution-signals.js.map +1 -0
  791. package/dist/lib/startup-resolution-signals.test.d.ts +2 -0
  792. package/dist/lib/startup-resolution-signals.test.d.ts.map +1 -0
  793. package/dist/lib/startup-resolution-signals.test.js +113 -0
  794. package/dist/lib/startup-resolution-signals.test.js.map +1 -0
  795. package/dist/lib/state.d.ts +51 -0
  796. package/dist/lib/state.d.ts.map +1 -0
  797. package/dist/lib/state.js +90 -0
  798. package/dist/lib/state.js.map +1 -0
  799. package/dist/lib/strip.d.ts +1 -0
  800. package/dist/lib/strip.d.ts.map +1 -1
  801. package/dist/lib/strip.js +15 -0
  802. package/dist/lib/strip.js.map +1 -1
  803. package/dist/lib/style.d.ts +96 -0
  804. package/dist/lib/style.d.ts.map +1 -0
  805. package/dist/lib/style.js +169 -0
  806. package/dist/lib/style.js.map +1 -0
  807. package/dist/lib/style.test.d.ts +7 -0
  808. package/dist/lib/style.test.d.ts.map +1 -0
  809. package/dist/lib/style.test.js +263 -0
  810. package/dist/lib/style.test.js.map +1 -0
  811. package/dist/lib/telemetry.d.ts +15 -0
  812. package/dist/lib/telemetry.d.ts.map +1 -0
  813. package/dist/lib/telemetry.js +47 -0
  814. package/dist/lib/telemetry.js.map +1 -0
  815. package/dist/lib/tokenConstants.d.ts +19 -0
  816. package/dist/lib/tokenConstants.d.ts.map +1 -0
  817. package/dist/lib/tokenConstants.js +19 -0
  818. package/dist/lib/tokenConstants.js.map +1 -0
  819. package/dist/lib/update-check.d.ts +56 -0
  820. package/dist/lib/update-check.d.ts.map +1 -0
  821. package/dist/lib/update-check.js +296 -0
  822. package/dist/lib/update-check.js.map +1 -0
  823. package/dist/lib/upgrade-runner.d.ts +22 -0
  824. package/dist/lib/upgrade-runner.d.ts.map +1 -0
  825. package/dist/lib/upgrade-runner.js +110 -0
  826. package/dist/lib/upgrade-runner.js.map +1 -0
  827. package/dist/lib/wizard-surfaces.d.ts +47 -0
  828. package/dist/lib/wizard-surfaces.d.ts.map +1 -0
  829. package/dist/lib/wizard-surfaces.js +176 -0
  830. package/dist/lib/wizard-surfaces.js.map +1 -0
  831. package/dist/lib/wizard-surfaces.test.d.ts +2 -0
  832. package/dist/lib/wizard-surfaces.test.d.ts.map +1 -0
  833. package/dist/lib/wizard-surfaces.test.js +127 -0
  834. package/dist/lib/wizard-surfaces.test.js.map +1 -0
  835. package/dist/lib/wizard-trust.d.ts +31 -0
  836. package/dist/lib/wizard-trust.d.ts.map +1 -0
  837. package/dist/lib/wizard-trust.js +66 -0
  838. package/dist/lib/wizard-trust.js.map +1 -0
  839. package/dist/lib/wizard-trust.test.d.ts +2 -0
  840. package/dist/lib/wizard-trust.test.d.ts.map +1 -0
  841. package/dist/lib/wizard-trust.test.js +32 -0
  842. package/dist/lib/wizard-trust.test.js.map +1 -0
  843. package/dist/lib/workspace-probe.d.ts +19 -0
  844. package/dist/lib/workspace-probe.d.ts.map +1 -0
  845. package/dist/lib/workspace-probe.js +27 -0
  846. package/dist/lib/workspace-probe.js.map +1 -0
  847. package/dist/lib/workspaceVocabCache.d.ts +60 -0
  848. package/dist/lib/workspaceVocabCache.d.ts.map +1 -0
  849. package/dist/lib/workspaceVocabCache.js +98 -0
  850. package/dist/lib/workspaceVocabCache.js.map +1 -0
  851. package/dist/scoreboard/diagnose.d.ts +182 -0
  852. package/dist/scoreboard/diagnose.d.ts.map +1 -0
  853. package/dist/scoreboard/diagnose.js +250 -0
  854. package/dist/scoreboard/diagnose.js.map +1 -0
  855. package/dist/scoreboard/diagnose.test.d.ts +12 -0
  856. package/dist/scoreboard/diagnose.test.d.ts.map +1 -0
  857. package/dist/scoreboard/diagnose.test.js +192 -0
  858. package/dist/scoreboard/diagnose.test.js.map +1 -0
  859. package/dist/scoreboard/localDrift.d.ts +23 -0
  860. package/dist/scoreboard/localDrift.d.ts.map +1 -0
  861. package/dist/scoreboard/localDrift.js +111 -0
  862. package/dist/scoreboard/localDrift.js.map +1 -0
  863. package/dist/scoreboard/localDrift.test.d.ts +9 -0
  864. package/dist/scoreboard/localDrift.test.d.ts.map +1 -0
  865. package/dist/scoreboard/localDrift.test.js +82 -0
  866. package/dist/scoreboard/localDrift.test.js.map +1 -0
  867. package/dist/setup/__tests__/coach-traces.test.d.ts +2 -0
  868. package/dist/setup/__tests__/coach-traces.test.d.ts.map +1 -0
  869. package/dist/setup/__tests__/coach-traces.test.js +189 -0
  870. package/dist/setup/__tests__/coach-traces.test.js.map +1 -0
  871. package/dist/setup/__tests__/setup-commands.test.d.ts +2 -0
  872. package/dist/setup/__tests__/setup-commands.test.d.ts.map +1 -0
  873. package/dist/setup/__tests__/setup-commands.test.js +177 -0
  874. package/dist/setup/__tests__/setup-commands.test.js.map +1 -0
  875. package/dist/setup/__tests__/state-machine.test.d.ts +2 -0
  876. package/dist/setup/__tests__/state-machine.test.d.ts.map +1 -0
  877. package/dist/setup/__tests__/state-machine.test.js +341 -0
  878. package/dist/setup/__tests__/state-machine.test.js.map +1 -0
  879. package/dist/setup/detect-surfaces.d.ts +21 -0
  880. package/dist/setup/detect-surfaces.d.ts.map +1 -0
  881. package/dist/setup/detect-surfaces.js +39 -0
  882. package/dist/setup/detect-surfaces.js.map +1 -0
  883. package/dist/setup/manifest-writer.d.ts +17 -0
  884. package/dist/setup/manifest-writer.d.ts.map +1 -0
  885. package/dist/setup/manifest-writer.js +153 -0
  886. package/dist/setup/manifest-writer.js.map +1 -0
  887. package/dist/setup/perimeter.d.ts +72 -0
  888. package/dist/setup/perimeter.d.ts.map +1 -0
  889. package/dist/setup/perimeter.js +128 -0
  890. package/dist/setup/perimeter.js.map +1 -0
  891. package/dist/setup/state-machine.d.ts +67 -0
  892. package/dist/setup/state-machine.d.ts.map +1 -0
  893. package/dist/setup/state-machine.js +124 -0
  894. package/dist/setup/state-machine.js.map +1 -0
  895. package/dist/surfaces/__tests__/adapter.test.d.ts +2 -0
  896. package/dist/surfaces/__tests__/adapter.test.d.ts.map +1 -0
  897. package/dist/surfaces/__tests__/adapter.test.js +90 -0
  898. package/dist/surfaces/__tests__/adapter.test.js.map +1 -0
  899. package/dist/surfaces/__tests__/pb-setup-passthrough.test.d.ts +2 -0
  900. package/dist/surfaces/__tests__/pb-setup-passthrough.test.d.ts.map +1 -0
  901. package/dist/surfaces/__tests__/pb-setup-passthrough.test.js +132 -0
  902. package/dist/surfaces/__tests__/pb-setup-passthrough.test.js.map +1 -0
  903. package/dist/surfaces/__tests__/telemetry.test.d.ts +2 -0
  904. package/dist/surfaces/__tests__/telemetry.test.d.ts.map +1 -0
  905. package/dist/surfaces/__tests__/telemetry.test.js +55 -0
  906. package/dist/surfaces/__tests__/telemetry.test.js.map +1 -0
  907. package/dist/surfaces/adapter.d.ts +70 -0
  908. package/dist/surfaces/adapter.d.ts.map +1 -0
  909. package/dist/surfaces/adapter.js +2 -0
  910. package/dist/surfaces/adapter.js.map +1 -0
  911. package/dist/surfaces/adapters/claude.d.ts +3 -0
  912. package/dist/surfaces/adapters/claude.d.ts.map +1 -0
  913. package/dist/surfaces/adapters/claude.js +67 -0
  914. package/dist/surfaces/adapters/claude.js.map +1 -0
  915. package/dist/surfaces/adapters/codex.d.ts +3 -0
  916. package/dist/surfaces/adapters/codex.d.ts.map +1 -0
  917. package/dist/surfaces/adapters/codex.js +61 -0
  918. package/dist/surfaces/adapters/codex.js.map +1 -0
  919. package/dist/surfaces/adapters/copilot.d.ts +3 -0
  920. package/dist/surfaces/adapters/copilot.d.ts.map +1 -0
  921. package/dist/surfaces/adapters/copilot.js +59 -0
  922. package/dist/surfaces/adapters/copilot.js.map +1 -0
  923. package/dist/surfaces/adapters/cursor.d.ts +3 -0
  924. package/dist/surfaces/adapters/cursor.d.ts.map +1 -0
  925. package/dist/surfaces/adapters/cursor.js +78 -0
  926. package/dist/surfaces/adapters/cursor.js.map +1 -0
  927. package/dist/surfaces/registry.d.ts +76 -0
  928. package/dist/surfaces/registry.d.ts.map +1 -0
  929. package/dist/surfaces/registry.js +117 -0
  930. package/dist/surfaces/registry.js.map +1 -0
  931. package/dist/surfaces/telemetry.d.ts +17 -0
  932. package/dist/surfaces/telemetry.d.ts.map +1 -0
  933. package/dist/surfaces/telemetry.js +31 -0
  934. package/dist/surfaces/telemetry.js.map +1 -0
  935. package/package.json +12 -4
  936. package/templates/archetypes/boundary.md +23 -0
  937. package/templates/archetypes/constraint.md +23 -0
  938. package/templates/archetypes/convention.md +23 -0
  939. package/templates/archetypes/policy.md +23 -0
  940. package/templates/archetypes/quality-gate.md +23 -0
  941. package/templates/archetypes/workflow.md +23 -0
  942. package/templates/general/code-integrity.md +11 -0
  943. package/templates/general/getting-started.md +12 -0
  944. package/templates/method-registry.json +16 -0
  945. package/templates/node-ts/code-integrity.md +13 -0
  946. package/templates/node-ts/testing.md +12 -0
  947. package/templates/python/code-integrity.md +13 -0
  948. package/templates/python/testing.md +12 -0
@@ -1,19 +1,254 @@
1
1
  /**
2
2
  * pb handshake — generate context files for AI developer tools.
3
- * The fourth delivery surface: context export (GLO-63, DEC-161).
3
+ * Context export wiring (read-only filesystem bridge; GLO-63, DEC-161) — not a product surface.
4
4
  */
5
- import { mkdirSync, writeFileSync, existsSync, readFileSync } from 'fs';
6
- import { join } from 'path';
5
+ import { mkdirSync, writeFileSync, existsSync, readFileSync, readdirSync, copyFileSync, appendFileSync, unlinkSync, statSync, renameSync, rmSync, realpathSync } from 'fs';
6
+ import { join, dirname, resolve, basename, relative, sep, isAbsolute } from 'path';
7
7
  import { homedir } from 'os';
8
- import { createInterface } from 'readline';
8
+ import { fileURLToPath } from 'url';
9
+ import { createHash } from 'crypto';
10
+ import { execSync } from 'child_process';
9
11
  import { getConfigOrGuide } from '../lib/config.js';
10
- import { mcpCall } from '../lib/client.js';
11
- import { detectRepo } from '../lib/repo-detect.js';
12
+ import { select as promptSelect, confirm as promptConfirm } from '../lib/prompts.js';
13
+ import { composeHooksFromIntents, getHookStatusForSurface } from '../lib/hook-intents.js';
14
+ import { kernelCall, kernelCallWithSession } from '../lib/client.js';
15
+ import { readSession } from '../lib/session.js';
16
+ import { detectRepo, extractWorkspaceProfile } from '../lib/repo-detect.js';
12
17
  import { generateContextMd } from '../generators/context-md.js';
13
18
  import { generateBriefingMd } from '../generators/briefing-md.js';
14
- import { MARKER, generateClaudeMd, generateCursorMdc, generateCopilotMd } from '../generators/adapters.js';
15
- import { readCanonicalSkills, readCanonicalRules, generateCursorSkill, generateCursorRule, generateClaudeRule, generateClaudeSkillRouter, shouldEmitToTarget, filterByLevel, } from '../generators/portable-knowledge.js';
19
+ import { MARKER, generateAgentsMd, generateClaudeMd, generateCursorMdc, generateCopilotMd } from '../generators/adapters.js';
20
+ import { readCanonicalSkills, readCanonicalRules, readPersonalLayer, readPersonalSkillsLayer, generateCursorSkill, generateCursorRule, generateCodexSkill, generateCodexSkillIndex, generateClaudeRule, generateClaudeSkillRouter, shouldEmitToTarget, filterByLevel, validateCodexSkills, evaluateConditions, STAGE_TO_MAX_LEVEL, LEVEL_ORDER, } from '../generators/portable-knowledge.js';
21
+ import { generateChainRules } from '../generators/chain-rules.js';
22
+ import { saveHandshakeState, loadPreviousState, diffHandshakeState, formatDiff, buildCurrentState, } from '../generators/handshake-diff.js';
23
+ import { resolveSurfaceProfile } from '../generators/surface-profiles.js';
16
24
  import { formatHandshakeReport } from '../formatters/handshake.js';
25
+ import { classifyAdapterFile, detectEol, spliceAppend, spliceReplace } from '../generators/region.js';
26
+ import { REGION_PROJECTIONS } from '../generators/region-projections.js';
27
+ import { readManifest, readManifestStatus, filterByAdoptionState } from '../generators/manifest.js';
28
+ import { generateBoundaryManifest, getBoundaryEnforcementMode } from '../generators/boundary-manifest.js';
29
+ import { loadMethodRegistry } from '../lib/method-registry.js';
30
+ import { CLIError, ErrorCode } from '../lib/errors.js';
31
+ import { trackEvent } from '../lib/telemetry.js';
32
+ import { replaceVocabTokens } from '../lib/canonicalRefs.js';
33
+ // WP-436 S3: vocab projector — resolves {{vocab:...}} tokens before writing to disk.
34
+ import { getOrFetchVocabCtx } from '../lib/workspaceVocabCache.js';
35
+ import { normalizeMaterializedFilename } from '../lib/normalizeMaterializedFilename.js';
36
+ import { parseSetupFrontmatter } from '../lib/frontmatter.js';
37
+ import { sanitizeStartupResolutionSignals } from '../lib/startup-resolution-signals.js';
38
+ import { assertSetupWritePath } from '../setup/perimeter.js';
39
+ // WP-421 S3: SurfaceAdapter reverse-map for tampered prompts (DEC-952, doneWhen #34).
40
+ import { canonicalPathForAnySurface, SURFACE_GOVERN_NO_SURFACES, SURFACE_REGISTRY, validateSurfacesForMode, } from '../surfaces/registry.js';
41
+ import { getReverseMapFallbackMessage, reportReverseMapMissing } from '../surfaces/telemetry.js';
42
+ const MAX_HANDSHAKE_WAIT_MS = 10_000; // 10 seconds
43
+ const POLL_INTERVAL_MS = 500; // 500 ms per poll
44
+ const MAX_POLLS = MAX_HANDSHAKE_WAIT_MS / POLL_INTERVAL_MS; // 20
45
+ // ── WP-379 S4: Dormant marker ─────────────────────────────────────────────────
46
+ /**
47
+ * DORMANT_MARKER — appended to previously-projected asset files when the asset's
48
+ * gate deactivates (e.g. workspace readiness exceeds the max threshold).
49
+ *
50
+ * Contract:
51
+ * - The file is NOT deleted. It persists on disk so that history is preserved
52
+ * and the agent surface remains inspectable.
53
+ * - The marker is appended at the end of the file, idempotent — if it already
54
+ * exists, no second append occurs.
55
+ * - The marker does NOT trigger a drift TEN. Dormant files are intentionally
56
+ * deactivated, not accidentally forked.
57
+ * - The marker is never included in active file writes — only dormant writes.
58
+ *
59
+ * Used by: writeDormantMarker() (write) and hasDormantMarker() (idempotency check).
60
+ * Exported for use in tests.
61
+ *
62
+ * Chain: WP-379 S4.
63
+ */
64
+ export const DORMANT_MARKER = '<!-- pb-status: dormant -->';
65
+ /**
66
+ * hasDormantMarker — check whether a file on disk already has the dormant marker.
67
+ * Used for idempotency: if the marker is already present, skip the append.
68
+ */
69
+ function hasDormantMarker(content) {
70
+ return content.includes(DORMANT_MARKER);
71
+ }
72
+ /**
73
+ * writeDormantMarker — append the dormant marker to a previously-projected file.
74
+ *
75
+ * Idempotent: if DORMANT_MARKER is already present, no-op.
76
+ * Only operates on files that have the auto-gen MARKER — we never touch
77
+ * manually-authored files.
78
+ *
79
+ * @param filePath Absolute path to the file.
80
+ * @returns 'written' | 'already-dormant' | 'skipped' (no auto-gen marker)
81
+ */
82
+ export function writeDormantMarkerToFile(filePath) {
83
+ if (!existsSync(filePath))
84
+ return 'skipped';
85
+ const content = readFileSync(filePath, 'utf8');
86
+ // Only mark files that were originally projected by pb handshake.
87
+ // Files without the auto-gen MARKER are manually authored — leave them alone.
88
+ if (!content.includes(MARKER))
89
+ return 'skipped';
90
+ if (hasDormantMarker(content))
91
+ return 'already-dormant';
92
+ // Append the marker on its own line. No trailing newline assumption —
93
+ // appendFileSync adds to whatever is already there.
94
+ appendFileSync(filePath, `\n${DORMANT_MARKER}\n`);
95
+ return 'written';
96
+ }
97
+ /**
98
+ * renameSurfaceForDormancy — rename a projected surface file to `<path>.dormant`
99
+ * so external scanners (which glob *.md / *.mdc) stop loading it. WP-426 E4.
100
+ * The HTML-comment marker is invisible to scanners; the extension change removes
101
+ * the file from their glob set. Only touches files carrying the auto-gen MARKER.
102
+ * Runs on an INDEPENDENT existence check (not gated on marker-write result) so
103
+ * legacy WP-379 marker-only files still rename. Idempotent.
104
+ * Codex P1: when a .dormant already exists, only replace it if its body matches the
105
+ * live file. A divergent .dormant (user edited it) is preserved and reported as 'drift'
106
+ * rather than force-deleted — no silent data loss.
107
+ * @returns 'renamed' | 'replaced' | 'already-dormant' | 'skipped' | 'drift'
108
+ */
109
+ export function renameSurfaceForDormancy(filePath) {
110
+ const dormantPath = `${filePath}.dormant`;
111
+ if (!existsSync(filePath))
112
+ return existsSync(dormantPath) ? 'already-dormant' : 'skipped';
113
+ if (!readFileSync(filePath, 'utf8').includes(MARKER))
114
+ return 'skipped';
115
+ const replacing = existsSync(dormantPath);
116
+ if (replacing) {
117
+ // Codex P1: the existing .dormant may carry user edits (e.g. the user reactivated to
118
+ // the live path but kept an edited dormant sibling). Compare normalized bodies
119
+ // (DORMANT_MARKER + volatile auto-gen timestamp stripped from both); if they diverge,
120
+ // preserve the .dormant and signal drift instead of force-replacing it.
121
+ const strip = (s) => normalizeHandshakeContentForComparison(s.split(DORMANT_MARKER).join('')).trimEnd();
122
+ if (strip(readFileSync(filePath, 'utf8')) !== strip(readFileSync(dormantPath, 'utf8'))) {
123
+ return 'drift';
124
+ }
125
+ rmSync(dormantPath, { force: true });
126
+ }
127
+ renameSync(filePath, dormantPath);
128
+ return replacing ? 'replaced' : 'renamed';
129
+ }
130
+ /**
131
+ * restoreSurfaceFromDormant — clean the orphan `<path>.dormant` sibling after a
132
+ * raised (observe→project) asset has been re-projected fresh from the DB body by
133
+ * the normal write loop. WP-426 E4.
134
+ * Normalizes the volatile auto-gen timestamp (via normalizeHandshakeContentForComparison)
135
+ * before comparing, else the lowering-time timestamp would always force a false
136
+ * 'orphan-drift'. Identical body → remove. Differs (user edited) → preserve + drift.
137
+ * @returns 'restored' | 'orphan-drift' | 'skipped'
138
+ */
139
+ export function restoreSurfaceFromDormant(filePath) {
140
+ const dormantPath = `${filePath}.dormant`;
141
+ if (!existsSync(dormantPath))
142
+ return 'skipped';
143
+ // WP-426 E4: no fresh live projection this run (surface filtered / user-owned / not written) →
144
+ // nothing to reconcile; absence of fresh is NOT evidence of a user edit. Leave the .dormant.
145
+ if (!existsSync(filePath))
146
+ return 'skipped';
147
+ const fresh = readFileSync(filePath, 'utf8');
148
+ // Codex P2: only reconcile against a PB-managed projection. A live file WITHOUT the auto-gen
149
+ // MARKER is a user-owned file (shouldWriteAdapter would have skipped reprojecting it),
150
+ // so it was NOT raised this run — comparing the .dormant against it would wrongly delete it or
151
+ // re-fire "edited while dormant" drift. Leave the .dormant untouched.
152
+ if (!fresh.includes(MARKER))
153
+ return 'skipped';
154
+ const dormant = readFileSync(dormantPath, 'utf8').split(DORMANT_MARKER).join('');
155
+ const norm = (s) => normalizeHandshakeContentForComparison(s).trimEnd();
156
+ if (norm(dormant) === norm(fresh)) {
157
+ rmSync(dormantPath, { force: true });
158
+ return 'restored';
159
+ }
160
+ return 'orphan-drift';
161
+ }
162
+ /**
163
+ * deriveDormantFilePaths — compute the set of on-disk file paths that would have
164
+ * been projected for a given dormant asset (by name and assetKind).
165
+ *
166
+ * Assets are projected to one or more surfaces (cursor/claude/codex) depending
167
+ * on shouldEmitToTarget. Since we don't re-run shouldEmitToTarget here, we
168
+ * speculatively probe all known surface paths and let writeDormantMarkerToFile
169
+ * decide whether each exists and has the auto-gen MARKER.
170
+ *
171
+ * @param asset The dormant asset from the server.
172
+ * @param cwd Current working directory (project root).
173
+ * @returns Each candidate path paired with its owning surface, so callers can
174
+ * filter by the run's allowedTargets (--surfaces) and the perimeter.
175
+ */
176
+ function deriveDormantFilePaths(asset, cwd) {
177
+ // Defense-in-depth: even though `name` originates from platform-seeded DB
178
+ // entries (not user input), validate it against a strict charset before
179
+ // interpolating into a filesystem path. Reject anything that could traverse
180
+ // out of the expected directories. WP-379 S4 review finding.
181
+ if (!/^[A-Za-z0-9 ._-]+$/.test(asset.name)) {
182
+ return [];
183
+ }
184
+ const out = [];
185
+ const { name, assetKind } = asset;
186
+ if (assetKind === 'skill') {
187
+ out.push({ path: join(cwd, '.cursor', 'skills', name, 'SKILL.md'), surface: 'cursor' });
188
+ out.push({ path: join(cwd, '.codex', 'skills', `${name}.md`), surface: 'codex' });
189
+ }
190
+ else if (assetKind === 'rule' || assetKind === 'hook') {
191
+ out.push({ path: join(cwd, '.cursor', 'rules', `${name}.mdc`), surface: 'cursor' });
192
+ out.push({ path: join(cwd, '.claude', 'rules', `${name}.md`), surface: 'claude' });
193
+ }
194
+ return out;
195
+ }
196
+ /**
197
+ * Single-shot health probe — calls `workspace.health` and inspects
198
+ * `starterSetupSeeded`. Does NOT poll internally; polling is the caller's
199
+ * responsibility (connect-context.ts).
200
+ *
201
+ * Returns:
202
+ * - `seeds-ready` — health query succeeded AND starterSetupSeeded is true
203
+ * - `seeds-pending` — health query succeeded but starterSetupSeeded is false
204
+ * - `probe-failed` — health query threw (network, auth, etc.)
205
+ */
206
+ export async function probeStarterSetupSeeded() {
207
+ try {
208
+ const health = await kernelCall('workspace.health', {});
209
+ if (health.starterSetupSeeded) {
210
+ return { status: 'seeds-ready' };
211
+ }
212
+ const starterGaps = (health.gaps ?? []).filter((g) => g.kind === 'starter-setup-missing' || g.kind === 'platform-domains-missing');
213
+ return {
214
+ status: 'seeds-pending',
215
+ gaps: starterGaps.length > 0 ? starterGaps : [
216
+ {
217
+ kind: 'starter-setup-missing',
218
+ severity: 'warn',
219
+ message: 'Starter setup seeds are still running.',
220
+ },
221
+ ],
222
+ };
223
+ }
224
+ catch (err) {
225
+ return {
226
+ status: 'probe-failed',
227
+ error: err instanceof Error ? err.message : String(err),
228
+ };
229
+ }
230
+ }
231
+ /**
232
+ * Poll `probeStarterSetupSeeded` up to MAX_POLLS times (10s at 500ms intervals).
233
+ * Returns the final probe result — caller decides how to render the outcome.
234
+ *
235
+ * Exported so connect-context.ts can use it without re-implementing the loop.
236
+ */
237
+ export async function pollUntilSeedsReady() {
238
+ for (let poll = 0; poll < MAX_POLLS; poll++) {
239
+ const result = await probeStarterSetupSeeded();
240
+ if (result.status === 'seeds-ready')
241
+ return result;
242
+ if (result.status === 'probe-failed')
243
+ return result; // don't retry on auth/network errors
244
+ // seeds-pending — wait before next poll
245
+ if (poll < MAX_POLLS - 1) {
246
+ await new Promise((res) => setTimeout(res, POLL_INTERVAL_MS));
247
+ }
248
+ }
249
+ // Final probe after exhausting waits — return whatever state we have
250
+ return probeStarterSetupSeeded();
251
+ }
17
252
  const LEVELS = {
18
253
  guide: {
19
254
  label: 'Guide me',
@@ -45,9 +280,6 @@ const LEVEL_KEYS = ['guide', 'work', 'silent', 'full-trust'];
45
280
  // Hook failure contract (TEN-712): all hook commands MUST end with '2>/dev/null || true'
46
281
  // so Claude Code always starts even if pb is unavailable. Never remove this suffix.
47
282
  const INIT_PERMISSION = 'Bash(pb:*)';
48
- const INIT_SESSION_START_CMD = 'pb session start 2>/dev/null || true';
49
- const INIT_SESSION_CLOSE_CMD = 'pb session close 2>/dev/null || true';
50
- const INIT_PRECOMPACT_CMD = 'pb session id > /dev/null 2>&1 && echo \'{"systemMessage": "pb session active — capture decisions/tensions before compacting: pb capture \\"DEC: ...\\""}\' || true';
51
283
  function readSettings(filePath) {
52
284
  if (!existsSync(filePath))
53
285
  return {};
@@ -59,9 +291,6 @@ function readSettings(filePath) {
59
291
  return {};
60
292
  }
61
293
  }
62
- function hasCommand(groups, cmd) {
63
- return (groups ?? []).some((g) => g.hooks?.some((h) => h.command === cmd));
64
- }
65
294
  // Team write: hooks + Bash(pb:*) → .claude/settings.json (safe to commit)
66
295
  function writeTeamSettings(cwd, dryRun) {
67
296
  const claudeDir = join(cwd, '.claude');
@@ -77,26 +306,10 @@ function writeTeamSettings(cwd, dryRun) {
77
306
  }
78
307
  settings.permissions = permissions;
79
308
  const hooks = (settings.hooks ?? {});
80
- if (!hasCommand(hooks.SessionStart, INIT_SESSION_START_CMD)) {
81
- hooks.SessionStart = [
82
- ...(hooks.SessionStart ?? []),
83
- { hooks: [{ type: 'command', command: INIT_SESSION_START_CMD, statusMessage: 'Opening pb session...' }] },
84
- ];
85
- added.push('SessionStart → pb session start');
86
- }
87
- if (!hasCommand(hooks.Stop, INIT_SESSION_CLOSE_CMD)) {
88
- hooks.Stop = [
89
- ...(hooks.Stop ?? []),
90
- { hooks: [{ type: 'command', command: INIT_SESSION_CLOSE_CMD, statusMessage: 'Closing pb session...' }] },
91
- ];
92
- added.push('Stop → pb session close');
93
- }
94
- if (!hasCommand(hooks.PreCompact, INIT_PRECOMPACT_CMD)) {
95
- hooks.PreCompact = [
96
- ...(hooks.PreCompact ?? []),
97
- { hooks: [{ type: 'command', command: INIT_PRECOMPACT_CMD }] },
98
- ];
99
- added.push('PreCompact → decision capture reminder');
309
+ const hookAdditions = composeHooksFromIntents(['session-start', 'session-close', 'pre-compact'], hooks);
310
+ for (const addition of hookAdditions) {
311
+ hooks[addition.event] = [...(hooks[addition.event] ?? []), addition.entry];
312
+ added.push(addition.label);
100
313
  }
101
314
  settings.hooks = hooks;
102
315
  if (!dryRun) {
@@ -133,31 +346,26 @@ function writePersonalSettings(levelKey, dryRun) {
133
346
  return added;
134
347
  }
135
348
  async function promptLevel() {
136
- const rl = createInterface({ input: process.stdin, output: process.stdout });
137
- console.log('\nHow much should Claude explain before acting?\n');
138
- LEVEL_KEYS.forEach((key, i) => {
139
- const l = LEVELS[key];
140
- console.log(` ${i + 1}. ${l.label.padEnd(12)} — ${l.description}`);
141
- });
142
- console.log('');
143
- return new Promise((resolve) => {
144
- rl.question('> ', (answer) => {
145
- rl.close();
146
- const n = parseInt(answer.trim(), 10);
147
- if (!(n >= 1 && n <= LEVEL_KEYS.length)) {
148
- console.log(' (Invalid choice — defaulting to "Just work")');
149
- }
150
- resolve(n >= 1 && n <= LEVEL_KEYS.length ? LEVEL_KEYS[n - 1] : 'work');
151
- });
349
+ const result = await promptSelect({
350
+ message: 'How much should Claude explain before acting?',
351
+ options: LEVEL_KEYS.map((key) => ({
352
+ value: key,
353
+ label: LEVELS[key].label,
354
+ hint: LEVELS[key].description,
355
+ })),
152
356
  });
357
+ return result;
153
358
  }
154
359
  export async function runHandshakeInit(options = {}) {
155
360
  const cwd = process.cwd();
156
361
  const dryRun = options.dryRun ?? false;
157
362
  const suffix = dryRun ? ' (dry run)' : '';
158
363
  if (options.level && !LEVEL_KEYS.includes(options.level)) {
159
- console.error(`Unknown level "${options.level}". Valid: ${LEVEL_KEYS.join(', ')}`);
160
- process.exit(1);
364
+ throw new CLIError(`Unknown level "${options.level}".`, {
365
+ code: ErrorCode.VALIDATION_FAILED,
366
+ category: 'validation',
367
+ guidance: `Valid levels: ${LEVEL_KEYS.join(', ')}`,
368
+ });
161
369
  }
162
370
  console.log('Setting up Claude Code integration...\n');
163
371
  // Step 1: Team config — always, no prompt
@@ -172,7 +380,7 @@ export async function runHandshakeInit(options = {}) {
172
380
  console.log(` + ${item}`);
173
381
  }
174
382
  // Step 2: Personal config — wizard or --level flag
175
- // Cast is safe: LEVEL_KEYS.includes() validated above; invalid level already called process.exit(1).
383
+ // Cast is safe: LEVEL_KEYS.includes() validated above; invalid level already threw CLIError.
176
384
  const levelKey = options.level ? options.level : await promptLevel();
177
385
  const level = LEVELS[levelKey];
178
386
  const personalAdded = writePersonalSettings(levelKey, dryRun);
@@ -194,6 +402,319 @@ export async function runHandshakeInit(options = {}) {
194
402
  if (!dryRun)
195
403
  console.log('Reload /hooks in Claude Code (or restart) to activate.');
196
404
  console.log('Run `pb handshake --init --level <guide|work|silent|full-trust>` to change level.');
405
+ // Step 2b: Report multi-surface hook opt-in status (WP-310 E3b)
406
+ // Reads manifest.hooks.{cursor,copilot} and prints an informational note for
407
+ // each opted-in surface. Silence = no manifest or no hooks flags set.
408
+ // DEC-536: Claude-native default is already wired above; this block only fires
409
+ // when the user has explicitly opted in via manifest.
410
+ const pbDirForManifest = join(cwd, '.productbrain');
411
+ const initManifest = readManifest(pbDirForManifest);
412
+ const multiSurfaceOptIns = [];
413
+ if (initManifest?.hooks?.cursor)
414
+ multiSurfaceOptIns.push('cursor');
415
+ if (initManifest?.hooks?.copilot)
416
+ multiSurfaceOptIns.push('copilot');
417
+ if (multiSurfaceOptIns.length > 0) {
418
+ console.log('');
419
+ for (const surface of multiSurfaceOptIns) {
420
+ const status = getHookStatusForSurface(surface);
421
+ if (status.writable) {
422
+ // Future-proofing path: surface has hook events, would write files.
423
+ console.log(`ℹ ${surface} hooks: opted in — hooks will be written`);
424
+ }
425
+ else {
426
+ console.log(`ℹ ${surface} hooks: opted in — no auto-hooks (${surface} has no session events; run \`pb session start\` manually)`);
427
+ }
428
+ }
429
+ }
430
+ // Step 3: Scaffold starter templates if .productbrain/rules/ is empty
431
+ const rulesDir = join(cwd, '.productbrain', 'rules');
432
+ const hasExistingRules = existsSync(rulesDir) && readdirSync(rulesDir).filter((f) => f.endsWith('.md')).length > 0;
433
+ if (!hasExistingRules) {
434
+ // Detect stack from repo to pick template set
435
+ const repo = detectRepo(cwd);
436
+ const stack = repo.detectedStack.map((s) => s.toLowerCase());
437
+ let templateSet;
438
+ if (stack.some((s) => ['typescript', 'sveltekit', 'nextjs', 'react'].includes(s))) {
439
+ templateSet = 'node-ts';
440
+ }
441
+ else if (stack.includes('python')) {
442
+ templateSet = 'python';
443
+ }
444
+ else {
445
+ templateSet = 'general';
446
+ }
447
+ // Resolve templates directory relative to this file
448
+ const __filename = fileURLToPath(import.meta.url);
449
+ const __dirname = dirname(__filename);
450
+ // From dist/commands/handshake.js → ../../templates/
451
+ const templatesRoot = resolve(__dirname, '..', '..', 'templates');
452
+ const templateDir = join(templatesRoot, templateSet);
453
+ if (existsSync(templateDir)) {
454
+ const templateFiles = readdirSync(templateDir).filter((f) => f.endsWith('.md'));
455
+ if (templateFiles.length > 0) {
456
+ console.log('');
457
+ console.log(`Scaffolding starter rules from ${templateSet} template...`);
458
+ if (!dryRun) {
459
+ mkdirSync(rulesDir, { recursive: true });
460
+ for (const file of templateFiles) {
461
+ const src = join(templateDir, file);
462
+ const dest = join(rulesDir, file);
463
+ copyFileSync(src, dest);
464
+ console.log(` + .productbrain/rules/${file}`);
465
+ }
466
+ console.log('');
467
+ console.log('Run `pb handshake` to sync the scaffolded rules to your AI tools.');
468
+ }
469
+ else {
470
+ for (const file of templateFiles) {
471
+ console.log(` + .productbrain/rules/${file} (dry run)`);
472
+ }
473
+ }
474
+ }
475
+ }
476
+ }
477
+ }
478
+ const LOCAL_REF_REGEX = /\b(?:WP|TEN|DEC|BET|FEAT|INS|STD|PRI|ARCH)[-\s#]*\d+\b|\bPR\s*#?\s*\d+\b/gi;
479
+ const STARTUP_RESOLUTION_RESOLVER_VERSION = 'startup-resolution-v1';
480
+ // Global cap on collected refs (spec "Contract: StartupResolutionSignals" — arrays are capped).
481
+ const LOCAL_REF_GLOBAL_CAP = 25;
482
+ // Per-source cap so a broad source (context.md) cannot starve the task-specific
483
+ // briefing.md refs (Codex@677). With the global cap at 25, a single source capped
484
+ // at 18 always leaves slots for at least one other source's distinct refs.
485
+ const LOCAL_REF_PER_SOURCE_CAP = 18;
486
+ // PR-ref diff dereference is time-boxed so an unauthed/slow `gh` cannot stall
487
+ // session-start (F4 — guard: time-box + skip gracefully on failure).
488
+ const PR_DIFF_TIMEOUT_MS = 4_000;
489
+ // Cap on how many PR refs we dereference per handshake — bounds the worst-case
490
+ // number of `gh` subprocess invocations.
491
+ const PR_DIFF_MAX_REFS = 5;
492
+ function runGitText(cwd, command) {
493
+ try {
494
+ const value = execSync(command, {
495
+ cwd,
496
+ encoding: 'utf8',
497
+ stdio: ['ignore', 'pipe', 'ignore'],
498
+ }).trim();
499
+ return value.length > 0 ? value : undefined;
500
+ }
501
+ catch {
502
+ return undefined;
503
+ }
504
+ }
505
+ /**
506
+ * Dereference a "PR #N" ref to its changed paths via `gh pr diff <N> --name-only`.
507
+ * F4 (spec "PR refs must dereference to diff paths when available"): the server
508
+ * resolver has no git/gh access, so PR-domain evidence can only be produced
509
+ * here in the CLI. Guarded: `gh` may be absent/unauthed or the PR may not exist,
510
+ * so failures are swallowed (returns []) and the call is time-boxed.
511
+ *
512
+ * Injectable via `run` so tests can drive the dereference without a real `gh`.
513
+ */
514
+ export function dereferencePrRefToPaths(cwd, prNumber, run = runGitTextTimeboxed) {
515
+ if (!Number.isInteger(prNumber) || prNumber <= 0)
516
+ return [];
517
+ const raw = run(cwd, `gh pr diff ${prNumber} --name-only`, PR_DIFF_TIMEOUT_MS);
518
+ if (!raw)
519
+ return [];
520
+ return raw.split(/\r?\n/).map((line) => line.trim()).filter(Boolean);
521
+ }
522
+ /** Time-boxed variant of runGitText for the PR-diff dereference (F4). */
523
+ function runGitTextTimeboxed(cwd, command, timeoutMs) {
524
+ try {
525
+ const value = execSync(command, {
526
+ cwd,
527
+ encoding: 'utf8',
528
+ stdio: ['ignore', 'pipe', 'ignore'],
529
+ timeout: timeoutMs,
530
+ }).trim();
531
+ return value.length > 0 ? value : undefined;
532
+ }
533
+ catch {
534
+ return undefined;
535
+ }
536
+ }
537
+ function readOptionalLocalText(cwd, path) {
538
+ const fullPath = join(cwd, path);
539
+ if (!existsSync(fullPath))
540
+ return undefined;
541
+ try {
542
+ return readFileSync(fullPath, 'utf8');
543
+ }
544
+ catch {
545
+ return undefined;
546
+ }
547
+ }
548
+ function normalizeLocalRef(ref) {
549
+ const trimmed = ref.trim().replace(/\s+/g, ' ');
550
+ const pr = trimmed.match(/^PR\s*#?\s*(\d+)$/i);
551
+ if (pr)
552
+ return `PR #${pr[1]}`;
553
+ const entry = trimmed.match(/^([A-Z]+)[-\s#]*(\d+)$/i);
554
+ if (entry)
555
+ return `${entry[1].toUpperCase()}-${entry[2]}`;
556
+ return trimmed;
557
+ }
558
+ /**
559
+ * Collect normalized local refs across several text sources, deduped.
560
+ *
561
+ * Codex@677: a per-source cap (LOCAL_REF_PER_SOURCE_CAP) is applied in addition
562
+ * to the global cap so a broad source (e.g. a context.md carrying 25+ IDs) cannot
563
+ * consume the whole budget before a later, task-specific source (briefing.md) is
564
+ * even read. Each source contributes at most its quota; refs already collected by
565
+ * an earlier source still dedupe across sources (and do not count against the
566
+ * later source's quota, since they are skipped before the per-source counter).
567
+ */
568
+ function collectLocalRefs(texts) {
569
+ const refs = [];
570
+ const seen = new Set();
571
+ for (const text of texts) {
572
+ if (!text)
573
+ continue;
574
+ let fromThisSource = 0;
575
+ for (const match of text.matchAll(LOCAL_REF_REGEX)) {
576
+ const ref = normalizeLocalRef(match[0]);
577
+ const key = ref.toLowerCase();
578
+ if (seen.has(key))
579
+ continue;
580
+ if (fromThisSource >= LOCAL_REF_PER_SOURCE_CAP)
581
+ break;
582
+ seen.add(key);
583
+ refs.push(ref);
584
+ fromThisSource++;
585
+ if (refs.length >= LOCAL_REF_GLOBAL_CAP)
586
+ return refs;
587
+ }
588
+ }
589
+ return refs;
590
+ }
591
+ function deriveHandshakeStartupTask(refs, changedPaths, callerTask) {
592
+ if (callerTask && callerTask.trim())
593
+ return callerTask.trim();
594
+ const parts = [];
595
+ if (refs.length > 0)
596
+ parts.push(refs.join(' '));
597
+ if (changedPaths.length > 0)
598
+ parts.push(`changed paths ${changedPaths.slice(0, 5).join(' ')}`);
599
+ return parts.length > 0 ? parts.join(' ') : undefined;
600
+ }
601
+ const DEFAULT_HANDSHAKE_SIGNAL_DEPS = {
602
+ runGit: runGitText,
603
+ prDiff: (cwd, prNumber) => dereferencePrRefToPaths(cwd, prNumber),
604
+ };
605
+ /** Extract the PR number from a normalized "PR #N" ref, else null. */
606
+ function prNumberFromRef(ref) {
607
+ const m = ref.match(/^PR\s*#?\s*(\d+)$/i);
608
+ if (!m)
609
+ return null;
610
+ const n = Number(m[1]);
611
+ return Number.isInteger(n) && n > 0 ? n : null;
612
+ }
613
+ export function deriveHandshakeStartupSignals(cwd, callerSignals = {}, deps = DEFAULT_HANDSHAKE_SIGNAL_DEPS) {
614
+ const { runGit, prDiff } = deps;
615
+ const branchName = runGit(cwd, 'git branch --show-current');
616
+ const commitSha = runGit(cwd, 'git rev-parse --short HEAD');
617
+ // Changed paths come from THREE sources, unioned + deduped:
618
+ // 1. Dirty worktree (`git diff --name-only HEAD`) — existing behavior.
619
+ // 2. Committed branch delta vs the merge-base with origin/main (Codex@665):
620
+ // after the branch is committed and the worktree is clean, source #1 is
621
+ // empty and the branch delta would be invisible without this.
622
+ // 3. PR-ref dereference (F4) — appended later, once refs are collected.
623
+ const dirtyChangedPaths = runGit(cwd, 'git diff --name-only HEAD')?.split(/\r?\n/).filter(Boolean) ?? [];
624
+ const mergeBase = runGit(cwd, 'git merge-base HEAD origin/main');
625
+ const branchChangedPaths = mergeBase
626
+ ? (runGit(cwd, `git diff --name-only ${mergeBase} HEAD`)?.split(/\r?\n/).filter(Boolean) ?? [])
627
+ : [];
628
+ const worktreeName = basename(cwd);
629
+ const contextText = readOptionalLocalText(cwd, '.productbrain/context.md');
630
+ const briefingText = readOptionalLocalText(cwd, '.productbrain/briefing.md');
631
+ const callerRefs = Array.isArray(callerSignals.reviewedArtifactRefs) ? callerSignals.reviewedArtifactRefs.join('\n') : undefined;
632
+ // Codex@677: briefing.md (task-specific) is read BEFORE context.md (broad) so
633
+ // its refs are collected first; combined with the per-source cap in
634
+ // collectLocalRefs, a context.md with 25+ IDs can no longer starve the briefing.
635
+ const refs = collectLocalRefs([
636
+ callerSignals.taskText ?? undefined,
637
+ callerSignals.explicitScope ?? undefined,
638
+ callerRefs,
639
+ branchName,
640
+ worktreeName,
641
+ briefingText,
642
+ contextText,
643
+ ]);
644
+ // F4: dereference any "PR #N" ref to its diff paths and fold them into the
645
+ // changedPaths evidence so PR refs contribute real path-domain signal. The
646
+ // server resolver records PR refs as dereferenced:false (no git access), so
647
+ // this is the only place the dereference can happen. Time-boxed + try/catch
648
+ // inside prDiff → absent/unauthed gh is a graceful no-op.
649
+ const prDiffPaths = [];
650
+ if (callerSignals.changedPaths === undefined) {
651
+ let derefed = 0;
652
+ for (const ref of refs) {
653
+ const prNumber = prNumberFromRef(ref);
654
+ if (prNumber === null)
655
+ continue;
656
+ if (derefed >= PR_DIFF_MAX_REFS)
657
+ break;
658
+ derefed++;
659
+ try {
660
+ for (const p of prDiff(cwd, prNumber))
661
+ prDiffPaths.push(p);
662
+ }
663
+ catch {
664
+ // gh absent / unauthed / PR missing — skip gracefully, leave existing behavior.
665
+ }
666
+ }
667
+ }
668
+ // Caller-supplied changedPaths win (backward compat); otherwise union the
669
+ // three local sources, deduped, order-stable.
670
+ const unionedChangedPaths = callerSignals.changedPaths ??
671
+ Array.from(new Set([...dirtyChangedPaths, ...branchChangedPaths, ...prDiffPaths]));
672
+ const baseSignals = sanitizeStartupResolutionSignals({
673
+ ...callerSignals,
674
+ sourceSurface: callerSignals.sourceSurface ?? 'cli',
675
+ resolverVersion: callerSignals.resolverVersion ?? STARTUP_RESOLUTION_RESOLVER_VERSION,
676
+ branchName: callerSignals.branchName ?? branchName,
677
+ worktreeName: callerSignals.worktreeName ?? worktreeName,
678
+ changedPaths: unionedChangedPaths,
679
+ reviewedArtifactRefs: callerSignals.reviewedArtifactRefs ?? refs,
680
+ commitSha: callerSignals.commitSha ?? commitSha,
681
+ });
682
+ const taskText = deriveHandshakeStartupTask(baseSignals.reviewedArtifactRefs ?? [], baseSignals.changedPaths ?? [], baseSignals.taskText);
683
+ const sanitized = sanitizeStartupResolutionSignals({
684
+ ...baseSignals,
685
+ taskText,
686
+ });
687
+ const hasMeaningfulEvidence = Boolean(sanitized.taskText) ||
688
+ (sanitized.changedPaths?.length ?? 0) > 0 ||
689
+ (sanitized.reviewedArtifactRefs?.length ?? 0) > 0 ||
690
+ Boolean(sanitized.explicitScope);
691
+ if (hasMeaningfulEvidence)
692
+ return sanitized;
693
+ // Codex@704: even with no task/path/ref/scope evidence, still emit a minimal
694
+ // envelope carrying the attribution fields (resolverVersion, commitSha,
695
+ // branchName). Returning {} here would drop the whole startup block at the
696
+ // caller, losing attribution AND the loud domain-unresolved signal exactly in
697
+ // the low-evidence case the resolver is meant to make queryable
698
+ // (spec "Loud Unresolved State"; AC #4). The envelope has no evidence fields,
699
+ // so the resolver sees no domain and emits domain-unresolved.
700
+ return sanitizeStartupResolutionSignals({
701
+ sourceSurface: sanitized.sourceSurface ?? 'cli',
702
+ resolverVersion: sanitized.resolverVersion ?? STARTUP_RESOLUTION_RESOLVER_VERSION,
703
+ branchName: sanitized.branchName,
704
+ worktreeName: sanitized.worktreeName,
705
+ commitSha: sanitized.commitSha,
706
+ changeId: sanitized.changeId,
707
+ });
708
+ }
709
+ /**
710
+ * Normalize volatile handshake-only timestamps before comparing generated files.
711
+ * This keeps the visible timestamps in generated artifacts while avoiding
712
+ * meaningless rewrites when semantic content is unchanged.
713
+ */
714
+ export function normalizeHandshakeContentForComparison(content) {
715
+ return content
716
+ .replace(/<!-- auto-generated by pb handshake — [0-9]{4}-[0-9]{2}-[0-9]{2}T[^>]+ -->/g, '<!-- auto-generated by pb handshake — <TIMESTAMP> -->')
717
+ .replace(/^_Generated: [0-9]{4}-[0-9]{2}-[0-9]{2}T.*_$/gm, '_Generated: <TIMESTAMP>_');
197
718
  }
198
719
  function shouldWriteAdapter(filePath, force) {
199
720
  if (force)
@@ -203,6 +724,227 @@ function shouldWriteAdapter(filePath, force) {
203
724
  const content = readFileSync(filePath, 'utf8');
204
725
  return content.includes(MARKER);
205
726
  }
727
+ function normalizeSurfaceName(surface) {
728
+ const stripped = surface.startsWith('.') ? surface.slice(1) : surface;
729
+ const normalized = stripped === 'github' ? 'copilot' : stripped;
730
+ return normalized in SURFACE_REGISTRY ? normalized : null;
731
+ }
732
+ function surfacePerimeterRoots(surface) {
733
+ if (surface === 'codex')
734
+ return ['.codex', SURFACE_REGISTRY.codex.hookFilePath];
735
+ if (surface === 'copilot')
736
+ return ['.github', SURFACE_REGISTRY.copilot.hookFilePath];
737
+ if (surface === 'claude')
738
+ return ['.claude', 'CLAUDE.md'];
739
+ return [`.${surface}`];
740
+ }
741
+ function modeRank(mode) {
742
+ return { off: 0, observe: 1, project: 2, govern: 3 }[mode];
743
+ }
744
+ function normalizeSetupAuthoringBody(body) {
745
+ return body.replace(/\r\n/g, '\n').replace(/\r/g, '\n').trimEnd();
746
+ }
747
+ function setupAuthoringAssetHash(asset) {
748
+ const canonical = {
749
+ entryId: asset.entryId,
750
+ name: asset.name,
751
+ description: asset.description ?? '',
752
+ assetKind: asset.assetKind,
753
+ triggers: asset.triggers ?? [],
754
+ semanticRefs: asset.semanticRefs ?? [],
755
+ body: normalizeSetupAuthoringBody(asset.body),
756
+ };
757
+ return `sha256:${createHash('sha256').update(JSON.stringify(canonical), 'utf8').digest('hex')}`;
758
+ }
759
+ function parseSetupAuthoringFrontmatter(raw) {
760
+ // Shared parser (lib/frontmatter.ts) — array-item quotes are stripped exactly
761
+ // as this function always did; the shared copy makes that behavior canonical
762
+ // for migrate-setup and setup-ingest too. `assetKind` from the union shape is
763
+ // ignored here (handshake derives kind from the directory).
764
+ const parsed = parseSetupFrontmatter(raw);
765
+ return {
766
+ frontmatterId: parsed.frontmatterId,
767
+ name: parsed.name,
768
+ description: parsed.description,
769
+ body: parsed.body,
770
+ triggers: parsed.triggers,
771
+ semanticRefs: parsed.semanticRefs,
772
+ };
773
+ }
774
+ function deriveSetupAuthoringEntryId(filename, kind) {
775
+ const base = basename(filename, '.md');
776
+ const snakeCase = base.toUpperCase().replace(/[^A-Z0-9]+/g, '_');
777
+ return `SETUP-${kind.toUpperCase()}-${snakeCase}`;
778
+ }
779
+ function scanSetupAuthoringFiles(productbrainDir) {
780
+ const dirs = [
781
+ { dir: 'skills', kind: 'skill' },
782
+ { dir: 'rules', kind: 'rule' },
783
+ { dir: 'hooks', kind: 'hook' },
784
+ ];
785
+ const items = [];
786
+ for (const { dir, kind } of dirs) {
787
+ const absDir = join(productbrainDir, dir);
788
+ if (!existsSync(absDir))
789
+ continue;
790
+ for (const file of readdirSync(absDir).filter((f) => f.endsWith('.md'))) {
791
+ const filePath = join(absDir, file);
792
+ const parsed = parseSetupAuthoringFrontmatter(readFileSync(filePath, 'utf8'));
793
+ const fallbackName = basename(file, '.md');
794
+ items.push({
795
+ filePath,
796
+ derivedEntryId: deriveSetupAuthoringEntryId(file, kind),
797
+ frontmatterId: parsed.frontmatterId,
798
+ name: parsed.name || fallbackName,
799
+ description: parsed.description,
800
+ body: parsed.body,
801
+ assetKind: kind,
802
+ triggers: parsed.triggers,
803
+ semanticRefs: parsed.semanticRefs,
804
+ });
805
+ }
806
+ }
807
+ return items.sort((a, b) => a.filePath.localeCompare(b.filePath));
808
+ }
809
+ function setupAuthoringDirForKind(kind) {
810
+ if (kind === 'skill')
811
+ return 'skills';
812
+ if (kind === 'rule')
813
+ return 'rules';
814
+ if (kind === 'hook')
815
+ return 'hooks';
816
+ return null;
817
+ }
818
+ function setupAuthoringFilename(name, entryId) {
819
+ const base = (name || entryId)
820
+ .replace(/[\/\\:*?"<>|]/g, '-')
821
+ .replace(/\s+/g, ' ')
822
+ .trim();
823
+ return `${base || entryId}.md`;
824
+ }
825
+ function setupAuthoringPath(cwd, asset) {
826
+ const dir = setupAuthoringDirForKind(asset.assetKind);
827
+ if (!dir)
828
+ return null;
829
+ // WP-426 E3: recorded authoring source path wins so reprojection lands where the
830
+ // user authored — no duplicate (TEN-1920). Stored relative to .productbrain/.
831
+ if (asset.authoringPath && asset.authoringPath.trim()) {
832
+ // Codex P1/P2: authoringPath comes from the DB and is untrusted at projection
833
+ // time. It is probed via existsSync/readFileSync (and later writeFileSync) in the
834
+ // writeback loop BEFORE any assertSetupWritePath guard runs. Containment alone is
835
+ // not enough — a bad DB row could still point at a directory (e.g. "skills") or an
836
+ // internal PB file (".authoring-sync.json", "manifest.yaml"), which the loop would
837
+ // mis-handle as markdown (throw on a directory read, or overwrite PB state).
838
+ // Constrain to a kind-appropriate markdown file (<dir>/**/*.md); otherwise fall
839
+ // back to the safe name-derived path.
840
+ const pbDir = join(cwd, '.productbrain');
841
+ const candidate = resolve(pbDir, asset.authoringPath);
842
+ const within = relative(pbDir, candidate);
843
+ const withinPosix = within.split(sep).join('/');
844
+ if (!within.startsWith('..') &&
845
+ !isAbsolute(within) &&
846
+ withinPosix.startsWith(`${dir}/`) &&
847
+ withinPosix.toLowerCase().endsWith('.md')) {
848
+ return candidate;
849
+ }
850
+ }
851
+ return join(cwd, '.productbrain', dir, setupAuthoringFilename(asset.name, asset.entryId));
852
+ }
853
+ function renderSetupAuthoringFile(asset) {
854
+ const lines = [
855
+ '---',
856
+ `id: ${asset.entryId}`,
857
+ `name: ${JSON.stringify(asset.name)}`,
858
+ `description: ${JSON.stringify(asset.description ?? '')}`,
859
+ `assetKind: ${asset.assetKind}`,
860
+ ];
861
+ const pushArray = (key, values) => {
862
+ if (!values || values.length === 0)
863
+ return;
864
+ lines.push(`${key}:`);
865
+ for (const value of values)
866
+ lines.push(` - ${JSON.stringify(value)}`);
867
+ };
868
+ pushArray('triggers', asset.triggers);
869
+ pushArray('semanticRefs', asset.semanticRefs);
870
+ lines.push('---', normalizeSetupAuthoringBody(asset.body), '');
871
+ return lines.join('\n');
872
+ }
873
+ function loadAuthoringSyncState(productbrainDir) {
874
+ const statePath = join(productbrainDir, '.authoring-sync.json');
875
+ if (!existsSync(statePath))
876
+ return { version: 1, assets: {} };
877
+ try {
878
+ const parsed = JSON.parse(readFileSync(statePath, 'utf8'));
879
+ if (parsed.version !== 1 || !parsed.assets || typeof parsed.assets !== 'object') {
880
+ return { version: 1, assets: {} };
881
+ }
882
+ // Codex P2: the dormant registries are consumed via .includes() in the dormant loop
883
+ // BEFORE its per-file try/catch, so a malformed .authoring-sync.json (a field set to
884
+ // an object/number instead of an array) would throw a TypeError and abort the whole
885
+ // handshake. Coerce to a string[] (mirrors the assets validation above) to fail open.
886
+ const toStringArray = (v) => Array.isArray(v) ? v.filter((x) => typeof x === 'string') : [];
887
+ return {
888
+ version: 1,
889
+ assets: parsed.assets,
890
+ dormantRenamed: toStringArray(parsed.dormantRenamed),
891
+ dormantReactivated: toStringArray(parsed.dormantReactivated),
892
+ };
893
+ }
894
+ catch {
895
+ return { version: 1, assets: {} };
896
+ }
897
+ }
898
+ function saveAuthoringSyncState(productbrainDir, state) {
899
+ const statePath = join(productbrainDir, '.authoring-sync.json');
900
+ assertSetupWritePath(statePath, { surfaces: [] });
901
+ mkdirSync(productbrainDir, { recursive: true });
902
+ writeFileSync(statePath, JSON.stringify(state, null, 2) + '\n');
903
+ }
904
+ const DRIFT_HASH_TRAILER_REGEX = /^<!--\s*pb-hash:\s*sha256:([0-9a-f]+)\s*-->\s*$/m;
905
+ const DRIFT_HASH_TRAILER_STRIP = /^<!--\s*pb-hash:.*-->\s*$/gm;
906
+ const DRIFT_TIMESTAMP_STRIP = /^<!--\s*pb-generated-at:.*-->\s*$/gm;
907
+ /**
908
+ * Classify a single projection-target file into one of the three drift buckets.
909
+ *
910
+ * Returns `null` when `filePath` does not exist (first-run / unprojected) — the
911
+ * write loop treats that as "would-write" and the file is not part of any bucket.
912
+ *
913
+ * @param filePath Absolute path to the projection file on disk.
914
+ * @returns { bucket, expectedHash, actualHash } when the file exists.
915
+ * `expectedHash`/`actualHash` are populated only for the
916
+ * tampered bucket so the headless refusal payload can include
917
+ * them verbatim. For clean / user-owned, both are `''`.
918
+ */
919
+ export function classifyDriftBucket(filePath) {
920
+ if (!existsSync(filePath))
921
+ return null;
922
+ const content = readFileSync(filePath, 'utf8');
923
+ // No auto-gen MARKER → user-owned. Never touch.
924
+ if (!content.includes(MARKER)) {
925
+ return { bucket: 'user-owned', expectedHash: '', actualHash: '' };
926
+ }
927
+ // Marker present but no hash trailer → legacy / pre-S0c projection: treat as
928
+ // clean (the hash trailer was added in WP-345 S0c). The user-owned-vs-clean
929
+ // semantic falls back to existing shouldWriteAdapter behavior.
930
+ const trailerMatch = content.match(DRIFT_HASH_TRAILER_REGEX);
931
+ if (!trailerMatch) {
932
+ return { bucket: 'pb-managed-clean', expectedHash: '', actualHash: '' };
933
+ }
934
+ const expectedHash = `sha256:${trailerMatch[1]}`;
935
+ // Recompute the actual hash from the body (strip trailer + timestamp, LF, trim).
936
+ const normalized = content
937
+ .replace(DRIFT_HASH_TRAILER_STRIP, '')
938
+ .replace(DRIFT_TIMESTAMP_STRIP, '')
939
+ .replace(/\r\n/g, '\n')
940
+ .replace(/\r/g, '\n')
941
+ .trimEnd();
942
+ const actualHash = `sha256:${createHash('sha256').update(normalized, 'utf8').digest('hex')}`;
943
+ if (actualHash === expectedHash) {
944
+ return { bucket: 'pb-managed-clean', expectedHash, actualHash };
945
+ }
946
+ return { bucket: 'pb-managed-tampered', expectedHash, actualHash };
947
+ }
206
948
  function deduplicateEntries(entries) {
207
949
  const seen = new Set();
208
950
  const result = [];
@@ -215,15 +957,178 @@ function deduplicateEntries(entries) {
215
957
  }
216
958
  return result;
217
959
  }
960
+ /**
961
+ * resolveProjectionCollision — WP-379 S5b
962
+ *
963
+ * Marker-scoped orphan unlink: enumerates target dirs (.cursor/rules/,
964
+ * .claude/rules/, .claude/skills/, .codex/skills/); for each file that has
965
+ * the auto-gen MARKER whose lowercase-normalized filename does NOT match any
966
+ * current active-asset materializedFilename, the file is unlinked.
967
+ *
968
+ * User-owned files (no MARKER) are never touched, regardless of name.
969
+ *
970
+ * Linux case-collision disambiguation:
971
+ * 1. Exact match (lowercase name == any canonical name): survives.
972
+ * 2. Case-variant with MARKER (marker file, no exact canonical match): unlinked.
973
+ * 3. Ambiguous (zero exact, multiple case-variants with MARKER):
974
+ * newest mtime wins; all others are unlinked; a collision TEN is
975
+ * appended to the session capture queue (not fired inline).
976
+ *
977
+ * Returns a list of unlink results so the caller can log/report them.
978
+ *
979
+ * @param cwd Project root (absolute path).
980
+ * @param assetNames The current set of canonical asset names from the server
981
+ * (e.g. ["Setup-ProductBrain", "chain-rules"]).
982
+ * @param log Progress log function.
983
+ * @param logErr Error log function.
984
+ */
985
+ export function resolveProjectionCollision(cwd, assetNames, log, logErr) {
986
+ // Target directories by extension suffix.
987
+ const TARGET_DIRS_BY_EXT = [
988
+ { dir: join(cwd, '.cursor', 'rules'), ext: '.mdc' },
989
+ { dir: join(cwd, '.claude', 'rules'), ext: '.md' },
990
+ { dir: join(cwd, '.claude', 'skills'), ext: '.md' },
991
+ { dir: join(cwd, '.codex', 'skills'), ext: '.md' },
992
+ ];
993
+ // Build a set of normalized canonical names (without extension) for fast lookup.
994
+ // We normalize all asset names to detect case-variant collisions.
995
+ // For each asset name we derive the normalized basename (the part before the ext).
996
+ // canonicalNormalizedNames: Set<normalized-stem> (lowercase + slug).
997
+ const canonicalNormalizedStems = new Set(assetNames.map((n) => normalizeMaterializedFilename(n)));
998
+ const results = [];
999
+ const collisionTens = [];
1000
+ for (const { dir, ext } of TARGET_DIRS_BY_EXT) {
1001
+ if (!existsSync(dir))
1002
+ continue;
1003
+ let files;
1004
+ try {
1005
+ files = readdirSync(dir);
1006
+ }
1007
+ catch {
1008
+ continue; // unreadable dir — skip
1009
+ }
1010
+ // Group files by their normalized stem.
1011
+ // normalizedStem → [ { filename, fullPath } ]
1012
+ const groups = new Map();
1013
+ for (const filename of files) {
1014
+ if (!filename.endsWith(ext))
1015
+ continue;
1016
+ const fullPath = join(dir, filename);
1017
+ // Only operate on files that have the auto-gen MARKER.
1018
+ let content;
1019
+ try {
1020
+ content = readFileSync(fullPath, 'utf8');
1021
+ }
1022
+ catch {
1023
+ continue; // unreadable file — skip
1024
+ }
1025
+ if (!content.includes(MARKER))
1026
+ continue; // user-owned — never touch
1027
+ const stem = basename(filename, ext);
1028
+ const normalizedStem = normalizeMaterializedFilename(stem);
1029
+ const group = groups.get(normalizedStem) ?? [];
1030
+ group.push({ filename, fullPath });
1031
+ groups.set(normalizedStem, group);
1032
+ }
1033
+ // Evaluate each normalized stem group.
1034
+ for (const [normalizedStem, members] of groups) {
1035
+ const isKnownCanonical = canonicalNormalizedStems.has(normalizedStem);
1036
+ if (!isKnownCanonical) {
1037
+ // All members of this group are orphans (no canonical asset with this stem).
1038
+ // Unlink them all — they're stale projections of an asset no longer in the server.
1039
+ for (const { filename, fullPath } of members) {
1040
+ try {
1041
+ unlinkSync(fullPath);
1042
+ log(`Orphan unlinked: ${fullPath}`);
1043
+ results.push({ action: 'unlinked', filePath: fullPath, reason: 'orphan-no-canonical-match' });
1044
+ }
1045
+ catch (err) {
1046
+ logErr(`Warning: could not unlink orphan ${fullPath} — ${err instanceof Error ? err.message : String(err)}`);
1047
+ results.push({ action: 'kept', filePath: fullPath, reason: 'unlink-failed' });
1048
+ }
1049
+ }
1050
+ continue;
1051
+ }
1052
+ // The stem IS known canonical. Check for case-collision.
1053
+ if (members.length === 1) {
1054
+ // Single file — no collision.
1055
+ results.push({ action: 'kept', filePath: members[0].fullPath, reason: 'canonical-exact' });
1056
+ continue;
1057
+ }
1058
+ // Multiple files with the same normalized stem → case-collision.
1059
+ // Rule: exact match (filename stem === normalized stem, i.e. already lowercase) wins.
1060
+ const exactMatches = members.filter(({ filename }) => {
1061
+ const stem = basename(filename, ext);
1062
+ return stem === normalizedStem; // lowercase-equal means already normalized
1063
+ });
1064
+ if (exactMatches.length === 1) {
1065
+ // Rule 1: exactly one exact match → keep it, unlink all case-variants.
1066
+ const keeper = exactMatches[0];
1067
+ results.push({ action: 'kept', filePath: keeper.fullPath, reason: 'case-exact-match-wins' });
1068
+ for (const member of members) {
1069
+ if (member.fullPath === keeper.fullPath)
1070
+ continue;
1071
+ try {
1072
+ unlinkSync(member.fullPath);
1073
+ log(`Case-variant unlinked: ${member.fullPath} (kept: ${keeper.filename})`);
1074
+ results.push({ action: 'unlinked', filePath: member.fullPath, reason: 'case-variant-unlinked' });
1075
+ }
1076
+ catch (err) {
1077
+ logErr(`Warning: could not unlink case-variant ${member.fullPath} — ${err instanceof Error ? err.message : String(err)}`);
1078
+ results.push({ action: 'kept', filePath: member.fullPath, reason: 'unlink-failed' });
1079
+ }
1080
+ }
1081
+ continue;
1082
+ }
1083
+ // Rule 3: ambiguous — zero exact matches (or multiple exact matches, which
1084
+ // can't happen on a case-sensitive FS). Newest mtime wins.
1085
+ // Sort by mtime descending: highest mtime = newest = winner.
1086
+ const withStats = members.map(({ filename, fullPath }) => {
1087
+ try {
1088
+ const { mtimeMs } = statSync(fullPath);
1089
+ return { filename, fullPath, mtimeMs };
1090
+ }
1091
+ catch {
1092
+ return { filename, fullPath, mtimeMs: 0 };
1093
+ }
1094
+ });
1095
+ withStats.sort((a, b) => b.mtimeMs - a.mtimeMs);
1096
+ const winner = withStats[0];
1097
+ log(`Case-collision ambiguous for ${normalizedStem}${ext}: newest mtime wins (${winner.filename})`);
1098
+ results.push({ action: 'collision-ten', filePath: winner.fullPath, reason: 'ambiguous-newest-mtime-wins' });
1099
+ const tenMsg = `Handshake case-collision: ambiguous filename for stem "${normalizedStem}${ext}" ` +
1100
+ `(${members.map((m) => m.filename).join(', ')}). ` +
1101
+ `Kept newest: ${winner.filename}. Consider renaming to ${normalizedStem}${ext}.`;
1102
+ collisionTens.push(tenMsg);
1103
+ for (const member of withStats.slice(1)) {
1104
+ try {
1105
+ unlinkSync(member.fullPath);
1106
+ log(`Case-variant (ambiguous) unlinked: ${member.fullPath}`);
1107
+ results.push({ action: 'unlinked', filePath: member.fullPath, reason: 'ambiguous-case-variant-unlinked' });
1108
+ }
1109
+ catch (err) {
1110
+ logErr(`Warning: could not unlink ambiguous case-variant ${member.fullPath} — ${err instanceof Error ? err.message : String(err)}`);
1111
+ results.push({ action: 'kept', filePath: member.fullPath, reason: 'unlink-failed' });
1112
+ }
1113
+ }
1114
+ }
1115
+ }
1116
+ return { results, collisionTens };
1117
+ }
218
1118
  export async function runHandshake(options = {}) {
219
- const config = await getConfigOrGuide(() => runHandshake(options));
1119
+ const config = await getConfigOrGuide(async () => { await runHandshake(options); });
220
1120
  if (!config)
221
- return;
1121
+ return undefined;
222
1122
  const cwd = process.cwd();
223
1123
  const force = options.force ?? false;
224
1124
  const dryRun = options.dryRun ?? false;
1125
+ // Preview mode: default when neither --apply nor --dry-run is passed.
1126
+ // --dry-run is kept as a backward-compat alias for preview (same behavior).
1127
+ const preview = !options.apply && !dryRun;
1128
+ const applyMode = options.apply === true && !dryRun;
225
1129
  const level = options.level;
226
1130
  const quiet = options.quiet ?? false;
1131
+ const generate = options.generate ?? false;
227
1132
  const timestamp = new Date().toISOString();
228
1133
  // Helper: emit progress line only when not quiet (used when handshake is a sub-step)
229
1134
  const log = (msg) => { if (!quiet)
@@ -233,8 +1138,11 @@ export async function runHandshake(options = {}) {
233
1138
  // Validate --level if provided (for handshake content filtering, not --init trust level)
234
1139
  const VALID_HANDSHAKE_LEVELS = ['beginner', 'intermediate', 'expert'];
235
1140
  if (level && !VALID_HANDSHAKE_LEVELS.includes(level)) {
236
- process.stderr.write(`Unknown level "${level}". Valid levels: ${VALID_HANDSHAKE_LEVELS.join(', ')}\n`);
237
- process.exit(1);
1141
+ throw new CLIError(`Unknown level "${level}".`, {
1142
+ code: ErrorCode.VALIDATION_FAILED,
1143
+ category: 'validation',
1144
+ guidance: `Valid levels: ${VALID_HANDSHAKE_LEVELS.join(', ')}`,
1145
+ });
238
1146
  }
239
1147
  // 1. Detect repo
240
1148
  const repo = detectRepo(cwd);
@@ -242,16 +1150,39 @@ export async function runHandshake(options = {}) {
242
1150
  if (repo.detectedStack.length > 0) {
243
1151
  log(`Stack: ${repo.detectedStack.join(', ')}`);
244
1152
  }
245
- // 2. Fetch orient view
1153
+ const startupSignals = deriveHandshakeStartupSignals(cwd, options.startupSignals ?? {});
1154
+ const orientArgs = { tier: 'standard' };
1155
+ if (options.invocationPath)
1156
+ orientArgs.invocationPath = options.invocationPath;
1157
+ if (Object.keys(startupSignals).length > 0) {
1158
+ orientArgs.startupSignals = startupSignals;
1159
+ if (startupSignals.taskText)
1160
+ orientArgs.task = startupSignals.taskText;
1161
+ }
1162
+ // 2. Fetch orient view + workspace readiness in parallel (budget max +200ms added latency)
246
1163
  log('Fetching workspace context...');
247
1164
  let orientView = null;
1165
+ let workspaceProfile = null;
248
1166
  try {
249
- orientView = await mcpCall('chain.getOrientView', {});
1167
+ const workspaceReadinessPromise = kernelCall('chain.workspaceReadiness', {}).catch(() => null);
1168
+ const [orientResult, readinessRaw] = await Promise.all([
1169
+ kernelCall('chain.getOrientView', orientArgs).catch((err) => {
1170
+ logErr(`Warning: could not fetch workspace context — ${err instanceof Error ? err.message : err}`);
1171
+ logErr('Continuing with limited context (Chain search + portable knowledge only).');
1172
+ return null;
1173
+ }),
1174
+ workspaceReadinessPromise,
1175
+ ]);
1176
+ orientView = orientResult;
1177
+ workspaceProfile = extractWorkspaceProfile(readinessRaw);
250
1178
  }
251
1179
  catch (err) {
252
1180
  logErr(`Warning: could not fetch workspace context — ${err instanceof Error ? err.message : err}`);
253
1181
  logErr('Continuing with limited context (Chain search + portable knowledge only).');
254
1182
  }
1183
+ if (workspaceProfile) {
1184
+ log(`Workspace profile: stage=${workspaceProfile.stage}, entries=${workspaceProfile.totalEntries}, governance=${workspaceProfile.governanceMode}`);
1185
+ }
255
1186
  // 3. Build search queries from repo context
256
1187
  const searchQueries = [];
257
1188
  if (repo.name && repo.name.length >= 2)
@@ -269,76 +1200,718 @@ export async function runHandshake(options = {}) {
269
1200
  let matchedEntries = [];
270
1201
  if (uniqueQueries.length > 0) {
271
1202
  log(`Searching Chain for: ${uniqueQueries.join(', ')}...`);
272
- const searchResults = await Promise.all(uniqueQueries.map((q) => mcpCall('chain.searchEntries', { query: q }).catch(() => [])));
1203
+ const searchResults = await Promise.all(uniqueQueries.map((q) => kernelCall('chain.searchEntries', { query: q }).catch(() => [])));
273
1204
  matchedEntries = deduplicateEntries(searchResults.flat());
274
1205
  }
275
- // 5. Read canonical skills & rules from .productbrain/
1206
+ // 5. Read canonical skills & rules DB-first (WP-345 S0c, TEN-1459), FS fallback.
1207
+ // Primary: query setup.listAssetsForUser from DB (workspace SSOT).
1208
+ // Fallback: read from .productbrain/ filesystem (legacy — used when DB is empty or unavailable).
276
1209
  const pbDir = join(cwd, '.productbrain');
277
- const allSkills = readCanonicalSkills(pbDir);
278
- const allRules = readCanonicalRules(pbDir);
279
- // Apply level filtering (after read, before target filtering in write loop)
280
- const canonicalSkills = filterByLevel(allSkills, level);
281
- const canonicalRules = filterByLevel(allRules, level);
1210
+ const manifestStatus = readManifestStatus(pbDir);
1211
+ const manifest = manifestStatus.manifest;
1212
+ const surfaceValidation = validateSurfacesForMode(manifestStatus.mode, manifestStatus.surfaces);
1213
+ if (applyMode && surfaceValidation.error === SURFACE_GOVERN_NO_SURFACES) {
1214
+ throw new CLIError('materialize: govern requires at least one registered manifest surface.', {
1215
+ code: ErrorCode.VALIDATION_FAILED,
1216
+ category: 'validation',
1217
+ guidance: 'Add surfaces such as `.cursor` or `.claude` to .productbrain/manifest.yaml.',
1218
+ });
1219
+ }
1220
+ for (const surface of surfaceValidation.unregisteredSurfaces) {
1221
+ logErr(`Warning: manifest surface "${surface}" is not registered; skipping it.`);
1222
+ }
1223
+ const manifestTargets = new Set(surfaceValidation.registeredSurfaces);
1224
+ const cliTargets = new Set();
1225
+ const ignoredCliSurfaces = [];
1226
+ for (const surface of options.surfaces ?? []) {
1227
+ const normalized = normalizeSurfaceName(surface);
1228
+ if (normalized && manifestTargets.has(normalized)) {
1229
+ cliTargets.add(normalized);
1230
+ }
1231
+ else {
1232
+ ignoredCliSurfaces.push(surface);
1233
+ }
1234
+ }
1235
+ if (ignoredCliSurfaces.length > 0) {
1236
+ logErr(`Warning: --surfaces ignored outside manifest.surfaces: ${ignoredCliSurfaces.join(', ')}`);
1237
+ }
1238
+ const allowedTargets = options.surfaces && options.surfaces.length > 0
1239
+ ? cliTargets
1240
+ : manifestTargets;
1241
+ const perimeterManifest = {
1242
+ surfaces: [...manifestTargets].flatMap(surfacePerimeterRoots),
1243
+ };
1244
+ const authorityCanWrite = manifestStatus.mode === 'project' || manifestStatus.mode === 'govern';
1245
+ const authorityPreviewOnly = applyMode && !authorityCanWrite;
1246
+ const writeMode = applyMode && authorityCanWrite;
1247
+ let dbSkills = [];
1248
+ let dbRules = [];
1249
+ let usedDbSource = false;
1250
+ let dbAssetRows = [];
1251
+ // WP-379 S4: dormant assets (gate-failed) — their on-disk files get the dormant marker.
1252
+ let dormantDbAssetRows = [];
1253
+ // WP-428 S2 (Finding #5): tracks entryIds whose body fetch from storage failed.
1254
+ // Hoisted here (same scope as dbAssetRows) so both the skills/rules projection loop
1255
+ // AND the authoring-file projection loop can skip failed assets.
1256
+ const bodyFetchFailedEntryIds = new Set();
1257
+ const dbProjectionHashes = new Map();
1258
+ const syncDriftTensToFire = [];
1259
+ const deferredAuthoringBaselineEntryIds = new Set();
1260
+ if (writeMode) {
1261
+ const authoringItems = scanSetupAuthoringFiles(pbDir);
1262
+ if (authoringItems.length > 0) {
1263
+ // WP-428 S2 (Critical #1): ingestSetupAssetsBatch is an internalMutation — it cannot
1264
+ // call ctx.storage.store(). We now call ingestSetupAssetWithBody (action) per-asset so
1265
+ // each asset gets bodyStorageId written. This loses intra-batch collision detection;
1266
+ // canonical-id collision check is now client-side before dispatch.
1267
+ //
1268
+ // Client-side collision detection (replaces server-side DEC-954 check in the batch mutation):
1269
+ const idToPaths = new Map();
1270
+ for (const item of authoringItems) {
1271
+ const canonicalId = item.frontmatterId?.trim() || item.derivedEntryId;
1272
+ const paths = idToPaths.get(canonicalId) ?? [];
1273
+ paths.push(item.filePath);
1274
+ idToPaths.set(canonicalId, paths);
1275
+ }
1276
+ const conflictingPaths = new Set();
1277
+ for (const [, paths] of idToPaths) {
1278
+ if (paths.length > 1) {
1279
+ for (const p of paths)
1280
+ conflictingPaths.add(p);
1281
+ }
1282
+ }
1283
+ if (conflictingPaths.size > 0) {
1284
+ logErr(`Setup authoring import partial: ${conflictingPaths.size} file(s) refused for duplicate setup id. ` +
1285
+ [...conflictingPaths].join(', '));
1286
+ }
1287
+ const itemsToIngest = authoringItems.filter((item) => !conflictingPaths.has(item.filePath));
1288
+ if (itemsToIngest.length > 0) {
1289
+ try {
1290
+ // Parallel uploads — concurrency limit 10 to avoid overwhelming the gateway.
1291
+ const CONCURRENCY = 10;
1292
+ const ingestResults = [];
1293
+ for (let i = 0; i < itemsToIngest.length; i += CONCURRENCY) {
1294
+ const batch = itemsToIngest.slice(i, i + CONCURRENCY);
1295
+ const settled = await Promise.allSettled(batch.map(async (item) => {
1296
+ const result = await kernelCall('setup.ingestSetupAssetWithBody', {
1297
+ entryId: item.derivedEntryId,
1298
+ frontmatterId: item.frontmatterId,
1299
+ name: item.name,
1300
+ description: item.description,
1301
+ body: item.body,
1302
+ assetKind: item.assetKind,
1303
+ triggers: item.triggers,
1304
+ semanticRefs: item.semanticRefs,
1305
+ authoringPath: relative(pbDir, item.filePath).split(sep).join('/'),
1306
+ });
1307
+ return { filePath: item.filePath, ...result };
1308
+ }));
1309
+ for (const r of settled) {
1310
+ if (r.status === 'fulfilled') {
1311
+ ingestResults.push(r.value);
1312
+ if (r.value.conflict === 'repo-wins') {
1313
+ syncDriftTensToFire.push(`Repo authoring file won setup sync conflict for ${r.value.entryId} at ${r.value.filePath}.`);
1314
+ logErr(`Warning: repo authoring file won sync conflict for ${r.value.entryId}; DB edit was overwritten.`);
1315
+ }
1316
+ }
1317
+ else {
1318
+ trackEvent('setup.authoring_import.item_failed', { error: r.reason instanceof Error ? r.reason.message : String(r.reason) });
1319
+ logErr(`Warning: setup authoring import failed for one asset — ${r.reason instanceof Error ? r.reason.message : String(r.reason)}`);
1320
+ }
1321
+ }
1322
+ }
1323
+ if (ingestResults.length > 0) {
1324
+ log(`Setup authoring import: ${ingestResults.length} file(s) checked (with bodyStorageId).`);
1325
+ }
1326
+ }
1327
+ catch (err) {
1328
+ trackEvent('setup.authoring_import.failed', { error: err instanceof Error ? err.message : String(err) });
1329
+ logErr(`Warning: setup authoring import failed — ${err instanceof Error ? err.message : String(err)}`);
1330
+ }
1331
+ }
1332
+ }
1333
+ }
1334
+ try {
1335
+ // WP-379 S4: listAssetsForUser now returns { activeAssets, dormantAssets }.
1336
+ // Wire format changed from DbAsset[] to { activeAssets: DbAsset[], dormantAssets: DbAsset[] }.
1337
+ // Fall back to empty arrays if the server returns the old flat-array shape (graceful degradation).
1338
+ const rawResponse = await kernelCall('setup.listAssetsForUser', {}).catch(() => null);
1339
+ let dbAssets = [];
1340
+ if (rawResponse !== null) {
1341
+ if (Array.isArray(rawResponse)) {
1342
+ // Pre-S4 server — treat entire response as active assets with no dormant list.
1343
+ dbAssets = rawResponse;
1344
+ dormantDbAssetRows = [];
1345
+ }
1346
+ else {
1347
+ dbAssets = rawResponse.activeAssets ?? [];
1348
+ dormantDbAssetRows = rawResponse.dormantAssets ?? [];
1349
+ }
1350
+ }
1351
+ if (dbAssets.length > 0) {
1352
+ dbAssetRows = dbAssets;
1353
+ // WP-428 S2: body is no longer inline — fetch from storage per-asset when bodyStorageId is set.
1354
+ // Projectable assets: non-disabled skill/rule/hook entries. Fetch bodies in parallel (bounded).
1355
+ const projectableAssets = dbAssets.filter((a) => !a.disabledByOwner && (a.assetKind === 'skill' || a.assetKind === 'rule' || a.assetKind === 'hook'));
1356
+ const assetsNeedingBodyFetch = projectableAssets.filter((a) => a.bodyStorageId);
1357
+ const bodyFetchMap = new Map(); // entryId → body
1358
+ // WP-428 S2 (Finding #5/#12): track fetch-failed assets so we can skip their projection.
1359
+ // Failed entryIds are excluded from dbSkills/dbRules — no empty body written, no lastProjectedHash update.
1360
+ // bodyFetchFailedEntryIds is declared at outer scope (also used by authoring-file projection loop).
1361
+ if (assetsNeedingBodyFetch.length > 0) {
1362
+ log(`Fetching ${assetsNeedingBodyFetch.length} asset body(s) from storage...`);
1363
+ const bodyFetchResults = await Promise.allSettled(assetsNeedingBodyFetch.map(async (asset) => {
1364
+ const result = await kernelCall('setup.fetchAssetBody', { bodyStorageId: asset.bodyStorageId });
1365
+ return { entryId: asset.entryId, name: asset.name, body: result.body };
1366
+ }));
1367
+ for (let i = 0; i < bodyFetchResults.length; i++) {
1368
+ const result = bodyFetchResults[i];
1369
+ if (result.status === 'fulfilled') {
1370
+ bodyFetchMap.set(result.value.entryId, result.value.body);
1371
+ }
1372
+ else {
1373
+ // WP-428 S2 (Finding #12): include entryId and name in the warning (Finding #5: skip projection).
1374
+ const asset = assetsNeedingBodyFetch[i];
1375
+ logErr(`Warning: failed to fetch body for ${asset.entryId} (${asset.name}) — ${result.reason instanceof Error ? result.reason.message : String(result.reason)}`);
1376
+ bodyFetchFailedEntryIds.add(asset.entryId);
1377
+ }
1378
+ }
1379
+ if (bodyFetchFailedEntryIds.size > 0) {
1380
+ // WP-439 S4: strict-by-default. Any body-fetch failure is a hard
1381
+ // failure unless --lenient is passed. The strict path surfaces an
1382
+ // actionable pointer at the audit/repair command operators can run
1383
+ // to diagnose and fix orphaned setup-asset rows.
1384
+ const failedList = [...bodyFetchFailedEntryIds].join(', ');
1385
+ if (options.lenient) {
1386
+ logErr(`Warning: ${bodyFetchFailedEntryIds.size} asset(s) skipped due to body fetch failure (--lenient): ${failedList}`);
1387
+ }
1388
+ else {
1389
+ throw new CLIError(`${bodyFetchFailedEntryIds.size} asset(s) failed body fetch: ${failedList}. ` +
1390
+ `Run \`pb setup-audit\` to diagnose, then \`pb setup-audit --repair\` to reseed. ` +
1391
+ `Pass --lenient to suppress this and continue with affected entries skipped (legacy behaviour).`, { code: ErrorCode.INTERNAL, category: 'internal' });
1392
+ }
1393
+ }
1394
+ }
1395
+ // Map DB assets to CanonicalSkill/CanonicalRule shapes
1396
+ for (const asset of dbAssets) {
1397
+ if (asset.disabledByOwner)
1398
+ continue;
1399
+ // WP-428 S2 (Finding #5): skip projection for assets whose body fetch failed.
1400
+ // Do NOT write empty body to disk and do NOT update lastProjectedHash.
1401
+ if (bodyFetchFailedEntryIds.has(asset.entryId))
1402
+ continue;
1403
+ // WP-428 S2: resolve body — prefer storage-fetched body, fall back to inline body (pre-S2 servers).
1404
+ // Fallback: pre-S2 servers (no bodyStorageId) return inline body. Once all servers are S2+, this can be deleted.
1405
+ const resolvedBody = bodyFetchMap.get(asset.entryId) ?? asset.body ?? '';
1406
+ if (asset.assetKind === 'skill') {
1407
+ dbSkills.push({
1408
+ name: asset.name,
1409
+ description: asset.description,
1410
+ triggers: asset.triggers ?? [],
1411
+ body: resolvedBody,
1412
+ sourcePath: `db:${asset.entryId}`,
1413
+ });
1414
+ }
1415
+ else if (asset.assetKind === 'rule' || asset.assetKind === 'hook') {
1416
+ dbRules.push({
1417
+ name: asset.name,
1418
+ description: asset.description,
1419
+ autoApply: false,
1420
+ body: resolvedBody,
1421
+ sourcePath: `db:${asset.entryId}`,
1422
+ });
1423
+ }
1424
+ }
1425
+ usedDbSource = true;
1426
+ log(`Setup assets: ${dbSkills.length} skills, ${dbRules.length} rules/hooks from DB (WP-345 DB-first path)`);
1427
+ if (dormantDbAssetRows.length > 0) {
1428
+ log(`Setup assets: ${dormantDbAssetRows.length} dormant (gate-filtered) asset(s) will be marked on disk`);
1429
+ }
1430
+ }
1431
+ }
1432
+ catch {
1433
+ // DB source unavailable — silently fall through to FS path
1434
+ }
1435
+ const allSkills = usedDbSource ? dbSkills : readCanonicalSkills(pbDir);
1436
+ const manualRules = usedDbSource ? dbRules : readCanonicalRules(pbDir);
1437
+ if (!usedDbSource) {
1438
+ log('Setup assets: reading from filesystem (DB source unavailable or empty)');
1439
+ }
1440
+ // 5a-pre. E4: Resolve semantic refs in DB assets (WP-345, DEC-A, DEC-C, DEC-K)
1441
+ // For each asset with semanticRefs[], resolve them via the Convex resolver and
1442
+ // replace {{ref:key}} placeholders in the body. Runs in apply mode only (not preview).
1443
+ // NG11: PostHog events fire from CLI side only (never inside Convex mutations).
1444
+ if (usedDbSource && writeMode) {
1445
+ const projectableDbAssets = dbAssetRows.filter((a) => !a.disabledByOwner && (a.assetKind === 'skill' || a.assetKind === 'rule' || a.assetKind === 'hook'));
1446
+ const assetsWithRefs = projectableDbAssets.filter((a) => a.semanticRefs && a.semanticRefs.length > 0);
1447
+ if (assetsWithRefs.length > 0) {
1448
+ log(`Resolving semantic refs for ${assetsWithRefs.length} asset(s)...`);
1449
+ // Collect unique ref keys across all assets
1450
+ const allRefKeys = [...new Set(assetsWithRefs.flatMap((a) => a.semanticRefs))];
1451
+ // Resolve all refs in a single batch call. Shape: SetupRefResolution[]
1452
+ // (DEC-767 / WP-354 Build-Order #6 — kind + status discriminator).
1453
+ let resolvedRefs = [];
1454
+ try {
1455
+ resolvedRefs = await kernelCall('setup.resolveSemanticRefs', { semanticRefs: allRefKeys });
1456
+ }
1457
+ catch (err) {
1458
+ trackEvent('setup.refs.resolve_failed', { error: err instanceof Error ? err.message : String(err) });
1459
+ logErr(`Warning: could not resolve semantic refs — ${err instanceof Error ? err.message : String(err)}`);
1460
+ }
1461
+ // Build resolved map: canonicalKey → display name. Only required refs
1462
+ // count as unresolved warnings; seed/unknown refs are not gates.
1463
+ const resolvedMap = new Map();
1464
+ let unresolvedCount = 0;
1465
+ for (const result of resolvedRefs) {
1466
+ if (result.status === 'resolved' && result.localEntryId) {
1467
+ resolvedMap.set(result.ref, result.localEntryId);
1468
+ trackEvent('skill.ref.resolved', { ref: result.ref, kind: result.kind });
1469
+ }
1470
+ else if (result.status === 'unsupported-future') {
1471
+ // seed: refs are explicitly future scope per WP-354 Build-Order #6 — not a warning.
1472
+ trackEvent('skill.ref.future', { ref: result.ref, kind: result.kind });
1473
+ }
1474
+ else if (result.required) {
1475
+ unresolvedCount++;
1476
+ trackEvent('skill.ref.unresolved', { ref: result.ref, kind: result.kind, status: result.status });
1477
+ }
1478
+ }
1479
+ if (unresolvedCount > 0) {
1480
+ logErr(`Warning: ${unresolvedCount} required semantic ref(s) could not be resolved.`);
1481
+ }
1482
+ // Projection must preserve ref tokens as portable machine-readable refs.
1483
+ // resolvedMap is only used for validation/telemetry here; generated setup
1484
+ // artifacts keep {{ref:domain:...}} / {{ref:entry:...}} intact.
1485
+ }
1486
+ // Compute projection hash for each projected DB asset (DEC-K).
1487
+ // The stored hash is sha256 of the normalized resolved asset body; DB persistence
1488
+ // is deferred until the write loop confirms a matching file was emitted.
1489
+ // Strip existing hash trailer and timestamp lines, normalize to LF, then hash.
1490
+ const HASH_TRAILER_REGEX = /^<!--\s*pb-hash:.*-->\s*$/gm;
1491
+ const TIMESTAMP_REGEX = /^<!--\s*pb-generated-at:.*-->\s*$/gm;
1492
+ for (const rawAsset of projectableDbAssets) {
1493
+ const resolvedAsset = [...dbSkills, ...dbRules].find((a) => a.sourcePath === `db:${rawAsset.entryId}`);
1494
+ if (!resolvedAsset)
1495
+ continue;
1496
+ try {
1497
+ // Build the projected body (what will be written to disk)
1498
+ const projectedBody = resolvedAsset.body;
1499
+ // Normalize: strip existing hash/timestamp trailers, convert to LF
1500
+ const normalized = projectedBody
1501
+ .replace(HASH_TRAILER_REGEX, '')
1502
+ .replace(TIMESTAMP_REGEX, '')
1503
+ .replace(/\r\n/g, '\n')
1504
+ .replace(/\r/g, '\n')
1505
+ .trimEnd();
1506
+ // Compute sha256 hash
1507
+ const hash = createHash('sha256').update(normalized, 'utf8').digest('hex');
1508
+ const hashTrailer = `<!-- pb-hash: sha256:${hash} -->`;
1509
+ // Append hash trailer to the projected body
1510
+ resolvedAsset.body = `${normalized}\n${hashTrailer}`;
1511
+ dbProjectionHashes.set(rawAsset.entryId, {
1512
+ hash: `sha256:${hash}`,
1513
+ assetKind: rawAsset.assetKind,
1514
+ });
1515
+ // Drift detection: compare against last known hash
1516
+ if (rawAsset.lastProjectedHash && rawAsset.lastProjectedHash !== `sha256:${hash}`) {
1517
+ trackEvent('skill.drift.detected', {
1518
+ entryId: rawAsset.entryId,
1519
+ assetKind: rawAsset.assetKind,
1520
+ });
1521
+ log(`Drift detected for asset ${rawAsset.entryId} — projecting updated version.`);
1522
+ }
1523
+ trackEvent('skill.projection.succeeded', {
1524
+ entryId: rawAsset.entryId,
1525
+ assetKind: rawAsset.assetKind,
1526
+ });
1527
+ }
1528
+ catch (err) {
1529
+ trackEvent('skill.projection.failed', {
1530
+ entryId: rawAsset.entryId,
1531
+ assetKind: rawAsset.assetKind,
1532
+ });
1533
+ logErr(`Warning: projection failed for ${rawAsset.entryId} — ${err instanceof Error ? err.message : String(err)}`);
1534
+ }
1535
+ }
1536
+ }
1537
+ // 5a. Optionally fetch and merge Chain-derived rules (--generate flag)
1538
+ let chainRulesStats = null;
1539
+ let chainGaps = [];
1540
+ let allRules = manualRules;
1541
+ if (generate) {
1542
+ log('Generating Chain-derived rules...');
1543
+ const chainResult = await generateChainRules(kernelCall, manualRules);
1544
+ if (chainResult.sentinel) {
1545
+ // MCP unavailable — inject sentinel rule and warn
1546
+ allRules = [...manualRules, chainResult.sentinel];
1547
+ logErr('Warning: Chain MCP unavailable — generated rules are disabled. Sentinel rule injected.');
1548
+ }
1549
+ else {
1550
+ // Merge generated rules after manual rules (manual takes precedence on dedup)
1551
+ allRules = [...manualRules, ...chainResult.rules];
1552
+ chainRulesStats = chainResult.stats;
1553
+ chainGaps = chainResult.gaps;
1554
+ const { generatedRules, suppressedByManual, suppressedByZeroEntries } = chainResult.stats;
1555
+ log(`Chain-derived rules: ${generatedRules} generated, ${suppressedByManual} suppressed by manual, ${suppressedByZeroEntries} gaps`);
1556
+ if (chainGaps.length > 0) {
1557
+ log(`Gaps (no matching governance entries): ${chainGaps.join(', ')}`);
1558
+ }
1559
+ // Diff: compare current state against previous run
1560
+ const previousState = loadPreviousState(pbDir);
1561
+ const currentState = buildCurrentState(chainResult.rules, chainResult.classified);
1562
+ saveHandshakeState(pbDir, chainResult.rules, chainResult.classified);
1563
+ const diff = diffHandshakeState(currentState, previousState);
1564
+ const diffText = formatDiff(diff);
1565
+ log(diffText);
1566
+ }
1567
+ }
1568
+ // 5b. Read personal layer (WP-310 E2) — machine-local rules/skills from .productbrain/.local/
1569
+ // Returns [] when the directory is absent. All returned entries have persist: 'local'.
1570
+ const personalRules = readPersonalLayer(pbDir);
1571
+ if (personalRules.length > 0) {
1572
+ // Name collision detection — warn when a personal rule overrides a team rule
1573
+ const teamRuleNames = new Set(allRules.map((r) => r.name));
1574
+ for (const pr of personalRules) {
1575
+ if (teamRuleNames.has(pr.name)) {
1576
+ logErr(`Personal rule "${pr.name}" overrides team rule (local takes precedence).`);
1577
+ }
1578
+ }
1579
+ // Merge: team rules first, personal rules appended (personal takes precedence via name collision above)
1580
+ allRules = [...allRules, ...personalRules];
1581
+ }
1582
+ const personalSkills = readPersonalSkillsLayer(pbDir);
1583
+ if (personalSkills.length > 0) {
1584
+ const teamSkillNames = new Set(allSkills.map((s) => s.name));
1585
+ for (const ps of personalSkills) {
1586
+ if (teamSkillNames.has(ps.name)) {
1587
+ logErr(`Personal skill "${ps.name}" overrides team skill (local takes precedence).`);
1588
+ }
1589
+ }
1590
+ allSkills.push(...personalSkills);
1591
+ }
1592
+ // 5d. Load method registry (WP-310 E4) — only when manifest is present.
1593
+ let registrySource;
1594
+ let registryStale;
1595
+ if (manifest) {
1596
+ const registryResult = await loadMethodRegistry(manifest.method_source, kernelCall).catch(() => null);
1597
+ if (registryResult) {
1598
+ registrySource = registryResult.source;
1599
+ registryStale = registryResult.stale;
1600
+ }
1601
+ }
1602
+ const adoptionFilteredSkills = filterByAdoptionState(allSkills, manifest);
1603
+ const adoptionFilteredRules = filterByAdoptionState(allRules, manifest);
1604
+ // Compute adoption counts for report (only meaningful when manifest is present)
1605
+ const adoptedRulesCount = manifest ? adoptionFilteredRules.length : undefined;
1606
+ const rejectedRulesCount = manifest ? allRules.length - adoptionFilteredRules.length : undefined;
1607
+ // Apply level filtering with stage-gating (after adoption filter, before target filtering in write loop)
1608
+ // Stage caps the effective level: blank→beginner, seed→intermediate, grounded+→expert.
1609
+ // If stage caps below the requested level, log it so the user knows why items were dropped.
1610
+ const profileStage = workspaceProfile?.stage;
1611
+ const levelFilteredSkills = filterByLevel(adoptionFilteredSkills, level, profileStage);
1612
+ const levelFilteredRules = filterByLevel(adoptionFilteredRules, level, profileStage);
1613
+ // Log when stage gating changes the effective level
1614
+ if (profileStage) {
1615
+ const stageCap = STAGE_TO_MAX_LEVEL[profileStage];
1616
+ if (stageCap) {
1617
+ const requestedIdx = level ? LEVEL_ORDER.indexOf(level) : LEVEL_ORDER.length - 1;
1618
+ const capIdx = LEVEL_ORDER.indexOf(stageCap);
1619
+ if (capIdx < requestedIdx) {
1620
+ log(`Stage "${profileStage}" caps level from ${level || 'expert'} to ${stageCap}`);
1621
+ }
1622
+ }
1623
+ }
1624
+ // Apply when-condition filtering (stage-aware, workspace profile + repo context)
1625
+ const canonicalSkills = levelFilteredSkills.filter((skill) => {
1626
+ const result = evaluateConditions(skill.conditions ?? {}, workspaceProfile, repo);
1627
+ if (dryRun && !result.included) {
1628
+ log(` EXCLUDED skill ${skill.name}: ${result.reasons.join(', ')}`);
1629
+ }
1630
+ return result.included;
1631
+ });
1632
+ const canonicalRules = levelFilteredRules.filter((rule) => {
1633
+ const result = evaluateConditions(rule.conditions ?? {}, workspaceProfile, repo);
1634
+ if (dryRun && !result.included) {
1635
+ log(` EXCLUDED rule ${rule.name}: ${result.reasons.join(', ')}`);
1636
+ }
1637
+ return result.included;
1638
+ });
1639
+ if (dryRun && canonicalSkills.length > 0) {
1640
+ log(` INCLUDED skills: ${canonicalSkills.map((s) => s.name).join(', ')}`);
1641
+ }
1642
+ if (dryRun && canonicalRules.length > 0) {
1643
+ log(` INCLUDED rules: ${canonicalRules.map((r) => r.name).join(', ')}`);
1644
+ }
282
1645
  if (canonicalSkills.length > 0 || canonicalRules.length > 0) {
283
1646
  const levelSuffix = level ? ` (level: ${level})` : '';
284
- log(`Portable knowledge: ${canonicalSkills.length} skill(s), ${canonicalRules.length} rule(s)${levelSuffix}`);
1647
+ const stageSuffix = profileStage ? `, stage: ${profileStage}` : '';
1648
+ const stackSuffix = repo.detectedStack.length > 0 ? `, stack: [${repo.detectedStack.join(', ')}]` : '';
1649
+ const totalSkills = allSkills.length;
1650
+ const totalRules = allRules.length;
1651
+ log(`Portable knowledge: ${canonicalSkills.length}/${totalSkills} skills, ${canonicalRules.length}/${totalRules} rules${levelSuffix}${stageSuffix}${stackSuffix}`);
285
1652
  }
286
1653
  // 6. Generate file contents
287
- const contextContent = orientView ? generateContextMd(orientView, repo, timestamp) : null;
1654
+ // Build workspace context for AGENTS.md enrichment (stage, focus, governance, entry count)
1655
+ const agentsWorkspaceContext = workspaceProfile
1656
+ ? {
1657
+ stage: workspaceProfile.stage,
1658
+ focus: orientView?.strategicContext?.currentWorkPackage ?? undefined,
1659
+ governanceMode: workspaceProfile.governanceMode,
1660
+ totalEntries: workspaceProfile.totalEntries,
1661
+ }
1662
+ : undefined;
1663
+ // Collect codex-targeted skills for AGENTS.md skill directory.
1664
+ // persist: 'local' entries are included; all projections are now local-only.
1665
+ const agentsCodexSkills = canonicalSkills
1666
+ .filter((s) => shouldEmitToTarget(s, 'codex'))
1667
+ .map((s) => ({
1668
+ name: s.name,
1669
+ description: s.description,
1670
+ triggers: s.triggers,
1671
+ }));
1672
+ // Collect copilot-targeted skills for copilot-instructions.md skill summaries
1673
+ const copilotSkills = canonicalSkills
1674
+ .filter((s) => shouldEmitToTarget(s, 'copilot'))
1675
+ .map((s) => ({
1676
+ name: s.name,
1677
+ description: s.description,
1678
+ triggers: s.triggers,
1679
+ }));
1680
+ // Collect copilot-targeted rules for copilot-instructions.md rule summaries
1681
+ const copilotRules = canonicalRules
1682
+ .filter((r) => shouldEmitToTarget(r, 'copilot'))
1683
+ .map((r) => ({
1684
+ name: r.name,
1685
+ description: r.description,
1686
+ }));
1687
+ const copilotProfile = resolveSurfaceProfile('copilot');
1688
+ const copilotOptions = {
1689
+ profile: copilotProfile,
1690
+ workspaceContext: agentsWorkspaceContext,
1691
+ skills: copilotSkills.length > 0 ? copilotSkills : undefined,
1692
+ rules: copilotRules.length > 0 ? copilotRules : undefined,
1693
+ };
1694
+ const contextContent = orientView ? generateContextMd(orientView, repo, timestamp, workspaceProfile?.stage) : null;
288
1695
  const briefingContent = generateBriefingMd(matchedEntries, repo, uniqueQueries, timestamp);
1696
+ const agentsContent = generateAgentsMd(timestamp, {
1697
+ workspaceContext: agentsWorkspaceContext,
1698
+ skills: agentsCodexSkills.length > 0 ? agentsCodexSkills : undefined,
1699
+ });
289
1700
  const claudeContent = generateClaudeMd(timestamp);
290
1701
  const cursorContent = generateCursorMdc(timestamp);
291
- const copilotContent = generateCopilotMd(timestamp);
1702
+ const copilotContent = generateCopilotMd(timestamp, copilotOptions);
1703
+ const boundaryEnforcementMode = getBoundaryEnforcementMode(manifest);
1704
+ const boundaryManifestContent = boundaryEnforcementMode === 'advisory'
1705
+ ? null
1706
+ : generateBoundaryManifest(pbDir);
292
1707
  // 7. Write files
293
1708
  const filesWritten = [];
294
1709
  const filesSkipped = [];
1710
+ const previewPlan = [];
1711
+ if (writeMode && usedDbSource) {
1712
+ const authoringSyncState = loadAuthoringSyncState(pbDir);
1713
+ let authoringSyncStateChanged = false;
1714
+ const personalAssets = dbAssetRows.filter((asset) => asset.scope === 'personal' &&
1715
+ !asset.disabledByOwner &&
1716
+ (asset.assetKind === 'skill' || asset.assetKind === 'rule' || asset.assetKind === 'hook'));
1717
+ for (const asset of personalAssets) {
1718
+ // WP-428 S2 (Finding #5): skip projection for assets whose body fetch failed.
1719
+ // Do NOT write the authoring file with empty body, do NOT update lastProjectedHash.
1720
+ if (bodyFetchFailedEntryIds.has(asset.entryId))
1721
+ continue;
1722
+ const authoringPath = setupAuthoringPath(cwd, asset);
1723
+ if (!authoringPath)
1724
+ continue;
1725
+ // Review: use relative() for correct cross-platform path computation (string-replace
1726
+ // mis-fires when cwd uses a non-'/' separator), matching the dormant passes below.
1727
+ const relativeAuthoringPath = relative(cwd, authoringPath);
1728
+ const bodyHash = setupAuthoringAssetHash(asset);
1729
+ const authoringExists = existsSync(authoringPath);
1730
+ const tracked = authoringSyncState.assets[asset.entryId];
1731
+ const trackedHere = tracked?.path === relativeAuthoringPath;
1732
+ const trackedMatchesServer = Boolean(trackedHere && asset.lastProjectedHash && tracked?.hash === asset.lastProjectedHash);
1733
+ if (!authoringExists && asset.lastProjectedHash && trackedHere && trackedMatchesServer) {
1734
+ try {
1735
+ const dormantResult = await kernelCall('setup.markPersonalSetupAssetDormantFromSync', { entryId: asset.entryId, expectedLastProjectedHash: tracked.hash });
1736
+ if (dormantResult.action === 'conflict') {
1737
+ syncDriftTensToFire.push(`Repo authoring deletion skipped for ${asset.entryId}; server baseline changed during sync, so writeback was deferred until the next DB read.`);
1738
+ logErr(`Warning: authoring deletion for ${asset.entryId} was not applied because the DB baseline changed during sync.`);
1739
+ deferredAuthoringBaselineEntryIds.add(asset.entryId);
1740
+ continue;
1741
+ }
1742
+ else {
1743
+ syncDriftTensToFire.push(`Repo authoring file was deleted for ${asset.entryId}; caller-owned personal setup asset marked dormant.`);
1744
+ log(`Setup authoring deletion: ${asset.entryId} marked dormant.`);
1745
+ delete authoringSyncState.assets[asset.entryId];
1746
+ authoringSyncStateChanged = true;
1747
+ continue;
1748
+ }
1749
+ }
1750
+ catch (err) {
1751
+ logErr(`Warning: could not mark ${asset.entryId} dormant after authoring deletion — ${err instanceof Error ? err.message : String(err)}`);
1752
+ continue;
1753
+ }
1754
+ }
1755
+ else if (!authoringExists && asset.lastProjectedHash && trackedHere) {
1756
+ syncDriftTensToFire.push(`Repo authoring deletion skipped for ${asset.entryId}; local sync baseline is stale, so DB writeback wins.`);
1757
+ logErr(`Warning: authoring deletion for ${asset.entryId} was not applied because the DB baseline changed.`);
1758
+ }
1759
+ const nextAuthoringContent = renderSetupAuthoringFile(asset);
1760
+ if (authoringExists) {
1761
+ const parsed = parseSetupAuthoringFrontmatter(readFileSync(authoringPath, 'utf8'));
1762
+ const fileHash = setupAuthoringAssetHash({
1763
+ entryId: parsed.frontmatterId?.trim() || asset.entryId,
1764
+ name: parsed.name || asset.name,
1765
+ description: parsed.description,
1766
+ body: parsed.body,
1767
+ assetKind: asset.assetKind,
1768
+ triggers: parsed.triggers,
1769
+ semanticRefs: parsed.semanticRefs,
1770
+ });
1771
+ if (fileHash === bodyHash) {
1772
+ if (asset.lastProjectedHash !== bodyHash) {
1773
+ await kernelCall('setup.updateLastProjectedHash', {
1774
+ entryId: asset.entryId,
1775
+ hash: bodyHash,
1776
+ }).catch(() => null);
1777
+ }
1778
+ authoringSyncState.assets[asset.entryId] = { path: relativeAuthoringPath, hash: bodyHash };
1779
+ authoringSyncStateChanged = true;
1780
+ continue;
1781
+ }
1782
+ if (!asset.lastProjectedHash) {
1783
+ syncDriftTensToFire.push(`Repo authoring file differs from DB for ${asset.entryId} at ${authoringPath} with no shared baseline; DB writeback skipped.`);
1784
+ logErr(`Warning: setup authoring drift for ${asset.entryId}; DB writeback skipped because no shared baseline exists.`);
1785
+ continue;
1786
+ }
1787
+ if (fileHash !== asset.lastProjectedHash &&
1788
+ bodyHash !== asset.lastProjectedHash) {
1789
+ syncDriftTensToFire.push(`Repo authoring file won setup sync conflict for ${asset.entryId} at ${authoringPath}.`);
1790
+ logErr(`Warning: repo authoring file won sync conflict for ${asset.entryId}; DB writeback skipped.`);
1791
+ continue;
1792
+ }
1793
+ if (fileHash !== asset.lastProjectedHash) {
1794
+ syncDriftTensToFire.push(`Repo authoring file differs from DB baseline for ${asset.entryId} at ${authoringPath}; DB writeback skipped.`);
1795
+ logErr(`Warning: setup authoring drift for ${asset.entryId}; DB writeback skipped.`);
1796
+ continue;
1797
+ }
1798
+ }
1799
+ try {
1800
+ assertSetupWritePath(authoringPath, perimeterManifest);
1801
+ mkdirSync(dirname(authoringPath), { recursive: true });
1802
+ writeFileSync(authoringPath, nextAuthoringContent);
1803
+ filesWritten.push(relativeAuthoringPath);
1804
+ await kernelCall('setup.updateLastProjectedHash', {
1805
+ entryId: asset.entryId,
1806
+ hash: bodyHash,
1807
+ }).catch(() => null);
1808
+ authoringSyncState.assets[asset.entryId] = { path: relativeAuthoringPath, hash: bodyHash };
1809
+ authoringSyncStateChanged = true;
1810
+ }
1811
+ catch (err) {
1812
+ logErr(`Warning: could not write setup authoring file for ${asset.entryId} — ${err instanceof Error ? err.message : String(err)}`);
1813
+ }
1814
+ }
1815
+ if (authoringSyncStateChanged) {
1816
+ try {
1817
+ saveAuthoringSyncState(pbDir, authoringSyncState);
1818
+ }
1819
+ catch (err) {
1820
+ logErr(`Warning: could not persist setup authoring sync state — ${err instanceof Error ? err.message : String(err)}`);
1821
+ }
1822
+ }
1823
+ }
295
1824
  const writes = [
296
1825
  ...(contextContent ? [{ path: join(cwd, '.productbrain', 'context.md'), relative: '.productbrain/context.md', content: contextContent, dirs: join(cwd, '.productbrain'), isAdapter: false }] : []),
297
1826
  { path: join(cwd, '.productbrain', 'briefing.md'), relative: '.productbrain/briefing.md', content: briefingContent, isAdapter: false },
298
- { path: join(cwd, 'CLAUDE.md'), relative: 'CLAUDE.md', content: claudeContent, isAdapter: true },
299
- { path: join(cwd, '.cursor', 'rules', 'chain.mdc'), relative: '.cursor/rules/chain.mdc', content: cursorContent, dirs: join(cwd, '.cursor', 'rules'), isAdapter: true },
300
- { path: join(cwd, '.github', 'copilot-instructions.md'), relative: '.github/copilot-instructions.md', content: copilotContent, dirs: join(cwd, '.github'), isAdapter: true },
1827
+ { path: join(cwd, 'AGENTS.md'), relative: 'AGENTS.md', content: agentsContent, isAdapter: true, target: 'codex', augmentTarget: true },
1828
+ { path: join(cwd, 'CLAUDE.md'), relative: 'CLAUDE.md', content: claudeContent, isAdapter: true, target: 'claude', augmentTarget: true },
1829
+ { path: join(cwd, '.cursor', 'rules', 'chain.mdc'), relative: '.cursor/rules/chain.mdc', content: cursorContent, dirs: join(cwd, '.cursor', 'rules'), isAdapter: true, target: 'cursor' },
1830
+ { path: join(cwd, '.github', 'copilot-instructions.md'), relative: '.github/copilot-instructions.md', content: copilotContent, dirs: join(cwd, '.github'), isAdapter: true, target: 'copilot' },
1831
+ ...(boundaryManifestContent ? [{
1832
+ path: join(cwd, '.productbrain', 'generated', 'boundaries.json'),
1833
+ relative: '.productbrain/generated/boundaries.json',
1834
+ content: boundaryManifestContent,
1835
+ dirs: join(cwd, '.productbrain', 'generated'),
1836
+ isAdapter: false,
1837
+ }] : []),
301
1838
  ];
302
1839
  // Add Cursor skill copies (filtered by target)
1840
+ const cursorProfile = resolveSurfaceProfile('cursor');
303
1841
  for (const skill of canonicalSkills) {
304
1842
  if (!shouldEmitToTarget(skill, 'cursor'))
305
1843
  continue;
1844
+ const dbAssetEntryId = skill.sourcePath.startsWith('db:') ? skill.sourcePath.slice(3) : undefined;
306
1845
  const skillDir = join(cwd, '.cursor', 'skills', skill.name);
307
1846
  writes.push({
308
1847
  path: join(skillDir, 'SKILL.md'),
309
1848
  relative: `.cursor/skills/${skill.name}/SKILL.md`,
310
- content: generateCursorSkill(skill),
1849
+ content: generateCursorSkill(skill, cursorProfile),
311
1850
  dirs: skillDir,
312
1851
  isAdapter: true,
1852
+ target: 'cursor',
1853
+ dbAssetEntryId,
313
1854
  });
314
1855
  }
1856
+ // Add Codex skill copies (projected markdown + index)
1857
+ const codexProfile = resolveSurfaceProfile('codex');
1858
+ const codexSkills = canonicalSkills.filter((s) => shouldEmitToTarget(s, 'codex'));
1859
+ for (const skill of codexSkills) {
1860
+ const dbAssetEntryId = skill.sourcePath.startsWith('db:') ? skill.sourcePath.slice(3) : undefined;
1861
+ writes.push({
1862
+ path: join(cwd, '.codex', 'skills', `${skill.name}.md`),
1863
+ relative: `.codex/skills/${skill.name}.md`,
1864
+ content: generateCodexSkill(skill, codexProfile),
1865
+ dirs: join(cwd, '.codex', 'skills'),
1866
+ isAdapter: true,
1867
+ target: 'codex',
1868
+ dbAssetEntryId,
1869
+ });
1870
+ }
1871
+ writes.push({
1872
+ path: join(cwd, '.codex', 'skills', 'README.md'),
1873
+ relative: '.codex/skills/README.md',
1874
+ content: generateCodexSkillIndex(codexSkills),
1875
+ dirs: join(cwd, '.codex', 'skills'),
1876
+ isAdapter: true,
1877
+ target: 'codex',
1878
+ });
1879
+ // Validate Codex-projected skills for dead references
1880
+ const codexWarnings = validateCodexSkills(codexSkills);
315
1881
  // Add Cursor rule copies (filtered by target)
316
1882
  for (const rule of canonicalRules) {
317
1883
  if (!shouldEmitToTarget(rule, 'cursor'))
318
1884
  continue;
1885
+ const dbAssetEntryId = rule.sourcePath.startsWith('db:') ? rule.sourcePath.slice(3) : undefined;
319
1886
  writes.push({
320
1887
  path: join(cwd, '.cursor', 'rules', `${rule.name}.mdc`),
321
1888
  relative: `.cursor/rules/${rule.name}.mdc`,
322
- content: generateCursorRule(rule),
1889
+ content: generateCursorRule(rule, cursorProfile),
323
1890
  dirs: join(cwd, '.cursor', 'rules'),
324
1891
  isAdapter: true,
1892
+ target: 'cursor',
1893
+ dbAssetEntryId,
325
1894
  });
326
1895
  }
327
1896
  // Add Claude Code rule copies (filtered by target)
1897
+ const claudeProfile = resolveSurfaceProfile('claude');
328
1898
  for (const rule of canonicalRules) {
329
1899
  if (!shouldEmitToTarget(rule, 'claude'))
330
1900
  continue;
1901
+ const dbAssetEntryId = rule.sourcePath.startsWith('db:') ? rule.sourcePath.slice(3) : undefined;
331
1902
  writes.push({
332
1903
  path: join(cwd, '.claude', 'rules', `${rule.name}.md`),
333
1904
  relative: `.claude/rules/${rule.name}.md`,
334
- content: generateClaudeRule(rule),
1905
+ content: generateClaudeRule(rule, claudeProfile),
335
1906
  dirs: join(cwd, '.claude', 'rules'),
336
1907
  isAdapter: true,
1908
+ target: 'claude',
1909
+ dbAssetEntryId,
337
1910
  });
338
1911
  }
339
1912
  // Add Claude Code skill router (filtered by target)
340
1913
  const claudeSkills = canonicalSkills.filter((s) => shouldEmitToTarget(s, 'claude'));
341
- const skillRouterContent = generateClaudeSkillRouter(claudeSkills);
1914
+ const skillRouterContent = generateClaudeSkillRouter(claudeSkills, claudeProfile);
342
1915
  if (skillRouterContent) {
343
1916
  writes.push({
344
1917
  path: join(cwd, '.claude', 'rules', 'skill-router.md'),
@@ -346,33 +1919,717 @@ export async function runHandshake(options = {}) {
346
1919
  content: skillRouterContent,
347
1920
  dirs: join(cwd, '.claude', 'rules'),
348
1921
  isAdapter: true,
1922
+ target: 'claude',
349
1923
  });
350
1924
  }
1925
+ // 7a. WP-379 S5b: Resolve projection collisions before writing.
1926
+ // In apply mode, enumerate target dirs and unlink any auto-generated files
1927
+ // whose normalized name no longer matches any active asset from the server.
1928
+ // This prevents case-variant orphans from accumulating across handshakes.
1929
+ // Runs only when we have a DB asset list (usedDbSource) — without a DB source,
1930
+ // we can't determine which files are canonical vs. orphan.
1931
+ const collisionTensToFire = [];
1932
+ if (writeMode && usedDbSource) {
1933
+ const activeAssetNames = dbAssetRows
1934
+ .filter((a) => !a.disabledByOwner)
1935
+ .map((a) => a.name);
1936
+ const { collisionTens } = resolveProjectionCollision(cwd, activeAssetNames, log, logErr);
1937
+ collisionTensToFire.push(...collisionTens);
1938
+ }
1939
+ // ── WP-436 S3: Vocab projector — fetch workspace vocab context once per handshake ──
1940
+ // Source-side (.productbrain/skills/*.md + rules/*.md) stays tokenized.
1941
+ // The projector resolves {{vocab:...}} tokens before writing adapter output to disk
1942
+ // (.cursor/rules/, .claude/rules/, CLAUDE.md, AGENTS.md, .github/copilot-instructions.md).
1943
+ // Fail-open: if vocab fetch fails, skip resolution and write raw token (no breakage).
1944
+ const handshakeVocabCtx = await getOrFetchVocabCtx(config.apiKey, async () => {
1945
+ try {
1946
+ const vocab = await kernelCall('chain.getVocabulary', {});
1947
+ if (vocab?.collectionLabels || vocab?.collectionDefaults) {
1948
+ return {
1949
+ ...(vocab.collectionLabels ? { collectionLabels: vocab.collectionLabels } : {}),
1950
+ ...(vocab.collectionDefaults ? { collectionDefaults: vocab.collectionDefaults } : {}),
1951
+ };
1952
+ }
1953
+ return null;
1954
+ }
1955
+ catch {
1956
+ return null; // fail-open
1957
+ }
1958
+ });
1959
+ const userOwnedSkipped = [];
1960
+ const projectedHashUpdates = new Map();
1961
+ const cleanBucketPaths = [];
1962
+ const tamperedBucket = [];
1963
+ const recordProjectedHash = (entryId) => {
1964
+ if (!applyMode || !entryId)
1965
+ return;
1966
+ if (deferredAuthoringBaselineEntryIds.has(entryId))
1967
+ return;
1968
+ const projection = dbProjectionHashes.get(entryId);
1969
+ if (projection)
1970
+ projectedHashUpdates.set(entryId, projection.hash);
1971
+ };
1972
+ // TEN-2155: region augmentation context + symlink dedupe.
1973
+ // codexActive gates the AGENTS.md skills-index pointer (region-projections.ts).
1974
+ const regionCtx = { codexActive: allowedTargets.has('codex') };
1975
+ const malformedRegionPaths = [];
1976
+ // If two augment targets resolve to the same inode (e.g. CLAUDE.md symlinked to AGENTS.md),
1977
+ // augment only the first-seen (AGENTS.md precedes CLAUDE.md in `writes`) to avoid double-injection.
1978
+ const seenAugmentRealpaths = new Set();
1979
+ const augmentTargetSkip = new Set();
351
1980
  for (const w of writes) {
1981
+ if (!w.augmentTarget || !w.target || !allowedTargets.has(w.target) || !existsSync(w.path))
1982
+ continue;
1983
+ let real;
1984
+ try {
1985
+ real = realpathSync(w.path);
1986
+ }
1987
+ catch {
1988
+ real = w.path;
1989
+ }
1990
+ if (seenAugmentRealpaths.has(real))
1991
+ augmentTargetSkip.add(w.path);
1992
+ else
1993
+ seenAugmentRealpaths.add(real);
1994
+ }
1995
+ for (const w of writes) {
1996
+ // Surface filtering: skip adapter writes for targets not in the allowed set
1997
+ if (w.target && !allowedTargets.has(w.target)) {
1998
+ filesSkipped.push({ path: w.relative, reason: `filtered (surface: ${w.target})` });
1999
+ if (preview)
2000
+ previewPlan.push({ path: w.relative, status: 'filtered' });
2001
+ continue;
2002
+ }
2003
+ // ── TEN-2155: augment user-owned CLAUDE.md / AGENTS.md with a marked PB region ──
2004
+ // Runs ahead of the legacy MARKER-keyed gates. Only existing augmentable/region-present
2005
+ // files are spliced here; absent + legacy-pb-managed fall through to the legacy path.
2006
+ if (w.augmentTarget) {
2007
+ const projection = REGION_PROJECTIONS[w.target];
2008
+ if (projection && existsSync(w.path)) {
2009
+ if (augmentTargetSkip.has(w.path)) {
2010
+ filesSkipped.push({ path: w.relative, reason: 'symlinked to another augment target — augmented once' });
2011
+ continue;
2012
+ }
2013
+ const disk = readFileSync(w.path, 'utf8');
2014
+ const cls = classifyAdapterFile(disk);
2015
+ if (cls === 'augmentable' || cls === 'region-present') {
2016
+ const eol = detectEol(disk);
2017
+ const region = replaceVocabTokens(projection.build(regionCtx, eol), handshakeVocabCtx);
2018
+ // Defense-in-depth: the composed region (post vocab-token resolution) must itself be a
2019
+ // single well-formed region. v1 content has no vocab tokens so this is a no-op, but it
2020
+ // guards the documented future override/vocab seam from injecting a stray sentinel/MARKER.
2021
+ if (classifyAdapterFile(region) !== 'region-present') {
2022
+ filesSkipped.push({ path: w.relative, reason: 'internal: composed PB region malformed — skipped to protect your file' });
2023
+ if (preview)
2024
+ previewPlan.push({ path: w.relative, status: 'needs-attention' });
2025
+ continue;
2026
+ }
2027
+ const candidate = cls === 'augmentable' ? spliceAppend(disk, region, eol) : spliceReplace(disk, region);
2028
+ if (candidate === disk) {
2029
+ filesSkipped.push({ path: w.relative, reason: 'unchanged' });
2030
+ if (preview)
2031
+ previewPlan.push({ path: w.relative, status: 'unchanged' });
2032
+ continue;
2033
+ }
2034
+ if (preview || dryRun) {
2035
+ filesWritten.push(w.relative + (dryRun ? ' (dry run)' : ''));
2036
+ if (preview)
2037
+ previewPlan.push({ path: w.relative, status: 'would-augment' });
2038
+ continue;
2039
+ }
2040
+ if (authorityPreviewOnly) {
2041
+ // apply + materialize observe/off: honor authority — do NOT write; mirror the legacy
2042
+ // preview-only skip so the report does not falsely list it as written.
2043
+ filesSkipped.push({ path: w.relative, reason: `preview-only (materialize: ${manifestStatus.mode})` });
2044
+ continue;
2045
+ }
2046
+ assertSetupWritePath(w.path, perimeterManifest);
2047
+ writeFileSync(w.path, candidate);
2048
+ filesWritten.push(w.relative);
2049
+ continue;
2050
+ }
2051
+ if (cls === 'malformed') {
2052
+ filesSkipped.push({ path: w.relative, reason: 'malformed PB region — left untouched; fix the pb:region sentinels' });
2053
+ if (preview)
2054
+ previewPlan.push({ path: w.relative, status: 'needs-attention' });
2055
+ malformedRegionPaths.push(w.relative);
2056
+ continue;
2057
+ }
2058
+ // cls 'pb-managed' → fall through to the legacy whole-file re-projection path below.
2059
+ // cls 'opt-out' (file carries the pb:no-augment sentinel — e.g. this repo's committed
2060
+ // constitution) → fall through too, landing on the legacy user-owned skip: left untouched,
2061
+ // never spliced. This is how a file declines augmentation without losing its user-owned status.
2062
+ }
2063
+ // absent file → fall through to legacy whole-file CREATE.
2064
+ }
352
2065
  if (w.isAdapter && !shouldWriteAdapter(w.path, force)) {
353
- filesSkipped.push({ path: w.relative, reason: 'exists without auto-generated marker (use --force to overwrite)' });
2066
+ // User-owned: a file at an adapter path without our auto-gen MARKER is the
2067
+ // user's own file. Leave it untouched — never overwrite, never treat as drift
2068
+ // or log a TEN (TEN-2150). Relayed to the connect screen via userOwnedSkipped.
2069
+ filesSkipped.push({ path: w.relative, reason: 'user-owned — left untouched (pb won\'t overwrite your file)' });
2070
+ if (preview) {
2071
+ previewPlan.push({ path: w.relative, status: 'user-owned' });
2072
+ }
2073
+ else {
2074
+ userOwnedSkipped.push(w.relative);
2075
+ }
354
2076
  continue;
355
2077
  }
356
- if (dryRun) {
357
- filesWritten.push(w.relative + ' (dry run)');
2078
+ if (authorityPreviewOnly) {
2079
+ filesSkipped.push({
2080
+ path: w.relative,
2081
+ reason: `preview-only (materialize: ${manifestStatus.mode})`,
2082
+ });
358
2083
  continue;
359
2084
  }
2085
+ if (preview || dryRun) {
2086
+ // In preview/dry-run mode: check content to distinguish new/update/unchanged
2087
+ if (existsSync(w.path)) {
2088
+ const current = readFileSync(w.path, 'utf8');
2089
+ const nextNormalized = normalizeHandshakeContentForComparison(w.content);
2090
+ const currentNormalized = normalizeHandshakeContentForComparison(current);
2091
+ if (nextNormalized === currentNormalized) {
2092
+ filesSkipped.push({ path: w.relative, reason: 'unchanged' });
2093
+ if (preview)
2094
+ previewPlan.push({ path: w.relative, status: 'unchanged' });
2095
+ }
2096
+ else {
2097
+ filesWritten.push(w.relative + (dryRun ? ' (dry run)' : ''));
2098
+ if (preview)
2099
+ previewPlan.push({ path: w.relative, status: 'would-update' });
2100
+ }
2101
+ }
2102
+ else {
2103
+ filesWritten.push(w.relative + (dryRun ? ' (dry run)' : ''));
2104
+ if (preview)
2105
+ previewPlan.push({ path: w.relative, status: 'would-write' });
2106
+ }
2107
+ continue;
2108
+ }
2109
+ // ── WP-421 S3: defer tampered adapter writes (doneWhen #17) ──────────────
2110
+ // For adapter projections that already exist on disk, classify into
2111
+ // pb-managed-clean / pb-managed-tampered. Tampered = MARKER present +
2112
+ // hash trailer mismatches body. Defer (do NOT overwrite); post-loop
2113
+ // resolution handles them. --force bypasses the tampered defer (legacy
2114
+ // semantic: explicit user opt-in to overwrite).
2115
+ if (w.isAdapter && !force) {
2116
+ const drift = classifyDriftBucket(w.path);
2117
+ if (drift && drift.bucket === 'pb-managed-tampered') {
2118
+ tamperedBucket.push({
2119
+ path: w.path,
2120
+ relative: w.relative,
2121
+ content: w.content,
2122
+ expectedHash: drift.expectedHash,
2123
+ actualHash: drift.actualHash,
2124
+ dirs: w.dirs,
2125
+ dbAssetEntryId: w.dbAssetEntryId,
2126
+ });
2127
+ // Do NOT write — defer until prompt/refusal resolves the file.
2128
+ continue;
2129
+ }
2130
+ if (drift && drift.bucket === 'pb-managed-clean') {
2131
+ cleanBucketPaths.push(w.relative);
2132
+ }
2133
+ }
2134
+ assertSetupWritePath(w.path, perimeterManifest);
360
2135
  if (w.dirs)
361
2136
  mkdirSync(w.dirs, { recursive: true });
362
- writeFileSync(w.path, w.content);
2137
+ // WP-436 S3: resolve {{vocab:...}} tokens before writing projected adapter files.
2138
+ // Source-side (.productbrain/skills/*.md, rules/*.md) stays tokenized.
2139
+ // Only adapter projections (cursor/rules, claude/rules, CLAUDE.md, AGENTS.md, etc.) get resolved.
2140
+ // Fail-open: if vocabCtx is undefined, replaceVocabTokens falls back to canonicalKey literals.
2141
+ const resolvedContent = w.isAdapter
2142
+ ? replaceVocabTokens(w.content, handshakeVocabCtx)
2143
+ : w.content;
2144
+ if (existsSync(w.path)) {
2145
+ const current = readFileSync(w.path, 'utf8');
2146
+ const nextNormalized = normalizeHandshakeContentForComparison(resolvedContent);
2147
+ const currentNormalized = normalizeHandshakeContentForComparison(current);
2148
+ if (nextNormalized === currentNormalized) {
2149
+ filesSkipped.push({ path: w.relative, reason: 'unchanged' });
2150
+ recordProjectedHash(w.dbAssetEntryId);
2151
+ continue;
2152
+ }
2153
+ }
2154
+ writeFileSync(w.path, resolvedContent);
363
2155
  filesWritten.push(w.relative);
2156
+ recordProjectedHash(w.dbAssetEntryId);
2157
+ }
2158
+ if (projectedHashUpdates.size > 0) {
2159
+ const updates = [...projectedHashUpdates];
2160
+ const results = await Promise.allSettled(updates.map(([entryId, hash]) => kernelCall('setup.updateLastProjectedHash', { entryId, hash })));
2161
+ results.forEach((result, index) => {
2162
+ if (result.status === 'rejected') {
2163
+ const [entryId] = updates[index];
2164
+ trackEvent('skill.projection.failed', {
2165
+ entryId,
2166
+ reason: result.reason instanceof Error ? result.reason.message : String(result.reason),
2167
+ });
2168
+ }
2169
+ });
2170
+ }
2171
+ // Ordering note: this refusal runs AFTER the projected-hash flush so that files legitimately
2172
+ // written THIS run still record their hashes; malformed files were never written (no hash to record).
2173
+ // TEN-2155: in non-interactive apply, a malformed PB region is a refusal, not a silent skip.
2174
+ // Gated on applyMode (NOT writeMode): a malformed region is a user-file INTEGRITY fault, so the
2175
+ // refusal fires on any headless `--apply` regardless of materialize authority (STD-263 invariant vi —
2176
+ // "headless → non-zero refusal", unqualified). writeMode would suppress it under observe/off, where
2177
+ // the malformed file still gets enumerated but the corruption signal would be silently dropped.
2178
+ if (applyMode && malformedRegionPaths.length > 0 && (options.noPrompt || !process.stdout.isTTY)) {
2179
+ throw new CLIError(`Malformed PB region in: ${malformedRegionPaths.join(', ')}`, {
2180
+ code: ErrorCode.VALIDATION_FAILED,
2181
+ category: 'validation',
2182
+ guidance: 'Fix the `<!-- pb:region:start -->` / `<!-- pb:region:end -->` sentinels (one balanced pair), then re-run `pb handshake`.',
2183
+ });
2184
+ }
2185
+ // ── WP-421 S3: tampered-bucket resolution (doneWhen #17) ────────────────────
2186
+ // Apply mode only. Tampered files were DEFERRED in the write loop above;
2187
+ // here we either prompt the user (interactive TTY) or refuse (headless).
2188
+ //
2189
+ // Headless = `--no-prompt` flag OR `process.stdout.isTTY === false`. When
2190
+ // headless: enumerate each tampered file to stderr, write a setup_receipt
2191
+ // row with kind='transition' (DEC-962) capturing `refusedTamperedFiles[]`,
2192
+ // then exit non-zero. NEVER auto-resolve. (#17 + exclusions: edits to
2193
+ // projection dirs are detected, NOT silently overwritten.)
2194
+ //
2195
+ // Interactive: for each tampered file, present adopt-or-revert. Adopt =
2196
+ // create a personal-scoped setup_asset draft from the tampered content
2197
+ // (see helper below). Revert = re-project canonical content over the
2198
+ // tampered file (write w.content to w.path).
2199
+ const adoptedTamperedPaths = [];
2200
+ const revertedTamperedPaths = [];
2201
+ if (writeMode && tamperedBucket.length > 0) {
2202
+ const headless = options.noPrompt === true || !process.stdout.isTTY;
2203
+ if (headless) {
2204
+ // ── Headless refusal path (doneWhen #17) ────────────────────────────────
2205
+ logErr('');
2206
+ logErr(`pb handshake: ${tamperedBucket.length} PB-managed projection file(s) were edited downstream of the auto-gen marker.`);
2207
+ logErr('Headless mode (--no-prompt or no TTY) cannot resolve adopt-or-revert — refusing.');
2208
+ logErr('');
2209
+ const refusedTamperedFiles = tamperedBucket.map((t) => ({
2210
+ path: t.relative,
2211
+ expectedHash: t.expectedHash,
2212
+ actualHash: t.actualHash,
2213
+ }));
2214
+ for (const refused of refusedTamperedFiles) {
2215
+ // Per #17: stderr enumerates each tampered file as
2216
+ // {path, expectedHash, actualHash, bucket}.
2217
+ logErr(` ${JSON.stringify({ ...refused, bucket: 'pb-managed-tampered' })}`);
2218
+ }
2219
+ logErr('');
2220
+ logErr('Re-run interactively to resolve, or use --force to overwrite (data loss).');
2221
+ // Write the kind='transition' setup_receipt row. Fail-open on the
2222
+ // network/auth side: if the row cannot be written, we still exit non-zero
2223
+ // (the audit trail is best-effort; refusal is mandatory).
2224
+ try {
2225
+ const manifestStatus = readManifestStatus(pbDir);
2226
+ await kernelCall('setup.recordTamperRefusal', {
2227
+ mode: manifestStatus.mode,
2228
+ refusedTamperedFiles,
2229
+ });
2230
+ trackEvent('setup.transition.refused', {
2231
+ fileCount: refusedTamperedFiles.length,
2232
+ mode: manifestStatus.mode,
2233
+ });
2234
+ }
2235
+ catch (err) {
2236
+ trackEvent('setup.transition.refused.write_failed', {
2237
+ error: err instanceof Error ? err.message : String(err),
2238
+ });
2239
+ logErr(`Warning: could not record transition receipt — ${err instanceof Error ? err.message : String(err)}`);
2240
+ }
2241
+ // Surface the report counts before exit (visibility for CI logs).
2242
+ if (!quiet) {
2243
+ process.stdout.write('\n');
2244
+ process.stdout.write(formatHandshakeReport({
2245
+ filesWritten,
2246
+ filesSkipped,
2247
+ matchedEntries,
2248
+ searchQueries: uniqueQueries,
2249
+ repo,
2250
+ codexWarnings: codexWarnings.length > 0 ? codexWarnings : undefined,
2251
+ chainRulesStats: chainRulesStats ?? undefined,
2252
+ chainGaps: chainGaps.length > 0 ? chainGaps : undefined,
2253
+ adoptedCount: adoptedRulesCount,
2254
+ rejectedCount: rejectedRulesCount,
2255
+ personalRuleCount: personalRules.length > 0 ? personalRules.length : undefined,
2256
+ personalSkillCount: personalSkills.length > 0 ? personalSkills.length : undefined,
2257
+ registrySource,
2258
+ registryStale,
2259
+ userOwnedSkipped: userOwnedSkipped.length > 0 ? userOwnedSkipped : undefined,
2260
+ managedCleanCount: cleanBucketPaths.length || undefined,
2261
+ tamperedFiles: refusedTamperedFiles,
2262
+ }) + '\n');
2263
+ }
2264
+ // Exit non-zero per #17.
2265
+ process.exit(1);
2266
+ }
2267
+ // ── Interactive path: prompt adopt-or-revert per file ──────────────────────
2268
+ log('');
2269
+ log(`pb handshake: ${tamperedBucket.length} PB-managed projection file(s) were edited downstream of the auto-gen marker.`);
2270
+ log('You can ADOPT (capture your edits as a personal-scoped draft) or REVERT (overwrite with canonical content).');
2271
+ // Batch yes-to-all / no-to-all when the user has many tampered files.
2272
+ // Threshold: 5 (arbitrary; mirrors the typical handshake projection set).
2273
+ let batchChoice = null;
2274
+ if (tamperedBucket.length >= 5) {
2275
+ const useBatch = await promptConfirm({
2276
+ message: `Apply the same choice to all ${tamperedBucket.length} tampered files?`,
2277
+ initialValue: false,
2278
+ });
2279
+ if (useBatch) {
2280
+ const choice = await promptSelect({
2281
+ message: 'Apply to all:',
2282
+ options: [
2283
+ { value: 'adopt', label: 'Adopt all — capture each as a personal-scoped draft' },
2284
+ { value: 'revert', label: 'Revert all — overwrite with canonical content' },
2285
+ ],
2286
+ });
2287
+ batchChoice = choice === 'adopt' ? 'adopt-all' : 'revert-all';
2288
+ }
2289
+ }
2290
+ for (const tamper of tamperedBucket) {
2291
+ // Reverse-map the projection path back to the canonical authoring path.
2292
+ const reverse = canonicalPathForAnySurface(tamper.relative);
2293
+ const canonicalHint = reverse
2294
+ ? `Canonical authoring path: ${reverse.canonicalPath}`
2295
+ : (() => {
2296
+ // Telemetry + fallback message per surfaces/telemetry.ts.
2297
+ reportReverseMapMissing({ surface: 'unknown', projectionPath: tamper.relative }, logErr);
2298
+ return `Canonical authoring path: ${getReverseMapFallbackMessage()}`;
2299
+ })();
2300
+ log('');
2301
+ log(`Tampered: ${tamper.relative}`);
2302
+ log(` expected: ${tamper.expectedHash}`);
2303
+ log(` actual: ${tamper.actualHash}`);
2304
+ log(` ${canonicalHint}`);
2305
+ let action;
2306
+ if (batchChoice === 'adopt-all')
2307
+ action = 'adopt';
2308
+ else if (batchChoice === 'revert-all')
2309
+ action = 'revert';
2310
+ else {
2311
+ action = await promptSelect({
2312
+ message: 'Adopt or revert?',
2313
+ options: [
2314
+ { value: 'adopt', label: 'Adopt — capture this as a personal-scoped setup_asset draft' },
2315
+ { value: 'revert', label: 'Revert — overwrite with canonical content' },
2316
+ ],
2317
+ });
2318
+ }
2319
+ if (action === 'revert') {
2320
+ // Re-project canonical content over the tampered file.
2321
+ // WP-436 S3: resolve vocab tokens before writing (all tampered files are adapters).
2322
+ if (tamper.dirs)
2323
+ mkdirSync(tamper.dirs, { recursive: true });
2324
+ assertSetupWritePath(tamper.path, perimeterManifest);
2325
+ writeFileSync(tamper.path, replaceVocabTokens(tamper.content, handshakeVocabCtx));
2326
+ revertedTamperedPaths.push(tamper.relative);
2327
+ recordProjectedHash(tamper.dbAssetEntryId);
2328
+ trackEvent('setup.tampered.reverted', { path: tamper.relative });
2329
+ }
2330
+ else {
2331
+ // Adopt: capture the tampered content as a personal-scoped draft.
2332
+ // Per DEC-953 sync rules: personal scope = push (no fork required).
2333
+ // The mutation is best-effort — if the adopt write fails, the file is
2334
+ // kept on disk untouched and a warning is logged. Adopt does NOT
2335
+ // revert; it preserves the user's edits AND records them as a draft.
2336
+ const draftName = basename(tamper.relative).replace(/\.(md|mdc)$/, '') + ' (adopted)';
2337
+ try {
2338
+ const tamperedContent = readFileSync(tamper.path, 'utf8');
2339
+ const session = readSession();
2340
+ const caller = session ? kernelCallWithSession : kernelCall;
2341
+ await caller('setup.ingestSetupAsset', {
2342
+ entryId: `SETUP-ADOPTED-${Date.now()}-${draftName.replace(/\s+/g, '-')}`,
2343
+ name: draftName,
2344
+ // WP-421 S3
2345
+ description: `Adopted from tampered projection at ${tamper.relative}.`,
2346
+ body: tamperedContent,
2347
+ assetKind: tamper.relative.includes('/skills/') ? 'skill' : 'rule',
2348
+ triggers: [],
2349
+ semanticRefs: [],
2350
+ });
2351
+ adoptedTamperedPaths.push(tamper.relative);
2352
+ trackEvent('setup.tampered.adopted', { path: tamper.relative });
2353
+ }
2354
+ catch (err) {
2355
+ logErr(`Warning: could not adopt ${tamper.relative} as draft — ${err instanceof Error ? err.message : String(err)}`);
2356
+ trackEvent('setup.tampered.adopt_failed', {
2357
+ path: tamper.relative,
2358
+ error: err instanceof Error ? err.message : String(err),
2359
+ });
2360
+ }
2361
+ }
2362
+ }
2363
+ }
2364
+ // 8a. Dormant marker + .dormant rename (WP-379 S4 + WP-426 E4) — apply mode only.
2365
+ const dormantMarkedPaths = [];
2366
+ if (writeMode && dormantDbAssetRows.length > 0) {
2367
+ const dormantState = loadAuthoringSyncState(pbDir);
2368
+ let dormantStateChanged = false;
2369
+ for (const dormantAsset of dormantDbAssetRows) {
2370
+ for (const { path: filePath, surface } of deriveDormantFilePaths(dormantAsset, cwd)) {
2371
+ // Codex P2: deriveDormantFilePaths emits every known surface path. Honor the run's
2372
+ // target set (allowedTargets = the --surfaces selection, else all manifest surfaces)
2373
+ // so a `--surfaces cursor` run never renames .claude/.codex to .dormant, and surfaces
2374
+ // outside the manifest are skipped silently (no false "could not dormant-mark" warning).
2375
+ if (!allowedTargets.has(surface))
2376
+ continue;
2377
+ // FIX 4: use relative() instead of string-replace for correct cross-platform behaviour.
2378
+ const rel = relative(cwd, filePath);
2379
+ const alreadyReactivated = dormantState.dormantReactivated?.includes(rel) ?? false;
2380
+ const previouslyRenamed = dormantState.dormantRenamed?.includes(rel) ?? false;
2381
+ try {
2382
+ assertSetupWritePath(filePath, perimeterManifest);
2383
+ // WP-426 E4: BUG 1 fix — hands-off set.
2384
+ //
2385
+ // Step 1: permanently hands-off — user already reactivated this path on a
2386
+ // prior run. Skip silently every time until they re-lower or raise in PB.
2387
+ // (Task 7 raise-cleanup must later also prune dormantReactivated for raised
2388
+ // assets — see the `dormantReactivated` comment on AuthoringSyncState.)
2389
+ if (alreadyReactivated) {
2390
+ continue; // leave file untouched; no TEN (already fired on first detection)
2391
+ }
2392
+ // Step 2: FIRST detection of manual reactivation — we previously renamed
2393
+ // this to .dormant but the user renamed it back to a live file. Push the
2394
+ // drift TEN exactly once, add to the permanent hands-off set, remove from
2395
+ // dormantRenamed, leave file untouched.
2396
+ if (previouslyRenamed && existsSync(filePath) && !existsSync(`${filePath}.dormant`)) {
2397
+ syncDriftTensToFire.push(`Dormant asset ${dormantAsset.entryId} was manually reactivated at ${rel}; left untouched. Re-lower or raise it in PB to resync.`);
2398
+ logErr(`Warning: ${rel} was manually un-dormanted; leaving it in place (drift).`);
2399
+ dormantState.dormantReactivated = [...(dormantState.dormantReactivated ?? []), rel];
2400
+ dormantState.dormantRenamed = (dormantState.dormantRenamed ?? []).filter((p) => p !== rel);
2401
+ dormantStateChanged = true;
2402
+ continue;
2403
+ }
2404
+ // Step 3: normal lowering — write dormant marker + rename to .dormant.
2405
+ // WP-426 E4 spec: every rename TARGET must also pass the perimeter guard.
2406
+ // Check the post-rename .dormant path BEFORE any FS mutation, so a guard
2407
+ // failure can't leave a half-dormant file (marker appended but not renamed).
2408
+ assertSetupWritePath(`${filePath}.dormant`, perimeterManifest);
2409
+ const markerResult = writeDormantMarkerToFile(filePath);
2410
+ if (markerResult === 'written')
2411
+ log(`Dormant marker written: ${filePath}`);
2412
+ const renameResult = renameSurfaceForDormancy(filePath);
2413
+ if (renameResult === 'renamed' || renameResult === 'replaced') {
2414
+ // dormantMarkedPaths holds post-rename .dormant paths (not the original surface paths).
2415
+ dormantMarkedPaths.push(`${filePath}.dormant`);
2416
+ if (!dormantState.dormantRenamed?.includes(rel)) {
2417
+ dormantState.dormantRenamed = [...(dormantState.dormantRenamed ?? []), rel];
2418
+ dormantStateChanged = true;
2419
+ }
2420
+ log(`Dormant rename: ${filePath} → ${filePath}.dormant`);
2421
+ }
2422
+ else if (renameResult === 'drift') {
2423
+ // Codex P1: an edited .dormant already exists alongside the live file. Preserve
2424
+ // both and flag, rather than overwriting the user's edited dormant copy.
2425
+ syncDriftTensToFire.push(`Edited dormant copy ${rel}.dormant diverges from the live surface for ${dormantAsset.entryId}; left both in place. Resolve manually.`);
2426
+ logErr(`Warning: ${rel}.dormant diverges from the live file; not replacing (possible manual edit).`);
2427
+ }
2428
+ }
2429
+ catch (err) {
2430
+ logErr(`Warning: could not dormant-mark ${filePath} — ${err instanceof Error ? err.message : String(err)}`);
2431
+ }
2432
+ }
2433
+ }
2434
+ if (dormantMarkedPaths.length > 0) {
2435
+ log('Run `pb setup observe --purge` to remove these instead of dormant-renaming.');
2436
+ }
2437
+ if (dormantStateChanged) {
2438
+ try {
2439
+ saveAuthoringSyncState(pbDir, dormantState);
2440
+ }
2441
+ catch (err) {
2442
+ logErr(`Warning: could not persist dormancy state — ${err instanceof Error ? err.message : String(err)}`);
2443
+ }
2444
+ }
2445
+ }
2446
+ // 8b. Raise-cleanup (WP-426 E4): for assets active this run, drop any orphan
2447
+ // <surface>.dormant left by a prior lowering (active surface was re-projected
2448
+ // fresh above). User-edited dormant copies are preserved + flagged. Fail-open.
2449
+ if (writeMode) {
2450
+ const raiseState = loadAuthoringSyncState(pbDir);
2451
+ let raiseStateChanged = false;
2452
+ const dormantIds = new Set(dormantDbAssetRows.map((a) => a.entryId));
2453
+ const activeRows = dbAssetRows.filter((a) => !dormantIds.has(a.entryId) &&
2454
+ (a.assetKind === 'skill' || a.assetKind === 'rule' || a.assetKind === 'hook'));
2455
+ for (const asset of activeRows) {
2456
+ // Codex P2: if this asset's body fetch failed, the write loop did NOT reproject its
2457
+ // surfaces this run (same skip the authoring loop applies), so there's no fresh surface
2458
+ // to reconcile against — skip raise-cleanup to avoid acting on stale content.
2459
+ if (bodyFetchFailedEntryIds.has(asset.entryId))
2460
+ continue;
2461
+ for (const { path: filePath, surface } of deriveDormantFilePaths(asset, cwd)) {
2462
+ // Codex P2: honor the run's target set (parity with the dormant pass) — a
2463
+ // `--surfaces cursor` run must not touch .claude/.codex .dormant files, and surfaces
2464
+ // outside the manifest are skipped silently (no false "raise-cleanup failed" warning).
2465
+ if (!allowedTargets.has(surface))
2466
+ continue;
2467
+ const rel = relative(cwd, filePath);
2468
+ try {
2469
+ assertSetupWritePath(filePath, perimeterManifest); // WP-426 E4: perimeter before any FS mutation (parity with the dormant pass)
2470
+ // E4 spec parity (review): restoreSurfaceFromDormant deletes the .dormant
2471
+ // sibling, so guard that delete TARGET too — exactly as the lowering pass
2472
+ // guards both filePath and ${filePath}.dormant.
2473
+ assertSetupWritePath(`${filePath}.dormant`, perimeterManifest);
2474
+ const r = restoreSurfaceFromDormant(filePath);
2475
+ if (r === 'restored') {
2476
+ log(`Raised: removed superseded ${filePath}.dormant`);
2477
+ }
2478
+ else if (r === 'orphan-drift') {
2479
+ syncDriftTensToFire.push(`Dormant copy ${rel}.dormant was edited while dormant for ${asset.entryId}; preserved on raise. Resolve manually.`);
2480
+ logErr(`Warning: ${rel}.dormant differs from the freshly raised projection; left in place.`);
2481
+ }
2482
+ // Prune the registry only when no .dormant sibling remains for this surface:
2483
+ // • 'restored' removed it, or
2484
+ // • it was already gone (manual .dormant→live reactivation; 'skipped', no .dormant).
2485
+ // Carry-over obligation (Phase 3) + Codex P1: the manual-reactivation case must
2486
+ // leave the hands-off set so a future lowering can re-evaluate the surface.
2487
+ // Codex P2: but if a .dormant STILL exists (surface-filtered 'skipped' with no
2488
+ // fresh live file, or a preserved 'orphan-drift'), KEEP the registry evidence —
2489
+ // otherwise a later manual .dormant→live reactivation would not be recognized as
2490
+ // previouslyRenamed and would be silently re-dormanted on the next lowering.
2491
+ if (!existsSync(`${filePath}.dormant`)) {
2492
+ if (raiseState.dormantRenamed?.includes(rel)) {
2493
+ raiseState.dormantRenamed = raiseState.dormantRenamed.filter((p) => p !== rel);
2494
+ raiseStateChanged = true;
2495
+ }
2496
+ if (raiseState.dormantReactivated?.includes(rel)) {
2497
+ raiseState.dormantReactivated = raiseState.dormantReactivated.filter((p) => p !== rel);
2498
+ raiseStateChanged = true;
2499
+ }
2500
+ }
2501
+ }
2502
+ catch (err) {
2503
+ logErr(`Warning: raise-cleanup failed for ${filePath} — ${err instanceof Error ? err.message : String(err)}`);
2504
+ }
2505
+ }
2506
+ }
2507
+ if (raiseStateChanged) {
2508
+ try {
2509
+ saveAuthoringSyncState(pbDir, raiseState);
2510
+ }
2511
+ catch { /* fail-open */ }
2512
+ }
2513
+ }
2514
+ // 8. User-owned files left untouched are NOT drift (TEN-2150).
2515
+ // A marker-less file at an adapter path is the user's own file: handshake never
2516
+ // wrote it, so there is nothing to "sync" and no draft TEN is logged. These files
2517
+ // are surfaced benignly under "Skipped:" and relayed to the connect screen via
2518
+ // report.userOwnedSkipped. (The legitimate "tampered" and authoring-sync drift
2519
+ // signals below are unaffected.)
2520
+ if (syncDriftTensToFire.length > 0) {
2521
+ const session = readSession();
2522
+ if (session) {
2523
+ for (const driftDescription of syncDriftTensToFire) {
2524
+ kernelCallWithSession('chain.createEntry', {
2525
+ collectionSlug: 'tensions',
2526
+ name: 'TEN: setup authoring sync drift — repo wins',
2527
+ status: 'draft',
2528
+ data: {
2529
+ kind: 'drift',
2530
+ description: driftDescription,
2531
+ },
2532
+ sessionId: session.sessionId,
2533
+ createdBy: `agent:${session.sessionId}`,
2534
+ }).catch(() => { });
2535
+ }
2536
+ }
2537
+ }
2538
+ // 8. Case-collision TENs (WP-379 S5b).
2539
+ // These are distinct from drift TENs: they record ambiguous filename collisions
2540
+ // where the "newest mtime wins" heuristic was applied. They always fire on a
2541
+ // detected collision (collision is a data quality issue, not a drift issue).
2542
+ if (collisionTensToFire.length > 0) {
2543
+ const session = readSession();
2544
+ if (session) {
2545
+ for (const tenDescription of collisionTensToFire) {
2546
+ kernelCallWithSession('chain.createEntry', {
2547
+ collectionSlug: 'tensions',
2548
+ name: `TEN: handshake case-collision — ambiguous filename resolved by mtime`,
2549
+ // Collision audit TENs intentionally stay draft — they need explicit human review,
2550
+ // not auto-commit, even in Open mode (mirrors smart-capture.ts recordCommitFailure).
2551
+ status: 'draft',
2552
+ data: { description: tenDescription },
2553
+ sessionId: session.sessionId,
2554
+ createdBy: `agent:${session.sessionId}`,
2555
+ }).catch(() => { });
2556
+ }
2557
+ }
2558
+ }
2559
+ // 8b. Setup receipt — record which assets were materialized (apply mode only)
2560
+ // Fail-open: receipt write is advisory, never blocks the handshake.
2561
+ if (applyMode) {
2562
+ const session = readSession();
2563
+ const caller = session ? kernelCallWithSession : kernelCall;
2564
+ try {
2565
+ const currentState = await caller('setup.getCurrentSetupState', {});
2566
+ const fromMode = currentState?.effectiveMode ?? 'observe';
2567
+ if (fromMode !== manifestStatus.mode) {
2568
+ await caller('setup.recordTransition', {
2569
+ fromMode,
2570
+ toMode: manifestStatus.mode,
2571
+ parseStatus: manifestStatus.parseStatus,
2572
+ surfaces: manifestStatus.surfaces,
2573
+ lock: manifestStatus.lock,
2574
+ });
2575
+ if (modeRank(manifestStatus.mode) < modeRank(fromMode)) {
2576
+ trackEvent('setup.transition.lowered', { fromMode, toMode: manifestStatus.mode });
2577
+ }
2578
+ }
2579
+ }
2580
+ catch (err) {
2581
+ trackEvent('setup.transition.write_failed', { error: err instanceof Error ? err.message : String(err) });
2582
+ logErr(`Warning: could not record setup transition — ${err instanceof Error ? err.message : String(err)}`);
2583
+ }
2584
+ }
2585
+ if (writeMode) {
2586
+ const session = readSession();
2587
+ const caller = session ? kernelCallWithSession : kernelCall;
2588
+ try {
2589
+ const receiptResult = await caller('setup.materializeSetup', {});
2590
+ if (receiptResult?.assetCount > 0) {
2591
+ log(`Setup receipt: ${receiptResult.assetCount} asset(s) recorded.`);
2592
+ }
2593
+ }
2594
+ catch (err) {
2595
+ trackEvent('setup.receipt.write_failed', { error: err instanceof Error ? err.message : String(err) });
2596
+ logErr(`Warning: could not write setup receipt — ${err instanceof Error ? err.message : String(err)}`);
2597
+ }
364
2598
  }
365
- // 8. Report
2599
+ // 9. Report
366
2600
  const report = {
367
2601
  filesWritten,
368
2602
  filesSkipped,
369
2603
  matchedEntries,
370
2604
  searchQueries: uniqueQueries,
371
2605
  repo,
2606
+ codexWarnings: codexWarnings.length > 0 ? codexWarnings : undefined,
2607
+ chainRulesStats: chainRulesStats ?? undefined,
2608
+ chainGaps: chainGaps.length > 0 ? chainGaps : undefined,
2609
+ adoptedCount: adoptedRulesCount,
2610
+ rejectedCount: rejectedRulesCount,
2611
+ personalRuleCount: personalRules.length > 0 ? personalRules.length : undefined,
2612
+ personalSkillCount: personalSkills.length > 0 ? personalSkills.length : undefined,
2613
+ registrySource,
2614
+ registryStale,
2615
+ preview: preview ? true : undefined,
2616
+ previewPlan: preview && previewPlan.length > 0 ? previewPlan : undefined,
2617
+ userOwnedSkipped: userOwnedSkipped.length > 0 ? userOwnedSkipped : undefined,
2618
+ // WP-421 S3: three-bucket drift report (doneWhen #17). PB-managed-clean is
2619
+ // the count of files whose marker + hash matched. Tampered files were
2620
+ // resolved (adopted/reverted) above and are reported separately.
2621
+ managedCleanCount: cleanBucketPaths.length > 0 ? cleanBucketPaths.length : undefined,
2622
+ adoptedTamperedPaths: adoptedTamperedPaths.length > 0 ? adoptedTamperedPaths : undefined,
2623
+ revertedTamperedPaths: revertedTamperedPaths.length > 0 ? revertedTamperedPaths : undefined,
372
2624
  };
373
2625
  if (!quiet) {
374
2626
  process.stdout.write('\n');
375
2627
  process.stdout.write(formatHandshakeReport(report) + '\n');
376
2628
  }
2629
+ // Return the report so non-UI callers (e.g. prepareConnectContext) can surface
2630
+ // skipped/user-owned files without re-deriving the skip logic (TEN-2107).
2631
+ return report;
377
2632
  }
2633
+ // WP-426 E3/E4: exported test-only surface (not part of the public CLI API).
2634
+ export const __test = { setupAuthoringPath, renameSurfaceForDormancy, restoreSurfaceFromDormant, loadAuthoringSyncState, MARKER };
378
2635
  //# sourceMappingURL=handshake.js.map