@chroxy/server 0.11.0 → 0.11.10

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 (317) hide show
  1. package/CONFIG.md +204 -18
  2. package/README.md +2 -2
  3. package/hooks/claude-native-route-check.mjs +17 -0
  4. package/hooks/permission-hook.sh +166 -11
  5. package/package.json +16 -12
  6. package/sidecar/Dockerfile +1 -1
  7. package/sidecar/PROTOCOL.md +9 -1
  8. package/sidecar/agent.js +102 -10
  9. package/sidecar/package-lock.json +3 -3
  10. package/src/acp-config.js +206 -0
  11. package/src/acp-session.js +954 -0
  12. package/src/activity-registry.js +110 -5
  13. package/src/agent-connections.js +293 -0
  14. package/src/agent-control/client.js +1616 -0
  15. package/src/agent-control/events.js +547 -0
  16. package/src/agent-control/local-connection.js +275 -0
  17. package/src/agent-control/mcp-server.js +545 -0
  18. package/src/anthropic-compatible-config.js +29 -4
  19. package/src/anthropic-compatible-session.js +15 -6
  20. package/src/anthropic-openai-shim.js +0 -1
  21. package/src/archive-continuation.js +164 -0
  22. package/src/assistant-response.js +24 -0
  23. package/src/auth-probes.js +120 -13
  24. package/src/background-shell-tracker.js +27 -0
  25. package/src/base-session.js +1203 -36
  26. package/src/bearer-recheck.js +49 -0
  27. package/src/billing-budget.js +19 -0
  28. package/src/billing-class.js +40 -7
  29. package/src/binary-provenance-trust.js +11 -5
  30. package/src/bound-client-policy.js +279 -0
  31. package/src/built-in-tools/bash-exec.js +46 -7
  32. package/src/built-in-tools/file-ops.js +69 -5
  33. package/src/built-in-tools/tool-transforms.js +1113 -13
  34. package/src/byok-compose-state.js +6 -9
  35. package/src/byok-credentials.js +2 -3
  36. package/src/byok-mcp-client.js +92 -7
  37. package/src/byok-mcp-config.js +628 -121
  38. package/src/byok-mcp-fleet.js +49 -7
  39. package/src/byok-mcp-oauth-store.js +18 -16
  40. package/src/byok-mcp-trust.js +3 -3
  41. package/src/byok-session.js +650 -175
  42. package/src/byok-tool-executor.js +2662 -26
  43. package/src/byok-tools.js +9 -2
  44. package/src/channels/chroxy-channel-server.js +2 -3
  45. package/src/checkpoint-manager.js +64 -46
  46. package/src/claude-channel-session.js +16 -10
  47. package/src/claude-model-catalog.js +131 -16
  48. package/src/claude-model-refresh.js +385 -0
  49. package/src/claude-model-request.js +103 -0
  50. package/src/claude-stream-parser.js +53 -3
  51. package/src/claude-tui/diagnostic-tail.js +83 -0
  52. package/src/claude-tui/form-driver.js +44 -7
  53. package/src/claude-tui/model-error.js +41 -0
  54. package/src/claude-tui/pty-driver.js +211 -88
  55. package/src/claude-tui/sanitize-question-payload.js +109 -0
  56. package/src/claude-tui/usage-limit.js +239 -0
  57. package/src/claude-tui-session.js +3513 -287
  58. package/src/claude-tui-tool-response.js +300 -0
  59. package/src/cli/agent-control-cmd.js +65 -0
  60. package/src/cli/config-cmd.js +4 -4
  61. package/src/cli/config-dir-cmd.js +234 -0
  62. package/src/cli/deploy-cmd.js +45 -10
  63. package/src/cli/doctor-cmd.js +7 -3
  64. package/src/cli/init-cmd.js +15 -16
  65. package/src/cli/providers-cmd.js +3 -3
  66. package/src/cli/schedule-cmd.js +68 -39
  67. package/src/cli/server-cmd.js +48 -1
  68. package/src/cli/service-cmd.js +33 -10
  69. package/src/cli/session-cmd.js +227 -74
  70. package/src/cli/shared.js +162 -11
  71. package/src/cli/tokens-cmd.js +4 -5
  72. package/src/cli/tunnel-cmd.js +255 -24
  73. package/src/cli/worktree-gc-cmd.js +2 -2
  74. package/src/cli-session.js +1066 -121
  75. package/src/cli.js +4 -0
  76. package/src/codex-app-server-client.js +36 -3
  77. package/src/codex-app-server-session.js +1454 -79
  78. package/src/codex-bundled-client.js +66 -0
  79. package/src/codex-model-catalog.js +548 -0
  80. package/src/codex-model-rejections.js +87 -0
  81. package/src/codex-native-resume.js +104 -0
  82. package/src/codex-protocol-capabilities.js +188 -0
  83. package/src/codex-session.js +450 -27
  84. package/src/config-dir-migration.js +444 -0
  85. package/src/config-dir.js +108 -0
  86. package/src/config.js +286 -26
  87. package/src/connection-authority.js +48 -0
  88. package/src/connection-info.js +5 -9
  89. package/src/connection-locality.js +60 -0
  90. package/src/container-liveness-monitor.js +189 -0
  91. package/src/continuation-metadata-worker.js +17 -0
  92. package/src/continuations.js +114 -0
  93. package/src/control-room/host-prune.js +5 -0
  94. package/src/control-room/integrations.js +7 -20
  95. package/src/control-room/skills-inventory.js +2 -2
  96. package/src/conversation-scanner.js +40 -18
  97. package/src/conversation-scope.js +59 -39
  98. package/src/conversation-search.js +24 -22
  99. package/src/credential-store.js +23 -19
  100. package/src/daemon-commit.js +92 -0
  101. package/src/daemon-idle-state.js +171 -0
  102. package/src/daemon-update-status.js +314 -0
  103. package/src/dashboard-build.js +70 -0
  104. package/src/declared-status.js +90 -0
  105. package/src/deepseek-credentials.js +2 -3
  106. package/src/dev-preview.js +169 -182
  107. package/src/device-preferences.js +2 -2
  108. package/src/diff-parser.js +87 -0
  109. package/src/discord-credentials.js +2 -3
  110. package/src/docker-byok-pool.js +3 -1
  111. package/src/docker-byok-session.js +678 -78
  112. package/src/docker-image-allowlist.js +23 -1
  113. package/src/docker-sdk-session.js +172 -3
  114. package/src/docker-session.js +284 -10
  115. package/src/doctor.js +767 -61
  116. package/src/edited-input.js +264 -0
  117. package/src/environment-manager.js +175 -36
  118. package/src/environments/authority.js +95 -0
  119. package/src/environments/backends/container-claude-spawn.js +94 -0
  120. package/src/environments/backends/docker.js +31 -10
  121. package/src/environments/backends/k8s.js +49 -24
  122. package/src/environments/destroy-with-sessions.js +142 -0
  123. package/src/event-ingest.js +118 -13
  124. package/src/event-normalizer.js +151 -233
  125. package/src/gemini-session.js +81 -5
  126. package/src/handler-utils.js +535 -68
  127. package/src/handlers/checkpoint-handlers.js +39 -4
  128. package/src/handlers/control-room-handlers.js +51 -11
  129. package/src/handlers/conversation-handlers.js +469 -56
  130. package/src/handlers/daemon-update-handlers.js +81 -0
  131. package/src/handlers/evaluator-handlers.js +14 -1
  132. package/src/handlers/feature-handlers.js +186 -69
  133. package/src/handlers/file-handlers.js +57 -13
  134. package/src/handlers/input-handlers.js +685 -48
  135. package/src/handlers/orchestration-handlers.js +3 -3
  136. package/src/handlers/scheduler-handlers.js +88 -14
  137. package/src/handlers/session-handlers.js +542 -20
  138. package/src/handlers/session-pr-status-handlers.js +283 -0
  139. package/src/handlers/session-pr-threads-handlers.js +247 -0
  140. package/src/handlers/settings-handlers.js +330 -42
  141. package/src/handlers/skills-handlers.js +13 -4
  142. package/src/handlers/summarize-handlers.js +32 -1
  143. package/src/handlers/survey-throttle.js +380 -0
  144. package/src/health-proof.js +40 -0
  145. package/src/history-page.js +104 -0
  146. package/src/http-routes.js +127 -8
  147. package/src/ide/search.js +24 -6
  148. package/src/ide/symbols.js +20 -6
  149. package/src/input-context.js +117 -0
  150. package/src/instruction-policy-compiler.js +229 -0
  151. package/src/instruction-policy.js +250 -0
  152. package/src/jsonl-reader.js +289 -82
  153. package/src/jsonl-subprocess-session.js +92 -8
  154. package/src/keychain.js +1 -1
  155. package/src/logger.js +63 -17
  156. package/src/mailbox-route.js +11 -22
  157. package/src/message-wire.js +386 -0
  158. package/src/model-discovery.js +43 -3
  159. package/src/models.js +1276 -174
  160. package/src/notification-prefs.js +12 -4
  161. package/src/notifications/discord-billing-sink.js +22 -5
  162. package/src/notifications/discord-ci-sink.js +243 -0
  163. package/src/notifications/discord-webhook-client.js +71 -0
  164. package/src/notifications/discord-webhook-sink.js +64 -23
  165. package/src/notifications/expo-push-sink.js +85 -6
  166. package/src/notifications/ready-body.js +16 -5
  167. package/src/ollama-session.js +34 -2
  168. package/src/orchestration/build-manager.js +9 -2
  169. package/src/orchestration/git-ops.js +41 -7
  170. package/src/orchestration/orchestration-manager.js +80 -33
  171. package/src/orchestration/permission-gate.js +13 -1
  172. package/src/orchestration/run-model.js +2 -1
  173. package/src/orchestration/turn-driver.js +44 -20
  174. package/src/orphan-reaper.js +413 -0
  175. package/src/outgoing-policy.js +183 -0
  176. package/src/pairing.js +92 -13
  177. package/src/path-hash-trust-ledger.js +642 -25
  178. package/src/permission-deadlines.js +82 -0
  179. package/src/permission-floor.js +1357 -42
  180. package/src/permission-hook.js +180 -69
  181. package/src/permission-manager.js +712 -83
  182. package/src/permission-mode-support.js +35 -0
  183. package/src/permission-resolver.js +122 -8
  184. package/src/permission-scope.js +43 -0
  185. package/src/platform.js +159 -43
  186. package/src/preview-ingress.js +54 -0
  187. package/src/preview-target.js +213 -0
  188. package/src/prompt-evaluator.js +34 -4
  189. package/src/provider-customization.js +180 -0
  190. package/src/providers.js +207 -4
  191. package/src/push.js +30 -4
  192. package/src/question-route-map.js +130 -0
  193. package/src/redaction.js +360 -8
  194. package/src/roster-broadcast.js +186 -0
  195. package/src/scheduled-task-store.js +318 -26
  196. package/src/scheduler.js +73 -3
  197. package/src/sdk-session.js +2060 -187
  198. package/src/server-cli/preview-shutdown.js +22 -0
  199. package/src/server-cli/push-notification-handler.js +11 -2
  200. package/src/server-cli/server-orchestrator.js +28 -5
  201. package/src/server-cli-child.js +33 -18
  202. package/src/server-cli.js +524 -38
  203. package/src/server-identity.js +15 -11
  204. package/src/service.js +396 -25
  205. package/src/session-archive-limits.js +15 -0
  206. package/src/session-archive.js +1029 -0
  207. package/src/session-busy-state.js +72 -0
  208. package/src/session-ci-watcher.js +926 -0
  209. package/src/session-context.js +7 -2
  210. package/src/session-manager.js +3491 -312
  211. package/src/session-message-history.js +722 -16
  212. package/src/session-pr-status.js +654 -0
  213. package/src/session-pr-threads.js +271 -0
  214. package/src/session-preset-trust.js +3 -3
  215. package/src/session-preset.js +10 -5
  216. package/src/session-state-persistence.js +94 -17
  217. package/src/session-timeout-manager.js +20 -0
  218. package/src/session-token-store.js +17 -8
  219. package/src/session-wake.js +240 -0
  220. package/src/shell-approval-info.js +2 -2
  221. package/src/shell-approval-store.js +3 -1
  222. package/src/skills-loader.js +7 -4
  223. package/src/skills-manager.js +2 -2
  224. package/src/skills-trust.js +95 -13
  225. package/src/skills-usage.js +2 -3
  226. package/src/snapshots-store.js +2 -3
  227. package/src/statusline.js +23 -11
  228. package/src/summarize-session.js +51 -4
  229. package/src/supervisor.js +22 -11
  230. package/src/sweep-stale-provider-dirs.js +95 -0
  231. package/src/thinking-level-wire.js +39 -0
  232. package/src/thinking-stream.js +179 -0
  233. package/src/tool-result.js +61 -0
  234. package/src/transcript-sources.js +109 -0
  235. package/src/transcript-tasks.js +372 -25
  236. package/src/trusted-file-read.js +302 -0
  237. package/src/tunnel/base.js +0 -8
  238. package/src/tunnel/cloudflare.js +34 -31
  239. package/src/tunnel/index.js +9 -0
  240. package/src/tunnel/preview.js +64 -0
  241. package/src/turn-outcome.js +137 -0
  242. package/src/usage-normalize.js +9 -5
  243. package/src/user-shell-session.js +59 -5
  244. package/src/utils/agent-sdk-version.js +107 -0
  245. package/src/utils/argv-safety.js +741 -0
  246. package/src/utils/auth-cause.js +60 -0
  247. package/src/utils/binary-package-manifest.js +597 -0
  248. package/src/utils/binary-version.js +224 -0
  249. package/src/utils/bounded-regex-worker.js +25 -0
  250. package/src/utils/bounded-regex.js +153 -0
  251. package/src/utils/claude-binary.js +43 -0
  252. package/src/utils/claude-config-dir.js +245 -0
  253. package/src/utils/claude-login-command.js +16 -0
  254. package/src/utils/claude-native-route.js +81 -0
  255. package/src/utils/componentwise-resolver.js +133 -37
  256. package/src/utils/deploy-control-files.js +87 -0
  257. package/src/utils/exec-failure-reason.js +21 -0
  258. package/src/utils/git-repo.js +42 -0
  259. package/src/utils/is-entry-point.js +137 -0
  260. package/src/utils/node-pty-probe.js +95 -0
  261. package/src/utils/node-pty-support.js +118 -0
  262. package/src/utils/opened-file-path.js +167 -0
  263. package/src/utils/path-containment.js +165 -0
  264. package/src/utils/permission-mode-sidecar.js +51 -0
  265. package/src/utils/preflight.js +360 -16
  266. package/src/utils/read-limiter.js +217 -0
  267. package/src/utils/regular-file.js +375 -0
  268. package/src/utils/safe-regex.js +113 -0
  269. package/src/utils/small-file.js +129 -0
  270. package/src/utils/spawn-env.js +189 -4
  271. package/src/utils/stale-session-dirs.js +182 -0
  272. package/src/utils/stat-identity.js +43 -0
  273. package/src/utils/verify-provenance.js +406 -13
  274. package/src/web-task-manager.js +256 -16
  275. package/src/worktree-gc.js +7 -2
  276. package/src/ws-auth.js +69 -21
  277. package/src/ws-broadcaster.js +14 -2
  278. package/src/ws-client-manager.js +16 -1
  279. package/src/ws-client-sender.js +7 -0
  280. package/src/ws-file-ops/browser.js +331 -58
  281. package/src/ws-file-ops/common.js +320 -14
  282. package/src/ws-file-ops/git.js +395 -42
  283. package/src/ws-file-ops/index.js +2 -0
  284. package/src/ws-file-ops/memory.js +137 -35
  285. package/src/ws-file-ops/open-nofollow.js +354 -0
  286. package/src/ws-file-ops/reader.js +571 -113
  287. package/src/ws-forwarding.js +329 -39
  288. package/src/ws-handler-context.js +70 -2
  289. package/src/ws-history.js +940 -172
  290. package/src/ws-message-handlers.js +17 -1
  291. package/src/ws-permissions.js +307 -37
  292. package/src/ws-schemas.js +5 -0
  293. package/src/ws-server.js +643 -96
  294. package/sidecar/node_modules/.package-lock.json +0 -29
  295. package/sidecar/node_modules/ws/LICENSE +0 -20
  296. package/sidecar/node_modules/ws/README.md +0 -548
  297. package/sidecar/node_modules/ws/browser.js +0 -8
  298. package/sidecar/node_modules/ws/index.js +0 -22
  299. package/sidecar/node_modules/ws/lib/buffer-util.js +0 -131
  300. package/sidecar/node_modules/ws/lib/constants.js +0 -19
  301. package/sidecar/node_modules/ws/lib/event-target.js +0 -292
  302. package/sidecar/node_modules/ws/lib/extension.js +0 -203
  303. package/sidecar/node_modules/ws/lib/limiter.js +0 -55
  304. package/sidecar/node_modules/ws/lib/permessage-deflate.js +0 -528
  305. package/sidecar/node_modules/ws/lib/receiver.js +0 -706
  306. package/sidecar/node_modules/ws/lib/sender.js +0 -602
  307. package/sidecar/node_modules/ws/lib/stream.js +0 -161
  308. package/sidecar/node_modules/ws/lib/subprotocol.js +0 -62
  309. package/sidecar/node_modules/ws/lib/validation.js +0 -152
  310. package/sidecar/node_modules/ws/lib/websocket-server.js +0 -554
  311. package/sidecar/node_modules/ws/lib/websocket.js +0 -1393
  312. package/sidecar/node_modules/ws/package.json +0 -70
  313. package/sidecar/node_modules/ws/wrapper.mjs +0 -21
  314. package/src/dashboard-next/dist/assets/index-C2ipeGlq.js +0 -111
  315. package/src/dashboard-next/dist/assets/index-DM-_dISG.css +0 -1
  316. package/src/dashboard-next/dist/index.html +0 -13
  317. package/src/dashboard-next/node_modules/.vite/vitest/da39a3ee5e6b4b0d3255bfef95601890afd80709/results.json +0 -1
@@ -1,9 +1,17 @@
1
- import { stat, open } from 'fs/promises'
1
+ import { stat } from 'fs/promises'
2
2
  import { constants as fsConstants } from 'fs'
3
- import { resolve, dirname, normalize, sep, extname } from 'path'
3
+ import { resolve, dirname, normalize, extname } from 'path'
4
4
  import { homedir } from 'os'
5
- import { realpathOfDeepestAncestor } from './common.js'
6
- import { encodeProjectPath } from '../jsonl-reader.js'
5
+ import { claudeConfigDir, isFilesystemRoot, resolveClaudeConfigDirOverride } from '../utils/claude-config-dir.js'
6
+ import { realpathOfDeepestAncestor, isConfigDirOrDirectChild, fileOpErrorMessage, clientSafeError } from './common.js'
7
+ import { truncateForLog } from './reader.js'
8
+ import { createLogger } from '../logger.js'
9
+ import { openNoFollow } from './open-nofollow.js'
10
+ import { isPathWithin } from '../utils/path-containment.js'
11
+ import { projectSlug } from '../jsonl-reader.js'
12
+ import { readPinnedHandle, readFdBounded } from '../utils/regular-file.js'
13
+
14
+ const log = createLogger('ws')
7
15
 
8
16
  /** stat-size ceiling before attempting a read (matches readFileContent) */
9
17
  const MAX_FILE_SIZE = 512 * 1024
@@ -109,16 +117,26 @@ function hasMarkdownExt(p) {
109
117
  * IMPORT_ALLOWED_EXTS is rejected up front — same skip shape as out-of-bounds —
110
118
  * closing the "@import a non-markdown sensitive file under ~/.claude" class (#6971).
111
119
  *
120
+ * With `{ markdownOutside: projectReal }` (the three fixed root files pass this,
121
+ * #8021), the target may resolve anywhere INSIDE `projectReal` — the dashboard's
122
+ * file viewer can already open every file there, so a `CLAUDE.md` symlinked to
123
+ * an extensionless `.cursorrules` keeps working — but a target OUTSIDE it (i.e.
124
+ * under ~/.claude) must be markdown. That closes `CLAUDE.md ->
125
+ * ~/.claude/.credentials.json`, which the fixed roots' exemption from the
126
+ * `@import` gate had left open.
127
+ *
112
128
  * Never throws — every failure mode folds into a ready-to-push `skipEntry`
113
129
  * (byte-identical MEMORY_SKIP_ERROR shape, echoing the LEXICAL request path —
114
130
  * no distinguishing oracle) instead of a resolved path.
115
131
  *
116
132
  * @param {string} lexicalAbsPath - Absolute LEXICAL (pre-realpath) target path
117
133
  * @param {string[]} allowedRoots - Real-path roots the target must resolve within
118
- * @param {{requireMarkdownExt?: boolean}} [opts] - `@import`-only markdown-extension gate
134
+ * @param {{requireMarkdownExt?: boolean, markdownOutside?: string}} [opts] - `requireMarkdownExt`:
135
+ * the `@import` markdown gate; `markdownOutside`: the project's real path, outside of
136
+ * which the target must be markdown (fixed root files, #8021)
119
137
  * @returns {Promise<{resolvedPath: string|null, skipEntry: object|null}>}
120
138
  */
121
- async function resolveConfinedMemoryPath(lexicalAbsPath, allowedRoots, { requireMarkdownExt = false } = {}) {
139
+ async function resolveConfinedMemoryPath(lexicalAbsPath, allowedRoots, { requireMarkdownExt = false, markdownOutside = null } = {}) {
122
140
  const base = { path: lexicalAbsPath, exists: false, content: null, truncated: false, skipped: false, error: null }
123
141
 
124
142
  // `@import` markdown allowlist (import path ONLY; the fixed root files are
@@ -126,8 +144,9 @@ async function resolveConfinedMemoryPath(lexicalAbsPath, allowedRoots, { require
126
144
  // `~/.claude/settings.json`, an identity key, a `.jsonl` transcript, an
127
145
  // extensionless file — is rejected with the SAME shape an out-of-bounds path
128
146
  // gets, so there's no distinguishing oracle between "out of bounds", "not
129
- // markdown", and "doesn't exist". Purely LEXICAL and BEFORE any realpath/stat,
130
- // so a non-markdown import never even touches the filesystem. (#6971)
147
+ // markdown", and "doesn't exist". This LEXICAL check runs BEFORE any
148
+ // realpath/stat, so a non-markdown import never touches the filesystem; the
149
+ // resolved target is checked again below (#8017). (#6971)
131
150
  if (requireMarkdownExt && !hasMarkdownExt(lexicalAbsPath)) {
132
151
  return { resolvedPath: null, skipEntry: { ...base, skipped: true, error: MEMORY_SKIP_ERROR } }
133
152
  }
@@ -142,7 +161,25 @@ async function resolveConfinedMemoryPath(lexicalAbsPath, allowedRoots, { require
142
161
  return { resolvedPath: null, skipEntry: { ...base, skipped: true, error: MEMORY_SKIP_ERROR } }
143
162
  }
144
163
 
145
- const withinRoot = allowedRoots.some((root) => resolvedPath === root || resolvedPath.startsWith(root + sep))
164
+ // #8017 — the lexical check above only sees the path AS WRITTEN. A `.md`-named
165
+ // symlink (`notes.md -> ~/.claude/.credentials.json`) passes it and resolves
166
+ // in-bounds, so the allowlist must hold for the RESOLVED target too — the
167
+ // path phase 2 actually opens. Same skip shape: no oracle.
168
+ if (requireMarkdownExt && !hasMarkdownExt(resolvedPath)) {
169
+ return { resolvedPath: null, skipEntry: { ...base, skipped: true, error: MEMORY_SKIP_ERROR } }
170
+ }
171
+
172
+ // #8021 — a fixed root may point anywhere in the project, but outside it
173
+ // (under ~/.claude) only at markdown. Same skip shape: no oracle.
174
+ // Nested roots, on purpose: when the session cwd is home (or above), ~/.claude
175
+ // is INSIDE the project, so this never fires — and `read_file` can then open
176
+ // ~/.claude/.credentials.json directly too. The invariant is "the memory panel
177
+ // exposes nothing the file viewer would refuse", pinned by memory-read.test.js.
178
+ if (markdownOutside && !isPathWithin(resolvedPath, markdownOutside) && !hasMarkdownExt(resolvedPath)) {
179
+ return { resolvedPath: null, skipEntry: { ...base, skipped: true, error: MEMORY_SKIP_ERROR } }
180
+ }
181
+
182
+ const withinRoot = allowedRoots.some((root) => isPathWithin(resolvedPath, root))
146
183
  if (!withinRoot) {
147
184
  // Outside every allowed root — reject WITHOUT stat'ing (no existence oracle).
148
185
  // Echo the LEXICAL request path (base.path), NOT the realpath-resolved path,
@@ -150,20 +187,34 @@ async function resolveConfinedMemoryPath(lexicalAbsPath, allowedRoots, { require
150
187
  return { resolvedPath: null, skipEntry: { ...base, skipped: true, error: MEMORY_SKIP_ERROR } }
151
188
  }
152
189
 
190
+ // The config directory itself and any file directly in it are daemon-managed
191
+ // state, so the memory panel shows nothing the file viewer refuses (the same
192
+ // invariant as the #8021 carve-out above). It only bites when the config
193
+ // directory sits INSIDE an allowed root, i.e. a project cwd at or above it. Same
194
+ // skip shape as every other refusal here: no oracle.
195
+ if (await isConfigDirOrDirectChild(resolvedPath)) {
196
+ return { resolvedPath: null, skipEntry: { ...base, skipped: true, error: MEMORY_SKIP_ERROR } }
197
+ }
198
+
153
199
  return { resolvedPath, skipEntry: null }
154
200
  }
155
201
 
156
202
  /**
157
203
  * Phase 2 of the confined-memory-file read: `stat`/`open`/read an ALREADY
158
204
  * resolved+validated real path (see `resolveConfinedMemoryPath`). The final
159
- * open uses O_NOFOLLOW to close the post-validation TOCTOU window (a symlink
160
- * swapped in at the target between validation and open is rejected, not
161
- * followed).
205
+ * open goes through `openNoFollow`, which closes the post-validation TOCTOU
206
+ * window (a symlink swapped in at the target between validation and open is
207
+ * rejected with ELOOP, not followed) on win32 as well as POSIX — the bare
208
+ * `O_NOFOLLOW` flag this used to pass is undefined on Windows and ORed to 0,
209
+ * so the refusal documented here was not actually happening there (#7280).
162
210
  *
163
211
  * @param {string} resolvedPath - Real (post-realpath, already containment-checked) path
212
+ * @param {() => Promise<void>} [__testBetweenStatAndOpen] - TEST-ONLY seam, run
213
+ * after the pre-open `stat` and before the open so a test can swap the file
214
+ * for a FIFO deterministically. Every production caller passes one argument.
164
215
  * @returns {Promise<{path: string, exists: boolean, content: string|null, truncated: boolean, skipped: boolean, error: string|null}>}
165
216
  */
166
- async function readResolvedMemoryFile(resolvedPath) {
217
+ export async function readResolvedMemoryFile(resolvedPath, __testBetweenStatAndOpen, pinRoot = null) {
167
218
  const base = { path: resolvedPath, exists: false, content: null, truncated: false, skipped: false, error: null }
168
219
 
169
220
  let fileStat
@@ -171,7 +222,8 @@ async function readResolvedMemoryFile(resolvedPath) {
171
222
  fileStat = await stat(resolvedPath)
172
223
  } catch (err) {
173
224
  if (err.code === 'ENOENT') return { ...base, exists: false }
174
- return { ...base, error: err.code === 'EACCES' ? 'Permission denied' : (err.message || 'Unknown error') }
225
+ log.error(`memory file stat failed: ${truncateForLog(err.message)}`)
226
+ return { ...base, error: fileOpErrorMessage(err) }
175
227
  }
176
228
 
177
229
  if (!fileStat.isFile()) {
@@ -181,17 +233,40 @@ async function readResolvedMemoryFile(resolvedPath) {
181
233
  return { ...base, exists: true, error: 'File too large (max 512KB)' }
182
234
  }
183
235
 
236
+ if (__testBetweenStatAndOpen) await __testBetweenStatAndOpen()
237
+
184
238
  let buf
185
239
  let fh
186
240
  try {
187
- fh = await open(resolvedPath, fsConstants.O_RDONLY | fsConstants.O_NOFOLLOW)
188
- buf = await fh.readFile()
241
+ fh = await openNoFollow(resolvedPath, fsConstants.O_RDONLY)
242
+ // #7938 — the `fileStat.isFile()` check above ran BEFORE this open, so it
243
+ // cannot see a non-regular file (a FIFO/device) swapped in during the
244
+ // TOCTOU window between that stat and this open. openNoFollow's O_NONBLOCK
245
+ // keeps the open itself from hanging on a planted FIFO with no writer, but
246
+ // reading its content is still the wrong thing to do — re-check isFile()
247
+ // on the OPENED fd, which cannot be raced the same way, before reading.
248
+ // Without this, a FIFO raced in here reads as an EMPTY file (a
249
+ // non-blocking read of a writerless FIFO is EOF): exists, content '',
250
+ // no error. Same text as the pre-open refusal above.
251
+ // Bounded read from the descriptor; with a pin root the opened object must
252
+ // also be the file this name denotes beneath that root (parents included).
253
+ try {
254
+ buf = pinRoot
255
+ ? await readPinnedHandle(fh, resolvedPath, pinRoot, MAX_FILE_SIZE)
256
+ : await readFdBounded(fh, MAX_FILE_SIZE)
257
+ } catch (pinErr) {
258
+ if (pinErr.code === 'ENOTREG') throw clientSafeError('Not a regular file')
259
+ if (pinErr.code === 'EFBIG') throw clientSafeError('File too large (max 512KB)')
260
+ if (pinErr.code === 'EACCES') throw Object.assign(new Error('pinned'), { code: 'ELOOP' })
261
+ throw pinErr
262
+ }
189
263
  } catch (err) {
190
264
  if (err.code === 'ELOOP') {
191
265
  // Symlink appeared at the canonical path after validation — reject.
192
266
  return { ...base, exists: true, skipped: true, error: 'Access denied: possible symlink race — read skipped' }
193
267
  }
194
- return { ...base, exists: true, error: err.message || 'Unknown error' }
268
+ log.error(`memory file read failed: ${truncateForLog(err.message)}`)
269
+ return { ...base, exists: true, error: fileOpErrorMessage(err) }
195
270
  } finally {
196
271
  await fh?.close()
197
272
  }
@@ -216,13 +291,18 @@ async function readResolvedMemoryFile(resolvedPath) {
216
291
  *
217
292
  * @param {string} lexicalAbsPath - Absolute LEXICAL (pre-realpath) target path
218
293
  * @param {string[]} allowedRoots - Real-path roots the target must resolve within
219
- * @param {{requireMarkdownExt?: boolean}} [opts] - `@import`-only markdown-extension gate
294
+ * @param {{requireMarkdownExt?: boolean, markdownOutside?: string}} [opts] - see resolveConfinedMemoryPath
220
295
  * @returns {Promise<{path: string|null, exists: boolean, content: string|null, truncated: boolean, skipped: boolean, error: string|null}>}
221
296
  */
297
+ /** The allowed root a resolved path lies beneath (the root its parents are pinned to). */
298
+ function pinRootFor(resolvedPath, allowedRoots) {
299
+ return allowedRoots.find((r) => isPathWithin(resolvedPath, r)) ?? null
300
+ }
301
+
222
302
  async function readConfinedMemoryFile(lexicalAbsPath, allowedRoots, opts = {}) {
223
303
  const { resolvedPath, skipEntry } = await resolveConfinedMemoryPath(lexicalAbsPath, allowedRoots, opts)
224
304
  if (skipEntry) return skipEntry
225
- return readResolvedMemoryFile(resolvedPath)
305
+ return readResolvedMemoryFile(resolvedPath, undefined, pinRootFor(resolvedPath, allowedRoots))
226
306
  }
227
307
 
228
308
  /**
@@ -247,7 +327,7 @@ async function collectImports(content, importingFileAbsPath, allowedRoots, visit
247
327
  const key = resolvedPath || lexicalTarget
248
328
  if (visited.has(key)) continue // cycle/dup guard — before any read
249
329
  visited.add(key)
250
- const entry = skipEntry ?? await readResolvedMemoryFile(resolvedPath)
330
+ const entry = skipEntry ?? await readResolvedMemoryFile(resolvedPath, undefined, pinRootFor(resolvedPath, allowedRoots))
251
331
  outEntries.push({ ...entry, scope: 'import', importedFrom: importingFileAbsPath })
252
332
  if (entry.exists && entry.content && !entry.skipped && !entry.error) {
253
333
  await collectImports(entry.content, entry.path, allowedRoots, visited, depth + 1, outEntries)
@@ -272,7 +352,8 @@ export function createMemoryOps(sendFn, resolveSessionCwd) {
272
352
  * https://code.claude.com/docs/en/memory ("content is ordered from the
273
353
  * filesystem root down to your working directory"; within one directory,
274
354
  * CLAUDE.local.md is read right after that directory's CLAUDE.md):
275
- * 1. global — ~/.claude/CLAUDE.md (loaded first, lowest precedence)
355
+ * 1. global — <claude config dir>/CLAUDE.md (loaded first, lowest precedence;
356
+ * the dir is CLAUDE_CONFIG_DIR when valid, else ~/.claude)
276
357
  * 2. project — <sessionCwd>/CLAUDE.md
277
358
  * 3. local — <sessionCwd>/CLAUDE.local.md (loaded last, highest precedence)
278
359
  * Managed/enterprise policy memory is out of scope — chroxy has no managed-
@@ -286,7 +367,7 @@ export function createMemoryOps(sendFn, resolveSessionCwd) {
286
367
  * design), so there is no client-controlled traversal surface for the three
287
368
  * root files. The ONLY variable input is @import references found INSIDE
288
369
  * those files' own content, which are confined to the same two roots
289
- * (session cwd, user's ~/.claude) via `readConfinedMemoryFile` AND restricted
370
+ * (session cwd, claude's config dir) via `readConfinedMemoryFile` AND restricted
290
371
  * to markdown targets (`.md`/`.markdown`) — an import resolving outside both
291
372
  * roots, or to a non-markdown file (e.g. `~/.claude/.credentials.json`), is
292
373
  * reported (for provenance/transparency) with `skipped: true` and is never
@@ -296,12 +377,12 @@ export function createMemoryOps(sendFn, resolveSessionCwd) {
296
377
  *
297
378
  * Also resolves the project's auto-generated MEMORY.md descriptor, keyed by
298
379
  * the SAME per-cwd path encoding `resolveJsonlPath` uses for transcript
299
- * storage (`encodeProjectPath` in jsonl-reader.js) — confirmed empirically
380
+ * storage (`projectSlug` in jsonl-reader.js) — confirmed empirically
300
381
  * against a live `~/.claude/projects/` tree to be keyed by the literal
301
382
  * session cwd, not the git repository root (each worktree gets its own,
302
383
  * separate memory directory).
303
384
  */
304
- async function readMemoryStack(ws, sessionCwd, requestId) {
385
+ async function readMemoryStack(ws, sessionCwd, requestId, { confineToProject = false } = {}) {
305
386
  const send = (payload) => sendFn(ws, requestId === undefined ? payload : { ...payload, requestId })
306
387
 
307
388
  if (!sessionCwd) {
@@ -313,29 +394,50 @@ export function createMemoryOps(sendFn, resolveSessionCwd) {
313
394
  try {
314
395
  cwdReal = await resolveSessionCwd(sessionCwd)
315
396
  } catch (err) {
316
- send({ type: 'memory_stack_result', entries: [], memoryFile: null, error: err.message || 'Failed to resolve session directory' })
397
+ log.error(`memory stack cwd resolve failed: ${truncateForLog(err.message)}`)
398
+ send({ type: 'memory_stack_result', entries: [], memoryFile: null, error: fileOpErrorMessage(err, 'Failed to resolve session directory') })
317
399
  return
318
400
  }
319
401
 
402
+ // The canonical form is read first and the async resolution second; the
403
+ // global memory root is used only when the two are equal and not a filesystem
404
+ // root, otherwise it is unavailable. claudeConfigDir() is canonical for an
405
+ // override but the default `<home>/.claude` is only lexical, so the canonical
406
+ // form comes from the same sync resolver for both.
407
+ const claudeHome = claudeConfigDir()
408
+ const claudeHomeCanonical = resolveClaudeConfigDirOverride(claudeHome)
320
409
  let claudeHomeReal
321
410
  try {
322
- claudeHomeReal = await realpathOfDeepestAncestor(resolve(homedir(), '.claude'))
411
+ claudeHomeReal = await realpathOfDeepestAncestor(claudeHome)
323
412
  } catch {
324
- claudeHomeReal = normalize(resolve(homedir(), '.claude'))
413
+ claudeHomeReal = normalize(claudeHome)
325
414
  }
326
-
327
- const allowedRoots = [cwdReal, claudeHomeReal]
415
+ const claudeRootUsable = claudeHomeCanonical !== null && claudeHomeReal === claudeHomeCanonical && !isFilesystemRoot(claudeHomeReal)
416
+ // The session's own generated-memory directory inside the config tree.
417
+ const encoded = projectSlug(cwdReal)
418
+ // The lexical name is the root, always. `projects/<slug>` may itself be a
419
+ // link: a project-confined reader then refuses it (a component of the root is a
420
+ // link, so nothing resolves beneath it), and an unconfined reader resolves the
421
+ // file's real path against the whole config tree anyway.
422
+ const projectMemoryRoot = resolve(claudeHomeReal, 'projects', encoded)
423
+ // A project-confined reader (a session-scoped connection) is given the
424
+ // session directory and THIS project's memory directory — not the whole
425
+ // config tree, which also holds the user-level memory and every other
426
+ // project's memory — and the user-level CLAUDE.md is not read at all.
427
+ const allowedRoots = confineToProject
428
+ ? (claudeRootUsable ? [cwdReal, projectMemoryRoot] : [cwdReal])
429
+ : (claudeRootUsable ? [cwdReal, claudeHomeReal] : [cwdReal])
328
430
  const visited = new Set()
329
431
  const entries = []
330
432
 
331
433
  const roots = [
332
- { scope: 'global', lexicalPath: resolve(claudeHomeReal, 'CLAUDE.md') },
434
+ ...(confineToProject ? [] : [{ scope: 'global', lexicalPath: resolve(claudeHomeReal, 'CLAUDE.md') }]),
333
435
  { scope: 'project', lexicalPath: resolve(cwdReal, 'CLAUDE.md') },
334
436
  { scope: 'local', lexicalPath: resolve(cwdReal, 'CLAUDE.local.md') },
335
437
  ]
336
438
 
337
439
  for (const { scope, lexicalPath } of roots) {
338
- const entry = await readConfinedMemoryFile(lexicalPath, allowedRoots)
440
+ const entry = await readConfinedMemoryFile(lexicalPath, allowedRoots, { markdownOutside: cwdReal })
339
441
  visited.add(entry.path || lexicalPath)
340
442
  entries.push({ ...entry, scope, importedFrom: null })
341
443
  if (entry.exists && entry.content && !entry.skipped && !entry.error) {
@@ -344,10 +446,10 @@ export function createMemoryOps(sendFn, resolveSessionCwd) {
344
446
  }
345
447
 
346
448
  // Auto-generated project memory (MEMORY.md) — restricted to the
347
- // ~/.claude root only (it never lives under the session cwd).
348
- const encoded = encodeProjectPath(cwdReal)
349
- const memoryFilePath = resolve(claudeHomeReal, 'projects', encoded, 'memory', 'MEMORY.md')
350
- const memoryFile = await readConfinedMemoryFile(memoryFilePath, [claudeHomeReal])
449
+ // claude config dir root only (it never lives under the session cwd), and
450
+ // its resolved target must be markdown like every other read here.
451
+ const memoryFilePath = resolve(projectMemoryRoot, 'memory', 'MEMORY.md')
452
+ const memoryFile = await readConfinedMemoryFile(memoryFilePath, claudeRootUsable ? [confineToProject ? projectMemoryRoot : claudeHomeReal] : [], { requireMarkdownExt: true })
351
453
 
352
454
  send({ type: 'memory_stack_result', entries, memoryFile, error: null })
353
455
  }