a3s-code-core 8.2.0

A3S Code Core - Embeddable AI agent library with tool execution
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
//! Bounded deterministic second-stage ranking for fused workspace evidence.

use super::hybrid_rank::compare_fused;
use super::{
    WorkspaceHybridSearchHit, WorkspaceRerankAlgorithm, WorkspaceRerankFallbackReason,
    WorkspaceRerankMode, WorkspaceRerankOptions, WorkspaceRerankStatus, WorkspaceRetrievalError,
    WorkspaceRetrievalResult,
};
use std::cmp::Ordering;
use std::collections::HashMap;
use std::sync::Arc;

mod features;
use features::{candidate_features, scratch_bytes, CandidateFeatures};

const MAX_CANDIDATES: usize = 100;
const MAX_FEATURE_BYTES_PER_CANDIDATE: usize = 4 * 1024;
const MAX_FINGERPRINTS_PER_CANDIDATE: usize = 128;
const MAX_SCRATCH_BYTES: usize = 4 * 1024 * 1024;
const MAX_RESULTS_PER_FILE: usize = 2;
const NEAR_DUPLICATE_THRESHOLD: f64 = 0.85;
const RELEVANCE_WEIGHT: f64 = 0.70;
const CHANNEL_AGREEMENT_WEIGHT: f64 = 0.10;
const DIVERSITY_WEIGHT: f64 = 0.20;
const NEAR_DUPLICATE_PENALTY: f64 = 0.50;
const MAX_CHANNELS: usize = 4;
const FNV_OFFSET: u64 = 0xcbf29ce484222325;
const FNV_PRIME: u64 = 0x100000001b3;

pub(super) struct RerankOutcome {
    pub hits: Vec<WorkspaceHybridSearchHit>,
    pub status: WorkspaceRerankStatus,
}

pub(super) fn rerank_status_not_run(requested_mode: WorkspaceRerankMode) -> WorkspaceRerankStatus {
    WorkspaceRerankStatus {
        requested_mode,
        applied_mode: WorkspaceRerankMode::RrfOnly,
        algorithm: WorkspaceRerankAlgorithm::RrfK60,
        input_candidates: 0,
        evaluated_candidates: 0,
        selected_candidates: 0,
        near_duplicate_candidates: 0,
        selected_near_duplicates: 0,
        feature_bytes: 0,
        accounted_scratch_bytes: 0,
        candidate_truncated: false,
        fallback: None,
    }
}

impl WorkspaceRerankOptions {
    /// Validate the hard candidate, feature, fingerprint, and scratch bounds.
    ///
    /// Hosts and language bindings can call this before catalog work or
    /// provider execution so invalid settings cause no source egress.
    pub fn validate(self) -> WorkspaceRetrievalResult<Self> {
        validate_range(
            self.max_candidates,
            1,
            MAX_CANDIDATES,
            "rerank.max_candidates",
        )?;
        validate_range(
            self.max_feature_bytes_per_candidate,
            4,
            MAX_FEATURE_BYTES_PER_CANDIDATE,
            "rerank.max_feature_bytes_per_candidate",
        )?;
        validate_range(
            self.max_fingerprints_per_candidate,
            1,
            MAX_FINGERPRINTS_PER_CANDIDATE,
            "rerank.max_fingerprints_per_candidate",
        )?;
        validate_range(
            self.max_scratch_bytes,
            1,
            MAX_SCRATCH_BYTES,
            "rerank.max_scratch_bytes",
        )?;
        Ok(self)
    }
}

fn validate_range(
    value: usize,
    minimum: usize,
    maximum: usize,
    field: &'static str,
) -> WorkspaceRetrievalResult<()> {
    if !(minimum..=maximum).contains(&value) {
        return Err(WorkspaceRetrievalError::InvalidConfiguration {
            field,
            reason: "is outside the supported bounded range",
        });
    }
    Ok(())
}

pub(super) fn rerank_fused_candidates(
    mut candidates: Vec<WorkspaceHybridSearchHit>,
    limit: usize,
    options: WorkspaceRerankOptions,
) -> RerankOutcome {
    candidates.sort_by(compare_fused);
    let input_candidates = candidates.len();
    if options.mode == WorkspaceRerankMode::RrfOnly {
        return rrf_only(
            candidates,
            limit,
            input_candidates,
            options.mode,
            0,
            false,
            None,
        );
    }
    if options.validate().is_err() {
        return rrf_only(
            candidates,
            limit,
            input_candidates,
            options.mode,
            0,
            false,
            Some(WorkspaceRerankFallbackReason::InvalidConfiguration),
        );
    }

    let candidate_truncated = candidates.len() > options.max_candidates;
    let evaluated_candidates = candidates.len().min(options.max_candidates);
    let accounted_scratch_bytes = scratch_bytes(evaluated_candidates, options);
    if accounted_scratch_bytes > options.max_scratch_bytes {
        return rrf_only(
            candidates,
            limit,
            input_candidates,
            options.mode,
            accounted_scratch_bytes,
            candidate_truncated,
            Some(WorkspaceRerankFallbackReason::ScratchBudgetExceeded),
        );
    }
    candidates = bounded_candidate_pool(candidates, options.max_candidates);

    let features = candidates
        .iter()
        .map(|candidate| candidate_features(&candidate.chunk.text, options))
        .collect::<Vec<_>>();
    let feature_bytes = features.iter().fold(0usize, |total, features| {
        total.saturating_add(features.feature_bytes)
    });
    let near_duplicate_candidates = count_near_duplicates(&candidates, &features);
    let (hits, selected_near_duplicates) = select_mmr(candidates, &features, limit);
    let selected_candidates = hits.len();

    RerankOutcome {
        hits,
        status: WorkspaceRerankStatus {
            requested_mode: options.mode,
            applied_mode: WorkspaceRerankMode::Deterministic,
            algorithm: WorkspaceRerankAlgorithm::RrfK60DeterministicMmrV1,
            input_candidates,
            evaluated_candidates: features.len(),
            selected_candidates,
            near_duplicate_candidates,
            selected_near_duplicates,
            feature_bytes,
            accounted_scratch_bytes,
            candidate_truncated,
            fallback: None,
        },
    }
}

fn bounded_candidate_pool(
    candidates: Vec<WorkspaceHybridSearchHit>,
    maximum: usize,
) -> Vec<WorkspaceHybridSearchHit> {
    if candidates.len() <= maximum {
        return candidates;
    }

    let mut selected = Vec::<usize>::with_capacity(maximum);
    for per_file_quota in 1..=MAX_RESULTS_PER_FILE {
        for (index, candidate) in candidates.iter().enumerate() {
            if selected.len() == maximum {
                break;
            }
            if selected.contains(&index) {
                continue;
            }
            let selected_for_file = selected
                .iter()
                .filter(|selected_index| {
                    candidates[**selected_index].chunk.path == candidate.chunk.path
                })
                .count();
            if selected_for_file < per_file_quota {
                selected.push(index);
            }
        }
    }
    for index in 0..candidates.len() {
        if selected.len() == maximum {
            break;
        }
        if !selected.contains(&index) {
            selected.push(index);
        }
    }
    selected.sort_unstable();

    let mut selected = selected.into_iter().peekable();
    candidates
        .into_iter()
        .enumerate()
        .filter_map(|(index, candidate)| {
            if selected.next_if_eq(&index).is_some() {
                Some(candidate)
            } else {
                None
            }
        })
        .collect()
}

fn rrf_only(
    candidates: Vec<WorkspaceHybridSearchHit>,
    limit: usize,
    input_candidates: usize,
    requested_mode: WorkspaceRerankMode,
    accounted_scratch_bytes: usize,
    candidate_truncated: bool,
    fallback: Option<WorkspaceRerankFallbackReason>,
) -> RerankOutcome {
    let mut per_file = HashMap::<Arc<str>, usize>::new();
    let hits = candidates
        .into_iter()
        .filter_map(|mut candidate| {
            let count = per_file
                .entry(Arc::clone(&candidate.chunk.path))
                .or_default();
            if *count >= MAX_RESULTS_PER_FILE {
                return None;
            }
            *count += 1;
            candidate.rerank_score = candidate.fused_score;
            candidate.redundancy_score = 0.0;
            Some(candidate)
        })
        .take(limit)
        .collect::<Vec<_>>();
    let selected_candidates = hits.len();
    RerankOutcome {
        hits,
        status: WorkspaceRerankStatus {
            requested_mode,
            applied_mode: WorkspaceRerankMode::RrfOnly,
            algorithm: WorkspaceRerankAlgorithm::RrfK60,
            input_candidates,
            evaluated_candidates: 0,
            selected_candidates,
            near_duplicate_candidates: 0,
            selected_near_duplicates: 0,
            feature_bytes: 0,
            accounted_scratch_bytes,
            candidate_truncated,
            fallback,
        },
    }
}

fn select_mmr(
    candidates: Vec<WorkspaceHybridSearchHit>,
    features: &[CandidateFeatures],
    limit: usize,
) -> (Vec<WorkspaceHybridSearchHit>, usize) {
    let exact_max = maximum_fused_score(&candidates, true);
    let non_exact_max = maximum_fused_score(&candidates, false);
    let mut selected = Vec::<usize>::with_capacity(limit.min(candidates.len()));
    let mut selected_scores = Vec::<(f64, f64)>::with_capacity(selected.capacity());
    let mut selected_flags = vec![false; candidates.len()];
    let mut per_file = HashMap::<Arc<str>, usize>::new();
    let mut selected_near_duplicates = 0usize;

    while selected.len() < limit {
        let select_exact = candidates.iter().enumerate().any(|(index, candidate)| {
            !selected_flags[index]
                && candidate.exact_identifier
                && per_file.get(&candidate.chunk.path).copied().unwrap_or(0) < MAX_RESULTS_PER_FILE
        });
        let maximum = if select_exact {
            exact_max
        } else {
            non_exact_max
        };
        let mut best = None::<(usize, f64, f64)>;
        for (index, candidate) in candidates.iter().enumerate() {
            if selected_flags[index]
                || candidate.exact_identifier != select_exact
                || per_file.get(&candidate.chunk.path).copied().unwrap_or(0) >= MAX_RESULTS_PER_FILE
            {
                continue;
            }
            let redundancy = selected.iter().fold(0.0_f64, |maximum, selected_index| {
                maximum.max(candidate_similarity(
                    candidate,
                    &features[index],
                    &candidates[*selected_index],
                    &features[*selected_index],
                ))
            });
            let score = selection_score(candidate, maximum, redundancy);
            let replace = best.is_none_or(|(best_index, best_score, _)| {
                score
                    .total_cmp(&best_score)
                    .then_with(|| best_index.cmp(&index))
                    == Ordering::Greater
            });
            if replace {
                best = Some((index, score, redundancy));
            }
        }
        let Some((index, score, redundancy)) = best else {
            break;
        };
        selected_flags[index] = true;
        *per_file
            .entry(Arc::clone(&candidates[index].chunk.path))
            .or_default() += 1;
        if redundancy >= NEAR_DUPLICATE_THRESHOLD {
            selected_near_duplicates = selected_near_duplicates.saturating_add(1);
        }
        selected.push(index);
        selected_scores.push((score, redundancy));
    }

    let hits = selected
        .into_iter()
        .zip(selected_scores)
        .map(|(index, (score, redundancy))| {
            let mut candidate = candidates[index].clone();
            candidate.rerank_score = score;
            candidate.redundancy_score = redundancy;
            candidate
        })
        .collect();
    (hits, selected_near_duplicates)
}

fn maximum_fused_score(candidates: &[WorkspaceHybridSearchHit], exact: bool) -> f64 {
    candidates
        .iter()
        .filter(|candidate| candidate.exact_identifier == exact)
        .map(|candidate| candidate.fused_score)
        .filter(|score| score.is_finite() && *score > 0.0)
        .max_by(f64::total_cmp)
        .unwrap_or(1.0)
}

fn selection_score(
    candidate: &WorkspaceHybridSearchHit,
    maximum_fused_score: f64,
    redundancy: f64,
) -> f64 {
    let relevance = (candidate.fused_score / maximum_fused_score).clamp(0.0, 1.0);
    let agreement = (candidate.channels.len() as f64 / MAX_CHANNELS as f64).clamp(0.0, 1.0);
    let duplicate_penalty = if redundancy >= NEAR_DUPLICATE_THRESHOLD {
        NEAR_DUPLICATE_PENALTY * redundancy
    } else {
        0.0
    };
    RELEVANCE_WEIGHT * relevance
        + CHANNEL_AGREEMENT_WEIGHT * agreement
        + DIVERSITY_WEIGHT * (1.0 - redundancy)
        - duplicate_penalty
}

fn count_near_duplicates(
    candidates: &[WorkspaceHybridSearchHit],
    features: &[CandidateFeatures],
) -> usize {
    candidates
        .iter()
        .enumerate()
        .filter(|(index, candidate)| {
            (0..*index).any(|prior| {
                candidate_similarity(
                    candidate,
                    &features[*index],
                    &candidates[prior],
                    &features[prior],
                ) >= NEAR_DUPLICATE_THRESHOLD
            })
        })
        .count()
}

fn candidate_similarity(
    left: &WorkspaceHybridSearchHit,
    left_features: &CandidateFeatures,
    right: &WorkspaceHybridSearchHit,
    right_features: &CandidateFeatures,
) -> f64 {
    interval_overlap(left, right).max(fingerprint_jaccard(
        &left_features.fingerprints,
        &right_features.fingerprints,
    ))
}

fn interval_overlap(left: &WorkspaceHybridSearchHit, right: &WorkspaceHybridSearchHit) -> f64 {
    if left.chunk.path != right.chunk.path {
        return 0.0;
    }
    let intersection = left
        .chunk
        .end_byte
        .min(right.chunk.end_byte)
        .saturating_sub(left.chunk.start_byte.max(right.chunk.start_byte));
    let denominator = left
        .chunk
        .end_byte
        .saturating_sub(left.chunk.start_byte)
        .min(right.chunk.end_byte.saturating_sub(right.chunk.start_byte));
    if denominator == 0 {
        0.0
    } else {
        intersection as f64 / denominator as f64
    }
}

fn fingerprint_jaccard(left: &[u64], right: &[u64]) -> f64 {
    if left.is_empty() || right.is_empty() {
        return 0.0;
    }
    let mut left_index = 0usize;
    let mut right_index = 0usize;
    let mut intersection = 0usize;
    while left_index < left.len() && right_index < right.len() {
        match left[left_index].cmp(&right[right_index]) {
            Ordering::Less => left_index += 1,
            Ordering::Greater => right_index += 1,
            Ordering::Equal => {
                intersection += 1;
                left_index += 1;
                right_index += 1;
            }
        }
    }
    let union = left
        .len()
        .saturating_add(right.len())
        .saturating_sub(intersection);
    intersection as f64 / union as f64
}