Write the optional UNHASHED section. This section is NOT included in the content hash. It's intended for data that shouldn't affect the change's identity: AI transcripts, reasoning traces, editor state, review comments, etc. The payload is provided as raw bytes (typically JSON-serialized). The writer compresses it with zstd but does NOT feed it through the blake3 hasher. # Arguments `data` - T
(&mut self, data: &[u8])
| 191 | /// - Zstd compression errors. |
| 192 | /// - I/O errors. |
| 193 | pub fn write_unhashed(&mut self, data: &[u8]) -> FormatResult<()> { |
| 194 | self.ensure_metadata_complete()?; |
| 195 | self.transition_to_at_least(WriterState::WritingContent)?; |
| 196 | |
| 197 | if self.state == WriterState::WroteUnhashed || self.state == WriterState::Finalized { |
| 198 | return Err(FormatError::UnexpectedSection { |
| 199 | got: "UNHASHED".to_string(), |
| 200 | expected: format!( |
| 201 | "UNHASHED can only be written once, state is {}", |
| 202 | self.state.name() |
| 203 | ), |
| 204 | }); |
| 205 | } |
| 206 | |
| 207 | // Compress the data |
| 208 | let compressed = zstd::encode_all(data, self.options.compression_level()) |
| 209 | .map_err(|e| FormatError::Compress(e.to_string()))?; |
| 210 | |
| 211 | // Write section header + compressed payload |
| 212 | let section_header = SectionHeader::new(SectionType::Unhashed, compressed.len() as u32); |
| 213 | section_header.write_to(self.writer)?; |
| 214 | self.writer.write_all(&compressed)?; |
| 215 | |
| 216 | // NOTE: Do NOT feed through hasher — unhashed section is excluded |
| 217 | // from the content hash by design. |
| 218 | |
| 219 | self.stats.sections_written += 1; |
| 220 | self.stats.total_uncompressed += data.len() as u64; |
| 221 | self.stats.total_compressed += compressed.len() as u64; |
| 222 | self.stats.total_bytes_written += (SectionHeader::SIZE + compressed.len()) as u64; |
| 223 | |
| 224 | self.state = WriterState::WroteUnhashed; |
| 225 | Ok(()) |
| 226 | } |
| 227 | } |