{"record":{"id":"8ca81df3455810ef","repo":"xai-org/grok-build","slug":"set-on-the-use-leader-path","errorCode":null,"errorMessage":"set on the use_leader path","messagePattern":"set on the use_leader path","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/codegen/xai-grok-pager/src/app/mod.rs","lineNumber":963,"sourceCode":"        &cancel,\n        connect_ui_timeout,\n        primary_target,\n        startup_failure::ConnectAttempt::First,\n        &timer,\n        async {\n            if use_leader {\n                crate::acp::connect_via_leader(&cancel, connect_flags, &raw_config).await\n            } else {\n                crate::acp::connect(&cancel, connect_flags).await\n            }\n        },\n    )\n    .await;\n    let (connect_result, embedded_fallback, timer, connect_target) = match connect_result {\n        Err(f) if use_leader && !cancel.is_cancelled() => {\n            tracing::warn!(error = %f.error, \"leader connect failed; falling back to embedded agent\");\n            timer.emit_telemetry(primary_target, f.outcome, f.timeout_secs, false);\n            let flags = fallback_flags.expect(\"set on the use_leader path\");\n            let timer = xai_grok_telemetry::startup::begin(crate::acp::Owner::Client);\n            let target = crate::acp::AgentKind::Embedded;\n            let fallback = bounded_connect(\n                &cancel,\n                connect_ui_timeout,\n                target,\n                startup_failure::ConnectAttempt::AfterFallback(startup_failure::EarlierAttempt {\n                    target: primary_target,\n                    wait: primary_started.elapsed(),\n                    outcome: f.outcome,\n                    longest_step: f.longest_step,\n                }),\n                &timer,\n                async { crate::acp::connect(&cancel, flags).await },\n            )\n            .await;\n            (fallback, true, timer, target)\n        }","sourceCodeStart":945,"sourceCodeEnd":981,"githubUrl":"https://github.com/xai-org/grok-build/blob/bc7f02eddd3d84085849dc19ed216f11c23b0571/crates/codegen/xai-grok-pager/src/app/mod.rs#L945-L981","documentation":"On the `use_leader` path, when the leader connect fails and the code falls back to the embedded agent, it unwraps `fallback_flags` with `.expect(\"set on the use_leader path\")`. The invariant is that the leader path sets `fallback_flags` before attempting the leader connect; panicking here means that invariant was broken.","triggerScenarios":"`use_leader` is true but the code path that populates `fallback_flags` (e.g. preparing embedded-agent connect flags) was skipped or reordered, so `Option::expect` fires on the fallback branch after a leader connect failure.","commonSituations":"Refactor that moved flag construction behind a condition not taken on the leader path; leader connect failing (which is itself common — bad leader address, agent down) and exposing the uninitialized flags; partial initialization due to an early return earlier in the function.","solutions":["Find where fallback_flags is assigned and ensure it's set unconditionally when use_leader is true, before the connect attempt.","Replace Option with an eagerly-built value so no unwrap is needed on the fallback path.","Add a debug_assert! right after the leader path to catch the unset invariant in tests.","Reproduce by forcing a leader connect failure (unreachable leader target) with use_leader enabled and verify fallback works."],"exampleFix":"// before\nlet flags = fallback_flags.expect(\"set on the use_leader path\");\n// after\nlet flags = fallback_flags.unwrap_or_else(FallbackFlags::default_for_embedded);","handlingStrategy":"validation","validationCode":"// assert the invariant early, before attempting leader connect\ndebug_assert!(\n    !use_leader || fallback_flags.is_some(),\n    \"fallback_flags must be set when use_leader\"\n);","typeGuard":null,"tryCatchPattern":"let flags = fallback_flags\n    .ok_or(FallbackError::FlagsNotInitialized)\n    .map_err(|e| { tracing::error!(%e, \"fallback flags missing\"); e })?;","preventionTips":["Build fallback_flags unconditionally before any connect attempt","Cover the leader-failure→embedded-fallback path with a test using an unreachable leader","Avoid Option for values that are logically always present on a path — use eager construction"],"tags":["rust","invariant","fallback","panic","leader-cluster"],"backgroundTag":"uninitialized-fallback-state","analyzedSha":"bc7f02eddd3d84085849dc19ed216f11c23b0571","analyzedAt":"2026-08-31T04:59:42.031Z","schemaVersion":2},"datasetVersion":"2026-08-31T09:17:48.483Z"}