Skip to content

Commit d72e237

Browse files
committed
fix(opencode): keep the timestamp scan inside the message's own time object
Two findings from a CodeRabbit review of this branch. `extract_message_timestamp` searched for the first `"time"` anywhere in the payload and then for `"created":` without a bound, so a `time` object belonging to something else could contribute the number. A failed scan is harmless — the caller falls back to a full parse — but a wrong one silently drops an in-range entry. Require the canonical `"time":` key, require the object to open right after it, and keep the `created` search inside that object. Checked against 1037 real records (600 database rows, 437 message files): still agrees with a full parse on every one, with no fallbacks. `has_json_file` judged entries with `path.is_dir()`, which follows symlinks, while the collection pass it feeds uses `file_type()` and skips them. Detection could therefore claim files that would never be read, and a symlinked cycle would recurse until the stack gave out. Match `collect_files_with_extension` instead.
1 parent cfed08d commit d72e237

1 file changed

Lines changed: 46 additions & 12 deletions

File tree

  • rust/crates/ccusage/src/adapter/opencode

‎rust/crates/ccusage/src/adapter/opencode/loader.rs‎

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -133,17 +133,24 @@ pub(crate) fn has_source(opencode_dir: &Path) -> bool {
133133
has_json_file(&opencode_dir.join("storage").join("message"))
134134
}
135135

136+
// Mirrors `collect_files_with_extension`: judge entries by `file_type()` so
137+
// symlinks are neither followed nor counted. Following them would let detection
138+
// claim files the collection pass then refuses to read, and a symlinked cycle
139+
// would recurse until the stack gives out.
136140
fn has_json_file(dir: &Path) -> bool {
137141
let Ok(entries) = fs::read_dir(dir) else {
138142
return false;
139143
};
140144
entries.filter_map(std::result::Result::ok).any(|entry| {
145+
let Ok(file_type) = entry.file_type() else {
146+
return false;
147+
};
141148
let path = entry.path();
142-
if path.is_dir() {
143-
has_json_file(&path)
144-
} else {
149+
if file_type.is_file() {
145150
path.extension()
146151
.is_some_and(|extension| extension == "json")
152+
} else {
153+
file_type.is_dir() && has_json_file(&path)
147154
}
148155
})
149156
}
@@ -309,15 +316,23 @@ fn entry_id(entry: &LoadedEntry) -> Option<&str> {
309316

310317
/// Pulls `time.created` millis from raw JSON to skip rows before a full parse.
311318
///
312-
/// Anything but the canonical `"time":{"created":<millis>}` returns `None`, and
313-
/// the caller falls back to a full parse, so a missed scan slows down, not drops.
319+
/// Only the canonical `"time": { ... "created": <digits> ... }` shape is
320+
/// recognized, and the search for `created` never leaves that object, so a
321+
/// `time` object belonging to something else in the payload cannot contribute a
322+
/// number. Everything else returns `None` and the caller falls back to a full
323+
/// parse: a scan that gives up costs time, whereas a scan that guesses wrong
324+
/// would silently drop an in-range entry.
314325
fn extract_message_timestamp(data: &str) -> Option<i64> {
315-
let time_start = data.find("\"time\"")?;
316-
let time_section = &data[time_start..];
317-
let created_start = time_section.find("\"created\":")?;
318-
let after_key = time_section[created_start + "\"created\":".len()..].trim_start();
326+
const TIME_KEY: &str = "\"time\":";
327+
const CREATED_KEY: &str = "\"created\":";
328+
329+
let time_object = data[data.find(TIME_KEY)? + TIME_KEY.len()..]
330+
.trim_start()
331+
.strip_prefix('{')?;
332+
let time_object = &time_object[..time_object.find('}')?];
333+
let after_key = time_object[time_object.find(CREATED_KEY)? + CREATED_KEY.len()..].trim_start();
319334
let end = after_key
320-
.find(|c: char| !c.is_ascii_digit())
335+
.find(|character: char| !character.is_ascii_digit())
321336
.unwrap_or(after_key.len());
322337
after_key[..end].parse::<i64>().ok()
323338
}
@@ -1123,13 +1138,32 @@ mod tests {
11231138
super::extract_message_timestamp(PRETTY_PRINTED_MESSAGE),
11241139
Some(1_767_312_000_000)
11251140
);
1126-
// Digits running to the end of the input, without a closing brace.
1141+
// Digits running to the end of the object, without a trailing comma.
11271142
assert_eq!(
1128-
super::extract_message_timestamp(r#""time":{"created": 42"#),
1143+
super::extract_message_timestamp(r#"{"time":{"created": 42}}"#),
11291144
Some(42)
11301145
);
11311146
}
11321147

1148+
#[test]
1149+
fn ignores_a_time_object_that_is_not_the_messages_own() {
1150+
// The scan must not reach past the first `time` object for a `created`
1151+
// key: guessing wrong here would drop an in-range message, while giving
1152+
// up only costs a full parse.
1153+
assert_eq!(
1154+
super::extract_message_timestamp(
1155+
r#"{"parts":[{"time":{"start":1}}],"time":{"created":1767312000000}}"#
1156+
),
1157+
None
1158+
);
1159+
// A quoted `"time"` that is a value rather than a key is not followed by
1160+
// a colon, so it cannot start a match either.
1161+
assert_eq!(
1162+
super::extract_message_timestamp(r#"{"unit":"time","created":1767312000000}"#),
1163+
None
1164+
);
1165+
}
1166+
11331167
#[test]
11341168
fn declines_to_extract_timestamps_it_cannot_trust() {
11351169
// Quoted numbers, negative values and missing keys all fail open so the

0 commit comments

Comments
 (0)