[codex-analytics] rework thread_source for thread analytics (#20949)

## Summary
- make `thread_source` an explicit optional thread-level field on
`thread/start`, `thread/fork`, and returned thread payloads
- persist `thread_source` in rollout/session metadata so resumed live
threads retain the original value
- replace the old best-effort `session_source` -> `thread_source`
mapping with an explicit caller-supplied analytics classification

## Why
Before this change, analytics `thread_source` was populated by a
best-effort mapping from `session_source`. `session_source` describes
the runtime/client surface, not the actual thread-level origin, so that
projection was not accurate enough to distinguish cases such as `user`,
`subagent`, `memory_consolidation`, and future thread origins reliably.

Making `thread_source` explicit keeps one thread-level analytics field
while letting callers provide the real classification directly instead
of recovering it indirectly from `session_source`.

## Impact
For new analytics events, `thread_source` now reflects the explicit
thread-level classification supplied by the caller rather than an
inferred value derived from `session_source`. Existing protocol fields
remain optional; callers that omit `threadSource` now produce `null`
instead of a best-effort inferred value.

## Validation
- `just write-app-server-schema`
- `cargo test -p codex-analytics -p codex-core -p
codex-app-server-protocol --no-run`
- `cargo test -p codex-app-server-protocol
generated_ts_optional_nullable_fields_only_in_params`
- `cargo test -p codex-analytics
thread_initialized_event_serializes_expected_shape`
- `cargo test -p codex-core
resume_stopped_thread_from_rollout_preserves_thread_source`
This commit is contained in:
rhan-oai
2026-05-06 02:12:31 +00:00
committed by GitHub
parent 94db03d5af
commit b3d4f1a9f0
98 changed files with 896 additions and 90 deletions
@@ -16,6 +16,7 @@ use codex_protocol::protocol::SandboxPolicy;
use codex_protocol::protocol::SessionSource;
use codex_protocol::protocol::SubAgentSource;
use codex_protocol::protocol::ThreadMemoryMode;
use codex_protocol::protocol::ThreadSource;
use super::proto;
use crate::GitInfoPatch;
@@ -296,6 +297,11 @@ pub(super) fn stored_thread_from_proto(
cwd: PathBuf::from(thread.cwd),
cli_version: thread.cli_version,
source,
thread_source: thread
.thread_source
.map(|thread_source| thread_source.parse::<ThreadSource>())
.transpose()
.map_err(|error| ThreadStoreError::Internal { message: error })?,
agent_nickname: thread.agent_nickname,
agent_role: thread.agent_role,
agent_path: thread.agent_path,
@@ -340,6 +346,7 @@ pub(super) fn stored_thread_to_proto(thread: StoredThread) -> proto::StoredThrea
cwd: thread.cwd.to_string_lossy().into_owned(),
cli_version: thread.cli_version,
source: Some(proto_session_source(&thread.source)),
thread_source: thread.thread_source.map(|source| source.to_string()),
git_info: thread.git_info.map(git_info_to_proto),
agent_nickname: thread.agent_nickname,
agent_role: thread.agent_role,
@@ -140,6 +140,7 @@ mod tests {
kind: proto::SessionSourceKind::Cli.into(),
..Default::default()
}),
thread_source: Some("user".to_string()),
git_info: Some(proto::GitInfo {
sha: Some("abc123".to_string()),
branch: Some("main".to_string()),
@@ -250,6 +251,7 @@ mod tests {
sub_agent_role: Some("explorer".to_string()),
..Default::default()
}),
thread_source: Some("subagent".to_string()),
git_info: Some(proto::GitInfo {
sha: Some("abc123".to_string()),
branch: Some("main".to_string()),
+1
View File
@@ -358,6 +358,7 @@ mod tests {
thread_id: ThreadId::new(),
forked_from_id: None,
source: SessionSource::Exec,
thread_source: None,
base_instructions: BaseInstructions::default(),
dynamic_tools: Vec::new(),
metadata: metadata.clone(),
@@ -132,6 +132,7 @@ message StoredThread {
optional string sandbox_policy_json = 21;
optional string token_usage_json = 22;
optional StoredThreadHistory history = 23;
optional string thread_source = 24;
}
message SessionSource {
@@ -168,6 +168,8 @@ pub struct StoredThread {
pub token_usage_json: ::core::option::Option<::prost::alloc::string::String>,
#[prost(message, optional, tag = "23")]
pub history: ::core::option::Option<StoredThreadHistory>,
#[prost(string, optional, tag = "24")]
pub thread_source: ::core::option::Option<::prost::alloc::string::String>,
}
#[derive(Clone, PartialEq, Eq, Hash, ::prost::Message)]
pub struct SessionSource {