timeplus-agentguard-codex-plugin 0.2.0__tar.gz → 0.4.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 → timeplus_agentguard_codex_plugin-0.4.0}/PKG-INFO +134 -10
- {timeplus_agentguard_codex_plugin-0.2.0 → timeplus_agentguard_codex_plugin-0.4.0}/README.md +133 -9
- {timeplus_agentguard_codex_plugin-0.2.0 → timeplus_agentguard_codex_plugin-0.4.0}/agentguard_codex/__init__.py +1 -1
- {timeplus_agentguard_codex_plugin-0.2.0 → timeplus_agentguard_codex_plugin-0.4.0}/agentguard_codex/cli.py +73 -21
- {timeplus_agentguard_codex_plugin-0.2.0 → timeplus_agentguard_codex_plugin-0.4.0}/agentguard_codex/events.py +87 -7
- timeplus_agentguard_codex_plugin-0.4.0/agentguard_codex/hookevents.py +218 -0
- {timeplus_agentguard_codex_plugin-0.2.0 → timeplus_agentguard_codex_plugin-0.4.0}/agentguard_codex/sink.py +4 -0
- timeplus_agentguard_codex_plugin-0.4.0/agentguard_codex/transcript.py +226 -0
- {timeplus_agentguard_codex_plugin-0.2.0 → timeplus_agentguard_codex_plugin-0.4.0}/pyproject.toml +1 -1
- {timeplus_agentguard_codex_plugin-0.2.0 → timeplus_agentguard_codex_plugin-0.4.0}/tests/test_cli.py +23 -0
- {timeplus_agentguard_codex_plugin-0.2.0 → timeplus_agentguard_codex_plugin-0.4.0}/tests/test_events.py +135 -14
- timeplus_agentguard_codex_plugin-0.4.0/tests/test_hookevents.py +360 -0
- timeplus_agentguard_codex_plugin-0.4.0/tests/test_transcript.py +213 -0
- {timeplus_agentguard_codex_plugin-0.2.0 → timeplus_agentguard_codex_plugin-0.4.0}/.gitignore +0 -0
- {timeplus_agentguard_codex_plugin-0.2.0 → timeplus_agentguard_codex_plugin-0.4.0}/Makefile +0 -0
- {timeplus_agentguard_codex_plugin-0.2.0 → timeplus_agentguard_codex_plugin-0.4.0}/agentguard_codex/__main__.py +0 -0
- {timeplus_agentguard_codex_plugin-0.2.0 → timeplus_agentguard_codex_plugin-0.4.0}/hooks.json +0 -0
- {timeplus_agentguard_codex_plugin-0.2.0 → timeplus_agentguard_codex_plugin-0.4.0}/tests/test_sink.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: timeplus-agentguard-codex-plugin
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.4.0
|
|
4
4
|
Summary: AgentGuard monitoring plugin for OpenAI Codex
|
|
5
5
|
Project-URL: Homepage, https://github.com/timeplus-io/agentguard
|
|
6
6
|
Project-URL: Repository, https://github.com/timeplus-io/agentguard
|
|
@@ -24,16 +24,24 @@ Description-Content-Type: text/markdown
|
|
|
24
24
|
AgentGuard monitoring plugin for [OpenAI Codex](https://github.com/openai/codex).
|
|
25
25
|
|
|
26
26
|
Codex runs a command hook for each lifecycle event and writes one JSON object
|
|
27
|
-
to stdin. This plugin reads that event
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
27
|
+
to stdin. This plugin reads that event and sends it to the
|
|
28
|
+
[AgentGuard Timeplus app](https://github.com/timeplus-io/apps/tree/main/apps/agentguard)
|
|
29
|
+
on one of two export paths:
|
|
30
|
+
|
|
31
|
+
| Export | What is sent | Where | Needs |
|
|
32
|
+
|---|---|---|---|
|
|
33
|
+
| `otel` (default) | One OpenTelemetry span per hook, through the [AgentGuard Python SDK](../../../sdk/python) | The app's OTLP/HTTP input (default `http://localhost:4318`) | Port 4318 |
|
|
34
|
+
| `hooks` | One row per hook in the `agentguard_hook_events` stream, like the Claude Code, OpenClaw and Hermes plugins | Timeplus ingest (default `http://localhost:3218`) | Port 3218, or port 8000 with the Timeplus REST API |
|
|
35
|
+
|
|
36
|
+
Exactly one path is active. There is no setting that sends a session on both,
|
|
37
|
+
so the app never sees the same session twice. See
|
|
38
|
+
[Hook-event export](#hook-event-export) for the second path.
|
|
31
39
|
|
|
32
40
|
## Requirements
|
|
33
41
|
|
|
34
42
|
- Python 3.10+
|
|
35
43
|
- 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)
|
|
44
|
+
- The AgentGuard Python SDK ([`timeplus-agentguard`](https://pypi.org/project/timeplus-agentguard/) on PyPI, installed automatically as a dependency). Only the `otel` export uses it; the `hooks` export uses the standard library alone.
|
|
37
45
|
|
|
38
46
|
## Installation
|
|
39
47
|
|
|
@@ -112,7 +120,21 @@ configure the plugin.
|
|
|
112
120
|
| `--agent-id` | `AGENTGUARD_AGENT_ID` | hostname |
|
|
113
121
|
| `--deployment-id` | `AGENTGUARD_DEPLOYMENT_ID` | `local` |
|
|
114
122
|
| `--deployment-name` | `AGENTGUARD_DEPLOYMENT_NAME` | `Local Dev` |
|
|
123
|
+
| `--export` | `AGENTGUARD_CODEX_EXPORT` | `otel` |
|
|
115
124
|
| `--endpoint` | `OTEL_EXPORTER_OTLP_ENDPOINT` | `http://localhost:4318` |
|
|
125
|
+
| | `AGENTGUARD_LLM_CONTENT` | `true` |
|
|
126
|
+
| | `AGENTGUARD_LLM_CONTENT_CHARS` | `4000` |
|
|
127
|
+
|
|
128
|
+
`--export` is `otel` or `hooks`. Any other value (including `both`) is
|
|
129
|
+
reported on stderr and treated as `otel`. `--endpoint` applies to the `otel`
|
|
130
|
+
export only; the `hooks` export has its own settings, listed under
|
|
131
|
+
[Hook-event export](#hook-event-export).
|
|
132
|
+
|
|
133
|
+
`AGENTGUARD_LLM_CONTENT` and `AGENTGUARD_LLM_CONTENT_CHARS` apply to both
|
|
134
|
+
exports. They control the response text of a model request: `false` (or `0`,
|
|
135
|
+
`off`, `no`) leaves the text out and keeps its length, the tool calls and the
|
|
136
|
+
phase; the second is the number of characters the text is cut to. See
|
|
137
|
+
[The response of a model request](#the-response-of-a-model-request).
|
|
116
138
|
|
|
117
139
|
Flags take precedence over environment variables. The plugin resolves every
|
|
118
140
|
setting (flag, then environment variable, then the default above) before
|
|
@@ -126,12 +148,84 @@ export AGENTGUARD_DEPLOYMENT_NAME="Production Codex"
|
|
|
126
148
|
export OTEL_EXPORTER_OTLP_ENDPOINT=http://timeplus.example.com:4318
|
|
127
149
|
```
|
|
128
150
|
|
|
151
|
+
## Hook-event export
|
|
152
|
+
|
|
153
|
+
Use this path when the app's OTLP input (port 4318) is not reachable from the
|
|
154
|
+
agent host, or when you want Codex in the same stream as the other plugins.
|
|
155
|
+
|
|
156
|
+
```bash
|
|
157
|
+
export AGENTGUARD_CODEX_EXPORT=hooks
|
|
158
|
+
export AGENTGUARD_TIMEPLUS_URL=http://timeplus.example.com:3218
|
|
159
|
+
export AGENTGUARD_USERNAME=<timeplus-user>
|
|
160
|
+
export AGENTGUARD_PASSWORD=<timeplus-password>
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
| Environment variable | Default | Purpose |
|
|
164
|
+
|---|---|---|
|
|
165
|
+
| `AGENTGUARD_TIMEPLUS_URL` | `http://localhost:3218` | Timeplus base URL |
|
|
166
|
+
| `AGENTGUARD_INGEST_API` | `timeplusd` | `timeplusd` (port 3218) or `rest` (the Timeplus REST API, port 8000) |
|
|
167
|
+
| `AGENTGUARD_WORKSPACE` | `default` | REST API workspace id; `default` is correct for self-hosted |
|
|
168
|
+
| `AGENTGUARD_DATABASE` | `ag` | Database of the AgentGuard app |
|
|
169
|
+
| `AGENTGUARD_STREAM` | `agentguard_hook_events` | Target stream |
|
|
170
|
+
| `AGENTGUARD_USERNAME` | `proton` | Timeplus username (Basic auth) |
|
|
171
|
+
| `AGENTGUARD_PASSWORD` | _(empty)_ | Timeplus password (Basic auth) |
|
|
172
|
+
|
|
173
|
+
These are the settings the other hook-event plugins use, with the same
|
|
174
|
+
meaning. They have no flags.
|
|
175
|
+
|
|
176
|
+
Each of the 14 hooks becomes one row:
|
|
177
|
+
|
|
178
|
+
| Column | Value |
|
|
179
|
+
|---|---|
|
|
180
|
+
| `hook_name` | The hook in snake case: `session_start`, `session_end`, `user_prompt_submit`, `pre_tool_use`, `post_tool_use`, `pre_model_request`, `post_model_request`, `stop`, `interrupt`, `subagent_start`, `permission_request`, `pre_compact`, `post_compact`. `SubagentStop` is written as `subagent_ended`, the name the stream already uses for it. |
|
|
181
|
+
| `agent_type` | `codex` |
|
|
182
|
+
| `session_id` | The Codex session id |
|
|
183
|
+
| `run_id` | The turn id, or the session id on hooks without a turn (`SessionStart`, `SessionEnd`) |
|
|
184
|
+
| `agent_id`, `deployment_id`, `deployment_name` | From the settings above |
|
|
185
|
+
| `tool_name` | The tool, on tool and permission hooks |
|
|
186
|
+
| `model`, `provider` | The model slug; the provider from the session transcript, or `openai` |
|
|
187
|
+
| `hook_decision` | `observe` |
|
|
188
|
+
| `event_data` | The Codex hook payload as received (`prompt`, `last_assistant_message`, `tool_input`, `tool_response`, `tool_use_id`, `token_usage`, `cwd`, `source`, `trigger`, and the subagent's `agent_id` and `agent_type`), plus the keys below |
|
|
189
|
+
|
|
190
|
+
These keys are added to `event_data`:
|
|
191
|
+
|
|
192
|
+
- `response` and `request` are on `post_model_request`, when the request is
|
|
193
|
+
found in the session transcript. See
|
|
194
|
+
[The response of a model request](#the-response-of-a-model-request).
|
|
195
|
+
|
|
196
|
+
- `agentguard.export` is always `hooks`. On the `otel` path the app copies
|
|
197
|
+
some spans into `agentguard_hook_events` with the same `agent_type`; this
|
|
198
|
+
key is how a view tells a row written by the plugin from a copied one.
|
|
199
|
+
- `agentguard.duration_ms` is on closing events that had an opening event:
|
|
200
|
+
`post_tool_use`, `post_model_request`, `post_compact`, `subagent_ended` and
|
|
201
|
+
`session_end`. See [Pairing and timing](#pairing-and-timing).
|
|
202
|
+
|
|
203
|
+
The rows of one hook process go out in a single request with a 2 second
|
|
204
|
+
timeout, so `SessionEnd` and `Interrupt` finish inside Codex's 3 second limit.
|
|
205
|
+
|
|
206
|
+
Check what arrived with:
|
|
207
|
+
|
|
208
|
+
```sql
|
|
209
|
+
SELECT event_time, hook_name, session_id, run_id, tool_name, model
|
|
210
|
+
FROM table(ag.agentguard_hook_events)
|
|
211
|
+
WHERE agent_type = 'codex' AND json_value(event_data, '$."agentguard.export"') = 'hooks'
|
|
212
|
+
ORDER BY event_time DESC
|
|
213
|
+
LIMIT 20;
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
The app does not turn these rows into `agentguard_cim_event` and
|
|
217
|
+
`agentguard_cim_metrics` rows yet. That is
|
|
218
|
+
[timeplus-io/apps#71](https://github.com/timeplus-io/apps/issues/71); until it
|
|
219
|
+
ships, the `hooks` export fills `agentguard_hook_events` only.
|
|
220
|
+
|
|
129
221
|
## What gets captured
|
|
130
222
|
|
|
131
223
|
The plugin handles all 14 events in the
|
|
132
|
-
[Codex hooks reference](https://developers.openai.com/codex/hooks).
|
|
133
|
-
|
|
134
|
-
|
|
224
|
+
[Codex hooks reference](https://developers.openai.com/codex/hooks). On the
|
|
225
|
+
`otel` export, described in the rest of this section, each event becomes one
|
|
226
|
+
span, exported with the OTel service name `codex`, which the AgentGuard app
|
|
227
|
+
reports as the agent type. The `hooks` export handles the same 14 events; its
|
|
228
|
+
row format is under [Hook-event export](#hook-event-export).
|
|
135
229
|
|
|
136
230
|
| Codex hook | Emitted span | What it carries |
|
|
137
231
|
|---|---|---|
|
|
@@ -185,6 +279,8 @@ Per span:
|
|
|
185
279
|
| `codex.request.id`, `codex.request.attempt`, `codex.request.kind`, `codex.thread_id` | model request spans | `request_id`, `attempt_number`, `kind`, `thread_id` |
|
|
186
280
|
| `codex.request.status`, `codex.request.error` | `post_model_request` | `status` (`completed`, `failed`, `cancelled`), `error` |
|
|
187
281
|
| `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` |
|
|
282
|
+
| `agentguard.response.text`, `.text_chars`, `.tool_calls`, `.phase` | `post_model_request`, when the request is found in the session transcript | the response in the transcript; `tool_calls` is a JSON list of `{id, name}` |
|
|
283
|
+
| `agentguard.request.effort`, `agentguard.request.summary` | `post_model_request`, same condition | the turn's `turn_context` in the transcript |
|
|
188
284
|
| `output.value` | `stop`, `subagent_stop` | `last_assistant_message` |
|
|
189
285
|
| `codex.stop_hook_active` | `stop`, `subagent_stop` | `stop_hook_active` |
|
|
190
286
|
| `codex.agent_transcript_path` | `subagent_stop` | `agent_transcript_path` |
|
|
@@ -214,6 +310,34 @@ These are three separate records, so counting one does not count another:
|
|
|
214
310
|
Subagent hooks carry the parent's `session.id`; `agentguard.subagent.id`
|
|
215
311
|
identifies the child.
|
|
216
312
|
|
|
313
|
+
### The response of a model request
|
|
314
|
+
|
|
315
|
+
The `PostModelRequest` payload has the token usage of a request and none of
|
|
316
|
+
its content. The plugin reads the content from the session transcript
|
|
317
|
+
(`transcript_path`) and adds it to the `post_model_request` row or span:
|
|
318
|
+
|
|
319
|
+
| Hook-event row (`event_data`) | Span attribute | Content |
|
|
320
|
+
|---|---|---|
|
|
321
|
+
| `response.text` | `agentguard.response.text` | The assistant text of the response, cut to `AGENTGUARD_LLM_CONTENT_CHARS`. Left out when `AGENTGUARD_LLM_CONTENT=false`. |
|
|
322
|
+
| `response.text_chars` | `agentguard.response.text_chars` | Length of the text before the cut |
|
|
323
|
+
| `response.tool_calls` | `agentguard.response.tool_calls` | The tool calls the response asked for, as `{id, name}`. The id is the transcript's `call_id`. Inputs and results are on the tool events. |
|
|
324
|
+
| `response.phase` | `agentguard.response.phase` | `commentary` or `final_answer`, when Codex records it |
|
|
325
|
+
| `request.effort`, `request.summary` | `agentguard.request.effort`, `agentguard.request.summary` | The reasoning settings of the turn |
|
|
326
|
+
|
|
327
|
+
Notes:
|
|
328
|
+
|
|
329
|
+
- The request is found by its turn and its token counts, because the hook and
|
|
330
|
+
the transcript share no id that is known to match. When it is not found
|
|
331
|
+
(a failed or cancelled attempt, a transcript that cannot be read, a format
|
|
332
|
+
this version does not know), the row or span is sent without these fields.
|
|
333
|
+
- The transcript may not hold the response yet when the hook runs. The plugin
|
|
334
|
+
waits up to 0.5 seconds for it; Codex waits for the hook during that time.
|
|
335
|
+
- Reasoning content is not sent: the transcript holds it encrypted. Its size
|
|
336
|
+
is `reasoning_output_tokens` in the token usage.
|
|
337
|
+
- The span attributes are AgentGuard's own names, not OpenInference's
|
|
338
|
+
`llm.output_messages`, so the app does not show a line of commentary before
|
|
339
|
+
a tool call as the assistant's reply.
|
|
340
|
+
|
|
217
341
|
### What the AgentGuard app shows
|
|
218
342
|
|
|
219
343
|
With the app's current OpenInference views (app 0.4.0), a Codex session gives:
|
|
@@ -257,7 +381,7 @@ status and token usage.
|
|
|
257
381
|
## Development
|
|
258
382
|
|
|
259
383
|
```bash
|
|
260
|
-
make test # unit tests; the end-to-end
|
|
384
|
+
make test # unit tests; the end-to-end OTel tests are skipped unless the SDK is installed
|
|
261
385
|
make build # sdist + wheel into dist/ (needs hatch)
|
|
262
386
|
```
|
|
263
387
|
|
|
@@ -3,16 +3,24 @@
|
|
|
3
3
|
AgentGuard monitoring plugin for [OpenAI Codex](https://github.com/openai/codex).
|
|
4
4
|
|
|
5
5
|
Codex runs a command hook for each lifecycle event and writes one JSON object
|
|
6
|
-
to stdin. This plugin reads that event
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
to stdin. This plugin reads that event and sends it to the
|
|
7
|
+
[AgentGuard Timeplus app](https://github.com/timeplus-io/apps/tree/main/apps/agentguard)
|
|
8
|
+
on one of two export paths:
|
|
9
|
+
|
|
10
|
+
| Export | What is sent | Where | Needs |
|
|
11
|
+
|---|---|---|---|
|
|
12
|
+
| `otel` (default) | One OpenTelemetry span per hook, through the [AgentGuard Python SDK](../../../sdk/python) | The app's OTLP/HTTP input (default `http://localhost:4318`) | Port 4318 |
|
|
13
|
+
| `hooks` | One row per hook in the `agentguard_hook_events` stream, like the Claude Code, OpenClaw and Hermes plugins | Timeplus ingest (default `http://localhost:3218`) | Port 3218, or port 8000 with the Timeplus REST API |
|
|
14
|
+
|
|
15
|
+
Exactly one path is active. There is no setting that sends a session on both,
|
|
16
|
+
so the app never sees the same session twice. See
|
|
17
|
+
[Hook-event export](#hook-event-export) for the second path.
|
|
10
18
|
|
|
11
19
|
## Requirements
|
|
12
20
|
|
|
13
21
|
- Python 3.10+
|
|
14
22
|
- 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)
|
|
23
|
+
- The AgentGuard Python SDK ([`timeplus-agentguard`](https://pypi.org/project/timeplus-agentguard/) on PyPI, installed automatically as a dependency). Only the `otel` export uses it; the `hooks` export uses the standard library alone.
|
|
16
24
|
|
|
17
25
|
## Installation
|
|
18
26
|
|
|
@@ -91,7 +99,21 @@ configure the plugin.
|
|
|
91
99
|
| `--agent-id` | `AGENTGUARD_AGENT_ID` | hostname |
|
|
92
100
|
| `--deployment-id` | `AGENTGUARD_DEPLOYMENT_ID` | `local` |
|
|
93
101
|
| `--deployment-name` | `AGENTGUARD_DEPLOYMENT_NAME` | `Local Dev` |
|
|
102
|
+
| `--export` | `AGENTGUARD_CODEX_EXPORT` | `otel` |
|
|
94
103
|
| `--endpoint` | `OTEL_EXPORTER_OTLP_ENDPOINT` | `http://localhost:4318` |
|
|
104
|
+
| | `AGENTGUARD_LLM_CONTENT` | `true` |
|
|
105
|
+
| | `AGENTGUARD_LLM_CONTENT_CHARS` | `4000` |
|
|
106
|
+
|
|
107
|
+
`--export` is `otel` or `hooks`. Any other value (including `both`) is
|
|
108
|
+
reported on stderr and treated as `otel`. `--endpoint` applies to the `otel`
|
|
109
|
+
export only; the `hooks` export has its own settings, listed under
|
|
110
|
+
[Hook-event export](#hook-event-export).
|
|
111
|
+
|
|
112
|
+
`AGENTGUARD_LLM_CONTENT` and `AGENTGUARD_LLM_CONTENT_CHARS` apply to both
|
|
113
|
+
exports. They control the response text of a model request: `false` (or `0`,
|
|
114
|
+
`off`, `no`) leaves the text out and keeps its length, the tool calls and the
|
|
115
|
+
phase; the second is the number of characters the text is cut to. See
|
|
116
|
+
[The response of a model request](#the-response-of-a-model-request).
|
|
95
117
|
|
|
96
118
|
Flags take precedence over environment variables. The plugin resolves every
|
|
97
119
|
setting (flag, then environment variable, then the default above) before
|
|
@@ -105,12 +127,84 @@ export AGENTGUARD_DEPLOYMENT_NAME="Production Codex"
|
|
|
105
127
|
export OTEL_EXPORTER_OTLP_ENDPOINT=http://timeplus.example.com:4318
|
|
106
128
|
```
|
|
107
129
|
|
|
130
|
+
## Hook-event export
|
|
131
|
+
|
|
132
|
+
Use this path when the app's OTLP input (port 4318) is not reachable from the
|
|
133
|
+
agent host, or when you want Codex in the same stream as the other plugins.
|
|
134
|
+
|
|
135
|
+
```bash
|
|
136
|
+
export AGENTGUARD_CODEX_EXPORT=hooks
|
|
137
|
+
export AGENTGUARD_TIMEPLUS_URL=http://timeplus.example.com:3218
|
|
138
|
+
export AGENTGUARD_USERNAME=<timeplus-user>
|
|
139
|
+
export AGENTGUARD_PASSWORD=<timeplus-password>
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
| Environment variable | Default | Purpose |
|
|
143
|
+
|---|---|---|
|
|
144
|
+
| `AGENTGUARD_TIMEPLUS_URL` | `http://localhost:3218` | Timeplus base URL |
|
|
145
|
+
| `AGENTGUARD_INGEST_API` | `timeplusd` | `timeplusd` (port 3218) or `rest` (the Timeplus REST API, port 8000) |
|
|
146
|
+
| `AGENTGUARD_WORKSPACE` | `default` | REST API workspace id; `default` is correct for self-hosted |
|
|
147
|
+
| `AGENTGUARD_DATABASE` | `ag` | Database of the AgentGuard app |
|
|
148
|
+
| `AGENTGUARD_STREAM` | `agentguard_hook_events` | Target stream |
|
|
149
|
+
| `AGENTGUARD_USERNAME` | `proton` | Timeplus username (Basic auth) |
|
|
150
|
+
| `AGENTGUARD_PASSWORD` | _(empty)_ | Timeplus password (Basic auth) |
|
|
151
|
+
|
|
152
|
+
These are the settings the other hook-event plugins use, with the same
|
|
153
|
+
meaning. They have no flags.
|
|
154
|
+
|
|
155
|
+
Each of the 14 hooks becomes one row:
|
|
156
|
+
|
|
157
|
+
| Column | Value |
|
|
158
|
+
|---|---|
|
|
159
|
+
| `hook_name` | The hook in snake case: `session_start`, `session_end`, `user_prompt_submit`, `pre_tool_use`, `post_tool_use`, `pre_model_request`, `post_model_request`, `stop`, `interrupt`, `subagent_start`, `permission_request`, `pre_compact`, `post_compact`. `SubagentStop` is written as `subagent_ended`, the name the stream already uses for it. |
|
|
160
|
+
| `agent_type` | `codex` |
|
|
161
|
+
| `session_id` | The Codex session id |
|
|
162
|
+
| `run_id` | The turn id, or the session id on hooks without a turn (`SessionStart`, `SessionEnd`) |
|
|
163
|
+
| `agent_id`, `deployment_id`, `deployment_name` | From the settings above |
|
|
164
|
+
| `tool_name` | The tool, on tool and permission hooks |
|
|
165
|
+
| `model`, `provider` | The model slug; the provider from the session transcript, or `openai` |
|
|
166
|
+
| `hook_decision` | `observe` |
|
|
167
|
+
| `event_data` | The Codex hook payload as received (`prompt`, `last_assistant_message`, `tool_input`, `tool_response`, `tool_use_id`, `token_usage`, `cwd`, `source`, `trigger`, and the subagent's `agent_id` and `agent_type`), plus the keys below |
|
|
168
|
+
|
|
169
|
+
These keys are added to `event_data`:
|
|
170
|
+
|
|
171
|
+
- `response` and `request` are on `post_model_request`, when the request is
|
|
172
|
+
found in the session transcript. See
|
|
173
|
+
[The response of a model request](#the-response-of-a-model-request).
|
|
174
|
+
|
|
175
|
+
- `agentguard.export` is always `hooks`. On the `otel` path the app copies
|
|
176
|
+
some spans into `agentguard_hook_events` with the same `agent_type`; this
|
|
177
|
+
key is how a view tells a row written by the plugin from a copied one.
|
|
178
|
+
- `agentguard.duration_ms` is on closing events that had an opening event:
|
|
179
|
+
`post_tool_use`, `post_model_request`, `post_compact`, `subagent_ended` and
|
|
180
|
+
`session_end`. See [Pairing and timing](#pairing-and-timing).
|
|
181
|
+
|
|
182
|
+
The rows of one hook process go out in a single request with a 2 second
|
|
183
|
+
timeout, so `SessionEnd` and `Interrupt` finish inside Codex's 3 second limit.
|
|
184
|
+
|
|
185
|
+
Check what arrived with:
|
|
186
|
+
|
|
187
|
+
```sql
|
|
188
|
+
SELECT event_time, hook_name, session_id, run_id, tool_name, model
|
|
189
|
+
FROM table(ag.agentguard_hook_events)
|
|
190
|
+
WHERE agent_type = 'codex' AND json_value(event_data, '$."agentguard.export"') = 'hooks'
|
|
191
|
+
ORDER BY event_time DESC
|
|
192
|
+
LIMIT 20;
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
The app does not turn these rows into `agentguard_cim_event` and
|
|
196
|
+
`agentguard_cim_metrics` rows yet. That is
|
|
197
|
+
[timeplus-io/apps#71](https://github.com/timeplus-io/apps/issues/71); until it
|
|
198
|
+
ships, the `hooks` export fills `agentguard_hook_events` only.
|
|
199
|
+
|
|
108
200
|
## What gets captured
|
|
109
201
|
|
|
110
202
|
The plugin handles all 14 events in the
|
|
111
|
-
[Codex hooks reference](https://developers.openai.com/codex/hooks).
|
|
112
|
-
|
|
113
|
-
|
|
203
|
+
[Codex hooks reference](https://developers.openai.com/codex/hooks). On the
|
|
204
|
+
`otel` export, described in the rest of this section, each event becomes one
|
|
205
|
+
span, exported with the OTel service name `codex`, which the AgentGuard app
|
|
206
|
+
reports as the agent type. The `hooks` export handles the same 14 events; its
|
|
207
|
+
row format is under [Hook-event export](#hook-event-export).
|
|
114
208
|
|
|
115
209
|
| Codex hook | Emitted span | What it carries |
|
|
116
210
|
|---|---|---|
|
|
@@ -164,6 +258,8 @@ Per span:
|
|
|
164
258
|
| `codex.request.id`, `codex.request.attempt`, `codex.request.kind`, `codex.thread_id` | model request spans | `request_id`, `attempt_number`, `kind`, `thread_id` |
|
|
165
259
|
| `codex.request.status`, `codex.request.error` | `post_model_request` | `status` (`completed`, `failed`, `cancelled`), `error` |
|
|
166
260
|
| `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` |
|
|
261
|
+
| `agentguard.response.text`, `.text_chars`, `.tool_calls`, `.phase` | `post_model_request`, when the request is found in the session transcript | the response in the transcript; `tool_calls` is a JSON list of `{id, name}` |
|
|
262
|
+
| `agentguard.request.effort`, `agentguard.request.summary` | `post_model_request`, same condition | the turn's `turn_context` in the transcript |
|
|
167
263
|
| `output.value` | `stop`, `subagent_stop` | `last_assistant_message` |
|
|
168
264
|
| `codex.stop_hook_active` | `stop`, `subagent_stop` | `stop_hook_active` |
|
|
169
265
|
| `codex.agent_transcript_path` | `subagent_stop` | `agent_transcript_path` |
|
|
@@ -193,6 +289,34 @@ These are three separate records, so counting one does not count another:
|
|
|
193
289
|
Subagent hooks carry the parent's `session.id`; `agentguard.subagent.id`
|
|
194
290
|
identifies the child.
|
|
195
291
|
|
|
292
|
+
### The response of a model request
|
|
293
|
+
|
|
294
|
+
The `PostModelRequest` payload has the token usage of a request and none of
|
|
295
|
+
its content. The plugin reads the content from the session transcript
|
|
296
|
+
(`transcript_path`) and adds it to the `post_model_request` row or span:
|
|
297
|
+
|
|
298
|
+
| Hook-event row (`event_data`) | Span attribute | Content |
|
|
299
|
+
|---|---|---|
|
|
300
|
+
| `response.text` | `agentguard.response.text` | The assistant text of the response, cut to `AGENTGUARD_LLM_CONTENT_CHARS`. Left out when `AGENTGUARD_LLM_CONTENT=false`. |
|
|
301
|
+
| `response.text_chars` | `agentguard.response.text_chars` | Length of the text before the cut |
|
|
302
|
+
| `response.tool_calls` | `agentguard.response.tool_calls` | The tool calls the response asked for, as `{id, name}`. The id is the transcript's `call_id`. Inputs and results are on the tool events. |
|
|
303
|
+
| `response.phase` | `agentguard.response.phase` | `commentary` or `final_answer`, when Codex records it |
|
|
304
|
+
| `request.effort`, `request.summary` | `agentguard.request.effort`, `agentguard.request.summary` | The reasoning settings of the turn |
|
|
305
|
+
|
|
306
|
+
Notes:
|
|
307
|
+
|
|
308
|
+
- The request is found by its turn and its token counts, because the hook and
|
|
309
|
+
the transcript share no id that is known to match. When it is not found
|
|
310
|
+
(a failed or cancelled attempt, a transcript that cannot be read, a format
|
|
311
|
+
this version does not know), the row or span is sent without these fields.
|
|
312
|
+
- The transcript may not hold the response yet when the hook runs. The plugin
|
|
313
|
+
waits up to 0.5 seconds for it; Codex waits for the hook during that time.
|
|
314
|
+
- Reasoning content is not sent: the transcript holds it encrypted. Its size
|
|
315
|
+
is `reasoning_output_tokens` in the token usage.
|
|
316
|
+
- The span attributes are AgentGuard's own names, not OpenInference's
|
|
317
|
+
`llm.output_messages`, so the app does not show a line of commentary before
|
|
318
|
+
a tool call as the assistant's reply.
|
|
319
|
+
|
|
196
320
|
### What the AgentGuard app shows
|
|
197
321
|
|
|
198
322
|
With the app's current OpenInference views (app 0.4.0), a Codex session gives:
|
|
@@ -236,7 +360,7 @@ status and token usage.
|
|
|
236
360
|
## Development
|
|
237
361
|
|
|
238
362
|
```bash
|
|
239
|
-
make test # unit tests; the end-to-end
|
|
363
|
+
make test # unit tests; the end-to-end OTel tests are skipped unless the SDK is installed
|
|
240
364
|
make build # sdist + wheel into dist/ (needs hatch)
|
|
241
365
|
```
|
|
242
366
|
|
|
@@ -1,9 +1,11 @@
|
|
|
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
|
-
|
|
6
|
-
|
|
4
|
+
stdin and hands it to :func:`agentguard_codex.events.dispatch`, which exports
|
|
5
|
+
it on one of two paths: OpenTelemetry spans through the AgentGuard Python SDK
|
|
6
|
+
(``otel``, the default), or rows in the ``agentguard_hook_events`` stream
|
|
7
|
+
(``hooks``). Exactly one path is active. See ``hooks.json`` for a sample Codex
|
|
8
|
+
hook configuration.
|
|
7
9
|
"""
|
|
8
10
|
|
|
9
11
|
from __future__ import annotations
|
|
@@ -21,6 +23,11 @@ DEFAULT_DEPLOYMENT_ID = "local"
|
|
|
21
23
|
DEFAULT_DEPLOYMENT_NAME = "Local Dev"
|
|
22
24
|
DEFAULT_ENDPOINT = "http://localhost:4318"
|
|
23
25
|
|
|
26
|
+
EXPORT_OTEL = "otel"
|
|
27
|
+
EXPORT_HOOKS = "hooks"
|
|
28
|
+
EXPORTS = (EXPORT_OTEL, EXPORT_HOOKS)
|
|
29
|
+
DEFAULT_EXPORT = EXPORT_OTEL
|
|
30
|
+
|
|
24
31
|
|
|
25
32
|
def build_parser() -> argparse.ArgumentParser:
|
|
26
33
|
parser = argparse.ArgumentParser(
|
|
@@ -28,10 +35,19 @@ def build_parser() -> argparse.ArgumentParser:
|
|
|
28
35
|
description="Forward OpenAI Codex hook events to AgentGuard",
|
|
29
36
|
formatter_class=argparse.RawDescriptionHelpFormatter,
|
|
30
37
|
epilog=(
|
|
31
|
-
"Reads one Codex hook event as JSON on stdin and exports\n"
|
|
32
|
-
"OpenTelemetry spans to the
|
|
38
|
+
"Reads one Codex hook event as JSON on stdin and exports it to the\n"
|
|
39
|
+
"AgentGuard Timeplus app: as OpenTelemetry spans to the app's OTLP\n"
|
|
40
|
+
"input (--export otel), or as rows in agentguard_hook_events\n"
|
|
41
|
+
"(--export hooks, configured with the AGENTGUARD_TIMEPLUS_URL,\n"
|
|
42
|
+
"AGENTGUARD_INGEST_API, AGENTGUARD_USERNAME and AGENTGUARD_PASSWORD\n"
|
|
43
|
+
"environment variables)."
|
|
33
44
|
),
|
|
34
45
|
)
|
|
46
|
+
parser.add_argument(
|
|
47
|
+
"--export",
|
|
48
|
+
default=None,
|
|
49
|
+
help="Export path, 'otel' or 'hooks' (default: $AGENTGUARD_CODEX_EXPORT or 'otel')",
|
|
50
|
+
)
|
|
35
51
|
parser.add_argument(
|
|
36
52
|
"--agent-id",
|
|
37
53
|
default=None,
|
|
@@ -50,7 +66,7 @@ def build_parser() -> argparse.ArgumentParser:
|
|
|
50
66
|
parser.add_argument(
|
|
51
67
|
"--endpoint",
|
|
52
68
|
default=None,
|
|
53
|
-
help="OTLP/HTTP endpoint (default: $OTEL_EXPORTER_OTLP_ENDPOINT or http://localhost:4318)",
|
|
69
|
+
help="OTLP/HTTP endpoint, used with --export otel (default: $OTEL_EXPORTER_OTLP_ENDPOINT or http://localhost:4318)",
|
|
54
70
|
)
|
|
55
71
|
return parser
|
|
56
72
|
|
|
@@ -91,6 +107,24 @@ def resolve_endpoint(endpoint: str | None) -> str:
|
|
|
91
107
|
)
|
|
92
108
|
|
|
93
109
|
|
|
110
|
+
def resolve_export(export: str | None) -> str:
|
|
111
|
+
"""Resolve the export path: flag, then env, then ``otel``.
|
|
112
|
+
|
|
113
|
+
An unknown value is reported on stderr and replaced by the default, so a
|
|
114
|
+
typo never stops telemetry and never selects both paths.
|
|
115
|
+
"""
|
|
116
|
+
value = (export or os.environ.get("AGENTGUARD_CODEX_EXPORT") or "").strip().lower()
|
|
117
|
+
if not value:
|
|
118
|
+
return DEFAULT_EXPORT
|
|
119
|
+
if value not in EXPORTS:
|
|
120
|
+
print(
|
|
121
|
+
f"agentguard-codex: unknown export {value!r}, using {DEFAULT_EXPORT!r}",
|
|
122
|
+
file=sys.stderr,
|
|
123
|
+
)
|
|
124
|
+
return DEFAULT_EXPORT
|
|
125
|
+
return value
|
|
126
|
+
|
|
127
|
+
|
|
94
128
|
def start_store_folder(agent_id: str) -> str:
|
|
95
129
|
"""Where the start times of open sessions, tools and requests live."""
|
|
96
130
|
return os.path.join(os.path.expanduser("~"), ".agentguard", agent_id, "codex-starts")
|
|
@@ -100,23 +134,41 @@ def main(argv: list[str] | None = None) -> int:
|
|
|
100
134
|
try:
|
|
101
135
|
args = build_parser().parse_args(argv)
|
|
102
136
|
|
|
137
|
+
agent_id = resolve_agent_id(args.agent_id)
|
|
138
|
+
deployment_id = resolve_deployment_id(args.deployment_id)
|
|
139
|
+
deployment_name = resolve_deployment_name(args.deployment_name)
|
|
140
|
+
|
|
103
141
|
# Imported lazily so ``import agentguard_codex.cli`` (and ``--help``)
|
|
104
142
|
# works without the AgentGuard SDK, and a missing SDK is handled by
|
|
105
|
-
# the same fail-safe path as any other plugin error.
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
143
|
+
# the same fail-safe path as any other plugin error. The hooks path
|
|
144
|
+
# never imports the SDK.
|
|
145
|
+
from agentguard_codex.sink import StartStore
|
|
146
|
+
|
|
147
|
+
starts = StartStore(start_store_folder(agent_id))
|
|
148
|
+
if resolve_export(args.export) == EXPORT_HOOKS:
|
|
149
|
+
from agentguard_codex.hookevents import HookEventSink, IngestConfig
|
|
150
|
+
|
|
151
|
+
sink = HookEventSink(
|
|
152
|
+
IngestConfig.from_env(),
|
|
153
|
+
starts,
|
|
154
|
+
agent_id=agent_id,
|
|
155
|
+
deployment_id=deployment_id,
|
|
156
|
+
deployment_name=deployment_name,
|
|
157
|
+
)
|
|
158
|
+
else:
|
|
159
|
+
from agentguard import AgentGuard
|
|
160
|
+
|
|
161
|
+
from agentguard_codex.sink import SdkSink
|
|
162
|
+
|
|
163
|
+
client = AgentGuard(
|
|
164
|
+
# The app reports the OTel service name as the agent type.
|
|
165
|
+
service_name=AGENT_TYPE,
|
|
166
|
+
agent_id=agent_id,
|
|
167
|
+
deployment_id=deployment_id,
|
|
168
|
+
deployment_name=deployment_name,
|
|
169
|
+
otlp_endpoint=resolve_endpoint(args.endpoint),
|
|
170
|
+
)
|
|
171
|
+
sink = SdkSink(client, starts)
|
|
120
172
|
|
|
121
173
|
try:
|
|
122
174
|
dispatch(json.loads(sys.stdin.read()), sink)
|