Hmbown/CodeWhale · error · anyhow::Error

sub-agent transcript history shrank from

Error message

sub-agent transcript history shrank from {} to {} messages

What it means

`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.

Solutions

  1. Compact or truncate only a copy; always pass the full in-memory history to sync_messages
  2. Persist truncations as explicit new records (e.g. a reset marker) instead of shrinking the slice
  3. Reset the persisted_messages counter / durable state when an intentional transcript reset is required, then resync

Example fix

// before
sync_messages(&messages[..last_n], durable) // Err: history shrank
// after
let full = messages.to_vec(); // keep full history; append a compaction marker instead of dropping messages
sync_messages(&full, durable)
Defensive patterns

Strategy: try-catch

Validate before calling

fn can_sync(persisted: usize, messages: &[Message]) -> bool {
    messages.len() >= persisted
}

Type guard

fn transcript_grew(messages: &[Message], persisted: usize) -> bool {
    messages.len() >= persisted
}

Try / catch

match sync_messages(&messages, durable) {
    Err(e) if e.to_string().contains("history shrank") => {
        // an intentional reset happened: rebuild durable state, then resync
        reset_durable_transcript()?;
        sync_messages(&messages, durable)
    }
    r => r,
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


AI-assisted analysis of Hmbown/CodeWhale@73e0f67d83 (2026-09-22). Data as JSON: /api/errors/ab36915dde830a61. Report an issue: GitHub.

Appendix: source

Thrown at crates/tui/src/tools/subagent/mod.rs:8489

        let relative_path = subagent_transcript_artifact_relative_path(agent_id);
        let path = checked_subagent_transcript_artifact_path(&state_root, agent_id)?;
        let header = json!({
            "kind": "subagent_transcript_header",
            "schema_version": SUBAGENT_TRANSCRIPT_ARTIFACT_SCHEMA_VERSION,
            "agent_id": agent_id,
        });
        create_private_subagent_transcript(&state_root, &path, &json_line(&header)?)?;
        Ok(Self {
            state_root,
            path,
            relative_path,
            persisted_messages: 0,
        })
    }

    fn sync_messages(&mut self, messages: &[Message], durable: bool) -> Result<()> {
        if messages.len() < self.persisted_messages {
            return Err(anyhow!(
                "sub-agent transcript history shrank from {} to {} messages",
                self.persisted_messages,
                messages.len()
            ));
        }

        let mut encoded = Vec::new();
        for (index, message) in messages.iter().enumerate().skip(self.persisted_messages) {
            encoded.extend(json_line(&json!({
                "kind": "message",
                "index": index,
                "message": message,
            }))?);
        }

        if !encoded.is_empty() || durable {
            append_private_subagent_transcript(&self.state_root, &self.path, &encoded, durable)?;
        }

View on GitHub (pinned to 73e0f67d83)