Skip to content

Commit 9b10478

Browse files
ryoppippiigorovh
andcommitted
fix(opencode): keep Claude Agent links past malformed content parts
The typed link pass failed as a whole when any content part, its native value, or sessionID had an unexpected shape, so one bad part unlinked a row that carried a valid link and its turn was counted twice. Lenient wrappers now read each part, native value, and sessionID on their own and skip any other shape through IgnoredAny, still without building a serde_json::Value. Refs #2004 Co-authored-by: igorovh <[email protected]>
1 parent 0048f5d commit 9b10478

1 file changed

Lines changed: 138 additions & 8 deletions

File tree

‎rust/adapters/opencode/src/parser.rs‎

Lines changed: 138 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1-
use std::sync::Arc;
1+
use std::{fmt, marker::PhantomData, sync::Arc};
22

33
use jiff::tz::TimeZone as JiffTimeZone;
4-
use serde::Deserialize;
4+
use serde::{
5+
Deserialize, Deserializer,
6+
de::{
7+
self, IgnoredAny, MapAccess, SeqAccess, Visitor,
8+
value::{MapAccessDeserializer, StrDeserializer},
9+
},
10+
};
511

612
use crate::{
713
LoadedEntry, PricingMap, TokenUsageRaw, UsageEntry, UsageMessage, apply_total_token_fallback,
@@ -116,24 +122,130 @@ struct OpenCodeV2Message {
116122
}
117123

118124
/// Reads only `content[].native.sessionID`. Derived structs skip every other
119-
/// field, such as message text, without building it, and a malformed part
120-
/// leaves the row unlinked rather than dropping it.
125+
/// field, such as message text, without building it, and the lenient wrappers
126+
/// turn a part or field of another shape into nothing, so one malformed part
127+
/// cannot unlink the row.
121128
#[derive(Deserialize)]
122129
struct OpenCodeNativeLinks {
123130
#[serde(default)]
124-
content: Vec<OpenCodeContentPart>,
131+
content: LenientSeq<Lenient<OpenCodeContentPart>>,
125132
}
126133

127134
#[derive(Deserialize)]
128135
struct OpenCodeContentPart {
129136
#[serde(default)]
130-
native: Option<OpenCodeNativeReference>,
137+
native: Lenient<OpenCodeNativeReference>,
131138
}
132139

133140
#[derive(Deserialize)]
134141
struct OpenCodeNativeReference {
135142
#[serde(rename = "sessionID", default)]
136-
session_id: Option<String>,
143+
session_id: Lenient<String>,
144+
}
145+
146+
macro_rules! skip_scalars {
147+
($empty:expr) => {
148+
fn visit_bool<E: de::Error>(self, _: bool) -> Result<Self::Value, E> {
149+
Ok($empty)
150+
}
151+
fn visit_i64<E: de::Error>(self, _: i64) -> Result<Self::Value, E> {
152+
Ok($empty)
153+
}
154+
fn visit_u64<E: de::Error>(self, _: u64) -> Result<Self::Value, E> {
155+
Ok($empty)
156+
}
157+
fn visit_f64<E: de::Error>(self, _: f64) -> Result<Self::Value, E> {
158+
Ok($empty)
159+
}
160+
fn visit_unit<E: de::Error>(self) -> Result<Self::Value, E> {
161+
Ok($empty)
162+
}
163+
};
164+
}
165+
166+
/// An object or string read as `T`, or `None` for a value of another shape,
167+
/// which is skipped without being built. A failure inside an object cannot be
168+
/// recovered, so `T`'s own fields must be lenient too.
169+
struct Lenient<T>(Option<T>);
170+
171+
impl<T> Default for Lenient<T> {
172+
fn default() -> Self {
173+
Self(None)
174+
}
175+
}
176+
177+
impl<'de, T: Deserialize<'de>> Deserialize<'de> for Lenient<T> {
178+
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
179+
struct LenientVisitor<T>(PhantomData<T>);
180+
181+
impl<'de, T: Deserialize<'de>> Visitor<'de> for LenientVisitor<T> {
182+
type Value = Lenient<T>;
183+
184+
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
185+
formatter.write_str("any JSON value")
186+
}
187+
188+
fn visit_map<A: MapAccess<'de>>(self, map: A) -> Result<Self::Value, A::Error> {
189+
T::deserialize(MapAccessDeserializer::new(map)).map(|value| Lenient(Some(value)))
190+
}
191+
192+
fn visit_str<E: de::Error>(self, value: &str) -> Result<Self::Value, E> {
193+
Ok(Lenient(
194+
T::deserialize(StrDeserializer::<E>::new(value)).ok(),
195+
))
196+
}
197+
198+
fn visit_seq<A: SeqAccess<'de>>(self, seq: A) -> Result<Self::Value, A::Error> {
199+
IgnoredAny.visit_seq(seq).map(|_| Lenient(None))
200+
}
201+
202+
skip_scalars!(Lenient(None));
203+
}
204+
205+
deserializer.deserialize_any(LenientVisitor(PhantomData))
206+
}
207+
}
208+
209+
/// An array read as `T` elements, or empty for a value of another shape, which
210+
/// is skipped without being built.
211+
struct LenientSeq<T>(Vec<T>);
212+
213+
impl<T> Default for LenientSeq<T> {
214+
fn default() -> Self {
215+
Self(Vec::new())
216+
}
217+
}
218+
219+
impl<'de, T: Deserialize<'de>> Deserialize<'de> for LenientSeq<T> {
220+
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
221+
struct LenientSeqVisitor<T>(PhantomData<T>);
222+
223+
impl<'de, T: Deserialize<'de>> Visitor<'de> for LenientSeqVisitor<T> {
224+
type Value = LenientSeq<T>;
225+
226+
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
227+
formatter.write_str("any JSON value")
228+
}
229+
230+
fn visit_seq<A: SeqAccess<'de>>(self, mut seq: A) -> Result<Self::Value, A::Error> {
231+
std::iter::from_fn(|| seq.next_element().transpose())
232+
.collect::<Result<_, _>>()
233+
.map(LenientSeq)
234+
}
235+
236+
fn visit_map<A: MapAccess<'de>>(self, map: A) -> Result<Self::Value, A::Error> {
237+
IgnoredAny.visit_map(map).map(|_| LenientSeq(Vec::new()))
238+
}
239+
240+
fn visit_str<E: de::Error>(self, _: &str) -> Result<Self::Value, E> {
241+
Ok(LenientSeq(Vec::new()))
242+
}
243+
244+
skip_scalars!(LenientSeq(Vec::new()));
245+
}
246+
247+
deserializer.deserialize_any(LenientSeqVisitor(PhantomData))
248+
}
137249
}
138250

139251
/// The Claude Code session a `claude-agent` row ran in, which names the
@@ -142,8 +254,9 @@ fn native_session_id(data: &str) -> Option<Arc<str>> {
142254
serde_json::from_str::<OpenCodeNativeLinks>(data)
143255
.ok()?
144256
.content
257+
.0
145258
.into_iter()
146-
.filter_map(|part| part.native?.session_id)
259+
.filter_map(|part| part.0?.native.0?.session_id.0)
147260
.map(|session_id| session_id.trim().to_owned())
148261
.find(|session_id| !session_id.is_empty())
149262
.map(Arc::from)
@@ -546,6 +659,23 @@ mod tests {
546659
);
547660
}
548661

662+
#[test]
663+
fn keeps_the_native_link_when_other_parts_are_malformed() {
664+
for content in [
665+
r#"[{"native":{"sessionID":"s"}},"junk"]"#,
666+
r#"[{"native":5},{"native":{"sessionID":7}},[1,{"a":2}],null,{"native":{"sessionID":"s"}}]"#,
667+
] {
668+
assert_eq!(
669+
v2_native_session(&format!(
670+
r#"{{"model":{{"id":"opus","providerID":"claude-agent"}},"tokens":{{"input":100}},"content":{content}}}"#
671+
))
672+
.as_deref(),
673+
Some("s"),
674+
"{content}"
675+
);
676+
}
677+
}
678+
549679
#[test]
550680
fn skips_blank_native_session_ids() {
551681
assert_eq!(

0 commit comments

Comments
 (0)