@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,98 @@
|
|
|
1
|
+
// utils/win-spawn.js — Windows-safe child_process spawning for batch shims.
|
|
2
|
+
//
|
|
3
|
+
// Background (#windows-native): when the binary resolver lands on a `.cmd`/`.bat`
|
|
4
|
+
// shim — e.g. an npm-global `claude.cmd` on a host with no native `claude.exe` —
|
|
5
|
+
// `child_process.spawn(cmd, args)` is BROKEN on Windows two different ways:
|
|
6
|
+
//
|
|
7
|
+
// 1. Node 24's CVE-2024-27980 fix makes spawning a `.cmd`/`.bat` WITHOUT a
|
|
8
|
+
// shell throw `EINVAL` outright.
|
|
9
|
+
// 2. `{ shell: true }` with an args array concatenates argv WITHOUT quoting
|
|
10
|
+
// (DEP0190), so any argument containing a space or shell metacharacter is
|
|
11
|
+
// corrupted. claude's `--append-system-prompt <text>` is full of both, so
|
|
12
|
+
// this is not a theoretical edge — it silently mangles the system prompt.
|
|
13
|
+
//
|
|
14
|
+
// node-pty does NOT share this problem (it routes through conpty / cmd.exe
|
|
15
|
+
// internally and runs a `.cmd` fine — verified), so ClaudeTuiSession's PTY spawn
|
|
16
|
+
// needs no wrapping. This helper is used by the plain-subprocess path in
|
|
17
|
+
// cli-session.js. (DockerSession overrides _spawnPersistentProcess to run
|
|
18
|
+
// `docker` directly, so it does not go through here. The other subprocess
|
|
19
|
+
// providers — codex/gemini/deepseek/ollama via jsonl-subprocess-session.js —
|
|
20
|
+
// resolve binaries the same way and have the same need; tracked in #6484.)
|
|
21
|
+
//
|
|
22
|
+
// The `.cmd`/`.bat` escaping is the battle-tested cmd.exe algorithm popularised
|
|
23
|
+
// by `cross-spawn` (https://qntm.org/cmd), which npm itself relies on. Each token
|
|
24
|
+
// is wrapped and its embedded quotes/backslashes fixed for the target program's
|
|
25
|
+
// CommandLineToArgvW parse, then cmd metacharacters are escaped — DOUBLED for a
|
|
26
|
+
// `.cmd`/`.bat`, because the npm shim forwards `%*` through a SECOND cmd parse
|
|
27
|
+
// before the real program sees the bytes. win-spawn.test.js round-trips
|
|
28
|
+
// adversarial args (embedded quotes, `& % ^ < > !`, trailing/embedded
|
|
29
|
+
// backslashes) through a realistic `%*`-forwarding shim to prove fidelity.
|
|
30
|
+
//
|
|
31
|
+
// On POSIX, and for a directly-runnable `.exe`/`.com` on Windows, the command +
|
|
32
|
+
// args pass through unchanged.
|
|
33
|
+
|
|
34
|
+
// cmd.exe metacharacters that must be caret-escaped when not inside the program's
|
|
35
|
+
// own quoting. (Same set cross-spawn uses.)
|
|
36
|
+
const CMD_META = /([()\][%!^"`<>&|;, *?])/g
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* True if `command` is a Windows batch shim (`.cmd`/`.bat`) — the only forms that
|
|
40
|
+
* need cmd.exe routing on the child_process path.
|
|
41
|
+
* @param {string} command
|
|
42
|
+
* @returns {boolean}
|
|
43
|
+
*/
|
|
44
|
+
export function isBatchShim(command) {
|
|
45
|
+
return typeof command === 'string' && /\.(?:cmd|bat)$/i.test(command)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function escapeCommand(command) {
|
|
49
|
+
return command.replace(CMD_META, '^$1')
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function escapeArgument(arg, doubleEscapeMeta) {
|
|
53
|
+
let a = `${arg}`
|
|
54
|
+
// Sequences of backslashes followed by a `"`: double the backslashes and
|
|
55
|
+
// escape the quote (CommandLineToArgvW rules).
|
|
56
|
+
a = a.replace(/(\\*)"/g, '$1$1\\"')
|
|
57
|
+
// A trailing run of backslashes would escape our own closing quote — double it.
|
|
58
|
+
a = a.replace(/(\\*)$/, '$1$1')
|
|
59
|
+
a = `"${a}"`
|
|
60
|
+
// Escape cmd metacharacters (including the wrapping quotes just added).
|
|
61
|
+
a = a.replace(CMD_META, '^$1')
|
|
62
|
+
// A `.cmd`/`.bat` is parsed by cmd TWICE (the `/c` line, then the shim's `%*`
|
|
63
|
+
// expansion), so metacharacters need a second escaping pass to survive both.
|
|
64
|
+
if (doubleEscapeMeta) a = a.replace(CMD_META, '^$1')
|
|
65
|
+
return a
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Adapt a (command, args) pair for `child_process.spawn` on the current
|
|
70
|
+
* platform. On Windows, a `.cmd`/`.bat` command is rewritten to run under
|
|
71
|
+
* `cmd.exe /d /s /c "<escaped command line>"` with `windowsVerbatimArguments`
|
|
72
|
+
* so Node does not re-quote our already-escaped line. Everything else is
|
|
73
|
+
* returned verbatim.
|
|
74
|
+
*
|
|
75
|
+
* Usage:
|
|
76
|
+
* const { command, args, options } = prepareSpawn(CLAUDE, argv)
|
|
77
|
+
* spawn(command, args, { ...baseOptions, ...options })
|
|
78
|
+
*
|
|
79
|
+
* @param {string} command
|
|
80
|
+
* @param {string[]} [args]
|
|
81
|
+
* @param {object} [opts]
|
|
82
|
+
* @param {NodeJS.Platform} [opts.platform=process.platform] - override for tests
|
|
83
|
+
* @returns {{ command: string, args: string[], options: object }}
|
|
84
|
+
*/
|
|
85
|
+
export function prepareSpawn(command, args = [], { platform = process.platform } = {}) {
|
|
86
|
+
if (platform !== 'win32' || !isBatchShim(command)) {
|
|
87
|
+
return { command, args, options: {} }
|
|
88
|
+
}
|
|
89
|
+
const comspec = process.env.COMSPEC || 'cmd.exe'
|
|
90
|
+
const line = [escapeCommand(command), ...args.map((a) => escapeArgument(a, true))].join(' ')
|
|
91
|
+
return {
|
|
92
|
+
command: comspec,
|
|
93
|
+
// `/d` skips AutoRun, `/s` makes cmd strip exactly the single outer quote
|
|
94
|
+
// pair (so our inner quoting is preserved verbatim), `/c` runs then exits.
|
|
95
|
+
args: ['/d', '/s', '/c', `"${line}"`],
|
|
96
|
+
options: { windowsVerbatimArguments: true },
|
|
97
|
+
}
|
|
98
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Node.js version gate.
|
|
3
|
+
*
|
|
4
|
+
* Imported as the first side-effect import in cli.js so it executes before
|
|
5
|
+
* any other module initialisation. In ESM the module graph is resolved
|
|
6
|
+
* top-down, so this body runs before Commander (or anything else) loads.
|
|
7
|
+
*/
|
|
8
|
+
const nodeMajor = Number(process.versions.node.split('.')[0])
|
|
9
|
+
if (nodeMajor < 22) {
|
|
10
|
+
process.stderr.write(
|
|
11
|
+
`Chroxy requires Node.js 22 or later.\n` +
|
|
12
|
+
`You are running Node.js ${process.versions.node}.\n` +
|
|
13
|
+
`Install Node 22: https://nodejs.org/en/download/\n`
|
|
14
|
+
)
|
|
15
|
+
process.exit(1)
|
|
16
|
+
}
|
|
@@ -0,0 +1,383 @@
|
|
|
1
|
+
import { EventEmitter } from 'events'
|
|
2
|
+
import { execFile } from 'child_process'
|
|
3
|
+
import { randomUUID } from 'crypto'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Manages Claude Code Web tasks (cloud sandbox delegation).
|
|
7
|
+
*
|
|
8
|
+
* Web tasks are fire-and-forget cloud operations launched via the `&` prefix.
|
|
9
|
+
* Unlike sessions, they run in Anthropic's infrastructure, not locally.
|
|
10
|
+
*
|
|
11
|
+
* Lifecycle: pending → running → completed | failed
|
|
12
|
+
*
|
|
13
|
+
* Feature detection:
|
|
14
|
+
* Parses `claude --help` at startup to check for `--remote` and `--teleport`
|
|
15
|
+
* flags. When unavailable, launchTask() returns a clear error message.
|
|
16
|
+
* Re-detection can be triggered manually (e.g. after CLI upgrade).
|
|
17
|
+
*
|
|
18
|
+
* Events:
|
|
19
|
+
* task_created { task } - New task launched
|
|
20
|
+
* task_updated { task } - Task status changed
|
|
21
|
+
* task_error { taskId, message } - Task failed or launch error
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
const POLL_INTERVAL_MS = 10_000 // 10s between status checks
|
|
25
|
+
const MAX_TASKS = 100 // evict oldest completed/failed tasks beyond this
|
|
26
|
+
const MAX_POLL_COUNT = 60 // 60 polls × 10s = 10 min max poll duration
|
|
27
|
+
|
|
28
|
+
export class WebTaskManager extends EventEmitter {
|
|
29
|
+
constructor({ cwd } = {}) {
|
|
30
|
+
super()
|
|
31
|
+
this._cwd = cwd || process.cwd()
|
|
32
|
+
this._tasks = new Map()
|
|
33
|
+
this._childProcesses = new Set()
|
|
34
|
+
this._remoteAvailable = false
|
|
35
|
+
this._teleportAvailable = false
|
|
36
|
+
this._detected = false
|
|
37
|
+
this._pollTimer = null
|
|
38
|
+
this._pollCount = 0
|
|
39
|
+
this._inPoll = false
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Whether the CLI supports --remote (web task launch) */
|
|
43
|
+
get isAvailable() {
|
|
44
|
+
return this._remoteAvailable
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Whether the CLI supports --teleport (pull cloud work locally) */
|
|
48
|
+
get teleportAvailable() {
|
|
49
|
+
return this._teleportAvailable
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Whether feature detection has completed */
|
|
53
|
+
get detected() {
|
|
54
|
+
return this._detected
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Detect available CLI features by parsing `claude --help`.
|
|
59
|
+
* Safe to call multiple times (e.g. after CLI upgrade).
|
|
60
|
+
*
|
|
61
|
+
* @param {object} [deps] - test seam: { exec } promisified execFile stand-in
|
|
62
|
+
*/
|
|
63
|
+
async detectFeatures(deps = {}) {
|
|
64
|
+
const exec = deps.exec || execFileAsync
|
|
65
|
+
try {
|
|
66
|
+
const stdout = await exec('claude', ['--help'])
|
|
67
|
+
this._remoteAvailable = stdout.includes('--remote')
|
|
68
|
+
this._teleportAvailable = stdout.includes('--teleport')
|
|
69
|
+
} catch {
|
|
70
|
+
this._remoteAvailable = false
|
|
71
|
+
this._teleportAvailable = false
|
|
72
|
+
}
|
|
73
|
+
this._detected = true
|
|
74
|
+
return {
|
|
75
|
+
remote: this._remoteAvailable,
|
|
76
|
+
teleport: this._teleportAvailable,
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Get the feature status object for WS broadcast.
|
|
82
|
+
*/
|
|
83
|
+
getFeatureStatus() {
|
|
84
|
+
return {
|
|
85
|
+
available: this._remoteAvailable,
|
|
86
|
+
remote: this._remoteAvailable,
|
|
87
|
+
teleport: this._teleportAvailable,
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Launch a cloud task.
|
|
93
|
+
* @param {string} prompt - Task description
|
|
94
|
+
* @param {Object} [opts]
|
|
95
|
+
* @param {string} [opts.cwd] - Working directory context
|
|
96
|
+
* @returns {{ taskId: string, task: Object }}
|
|
97
|
+
* @throws {Error} if feature not available
|
|
98
|
+
*/
|
|
99
|
+
launchTask(prompt, { cwd } = {}) {
|
|
100
|
+
if (!prompt || typeof prompt !== 'string' || !prompt.trim()) {
|
|
101
|
+
throw new Error('Task prompt is required')
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (!this._remoteAvailable) {
|
|
105
|
+
throw new WebTaskUnavailableError()
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const taskId = randomUUID()
|
|
109
|
+
const task = {
|
|
110
|
+
taskId,
|
|
111
|
+
prompt: prompt.trim(),
|
|
112
|
+
status: 'pending',
|
|
113
|
+
createdAt: Date.now(),
|
|
114
|
+
updatedAt: Date.now(),
|
|
115
|
+
result: null,
|
|
116
|
+
error: null,
|
|
117
|
+
cwd: cwd || this._cwd,
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
this._tasks.set(taskId, task)
|
|
121
|
+
this._evictIfNeeded()
|
|
122
|
+
this.emit('task_created', { ...task })
|
|
123
|
+
|
|
124
|
+
// Spawn the remote process
|
|
125
|
+
this._spawnRemoteTask(task)
|
|
126
|
+
|
|
127
|
+
return { taskId, task: { ...task } }
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* List all tracked tasks.
|
|
132
|
+
* @returns {Array<Object>}
|
|
133
|
+
*/
|
|
134
|
+
listTasks() {
|
|
135
|
+
return [...this._tasks.values()].map(t => ({ ...t }))
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Get a single task by ID.
|
|
140
|
+
* @param {string} taskId
|
|
141
|
+
* @returns {Object|null}
|
|
142
|
+
*/
|
|
143
|
+
getTask(taskId) {
|
|
144
|
+
const task = this._tasks.get(taskId)
|
|
145
|
+
return task ? { ...task } : null
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Teleport a completed cloud task into a local session.
|
|
150
|
+
* @param {string} taskId
|
|
151
|
+
* @throws {Error} if teleport not available or task not found
|
|
152
|
+
*/
|
|
153
|
+
async teleportTask(taskId) {
|
|
154
|
+
if (!this._teleportAvailable) {
|
|
155
|
+
throw new Error('Claude CLI --teleport flag is not available. Update your CLI to enable this feature.')
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const task = this._tasks.get(taskId)
|
|
159
|
+
if (!task) {
|
|
160
|
+
throw new Error(`Task not found: ${taskId}`)
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
try {
|
|
164
|
+
const stdout = await execFileAsync('claude', ['--teleport', task.taskId], { cwd: task.cwd })
|
|
165
|
+
return { success: true, output: stdout }
|
|
166
|
+
} catch (err) {
|
|
167
|
+
throw new Error(`Teleport failed: ${err.message}`)
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Evict oldest completed/failed tasks when map exceeds MAX_TASKS.
|
|
173
|
+
* @private
|
|
174
|
+
*/
|
|
175
|
+
_evictIfNeeded() {
|
|
176
|
+
if (this._tasks.size <= MAX_TASKS) return
|
|
177
|
+
|
|
178
|
+
const terminal = [...this._tasks.values()]
|
|
179
|
+
.filter(t => t.status === 'completed' || t.status === 'failed')
|
|
180
|
+
.sort((a, b) => a.updatedAt - b.updatedAt)
|
|
181
|
+
|
|
182
|
+
while (this._tasks.size > MAX_TASKS && terminal.length > 0) {
|
|
183
|
+
this._tasks.delete(terminal.shift().taskId)
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Spawn the remote CLI process for a task.
|
|
189
|
+
* @private
|
|
190
|
+
*/
|
|
191
|
+
_spawnRemoteTask(task) {
|
|
192
|
+
// Use execFile with args array to prevent command injection
|
|
193
|
+
const child = execFile('claude', ['--remote', task.prompt], { cwd: task.cwd, timeout: 300_000 }, (err, stdout, stderr) => {
|
|
194
|
+
this._childProcesses.delete(child)
|
|
195
|
+
|
|
196
|
+
if (err) {
|
|
197
|
+
task.status = 'failed'
|
|
198
|
+
task.error = err.message || stderr || 'Unknown error'
|
|
199
|
+
task.updatedAt = Date.now()
|
|
200
|
+
this.emit('task_updated', { ...task })
|
|
201
|
+
this.emit('task_error', { taskId: task.taskId, message: task.error })
|
|
202
|
+
return
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// Parse task ID from CLI output if available
|
|
206
|
+
const remoteIdMatch = stdout.match(/task[:\s]+([a-zA-Z0-9-]+)/i)
|
|
207
|
+
if (remoteIdMatch) {
|
|
208
|
+
task.remoteTaskId = remoteIdMatch[1]
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
task.status = 'running'
|
|
212
|
+
task.updatedAt = Date.now()
|
|
213
|
+
this.emit('task_updated', { ...task })
|
|
214
|
+
|
|
215
|
+
// Start polling if not already
|
|
216
|
+
this._startPolling()
|
|
217
|
+
})
|
|
218
|
+
|
|
219
|
+
this._childProcesses.add(child)
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Start periodic polling for running task statuses.
|
|
224
|
+
* @private
|
|
225
|
+
*/
|
|
226
|
+
_startPolling() {
|
|
227
|
+
if (this._pollTimer) return
|
|
228
|
+
|
|
229
|
+
this._pollCount = 0
|
|
230
|
+
this._pollTimer = setInterval(() => {
|
|
231
|
+
// _pollTaskStatus is async; swallow rejections so a status-check fault
|
|
232
|
+
// can't surface as an unhandledRejection and crash the daemon (#5327).
|
|
233
|
+
Promise.resolve(this._pollTaskStatus()).catch(() => {})
|
|
234
|
+
}, POLL_INTERVAL_MS)
|
|
235
|
+
// Don't keep the event loop (and the whole daemon) alive solely to poll a
|
|
236
|
+
// handful of cloud tasks (#5327). Guarded for non-Node timer stand-ins.
|
|
237
|
+
if (this._pollTimer && typeof this._pollTimer.unref === 'function') {
|
|
238
|
+
this._pollTimer.unref()
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Stop polling.
|
|
244
|
+
* @private
|
|
245
|
+
*/
|
|
246
|
+
_stopPolling() {
|
|
247
|
+
if (this._pollTimer) {
|
|
248
|
+
clearInterval(this._pollTimer)
|
|
249
|
+
this._pollTimer = null
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Poll for status updates on running tasks.
|
|
255
|
+
* @private
|
|
256
|
+
*/
|
|
257
|
+
async _pollTaskStatus() {
|
|
258
|
+
// Re-entrancy guard (#5327 review): if the previous poll is still awaiting a
|
|
259
|
+
// status check (a real CLI call can exceed POLL_INTERVAL_MS), skip this tick
|
|
260
|
+
// rather than overlapping. Overlap would advance _pollCount faster than
|
|
261
|
+
// wall-clock (premature timeout) and race task-state transitions. The flag
|
|
262
|
+
// is cleared in finally so a rejection can't wedge polling permanently.
|
|
263
|
+
if (this._inPoll) return
|
|
264
|
+
this._inPoll = true
|
|
265
|
+
try {
|
|
266
|
+
this._pollCount++
|
|
267
|
+
|
|
268
|
+
const running = [...this._tasks.values()].filter(t => t.status === 'running')
|
|
269
|
+
if (running.length === 0) {
|
|
270
|
+
this._stopPolling()
|
|
271
|
+
return
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// Genuine timeout backstop: a task STILL running after MAX_POLL_COUNT polls
|
|
275
|
+
// (10 min) is force-failed so a stuck cloud task can't poll forever. This
|
|
276
|
+
// used to be the ONLY exit — every healthy task was force-failed here
|
|
277
|
+
// because nothing implemented the running→completed transition (#5327).
|
|
278
|
+
// Checked before any await so the synchronous timeout path stays sync.
|
|
279
|
+
if (this._pollCount >= MAX_POLL_COUNT) {
|
|
280
|
+
for (const task of running) {
|
|
281
|
+
task.status = 'failed'
|
|
282
|
+
task.error = 'Task timed out waiting for status update'
|
|
283
|
+
task.updatedAt = Date.now()
|
|
284
|
+
this.emit('task_updated', { ...task })
|
|
285
|
+
this.emit('task_error', { taskId: task.taskId, message: task.error })
|
|
286
|
+
}
|
|
287
|
+
this._stopPolling()
|
|
288
|
+
return
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
// Check each running task's remote status and transition it to a terminal
|
|
292
|
+
// state when the cloud reports done/failed (#5327). _checkRemoteStatus is a
|
|
293
|
+
// seam — a real CLI status command (or a test) drives the transition; the
|
|
294
|
+
// default reports 'running' (unknown) until that command ships.
|
|
295
|
+
for (const task of running) {
|
|
296
|
+
let result
|
|
297
|
+
try {
|
|
298
|
+
result = await this._checkRemoteStatus(task)
|
|
299
|
+
} catch {
|
|
300
|
+
// A transient status-check failure must not fail the task — let the
|
|
301
|
+
// timeout backstop catch a genuinely stuck one. Skip this pass.
|
|
302
|
+
continue
|
|
303
|
+
}
|
|
304
|
+
const next = result && result.status
|
|
305
|
+
if (next === 'completed') {
|
|
306
|
+
task.status = 'completed'
|
|
307
|
+
task.result = result.result ?? null
|
|
308
|
+
task.error = null
|
|
309
|
+
task.updatedAt = Date.now()
|
|
310
|
+
this.emit('task_updated', { ...task })
|
|
311
|
+
} else if (next === 'failed') {
|
|
312
|
+
task.status = 'failed'
|
|
313
|
+
task.error = result.error || 'Task failed'
|
|
314
|
+
task.updatedAt = Date.now()
|
|
315
|
+
this.emit('task_updated', { ...task })
|
|
316
|
+
this.emit('task_error', { taskId: task.taskId, message: task.error })
|
|
317
|
+
}
|
|
318
|
+
// Any other value (incl. 'running'/undefined) leaves the task running.
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
// Once nothing is left running, stop the timer rather than spinning until
|
|
322
|
+
// the timeout cap.
|
|
323
|
+
if (![...this._tasks.values()].some(t => t.status === 'running')) {
|
|
324
|
+
this._stopPolling()
|
|
325
|
+
}
|
|
326
|
+
} finally {
|
|
327
|
+
this._inPoll = false
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* Check the remote status of a single running task. Seam for the
|
|
333
|
+
* (not-yet-shipped) CLI status command — override or inject in tests to
|
|
334
|
+
* drive the running→completed/failed transition.
|
|
335
|
+
*
|
|
336
|
+
* @param {Object} _task - the running task
|
|
337
|
+
* @returns {Promise<{ status: 'running'|'completed'|'failed', result?: any, error?: string }>}
|
|
338
|
+
*
|
|
339
|
+
* Default: no CLI status command exists yet, so report 'running' (unknown)
|
|
340
|
+
* and let the poll keep waiting until one ships or the timeout backstop fires.
|
|
341
|
+
*/
|
|
342
|
+
async _checkRemoteStatus(_task) {
|
|
343
|
+
return { status: 'running' }
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/**
|
|
347
|
+
* Clean up timers and state.
|
|
348
|
+
*/
|
|
349
|
+
destroy() {
|
|
350
|
+
this._stopPolling()
|
|
351
|
+
// Kill any in-flight child processes
|
|
352
|
+
for (const child of this._childProcesses) {
|
|
353
|
+
try { child.kill() } catch {}
|
|
354
|
+
}
|
|
355
|
+
this._childProcesses.clear()
|
|
356
|
+
this._tasks.clear()
|
|
357
|
+
this.removeAllListeners()
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* Error thrown when web task features are not available.
|
|
363
|
+
*/
|
|
364
|
+
export class WebTaskUnavailableError extends Error {
|
|
365
|
+
constructor() {
|
|
366
|
+
super('Claude Code Web requires Claude CLI with --remote support. Update your Claude CLI to enable this feature.')
|
|
367
|
+
this.name = 'WebTaskUnavailableError'
|
|
368
|
+
this.code = 'WEB_TASK_UNAVAILABLE'
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/**
|
|
373
|
+
* Promisified execFile helper (no shell — safe from injection).
|
|
374
|
+
* @private
|
|
375
|
+
*/
|
|
376
|
+
function execFileAsync(cmd, args = [], opts = {}) {
|
|
377
|
+
return new Promise((resolve, reject) => {
|
|
378
|
+
execFile(cmd, args, { timeout: 15_000, ...opts }, (err, stdout) => {
|
|
379
|
+
if (err) return reject(err)
|
|
380
|
+
resolve(stdout)
|
|
381
|
+
})
|
|
382
|
+
})
|
|
383
|
+
}
|