@bitkyc08/opencodex 2.99.0 → 2.100.1
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/AGENTS_INSTALL.md +19 -1
- package/README.md +37 -33
- package/SPONSORS.md +5 -1
- package/bin/ocx.mjs +1 -1
- package/gui/dist/assets/{App-C1hGwaN1.css → App-COo8Yopd.css} +1 -1
- package/gui/dist/assets/App-g3Cm2xhZ.js +59 -0
- package/gui/dist/assets/Tray-BMDkAiXy.js +1 -0
- package/gui/dist/assets/brand-mark-nr3ziR-T.js +1 -0
- package/gui/dist/assets/index-Bziu8gTy.css +2 -0
- package/gui/dist/assets/index-OYzhQUNP.js +94 -0
- package/gui/dist/assets/{segment-thumb-features-fYEMxBaW.js → segment-thumb-features-DKCVUUTi.js} +1 -1
- package/gui/dist/classic/assets/App-CgPBKUP-.js +58 -0
- package/gui/dist/classic/assets/{Tray-BqZueZOx.js → Tray-BedmOsE7.js} +1 -1
- package/gui/dist/classic/assets/index-BR-HhNrp.js +94 -0
- package/gui/dist/classic/assets/quota-summary-CXKhvSmQ.js +1 -0
- package/gui/dist/classic/index.html +1 -1
- package/gui/dist/index.html +2 -2
- package/native/ocx-child/Cargo.lock +215 -0
- package/native/ocx-child/Cargo.toml +31 -0
- package/native/ocx-child/src/cli.rs +1901 -0
- package/native/ocx-child/src/codex_home.rs +429 -0
- package/native/ocx-child/src/codex_settings.rs +231 -0
- package/native/ocx-child/src/daemon.rs +363 -0
- package/native/ocx-child/src/http_wire.rs +619 -0
- package/native/ocx-child/src/journal.rs +977 -0
- package/native/ocx-child/src/lib.rs +22 -0
- package/native/ocx-child/src/main.rs +9 -0
- package/native/ocx-child/src/parity.rs +102 -0
- package/native/ocx-child/src/platform.rs +1487 -0
- package/native/ocx-child/src/relay.rs +1288 -0
- package/native/ocx-child/src/relay_auth.rs +166 -0
- package/native/ocx-child/src/routes.rs +236 -0
- package/native/ocx-child/src/state.rs +190 -0
- package/native/ocx-child/src/test_hooks.rs +58 -0
- package/native/ocx-child/src/toml_surgeon.rs +1156 -0
- package/package.json +8 -2
- package/src/adapters/anthropic-image-normalize.ts +1 -1
- package/src/adapters/anthropic.ts +28 -2
- package/src/adapters/claude-cli/adapter.ts +5 -25
- package/src/adapters/claude-cli/instruction-images.ts +31 -0
- package/src/adapters/claude-cli/stable-replay.ts +42 -14
- package/src/adapters/claude-cli/usage-admission.ts +53 -64
- package/src/adapters/client-fingerprint.ts +1 -1
- package/src/adapters/coding-agent/protocol.ts +1 -1
- package/src/adapters/devin/live-models.ts +3 -0
- package/src/adapters/google-http.ts +3 -0
- package/src/adapters/google-vertex-key-project.ts +10 -2
- package/src/adapters/google.ts +40 -8
- package/src/adapters/kiro/usage.ts +3 -13
- package/src/adapters/openai-chat/mistral-image-limit.ts +28 -15
- package/src/adapters/openai-chat-images.ts +100 -0
- package/src/adapters/openai-chat.ts +6 -9
- package/src/adapters/openai-responses/passthrough.ts +5 -2
- package/src/adapters/openai-responses/request-strips.ts +32 -0
- package/src/adapters/openai-responses/tool-schema.ts +9 -4
- package/src/chat/inbound.ts +1 -1
- package/src/claude/auth-detect.ts +26 -8
- package/src/claude/inbound-content-options.ts +18 -3
- package/src/claude/inbound.ts +5 -2
- package/src/cli/bundled-bun-diagnostic.ts +91 -0
- package/src/cli/capabilities-agents-routing.ts +2 -2
- package/src/cli/capabilities-observe-system.ts +60 -0
- package/src/cli/combo-input.ts +6 -0
- package/src/cli/combo-result.ts +1 -1
- package/src/cli/combo.ts +4 -0
- package/src/cli/config-command.ts +16 -2
- package/src/cli/connect.ts +12 -10
- package/src/cli/dispatch.ts +4 -0
- package/src/cli/doctor.ts +11 -0
- package/src/cli/help.ts +1 -0
- package/src/cli/init.ts +4 -0
- package/src/cli/link-enrollment.ts +152 -25
- package/src/cli/link-local-control.ts +49 -0
- package/src/cli/link.ts +37 -10
- package/src/cli/provider-lifecycle-runtime.ts +4 -2
- package/src/cli/registry.ts +13 -0
- package/src/cli/star-prompt.ts +2 -0
- package/src/cli/status.ts +1 -1
- package/src/cli/telemetry-profile.ts +206 -0
- package/src/cli/telemetry.ts +260 -0
- package/src/client/hub-relay.ts +26 -0
- package/src/client/machine-api.ts +3 -0
- package/src/clients/config-export/t3code.ts +44 -5
- package/src/clients/omo-role-models.ts +1 -1
- package/src/codex/catalog/bundled.ts +44 -2
- package/src/codex/runtime.ts +12 -0
- package/src/codex/shim-restore-lock.ts +67 -30
- package/src/combos/types.ts +21 -1
- package/src/config/atomic-write.ts +24 -1
- package/src/config/diagnostics.ts +15 -0
- package/src/config/live-reconcile.ts +17 -0
- package/src/config/load-degrade.ts +16 -0
- package/src/config/provider-validation.ts +29 -0
- package/src/config/schema/config-schema.ts +24 -0
- package/src/config/schema/leaf-validators.ts +17 -0
- package/src/config/telemetry-carry.ts +30 -0
- package/src/config.ts +13 -1
- package/src/generated/compatibility-version.json +422 -170
- package/src/generated/model-metadata.ts +3 -3
- package/src/integrations/aside-profiles.ts +7 -1
- package/src/integrations/owned-refresh.ts +9 -2
- package/src/lab/artifacts/secure-fs.ts +12 -11
- package/src/lab/fabric/scratch.ts +11 -9
- package/src/lab/public/community.ts +2 -3
- package/src/lab/public/file-safety.ts +18 -20
- package/src/lab/public/mutation-lock.ts +109 -337
- package/src/lab/public/private-file.ts +4 -3
- package/src/lib/bun-runtime-preflight.ts +1 -1
- package/src/lib/codex-catalog-read-only.ts +122 -0
- package/src/lib/config-ownership.ts +56 -18
- package/src/lib/errors.ts +11 -0
- package/src/lib/gcp-adc.ts +167 -48
- package/src/lib/jsonc.ts +4 -2
- package/src/lib/local-control-socket.ts +88 -0
- package/src/lib/privacy.ts +15 -0
- package/src/lib/provider-client-headers.ts +3 -0
- package/src/lib/token-estimate.ts +13 -2
- package/src/link/child-binary-source.ts +228 -0
- package/src/link/ephemeral-child.ts +45 -0
- package/src/link/remote-plant.ts +306 -0
- package/src/link/remote-windows.ts +59 -5
- package/src/link/ssh-argv.ts +71 -8
- package/src/link/status-projection.ts +2 -0
- package/src/link/store.ts +5 -1
- package/src/oauth/anthropic-model-quota.ts +12 -0
- package/src/oauth/anthropic-routing.ts +11 -4
- package/src/oauth/claude-keychain-read.ts +165 -0
- package/src/oauth/kiro-iam-identity-center.ts +4 -1
- package/src/oauth/local-token-detect.ts +14 -17
- package/src/protocols/envelope.ts +20 -2
- package/src/providers/api-key-selection.ts +8 -2
- package/src/providers/claude-cli-identity.ts +9 -3
- package/src/providers/claude-cli-usage-state.ts +93 -0
- package/src/providers/claude-cli-usage.ts +3 -2
- package/src/providers/github-copilot-auto.ts +25 -5
- package/src/providers/meta-muse-plan-refusal.ts +94 -0
- package/src/providers/model-rename-fields.ts +3 -0
- package/src/providers/quota/anthropic-family-headers.ts +8 -1
- package/src/providers/quota/anthropic-usage-windows.ts +49 -2
- package/src/providers/quota/vendor-probes-key.ts +23 -4
- package/src/providers/quota/vendor-probes-oauth.ts +59 -0
- package/src/providers/quota/workbuddy.ts +2 -2
- package/src/providers/quota-types.ts +7 -0
- package/src/providers/quota.ts +25 -18
- package/src/providers/registry/entries-core.ts +7 -4
- package/src/providers/registry/entries-extended.ts +42 -8
- package/src/providers/registry/model-ids.ts +1 -0
- package/src/providers/registry/model-seeds.ts +10 -2
- package/src/providers/registry/types.ts +6 -0
- package/src/providers/resolved-model-policy.ts +4 -0
- package/src/providers/routed-config.ts +30 -3
- package/src/providers/stale-no-vision-seed-migration.ts +7 -0
- package/src/providers/vertex-environment-authority.ts +50 -0
- package/src/providers/workbuddy-credit-balance.ts +38 -6
- package/src/quota-pool/assemble.ts +14 -6
- package/src/responses/retained-compaction.ts +31 -5
- package/src/router.ts +1 -0
- package/src/server/adapter-resolve.ts +11 -6
- package/src/server/audio-dictation.ts +21 -7
- package/src/server/auth-cors.ts +19 -0
- package/src/server/background-lifecycle.ts +71 -0
- package/src/server/chat-completions.ts +2 -0
- package/src/server/claude-messages.ts +30 -3
- package/src/server/index/live-sideband.ts +15 -1
- package/src/server/index/local-control-listener.ts +515 -0
- package/src/server/index/optional-listeners.ts +21 -1
- package/src/server/index/serve-options.ts +4 -3
- package/src/server/index/websocket-handler.ts +2 -0
- package/src/server/index.ts +3 -0
- package/src/server/management/combo-routes.ts +6 -1
- package/src/server/management/context.ts +1 -0
- package/src/server/management/link-child-preflight.ts +74 -0
- package/src/server/management/link-enrollment.ts +190 -25
- package/src/server/management/link-route-state.ts +6 -1
- package/src/server/management/link-routes.ts +27 -9
- package/src/server/management/model-rows.ts +4 -0
- package/src/server/management/oauth-account-routes.ts +9 -4
- package/src/server/management/provider-overwrite-carry.ts +9 -2
- package/src/server/management/provider-routes.ts +14 -0
- package/src/server/management/provider-thinking-display-patch.ts +54 -0
- package/src/server/management/route-registry.ts +7 -0
- package/src/server/management/session-routes.ts +4 -1
- package/src/server/management/telemetry-routes.ts +37 -0
- package/src/server/management/usage-aggregate-cache.ts +290 -67
- package/src/server/management-api.ts +11 -2
- package/src/server/messages-native-preflight.ts +166 -0
- package/src/server/messages-native.ts +143 -43
- package/src/server/messages-shared.ts +14 -0
- package/src/server/proxy-liveness.ts +99 -6
- package/src/server/request-log-conversation.ts +18 -2
- package/src/server/request-log.ts +7 -2
- package/src/server/responses/adapter-continuation.ts +1 -0
- package/src/server/responses/adapter-dispatch.ts +11 -28
- package/src/server/responses/adapter-failure-message.ts +68 -0
- package/src/server/responses/combo-stream-preflight.ts +14 -2
- package/src/server/responses/compact.ts +13 -1
- package/src/server/responses/core-auth.ts +26 -5
- package/src/server/responses/core-codex-account.ts +8 -1
- package/src/server/responses/core-combo-failure.ts +6 -1
- package/src/server/responses/core-combo-native.ts +150 -37
- package/src/server/responses/core-combo.ts +15 -9
- package/src/server/responses/core-normalize.ts +1 -0
- package/src/server/responses/core-options.ts +7 -1
- package/src/server/responses/core-pipeline.ts +21 -1
- package/src/server/responses/fetch-helpers.ts +5 -0
- package/src/server/responses/opengateway-completion-guard.ts +7 -3
- package/src/server/responses/passthrough-delivery.ts +8 -1
- package/src/server/responses/passthrough-error.ts +22 -0
- package/src/server/responses/request-prepare.ts +7 -1
- package/src/server/responses/request-transport.ts +28 -5
- package/src/server/responses/run-turn-execution.ts +13 -0
- package/src/server/sse-frame-buffer.ts +5 -0
- package/src/server/ws-bridge.ts +10 -0
- package/src/service/launchd.ts +78 -4
- package/src/telemetry/aggregate.ts +348 -0
- package/src/telemetry/consent.ts +128 -0
- package/src/telemetry/contract.ts +403 -0
- package/src/telemetry/disclosure.ts +19 -0
- package/src/telemetry/install-id.ts +226 -0
- package/src/telemetry/payload.ts +129 -0
- package/src/telemetry/profile/client.ts +150 -0
- package/src/telemetry/profile/contract.ts +288 -0
- package/src/telemetry/profile/disclosure.ts +14 -0
- package/src/telemetry/profile/paths.ts +13 -0
- package/src/telemetry/profile/routes.ts +99 -0
- package/src/telemetry/profile/service.ts +234 -0
- package/src/telemetry/profile/state.ts +189 -0
- package/src/telemetry/profile/sync.ts +276 -0
- package/src/telemetry/routes.ts +183 -0
- package/src/telemetry/scheduler.ts +386 -0
- package/src/telemetry/sender.ts +97 -0
- package/src/telemetry/state.ts +334 -0
- package/src/types/combo-jev-effort-mode.ts +11 -0
- package/src/types/config.ts +3 -0
- package/src/types/provider.ts +11 -1
- package/src/types/request.ts +2 -0
- package/src/types/telemetry.ts +15 -0
- package/src/types/wire.ts +10 -0
- package/src/types.ts +5 -0
- package/src/update/index.ts +7 -1
- package/src/update/minisign.d.mts +17 -0
- package/src/update/minisign.mjs +88 -0
- package/src/update/signed-supply.d.mts +59 -0
- package/src/update/signed-supply.mjs +289 -0
- package/src/update/trust-root.json +38 -0
- package/src/update/update-bounded-json.d.mts +4 -0
- package/src/update/update-bounded-json.mjs +113 -0
- package/src/update/update-bytes.d.mts +2 -0
- package/src/update/update-bytes.mjs +23 -0
- package/src/update/update-candidate-id.d.mts +2 -0
- package/src/update/update-candidate-id.mjs +23 -0
- package/src/update/update-checkpoint-codec.d.mts +33 -0
- package/src/update/update-checkpoint-codec.mjs +217 -0
- package/src/update/update-contracts.d.mts +24 -0
- package/src/update/update-contracts.mjs +207 -0
- package/src/update/update-signature-threshold.d.mts +12 -0
- package/src/update/update-signature-threshold.mjs +47 -0
- package/src/update/update-signed-version.d.mts +5 -0
- package/src/update/update-signed-version.mjs +25 -0
- package/src/update/update-trust-anchor.d.mts +2 -0
- package/src/update/update-trust-anchor.mjs +2 -0
- package/src/usage/cache-diagnostic.ts +195 -50
- package/src/usage/expected-prices.ts +20 -2
- package/src/usage/filter-index.ts +1469 -0
- package/src/usage/ledger-scanner.ts +245 -4
- package/src/usage/source-filter.ts +10 -2
- package/src/usage/summary.ts +16 -0
- package/src/web-search/parse.ts +265 -38
- package/gui/dist/assets/App-BepstgEN.js +0 -59
- package/gui/dist/assets/Tray-BinxbEZo.js +0 -1
- package/gui/dist/assets/brand-mark-wzPe3R-J.js +0 -1
- package/gui/dist/assets/index-C-9X2yaI.js +0 -94
- package/gui/dist/assets/index-bnUWypyz.css +0 -2
- package/gui/dist/classic/assets/App-DX88_GCW.js +0 -58
- package/gui/dist/classic/assets/index-fSP71pLB.js +0 -94
- package/gui/dist/classic/assets/quota-summary-y2fl0WBv.js +0 -1
|
@@ -0,0 +1,363 @@
|
|
|
1
|
+
use std::io::{BufReader, Write};
|
|
2
|
+
use std::net::{SocketAddr, TcpListener, TcpStream};
|
|
3
|
+
use std::process::{Child, ChildStdin};
|
|
4
|
+
use std::thread;
|
|
5
|
+
use std::time::{Duration, Instant};
|
|
6
|
+
|
|
7
|
+
use serde::Deserialize;
|
|
8
|
+
|
|
9
|
+
use crate::http_wire::{read_bounded_body, read_response_head, response_framing};
|
|
10
|
+
use crate::platform::{
|
|
11
|
+
SecretBytes, constant_time_eq, now_epoch_ms, process_is_alive, random_hex, spawn_detached_serve,
|
|
12
|
+
};
|
|
13
|
+
use crate::relay_auth::compute_child_shutdown_proof;
|
|
14
|
+
use crate::state::{ChildPaths, ChildStateV1, write_child_state};
|
|
15
|
+
|
|
16
|
+
#[derive(Debug, Clone, Deserialize)]
|
|
17
|
+
#[serde(rename_all = "camelCase")]
|
|
18
|
+
pub struct LocalHealthz {
|
|
19
|
+
pub service: String,
|
|
20
|
+
pub role: Option<String>,
|
|
21
|
+
pub ephemeral_child: Option<bool>,
|
|
22
|
+
pub pid: u32,
|
|
23
|
+
pub port: u16,
|
|
24
|
+
pub started_at: Option<String>,
|
|
25
|
+
#[serde(default)]
|
|
26
|
+
pub generation_committed: Option<bool>,
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
pub fn pick_ephemeral_link_port() -> Result<u16, String> {
|
|
30
|
+
let seed_hex = random_hex(2).unwrap_or_else(|_| "0000".to_owned());
|
|
31
|
+
let offset = u16::from_str_radix(&seed_hex, 16).unwrap_or(0) % 10_000;
|
|
32
|
+
for step in 0..64u16 {
|
|
33
|
+
let candidate = 20_000 + ((offset.wrapping_add(step.wrapping_mul(137))) % 10_000);
|
|
34
|
+
if let Ok(listener) = TcpListener::bind(SocketAddr::from(([127, 0, 0, 1], candidate))) {
|
|
35
|
+
let p = listener.local_addr().map(|a| a.port()).unwrap_or(candidate);
|
|
36
|
+
drop(listener);
|
|
37
|
+
return Ok(p);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
let listener = TcpListener::bind(SocketAddr::from(([127, 0, 0, 1], 0)))
|
|
41
|
+
.map_err(|e| format!("failed to allocate loopback port: {e}"))?;
|
|
42
|
+
let port = listener
|
|
43
|
+
.local_addr()
|
|
44
|
+
.map_err(|e| format!("local_addr failed: {e}"))?
|
|
45
|
+
.port();
|
|
46
|
+
Ok(port)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
pub fn probe_local_healthz(port: u16, timeout: Duration) -> Option<LocalHealthz> {
|
|
50
|
+
let addr = SocketAddr::from(([127, 0, 0, 1], port));
|
|
51
|
+
let mut stream = TcpStream::connect_timeout(&addr, timeout).ok()?;
|
|
52
|
+
let _ = stream.set_read_timeout(Some(timeout));
|
|
53
|
+
let _ = stream.set_write_timeout(Some(timeout));
|
|
54
|
+
let req =
|
|
55
|
+
format!("GET /healthz HTTP/1.1\r\nHost: 127.0.0.1:{port}\r\nConnection: close\r\n\r\n");
|
|
56
|
+
stream.write_all(req.as_bytes()).ok()?;
|
|
57
|
+
stream.flush().ok()?;
|
|
58
|
+
let mut reader = BufReader::new(stream);
|
|
59
|
+
let head = read_response_head(&mut reader).ok()?;
|
|
60
|
+
if head.status != 200 {
|
|
61
|
+
return None;
|
|
62
|
+
}
|
|
63
|
+
let framing = response_framing(head.status, "GET", &head.headers).ok()?;
|
|
64
|
+
let body = read_bounded_body(&mut reader, framing, 64 * 1024).ok()?;
|
|
65
|
+
serde_json::from_slice(&body).ok()
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
pub fn is_port_bound(port: u16) -> bool {
|
|
69
|
+
TcpStream::connect_timeout(
|
|
70
|
+
&SocketAddr::from(([127, 0, 0, 1], port)),
|
|
71
|
+
Duration::from_millis(150),
|
|
72
|
+
)
|
|
73
|
+
.is_ok()
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
pub fn ensure_port_available_for_attach(port: u16, owned_pid: Option<u32>) -> Result<(), String> {
|
|
77
|
+
if let Some(health) = probe_local_healthz(port, Duration::from_millis(500)) {
|
|
78
|
+
if health.service == "opencodex"
|
|
79
|
+
&& health.ephemeral_child == Some(true)
|
|
80
|
+
&& owned_pid == Some(health.pid)
|
|
81
|
+
{
|
|
82
|
+
return Ok(());
|
|
83
|
+
}
|
|
84
|
+
return Err(format!(
|
|
85
|
+
"ocx-child-conflict:child_port_busy: attach refused: 127.0.0.1:{port} is already served by foreign listener (pid {})",
|
|
86
|
+
health.pid
|
|
87
|
+
));
|
|
88
|
+
}
|
|
89
|
+
if TcpListener::bind(SocketAddr::from(([127, 0, 0, 1], port))).is_err() {
|
|
90
|
+
return Err(format!(
|
|
91
|
+
"ocx-child-conflict:child_port_busy: attach refused: 127.0.0.1:{port} is already in use"
|
|
92
|
+
));
|
|
93
|
+
}
|
|
94
|
+
Ok(())
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
fn send_attested_shutdown(port: u16, shutdown_secret: &str) -> bool {
|
|
98
|
+
let Ok(nonce) = random_hex(32) else {
|
|
99
|
+
return false;
|
|
100
|
+
};
|
|
101
|
+
let addr = SocketAddr::from(([127, 0, 0, 1], port));
|
|
102
|
+
let Ok(mut stream) = TcpStream::connect_timeout(&addr, Duration::from_millis(500)) else {
|
|
103
|
+
return false;
|
|
104
|
+
};
|
|
105
|
+
let _ = stream.set_read_timeout(Some(Duration::from_millis(500)));
|
|
106
|
+
let _ = stream.set_write_timeout(Some(Duration::from_millis(500)));
|
|
107
|
+
let req = format!(
|
|
108
|
+
"POST /__ocx_child/shutdown HTTP/1.1\r\nHost: 127.0.0.1:{port}\r\nx-ocx-child-shutdown-challenge: {nonce}\r\nContent-Length: 0\r\nConnection: keep-alive\r\n\r\n"
|
|
109
|
+
);
|
|
110
|
+
if stream.write_all(req.as_bytes()).is_err() || stream.flush().is_err() {
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
let Ok(read_stream) = stream.try_clone() else {
|
|
114
|
+
return false;
|
|
115
|
+
};
|
|
116
|
+
let mut reader = BufReader::new(read_stream);
|
|
117
|
+
let Ok(head) = read_response_head(&mut reader) else {
|
|
118
|
+
return false;
|
|
119
|
+
};
|
|
120
|
+
let proof_count = head
|
|
121
|
+
.headers
|
|
122
|
+
.iter()
|
|
123
|
+
.filter(|(k, _)| k.eq_ignore_ascii_case("x-ocx-child-shutdown-proof"))
|
|
124
|
+
.count();
|
|
125
|
+
let nonce_count = head
|
|
126
|
+
.headers
|
|
127
|
+
.iter()
|
|
128
|
+
.filter(|(k, _)| k.eq_ignore_ascii_case("x-ocx-child-shutdown-nonce"))
|
|
129
|
+
.count();
|
|
130
|
+
let Some(server_nonce) = head.header("x-ocx-child-shutdown-nonce") else {
|
|
131
|
+
return false;
|
|
132
|
+
};
|
|
133
|
+
let binding = format!("{nonce}{server_nonce}");
|
|
134
|
+
let Some(expected) = compute_child_shutdown_proof(shutdown_secret, "listener", &binding) else {
|
|
135
|
+
return false;
|
|
136
|
+
};
|
|
137
|
+
let Some(caller) = compute_child_shutdown_proof(shutdown_secret, "caller", &binding) else {
|
|
138
|
+
return false;
|
|
139
|
+
};
|
|
140
|
+
let lengths: Vec<&str> = head
|
|
141
|
+
.headers
|
|
142
|
+
.iter()
|
|
143
|
+
.filter(|(k, _)| k.eq_ignore_ascii_case("content-length"))
|
|
144
|
+
.map(|(_, v)| v.as_str())
|
|
145
|
+
.collect();
|
|
146
|
+
if head.status != 204
|
|
147
|
+
|| proof_count != 1
|
|
148
|
+
|| nonce_count != 1
|
|
149
|
+
|| !crate::relay_auth::is_valid_hex64(server_nonce)
|
|
150
|
+
|| lengths != ["0"]
|
|
151
|
+
|| head
|
|
152
|
+
.headers
|
|
153
|
+
.iter()
|
|
154
|
+
.any(|(k, _)| k.eq_ignore_ascii_case("transfer-encoding"))
|
|
155
|
+
|| !head
|
|
156
|
+
.header("x-ocx-child-shutdown-proof")
|
|
157
|
+
.is_some_and(|proof| constant_time_eq(proof.as_bytes(), expected.as_bytes()))
|
|
158
|
+
{
|
|
159
|
+
return false;
|
|
160
|
+
}
|
|
161
|
+
let req = format!(
|
|
162
|
+
"POST /__ocx_child/shutdown HTTP/1.1\r\nHost: 127.0.0.1:{port}\r\nx-ocx-child-shutdown-proof: {caller}\r\nContent-Length: 0\r\nConnection: close\r\n\r\n"
|
|
163
|
+
);
|
|
164
|
+
if stream.write_all(req.as_bytes()).is_err() || stream.flush().is_err() {
|
|
165
|
+
return false;
|
|
166
|
+
}
|
|
167
|
+
read_response_head(&mut reader).is_ok_and(|h| h.status == 200)
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
pub fn stop_owned_daemon(state: &ChildStateV1) -> Result<(), String> {
|
|
171
|
+
let port = state.listen_port;
|
|
172
|
+
let live_health = probe_local_healthz(port, Duration::from_millis(500));
|
|
173
|
+
let observed_pid = match &live_health {
|
|
174
|
+
Some(h)
|
|
175
|
+
if h.service == "opencodex"
|
|
176
|
+
&& h.ephemeral_child == Some(true)
|
|
177
|
+
&& h.port == port
|
|
178
|
+
&& h.pid != 0 =>
|
|
179
|
+
{
|
|
180
|
+
h.pid
|
|
181
|
+
}
|
|
182
|
+
None if !is_port_bound(port)
|
|
183
|
+
&& state.daemon_pid.is_none_or(|pid| !process_is_alive(pid)) =>
|
|
184
|
+
{
|
|
185
|
+
return Ok(());
|
|
186
|
+
}
|
|
187
|
+
_ => return Err("cannot establish that the child daemon has stopped".to_owned()),
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
if !send_attested_shutdown(port, &state.shutdown_secret) {
|
|
191
|
+
return Err("child daemon did not accept the authenticated shutdown".to_owned());
|
|
192
|
+
}
|
|
193
|
+
let deadline = Instant::now() + Duration::from_secs(3);
|
|
194
|
+
while Instant::now() < deadline {
|
|
195
|
+
let port_down = !is_port_bound(port);
|
|
196
|
+
let pid_down = !process_is_alive(observed_pid);
|
|
197
|
+
if port_down && pid_down {
|
|
198
|
+
return Ok(());
|
|
199
|
+
}
|
|
200
|
+
thread::sleep(Duration::from_millis(40));
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
Err(format!(
|
|
204
|
+
"child daemon on 127.0.0.1:{port} did not stop; refusing unverified termination"
|
|
205
|
+
))
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/// A daemon that is ready but not yet committed. It answers only `/healthz` until
|
|
209
|
+
/// `commit` writes the commit line for its generation. Dropping it uncommitted kills the
|
|
210
|
+
/// child; if this process dies instead, the child sees EOF on stdin and exits without serving.
|
|
211
|
+
pub struct PendingDaemon {
|
|
212
|
+
child: Option<Child>,
|
|
213
|
+
stdin: Option<ChildStdin>,
|
|
214
|
+
generation: String,
|
|
215
|
+
pid: u32,
|
|
216
|
+
started_ms: u64,
|
|
217
|
+
port: u16,
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
impl PendingDaemon {
|
|
221
|
+
pub fn pid(&self) -> u32 {
|
|
222
|
+
self.pid
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/// Commit the generation. Call only after the state naming this generation and PID is
|
|
226
|
+
/// durable (and, for attach, after the rollback record is gone).
|
|
227
|
+
pub fn commit(mut self) -> Result<(u32, u64), String> {
|
|
228
|
+
crate::test_hooks::barrier("daemon-before-commit");
|
|
229
|
+
let mut stdin = self.stdin.take().ok_or("spawned child stdin unavailable")?;
|
|
230
|
+
stdin
|
|
231
|
+
.write_all(
|
|
232
|
+
format!(
|
|
233
|
+
"commit {}
|
|
234
|
+
",
|
|
235
|
+
self.generation
|
|
236
|
+
)
|
|
237
|
+
.as_bytes(),
|
|
238
|
+
)
|
|
239
|
+
.and_then(|()| stdin.flush())
|
|
240
|
+
.map_err(|e| format!("failed to commit spawned child: {e}"))?;
|
|
241
|
+
drop(stdin);
|
|
242
|
+
let deadline = Instant::now() + Duration::from_secs(5);
|
|
243
|
+
while Instant::now() < deadline {
|
|
244
|
+
if let Some(child) = self.child.as_mut()
|
|
245
|
+
&& let Some(status) = child
|
|
246
|
+
.try_wait()
|
|
247
|
+
.map_err(|e| format!("failed to observe spawned child: {e}"))?
|
|
248
|
+
{
|
|
249
|
+
return Err(format!("ocx-child serve exited during commit: {status}"));
|
|
250
|
+
}
|
|
251
|
+
if let Some(h) = probe_local_healthz(self.port, Duration::from_millis(400))
|
|
252
|
+
&& h.pid == self.pid
|
|
253
|
+
&& h.generation_committed == Some(true)
|
|
254
|
+
{
|
|
255
|
+
// Leave the committed child running; dropping a std Child does not kill it.
|
|
256
|
+
self.child = None;
|
|
257
|
+
return Ok((self.pid, self.started_ms));
|
|
258
|
+
}
|
|
259
|
+
thread::sleep(Duration::from_millis(20));
|
|
260
|
+
}
|
|
261
|
+
Err(format!(
|
|
262
|
+
"ocx-child serve on 127.0.0.1:{} did not confirm its commit",
|
|
263
|
+
self.port
|
|
264
|
+
))
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
impl Drop for PendingDaemon {
|
|
269
|
+
fn drop(&mut self) {
|
|
270
|
+
if let Some(mut child) = self.child.take() {
|
|
271
|
+
let _ = child.kill();
|
|
272
|
+
let _ = child.wait();
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/// Concatenate parts into a buffer allocated once at its final size and wiped on drop.
|
|
278
|
+
fn secret_line(parts: &[&[u8]]) -> SecretBytes {
|
|
279
|
+
let mut line = Vec::with_capacity(parts.iter().map(|p| p.len()).sum());
|
|
280
|
+
for part in parts {
|
|
281
|
+
line.extend_from_slice(part);
|
|
282
|
+
}
|
|
283
|
+
SecretBytes::new(line)
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/// Stop the owned daemon and start a new generation. The new generation is recorded in
|
|
287
|
+
/// state before the spawn (the child's start gate requires it) and the PID after readiness;
|
|
288
|
+
/// the caller commits once its own durable work is done.
|
|
289
|
+
pub fn start_or_rollover_daemon(
|
|
290
|
+
paths: &ChildPaths,
|
|
291
|
+
state: &mut ChildStateV1,
|
|
292
|
+
min_started_at_ms: Option<u64>,
|
|
293
|
+
) -> Result<PendingDaemon, String> {
|
|
294
|
+
stop_owned_daemon(state)?;
|
|
295
|
+
if let Some(floor_ms) = min_started_at_ms {
|
|
296
|
+
let now = now_epoch_ms();
|
|
297
|
+
if now < floor_ms {
|
|
298
|
+
thread::sleep(Duration::from_millis((floor_ms - now).min(100)));
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
ensure_port_available_for_attach(state.listen_port, None)?;
|
|
302
|
+
let exe = std::env::current_exe()
|
|
303
|
+
.map_err(|e| format!("failed to resolve current executable: {e}"))?;
|
|
304
|
+
let generation = random_hex(16)?;
|
|
305
|
+
state.daemon_pid = None;
|
|
306
|
+
state.daemon_started_at_ms = None;
|
|
307
|
+
state.daemon_generation = Some(generation.clone());
|
|
308
|
+
write_child_state(&paths.state_file, state)?;
|
|
309
|
+
let start_line = secret_line(&[
|
|
310
|
+
b"ocx-child-start/1 ",
|
|
311
|
+
generation.as_bytes(),
|
|
312
|
+
b" ",
|
|
313
|
+
state.shutdown_secret.as_bytes(),
|
|
314
|
+
b"\n",
|
|
315
|
+
]);
|
|
316
|
+
let spawned = spawn_detached_serve(
|
|
317
|
+
&exe,
|
|
318
|
+
&paths.opencodex_home,
|
|
319
|
+
&paths.codex_home,
|
|
320
|
+
state.listen_port,
|
|
321
|
+
start_line.as_slice(),
|
|
322
|
+
)?;
|
|
323
|
+
let spawned_pid = spawned.id();
|
|
324
|
+
let mut pending = PendingDaemon {
|
|
325
|
+
child: Some(spawned),
|
|
326
|
+
stdin: None,
|
|
327
|
+
generation,
|
|
328
|
+
pid: spawned_pid,
|
|
329
|
+
started_ms: 0,
|
|
330
|
+
port: state.listen_port,
|
|
331
|
+
};
|
|
332
|
+
pending.stdin = pending.child.as_mut().and_then(|c| c.stdin.take());
|
|
333
|
+
|
|
334
|
+
let deadline = Instant::now() + Duration::from_secs(10);
|
|
335
|
+
while Instant::now() < deadline {
|
|
336
|
+
if let Some(child) = pending.child.as_mut()
|
|
337
|
+
&& let Some(status) = child
|
|
338
|
+
.try_wait()
|
|
339
|
+
.map_err(|e| format!("failed to observe spawned child: {e}"))?
|
|
340
|
+
{
|
|
341
|
+
return Err(format!("ocx-child serve exited before readiness: {status}"));
|
|
342
|
+
}
|
|
343
|
+
if let Some(h) = probe_local_healthz(state.listen_port, Duration::from_millis(400))
|
|
344
|
+
&& h.service == "opencodex"
|
|
345
|
+
&& h.role.as_deref() == Some("client")
|
|
346
|
+
&& h.ephemeral_child == Some(true)
|
|
347
|
+
&& h.pid == spawned_pid
|
|
348
|
+
{
|
|
349
|
+
let started_ms = now_epoch_ms().max(min_started_at_ms.unwrap_or(0));
|
|
350
|
+
state.daemon_pid = Some(spawned_pid);
|
|
351
|
+
state.daemon_started_at_ms = Some(started_ms);
|
|
352
|
+
write_child_state(&paths.state_file, state)
|
|
353
|
+
.map_err(|e| format!("failed to record spawned child: {e}"))?;
|
|
354
|
+
pending.started_ms = started_ms;
|
|
355
|
+
return Ok(pending);
|
|
356
|
+
}
|
|
357
|
+
thread::sleep(Duration::from_millis(50));
|
|
358
|
+
}
|
|
359
|
+
Err(format!(
|
|
360
|
+
"ocx-child serve failed to answer /healthz on 127.0.0.1:{}",
|
|
361
|
+
state.listen_port
|
|
362
|
+
))
|
|
363
|
+
}
|