@chroxy/server 0.10.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.
- package/CONFIG.md +695 -0
- package/README.md +105 -0
- package/hooks/permission-hook.sh +285 -0
- package/package.json +69 -0
- package/scripts/fix-node-pty-helper.js +44 -0
- package/sidecar/Dockerfile +40 -0
- package/sidecar/PROTOCOL.md +691 -0
- package/sidecar/agent.js +1290 -0
- package/sidecar/package-lock.json +43 -0
- package/sidecar/package.json +17 -0
- package/src/activity-registry.js +491 -0
- package/src/anthropic-compatible-config.js +534 -0
- package/src/anthropic-compatible-session.js +579 -0
- package/src/anthropic-openai-shim.js +109 -0
- package/src/anthropic-openai-translate.js +377 -0
- package/src/auth-probes.js +296 -0
- package/src/background-shell-tracker.js +320 -0
- package/src/background-shells.js +125 -0
- package/src/base-session.js +1518 -0
- package/src/billing-budget.js +191 -0
- package/src/billing-canary-monitor.js +203 -0
- package/src/billing-class.js +149 -0
- package/src/bind-host.js +82 -0
- package/src/built-in-tools/bash-exec.js +167 -0
- package/src/built-in-tools/file-ops.js +198 -0
- package/src/built-in-tools/tool-transforms.js +179 -0
- package/src/builtin-commands.js +107 -0
- package/src/byok-compose-state-shared.js +31 -0
- package/src/byok-compose-state.js +234 -0
- package/src/byok-credentials.js +127 -0
- package/src/byok-event-translator.js +136 -0
- package/src/byok-mcp-client.js +404 -0
- package/src/byok-mcp-config.js +159 -0
- package/src/byok-mcp-fleet.js +214 -0
- package/src/byok-mcp-trust.js +175 -0
- package/src/byok-session.js +2019 -0
- package/src/byok-subagent-profiles.js +101 -0
- package/src/byok-tool-executor.js +813 -0
- package/src/byok-tools.js +271 -0
- package/src/channels/PACKAGING.md +181 -0
- package/src/channels/README.md +99 -0
- package/src/channels/chroxy-channel-server.js +248 -0
- package/src/checkpoint-manager.js +515 -0
- package/src/child-stream-guard.js +38 -0
- package/src/claude-channel-session.js +185 -0
- package/src/claude-model-catalog.js +266 -0
- package/src/claude-stream-parser.js +106 -0
- package/src/claude-tui/form-driver.js +874 -0
- package/src/claude-tui/pty-driver.js +520 -0
- package/src/claude-tui/tested-cli-version.js +22 -0
- package/src/claude-tui-attachments.js +342 -0
- package/src/claude-tui-session.js +4029 -0
- package/src/cli/config-cmd.js +34 -0
- package/src/cli/credentials-cmd.js +59 -0
- package/src/cli/deploy-cmd.js +167 -0
- package/src/cli/doctor-cmd.js +52 -0
- package/src/cli/identity-cmd.js +82 -0
- package/src/cli/init-cmd.js +209 -0
- package/src/cli/pages-cmd.js +167 -0
- package/src/cli/pair-code-cmd.js +108 -0
- package/src/cli/pair-discord-cmd.js +100 -0
- package/src/cli/providers-cmd.js +168 -0
- package/src/cli/server-cmd.js +94 -0
- package/src/cli/service-cmd.js +237 -0
- package/src/cli/session-cmd.js +143 -0
- package/src/cli/shared.js +194 -0
- package/src/cli/shell-cmd.js +130 -0
- package/src/cli/status-cmd.js +204 -0
- package/src/cli/tunnel-cmd.js +101 -0
- package/src/cli/update-cmd.js +45 -0
- package/src/cli/worktree-gc-cmd.js +195 -0
- package/src/cli-session.js +1820 -0
- package/src/cli.js +60 -0
- package/src/codex-app-server-client.js +156 -0
- package/src/codex-app-server-session.js +572 -0
- package/src/codex-session.js +604 -0
- package/src/config.js +1757 -0
- package/src/connection-info.js +42 -0
- package/src/connection-locality.js +55 -0
- package/src/content-blocks.js +42 -0
- package/src/control-room/byok-pool.js +130 -0
- package/src/control-room/constants.js +26 -0
- package/src/control-room/containers.js +258 -0
- package/src/control-room/emulators.js +279 -0
- package/src/control-room/handler-factory.js +142 -0
- package/src/control-room/host-prune.js +387 -0
- package/src/control-room/integrations.js +929 -0
- package/src/control-room/repo-runtime-config.js +239 -0
- package/src/control-room/repo-set.js +162 -0
- package/src/control-room/runners.js +479 -0
- package/src/control-room/simulators.js +223 -0
- package/src/control-room/skills-inventory.js +303 -0
- package/src/control-room/survey.js +685 -0
- package/src/control-room/wsl.js +182 -0
- package/src/conversation-scanner.js +270 -0
- package/src/conversation-scope.js +52 -0
- package/src/conversation-search.js +216 -0
- package/src/cost-budget-manager.js +190 -0
- package/src/credential-cipher.js +179 -0
- package/src/credential-store.js +786 -0
- package/src/credential-test.js +145 -0
- package/src/credentials-file.js +58 -0
- package/src/dashboard-next/dist/assets/index-C2ipeGlq.js +111 -0
- package/src/dashboard-next/dist/assets/index-DM-_dISG.css +1 -0
- package/src/dashboard-next/dist/index.html +13 -0
- package/src/deepseek-credentials.js +58 -0
- package/src/deepseek-session.js +192 -0
- package/src/detect-thinking-keyword.js +67 -0
- package/src/dev-preview.js +227 -0
- package/src/devcontainer-config.js +269 -0
- package/src/device-preferences.js +267 -0
- package/src/diagnostics.js +187 -0
- package/src/diff-parser.js +157 -0
- package/src/discord-credentials.js +188 -0
- package/src/discord-pair-delivery.js +113 -0
- package/src/docker-byok-pool-stats.js +221 -0
- package/src/docker-byok-pool.js +841 -0
- package/src/docker-byok-session.js +2172 -0
- package/src/docker-image-allowlist.js +99 -0
- package/src/docker-sdk-session.js +396 -0
- package/src/docker-session.js +382 -0
- package/src/doctor-billing.js +179 -0
- package/src/doctor.js +667 -0
- package/src/duration.js +77 -0
- package/src/environment-manager.js +753 -0
- package/src/environment-statuses.js +20 -0
- package/src/environments/backends/docker.js +931 -0
- package/src/environments/backends/k8s.js +3030 -0
- package/src/environments/backends/rancher.js +326 -0
- package/src/environments/backends/types.js +332 -0
- package/src/event-ingest.js +546 -0
- package/src/event-normalizer.js +1083 -0
- package/src/external-session-registry.js +0 -0
- package/src/file-store.js +52 -0
- package/src/gemini-session.js +498 -0
- package/src/get-public-ip.js +52 -0
- package/src/git.js +15 -0
- package/src/github-webhook.js +256 -0
- package/src/handler-utils.js +663 -0
- package/src/handlers/checkpoint-handlers.js +202 -0
- package/src/handlers/control-room-handlers.js +1945 -0
- package/src/handlers/conversation-handlers.js +201 -0
- package/src/handlers/credential-handlers.js +247 -0
- package/src/handlers/evaluator-handlers.js +94 -0
- package/src/handlers/feature-handlers.js +332 -0
- package/src/handlers/file-handlers.js +142 -0
- package/src/handlers/ide-handlers.js +238 -0
- package/src/handlers/input-handlers.js +1048 -0
- package/src/handlers/pairing-handlers.js +94 -0
- package/src/handlers/repo-handlers.js +295 -0
- package/src/handlers/session-handlers.js +660 -0
- package/src/handlers/settings-handlers.js +818 -0
- package/src/handlers/skills-handlers.js +682 -0
- package/src/handlers/summarize-handlers.js +172 -0
- package/src/handlers/token-handlers.js +57 -0
- package/src/http-oversize.js +51 -0
- package/src/http-routes.js +1158 -0
- package/src/ide/search.js +268 -0
- package/src/ide/symbols.js +423 -0
- package/src/ip-utils.js +85 -0
- package/src/json-state-file.js +169 -0
- package/src/jsonl-reader.js +187 -0
- package/src/jsonl-subprocess-session.js +441 -0
- package/src/keychain.js +458 -0
- package/src/lan-ip.js +38 -0
- package/src/logger.js +388 -0
- package/src/mailbox-route.js +252 -0
- package/src/mcp-tools.js +46 -0
- package/src/message-transform.js +102 -0
- package/src/metrics.js +67 -0
- package/src/model-discovery.js +288 -0
- package/src/models.js +1426 -0
- package/src/notification-prefs.js +544 -0
- package/src/notifications/discord-billing-sink.js +323 -0
- package/src/notifications/discord-webhook-client.js +187 -0
- package/src/notifications/discord-webhook-sink.js +768 -0
- package/src/notifications/expo-push-sink.js +414 -0
- package/src/notifications/ready-body.js +177 -0
- package/src/notifications/sink-registry.js +98 -0
- package/src/notifications/sink.js +82 -0
- package/src/ollama-session.js +221 -0
- package/src/ollama-tags.js +238 -0
- package/src/openai-compatible-session.js +118 -0
- package/src/pages-store.js +255 -0
- package/src/pairing.js +836 -0
- package/src/path-hash-trust-ledger.js +311 -0
- package/src/per-session-settings.js +366 -0
- package/src/permission-audit.js +120 -0
- package/src/permission-hook.js +257 -0
- package/src/permission-manager.js +829 -0
- package/src/permission-resolver.js +144 -0
- package/src/platform.js +139 -0
- package/src/prompt-evaluator.js +386 -0
- package/src/providers.js +436 -0
- package/src/push.js +565 -0
- package/src/rate-limiter.js +433 -0
- package/src/redaction.js +206 -0
- package/src/sdk-session.js +1773 -0
- package/src/semver.js +92 -0
- package/src/server-cli/push-notification-handler.js +240 -0
- package/src/server-cli/server-orchestrator.js +168 -0
- package/src/server-cli/startup-display.js +146 -0
- package/src/server-cli/tunnel-lifecycle-handler.js +219 -0
- package/src/server-cli-child.js +207 -0
- package/src/server-cli.js +1223 -0
- package/src/server-identity.js +383 -0
- package/src/service.js +835 -0
- package/src/session-context.js +49 -0
- package/src/session-lock.js +60 -0
- package/src/session-manager.js +3257 -0
- package/src/session-message-history.js +511 -0
- package/src/session-preset-trust.js +78 -0
- package/src/session-preset.js +458 -0
- package/src/session-state-persistence.js +307 -0
- package/src/session-timeout-manager.js +166 -0
- package/src/session-token-store.js +83 -0
- package/src/shell-approval-info.js +45 -0
- package/src/shell-approval-store.js +163 -0
- package/src/shell-audit.js +63 -0
- package/src/skills-budget.js +181 -0
- package/src/skills-content-validator.js +106 -0
- package/src/skills-frontmatter.js +422 -0
- package/src/skills-loader.js +1893 -0
- package/src/skills-manager.js +256 -0
- package/src/skills-trust.js +532 -0
- package/src/skills-usage.js +335 -0
- package/src/snapshots-store.js +345 -0
- package/src/ssrf-guard.js +130 -0
- package/src/subagent-counter.js +149 -0
- package/src/summarize-session.js +268 -0
- package/src/supervisor.js +929 -0
- package/src/test-client.js +131 -0
- package/src/token-compare.js +28 -0
- package/src/token-manager.js +220 -0
- package/src/tool-result.js +87 -0
- package/src/transcript-tasks.js +334 -0
- package/src/tunnel/base.js +294 -0
- package/src/tunnel/cloudflare.js +281 -0
- package/src/tunnel/index.js +54 -0
- package/src/tunnel-check.js +84 -0
- package/src/turn-tracker.js +0 -0
- package/src/user-shell-registry.js +218 -0
- package/src/user-shell-session.js +411 -0
- package/src/utils/check-dependencies.js +70 -0
- package/src/utils/concurrency.js +24 -0
- package/src/utils/context-window-learn.js +148 -0
- package/src/utils/error-message.js +25 -0
- package/src/utils/format-bytes.js +35 -0
- package/src/utils/preflight.js +143 -0
- package/src/utils/resolve-binary.js +91 -0
- package/src/utils/respawn-rate-limiter.js +59 -0
- package/src/utils/sleep.js +58 -0
- package/src/utils/spawn-env.js +193 -0
- package/src/utils/validation-patterns.js +23 -0
- package/src/utils/win-spawn.js +98 -0
- package/src/version-check.js +16 -0
- package/src/web-task-manager.js +383 -0
- package/src/worktree-gc.js +418 -0
- package/src/worktree-reaper.js +168 -0
- package/src/ws-auth.js +645 -0
- package/src/ws-broadcaster.js +385 -0
- package/src/ws-client-manager.js +476 -0
- package/src/ws-client-sender.js +115 -0
- package/src/ws-file-ops/browser.js +594 -0
- package/src/ws-file-ops/common.js +163 -0
- package/src/ws-file-ops/git.js +298 -0
- package/src/ws-file-ops/index.js +52 -0
- package/src/ws-file-ops/reader.js +595 -0
- package/src/ws-forwarding.js +543 -0
- package/src/ws-handler-context.js +196 -0
- package/src/ws-history.js +1251 -0
- package/src/ws-message-handlers.js +165 -0
- package/src/ws-permissions.js +676 -0
- package/src/ws-schemas.js +119 -0
- package/src/ws-server.js +2866 -0
|
@@ -0,0 +1,387 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Host prune guardrails survey (#6140, epic #5530) — READ-ONLY.
|
|
3
|
+
*
|
|
4
|
+
* Surfaces reclaimable HOST docker pressure scoped STRICTLY to chroxy's OWN
|
|
5
|
+
* resources, so the Control Room can prune dangling chroxy artifacts (the
|
|
6
|
+
* container analog of `chroxy worktree gc`) without ever touching a non-chroxy
|
|
7
|
+
* workload. #6155: chroxy stamps a `com.chroxy.managed=true` docker label on the
|
|
8
|
+
* resources it creates via `docker run` / `docker commit` (docker-backend
|
|
9
|
+
* `_startContainer` / `_commitContainer`), and the survey identifies resources BY
|
|
10
|
+
* THAT LABEL first, falling back to the legacy naming convention — which also
|
|
11
|
+
* covers compose-managed containers (`docker compose up`, project `chroxy-env-*`)
|
|
12
|
+
* that don't receive the CLI `--label`, and any resource created before the label:
|
|
13
|
+
* - containers: label `com.chroxy.managed=true`, else name prefix `chroxy-env-`
|
|
14
|
+
* - images: label `com.chroxy.managed=true`, else repositories `chroxy-env`
|
|
15
|
+
* and `chroxy-byok-snap` (BYOK pool snapshots)
|
|
16
|
+
* There are no chroxy-managed named volumes (the backends use bind mounts).
|
|
17
|
+
*
|
|
18
|
+
* "Prunable" is ORPHAN-ONLY: a stopped (exited/created/dead — never running)
|
|
19
|
+
* chroxy container, or a chroxy snapshot image, that is NOT currently tracked by
|
|
20
|
+
* a live EnvironmentManager record. Tracked resources are excluded so a prune
|
|
21
|
+
* can never pull a container/image out from under a live environment.
|
|
22
|
+
*
|
|
23
|
+
* Degradation-first, like the sibling surveys: docker absent / daemon down / a
|
|
24
|
+
* stuck probe → `dockerAvailable: false` with a `note` and empty lists, never an
|
|
25
|
+
* error. Every external interaction is injectable so tests never touch real
|
|
26
|
+
* docker/exec:
|
|
27
|
+
* - `listEnvironments()` — the EnvironmentManager's records (to exclude tracked).
|
|
28
|
+
* - `_execFile(file, args, opts)` — promisified `child_process.execFile`.
|
|
29
|
+
* - `_now()` — clock.
|
|
30
|
+
*
|
|
31
|
+
* Pure parse/classify helpers are exported individually for unit tests.
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
import { execFile } from 'child_process'
|
|
35
|
+
import { promisify } from 'util'
|
|
36
|
+
import { parseByteSize } from './containers.js'
|
|
37
|
+
import { listSnapshots } from '../snapshots-store.js'
|
|
38
|
+
import { CHROXY_MANAGED_LABEL } from '../environments/backends/docker.js'
|
|
39
|
+
import { getErrorMessage } from '../utils/error-message.js'
|
|
40
|
+
import { EXEC_TIMEOUT_MS } from './constants.js'
|
|
41
|
+
|
|
42
|
+
const execFileAsync = promisify(execFile)
|
|
43
|
+
|
|
44
|
+
const EXEC_MAX_BUFFER = 8 * 1024 * 1024
|
|
45
|
+
const EXEC_OPTS = { timeout: EXEC_TIMEOUT_MS, maxBuffer: EXEC_MAX_BUFFER }
|
|
46
|
+
|
|
47
|
+
/** chroxy container name prefix (docker-backend `--name chroxy-env-<id>`). */
|
|
48
|
+
export const CHROXY_CONTAINER_PREFIX = 'chroxy-env-'
|
|
49
|
+
/** chroxy image repositories (env snapshots + BYOK pool snapshots). */
|
|
50
|
+
export const CHROXY_IMAGE_REPOS = Object.freeze(['chroxy-env', 'chroxy-byok-snap'])
|
|
51
|
+
/** Container states that are safe to prune — never `running`/`paused`/`restarting`. */
|
|
52
|
+
export const PRUNABLE_CONTAINER_STATES = Object.freeze(['exited', 'created', 'dead'])
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Parse `docker ps -a --format '{{.ID}}\t{{.Names}}\t{{.State}}\t{{.Size}}'`
|
|
56
|
+
* output into prunable chroxy container records. Keeps only names that actually
|
|
57
|
+
* start with the chroxy prefix (the docker `name=` filter is a loose substring
|
|
58
|
+
* match, so a non-chroxy container whose name merely CONTAINS `chroxy-env` is
|
|
59
|
+
* dropped here as defense-in-depth) and only prunable (stopped) states. The
|
|
60
|
+
* `Size` token is `"<writable> (virtual <total>)"`; we count the writable layer
|
|
61
|
+
* (what `docker rm` actually frees).
|
|
62
|
+
*
|
|
63
|
+
* #6155: `trusted` skips the name-prefix check — the caller already constrained
|
|
64
|
+
* the query by `--filter label=com.chroxy.managed=true`, so membership is proven
|
|
65
|
+
* by the label even if the name doesn't follow the legacy `chroxy-env-*` shape.
|
|
66
|
+
* The default (name-prefix required) is the legacy/fallback path.
|
|
67
|
+
*
|
|
68
|
+
* @param {string} stdout
|
|
69
|
+
* @param {{trusted?: boolean}} [opts]
|
|
70
|
+
* @returns {Array<{id: string, name: string, state: string, sizeBytes: number|null}>}
|
|
71
|
+
*/
|
|
72
|
+
export function parseContainerLines(stdout, { trusted = false } = {}) {
|
|
73
|
+
if (typeof stdout !== 'string') return []
|
|
74
|
+
const out = []
|
|
75
|
+
for (const line of stdout.split('\n')) {
|
|
76
|
+
const trimmed = line.trim()
|
|
77
|
+
if (!trimmed) continue
|
|
78
|
+
const [id, name, state, ...sizeParts] = trimmed.split('\t')
|
|
79
|
+
if (!id || typeof name !== 'string') continue
|
|
80
|
+
if (!trusted && !name.startsWith(CHROXY_CONTAINER_PREFIX)) continue
|
|
81
|
+
const st = (state || '').trim().toLowerCase()
|
|
82
|
+
if (!PRUNABLE_CONTAINER_STATES.includes(st)) continue
|
|
83
|
+
// Size = "1.09kB (virtual 5.59MB)" → writable side before " (".
|
|
84
|
+
const sizeToken = sizeParts.join('\t').split('(')[0]
|
|
85
|
+
out.push({ id: id.trim(), name: name.trim(), state: st, sizeBytes: parseByteSize(sizeToken ? sizeToken.trim() : '') })
|
|
86
|
+
}
|
|
87
|
+
return out
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Parse `docker images <repo> --format '{{.ID}}\t{{.Repository}}\t{{.Tag}}\t{{.Size}}'`
|
|
92
|
+
* output into chroxy image records, dropping any whose repository is not in the
|
|
93
|
+
* chroxy set (defense-in-depth). A `<none>` (dangling) tag is KEPT — those are
|
|
94
|
+
* prunable orphan layers — with `ref` falling back to the bare repository (it's
|
|
95
|
+
* display/exclusion-matching only; removal is always by image `id`, and a bare
|
|
96
|
+
* repo ref never collides with a retained `repo:tag` exclusion entry).
|
|
97
|
+
*
|
|
98
|
+
* #6155: `trusted` skips the repository-membership check — the caller already
|
|
99
|
+
* constrained the query by `--filter label=com.chroxy.managed=true`, so the image
|
|
100
|
+
* is chroxy's by label regardless of its repository. The default (repo must be in
|
|
101
|
+
* CHROXY_IMAGE_REPOS) is the legacy/fallback path.
|
|
102
|
+
*
|
|
103
|
+
* @param {string} stdout
|
|
104
|
+
* @param {{trusted?: boolean}} [opts]
|
|
105
|
+
* @returns {Array<{id: string, ref: string, repository: string, sizeBytes: number|null}>}
|
|
106
|
+
*/
|
|
107
|
+
export function parseImageLines(stdout, { trusted = false } = {}) {
|
|
108
|
+
if (typeof stdout !== 'string') return []
|
|
109
|
+
const out = []
|
|
110
|
+
for (const line of stdout.split('\n')) {
|
|
111
|
+
const trimmed = line.trim()
|
|
112
|
+
if (!trimmed) continue
|
|
113
|
+
const [id, repository, tag, size] = trimmed.split('\t')
|
|
114
|
+
if (!id || typeof repository !== 'string') continue
|
|
115
|
+
if (!trusted && !CHROXY_IMAGE_REPOS.includes(repository.trim())) continue
|
|
116
|
+
const repo = repository.trim()
|
|
117
|
+
const t = (tag || '').trim()
|
|
118
|
+
const ref = t && t !== '<none>' ? `${repo}:${t}` : repo
|
|
119
|
+
out.push({ id: id.trim(), ref, repository: repo, sizeBytes: parseByteSize((size || '').trim()) })
|
|
120
|
+
}
|
|
121
|
+
return out
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** Sum a list of `{ sizeBytes }` records, treating null as 0. */
|
|
125
|
+
export function sumBytes(records) {
|
|
126
|
+
return (records || []).reduce((acc, r) => acc + (Number.isFinite(r?.sizeBytes) ? r.sizeBytes : 0), 0)
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Build the set of container ids + RETAINED image refs the EnvironmentManager
|
|
131
|
+
* records reference, so the prune survey excludes them (orphan-only). Critically,
|
|
132
|
+
* this includes BOTH `env.image` (the env's current base/committed image) AND
|
|
133
|
+
* every `env.snapshots[].image` (committed `chroxy-env:<id>-<ts>` snapshots a user
|
|
134
|
+
* deliberately saved) — missing the latter would `docker rmi` retained snapshots
|
|
135
|
+
* (data loss, #6140 review). BYOK `chroxy-byok-snap:*` snapshots are tracked
|
|
136
|
+
* separately via the sidecar store (see `surveyHostPrune`).
|
|
137
|
+
*
|
|
138
|
+
* Matches both full and 12-char short container ids (the writable-layer id docker
|
|
139
|
+
* may echo) by recording the id and its 12-char prefix.
|
|
140
|
+
*
|
|
141
|
+
* @param {Array<object>} envs
|
|
142
|
+
* @returns {{ containerIds: Set<string>, imageRefs: Set<string> }}
|
|
143
|
+
*/
|
|
144
|
+
export function trackedResources(envs) {
|
|
145
|
+
const containerIds = new Set()
|
|
146
|
+
const imageRefs = new Set()
|
|
147
|
+
for (const env of Array.isArray(envs) ? envs : []) {
|
|
148
|
+
const cid = typeof env?.containerId === 'string' ? env.containerId : ''
|
|
149
|
+
if (cid) {
|
|
150
|
+
containerIds.add(cid)
|
|
151
|
+
containerIds.add(cid.slice(0, 12))
|
|
152
|
+
}
|
|
153
|
+
const image = typeof env?.image === 'string' ? env.image : ''
|
|
154
|
+
if (image) imageRefs.add(image)
|
|
155
|
+
// #6140 review: retained per-env snapshots are referenced ONLY here, never as
|
|
156
|
+
// env.image — they must be excluded or the prune deletes saved snapshots.
|
|
157
|
+
for (const snap of Array.isArray(env?.snapshots) ? env.snapshots : []) {
|
|
158
|
+
const ref = typeof snap?.image === 'string' ? snap.image : ''
|
|
159
|
+
if (ref) imageRefs.add(ref)
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
return { containerIds, imageRefs }
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** True if a surveyed container id is tracked by a live env (full or short id). */
|
|
166
|
+
function isTrackedContainer(id, tracked) {
|
|
167
|
+
if (!id) return false
|
|
168
|
+
if (tracked.containerIds.has(id) || tracked.containerIds.has(id.slice(0, 12))) return true
|
|
169
|
+
// A stored full id may be longer than the surveyed short id — match forward.
|
|
170
|
+
for (const tid of tracked.containerIds) {
|
|
171
|
+
if (tid.length >= 12 && tid.startsWith(id)) return true
|
|
172
|
+
if (id.length >= 12 && id.startsWith(tid)) return true
|
|
173
|
+
}
|
|
174
|
+
return false
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Survey reclaimable, chroxy-scoped, ORPHAN-ONLY host docker pressure.
|
|
179
|
+
*
|
|
180
|
+
* @param {object} [opts]
|
|
181
|
+
* @param {() => Array<object>} [opts.listEnvironments] - live env records (to exclude tracked).
|
|
182
|
+
* @param {() => Array<{tag: string}>} [opts.listByokSnapshots] - BYOK snapshot sidecars (to exclude retained `chroxy-byok-snap:*` images).
|
|
183
|
+
* @param {Function} [opts._execFile] - promisified execFile seam.
|
|
184
|
+
* @param {() => Date} [opts._now] - clock seam.
|
|
185
|
+
* @returns {Promise<{generatedAt: string, dockerAvailable: boolean, note: string|null,
|
|
186
|
+
* containers: Array, images: Array, summary: {containerCount: number, imageCount: number,
|
|
187
|
+
* reclaimableBytes: number}}>}
|
|
188
|
+
*/
|
|
189
|
+
export async function surveyHostPrune(opts = {}) {
|
|
190
|
+
const {
|
|
191
|
+
listEnvironments = () => [],
|
|
192
|
+
listByokSnapshots = () => listSnapshots(),
|
|
193
|
+
_execFile = execFileAsync,
|
|
194
|
+
_now = () => new Date(),
|
|
195
|
+
} = opts
|
|
196
|
+
const now = _now()
|
|
197
|
+
const base = {
|
|
198
|
+
generatedAt: now.toISOString(),
|
|
199
|
+
dockerAvailable: true,
|
|
200
|
+
note: null,
|
|
201
|
+
containers: [],
|
|
202
|
+
images: [],
|
|
203
|
+
summary: { containerCount: 0, imageCount: 0, reclaimableBytes: 0 },
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const tracked = trackedResources(typeof listEnvironments === 'function' ? listEnvironments() : [])
|
|
207
|
+
// #6140 review: retained BYOK snapshots live in the sidecar store, NOT in any
|
|
208
|
+
// env record — their `chroxy-byok-snap:*` images must be excluded too. A sidecar
|
|
209
|
+
// is the retention record; an image WITH one is user-retained → never prunable.
|
|
210
|
+
const retainedByokTags = new Set()
|
|
211
|
+
let byokReadFailed = false
|
|
212
|
+
try {
|
|
213
|
+
for (const s of (typeof listByokSnapshots === 'function' ? listByokSnapshots() : []) || []) {
|
|
214
|
+
if (s && typeof s.tag === 'string' && s.tag) retainedByokTags.add(s.tag)
|
|
215
|
+
}
|
|
216
|
+
} catch {
|
|
217
|
+
// A sidecar-store read failure must NOT widen the prune set — fail closed by
|
|
218
|
+
// skipping image pruning entirely (a missing sidecar list could otherwise let
|
|
219
|
+
// a retained snapshot look like an orphan).
|
|
220
|
+
byokReadFailed = true
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// Stopped chroxy containers. #6155: identify by label first (robust), then the
|
|
224
|
+
// legacy `chroxy-env-*` name convention (also covers compose-managed containers,
|
|
225
|
+
// which don't get the CLI label). Union, dedup by id.
|
|
226
|
+
//
|
|
227
|
+
// The LEGACY NAME query is the "is docker up" probe: if it throws, docker is
|
|
228
|
+
// unavailable → degrade the whole survey. The supplementary LABEL query
|
|
229
|
+
// soft-degrades (a note, keep the name results) — losing it can only *under*-
|
|
230
|
+
// prune, never over-prune, and the name query already finds every current
|
|
231
|
+
// chroxy container. (Mirrors the image-side handling below.)
|
|
232
|
+
let containers
|
|
233
|
+
let containerNote = null
|
|
234
|
+
const statusFilters = ['--filter', 'status=exited', '--filter', 'status=created', '--filter', 'status=dead']
|
|
235
|
+
const psFmt = '{{.ID}}\t{{.Names}}\t{{.State}}\t{{.Size}}'
|
|
236
|
+
let byNameOut
|
|
237
|
+
try {
|
|
238
|
+
byNameOut = await _execFile('docker', [
|
|
239
|
+
'ps', '-a', '--size', '--filter', 'name=chroxy-env', ...statusFilters, '--format', psFmt,
|
|
240
|
+
], EXEC_OPTS)
|
|
241
|
+
} catch (err) {
|
|
242
|
+
return {
|
|
243
|
+
...base,
|
|
244
|
+
dockerAvailable: false,
|
|
245
|
+
note: `docker is unavailable — host prune survey skipped (${getErrorMessage(err, 'exec failed')}).`,
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
let byLabelLines = []
|
|
249
|
+
try {
|
|
250
|
+
const byLabel = await _execFile('docker', [
|
|
251
|
+
'ps', '-a', '--size', '--filter', `label=${CHROXY_MANAGED_LABEL}=true`, ...statusFilters, '--format', psFmt,
|
|
252
|
+
], EXEC_OPTS)
|
|
253
|
+
byLabelLines = parseContainerLines(byLabel.stdout, { trusted: true })
|
|
254
|
+
} catch (err) {
|
|
255
|
+
containerNote = `the labeled chroxy container set could not be listed (${getErrorMessage(err, 'exec failed')}).`
|
|
256
|
+
}
|
|
257
|
+
{
|
|
258
|
+
const seen = new Set()
|
|
259
|
+
containers = []
|
|
260
|
+
for (const c of [...byLabelLines, ...parseContainerLines(byNameOut.stdout)]) {
|
|
261
|
+
if (seen.has(c.id)) continue
|
|
262
|
+
seen.add(c.id)
|
|
263
|
+
if (!isTrackedContainer(c.id, tracked)) containers.push(c)
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
// chroxy images across both repos. A per-repo failure degrades just that repo
|
|
268
|
+
// (the survey stays valid with whatever resolved). If the BYOK sidecar store
|
|
269
|
+
// couldn't be read, SKIP image pruning entirely (fail closed) — without the
|
|
270
|
+
// retained-tag set we can't tell a retained snapshot from an orphan.
|
|
271
|
+
const images = []
|
|
272
|
+
let imageNote = null
|
|
273
|
+
if (byokReadFailed) {
|
|
274
|
+
imageNote = 'BYOK snapshot registry unavailable — image pruning skipped to avoid deleting retained snapshots.'
|
|
275
|
+
} else {
|
|
276
|
+
const imgFmt = '{{.ID}}\t{{.Repository}}\t{{.Tag}}\t{{.Size}}'
|
|
277
|
+
const seenImg = new Set()
|
|
278
|
+
// Excluded if referenced by a live env (image or saved snapshot) OR retained
|
|
279
|
+
// as a BYOK snapshot sidecar. Only true orphans survive. Dedup by id so an
|
|
280
|
+
// image found by both the label and the repo query is considered once.
|
|
281
|
+
const consider = (img) => {
|
|
282
|
+
if (seenImg.has(img.id)) return
|
|
283
|
+
seenImg.add(img.id)
|
|
284
|
+
if (!tracked.imageRefs.has(img.ref) && !retainedByokTags.has(img.ref)) images.push(img)
|
|
285
|
+
}
|
|
286
|
+
// #6155: label-identified images first (robust, any repo), then the legacy
|
|
287
|
+
// per-repo lists for images created before the label existed.
|
|
288
|
+
try {
|
|
289
|
+
const { stdout } = await _execFile('docker', [
|
|
290
|
+
'images', '--filter', `label=${CHROXY_MANAGED_LABEL}=true`, '--format', imgFmt,
|
|
291
|
+
], EXEC_OPTS)
|
|
292
|
+
for (const img of parseImageLines(stdout, { trusted: true })) consider(img)
|
|
293
|
+
} catch (err) {
|
|
294
|
+
imageNote = `the labeled chroxy image set could not be listed (${getErrorMessage(err, 'exec failed')}).`
|
|
295
|
+
}
|
|
296
|
+
for (const repo of CHROXY_IMAGE_REPOS) {
|
|
297
|
+
try {
|
|
298
|
+
const { stdout } = await _execFile('docker', ['images', repo, '--format', imgFmt], EXEC_OPTS)
|
|
299
|
+
for (const img of parseImageLines(stdout)) consider(img)
|
|
300
|
+
} catch (err) {
|
|
301
|
+
imageNote = `some chroxy image repositories could not be listed (${getErrorMessage(err, 'exec failed')}).`
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
return {
|
|
307
|
+
generatedAt: now.toISOString(),
|
|
308
|
+
dockerAvailable: true,
|
|
309
|
+
note: [containerNote, imageNote].filter(Boolean).join(' ') || null,
|
|
310
|
+
containers,
|
|
311
|
+
images,
|
|
312
|
+
summary: {
|
|
313
|
+
containerCount: containers.length,
|
|
314
|
+
imageCount: images.length,
|
|
315
|
+
reclaimableBytes: sumBytes(containers) + sumBytes(images),
|
|
316
|
+
},
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/** Valid `kind` selectors for a host prune action. */
|
|
321
|
+
export const PRUNE_KINDS = Object.freeze(['containers', 'images', 'all'])
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* Execute a host prune (#6140). Re-surveys server-side (the SAME chroxy-scoped,
|
|
325
|
+
* orphan-only set the read survey reports) and removes ONLY those exact resource
|
|
326
|
+
* ids — never a blanket `docker system/container/image prune`, never a running or
|
|
327
|
+
* tracked resource, never a non-chroxy resource. The re-survey at action time is
|
|
328
|
+
* the authority: a client-supplied list is never trusted (there is none — the
|
|
329
|
+
* action takes only a `kind`).
|
|
330
|
+
*
|
|
331
|
+
* `docker rm` / `docker rmi` failures (e.g. an image still referenced by another
|
|
332
|
+
* image, or a container that started racing the survey) are recorded per-resource
|
|
333
|
+
* and do not abort the rest; the removed counts/bytes reflect only what succeeded.
|
|
334
|
+
*
|
|
335
|
+
* @param {object} [opts]
|
|
336
|
+
* @param {'containers'|'images'|'all'} [opts.kind='all']
|
|
337
|
+
* @param {() => Array<object>} [opts.listEnvironments]
|
|
338
|
+
* @param {Function} [opts._execFile]
|
|
339
|
+
* @param {() => Date} [opts._now]
|
|
340
|
+
* @returns {Promise<{kind: string, dockerAvailable: boolean, removedContainers: number,
|
|
341
|
+
* removedImages: number, reclaimedBytes: number, failures: Array<{ref: string, error: string}>}>}
|
|
342
|
+
*/
|
|
343
|
+
export async function runHostPrune(opts = {}) {
|
|
344
|
+
const {
|
|
345
|
+
kind = 'all',
|
|
346
|
+
listEnvironments = () => [],
|
|
347
|
+
listByokSnapshots = () => listSnapshots(),
|
|
348
|
+
_execFile = execFileAsync,
|
|
349
|
+
_now = () => new Date(),
|
|
350
|
+
} = opts
|
|
351
|
+
const selected = PRUNE_KINDS.includes(kind) ? kind : 'all'
|
|
352
|
+
const survey = await surveyHostPrune({ listEnvironments, listByokSnapshots, _execFile, _now })
|
|
353
|
+
|
|
354
|
+
const result = {
|
|
355
|
+
kind: selected,
|
|
356
|
+
dockerAvailable: survey.dockerAvailable,
|
|
357
|
+
removedContainers: 0,
|
|
358
|
+
removedImages: 0,
|
|
359
|
+
reclaimedBytes: 0,
|
|
360
|
+
failures: [],
|
|
361
|
+
}
|
|
362
|
+
if (!survey.dockerAvailable) return result
|
|
363
|
+
|
|
364
|
+
if (selected === 'containers' || selected === 'all') {
|
|
365
|
+
for (const c of survey.containers) {
|
|
366
|
+
try {
|
|
367
|
+
await _execFile('docker', ['rm', c.id], EXEC_OPTS)
|
|
368
|
+
result.removedContainers += 1
|
|
369
|
+
result.reclaimedBytes += Number.isFinite(c.sizeBytes) ? c.sizeBytes : 0
|
|
370
|
+
} catch (err) {
|
|
371
|
+
result.failures.push({ ref: c.name || c.id, error: getErrorMessage(err, 'docker rm failed') })
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
if (selected === 'images' || selected === 'all') {
|
|
376
|
+
for (const img of survey.images) {
|
|
377
|
+
try {
|
|
378
|
+
await _execFile('docker', ['rmi', img.id], EXEC_OPTS)
|
|
379
|
+
result.removedImages += 1
|
|
380
|
+
result.reclaimedBytes += Number.isFinite(img.sizeBytes) ? img.sizeBytes : 0
|
|
381
|
+
} catch (err) {
|
|
382
|
+
result.failures.push({ ref: img.ref || img.id, error: getErrorMessage(err, 'docker rmi failed') })
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
return result
|
|
387
|
+
}
|