Skip to content

[codex-analytics] centralize thread analytics state#20241

Merged
rhan-oai merged 0 commit intopr20239from
codex/thread-analytics-state
Apr 30, 2026
Merged

[codex-analytics] centralize thread analytics state#20241
rhan-oai merged 0 commit intopr20239from
codex/thread-analytics-state

Conversation

@rhan-oai
Copy link
Copy Markdown
Collaborator

@rhan-oai rhan-oai commented Apr 29, 2026

Why

Several analytics event families need the same reducer context: the app-server client/runtime associated with a thread and, for lifecycle-oriented events, the thread metadata captured during initialization. Keeping connection ids and thread metadata in separate maps makes each new event reconstruct the same join and duplicate the same missing-state handling.

What changed

  • Replaces the separate thread_connections and thread_metadata maps with one threads map that stores reducer-owned state for each thread.
  • Adds one generalized analytics context resolver that supports thread-derived events, events with known connection provenance, and events with optional known provenance.
  • Centralizes missing-context warnings so guardian, compaction, turn-steer, and turn events share the same lookup/drop path.
  • Copies parent connection metadata onto subagent thread state so descendant analytics can resolve through the same thread context model.

Verification

  • cargo test -p codex-analytics

Stack created with Sapling. Best reviewed with ReviewStack.

@rhan-oai rhan-oai force-pushed the codex/thread-analytics-state branch 2 times, most recently from 163e5e0 to ff2f306 Compare April 29, 2026 18:27
@rhan-oai rhan-oai force-pushed the pr20239 branch 3 times, most recently from 93b611d to a5f2318 Compare April 29, 2026 18:53
@rhan-oai rhan-oai force-pushed the codex/thread-analytics-state branch from ff2f306 to 54756f4 Compare April 29, 2026 18:58
@rhan-oai rhan-oai force-pushed the codex/thread-analytics-state branch from 54756f4 to 4340ce1 Compare April 29, 2026 19:05
rhan-oai added a commit that referenced this pull request Apr 29, 2026
## Why

Codex analytics needs a typed seam for app-server-originated
request/response traffic so future tool-approval analytics can consume
those facts without adding bespoke callsite tracking each time. Server
responses arrive as JSON-RPC `id + result` payloads, so analytics has to
reconstruct the matching typed response from the original typed request
while that request context still exists in app-server.

This also puts analytics on the app-server outbound path, which needs to
avoid keeping the runtime alive during shutdown. The final ownership fix
keeps the normal strong auth-manager retention in analytics and makes
the external-auth refresh bridge hold a weak back-reference to
`OutgoingMessageSender`, breaking the runtime cycle at the bridge
boundary instead of exposing retention policy through the analytics
client API.

## What changed

- Adds typed `ServerRequest` and `ServerResponse` analytics facts, plus
`AnalyticsEventsClient::track_server_request` and
`track_server_response`.
- Renames the existing client-side facts to `ClientRequest` and
`ClientResponse` so reducers can distinguish client-to-server traffic
from server-to-client traffic.
- Adds `ServerRequest::response_from_result`, allowing a stored typed
request to decode the matching typed server response from a raw JSON-RPC
result payload.
- Threads `AnalyticsEventsClient` through `OutgoingMessageSender` and
records targeted server requests, replayed targeted requests, and
matching targeted responses with the responding connection id needed for
correlation.
- Intentionally leaves broadcast server requests/responses out of
analytics for now because the current model is per connection, while
broadcasts fan one logical request out across multiple connections.
- Breaks the app-server shutdown cycle by storing
`Weak<OutgoingMessageSender>` in `ExternalAuthRefreshBridge` and
upgrading it only when an external-auth refresh is actually requested.
- Keeps reducer ingestion of the new server-side facts as no-ops for
now; this PR is plumbing for later tool-approval analytics work.

## Verification

- `cargo test -p codex-analytics`
- `cargo test -p codex-app-server outgoing_message::tests::`
- Covers typed-response reconstruction plus the targeted, replayed,
broadcast-exclusion, and response-attribution analytics paths.

## Follow-up

This PR intentionally stops at ingestion plumbing, so `ServerRequest`
and `ServerResponse` facts are still reducer no-ops. Once a follow-up PR
adds real downstream analytics output for those facts:

- replace the temporary pre-reducer observation seam with reducer tests
for the emitted event shape;
- add end-to-end coverage in `app-server/tests/suite/v2/analytics.rs`
for the real app-server workflow and captured analytics payload;
- remove the temporary sender-level observer tests added here in favor
of the real-output coverage above.

---

[//]: # (BEGIN SAPLING FOOTER)
Stack created with [Sapling](https://sapling-scm.com). Best reviewed
with [ReviewStack](https://reviewstack.dev/openai/codex/pull/17088).
* #18748
* #18747
* #17090
* #17089
* #20241
* #20239
* __->__ #17088
@rhan-oai rhan-oai force-pushed the codex/thread-analytics-state branch from 4340ce1 to b39f92b Compare April 29, 2026 20:55
@rhan-oai rhan-oai force-pushed the codex/thread-analytics-state branch from b39f92b to 4340ce1 Compare April 29, 2026 21:14
@rhan-oai rhan-oai force-pushed the pr20239 branch 2 times, most recently from ebb1741 to bc7c0d5 Compare April 29, 2026 21:18
@rhan-oai rhan-oai force-pushed the codex/thread-analytics-state branch from 4340ce1 to f1478ed Compare April 29, 2026 21:18
@rhan-oai rhan-oai marked this pull request as ready for review April 29, 2026 21:42
Copy link
Copy Markdown
Contributor

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f1478ede76

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread codex-rs/analytics/src/reducer.rs
Comment thread codex-rs/analytics/src/reducer.rs
@rhan-oai rhan-oai force-pushed the codex/thread-analytics-state branch from f1478ed to 9412dae Compare April 29, 2026 22:37
@rhan-oai rhan-oai requested a review from a team as a code owner April 29, 2026 22:48
@rhan-oai rhan-oai force-pushed the codex/thread-analytics-state branch from 9412dae to 96f46a2 Compare April 29, 2026 22:54
@rhan-oai rhan-oai force-pushed the codex/thread-analytics-state branch 2 times, most recently from bf921a0 to c2e3246 Compare April 29, 2026 23:14
@rhan-oai rhan-oai force-pushed the pr20239 branch 2 times, most recently from d5bedde to 4dfe7b0 Compare April 30, 2026 00:30
@rhan-oai rhan-oai force-pushed the codex/thread-analytics-state branch from c2e3246 to ffbe9fe Compare April 30, 2026 01:24
@rhan-oai rhan-oai merged commit ffbe9fe into pr20239 Apr 30, 2026
1 check passed
@rhan-oai rhan-oai deleted the codex/thread-analytics-state branch April 30, 2026 01:24
@github-actions github-actions Bot locked and limited conversation to collaborators Apr 30, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant