parseShallowCustomRecord function
Header-only decode for giant custom records (issue #503 round 3b).
The resume boundary walk crosses hundreds of model_request_summary
ledger payloads (~0.75 MB each) that count ZERO context tokens; a full
jsonDecode + isolate transfer per record dominated the walk. The
canonical writer order is type,id,parentId,timestamp,customType,data,
so when the line is huge and starts as a custom record we decode only
the prefix up to ,"data": and stub data to null. Any deviation —
foreign field order, missing data key, malformed prefix — falls back
(returns null) so the caller does a full decode. custom_message
records project into context and are NEVER shallow-parsed (their type
string differs, so the prefix test already excludes them).
Implementation
SessionRecord? parseShallowCustomRecord(String line) {
if (line.length < shallowCustomRecordThreshold) return null;
if (!line.startsWith('{"type":"custom"')) return null;
final header = shallowCustomHeader(line);
if (header == null) return null;
try {
final decoded = jsonDecode(header);
if (decoded is! Map<String, dynamic>) return null;
if (decoded['type'] != 'custom') return null;
if (decoded['id'] is! String) return null;
if (decoded['timestamp'] is! String) return null;
if (decoded['customType'] is! String) return null;
return CustomRecord(
id: decoded['id'] as String,
parentId: decoded['parentId'] as String?,
timestamp: DateTime.parse(decoded['timestamp'] as String),
customType: decoded['customType'] as String,
data: null,
);
} on Object {
return null; // malformed prefix — the caller falls back to full decode
}
}