Skip to content

Commit 5b50815

Browse files
fix(examples): prevent auto-running the local Temporal shell workflow (#5218)
* fix(examples): prevent auto-running the local Temporal shell workflow * test: restrict Temporal Unix entrypoint checks to supported platforms
1 parent de3b1d7 commit 5b50815

3 files changed

Lines changed: 82 additions & 0 deletions

File tree

‎examples/run_examples.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@
8080
"examples/sandbox/extensions/blaxel_runner.py",
8181
"examples/sandbox/extensions/cloudflare_runner.py",
8282
"examples/sandbox/extensions/daytona/usaspending_text2sql/setup_db.py",
83+
# This local workflow gives model-selected shell commands access to the host.
84+
"examples/sandbox/extensions/temporal/local_hello_workflow.py",
8385
"examples/sandbox/extensions/temporal/temporal_sandbox_agent.py",
8486
# Temporarily disabled due to credential issues.
8587
"examples/sandbox/extensions/vercel_runner.py",

‎examples/sandbox/extensions/temporal/local_hello_workflow.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
credentials. It does require ``OPENAI_API_KEY`` because the model call runs through the Temporal
99
OpenAI Agents plugin as an activity.
1010
11+
The model selects shell commands that run on the host with the process environment.
12+
Use this example only for trusted local execution or within external isolation. It refuses
13+
automated example mode because the local Unix backend does not provide OS confinement on Linux.
14+
1115
Usage:
1216
uv run --extra temporal python -m examples.sandbox.extensions.temporal.local_hello_workflow
1317
"""
@@ -36,6 +40,7 @@
3640
from agents.sandbox.capabilities import Shell
3741
from agents.sandbox.entries import File
3842
from agents.sandbox.sandboxes import UnixLocalSandboxClient, UnixLocalSandboxClientOptions
43+
from examples.auto_mode import is_auto_mode
3944

4045
TASK_QUEUE = "local-temporal-sandbox-agent"
4146
WORKFLOW_ID = "local-temporal-sandbox-agent-workflow"
@@ -113,6 +118,11 @@ def _trace_mode_from_env() -> str:
113118

114119

115120
async def main() -> None:
121+
if is_auto_mode():
122+
raise SystemExit(
123+
"This example cannot run in auto mode: model-selected shell commands run on the host. "
124+
"Run it explicitly only for trusted local execution or within external isolation."
125+
)
116126
_require_env("OPENAI_API_KEY")
117127
model = os.getenv("EXAMPLES_TEMPORAL_MODEL", DEFAULT_MODEL)
118128
trace_mode = _trace_mode_from_env()

‎tests/test_run_examples_script.py‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,82 @@
22

33
import sys
44
from pathlib import Path
5+
from unittest.mock import AsyncMock, Mock
56

67
import pytest
78

89
import examples.run_examples as run_examples
910

1011

12+
@pytest.mark.parametrize("auto_source", ["argument", "environment", "manual"])
13+
def test_local_temporal_runner_selection(
14+
monkeypatch: pytest.MonkeyPatch,
15+
tmp_path: Path,
16+
capsys: pytest.CaptureFixture[str],
17+
auto_source: str,
18+
) -> None:
19+
monkeypatch.delenv("EXAMPLES_AUTO_SKIP", raising=False)
20+
monkeypatch.setenv(
21+
"EXAMPLES_INTERACTIVE_MODE", "auto" if auto_source == "environment" else "manual"
22+
)
23+
monkeypatch.setattr(run_examples, "build_command_path", lambda: "")
24+
spawn = Mock(side_effect=AssertionError("The runner must not start this example"))
25+
monkeypatch.setattr(run_examples.subprocess, "Popen", spawn)
26+
args = [
27+
"run_examples.py",
28+
"--filter",
29+
"local_hello_workflow",
30+
"--logs-dir",
31+
str(tmp_path / "logs"),
32+
"--main-log",
33+
str(tmp_path / "main.log"),
34+
"--artifacts-dir",
35+
str(tmp_path / "artifacts"),
36+
]
37+
if auto_source == "argument":
38+
args.append("--auto-mode")
39+
elif auto_source == "manual":
40+
args.append("--dry-run")
41+
monkeypatch.setattr(sys, "argv", args)
42+
43+
assert run_examples.main() == 0
44+
45+
output = capsys.readouterr().out
46+
relpath = "examples/sandbox/extensions/temporal/local_hello_workflow.py"
47+
assert f"- {'RUN ' if auto_source == 'manual' else 'SKIP'} {relpath}" in output
48+
if auto_source != "manual":
49+
assert "(skipped: auto-skip)" in output
50+
spawn.assert_not_called()
51+
52+
53+
@pytest.mark.parametrize("mode", ["auto", "AUTO", "manual"])
54+
@pytest.mark.skipif(sys.platform == "win32", reason="The example requires the Unix-only backend")
55+
@pytest.mark.asyncio
56+
async def test_local_temporal_entrypoint_refuses_auto_mode(
57+
monkeypatch: pytest.MonkeyPatch, mode: str
58+
) -> None:
59+
pytest.importorskip("temporalio")
60+
from examples.sandbox.extensions.temporal import local_hello_workflow
61+
62+
monkeypatch.setenv("EXAMPLES_INTERACTIVE_MODE", mode)
63+
monkeypatch.setenv("OPENAI_API_KEY", "synthetic-test-key")
64+
# A custom skip list can select the example, but cannot authorize auto-mode execution.
65+
monkeypatch.setenv("EXAMPLES_AUTO_SKIP", "examples/basic/hello_world.py")
66+
start_server = AsyncMock(side_effect=RuntimeError("test server startup boundary"))
67+
monkeypatch.setattr(
68+
local_hello_workflow.WorkflowEnvironment, "start_time_skipping", start_server
69+
)
70+
71+
if mode.lower() == "auto":
72+
with pytest.raises(SystemExit, match="cannot run in auto mode"):
73+
await local_hello_workflow.main()
74+
start_server.assert_not_awaited()
75+
else:
76+
with pytest.raises(RuntimeError, match="test server startup boundary"):
77+
await local_hello_workflow.main()
78+
start_server.assert_awaited_once()
79+
80+
1181
def test_default_auto_skip_excludes_prerequisite_bound_examples() -> None:
1282
expected = {
1383
"examples/sandbox/docker/mounts/azure_mount_read_write.py",

0 commit comments

Comments
 (0)