{"record":{"id":"dfd97c0302e06c18","repo":"xai-org/grok-build","slug":"a-successful-full-replace-sample-stashes-its-compa","errorCode":null,"errorMessage":"a successful full-replace sample stashes its CompactOutput","messagePattern":"a successful full-replace sample stashes its CompactOutput","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/codegen/xai-grok-shell/src/session/compaction.rs","lineNumber":1315,"sourceCode":"                user_context.as_deref(),\n                use_short_prompt,\n                &sampling_config.model,\n                trigger,\n                compact_summary\n                    .as_deref()\n                    .or(telemetry.last_rejected_summary.as_deref()),\n                last_error.as_ref(),\n                telemetry.attempts,\n                telemetry.attempt_details,\n                started_at,\n            );\n        }\n        let compact_output = match compact_summary {\n            Some(_) => match two_pass_output {\n                Some(tp) => tp,\n                None => sampler\n                    .take_last_success()\n                    .expect(\"a successful full-replace sample stashes its CompactOutput\"),\n            },\n            None => {\n                let span = tracing::Span::current();\n                span.record(\"compaction_attempts\", telemetry.attempts as i64);\n                span.record(\n                    \"compaction_degenerate_rejections\",\n                    telemetry.degenerate_rejections as i64,\n                );\n                span.record(\n                    \"compaction_input_overflow_rejections\",\n                    input_overflow_rejections as i64,\n                );\n                span.record(\n                    \"compaction_deterministic_rejections\",\n                    telemetry.deterministic_rejections as i64,\n                );\n                span.record(\n                    \"compaction_transient_rejections\",","sourceCodeStart":1297,"sourceCodeEnd":1333,"githubUrl":"https://github.com/xai-org/grok-build/blob/bc7f02eddd3d84085849dc19ed216f11c23b0571/crates/codegen/xai-grok-shell/src/session/compaction.rs#L1297-L1333","documentation":"During two-pass compaction, when a compact summary was produced the code needs the sampler's stashed `CompactOutput` from the last successful full-replace sample and unwraps with expect. The invariant: if `compact_summary` is Some, a successful full-replace sample must have been recorded; the panic signals that bookkeeping broke.","triggerScenarios":"`compact_summary` is Some but `two_pass_output` is None and `sampler.take_last_success()` returns None — i.e. the summary was derived without a corresponding successful full-replace sample being stashed in the sampler.","commonSituations":"Sampler rejection bookkeeping drift after a refactor of the sample/summary flow; compaction runs where all full-replace samples degenerated yet a summary was still produced; interplay between retry attempts and sampler state on repeated compaction calls.","solutions":["Trace why the sampler recorded no success: log each full-replace sample attempt and its accept/reject outcome.","Ensure compact_summary is only set when a successful full-replace sample was stashed (make the coupling explicit in code).","Check degenerate-rejection logic — a rejected sample shouldn't coexist with a set compact_summary.","Propagate a CompactionError instead of panicking so the session can fall back to no-compaction."],"exampleFix":"// before\n.expect(\"a successful full-replace sample stashes its CompactOutput\")\n// after\n.ok_or_else(|| CompactionError::SamplerStateInconsistent)?","handlingStrategy":"type-guard","validationCode":"// only consume the sampler when a success was actually recorded\nif compact_summary.is_some() && two_pass_output.is_none() && !sampler.has_last_success() {\n    return Err(CompactionError::SamplerStateInconsistent);\n}","typeGuard":"fn sampler_ready(sampler: &Sampler) -> bool {\n    sampler.has_last_success()\n}","tryCatchPattern":"let out = sampler.take_last_success().ok_or_else(|| {\n    tracing::error!(\"summary present without a successful full-replace sample\");\n    CompactionError::SamplerStateInconsistent\n})?;","preventionTips":["Set compact_summary only in the same code path that stashes the sample success","Log every sample accept/reject to keep sampler state observable","Add a test where all full-replace samples degenerate and assert no summary is produced"],"tags":["rust","compaction","invariant","panic","llm-session"],"backgroundTag":"compaction-state-inconsistent","analyzedSha":"bc7f02eddd3d84085849dc19ed216f11c23b0571","analyzedAt":"2026-08-31T04:59:42.031Z","schemaVersion":2},"datasetVersion":"2026-08-31T09:17:48.483Z"}