agentcyberrange 0.1.0__tar.gz
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.
Potentially problematic release.
This version of agentcyberrange might be problematic. Click here for more details.
- agentcyberrange-0.1.0/.gitignore +13 -0
- agentcyberrange-0.1.0/LICENSE +21 -0
- agentcyberrange-0.1.0/PKG-INFO +275 -0
- agentcyberrange-0.1.0/README.md +245 -0
- agentcyberrange-0.1.0/examples/codex_cli.py +81 -0
- agentcyberrange-0.1.0/examples/manual_client.py +26 -0
- agentcyberrange-0.1.0/examples/quickstart.py +43 -0
- agentcyberrange-0.1.0/pyproject.toml +79 -0
- agentcyberrange-0.1.0/src/agentcyberrange/__init__.py +79 -0
- agentcyberrange-0.1.0/src/agentcyberrange/_version.py +1 -0
- agentcyberrange-0.1.0/src/agentcyberrange/client.py +509 -0
- agentcyberrange-0.1.0/src/agentcyberrange/errors.py +144 -0
- agentcyberrange-0.1.0/src/agentcyberrange/handles.py +106 -0
- agentcyberrange-0.1.0/src/agentcyberrange/models.py +249 -0
- agentcyberrange-0.1.0/src/agentcyberrange/py.typed +0 -0
- agentcyberrange-0.1.0/src/agentcyberrange/runner.py +108 -0
- agentcyberrange-0.1.0/src/agentcyberrange/scheduler.py +784 -0
- agentcyberrange-0.1.0/src/agentcyberrange/state.py +108 -0
- agentcyberrange-0.1.0/src/agentcyberrange/submission.py +29 -0
- agentcyberrange-0.1.0/uv.lock +573 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Agent Cyber Range contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: agentcyberrange
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Async Python SDK for Agent Cyber Range
|
|
5
|
+
Project-URL: Homepage, https://github.com/cynicalight/py-agentcyberrange
|
|
6
|
+
Project-URL: Repository, https://github.com/cynicalight/py-agentcyberrange
|
|
7
|
+
Project-URL: Issues, https://github.com/cynicalight/py-agentcyberrange/issues
|
|
8
|
+
Author: Agent Cyber Range contributors
|
|
9
|
+
License-Expression: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: asyncio,cyber-range,evaluation,sdk
|
|
12
|
+
Classifier: Development Status :: 3 - Alpha
|
|
13
|
+
Classifier: Framework :: AsyncIO
|
|
14
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
19
|
+
Classifier: Typing :: Typed
|
|
20
|
+
Requires-Python: >=3.11
|
|
21
|
+
Requires-Dist: httpx[socks]>=0.27
|
|
22
|
+
Requires-Dist: pydantic>=2.7
|
|
23
|
+
Provides-Extra: dev
|
|
24
|
+
Requires-Dist: build>=1.2; extra == 'dev'
|
|
25
|
+
Requires-Dist: mypy>=1.10; extra == 'dev'
|
|
26
|
+
Requires-Dist: pytest-asyncio>=0.23; extra == 'dev'
|
|
27
|
+
Requires-Dist: pytest>=8.2; extra == 'dev'
|
|
28
|
+
Requires-Dist: ruff>=0.5; extra == 'dev'
|
|
29
|
+
Description-Content-Type: text/markdown
|
|
30
|
+
|
|
31
|
+
# Agent Cyber Range SDK
|
|
32
|
+
|
|
33
|
+
`agentcyberrange` is the Python SDK for running autonomous Agents against the
|
|
34
|
+
Agent Cyber Range evaluation platform. It discovers the available Challenges,
|
|
35
|
+
keeps a bounded number of Arenas running, waits for capacity, and submits each
|
|
36
|
+
Arena when the Agent finishes or reaches its deadline.
|
|
37
|
+
|
|
38
|
+
The evaluation system is partially open source. You can inspect and test the SDK
|
|
39
|
+
locally from this [GitHub repository](https://github.com/cynicalight/py-agentcyberrange)
|
|
40
|
+
before requesting an official evaluation.
|
|
41
|
+
|
|
42
|
+
Python 3.11 or newer is required.
|
|
43
|
+
|
|
44
|
+
## Install
|
|
45
|
+
|
|
46
|
+
Install the latest release from PyPI:
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
python -m pip install agentcyberrange
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
For local development from a repository checkout:
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
uv sync --extra dev
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## Quick start
|
|
59
|
+
|
|
60
|
+
Set the Task Token issued by Agent Cyber Range:
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
export AGENTCYBERRANGE_TASK_TOKEN='replace-with-secret'
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Then provide one asynchronous Agent function:
|
|
67
|
+
|
|
68
|
+
```python
|
|
69
|
+
import asyncio
|
|
70
|
+
import os
|
|
71
|
+
|
|
72
|
+
from agentcyberrange import (
|
|
73
|
+
AgentCyberRangeClient,
|
|
74
|
+
AgentOutputPath,
|
|
75
|
+
ArenaHandle,
|
|
76
|
+
run_agent,
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
async def run_your_agent(arena: ArenaHandle) -> AgentOutputPath | None:
|
|
81
|
+
# Replace this body with your Agent implementation.
|
|
82
|
+
print(arena.task_prompt)
|
|
83
|
+
print(arena.entry_urls)
|
|
84
|
+
agent_output: AgentOutputPath | None = None
|
|
85
|
+
return agent_output
|
|
86
|
+
|
|
87
|
+
async def main() -> None:
|
|
88
|
+
async with AgentCyberRangeClient(
|
|
89
|
+
base_url=os.environ.get(
|
|
90
|
+
"AGENTCYBERRANGE_BASE_URL", "https://eval.agentcyberrange.io/"
|
|
91
|
+
),
|
|
92
|
+
task_token=os.environ["AGENTCYBERRANGE_TASK_TOKEN"],
|
|
93
|
+
) as client:
|
|
94
|
+
challenges = await client.list_challenges()
|
|
95
|
+
# Replace these with one or more specific Challenge IDs if needed.
|
|
96
|
+
challenge_ids = [challenge.challenge_id for challenge in challenges[:5]]
|
|
97
|
+
result = await run_agent(
|
|
98
|
+
run_your_agent,
|
|
99
|
+
max_concurrency=2,
|
|
100
|
+
challenge_ids=challenge_ids,
|
|
101
|
+
client=client,
|
|
102
|
+
)
|
|
103
|
+
print(result.model_dump_json(indent=2))
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
asyncio.run(main())
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
The Quick Start keeps the main customization points visible: the Agent callback, the
|
|
110
|
+
server-provided Prompt, and two concurrent Agents. The SDK returns the available
|
|
111
|
+
Challenge list, and the example selects the first five for this run. It leaves
|
|
112
|
+
`agent_timeout` unset, so Web Challenges use the 30-minute default and post Challenges use
|
|
113
|
+
the 2-hour default. Pass `agent_timeout` directly to apply one shorter limit across the
|
|
114
|
+
selected Challenges.
|
|
115
|
+
|
|
116
|
+
Environment variables are reserved for connection configuration
|
|
117
|
+
(`AGENTCYBERRANGE_TASK_TOKEN` and, optionally, `AGENTCYBERRANGE_BASE_URL`). Per-run
|
|
118
|
+
policy such as concurrency, Agent timeout, model, and permission mode stays explicit in
|
|
119
|
+
Python arguments or CLI flags instead of hidden process-global configuration.
|
|
120
|
+
|
|
121
|
+
Fleet and the SDK both limit a Web (non-post) Arena to 30 minutes and a post Arena to 2
|
|
122
|
+
hours. These values are the SDK's defaults and hard maximums. When `agent_timeout` is
|
|
123
|
+
omitted, the SDK selects the limit from the Challenge type.
|
|
124
|
+
|
|
125
|
+
`agent_timeout` is an optional local limit in seconds. `run_agent()` applies the same
|
|
126
|
+
requested value to every selected Challenge, then caps it at the type-specific maximum.
|
|
127
|
+
For example, one hour becomes 30 minutes for Web and remains one hour for post; three
|
|
128
|
+
hours becomes 30 minutes for Web and 2 hours for post. The effective deadline is the
|
|
129
|
+
earlier of this local limit and the individual Arena's authoritative server deadline.
|
|
130
|
+
When it is reached, the SDK signals `arena.cancel_event` and cancels the Agent callback. A
|
|
131
|
+
post Challenge is submitted with an empty body. A non-post callback may finish its
|
|
132
|
+
cancellation cleanup by returning an output archive, which the SDK submits immediately;
|
|
133
|
+
if cancellation produces no archive, the SDK submits a minimal empty `final_answer`
|
|
134
|
+
archive instead. A timeout or deadline therefore always triggers a terminal submit rather
|
|
135
|
+
than abandoning the Arena with a close request. The returned task outcome records the
|
|
136
|
+
timeout or deadline in `error_code` and `error_message`.
|
|
137
|
+
|
|
138
|
+
`on_event` receives lifecycle and retry events. Capacity waits include the HTTP status,
|
|
139
|
+
error code, request ID, `Retry-After`, and attempt number in `SchedulerEvent`, so callers
|
|
140
|
+
can report an automatic wait without disabling it. Pass a `RetryPolicy` to `run_agent()`
|
|
141
|
+
when a finite `capacity_wait_timeout` is required.
|
|
142
|
+
|
|
143
|
+
`arena_ready_delay` adds a cancellable settling period after Fleet reports an Arena as
|
|
144
|
+
running and before the Agent callback starts. This is useful when an exposed service needs
|
|
145
|
+
a few extra seconds to begin accepting connections.
|
|
146
|
+
|
|
147
|
+
The SDK automatically:
|
|
148
|
+
|
|
149
|
+
- discovers all currently available Challenges;
|
|
150
|
+
- keeps at most `max_concurrency` Work Slots active;
|
|
151
|
+
- waits and retries when the control plane has no free capacity;
|
|
152
|
+
- refills a Work Slot as soon as an Arena is submitted;
|
|
153
|
+
- submits a post Challenge when the Agent returns, raises, times out, or reaches the
|
|
154
|
+
server deadline;
|
|
155
|
+
- submits a non-post Challenge with the returned output archive, or with a minimal empty
|
|
156
|
+
`final_answer` archive when the Agent returns no path or raises.
|
|
157
|
+
|
|
158
|
+
## Run Codex
|
|
159
|
+
|
|
160
|
+
Install and authenticate the Codex CLI, set the Agent Cyber Range Task Token, then run:
|
|
161
|
+
|
|
162
|
+
```bash
|
|
163
|
+
uv run python examples/codex_cli.py
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
The example uses the default SDK settings and Codex model. It starts a non-interactive,
|
|
167
|
+
ephemeral `codex exec` session in a temporary workspace and enables network access inside
|
|
168
|
+
the `workspace-write` sandbox. See the
|
|
169
|
+
[Codex non-interactive mode documentation](https://learn.chatgpt.com/docs/non-interactive-mode).
|
|
170
|
+
|
|
171
|
+
The Codex example passes the server-provided `arena.task_prompt` to Codex unchanged. It
|
|
172
|
+
removes `AGENTCYBERRANGE_TASK_TOKEN` and `AGENTCYBERRANGE_BASE_URL` from the child
|
|
173
|
+
environment, so Codex cannot directly operate the control plane. If Codex creates a
|
|
174
|
+
`final_answer/` directory for a non-post Challenge, the callback archives it under
|
|
175
|
+
`.agentcyberrange/agent-output/` and returns that archive path to the Scheduler. A post
|
|
176
|
+
Challenge returns `None` and is submitted with an empty body.
|
|
177
|
+
|
|
178
|
+
## Low-level client
|
|
179
|
+
|
|
180
|
+
Use `AgentCyberRangeClient` when you need to manage one Arena manually:
|
|
181
|
+
|
|
182
|
+
```python
|
|
183
|
+
import asyncio
|
|
184
|
+
import os
|
|
185
|
+
|
|
186
|
+
from agentcyberrange import AgentCyberRangeClient
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
async def main() -> None:
|
|
190
|
+
async with AgentCyberRangeClient(
|
|
191
|
+
base_url=os.getenv(
|
|
192
|
+
"AGENTCYBERRANGE_BASE_URL",
|
|
193
|
+
"https://eval.agentcyberrange.io/",
|
|
194
|
+
),
|
|
195
|
+
task_token=os.environ["AGENTCYBERRANGE_TASK_TOKEN"],
|
|
196
|
+
) as client:
|
|
197
|
+
challenges = await client.list_challenges()
|
|
198
|
+
if not challenges:
|
|
199
|
+
raise RuntimeError("Agent Cyber Range has no available Challenges")
|
|
200
|
+
arena = await client.create_arena(challenges[0].challenge_id)
|
|
201
|
+
print(arena.task_prompt)
|
|
202
|
+
print(arena.entry_urls)
|
|
203
|
+
result = await client.submit(arena.arena_id)
|
|
204
|
+
print(result.verdict)
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
asyncio.run(main())
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
`create_arena()` automatically waits on `capacity_exhausted` and reuses the same
|
|
211
|
+
Idempotency-Key across retries. The client honors standard proxy environment variables;
|
|
212
|
+
set `trust_env=False` when a localhost backend must bypass them.
|
|
213
|
+
|
|
214
|
+
`submit()` also accepts a `.tar.gz` or `.zip` path for a non-post Challenge:
|
|
215
|
+
|
|
216
|
+
```python
|
|
217
|
+
result = await client.submit(
|
|
218
|
+
arena.arena_id,
|
|
219
|
+
agent_output="outputs/agent-output.zip",
|
|
220
|
+
)
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
The SDK resolves a relative output path against the process working directory when
|
|
224
|
+
`submit()` starts, then reads the file once so an ambiguous HTTP result can safely retry
|
|
225
|
+
the same archive. The file must be non-empty and no larger than 10 MiB.
|
|
226
|
+
|
|
227
|
+
With `run_agent()`, return the archive path when a non-post Agent produced one. Returning
|
|
228
|
+
`None` is valid for both Challenge types: the Scheduler uses an empty request body for a
|
|
229
|
+
post Challenge and a minimal empty `final_answer` archive for a non-post Challenge.
|
|
230
|
+
|
|
231
|
+
```python
|
|
232
|
+
from agentcyberrange import AgentOutputPath, ArenaHandle
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
async def run_your_agent(arena: ArenaHandle) -> AgentOutputPath | None:
|
|
236
|
+
agent_output = await your_agent(arena)
|
|
237
|
+
return agent_output
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
## Examples
|
|
241
|
+
|
|
242
|
+
- [`examples/quickstart.py`](examples/quickstart.py): minimal custom Agent callback
|
|
243
|
+
- [`examples/codex_cli.py`](examples/codex_cli.py): Codex CLI integration
|
|
244
|
+
- [`examples/manual_client.py`](examples/manual_client.py): low-level client flow
|
|
245
|
+
|
|
246
|
+
## Development verification
|
|
247
|
+
|
|
248
|
+
```bash
|
|
249
|
+
uv sync --extra dev
|
|
250
|
+
uv run pytest
|
|
251
|
+
uv run ruff check .
|
|
252
|
+
uv run mypy src
|
|
253
|
+
uv build
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
`scripts/live_backend_smoke.py` exercises every low-level client method.
|
|
257
|
+
`scripts/live_scheduler_smoke.py` verifies rolling refill and capacity fallback.
|
|
258
|
+
`scripts/run_agent_test.py` runs mock or external Agent processes against the deployed
|
|
259
|
+
control plane and asserts that no active Arena is left behind.
|
|
260
|
+
|
|
261
|
+
For a repository-only live Agent test:
|
|
262
|
+
|
|
263
|
+
```bash
|
|
264
|
+
uv run python scripts/run_agent_test.py
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
To replace the mock Agent with another process:
|
|
268
|
+
|
|
269
|
+
```bash
|
|
270
|
+
uv run python scripts/run_agent_test.py \
|
|
271
|
+
--agent-command 'codex exec --ephemeral --skip-git-repo-check --sandbox workspace-write -c sandbox_workspace_write.network_access=true -'
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
The default base URL is `https://eval.agentcyberrange.io/`. Override it with
|
|
275
|
+
`AGENTCYBERRANGE_BASE_URL` when testing another deployment.
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
# Agent Cyber Range SDK
|
|
2
|
+
|
|
3
|
+
`agentcyberrange` is the Python SDK for running autonomous Agents against the
|
|
4
|
+
Agent Cyber Range evaluation platform. It discovers the available Challenges,
|
|
5
|
+
keeps a bounded number of Arenas running, waits for capacity, and submits each
|
|
6
|
+
Arena when the Agent finishes or reaches its deadline.
|
|
7
|
+
|
|
8
|
+
The evaluation system is partially open source. You can inspect and test the SDK
|
|
9
|
+
locally from this [GitHub repository](https://github.com/cynicalight/py-agentcyberrange)
|
|
10
|
+
before requesting an official evaluation.
|
|
11
|
+
|
|
12
|
+
Python 3.11 or newer is required.
|
|
13
|
+
|
|
14
|
+
## Install
|
|
15
|
+
|
|
16
|
+
Install the latest release from PyPI:
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
python -m pip install agentcyberrange
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
For local development from a repository checkout:
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
uv sync --extra dev
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Quick start
|
|
29
|
+
|
|
30
|
+
Set the Task Token issued by Agent Cyber Range:
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
export AGENTCYBERRANGE_TASK_TOKEN='replace-with-secret'
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Then provide one asynchronous Agent function:
|
|
37
|
+
|
|
38
|
+
```python
|
|
39
|
+
import asyncio
|
|
40
|
+
import os
|
|
41
|
+
|
|
42
|
+
from agentcyberrange import (
|
|
43
|
+
AgentCyberRangeClient,
|
|
44
|
+
AgentOutputPath,
|
|
45
|
+
ArenaHandle,
|
|
46
|
+
run_agent,
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
async def run_your_agent(arena: ArenaHandle) -> AgentOutputPath | None:
|
|
51
|
+
# Replace this body with your Agent implementation.
|
|
52
|
+
print(arena.task_prompt)
|
|
53
|
+
print(arena.entry_urls)
|
|
54
|
+
agent_output: AgentOutputPath | None = None
|
|
55
|
+
return agent_output
|
|
56
|
+
|
|
57
|
+
async def main() -> None:
|
|
58
|
+
async with AgentCyberRangeClient(
|
|
59
|
+
base_url=os.environ.get(
|
|
60
|
+
"AGENTCYBERRANGE_BASE_URL", "https://eval.agentcyberrange.io/"
|
|
61
|
+
),
|
|
62
|
+
task_token=os.environ["AGENTCYBERRANGE_TASK_TOKEN"],
|
|
63
|
+
) as client:
|
|
64
|
+
challenges = await client.list_challenges()
|
|
65
|
+
# Replace these with one or more specific Challenge IDs if needed.
|
|
66
|
+
challenge_ids = [challenge.challenge_id for challenge in challenges[:5]]
|
|
67
|
+
result = await run_agent(
|
|
68
|
+
run_your_agent,
|
|
69
|
+
max_concurrency=2,
|
|
70
|
+
challenge_ids=challenge_ids,
|
|
71
|
+
client=client,
|
|
72
|
+
)
|
|
73
|
+
print(result.model_dump_json(indent=2))
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
asyncio.run(main())
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
The Quick Start keeps the main customization points visible: the Agent callback, the
|
|
80
|
+
server-provided Prompt, and two concurrent Agents. The SDK returns the available
|
|
81
|
+
Challenge list, and the example selects the first five for this run. It leaves
|
|
82
|
+
`agent_timeout` unset, so Web Challenges use the 30-minute default and post Challenges use
|
|
83
|
+
the 2-hour default. Pass `agent_timeout` directly to apply one shorter limit across the
|
|
84
|
+
selected Challenges.
|
|
85
|
+
|
|
86
|
+
Environment variables are reserved for connection configuration
|
|
87
|
+
(`AGENTCYBERRANGE_TASK_TOKEN` and, optionally, `AGENTCYBERRANGE_BASE_URL`). Per-run
|
|
88
|
+
policy such as concurrency, Agent timeout, model, and permission mode stays explicit in
|
|
89
|
+
Python arguments or CLI flags instead of hidden process-global configuration.
|
|
90
|
+
|
|
91
|
+
Fleet and the SDK both limit a Web (non-post) Arena to 30 minutes and a post Arena to 2
|
|
92
|
+
hours. These values are the SDK's defaults and hard maximums. When `agent_timeout` is
|
|
93
|
+
omitted, the SDK selects the limit from the Challenge type.
|
|
94
|
+
|
|
95
|
+
`agent_timeout` is an optional local limit in seconds. `run_agent()` applies the same
|
|
96
|
+
requested value to every selected Challenge, then caps it at the type-specific maximum.
|
|
97
|
+
For example, one hour becomes 30 minutes for Web and remains one hour for post; three
|
|
98
|
+
hours becomes 30 minutes for Web and 2 hours for post. The effective deadline is the
|
|
99
|
+
earlier of this local limit and the individual Arena's authoritative server deadline.
|
|
100
|
+
When it is reached, the SDK signals `arena.cancel_event` and cancels the Agent callback. A
|
|
101
|
+
post Challenge is submitted with an empty body. A non-post callback may finish its
|
|
102
|
+
cancellation cleanup by returning an output archive, which the SDK submits immediately;
|
|
103
|
+
if cancellation produces no archive, the SDK submits a minimal empty `final_answer`
|
|
104
|
+
archive instead. A timeout or deadline therefore always triggers a terminal submit rather
|
|
105
|
+
than abandoning the Arena with a close request. The returned task outcome records the
|
|
106
|
+
timeout or deadline in `error_code` and `error_message`.
|
|
107
|
+
|
|
108
|
+
`on_event` receives lifecycle and retry events. Capacity waits include the HTTP status,
|
|
109
|
+
error code, request ID, `Retry-After`, and attempt number in `SchedulerEvent`, so callers
|
|
110
|
+
can report an automatic wait without disabling it. Pass a `RetryPolicy` to `run_agent()`
|
|
111
|
+
when a finite `capacity_wait_timeout` is required.
|
|
112
|
+
|
|
113
|
+
`arena_ready_delay` adds a cancellable settling period after Fleet reports an Arena as
|
|
114
|
+
running and before the Agent callback starts. This is useful when an exposed service needs
|
|
115
|
+
a few extra seconds to begin accepting connections.
|
|
116
|
+
|
|
117
|
+
The SDK automatically:
|
|
118
|
+
|
|
119
|
+
- discovers all currently available Challenges;
|
|
120
|
+
- keeps at most `max_concurrency` Work Slots active;
|
|
121
|
+
- waits and retries when the control plane has no free capacity;
|
|
122
|
+
- refills a Work Slot as soon as an Arena is submitted;
|
|
123
|
+
- submits a post Challenge when the Agent returns, raises, times out, or reaches the
|
|
124
|
+
server deadline;
|
|
125
|
+
- submits a non-post Challenge with the returned output archive, or with a minimal empty
|
|
126
|
+
`final_answer` archive when the Agent returns no path or raises.
|
|
127
|
+
|
|
128
|
+
## Run Codex
|
|
129
|
+
|
|
130
|
+
Install and authenticate the Codex CLI, set the Agent Cyber Range Task Token, then run:
|
|
131
|
+
|
|
132
|
+
```bash
|
|
133
|
+
uv run python examples/codex_cli.py
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
The example uses the default SDK settings and Codex model. It starts a non-interactive,
|
|
137
|
+
ephemeral `codex exec` session in a temporary workspace and enables network access inside
|
|
138
|
+
the `workspace-write` sandbox. See the
|
|
139
|
+
[Codex non-interactive mode documentation](https://learn.chatgpt.com/docs/non-interactive-mode).
|
|
140
|
+
|
|
141
|
+
The Codex example passes the server-provided `arena.task_prompt` to Codex unchanged. It
|
|
142
|
+
removes `AGENTCYBERRANGE_TASK_TOKEN` and `AGENTCYBERRANGE_BASE_URL` from the child
|
|
143
|
+
environment, so Codex cannot directly operate the control plane. If Codex creates a
|
|
144
|
+
`final_answer/` directory for a non-post Challenge, the callback archives it under
|
|
145
|
+
`.agentcyberrange/agent-output/` and returns that archive path to the Scheduler. A post
|
|
146
|
+
Challenge returns `None` and is submitted with an empty body.
|
|
147
|
+
|
|
148
|
+
## Low-level client
|
|
149
|
+
|
|
150
|
+
Use `AgentCyberRangeClient` when you need to manage one Arena manually:
|
|
151
|
+
|
|
152
|
+
```python
|
|
153
|
+
import asyncio
|
|
154
|
+
import os
|
|
155
|
+
|
|
156
|
+
from agentcyberrange import AgentCyberRangeClient
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
async def main() -> None:
|
|
160
|
+
async with AgentCyberRangeClient(
|
|
161
|
+
base_url=os.getenv(
|
|
162
|
+
"AGENTCYBERRANGE_BASE_URL",
|
|
163
|
+
"https://eval.agentcyberrange.io/",
|
|
164
|
+
),
|
|
165
|
+
task_token=os.environ["AGENTCYBERRANGE_TASK_TOKEN"],
|
|
166
|
+
) as client:
|
|
167
|
+
challenges = await client.list_challenges()
|
|
168
|
+
if not challenges:
|
|
169
|
+
raise RuntimeError("Agent Cyber Range has no available Challenges")
|
|
170
|
+
arena = await client.create_arena(challenges[0].challenge_id)
|
|
171
|
+
print(arena.task_prompt)
|
|
172
|
+
print(arena.entry_urls)
|
|
173
|
+
result = await client.submit(arena.arena_id)
|
|
174
|
+
print(result.verdict)
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
asyncio.run(main())
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
`create_arena()` automatically waits on `capacity_exhausted` and reuses the same
|
|
181
|
+
Idempotency-Key across retries. The client honors standard proxy environment variables;
|
|
182
|
+
set `trust_env=False` when a localhost backend must bypass them.
|
|
183
|
+
|
|
184
|
+
`submit()` also accepts a `.tar.gz` or `.zip` path for a non-post Challenge:
|
|
185
|
+
|
|
186
|
+
```python
|
|
187
|
+
result = await client.submit(
|
|
188
|
+
arena.arena_id,
|
|
189
|
+
agent_output="outputs/agent-output.zip",
|
|
190
|
+
)
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
The SDK resolves a relative output path against the process working directory when
|
|
194
|
+
`submit()` starts, then reads the file once so an ambiguous HTTP result can safely retry
|
|
195
|
+
the same archive. The file must be non-empty and no larger than 10 MiB.
|
|
196
|
+
|
|
197
|
+
With `run_agent()`, return the archive path when a non-post Agent produced one. Returning
|
|
198
|
+
`None` is valid for both Challenge types: the Scheduler uses an empty request body for a
|
|
199
|
+
post Challenge and a minimal empty `final_answer` archive for a non-post Challenge.
|
|
200
|
+
|
|
201
|
+
```python
|
|
202
|
+
from agentcyberrange import AgentOutputPath, ArenaHandle
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
async def run_your_agent(arena: ArenaHandle) -> AgentOutputPath | None:
|
|
206
|
+
agent_output = await your_agent(arena)
|
|
207
|
+
return agent_output
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
## Examples
|
|
211
|
+
|
|
212
|
+
- [`examples/quickstart.py`](examples/quickstart.py): minimal custom Agent callback
|
|
213
|
+
- [`examples/codex_cli.py`](examples/codex_cli.py): Codex CLI integration
|
|
214
|
+
- [`examples/manual_client.py`](examples/manual_client.py): low-level client flow
|
|
215
|
+
|
|
216
|
+
## Development verification
|
|
217
|
+
|
|
218
|
+
```bash
|
|
219
|
+
uv sync --extra dev
|
|
220
|
+
uv run pytest
|
|
221
|
+
uv run ruff check .
|
|
222
|
+
uv run mypy src
|
|
223
|
+
uv build
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
`scripts/live_backend_smoke.py` exercises every low-level client method.
|
|
227
|
+
`scripts/live_scheduler_smoke.py` verifies rolling refill and capacity fallback.
|
|
228
|
+
`scripts/run_agent_test.py` runs mock or external Agent processes against the deployed
|
|
229
|
+
control plane and asserts that no active Arena is left behind.
|
|
230
|
+
|
|
231
|
+
For a repository-only live Agent test:
|
|
232
|
+
|
|
233
|
+
```bash
|
|
234
|
+
uv run python scripts/run_agent_test.py
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
To replace the mock Agent with another process:
|
|
238
|
+
|
|
239
|
+
```bash
|
|
240
|
+
uv run python scripts/run_agent_test.py \
|
|
241
|
+
--agent-command 'codex exec --ephemeral --skip-git-repo-check --sandbox workspace-write -c sandbox_workspace_write.network_access=true -'
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
The default base URL is `https://eval.agentcyberrange.io/`. Override it with
|
|
245
|
+
`AGENTCYBERRANGE_BASE_URL` when testing another deployment.
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import asyncio
|
|
4
|
+
import os
|
|
5
|
+
import tarfile
|
|
6
|
+
import tempfile
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
|
|
9
|
+
from agentcyberrange import AgentOutputPath, ArenaHandle, is_post_challenge, run_agent
|
|
10
|
+
|
|
11
|
+
CODEX_COMMAND = (
|
|
12
|
+
"codex",
|
|
13
|
+
"exec",
|
|
14
|
+
"--ephemeral",
|
|
15
|
+
"--skip-git-repo-check",
|
|
16
|
+
"--sandbox",
|
|
17
|
+
"workspace-write",
|
|
18
|
+
"--config",
|
|
19
|
+
"sandbox_workspace_write.network_access=true",
|
|
20
|
+
"-",
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def archive_agent_output(workspace: str, arena_id: str) -> AgentOutputPath | None:
|
|
25
|
+
final_answer = Path(workspace) / "final_answer"
|
|
26
|
+
if not final_answer.is_dir():
|
|
27
|
+
return None
|
|
28
|
+
|
|
29
|
+
output_dir = Path.cwd() / ".agentcyberrange" / "agent-output"
|
|
30
|
+
output_dir.mkdir(parents=True, exist_ok=True)
|
|
31
|
+
output_path = output_dir / f"{arena_id}.tar.gz"
|
|
32
|
+
with tarfile.open(output_path, "w:gz") as archive:
|
|
33
|
+
archive.add(final_answer, arcname="final_answer")
|
|
34
|
+
return output_path
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
async def stop_codex(process: asyncio.subprocess.Process) -> None:
|
|
38
|
+
"""Terminate Codex, escalating to kill after five seconds."""
|
|
39
|
+
if process.returncode is not None:
|
|
40
|
+
return
|
|
41
|
+
process.terminate()
|
|
42
|
+
try:
|
|
43
|
+
await asyncio.wait_for(process.wait(), timeout=5)
|
|
44
|
+
except TimeoutError:
|
|
45
|
+
process.kill()
|
|
46
|
+
await process.wait()
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
async def call_codex(arena: ArenaHandle) -> AgentOutputPath | None:
|
|
50
|
+
"""Run Codex non-interactively against one authorized Arena."""
|
|
51
|
+
codex_environment = os.environ.copy()
|
|
52
|
+
codex_environment.pop("AGENTCYBERRANGE_TASK_TOKEN", None)
|
|
53
|
+
codex_environment.pop("AGENTCYBERRANGE_BASE_URL", None)
|
|
54
|
+
|
|
55
|
+
with tempfile.TemporaryDirectory(prefix="agentcyberrange-") as workspace:
|
|
56
|
+
process = await asyncio.create_subprocess_exec(
|
|
57
|
+
*CODEX_COMMAND,
|
|
58
|
+
cwd=workspace,
|
|
59
|
+
env=codex_environment,
|
|
60
|
+
stdin=asyncio.subprocess.PIPE,
|
|
61
|
+
)
|
|
62
|
+
try:
|
|
63
|
+
await process.communicate(arena.task_prompt.encode())
|
|
64
|
+
except asyncio.CancelledError:
|
|
65
|
+
await stop_codex(process)
|
|
66
|
+
raise
|
|
67
|
+
if process.returncode != 0:
|
|
68
|
+
raise RuntimeError(f"Codex exited with status {process.returncode}")
|
|
69
|
+
if is_post_challenge(arena.challenge_id):
|
|
70
|
+
return None
|
|
71
|
+
agent_output = archive_agent_output(workspace, arena.arena_id)
|
|
72
|
+
return agent_output
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
async def main() -> None:
|
|
76
|
+
result = await run_agent(call_codex)
|
|
77
|
+
print(result.model_dump_json(indent=2))
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
if __name__ == "__main__":
|
|
81
|
+
asyncio.run(main())
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import asyncio
|
|
4
|
+
import os
|
|
5
|
+
|
|
6
|
+
from agentcyberrange import AgentCyberRangeClient
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
async def main() -> None:
|
|
10
|
+
async with AgentCyberRangeClient(
|
|
11
|
+
base_url=os.environ.get(
|
|
12
|
+
"AGENTCYBERRANGE_BASE_URL", "https://eval.agentcyberrange.io/"
|
|
13
|
+
),
|
|
14
|
+
task_token=os.environ["AGENTCYBERRANGE_TASK_TOKEN"],
|
|
15
|
+
) as client:
|
|
16
|
+
challenges = await client.list_challenges()
|
|
17
|
+
if not challenges:
|
|
18
|
+
raise RuntimeError("Agent Cyber Range has no available Challenges")
|
|
19
|
+
arena = await client.create_arena(challenges[0].challenge_id)
|
|
20
|
+
print(arena.task_prompt)
|
|
21
|
+
result = await client.submit(arena.arena_id)
|
|
22
|
+
print(result.verdict)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
if __name__ == "__main__":
|
|
26
|
+
asyncio.run(main())
|