@chroxy/server 0.10.0 → 0.11.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 +411 -14
- package/hooks/permission-hook.sh +253 -3
- package/package.json +8 -15
- package/sidecar/node_modules/.package-lock.json +29 -0
- package/sidecar/node_modules/ws/LICENSE +20 -0
- package/sidecar/node_modules/ws/README.md +548 -0
- package/sidecar/node_modules/ws/browser.js +8 -0
- package/sidecar/node_modules/ws/index.js +22 -0
- package/sidecar/node_modules/ws/lib/buffer-util.js +131 -0
- package/sidecar/node_modules/ws/lib/constants.js +19 -0
- package/sidecar/node_modules/ws/lib/event-target.js +292 -0
- package/sidecar/node_modules/ws/lib/extension.js +203 -0
- package/sidecar/node_modules/ws/lib/limiter.js +55 -0
- package/sidecar/node_modules/ws/lib/permessage-deflate.js +528 -0
- package/sidecar/node_modules/ws/lib/receiver.js +706 -0
- package/sidecar/node_modules/ws/lib/sender.js +602 -0
- package/sidecar/node_modules/ws/lib/stream.js +161 -0
- package/sidecar/node_modules/ws/lib/subprotocol.js +62 -0
- package/sidecar/node_modules/ws/lib/validation.js +152 -0
- package/sidecar/node_modules/ws/lib/websocket-server.js +554 -0
- package/sidecar/node_modules/ws/lib/websocket.js +1393 -0
- package/sidecar/node_modules/ws/package.json +70 -0
- package/sidecar/node_modules/ws/wrapper.mjs +21 -0
- package/src/base-session.js +151 -6
- package/src/billing-budget.js +5 -1
- package/src/binary-provenance-trust.js +95 -0
- package/src/built-in-tools/file-ops.js +5 -4
- package/src/byok-mcp-client.js +1061 -13
- package/src/byok-mcp-config.js +1201 -4
- package/src/byok-mcp-fleet.js +374 -29
- package/src/byok-mcp-oauth-store.js +327 -0
- package/src/byok-mcp-oauth.js +635 -0
- package/src/byok-mcp-trust.js +254 -37
- package/src/byok-session.js +789 -13
- package/src/byok-tool-executor.js +20 -13
- package/src/checkpoint-manager.js +33 -6
- package/src/chroxy-host-metadata.js +122 -0
- package/src/claude-stream-parser.js +56 -0
- package/src/claude-tui/pty-driver.js +54 -14
- package/src/claude-tui-session.js +106 -7
- package/src/cli/config-cmd.js +2 -2
- package/src/cli/deploy-cmd.js +1 -1
- package/src/cli/init-cmd.js +1 -1
- package/src/cli/providers-cmd.js +3 -3
- package/src/cli/schedule-cmd.js +775 -0
- package/src/cli/server-cmd.js +16 -1
- package/src/cli/service-cmd.js +39 -19
- package/src/cli/shared.js +7 -4
- package/src/cli/tokens-cmd.js +213 -0
- package/src/cli/tunnel-cmd.js +1 -1
- package/src/cli-session.js +66 -10
- package/src/cli.js +4 -0
- package/src/codex-app-server-session.js +671 -39
- package/src/codex-session.js +121 -18
- package/src/config.js +453 -27
- package/src/control-room/integrations.js +16 -10
- package/src/control-room/skills-inventory.js +6 -1
- package/src/control-room/survey.js +32 -0
- package/src/credential-store.js +256 -10
- package/src/dashboard-next/node_modules/.vite/vitest/da39a3ee5e6b4b0d3255bfef95601890afd80709/results.json +1 -0
- package/src/diagnostics.js +3 -0
- package/src/docker-sdk-session.js +16 -1
- package/src/docker-session.js +9 -0
- package/src/doctor.js +19 -8
- package/src/environments/backends/docker.js +8 -0
- package/src/environments/backends/k8s.js +9 -0
- package/src/event-ingest.js +19 -1
- package/src/event-normalizer.js +354 -19
- package/src/external-session-registry.js +0 -0
- package/src/gemini-session.js +19 -2
- package/src/github-webhook.js +188 -4
- package/src/handler-utils.js +42 -8
- package/src/handlers/checkpoint-handlers.js +126 -19
- package/src/handlers/control-room-handlers.js +48 -18
- package/src/handlers/conversation-handlers.js +127 -2
- package/src/handlers/credential-handlers.js +43 -2
- package/src/handlers/file-handlers.js +47 -3
- package/src/handlers/github-webhook-handlers.js +204 -0
- package/src/handlers/input-handlers.js +4 -0
- package/src/handlers/orchestration-handlers.js +238 -0
- package/src/handlers/scheduler-handlers.js +510 -0
- package/src/handlers/session-handlers.js +10 -2
- package/src/handlers/settings-handlers.js +510 -11
- package/src/handlers/token-handlers.js +11 -2
- package/src/http-routes.js +159 -0
- package/src/jsonl-reader.js +60 -6
- package/src/jsonl-subprocess-session.js +35 -6
- package/src/keychain.js +159 -12
- package/src/logger.js +182 -19
- package/src/notification-prefs.js +88 -6
- package/src/notifications/discord-webhook-sink.js +30 -1
- package/src/orchestration/build-manager.js +56 -0
- package/src/orchestration/decision-contract.js +245 -0
- package/src/orchestration/git-ops.js +347 -0
- package/src/orchestration/orchestration-manager.js +1295 -0
- package/src/orchestration/permission-gate.js +109 -0
- package/src/orchestration/role-prompts.js +136 -0
- package/src/orchestration/run-budget.js +81 -0
- package/src/orchestration/run-ledger.js +487 -0
- package/src/orchestration/run-model.js +205 -0
- package/src/orchestration/run-record.js +336 -0
- package/src/orchestration/run-report.js +194 -0
- package/src/orchestration/to-wire.js +201 -0
- package/src/orchestration/turn-driver.js +398 -0
- package/src/pairing.js +146 -1
- package/src/permission-audit.js +79 -3
- package/src/permission-floor.js +455 -0
- package/src/permission-manager.js +492 -40
- package/src/permission-resolver.js +55 -7
- package/src/permission-rule-store.js +411 -0
- package/src/platform.js +367 -23
- package/src/providers.js +21 -2
- package/src/schedule-parser.js +311 -0
- package/src/scheduled-task-store.js +653 -0
- package/src/scheduler.js +1029 -0
- package/src/sdk-session.js +338 -5
- package/src/server-cli/server-orchestrator.js +23 -0
- package/src/server-cli/tunnel-lifecycle-handler.js +3 -2
- package/src/server-cli.js +238 -26
- package/src/server-identity.js +28 -4
- package/src/service.js +291 -33
- package/src/session-manager.js +417 -25
- package/src/session-message-history.js +27 -21
- package/src/session-title.js +168 -0
- package/src/session-token-store.js +60 -35
- package/src/skills-usage.js +20 -2
- package/src/statusline.js +584 -0
- package/src/subagent-counter.js +9 -1
- package/src/supervisor.js +77 -27
- package/src/token-manager.js +39 -9
- package/src/transcript-tasks.js +41 -6
- package/src/tunnel/cloudflare.js +115 -8
- package/src/tunnel/index.js +4 -1
- package/src/tunnel-check.js +1 -1
- package/src/turn-tracker.js +0 -0
- package/src/usage-normalize.js +118 -0
- package/src/user-shell-session.js +98 -32
- package/src/utils/componentwise-resolver.js +205 -0
- package/src/utils/composite-key.js +45 -0
- package/src/utils/iso-datetime.js +51 -0
- package/src/utils/preflight.js +140 -28
- package/src/utils/spawn-env.js +8 -2
- package/src/utils/verify-binary.js +276 -0
- package/src/utils/verify-provenance.js +241 -0
- package/src/utils/wire-counters.js +48 -0
- package/src/worktree-gc.js +4 -1
- package/src/ws-file-ops/browser.js +42 -11
- package/src/ws-file-ops/common.js +68 -1
- package/src/ws-file-ops/git.js +221 -3
- package/src/ws-file-ops/index.js +5 -0
- package/src/ws-file-ops/memory.js +358 -0
- package/src/ws-file-ops/reader.js +124 -0
- package/src/ws-forwarding.js +44 -16
- package/src/ws-handler-context.js +19 -0
- package/src/ws-history.js +84 -11
- package/src/ws-message-handlers.js +6 -0
- package/src/ws-outbound-schemas.js +207 -0
- package/src/ws-permissions.js +285 -4
- package/src/ws-schemas.js +1 -0
- package/src/ws-server.js +301 -17
package/src/service.js
CHANGED
|
@@ -1,13 +1,17 @@
|
|
|
1
1
|
import { homedir, platform } from 'os'
|
|
2
|
-
import { join, dirname } from 'path'
|
|
2
|
+
import { join, dirname, win32 as pathWin32 } from 'path'
|
|
3
3
|
import { existsSync, readFileSync, mkdirSync, unlinkSync, readdirSync, writeFileSync, chmodSync } from 'fs'
|
|
4
|
-
import { writeFileRestricted } from './platform.js'
|
|
4
|
+
import { writeFileRestricted, isWindows } from './platform.js'
|
|
5
5
|
import { execFileSync } from 'child_process'
|
|
6
6
|
import { fileURLToPath } from 'url'
|
|
7
7
|
|
|
8
8
|
const SERVICE_LABEL = 'com.chroxy.server'
|
|
9
9
|
const DEFAULT_CONFIG_DIR = join(homedir(), '.chroxy')
|
|
10
10
|
const WRAPPER_NAME = 'service-wrapper.sh'
|
|
11
|
+
// Windows Task Scheduler autostart (#6647). The task name the daemon registers
|
|
12
|
+
// under, and the .cmd wrapper it runs (the Windows analogue of WRAPPER_NAME).
|
|
13
|
+
const WINDOWS_TASK_NAME = 'Chroxy'
|
|
14
|
+
const WINDOWS_WRAPPER_NAME = 'service-wrapper.cmd'
|
|
11
15
|
|
|
12
16
|
// Keychain coordinates for secrets resolved by the service wrapper at spawn
|
|
13
17
|
// time. The api-token pair mirrors keychain.js (service 'chroxy', account
|
|
@@ -190,11 +194,79 @@ export function getWindowsAlternatives() {
|
|
|
190
194
|
]
|
|
191
195
|
}
|
|
192
196
|
|
|
197
|
+
/**
|
|
198
|
+
* Generate the Windows `.cmd` service wrapper that Task Scheduler runs (#6647).
|
|
199
|
+
*
|
|
200
|
+
* `schtasks`' basic `/Create` flags can neither set a working directory nor
|
|
201
|
+
* capture the daemon's stdout/stderr, so — mirroring the POSIX wrapper (#5491) —
|
|
202
|
+
* a small batch file sets the cwd, prepends node + claude to PATH, and runs the
|
|
203
|
+
* daemon with output redirected to the log dir.
|
|
204
|
+
*
|
|
205
|
+
* Unlike launchd/systemd there is NO keychain to resolve here, so NOTHING
|
|
206
|
+
* sensitive is written into this file or the task action: the daemon reads its
|
|
207
|
+
* config/credentials from its own store at runtime (today plaintext under
|
|
208
|
+
* `~/.chroxy`; DPAPI-encrypted per-user storage is planned in #6644, and the
|
|
209
|
+
* wrapper runs AS the user so those creds will be reachable once it lands).
|
|
210
|
+
* Keeping secrets out of the task action is an explicit #6647 goal.
|
|
211
|
+
*
|
|
212
|
+
* @param {object} config
|
|
213
|
+
* @param {string} config.nodePath - Resolved node binary path.
|
|
214
|
+
* @param {string} config.chroxyBin - Resolved chroxy CLI entry point.
|
|
215
|
+
* @param {string} [config.claudeBin] - Resolved claude binary (its dir is added to PATH).
|
|
216
|
+
* @param {string} [config.cwd] - Working directory for the daemon.
|
|
217
|
+
* @param {string} [config.logDir] - Log directory for stdout/stderr redirection.
|
|
218
|
+
* @returns {string} Windows batch (.cmd) script.
|
|
219
|
+
*/
|
|
220
|
+
export function generateWindowsServiceWrapper(config) {
|
|
221
|
+
const {
|
|
222
|
+
nodePath,
|
|
223
|
+
chroxyBin,
|
|
224
|
+
claudeBin,
|
|
225
|
+
cwd = homedir(),
|
|
226
|
+
logDir = join(homedir(), '.chroxy', 'logs'),
|
|
227
|
+
} = config
|
|
228
|
+
|
|
229
|
+
// Use Windows path semantics regardless of host so the generated batch is
|
|
230
|
+
// correct even when this runs on a POSIX CI runner under test (#6647).
|
|
231
|
+
const pathPrepend = [pathWin32.dirname(nodePath), claudeBin ? pathWin32.dirname(claudeBin) : '']
|
|
232
|
+
.filter(Boolean)
|
|
233
|
+
.join(';')
|
|
234
|
+
const stdoutLog = pathWin32.join(logDir, 'chroxy-stdout.log')
|
|
235
|
+
const stderrLog = pathWin32.join(logDir, 'chroxy-stderr.log')
|
|
236
|
+
|
|
237
|
+
// CRLF line endings + ASCII-only (no em-dash): a .cmd is read by cmd.exe in
|
|
238
|
+
// the OEM code page, where a UTF-8 em-dash renders as garbage, and CRLF is the
|
|
239
|
+
// conventional/robust ending for a generated batch file (#6647 review).
|
|
240
|
+
// #7162: rotate oversized captures HERE, before the `>>` redirect opens a
|
|
241
|
+
// handle on them. cmd's redirect handle lacks POSIX append semantics, so
|
|
242
|
+
// the daemon cannot safely truncate these in place on Windows (a truncate
|
|
243
|
+
// under the open handle NUL-pads the gap on the next write); a rename
|
|
244
|
+
// before the handle exists is the safe equivalent. 5242880 = 5MB, matching
|
|
245
|
+
// the POSIX-side STDIO_CAPTURE_MAX_SIZE.
|
|
246
|
+
const rotateCapture = (live, old) =>
|
|
247
|
+
`if exist "${live}" for %%A in ("${live}") do if %%~zA gtr 5242880 move /y "${live}" "${old}"`
|
|
248
|
+
const lines = [
|
|
249
|
+
'@echo off',
|
|
250
|
+
'rem Chroxy service wrapper - generated by `chroxy service install` (#6647).',
|
|
251
|
+
'rem Runs the daemon under Windows Task Scheduler. Regenerated on every',
|
|
252
|
+
'rem `service install`; edits are lost.',
|
|
253
|
+
`set "PATH=${pathPrepend};%PATH%"`,
|
|
254
|
+
'set "CHROXY_DAEMON=1"',
|
|
255
|
+
`cd /d "${cwd}"`,
|
|
256
|
+
rotateCapture(stdoutLog, pathWin32.join(logDir, 'chroxy-stdout.old.log')),
|
|
257
|
+
rotateCapture(stderrLog, pathWin32.join(logDir, 'chroxy-stderr.old.log')),
|
|
258
|
+
`"${nodePath}" "${chroxyBin}" start >> "${stdoutLog}" 2>> "${stderrLog}"`,
|
|
259
|
+
]
|
|
260
|
+
return lines.join('\r\n') + '\r\n'
|
|
261
|
+
}
|
|
262
|
+
|
|
193
263
|
/**
|
|
194
264
|
* Search common locations for Node 22, verify the version, and return the path.
|
|
195
265
|
* Throws with a helpful message if not found.
|
|
196
266
|
*/
|
|
197
267
|
export function resolveNode22Path() {
|
|
268
|
+
if (isWindows) return resolveWindowsNodePath()
|
|
269
|
+
|
|
198
270
|
const candidates = []
|
|
199
271
|
|
|
200
272
|
// Homebrew (Apple Silicon)
|
|
@@ -253,6 +325,38 @@ export function resolveNode22Path() {
|
|
|
253
325
|
)
|
|
254
326
|
}
|
|
255
327
|
|
|
328
|
+
/**
|
|
329
|
+
* Windows node resolution (#6647). Unlike POSIX — where the daemon is pinned to a
|
|
330
|
+
* Homebrew/nvm `node@22` — Windows has no pinning convention, so accept any Node
|
|
331
|
+
* >= 22 (the documented minimum). The installer already runs under Node, so if
|
|
332
|
+
* that interpreter satisfies the minimum, reuse its absolute path; otherwise fall
|
|
333
|
+
* back to `where node`.
|
|
334
|
+
*/
|
|
335
|
+
function resolveWindowsNodePath() {
|
|
336
|
+
const major = (v) => parseInt(String(v).replace(/^v/, '').split('.')[0], 10)
|
|
337
|
+
|
|
338
|
+
const running = process.versions?.node
|
|
339
|
+
if (running && major(running) >= 22) return process.execPath
|
|
340
|
+
|
|
341
|
+
try {
|
|
342
|
+
const out = execFileSync('where', ['node'], { encoding: 'utf-8' })
|
|
343
|
+
const first = out.split(/\r?\n/).map((s) => s.trim()).find(Boolean)
|
|
344
|
+
if (first && existsSync(first)) {
|
|
345
|
+
const version = execFileSync(first, ['--version'], { encoding: 'utf-8' }).trim()
|
|
346
|
+
if (major(version) >= 22) return first
|
|
347
|
+
}
|
|
348
|
+
} catch {
|
|
349
|
+
// `where` missing or node not on PATH — fall through to the error below.
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
throw new Error(
|
|
353
|
+
'Could not find Node.js >= 22 (the minimum supported version). Install it via:\n' +
|
|
354
|
+
' winget install OpenJS.NodeJS.LTS\n' +
|
|
355
|
+
' or nvm-windows: nvm install 22\n' +
|
|
356
|
+
'then re-run `chroxy service install`.'
|
|
357
|
+
)
|
|
358
|
+
}
|
|
359
|
+
|
|
256
360
|
/**
|
|
257
361
|
* Find the chroxy CLI entry point.
|
|
258
362
|
* First checks if we're running from the monorepo source, then falls back to `which chroxy`.
|
|
@@ -265,11 +369,13 @@ export function resolveChroxyBin() {
|
|
|
265
369
|
return monorepoCliPath
|
|
266
370
|
}
|
|
267
371
|
|
|
268
|
-
// Fall back to globally installed chroxy
|
|
372
|
+
// Fall back to globally installed chroxy (`where` on Windows, `which` on POSIX;
|
|
373
|
+
// `where` can print several lines, so take the first resolvable one — #6647).
|
|
269
374
|
try {
|
|
270
|
-
const
|
|
271
|
-
|
|
272
|
-
|
|
375
|
+
const out = execFileSync(isWindows ? 'where' : 'which', ['chroxy'], { encoding: 'utf-8' })
|
|
376
|
+
const resolved = out.split(/\r?\n/).map((s) => s.trim()).find((p) => p && existsSync(p))
|
|
377
|
+
if (resolved) {
|
|
378
|
+
return resolved
|
|
273
379
|
}
|
|
274
380
|
} catch {
|
|
275
381
|
// not found
|
|
@@ -300,9 +406,12 @@ export function resolveChroxyBin() {
|
|
|
300
406
|
*/
|
|
301
407
|
export function resolveClaudeBin(options = {}) {
|
|
302
408
|
const which = options._which || execFileSync
|
|
409
|
+
// `where` on Windows (it can print several lines — take the first resolvable
|
|
410
|
+
// one), `which` on POSIX (#6647).
|
|
303
411
|
try {
|
|
304
|
-
const
|
|
305
|
-
|
|
412
|
+
const out = which(isWindows ? 'where' : 'which', ['claude'], { encoding: 'utf-8' })
|
|
413
|
+
const resolved = String(out).split(/\r?\n/).map((s) => s.trim()).find((p) => p && existsSync(p))
|
|
414
|
+
if (resolved) {
|
|
306
415
|
return resolved
|
|
307
416
|
}
|
|
308
417
|
} catch {
|
|
@@ -466,12 +575,7 @@ export function installService(config) {
|
|
|
466
575
|
const paths = getServicePaths(plat)
|
|
467
576
|
|
|
468
577
|
if (plat === 'win32') {
|
|
469
|
-
|
|
470
|
-
return {
|
|
471
|
-
installed: false,
|
|
472
|
-
message: 'Windows does not support native service installation via chroxy. Use one of the alternatives below to run Chroxy at startup.',
|
|
473
|
-
alternatives,
|
|
474
|
-
}
|
|
578
|
+
return installWindowsService(config, paths)
|
|
475
579
|
}
|
|
476
580
|
|
|
477
581
|
const servicePath = config._servicePath || (plat === 'darwin' ? paths.plistPath : paths.unitPath)
|
|
@@ -550,13 +654,134 @@ export function installService(config) {
|
|
|
550
654
|
}, stateDir)
|
|
551
655
|
}
|
|
552
656
|
|
|
657
|
+
/**
|
|
658
|
+
* Register Chroxy as a Windows autostart via Task Scheduler (#6647).
|
|
659
|
+
*
|
|
660
|
+
* Writes the `.cmd` wrapper, then `schtasks /Create /TN Chroxy /TR <wrapper>
|
|
661
|
+
* /SC ONLOGON /RL HIGHEST /IT /F`:
|
|
662
|
+
* - `/SC ONLOGON` — start when the user logs on (NOT `/SC ONSTART`).
|
|
663
|
+
* - `/RL HIGHEST` — run with the user's highest privileges.
|
|
664
|
+
* - `/IT` — interactive token: run only while the user is logged on,
|
|
665
|
+
* using their session token, so NO stored password is
|
|
666
|
+
* required (schtasks would otherwise prompt for one).
|
|
667
|
+
* - `/F` — overwrite an existing task (idempotent reinstall).
|
|
668
|
+
*
|
|
669
|
+
* ONLOGON + `/IT` (vs `/SC ONSTART` with `/RU SYSTEM`) is deliberate: Chroxy is a
|
|
670
|
+
* per-user dev daemon. Running at boot as SYSTEM would start it before anyone
|
|
671
|
+
* logs on, as SYSTEM rather than the user — no interactive session, and (once
|
|
672
|
+
* per-user credential encryption lands, DPAPI, #6644) no access to the user's
|
|
673
|
+
* credentials — wrong for this workload. The trade-off (daemon starts at logon,
|
|
674
|
+
* not boot) is documented for the user in `service-cmd.js`.
|
|
675
|
+
*
|
|
676
|
+
* NOTE: creating an ONLOGON task requires an elevated (Administrator) token —
|
|
677
|
+
* a non-elevated `schtasks /Create /SC ONLOGON` fails with "Access is denied."
|
|
678
|
+
* The catch below surfaces that stderr and tells the user to re-run elevated.
|
|
679
|
+
*
|
|
680
|
+
* @param {object} config - Same shape as installService's config.
|
|
681
|
+
* @param {object} paths - getServicePaths('win32') result.
|
|
682
|
+
* @returns {{ installed: true, platform: 'win32', taskName: string, wrapperPath: string }}
|
|
683
|
+
*/
|
|
684
|
+
function installWindowsService(config, paths) {
|
|
685
|
+
const stateDir = config._stateDir || DEFAULT_CONFIG_DIR
|
|
686
|
+
const logDir = config._logDir || paths.logDir
|
|
687
|
+
const wrapperPath = config._wrapperPath || join(stateDir, WINDOWS_WRAPPER_NAME)
|
|
688
|
+
const taskName = config._taskName || WINDOWS_TASK_NAME
|
|
689
|
+
const exec = config._exec || execFileSync
|
|
690
|
+
const cwd = config.cwd || homedir()
|
|
691
|
+
|
|
692
|
+
if (!existsSync(logDir)) {
|
|
693
|
+
mkdirSync(logDir, { recursive: true })
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
// Write the wrapper Task Scheduler runs (sets cwd + PATH + log redirection).
|
|
697
|
+
const wrapperContent = generateWindowsServiceWrapper({
|
|
698
|
+
nodePath: config.nodePath,
|
|
699
|
+
chroxyBin: config.chroxyBin,
|
|
700
|
+
claudeBin: config.claudeBin,
|
|
701
|
+
cwd,
|
|
702
|
+
logDir,
|
|
703
|
+
})
|
|
704
|
+
writeServiceWrapper(wrapperPath, wrapperContent)
|
|
705
|
+
|
|
706
|
+
// Register the scheduled task. `/TR` is quoted so a wrapper path with spaces
|
|
707
|
+
// runs correctly; it holds only the wrapper path — never a secret (#6647).
|
|
708
|
+
if (!config._skipRegister) {
|
|
709
|
+
try {
|
|
710
|
+
exec('schtasks', [
|
|
711
|
+
'/Create',
|
|
712
|
+
'/TN', taskName,
|
|
713
|
+
'/TR', `"${wrapperPath}"`,
|
|
714
|
+
'/SC', 'ONLOGON',
|
|
715
|
+
'/RL', 'HIGHEST',
|
|
716
|
+
'/IT',
|
|
717
|
+
'/F',
|
|
718
|
+
], { stdio: 'pipe' })
|
|
719
|
+
} catch (err) {
|
|
720
|
+
// `schtasks` runs with piped stdio, so on failure execFileSync throws a
|
|
721
|
+
// generic "Command failed" and drops the real reason. Surface schtasks'
|
|
722
|
+
// stderr, and — since creating an ONLOGON task requires elevation, so a
|
|
723
|
+
// non-elevated install fails with "Access is denied." — add the fix (#6647).
|
|
724
|
+
const detail = String(err?.stderr || '').trim()
|
|
725
|
+
const denied = /access is denied/i.test(detail)
|
|
726
|
+
throw new Error(
|
|
727
|
+
`schtasks could not register the Chroxy task${detail ? `: ${detail}` : ` (${err.message})`}` +
|
|
728
|
+
(denied
|
|
729
|
+
? '\nRegistering a logon-triggered task requires elevation — run `chroxy service install` from an Administrator terminal.'
|
|
730
|
+
: ''),
|
|
731
|
+
)
|
|
732
|
+
}
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
saveServiceState({
|
|
736
|
+
installedAt: new Date().toISOString(),
|
|
737
|
+
platform: 'win32',
|
|
738
|
+
taskName,
|
|
739
|
+
// servicePath mirrors the wrapper so the generic uninstall cleanup removes it.
|
|
740
|
+
servicePath: wrapperPath,
|
|
741
|
+
wrapperPath,
|
|
742
|
+
nodePath: config.nodePath,
|
|
743
|
+
chroxyBin: config.chroxyBin,
|
|
744
|
+
claudeBin: config.claudeBin,
|
|
745
|
+
cwd,
|
|
746
|
+
// ONLOGON always starts the daemon at login; there is no separate toggle.
|
|
747
|
+
startAtLogin: true,
|
|
748
|
+
}, stateDir)
|
|
749
|
+
|
|
750
|
+
return { installed: true, platform: 'win32', taskName, wrapperPath }
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
/**
|
|
754
|
+
* Query the Windows scheduled task's registration + run state via
|
|
755
|
+
* `schtasks /Query /TN Chroxy /V /FO LIST` (#6647). Best-effort: returns
|
|
756
|
+
* `{ registered: false }` when the task is absent or schtasks is unavailable.
|
|
757
|
+
*
|
|
758
|
+
* @param {object} [options]
|
|
759
|
+
* @param {(cmd: string, args: string[], opts: object) => string} [options._exec]
|
|
760
|
+
* @param {string} [options._taskName]
|
|
761
|
+
* @returns {{ registered: boolean, status: string|null }}
|
|
762
|
+
*/
|
|
763
|
+
export function getWindowsTaskStatus(options = {}) {
|
|
764
|
+
const exec = options._exec || execFileSync
|
|
765
|
+
const taskName = options._taskName || WINDOWS_TASK_NAME
|
|
766
|
+
try {
|
|
767
|
+
const out = String(exec('schtasks', ['/Query', '/TN', taskName, '/V', '/FO', 'LIST'], { encoding: 'utf-8' }))
|
|
768
|
+
// The `/V /FO LIST` output has a `Status: Running|Ready|Disabled` line.
|
|
769
|
+
const m = out.match(/^\s*Status:\s*(.+?)\s*$/mi)
|
|
770
|
+
return { registered: true, status: m ? m[1].trim() : null }
|
|
771
|
+
} catch {
|
|
772
|
+
return { registered: false, status: null }
|
|
773
|
+
}
|
|
774
|
+
}
|
|
775
|
+
|
|
553
776
|
/**
|
|
554
777
|
* Uninstall the Chroxy system daemon.
|
|
555
778
|
* Stops the service if running, removes the service file and state.
|
|
556
779
|
*
|
|
557
780
|
* @param {object} [options]
|
|
558
781
|
* @param {string} [options._stateDir] - Override state directory (testing)
|
|
559
|
-
* @param {boolean} [options._skipUnregister] - Skip launchctl/systemctl unregistration (testing)
|
|
782
|
+
* @param {boolean} [options._skipUnregister] - Skip launchctl/systemctl/schtasks unregistration (testing)
|
|
783
|
+
* @param {(cmd: string, args: string[], opts: object) => void} [options._exec]
|
|
784
|
+
* Injectable exec for testing (defaults to execFileSync).
|
|
560
785
|
*/
|
|
561
786
|
export function uninstallService(options = {}) {
|
|
562
787
|
const stateDir = options._stateDir || DEFAULT_CONFIG_DIR
|
|
@@ -568,11 +793,14 @@ export function uninstallService(options = {}) {
|
|
|
568
793
|
|
|
569
794
|
// Unregister from system (unless testing)
|
|
570
795
|
if (!options._skipUnregister) {
|
|
796
|
+
const exec = options._exec || execFileSync
|
|
571
797
|
try {
|
|
572
798
|
if (state.platform === 'darwin') {
|
|
573
|
-
|
|
799
|
+
exec('launchctl', ['bootout', `gui/${process.getuid()}/${SERVICE_LABEL}`], { stdio: 'ignore' })
|
|
800
|
+
} else if (state.platform === 'win32') {
|
|
801
|
+
exec('schtasks', ['/Delete', '/TN', state.taskName || WINDOWS_TASK_NAME, '/F'], { stdio: 'ignore' })
|
|
574
802
|
} else {
|
|
575
|
-
|
|
803
|
+
exec('systemctl', ['--user', 'disable', '--now', 'chroxy.service'], { stdio: 'ignore' })
|
|
576
804
|
}
|
|
577
805
|
} catch {
|
|
578
806
|
// Service may already be stopped/unloaded — continue cleanup
|
|
@@ -614,11 +842,12 @@ export function startService(options = {}) {
|
|
|
614
842
|
const exec = options._exec || execFileSync
|
|
615
843
|
|
|
616
844
|
if (plat === 'win32') {
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
845
|
+
// Run the registered scheduled task now (#6647).
|
|
846
|
+
const taskName = options._taskName || WINDOWS_TASK_NAME
|
|
847
|
+
if (!options._skipExec) {
|
|
848
|
+
exec('schtasks', ['/Run', '/TN', taskName], { stdio: 'pipe' })
|
|
621
849
|
}
|
|
850
|
+
return { started: true, message: 'Service started' }
|
|
622
851
|
}
|
|
623
852
|
|
|
624
853
|
if (!options._skipExec) {
|
|
@@ -702,19 +931,22 @@ function bootstrapLaunchd(plistPath, exec) {
|
|
|
702
931
|
export function stopService(options = {}) {
|
|
703
932
|
const plat = options._platform || platform()
|
|
704
933
|
const paths = getServicePaths(plat)
|
|
934
|
+
const exec = options._exec || execFileSync
|
|
705
935
|
|
|
706
936
|
if (plat === 'win32') {
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
937
|
+
// End the running scheduled task's action (#6647).
|
|
938
|
+
const taskName = options._taskName || WINDOWS_TASK_NAME
|
|
939
|
+
if (!options._skipExec) {
|
|
940
|
+
exec('schtasks', ['/End', '/TN', taskName], { stdio: 'pipe' })
|
|
710
941
|
}
|
|
942
|
+
return { stopped: true, message: 'Service stopped' }
|
|
711
943
|
}
|
|
712
944
|
|
|
713
945
|
if (!options._skipExec) {
|
|
714
946
|
if (paths.type === 'launchd') {
|
|
715
|
-
|
|
947
|
+
exec('launchctl', ['bootout', `gui/${process.getuid()}/${SERVICE_LABEL}`], { stdio: 'pipe' })
|
|
716
948
|
} else {
|
|
717
|
-
|
|
949
|
+
exec('systemctl', ['--user', 'stop', 'chroxy.service'], { stdio: 'pipe' })
|
|
718
950
|
}
|
|
719
951
|
}
|
|
720
952
|
|
|
@@ -819,17 +1051,43 @@ export async function getFullServiceStatus(options = {}) {
|
|
|
819
1051
|
}
|
|
820
1052
|
}
|
|
821
1053
|
|
|
822
|
-
// Read recent log lines
|
|
823
|
-
|
|
824
|
-
|
|
1054
|
+
// Read recent log lines. #7162: three files carry the daemon's recent
|
|
1055
|
+
// activity and NONE alone is sufficient — chroxy.log is written only by
|
|
1056
|
+
// the forked server child; the supervisor's lifecycle lines (crash loops,
|
|
1057
|
+
// restart backoff) exist only in the stdout capture; and the supervisor's
|
|
1058
|
+
// warn/error plus hard-crash traces land only in the stderr capture (the
|
|
1059
|
+
// supervisor process has no file log). Merge all three tails by timestamp
|
|
1060
|
+
// so a crash-looping daemon's restart lines and last error surface next to
|
|
1061
|
+
// the child's last words, and stale files lose to fresher lines naturally.
|
|
1062
|
+
const tails = []
|
|
1063
|
+
for (const name of ['chroxy.log', 'chroxy-stdout.log', 'chroxy-stderr.log']) {
|
|
1064
|
+
const logFile = join(configDir, 'logs', name)
|
|
1065
|
+
if (!existsSync(logFile)) continue
|
|
825
1066
|
try {
|
|
826
|
-
const
|
|
827
|
-
|
|
828
|
-
|
|
1067
|
+
const lines = readFileSync(logFile, 'utf-8').trimEnd().split('\n').slice(-10)
|
|
1068
|
+
let last = 0
|
|
1069
|
+
for (const text of lines) {
|
|
1070
|
+
if (!text) continue
|
|
1071
|
+
// Plain format leads with the ISO stamp; JSON mode carries "ts".
|
|
1072
|
+
// Unstamped lines (crash traces, banners) inherit the preceding
|
|
1073
|
+
// stamped line's time so multi-line output stays together.
|
|
1074
|
+
const m = /^(\d{4}-\d{2}-\d{2}T[\d:.]+Z)/.exec(text)
|
|
1075
|
+
|| /^\{"ts":"(\d{4}-[^"]+)"/.exec(text)
|
|
1076
|
+
if (m) {
|
|
1077
|
+
const parsed = Date.parse(m[1])
|
|
1078
|
+
if (!Number.isNaN(parsed)) last = parsed
|
|
1079
|
+
}
|
|
1080
|
+
tails.push({ ts: last, text })
|
|
1081
|
+
}
|
|
829
1082
|
} catch {
|
|
830
|
-
//
|
|
1083
|
+
// Unreadable candidate — surface whatever the other file has
|
|
831
1084
|
}
|
|
832
1085
|
}
|
|
1086
|
+
if (tails.length > 0) {
|
|
1087
|
+
// Array.prototype.sort is stable: equal timestamps keep push order.
|
|
1088
|
+
tails.sort((a, b) => a.ts - b.ts)
|
|
1089
|
+
result.recentLogs = tails.slice(-5).map(t => t.text)
|
|
1090
|
+
}
|
|
833
1091
|
|
|
834
1092
|
return result
|
|
835
1093
|
}
|