From da38916c20f3034377d41f7e1eb25eb6009afe94 Mon Sep 17 00:00:00 2001 From: pakrym-oai Date: Tue, 10 Mar 2026 10:24:23 -0700 Subject: [PATCH] codex: fix Linux CI for code_mode --- codex-rs/code-mode/BUILD.bazel | 12 +++ codex-rs/code-mode/src/libc_stack_end_shim.cc | 25 +++++ codex-rs/core/src/tools/context.rs | 94 ------------------- codex-rs/core/src/tools/registry.rs | 7 -- 4 files changed, 37 insertions(+), 101 deletions(-) create mode 100644 codex-rs/code-mode/src/libc_stack_end_shim.cc diff --git a/codex-rs/code-mode/BUILD.bazel b/codex-rs/code-mode/BUILD.bazel index 0119c6e7cd..017c6810ad 100644 --- a/codex-rs/code-mode/BUILD.bazel +++ b/codex-rs/code-mode/BUILD.bazel @@ -1,7 +1,19 @@ +load("@rules_cc//cc:defs.bzl", "cc_library") load("//:defs.bzl", "codex_rust_crate") codex_rust_crate( name = "code-mode", crate_name = "codex_code_mode", compile_data = ["src/code_mode_bridge.js"], + deps_extra = select({ + "@platforms//os:linux": [":libc_stack_end_shim"], + "//conditions:default": [], + }), +) + +cc_library( + name = "libc_stack_end_shim", + srcs = ["src/libc_stack_end_shim.cc"], + target_compatible_with = ["@platforms//os:linux"], + visibility = ["//visibility:private"], ) diff --git a/codex-rs/code-mode/src/libc_stack_end_shim.cc b/codex-rs/code-mode/src/libc_stack_end_shim.cc new file mode 100644 index 0000000000..9e5ba6441e --- /dev/null +++ b/codex-rs/code-mode/src/libc_stack_end_shim.cc @@ -0,0 +1,25 @@ +// Bazel's Linux linker setup does not currently resolve V8's +// `__libc_stack_end` reference from the system runtime. Provide a weak +// definition and initialize it from the current thread's stack bounds so the +// prebuilt archive can link and main-thread stack probing still has a sensible +// fallback. +#include +#include +#include + +extern "C" __attribute__((weak)) void* __libc_stack_end = nullptr; + +__attribute__((constructor)) static void init_libc_stack_end() { + pthread_attr_t attr; + if (pthread_getattr_np(pthread_self(), &attr) != 0) { + return; + } + + void* base = nullptr; + size_t size = 0; + if (pthread_attr_getstack(&attr, &base, &size) == 0 && base != nullptr) { + __libc_stack_end = static_cast(base) + size; + } + + pthread_attr_destroy(&attr); +} diff --git a/codex-rs/core/src/tools/context.rs b/codex-rs/core/src/tools/context.rs index b5e7995660..53060bd027 100644 --- a/codex-rs/core/src/tools/context.rs +++ b/codex-rs/core/src/tools/context.rs @@ -15,8 +15,6 @@ use codex_protocol::models::ResponseInputItem; use codex_protocol::models::ShellToolCallParams; use codex_protocol::models::function_call_output_content_items_to_text; use codex_utils_string::take_bytes_at_char_boundary; -use serde::Serialize; -use serde_json::Value as JsonValue; use std::borrow::Cow; use std::sync::Arc; use std::time::Duration; @@ -76,10 +74,6 @@ pub trait ToolOutput: Send { fn success_for_logging(&self) -> bool; fn to_response_item(&self, call_id: &str, payload: &ToolPayload) -> ResponseInputItem; - - fn code_mode_result(&self, payload: &ToolPayload) -> JsonValue { - response_input_to_code_mode_result(self.to_response_item("", payload)) - } } pub struct McpToolOutput { @@ -180,35 +174,6 @@ impl ToolOutput for ExecCommandToolOutput { Some(true), ) } - - fn code_mode_result(&self, _payload: &ToolPayload) -> JsonValue { - #[derive(Serialize)] - struct UnifiedExecCodeModeResult { - #[serde(skip_serializing_if = "Option::is_none")] - chunk_id: Option, - wall_time_seconds: f64, - #[serde(skip_serializing_if = "Option::is_none")] - exit_code: Option, - #[serde(skip_serializing_if = "Option::is_none")] - session_id: Option, - #[serde(skip_serializing_if = "Option::is_none")] - original_token_count: Option, - output: String, - } - - let result = UnifiedExecCodeModeResult { - chunk_id: (!self.chunk_id.is_empty()).then(|| self.chunk_id.clone()), - wall_time_seconds: self.wall_time.as_secs_f64(), - exit_code: self.exit_code, - session_id: self.process_id.clone(), - original_token_count: self.original_token_count, - output: self.truncated_output(), - }; - - serde_json::to_value(result).unwrap_or_else(|err| { - JsonValue::String(format!("failed to serialize exec result: {err}")) - }) - } } impl ExecCommandToolOutput { @@ -247,65 +212,6 @@ impl ExecCommandToolOutput { } } -fn response_input_to_code_mode_result(response: ResponseInputItem) -> JsonValue { - match response { - ResponseInputItem::Message { content, .. } => content_items_to_code_mode_result( - &content - .into_iter() - .map(|item| match item { - codex_protocol::models::ContentItem::InputText { text } - | codex_protocol::models::ContentItem::OutputText { text } => { - FunctionCallOutputContentItem::InputText { text } - } - codex_protocol::models::ContentItem::InputImage { image_url } => { - FunctionCallOutputContentItem::InputImage { - image_url, - detail: None, - } - } - }) - .collect::>(), - ), - ResponseInputItem::FunctionCallOutput { output, .. } - | ResponseInputItem::CustomToolCallOutput { output, .. } => match output.body { - FunctionCallOutputBody::Text(text) => JsonValue::String(text), - FunctionCallOutputBody::ContentItems(items) => { - content_items_to_code_mode_result(&items) - } - }, - ResponseInputItem::McpToolCallOutput { result, .. } => match result { - Ok(result) => match FunctionCallOutputPayload::from(&result).body { - FunctionCallOutputBody::Text(text) => JsonValue::String(text), - FunctionCallOutputBody::ContentItems(items) => { - content_items_to_code_mode_result(&items) - } - }, - Err(error) => JsonValue::String(error), - }, - } -} - -fn content_items_to_code_mode_result(items: &[FunctionCallOutputContentItem]) -> JsonValue { - JsonValue::String( - items - .iter() - .filter_map(|item| match item { - FunctionCallOutputContentItem::InputText { text } if !text.trim().is_empty() => { - Some(text.clone()) - } - FunctionCallOutputContentItem::InputImage { image_url, .. } - if !image_url.trim().is_empty() => - { - Some(image_url.clone()) - } - FunctionCallOutputContentItem::InputText { .. } - | FunctionCallOutputContentItem::InputImage { .. } => None, - }) - .collect::>() - .join("\n"), - ) -} - fn function_tool_response( call_id: &str, payload: &ToolPayload, diff --git a/codex-rs/core/src/tools/registry.rs b/codex-rs/core/src/tools/registry.rs index f78df2f1a7..a571013512 100644 --- a/codex-rs/core/src/tools/registry.rs +++ b/codex-rs/core/src/tools/registry.rs @@ -72,13 +72,6 @@ impl AnyToolResult { } = self; result.to_response_item(&call_id, &payload) } - - pub(crate) fn code_mode_result(self) -> serde_json::Value { - let Self { - payload, result, .. - } = self; - result.code_mode_result(&payload) - } } #[async_trait]