Skip to content

Commit 65c8b8b

Browse files
Qualify the final Python SDK wheel on macOS
2 parents 724a83f + 2c3046c commit 65c8b8b

11 files changed

Lines changed: 426 additions & 34 deletions

‎.github/workflows/check-managed-macos-command.yml‎

Lines changed: 89 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Check the managed local command in the Python wheel
1+
name: Qualify the final Python SDK wheel on macOS
22
on:
33
push:
44
branches: [test/managed-macos-command-qualification-20261104]
@@ -9,9 +9,9 @@ permissions:
99

1010
jobs:
1111
build:
12-
name: Build and test the exact Python SDK package
12+
name: Qualify final Python SDK wheel and tests
1313
runs-on: macos-latest
14-
timeout-minutes: 120
14+
timeout-minutes: 180
1515
defaults:
1616
run:
1717
shell: bash
@@ -24,12 +24,12 @@ jobs:
2424
steps:
2525
- uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803
2626
with:
27-
ref: 902b813d72a79ab325defa61c6c77fd1a90b1a94
27+
ref: 2c3046ca903a513548c847b6a85f16c365efd750
2828
persist-credentials: false
2929
- name: Verify exact SDK checkout
3030
run: |
31-
test "$(git rev-parse HEAD)" = '902b813d72a79ab325defa61c6c77fd1a90b1a94'
32-
test "$(git rev-parse HEAD^{tree})" = 'f2d7539c176cc795ae826b531dfd40fc8cdb3863'
31+
test "$(git rev-parse HEAD)" = '2c3046ca903a513548c847b6a85f16c365efd750'
32+
test "$(git rev-parse HEAD^{tree})" = '88d2ef6d320399e0f9d11994a21ad4dbb398ecd3'
3333
git show -s --format='%H %T %s' HEAD
3434
- name: Select shared Cargo directories
3535
run: |
@@ -156,39 +156,102 @@ jobs:
156156
PYTHON
157157
- name: Build the SDK and native runtime together
158158
run: uv build --wheel "$OPENAI_AGENTS_BUILD_SOURCE" --out-dir "$GITHUB_WORKSPACE/dist"
159-
- name: Run public local Session tests against the installed wheel
159+
- name: Build the shared test executor
160+
if: steps.native-source.outputs.run_id == ''
161+
run: |
162+
python - <<'PYTHON'
163+
import os
164+
import shutil
165+
import subprocess
166+
import sys
167+
from pathlib import Path
168+
root = Path(os.environ["OPENAI_AGENTS_BUILD_SOURCE"]) / "packages/codex"
169+
fixture = Path("tests/fixtures/exec_server.rs")
170+
(root / fixture).parent.mkdir(parents=True, exist_ok=True)
171+
shutil.copy2(Path("packages/codex") / fixture, root / fixture)
172+
sys.path.insert(0, str(root / "scripts"))
173+
from native_env import build_environment, cargo_build_options
174+
environment = build_environment(root)
175+
options, build_target = cargo_build_options(root, environment)
176+
subprocess.run(["cargo", "build", "--release", "--locked", "--timings", *options, "--example", "exec-server-fixture"], cwd=root, env=environment, check=True)
177+
target = Path(environment["CARGO_TARGET_DIR"])
178+
if not target.is_absolute():
179+
target = root / target
180+
if build_target:
181+
target /= Path(build_target).stem
182+
name = "exec-server-fixture" + (".exe" if os.name == "nt" else "")
183+
shutil.copy2(target / "release" / "examples" / name, Path("dist") / name)
184+
PYTHON
185+
- name: Test native macOS Unix socket policy with the installed wheel
186+
if: runner.os == 'macOS'
187+
run: |
188+
stage=$(mktemp -d "$RUNNER_TEMP/macos-ipc.XXXXXX")
189+
mkdir -p "$stage/tests/qualification" "$stage/home"
190+
cp tests/{__init__,_asyncio,native_executor_fixtures,native_fixtures}.py "$stage/tests"
191+
cp tests/qualification/{macos_ipc,offline_ipc,unix_socket_command}.py "$stage/tests/qualification"
192+
uv venv --seed --python "$(command -v python)" "$stage/venv"
193+
uv export --frozen --group dev --no-emit-workspace --no-hashes \
194+
--format requirements.txt --output-file "$stage/constraints.txt" > /dev/null
195+
python="$stage/venv/bin/python"
196+
"$python" -m pip install --constraint "$stage/constraints.txt" dist/*.whl aiohttp pytest pytest-asyncio
197+
fixture="$GITHUB_WORKSPACE/dist/exec-server-fixture"
198+
chmod +x "$fixture"
199+
cd "$stage"
200+
env -i PATH=/usr/bin:/bin HOME="$stage/home" SHELL=/bin/sh \
201+
"$python" -I -B -u -c 'import os,runpy,sys; sys.path.insert(0,os.getcwd()); runpy.run_module("tests.qualification.macos_ipc",run_name="__main__")' "$fixture"
202+
- name: Run SDK acceptance with the installed wheel
160203
env:
161204
OPENAI_API_KEY: fake-for-tests
205+
CODEX_EXEC_SERVER_TEST_BINARY: ${{ github.workspace }}/dist/exec-server-fixture
206+
RUST_MIN_STACK: '16777216'
162207
run: |
163208
set -eu
164209
sw_vers
165210
uv sync --frozen --group dev --no-install-project
166211
uv pip install --python .venv/bin/python --no-deps dist/*.whl
167-
.venv/bin/python -I -B - "$RUNNER_TEMP/local-session.xml" <<'PYTEST'
168-
import os, pathlib, platform, sys, sysconfig
169-
import agents._native, pytest
170-
assert sys.platform == "darwin" and platform.machine() == "arm64"
171-
assert pathlib.Path(agents._native.__file__).resolve().is_relative_to(pathlib.Path(sysconfig.get_path("platlib")).resolve())
212+
env -u PYTHONPATH -u OPENAI_AGENTS_TEST_IN_CODEX_SANDBOX .venv/bin/python -I -B - "$RUNNER_TEMP/local-session.xml" <<'PYTEST'
213+
import hashlib, os, pathlib, platform, sys, sysconfig, xml.etree.ElementTree as ET
214+
import agents._native, agents.session, pytest
215+
assert sys.platform == 'darwin' and platform.machine() == 'arm64'
216+
installed = pathlib.Path(sysconfig.get_path('platlib')).resolve()
217+
for module in (agents, agents._native, agents.session):
218+
assert pathlib.Path(module.__file__).resolve().is_relative_to(installed), module.__file__
219+
assert hashlib.sha256(pathlib.Path(agents.session.__file__).read_bytes()).hexdigest() == '69954f1f52e07e323fd99d7354c044a2d566c5d1decec48c98a867e2eaed3797'
172220
sys.path.insert(0, os.getcwd())
173-
sys.exit(pytest.main(["-q", "-rA", "tests/test_native_executor_local.py", "--junitxml="+sys.argv[1]]))
221+
junit = sys.argv[1]
222+
status = pytest.main([
223+
'-q', '-rA', '--junitxml=' + junit,
224+
'tests/test_native_executor_local.py',
225+
'tests/test_native_yielded_executor_owner.py',
226+
'tests/test_native_executor_policies.py::test_force_stop_fences_sampling_before_executor_result_settles',
227+
'tests/test_supervised_worker_example.py::test_two_workers_revise_reconnect_recover_and_publish_once',
228+
])
229+
if status:
230+
sys.exit(status)
231+
required = {
232+
'test_macos_session_close_stops_child_after_command_exit',
233+
'test_macos_local_command_cannot_delegate_execution_to_launchd',
234+
'test_local_write_root_replacement_survives_portable_resume',
235+
'test_turn_write_policy_and_reopen_checks_the_applied_policy',
236+
'test_two_workers_revise_reconnect_recover_and_publish_once',
237+
}
238+
required.update(
239+
f'test_force_stop_fences_sampling_before_executor_result_settles[{case}]'
240+
for case in ('turn-False', 'turn-True', 'continuation-False', 'graph-False', 'close-False')
241+
)
242+
passed = {
243+
case.attrib['name'] for case in ET.parse(junit).findall('.//testcase')
244+
if all(case.find(tag) is None for tag in ('skipped', 'failure', 'error'))
245+
}
246+
assert required <= passed, sorted(required - passed)
247+
print('Verified installed-wheel cases:', sorted(required))
174248
PYTEST
175-
python3 - "$RUNNER_TEMP/local-session.xml" <<'VERIFY'
176-
import sys
177-
import xml.etree.ElementTree as ET
178-
cases = ET.parse(sys.argv[1]).findall(".//testcase")
179-
required = {"test_macos_session_close_stops_child_after_command_exit",
180-
"test_local_write_root_replacement_survives_portable_resume",
181-
"test_turn_write_policy_and_reopen_checks_the_applied_policy"}
182-
passed = {c.get("name","").split("[")[0] for c in cases if all(c.find(tag) is None for tag in ("skipped","failure","error"))}
183-
assert required <= passed, sorted(required-passed)
184-
print("Verified installed-wheel cases:", sorted(required))
185-
VERIFY
186249
- uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9
187250
with:
188-
name: verified-managed-local-python-wheel
251+
name: verified-final-python-wheel-macos
189252
path: |
190253
dist/*.whl
191-
dist/*.json
254+
dist/*.pristine.json
192255
${{ runner.temp }}/local-session.xml
193256
if-no-files-found: error
194257
retention-days: 7
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
diff --git a/codex-rs/core/src/client.rs b/codex-rs/core/src/client.rs
2+
index b013cf6..4a62119 100644
3+
--- a/codex-rs/core/src/client.rs
4+
+++ b/codex-rs/core/src/client.rs
5+
@@ -1780,6 +1780,7 @@ impl ModelClientSession {
6+
client_setup.api_auth,
7+
)
8+
.with_telemetry(Some(request_telemetry), Some(sse_telemetry));
9+
+ crate::model_request::wait_if_sampling_stopped(prompt.tool_admission.as_deref()).await;
10+
crate::model_request::admit_request(&interceptors).map_err(CodexErr::InvalidRequest)?;
11+
let stream_result = client.stream_request(request, options).await;
12+
13+
@@ -2092,6 +2093,7 @@ impl ModelClientSession {
14+
),
15+
],
16+
);
17+
+ crate::model_request::wait_if_sampling_stopped(prompt.tool_admission.as_deref()).await;
18+
crate::model_request::admit_request(&interceptors).map_err(CodexErr::InvalidRequest)?;
19+
let stream_result = websocket_connection
20+
.stream_request(
21+
diff --git a/codex-rs/core/src/model_request.rs b/codex-rs/core/src/model_request.rs
22+
index c59b85a..e254bac 100644
23+
--- a/codex-rs/core/src/model_request.rs
24+
+++ b/codex-rs/core/src/model_request.rs
25+
@@ -7,10 +7,21 @@ use codex_extension_api::ModelRequestKind;
26+
use codex_extension_api::ModelResponseInterceptor;
27+
use codex_extension_api::ModelResponseStream;
28+
use codex_extension_api::ModelToolAdmission;
29+
+use codex_extension_api::TurnSamplingStop;
30+
use codex_protocol::models::ResponseItem;
31+
use std::collections::HashMap;
32+
use std::sync::Arc;
33+
34+
+pub(crate) async fn wait_if_sampling_stopped(admission: Option<&dyn ModelToolAdmission>) {
35+
+ if admission
36+
+ .and_then(|admission| admission.turn_store().get::<TurnSamplingStop>())
37+
+ .is_some_and(|stop| stop.requested())
38+
+ {
39+
+ // Keep the native interrupt responsible for aborting the task and joining cleanup.
40+
+ std::future::pending::<()>().await;
41+
+ }
42+
+}
43+
+
44+
pub(crate) fn prepare(
45+
contributors: &[Arc<dyn ModelRequestContributor>],
46+
thread_id: &str,
47+
diff --git a/codex-rs/ext/extension-api/src/lib.rs b/codex-rs/ext/extension-api/src/lib.rs
48+
index ea9ac3f..940dc5f 100644
49+
--- a/codex-rs/ext/extension-api/src/lib.rs
50+
+++ b/codex-rs/ext/extension-api/src/lib.rs
51+
@@ -124,6 +124,7 @@ pub use model_request::ModelResponseInterceptor;
52+
pub use model_request::ModelResponseStream;
53+
pub use model_request::ModelToolAdmission;
54+
pub use model_request::ResponseEvent;
55+
+pub use model_request::TurnSamplingStop;
56+
57+
/// SDK host context for recording the settings used to prepare a sampling request.
58+
pub struct SdkTurnReceiptSeed {
59+
diff --git a/codex-rs/ext/extension-api/src/model_request.rs b/codex-rs/ext/extension-api/src/model_request.rs
60+
index 692de0b..45fa289 100644
61+
--- a/codex-rs/ext/extension-api/src/model_request.rs
62+
+++ b/codex-rs/ext/extension-api/src/model_request.rs
63+
@@ -6,6 +6,23 @@ use codex_protocol::models::ResponseItem;
64+
use futures::stream::BoxStream;
65+
use std::collections::HashMap;
66+
use std::sync::Arc;
67+
+use std::sync::atomic::AtomicBool;
68+
+use std::sync::atomic::Ordering;
69+
+
70+
+/// Stops admission to model requests while the host interrupts this turn.
71+
+/// Install in the turn store and retain that same receipt when requesting stop.
72+
+#[derive(Debug, Default)]
73+
+pub struct TurnSamplingStop(AtomicBool);
74+
+
75+
+impl TurnSamplingStop {
76+
+ pub fn request(&self) {
77+
+ self.0.store(true, Ordering::Release);
78+
+ }
79+
+
80+
+ pub fn requested(&self) -> bool {
81+
+ self.0.load(Ordering::Acquire)
82+
+ }
83+
+}
84+
85+
/// Model response events owned by an interceptor or its downstream consumer.
86+
pub type ModelResponseStream = BoxStream<'static, Result<ResponseEvent, ModelResponseError>>;

‎packages/codex/patches/series.json‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,11 @@
115115
{
116116
"path": "0029-own-managed-darwin-command-cohort.patch",
117117
"sha256": "2df2d03f5f8c88124e6b54a9c85f02f1b9aff8b56e768f289eaa75c0971ccb43"
118+
},
119+
{
120+
"path": "0030-stop-owned-turn-sampling.patch",
121+
"sha256": "fbc98457cdfd3749088dbd32370eef5e1d3c4bc9fcbd7ca6ac3e9b4fb7cea713"
118122
}
119123
],
120-
"result_tree": "bb10b3bd4713f79bb2c18cb24c58d4b16a9e2086"
124+
"result_tree": "2acbe545cf0fc2ced2f5b6e32e84de1b7ace452d"
121125
}

‎packages/codex/rust/environment.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1303,7 +1303,7 @@ mod tests {
13031303
use std::fs;
13041304
use std::os::unix::fs::symlink;
13051305

1306-
use super::{EnvironmentManager, ExecServerRuntimeOptions, PathUri};
1306+
use super::{AbsolutePathBuf, EnvironmentManager, ExecServerRuntimeOptions};
13071307

13081308
let fixture = tempfile::tempdir()?;
13091309
let workspace = fixture.path().join("workspace");
@@ -1321,7 +1321,7 @@ mod tests {
13211321
let paths = ExecServerRuntimeOptions::new(launcher, None)?;
13221322
let manager = EnvironmentManager::create_for_tests(None, Some(paths)).await;
13231323
let environment = manager.try_local_environment().unwrap();
1324-
let write_root = PathUri::from_host_native_path(workspace.canonicalize()?)?;
1324+
let write_root = AbsolutePathBuf::try_from(workspace.canonicalize()?)?;
13251325

13261326
assert_eq!(
13271327
super::check_local_helper_write_roots(&environment, &[write_root])

‎packages/codex/rust/lib.rs‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1208,6 +1208,18 @@ impl NativeThread {
12081208
})
12091209
}
12101210

1211+
fn _request_sampling_stop(&self, expected_turn_id: &str) -> PyResult<()> {
1212+
if let Some(inner) = self.live()?
1213+
&& let Some(tools) = inner
1214+
.thread
1215+
.thread_extension_data()
1216+
.get::<python_tools::PythonTools>()
1217+
{
1218+
tools.request_sampling_stop(expected_turn_id);
1219+
}
1220+
Ok(())
1221+
}
1222+
12111223
#[pyo3(signature = (expected_turn_id, *, queued_compaction=false))]
12121224
fn _interrupt_session<'py>(
12131225
&self,
@@ -1241,7 +1253,7 @@ impl NativeThread {
12411253
.ok_or_else(|| py_error("Graph interruption requires a managed Session"))?;
12421254
let request = {
12431255
let _context_operation = inner.engine.operation(false).map_err(py_error)?;
1244-
tools.request_graph_interrupt().map_err(py_error)?
1256+
tools.request_graph_interrupt(force).map_err(py_error)?
12451257
};
12461258
async_bridge::future_into_py(py, async move {
12471259
let _request = request;

‎packages/codex/rust/native_agents.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1239,7 +1239,7 @@ mod steering_capture_tests {
12391239
.thread_extension_data()
12401240
.get::<crate::python_tools::PythonTools>()
12411241
.unwrap();
1242-
let _stop = stop.then(|| root_tools.request_graph_interrupt().unwrap());
1242+
let _stop = stop.then(|| root_tools.request_graph_interrupt(false).unwrap());
12431243
let successor = continue_retained(&state, &original).await.unwrap();
12441244
if stop {
12451245
assert!(successor.is_none());

‎packages/codex/rust/python_tools.rs‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use codex_extension_api::{
1010
ContextContributor, ExtensionData, ExtensionFuture, ThreadIdleInput,
1111
ThreadLifecycleContributor, ThreadStartInput, ThreadStopInput, ToolCallOutcome,
1212
ToolContributor, ToolFinishInput, ToolLifecycleContributor, ToolLifecycleFuture, ToolPolicy,
13-
TurnErrorInput, TurnLifecycleContributor, TurnStartInput, TurnStartPhase,
13+
TurnErrorInput, TurnLifecycleContributor, TurnSamplingStop, TurnStartInput, TurnStartPhase,
1414
WorldStateContributionInput, WorldStateSectionContribution,
1515
};
1616
use codex_protocol::dynamic_tools::{
@@ -210,6 +210,7 @@ pub(crate) struct TurnAdmission {
210210
state: watch::Sender<AdmissionState>,
211211
completed_calls: Mutex<HashSet<String>>,
212212
graceful_requested: AtomicBool,
213+
sampling_stop: Arc<TurnSamplingStop>,
213214
// A turn born while the graph is stopped must stay fenced even if a
214215
// later explicit submission starts a new graph generation.
215216
graph_fenced: bool,
@@ -516,9 +517,22 @@ pub fn checkpoint_specs(specs: &[DynamicToolSpec]) -> Result<Value> {
516517
}
517518

518519
impl PythonTools {
520+
pub(crate) fn request_sampling_stop(&self, turn_id: &str) {
521+
let admission = self
522+
.admission
523+
.lock()
524+
.expect("Python admission receipt poisoned");
525+
if let Some(receipt) = admission
526+
.as_ref()
527+
.filter(|receipt| receipt.turn_id == turn_id)
528+
{
529+
receipt.sampling_stop.request();
530+
}
531+
}
532+
519533
/// Reserve the stop before returning an awaitable to Python. Native tools
520534
/// may finish while its settlement task is still waiting to be scheduled.
521-
pub(crate) fn request_graph_interrupt(&self) -> Result<GraphStopRequest> {
535+
pub(crate) fn request_graph_interrupt(&self, force: bool) -> Result<GraphStopRequest> {
522536
ensure!(
523537
self.instance.is_none(),
524538
"Only the root can interrupt its graph"
@@ -529,6 +543,9 @@ impl PythonTools {
529543
graph.fenced = true;
530544
for (_, _, receipt) in self.graph_turns(false) {
531545
receipt.graceful_requested.store(true, Ordering::Release);
546+
if force {
547+
receipt.sampling_stop.request();
548+
}
532549
}
533550
}
534551
self.family.graph_changed.send_replace(());
@@ -2209,6 +2226,7 @@ impl TurnLifecycleContributor for PythonContributor {
22092226
state: watch::channel(AdmissionState::Pending).0,
22102227
completed_calls: Mutex::default(),
22112228
graceful_requested: AtomicBool::new(false),
2229+
sampling_stop: input.turn_store.get_or_init(TurnSamplingStop::default),
22122230
graph_fenced: graph.fenced,
22132231
terminal: AtomicBool::new(false),
22142232
local_command_cleanup_failed: AtomicBool::new(false),
@@ -3717,7 +3735,7 @@ pub(crate) mod managed_tests {
37173735
assert_eq!(history.matches("unique steering tail in the native gap").count(), 1);
37183736
if outcome == "stop" {
37193737
let tools = state.thread.thread_extension_data().get::<PythonTools>().unwrap();
3720-
let _stop = tools.request_graph_interrupt().unwrap();
3738+
let _stop = tools.request_graph_interrupt(false).unwrap();
37213739
let rejected = crate::ops::submit_replaceable(&state, continuation).await.unwrap();
37223740
assert_eq!(rejected["status"], "not_submitted");
37233741
assert_eq!(provider.requests.lock().unwrap().len(), 1);

‎src/agents/session.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4839,6 +4839,11 @@ async def _cancel(
48394839
return
48404840
assert state.task is not None
48414841
should_cancel = not state.stopping and not state.failure.done()
4842+
if should_cancel and state.handle is not None:
4843+
# Callback cleanup can finish before Core handles the interrupt.
4844+
# The public root may now own a later native continuation.
4845+
assert state.handle.turn_id is not None
4846+
self._native._request_sampling_stop(state.handle.turn_id)
48424847
# A final manual reply can clear restored_pending while native recovery
48434848
# is in flight. Keep ownership of the terminal retirement already chosen.
48444849
state.retire_manual = state.retire_manual or state.restored_pending

0 commit comments

Comments
 (0)