@dsh-ray/lark-channel 0.0.1-seed.0 → 0.2.0

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 (344) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +206 -1
  3. package/bin/dsh-lark-setup.js +37 -0
  4. package/bin/dsh-ray-setup.js +34 -0
  5. package/bin/dsh-supervised-growth-setup.js +34 -0
  6. package/cordis.patch.yml +23 -0
  7. package/docs/operations.md +133 -0
  8. package/docs/progress-security.md +125 -0
  9. package/docs/rsi-setup.md +216 -0
  10. package/docs/setup.md +358 -0
  11. package/docs/supervised-growth.md +95 -0
  12. package/lib/adapter.d.ts +185 -0
  13. package/lib/adapter.d.ts.map +1 -0
  14. package/lib/adapter.js +2072 -0
  15. package/lib/adapter.js.map +1 -0
  16. package/lib/answer-card.d.ts +40 -0
  17. package/lib/answer-card.d.ts.map +1 -0
  18. package/lib/answer-card.js +262 -0
  19. package/lib/answer-card.js.map +1 -0
  20. package/lib/approval.d.ts +26 -0
  21. package/lib/approval.d.ts.map +1 -0
  22. package/lib/approval.js +104 -0
  23. package/lib/approval.js.map +1 -0
  24. package/lib/business-cli-install.d.ts +9 -0
  25. package/lib/business-cli-install.d.ts.map +1 -0
  26. package/lib/business-cli-install.js +367 -0
  27. package/lib/business-cli-install.js.map +1 -0
  28. package/lib/business-environment.d.ts +2 -0
  29. package/lib/business-environment.d.ts.map +1 -0
  30. package/lib/business-environment.js +16 -0
  31. package/lib/business-environment.js.map +1 -0
  32. package/lib/business-skill.d.ts +32 -0
  33. package/lib/business-skill.d.ts.map +1 -0
  34. package/lib/business-skill.js +202 -0
  35. package/lib/business-skill.js.map +1 -0
  36. package/lib/command-card.d.ts +3 -0
  37. package/lib/command-card.d.ts.map +1 -0
  38. package/lib/command-card.js +480 -0
  39. package/lib/command-card.js.map +1 -0
  40. package/lib/config.d.ts +32 -0
  41. package/lib/config.d.ts.map +1 -0
  42. package/lib/config.js +115 -0
  43. package/lib/config.js.map +1 -0
  44. package/lib/group-thread.d.ts +20 -0
  45. package/lib/group-thread.d.ts.map +1 -0
  46. package/lib/group-thread.js +36 -0
  47. package/lib/group-thread.js.map +1 -0
  48. package/lib/index.d.ts +34 -0
  49. package/lib/index.d.ts.map +1 -0
  50. package/lib/index.js +53 -0
  51. package/lib/index.js.map +1 -0
  52. package/lib/launchd-control.d.ts +24 -0
  53. package/lib/launchd-control.d.ts.map +1 -0
  54. package/lib/launchd-control.js +68 -0
  55. package/lib/launchd-control.js.map +1 -0
  56. package/lib/launchd-identity.d.ts +34 -0
  57. package/lib/launchd-identity.d.ts.map +1 -0
  58. package/lib/launchd-identity.js +184 -0
  59. package/lib/launchd-identity.js.map +1 -0
  60. package/lib/launchd-target-lock.d.ts +6 -0
  61. package/lib/launchd-target-lock.d.ts.map +1 -0
  62. package/lib/launchd-target-lock.js +48 -0
  63. package/lib/launchd-target-lock.js.map +1 -0
  64. package/lib/launchd.d.ts +62 -0
  65. package/lib/launchd.d.ts.map +1 -0
  66. package/lib/launchd.js +370 -0
  67. package/lib/launchd.js.map +1 -0
  68. package/lib/model-picker.d.ts +24 -0
  69. package/lib/model-picker.d.ts.map +1 -0
  70. package/lib/model-picker.js +95 -0
  71. package/lib/model-picker.js.map +1 -0
  72. package/lib/normalize.d.ts +12 -0
  73. package/lib/normalize.d.ts.map +1 -0
  74. package/lib/normalize.js +85 -0
  75. package/lib/normalize.js.map +1 -0
  76. package/lib/permission-picker.d.ts +29 -0
  77. package/lib/permission-picker.d.ts.map +1 -0
  78. package/lib/permission-picker.js +145 -0
  79. package/lib/permission-picker.js.map +1 -0
  80. package/lib/progress.d.ts +34 -0
  81. package/lib/progress.d.ts.map +1 -0
  82. package/lib/progress.js +354 -0
  83. package/lib/progress.js.map +1 -0
  84. package/lib/resident.d.ts +13 -0
  85. package/lib/resident.d.ts.map +1 -0
  86. package/lib/resident.js +41 -0
  87. package/lib/resident.js.map +1 -0
  88. package/lib/rsi-authority-config.d.ts +29 -0
  89. package/lib/rsi-authority-config.d.ts.map +1 -0
  90. package/lib/rsi-authority-config.js +233 -0
  91. package/lib/rsi-authority-config.js.map +1 -0
  92. package/lib/rsi-authority-resources.d.ts +36 -0
  93. package/lib/rsi-authority-resources.d.ts.map +1 -0
  94. package/lib/rsi-authority-resources.js +139 -0
  95. package/lib/rsi-authority-resources.js.map +1 -0
  96. package/lib/rsi-authority-runtime.d.ts +51 -0
  97. package/lib/rsi-authority-runtime.d.ts.map +1 -0
  98. package/lib/rsi-authority-runtime.js +496 -0
  99. package/lib/rsi-authority-runtime.js.map +1 -0
  100. package/lib/rsi-bootstrap-manifest.d.ts +53 -0
  101. package/lib/rsi-bootstrap-manifest.d.ts.map +1 -0
  102. package/lib/rsi-bootstrap-manifest.js +507 -0
  103. package/lib/rsi-bootstrap-manifest.js.map +1 -0
  104. package/lib/rsi-bootstrap.d.ts +61 -0
  105. package/lib/rsi-bootstrap.d.ts.map +1 -0
  106. package/lib/rsi-bootstrap.js +597 -0
  107. package/lib/rsi-bootstrap.js.map +1 -0
  108. package/lib/rsi-build.d.ts +76 -0
  109. package/lib/rsi-build.d.ts.map +1 -0
  110. package/lib/rsi-build.js +210 -0
  111. package/lib/rsi-build.js.map +1 -0
  112. package/lib/rsi-host-authorities.d.ts +18 -0
  113. package/lib/rsi-host-authorities.d.ts.map +1 -0
  114. package/lib/rsi-host-authorities.js +129 -0
  115. package/lib/rsi-host-authorities.js.map +1 -0
  116. package/lib/rsi-host-update.d.ts +179 -0
  117. package/lib/rsi-host-update.d.ts.map +1 -0
  118. package/lib/rsi-host-update.js +568 -0
  119. package/lib/rsi-host-update.js.map +1 -0
  120. package/lib/rsi-install-inputs.d.ts +45 -0
  121. package/lib/rsi-install-inputs.d.ts.map +1 -0
  122. package/lib/rsi-install-inputs.js +345 -0
  123. package/lib/rsi-install-inputs.js.map +1 -0
  124. package/lib/rsi-install.d.ts +117 -0
  125. package/lib/rsi-install.d.ts.map +1 -0
  126. package/lib/rsi-install.js +791 -0
  127. package/lib/rsi-install.js.map +1 -0
  128. package/lib/rsi-linux-cgroup-roots.d.ts +3 -0
  129. package/lib/rsi-linux-cgroup-roots.d.ts.map +1 -0
  130. package/lib/rsi-linux-cgroup-roots.js +3 -0
  131. package/lib/rsi-linux-cgroup-roots.js.map +1 -0
  132. package/lib/rsi-linux-creation-build.d.ts +36 -0
  133. package/lib/rsi-linux-creation-build.d.ts.map +1 -0
  134. package/lib/rsi-linux-creation-build.js +213 -0
  135. package/lib/rsi-linux-creation-build.js.map +1 -0
  136. package/lib/rsi-linux-native-materialize.d.ts +31 -0
  137. package/lib/rsi-linux-native-materialize.d.ts.map +1 -0
  138. package/lib/rsi-linux-native-materialize.js +162 -0
  139. package/lib/rsi-linux-native-materialize.js.map +1 -0
  140. package/lib/rsi-linux-native-relocation.d.ts +124 -0
  141. package/lib/rsi-linux-native-relocation.d.ts.map +1 -0
  142. package/lib/rsi-linux-native-relocation.js +1571 -0
  143. package/lib/rsi-linux-native-relocation.js.map +1 -0
  144. package/lib/rsi-linux-native-resources.d.ts +97 -0
  145. package/lib/rsi-linux-native-resources.d.ts.map +1 -0
  146. package/lib/rsi-linux-native-resources.js +521 -0
  147. package/lib/rsi-linux-native-resources.js.map +1 -0
  148. package/lib/rsi-linux-repository-bootstrap.d.ts +17 -0
  149. package/lib/rsi-linux-repository-bootstrap.d.ts.map +1 -0
  150. package/lib/rsi-linux-repository-bootstrap.js +317 -0
  151. package/lib/rsi-linux-repository-bootstrap.js.map +1 -0
  152. package/lib/rsi-linux-runner-states.d.ts +20 -0
  153. package/lib/rsi-linux-runner-states.d.ts.map +1 -0
  154. package/lib/rsi-linux-runner-states.js +70 -0
  155. package/lib/rsi-linux-runner-states.js.map +1 -0
  156. package/lib/rsi-linux-runtime-bootstrap.d.ts +10 -0
  157. package/lib/rsi-linux-runtime-bootstrap.d.ts.map +1 -0
  158. package/lib/rsi-linux-runtime-bootstrap.js +38 -0
  159. package/lib/rsi-linux-runtime-bootstrap.js.map +1 -0
  160. package/lib/rsi-linux-source-build.d.ts +28 -0
  161. package/lib/rsi-linux-source-build.d.ts.map +1 -0
  162. package/lib/rsi-linux-source-build.js +43 -0
  163. package/lib/rsi-linux-source-build.js.map +1 -0
  164. package/lib/rsi-local-activation.d.ts +78 -0
  165. package/lib/rsi-local-activation.d.ts.map +1 -0
  166. package/lib/rsi-local-activation.js +486 -0
  167. package/lib/rsi-local-activation.js.map +1 -0
  168. package/lib/rsi-local-cohort.d.ts +70 -0
  169. package/lib/rsi-local-cohort.d.ts.map +1 -0
  170. package/lib/rsi-local-cohort.js +810 -0
  171. package/lib/rsi-local-cohort.js.map +1 -0
  172. package/lib/rsi-local-install.d.ts +70 -0
  173. package/lib/rsi-local-install.d.ts.map +1 -0
  174. package/lib/rsi-local-install.js +363 -0
  175. package/lib/rsi-local-install.js.map +1 -0
  176. package/lib/rsi-local-pair-install.d.ts +53 -0
  177. package/lib/rsi-local-pair-install.d.ts.map +1 -0
  178. package/lib/rsi-local-pair-install.js +215 -0
  179. package/lib/rsi-local-pair-install.js.map +1 -0
  180. package/lib/rsi-local-profile-update.d.ts +25 -0
  181. package/lib/rsi-local-profile-update.d.ts.map +1 -0
  182. package/lib/rsi-local-profile-update.js +226 -0
  183. package/lib/rsi-local-profile-update.js.map +1 -0
  184. package/lib/rsi-local-roots-extension.d.ts +11 -0
  185. package/lib/rsi-local-roots-extension.d.ts.map +1 -0
  186. package/lib/rsi-local-roots-extension.js +31 -0
  187. package/lib/rsi-local-roots-extension.js.map +1 -0
  188. package/lib/rsi-local-update-build.d.ts +37 -0
  189. package/lib/rsi-local-update-build.d.ts.map +1 -0
  190. package/lib/rsi-local-update-build.js +75 -0
  191. package/lib/rsi-local-update-build.js.map +1 -0
  192. package/lib/rsi-local-update.d.ts +39 -0
  193. package/lib/rsi-local-update.d.ts.map +1 -0
  194. package/lib/rsi-local-update.js +205 -0
  195. package/lib/rsi-local-update.js.map +1 -0
  196. package/lib/rsi-memory-learning.d.ts +36 -0
  197. package/lib/rsi-memory-learning.d.ts.map +1 -0
  198. package/lib/rsi-memory-learning.js +111 -0
  199. package/lib/rsi-memory-learning.js.map +1 -0
  200. package/lib/rsi-native-install.d.ts +38 -0
  201. package/lib/rsi-native-install.d.ts.map +1 -0
  202. package/lib/rsi-native-install.js +312 -0
  203. package/lib/rsi-native-install.js.map +1 -0
  204. package/lib/rsi-native-launchd.d.ts +7 -0
  205. package/lib/rsi-native-launchd.d.ts.map +1 -0
  206. package/lib/rsi-native-launchd.js +49 -0
  207. package/lib/rsi-native-launchd.js.map +1 -0
  208. package/lib/rsi-native-profile.d.ts +76 -0
  209. package/lib/rsi-native-profile.d.ts.map +1 -0
  210. package/lib/rsi-native-profile.js +530 -0
  211. package/lib/rsi-native-profile.js.map +1 -0
  212. package/lib/rsi-native-runtime-bootstrap.d.ts +44 -0
  213. package/lib/rsi-native-runtime-bootstrap.d.ts.map +1 -0
  214. package/lib/rsi-native-runtime-bootstrap.js +619 -0
  215. package/lib/rsi-native-runtime-bootstrap.js.map +1 -0
  216. package/lib/rsi-ordinary-use-learning.d.ts +60 -0
  217. package/lib/rsi-ordinary-use-learning.d.ts.map +1 -0
  218. package/lib/rsi-ordinary-use-learning.js +103 -0
  219. package/lib/rsi-ordinary-use-learning.js.map +1 -0
  220. package/lib/rsi-owner-profile.d.ts +37 -0
  221. package/lib/rsi-owner-profile.d.ts.map +1 -0
  222. package/lib/rsi-owner-profile.js +178 -0
  223. package/lib/rsi-owner-profile.js.map +1 -0
  224. package/lib/rsi-plugin-creation.d.ts +46 -0
  225. package/lib/rsi-plugin-creation.d.ts.map +1 -0
  226. package/lib/rsi-plugin-creation.js +227 -0
  227. package/lib/rsi-plugin-creation.js.map +1 -0
  228. package/lib/rsi-plugin-revision.d.ts +38 -0
  229. package/lib/rsi-plugin-revision.d.ts.map +1 -0
  230. package/lib/rsi-plugin-revision.js +221 -0
  231. package/lib/rsi-plugin-revision.js.map +1 -0
  232. package/lib/rsi-profile.d.ts +81 -0
  233. package/lib/rsi-profile.d.ts.map +1 -0
  234. package/lib/rsi-profile.js +551 -0
  235. package/lib/rsi-profile.js.map +1 -0
  236. package/lib/rsi-release-build.d.ts +59 -0
  237. package/lib/rsi-release-build.d.ts.map +1 -0
  238. package/lib/rsi-release-build.js +219 -0
  239. package/lib/rsi-release-build.js.map +1 -0
  240. package/lib/rsi-scanner-retirement-config.d.ts +34 -0
  241. package/lib/rsi-scanner-retirement-config.d.ts.map +1 -0
  242. package/lib/rsi-scanner-retirement-config.js +272 -0
  243. package/lib/rsi-scanner-retirement-config.js.map +1 -0
  244. package/lib/rsi-scanner-retirement-runtime.d.ts +23 -0
  245. package/lib/rsi-scanner-retirement-runtime.d.ts.map +1 -0
  246. package/lib/rsi-scanner-retirement-runtime.js +98 -0
  247. package/lib/rsi-scanner-retirement-runtime.js.map +1 -0
  248. package/lib/rsi-scanner-retirement-stage.d.ts +55 -0
  249. package/lib/rsi-scanner-retirement-stage.d.ts.map +1 -0
  250. package/lib/rsi-scanner-retirement-stage.js +181 -0
  251. package/lib/rsi-scanner-retirement-stage.js.map +1 -0
  252. package/lib/rsi-scanner-retirement-transition.d.ts +57 -0
  253. package/lib/rsi-scanner-retirement-transition.d.ts.map +1 -0
  254. package/lib/rsi-scanner-retirement-transition.js +330 -0
  255. package/lib/rsi-scanner-retirement-transition.js.map +1 -0
  256. package/lib/rsi-service-environment.d.ts +6 -0
  257. package/lib/rsi-service-environment.d.ts.map +1 -0
  258. package/lib/rsi-service-environment.js +113 -0
  259. package/lib/rsi-service-environment.js.map +1 -0
  260. package/lib/rsi-service-setup.d.ts +10 -0
  261. package/lib/rsi-service-setup.d.ts.map +1 -0
  262. package/lib/rsi-service-setup.js +34 -0
  263. package/lib/rsi-service-setup.js.map +1 -0
  264. package/lib/rsi-setup.d.ts +92 -0
  265. package/lib/rsi-setup.d.ts.map +1 -0
  266. package/lib/rsi-setup.js +908 -0
  267. package/lib/rsi-setup.js.map +1 -0
  268. package/lib/rsi-source-maintenance.d.ts +33 -0
  269. package/lib/rsi-source-maintenance.d.ts.map +1 -0
  270. package/lib/rsi-source-maintenance.js +407 -0
  271. package/lib/rsi-source-maintenance.js.map +1 -0
  272. package/lib/rsi-source-update.d.ts +26 -0
  273. package/lib/rsi-source-update.d.ts.map +1 -0
  274. package/lib/rsi-source-update.js +276 -0
  275. package/lib/rsi-source-update.js.map +1 -0
  276. package/lib/rsi-source.d.ts +34 -0
  277. package/lib/rsi-source.d.ts.map +1 -0
  278. package/lib/rsi-source.js +487 -0
  279. package/lib/rsi-source.js.map +1 -0
  280. package/lib/rsi-systemd-bootstrap.d.ts +46 -0
  281. package/lib/rsi-systemd-bootstrap.d.ts.map +1 -0
  282. package/lib/rsi-systemd-bootstrap.js +271 -0
  283. package/lib/rsi-systemd-bootstrap.js.map +1 -0
  284. package/lib/sdk.d.ts +216 -0
  285. package/lib/sdk.d.ts.map +1 -0
  286. package/lib/sdk.js +1990 -0
  287. package/lib/sdk.js.map +1 -0
  288. package/lib/service.d.ts +63 -0
  289. package/lib/service.d.ts.map +1 -0
  290. package/lib/service.js +599 -0
  291. package/lib/service.js.map +1 -0
  292. package/lib/setup-business.d.ts +49 -0
  293. package/lib/setup-business.d.ts.map +1 -0
  294. package/lib/setup-business.js +335 -0
  295. package/lib/setup-business.js.map +1 -0
  296. package/lib/setup-materialization.d.ts +15 -0
  297. package/lib/setup-materialization.d.ts.map +1 -0
  298. package/lib/setup-materialization.js +221 -0
  299. package/lib/setup-materialization.js.map +1 -0
  300. package/lib/setup-profile.d.ts +23 -0
  301. package/lib/setup-profile.d.ts.map +1 -0
  302. package/lib/setup-profile.js +1248 -0
  303. package/lib/setup-profile.js.map +1 -0
  304. package/lib/setup.d.ts +298 -0
  305. package/lib/setup.d.ts.map +1 -0
  306. package/lib/setup.js +3310 -0
  307. package/lib/setup.js.map +1 -0
  308. package/lib/supervised-growth-profile.d.ts +111 -0
  309. package/lib/supervised-growth-profile.d.ts.map +1 -0
  310. package/lib/supervised-growth-profile.js +903 -0
  311. package/lib/supervised-growth-profile.js.map +1 -0
  312. package/lib/supervised-growth-setup.d.ts +201 -0
  313. package/lib/supervised-growth-setup.d.ts.map +1 -0
  314. package/lib/supervised-growth-setup.js +1005 -0
  315. package/lib/supervised-growth-setup.js.map +1 -0
  316. package/lib/systemd.d.ts +70 -0
  317. package/lib/systemd.d.ts.map +1 -0
  318. package/lib/systemd.js +308 -0
  319. package/lib/systemd.js.map +1 -0
  320. package/lib/tool-approval.d.ts +22 -0
  321. package/lib/tool-approval.d.ts.map +1 -0
  322. package/lib/tool-approval.js +107 -0
  323. package/lib/tool-approval.js.map +1 -0
  324. package/lib/types.d.ts +351 -0
  325. package/lib/types.d.ts.map +1 -0
  326. package/lib/types.js +11 -0
  327. package/lib/types.js.map +1 -0
  328. package/lib/user-question.d.ts +31 -0
  329. package/lib/user-question.d.ts.map +1 -0
  330. package/lib/user-question.js +161 -0
  331. package/lib/user-question.js.map +1 -0
  332. package/lib/version.d.ts +2 -0
  333. package/lib/version.d.ts.map +1 -0
  334. package/lib/version.js +2 -0
  335. package/lib/version.js.map +1 -0
  336. package/lib/windows-task.d.ts +42 -0
  337. package/lib/windows-task.d.ts.map +1 -0
  338. package/lib/windows-task.js +143 -0
  339. package/lib/windows-task.js.map +1 -0
  340. package/lib/ws-card-callback.d.ts +7 -0
  341. package/lib/ws-card-callback.d.ts.map +1 -0
  342. package/lib/ws-card-callback.js +51 -0
  343. package/lib/ws-card-callback.js.map +1 -0
  344. package/package.json +162 -4
@@ -0,0 +1,568 @@
1
+ import { isDeepStrictEqual } from 'node:util';
2
+ import { createHash, createPublicKey, sign, verify } from 'node:crypto';
3
+ import { isAbsolute, resolve } from 'node:path';
4
+ import { isAlias, isMap, isScalar, isSeq, parseDocument } from 'yaml';
5
+ import { runtimeConfigDigest } from '@dsh-ray/plugin-control-plane';
6
+ import { rawLoaderConfig } from './rsi-bootstrap-manifest.js';
7
+ function fail(message) { throw new Error(`rsi host update: ${message}`); }
8
+ const sha256 = (source) => createHash('sha256').update(source).digest('hex');
9
+ const hex = /^[a-f0-9]{64}$/u;
10
+ const profileName = /^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$/u;
11
+ const backupName = /^\.([A-Za-z0-9][A-Za-z0-9._-]{0,63})\.plugin-backup-([A-Za-z0-9-]{1,36})$/u;
12
+ function canonical(value) {
13
+ if (Array.isArray(value))
14
+ return `[${value.map(canonical).join(',')}]`;
15
+ if (record(value))
16
+ return `{${Object.entries(value).filter(([, item]) => item !== undefined)
17
+ .sort(([a], [b]) => a.localeCompare(b)).map(([key, item]) => `${JSON.stringify(key)}:${canonical(item)}`).join(',')}}`;
18
+ return JSON.stringify(value);
19
+ }
20
+ function adoptionFileIdentityShape(file) {
21
+ return record(file) && isDeepStrictEqual(Object.keys(file).sort(), ['dev', 'ino', 'uid', 'gid', 'mode', 'nlink'].sort())
22
+ && ['dev', 'ino'].every(key => typeof file[key] === 'string' && /^(?:0|[1-9][0-9]*)$/u.test(file[key]))
23
+ && ['uid', 'gid', 'mode'].every(key => Number.isSafeInteger(file[key]) && Number(file[key]) >= 0)
24
+ && file.nlink === 1;
25
+ }
26
+ function emptyAdoptionHandoffShape(value, resources, transactionId) {
27
+ if (!record(value) || !isDeepStrictEqual(Object.keys(value).sort(), ['protocol', 'transactionId', 'logicalPath', 'planDigest', 'proofDigest', 'hostAnchor', 'fileIdentity'].sort())
28
+ || value.protocol !== 'dsh-owner-empty-adoption-handoff/v1' || value.transactionId !== transactionId
29
+ || value.logicalPath !== `${resources.stateRoot}/control-plane/creation-adoptions.sqlite`
30
+ || !['planDigest', 'proofDigest', 'hostAnchor'].every(key => typeof value[key] === 'string' && hex.test(value[key]))
31
+ || !adoptionFileIdentityShape(value.fileIdentity))
32
+ fail('empty adoption handoff metadata differs');
33
+ }
34
+ function sealedAdoptionHandoffShape(value, resources, transactionId) {
35
+ const archiveDirectory = `${resources.stateRoot}/control-plane/creation-adoptions.sealed/`;
36
+ const usage = record(value) && record(value.inheritedUsage) ? value.inheritedUsage : undefined;
37
+ const live = record(value) ? value.fileIdentity : undefined, archive = record(value) ? value.archiveFileIdentity : undefined;
38
+ // The archive file name is the old authority digest (unique per authority, prevents ABA), not archiveDigest.
39
+ if (!record(value) || !isDeepStrictEqual(Object.keys(value).sort(), ['protocol', 'transactionId', 'logicalPath', 'archiveLogicalPath', 'planDigest', 'proofDigest', 'hostAnchor',
40
+ 'archiveDigest', 'pendingReverificationDigest', 'fileIdentity', 'archiveFileIdentity', 'inheritedUsage'].sort())
41
+ || value.protocol !== 'dsh-owner-sealed-adoption-handoff/v1' || value.transactionId !== transactionId
42
+ || value.logicalPath !== `${resources.stateRoot}/control-plane/creation-adoptions.sqlite`
43
+ || typeof value.archiveLogicalPath !== 'string' || !value.archiveLogicalPath.startsWith(archiveDirectory)
44
+ || !/^[a-f0-9]{64}\.sqlite$/u.test(value.archiveLogicalPath.slice(archiveDirectory.length))
45
+ || !['planDigest', 'proofDigest', 'hostAnchor', 'archiveDigest', 'pendingReverificationDigest']
46
+ .every(key => typeof value[key] === 'string' && hex.test(value[key]))
47
+ || !adoptionFileIdentityShape(live) || !adoptionFileIdentityShape(archive) || !record(live) || !record(archive)
48
+ || live.dev === archive.dev && live.ino === archive.ino || (Number(archive.mode) & 0o777) !== 0o400
49
+ || !record(usage) || !isDeepStrictEqual(Object.keys(usage).sort(), ['adoptions', 'calls'])
50
+ || !['adoptions', 'calls'].every(key => Number.isSafeInteger(usage[key]) && Number(usage[key]) >= 0))
51
+ fail('sealed adoption handoff metadata differs');
52
+ }
53
+ const REVIEW_LANES = ['creation-reviews', 'revision-reviews', 'revision-regressions'];
54
+ function reviewGrantHandoffsShape(value, transactionId) {
55
+ const lanes = record(value) && Array.isArray(value.lanes) ? value.lanes : undefined;
56
+ if (!record(value) || !isDeepStrictEqual(Object.keys(value).sort(), ['digest', 'lanes', 'protocol', 'transactionId'])
57
+ || value.protocol !== 'dsh-owner-review-grant-handoffs/v1' || value.transactionId !== transactionId
58
+ || typeof value.digest !== 'string' || !hex.test(value.digest)
59
+ || !lanes || lanes.length < 1 || lanes.length > REVIEW_LANES.length
60
+ || lanes.some(lane => !record(lane) || !isDeepStrictEqual(Object.keys(lane).sort(), ['headDigest', 'lane', 'status'])
61
+ || !REVIEW_LANES.includes(lane.lane)
62
+ || !(lane.status === 'empty' && lane.headDigest === null
63
+ || lane.status === 'handed-off' && typeof lane.headDigest === 'string' && hex.test(lane.headDigest)))
64
+ // Configured lanes only, each once, in the fixed lane order; present only when some lane handed off.
65
+ || lanes.some((lane, index) => index > 0 && REVIEW_LANES.indexOf(lane.lane)
66
+ <= REVIEW_LANES.indexOf(lanes[index - 1].lane))
67
+ || !lanes.some(lane => lane.status === 'handed-off'))
68
+ fail('review grant handoffs metadata differs');
69
+ }
70
+ export function rsiHostUpdateOverlayDigest(value) { return sha256(canonical(value)); }
71
+ function overlayShape(value, resources, home, profile, bootstrapDigest, sequence, previousDigest) {
72
+ if (!record(value) || !isDeepStrictEqual(Object.keys(value).sort(), ['schemaVersion', 'kind', 'transactionId', 'dshHome',
73
+ 'targetProfile', 'installationId', 'currentPlanId', 'activationId', 'sequence', 'previousDigest', 'bootstrapDigest', 'files', 'patches',
74
+ 'runtimeReceiptDigest', 'planDigest', 'result', 'issuedAt', 'authority', 'keyId', 'signature'].sort())
75
+ || value.schemaVersion !== 1 || value.kind !== 'rsi-host-update-overlay'
76
+ || value.dshHome !== home || value.targetProfile !== profile || value.installationId !== resources.installationId
77
+ || typeof value.currentPlanId !== 'string' || !value.currentPlanId || typeof value.activationId !== 'string' || !value.activationId
78
+ || value.sequence !== sequence || value.previousDigest !== previousDigest || value.bootstrapDigest !== bootstrapDigest
79
+ || value.authority !== resources.identities.host.authority || value.keyId !== resources.identities.host.keyId
80
+ || typeof value.transactionId !== 'string' || !value.transactionId || !Number.isSafeInteger(value.issuedAt)
81
+ || !hex.test(String(value.runtimeReceiptDigest)) || !hex.test(String(value.planDigest))
82
+ || !record(value.files) || !record(value.patches) || Object.keys(value.files).length > 64
83
+ || Object.keys(value.patches).length < 2 || Object.keys(value.patches).length > 8
84
+ || Object.entries(value.files).some(([path, digest]) => !isAbsolute(path) || resolve(path) !== path
85
+ || !path.startsWith(`${resources.configRoot}/`) || !hex.test(String(digest)))
86
+ || !hex.test(String(value.patches[profile]))
87
+ || !profileName.test(String(record(value.result) ? value.result.coordinatorProfile : undefined))
88
+ || !hex.test(String(value.patches[String(record(value.result) ? value.result.coordinatorProfile : undefined)]))
89
+ || Object.entries(value.patches).some(([name, digest]) => !(profileName.test(name)
90
+ || backupName.exec(name)?.[1] === profile) || !hex.test(String(digest)))
91
+ || !record(value.result) || value.result.targetProfile !== profile || value.result.coordinatorProfile === profile
92
+ || value.result.manifestPath !== `${resources.configRoot}/manifest.json`
93
+ || value.result.manifestDigest !== value.files[value.result.manifestPath]
94
+ || typeof value.signature !== 'string' || value.signature.length > 256)
95
+ fail('overlay shape or scope differs');
96
+ const { signature, ...unsigned } = value;
97
+ const key = createPublicKey(resources.identities.host.publicKeyPem);
98
+ if (key.asymmetricKeyType !== 'ed25519'
99
+ || !verify(null, Buffer.from(canonical(unsigned)), key, Buffer.from(signature, 'base64')))
100
+ fail('overlay signature differs');
101
+ }
102
+ function repairOverlayShape(value, resources, home, profile, bootstrapDigest, sequence, previousDigest) {
103
+ if (!record(value) || !isDeepStrictEqual(Object.keys(value).sort(), ['schemaVersion', 'kind', 'transactionId', 'dshHome',
104
+ 'targetProfile', 'installationId', 'sequence', 'previousDigest', 'bootstrapDigest', 'files', 'patches',
105
+ 'runtimeReceiptDigest', 'planDigest', 'result', 'repair', 'issuedAt', 'authority', 'keyId', 'signature'].sort())
106
+ || value.schemaVersion !== 1 || value.kind !== 'rsi-owner-repair-overlay'
107
+ || value.dshHome !== home || value.targetProfile !== profile || value.installationId !== resources.installationId
108
+ || value.sequence !== sequence || value.previousDigest !== previousDigest || value.bootstrapDigest !== bootstrapDigest
109
+ || value.authority !== resources.identities.host.authority || value.keyId !== resources.identities.host.keyId
110
+ || typeof value.transactionId !== 'string' || !value.transactionId || !Number.isSafeInteger(value.issuedAt)
111
+ || !hex.test(String(value.runtimeReceiptDigest)) || !hex.test(String(value.planDigest))
112
+ || !record(value.files) || !record(value.patches) || Object.keys(value.files).length > 64
113
+ || Object.entries(value.files).some(([path, digest]) => !isAbsolute(path) || resolve(path) !== path
114
+ || !path.startsWith(`${resources.configRoot}/`) || !hex.test(String(digest)))
115
+ || !record(value.result) || !isDeepStrictEqual(Object.keys(value.result).sort(), ['schemaVersion', 'manifestPath', 'manifestDigest', 'targetProfile', 'coordinatorProfile'].sort())
116
+ || value.result.schemaVersion !== 1 || value.result.targetProfile !== profile
117
+ || !profileName.test(String(value.result.coordinatorProfile)) || value.result.coordinatorProfile === profile
118
+ || value.result.manifestPath !== `${resources.configRoot}/manifest.json`
119
+ || value.result.manifestDigest !== value.files[value.result.manifestPath]
120
+ || !isDeepStrictEqual(Object.keys(value.patches).sort(), [profile, value.result.coordinatorProfile].sort())
121
+ || Object.values(value.patches).some(digest => !hex.test(String(digest)))
122
+ || !record(value.repair) || !isDeepStrictEqual(Object.keys(value.repair).sort(), ['sourceMaintenanceDigest', 'before', 'after', 'permissionPreset'].sort())
123
+ || !hex.test(String(value.repair.sourceMaintenanceDigest))
124
+ || !['workspace-write', 'danger-full-access'].includes(String(value.repair.permissionPreset))
125
+ || !record(value.repair.before) || !isDeepStrictEqual(Object.keys(value.repair.before).sort(), ['files', 'patches', 'runtimeReceiptDigest', 'creationReceiptDigest', 'journalDigest'].sort())
126
+ || !record(value.repair.before.files) || !record(value.repair.before.patches)
127
+ || !isDeepStrictEqual(Object.keys(value.repair.before.files).sort(), Object.keys(value.files).sort())
128
+ || !isDeepStrictEqual(Object.keys(value.repair.before.patches).sort(), Object.keys(value.patches).sort())
129
+ || Object.values(value.repair.before.files).some(digest => !hex.test(String(digest)))
130
+ || Object.values(value.repair.before.patches).some(digest => !hex.test(String(digest)))
131
+ || !hex.test(String(value.repair.before.runtimeReceiptDigest))
132
+ || !hex.test(String(value.repair.before.creationReceiptDigest))
133
+ || !hex.test(String(value.repair.before.journalDigest))
134
+ || !record(value.repair.after) || !isDeepStrictEqual(Object.keys(value.repair.after).sort(), ['creationReceiptDigest', 'journalDigest'].sort())
135
+ || !hex.test(String(value.repair.after.creationReceiptDigest)) || !hex.test(String(value.repair.after.journalDigest))
136
+ || typeof value.signature !== 'string' || value.signature.length > 256)
137
+ fail('repair overlay shape or scope differs');
138
+ const { signature, ...unsigned } = value;
139
+ const key = createPublicKey(resources.identities.host.publicKeyPem);
140
+ if (key.asymmetricKeyType !== 'ed25519'
141
+ || !verify(null, Buffer.from(canonical(unsigned)), key, Buffer.from(signature, 'base64')))
142
+ fail('repair overlay signature differs');
143
+ }
144
+ function programUpdateOverlayShape(value, resources, home, profile, bootstrapDigest, sequence, previousDigest) {
145
+ if (!record(value) || !isDeepStrictEqual(Object.keys(value).sort(), ['schemaVersion', 'kind', 'transactionId', 'dshHome',
146
+ 'targetProfile', 'installationId', 'sequence', 'previousDigest', 'bootstrapDigest', 'files', 'patches',
147
+ 'runtimeReceiptDigest', 'planDigest', 'result', 'update', 'issuedAt', 'authority', 'keyId', 'signature'].sort())
148
+ || value.schemaVersion !== 1 || value.kind !== 'rsi-owner-program-update-overlay'
149
+ || value.dshHome !== home || value.targetProfile !== profile || value.installationId !== resources.installationId
150
+ || value.sequence !== sequence || value.previousDigest !== previousDigest || value.bootstrapDigest !== bootstrapDigest
151
+ || value.authority !== resources.identities.host.authority || value.keyId !== resources.identities.host.keyId
152
+ || typeof value.transactionId !== 'string' || !value.transactionId || !Number.isSafeInteger(value.issuedAt)
153
+ || !hex.test(String(value.runtimeReceiptDigest)) || !hex.test(String(value.planDigest))
154
+ || !record(value.files) || !record(value.patches) || Object.keys(value.files).length > 64
155
+ || Object.entries(value.files).some(([path, digest]) => !isAbsolute(path) || resolve(path) !== path
156
+ || !path.startsWith(`${resources.configRoot}/`) || !hex.test(String(digest)))
157
+ || !record(value.result) || !isDeepStrictEqual(Object.keys(value.result).sort(), ['schemaVersion', 'manifestPath', 'manifestDigest', 'targetProfile', 'coordinatorProfile'].sort())
158
+ || value.result.schemaVersion !== 1 || value.result.targetProfile !== profile
159
+ || !profileName.test(String(value.result.coordinatorProfile)) || value.result.coordinatorProfile === profile
160
+ || value.result.manifestPath !== `${resources.configRoot}/manifest.json`
161
+ || value.result.manifestDigest !== value.files[value.result.manifestPath]
162
+ || !isDeepStrictEqual(Object.keys(value.patches).sort(), [profile, value.result.coordinatorProfile].sort())
163
+ || Object.values(value.patches).some(digest => !hex.test(String(digest)))
164
+ || !record(value.update) || !isDeepStrictEqual(Object.keys(value.update).sort(), ['sourceMaintenanceDigest', 'authorityDigest', 'hostPlanDigest', 'before', 'after',
165
+ ...(record(value.update) && Object.hasOwn(value.update, 'emptyAdoptionHandoff') ? ['emptyAdoptionHandoff'] : []),
166
+ ...(record(value.update) && Object.hasOwn(value.update, 'sealedAdoptionHandoff') ? ['sealedAdoptionHandoff'] : []),
167
+ ...(record(value.update) && Object.hasOwn(value.update, 'reviewGrantHandoffs') ? ['reviewGrantHandoffs'] : [])].sort())
168
+ || Object.hasOwn(value.update, 'emptyAdoptionHandoff') && Object.hasOwn(value.update, 'sealedAdoptionHandoff')
169
+ || !hex.test(String(value.update.sourceMaintenanceDigest))
170
+ || !hex.test(String(value.update.authorityDigest))
171
+ || !hex.test(String(value.update.hostPlanDigest))
172
+ || !record(value.update.before) || !isDeepStrictEqual(Object.keys(value.update.before).sort(), ['files', 'patches', 'runtimeReceiptDigest', 'creationReceiptDigest', 'journalDigest'].sort())
173
+ || !record(value.update.before.files) || !record(value.update.before.patches)
174
+ || !isDeepStrictEqual(Object.keys(value.update.before.files).sort(), Object.keys(value.files).sort())
175
+ || !isDeepStrictEqual(Object.keys(value.update.before.patches).sort(), Object.keys(value.patches).sort())
176
+ || Object.values(value.update.before.files).some(digest => !hex.test(String(digest)))
177
+ || Object.values(value.update.before.patches).some(digest => !hex.test(String(digest)))
178
+ || !hex.test(String(value.update.before.runtimeReceiptDigest))
179
+ || !hex.test(String(value.update.before.creationReceiptDigest))
180
+ || !hex.test(String(value.update.before.journalDigest))
181
+ || !record(value.update.after) || !isDeepStrictEqual(Object.keys(value.update.after).sort(), ['creationReceiptDigest', 'journalDigest'].sort())
182
+ || !hex.test(String(value.update.after.creationReceiptDigest)) || !hex.test(String(value.update.after.journalDigest))
183
+ || typeof value.signature !== 'string' || value.signature.length > 256)
184
+ fail('program update overlay shape or scope differs');
185
+ if (Object.hasOwn(value.update, 'emptyAdoptionHandoff'))
186
+ emptyAdoptionHandoffShape(value.update.emptyAdoptionHandoff, resources, String(value.transactionId));
187
+ if (Object.hasOwn(value.update, 'sealedAdoptionHandoff'))
188
+ sealedAdoptionHandoffShape(value.update.sealedAdoptionHandoff, resources, String(value.transactionId));
189
+ if (Object.hasOwn(value.update, 'reviewGrantHandoffs'))
190
+ reviewGrantHandoffsShape(value.update.reviewGrantHandoffs, String(value.transactionId));
191
+ const { signature, ...unsigned } = value;
192
+ const key = createPublicKey(resources.identities.host.publicKeyPem);
193
+ if (key.asymmetricKeyType !== 'ed25519'
194
+ || !verify(null, Buffer.from(canonical(unsigned)), key, Buffer.from(signature, 'base64')))
195
+ fail('program update overlay signature differs');
196
+ }
197
+ export function readRsiHostUpdateOverlayChain(input) {
198
+ if (input.source === undefined)
199
+ return { records: [] };
200
+ let parsed;
201
+ try {
202
+ parsed = JSON.parse(input.source);
203
+ }
204
+ catch {
205
+ fail('overlay JSON is invalid');
206
+ }
207
+ if (!record(parsed) || parsed.schemaVersion !== 1 || parsed.kind !== 'rsi-host-update-overlays'
208
+ || !Array.isArray(parsed.records) || parsed.records.length < 1 || parsed.records.length > 64)
209
+ fail('overlay chain is invalid');
210
+ const records = [];
211
+ const bootstrapDigest = sha256(input.bootstrapSource);
212
+ for (const value of parsed.records) {
213
+ const sequence = records.length + 1;
214
+ const previousDigest = records.length ? rsiHostUpdateOverlayDigest(records.at(-1)) : null;
215
+ if (record(value) && value.kind === 'rsi-owner-repair-overlay') {
216
+ repairOverlayShape(value, input.resources, input.dshHome, input.profile, bootstrapDigest, sequence, previousDigest);
217
+ }
218
+ else if (record(value) && value.kind === 'rsi-owner-program-update-overlay') {
219
+ programUpdateOverlayShape(value, input.resources, input.dshHome, input.profile, bootstrapDigest, sequence, previousDigest);
220
+ }
221
+ else {
222
+ overlayShape(value, input.resources, input.dshHome, input.profile, bootstrapDigest, sequence, previousDigest);
223
+ }
224
+ if (records.length && value.issuedAt < records.at(-1).issuedAt)
225
+ fail('overlay time moved backwards');
226
+ records.push(value);
227
+ }
228
+ return { records, latest: records.at(-1) };
229
+ }
230
+ export function signRsiHostUpdateOverlay(input, privateKeyPem, resources, bootstrapSource, previous) {
231
+ const record = { ...input, signature: sign(null, Buffer.from(canonical(input)), privateKeyPem).toString('base64') };
232
+ overlayShape(record, resources, input.dshHome, input.targetProfile, sha256(bootstrapSource), previous.length + 1, previous.length ? rsiHostUpdateOverlayDigest(previous.at(-1)) : null);
233
+ return record;
234
+ }
235
+ export function signRsiOwnerRepairOverlay(input, privateKeyPem, resources, bootstrapSource, previous) {
236
+ const record = { ...input, signature: sign(null, Buffer.from(canonical(input)), privateKeyPem).toString('base64') };
237
+ repairOverlayShape(record, resources, input.dshHome, input.targetProfile, sha256(bootstrapSource), previous.length + 1, previous.length ? rsiHostUpdateOverlayDigest(previous.at(-1)) : null);
238
+ return record;
239
+ }
240
+ export function signRsiOwnerProgramUpdateOverlay(input, privateKeyPem, resources, bootstrapSource, previous) {
241
+ const record = { ...input, signature: sign(null, Buffer.from(canonical(input)), privateKeyPem).toString('base64') };
242
+ programUpdateOverlayShape(record, resources, input.dshHome, input.targetProfile, sha256(bootstrapSource), previous.length + 1, previous.length ? rsiHostUpdateOverlayDigest(previous.at(-1)) : null);
243
+ return record;
244
+ }
245
+ function record(value) {
246
+ return value !== null && typeof value === 'object' && !Array.isArray(value);
247
+ }
248
+ /** Replace only exact pinned assets and the installed DSH baseline version.
249
+ * Arbitrary old-Host path strings are rejected instead of guessed. */
250
+ export function rebaseRsiHostValue(value, rebase) {
251
+ const visit = (item) => {
252
+ if (typeof item === 'string') {
253
+ if (item === rebase.oldHostRoot || item.startsWith(`${rebase.oldHostRoot}/`))
254
+ fail(`unmapped Host path: ${item}`);
255
+ return item;
256
+ }
257
+ if (Array.isArray(item))
258
+ return item.map(visit);
259
+ if (!record(item))
260
+ return item;
261
+ const oldPath = typeof item.path === 'string' ? item.path : undefined;
262
+ const pin = oldPath === undefined ? undefined : rebase.pins[oldPath];
263
+ if (pin !== undefined && oldPath !== undefined) {
264
+ if (typeof item.sha256 !== 'string' || !/^[a-f0-9]{64}$/u.test(item.sha256))
265
+ fail('pinned asset is malformed');
266
+ return Object.fromEntries(Object.entries(item).map(([key, child]) => [key,
267
+ key === 'path' ? pin.path : key === 'sha256' ? pin.sha256
268
+ : key === 'version' && child === rebase.oldHostVersion && oldPath.startsWith(`${rebase.oldHostRoot}/`)
269
+ ? rebase.candidateHostVersion : visit(child)]));
270
+ }
271
+ return Object.fromEntries(Object.entries(item).map(([key, child]) => [key,
272
+ key === 'dshBaseline' && child === rebase.oldHostVersion ? rebase.candidateHostVersion : visit(child)]));
273
+ };
274
+ return visit(value);
275
+ }
276
+ function document(source, label) {
277
+ // Include's !!js is an inert Loader expression at this stage. Resolve only
278
+ // that scalar tag; rawLoaderConfig below rejects every other tagged node.
279
+ const value = parseDocument(source, { uniqueKeys: true, strict: true,
280
+ customTags: [{ tag: 'tag:yaml.org,2002:js', resolve: (source) => source }] });
281
+ if (value.errors.length || value.warnings.length || !isSeq(value.contents))
282
+ fail(`${label} is not a bounded patch`);
283
+ // Existing Loader patches may share an object using YAML anchors. Expand
284
+ // those references as YAML nodes, before applying the strict raw projection;
285
+ // this never evaluates a !!js expression or turns it into a plain map.
286
+ let nodes = 0;
287
+ let aliases = 0;
288
+ const expand = (node, ancestors, depth) => {
289
+ if (++nodes > 100_000 || depth > 128)
290
+ fail(`${label} exceeds YAML expansion bounds`);
291
+ if (isAlias(node)) {
292
+ if (++aliases > 256)
293
+ fail(`${label} exceeds YAML alias bounds`);
294
+ const resolved = node.resolve(value);
295
+ if (!resolved || ancestors.has(resolved))
296
+ fail(`${label} has an unresolved or cyclic YAML alias`);
297
+ return expand(resolved.clone(), new Set([...ancestors, resolved]), depth + 1);
298
+ }
299
+ if (isMap(node)) {
300
+ if (node.items.some(pair => (isScalar(pair.key) ? pair.key.value : pair.key) === '__jsExpr')) {
301
+ fail(`${label} contains an untagged Loader expression marker`);
302
+ }
303
+ for (const pair of node.items) {
304
+ if (pair.value)
305
+ pair.value = expand(pair.value, new Set([...ancestors, node]), depth + 1);
306
+ }
307
+ }
308
+ else if (isSeq(node)) {
309
+ for (let index = 0; index < node.items.length; index++) {
310
+ const child = node.items[index];
311
+ if (child)
312
+ node.items[index] = expand(child, new Set([...ancestors, node]), depth + 1);
313
+ }
314
+ }
315
+ return node;
316
+ };
317
+ value.contents = expand(value.contents, new Set(), 0);
318
+ const clearAnchors = (node) => {
319
+ if (isAlias(node))
320
+ fail(`${label} has an unexpanded YAML alias`);
321
+ delete node.anchor;
322
+ if (isMap(node))
323
+ for (const pair of node.items)
324
+ if (pair.value)
325
+ clearAnchors(pair.value);
326
+ if (isSeq(node))
327
+ for (const item of node.items)
328
+ if (item)
329
+ clearAnchors(item);
330
+ };
331
+ clearAnchors(value.contents);
332
+ rawLoaderConfig(value.contents, label);
333
+ return value;
334
+ }
335
+ function row(document, id) {
336
+ let found;
337
+ const operations = document.contents;
338
+ if (!isSeq(operations))
339
+ fail('patch operations are invalid');
340
+ for (const operation of operations.items) {
341
+ if (!isMap(operation))
342
+ fail('patch operation is not a map');
343
+ if (operation.get('id') === id) {
344
+ if (found)
345
+ fail(`duplicate patch row: ${id}`);
346
+ found = operation;
347
+ }
348
+ for (const kind of ['insert', 'update']) {
349
+ const entries = operation.get(kind, true);
350
+ if (entries === undefined)
351
+ continue;
352
+ if (!isSeq(entries))
353
+ fail('patch rows are not a sequence');
354
+ for (const candidate of entries.items) {
355
+ if (!isMap(candidate))
356
+ fail('patch row is not a map');
357
+ if (candidate.get('id') === id) {
358
+ if (found)
359
+ fail(`duplicate patch row: ${id}`);
360
+ found = candidate;
361
+ }
362
+ }
363
+ }
364
+ }
365
+ if (!found)
366
+ fail(`patch row is missing: ${id}`);
367
+ return found;
368
+ }
369
+ function config(value) {
370
+ const node = value.get('config', true);
371
+ if (!isMap(node))
372
+ fail('patch row config is not a map');
373
+ const result = JSON.parse(JSON.stringify(rawLoaderConfig(node, 'patch row config')));
374
+ if (!record(result))
375
+ fail('patch row config is invalid');
376
+ return result;
377
+ }
378
+ function hasJsTag(node) {
379
+ if (!node)
380
+ return false;
381
+ if (isScalar(node))
382
+ return node.tag === 'tag:yaml.org,2002:js';
383
+ if (isSeq(node))
384
+ return node.items.some(item => hasJsTag(item));
385
+ if (isMap(node))
386
+ return node.items.some(pair => hasJsTag(pair.value));
387
+ return false;
388
+ }
389
+ function hasRawExpression(value) {
390
+ if (Array.isArray(value))
391
+ return value.some(hasRawExpression);
392
+ return record(value) && (Object.hasOwn(value, '__jsExpr') || Object.values(value).some(hasRawExpression));
393
+ }
394
+ function updateConfigNode(patch, node, replacement) {
395
+ const before = JSON.parse(JSON.stringify(rawLoaderConfig(node, 'patch config')));
396
+ if (isDeepStrictEqual(before, replacement))
397
+ return node;
398
+ if (isScalar(node) && node.tag === 'tag:yaml.org,2002:js')
399
+ fail('tagged Loader expression changed');
400
+ if (isMap(node) && record(replacement) && hasJsTag(node)) {
401
+ const remove = [];
402
+ for (const pair of node.items) {
403
+ const key = isScalar(pair.key) ? pair.key.value : pair.key;
404
+ if (typeof key !== 'string')
405
+ fail('patch config key is invalid');
406
+ if (!Object.hasOwn(replacement, key)) {
407
+ if (hasJsTag(pair.value))
408
+ fail('tagged Loader expression removed');
409
+ remove.push(key);
410
+ }
411
+ else {
412
+ const child = pair.value;
413
+ if (!child)
414
+ fail('patch config value is invalid');
415
+ pair.value = updateConfigNode(patch, child, replacement[key]);
416
+ }
417
+ }
418
+ for (const key of remove)
419
+ node.delete(key);
420
+ for (const [key, value] of Object.entries(replacement)) {
421
+ if (!node.has(key)) {
422
+ if (hasRawExpression(value))
423
+ fail('tagged Loader expression inserted');
424
+ node.set(key, patch.createNode(value));
425
+ }
426
+ }
427
+ return node;
428
+ }
429
+ if (isSeq(node) && Array.isArray(replacement) && hasJsTag(node)) {
430
+ if (node.items.length !== replacement.length)
431
+ fail('tagged Loader expression sequence changed');
432
+ for (let index = 0; index < replacement.length; index++) {
433
+ const child = node.items[index];
434
+ if (!child)
435
+ fail('patch config value is invalid');
436
+ node.items[index] = updateConfigNode(patch, child, replacement[index]);
437
+ }
438
+ return node;
439
+ }
440
+ if (hasJsTag(node) || hasRawExpression(replacement))
441
+ fail('tagged Loader expression changed');
442
+ return patch.createNode(replacement);
443
+ }
444
+ function setConfig(patch, id, replacement) {
445
+ const target = row(patch, id);
446
+ const node = target.get('config', true);
447
+ if (!isMap(node))
448
+ fail('patch row config is not a map');
449
+ target.set('config', updateConfigNode(patch, node, replacement));
450
+ }
451
+ /** Read the same inert Loader values used by the runtime observer. */
452
+ export function readRsiHostPatchConfig(source, id) {
453
+ return config(row(document(source, 'owner patch'), id));
454
+ }
455
+ /** Change a bounded row without rebuilding unrelated !!js expression nodes. */
456
+ export function replaceRsiHostPatchConfig(source, id, expected, replacement) {
457
+ const patch = document(source, 'owner patch');
458
+ if (!isDeepStrictEqual(config(row(patch, id)), expected))
459
+ fail(`owner patch row changed: ${id}`);
460
+ setConfig(patch, id, replacement);
461
+ return patch.toString({ lineWidth: 0 });
462
+ }
463
+ function targetDigest(manifest, id, patch) {
464
+ const source = config(row(patch, id));
465
+ return runtimeConfigDigest(id === 'dsh-ray-assistant-verifier'
466
+ ? { ...source, sourceReviews: manifest.sourceReviews } : source);
467
+ }
468
+ /** Preserve unrelated patch rows and original observer identities. */
469
+ export function rebaseRsiHostProfiles(input) {
470
+ const oldTarget = document(input.targetPatch, 'target patch');
471
+ const coordinator = document(input.coordinatorPatch, 'coordinator patch');
472
+ const changed = rebaseRsiHostValue(input.manifest, input.rebase);
473
+ const target = document(input.targetPatch, 'target patch');
474
+ const replacements = new Map([
475
+ ['dsh-ray-plugin-control-plane', changed.controlPlane],
476
+ ['dsh-ray-assistant-growth-driver', changed.growthDriver],
477
+ ]);
478
+ for (const [id, value] of replacements) {
479
+ const current = config(row(oldTarget, id));
480
+ const original = id === 'dsh-ray-plugin-control-plane' ? input.manifest.controlPlane : input.manifest.growthDriver;
481
+ if (!isDeepStrictEqual(current, original))
482
+ fail(`${id} patch differs from the owner manifest`);
483
+ setConfig(target, id, value);
484
+ }
485
+ const verifier = row(target, 'dsh-ray-assistant-verifier');
486
+ const verifierConfig = config(verifier);
487
+ if (!isDeepStrictEqual(verifierConfig.sourceReviews, input.manifest.sourceReviews))
488
+ fail('verifier patch differs from the owner manifest');
489
+ setConfig(target, 'dsh-ray-assistant-verifier', { ...verifierConfig, sourceReviews: changed.sourceReviews });
490
+ const targets = changed.controlPlane.runtimeObserver?.targets;
491
+ if (!targets)
492
+ fail('runtime observer targets are missing');
493
+ for (const targetEntry of targets) {
494
+ if (!replacements.has(targetEntry.entryId) && targetEntry.entryId !== 'dsh-ray-assistant-verifier')
495
+ continue;
496
+ const oldDigest = targetDigest(input.manifest, targetEntry.entryId, oldTarget);
497
+ if (targetEntry.configDigest !== oldDigest)
498
+ fail(`observer digest differs: ${targetEntry.entryId}`);
499
+ targetEntry.configDigest = targetDigest(changed, targetEntry.entryId, target);
500
+ }
501
+ setConfig(target, 'dsh-ray-plugin-control-plane', changed.controlPlane);
502
+ // A coordinator patch may contain only its declared standalone scope. It
503
+ // must not carry any old Host path that this rebase cannot account for.
504
+ if (input.coordinatorPatch.includes(input.rebase.oldHostRoot))
505
+ fail('coordinator patch has an unmapped Host path');
506
+ return { manifest: changed, targetPatch: target.toString({ lineWidth: 0 }), coordinatorPatch: coordinator.toString({ lineWidth: 0 }) };
507
+ }
508
+ /** Rebase the deployed patch itself. An adoption may have replaced the original
509
+ * bootstrap rows, so the historical manifest cannot be used as their source. */
510
+ export function rebaseRsiHostActivePatch(input) {
511
+ const documentBefore = document(input.patch, 'active patch');
512
+ const documentAfter = document(input.patch, 'active patch');
513
+ const ids = ['dsh-ray-plugin-control-plane', 'dsh-ray-assistant-growth-driver',
514
+ 'dsh-ray-assistant-verifier'];
515
+ for (const id of ids) {
516
+ const current = config(row(documentBefore, id));
517
+ setConfig(documentAfter, id, rebaseRsiHostValue(current, input.rebase));
518
+ }
519
+ const cp = config(row(documentAfter, ids[0]));
520
+ const observer = cp.runtimeObserver;
521
+ if (!record(observer) || !Array.isArray(observer.targets))
522
+ fail('active runtime observer is missing');
523
+ for (const entry of observer.targets) {
524
+ if (!record(entry) || typeof entry.entryId !== 'string' || typeof entry.configDigest !== 'string')
525
+ fail('active observer entry differs');
526
+ if (!ids.includes(entry.entryId))
527
+ continue;
528
+ const oldConfig = config(row(documentBefore, entry.entryId));
529
+ const newConfig = config(row(documentAfter, entry.entryId));
530
+ const oldDigest = runtimeConfigDigest(oldConfig);
531
+ if (entry.configDigest !== oldDigest)
532
+ fail(`active observer digest differs: ${entry.entryId}`);
533
+ entry.configDigest = runtimeConfigDigest(newConfig);
534
+ }
535
+ setConfig(documentAfter, ids[0], cp);
536
+ if (JSON.stringify(rawLoaderConfig(documentAfter.contents, 'active patch')).includes(input.rebase.oldHostRoot)) {
537
+ fail('active patch retains an unmapped Host path');
538
+ }
539
+ return documentAfter.toString({ lineWidth: 0 });
540
+ }
541
+ /** The owner manifest is historical bootstrap authority; rebase only its
542
+ * verified baseline pins and observer digests, independently of live adoption. */
543
+ export function rebaseRsiHostManifest(manifest, rebase) {
544
+ const changed = rebaseRsiHostValue(manifest, rebase);
545
+ const oldTargets = manifest.controlPlane.runtimeObserver?.targets;
546
+ const newTargets = changed.controlPlane.runtimeObserver?.targets;
547
+ if (!oldTargets || !newTargets || oldTargets.length !== newTargets.length)
548
+ fail('owner observer targets differ');
549
+ const configFor = (source, id) => id === 'dsh-ray-plugin-control-plane'
550
+ ? source.controlPlane : id === 'dsh-ray-assistant-growth-driver' ? source.growthDriver
551
+ : id === 'dsh-ray-assistant-verifier' ? { sourceReviews: source.sourceReviews } : undefined;
552
+ for (let index = 0; index < oldTargets.length; index++) {
553
+ const oldTarget = oldTargets[index], nextTarget = newTargets[index];
554
+ if (oldTarget.entryId !== nextTarget.entryId)
555
+ fail('owner observer entry changed');
556
+ const before = configFor(manifest, oldTarget.entryId);
557
+ const after = configFor(changed, nextTarget.entryId);
558
+ if (before === undefined || after === undefined)
559
+ continue;
560
+ if (oldTarget.entryId === 'dsh-ray-assistant-verifier')
561
+ continue; // Its full patch config is not stored in the manifest.
562
+ if (oldTarget.configDigest !== runtimeConfigDigest(before))
563
+ fail('owner observer digest differs');
564
+ nextTarget.configDigest = runtimeConfigDigest(after);
565
+ }
566
+ return changed;
567
+ }
568
+ //# sourceMappingURL=rsi-host-update.js.map