{"record":{"id":"ab36915dde830a61","repo":"Hmbown/CodeWhale","slug":"sub-agent-transcript-history-shrank-from-to","errorCode":null,"errorMessage":"sub-agent transcript history shrank from {} to {} messages","messagePattern":"sub-agent transcript history shrank from (.+?) to (.+?) messages","errorType":"validation","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/tui/src/tools/subagent/mod.rs","lineNumber":8489,"sourceCode":"        let relative_path = subagent_transcript_artifact_relative_path(agent_id);\n        let path = checked_subagent_transcript_artifact_path(&state_root, agent_id)?;\n        let header = json!({\n            \"kind\": \"subagent_transcript_header\",\n            \"schema_version\": SUBAGENT_TRANSCRIPT_ARTIFACT_SCHEMA_VERSION,\n            \"agent_id\": agent_id,\n        });\n        create_private_subagent_transcript(&state_root, &path, &json_line(&header)?)?;\n        Ok(Self {\n            state_root,\n            path,\n            relative_path,\n            persisted_messages: 0,\n        })\n    }\n\n    fn sync_messages(&mut self, messages: &[Message], durable: bool) -> Result<()> {\n        if messages.len() < self.persisted_messages {\n            return Err(anyhow!(\n                \"sub-agent transcript history shrank from {} to {} messages\",\n                self.persisted_messages,\n                messages.len()\n            ));\n        }\n\n        let mut encoded = Vec::new();\n        for (index, message) in messages.iter().enumerate().skip(self.persisted_messages) {\n            encoded.extend(json_line(&json!({\n                \"kind\": \"message\",\n                \"index\": index,\n                \"message\": message,\n            }))?);\n        }\n\n        if !encoded.is_empty() || durable {\n            append_private_subagent_transcript(&self.state_root, &self.path, &encoded, durable)?;\n        }","sourceCodeStart":8471,"sourceCodeEnd":8507,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/73e0f67d83c59909b571efdfc88c4bc28c309cb1/crates/tui/src/tools/subagent/mod.rs#L8471-L8507","documentation":"`sync_messages` persists the sub-agent transcript and tracks how many messages are already durable. If a caller passes a message slice shorter than what was persisted, the transcript would silently lose history, so the sync is rejected with this error.","triggerScenarios":"Passing a truncated/rebuilt `Vec<Message>` (e.g. after context compaction, a retry that rebuilt messages from scratch, or a rollback) into `sync_messages` when the durable transcript already contains more messages.","commonSituations":"Context-window trimming code feeding a compacted history back into the sub-agent persistence path; replaying an older snapshot over a newer durable transcript; a bug in message-buffer slicing.","solutions":["Compact or truncate only a copy; always pass the full in-memory history to sync_messages","Persist truncations as explicit new records (e.g. a reset marker) instead of shrinking the slice","Reset the persisted_messages counter / durable state when an intentional transcript reset is required, then resync"],"exampleFix":"// before\nsync_messages(&messages[..last_n], durable) // Err: history shrank\n// after\nlet full = messages.to_vec(); // keep full history; append a compaction marker instead of dropping messages\nsync_messages(&full, durable)","handlingStrategy":"try-catch","validationCode":"fn can_sync(persisted: usize, messages: &[Message]) -> bool {\n    messages.len() >= persisted\n}","typeGuard":"fn transcript_grew(messages: &[Message], persisted: usize) -> bool {\n    messages.len() >= persisted\n}","tryCatchPattern":"match sync_messages(&messages, durable) {\n    Err(e) if e.to_string().contains(\"history shrank\") => {\n        // an intentional reset happened: rebuild durable state, then resync\n        reset_durable_transcript()?;\n        sync_messages(&messages, durable)\n    }\n    r => r,\n}","preventionTips":["Never pass a compacted/truncated slice to the persistence sync; pass full history","If compaction must reduce context, do it on a working copy and keep the durable transcript append-only","Reset persisted state explicitly when replaying from an older snapshot"],"tags":["subagent","rust","persistence"],"backgroundTag":"internal-invariant-violation","analyzedSha":"73e0f67d83c59909b571efdfc88c4bc28c309cb1","analyzedAt":"2026-09-22T01:30:00.501Z","contentChangedAt":"2026-09-22T01:30:00.501Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}