@countinghouse/ch-local 0.0.0-stage → 1.0.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 (262) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +361 -2
  3. package/bin/ch-local.js +472 -0
  4. package/clients/claude-code/.claude-plugin/plugin.json +6 -0
  5. package/clients/claude-code/.mcp.json +11 -0
  6. package/clients/claude-code/bin/launch.js +235 -0
  7. package/clients/claude-code/bin/win-acl.js +420 -0
  8. package/clients/claude-code/skills/ci-failure-triage/SKILL.md +21 -0
  9. package/clients/claude-code/skills/countinghouse-module/SKILL.md +177 -0
  10. package/clients/claude-code/skills/countinghouse-setup/SKILL.md +74 -0
  11. package/lib/allowlist.js +65 -0
  12. package/lib/aside.js +16 -0
  13. package/lib/atomic.js +45 -0
  14. package/lib/auto-apply.js +245 -0
  15. package/lib/auto-upgrade.js +98 -0
  16. package/lib/availability.js +307 -0
  17. package/lib/ceiling.js +280 -0
  18. package/lib/clean-text.js +20 -0
  19. package/lib/cli-change.js +38 -0
  20. package/lib/client-conditions-fixtures.js +143 -0
  21. package/lib/client-conditions.js +656 -0
  22. package/lib/clients.js +179 -0
  23. package/lib/command-line.js +202 -0
  24. package/lib/conditions-command.js +116 -0
  25. package/lib/control.js +134 -0
  26. package/lib/credential-store.js +414 -0
  27. package/lib/credentials-command.js +344 -0
  28. package/lib/daemon-requests.js +137 -0
  29. package/lib/daemon.js +888 -0
  30. package/lib/describe-module.js +137 -0
  31. package/lib/description-check.js +175 -0
  32. package/lib/failed-json.js +43 -0
  33. package/lib/gc.js +274 -0
  34. package/lib/github-token.js +57 -0
  35. package/lib/handoff-io.js +73 -0
  36. package/lib/held-back.js +116 -0
  37. package/lib/hidden-input.js +64 -0
  38. package/lib/history.js +49 -0
  39. package/lib/home.js +372 -0
  40. package/lib/in-place.js +1036 -0
  41. package/lib/install-plan.js +142 -0
  42. package/lib/install.js +846 -0
  43. package/lib/installed-description.js +153 -0
  44. package/lib/integrity.js +7 -0
  45. package/lib/lock.js +456 -0
  46. package/lib/lockfile-rules.js +98 -0
  47. package/lib/machine.js +20 -0
  48. package/lib/module-credentials.js +403 -0
  49. package/lib/module-record.js +298 -0
  50. package/lib/module-roles.js +44 -0
  51. package/lib/module-seams.js +26 -0
  52. package/lib/module-store.js +202 -0
  53. package/lib/module-values.js +334 -0
  54. package/lib/names.js +31 -0
  55. package/lib/next-steps.js +8 -0
  56. package/lib/node-check.js +56 -0
  57. package/lib/npm-fetch.js +123 -0
  58. package/lib/npm-release.js +99 -0
  59. package/lib/npm-upgrade.js +146 -0
  60. package/lib/npm.js +41 -0
  61. package/lib/outside-edits.js +162 -0
  62. package/lib/own-applied.js +175 -0
  63. package/lib/own-command.js +380 -0
  64. package/lib/own-file.js +120 -0
  65. package/lib/own-record.js +96 -0
  66. package/lib/own-tree.js +336 -0
  67. package/lib/pack-table.js +290 -0
  68. package/lib/packs-command.js +570 -0
  69. package/lib/packs.js +219 -0
  70. package/lib/pins.js +64 -0
  71. package/lib/pipe-probe.js +82 -0
  72. package/lib/plain-read.js +63 -0
  73. package/lib/private-file.js +90 -0
  74. package/lib/private-write.js +33 -0
  75. package/lib/provenance.js +234 -0
  76. package/lib/record.js +82 -0
  77. package/lib/registry.js +22 -0
  78. package/lib/release-pins.js +350 -0
  79. package/lib/release-pins.json +4225 -0
  80. package/lib/release-tags.js +12 -0
  81. package/lib/release.js +36 -0
  82. package/lib/restart-command.js +161 -0
  83. package/lib/rollback.js +227 -0
  84. package/lib/run-liveness.js +15 -0
  85. package/lib/runtime-log.js +287 -0
  86. package/lib/sandbox-support.js +17 -0
  87. package/lib/semver-order.js +40 -0
  88. package/lib/session-socket.js +69 -0
  89. package/lib/session-table.js +238 -0
  90. package/lib/session.js +281 -0
  91. package/lib/settings-command.js +82 -0
  92. package/lib/settings.js +564 -0
  93. package/lib/shim-binary.js +152 -0
  94. package/lib/shim-config.js +159 -0
  95. package/lib/shim-install.js +365 -0
  96. package/lib/sigstore/README.md +44 -0
  97. package/lib/sigstore/npm-keys.json +18 -0
  98. package/lib/sigstore/trusted-root.json +126 -0
  99. package/lib/snippets.js +76 -0
  100. package/lib/start.js +721 -0
  101. package/lib/store-files.js +48 -0
  102. package/lib/store-paths.js +33 -0
  103. package/lib/swap-preload.js +38 -0
  104. package/lib/tar.js +220 -0
  105. package/lib/uninstall.js +314 -0
  106. package/lib/unpack.js +41 -0
  107. package/lib/upgrade-stamp.js +63 -0
  108. package/lib/upgrade.js +281 -0
  109. package/lib/watch.js +40 -0
  110. package/lib/win-acl.js +420 -0
  111. package/node_modules/@sigstore/bundle/LICENSE +202 -0
  112. package/node_modules/@sigstore/bundle/README.md +19 -0
  113. package/node_modules/@sigstore/bundle/dist/build.d.ts +18 -0
  114. package/node_modules/@sigstore/bundle/dist/build.js +100 -0
  115. package/node_modules/@sigstore/bundle/dist/bundle.d.ts +74 -0
  116. package/node_modules/@sigstore/bundle/dist/bundle.js +24 -0
  117. package/node_modules/@sigstore/bundle/dist/error.d.ts +4 -0
  118. package/node_modules/@sigstore/bundle/dist/error.js +25 -0
  119. package/node_modules/@sigstore/bundle/dist/index.d.ts +8 -0
  120. package/node_modules/@sigstore/bundle/dist/index.js +43 -0
  121. package/node_modules/@sigstore/bundle/dist/serialized.d.ts +74 -0
  122. package/node_modules/@sigstore/bundle/dist/serialized.js +49 -0
  123. package/node_modules/@sigstore/bundle/dist/utility.d.ts +14 -0
  124. package/node_modules/@sigstore/bundle/dist/utility.js +2 -0
  125. package/node_modules/@sigstore/bundle/dist/validate.d.ts +7 -0
  126. package/node_modules/@sigstore/bundle/dist/validate.js +199 -0
  127. package/node_modules/@sigstore/bundle/package.json +35 -0
  128. package/node_modules/@sigstore/core/LICENSE +202 -0
  129. package/node_modules/@sigstore/core/README.md +10 -0
  130. package/node_modules/@sigstore/core/dist/asn1/error.d.ts +4 -0
  131. package/node_modules/@sigstore/core/dist/asn1/error.js +24 -0
  132. package/node_modules/@sigstore/core/dist/asn1/index.d.ts +1 -0
  133. package/node_modules/@sigstore/core/dist/asn1/index.js +20 -0
  134. package/node_modules/@sigstore/core/dist/asn1/length.d.ts +3 -0
  135. package/node_modules/@sigstore/core/dist/asn1/length.js +62 -0
  136. package/node_modules/@sigstore/core/dist/asn1/obj.d.ts +14 -0
  137. package/node_modules/@sigstore/core/dist/asn1/obj.js +155 -0
  138. package/node_modules/@sigstore/core/dist/asn1/parse.d.ts +6 -0
  139. package/node_modules/@sigstore/core/dist/asn1/parse.js +124 -0
  140. package/node_modules/@sigstore/core/dist/asn1/tag.d.ts +16 -0
  141. package/node_modules/@sigstore/core/dist/asn1/tag.js +89 -0
  142. package/node_modules/@sigstore/core/dist/crypto.d.ts +6 -0
  143. package/node_modules/@sigstore/core/dist/crypto.js +69 -0
  144. package/node_modules/@sigstore/core/dist/dsse.d.ts +1 -0
  145. package/node_modules/@sigstore/core/dist/dsse.js +29 -0
  146. package/node_modules/@sigstore/core/dist/encoding.d.ts +2 -0
  147. package/node_modules/@sigstore/core/dist/encoding.js +27 -0
  148. package/node_modules/@sigstore/core/dist/index.d.ts +9 -0
  149. package/node_modules/@sigstore/core/dist/index.js +66 -0
  150. package/node_modules/@sigstore/core/dist/json.d.ts +1 -0
  151. package/node_modules/@sigstore/core/dist/json.js +60 -0
  152. package/node_modules/@sigstore/core/dist/oid.d.ts +3 -0
  153. package/node_modules/@sigstore/core/dist/oid.js +20 -0
  154. package/node_modules/@sigstore/core/dist/pem.d.ts +2 -0
  155. package/node_modules/@sigstore/core/dist/pem.js +43 -0
  156. package/node_modules/@sigstore/core/dist/rfc3161/error.d.ts +2 -0
  157. package/node_modules/@sigstore/core/dist/rfc3161/error.js +21 -0
  158. package/node_modules/@sigstore/core/dist/rfc3161/index.d.ts +1 -0
  159. package/node_modules/@sigstore/core/dist/rfc3161/index.js +20 -0
  160. package/node_modules/@sigstore/core/dist/rfc3161/timestamp.d.ts +36 -0
  161. package/node_modules/@sigstore/core/dist/rfc3161/timestamp.js +212 -0
  162. package/node_modules/@sigstore/core/dist/rfc3161/tstinfo.d.ts +12 -0
  163. package/node_modules/@sigstore/core/dist/rfc3161/tstinfo.js +72 -0
  164. package/node_modules/@sigstore/core/dist/stream.d.ts +21 -0
  165. package/node_modules/@sigstore/core/dist/stream.js +117 -0
  166. package/node_modules/@sigstore/core/dist/x509/cert.d.ts +43 -0
  167. package/node_modules/@sigstore/core/dist/x509/cert.js +245 -0
  168. package/node_modules/@sigstore/core/dist/x509/ext.d.ts +41 -0
  169. package/node_modules/@sigstore/core/dist/x509/ext.js +146 -0
  170. package/node_modules/@sigstore/core/dist/x509/index.d.ts +2 -0
  171. package/node_modules/@sigstore/core/dist/x509/index.js +23 -0
  172. package/node_modules/@sigstore/core/dist/x509/sct.d.ts +25 -0
  173. package/node_modules/@sigstore/core/dist/x509/sct.js +158 -0
  174. package/node_modules/@sigstore/core/package.json +31 -0
  175. package/node_modules/@sigstore/protobuf-specs/LICENSE +202 -0
  176. package/node_modules/@sigstore/protobuf-specs/README.md +5 -0
  177. package/node_modules/@sigstore/protobuf-specs/dist/__generated__/envelope.d.ts +44 -0
  178. package/node_modules/@sigstore/protobuf-specs/dist/__generated__/envelope.js +59 -0
  179. package/node_modules/@sigstore/protobuf-specs/dist/__generated__/events.d.ts +64 -0
  180. package/node_modules/@sigstore/protobuf-specs/dist/__generated__/events.js +174 -0
  181. package/node_modules/@sigstore/protobuf-specs/dist/__generated__/google/api/field_behavior.d.ts +74 -0
  182. package/node_modules/@sigstore/protobuf-specs/dist/__generated__/google/api/field_behavior.js +141 -0
  183. package/node_modules/@sigstore/protobuf-specs/dist/__generated__/google/protobuf/any.d.ts +74 -0
  184. package/node_modules/@sigstore/protobuf-specs/dist/__generated__/google/protobuf/any.js +35 -0
  185. package/node_modules/@sigstore/protobuf-specs/dist/__generated__/google/protobuf/descriptor.d.ts +1309 -0
  186. package/node_modules/@sigstore/protobuf-specs/dist/__generated__/google/protobuf/descriptor.js +2191 -0
  187. package/node_modules/@sigstore/protobuf-specs/dist/__generated__/google/protobuf/timestamp.d.ts +113 -0
  188. package/node_modules/@sigstore/protobuf-specs/dist/__generated__/google/protobuf/timestamp.js +29 -0
  189. package/node_modules/@sigstore/protobuf-specs/dist/__generated__/rekor/v2/dsse.d.ts +23 -0
  190. package/node_modules/@sigstore/protobuf-specs/dist/__generated__/rekor/v2/dsse.js +55 -0
  191. package/node_modules/@sigstore/protobuf-specs/dist/__generated__/rekor/v2/entry.d.ts +43 -0
  192. package/node_modules/@sigstore/protobuf-specs/dist/__generated__/rekor/v2/entry.js +81 -0
  193. package/node_modules/@sigstore/protobuf-specs/dist/__generated__/rekor/v2/hashedrekord.d.ts +22 -0
  194. package/node_modules/@sigstore/protobuf-specs/dist/__generated__/rekor/v2/hashedrekord.js +56 -0
  195. package/node_modules/@sigstore/protobuf-specs/dist/__generated__/rekor/v2/verifier.d.ts +35 -0
  196. package/node_modules/@sigstore/protobuf-specs/dist/__generated__/rekor/v2/verifier.js +74 -0
  197. package/node_modules/@sigstore/protobuf-specs/dist/__generated__/sigstore_bundle.d.ts +146 -0
  198. package/node_modules/@sigstore/protobuf-specs/dist/__generated__/sigstore_bundle.js +103 -0
  199. package/node_modules/@sigstore/protobuf-specs/dist/__generated__/sigstore_common.d.ts +304 -0
  200. package/node_modules/@sigstore/protobuf-specs/dist/__generated__/sigstore_common.js +612 -0
  201. package/node_modules/@sigstore/protobuf-specs/dist/__generated__/sigstore_monitor.d.ts +33 -0
  202. package/node_modules/@sigstore/protobuf-specs/dist/__generated__/sigstore_monitor.js +55 -0
  203. package/node_modules/@sigstore/protobuf-specs/dist/__generated__/sigstore_rekor.d.ts +144 -0
  204. package/node_modules/@sigstore/protobuf-specs/dist/__generated__/sigstore_rekor.js +137 -0
  205. package/node_modules/@sigstore/protobuf-specs/dist/__generated__/sigstore_trustroot.d.ts +386 -0
  206. package/node_modules/@sigstore/protobuf-specs/dist/__generated__/sigstore_trustroot.js +284 -0
  207. package/node_modules/@sigstore/protobuf-specs/dist/__generated__/sigstore_verification.d.ts +191 -0
  208. package/node_modules/@sigstore/protobuf-specs/dist/__generated__/sigstore_verification.js +281 -0
  209. package/node_modules/@sigstore/protobuf-specs/dist/index.d.ts +6 -0
  210. package/node_modules/@sigstore/protobuf-specs/dist/index.js +37 -0
  211. package/node_modules/@sigstore/protobuf-specs/dist/rekor/v2/index.d.ts +4 -0
  212. package/node_modules/@sigstore/protobuf-specs/dist/rekor/v2/index.js +35 -0
  213. package/node_modules/@sigstore/protobuf-specs/package.json +35 -0
  214. package/node_modules/@sigstore/verify/README.md +9 -0
  215. package/node_modules/@sigstore/verify/dist/bundle/dsse.d.ts +12 -0
  216. package/node_modules/@sigstore/verify/dist/bundle/dsse.js +44 -0
  217. package/node_modules/@sigstore/verify/dist/bundle/index.d.ts +4 -0
  218. package/node_modules/@sigstore/verify/dist/bundle/index.js +59 -0
  219. package/node_modules/@sigstore/verify/dist/bundle/message.d.ts +13 -0
  220. package/node_modules/@sigstore/verify/dist/bundle/message.js +54 -0
  221. package/node_modules/@sigstore/verify/dist/error.d.ts +16 -0
  222. package/node_modules/@sigstore/verify/dist/error.js +34 -0
  223. package/node_modules/@sigstore/verify/dist/index.d.ts +6 -0
  224. package/node_modules/@sigstore/verify/dist/index.js +28 -0
  225. package/node_modules/@sigstore/verify/dist/key/certificate.d.ts +21 -0
  226. package/node_modules/@sigstore/verify/dist/key/certificate.js +216 -0
  227. package/node_modules/@sigstore/verify/dist/key/index.d.ts +10 -0
  228. package/node_modules/@sigstore/verify/dist/key/index.js +75 -0
  229. package/node_modules/@sigstore/verify/dist/key/sct.d.ts +4 -0
  230. package/node_modules/@sigstore/verify/dist/key/sct.js +78 -0
  231. package/node_modules/@sigstore/verify/dist/policy.d.ts +5 -0
  232. package/node_modules/@sigstore/verify/dist/policy.js +50 -0
  233. package/node_modules/@sigstore/verify/dist/shared.types.d.ts +50 -0
  234. package/node_modules/@sigstore/verify/dist/shared.types.js +2 -0
  235. package/node_modules/@sigstore/verify/dist/timestamp/index.d.ts +11 -0
  236. package/node_modules/@sigstore/verify/dist/timestamp/index.js +24 -0
  237. package/node_modules/@sigstore/verify/dist/timestamp/tsa.d.ts +3 -0
  238. package/node_modules/@sigstore/verify/dist/timestamp/tsa.js +63 -0
  239. package/node_modules/@sigstore/verify/dist/tlog/checkpoint.d.ts +11 -0
  240. package/node_modules/@sigstore/verify/dist/tlog/checkpoint.js +154 -0
  241. package/node_modules/@sigstore/verify/dist/tlog/dsse.d.ts +6 -0
  242. package/node_modules/@sigstore/verify/dist/tlog/dsse.js +106 -0
  243. package/node_modules/@sigstore/verify/dist/tlog/hashedrekord.d.ts +6 -0
  244. package/node_modules/@sigstore/verify/dist/tlog/hashedrekord.js +94 -0
  245. package/node_modules/@sigstore/verify/dist/tlog/index.d.ts +5 -0
  246. package/node_modules/@sigstore/verify/dist/tlog/index.js +92 -0
  247. package/node_modules/@sigstore/verify/dist/tlog/intoto.d.ts +3 -0
  248. package/node_modules/@sigstore/verify/dist/tlog/intoto.js +62 -0
  249. package/node_modules/@sigstore/verify/dist/tlog/merkle.d.ts +3 -0
  250. package/node_modules/@sigstore/verify/dist/tlog/merkle.js +104 -0
  251. package/node_modules/@sigstore/verify/dist/tlog/set.d.ts +3 -0
  252. package/node_modules/@sigstore/verify/dist/tlog/set.js +60 -0
  253. package/node_modules/@sigstore/verify/dist/trust/filter.d.ts +8 -0
  254. package/node_modules/@sigstore/verify/dist/trust/filter.js +23 -0
  255. package/node_modules/@sigstore/verify/dist/trust/index.d.ts +5 -0
  256. package/node_modules/@sigstore/verify/dist/trust/index.js +90 -0
  257. package/node_modules/@sigstore/verify/dist/trust/trust.types.d.ts +29 -0
  258. package/node_modules/@sigstore/verify/dist/trust/trust.types.js +2 -0
  259. package/node_modules/@sigstore/verify/dist/verifier.d.ts +31 -0
  260. package/node_modules/@sigstore/verify/dist/verifier.js +160 -0
  261. package/node_modules/@sigstore/verify/package.json +36 -0
  262. package/package.json +37 -4
package/lib/session.js ADDED
@@ -0,0 +1,281 @@
1
+ 'use strict';
2
+ // `ch-local session [--home <abs>]`: what every client entry runs (clients
3
+ // add and the snippets; the Claude Code plugin's launch.js once its
4
+ // .mcp.json says `session`, A's follow-up to #120), so the choice
5
+ // between the shared runtime and this session's own is made in one place,
6
+ // in the home's own code, at every session start (the reviewer's high on
7
+ // #120, 1 (a)):
8
+ //
9
+ // - the shared runtime, through the native shim at <home>/shim/ch-shim,
10
+ // when the home's record names the shim install placed (install records it
11
+ // only once it is in place: lib/install.js shimStep) AND that file passes
12
+ // lib/shim-binary.js's checks now (<home> this user's; <home>/shim this
13
+ // user's, 0700, not a link; ch-shim a regular file of this user's, not a
14
+ // link, no group or other write, no setuid or setgid, executable by its
15
+ // owner), AND its content is the version the record names (versions.json,
16
+ // by sha512: a rollback between its record and its alignShim, or one whose
17
+ // alignShim threw, runs no other release's shim), AND it is a program for
18
+ // this machine that can be run from where it is (not a noexec mount).
19
+ // - a home another user owns starts nothing (exit 2): its record and its
20
+ // shim are theirs to change.
21
+ // - else this session's own runtime (start --stdio), with a line on stderr
22
+ // when a recorded shim fails a check.
23
+ //
24
+ // So a set-aside shim (one still in use, SHIM_BUSY; a rollback to a release
25
+ // with none) never leaves an entry naming a file that is not there: the
26
+ // entry is this launcher, which is always there.
27
+ //
28
+ // POSIX: the shim replaces this process (process.execve, in Node since
29
+ // 22.15 and 23.11; experimental), so a session costs no Node of its own
30
+ // beside it. An execve that fails ends this process on Node 22 and 24 (it
31
+ // aborts, exit 134: nothing to catch) and throws on 26, where the session
32
+ // falls back to its own runtime. The checks above run just before it, and
33
+ // the file's first bytes must be a program for this platform and
34
+ // architecture (formatProblem), so what is left is a file swapped since, by
35
+ // this user, or the kernel refusing for a reason of its own.
36
+ // Windows has no execve: the shim runs as a child with this process's stdio,
37
+ // and this Node waits for it (one more Node per session there: a 1.1 row).
38
+ //
39
+ // Nothing here touches process.stdin, stdout or stderr before the shim runs:
40
+ // Node opening a stdio pipe makes it O_NONBLOCK, and the shim would inherit
41
+ // that across the exec. A line goes out by fs.writeSync(2) instead.
42
+ const fs = require('node:fs');
43
+ const path = require('node:path');
44
+
45
+
46
+ // Whether a file's first bytes are a program this machine runs: ELF (64-bit,
47
+ // little-endian) on Linux, Mach-O (64-bit, or a fat file holding one) on
48
+ // macOS, each for this architecture -- so a binary the kernel would refuse
49
+ // (ENOEXEC, which aborts Node 22 and 24's execve) is never exec'd, and the
50
+ // session runs its own runtime instead (the reviewer's high on #120, 1 (a)).
51
+ // Windows spawns, where a refusal is an error to catch: not checked here.
52
+ // (head: Buffer, platform, arch) -> null | why not
53
+ const ELF_MACHINE = {x64: 0x3e, arm64: 0xb7};
54
+ const MACHO_CPU = {x64: 0x01000007, arm64: 0x0100000c};
55
+ function formatProblem(head, platform, arch) {
56
+ if (platform === 'win32') return null;
57
+ if (platform === 'linux') {
58
+ if (head.length < 20 || head.readUInt32BE(0) !== 0x7f454c46) return 'not an ELF program';
59
+ const want = Object.prototype.hasOwnProperty.call(ELF_MACHINE, arch) ? ELF_MACHINE[arch] : null;
60
+ return head[4] === 2 && head[5] === 1 && want != null && head.readUInt16LE(18) === want ? null : `not an ELF program for ${arch}`;
61
+ }
62
+ if (platform === 'darwin') {
63
+ const want = Object.prototype.hasOwnProperty.call(MACHO_CPU, arch) ? MACHO_CPU[arch] : null;
64
+ if (head.length < 8) return 'not a Mach-O program';
65
+ if (head.readUInt32LE(0) === 0xfeedfacf) return want != null && head.readInt32LE(4) === want ? null : `not a Mach-O program for ${arch}`;
66
+ const fat = head.readUInt32BE(0);
67
+ if (fat !== 0xcafebabe && fat !== 0xcafebabf) return 'not a Mach-O program';
68
+ const n = head.readUInt32BE(4);
69
+ const size = fat === 0xcafebabe ? 20 : 32;
70
+ if (n < 1 || n > 30 || 8 + n * size > head.length) return 'not a Mach-O program';
71
+ for (let i = 0; i < n; i++) if (want != null && head.readInt32BE(8 + i * size) === want) return null;
72
+ return `not a Mach-O program for ${arch}`;
73
+ }
74
+ return `no shim runs on ${platform}`;
75
+ }
76
+
77
+ // The one decision. ({where, platform, arch, geteuid, acl}) -> {shim:
78
+ // absolute path} | {shim: null, note: line | null}. A record that can't be
79
+ // read is the per-session runtime's to report (start reads it too).
80
+ function sessionChoice({where, platform = process.platform, arch = process.arch, geteuid, acl, access = fs.accessSync} = {}) {
81
+ // a home another user could change runs nothing, the session's own runtime
82
+ // included: what runs from it is given every stored key (the reviewer's
83
+ // pre-work high, A-home-check, 2; it was a fallback to start). First, and
84
+ // whatever the record says. -> refuse: its line
85
+ const {homeOpenProblem, HOME_OPEN} = require('./home');
86
+ // (this machine's own platform, as start judges it: the medium on #126's round, 8)
87
+ const open = homeOpenProblem(where.home, {fileAcl: process.platform === 'win32' && acl != null ? acl.fileAcl : null, principalNames: process.platform === 'win32' && acl != null ? acl.principalNames : null});
88
+ if (open != null) return {shim: null, note: null, refuse: `${where.home}: ${open.why}, ${open.consequence || HOME_OPEN}${open.fix != null ? `; ${open.fix}${open.final ? '' : ', then start again'}` : ''}`};
89
+ let record = null;
90
+ try { record = require('./module-record').readModulesRecord(where); } catch (err) { return {shim: null, note: null}; }
91
+ if (record == null || record.shim == null) return {shim: null, note: null};
92
+ const here = require('./shim-binary').installedShim(where.home, {platform, geteuid, acl});
93
+ const installLine = () => require('./command-line').chLocal(where, 'install', platform);
94
+ if (here != null && here.file != null) {
95
+ // (read whole, as the shim's files are: lib/plain-read.js)
96
+ const {readPlain, readVersions} = require('./plain-read');
97
+ const bin = readPlain(here.file);
98
+ // the version the record names, by its content (versions.json, as
99
+ // install and rollback write it): a rollback that wrote the restored
100
+ // record but has not aligned the shim yet, or whose alignShim threw,
101
+ // never has a session run the release rolled back from (the reviewer's
102
+ // high on #120, round 2, 2)
103
+ const versions = bin.bytes != null ? readVersions(path.join(where.home, 'shim', 'versions.json'), platform) : {};
104
+ const at = bin.bytes != null ? versions[require('node:crypto').createHash('sha512').update(bin.bytes).digest('hex')] : undefined;
105
+ if (at !== record.shim.version) {
106
+ return {shim: null, note: `ch-local: the shared runtime's shim in place is not the one the installed release records (${record.shim.version}); this session runs its own runtime. Run \`${installLine()}\` to put it right.\n`};
107
+ }
108
+ if (formatProblem(bin.bytes.subarray(0, 4096), platform, arch) != null) {
109
+ return {shim: null, note: `ch-local: the shared runtime's shim is not a program this machine runs (${platform}-${arch}); this session runs its own runtime. Run \`${installLine()}\` to put it right.\n`};
110
+ }
111
+ // (runnable here: a noexec mount passes every check above, and its
112
+ // execve would abort Node 22 and 24 -- round 2, 3; a security module's
113
+ // refusal at exec stays a limit, the header's)
114
+ if (platform !== 'win32') {
115
+ try { access(here.file, fs.constants.X_OK); } catch (err) {
116
+ return {shim: null, note: `ch-local: the shared runtime's shim can't be run from ${path.dirname(here.file)} (${err.code || 'refused'}; a folder mounted noexec?); this session runs its own runtime.\n`};
117
+ }
118
+ }
119
+ // (shim.json's node and chLocal there: a Node upgrade that moved node
120
+ // leaves the shim starting nothing; the own runtime runs in this Node,
121
+ // and install writes shim.json again -- A3)
122
+ // (the session's ACL reader: Windows reads each path once -- the medium, 1)
123
+ const unusable = require('./shim-config').shimJsonRunProblem(where.home, {platform, access, fileAcl: acl != null ? acl.fileAcl : null});
124
+ if (unusable != null) {
125
+ // (shim.json itself, or what it names: the medium, 2)
126
+ const what = unusable.which === 'shim.json' ? `can't be used (${unusable.code})`
127
+ : `name a ${unusable.which === 'node' ? 'Node' : 'ch-local'} that can't be used (${unusable.which}: ${unusable.code})`;
128
+ return {shim: null, note: `ch-local: the shared runtime's settings (shim.json) ${what}; this session runs its own runtime. Run \`${installLine()}\` to put it right.\n`};
129
+ }
130
+ // (an unreleased home says so at every start; with the shim, start runs
131
+ // only in the daemon, whose stderr no client sees, so the session says
132
+ // it before the exec -- the reviewer's high on A-shim-from, F2)
133
+ return {shim: here.file, note: record.unreleased === true ? `ch-local: ${require('./pins').UNRELEASED_LINE}\n` : null};
134
+ }
135
+ const why = here == null ? `${path.join(where.home, 'shim', require('./shim-binary').exeOf(platform))} is not there` : here.problem;
136
+ return {shim: null, note: `ch-local: the shared runtime's shim is not used: ${why}; this session runs its own runtime. Run \`${installLine()}\` to put it right.\n`};
137
+ }
138
+
139
+ // Whether a home is another user's: POSIX by its owner's uid, Windows by
140
+ // its owner's SID (this user, Administrators or SYSTEM are this user's
141
+ // own, as lib/win-acl.js privateProblem has them). A home not there yet is
142
+ // none of this: start says what to do. -> null | why
143
+ function ownerProblem(home, platform = process.platform, {geteuid = process.geteuid, acl} = {}) {
144
+ let real;
145
+ // (only a home not there yet is no home: the reviewer's high on #126, L4)
146
+ try { real = fs.realpathSync(home); } catch (err) { return err.code === 'ENOENT' ? null : `it could not be read (${err.code || err.message})`; }
147
+ if (platform === 'win32') {
148
+ const winAcl = require('./win-acl');
149
+ let a;
150
+ try { a = ((acl != null && acl.fileAcl) || winAcl.fileAcl)(real); } catch (err) { return `its owner could not be read (${err.code || err.message})`; }
151
+ if (a == null || typeof a.owner !== 'string' || typeof a.user !== 'string') return 'its owner could not be read';
152
+ return [a.user, winAcl.SYSTEM, winAcl.ADMINISTRATORS].includes(a.owner) ? null : `it is owned by ${a.owner}, not this user`;
153
+ }
154
+ let st;
155
+ try { st = fs.statSync(real); } catch (err) { return `it could not be read (${err.code || err.message})`; }
156
+ const me = typeof geteuid === 'function' ? geteuid() : null;
157
+ return me != null && st.uid !== me ? `it is owned by uid ${st.uid}, not this user` : null;
158
+ }
159
+
160
+ // Runs the shim in place of this process (POSIX) or as a child with this
161
+ // process's stdio (Windows, or a Node with no execve). -> exit code (the
162
+ // child's), or null when it could not be started at all (then the caller
163
+ // runs the per-session runtime).
164
+ async function runShim(shim, home, {platform = process.platform, env = process.env, execve = process.execve, spawn = require('node:child_process').spawn} = {}) {
165
+ const args = ['--home', home];
166
+ if (platform !== 'win32' && typeof execve === 'function') {
167
+ try {
168
+ execve.call(process, shim, [shim, ...args], env);
169
+ } catch (err) {
170
+ // (Node 26 on: a failed execve throws; 22 and 24 abort, see the header)
171
+ say(`ch-local: the shared runtime's shim could not be started (${err.code || err.message}); this session runs its own runtime\n`);
172
+ return null;
173
+ }
174
+ }
175
+ return await new Promise((resolve) => {
176
+ let started = false;
177
+ const child = spawn(shim, args, {stdio: 'inherit', env, windowsHide: true});
178
+ child.on('spawn', () => { started = true; });
179
+ const forward = (signal) => {
180
+ if (child.exitCode != null || child.signalCode != null) return;
181
+ try { child.kill(signal); } catch (e) { child.kill(); }
182
+ };
183
+ const SIGNALS = ['SIGTERM', 'SIGINT', 'SIGHUP'];
184
+ const done = (code) => {
185
+ // (no forwarder outlives the child: the session's own runtime, after
186
+ // a shim that never started, traps its own -- the medium on #120)
187
+ for (const signal of SIGNALS) process.removeListener(signal, forward);
188
+ resolve(code);
189
+ };
190
+ child.on('error', (err) => {
191
+ if (started) return;
192
+ say(`ch-local: the shared runtime's shim could not be started (${err.code || err.message}); this session runs its own runtime\n`);
193
+ done(null);
194
+ });
195
+ for (const signal of SIGNALS) process.on(signal, forward);
196
+ child.on('exit', (code) => done(code != null ? code : 1));
197
+ });
198
+ }
199
+
200
+ // a line on stderr without opening it as a Node stream (see the header)
201
+ function say(line) {
202
+ try { fs.writeSync(2, line); } catch (e) { /* no stderr: nothing to say it on */ }
203
+ }
204
+
205
+ // ({home}, io) -> exit code. home: --home's (checked by the caller), else
206
+ // the home rule.
207
+ // The ACL object a session checks with on Windows: win-acl's functions, a
208
+ // test's own (io.winAcl) over them, and the session's cached reader over
209
+ // both, so each path's ACL is read once (the reviewer's high on #126, L9).
210
+ // It was io.winAcl alone, which a real io has none of, so every Windows
211
+ // session's shim check threw (A2, a regression from #126). POSIX: io.winAcl
212
+ // as given. -> {fileAcl, privateProblem, ...} | io.winAcl
213
+ function sessionAcl(io, where) {
214
+ if ((io.platform || process.platform) !== 'win32') return io.winAcl;
215
+ const winAcl = require('./win-acl');
216
+ const base = (io.winAcl != null && io.winAcl.fileAcl) || winAcl.fileAcl;
217
+ const read = new Map();
218
+ // (keyed by the kernel's real path: JS realpathSync keeps an 8.3 short
219
+ // name such as RUNNER~1 that the native one expands, and the two checks
220
+ // resolve the home each their own way: #126's Windows leg)
221
+ const key = (p) => { try { return fs.realpathSync.native(String(p)).toLowerCase(); } catch (err) { return path.resolve(String(p)).toLowerCase(); } };
222
+ // (the paths sessionChoice reads, in one PowerShell run where win-acl's
223
+ // own reader is used: the home, the shim and shim.json -- the medium on
224
+ // A3, 1)
225
+ // (a test's own batch reader when it gives one: the medium on A2, 3)
226
+ const batch = io.winAcl != null && io.winAcl.fileAcl != null ? null : ((io.winAcl != null && io.winAcl.fileAcls) || winAcl.fileAcls);
227
+ const known = [where.home, path.join(where.home, 'shim'), path.join(where.home, 'shim', 'ch-shim.exe'), path.join(where.home, 'shim.json')];
228
+ const readAll = (p) => {
229
+ const list = [p, ...known.filter((f) => key(f) !== key(p) && !read.has(key(f)) && fs.existsSync(f))];
230
+ let acls = null;
231
+ try { acls = batch(list); } catch (err) { acls = null; }
232
+ if (acls == null) { read.set(key(p), base(p)); return; }
233
+ list.forEach((f, i) => read.set(key(f), acls[i]));
234
+ };
235
+ const fileAcl = (p) => { if (!read.has(key(p))) { if (batch != null) readAll(p); else read.set(key(p), base(p)); } return read.get(key(p)); };
236
+ // (only io.winAcl's functions take a check's place: a field given as
237
+ // undefined never removes win-acl's -- the medium on A2, 4)
238
+ const given = Object.fromEntries(Object.entries(io.winAcl || {}).filter(([, v]) => typeof v === 'function'));
239
+ // (no prefetch: a private read or the store read given this reader uses
240
+ // its fileAcl, the session's own batch, never a run of its own)
241
+ const acl = Object.assign({}, winAcl, given, {fileAcl});
242
+ delete acl.prefetch;
243
+ return acl;
244
+ }
245
+
246
+ async function session({home = null}, io) {
247
+ const {layout} = require('./home');
248
+ const where = home != null ? layout({...io.env, COUNTINGHOUSE_HOME: home}, io.homedir) : layout(io.env, io.homedir);
249
+ // a home another user owns runs nothing, either way: its record, its
250
+ // shim and its app are theirs to change (the reviewer's high on #120,
251
+ // round 2, 1; only how open a home of this user's is falls back, below)
252
+ // (Windows: the home's ACL read once for both checks below, each read a
253
+ // PowerShell run: the reviewer's high on #126, L9)
254
+ const acl = sessionAcl(io, where);
255
+ const theirs = ownerProblem(where.home, io.platform, {geteuid: io.geteuid, acl});
256
+ if (theirs != null) {
257
+ say(`ch-local session: ${where.home}: ${theirs}; nothing was started\n`);
258
+ return 2;
259
+ }
260
+ const choice = sessionChoice({where, platform: io.platform, acl});
261
+ if (choice.refuse != null) {
262
+ say(`ch-local session: ${choice.refuse}; nothing was started\n`);
263
+ return 2;
264
+ }
265
+ if (choice.note != null) say(choice.note);
266
+ if (choice.shim != null) {
267
+ // (io.execve, io.shimSpawn: a test's; else Node's own)
268
+ const code = await runShim(choice.shim, where.home, {platform: io.platform, env: io.env, execve: io.execve, spawn: io.shimSpawn});
269
+ if (code != null) return code;
270
+ }
271
+ // (the home as the client's environment would name it, not start's
272
+ // --home, which is the shim's own-runtime hand-over and confines every
273
+ // path to the home: the medium on #120. io's other fields stay as they
274
+ // are, read only when used)
275
+ // (an own-property copy, its getters kept as getters: lib/auto-apply.js
276
+ // copies io with Object.assign, which a prototype's fields would miss)
277
+ const own = home != null ? Object.defineProperties({}, {...Object.getOwnPropertyDescriptors(io), env: {value: {...io.env, COUNTINGHOUSE_HOME: home}, enumerable: true, writable: true, configurable: true}}) : io;
278
+ return await require('./start').start({passThrough: [], home: null, ownRuntime: null}, own);
279
+ }
280
+
281
+ module.exports = {session, sessionChoice, sessionAcl, runShim, formatProblem, ownerProblem};
@@ -0,0 +1,82 @@
1
+ 'use strict';
2
+ // ch-local settings [get | set <name> <value> | reset] (packs in place, part
3
+ // 2, plan Task B4), and a module's settings (L3: `modules [<m>]`,
4
+ // `set <m>.<setting> [<value>]`, `reset <m>[.<setting>]`): for a user at a
5
+ // terminal, or an agent with a shell. set may loosen
6
+ // a guarding setting (the user's own terminal); a running start applies what
7
+ // it can at once and the rest at its next start. Every change is recorded in
8
+ // <home>/history.jsonl.
9
+ const {layout} = require('./home');
10
+ const {resetSettings, planReset, listSettings, readSettings, shown: shownOf} = require('./settings');
11
+ const {ceilingFor, ceilingRefusal} = require('./ceiling');
12
+ const {cliSet} = require('./cli-change');
13
+ const {chLocal, restartEither} = require('./command-line');
14
+
15
+ const shown = (v) => shownOf(null, v);
16
+
17
+ async function settings({action, name, value}, io) {
18
+ const say = (s) => io.stdout.write(s + '\n');
19
+ const where = layout(io.env, io.homedir);
20
+ // a module's settings (#413 section 4.3; lib/module-values.js)
21
+ const mv = require('./module-values');
22
+ if (action === 'modules') return mv.listModuleSettings(where, io, name == null ? null : name);
23
+ if (action === 'set' && mv.isModuleName(name)) return mv.setModuleSetting(where, io, name, value);
24
+ if (action === 'reset' && name != null) {
25
+ // a name of ch-local's own (a cap, enabled.*, pack.*): reset is bare, or
26
+ // a module's (the medium on L3, 9)
27
+ if (require('./settings').entryOf(name) != null) {
28
+ io.stderr.write(`ch-local: settings reset takes no setting of ch-local's own: run \`${chLocal(where, 'settings reset')}\` for all of them, or set ${name} to the value you want; nothing was changed\n`);
29
+ return 2;
30
+ }
31
+ return mv.resetModuleSetting(where, io, name);
32
+ }
33
+ if (action === 'set' && value === undefined) { io.stderr.write(`ch-local: ${name} needs a value; nothing was changed\n`); return 2; }
34
+ if (action === 'reset') {
35
+ // a reset is the loosenings it implies (a pack turned off comes back on,
36
+ // a tightened cap goes back up): each bound by the operator's ceiling,
37
+ // then one question at a terminal listing them; one that only tightens
38
+ // is made at once
39
+ const plan = planReset(where);
40
+ const ceiling = ceilingFor(where, {platform: io.platform, env: io.env});
41
+ const refused = plan.loosens.map((l) => ceilingRefusal(ceiling, l.name, l.new)).filter((r) => r != null);
42
+ if (refused.length > 0 || (plan.unknown && ceiling.state === 'untrusted')) {
43
+ io.stderr.write(`ch-local: the reset is not made: ${refused.length > 0 ? refused.map((r) => r.message.replace(/; nothing was changed$/, '')).join('; ') : require('./ceiling').untrustedWhy(ceiling)}\n`);
44
+ return 2;
45
+ }
46
+ const what = plan.unknown ? ['the settings file is not valid, so what it holds is not known'] : plan.loosens.map((l) => `${l.name} ${shownOf(l.name, l.old)} -> ${shownOf(l.name, l.new)}`);
47
+ if (what.length > 0) {
48
+ if (io.interactive !== true) { io.stderr.write('ch-local: a reset that loosens a guarding setting is confirmed at a terminal: run it in one; nothing was changed\n'); return 2; }
49
+ const answer = String(await io.ask(`Reset every setting to its default? This loosens: ${what.join(', ')}. (A module's settings are kept.) [y/N] `)).trim().toLowerCase();
50
+ if (answer !== 'y' && answer !== 'yes') { io.stderr.write('ch-local: nothing was changed\n'); return 1; }
51
+ }
52
+ let done;
53
+ try {
54
+ done = resetSettings(where, {by: 'cli', expect: plan.expect, confirmed: what.length > 0 ? 'cli' : undefined});
55
+ } catch (err) {
56
+ if (typeof err.code === 'string' && err.code.startsWith('SETTING')) { io.stderr.write(`ch-local: ${err.message}\n`); return 1; }
57
+ throw err;
58
+ }
59
+ // (a module's settings, and names a newer runtime wrote, are not ch-local's
60
+ // to reset: kept, and said)
61
+ const keptLine = done.kept > 0 ? `; ${done.kept} setting${done.kept === 1 ? '' : 's'} of modules or of a newer runtime kept as they were` : '';
62
+ say(`every setting is back to its default${keptLine}; a running client applies it now where it can, and otherwise when it restarts (${restartEither(where)})`);
63
+ return 0;
64
+ }
65
+ if (action === 'get') {
66
+ // a file that is not valid is said, never listed as defaults
67
+ const read = readSettings(where);
68
+ if (read.error != null) {
69
+ io.stderr.write(`ch-local: ${read.error}; ch-local start refuses to start until it is fixed\n`);
70
+ return 1;
71
+ }
72
+ if (read.warning != null) io.stderr.write(`ch-local: ${read.warning}\n`);
73
+ for (const s of listSettings(where)) say(`${s.name} = ${shown(s.value)} (default ${shown(s.default)}; ${s.bounds}${s.guarding ? '; guarding' : ''})`);
74
+ return 0;
75
+ }
76
+ // a loosening: bound by the operator's ceiling, and asked at a terminal (lib/cli-change.js)
77
+ const {code, result: r} = await cliSet(where, name, value, io);
78
+ if (code === 0) say(`${r.name}: ${shownOf(r.name, r.old)} -> ${shownOf(r.name, r.new)}${r.loosened ? ' (this loosens a guarding setting)' : ''}; a running client applies it now where it can, and otherwise when it restarts (${restartEither(where)})`);
79
+ return code;
80
+ }
81
+
82
+ module.exports = {settings};