gentle-pi 4.0.0 → 4.1.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 (398) hide show
  1. package/README.md +122 -8
  2. package/assets/agents/gentle-ai-explore.md +11 -4
  3. package/assets/agents/gentle-ai-verify.md +23 -4
  4. package/assets/agents/gentle-ai-worker.md +6 -6
  5. package/assets/install-wizard/index.html +47 -0
  6. package/assets/install-wizard/wizard.css +1279 -0
  7. package/assets/install-wizard/wizard.js +951 -0
  8. package/assets/orchestrator-delegation.md +70 -163
  9. package/assets/orchestrator-memory.md +11 -4
  10. package/assets/orchestrator-prompts.md +48 -0
  11. package/assets/orchestrator-tracking.md +53 -0
  12. package/assets/orchestrator-verification.md +28 -0
  13. package/assets/orchestrator-writer.md +38 -0
  14. package/assets/orchestrator.md +29 -25
  15. package/assets/sounds/LICENSE.md +52 -0
  16. package/assets/sounds/attention.wav +0 -0
  17. package/assets/sounds/error.wav +0 -0
  18. package/assets/sounds/success.wav +0 -0
  19. package/bin/engram-guard +9 -0
  20. package/bin/gentle-shell-install.mjs +302 -0
  21. package/bin/gentle-shell.mjs +703 -22
  22. package/contracts/review-provider-contract-mirror/provider-contract.lock.json +7 -7
  23. package/contracts/review-provider-contract-mirror/{v1.2.0 → v1.3.0}/bundle/manifest.json +2 -2
  24. package/contracts/review-provider-contract-mirror/{v1.2.0 → v1.3.0}/bundle/orchestration/pi.md +16 -4
  25. package/contracts/review-provider-contract-mirror/{v1.2.0 → v1.3.0}/generated/provider-capabilities.baseline.json +2 -2
  26. package/contracts/review-provider-contract-mirror/{v1.2.0 → v1.3.0}/generated/provider-roles.baseline.json +1 -1
  27. package/docs/bundled-install.md +397 -0
  28. package/docs/delegated-verification.md +3 -5
  29. package/docs/gentle-agents-activity.md +513 -0
  30. package/docs/gentle-shell-uninstall.md +136 -0
  31. package/docs/gentle-shell.md +81 -31
  32. package/docs/install-wizard.md +2040 -0
  33. package/docs/native-authority-architecture.md +1 -1
  34. package/docs/permission-modes.md +82 -0
  35. package/docs/prompt-history.md +1 -1
  36. package/docs/readme-reference.md +267 -40
  37. package/docs/render-cache-performance.md +28 -0
  38. package/docs/sound-notifications-proposal.md +191 -0
  39. package/docs/sound-notifications.md +114 -0
  40. package/docs/yolo-mode.md +9 -9
  41. package/extensions/child-context.ts +11 -2
  42. package/extensions/child-safety.ts +4 -2
  43. package/extensions/codegraph-tools.ts +18 -5
  44. package/extensions/gentle-agents.ts +688 -88
  45. package/extensions/gentle-ai.ts +886 -238
  46. package/extensions/gentle-herdr-activity.ts +81 -0
  47. package/extensions/gentle-notifications.ts +104 -0
  48. package/extensions/gentle-shell.ts +265 -69
  49. package/extensions/gentle-todo.ts +108 -13
  50. package/extensions/history/index.ts +11 -6
  51. package/extensions/pi-pretty.ts +39 -3
  52. package/extensions/quiet-tools.ts +91 -21
  53. package/extensions/skill-registry.ts +175 -46
  54. package/extensions/startup-banner.ts +11 -2
  55. package/lib/agent-home.ts +10 -1
  56. package/lib/agent-model-resolution.ts +80 -0
  57. package/lib/agent-profiles.ts +13 -3
  58. package/lib/agents-completion-delivery.ts +11 -3
  59. package/lib/agents-messaging.ts +1 -1
  60. package/lib/agents-protocol.ts +40 -1
  61. package/lib/agents-runner.ts +90 -15
  62. package/lib/auto-permission-classifier.ts +280 -0
  63. package/lib/auto-permission-runtime.ts +268 -0
  64. package/lib/background-jobs-tools.ts +202 -0
  65. package/lib/background-jobs.ts +253 -0
  66. package/lib/background-monitor.ts +154 -0
  67. package/lib/bounded-writer-admission.ts +83 -10
  68. package/lib/builtin-codemode-optout.ts +166 -0
  69. package/lib/card-content-policy.ts +85 -0
  70. package/lib/child-package-injection.ts +62 -0
  71. package/lib/codemode-renderer.ts +14 -4
  72. package/lib/command-palette-catalog.ts +3 -2
  73. package/lib/context-threshold.ts +26 -0
  74. package/lib/gentle-ai-binary.ts +109 -4
  75. package/lib/gentle-shell-bundled.ts +517 -0
  76. package/lib/gentle-shell-launcher.ts +74 -23
  77. package/lib/gentle-shell-startup-flow.ts +54 -0
  78. package/lib/gentle-shell-startup-update.ts +92 -0
  79. package/lib/gentle-shell-uninstall.ts +840 -0
  80. package/lib/gentle-shell-update-panel.ts +231 -0
  81. package/lib/herdr-activity.ts +183 -0
  82. package/lib/inprocess-reviewer.ts +3 -0
  83. package/lib/jobs-sidebar-state.ts +20 -0
  84. package/lib/jobs-view.ts +161 -0
  85. package/lib/model-price-ratio.ts +83 -0
  86. package/lib/nan-provider.ts +33 -11
  87. package/lib/native-review-cli.ts +194 -73
  88. package/lib/notification-audio-native.ts +162 -0
  89. package/lib/notification-audio-windows.ts +399 -0
  90. package/lib/notification-audio.ts +332 -0
  91. package/lib/notification-customize.ts +241 -0
  92. package/lib/notification-events.ts +113 -0
  93. package/lib/notification-policy.ts +148 -0
  94. package/lib/notification-pulse-client.ts +367 -0
  95. package/lib/notification-pulse-protocol.ts +305 -0
  96. package/lib/notification-pulse-stream.ts +213 -0
  97. package/lib/notification-pulse-worker.ts +66 -0
  98. package/lib/notification-scheduler.ts +173 -0
  99. package/lib/notification-service.ts +202 -0
  100. package/lib/notification-ui.ts +87 -0
  101. package/lib/odd-phase.ts +1 -1
  102. package/lib/orchestrator-alias.ts +20 -0
  103. package/lib/orchestrator-catalog.ts +79 -0
  104. package/lib/orchestrator-consultation.ts +61 -0
  105. package/lib/orchestrator-discovery.ts +49 -0
  106. package/lib/orchestrator-helper-consent.ts +122 -0
  107. package/lib/orchestrator-helper.ts +167 -0
  108. package/lib/orchestrator-message-card.ts +105 -0
  109. package/lib/orchestrator-presence.ts +115 -14
  110. package/lib/orchestrator-scope.ts +80 -0
  111. package/lib/orchestrator-state.ts +107 -0
  112. package/lib/orchestrator-tool-card.ts +280 -0
  113. package/lib/orchestrator-work-search.ts +218 -0
  114. package/lib/orchestrator-work.ts +79 -0
  115. package/lib/permission-mode-policy.ts +59 -0
  116. package/lib/review-candidate-view-owner.ts +8 -6
  117. package/lib/review-candidate-view.ts +3 -2
  118. package/lib/review-compact-contract.ts +2 -2
  119. package/lib/review-host-relay.ts +7 -2
  120. package/lib/review-integration-v2.ts +100 -22
  121. package/lib/review-publication-gate.ts +1 -1
  122. package/lib/review-risk-assessment.ts +77 -51
  123. package/lib/review-sidebar-state.ts +2 -1
  124. package/lib/review-transaction.ts +72 -3
  125. package/lib/runtime-metrics-children.ts +1 -1
  126. package/lib/runtime-metrics-native.ts +3 -2
  127. package/lib/selection-engine.ts +18 -6
  128. package/lib/session-change-capture.ts +5 -4
  129. package/lib/session-changes.ts +27 -22
  130. package/lib/session-profile-binding.ts +83 -0
  131. package/lib/session-profile-freeze.ts +266 -0
  132. package/lib/session-worktree-registry.ts +74 -8
  133. package/lib/shell-bar.ts +41 -11
  134. package/lib/shell-changes-view.ts +40 -13
  135. package/lib/shell-changes.ts +86 -71
  136. package/lib/shell-sidebar-layout.ts +5 -1
  137. package/lib/shell-todo.ts +62 -19
  138. package/lib/shell-usage.ts +4 -3
  139. package/lib/status-timing-diagnostics.ts +201 -0
  140. package/lib/telemetry-trigger.ts +3 -1
  141. package/lib/vim-editor-adapter.ts +7 -3
  142. package/lib/visual-customization-policy.ts +20 -9
  143. package/lib/visual-customize-view.ts +171 -20
  144. package/lib/writer-surfaces.ts +162 -0
  145. package/lib/yolo-session-policy.ts +198 -121
  146. package/package.json +1 -1
  147. package/runtime/child-package-injection.mjs +63 -0
  148. package/runtime/gentle-ai-binary.mjs +109 -4
  149. package/runtime/gentle-shell-bundled.mjs +518 -0
  150. package/runtime/gentle-shell-launcher.mjs +73 -22
  151. package/runtime/gentle-shell-startup-flow.mjs +55 -0
  152. package/runtime/gentle-shell-startup-update.mjs +93 -0
  153. package/runtime/gentle-shell-uninstall.mjs +841 -0
  154. package/runtime/gentle-shell-update-panel.mjs +232 -0
  155. package/runtime/native-review-cli.mjs +191 -70
  156. package/runtime/review-integration-v2.mjs +100 -22
  157. package/runtime/review-risk-assessment.mjs +77 -51
  158. package/runtime/status-timing-diagnostics.mjs +202 -0
  159. package/runtime/telemetry-trigger.mjs +3 -1
  160. package/scripts/bootstrap.cmd +255 -0
  161. package/scripts/bootstrap.sh +168 -0
  162. package/scripts/build-distribution.mjs +157 -0
  163. package/scripts/build-installer-bundles.mjs +136 -0
  164. package/scripts/build-runtime-modules.mjs +7 -0
  165. package/scripts/bundled-install.mjs +1308 -0
  166. package/scripts/bundled-wizard.mjs +399 -0
  167. package/scripts/check-types.mjs +4 -6
  168. package/scripts/gentle-ai-installer.mjs +79 -32
  169. package/scripts/install-gentle-ai.mjs +48 -1
  170. package/scripts/install-tui-mode-setting.mjs +10 -1
  171. package/scripts/install-wizard-preview.mjs +154 -0
  172. package/scripts/installer-downloads.mjs +648 -0
  173. package/scripts/installer-preflight.mjs +387 -0
  174. package/scripts/installer-probes.mjs +564 -0
  175. package/scripts/installer-runner.mjs +1102 -0
  176. package/scripts/installer-server.mjs +881 -0
  177. package/scripts/installer-windows-artifacts.json +15 -0
  178. package/scripts/installer-windows.mjs +752 -0
  179. package/scripts/main-channel.mjs +452 -0
  180. package/scripts/npm-pack-result.mjs +31 -0
  181. package/scripts/render-cache-performance-worker.mjs +205 -0
  182. package/scripts/render-cache-performance.mjs +115 -0
  183. package/scripts/run-test-suite.mjs +22 -8
  184. package/scripts/test-installer-acceptance.sh +161 -0
  185. package/scripts/test-packed-runner.mjs +23 -5
  186. package/scripts/types-baseline.json +2 -92
  187. package/scripts/verify-package-files.mjs +82 -18
  188. package/skills/gentle-ai/SKILL.md +12 -11
  189. package/tests/agent-home.test.ts +56 -3
  190. package/tests/agent-model-resolution.test.ts +113 -0
  191. package/tests/agent-profiles.test.ts +12 -0
  192. package/tests/agent-risk-escalation.test.ts +123 -0
  193. package/tests/agents-completion-delivery.test.ts +17 -0
  194. package/tests/agents-protocol.test.ts +57 -0
  195. package/tests/agents-queries.test.ts +9 -9
  196. package/tests/agents-runner-process.test.ts +31 -0
  197. package/tests/agents-runner.test.ts +89 -4
  198. package/tests/auto-mode-invariants.test.ts +258 -0
  199. package/tests/auto-mode-sdk.test.ts +57 -0
  200. package/tests/auto-mode.test.ts +49 -0
  201. package/tests/auto-permission-classifier.test.ts +283 -0
  202. package/tests/auto-permission-runtime.test.ts +196 -0
  203. package/tests/background-cache-warming.test.ts +2 -1
  204. package/tests/background-jobs.test.ts +227 -0
  205. package/tests/background-monitor.test.ts +195 -0
  206. package/tests/background-subagents-default-mode.test.ts +28 -5
  207. package/tests/bounded-stop-contract.test.ts +85 -0
  208. package/tests/bounded-writer-admission.test.ts +52 -1
  209. package/tests/bridge-wake-lifecycle.test.ts +129 -0
  210. package/tests/bridge-wake-rendering.test.ts +56 -0
  211. package/tests/build-distribution.test.ts +269 -0
  212. package/tests/builtin-codemode-optout.test.ts +242 -0
  213. package/tests/bundled-install.test.ts +1319 -0
  214. package/tests/bundled-wizard.test.ts +505 -0
  215. package/tests/card-content-policy.test.ts +68 -0
  216. package/tests/card-style-policy.test.ts +34 -2
  217. package/tests/child-codegraph-bootstrap.test.ts +74 -0
  218. package/tests/child-package-entrypoints.test.ts +65 -0
  219. package/tests/child-package-injection.test.ts +74 -0
  220. package/tests/child-safety.test.ts +12 -0
  221. package/tests/child-web-package.test.ts +224 -0
  222. package/tests/codegraph-tools.test.ts +35 -0
  223. package/tests/codemode-rendering.test.ts +107 -2
  224. package/tests/command-palette.test.ts +5 -5
  225. package/tests/context-threshold.test.ts +31 -0
  226. package/tests/delegate-for-reason-contract.test.ts +327 -0
  227. package/tests/delegated-key-learnings-contract.test.ts +3 -2
  228. package/tests/devbinary/native-review-parity.devtest.ts +1 -1
  229. package/tests/devbinary/pi-host-relay.devtest.ts +173 -3
  230. package/tests/engram-safety.test.ts +288 -0
  231. package/tests/generic-agent-tools.test.ts +16 -1
  232. package/tests/gentle-agents.test.ts +1722 -59
  233. package/tests/gentle-ai-binary-isolation.test.ts +135 -0
  234. package/tests/gentle-ai-binary.test.ts +19 -2
  235. package/tests/gentle-ai-child-guards.test.ts +202 -0
  236. package/tests/gentle-ai-dev-binary-surfacing.test.ts +125 -15
  237. package/tests/gentle-ai-installer.test.ts +548 -58
  238. package/tests/gentle-ai-prompt-model-routing.test.ts +123 -0
  239. package/tests/gentle-ai-renderer.test.ts +6 -6
  240. package/tests/gentle-ai.test.ts +999 -31
  241. package/tests/gentle-herdr-activity.test.ts +100 -0
  242. package/tests/gentle-notifications.test.ts +427 -0
  243. package/tests/gentle-shell-bin.test.ts +249 -5
  244. package/tests/gentle-shell-bundled.test.ts +1002 -0
  245. package/tests/gentle-shell-launcher.test.ts +234 -13
  246. package/tests/gentle-shell-startup-flow.test.ts +75 -0
  247. package/tests/gentle-shell-startup-update.test.ts +85 -0
  248. package/tests/gentle-shell-uninstall.test.ts +978 -0
  249. package/tests/gentle-shell-update-panel.test.ts +158 -0
  250. package/tests/gentle-shell.test.ts +541 -96
  251. package/tests/gentle-todo.test.ts +253 -2
  252. package/tests/herdr-activity.test.ts +243 -0
  253. package/tests/history-agent-home.test.ts +81 -0
  254. package/tests/history-child-guard.test.ts +70 -0
  255. package/tests/inprocess-reviewer.test.ts +20 -0
  256. package/tests/install-gentle-ai-failure.test.ts +69 -0
  257. package/tests/install-tui-mode-setting.test.ts +15 -0
  258. package/tests/install-wizard.test.ts +1223 -0
  259. package/tests/installer-bundles.test.ts +122 -0
  260. package/tests/installer-posix-bootstrap.test.ts +1201 -0
  261. package/tests/installer-preflight.test.ts +750 -0
  262. package/tests/installer-probes.test.ts +1106 -0
  263. package/tests/installer-runner.test.ts +2158 -0
  264. package/tests/installer-server.test.ts +1567 -0
  265. package/tests/installer-windows-bootstrap.test.ts +2410 -0
  266. package/tests/jobs-view.test.ts +160 -0
  267. package/tests/main-channel-upgrade.test.ts +389 -0
  268. package/tests/main-channel.test.ts +344 -0
  269. package/tests/maintainer/provider-relay.maintest.ts +13 -12
  270. package/tests/model-price-ratio.test.ts +116 -0
  271. package/tests/model-profiles-preservation.test.ts +153 -0
  272. package/tests/model-routing-package.test.ts +10 -0
  273. package/tests/nan-provider-runtime.test.ts +156 -0
  274. package/tests/nan-provider.test.ts +28 -18
  275. package/tests/native-binary-gate.test.ts +2 -0
  276. package/tests/native-review-capability-contract.test.ts +7 -1
  277. package/tests/native-review-cli.test.ts +236 -1
  278. package/tests/native-review-consent.test.ts +2 -2
  279. package/tests/native-review-parity-runtime.test.ts +1 -1
  280. package/tests/native-review-parity.test.ts +12 -48
  281. package/tests/native-risk-evidence-parity.test.ts +21 -0
  282. package/tests/no-self-pause-contract.test.ts +87 -0
  283. package/tests/notification-audio-native.test.ts +281 -0
  284. package/tests/notification-audio-windows.test.ts +581 -0
  285. package/tests/notification-audio.test.ts +548 -0
  286. package/tests/notification-customize.test.ts +655 -0
  287. package/tests/notification-events.test.ts +115 -0
  288. package/tests/notification-policy.test.ts +201 -0
  289. package/tests/notification-pulse-client.test.ts +356 -0
  290. package/tests/notification-pulse-protocol.test.ts +213 -0
  291. package/tests/notification-pulse-stream.test.ts +320 -0
  292. package/tests/notification-scheduler.test.ts +258 -0
  293. package/tests/notification-ui.test.ts +157 -0
  294. package/tests/npm-pack-result.test.ts +41 -0
  295. package/tests/odd-integration.test.ts +14 -5
  296. package/tests/odd-routing-canonical-ratchet.test.ts +53 -12
  297. package/tests/odd-routing-contract.test.ts +80 -23
  298. package/tests/orchestrator-budget.test.ts +29 -6
  299. package/tests/orchestrator-catalog.test.ts +146 -0
  300. package/tests/orchestrator-consultation-sdk.test.ts +614 -0
  301. package/tests/orchestrator-consultation.test.ts +111 -0
  302. package/tests/orchestrator-discovery.test.ts +241 -0
  303. package/tests/orchestrator-helper-consent.test.ts +171 -0
  304. package/tests/orchestrator-helper.test.ts +297 -0
  305. package/tests/orchestrator-message-card.test.ts +190 -0
  306. package/tests/orchestrator-presence.test.ts +23 -0
  307. package/tests/orchestrator-rdd-ownership.test.ts +6 -5
  308. package/tests/orchestrator-scope.test.ts +107 -0
  309. package/tests/orchestrator-state.test.ts +114 -0
  310. package/tests/orchestrator-status-publication-contract.test.ts +34 -0
  311. package/tests/orchestrator-tool-card.test.ts +398 -0
  312. package/tests/orchestrator-work-search.test.ts +318 -0
  313. package/tests/orchestrator-work.test.ts +147 -0
  314. package/tests/package-manifest.test.ts +101 -17
  315. package/tests/packed-runner-owned-path.test.ts +35 -1
  316. package/tests/parallel-reason-contract.test.ts +21 -0
  317. package/tests/parallel-writer-admission.test.ts +210 -0
  318. package/tests/permission-mode-policy.test.ts +194 -0
  319. package/tests/persona-single-channel.test.ts +4 -12
  320. package/tests/pi-pretty.test.ts +56 -0
  321. package/tests/provider-defect-handoff.test.ts +4 -3
  322. package/tests/quiet-tool-rendering.test.ts +160 -3
  323. package/tests/rdd-aware-verification-contract.test.ts +29 -41
  324. package/tests/render-cache-performance.test.ts +48 -0
  325. package/tests/review-agent-end-preflight.test.ts +62 -10
  326. package/tests/review-candidate-view.test.ts +101 -12
  327. package/tests/review-contract-prompt.test.ts +33 -4
  328. package/tests/review-controller-native-recovery.test.ts +327 -17
  329. package/tests/review-controller-native-routing.test.ts +255 -1
  330. package/tests/review-controller-workspace-root.test.ts +43 -2
  331. package/tests/review-controller.test.ts +2 -2
  332. package/tests/review-correction-lifecycle.test.ts +2 -1
  333. package/tests/review-gate.test.ts +7 -4
  334. package/tests/review-graph-schema.test.ts +2 -2
  335. package/tests/review-host-relay-routing.test.ts +5 -3
  336. package/tests/review-host-relay.test.ts +49 -2
  337. package/tests/review-integration-v2-forward.test.ts +21 -1
  338. package/tests/review-json-arguments.test.ts +167 -0
  339. package/tests/review-last-event-closure.test.ts +157 -8
  340. package/tests/review-ledger-contract.test.ts +5 -4
  341. package/tests/review-policy-judgment-day.test.ts +1 -1
  342. package/tests/review-policy-ordinary.test.ts +2 -2
  343. package/tests/review-relay-transport-agent.test.ts +66 -1
  344. package/tests/review-risk-assessment.test.ts +109 -33
  345. package/tests/review-session-standing-permission-controller.test.ts +16 -17
  346. package/tests/review-session-standing-permission-ipc.test.ts +6 -5
  347. package/tests/review-test-fixtures.ts +7 -1
  348. package/tests/review-transaction.test.ts +18 -1
  349. package/tests/run-test-suite.test.ts +83 -5
  350. package/tests/runtime-harness.mjs +42 -15
  351. package/tests/runtime-metrics-extension.test.ts +1 -1
  352. package/tests/runtime-metrics-native.test.ts +31 -1
  353. package/tests/selection-engine.test.ts +30 -0
  354. package/tests/session-change-capture.test.ts +10 -10
  355. package/tests/session-changes-shell.test.ts +242 -48
  356. package/tests/session-changes.test.ts +17 -24
  357. package/tests/session-profile-binding.test.ts +163 -0
  358. package/tests/session-profile-freeze.test.ts +381 -0
  359. package/tests/session-subject-routing-contract.test.ts +34 -0
  360. package/tests/session-worktree-registry.test.ts +57 -2
  361. package/tests/shell-bar.test.ts +40 -0
  362. package/tests/shell-changes-view.test.ts +85 -34
  363. package/tests/shell-changes.test.ts +61 -62
  364. package/tests/shell-render-cache-runtime.test.ts +69 -0
  365. package/tests/shell-render-cache.test.ts +129 -0
  366. package/tests/shell-sidebar-layout.test.ts +27 -2
  367. package/tests/shell-todo.test.ts +120 -2
  368. package/tests/shell-usage-view.test.ts +1 -1
  369. package/tests/shell-usage.test.ts +35 -0
  370. package/tests/short-check-routing-contract.test.ts +49 -0
  371. package/tests/skill-registry-watcher.test.ts +119 -0
  372. package/tests/skill-registry.test.ts +311 -5
  373. package/tests/startup-banner.test.ts +117 -3
  374. package/tests/startup-home-repo.test.ts +170 -0
  375. package/tests/status-timing-diagnostics.test.ts +283 -0
  376. package/tests/support/orchestrator-modules.ts +19 -0
  377. package/tests/task-size-routing-contract.test.ts +241 -0
  378. package/tests/telemetry-trigger.test.ts +27 -1
  379. package/tests/test-strength-contract.test.ts +124 -0
  380. package/tests/verify-agent-contract.test.ts +90 -0
  381. package/tests/verify-first-launch-contract.test.ts +139 -0
  382. package/tests/verify-package-files.test.ts +56 -0
  383. package/tests/vim-editor-adapter.test.ts +7 -7
  384. package/tests/visual-customization-policy.test.ts +32 -3
  385. package/tests/visual-customize-view.test.ts +288 -10
  386. package/tests/writer-edit-surface-scope.test.ts +59 -2
  387. package/tests/writer-surfaces.test.ts +199 -0
  388. package/tests/yolo-customize.test.ts +18 -3
  389. package/tests/yolo-mode-runtime.test.ts +37 -10
  390. package/tests/yolo-mode.test.ts +32 -4
  391. package/tests/yolo-session-policy.test.ts +1 -1
  392. /package/contracts/review-provider-contract-mirror/{v1.2.0 → v1.3.0}/bundle/README.md +0 -0
  393. /package/contracts/review-provider-contract-mirror/{v1.2.0 → v1.3.0}/bundle/schemas/lens.schema.json +0 -0
  394. /package/contracts/review-provider-contract-mirror/{v1.2.0 → v1.3.0}/bundle/schemas/refuter.schema.json +0 -0
  395. /package/contracts/review-provider-contract-mirror/{v1.2.0 → v1.3.0}/bundle/schemas/targeted-validator.schema.json +0 -0
  396. /package/contracts/review-provider-contract-mirror/{v1.2.0 → v1.3.0}/bundle/vectors/lens.json +0 -0
  397. /package/contracts/review-provider-contract-mirror/{v1.2.0 → v1.3.0}/bundle/vectors/refuter.json +0 -0
  398. /package/contracts/review-provider-contract-mirror/{v1.2.0 → v1.3.0}/bundle/vectors/targeted-validator.json +0 -0
@@ -10,11 +10,14 @@ import {
10
10
  closeSync,
11
11
  constants as fsConstants,
12
12
  existsSync,
13
+ fstatSync,
14
+ lstatSync,
13
15
  mkdirSync,
14
16
  mkdtempSync,
15
17
  openSync,
16
18
  readdirSync,
17
19
  readFileSync,
20
+ readSync,
18
21
  realpathSync,
19
22
  renameSync,
20
23
  rmSync,
@@ -24,6 +27,7 @@ import {
24
27
  import { constants as osConstants, homedir, tmpdir } from "node:os";
25
28
  import { delimiter, dirname, join, resolve as resolvePath } from "node:path";
26
29
  import { spawn, spawnSync } from "node:child_process";
30
+ import { createHash } from "node:crypto";
27
31
  import { fileURLToPath } from "node:url";
28
32
  import {
29
33
  buildPiInvocation,
@@ -54,6 +58,33 @@ import {
54
58
  restoreJsonField,
55
59
  shellQuote,
56
60
  } from "../runtime/gentle-shell-launcher.mjs";
61
+ import {
62
+ BUNDLED_VERIFY_ENV,
63
+ bundledEngramSchema,
64
+ classifyEngramStartup,
65
+ ENGRAM_COMPANION_SOURCE,
66
+ ENGRAM_PROVISION_FAILED_NOTICE,
67
+ ENGRAM_RECOVERY_NOTICE,
68
+ engramHeaderAllows,
69
+ engramProbeAllowed,
70
+ engramProvenanceMatches,
71
+ engramProvenancePath,
72
+ parseEngramProvenance,
73
+ piVersionOrHelpRequested,
74
+ probeEngramEndpoint,
75
+ sqliteHeaderUserVersion,
76
+ supportsEngramStartupPolicy,
77
+ bundledChildEnv,
78
+ bundledPi,
79
+ bundledSetupEnv,
80
+ bundledUpdateNext,
81
+ bundledUpdateNotice,
82
+ bundledUpdatePlan,
83
+ nodeBundledFs,
84
+ parseBundledUpgradeArgs,
85
+ resolveBundledInstall,
86
+ withBundledNpmCommand,
87
+ } from "../runtime/gentle-shell-bundled.mjs";
57
88
  import {
58
89
  parseResumeHandoff,
59
90
  planResumeHint,
@@ -61,8 +92,12 @@ import {
61
92
  RESUME_HANDOFF_ENV,
62
93
  RESUME_HANDOFF_FILE,
63
94
  } from "../runtime/gentle-shell-resume-hint.mjs";
64
- import { GENTLE_AI_VERSION, gentleAiBinaryPath, PackageLocalGentleAiBinaryMissingError } from "../runtime/gentle-ai-binary.mjs";
95
+ import { bundledGentleAiPaths, GENTLE_AI_VERSION, gentleAiBinaryPath, gentleAiIsolationEnv, PackageLocalGentleAiBinaryMissingError } from "../runtime/gentle-ai-binary.mjs";
65
96
  import { DEFAULT_THEME_NAME, installIsolatedTuiModeSetting } from "../scripts/install-tui-mode-setting.mjs";
97
+ import { artifactFor, engramPinVersion } from "../scripts/installer-downloads.mjs";
98
+ import { shouldCheckStartupUpdate, findStartupUpdate } from "../runtime/gentle-shell-startup-update.mjs";
99
+ import { promptStartupUpdate } from "../runtime/gentle-shell-update-panel.mjs";
100
+ import { applyStartupChoice, omitStartupVersion, resolveStartupRelaunch } from "../runtime/gentle-shell-startup-flow.mjs";
66
101
 
67
102
  const packageRoot = dirname(dirname(fileURLToPath(import.meta.url)));
68
103
 
@@ -128,6 +163,218 @@ function signalExitCode(signal) {
128
163
  return 128 + (typeof number === "number" ? number : 0);
129
164
  }
130
165
 
166
+ // A bundled install (S8): this package inside <prefix>/versions/<id> that
167
+ // `current` names (lib/gentle-shell-bundled.ts decides from these reads).
168
+ const bundledFs = nodeBundledFs;
169
+
170
+ // Set in a bundled install: the environment every child (Pi, setup, the version
171
+ // probe) starts from instead of process.env (the user's own plus Gentle Shell's
172
+ // variables), and the folders setup puts ahead of the user's PATH.
173
+ let bundledEnv;
174
+ let bundledSetupDirs;
175
+ // The bundled install and its Pi cli, for bundledProcessEnv and setup's Engram.
176
+ let bundledInstall;
177
+ let bundledCli;
178
+ let bundledMemory;
179
+ // The Shell-owned guard ENGRAM_BIN names when memory is disabled (bin/engram-guard).
180
+ const ENGRAM_GUARD = join(packageRoot, "bin", "engram-guard");
181
+
182
+ // The pinned core only as ensureEngram published it: its runtime marker for
183
+ // this release's archive and a regular file. Never a link or a PATH engram.
184
+ function verifiedBundledCore() {
185
+ const core = bundledGentleAiPaths(bundledInstall.root, process.platform).engram;
186
+ try {
187
+ const marker = readFileSync(join(dirname(core), ".gentle-shell-runtime"), "utf8");
188
+ if (marker === `${artifactFor("engram", process.platform, process.arch).url}\n` && lstatSync(core).isFile()) return core;
189
+ } catch {
190
+ // Missing, unmarked, or an unsupported target: no verified core.
191
+ }
192
+ return undefined;
193
+ }
194
+
195
+ function sha256File(path) {
196
+ return createHash("sha256").update(readFileSync(path)).digest("hex");
197
+ }
198
+
199
+ function readSmallRegularFile(path, limit) {
200
+ try {
201
+ const info = lstatSync(path);
202
+ return info.isFile() && info.size <= limit ? readFileSync(path, "utf8") : undefined;
203
+ } catch {
204
+ return undefined;
205
+ }
206
+ }
207
+
208
+ // The database's raw SQLite header, read as a plain file (never through
209
+ // SQLite): a regular, unlinked engram.db whose opened identity is the one seen.
210
+ function readSqliteUserVersion(path) {
211
+ let fd;
212
+ try {
213
+ const seen = lstatSync(path);
214
+ if (!seen.isFile()) return undefined;
215
+ fd = openSync(path, fsConstants.O_RDONLY | (fsConstants.O_NOFOLLOW ?? 0));
216
+ const opened = fstatSync(fd);
217
+ if (opened.ino !== seen.ino || opened.dev !== seen.dev) return undefined;
218
+ const header = Buffer.alloc(100);
219
+ return readSync(fd, header, 0, 100, 0) === 100 ? sqliteHeaderUserVersion(header, opened.size) : undefined;
220
+ } catch {
221
+ return undefined;
222
+ } finally {
223
+ if (fd !== undefined) closeSync(fd);
224
+ }
225
+ }
226
+
227
+ // The default data directory without opening it: presence, canonical path,
228
+ // the .instance-id file and the database header. A link is never followed.
229
+ function observeEngramStore() {
230
+ const dir = join(homedir(), ".engram");
231
+ let dataDir;
232
+ try {
233
+ if (!lstatSync(dir).isDirectory()) return { store: "unknown" };
234
+ dataDir = realpathSync(dir);
235
+ } catch (error) {
236
+ return { store: error.code === "ENOENT" ? "absent" : "unknown" };
237
+ }
238
+ return { store: "present", dir, dataDir, instanceId: readSmallRegularFile(join(dir, ".instance-id"), 128)?.trim() };
239
+ }
240
+
241
+ // Only the identity files Engram's `instance-id` writes: a store Shell
242
+ // provisioned whose session has not created its database yet.
243
+ function identityOnlyStore(dir) {
244
+ try {
245
+ return readdirSync(dir).every((name) => name === ".instance-id" || name === ".instance-id.lock" || name.startsWith(".instance-id-"));
246
+ } catch {
247
+ return false;
248
+ }
249
+ }
250
+
251
+ // Shell's own store: the record Shell wrote when it provisioned this exact
252
+ // directory's identity, the same core digest, and a database header no newer
253
+ // than the bundled schema (or no database yet).
254
+ function ownEngramStore(observed, core) {
255
+ let record;
256
+ try {
257
+ record = parseEngramProvenance(readJsonIfExists(engramProvenancePath(bundledInstall.root, process.platform)));
258
+ } catch {
259
+ return undefined;
260
+ }
261
+ if (record === undefined || observed.store !== "present" || core === undefined) return undefined;
262
+ if (!engramProvenanceMatches(record, { dataDir: observed.dataDir, instanceId: observed.instanceId, coreSha256: sha256File(core) })) return undefined;
263
+ const headerAllows = engramHeaderAllows(readSqliteUserVersion(join(observed.dir, "engram.db")), bundledEngramSchema(engramPinVersion));
264
+ return headerAllows || identityOnlyStore(observed.dir) ? { instanceId: record.instanceId } : undefined;
265
+ }
266
+
267
+ // Evidence only: file metadata, the raw header and a bounded read-only probe
268
+ // of the default endpoint. No Engram executable, SQLite, identity repair, or
269
+ // contact with an explicitly selected endpoint.
270
+ async function classifyBundledMemory() {
271
+ const core = verifiedBundledCore();
272
+ const decision = { env: process.env, core: core !== undefined, coreVersion: engramPinVersion };
273
+ if (!engramProbeAllowed(process.env)) return classifyEngramStartup({ ...decision, store: "unknown", endpoint: { kind: "unknown" } });
274
+ const observed = observeEngramStore();
275
+ const endpoint = await probeEngramEndpoint();
276
+ return classifyEngramStartup({ ...decision, store: observed.store, endpoint, provenance: ownEngramStore(observed, core) });
277
+ }
278
+
279
+ // Verified read-only in Engram v3.3.2: `engram instance-id` first runs a GitHub
280
+ // update check that sends GH_TOKEN or GITHUB_TOKEN (cmd/engram/main.go:742-744,
281
+ // 835-847; internal/version/check.go:23-24,60-70,171-176) unless
282
+ // ENGRAM_NO_UPDATE_CHECK is set (check.go:19,49-51,114-117). It then resolves the
283
+ // default ~/.engram (main.go:749-765) and runs EnsureInstanceID
284
+ // (internal/store/instance.go:15-79): the data dir, `.instance-id.lock` (waits
285
+ // up to 60s, internal/store/migration_lock.go:29-76) and `.instance-id`. It
286
+ // returns (main.go:767-775) before orphan migration (main.go:779) and opens no
287
+ // database. The timeout stays below that lock wait: a held lock means a
288
+ // concurrent creator, and memory then stays off for the launch.
289
+ const ENGRAM_PROVISION_TIMEOUT_MS = 15000;
290
+
291
+ // A fresh launch's store identity, provisioned by Shell itself with the verified
292
+ // core before Pi starts, then recorded. False when any step fails.
293
+ function provisionEngramStore() {
294
+ const core = verifiedBundledCore();
295
+ if (core === undefined || observeEngramStore().store !== "absent") return false;
296
+ let target;
297
+ try {
298
+ target = join(realpathSync(homedir()), ".engram");
299
+ } catch {
300
+ return false;
301
+ }
302
+ // The provider's own environment, except for this one invocation: no update
303
+ // check and no GitHub tokens. ENGRAM_DATA_DIR is never set here.
304
+ const env = { ...process.env, ENGRAM_NO_UPDATE_CHECK: "1" };
305
+ delete env.GH_TOKEN;
306
+ delete env.GITHUB_TOKEN;
307
+ const result = spawnSync(core, ["instance-id"], { env, encoding: "utf8", timeout: ENGRAM_PROVISION_TIMEOUT_MS, stdio: ["ignore", "pipe", "pipe"], windowsHide: true });
308
+ const observed = observeEngramStore();
309
+ if (result.status !== 0 || observed.store !== "present" || observed.dataDir !== target || !/^[a-f0-9]{32}$/.test(observed.instanceId ?? "") ||
310
+ String(result.stdout).trim() !== observed.instanceId) return false;
311
+ return writeEngramProvenance({ schema: 1, dataDir: target, instanceId: observed.instanceId, coreSha256: sha256File(core) });
312
+ }
313
+
314
+ function writeEngramProvenance(record) {
315
+ try {
316
+ const path = engramProvenancePath(bundledInstall.root, process.platform);
317
+ mkdirSync(dirname(path), { recursive: true, mode: 0o700 });
318
+ const temporary = `${path}.${process.pid}.tmp`;
319
+ writeFileSync(temporary, `${JSON.stringify(record, null, 2)}\n`, { mode: 0o600, flag: "wx" });
320
+ renameSync(temporary, path);
321
+ return true;
322
+ } catch (error) {
323
+ process.stderr.write(`gentle-shell: could not record Engram provenance (${error.message})\n`);
324
+ return false;
325
+ }
326
+ }
327
+
328
+ // Use Pi's supported configuration-only install command, never an alternate
329
+ // extension loader. Verify advertised enforcement before trusting the policy.
330
+ async function ensureEngramCompanion(home, runtime, stdio, timeoutMs) {
331
+ const metadata = join(home.dir, "npm", "node_modules", "gentle-engram", "package.json");
332
+ let packages;
333
+ try {
334
+ packages = JSON.parse(readJsonIfExists(join(home.dir, "settings.json")) ?? "{}").packages ?? [];
335
+ } catch {
336
+ return { ok: false, exitCode: 1, message: "gentle-shell: cannot verify Engram startupPolicy from malformed settings; repair settings and recheck" };
337
+ }
338
+ if (!Array.isArray(packages)) return { ok: false, exitCode: 1, message: "gentle-shell: cannot verify Engram startupPolicy from non-array packages" };
339
+ let pinned = false;
340
+ let local = false;
341
+ let npmSelected = false;
342
+ for (const entry of packages) {
343
+ const source = typeof entry === "string" ? entry : entry?.source;
344
+ if (source === ENGRAM_COMPANION_SOURCE) pinned = true;
345
+ if (typeof source === "string" && /^npm:gentle-engram(?:@|$)/.test(source)) npmSelected = true;
346
+ if (typeof source !== "string" || source.startsWith("npm:")) continue;
347
+ // Preserve an explicit local companion selection rather than overwrite it.
348
+ const selected = resolvePath(home.dir, source);
349
+ if (readPackageName(selected) !== "gentle-engram") continue;
350
+ if (!supportsEngramStartupPolicy(readJsonIfExists(join(selected, "package.json")))) {
351
+ return { ok: false, exitCode: 1, message: `gentle-shell: selected companion ${source} lacks engram.startupPolicy=1; update it and recheck (selection preserved)` };
352
+ }
353
+ local = true;
354
+ }
355
+ if (local && !npmSelected) return { ok: true };
356
+ if (pinned && supportsEngramStartupPolicy(readJsonIfExists(metadata))) return { ok: true };
357
+ process.stderr.write(`gentle-shell: installing policy-capable companion ${ENGRAM_COMPANION_SOURCE} through Pi\n`);
358
+ const result = await spawnAndWait(runtime.command, [...runtime.args, "install", ENGRAM_COMPANION_SOURCE], buildSetupEnv(home, runtime), stdio, timeoutMs);
359
+ let declared = false;
360
+ try {
361
+ declared = JSON.parse(readJsonIfExists(join(home.dir, "settings.json")) ?? "{}").packages?.some((entry) =>
362
+ (typeof entry === "string" ? entry : entry?.source) === ENGRAM_COMPANION_SOURCE) === true;
363
+ } catch { /* Missing or malformed readback cannot establish enforcement. */ }
364
+ if (!result.ok || result.timedOut || !declared || !supportsEngramStartupPolicy(readJsonIfExists(metadata))) {
365
+ return { ok: false, exitCode: 1, message: "gentle-shell: companion installation did not establish engram.startupPolicy=1; the Engram guard still keeps unverified stores closed. Repair gentle-engram, then restart Shell." };
366
+ }
367
+ return { ok: true };
368
+ }
369
+
370
+ // The bundled children's environment (bundledChildEnv), with the pinned Engram
371
+ // only once it is published: computed again right before Pi starts, so an
372
+ // Engram the automatic setup just published is used in the same launch.
373
+ function bundledProcessEnv() {
374
+ return bundledChildEnv(process.env, { node: process.execPath, cli: bundledCli, prefix: bundledInstall.root, engram: verifiedBundledCore(),
375
+ guard: ENGRAM_GUARD, memory: bundledMemory });
376
+ }
377
+
131
378
  function ownPackageVersion() {
132
379
  const packageJson = JSON.parse(readFileSync(join(packageRoot, "package.json"), "utf8"));
133
380
  return packageJson.version;
@@ -550,15 +797,50 @@ function ensurePackageLocalGentleAi(binaryPath, pinnedVersion, stdio) {
550
797
  // home, and the resolved pi runtime's directory is prepended to PATH so
551
798
  // gentle-ai's (or pi's own) preflight finds `pi` even when it is bundled or
552
799
  // given through GENTLE_SHELL_PI.
553
- function buildSetupEnv(home, runtime) {
800
+ function buildSetupEnv(home, runtime, piLauncherDir) {
801
+ // Bundled: our node, then the version's `pi`, ahead of the user's PATH, and
802
+ // Gentle AI's state, persona and Engram inside the prefix (S9).
803
+ if (bundledEnv) {
804
+ const env = bundledSetupEnv({ ...bundledEnv, PI_CODING_AGENT_DIR: home.dir, GENTLE_PI_AGENT_HOME: home.dir }, { dirs: bundledSetupDirs, platform: process.platform });
805
+ return gentleAiIsolationEnv(env, { packageRoot });
806
+ }
554
807
  return {
555
808
  ...process.env,
556
809
  PI_CODING_AGENT_DIR: home.dir,
557
810
  GENTLE_PI_AGENT_HOME: home.dir,
558
- PATH: `${dirname(runtime.command)}${delimiter}${process.env.PATH ?? ""}`,
811
+ PATH: [...(piLauncherDir ? [piLauncherDir] : []), dirname(runtime.command), process.env.PATH ?? ""].join(delimiter),
559
812
  };
560
813
  }
561
814
 
815
+ // Gentle AI requires a `pi` on its PATH and runs `pi install npm:gentle-pi`
816
+ // with it. A Pi gentle-shell resolved elsewhere (the adjacent package, run
817
+ // as `node <cli>`, or GENTLE_SHELL_PI) is not that `pi`, so a first launch
818
+ // from a terminal without one failed every time. This writes a private,
819
+ // temporary `pi` that runs exactly the resolved runtime; null when the
820
+ // runtime already is the `pi` on PATH (or for a bundled install, whose
821
+ // setup PATH already starts with the version's node_modules/.bin).
822
+ function writeSetupPiLauncher(runtime) {
823
+ if (bundledEnv || runtime.kind === "path") return null;
824
+ let dir;
825
+ try {
826
+ dir = mkdtempSync(join(tmpdir(), "gentle-shell-pi-"));
827
+ } catch {
828
+ // No usable temp folder: setup runs as it did before, without this `pi`.
829
+ return null;
830
+ }
831
+ if (process.platform === "win32") {
832
+ // Batch expands % even inside quotes; double it so a path stays literal.
833
+ const quote = (value) => `"${value.replaceAll("%", "%%")}"`;
834
+ // A batch runtime needs `call`, or control never returns for `exit /b`.
835
+ const call = /\.(?:cmd|bat)$/i.test(runtime.command) ? "call " : "";
836
+ writeFileSync(join(dir, "pi.cmd"), `@echo off\r\n${call}${[runtime.command, ...runtime.args].map(quote).join(" ")} %*\r\nexit /b %errorlevel%\r\n`);
837
+ } else {
838
+ const quote = (value) => `'${value.replaceAll("'", "'\\''")}'`;
839
+ writeFileSync(join(dir, "pi"), `#!/bin/sh\nexec ${[runtime.command, ...runtime.args].map(quote).join(" ")} "$@"\n`, { mode: 0o700 });
840
+ }
841
+ return dir;
842
+ }
843
+
562
844
  // The shared Pi persona file gentle-ai writes on every install, regardless
563
845
  // of the target home: its own PiPersonaConfigPath always resolves against
564
846
  // the OS home, never PI_CODING_AGENT_DIR (gentle-ai internal/components/persona/inject.go),
@@ -570,6 +852,29 @@ function sharedPersonaPath() {
570
852
  return join(homedir(), ".pi", "gentle-ai", "persona.json");
571
853
  }
572
854
 
855
+ // The persona files a setup run may rewrite. Bundled (S9): the prefix's own,
856
+ // which a Gentle AI with GENTLE_AI_PI_CONFIG_DIR (gentle-ai #5462) writes, and
857
+ // still the user's, which a pinned Gentle AI without it writes; restoring an
858
+ // unchanged file does nothing.
859
+ function setupPersonaPaths() {
860
+ if (!bundledInstall) return [sharedPersonaPath()];
861
+ return [join(bundledGentleAiPaths(bundledInstall.root, process.platform).piConfig, "persona.json"), sharedPersonaPath()];
862
+ }
863
+
864
+ // Bundled setup (S9): the pinned Engram GENTLE_AI_ENGRAM_BIN names, published
865
+ // once in runtime/ (verified, never brew or the user's PATH). A failure stops
866
+ // setup rather than let Gentle AI acquire another Engram.
867
+ async function ensureBundledEngram() {
868
+ const { ensureEngram, prefixLayout } = await import("../scripts/bundled-install.mjs");
869
+ const layout = prefixLayout({ platform: process.platform, env: process.env, home: homedir(), root: bundledInstall.root });
870
+ try {
871
+ await ensureEngram({ layout, platform: process.platform, arch: process.arch, env: process.env });
872
+ return { ok: true };
873
+ } catch (error) {
874
+ return { ok: false, exitCode: 1, message: `gentle-shell: could not install the pinned Engram in ${layout.engramDir} (${error.message})` };
875
+ }
876
+ }
877
+
573
878
  // Records `path`'s current state before a child process that might rewrite
574
879
  // it runs: whether it exists, and if so its exact bytes and mode. Returns
575
880
  // `{ path, existed: false }` for a missing file so restoreFile below knows
@@ -818,10 +1123,24 @@ async function runSetupFlow(home, runtime, { dryRun, stdio, timeoutMs }) {
818
1123
 
819
1124
  process.stderr.write(`gentle-shell: provisioning ${home.dir} with the gentle-ai companion packages\n`);
820
1125
 
1126
+ if (bundledInstall && !dryRun) {
1127
+ const engram = await ensureBundledEngram();
1128
+ if (!engram.ok) return engram;
1129
+ }
1130
+ // The memory decision never skips setup: it only selects the Engram
1131
+ // environment (core or guard, policy, ENGRAM_URL) setup's children and the
1132
+ // launched Pi get. Gentle AI's `install --agent pi` and `pi-engram init` open
1133
+ // no store. Reclassified now that the verified core may just be published.
1134
+ if (bundledInstall) {
1135
+ bundledMemory = await classifyBundledMemory();
1136
+ bundledEnv = bundledProcessEnv();
1137
+ if (bundledMemory.notice) process.stderr.write(`gentle-shell: ${bundledMemory.notice}\n`);
1138
+ }
1139
+
821
1140
  const setupArgs = ["install", "--agent", "pi", "--scope", "global", ...(dryRun ? ["--dry-run"] : [])];
822
- const env = buildSetupEnv(home, runtime);
823
- const personaPath = sharedPersonaPath();
824
- const personaSnapshot = safely("snapshot your Pi persona file", personaPath, undefined, () => snapshotFile(personaPath));
1141
+ const piLauncherDir = writeSetupPiLauncher(runtime);
1142
+ const env = buildSetupEnv(home, runtime, piLauncherDir);
1143
+ const personaSnapshots = setupPersonaPaths().map((path) => [path, safely("snapshot your Pi persona file", path, undefined, () => snapshotFile(path))]);
825
1144
  const statePath = sharedGentleAiStatePath();
826
1145
  const originalStateText = safely("read your Gentle AI state file", statePath, undefined, () => readParsableJsonText(statePath));
827
1146
  // Theme restore (never for --link — that home is the user's own
@@ -838,8 +1157,11 @@ async function runSetupFlow(home, runtime, { dryRun, stdio, timeoutMs }) {
838
1157
  try {
839
1158
  installResult = await spawnAndWait(binaryPath, setupArgs, env, stdio, timeoutMs);
840
1159
  } finally {
841
- if (personaSnapshot !== undefined && safely("restore your Pi persona file", personaPath, false, () => restoreFile(personaSnapshot))) {
842
- process.stderr.write(`gentle-shell: kept your Pi persona unchanged (gentle-ai rewrote ${personaPath}; tracked upstream)\n`);
1160
+ if (piLauncherDir) rmSync(piLauncherDir, { recursive: true, force: true });
1161
+ for (const [personaPath, personaSnapshot] of personaSnapshots) {
1162
+ if (personaSnapshot !== undefined && safely("restore your Pi persona file", personaPath, false, () => restoreFile(personaSnapshot))) {
1163
+ process.stderr.write(`gentle-shell: kept your Pi persona unchanged (gentle-ai rewrote ${personaPath}; tracked upstream)\n`);
1164
+ }
843
1165
  }
844
1166
  if (safely("restore your Gentle AI managed-asset record", statePath, false, () => restoreManagedAssetDigestField(statePath, originalStateText))) {
845
1167
  process.stderr.write(
@@ -861,7 +1183,63 @@ async function runSetupFlow(home, runtime, { dryRun, stdio, timeoutMs }) {
861
1183
  }
862
1184
  if (!installResult.ok) return installResult;
863
1185
 
864
- return runPostInstallCleanup(home, runtime, dryRun, stdio, timeoutMs);
1186
+ const cleanupResult = await runPostInstallCleanup(home, runtime, dryRun, stdio, timeoutMs);
1187
+ if (cleanupResult.ok && !dryRun) {
1188
+ warnDisabledMCP(home);
1189
+ // The guard and policy protect memory, so the companion check only warns.
1190
+ const companion = bundledInstall ? await ensureEngramCompanion(home, runtime, stdio, timeoutMs) : { ok: true };
1191
+ if (!companion.ok) process.stderr.write(`${companion.message}\n`);
1192
+ }
1193
+ return cleanupResult;
1194
+ }
1195
+
1196
+ function mcpAdapterActivity(packages) {
1197
+ if (typeof packages === "string") return packages === "npm:pi-mcp-adapter" || packages.startsWith("npm:pi-mcp-adapter@") ? "active" : "inactive";
1198
+ if (Array.isArray(packages)) {
1199
+ const activities = packages.map(mcpAdapterActivity);
1200
+ return activities.includes("active") ? "active" : activities.includes("unknown") ? "unknown" : "inactive";
1201
+ }
1202
+ if (packages === null || typeof packages !== "object") return "inactive";
1203
+ if (typeof packages.source !== "string") return mcpAdapterActivity(Object.keys(packages));
1204
+ if (mcpAdapterActivity(packages.source) === "inactive") return "inactive";
1205
+ const patterns = packages.extensions;
1206
+ if (patterns === undefined) return packages.autoload === false ? "inactive" : "active";
1207
+ if (!Array.isArray(patterns)) return "unknown";
1208
+ if (patterns.length === 0) return "inactive";
1209
+ if (packages.autoload === false) {
1210
+ return patterns.some(pattern => typeof pattern === "string" && !pattern.startsWith("!") && !pattern.startsWith("-")) ? "unknown" : "inactive";
1211
+ }
1212
+ // Ordinary exclusions run after includes, but exact +path entries can
1213
+ // restore resources. Other filters need actual package files to resolve.
1214
+ if (patterns.includes("!**") && !patterns.some(pattern => typeof pattern === "string" && pattern.startsWith("+"))) return "inactive";
1215
+ return "unknown";
1216
+ }
1217
+
1218
+ // Onboarding cannot prove who disabled MCP: diagnose, never re-enable servers.
1219
+ // This also covers automatic provisioning because both routes share setup.
1220
+ function warnDisabledMCP(home) {
1221
+ const settingsPath = join(home.dir, "settings.json");
1222
+ const mcpPath = join(home.dir, "mcp.json");
1223
+ let inspecting = settingsPath;
1224
+ try {
1225
+ const settingsText = readJsonIfExists(settingsPath);
1226
+ if (settingsText === undefined) return;
1227
+ const settings = JSON.parse(settingsText);
1228
+ if (!Array.isArray(settings?.extensions) || !settings.extensions.includes("-builtin:mcp")) return;
1229
+ const activity = mcpAdapterActivity(settings.packages);
1230
+ if (activity === "active") return;
1231
+ inspecting = mcpPath;
1232
+ const mcpText = readJsonIfExists(mcpPath);
1233
+ if (mcpText === undefined) return;
1234
+ const servers = JSON.parse(mcpText)?.mcpServers;
1235
+ if (servers === null || typeof servers !== "object" || Array.isArray(servers) || Object.keys(servers).length === 0) return;
1236
+ const diagnosis = activity === "unknown"
1237
+ ? `pi-mcp-adapter activity cannot be confirmed from its extension filters, so servers in ${mcpPath} may not load`
1238
+ : `pi-mcp-adapter is absent or inactive, so servers in ${mcpPath} will not load`;
1239
+ process.stderr.write(`gentle-shell: WARNING: Pi built-in MCP is disabled in ${settingsPath}; ${diagnosis}. If you want these servers enabled, remove -builtin:mcp from extensions in ${settingsPath}, then restart Pi. If MCP is intentionally disabled, keep the setting.\n`);
1240
+ } catch (error) {
1241
+ process.stderr.write(`gentle-shell: WARNING: Pi MCP configuration could not be inspected in ${inspecting}: ${error.message}; inspect or repair it manually.\n`);
1242
+ }
865
1243
  }
866
1244
 
867
1245
  // The stderr line printed once `source` is actually removed from `home`.
@@ -941,7 +1319,11 @@ async function removePostInstallSources(sources, index, home, runtime, stdio, ti
941
1319
  // --dry-run, runs the shared flow with the child's stdio inherited (today's
942
1320
  // behavior, unchanged), then exits with its result — this is the one place
943
1321
  // that keeps the pre-S7 exit semantics `handleSetupCommand` always had.
944
- async function handleSetupCommand(commandArgs, home, runtime) {
1322
+ // A successful real run records the home exactly as auto-provisioning does
1323
+ // (#2106: both installers finish with this command, and the first launch
1324
+ // must not install the packages again), but only for a home auto-provisioning
1325
+ // itself would own: never pi's own default home or an unrelated directory.
1326
+ async function handleSetupCommand(commandArgs, home, runtime, { homeHadContentBeforeBootstrap }) {
945
1327
  let dryRun = false;
946
1328
  for (const arg of commandArgs) {
947
1329
  if (arg === "--dry-run") {
@@ -953,6 +1335,19 @@ async function handleSetupCommand(commandArgs, home, runtime) {
953
1335
 
954
1336
  const result = await runSetupFlow(home, runtime, { dryRun, stdio: "inherit" });
955
1337
  if (!result.ok && result.message !== undefined) process.stderr.write(`${result.message}\n`);
1338
+ if (result.ok && !dryRun && (home.mode === "isolated" || home.mode === "path")) {
1339
+ const configPath = resolveConfigPath();
1340
+ const homeKey = safeRealpath(home.dir);
1341
+ const config = readRawConfig(configPath);
1342
+ if (!homeIsForeign(home, provisionedEntry(config, homeKey), homeHadContentBeforeBootstrap)) {
1343
+ try {
1344
+ writeRawConfig(configPath, recordProvisioned(config, homeKey, resolveSetupGentleAiPin(), ownPackageVersion(), new Date().toISOString()));
1345
+ } catch (error) {
1346
+ // Setup itself succeeded; the next launch only sets the home up again.
1347
+ process.stderr.write(`gentle-shell: could not record the setup in ${configPath} (${error.message})\n`);
1348
+ }
1349
+ }
1350
+ }
956
1351
  process.exit(result.exitCode);
957
1352
  }
958
1353
 
@@ -1179,29 +1574,268 @@ async function maybeAutoProvisionHome(home, runtime, { homeHadContentBeforeBoots
1179
1574
  }
1180
1575
  }
1181
1576
 
1577
+ // The adapters `upgrade` and `self-uninstall` run npm and pnpm with: { fs, which, run, invocation? }.
1578
+ async function packageManagerAdapters() {
1579
+ const { hostAdapters } = await import("../scripts/installer-probes.mjs");
1580
+ const { upgradeInvocation } = await import("../scripts/installer-runner.mjs");
1581
+ const { run, fs: probeFs } = hostAdapters();
1582
+ // Windows: npm and pnpm are .cmd shims; run what they run, never through cmd.exe.
1583
+ const invocation = upgradeInvocation({ platform: process.platform, env: process.env, run, fs: probeFs });
1584
+ return {
1585
+ fs: await import("node:fs/promises"),
1586
+ which: async (name) => findOnPath(name) ?? null,
1587
+ ...(invocation ? { invocation } : {}),
1588
+ run: (command, argv, options = {}) => run(command, argv, { env: options.env ?? process.env, cwd: options.cwd, deadlineMs: options.deadlineMs ?? 20 * 60_000 }),
1589
+ };
1590
+ }
1591
+
1592
+ // `gentle-shell upgrade`: runs before any Pi runtime check, since it may replace this package.
1593
+ // On release, a global pnpm or npm install moves to the bundled one when the
1594
+ // latest release publishes its assets (S4), with one question about the old package.
1595
+ async function handleUpgradeCommand(commandArgs) {
1596
+ const { MainChannelError, runUpgrade } = await import("../scripts/main-channel.mjs");
1597
+ const { fetchDistribution, migrateGlobalShell, windowsPathRegistry } = await import("../scripts/bundled-wizard.mjs");
1598
+ const { askLine } = await import("../runtime/gentle-shell-uninstall.mjs");
1599
+ const managers = await packageManagerAdapters();
1600
+ const out = (line) => process.stdout.write(`${line}\n`);
1601
+ const migrate = ({ latest, owner }) => migrateGlobalShell({
1602
+ latest,
1603
+ currentVersion: ownPackageVersion(),
1604
+ platform: process.platform,
1605
+ arch: process.arch,
1606
+ env: process.env,
1607
+ home: homedir(),
1608
+ interactive: process.stdin.isTTY === true && process.stdout.isTTY === true,
1609
+ // End of input or Ctrl+C answers No.
1610
+ ask: (question) => askLine(question, { input: process.stdin, output: process.stdout }).catch(() => ""),
1611
+ out,
1612
+ adapters: { owner, distribution: (version) => fetchDistribution({ version }), run: managers.run,
1613
+ ...(process.platform === "win32" ? { registry: windowsPathRegistry(process.env) } : {}) },
1614
+ });
1615
+ try {
1616
+ process.exitCode = await runUpgrade({
1617
+ args: commandArgs,
1618
+ ctx: { env: process.env, home: homedir() },
1619
+ platform: process.platform,
1620
+ packageRoot,
1621
+ currentVersion: ownPackageVersion(),
1622
+ adapters: { fetch: globalThis.fetch, ...managers, migrate },
1623
+ out,
1624
+ });
1625
+ } catch (error) {
1626
+ if (error instanceof MainChannelError) fail(`gentle-shell upgrade: ${error.message}`, error.code === "upgrade-usage" ? 2 : 1);
1627
+ throw error;
1628
+ }
1629
+ }
1630
+
1631
+ // `gentle-shell upgrade` in a bundled install: the latest release installed
1632
+ // beside the active version, verified, then `current` switched (two versions
1633
+ // kept), or `--rollback` to the kept one. Returns the exit code (a failure is
1634
+ // printed, not thrown, so `update --all` can go on); a failure leaves `current`
1635
+ // as it was.
1636
+ async function handleBundledUpgrade(bundled, commandArgs) {
1637
+ const parsed = parseBundledUpgradeArgs(commandArgs);
1638
+ if ("error" in parsed) fail(`gentle-shell upgrade: ${parsed.error}`, parsed.usage ? 2 : 1);
1639
+ const { prefixLayout, rollbackBundled, upgradeBundled } = await import("../scripts/bundled-install.mjs");
1640
+ const layout = prefixLayout({ platform: process.platform, env: process.env, home: homedir(), root: bundled.root });
1641
+ const out = (line) => process.stdout.write(`${line}\n`);
1642
+ try {
1643
+ if (parsed.action === "rollback") {
1644
+ const { from, to } = await rollbackBundled({ layout, home: homedir(), platform: process.platform, arch: process.arch, env: process.env });
1645
+ out(`Switched Gentle Shell from ${from} to ${to}.`);
1646
+ return 0;
1647
+ }
1648
+ const { fetchDistribution } = await import("../scripts/bundled-wizard.mjs");
1649
+ const { latestRelease } = await import("../scripts/main-channel.mjs");
1650
+ await upgradeBundled({
1651
+ layout,
1652
+ home: homedir(),
1653
+ platform: process.platform,
1654
+ arch: process.arch,
1655
+ env: process.env,
1656
+ adapters: { latest: () => latestRelease(globalThis.fetch), distribution: (version) => fetchDistribution({ version }) },
1657
+ out,
1658
+ });
1659
+ return 0;
1660
+ } catch (error) {
1661
+ process.stderr.write(`gentle-shell upgrade: ${error.message}${error.stderr ? `\n${error.stderr}` : ""}\n`);
1662
+ return 1;
1663
+ }
1664
+ }
1665
+
1666
+ // The bundled npm as the `npmCommand` of a home Gentle Shell owns (the isolated
1667
+ // home, or a --home it created or provisioned; never --link or a foreign home),
1668
+ // so Pi installs packages with our npm while its tools keep the user's PATH.
1669
+ // Only that key is written, and only when it differs.
1670
+ async function ensureBundledNpmCommand(bundled, home, homeHadContentBeforeBootstrap) {
1671
+ if (home.mode === "link") return;
1672
+ const previous = provisionedEntry(readRawConfig(resolveConfigPath()), safeRealpath(home.dir));
1673
+ if (homeIsForeign(home, previous, homeHadContentBeforeBootstrap) || !isDirectory(home.dir)) return;
1674
+ const { bundledNpmCommand, ensureNpmShim, prefixLayout } = await import("../scripts/bundled-install.mjs");
1675
+ const layout = prefixLayout({ platform: process.platform, env: process.env, home: homedir(), root: bundled.root });
1676
+ // The entry npmCommand names, also for a runtime installed before it existed.
1677
+ ensureNpmShim(layout);
1678
+ const settingsPath = join(home.dir, "settings.json");
1679
+ const edit = withBundledNpmCommand(readJsonIfExists(settingsPath), bundledNpmCommand(layout));
1680
+ if (edit.status === "malformed") {
1681
+ process.stderr.write(`gentle-shell: ${settingsPath} is not a JSON object, so Pi's package installs use the npm on your PATH\n`);
1682
+ } else if (edit.status === "write") {
1683
+ writeFileAtomically(settingsPath, edit.text);
1684
+ }
1685
+ }
1686
+
1687
+ // `gentle-shell self-uninstall` in a bundled install (S6, S7): the prefix's own
1688
+ // folders and exactly the PATH entry its receipt records; on Windows the running
1689
+ // node.exe and launcher are removed by a detached process after this one exits.
1690
+ async function handleBundledSelfUninstall(bundled, commandArgs) {
1691
+ const { askLine, runBundledSelfUninstall } = await import("../runtime/gentle-shell-uninstall.mjs");
1692
+ const { describePathReceipt, prefixLayout, readPathReceipt, removePathEntry, scheduleWindowsRemoval } = await import("../scripts/bundled-install.mjs");
1693
+ const windows = process.platform === "win32";
1694
+ const layout = prefixLayout({ platform: process.platform, env: process.env, home: homedir(), root: bundled.root });
1695
+ // Trusted only for the link or startup file the PATH plan can choose for this user.
1696
+ const receipt = readPathReceipt(layout, { env: process.env, home: homedir() });
1697
+ let pathEntry = receipt;
1698
+ if (receipt.status === "recorded") {
1699
+ const registry = windows ? (await import("../scripts/bundled-wizard.mjs")).windowsPathRegistry(process.env) : undefined;
1700
+ pathEntry = { status: "recorded", description: describePathReceipt(receipt.receipt), remove: () => removePathEntry(receipt.receipt, { registry }) };
1701
+ }
1702
+ return runBundledSelfUninstall({
1703
+ args: commandArgs,
1704
+ env: process.env,
1705
+ homedir: homedir(),
1706
+ platform: process.platform,
1707
+ interactive: process.stdin.isTTY === true,
1708
+ root: bundled.root,
1709
+ pathEntry,
1710
+ process: { pid: process.pid, execPath: process.execPath },
1711
+ ...(windows ? { deferRemoval: (request) => scheduleWindowsRemoval({ ...request, env: process.env }) } : {}),
1712
+ ask: (question) => askLine(question, { input: process.stdin, output: process.stdout }),
1713
+ out: (line) => process.stdout.write(`${line}\n`),
1714
+ err: (line) => process.stderr.write(`${line}\n`),
1715
+ });
1716
+ }
1717
+
1718
+ // `gentle-shell self-uninstall`: like upgrade, runs before any Pi runtime check,
1719
+ // so it also works when pi is missing.
1720
+ async function handleSelfUninstallCommand(commandArgs) {
1721
+ const { askLine, runSelfUninstall } = await import("../runtime/gentle-shell-uninstall.mjs");
1722
+ const { ownerManager } = await import("../scripts/main-channel.mjs");
1723
+ const managers = await packageManagerAdapters();
1724
+ const ctx = { env: process.env, home: homedir() };
1725
+ process.exitCode = await runSelfUninstall({
1726
+ args: commandArgs,
1727
+ env: process.env,
1728
+ homedir: homedir(),
1729
+ platform: process.platform,
1730
+ interactive: process.stdin.isTTY === true,
1731
+ resolveOwner: () => ownerManager({ ctx, platform: process.platform, packageRoot, adapters: managers, command: "uninstall" }),
1732
+ run: (command, argv) => managers.run(command, argv),
1733
+ ask: (question) => askLine(question, { input: process.stdin, output: process.stdout }),
1734
+ out: (line) => process.stdout.write(`${line}\n`),
1735
+ err: (line) => process.stderr.write(`${line}\n`),
1736
+ });
1737
+ }
1738
+
1739
+ // Before Pi exists: only the bundled interactive opening may offer an update.
1740
+ async function startupUpdate(bundled, args, startupEnv) {
1741
+ if (!shouldCheckStartupUpdate({ args, env: startupEnv, bundled: Boolean(bundled),
1742
+ inputTTY: process.stdin.isTTY === true, outputTTY: process.stdout.isTTY === true })) return undefined;
1743
+ const configPath = resolveConfigPath();
1744
+ let skipped;
1745
+ try { skipped = readRawConfig(configPath).skippedStartupRelease; } catch { /* A notice must not prevent opening. */ }
1746
+ const { latestRelease } = await import("../scripts/main-channel.mjs");
1747
+ const offer = await findStartupUpdate({ current: ownPackageVersion(), skipped,
1748
+ lookup: (signal) => latestRelease((url, options) => fetch(url, { ...options, signal })) });
1749
+ if (!offer) return undefined;
1750
+ let choice;
1751
+ try { choice = await promptStartupUpdate(offer, { color: process.stdout.hasColors?.() === true }); }
1752
+ catch { return undefined; }
1753
+ return applyStartupChoice(choice, {
1754
+ omit: () => omitStartupVersion(() => readJsonIfExists(configPath), (text) => {
1755
+ mkdirSync(dirname(configPath), { recursive: true, mode: 0o700 });
1756
+ writeFileAtomically(configPath, text, { mode: 0o600 });
1757
+ }, offer.latest),
1758
+ warn: (message) => process.stderr.write(`gentle-shell: ${message}\n`),
1759
+ upgrade: () => handleBundledUpgrade(bundled, []),
1760
+ relaunch: async () => {
1761
+ const next = await resolveStartupRelaunch({ root: bundled.root, home: homedir(), platform: process.platform,
1762
+ env: process.env, argv: process.argv.slice(2) });
1763
+ return new Promise((resolve, reject) => {
1764
+ const child = spawn(next.command, next.args, { stdio: "inherit", env: next.env, shell: false });
1765
+ const forwards = ["SIGINT", "SIGTERM", "SIGHUP"].map((signal) => {
1766
+ const handler = () => child.kill(signal);
1767
+ process.on(signal, handler);
1768
+ return [signal, handler];
1769
+ });
1770
+ const cleanup = () => { for (const [signal, handler] of forwards) process.off(signal, handler); };
1771
+ child.once("error", (error) => { cleanup(); reject(error); });
1772
+ child.once("exit", (code, signal) => { cleanup(); resolve(signal ? signalExitCode(signal) : (code ?? 1)); });
1773
+ });
1774
+ },
1775
+ });
1776
+ }
1777
+
1182
1778
  async function main() {
1779
+ // One launch only: never leak the update handoff opt-out into Pi or its tools.
1780
+ const startupEnv = { ...process.env };
1781
+ delete process.env.GENTLE_SHELL_SKIP_STARTUP_UPDATE;
1183
1782
  const args = parseLauncherArgs(process.argv.slice(2));
1184
1783
  if (args.error !== undefined) fail(`${args.error}\nRun 'gentle-shell --help' for usage.`, 2);
1185
1784
  if (args.help) {
1186
- process.stdout.write(`${helpText()}\n`);
1785
+ process.stdout.write(`${helpText()}\nBundled Engram safety: memory opens only a fresh, verified or Shell-provisioned store; otherwise ENGRAM_BIN\nis Shell's guard and memory stays off. Back up data, update Engram, restart its server and Shell, then recheck.\nAn explicit ENGRAM_BIN or ENGRAM_URL is kept and bypasses the guard.\n`);
1187
1786
  process.exit(0);
1188
1787
  }
1189
1788
  if (args.command === "home") {
1190
1789
  handleHomeCommand(args.commandArgs);
1191
1790
  return;
1192
1791
  }
1792
+ // The verification input applies to `--version` only (scripts/bundled-install.mjs upgradeBundled).
1793
+ const verifying = args.version && args.command === undefined ? process.env[BUNDLED_VERIFY_ENV] : undefined;
1794
+ const bundled = resolveBundledInstall({ packageRoot, platform: process.platform, fs: bundledFs, verifying });
1795
+ if (args.command === "upgrade") {
1796
+ if (bundled) process.exitCode = await handleBundledUpgrade(bundled, args.commandArgs);
1797
+ else await handleUpgradeCommand(args.commandArgs);
1798
+ return;
1799
+ }
1800
+ if (args.command === "self-uninstall") {
1801
+ if (bundled) process.exitCode = await handleBundledSelfUninstall(bundled, args.commandArgs);
1802
+ else await handleSelfUninstallCommand(args.commandArgs);
1803
+ return;
1804
+ }
1805
+
1806
+ const startupCode = await startupUpdate(bundled, args, startupEnv);
1807
+ if (startupCode !== undefined) {
1808
+ process.exitCode = startupCode;
1809
+ return;
1810
+ }
1193
1811
 
1194
1812
  const config = loadConfig();
1195
1813
  let home = resolveHome({ args, env: process.env, homedir: homedir(), config });
1196
1814
  if (home.mode === "path") home = { ...home, dir: resolvePath(home.dir) };
1197
1815
 
1198
- const runtime = resolvePiRuntime({
1199
- env: process.env,
1200
- resolveBundledCli,
1201
- findOnPath,
1202
- nodeExecPath: process.execPath,
1203
- });
1204
- if (runtime === undefined) fail(missingPiMessage(), 1);
1816
+ // Version and help runs (Shell's own or forwarded to Pi) never probe Engram,
1817
+ // ensure the companion, print memory notices or touch provenance.
1818
+ const engramQuiet = args.version || piVersionOrHelpRequested(args.passthrough);
1819
+ let runtime;
1820
+ if (bundled) {
1821
+ // S8: only the version's own Pi, never GENTLE_SHELL_PI, an adjacent fallback or PATH.
1822
+ const pi = bundledPi(bundled, { platform: process.platform, fs: bundledFs });
1823
+ if (!pi.ok) fail(pi.message, 1);
1824
+ runtime = { kind: "bundled", command: process.execPath, args: [pi.cli] };
1825
+ bundledInstall = bundled;
1826
+ bundledCli = pi.cli;
1827
+ bundledMemory = engramQuiet ? { policy: "disabled", notice: ENGRAM_RECOVERY_NOTICE } : await classifyBundledMemory();
1828
+ bundledEnv = bundledProcessEnv();
1829
+ bundledSetupDirs = [dirname(process.execPath), join(bundled.versionDir, "node_modules", ".bin")];
1830
+ } else {
1831
+ runtime = resolvePiRuntime({
1832
+ env: process.env,
1833
+ resolveBundledCli,
1834
+ findOnPath,
1835
+ nodeExecPath: process.execPath,
1836
+ });
1837
+ if (runtime === undefined) fail(missingPiMessage(), 1);
1838
+ }
1205
1839
 
1206
1840
  const versionProbePlan = planSpawn({ command: runtime.command, args: [...runtime.args, "--version"], platform: process.platform });
1207
1841
  const versionProbe = spawnSync(versionProbePlan.command, versionProbePlan.args, {
@@ -1209,6 +1843,7 @@ async function main() {
1209
1843
  timeout: 15000,
1210
1844
  encoding: "utf8",
1211
1845
  shell: versionProbePlan.shell,
1846
+ env: bundledEnv ?? process.env,
1212
1847
  });
1213
1848
  if (versionProbe.error) fail(`Could not run the pi runtime at "${runtime.command}": ${versionProbe.error.message}`, 1);
1214
1849
  const versionCheck = checkPiVersion(versionProbe.stdout ?? "");
@@ -1219,6 +1854,23 @@ async function main() {
1219
1854
  process.exit(0);
1220
1855
  }
1221
1856
 
1857
+ // S5: in a bundled install Pi updates only with Gentle Shell, so Pi's own
1858
+ // update becomes `gentle-shell upgrade` (run when interactive, named
1859
+ // otherwise); package updates still reach Pi.
1860
+ let passthrough = args.passthrough;
1861
+ if (bundled && args.piSubcommand === "update") {
1862
+ const plan = bundledUpdatePlan(args.passthrough);
1863
+ if (plan.self) {
1864
+ const interactive = process.stdin.isTTY === true && process.stdout.isTTY === true;
1865
+ for (const line of bundledUpdateNotice({ piVersion: versionCheck.version, interactive })) process.stdout.write(`${line}\n`);
1866
+ const code = interactive ? await handleBundledUpgrade(bundled, []) : undefined;
1867
+ const next = bundledUpdateNext(plan, code);
1868
+ if ("exitCode" in next) process.exit(next.exitCode);
1869
+ if (next.note) process.stderr.write(`${next.note}\n`);
1870
+ }
1871
+ passthrough = plan.piArgs;
1872
+ }
1873
+
1222
1874
  // Home-ownership signal for auto-provisioning (S9, homeIsForeign): must be
1223
1875
  // read before the isolated-home bootstrap below creates and seeds a
1224
1876
  // brand-new --home directory with its own settings.json — after that
@@ -1236,14 +1888,24 @@ async function main() {
1236
1888
  // Isolated-home bootstrap: only on a home gentle-shell has not seen before
1237
1889
  // (link never bootstraps — it reuses the user's own pi agent home as-is).
1238
1890
  if ((home.mode === "isolated" || home.mode === "path") && !existsSync(home.dir)) {
1239
- mkdirSync(home.dir, { recursive: true });
1891
+ // 0700: under umask 002 a default mode would leave ~/.gentle-shell group-writable,
1892
+ // which the bundled install's prefix claim must tighten or refuse.
1893
+ mkdirSync(home.dir, { recursive: true, mode: 0o700 });
1240
1894
  await installIsolatedTuiModeSetting(home.dir);
1241
1895
  writeHomeOwnershipMarker(home); // R3-001: lets a failed first auto-provision attempt still be retried later
1242
1896
  process.stderr.write(`gentle-shell: using a separate home at ${home.dir}. Run 'gentle-shell --link' to reuse your pi sign-ins and chats.\n`);
1243
1897
  }
1244
1898
 
1899
+ if (bundled) {
1900
+ try {
1901
+ await ensureBundledNpmCommand(bundled, home, homeHadContentBeforeBootstrap);
1902
+ } catch (error) {
1903
+ process.stderr.write(`gentle-shell: could not give Pi the bundled npm in ${join(home.dir, "settings.json")} (${error.message}); continuing\n`);
1904
+ }
1905
+ }
1906
+
1245
1907
  if (args.command === "setup") {
1246
- await handleSetupCommand(args.commandArgs, home, runtime);
1908
+ await handleSetupCommand(args.commandArgs, home, runtime, { homeHadContentBeforeBootstrap });
1247
1909
  return;
1248
1910
  }
1249
1911
 
@@ -1371,6 +2033,23 @@ async function main() {
1371
2033
  }
1372
2034
  }
1373
2035
 
2036
+ if (bundled) {
2037
+ // Recheck after setup and before provider loading: never carry a stale
2038
+ // fresh-store decision across an installer that could create data.
2039
+ if (args.piSubcommand === undefined && !engramQuiet) {
2040
+ bundledMemory = await classifyBundledMemory();
2041
+ bundledEnv = bundledProcessEnv();
2042
+ // The guard protects memory whatever provider loads, so a companion that
2043
+ // could not be verified is reported, never a reason to stop Shell.
2044
+ const companion = await ensureEngramCompanion(home, runtime, ["ignore", 2, 2], 10000);
2045
+ if (!companion.ok) process.stderr.write(`${companion.message}\n`);
2046
+ // A fresh store's identity and provenance exist before Pi starts, or
2047
+ // memory stays off: Pi never runs with allow without a written record.
2048
+ if (bundledMemory.fresh && !provisionEngramStore()) bundledMemory = { policy: "disabled", notice: ENGRAM_PROVISION_FAILED_NOTICE };
2049
+ if (bundledMemory.notice) process.stderr.write(`gentle-shell: ${bundledMemory.notice}\n`);
2050
+ }
2051
+ bundledEnv = bundledProcessEnv();
2052
+ }
1374
2053
  const invocation = buildPiInvocation({
1375
2054
  runtime,
1376
2055
  home,
@@ -1379,10 +2058,12 @@ async function main() {
1379
2058
  takeOver,
1380
2059
  otherPackagePaths,
1381
2060
  looseExtensionEntries,
1382
- passthrough: [...managedHerdrExtensionArgs(home, args), ...args.passthrough],
2061
+ passthrough: [...managedHerdrExtensionArgs(home, args), ...passthrough],
1383
2062
  piSubcommand: args.piSubcommand,
1384
- baseEnv: process.env,
2063
+ baseEnv: bundledEnv ?? process.env,
1385
2064
  homedir: homedir(),
2065
+ // The spawn below sets no cwd, so pi runs in the launcher's own.
2066
+ cwd: process.cwd(),
1386
2067
  });
1387
2068
 
1388
2069
  // Only an interactive session ends with pi's exit resume hint, which