{"record":{"id":"04f42aba1883c7b1","repo":"Hmbown/CodeWhale","slug":"sub-agent-transcript-message-is-missing-its-index","errorCode":null,"errorMessage":"sub-agent transcript message is missing its index","messagePattern":"sub-agent transcript message is missing its index","errorType":"validation","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/tui/src/tools/subagent/mod.rs","lineNumber":8581,"sourceCode":"            != Some(u64::from(SUBAGENT_TRANSCRIPT_ARTIFACT_SCHEMA_VERSION))\n        || header.get(\"agent_id\").and_then(Value::as_str) != Some(agent_id)\n    {\n        return Err(anyhow!(\n            \"sub-agent transcript artifact header does not match agent {agent_id}\"\n        ));\n    }\n\n    let mut messages = Vec::new();\n    for line in lines.filter(|line| !line.trim().is_empty()) {\n        let record: Value = serde_json::from_str(line)?;\n        if record.get(\"kind\").and_then(Value::as_str) != Some(\"message\") {\n            return Err(anyhow!(\"unknown sub-agent transcript artifact record\"));\n        }\n        let index = record\n            .get(\"index\")\n            .and_then(Value::as_u64)\n            .and_then(|value| usize::try_from(value).ok())\n            .ok_or_else(|| anyhow!(\"sub-agent transcript message is missing its index\"))?;\n        if index != messages.len() {\n            return Err(anyhow!(\n                \"sub-agent transcript message index {index} does not follow {}\",\n                messages.len()\n            ));\n        }\n        let message = serde_json::from_value::<Message>(\n            record\n                .get(\"message\")\n                .cloned()\n                .ok_or_else(|| anyhow!(\"sub-agent transcript record is missing its message\"))?,\n        )?;\n        messages.push(message);\n    }\n    Ok(messages)\n}\n\nfn remove_subagent_transcript_artifact(state_root: &Path, agent_id: &str) -> Result<bool> {","sourceCodeStart":8563,"sourceCodeEnd":8599,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/73e0f67d83c59909b571efdfc88c4bc28c309cb1/crates/tui/src/tools/subagent/mod.rs#L8563-L8599","documentation":"Sub-agent transcript artifact parsing: a 'message' record in the JSONL artifact lacks its index field, so message ordering cannot be reconstructed; loading fails rather than presenting a misordered transcript.","triggerScenarios":"A message record line in the artifact missing the `index` field, or with an index that is not a u64 (e.g. a string, null, or negative) — typically from a hand-edited or externally generated record.","commonSituations":"Custom tooling writing transcript lines without the index field; manual edits dropping a field; serialization from a different record type that lacks the field.","solutions":["Add a correct integer `index` field to the offending record (0-based, sequential)","Renumber all records sequentially from 0 if indexes are missing across the file","Restore the artifact from backup and let the runtime rewrite it"],"exampleFix":"// before\n{\"kind\":\"message\",\"role\":\"user\",\"content\":\"hi\"}\n// after\n{\"kind\":\"message\",\"index\":0,\"role\":\"user\",\"content\":\"hi\"}","handlingStrategy":"validation","validationCode":"fn record_has_sequential_index(v: &serde_json::Value, expected: usize) -> Result<(), String> {\n    let idx = v.get(\"index\").and_then(|i| i.as_u64()).and_then(|i| usize::try_from(i).ok()).ok_or(\"missing index\")?;\n    if idx == expected { Ok(()) } else { Err(format!(\"index {idx} != expected {expected}\")) }\n}","typeGuard":"fn has_valid_index(v: &serde_json::Value, expected: usize) -> bool {\n    v.get(\"index\").and_then(|i| i.as_u64()).and_then(|i| usize::try_from(i).ok()) == Some(expected)\n}","tryCatchPattern":"match load_transcript(state_root, agent_id) {\n    Err(e) if e.to_string().contains(\"missing its index\") => renumber_and_reload(state_root, agent_id),\n    r => r,\n}","preventionTips":["Always write records with a 0-based sequential integer `index` field","Run any custom writer through the same serializer the runtime uses","Validate generated records (kind, index, ordering) before appending them to an artifact"],"tags":["subagent","rust","schema"],"backgroundTag":"schema-validation-failed","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"}