@chroxy/server 0.11.0 → 0.11.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CONFIG.md +206 -18
- package/README.md +2 -2
- package/hooks/claude-native-route-check.mjs +17 -0
- package/hooks/permission-hook.sh +166 -11
- package/package.json +16 -12
- package/sidecar/Dockerfile +1 -1
- package/sidecar/PROTOCOL.md +9 -1
- package/sidecar/agent.js +102 -10
- package/sidecar/package-lock.json +3 -3
- package/src/acp-config.js +206 -0
- package/src/acp-session.js +958 -0
- package/src/activity-registry.js +110 -5
- package/src/agent-connections.js +304 -0
- package/src/agent-control/client.js +1645 -0
- package/src/agent-control/events.js +551 -0
- package/src/agent-control/local-connection.js +293 -0
- package/src/agent-control/mcp-server.js +547 -0
- package/src/anthropic-compatible-config.js +30 -4
- package/src/anthropic-compatible-session.js +15 -6
- package/src/anthropic-gemini-shim.js +444 -0
- package/src/anthropic-gemini-translate.js +1030 -0
- package/src/anthropic-openai-shim.js +0 -1
- package/src/archive-continuation.js +164 -0
- package/src/assistant-response.js +24 -0
- package/src/auth-probes.js +120 -13
- package/src/background-shell-tracker.js +27 -0
- package/src/base-session.js +1520 -36
- package/src/bearer-recheck.js +49 -0
- package/src/billing-budget.js +19 -0
- package/src/billing-class.js +40 -7
- package/src/binary-provenance-trust.js +11 -5
- package/src/bound-client-policy.js +291 -0
- package/src/built-in-tools/bash-exec.js +46 -7
- package/src/built-in-tools/file-ops.js +69 -5
- package/src/built-in-tools/tool-transforms.js +1113 -13
- package/src/byok-compose-state.js +6 -9
- package/src/byok-credentials.js +2 -3
- package/src/byok-mcp-client.js +92 -7
- package/src/byok-mcp-config.js +633 -122
- package/src/byok-mcp-fleet.js +49 -7
- package/src/byok-mcp-oauth-store.js +18 -16
- package/src/byok-mcp-trust.js +37 -23
- package/src/byok-session.js +707 -179
- package/src/byok-tool-executor.js +2742 -26
- package/src/byok-tools.js +9 -2
- package/src/channels/chroxy-channel-server.js +2 -3
- package/src/checkpoint-manager.js +64 -46
- package/src/claude-channel-session.js +16 -10
- package/src/claude-model-catalog.js +131 -16
- package/src/claude-model-refresh.js +385 -0
- package/src/claude-model-request.js +103 -0
- package/src/claude-setting-sources.js +74 -0
- package/src/claude-stream-parser.js +53 -3
- package/src/claude-tui/diagnostic-tail.js +83 -0
- package/src/claude-tui/form-driver.js +44 -7
- package/src/claude-tui/model-error.js +41 -0
- package/src/claude-tui/pty-driver.js +215 -88
- package/src/claude-tui/sanitize-question-payload.js +109 -0
- package/src/claude-tui/usage-limit.js +239 -0
- package/src/claude-tui-session.js +3563 -284
- package/src/claude-tui-tool-response.js +300 -0
- package/src/cli/agent-cmd.js +236 -0
- package/src/cli/agent-control-cmd.js +65 -0
- package/src/cli/config-cmd.js +4 -4
- package/src/cli/config-dir-cmd.js +234 -0
- package/src/cli/connection-state.js +54 -0
- package/src/cli/deploy-cmd.js +45 -10
- package/src/cli/doctor-cmd.js +7 -3
- package/src/cli/freshness-cmd.js +53 -0
- package/src/cli/init-cmd.js +15 -16
- package/src/cli/pages-cmd.js +6 -4
- package/src/cli/pair-code-cmd.js +7 -6
- package/src/cli/pair-discord-cmd.js +7 -6
- package/src/cli/providers-cmd.js +3 -3
- package/src/cli/schedule-cmd.js +91 -40
- package/src/cli/server-cmd.js +54 -1
- package/src/cli/service-cmd.js +33 -10
- package/src/cli/session-cmd.js +227 -74
- package/src/cli/shared.js +162 -11
- package/src/cli/shell-cmd.js +7 -4
- package/src/cli/status-cmd.js +19 -5
- package/src/cli/tokens-cmd.js +4 -5
- package/src/cli/tunnel-cmd.js +255 -24
- package/src/cli/worktree-gc-cmd.js +2 -2
- package/src/cli-session.js +1250 -126
- package/src/cli.js +8 -0
- package/src/codex-app-server-client.js +36 -3
- package/src/codex-app-server-session.js +1517 -81
- package/src/codex-bundled-client.js +66 -0
- package/src/codex-error-text.js +114 -0
- package/src/codex-model-catalog.js +548 -0
- package/src/codex-model-rejections.js +87 -0
- package/src/codex-native-resume.js +104 -0
- package/src/codex-protocol-capabilities.js +188 -0
- package/src/codex-session.js +450 -27
- package/src/config-dir-migration.js +449 -0
- package/src/config-dir.js +108 -0
- package/src/config.js +306 -26
- package/src/connection-authority.js +48 -0
- package/src/connection-info.js +78 -23
- package/src/connection-locality.js +60 -0
- package/src/container-liveness-monitor.js +189 -0
- package/src/continuation-metadata-worker.js +17 -0
- package/src/continuations.js +227 -0
- package/src/control-room/host-prune.js +5 -0
- package/src/control-room/integrations.js +7 -20
- package/src/control-room/skills-inventory.js +2 -2
- package/src/conversation-scanner.js +42 -39
- package/src/conversation-scope.js +59 -39
- package/src/conversation-search.js +28 -39
- package/src/credential-store.js +23 -19
- package/src/daemon-commit.js +92 -0
- package/src/daemon-idle-state.js +171 -0
- package/src/daemon-update-status.js +314 -0
- package/src/dashboard-build.js +70 -0
- package/src/declared-status.js +90 -0
- package/src/deepseek-credentials.js +2 -3
- package/src/dev-preview.js +170 -182
- package/src/device-preferences.js +2 -2
- package/src/diff-parser.js +87 -0
- package/src/discord-credentials.js +2 -3
- package/src/docker-byok-pool.js +3 -1
- package/src/docker-byok-session.js +678 -78
- package/src/docker-image-allowlist.js +23 -1
- package/src/docker-sdk-session.js +172 -3
- package/src/docker-session.js +294 -11
- package/src/doctor.js +781 -64
- package/src/durable-write.js +35 -0
- package/src/edited-input.js +264 -0
- package/src/environment-manager.js +175 -36
- package/src/environments/authority.js +101 -0
- package/src/environments/backends/container-claude-spawn.js +94 -0
- package/src/environments/backends/docker.js +31 -10
- package/src/environments/backends/k8s.js +49 -24
- package/src/environments/destroy-with-sessions.js +142 -0
- package/src/event-ingest.js +118 -13
- package/src/event-normalizer.js +186 -235
- package/src/folder-trust-wire.js +137 -0
- package/src/folder-trust-worker.js +17 -0
- package/src/folder-trust.js +1572 -0
- package/src/freshness/activity-meter.js +101 -0
- package/src/freshness/adapter.js +199 -0
- package/src/freshness/checks.js +239 -0
- package/src/freshness/config.js +98 -0
- package/src/freshness/core.js +185 -0
- package/src/freshness/derive.js +680 -0
- package/src/freshness/exchange.js +305 -0
- package/src/freshness/file-ledger.js +400 -0
- package/src/freshness/grade.js +81 -0
- package/src/freshness/hash.js +45 -0
- package/src/freshness/inbox.js +163 -0
- package/src/freshness/index.js +20 -0
- package/src/freshness/investigation.js +474 -0
- package/src/freshness/keys.js +177 -0
- package/src/freshness/ledger.js +122 -0
- package/src/freshness/outbox.js +131 -0
- package/src/freshness/policy.js +599 -0
- package/src/freshness/queue.js +843 -0
- package/src/freshness/read-errors.js +23 -0
- package/src/freshness/render.js +153 -0
- package/src/freshness/runtime.js +113 -0
- package/src/freshness/session-registry-adapter.js +161 -0
- package/src/freshness/state-file-adapter.js +167 -0
- package/src/freshness-runner/build-info.js +139 -0
- package/src/freshness-runner/cli.js +105 -0
- package/src/freshness-runner/run-tick.js +109 -0
- package/src/freshness-runner/stamp.js +57 -0
- package/src/freshness-sources/claude-transcript-adapter.js +211 -0
- package/src/freshness-sources/codex-transcript-adapter.js +33 -0
- package/src/freshness-sources/index.js +8 -0
- package/src/gemini-byok-session.js +292 -0
- package/src/gemini-credentials.js +67 -0
- package/src/gemini-key-safety.js +246 -0
- package/src/gemini-models.js +386 -0
- package/src/gemini-session.js +81 -5
- package/src/handler-utils.js +540 -80
- package/src/handlers/checkpoint-handlers.js +47 -4
- package/src/handlers/control-room-handlers.js +51 -11
- package/src/handlers/conversation-handlers.js +469 -56
- package/src/handlers/daemon-update-handlers.js +81 -0
- package/src/handlers/evaluator-handlers.js +14 -1
- package/src/handlers/feature-handlers.js +186 -69
- package/src/handlers/file-handlers.js +57 -13
- package/src/handlers/folder-trust-handlers.js +90 -0
- package/src/handlers/input-handlers.js +714 -48
- package/src/handlers/orchestration-handlers.js +3 -3
- package/src/handlers/principal-admin-handlers.js +229 -0
- package/src/handlers/principal-handlers.js +262 -0
- package/src/handlers/scheduler-handlers.js +92 -18
- package/src/handlers/session-handlers.js +551 -20
- package/src/handlers/session-pr-status-handlers.js +283 -0
- package/src/handlers/session-pr-threads-handlers.js +247 -0
- package/src/handlers/settings-handlers.js +372 -50
- package/src/handlers/skills-handlers.js +20 -4
- package/src/handlers/summarize-handlers.js +32 -1
- package/src/handlers/survey-throttle.js +380 -0
- package/src/health-proof.js +40 -0
- package/src/history-page.js +112 -0
- package/src/http-routes.js +127 -8
- package/src/ide/search.js +24 -6
- package/src/ide/symbols.js +20 -6
- package/src/input-context.js +117 -0
- package/src/instruction-policy-compiler.js +229 -0
- package/src/instruction-policy.js +250 -0
- package/src/jsonl-reader.js +378 -91
- package/src/jsonl-subprocess-session.js +96 -9
- package/src/keychain.js +1 -1
- package/src/logger.js +166 -30
- package/src/mailbox-route.js +113 -22
- package/src/message-wire.js +386 -0
- package/src/model-discovery.js +43 -3
- package/src/models.js +1276 -174
- package/src/notification-prefs.js +27 -4
- package/src/notifications/discord-billing-sink.js +22 -5
- package/src/notifications/discord-ci-sink.js +243 -0
- package/src/notifications/discord-webhook-client.js +71 -0
- package/src/notifications/discord-webhook-sink.js +64 -23
- package/src/notifications/expo-push-sink.js +85 -6
- package/src/notifications/ready-body.js +16 -5
- package/src/ollama-session.js +34 -2
- package/src/orchestration/build-manager.js +9 -2
- package/src/orchestration/git-ops.js +41 -7
- package/src/orchestration/orchestration-manager.js +112 -36
- package/src/orchestration/permission-gate.js +17 -2
- package/src/orchestration/run-model.js +2 -1
- package/src/orchestration/run-record.js +11 -0
- package/src/orchestration/turn-driver.js +185 -43
- package/src/orphan-reaper.js +470 -0
- package/src/outgoing-policy.js +189 -0
- package/src/pairing.js +92 -13
- package/src/path-hash-trust-ledger.js +642 -25
- package/src/permission-deadlines.js +82 -0
- package/src/permission-floor.js +1410 -43
- package/src/permission-hook.js +180 -69
- package/src/permission-manager.js +737 -89
- package/src/permission-mode-support.js +35 -0
- package/src/permission-resolved-by.js +89 -0
- package/src/permission-resolver.js +126 -8
- package/src/permission-scope.js +43 -0
- package/src/platform.js +315 -47
- package/src/preview-ingress.js +54 -0
- package/src/preview-target.js +213 -0
- package/src/principal-audit.js +189 -0
- package/src/principal-binding.js +25 -0
- package/src/principal-brakes.js +21 -0
- package/src/principal-effect.js +101 -0
- package/src/principal-journal.js +905 -0
- package/src/principal-kill-switch.js +183 -0
- package/src/principal-label.js +5 -0
- package/src/principal-pending-permissions.js +177 -0
- package/src/principal-policy.js +1094 -0
- package/src/principal-sessions-snapshot.js +208 -0
- package/src/principal-store.js +488 -0
- package/src/principal-subscriptions.js +283 -0
- package/src/prompt-evaluator.js +34 -4
- package/src/provider-customization.js +180 -0
- package/src/providers.js +209 -4
- package/src/push.js +30 -4
- package/src/question-route-map.js +130 -0
- package/src/redaction.js +531 -20
- package/src/roster-broadcast.js +186 -0
- package/src/scheduled-task-store.js +330 -26
- package/src/scheduler.js +368 -11
- package/src/sdk-session.js +2114 -188
- package/src/server-cli/preview-shutdown.js +22 -0
- package/src/server-cli/push-notification-handler.js +17 -3
- package/src/server-cli/server-orchestrator.js +28 -5
- package/src/server-cli-child.js +33 -18
- package/src/server-cli.js +553 -38
- package/src/server-identity.js +15 -11
- package/src/service.js +396 -25
- package/src/session-archive-limits.js +15 -0
- package/src/session-archive.js +1029 -0
- package/src/session-busy-state.js +72 -0
- package/src/session-ci-watcher.js +1099 -0
- package/src/session-context.js +7 -2
- package/src/session-manager.js +4062 -312
- package/src/session-message-history.js +841 -16
- package/src/session-name-allocator.js +685 -0
- package/src/session-pr-status.js +654 -0
- package/src/session-pr-threads.js +271 -0
- package/src/session-preset-trust.js +3 -3
- package/src/session-preset.js +10 -5
- package/src/session-state-persistence.js +94 -17
- package/src/session-timeout-manager.js +20 -0
- package/src/session-token-store.js +17 -8
- package/src/session-wake.js +240 -0
- package/src/shell-approval-info.js +2 -2
- package/src/shell-approval-store.js +3 -1
- package/src/skills-loader.js +7 -4
- package/src/skills-manager.js +2 -2
- package/src/skills-trust.js +95 -13
- package/src/skills-usage.js +2 -3
- package/src/snapshots-store.js +2 -3
- package/src/statusline.js +100 -29
- package/src/summarize-session.js +53 -4
- package/src/supervisor.js +22 -11
- package/src/sweep-stale-provider-dirs.js +95 -0
- package/src/thinking-level-wire.js +39 -0
- package/src/thinking-stream.js +179 -0
- package/src/tool-result.js +61 -0
- package/src/transcript-sources.js +109 -0
- package/src/transcript-tasks.js +372 -25
- package/src/trusted-file-read.js +302 -0
- package/src/tunnel/base.js +0 -8
- package/src/tunnel/cloudflare.js +34 -31
- package/src/tunnel/index.js +9 -0
- package/src/tunnel/preview.js +64 -0
- package/src/turn-outcome.js +140 -0
- package/src/usage-normalize.js +9 -5
- package/src/user-shell-session.js +59 -5
- package/src/utils/agent-sdk-version.js +107 -0
- package/src/utils/argv-safety.js +770 -0
- package/src/utils/auth-cause.js +60 -0
- package/src/utils/binary-package-manifest.js +639 -0
- package/src/utils/binary-version.js +224 -0
- package/src/utils/bounded-regex-worker.js +29 -0
- package/src/utils/bounded-regex.js +162 -0
- package/src/utils/claude-binary.js +43 -0
- package/src/utils/claude-config-dir.js +245 -0
- package/src/utils/claude-login-command.js +16 -0
- package/src/utils/claude-native-route.js +81 -0
- package/src/utils/componentwise-resolver.js +133 -37
- package/src/utils/deploy-control-files.js +87 -0
- package/src/utils/exec-failure-reason.js +21 -0
- package/src/utils/git-repo.js +42 -0
- package/src/utils/is-entry-point.js +137 -0
- package/src/utils/node-pty-probe.js +95 -0
- package/src/utils/node-pty-support.js +118 -0
- package/src/utils/opened-file-path.js +167 -0
- package/src/utils/path-containment.js +165 -0
- package/src/utils/permission-mode-sidecar.js +51 -0
- package/src/utils/pid-liveness.js +47 -0
- package/src/utils/preflight.js +360 -16
- package/src/utils/read-limiter.js +217 -0
- package/src/utils/regular-file.js +375 -0
- package/src/utils/safe-regex.js +113 -0
- package/src/utils/small-file.js +129 -0
- package/src/utils/spawn-env.js +189 -4
- package/src/utils/stale-session-dirs.js +182 -0
- package/src/utils/stat-identity.js +43 -0
- package/src/utils/verify-provenance.js +406 -13
- package/src/web-task-manager.js +297 -16
- package/src/worktree-gc.js +202 -6
- package/src/ws-auth.js +163 -31
- package/src/ws-broadcaster.js +49 -5
- package/src/ws-client-manager.js +24 -1
- package/src/ws-client-sender.js +7 -0
- package/src/ws-file-ops/browser.js +363 -58
- package/src/ws-file-ops/common.js +320 -14
- package/src/ws-file-ops/git.js +395 -42
- package/src/ws-file-ops/index.js +2 -0
- package/src/ws-file-ops/memory.js +137 -35
- package/src/ws-file-ops/open-nofollow.js +354 -0
- package/src/ws-file-ops/reader.js +571 -113
- package/src/ws-forwarding.js +349 -39
- package/src/ws-handler-context.js +88 -2
- package/src/ws-history.js +1010 -175
- package/src/ws-message-handlers.js +25 -1
- package/src/ws-permissions.js +307 -37
- package/src/ws-schemas.js +5 -0
- package/src/ws-server.js +885 -98
- package/sidecar/node_modules/.package-lock.json +0 -29
- package/sidecar/node_modules/ws/LICENSE +0 -20
- package/sidecar/node_modules/ws/README.md +0 -548
- package/sidecar/node_modules/ws/browser.js +0 -8
- package/sidecar/node_modules/ws/index.js +0 -22
- package/sidecar/node_modules/ws/lib/buffer-util.js +0 -131
- package/sidecar/node_modules/ws/lib/constants.js +0 -19
- package/sidecar/node_modules/ws/lib/event-target.js +0 -292
- package/sidecar/node_modules/ws/lib/extension.js +0 -203
- package/sidecar/node_modules/ws/lib/limiter.js +0 -55
- package/sidecar/node_modules/ws/lib/permessage-deflate.js +0 -528
- package/sidecar/node_modules/ws/lib/receiver.js +0 -706
- package/sidecar/node_modules/ws/lib/sender.js +0 -602
- package/sidecar/node_modules/ws/lib/stream.js +0 -161
- package/sidecar/node_modules/ws/lib/subprotocol.js +0 -62
- package/sidecar/node_modules/ws/lib/validation.js +0 -152
- package/sidecar/node_modules/ws/lib/websocket-server.js +0 -554
- package/sidecar/node_modules/ws/lib/websocket.js +0 -1393
- package/sidecar/node_modules/ws/package.json +0 -70
- package/sidecar/node_modules/ws/wrapper.mjs +0 -21
- package/src/dashboard-next/dist/assets/index-C2ipeGlq.js +0 -111
- package/src/dashboard-next/dist/assets/index-DM-_dISG.css +0 -1
- package/src/dashboard-next/dist/index.html +0 -13
- package/src/dashboard-next/node_modules/.vite/vitest/da39a3ee5e6b4b0d3255bfef95601890afd80709/results.json +0 -1
|
@@ -19,7 +19,20 @@
|
|
|
19
19
|
|
|
20
20
|
// Tools a worker must never be allowed to use headlessly: sub-delegation and
|
|
21
21
|
// network fetches (the committee owns the delegation tree; keeps cost flat).
|
|
22
|
-
|
|
22
|
+
// #7340: `Agent` is Claude Code's current name for the sub-delegation tool and
|
|
23
|
+
// `Task` the older one — both listed, since chroxy runs against whatever
|
|
24
|
+
// `claude` the user has installed. `_decide` already ends in `return 'deny'`,
|
|
25
|
+
// so the omission was not a fail-open; it did mean an `Agent` call was denied
|
|
26
|
+
// by the catch-all rather than by the rule that exists to say why.
|
|
27
|
+
// Exported so its test can assert MEMBERSHIP directly. A behavioural test
|
|
28
|
+
// cannot: `_decide` ends in `return 'deny'`, so every tool in this set is
|
|
29
|
+
// denied whether or not the set is consulted, and the whole branch can be
|
|
30
|
+
// deleted with no observable change today. That makes the branch a guard
|
|
31
|
+
// against a FUTURE allow path being added above it, and the invariant worth
|
|
32
|
+
// testing is its POSITION, not its effect (see the test).
|
|
33
|
+
import { RESOLVED_BY } from '../permission-resolved-by.js'
|
|
34
|
+
|
|
35
|
+
export const ALWAYS_DENY = new Set(['Task', 'Agent', 'WebFetch', 'WebSearch'])
|
|
23
36
|
|
|
24
37
|
export class OrchestrationPermissionGate {
|
|
25
38
|
/**
|
|
@@ -65,6 +78,7 @@ export class OrchestrationPermissionGate {
|
|
|
65
78
|
const toolName = data?.toolName ?? data?.tool ?? ''
|
|
66
79
|
const requestId = data?.requestId ?? data?.id ?? null
|
|
67
80
|
if (requestId == null) return
|
|
81
|
+
if (toolName === 'dev_preview' || data?.explicitConsent !== false || data?.nonDelegable !== false) return
|
|
68
82
|
const decision = this._decide(role, toolName, data?.input ?? {})
|
|
69
83
|
if (decision === 'escalate') {
|
|
70
84
|
// Implement-worker Bash that isn't allowlisted: surface to the user. Until
|
|
@@ -101,7 +115,8 @@ export class OrchestrationPermissionGate {
|
|
|
101
115
|
return
|
|
102
116
|
}
|
|
103
117
|
try {
|
|
104
|
-
|
|
118
|
+
// #9119: a policy decision (the allowlist), never a person.
|
|
119
|
+
session.respondToPermission(requestId, decision === 'allow' ? 'allow' : 'deny', undefined, undefined, undefined, RESOLVED_BY.rule)
|
|
105
120
|
} catch (err) {
|
|
106
121
|
this._log?.warn?.(`permission-gate: respond failed for ${sessionId}: ${err?.message || err}`)
|
|
107
122
|
}
|
|
@@ -105,7 +105,8 @@ export function assertRunTransition(from, to) {
|
|
|
105
105
|
if (!RUN_STATUSES.has(to)) throw new TransitionError('run', from, `${to} (unknown state)`)
|
|
106
106
|
if (from === to && to === 'executing') return true
|
|
107
107
|
// `failed` is an ERROR terminal, not forward progress: an unrecoverable engine
|
|
108
|
-
// error (or a user
|
|
108
|
+
// error (or a user choosing fail-run on an escalation; a declined plan cancels
|
|
109
|
+
// instead, #7131) can end a run from any
|
|
109
110
|
// non-terminal state. Enumerating it per-row would leave whichever row was
|
|
110
111
|
// missed as a fail-closed landmine that wedges a live run.
|
|
111
112
|
if ((to === 'cancelling' || to === 'suspended' || to === 'failed') && !TERMINAL_RUN_STATUSES.has(from)) return true
|
|
@@ -68,6 +68,17 @@ export function normalizeUsage(usage) {
|
|
|
68
68
|
}
|
|
69
69
|
}
|
|
70
70
|
|
|
71
|
+
/**
|
|
72
|
+
* Whether a projected terminal payload reports any spend: a finite cost (zero and
|
|
73
|
+
* negative included) or any token count the ledger would fold (via normalizeUsage).
|
|
74
|
+
* The ONE spend predicate: the turn driver (which decides whether a terminal event
|
|
75
|
+
* uses up the single late-usage delivery) and the manager (which decides whether to
|
|
76
|
+
* record) must agree, or spend is dropped or never delivered (#9147).
|
|
77
|
+
*/
|
|
78
|
+
export function hasSpend(result) {
|
|
79
|
+
return !!result && (Number.isFinite(result.cost) || Object.values(normalizeUsage(result.usage)).some((n) => n > 0))
|
|
80
|
+
}
|
|
81
|
+
|
|
71
82
|
// Fold one turn's numbers into a cell in place. `cost` is the provider-reported
|
|
72
83
|
// signed cost (null/undefined = unknown); `pricedCostUsd` is the server-derived
|
|
73
84
|
// fallback used ONLY when cost is unknown.
|
|
@@ -45,32 +45,40 @@
|
|
|
45
45
|
* following turn. No in-tree provider does that (every emit site listed above is
|
|
46
46
|
* a single synchronous frame); closing it by construction needs a provider-side
|
|
47
47
|
* turn id on the event payload.
|
|
48
|
-
* -
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
*
|
|
48
|
+
* - Interrupted turns that LOOK finished (#7036, #7072). CliSession emits its
|
|
49
|
+
* synthetic interrupted `result` (cost:null, load-bearing for clearing the
|
|
50
|
+
* dashboard spinner) BEFORE `stopped`. Two harms follow: the turn settles as a
|
|
51
|
+
* SUCCESS on the `result`, and (since #7033) the trailing `stopped` lands on the
|
|
52
|
+
* NEXT queued turn and kills it.
|
|
53
53
|
*
|
|
54
|
-
* The QUEUED-TURN KILL is
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
*
|
|
54
|
+
* The QUEUED-TURN KILL is closed by construction: only claude-cli emits `result`
|
|
55
|
+
* then `stopped` for one interrupt, and claude-cli cannot enter TurnDriver by ANY
|
|
56
|
+
* route — the scheduler requires capabilities.inProcessPermissions (cli-session
|
|
57
|
+
* sets it false), the orchestration WORKER roles are gated on
|
|
58
|
+
* AUDIT/IMPLEMENT_ELIGIBLE_PROVIDERS, and the ARCHITECT role on
|
|
59
|
+
* ARCHITECT_ELIGIBLE_PROVIDERS.
|
|
60
60
|
*
|
|
61
|
-
* The FALSE SUCCESS is
|
|
62
|
-
*
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
* `
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
*
|
|
61
|
+
* The FALSE SUCCESS is closed by a provider-side flag: a `result` payload carries
|
|
62
|
+
* `interrupted: true` when the turn ended by abort/interrupt/stall rather than
|
|
63
|
+
* completion, and this driver rejects it (TURN_STOPPED, or TURN_ERROR when the
|
|
64
|
+
* payload also carries `failureMessage`). The flag is set by BaseSession.emit for
|
|
65
|
+
* every result whose `turnOutcome` is `stopped` (the SDK's aborted_* results, an
|
|
66
|
+
* ACP `cancelled`, and the Codex app-server's `turn/completed {status:
|
|
67
|
+
* 'interrupted'}`, #9241), by claude-byok when an interrupt lands in the tool phase,
|
|
68
|
+
* by claude-sdk's stream-stall result and by claude-cli's synthetic interrupted
|
|
69
|
+
* result. It is internal: event-normalizer whitelists the result fields it puts
|
|
70
|
+
* on the wire, so clients never see it.
|
|
71
|
+
*
|
|
72
|
+
* Codex app-server: `_finishTurn` now reads `turn.status` and reports `interrupted`
|
|
73
|
+
* as a `stopped` result. That shape is documented by OpenAI but has not been pinned
|
|
74
|
+
* against a live binary in this repo's tests, so an operator Stop on a codex session
|
|
75
|
+
* settling as a success remains possible if the binary answers differently.
|
|
70
76
|
* - Watchdog: on timeout, interrupt() the session and reject TURN_TIMEOUT.
|
|
71
77
|
* - `session_destroyed` mid-turn → SESSION_GONE.
|
|
72
78
|
*/
|
|
73
79
|
|
|
80
|
+
import { hasSpend } from './run-record.js'
|
|
81
|
+
|
|
74
82
|
export const MAX_ACCUM_BYTES = 2 * 1024 * 1024
|
|
75
83
|
export const DEFAULT_TURN_TIMEOUT_MS = 30 * 60 * 1000
|
|
76
84
|
// After a timeout+interrupt the mutex is held in a "draining" state until the
|
|
@@ -80,12 +88,29 @@ export const DEFAULT_TURN_TIMEOUT_MS = 30 * 60 * 1000
|
|
|
80
88
|
export const DEFAULT_DRAIN_TIMEOUT_MS = 10 * 1000
|
|
81
89
|
const TRUNCATION_MARKER = '\n…[chroxy-orch: output truncated at 2MB]…\n'
|
|
82
90
|
|
|
91
|
+
// Both result and error events can carry a turn's sole accounting payload.
|
|
92
|
+
// Preserve the same projection for either terminal event; never copy nested
|
|
93
|
+
// exception properties, error codes or unrelated provider metadata.
|
|
94
|
+
function projectTerminalUsage(data) {
|
|
95
|
+
return {
|
|
96
|
+
cost: data?.cost ?? null,
|
|
97
|
+
duration: data?.duration ?? null,
|
|
98
|
+
usage: data?.usage ?? null,
|
|
99
|
+
modelUsage: data?.modelUsage ?? null,
|
|
100
|
+
model: data?.model ?? null,
|
|
101
|
+
numTurns: Number.isFinite(data?.numTurns) ? data.numTurns : null,
|
|
102
|
+
apiDurationMs: Number.isFinite(data?.apiDurationMs) ? data.apiDurationMs : null,
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
83
106
|
export class TurnError extends Error {
|
|
84
|
-
constructor(code, message, { partialText = '' } = {}) {
|
|
107
|
+
constructor(code, message, { partialText = '', result = null, terminalEvent = null } = {}) {
|
|
85
108
|
super(message || code)
|
|
86
109
|
this.name = 'TurnError'
|
|
87
|
-
this.code = code // TURN_ERROR | TURN_TIMEOUT | TURN_STOPPED | SESSION_GONE | SEND_FAILED
|
|
110
|
+
this.code = code // TURN_ERROR | TURN_TIMEOUT | TURN_STOPPED | SESSION_GONE | SEND_FAILED | NOT_ADMITTED
|
|
88
111
|
this.partialText = partialText
|
|
112
|
+
this.result = result // projected terminal accounting, never thrown error metadata
|
|
113
|
+
this.terminalEvent = terminalEvent // result | error; null without a terminal payload
|
|
89
114
|
}
|
|
90
115
|
}
|
|
91
116
|
|
|
@@ -98,6 +123,15 @@ export class TurnDriver {
|
|
|
98
123
|
if (!sessionManager || typeof sessionManager.on !== 'function') {
|
|
99
124
|
throw new Error('TurnDriver requires a sessionManager EventEmitter')
|
|
100
125
|
}
|
|
126
|
+
// #9184: the turn-driven registry is what keeps a daemon wake out of a driven
|
|
127
|
+
// session (#8327). A manager without it must fail HERE, not silently run
|
|
128
|
+
// unguarded ("cannot check" reading as "nothing to check"). There is no
|
|
129
|
+
// guard-free mode: every construction site passes a real SessionManager.
|
|
130
|
+
for (const method of ['markTurnDriven', 'releaseTurnDriven']) {
|
|
131
|
+
if (typeof sessionManager[method] !== 'function') {
|
|
132
|
+
throw new Error(`TurnDriver requires a sessionManager with ${method}() (the turn-driven registry, #8327)`)
|
|
133
|
+
}
|
|
134
|
+
}
|
|
101
135
|
this._sm = sessionManager
|
|
102
136
|
this._log = log
|
|
103
137
|
this._defaultTimeoutMs = defaultTimeoutMs
|
|
@@ -106,6 +140,7 @@ export class TurnDriver {
|
|
|
106
140
|
this._occupied = new Set() // sessionId -> has a turn running or reserved
|
|
107
141
|
this._waiters = new Map() // sessionId -> FIFO array of { start, reject }
|
|
108
142
|
this._handoff = new Set() // sessionId -> a deferred _startNext is already queued
|
|
143
|
+
this._late = new Map() // sessionId -> timed-out ctx still owed this frame's terminal usage (#9147)
|
|
109
144
|
this._onSessionEvent = this._handleSessionEvent.bind(this)
|
|
110
145
|
this._onSessionDestroyed = this._handleSessionDestroyed.bind(this)
|
|
111
146
|
this._sm.on('session_event', this._onSessionEvent)
|
|
@@ -123,8 +158,9 @@ export class TurnDriver {
|
|
|
123
158
|
const active = [...this._active.values()]
|
|
124
159
|
this._waiters.clear()
|
|
125
160
|
this._active.clear()
|
|
126
|
-
this._occupied.
|
|
161
|
+
for (const id of [...this._occupied]) this._vacate(id)
|
|
127
162
|
this._handoff.clear()
|
|
163
|
+
this._late.clear()
|
|
128
164
|
for (const w of waiters) {
|
|
129
165
|
try { w.reject(new TurnError('SESSION_GONE', 'TurnDriver disposed')) } catch { /* ignore */ }
|
|
130
166
|
}
|
|
@@ -145,17 +181,36 @@ export class TurnDriver {
|
|
|
145
181
|
* tick after the prior turn releases — never inside the frame that released it
|
|
146
182
|
* (_scheduleNext).
|
|
147
183
|
* @returns {Promise<{ text: string, result: { cost, duration, usage, modelUsage, model, numTurns, apiDurationMs } }>}
|
|
184
|
+
*
|
|
185
|
+
* `admit` (optional, #9242): a predicate re-asked at the moment the prompt would
|
|
186
|
+
* actually reach the provider — when this turn starts after waiting on the
|
|
187
|
+
* per-session mutex, and (through the provider's flush-time admission hook,
|
|
188
|
+
* BaseSession `admitAtFlush`) when a provider that queued the send dispatches it.
|
|
189
|
+
* Anything but `true`, or a throw, means NOT admitted: the prompt is never sent
|
|
190
|
+
* and the turn rejects `NOT_ADMITTED`. Callers that pass none are unchanged.
|
|
191
|
+
*
|
|
192
|
+
* `onLateUsage(result, terminalEvent)` (optional, #9147): called AT MOST ONCE with the
|
|
193
|
+
* projected accounting of a `result`/`error` that arrives AFTER the turn already
|
|
194
|
+
* rejected without one — while a TURN_TIMEOUT drains (the interrupted session's
|
|
195
|
+
* trailing `result` is swallowed there). Without it that spend never reached the
|
|
196
|
+
* caller's ledger. A bare `stopped` is deliberately NOT covered: every provider
|
|
197
|
+
* reports an interrupted turn's accounting in a `result` BEFORE `stopped` (which
|
|
198
|
+
* the result path already carries), `stopped`'s own payload is never accounting,
|
|
199
|
+
* and a result after it in the same frame cannot be attributed. The event is
|
|
200
|
+
* consumed here (swallowed by the drain / dropped by the epoch guard) so it can never
|
|
201
|
+
* ALSO reach the next turn: the caller records it exactly once. A throw is logged.
|
|
202
|
+
* Callers that pass none are unchanged.
|
|
148
203
|
* @throws {TurnError}
|
|
149
204
|
*/
|
|
150
|
-
driveTurn(sessionId, prompt, { label = null, timeoutMs = null } = {}) {
|
|
205
|
+
driveTurn(sessionId, prompt, { label = null, timeoutMs = null, admit = null, onLateUsage = null } = {}) {
|
|
151
206
|
if (this._disposed) return Promise.reject(new TurnError('SESSION_GONE', 'TurnDriver disposed'))
|
|
152
207
|
const entry = this._sm.getSession?.(sessionId)
|
|
153
208
|
if (!entry || !entry.session) return Promise.reject(new TurnError('SESSION_GONE', `session ${sessionId} not found`))
|
|
154
209
|
|
|
155
210
|
return new Promise((resolve, reject) => {
|
|
156
|
-
const start = () => this._beginTurn(sessionId, prompt, { label, timeoutMs }, resolve, reject)
|
|
211
|
+
const start = () => this._beginTurn(sessionId, prompt, { label, timeoutMs, admit, onLateUsage }, resolve, reject)
|
|
157
212
|
if (!this._occupied.has(sessionId)) {
|
|
158
|
-
this.
|
|
213
|
+
this._occupy(sessionId)
|
|
159
214
|
start()
|
|
160
215
|
} else {
|
|
161
216
|
const q = this._waiters.get(sessionId) || []
|
|
@@ -165,7 +220,12 @@ export class TurnDriver {
|
|
|
165
220
|
})
|
|
166
221
|
}
|
|
167
222
|
|
|
168
|
-
|
|
223
|
+
_admitted(admit) {
|
|
224
|
+
if (typeof admit !== 'function') return true
|
|
225
|
+
try { return admit() === true } catch { return false }
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
_beginTurn(sessionId, prompt, { label, timeoutMs, admit = null, onLateUsage = null }, resolve, reject) {
|
|
169
229
|
// Re-fetch the session at START time, not at driveTurn() time: a queued turn
|
|
170
230
|
// may have waited behind the mutex while its session was destroyed/replaced.
|
|
171
231
|
const entry = this._sm.getSession?.(sessionId)
|
|
@@ -174,6 +234,11 @@ export class TurnDriver {
|
|
|
174
234
|
this._scheduleNext(sessionId)
|
|
175
235
|
return
|
|
176
236
|
}
|
|
237
|
+
if (!this._admitted(admit)) {
|
|
238
|
+
reject(new TurnError('NOT_ADMITTED', `turn for session ${sessionId} was not admitted at dispatch`))
|
|
239
|
+
this._scheduleNext(sessionId)
|
|
240
|
+
return
|
|
241
|
+
}
|
|
177
242
|
const ctx = {
|
|
178
243
|
sessionId,
|
|
179
244
|
label,
|
|
@@ -183,6 +248,7 @@ export class TurnDriver {
|
|
|
183
248
|
truncated: false,
|
|
184
249
|
settled: false,
|
|
185
250
|
timer: null,
|
|
251
|
+
onLateUsage: typeof onLateUsage === 'function' ? onLateUsage : null,
|
|
186
252
|
resolve,
|
|
187
253
|
reject,
|
|
188
254
|
}
|
|
@@ -204,7 +270,20 @@ export class TurnDriver {
|
|
|
204
270
|
// Fire-and-forget send; a rejection (closing socket, etc.) is SEND_FAILED —
|
|
205
271
|
// NEVER leave it unhandled (an unhandled rejection crashes the daemon).
|
|
206
272
|
try {
|
|
207
|
-
const ret = entry.session.sendMessage(prompt, [], {
|
|
273
|
+
const ret = entry.session.sendMessage(prompt, [], {
|
|
274
|
+
clientMessageId: `orch-${label || 'turn'}`,
|
|
275
|
+
// #9242: a provider that QUEUES this send re-asks at flush; a refusal drops the
|
|
276
|
+
// prompt (never sent) and ends this turn instead of leaving it to time out.
|
|
277
|
+
...(typeof admit === 'function'
|
|
278
|
+
? {
|
|
279
|
+
admitAtFlush: () => {
|
|
280
|
+
const ok = this._admitted(admit)
|
|
281
|
+
if (!ok) this._finishTurn(ctx, () => ctx.reject(new TurnError('NOT_ADMITTED', `queued turn for session ${sessionId} was not admitted at flush`, { partialText: ctx.text() })))
|
|
282
|
+
return ok
|
|
283
|
+
},
|
|
284
|
+
}
|
|
285
|
+
: {}),
|
|
286
|
+
})
|
|
208
287
|
if (ret && typeof ret.then === 'function') {
|
|
209
288
|
ret.catch((err) => {
|
|
210
289
|
this._finishTurn(ctx, () => ctx.reject(new TurnError('SEND_FAILED', (err && err.message) || 'sendMessage rejected', { partialText: ctx.text() })))
|
|
@@ -246,17 +325,43 @@ export class TurnDriver {
|
|
|
246
325
|
const next = q.shift() // stays occupied — hand the slot to the next turn
|
|
247
326
|
next.start()
|
|
248
327
|
} else {
|
|
249
|
-
this.
|
|
328
|
+
this._vacate(sessionId)
|
|
250
329
|
this._waiters.delete(sessionId)
|
|
251
330
|
}
|
|
252
331
|
}
|
|
253
332
|
|
|
333
|
+
// #8327: `_occupied` is mirrored into the SessionManager's ONE registry, so a
|
|
334
|
+
// daemon wake (daemonTurnRefusal -> 'orchestrated') can see every driver's
|
|
335
|
+
// sessions. These two are the ONLY places that touch `_occupied` — a new
|
|
336
|
+
// construction site gets the registration for free, through its sessionManager.
|
|
337
|
+
_occupy(sessionId) {
|
|
338
|
+
this._occupied.add(sessionId)
|
|
339
|
+
try { this._sm.markTurnDriven(sessionId, this) } catch (err) { this._log?.warn?.(`TurnDriver: markTurnDriven failed: ${err?.message || err}`) }
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
_vacate(sessionId) {
|
|
343
|
+
this._occupied.delete(sessionId)
|
|
344
|
+
try { this._sm.releaseTurnDriven(sessionId, this) } catch (err) { this._log?.warn?.(`TurnDriver: releaseTurnDriven failed: ${err?.message || err}`) }
|
|
345
|
+
}
|
|
346
|
+
|
|
254
347
|
_handleSessionEvent({ sessionId, event, data } = {}) {
|
|
255
348
|
const ctx = this._active.get(sessionId)
|
|
256
|
-
if (!ctx)
|
|
349
|
+
if (!ctx) {
|
|
350
|
+
// epoch guard: no active/draining turn for this session → drop. The one
|
|
351
|
+
// exception is the same-frame slot of a turn that just rejected without its
|
|
352
|
+
// accounting (#9147): its own trailing result/error is owed to that turn.
|
|
353
|
+
const late = this._late.get(sessionId)
|
|
354
|
+
if (late && (event === 'result' || event === 'error')) this._reportLateUsage(late, event, data)
|
|
355
|
+
return
|
|
356
|
+
}
|
|
257
357
|
if (ctx.draining) {
|
|
258
358
|
// A settled-but-draining turn (post-timeout). Swallow its trailing output;
|
|
259
359
|
// a terminal event confirms the session stopped → release the mutex.
|
|
360
|
+
if (event === 'result' || event === 'error') this._reportLateUsage(ctx, event, data)
|
|
361
|
+
// `stream_end` (or a terminal event with no spend, e.g. a BYOK non-fatal
|
|
362
|
+
// `error`) ends the drain BEFORE the interrupted turn's spend-bearing `result`
|
|
363
|
+
// in the same frame: keep that frame's slot open so the spend is not dropped.
|
|
364
|
+
if (event === 'stream_end') this._armLate(ctx)
|
|
260
365
|
if (event === 'result' || event === 'error' || event === 'stopped' || event === 'stream_end') {
|
|
261
366
|
this._endDrain(ctx)
|
|
262
367
|
}
|
|
@@ -280,25 +385,36 @@ export class TurnDriver {
|
|
|
280
385
|
break
|
|
281
386
|
}
|
|
282
387
|
case 'result': {
|
|
283
|
-
|
|
284
|
-
//
|
|
285
|
-
//
|
|
286
|
-
//
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
388
|
+
const result = projectTerminalUsage(data)
|
|
389
|
+
// #7072 — a terminal-looking `result` the provider marked `interrupted:
|
|
390
|
+
// true` is terminal but NOT a success: the turn was cut short (a Stop that
|
|
391
|
+
// the provider answered with a normal result, a BYOK interrupt in the tool
|
|
392
|
+
// phase, a stalled stream). Settle it as a failure here, on the result,
|
|
393
|
+
// because the event that follows it in the same synchronous frame (`stopped`
|
|
394
|
+
// / `error`) is dropped by the epoch guard once this ctx is gone — which is
|
|
395
|
+
// exactly how a truncated plan or review came to be read as authoritative.
|
|
396
|
+
// A provider that also knows WHY it failed (a stall) names it in
|
|
397
|
+
// `failureMessage`, and the caller sees TURN_ERROR; otherwise it is the
|
|
398
|
+
// same TURN_STOPPED an interrupt that emits `stopped` instead produces.
|
|
399
|
+
if (data?.interrupted === true) {
|
|
400
|
+
const partialText = this._assembleText(ctx)
|
|
401
|
+
const failure = typeof data.failureMessage === 'string' && data.failureMessage ? data.failureMessage : null
|
|
402
|
+
this._finishTurn(ctx, () => ctx.reject(failure
|
|
403
|
+
? new TurnError('TURN_ERROR', failure, { partialText, result, terminalEvent: 'result' })
|
|
404
|
+
: new TurnError('TURN_STOPPED', 'turn interrupted before completing', { partialText, result, terminalEvent: 'result' })))
|
|
405
|
+
break
|
|
295
406
|
}
|
|
296
407
|
const text = this._assembleText(ctx)
|
|
297
408
|
this._finishTurn(ctx, () => ctx.resolve({ text, result }))
|
|
298
409
|
break
|
|
299
410
|
}
|
|
300
411
|
case 'error': {
|
|
301
|
-
|
|
412
|
+
// BYOK can emit error instead of result after completed rounds. Its
|
|
413
|
+
// top-level partial spend is genuine terminal accounting (#8554), unlike
|
|
414
|
+
// arbitrary properties on a thrown/rejected sendMessage exception.
|
|
415
|
+
this._finishTurn(ctx, () => ctx.reject(new TurnError('TURN_ERROR', (data && data.message) || 'session error', {
|
|
416
|
+
partialText: this._assembleText(ctx), result: projectTerminalUsage(data), terminalEvent: 'error',
|
|
417
|
+
})))
|
|
302
418
|
break
|
|
303
419
|
}
|
|
304
420
|
case 'stopped': {
|
|
@@ -327,6 +443,32 @@ export class TurnDriver {
|
|
|
327
443
|
}
|
|
328
444
|
}
|
|
329
445
|
|
|
446
|
+
// #9147 — a turn that rejected WITHOUT a terminal accounting payload (a TURN_TIMEOUT
|
|
447
|
+
// draining) may still be owed its session's trailing result/error. Hold a slot for
|
|
448
|
+
// the rest of the current synchronous frame only — cleared on the same tick the mutex
|
|
449
|
+
// hand-off happens, so it can never see the next turn's events.
|
|
450
|
+
_armLate(ctx) {
|
|
451
|
+
if (!ctx.onLateUsage) return
|
|
452
|
+
// Already armed this frame: its one nextTick clear is queued; do not queue another.
|
|
453
|
+
if (this._late.get(ctx.sessionId) === ctx) return
|
|
454
|
+
this._late.set(ctx.sessionId, ctx)
|
|
455
|
+
process.nextTick(() => { if (this._late.get(ctx.sessionId) === ctx) this._late.delete(ctx.sessionId) })
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
_reportLateUsage(ctx, event, data) {
|
|
459
|
+
if (!ctx.onLateUsage) return
|
|
460
|
+
const projected = projectTerminalUsage(data)
|
|
461
|
+
// A terminal event with no spend does not use up the single delivery: a
|
|
462
|
+
// spend-bearing result in the same frame is still owed to this turn.
|
|
463
|
+
if (!hasSpend(projected)) { this._armLate(ctx); return }
|
|
464
|
+
// Exactly once: delivery closes the same-frame slot and the drain-ending event
|
|
465
|
+
// removed the ctx from _active, so no later event can reach this ctx again.
|
|
466
|
+
if (this._late.get(ctx.sessionId) === ctx) this._late.delete(ctx.sessionId)
|
|
467
|
+
try { ctx.onLateUsage(projected, event) } catch (err) {
|
|
468
|
+
this._log?.warn?.(`TurnDriver: onLateUsage failed: ${err?.message || err}`)
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
|
|
330
472
|
_handleSessionDestroyed({ sessionId } = {}) {
|
|
331
473
|
const ctx = this._active.get(sessionId)
|
|
332
474
|
if (!ctx || ctx.settled) return
|