@chroxy/server 0.11.0 → 0.11.11
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.
- package/CONFIG.md +206 -18
- package/README.md +2 -2
- package/hooks/claude-native-route-check.mjs +17 -0
- package/hooks/permission-hook.sh +166 -11
- package/package.json +16 -12
- package/sidecar/Dockerfile +1 -1
- package/sidecar/PROTOCOL.md +9 -1
- package/sidecar/agent.js +102 -10
- package/sidecar/package-lock.json +3 -3
- package/src/acp-config.js +206 -0
- package/src/acp-session.js +958 -0
- package/src/activity-registry.js +110 -5
- package/src/agent-connections.js +304 -0
- package/src/agent-control/client.js +1645 -0
- package/src/agent-control/events.js +551 -0
- package/src/agent-control/local-connection.js +293 -0
- package/src/agent-control/mcp-server.js +547 -0
- package/src/anthropic-compatible-config.js +30 -4
- package/src/anthropic-compatible-session.js +15 -6
- package/src/anthropic-gemini-shim.js +444 -0
- package/src/anthropic-gemini-translate.js +1030 -0
- package/src/anthropic-openai-shim.js +0 -1
- package/src/archive-continuation.js +164 -0
- package/src/assistant-response.js +24 -0
- package/src/auth-probes.js +120 -13
- package/src/background-shell-tracker.js +27 -0
- package/src/base-session.js +1520 -36
- package/src/bearer-recheck.js +49 -0
- package/src/billing-budget.js +19 -0
- package/src/billing-class.js +40 -7
- package/src/binary-provenance-trust.js +11 -5
- package/src/bound-client-policy.js +291 -0
- package/src/built-in-tools/bash-exec.js +46 -7
- package/src/built-in-tools/file-ops.js +69 -5
- package/src/built-in-tools/tool-transforms.js +1113 -13
- package/src/byok-compose-state.js +6 -9
- package/src/byok-credentials.js +2 -3
- package/src/byok-mcp-client.js +92 -7
- package/src/byok-mcp-config.js +633 -122
- package/src/byok-mcp-fleet.js +49 -7
- package/src/byok-mcp-oauth-store.js +18 -16
- package/src/byok-mcp-trust.js +37 -23
- package/src/byok-session.js +707 -179
- package/src/byok-tool-executor.js +2742 -26
- package/src/byok-tools.js +9 -2
- package/src/channels/chroxy-channel-server.js +2 -3
- package/src/checkpoint-manager.js +64 -46
- package/src/claude-channel-session.js +16 -10
- package/src/claude-model-catalog.js +131 -16
- package/src/claude-model-refresh.js +385 -0
- package/src/claude-model-request.js +103 -0
- package/src/claude-setting-sources.js +74 -0
- package/src/claude-stream-parser.js +53 -3
- package/src/claude-tui/diagnostic-tail.js +83 -0
- package/src/claude-tui/form-driver.js +44 -7
- package/src/claude-tui/model-error.js +41 -0
- package/src/claude-tui/pty-driver.js +215 -88
- package/src/claude-tui/sanitize-question-payload.js +109 -0
- package/src/claude-tui/usage-limit.js +239 -0
- package/src/claude-tui-session.js +3563 -284
- package/src/claude-tui-tool-response.js +300 -0
- package/src/cli/agent-cmd.js +236 -0
- package/src/cli/agent-control-cmd.js +65 -0
- package/src/cli/config-cmd.js +4 -4
- package/src/cli/config-dir-cmd.js +234 -0
- package/src/cli/connection-state.js +54 -0
- package/src/cli/deploy-cmd.js +45 -10
- package/src/cli/doctor-cmd.js +7 -3
- package/src/cli/freshness-cmd.js +53 -0
- package/src/cli/init-cmd.js +15 -16
- package/src/cli/pages-cmd.js +6 -4
- package/src/cli/pair-code-cmd.js +7 -6
- package/src/cli/pair-discord-cmd.js +7 -6
- package/src/cli/providers-cmd.js +3 -3
- package/src/cli/schedule-cmd.js +91 -40
- package/src/cli/server-cmd.js +54 -1
- package/src/cli/service-cmd.js +33 -10
- package/src/cli/session-cmd.js +227 -74
- package/src/cli/shared.js +162 -11
- package/src/cli/shell-cmd.js +7 -4
- package/src/cli/status-cmd.js +19 -5
- package/src/cli/tokens-cmd.js +4 -5
- package/src/cli/tunnel-cmd.js +255 -24
- package/src/cli/worktree-gc-cmd.js +2 -2
- package/src/cli-session.js +1250 -126
- package/src/cli.js +8 -0
- package/src/codex-app-server-client.js +36 -3
- package/src/codex-app-server-session.js +1517 -81
- package/src/codex-bundled-client.js +66 -0
- package/src/codex-error-text.js +114 -0
- package/src/codex-model-catalog.js +548 -0
- package/src/codex-model-rejections.js +87 -0
- package/src/codex-native-resume.js +104 -0
- package/src/codex-protocol-capabilities.js +188 -0
- package/src/codex-session.js +450 -27
- package/src/config-dir-migration.js +449 -0
- package/src/config-dir.js +108 -0
- package/src/config.js +306 -26
- package/src/connection-authority.js +48 -0
- package/src/connection-info.js +78 -23
- package/src/connection-locality.js +60 -0
- package/src/container-liveness-monitor.js +189 -0
- package/src/continuation-metadata-worker.js +17 -0
- package/src/continuations.js +227 -0
- package/src/control-room/host-prune.js +5 -0
- package/src/control-room/integrations.js +7 -20
- package/src/control-room/skills-inventory.js +2 -2
- package/src/conversation-scanner.js +42 -39
- package/src/conversation-scope.js +59 -39
- package/src/conversation-search.js +28 -39
- package/src/credential-store.js +23 -19
- package/src/daemon-commit.js +92 -0
- package/src/daemon-idle-state.js +171 -0
- package/src/daemon-update-status.js +314 -0
- package/src/dashboard-build.js +70 -0
- package/src/declared-status.js +90 -0
- package/src/deepseek-credentials.js +2 -3
- package/src/dev-preview.js +170 -182
- package/src/device-preferences.js +2 -2
- package/src/diff-parser.js +87 -0
- package/src/discord-credentials.js +2 -3
- package/src/docker-byok-pool.js +3 -1
- package/src/docker-byok-session.js +678 -78
- package/src/docker-image-allowlist.js +23 -1
- package/src/docker-sdk-session.js +172 -3
- package/src/docker-session.js +294 -11
- package/src/doctor.js +781 -64
- package/src/durable-write.js +35 -0
- package/src/edited-input.js +264 -0
- package/src/environment-manager.js +175 -36
- package/src/environments/authority.js +101 -0
- package/src/environments/backends/container-claude-spawn.js +94 -0
- package/src/environments/backends/docker.js +31 -10
- package/src/environments/backends/k8s.js +49 -24
- package/src/environments/destroy-with-sessions.js +142 -0
- package/src/event-ingest.js +118 -13
- package/src/event-normalizer.js +186 -235
- package/src/folder-trust-wire.js +137 -0
- package/src/folder-trust-worker.js +17 -0
- package/src/folder-trust.js +1572 -0
- package/src/freshness/activity-meter.js +101 -0
- package/src/freshness/adapter.js +199 -0
- package/src/freshness/checks.js +239 -0
- package/src/freshness/config.js +98 -0
- package/src/freshness/core.js +185 -0
- package/src/freshness/derive.js +680 -0
- package/src/freshness/exchange.js +305 -0
- package/src/freshness/file-ledger.js +400 -0
- package/src/freshness/grade.js +81 -0
- package/src/freshness/hash.js +45 -0
- package/src/freshness/inbox.js +163 -0
- package/src/freshness/index.js +20 -0
- package/src/freshness/investigation.js +474 -0
- package/src/freshness/keys.js +177 -0
- package/src/freshness/ledger.js +122 -0
- package/src/freshness/outbox.js +131 -0
- package/src/freshness/policy.js +599 -0
- package/src/freshness/queue.js +843 -0
- package/src/freshness/read-errors.js +23 -0
- package/src/freshness/render.js +153 -0
- package/src/freshness/runtime.js +113 -0
- package/src/freshness/session-registry-adapter.js +161 -0
- package/src/freshness/state-file-adapter.js +167 -0
- package/src/freshness-runner/build-info.js +139 -0
- package/src/freshness-runner/cli.js +105 -0
- package/src/freshness-runner/run-tick.js +109 -0
- package/src/freshness-runner/stamp.js +57 -0
- package/src/freshness-sources/claude-transcript-adapter.js +211 -0
- package/src/freshness-sources/codex-transcript-adapter.js +33 -0
- package/src/freshness-sources/index.js +8 -0
- package/src/gemini-byok-session.js +292 -0
- package/src/gemini-credentials.js +67 -0
- package/src/gemini-key-safety.js +246 -0
- package/src/gemini-models.js +386 -0
- package/src/gemini-session.js +81 -5
- package/src/handler-utils.js +540 -80
- package/src/handlers/checkpoint-handlers.js +47 -4
- package/src/handlers/control-room-handlers.js +51 -11
- package/src/handlers/conversation-handlers.js +469 -56
- package/src/handlers/daemon-update-handlers.js +81 -0
- package/src/handlers/evaluator-handlers.js +14 -1
- package/src/handlers/feature-handlers.js +186 -69
- package/src/handlers/file-handlers.js +57 -13
- package/src/handlers/folder-trust-handlers.js +90 -0
- package/src/handlers/input-handlers.js +714 -48
- package/src/handlers/orchestration-handlers.js +3 -3
- package/src/handlers/principal-admin-handlers.js +229 -0
- package/src/handlers/principal-handlers.js +262 -0
- package/src/handlers/scheduler-handlers.js +92 -18
- package/src/handlers/session-handlers.js +551 -20
- package/src/handlers/session-pr-status-handlers.js +283 -0
- package/src/handlers/session-pr-threads-handlers.js +247 -0
- package/src/handlers/settings-handlers.js +372 -50
- package/src/handlers/skills-handlers.js +20 -4
- package/src/handlers/summarize-handlers.js +32 -1
- package/src/handlers/survey-throttle.js +380 -0
- package/src/health-proof.js +40 -0
- package/src/history-page.js +112 -0
- package/src/http-routes.js +127 -8
- package/src/ide/search.js +24 -6
- package/src/ide/symbols.js +20 -6
- package/src/input-context.js +117 -0
- package/src/instruction-policy-compiler.js +229 -0
- package/src/instruction-policy.js +250 -0
- package/src/jsonl-reader.js +378 -91
- package/src/jsonl-subprocess-session.js +96 -9
- package/src/keychain.js +1 -1
- package/src/logger.js +166 -30
- package/src/mailbox-route.js +113 -22
- package/src/message-wire.js +386 -0
- package/src/model-discovery.js +43 -3
- package/src/models.js +1276 -174
- package/src/notification-prefs.js +27 -4
- package/src/notifications/discord-billing-sink.js +22 -5
- package/src/notifications/discord-ci-sink.js +243 -0
- package/src/notifications/discord-webhook-client.js +71 -0
- package/src/notifications/discord-webhook-sink.js +64 -23
- package/src/notifications/expo-push-sink.js +85 -6
- package/src/notifications/ready-body.js +16 -5
- package/src/ollama-session.js +34 -2
- package/src/orchestration/build-manager.js +9 -2
- package/src/orchestration/git-ops.js +41 -7
- package/src/orchestration/orchestration-manager.js +112 -36
- package/src/orchestration/permission-gate.js +17 -2
- package/src/orchestration/run-model.js +2 -1
- package/src/orchestration/run-record.js +11 -0
- package/src/orchestration/turn-driver.js +185 -43
- package/src/orphan-reaper.js +470 -0
- package/src/outgoing-policy.js +189 -0
- package/src/pairing.js +92 -13
- package/src/path-hash-trust-ledger.js +642 -25
- package/src/permission-deadlines.js +82 -0
- package/src/permission-floor.js +1410 -43
- package/src/permission-hook.js +180 -69
- package/src/permission-manager.js +737 -89
- package/src/permission-mode-support.js +35 -0
- package/src/permission-resolved-by.js +89 -0
- package/src/permission-resolver.js +126 -8
- package/src/permission-scope.js +43 -0
- package/src/platform.js +315 -47
- package/src/preview-ingress.js +54 -0
- package/src/preview-target.js +213 -0
- package/src/principal-audit.js +189 -0
- package/src/principal-binding.js +25 -0
- package/src/principal-brakes.js +21 -0
- package/src/principal-effect.js +101 -0
- package/src/principal-journal.js +905 -0
- package/src/principal-kill-switch.js +183 -0
- package/src/principal-label.js +5 -0
- package/src/principal-pending-permissions.js +177 -0
- package/src/principal-policy.js +1094 -0
- package/src/principal-sessions-snapshot.js +208 -0
- package/src/principal-store.js +488 -0
- package/src/principal-subscriptions.js +283 -0
- package/src/prompt-evaluator.js +34 -4
- package/src/provider-customization.js +180 -0
- package/src/providers.js +209 -4
- package/src/push.js +30 -4
- package/src/question-route-map.js +130 -0
- package/src/redaction.js +531 -20
- package/src/roster-broadcast.js +186 -0
- package/src/scheduled-task-store.js +330 -26
- package/src/scheduler.js +368 -11
- package/src/sdk-session.js +2114 -188
- package/src/server-cli/preview-shutdown.js +22 -0
- package/src/server-cli/push-notification-handler.js +17 -3
- package/src/server-cli/server-orchestrator.js +28 -5
- package/src/server-cli-child.js +33 -18
- package/src/server-cli.js +553 -38
- package/src/server-identity.js +15 -11
- package/src/service.js +396 -25
- package/src/session-archive-limits.js +15 -0
- package/src/session-archive.js +1029 -0
- package/src/session-busy-state.js +72 -0
- package/src/session-ci-watcher.js +1099 -0
- package/src/session-context.js +7 -2
- package/src/session-manager.js +4062 -312
- package/src/session-message-history.js +841 -16
- package/src/session-name-allocator.js +685 -0
- package/src/session-pr-status.js +654 -0
- package/src/session-pr-threads.js +271 -0
- package/src/session-preset-trust.js +3 -3
- package/src/session-preset.js +10 -5
- package/src/session-state-persistence.js +94 -17
- package/src/session-timeout-manager.js +20 -0
- package/src/session-token-store.js +17 -8
- package/src/session-wake.js +240 -0
- package/src/shell-approval-info.js +2 -2
- package/src/shell-approval-store.js +3 -1
- package/src/skills-loader.js +7 -4
- package/src/skills-manager.js +2 -2
- package/src/skills-trust.js +95 -13
- package/src/skills-usage.js +2 -3
- package/src/snapshots-store.js +2 -3
- package/src/statusline.js +100 -29
- package/src/summarize-session.js +53 -4
- package/src/supervisor.js +22 -11
- package/src/sweep-stale-provider-dirs.js +95 -0
- package/src/thinking-level-wire.js +39 -0
- package/src/thinking-stream.js +179 -0
- package/src/tool-result.js +61 -0
- package/src/transcript-sources.js +109 -0
- package/src/transcript-tasks.js +372 -25
- package/src/trusted-file-read.js +302 -0
- package/src/tunnel/base.js +0 -8
- package/src/tunnel/cloudflare.js +34 -31
- package/src/tunnel/index.js +9 -0
- package/src/tunnel/preview.js +64 -0
- package/src/turn-outcome.js +140 -0
- package/src/usage-normalize.js +9 -5
- package/src/user-shell-session.js +59 -5
- package/src/utils/agent-sdk-version.js +107 -0
- package/src/utils/argv-safety.js +770 -0
- package/src/utils/auth-cause.js +60 -0
- package/src/utils/binary-package-manifest.js +639 -0
- package/src/utils/binary-version.js +224 -0
- package/src/utils/bounded-regex-worker.js +29 -0
- package/src/utils/bounded-regex.js +162 -0
- package/src/utils/claude-binary.js +43 -0
- package/src/utils/claude-config-dir.js +245 -0
- package/src/utils/claude-login-command.js +16 -0
- package/src/utils/claude-native-route.js +81 -0
- package/src/utils/componentwise-resolver.js +133 -37
- package/src/utils/deploy-control-files.js +87 -0
- package/src/utils/exec-failure-reason.js +21 -0
- package/src/utils/git-repo.js +42 -0
- package/src/utils/is-entry-point.js +137 -0
- package/src/utils/node-pty-probe.js +95 -0
- package/src/utils/node-pty-support.js +118 -0
- package/src/utils/opened-file-path.js +167 -0
- package/src/utils/path-containment.js +165 -0
- package/src/utils/permission-mode-sidecar.js +51 -0
- package/src/utils/pid-liveness.js +47 -0
- package/src/utils/preflight.js +360 -16
- package/src/utils/read-limiter.js +217 -0
- package/src/utils/regular-file.js +375 -0
- package/src/utils/safe-regex.js +113 -0
- package/src/utils/small-file.js +129 -0
- package/src/utils/spawn-env.js +189 -4
- package/src/utils/stale-session-dirs.js +182 -0
- package/src/utils/stat-identity.js +43 -0
- package/src/utils/verify-provenance.js +406 -13
- package/src/web-task-manager.js +297 -16
- package/src/worktree-gc.js +202 -6
- package/src/ws-auth.js +163 -31
- package/src/ws-broadcaster.js +49 -5
- package/src/ws-client-manager.js +24 -1
- package/src/ws-client-sender.js +7 -0
- package/src/ws-file-ops/browser.js +363 -58
- package/src/ws-file-ops/common.js +320 -14
- package/src/ws-file-ops/git.js +395 -42
- package/src/ws-file-ops/index.js +2 -0
- package/src/ws-file-ops/memory.js +137 -35
- package/src/ws-file-ops/open-nofollow.js +354 -0
- package/src/ws-file-ops/reader.js +571 -113
- package/src/ws-forwarding.js +349 -39
- package/src/ws-handler-context.js +88 -2
- package/src/ws-history.js +1010 -175
- package/src/ws-message-handlers.js +25 -1
- package/src/ws-permissions.js +307 -37
- package/src/ws-schemas.js +5 -0
- package/src/ws-server.js +885 -98
- package/sidecar/node_modules/.package-lock.json +0 -29
- package/sidecar/node_modules/ws/LICENSE +0 -20
- package/sidecar/node_modules/ws/README.md +0 -548
- package/sidecar/node_modules/ws/browser.js +0 -8
- package/sidecar/node_modules/ws/index.js +0 -22
- package/sidecar/node_modules/ws/lib/buffer-util.js +0 -131
- package/sidecar/node_modules/ws/lib/constants.js +0 -19
- package/sidecar/node_modules/ws/lib/event-target.js +0 -292
- package/sidecar/node_modules/ws/lib/extension.js +0 -203
- package/sidecar/node_modules/ws/lib/limiter.js +0 -55
- package/sidecar/node_modules/ws/lib/permessage-deflate.js +0 -528
- package/sidecar/node_modules/ws/lib/receiver.js +0 -706
- package/sidecar/node_modules/ws/lib/sender.js +0 -602
- package/sidecar/node_modules/ws/lib/stream.js +0 -161
- package/sidecar/node_modules/ws/lib/subprotocol.js +0 -62
- package/sidecar/node_modules/ws/lib/validation.js +0 -152
- package/sidecar/node_modules/ws/lib/websocket-server.js +0 -554
- package/sidecar/node_modules/ws/lib/websocket.js +0 -1393
- package/sidecar/node_modules/ws/package.json +0 -70
- package/sidecar/node_modules/ws/wrapper.mjs +0 -21
- package/src/dashboard-next/dist/assets/index-C2ipeGlq.js +0 -111
- package/src/dashboard-next/dist/assets/index-DM-_dISG.css +0 -1
- package/src/dashboard-next/dist/index.html +0 -13
- package/src/dashboard-next/node_modules/.vite/vitest/da39a3ee5e6b4b0d3255bfef95601890afd80709/results.json +0 -1
|
@@ -0,0 +1,449 @@
|
|
|
1
|
+
import { chmodSync, cpSync, existsSync, lstatSync, mkdirSync, readdirSync, readFileSync, statSync } from 'fs'
|
|
2
|
+
import { join, resolve } from 'path'
|
|
3
|
+
import { configDir, defaultConfigDir } from './config-dir.js'
|
|
4
|
+
import { writeFileRestricted } from './platform.js'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* #7240 — daemon state left behind at `~/.chroxy` when `CHROXY_CONFIG_DIR`
|
|
8
|
+
* points somewhere else.
|
|
9
|
+
*
|
|
10
|
+
* #7052 made the variable relocate ALL daemon state. For anyone who already set
|
|
11
|
+
* it, that means roughly twenty files stop being read on upgrade — and none of
|
|
12
|
+
* them announce it. Two are sharp: an unmoved `server-identity.json` on a
|
|
13
|
+
* keychain-less host makes the daemon mint a NEW identity key (pinned clients
|
|
14
|
+
* report a possible MITM, and #5615's fail-safe cannot fire because
|
|
15
|
+
* absent-everywhere is indistinguishable from first run), and an unmoved
|
|
16
|
+
* `config.json` loses the apiToken, after which `chroxy init` mints a fresh one
|
|
17
|
+
* and every paired device has to re-pair.
|
|
18
|
+
*
|
|
19
|
+
* **The stranded set is derived, never enumerated.** The obvious shape is a
|
|
20
|
+
* const array of the ~20 known state filenames, and it is the repo's documented
|
|
21
|
+
* false-safety pattern (`docs/false-safety-guards.md`): a hardcoded list next to
|
|
22
|
+
* a set that grows. The next module to add a state file would not be in it, and
|
|
23
|
+
* the check would report a clean tree while silently missing that file — the
|
|
24
|
+
* exact failure that #7192/#7197 were filed for. So the set is computed instead:
|
|
25
|
+
* every entry present under the default root and absent under the resolved one.
|
|
26
|
+
* A new state file is covered the day it is written, with no list to update.
|
|
27
|
+
*
|
|
28
|
+
* The one list here is {@link EPHEMERAL_ENTRIES}, and it is deliberately safe to
|
|
29
|
+
* get wrong: both detection and migration consult it, so they can never disagree,
|
|
30
|
+
* and a missing name means a lock file gets reported and copied — not that state
|
|
31
|
+
* goes silently unmigrated.
|
|
32
|
+
*
|
|
33
|
+
* Policy (decided for #7240): **warn loudly, copy on explicit request.** The
|
|
34
|
+
* daemon cannot distinguish "operator just relocated and wants their state" from
|
|
35
|
+
* "operator pointed at a deliberately clean root" — a container, a per-project
|
|
36
|
+
* dir — and copying an identity key and credentials into a directory that may be
|
|
37
|
+
* shared, synced or bind-mounted is the operator's security decision to make.
|
|
38
|
+
* The cited precedents (`maybeEncryptCredentialsAtRest`, `migrateToken`) upgrade
|
|
39
|
+
* a file in place at a path the daemon already owns; this crosses a boundary the
|
|
40
|
+
* operator explicitly drew. `chroxy config-dir migrate` performs the copy.
|
|
41
|
+
*/
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Runtime-ephemeral entries: never reported as stranded, never copied.
|
|
45
|
+
*
|
|
46
|
+
* `supervisor.pid` and `update.lock` describe a process that ran against the
|
|
47
|
+
* OLD root. Copying either forward hands the new root a PID/lock it does not
|
|
48
|
+
* own; leaving them out of detection is what stops the startup warning
|
|
49
|
+
* becoming permanent noise once everything real has been migrated.
|
|
50
|
+
*/
|
|
51
|
+
export const EPHEMERAL_ENTRIES = new Set(['supervisor.pid', 'update.lock'])
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* The two entries whose absence is actively destructive rather than merely
|
|
55
|
+
* inconvenient, plus the credential store.
|
|
56
|
+
*
|
|
57
|
+
* Used ONLY to order and emphasise the warning — never to gate detection or the
|
|
58
|
+
* copy. If this drifts, the message is less pointed; nothing goes unreported.
|
|
59
|
+
*/
|
|
60
|
+
export const HIGH_CONSEQUENCE_ENTRIES = ['config.json', 'server-identity.json', 'credentials.json']
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Do two paths name the same directory?
|
|
64
|
+
*
|
|
65
|
+
* Compared by device + inode, not by string. A string compare has to get
|
|
66
|
+
* symlinks, bind mounts, case-insensitive filesystems, trailing slashes and
|
|
67
|
+
* `..` spellings all right at once, and the repo has been bitten by exactly
|
|
68
|
+
* that class before (#6928's separator handling, and a case-insensitive
|
|
69
|
+
* `~/Projects` path that compared unequal to its own realpath). dev+ino is
|
|
70
|
+
* true identity and sidesteps every one of them.
|
|
71
|
+
*
|
|
72
|
+
* Windows reports `ino` as 0 on some filesystems, so fall back to a normalized
|
|
73
|
+
* (case-insensitive there) path compare when either inode is unusable.
|
|
74
|
+
*
|
|
75
|
+
* The stat is `{ bigint: true }` on purpose: an NTFS file ID is 64 bits, and a
|
|
76
|
+
* plain stat hands it back as a double that loses everything above 2^53, so two
|
|
77
|
+
* different directories created back to back can round to the same Number and
|
|
78
|
+
* read as one (#8750). BigInt `dev`/`ino` are exact.
|
|
79
|
+
*/
|
|
80
|
+
function sameDir(a, b, statImpl = statSync) {
|
|
81
|
+
try {
|
|
82
|
+
const sa = statImpl(a, { bigint: true })
|
|
83
|
+
const sb = statImpl(b, { bigint: true })
|
|
84
|
+
if (sa.ino && sb.ino) return sa.dev === sb.dev && sa.ino === sb.ino
|
|
85
|
+
} catch {
|
|
86
|
+
// One side is missing or unreadable — fall through to the path compare,
|
|
87
|
+
// which correctly reports two different paths as different.
|
|
88
|
+
}
|
|
89
|
+
const na = resolve(a)
|
|
90
|
+
const nb = resolve(b)
|
|
91
|
+
return process.platform === 'win32' ? na.toLowerCase() === nb.toLowerCase() : na === nb
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* @typedef {object} StrandedState
|
|
96
|
+
* @property {boolean} relocated The resolved root differs from `~/.chroxy`.
|
|
97
|
+
* @property {string} source The default root that may hold stranded state.
|
|
98
|
+
* @property {string} target The root the daemon actually reads.
|
|
99
|
+
* @property {string[]} stranded Entry names present in source, absent in target (sorted).
|
|
100
|
+
* @property {string[]} highConsequence The subset of `stranded` that is destructive to lose.
|
|
101
|
+
* @property {string|null} unreadable Why the source could not be listed, if it could not be.
|
|
102
|
+
*/
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Detect state stranded at the default root.
|
|
106
|
+
*
|
|
107
|
+
* Never throws: an unreadable source is reported via `unreadable` so callers on
|
|
108
|
+
* the boot path can degrade rather than fail.
|
|
109
|
+
*
|
|
110
|
+
* @param {{ source?: string, target?: string, statImpl?: typeof statSync }} [opts] Injection seams for tests.
|
|
111
|
+
* @returns {StrandedState}
|
|
112
|
+
*/
|
|
113
|
+
export function detectStrandedState({ source = defaultConfigDir(), target = configDir(), statImpl } = {}) {
|
|
114
|
+
const result = {
|
|
115
|
+
relocated: false,
|
|
116
|
+
source,
|
|
117
|
+
target,
|
|
118
|
+
stranded: [],
|
|
119
|
+
highConsequence: [],
|
|
120
|
+
unreadable: null,
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// Covers every not-relocated case at once: the variable unset, set empty, set
|
|
124
|
+
// to a relative path (config-dir.js refuses those back to the default), or set
|
|
125
|
+
// to `~/.chroxy` itself by a different spelling or through a symlink.
|
|
126
|
+
if (sameDir(source, target, statImpl)) return result
|
|
127
|
+
result.relocated = true
|
|
128
|
+
|
|
129
|
+
if (!existsSync(source)) return result
|
|
130
|
+
|
|
131
|
+
let entries
|
|
132
|
+
try {
|
|
133
|
+
entries = readdirSync(source)
|
|
134
|
+
} catch (err) {
|
|
135
|
+
result.unreadable = err.message
|
|
136
|
+
return result
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
for (const name of entries) {
|
|
140
|
+
if (EPHEMERAL_ENTRIES.has(name)) continue
|
|
141
|
+
if (!existsSync(join(target, name))) result.stranded.push(name)
|
|
142
|
+
}
|
|
143
|
+
result.stranded.sort()
|
|
144
|
+
result.highConsequence = HIGH_CONSEQUENCE_ENTRIES.filter((n) => result.stranded.includes(n))
|
|
145
|
+
return result
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* #7244 — acknowledging a deliberate second root.
|
|
150
|
+
*
|
|
151
|
+
* Running a daemon at `CHROXY_CONFIG_DIR=/scratch/x` beside a real `~/.chroxy`
|
|
152
|
+
* is a legitimate setup (the isolated-preview smoke recipe does exactly that),
|
|
153
|
+
* and the stranded warning then fires at every boot with no way to say "I know".
|
|
154
|
+
*
|
|
155
|
+
* The acknowledgement is a SNAPSHOT of entry names, not a flag. `chroxy
|
|
156
|
+
* config-dir ack` records the names stranded right now into
|
|
157
|
+
* {@link ACK_FILE_NAME} in the TARGET root; the warning stays quiet only while
|
|
158
|
+
* every currently stranded name is in that snapshot. An entry whose NAME is
|
|
159
|
+
* not in it warns again. A suppression
|
|
160
|
+
* flag (`CHROXY_CONFIG_DIR_ACK=1`) was rejected because once set it also
|
|
161
|
+
* silences a stranded entry that appears later — the false-safety shape
|
|
162
|
+
* #7052/#7238/#7239 closed. An automatic marker written at boot was rejected
|
|
163
|
+
* because it would silence the warning after one boot with no operator
|
|
164
|
+
* decision, so an accidental stranding is reported once and then forgotten.
|
|
165
|
+
*
|
|
166
|
+
* The subset logic lives HERE and nowhere else: startup, `chroxy doctor`,
|
|
167
|
+
* `config-dir status` and `config-dir ack` all go through it. Matching is by
|
|
168
|
+
* entry name; contents are not hashed.
|
|
169
|
+
*
|
|
170
|
+
* Consequence of name-matching: a name acknowledged earlier, then removed and
|
|
171
|
+
* re-created at the default root, stays quiet until the next `ack` replaces the
|
|
172
|
+
* snapshot. That is deliberate. Keying on content or mtime would re-warn at
|
|
173
|
+
* every boot for the files a live daemon keeps rewriting at the default root,
|
|
174
|
+
* which is the noise this exists to end.
|
|
175
|
+
*/
|
|
176
|
+
export const ACK_FILE_NAME = 'config-dir-ack.json'
|
|
177
|
+
const ACK_VERSION = 1
|
|
178
|
+
// The file is a few hundred bytes; a bigger one is not ours, so do not read it.
|
|
179
|
+
export const ACK_MAX_BYTES = 1024 * 1024
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Read the entry names acknowledged for this detection's source root.
|
|
183
|
+
*
|
|
184
|
+
* Returns `null` — "no acknowledgement" — for a missing, unreadable, oversized,
|
|
185
|
+
* malformed or wrong-version file, and for one recorded against a different
|
|
186
|
+
* source root. It never throws: the caller is the boot path, and the safe
|
|
187
|
+
* answer to "cannot tell" is to warn as before, not to stay quiet or fail.
|
|
188
|
+
*
|
|
189
|
+
* @param {StrandedState} detection
|
|
190
|
+
* @returns {string[]|null}
|
|
191
|
+
*/
|
|
192
|
+
export function readStrandedAck(detection) {
|
|
193
|
+
try {
|
|
194
|
+
const file = join(detection.target, ACK_FILE_NAME)
|
|
195
|
+
const st = statSync(file)
|
|
196
|
+
if (!st.isFile() || st.size > ACK_MAX_BYTES) return null
|
|
197
|
+
const doc = JSON.parse(readFileSync(file, 'utf-8'))
|
|
198
|
+
if (doc === null || typeof doc !== 'object' || Array.isArray(doc)) return null
|
|
199
|
+
if (doc.version !== ACK_VERSION) return null
|
|
200
|
+
if (doc.source !== detection.source) return null
|
|
201
|
+
if (!Array.isArray(doc.acknowledged) || !doc.acknowledged.every((n) => typeof n === 'string')) return null
|
|
202
|
+
return doc.acknowledged
|
|
203
|
+
} catch {
|
|
204
|
+
return null
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Split a detection into the entries still unacknowledged and those that are.
|
|
210
|
+
*
|
|
211
|
+
* Pure. `stranded` / `highConsequence` on the result describe only the
|
|
212
|
+
* UNACKNOWLEDGED entries, so every consumer that already reads those fields
|
|
213
|
+
* (the warning, doctor) reports the right set without further changes;
|
|
214
|
+
* `acknowledged` carries the stranded names that were covered.
|
|
215
|
+
*
|
|
216
|
+
* @param {StrandedState} detection
|
|
217
|
+
* @param {string[]|null} ackNames
|
|
218
|
+
* @returns {StrandedState & { acknowledged: string[] }}
|
|
219
|
+
*/
|
|
220
|
+
export function partitionStranded(detection, ackNames) {
|
|
221
|
+
const known = new Set(ackNames ?? [])
|
|
222
|
+
const stranded = detection.stranded.filter((n) => !known.has(n))
|
|
223
|
+
return {
|
|
224
|
+
...detection,
|
|
225
|
+
stranded,
|
|
226
|
+
highConsequence: detection.highConsequence.filter((n) => stranded.includes(n)),
|
|
227
|
+
acknowledged: detection.stranded.filter((n) => known.has(n)),
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Read the acknowledgement for `detection` and apply it. The one call startup
|
|
233
|
+
* and doctor make.
|
|
234
|
+
*
|
|
235
|
+
* @param {StrandedState} detection
|
|
236
|
+
* @returns {StrandedState & { acknowledged: string[] }}
|
|
237
|
+
*/
|
|
238
|
+
export function applyStrandedAck(detection) {
|
|
239
|
+
if (!detection.relocated || detection.unreadable) return { ...detection, acknowledged: [] }
|
|
240
|
+
return partitionStranded(detection, readStrandedAck(detection))
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Record the currently stranded names as acknowledged, replacing any earlier
|
|
245
|
+
* snapshot. Written 0600 and atomically into the TARGET root.
|
|
246
|
+
*
|
|
247
|
+
* @param {StrandedState} detection
|
|
248
|
+
* @param {{ now?: () => Date }} [opts]
|
|
249
|
+
* @returns {{ file: string, acknowledged: string[] }}
|
|
250
|
+
*/
|
|
251
|
+
export function writeStrandedAck(detection, { now = () => new Date() } = {}) {
|
|
252
|
+
mkdirSync(detection.target, { recursive: true, mode: 0o700 })
|
|
253
|
+
const file = join(detection.target, ACK_FILE_NAME)
|
|
254
|
+
const doc = {
|
|
255
|
+
version: ACK_VERSION,
|
|
256
|
+
source: detection.source,
|
|
257
|
+
acknowledged: [...detection.stranded],
|
|
258
|
+
at: now().toISOString(),
|
|
259
|
+
}
|
|
260
|
+
writeFileRestricted(file, JSON.stringify(doc, null, 2))
|
|
261
|
+
return { file, acknowledged: doc.acknowledged }
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* POSIX-quote a path for a shell command we print for the operator to paste.
|
|
266
|
+
*
|
|
267
|
+
* The `cp -a` hint below is a command a human copies and runs, so an unquoted
|
|
268
|
+
* path with a space runs a DIFFERENT command — `cp -a /srv/my state/. /dst/`
|
|
269
|
+
* copies two wrong sources — and one with a shell metacharacter is worse.
|
|
270
|
+
* Single-quoting is the only form that neutralises everything; an embedded
|
|
271
|
+
* single quote is closed, escaped and reopened, which is the standard idiom.
|
|
272
|
+
*/
|
|
273
|
+
function shellQuote(value) {
|
|
274
|
+
return `'${String(value).replace(/'/g, "'\\''")}'`
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* The copy-pasteable acknowledgement command. It carries `CHROXY_CONFIG_DIR`
|
|
279
|
+
* because run without it the command resolves the default root and answers
|
|
280
|
+
* "not relocated, nothing to acknowledge" (#7244).
|
|
281
|
+
*
|
|
282
|
+
* @param {string} target The resolved (relocated) root.
|
|
283
|
+
* @returns {string}
|
|
284
|
+
*/
|
|
285
|
+
export function ackCommand(target) {
|
|
286
|
+
return `CHROXY_CONFIG_DIR=${shellQuote(target)} chroxy config-dir ack`
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* The lines the daemon prints before exiting when no API token is configured.
|
|
291
|
+
*
|
|
292
|
+
* With config.json stranded, `chroxy init` is the WRONG advice (it mints a new
|
|
293
|
+
* token and forces every device to re-pair), and an acknowledgement must not
|
|
294
|
+
* change that: it quiets a warning, not a diagnosis. So this reads the
|
|
295
|
+
* high-consequence set AND the acknowledged names, which makes it correct
|
|
296
|
+
* whether it is handed the raw detection or an acknowledgement-filtered one.
|
|
297
|
+
*
|
|
298
|
+
* @param {(StrandedState & { acknowledged?: string[] })|null} detection
|
|
299
|
+
* @returns {string[]}
|
|
300
|
+
*/
|
|
301
|
+
export function missingTokenAdvice(detection) {
|
|
302
|
+
const configStranded = detection?.relocated
|
|
303
|
+
&& (detection.highConsequence.includes('config.json') || (detection.acknowledged ?? []).includes('config.json'))
|
|
304
|
+
if (!configStranded) return ['[!] No API token configured. Run \'chroxy init\' first.']
|
|
305
|
+
return [
|
|
306
|
+
'[!] No API token configured — but config.json is still at '
|
|
307
|
+
+ `${detection.source} while the daemon is reading ${detection.target}.`,
|
|
308
|
+
' Do NOT run \'chroxy init\' — it mints a fresh token and forces every device to re-pair.',
|
|
309
|
+
' Move your existing state instead: chroxy config-dir migrate',
|
|
310
|
+
]
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* Build the startup / doctor warning for a detection result.
|
|
315
|
+
*
|
|
316
|
+
* @param {StrandedState} detection
|
|
317
|
+
* @returns {string[]} Lines, or empty when there is nothing to say.
|
|
318
|
+
*/
|
|
319
|
+
export function formatStrandedWarning(detection) {
|
|
320
|
+
if (!detection.relocated) return []
|
|
321
|
+
if (detection.unreadable) {
|
|
322
|
+
return [`Could not check ${detection.source} for stranded state: ${detection.unreadable}`]
|
|
323
|
+
}
|
|
324
|
+
if (detection.stranded.length === 0) return []
|
|
325
|
+
|
|
326
|
+
const { stranded, source, target } = detection
|
|
327
|
+
const lines = [
|
|
328
|
+
`CHROXY_CONFIG_DIR is set, but ${stranded.length} state `
|
|
329
|
+
+ `${stranded.length === 1 ? 'entry is' : 'entries are'} still at ${source}:`,
|
|
330
|
+
]
|
|
331
|
+
// Three per line keeps the longest names readable in a terminal.
|
|
332
|
+
for (let i = 0; i < stranded.length; i += 3) {
|
|
333
|
+
lines.push(` ${stranded.slice(i, i + 3).join(' ')}`)
|
|
334
|
+
}
|
|
335
|
+
lines.push('')
|
|
336
|
+
lines.push(`The daemon is reading ${target} and will NOT find them.`)
|
|
337
|
+
|
|
338
|
+
if (detection.highConsequence.includes('config.json')) {
|
|
339
|
+
lines.push('Do NOT run \'chroxy init\' — it mints a fresh token and forces every device to re-pair.')
|
|
340
|
+
}
|
|
341
|
+
if (detection.highConsequence.includes('server-identity.json')) {
|
|
342
|
+
lines.push('An unmoved server-identity.json makes the daemon mint a new identity key,')
|
|
343
|
+
lines.push('which pinned clients report as a possible MITM.')
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
lines.push('')
|
|
347
|
+
lines.push('Copy them once: chroxy config-dir migrate')
|
|
348
|
+
lines.push(` or: cp -a ${shellQuote(`${source}/.`)} ${shellQuote(`${target}/`)}`)
|
|
349
|
+
const acked = detection.acknowledged?.length ?? 0
|
|
350
|
+
if (acked > 0) {
|
|
351
|
+
lines.push(`${acked} other ${acked === 1 ? 'entry was' : 'entries were'} acknowledged earlier and ${acked === 1 ? 'is' : 'are'} not listed above.`)
|
|
352
|
+
}
|
|
353
|
+
lines.push(`Keeping this root on purpose? ${ackCommand(target)} (re-warns for any entry not acknowledged)`)
|
|
354
|
+
return lines
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
* The lines the daemon logs at startup: the warning for what is still
|
|
359
|
+
* unacknowledged (#7244). The startup path's whole seam, so a test can pin it
|
|
360
|
+
* without booting the server.
|
|
361
|
+
*
|
|
362
|
+
* @param {StrandedState} detection
|
|
363
|
+
* @returns {string[]}
|
|
364
|
+
*/
|
|
365
|
+
export function startupStrandedWarning(detection) {
|
|
366
|
+
return formatStrandedWarning(applyStrandedAck(detection))
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
/**
|
|
370
|
+
* Mirror source directory modes onto a freshly copied tree.
|
|
371
|
+
*
|
|
372
|
+
* `cpSync` preserves FILE modes (a 0600 credentials.json lands 0600) but creates
|
|
373
|
+
* directories at the default 0755 — so `skills/` at 0700 would widen on copy.
|
|
374
|
+
* Verified, not assumed: the behaviour is asserted in
|
|
375
|
+
* `tests/config-dir-migration.test.js`.
|
|
376
|
+
*
|
|
377
|
+
* Symlinks are skipped rather than followed: `cpSync` copies them as symlinks,
|
|
378
|
+
* and `chmodSync` follows them, which would otherwise re-mode a file outside the
|
|
379
|
+
* tree entirely.
|
|
380
|
+
*/
|
|
381
|
+
function mirrorDirectoryModes(srcPath, destPath) {
|
|
382
|
+
let st
|
|
383
|
+
try {
|
|
384
|
+
st = lstatSync(srcPath)
|
|
385
|
+
} catch {
|
|
386
|
+
return
|
|
387
|
+
}
|
|
388
|
+
if (!st.isDirectory()) return
|
|
389
|
+
try {
|
|
390
|
+
chmodSync(destPath, st.mode & 0o777)
|
|
391
|
+
} catch {
|
|
392
|
+
// Best-effort: a mode we cannot set is not a reason to abort a copy that
|
|
393
|
+
// otherwise succeeded. The copied content is already in place.
|
|
394
|
+
}
|
|
395
|
+
let entries
|
|
396
|
+
try {
|
|
397
|
+
entries = readdirSync(srcPath)
|
|
398
|
+
} catch {
|
|
399
|
+
return
|
|
400
|
+
}
|
|
401
|
+
for (const name of entries) mirrorDirectoryModes(join(srcPath, name), join(destPath, name))
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
/**
|
|
405
|
+
* Copy stranded entries from the default root into the resolved one.
|
|
406
|
+
*
|
|
407
|
+
* **Never overwrites.** Only entries absent from the target are copied, which is
|
|
408
|
+
* what {@link detectStrandedState} already computes, so a partially-migrated
|
|
409
|
+
* root converges instead of clobbering.
|
|
410
|
+
*
|
|
411
|
+
* @param {{ source?: string, target?: string, detection?: StrandedState }} [opts]
|
|
412
|
+
* @returns {{ relocated: boolean, copied: string[], failed: Array<{ name: string, error: string }>, source: string, target: string, reason?: string }}
|
|
413
|
+
*/
|
|
414
|
+
export function migrateStrandedState({ source, target, detection } = {}) {
|
|
415
|
+
const det = detection ?? detectStrandedState({
|
|
416
|
+
...(source === undefined ? {} : { source }),
|
|
417
|
+
...(target === undefined ? {} : { target }),
|
|
418
|
+
})
|
|
419
|
+
const out = { relocated: det.relocated, copied: [], failed: [], source: det.source, target: det.target }
|
|
420
|
+
|
|
421
|
+
if (!det.relocated) return { ...out, reason: 'not-relocated' }
|
|
422
|
+
if (det.unreadable) return { ...out, reason: `source-unreadable: ${det.unreadable}` }
|
|
423
|
+
if (det.stranded.length === 0) return { ...out, reason: 'nothing-stranded' }
|
|
424
|
+
|
|
425
|
+
// mkdir's `mode` is masked by the umask, so chmod explicitly afterwards —
|
|
426
|
+
// the same belt-and-braces logger.js uses for the log directory.
|
|
427
|
+
try {
|
|
428
|
+
mkdirSync(det.target, { recursive: true, mode: 0o700 })
|
|
429
|
+
chmodSync(det.target, 0o700)
|
|
430
|
+
} catch (err) {
|
|
431
|
+
return { ...out, reason: `target-unwritable: ${err.message}` }
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
for (const name of det.stranded) {
|
|
435
|
+
const from = join(det.source, name)
|
|
436
|
+
const to = join(det.target, name)
|
|
437
|
+
try {
|
|
438
|
+
// errorOnExist + force:false is a second lock on "never overwrite": the
|
|
439
|
+
// detection already excluded anything present in the target, but the two
|
|
440
|
+
// reads are not atomic and the copy must lose that race, not win it.
|
|
441
|
+
cpSync(from, to, { recursive: true, preserveTimestamps: true, errorOnExist: true, force: false })
|
|
442
|
+
mirrorDirectoryModes(from, to)
|
|
443
|
+
out.copied.push(name)
|
|
444
|
+
} catch (err) {
|
|
445
|
+
out.failed.push({ name, error: err.message })
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
return out
|
|
449
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { homedir } from 'os'
|
|
2
|
+
import { join, isAbsolute } from 'path'
|
|
3
|
+
|
|
4
|
+
// Warn once per process, not per call — this is on the hot path for every path
|
|
5
|
+
// resolution in the daemon. `console.warn` rather than the logger because
|
|
6
|
+
// logger.js imports THIS module for its own log directory.
|
|
7
|
+
let warnedRelative = false
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The root as it resolves with NO override — always `~/.chroxy`.
|
|
11
|
+
*
|
|
12
|
+
* Exported because #7240 needs to compare the resolved root against the default
|
|
13
|
+
* one to find state stranded by a relocation, and that comparison is the single
|
|
14
|
+
* legitimate reason to want the un-overridden path. Keeping it here means
|
|
15
|
+
* `join(homedir(), '.chroxy')` still appears in exactly one module, so
|
|
16
|
+
* `lint-config-dir.mjs` keeps exempting exactly one file and the ratchet stays
|
|
17
|
+
* honest — a second exemption is how that guard would start to rot.
|
|
18
|
+
*
|
|
19
|
+
* Callers who want the root the daemon actually READS want {@link configDir}.
|
|
20
|
+
*
|
|
21
|
+
* @returns {string} Absolute path to the default config/state root.
|
|
22
|
+
*/
|
|
23
|
+
export function defaultConfigDir() {
|
|
24
|
+
return join(homedir(), '.chroxy')
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* The daemon's config/state root — the single resolver for `~/.chroxy`.
|
|
29
|
+
*
|
|
30
|
+
* **This is a function, and that is the whole point (#7052).** The obvious
|
|
31
|
+
* shape is a module-scope constant:
|
|
32
|
+
*
|
|
33
|
+
* const CONFIG_DIR = process.env.CHROXY_CONFIG_DIR || join(homedir(), '.chroxy')
|
|
34
|
+
*
|
|
35
|
+
* and it is silently wrong. A `const` evaluates once, at *import*, so it
|
|
36
|
+
* captures whatever the environment happened to be when the module graph was
|
|
37
|
+
* first walked. Pointing such a constant at an env-reading expression changes
|
|
38
|
+
* nothing: the variable still is not consulted at the moment that matters.
|
|
39
|
+
* That is not a hypothetical — sixteen modules independently grew their own
|
|
40
|
+
* inline `process.env.CHROXY_CONFIG_DIR || join(homedir(), '.chroxy')` copy
|
|
41
|
+
* *because* the shared constant could not be made to work, while sixteen
|
|
42
|
+
* others kept a hardcoded home-rooted `const` and quietly ignored the
|
|
43
|
+
* override. `CHROXY_CONFIG_DIR` therefore relocated only half the daemon's
|
|
44
|
+
* state, which is exactly the split-brain #7052 describes.
|
|
45
|
+
*
|
|
46
|
+
* The subtle case is a default parameter:
|
|
47
|
+
*
|
|
48
|
+
* export function readRepos(p = DEFAULT_CONFIG_PATH) {} // frozen
|
|
49
|
+
* export function readRepos(p = defaultConfigPath()) {} // live
|
|
50
|
+
*
|
|
51
|
+
* The default *expression* is evaluated per call either way — but in the first
|
|
52
|
+
* form all it does is re-read a binding that was assigned once. Only the second
|
|
53
|
+
* re-reads the environment.
|
|
54
|
+
*
|
|
55
|
+
* So: no caching, no memoization, no module-scope capture. A test's
|
|
56
|
+
* `beforeEach` that sets `CHROXY_CONFIG_DIR` to a temp dir must be respected by
|
|
57
|
+
* calls made after it, which is only true if the read happens per call.
|
|
58
|
+
*
|
|
59
|
+
* @returns {string} Absolute path to the config/state root.
|
|
60
|
+
*/
|
|
61
|
+
export function configDir() {
|
|
62
|
+
const raw = process.env.CHROXY_CONFIG_DIR
|
|
63
|
+
if (!raw) return defaultConfigDir()
|
|
64
|
+
|
|
65
|
+
// A relative value is REFUSED rather than resolved. Resolving it would only
|
|
66
|
+
// make the existing hazard explicit: because the read is per call, every fs
|
|
67
|
+
// operation would resolve it against `process.cwd()` at that moment, so
|
|
68
|
+
// `CHROXY_CONFIG_DIR=state` scatters credentials.json, the daemon identity
|
|
69
|
+
// key and the trust ledgers into whatever directory the daemon happened to be
|
|
70
|
+
// launched from — a git working tree, if `chroxy start` was run from a repo.
|
|
71
|
+
// Falling back to the known-safe default keeps secrets in one predictable
|
|
72
|
+
// place, and matches how @chroxy/protocol's project.ts already treats a
|
|
73
|
+
// relative CHROXY_WORKTREES_ROOT.
|
|
74
|
+
if (!isAbsolute(raw)) {
|
|
75
|
+
if (!warnedRelative) {
|
|
76
|
+
warnedRelative = true
|
|
77
|
+
console.warn(
|
|
78
|
+
`[config-dir] ignoring CHROXY_CONFIG_DIR=${JSON.stringify(raw)}: not an absolute path. `
|
|
79
|
+
+ `Using ${defaultConfigDir()} instead.`,
|
|
80
|
+
)
|
|
81
|
+
}
|
|
82
|
+
return defaultConfigDir()
|
|
83
|
+
}
|
|
84
|
+
return raw
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Test seam: reset the once-per-process relative-path warning. */
|
|
88
|
+
export function _resetConfigDirWarningForTest() {
|
|
89
|
+
warnedRelative = false
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* A path inside {@link configDir}, resolved at call time.
|
|
94
|
+
*
|
|
95
|
+
* configPath('config.json') // <root>/config.json
|
|
96
|
+
* configPath('logs') // <root>/logs
|
|
97
|
+
* configPath('snapshots', 'a.json') // <root>/snapshots/a.json
|
|
98
|
+
*
|
|
99
|
+
* An empty `CHROXY_CONFIG_DIR` falls back to `~/.chroxy`, matching the `||`
|
|
100
|
+
* semantics every inline copy this replaces already had — the migration is a
|
|
101
|
+
* true no-op for the sites that were already correct.
|
|
102
|
+
*
|
|
103
|
+
* @param {...string} segments Path segments appended to the root.
|
|
104
|
+
* @returns {string} Absolute path.
|
|
105
|
+
*/
|
|
106
|
+
export function configPath(...segments) {
|
|
107
|
+
return join(configDir(), ...segments)
|
|
108
|
+
}
|