{"record":{"id":"c9481332d5b4bac4","repo":"gitbutlerapp/gitbutler","slug":"transcript-contains-malformed-json-before-the-fina","errorCode":null,"errorMessage":"transcript contains malformed JSON before the final record","messagePattern":"transcript contains malformed JSON before the final record","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/but-agentlog/src/transcript.rs","lineNumber":47,"sourceCode":"            session_id: None,\n            provider: match agent {\n                Agent::Codex => None,\n                Agent::Claude => Some(\"anthropic\".to_string()),\n            },\n            model: None,\n            tool_version: None,\n            thread_source: None,\n            records: Vec::new(),\n        };\n        let mut codex_tool_names = HashMap::new();\n        let mut codex_spawn_prompts = HashSet::new();\n        let mut claude_tool_names = HashMap::new();\n\n        while let Some((index, trimmed)) = raw_records.next() {\n            let parsed = match serde_json::from_slice::<Value>(trimmed) {\n                Ok(parsed) => parsed,\n                Err(_) if raw_records.peek().is_none() => continue,\n                Err(_) => bail!(\"transcript contains malformed JSON before the final record\"),\n            };\n\n            let record = match agent {\n                Agent::Codex => {\n                    transcript.apply_codex_metadata(&parsed);\n                    ParsedRecord::from_codex_source(\n                        index,\n                        trimmed,\n                        parsed,\n                        &mut codex_tool_names,\n                        &mut codex_spawn_prompts,\n                        transcript.thread_source.as_deref(),\n                    )\n                }\n                Agent::Claude => {\n                    transcript.apply_claude_metadata(&parsed);\n                    ParsedRecord::from_claude_source(index, trimmed, parsed, &mut claude_tool_names)\n                }","sourceCodeStart":29,"sourceCodeEnd":65,"githubUrl":"https://github.com/gitbutlerapp/gitbutler/blob/58e5313667b857ef39a730e380af31816a7b1768/crates/but-agentlog/src/transcript.rs#L29-L65","documentation":"Thrown by transcript parsing when a JSONL transcript record (from Claude or Codex logs) fails to deserialize, and the malformed record is not the last one in the file. Only a trailing malformed record is tolerated (skipped); any malformed record earlier in the file aborts the whole parse, because the parser cannot trust the structure of the remaining transcript.","triggerScenarios":"Parsing an agent transcript file that contains a truncated, partially-written, or corrupted JSON line anywhere except the final line — e.g. a crash or concurrent write left an incomplete record mid-file.","commonSituations":"Reading Claude Code or Codex session transcripts after an editor/agent crash, appending to a live transcript while parsing, or manually editing a JSONL file and leaving a bad line.","solutions":["Open the transcript file and find the first line that is not valid JSON; repair or remove it","Validate each line with a JSONL linter before parsing","Re-generate the transcript by replaying the agent session if repair is not possible","If you only need the last record, extract the trailing line instead of full-parse"],"exampleFix":"// before\nlet transcript = Transcript::parse(path, agent)?; // bails on mid-file malformed JSON\n// after\nlet raw = std::fs::read_to_string(path)?;\nlet cleaned: Vec<&str> = raw.lines().enumerate().filter(|(i, l)| {\n    serde_json::from_str::<serde_json::Value>(l).is_ok() || *i + 1 == raw.lines().count()\n}).map(|(_, l)| l).collect();\nlet transcript = Transcript::parse_lines(cleaned, agent)?;","handlingStrategy":"validation","validationCode":"const lines = raw.split(\"\\n\").filter(Boolean);\nconst bad = lines.slice(0, -1).findIndex(l => { try { JSON.parse(l); return false; } catch { return true; } });\nif (bad !== -1) throw new Error(`malformed JSONL record at line ${bad + 1}`);","typeGuard":"function isParseableJsonl(lines: string[]): boolean {\n  return lines.slice(0, -1).every(l => { try { JSON.parse(l); return true; } catch { return false; } });\n}","tryCatchPattern":"try {\n  const t = Transcript::parse(path, agent);\n} catch (e) {\n  if (String(e).includes(\"malformed JSON before the final record\")) {\n    t = sanitizeTranscript(path, agent); // drop/repair bad lines, keep valid tail\n  } else { throw e; }\n}","preventionTips":["Never parse a transcript while the agent is still appending to it","Validate JSONL line-by-line before bulk parsing","Add a pre-parse sanitizer that drops or quarantines unparseable lines","Treat trailing-line corruption as skippable, mid-file corruption as fatal"],"tags":["jsonl","parsing","transcript","corruption"],"backgroundTag":"json-parse-error","analyzedSha":"58e5313667b857ef39a730e380af31816a7b1768","analyzedAt":"2026-09-18T06:50:32.052Z","contentChangedAt":"2026-09-18T06:50:32.052Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}