timeplus-agentguard-codex-plugin 0.1.1__tar.gz → 0.2.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.
- timeplus_agentguard_codex_plugin-0.2.0/PKG-INFO +302 -0
- timeplus_agentguard_codex_plugin-0.2.0/README.md +281 -0
- {timeplus_agentguard_codex_plugin-0.1.1 → timeplus_agentguard_codex_plugin-0.2.0}/agentguard_codex/__init__.py +1 -1
- {timeplus_agentguard_codex_plugin-0.1.1 → timeplus_agentguard_codex_plugin-0.2.0}/agentguard_codex/cli.py +25 -13
- timeplus_agentguard_codex_plugin-0.2.0/agentguard_codex/events.py +387 -0
- timeplus_agentguard_codex_plugin-0.2.0/agentguard_codex/sink.py +108 -0
- timeplus_agentguard_codex_plugin-0.2.0/hooks.json +47 -0
- {timeplus_agentguard_codex_plugin-0.1.1 → timeplus_agentguard_codex_plugin-0.2.0}/pyproject.toml +1 -1
- timeplus_agentguard_codex_plugin-0.2.0/tests/test_cli.py +441 -0
- timeplus_agentguard_codex_plugin-0.2.0/tests/test_events.py +697 -0
- timeplus_agentguard_codex_plugin-0.2.0/tests/test_sink.py +156 -0
- timeplus_agentguard_codex_plugin-0.1.1/PKG-INFO +0 -154
- timeplus_agentguard_codex_plugin-0.1.1/README.md +0 -133
- timeplus_agentguard_codex_plugin-0.1.1/agentguard_codex/events.py +0 -115
- timeplus_agentguard_codex_plugin-0.1.1/hooks.json +0 -20
- timeplus_agentguard_codex_plugin-0.1.1/tests/test_cli.py +0 -119
- timeplus_agentguard_codex_plugin-0.1.1/tests/test_events.py +0 -188
- {timeplus_agentguard_codex_plugin-0.1.1 → timeplus_agentguard_codex_plugin-0.2.0}/.gitignore +0 -0
- {timeplus_agentguard_codex_plugin-0.1.1 → timeplus_agentguard_codex_plugin-0.2.0}/Makefile +0 -0
- {timeplus_agentguard_codex_plugin-0.1.1 → timeplus_agentguard_codex_plugin-0.2.0}/agentguard_codex/__main__.py +0 -0
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: timeplus-agentguard-codex-plugin
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: AgentGuard monitoring plugin for OpenAI Codex
|
|
5
|
+
Project-URL: Homepage, https://github.com/timeplus-io/agentguard
|
|
6
|
+
Project-URL: Repository, https://github.com/timeplus-io/agentguard
|
|
7
|
+
License: Apache-2.0
|
|
8
|
+
Keywords: agentguard,ai-agent,codex,monitoring,observability,security
|
|
9
|
+
Classifier: Development Status :: 3 - Alpha
|
|
10
|
+
Classifier: Intended Audience :: Developers
|
|
11
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
16
|
+
Classifier: Topic :: Security
|
|
17
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
18
|
+
Requires-Python: >=3.10
|
|
19
|
+
Requires-Dist: timeplus-agentguard>=0.3.0
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
|
|
22
|
+
# agentguard-codex
|
|
23
|
+
|
|
24
|
+
AgentGuard monitoring plugin for [OpenAI Codex](https://github.com/openai/codex).
|
|
25
|
+
|
|
26
|
+
Codex runs a command hook for each lifecycle event and writes one JSON object
|
|
27
|
+
to stdin. This plugin reads that event, turns it into an OpenTelemetry span
|
|
28
|
+
with the [AgentGuard Python SDK](../../../sdk/python), and exports it to
|
|
29
|
+
the [AgentGuard Timeplus app](https://github.com/timeplus-io/apps/tree/main/apps/agentguard)'s
|
|
30
|
+
OTLP/HTTP input (default `http://localhost:4318`).
|
|
31
|
+
|
|
32
|
+
## Requirements
|
|
33
|
+
|
|
34
|
+
- Python 3.10+
|
|
35
|
+
- A Codex build with hooks enabled
|
|
36
|
+
- The AgentGuard Python SDK ([`timeplus-agentguard`](https://pypi.org/project/timeplus-agentguard/) on PyPI, installed automatically as a dependency)
|
|
37
|
+
|
|
38
|
+
## Installation
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
pip install timeplus-agentguard-codex-plugin
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
To work on the plugin itself, install it from a checkout instead:
|
|
45
|
+
`pip install -e agents/codex/agentguard-plugin`.
|
|
46
|
+
|
|
47
|
+
## Configure Codex hooks
|
|
48
|
+
|
|
49
|
+
Merge [`hooks.json`](hooks.json) into your Codex hook configuration —
|
|
50
|
+
`~/.codex/hooks.json`, or the equivalent `[hooks]` table in
|
|
51
|
+
`~/.codex/config.toml`:
|
|
52
|
+
|
|
53
|
+
```json
|
|
54
|
+
{
|
|
55
|
+
"hooks": {
|
|
56
|
+
"SessionStart": [
|
|
57
|
+
{ "hooks": [ { "type": "command", "command": "agentguard-codex", "timeout": 10 } ] }
|
|
58
|
+
],
|
|
59
|
+
"Stop": [
|
|
60
|
+
{ "hooks": [ { "type": "command", "command": "agentguard-codex", "timeout": 10 } ] }
|
|
61
|
+
]
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
The sample registers all 14 Codex lifecycle events with the same command.
|
|
67
|
+
Codex limits `SessionEnd` and `Interrupt` hooks to 3 seconds, so the sample
|
|
68
|
+
gives those two a `timeout` of 3 and the rest 10.
|
|
69
|
+
|
|
70
|
+
Codex skips a new or changed user-level hook until you review it. Run `/hooks`
|
|
71
|
+
in Codex once and approve the AgentGuard entries. Managed (system/MDM) hooks are
|
|
72
|
+
trusted by policy and skip this step.
|
|
73
|
+
|
|
74
|
+
If the plugin is not installed as a console script, use
|
|
75
|
+
`python -m agentguard_codex` as the command instead.
|
|
76
|
+
|
|
77
|
+
### Upgrading from the five-hook configuration
|
|
78
|
+
|
|
79
|
+
Versions before 0.2.0 handled five events: `SessionStart`, `SessionEnd`,
|
|
80
|
+
`UserPromptSubmit`, `PreToolUse` and `PostToolUse`. To capture the other nine:
|
|
81
|
+
|
|
82
|
+
1. Upgrade the package: `pip install -U timeplus-agentguard-codex-plugin`
|
|
83
|
+
(or `pipx upgrade timeplus-agentguard-codex-plugin`). Check with
|
|
84
|
+
`pip show timeplus-agentguard-codex-plugin` that the version is 0.2.0 or later.
|
|
85
|
+
Registering the new events against an older version does nothing: it
|
|
86
|
+
ignores events it does not know.
|
|
87
|
+
2. Add the nine new events to your hook configuration — `PreModelRequest`,
|
|
88
|
+
`PostModelRequest`, `Stop`, `Interrupt`, `SubagentStart`, `SubagentStop`,
|
|
89
|
+
`PermissionRequest`, `PreCompact`, `PostCompact` — copying the entries from
|
|
90
|
+
[`hooks.json`](hooks.json). Use the same `command` as your existing five
|
|
91
|
+
entries (including any absolute path, flags or `env` prefix you added), and
|
|
92
|
+
leave the existing entries in place.
|
|
93
|
+
3. Set `"timeout": 3` on `Interrupt`, and lower `SessionEnd` to 3 if yours
|
|
94
|
+
still says 10.
|
|
95
|
+
4. Start Codex, run `/hooks`, and approve the new and changed entries. Codex
|
|
96
|
+
skips them until you do; the five existing hooks keep running meanwhile.
|
|
97
|
+
|
|
98
|
+
The spans of the five existing events change in 0.2.0 too (see
|
|
99
|
+
[What gets captured](#what-gets-captured)): the agent type is now `codex`
|
|
100
|
+
instead of `agentguard-sdk`, the prompt is a `user_prompt_submit` span instead
|
|
101
|
+
of an `llm` span, and a `session_start` span is sent when the session starts.
|
|
102
|
+
Queries that filter on `service_name = 'agentguard-sdk'` or `span_name = 'llm'`
|
|
103
|
+
need updating.
|
|
104
|
+
|
|
105
|
+
## Configuration
|
|
106
|
+
|
|
107
|
+
Hooks run in a fresh process, so environment variables are the usual way to
|
|
108
|
+
configure the plugin.
|
|
109
|
+
|
|
110
|
+
| Flag | Environment variable | Default |
|
|
111
|
+
|---|---|---|
|
|
112
|
+
| `--agent-id` | `AGENTGUARD_AGENT_ID` | hostname |
|
|
113
|
+
| `--deployment-id` | `AGENTGUARD_DEPLOYMENT_ID` | `local` |
|
|
114
|
+
| `--deployment-name` | `AGENTGUARD_DEPLOYMENT_NAME` | `Local Dev` |
|
|
115
|
+
| `--endpoint` | `OTEL_EXPORTER_OTLP_ENDPOINT` | `http://localhost:4318` |
|
|
116
|
+
|
|
117
|
+
Flags take precedence over environment variables. The plugin resolves every
|
|
118
|
+
setting (flag, then environment variable, then the default above) before
|
|
119
|
+
constructing the SDK hooks, so each short-lived hook process sees the same
|
|
120
|
+
values — including the agent id, which keys the on-disk Pre/Post-tool timing
|
|
121
|
+
store to match the spans. For example:
|
|
122
|
+
|
|
123
|
+
```bash
|
|
124
|
+
export AGENTGUARD_DEPLOYMENT_ID=production
|
|
125
|
+
export AGENTGUARD_DEPLOYMENT_NAME="Production Codex"
|
|
126
|
+
export OTEL_EXPORTER_OTLP_ENDPOINT=http://timeplus.example.com:4318
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
## What gets captured
|
|
130
|
+
|
|
131
|
+
The plugin handles all 14 events in the
|
|
132
|
+
[Codex hooks reference](https://developers.openai.com/codex/hooks). Each event
|
|
133
|
+
becomes one span, exported with the OTel service name `codex`, which the
|
|
134
|
+
AgentGuard app reports as the agent type.
|
|
135
|
+
|
|
136
|
+
| Codex hook | Emitted span | What it carries |
|
|
137
|
+
|---|---|---|
|
|
138
|
+
| `SessionStart` | `session_start` | Start source (`startup`, `resume`, ...); sent immediately, so a running session is visible |
|
|
139
|
+
| `SessionEnd` | `session` | The whole session, from `SessionStart` to `SessionEnd` |
|
|
140
|
+
| `UserPromptSubmit` | `user_prompt_submit` | The prompt (`input.value`) |
|
|
141
|
+
| `PreToolUse` | none (start time recorded) | |
|
|
142
|
+
| `PostToolUse` | `tool` | `tool.name`, `tool.parameters`, `output.value`, tool duration |
|
|
143
|
+
| `PreModelRequest` | `pre_model_request` | Request id, attempt number, kind |
|
|
144
|
+
| `PostModelRequest` | `post_model_request` | The LLM call: status, error, token usage, attempt duration |
|
|
145
|
+
| `Stop` | `stop` | The final assistant message (`output.value`) |
|
|
146
|
+
| `Interrupt` | `interrupt` | The interrupted turn |
|
|
147
|
+
| `SubagentStart` | `subagent_start` | Subagent id and type |
|
|
148
|
+
| `SubagentStop` | `subagent_stop` | Subagent id and type, its last message, subagent duration |
|
|
149
|
+
| `PermissionRequest` | `permission_request` | `tool.name`, `tool.parameters`, the request description |
|
|
150
|
+
| `PreCompact` | `pre_compact` | Trigger (`manual` or `auto`) |
|
|
151
|
+
| `PostCompact` | `post_compact` | Trigger, compaction duration |
|
|
152
|
+
|
|
153
|
+
### Span attributes
|
|
154
|
+
|
|
155
|
+
On every span, when Codex sends the field:
|
|
156
|
+
|
|
157
|
+
| Attribute | Source |
|
|
158
|
+
|---|---|
|
|
159
|
+
| `session.id` | `session_id` |
|
|
160
|
+
| `run.id` | `turn_id` (turn-scoped hooks) |
|
|
161
|
+
| `agentguard.hook_name` | the hook, in snake case (`session_start`, `post_tool_use`, ...) |
|
|
162
|
+
| `llm.model_name` | `model` |
|
|
163
|
+
| `llm.provider` | `model_provider` from the first record of the session transcript; `openai` when the transcript cannot be read |
|
|
164
|
+
| `cwd` | `cwd`, the session's working directory |
|
|
165
|
+
| `codex.permission_mode` | `permission_mode` |
|
|
166
|
+
| `agentguard.subagent.id`, `agentguard.subagent.type` | `agent_id`, `agent_type` (subagent spans, and any other hook fired inside a subagent) |
|
|
167
|
+
|
|
168
|
+
Three spans carry the kind attribute the app's OpenInference views key on:
|
|
169
|
+
|
|
170
|
+
| Span | Kind attribute | The app derives |
|
|
171
|
+
|---|---|---|
|
|
172
|
+
| `post_model_request` | `openinference.span.kind=LLM` | `llm_request` / `llm_response` events, and the `llm.*` metrics |
|
|
173
|
+
| `tool` | `openinference.span.kind=TOOL` | `tool_invoke` / `tool_complete` events |
|
|
174
|
+
| `session` | `agentguard.span.kind=SESSION` | `session_start` / `session_end` events |
|
|
175
|
+
|
|
176
|
+
Per span:
|
|
177
|
+
|
|
178
|
+
| Attribute | Set on | Source |
|
|
179
|
+
|---|---|---|
|
|
180
|
+
| `source` | `session_start` | `source` |
|
|
181
|
+
| `codex.session.end_reason` | `session` | `reason` |
|
|
182
|
+
| `input.value` | `user_prompt_submit` | `prompt`, as JSON: `{"messages": [{"role": "user", "content": "..."}]}` |
|
|
183
|
+
| `tool.name`, `tool.parameters`, `output.value`, `tool_use_id` | `tool` | `tool_name`, `tool_input`, `tool_response`, `tool_use_id` |
|
|
184
|
+
| `task.id` | `tool`: the tool call id; model request spans: the request id; subagent spans: the subagent id | `tool_use_id`, `request_id`, `agent_id` |
|
|
185
|
+
| `codex.request.id`, `codex.request.attempt`, `codex.request.kind`, `codex.thread_id` | model request spans | `request_id`, `attempt_number`, `kind`, `thread_id` |
|
|
186
|
+
| `codex.request.status`, `codex.request.error` | `post_model_request` | `status` (`completed`, `failed`, `cancelled`), `error` |
|
|
187
|
+
| `llm.token_count.prompt`, `.completion`, `.total`, `.prompt_details.cache_read`, `.prompt_details.cache_write`, `.completion_details.reasoning` | `post_model_request`, when Codex reports usage | `token_usage` |
|
|
188
|
+
| `output.value` | `stop`, `subagent_stop` | `last_assistant_message` |
|
|
189
|
+
| `codex.stop_hook_active` | `stop`, `subagent_stop` | `stop_hook_active` |
|
|
190
|
+
| `codex.agent_transcript_path` | `subagent_stop` | `agent_transcript_path` |
|
|
191
|
+
| `tool.name`, `tool.parameters`, `codex.permission.description` | `permission_request` | `tool_name`, `tool_input`, `tool_input.description` |
|
|
192
|
+
| `codex.compact.trigger` | `pre_compact`, `post_compact` | `trigger` |
|
|
193
|
+
|
|
194
|
+
An attribute is left off the span when Codex does not send the field. A
|
|
195
|
+
`post_model_request` span with status `failed` also has the OTel span status
|
|
196
|
+
`ERROR`. Token counts are passed through as Codex reports them; whether
|
|
197
|
+
`input_tokens` already includes the cached tokens is not yet confirmed (see
|
|
198
|
+
[`docs/todo.md`](../docs/todo.md), item 0.4).
|
|
199
|
+
|
|
200
|
+
### Prompts, model requests and final responses
|
|
201
|
+
|
|
202
|
+
These are three separate records, so counting one does not count another:
|
|
203
|
+
|
|
204
|
+
- **Prompt** — one `user_prompt_submit` span per prompt. It is not an LLM
|
|
205
|
+
call: it has no duration and no token usage.
|
|
206
|
+
- **Model request** — one `pre_model_request` / `post_model_request` pair per
|
|
207
|
+
attempt. Only `post_model_request` is an LLM-kind span, so there is one LLM
|
|
208
|
+
call per attempt, with its real latency and token usage. A retry is a new
|
|
209
|
+
pair with the same `codex.request.id` and the next `codex.request.attempt`.
|
|
210
|
+
- **Final response** — one `stop` span per completed turn, with the assistant's
|
|
211
|
+
last message and no usage. `Stop` ends a turn, not the session. An
|
|
212
|
+
interrupted turn produces an `interrupt` span instead.
|
|
213
|
+
|
|
214
|
+
Subagent hooks carry the parent's `session.id`; `agentguard.subagent.id`
|
|
215
|
+
identifies the child.
|
|
216
|
+
|
|
217
|
+
### What the AgentGuard app shows
|
|
218
|
+
|
|
219
|
+
With the app's current OpenInference views (app 0.4.0), a Codex session gives:
|
|
220
|
+
|
|
221
|
+
- `agentguard_cim_event`: `session_start` / `session_end`, `llm_request` /
|
|
222
|
+
`llm_response` per model request, and `tool_invoke` / `tool_complete`, each
|
|
223
|
+
with agent type `codex`, the model, the provider, and `cwd` in
|
|
224
|
+
`raw_event_data`.
|
|
225
|
+
- `agentguard_cim_metrics`: `llm.call`, `llm.latency_ms` and the token metrics,
|
|
226
|
+
one set per model request.
|
|
227
|
+
|
|
228
|
+
The prompt text, the reply text and the live `session_start` are in
|
|
229
|
+
`otel_traces` (the `user_prompt_submit`, `stop` and `session_start` spans) but
|
|
230
|
+
the app does not turn them into CIM events yet; that is
|
|
231
|
+
[timeplus-io/apps#69](https://github.com/timeplus-io/apps/issues/69).
|
|
232
|
+
|
|
233
|
+
### Pairing and timing
|
|
234
|
+
|
|
235
|
+
Each hook is its own process, so opening events record their start time on
|
|
236
|
+
disk, in `~/.agentguard/<agent-id>/codex-starts/`, and the closing event's
|
|
237
|
+
span starts there. Tool calls are paired by session, turn and `tool_use_id`
|
|
238
|
+
(so overlapping tool calls do not overwrite each other's start time), model
|
|
239
|
+
requests by request id and attempt number, subagents by subagent id,
|
|
240
|
+
compactions by turn, and the session by session id. Without a matching opening event the
|
|
241
|
+
span is still emitted, with no duration. Entries nobody collected are removed
|
|
242
|
+
after 24 hours.
|
|
243
|
+
|
|
244
|
+
### Observe only
|
|
245
|
+
|
|
246
|
+
The plugin never answers a hook. It writes nothing to stdout and exits 0, which
|
|
247
|
+
Codex treats as "no opinion": `PermissionRequest` is recorded and the approval
|
|
248
|
+
decision stays with Codex's normal flow, `Stop` does not continue the turn, and
|
|
249
|
+
`PreCompact` does not stop a compaction. Hook events this plugin does not know
|
|
250
|
+
are ignored, so a newer Codex can add hooks without breaking it.
|
|
251
|
+
|
|
252
|
+
The model request hooks run once per attempt, and each hook is a new process
|
|
253
|
+
(about 0.15 s on a laptop with a reachable endpoint). Remove `PreModelRequest`
|
|
254
|
+
and `PostModelRequest` from your configuration if you do not need per-attempt
|
|
255
|
+
status and token usage.
|
|
256
|
+
|
|
257
|
+
## Development
|
|
258
|
+
|
|
259
|
+
```bash
|
|
260
|
+
make test # unit tests; the end-to-end telemetry tests are skipped unless the SDK is installed
|
|
261
|
+
make build # sdist + wheel into dist/ (needs hatch)
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
The plugin writes nothing to stdout and returns 0 even when it fails — any
|
|
265
|
+
error goes to stderr — so hook output cannot alter a Codex turn.
|
|
266
|
+
|
|
267
|
+
### Local end-to-end test
|
|
268
|
+
|
|
269
|
+
```bash
|
|
270
|
+
cd /path/to/AgentGuard
|
|
271
|
+
|
|
272
|
+
# 1. venv with the plugin and the in-repo SDK (drop `-e sdk/python` to use the PyPI release)
|
|
273
|
+
uv venv ~/venvs/agentguard-codex --python 3.12
|
|
274
|
+
uv pip install --python ~/venvs/agentguard-codex/bin/python \
|
|
275
|
+
-e sdk/python -e agents/codex/agentguard-plugin
|
|
276
|
+
|
|
277
|
+
# 2. merge this package's hooks.json into ~/.codex/hooks.json, replacing
|
|
278
|
+
# "command" with the venv's absolute path plus the plugin's env vars:
|
|
279
|
+
# "command": "env AGENTGUARD_AGENT_ID=codex-local \
|
|
280
|
+
# OTEL_EXPORTER_OTLP_ENDPOINT=http://127.0.0.1:4318 \
|
|
281
|
+
# /Users/you/venvs/agentguard-codex/bin/agentguard-codex"
|
|
282
|
+
|
|
283
|
+
# 3. start Codex, run /hooks, review and trust the AgentGuard entries, quit
|
|
284
|
+
codex
|
|
285
|
+
|
|
286
|
+
# 4. run a turn
|
|
287
|
+
codex exec --skip-git-repo-check -s workspace-write \
|
|
288
|
+
"Run pwd, then create done.txt containing ok."
|
|
289
|
+
|
|
290
|
+
# 5. inspect the exported spans at the configured OTLP endpoint (the app's
|
|
291
|
+
# otel_traces, or any local OTLP collector), then remove the AgentGuard
|
|
292
|
+
# entries from ~/.codex/hooks.json
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
## Publishing
|
|
296
|
+
|
|
297
|
+
Releases are published to PyPI by CI:
|
|
298
|
+
|
|
299
|
+
1. Bump `version` in `pyproject.toml` and `__version__` in `agentguard_codex/__init__.py`, and merge.
|
|
300
|
+
2. Publish a GitHub Release tagged `codex-plugin-v<version>` (for example `codex-plugin-v0.1.1`).
|
|
301
|
+
|
|
302
|
+
The `Publish Codex plugin` workflow tests, builds and uploads `timeplus-agentguard-codex-plugin`. It fails if the tag does not match the package version. Running it manually builds and validates without uploading; `make check` does the same locally (needs `hatch` and `twine`).
|
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
# agentguard-codex
|
|
2
|
+
|
|
3
|
+
AgentGuard monitoring plugin for [OpenAI Codex](https://github.com/openai/codex).
|
|
4
|
+
|
|
5
|
+
Codex runs a command hook for each lifecycle event and writes one JSON object
|
|
6
|
+
to stdin. This plugin reads that event, turns it into an OpenTelemetry span
|
|
7
|
+
with the [AgentGuard Python SDK](../../../sdk/python), and exports it to
|
|
8
|
+
the [AgentGuard Timeplus app](https://github.com/timeplus-io/apps/tree/main/apps/agentguard)'s
|
|
9
|
+
OTLP/HTTP input (default `http://localhost:4318`).
|
|
10
|
+
|
|
11
|
+
## Requirements
|
|
12
|
+
|
|
13
|
+
- Python 3.10+
|
|
14
|
+
- A Codex build with hooks enabled
|
|
15
|
+
- The AgentGuard Python SDK ([`timeplus-agentguard`](https://pypi.org/project/timeplus-agentguard/) on PyPI, installed automatically as a dependency)
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
pip install timeplus-agentguard-codex-plugin
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
To work on the plugin itself, install it from a checkout instead:
|
|
24
|
+
`pip install -e agents/codex/agentguard-plugin`.
|
|
25
|
+
|
|
26
|
+
## Configure Codex hooks
|
|
27
|
+
|
|
28
|
+
Merge [`hooks.json`](hooks.json) into your Codex hook configuration —
|
|
29
|
+
`~/.codex/hooks.json`, or the equivalent `[hooks]` table in
|
|
30
|
+
`~/.codex/config.toml`:
|
|
31
|
+
|
|
32
|
+
```json
|
|
33
|
+
{
|
|
34
|
+
"hooks": {
|
|
35
|
+
"SessionStart": [
|
|
36
|
+
{ "hooks": [ { "type": "command", "command": "agentguard-codex", "timeout": 10 } ] }
|
|
37
|
+
],
|
|
38
|
+
"Stop": [
|
|
39
|
+
{ "hooks": [ { "type": "command", "command": "agentguard-codex", "timeout": 10 } ] }
|
|
40
|
+
]
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
The sample registers all 14 Codex lifecycle events with the same command.
|
|
46
|
+
Codex limits `SessionEnd` and `Interrupt` hooks to 3 seconds, so the sample
|
|
47
|
+
gives those two a `timeout` of 3 and the rest 10.
|
|
48
|
+
|
|
49
|
+
Codex skips a new or changed user-level hook until you review it. Run `/hooks`
|
|
50
|
+
in Codex once and approve the AgentGuard entries. Managed (system/MDM) hooks are
|
|
51
|
+
trusted by policy and skip this step.
|
|
52
|
+
|
|
53
|
+
If the plugin is not installed as a console script, use
|
|
54
|
+
`python -m agentguard_codex` as the command instead.
|
|
55
|
+
|
|
56
|
+
### Upgrading from the five-hook configuration
|
|
57
|
+
|
|
58
|
+
Versions before 0.2.0 handled five events: `SessionStart`, `SessionEnd`,
|
|
59
|
+
`UserPromptSubmit`, `PreToolUse` and `PostToolUse`. To capture the other nine:
|
|
60
|
+
|
|
61
|
+
1. Upgrade the package: `pip install -U timeplus-agentguard-codex-plugin`
|
|
62
|
+
(or `pipx upgrade timeplus-agentguard-codex-plugin`). Check with
|
|
63
|
+
`pip show timeplus-agentguard-codex-plugin` that the version is 0.2.0 or later.
|
|
64
|
+
Registering the new events against an older version does nothing: it
|
|
65
|
+
ignores events it does not know.
|
|
66
|
+
2. Add the nine new events to your hook configuration — `PreModelRequest`,
|
|
67
|
+
`PostModelRequest`, `Stop`, `Interrupt`, `SubagentStart`, `SubagentStop`,
|
|
68
|
+
`PermissionRequest`, `PreCompact`, `PostCompact` — copying the entries from
|
|
69
|
+
[`hooks.json`](hooks.json). Use the same `command` as your existing five
|
|
70
|
+
entries (including any absolute path, flags or `env` prefix you added), and
|
|
71
|
+
leave the existing entries in place.
|
|
72
|
+
3. Set `"timeout": 3` on `Interrupt`, and lower `SessionEnd` to 3 if yours
|
|
73
|
+
still says 10.
|
|
74
|
+
4. Start Codex, run `/hooks`, and approve the new and changed entries. Codex
|
|
75
|
+
skips them until you do; the five existing hooks keep running meanwhile.
|
|
76
|
+
|
|
77
|
+
The spans of the five existing events change in 0.2.0 too (see
|
|
78
|
+
[What gets captured](#what-gets-captured)): the agent type is now `codex`
|
|
79
|
+
instead of `agentguard-sdk`, the prompt is a `user_prompt_submit` span instead
|
|
80
|
+
of an `llm` span, and a `session_start` span is sent when the session starts.
|
|
81
|
+
Queries that filter on `service_name = 'agentguard-sdk'` or `span_name = 'llm'`
|
|
82
|
+
need updating.
|
|
83
|
+
|
|
84
|
+
## Configuration
|
|
85
|
+
|
|
86
|
+
Hooks run in a fresh process, so environment variables are the usual way to
|
|
87
|
+
configure the plugin.
|
|
88
|
+
|
|
89
|
+
| Flag | Environment variable | Default |
|
|
90
|
+
|---|---|---|
|
|
91
|
+
| `--agent-id` | `AGENTGUARD_AGENT_ID` | hostname |
|
|
92
|
+
| `--deployment-id` | `AGENTGUARD_DEPLOYMENT_ID` | `local` |
|
|
93
|
+
| `--deployment-name` | `AGENTGUARD_DEPLOYMENT_NAME` | `Local Dev` |
|
|
94
|
+
| `--endpoint` | `OTEL_EXPORTER_OTLP_ENDPOINT` | `http://localhost:4318` |
|
|
95
|
+
|
|
96
|
+
Flags take precedence over environment variables. The plugin resolves every
|
|
97
|
+
setting (flag, then environment variable, then the default above) before
|
|
98
|
+
constructing the SDK hooks, so each short-lived hook process sees the same
|
|
99
|
+
values — including the agent id, which keys the on-disk Pre/Post-tool timing
|
|
100
|
+
store to match the spans. For example:
|
|
101
|
+
|
|
102
|
+
```bash
|
|
103
|
+
export AGENTGUARD_DEPLOYMENT_ID=production
|
|
104
|
+
export AGENTGUARD_DEPLOYMENT_NAME="Production Codex"
|
|
105
|
+
export OTEL_EXPORTER_OTLP_ENDPOINT=http://timeplus.example.com:4318
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
## What gets captured
|
|
109
|
+
|
|
110
|
+
The plugin handles all 14 events in the
|
|
111
|
+
[Codex hooks reference](https://developers.openai.com/codex/hooks). Each event
|
|
112
|
+
becomes one span, exported with the OTel service name `codex`, which the
|
|
113
|
+
AgentGuard app reports as the agent type.
|
|
114
|
+
|
|
115
|
+
| Codex hook | Emitted span | What it carries |
|
|
116
|
+
|---|---|---|
|
|
117
|
+
| `SessionStart` | `session_start` | Start source (`startup`, `resume`, ...); sent immediately, so a running session is visible |
|
|
118
|
+
| `SessionEnd` | `session` | The whole session, from `SessionStart` to `SessionEnd` |
|
|
119
|
+
| `UserPromptSubmit` | `user_prompt_submit` | The prompt (`input.value`) |
|
|
120
|
+
| `PreToolUse` | none (start time recorded) | |
|
|
121
|
+
| `PostToolUse` | `tool` | `tool.name`, `tool.parameters`, `output.value`, tool duration |
|
|
122
|
+
| `PreModelRequest` | `pre_model_request` | Request id, attempt number, kind |
|
|
123
|
+
| `PostModelRequest` | `post_model_request` | The LLM call: status, error, token usage, attempt duration |
|
|
124
|
+
| `Stop` | `stop` | The final assistant message (`output.value`) |
|
|
125
|
+
| `Interrupt` | `interrupt` | The interrupted turn |
|
|
126
|
+
| `SubagentStart` | `subagent_start` | Subagent id and type |
|
|
127
|
+
| `SubagentStop` | `subagent_stop` | Subagent id and type, its last message, subagent duration |
|
|
128
|
+
| `PermissionRequest` | `permission_request` | `tool.name`, `tool.parameters`, the request description |
|
|
129
|
+
| `PreCompact` | `pre_compact` | Trigger (`manual` or `auto`) |
|
|
130
|
+
| `PostCompact` | `post_compact` | Trigger, compaction duration |
|
|
131
|
+
|
|
132
|
+
### Span attributes
|
|
133
|
+
|
|
134
|
+
On every span, when Codex sends the field:
|
|
135
|
+
|
|
136
|
+
| Attribute | Source |
|
|
137
|
+
|---|---|
|
|
138
|
+
| `session.id` | `session_id` |
|
|
139
|
+
| `run.id` | `turn_id` (turn-scoped hooks) |
|
|
140
|
+
| `agentguard.hook_name` | the hook, in snake case (`session_start`, `post_tool_use`, ...) |
|
|
141
|
+
| `llm.model_name` | `model` |
|
|
142
|
+
| `llm.provider` | `model_provider` from the first record of the session transcript; `openai` when the transcript cannot be read |
|
|
143
|
+
| `cwd` | `cwd`, the session's working directory |
|
|
144
|
+
| `codex.permission_mode` | `permission_mode` |
|
|
145
|
+
| `agentguard.subagent.id`, `agentguard.subagent.type` | `agent_id`, `agent_type` (subagent spans, and any other hook fired inside a subagent) |
|
|
146
|
+
|
|
147
|
+
Three spans carry the kind attribute the app's OpenInference views key on:
|
|
148
|
+
|
|
149
|
+
| Span | Kind attribute | The app derives |
|
|
150
|
+
|---|---|---|
|
|
151
|
+
| `post_model_request` | `openinference.span.kind=LLM` | `llm_request` / `llm_response` events, and the `llm.*` metrics |
|
|
152
|
+
| `tool` | `openinference.span.kind=TOOL` | `tool_invoke` / `tool_complete` events |
|
|
153
|
+
| `session` | `agentguard.span.kind=SESSION` | `session_start` / `session_end` events |
|
|
154
|
+
|
|
155
|
+
Per span:
|
|
156
|
+
|
|
157
|
+
| Attribute | Set on | Source |
|
|
158
|
+
|---|---|---|
|
|
159
|
+
| `source` | `session_start` | `source` |
|
|
160
|
+
| `codex.session.end_reason` | `session` | `reason` |
|
|
161
|
+
| `input.value` | `user_prompt_submit` | `prompt`, as JSON: `{"messages": [{"role": "user", "content": "..."}]}` |
|
|
162
|
+
| `tool.name`, `tool.parameters`, `output.value`, `tool_use_id` | `tool` | `tool_name`, `tool_input`, `tool_response`, `tool_use_id` |
|
|
163
|
+
| `task.id` | `tool`: the tool call id; model request spans: the request id; subagent spans: the subagent id | `tool_use_id`, `request_id`, `agent_id` |
|
|
164
|
+
| `codex.request.id`, `codex.request.attempt`, `codex.request.kind`, `codex.thread_id` | model request spans | `request_id`, `attempt_number`, `kind`, `thread_id` |
|
|
165
|
+
| `codex.request.status`, `codex.request.error` | `post_model_request` | `status` (`completed`, `failed`, `cancelled`), `error` |
|
|
166
|
+
| `llm.token_count.prompt`, `.completion`, `.total`, `.prompt_details.cache_read`, `.prompt_details.cache_write`, `.completion_details.reasoning` | `post_model_request`, when Codex reports usage | `token_usage` |
|
|
167
|
+
| `output.value` | `stop`, `subagent_stop` | `last_assistant_message` |
|
|
168
|
+
| `codex.stop_hook_active` | `stop`, `subagent_stop` | `stop_hook_active` |
|
|
169
|
+
| `codex.agent_transcript_path` | `subagent_stop` | `agent_transcript_path` |
|
|
170
|
+
| `tool.name`, `tool.parameters`, `codex.permission.description` | `permission_request` | `tool_name`, `tool_input`, `tool_input.description` |
|
|
171
|
+
| `codex.compact.trigger` | `pre_compact`, `post_compact` | `trigger` |
|
|
172
|
+
|
|
173
|
+
An attribute is left off the span when Codex does not send the field. A
|
|
174
|
+
`post_model_request` span with status `failed` also has the OTel span status
|
|
175
|
+
`ERROR`. Token counts are passed through as Codex reports them; whether
|
|
176
|
+
`input_tokens` already includes the cached tokens is not yet confirmed (see
|
|
177
|
+
[`docs/todo.md`](../docs/todo.md), item 0.4).
|
|
178
|
+
|
|
179
|
+
### Prompts, model requests and final responses
|
|
180
|
+
|
|
181
|
+
These are three separate records, so counting one does not count another:
|
|
182
|
+
|
|
183
|
+
- **Prompt** — one `user_prompt_submit` span per prompt. It is not an LLM
|
|
184
|
+
call: it has no duration and no token usage.
|
|
185
|
+
- **Model request** — one `pre_model_request` / `post_model_request` pair per
|
|
186
|
+
attempt. Only `post_model_request` is an LLM-kind span, so there is one LLM
|
|
187
|
+
call per attempt, with its real latency and token usage. A retry is a new
|
|
188
|
+
pair with the same `codex.request.id` and the next `codex.request.attempt`.
|
|
189
|
+
- **Final response** — one `stop` span per completed turn, with the assistant's
|
|
190
|
+
last message and no usage. `Stop` ends a turn, not the session. An
|
|
191
|
+
interrupted turn produces an `interrupt` span instead.
|
|
192
|
+
|
|
193
|
+
Subagent hooks carry the parent's `session.id`; `agentguard.subagent.id`
|
|
194
|
+
identifies the child.
|
|
195
|
+
|
|
196
|
+
### What the AgentGuard app shows
|
|
197
|
+
|
|
198
|
+
With the app's current OpenInference views (app 0.4.0), a Codex session gives:
|
|
199
|
+
|
|
200
|
+
- `agentguard_cim_event`: `session_start` / `session_end`, `llm_request` /
|
|
201
|
+
`llm_response` per model request, and `tool_invoke` / `tool_complete`, each
|
|
202
|
+
with agent type `codex`, the model, the provider, and `cwd` in
|
|
203
|
+
`raw_event_data`.
|
|
204
|
+
- `agentguard_cim_metrics`: `llm.call`, `llm.latency_ms` and the token metrics,
|
|
205
|
+
one set per model request.
|
|
206
|
+
|
|
207
|
+
The prompt text, the reply text and the live `session_start` are in
|
|
208
|
+
`otel_traces` (the `user_prompt_submit`, `stop` and `session_start` spans) but
|
|
209
|
+
the app does not turn them into CIM events yet; that is
|
|
210
|
+
[timeplus-io/apps#69](https://github.com/timeplus-io/apps/issues/69).
|
|
211
|
+
|
|
212
|
+
### Pairing and timing
|
|
213
|
+
|
|
214
|
+
Each hook is its own process, so opening events record their start time on
|
|
215
|
+
disk, in `~/.agentguard/<agent-id>/codex-starts/`, and the closing event's
|
|
216
|
+
span starts there. Tool calls are paired by session, turn and `tool_use_id`
|
|
217
|
+
(so overlapping tool calls do not overwrite each other's start time), model
|
|
218
|
+
requests by request id and attempt number, subagents by subagent id,
|
|
219
|
+
compactions by turn, and the session by session id. Without a matching opening event the
|
|
220
|
+
span is still emitted, with no duration. Entries nobody collected are removed
|
|
221
|
+
after 24 hours.
|
|
222
|
+
|
|
223
|
+
### Observe only
|
|
224
|
+
|
|
225
|
+
The plugin never answers a hook. It writes nothing to stdout and exits 0, which
|
|
226
|
+
Codex treats as "no opinion": `PermissionRequest` is recorded and the approval
|
|
227
|
+
decision stays with Codex's normal flow, `Stop` does not continue the turn, and
|
|
228
|
+
`PreCompact` does not stop a compaction. Hook events this plugin does not know
|
|
229
|
+
are ignored, so a newer Codex can add hooks without breaking it.
|
|
230
|
+
|
|
231
|
+
The model request hooks run once per attempt, and each hook is a new process
|
|
232
|
+
(about 0.15 s on a laptop with a reachable endpoint). Remove `PreModelRequest`
|
|
233
|
+
and `PostModelRequest` from your configuration if you do not need per-attempt
|
|
234
|
+
status and token usage.
|
|
235
|
+
|
|
236
|
+
## Development
|
|
237
|
+
|
|
238
|
+
```bash
|
|
239
|
+
make test # unit tests; the end-to-end telemetry tests are skipped unless the SDK is installed
|
|
240
|
+
make build # sdist + wheel into dist/ (needs hatch)
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
The plugin writes nothing to stdout and returns 0 even when it fails — any
|
|
244
|
+
error goes to stderr — so hook output cannot alter a Codex turn.
|
|
245
|
+
|
|
246
|
+
### Local end-to-end test
|
|
247
|
+
|
|
248
|
+
```bash
|
|
249
|
+
cd /path/to/AgentGuard
|
|
250
|
+
|
|
251
|
+
# 1. venv with the plugin and the in-repo SDK (drop `-e sdk/python` to use the PyPI release)
|
|
252
|
+
uv venv ~/venvs/agentguard-codex --python 3.12
|
|
253
|
+
uv pip install --python ~/venvs/agentguard-codex/bin/python \
|
|
254
|
+
-e sdk/python -e agents/codex/agentguard-plugin
|
|
255
|
+
|
|
256
|
+
# 2. merge this package's hooks.json into ~/.codex/hooks.json, replacing
|
|
257
|
+
# "command" with the venv's absolute path plus the plugin's env vars:
|
|
258
|
+
# "command": "env AGENTGUARD_AGENT_ID=codex-local \
|
|
259
|
+
# OTEL_EXPORTER_OTLP_ENDPOINT=http://127.0.0.1:4318 \
|
|
260
|
+
# /Users/you/venvs/agentguard-codex/bin/agentguard-codex"
|
|
261
|
+
|
|
262
|
+
# 3. start Codex, run /hooks, review and trust the AgentGuard entries, quit
|
|
263
|
+
codex
|
|
264
|
+
|
|
265
|
+
# 4. run a turn
|
|
266
|
+
codex exec --skip-git-repo-check -s workspace-write \
|
|
267
|
+
"Run pwd, then create done.txt containing ok."
|
|
268
|
+
|
|
269
|
+
# 5. inspect the exported spans at the configured OTLP endpoint (the app's
|
|
270
|
+
# otel_traces, or any local OTLP collector), then remove the AgentGuard
|
|
271
|
+
# entries from ~/.codex/hooks.json
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
## Publishing
|
|
275
|
+
|
|
276
|
+
Releases are published to PyPI by CI:
|
|
277
|
+
|
|
278
|
+
1. Bump `version` in `pyproject.toml` and `__version__` in `agentguard_codex/__init__.py`, and merge.
|
|
279
|
+
2. Publish a GitHub Release tagged `codex-plugin-v<version>` (for example `codex-plugin-v0.1.1`).
|
|
280
|
+
|
|
281
|
+
The `Publish Codex plugin` workflow tests, builds and uploads `timeplus-agentguard-codex-plugin`. It fails if the tag does not match the package version. Running it manually builds and validates without uploading; `make check` does the same locally (needs `hatch` and `twine`).
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"""``agentguard-codex`` — Codex hook entry point for AgentGuard.
|
|
2
2
|
|
|
3
3
|
Codex runs this command once per hook event. It reads the event JSON from
|
|
4
|
-
stdin and hands it to :func:`agentguard_codex.events.dispatch`, which
|
|
5
|
-
the AgentGuard Python SDK. See ``hooks.json`` for a sample Codex hook
|
|
4
|
+
stdin and hands it to :func:`agentguard_codex.events.dispatch`, which emits
|
|
5
|
+
spans through the AgentGuard Python SDK. See ``hooks.json`` for a sample Codex hook
|
|
6
6
|
configuration.
|
|
7
7
|
"""
|
|
8
8
|
|
|
@@ -16,6 +16,7 @@ import sys
|
|
|
16
16
|
|
|
17
17
|
from agentguard_codex.events import dispatch
|
|
18
18
|
|
|
19
|
+
AGENT_TYPE = "codex"
|
|
19
20
|
DEFAULT_DEPLOYMENT_ID = "local"
|
|
20
21
|
DEFAULT_DEPLOYMENT_NAME = "Local Dev"
|
|
21
22
|
DEFAULT_ENDPOINT = "http://localhost:4318"
|
|
@@ -55,12 +56,10 @@ def build_parser() -> argparse.ArgumentParser:
|
|
|
55
56
|
|
|
56
57
|
|
|
57
58
|
def resolve_agent_id(agent_id: str | None) -> str:
|
|
58
|
-
"""Resolve the agent id
|
|
59
|
+
"""Resolve the agent id: flag, then env, then the hostname.
|
|
59
60
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
here and pass the result through; otherwise agents that omit ``--agent-id``
|
|
63
|
-
would all share one Pre/Post-tool timing store.
|
|
61
|
+
Resolved here, not left to the SDK, because the same value names the
|
|
62
|
+
folder of the on-disk start-time store.
|
|
64
63
|
"""
|
|
65
64
|
return agent_id or os.environ.get("AGENTGUARD_AGENT_ID") or socket.gethostname()
|
|
66
65
|
|
|
@@ -92,6 +91,11 @@ def resolve_endpoint(endpoint: str | None) -> str:
|
|
|
92
91
|
)
|
|
93
92
|
|
|
94
93
|
|
|
94
|
+
def start_store_folder(agent_id: str) -> str:
|
|
95
|
+
"""Where the start times of open sessions, tools and requests live."""
|
|
96
|
+
return os.path.join(os.path.expanduser("~"), ".agentguard", agent_id, "codex-starts")
|
|
97
|
+
|
|
98
|
+
|
|
95
99
|
def main(argv: list[str] | None = None) -> int:
|
|
96
100
|
try:
|
|
97
101
|
args = build_parser().parse_args(argv)
|
|
@@ -99,17 +103,25 @@ def main(argv: list[str] | None = None) -> int:
|
|
|
99
103
|
# Imported lazily so ``import agentguard_codex.cli`` (and ``--help``)
|
|
100
104
|
# works without the AgentGuard SDK, and a missing SDK is handled by
|
|
101
105
|
# the same fail-safe path as any other plugin error.
|
|
102
|
-
from agentguard
|
|
106
|
+
from agentguard import AgentGuard
|
|
107
|
+
|
|
108
|
+
from agentguard_codex.sink import SdkSink, StartStore
|
|
103
109
|
|
|
104
|
-
|
|
105
|
-
|
|
110
|
+
agent_id = resolve_agent_id(args.agent_id)
|
|
111
|
+
client = AgentGuard(
|
|
112
|
+
# The app reports the OTel service name as the agent type.
|
|
113
|
+
service_name=AGENT_TYPE,
|
|
114
|
+
agent_id=agent_id,
|
|
106
115
|
deployment_id=resolve_deployment_id(args.deployment_id),
|
|
107
116
|
deployment_name=resolve_deployment_name(args.deployment_name),
|
|
108
|
-
|
|
117
|
+
otlp_endpoint=resolve_endpoint(args.endpoint),
|
|
109
118
|
)
|
|
119
|
+
sink = SdkSink(client, StartStore(start_store_folder(agent_id)))
|
|
110
120
|
|
|
111
|
-
|
|
112
|
-
|
|
121
|
+
try:
|
|
122
|
+
dispatch(json.loads(sys.stdin.read()), sink)
|
|
123
|
+
finally:
|
|
124
|
+
sink.close()
|
|
113
125
|
except SystemExit:
|
|
114
126
|
# argparse raises SystemExit for --help (0) and bad flags (2). A bad
|
|
115
127
|
# flag must not change the Codex turn, so report success either way.
|