Skip to content

[codex-analytics] centralize thread analytics state#20300

Merged
rhan-oai merged 1 commit intomainfrom
rhan/thread-analytics-state
May 1, 2026
Merged

[codex-analytics] centralize thread analytics state#20300
rhan-oai merged 1 commit intomainfrom
rhan/thread-analytics-state

Conversation

@rhan-oai
Copy link
Copy Markdown
Collaborator

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

Why

Several analytics event families need the same per-thread attribution state: the app-server client/runtime associated with a thread and, for lifecycle-oriented events, the thread metadata captured during initialization. Keeping connection ids and lifecycle metadata in separate maps made each consumer rebuild the same thread context and made subagent attribution harder to resolve consistently.

What changed

  • Replaces the separate thread connection and metadata maps with one reducer-owned threads map.
  • Routes guardian, compaction, turn-steer, and turn analytics through shared thread-state lookups while preserving turn-origin attribution for turn events and request-origin attribution for steer events.
  • Lets newly observed spawned subagent threads inherit their parent thread connection so later thread-scoped analytics can resolve through the same state model.
  • Adds regression coverage for standalone SubAgentThreadStarted publication plus the SubAgentSource::ThreadSpawn parent fallback through a thread-scoped consumer that depends on inherited connection state.

Verification

  • cargo test -p codex-analytics

Stack created with Sapling. Best reviewed with ReviewStack.

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: ffbe9fe5c4

ℹ️ 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 Outdated
subagent_source: Some(subagent_source_name(&input.subagent_source)),
parent_thread_id,
});
thread_state.connection_id = parent_connection_id;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P1 Badge Preserve thread connection when recording subagent start

Avoid unconditionally overwriting thread_state.connection_id here. If SubAgentThreadStarted is ingested after emit_thread_initialized for the same thread (a common ordering), this replaces the real thread connection with the parent (or None). Later context resolution for guardian/compaction uses this field, so events can be misattributed to the wrong client/runtime or dropped entirely.

Useful? React with 👍 / 👎.

@rhan-oai rhan-oai force-pushed the rhan/thread-analytics-state branch from ffbe9fe to 4dae1e1 Compare April 30, 2026 16:40
@rhan-oai rhan-oai requested a review from a team as a code owner April 30, 2026 16:40
@rhan-oai rhan-oai force-pushed the rhan/thread-analytics-state branch 4 times, most recently from c7b6fb3 to a89ccf9 Compare April 30, 2026 17:38
@rhan-oai
Copy link
Copy Markdown
Collaborator Author

@codex

@chatgpt-codex-connector
Copy link
Copy Markdown
Contributor

Codex Review: Didn't find any major issues. Can't wait for the next one!

ℹ️ 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".

Copy link
Copy Markdown
Collaborator

@owenlin0 owenlin0 left a comment

Choose a reason for hiding this comment

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

i find that there are a lot of concepts to understand, can we simplify?

seems like the branch added a fairly abstract mini-framework:

  • AnalyticsDropSite: where the event was dropped
  • MissingAnalyticsContext: why it was dropped
  • AnalyticsConnectionSource: how to find the connection
  • ThreadMetadataRequirement: whether metadata is needed
  • ResolvedAnalyticsContext: the resolved output

are all those necessary?

wonder if we can have a resolve_analytics_context that looks something like:

let Some((connection_state, thread_metadata)) =
    self.thread_context_or_warn("compaction", &input.thread_id, Some(&input.turn_id))
else {
    return;
};

or something like that

@rhan-oai rhan-oai force-pushed the rhan/thread-analytics-state branch 7 times, most recently from 19a23ea to b03dd51 Compare April 30, 2026 20:34
@rhan-oai rhan-oai force-pushed the rhan/thread-analytics-state branch from b03dd51 to 3fc7386 Compare April 30, 2026 20:47
@rhan-oai rhan-oai force-pushed the rhan/thread-analytics-state branch from 3fc7386 to d877e6a Compare April 30, 2026 20:58
@rhan-oai rhan-oai merged commit 6b1b227 into main May 1, 2026
25 checks passed
@rhan-oai rhan-oai deleted the rhan/thread-analytics-state branch May 1, 2026 01:58
@github-actions github-actions Bot locked and limited conversation to collaborators May 1, 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.

2 participants