Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions src/harness/model/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ pub use types::*;
enum ContextPatternMatch {
/// Pattern may appear anywhere in the lowercased model id.
Substring,
/// Pattern must be a complete segment delimited by common provider/id
/// separators. This avoids false positives for short model ids such as
/// `o1` and `o3`.
/// Pattern must appear as a bounded segment: delimited by a non-alphanumeric
/// boundary (or the string start/end) on both sides. This lets short o-series
/// ids such as `o1` and `o3` resolve even when embedded mid-name (e.g.
/// `ollama/mistral-for-o1-benchmark`) while avoiding false positives for
/// substrings such as `solo1-7b`, `proto3-chat`, or `octo3thing`.
Segment,
}

Expand Down Expand Up @@ -75,11 +77,20 @@ fn matches_context_pattern(lower: &str, pattern: &str, mode: ContextPatternMatch
match mode {
ContextPatternMatch::Substring => lower.contains(pattern),
ContextPatternMatch::Segment => {
let model_name = lower.rsplit(['/', ':']).next().unwrap_or(lower);
model_name
.split(['-', '_', '.'])
.next()
.is_some_and(|segment| segment == pattern)
// Match `pattern` as a bounded segment: every occurrence is accepted
// only when the byte before it and the byte after it are both
// non-alphanumeric (or absent at the string edge). Scanning the whole
// id — not just the final `/`- or `:`-delimited component — lets a
// token embedded mid-name (`ollama/mistral-for-o1-benchmark`) resolve,
// while the boundary guard keeps substrings like `solo1-7b`,
// `proto3-chat`, and `octo3thing` unmatched.
let bytes = lower.as_bytes();
lower.match_indices(pattern).any(|(start, matched)| {
let before_ok = start == 0 || !bytes[start - 1].is_ascii_alphanumeric();
let end = start + matched.len();
let after_ok = end >= bytes.len() || !bytes[end].is_ascii_alphanumeric();
before_ok && after_ok
})
}
}
}
Expand Down
18 changes: 14 additions & 4 deletions src/harness/model/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ fn context_window_patterns_cover_common_provider_families() {

#[test]
fn o1_o3_context_patterns_require_segment_boundaries() {
// Canonical o-series ids still resolve to the 200K reasoning window.
assert_eq!(context_window_for_model_id("o1"), Some(200_000));
assert_eq!(context_window_for_model_id("o1-mini"), Some(200_000));
assert_eq!(context_window_for_model_id("o3-mini"), Some(200_000));
Expand All @@ -128,13 +129,22 @@ fn o1_o3_context_patterns_require_segment_boundaries() {
Some(200_000)
);

assert_eq!(context_window_for_model_id("solo1-7b"), None);
assert_eq!(context_window_for_model_id("proto3-chat"), None);
assert_eq!(context_window_for_model_id("octo3thing"), None);
// A bounded o1/o3 token embedded mid-name resolves too: it is delimited by
// non-alphanumeric boundaries on both sides.
assert_eq!(
context_window_for_model_id("ollama/mistral-for-o1-benchmark"),
None
Some(200_000)
);
assert_eq!(
context_window_for_model_id("vllm/qwen-o3-eval.bench"),
Some(200_000)
);

// The boundary guard keeps o1/o3 substrings from over-matching.
assert_eq!(context_window_for_model_id("solo1-7b"), None);
assert_eq!(context_window_for_model_id("proto3-chat"), None);
assert_eq!(context_window_for_model_id("octo3thing"), None);
assert_eq!(context_window_for_model_id("totally-unknown-model"), None);
}

#[test]
Expand Down