image.vision\aoci\aoci_budget.mbt
50% · 18 / 36 points · 139 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # AOCI Token 配额管理 AOCI Token Budget Management - 3
/// - 4
/// Token 配额/分区/降级压缩 - 5
/// 令牌配额/分区/降级压缩 Token quota/partitioning/degraded compression - 6
// AOCI-ContextBudget — Token 配额/分区/降级压缩 - 7
// 给定 Token 预算,按优先级分配符号加载,超预算时降级 - 8
- 9
///| - 10
#derive(Eq, @debug.Debug) - 11
pub(all) struct ContextBudget { - 12
total_quota : Int32 - 13
used : Int32 - 14
partition : Array[(String, Int32)] - 15
} - 16
- 17
///| - 18
pub extend ContextBudget with Eq::{not_equal, equal} - 19
- 20
///| - 21
pub extend ContextBudget with @debug.Debug::{to_repr} - 22
- 23
///| - 24
pub fn ContextBudget::make(quota : Int32) -> ContextBudget { - 25●
{ total_quota: quota, used: 0, partition: [], } - 26
} - 27
- 28
///| - 29
pub fn ContextBudget::remaining(self : ContextBudget) -> Int32 { - 30●
self.total_quota - self.used - 31
} - 32
- 33
///| - 34
pub fn ContextBudget::is_exhausted(self : ContextBudget) -> Bool { - 35●
self.used >= self.total_quota - 36
} - 37
- 38
///| - 39
pub fn ContextBudget::add_partition( - 40
self : ContextBudget, - 41
name : String, - 42
quota : Int32, - 43
) -> ContextBudget { - 44●
let partition = self.partition.copy() - 45●
partition.push((name, quota)) - 46
{ total_quota: self.total_quota, used: self.used, partition, } - 47
} - 48
- 49
///| - 50
pub fn ContextBudget::allocate( - 51
self : ContextBudget, - 52
symbols : Array[SymbolCoord], - 53
) -> Array[SymbolCoord] { - 54●
let result : Array[SymbolCoord] = [] - 55
let mut remaining = self.total_quota - self.used - 56
for sym in symbols { - 57●
let cost = sym.estimated_tokens() - 58
if cost <= remaining { - 59●
result.push(sym) - 60
remaining = remaining - cost - 61
} else { - 62●
let degraded = degrade_symbol(sym, remaining) - 63●
if degraded.estimated_tokens() <= remaining { - 64○
result.push(degraded) - 65○
remaining = remaining - degraded.estimated_tokens() - 66
} - 67
} - 68
if remaining <= 0 { - 69○
break - 70
} - 71
} - 72
result - 73
} - 74
- 75
///| - 76
pub fn ContextBudget::allocate_with_priority( - 77
self : ContextBudget, - 78
symbols : Array[(SymbolCoord, Int32)], - 79
) -> Array[SymbolCoord] { - 80○
let sorted = sort_by_priority(symbols) - 81
let coords : Array[SymbolCoord] = [] - 82
for entry in sorted { - 83○
coords.push(entry.0) - 84
} - 85○
self.allocate(coords) - 86
} - 87
- 88
///| - 89
fn degrade_symbol(sym : SymbolCoord, budget : Int32) -> SymbolCoord { - 90●
if budget < 20 { - 91●
SymbolCoord::make( - 92
pkg=sym.pkg, - 93
name=sym.name, - 94
kind=sym.kind, - 95
signature="", - 96
doc="", - 97
keywords=sym.keywords, - 98
) - 99○
} else if budget < 50 { - 100○
SymbolCoord::make( - 101
pkg=sym.pkg, - 102
name=sym.name, - 103
kind=sym.kind, - 104
signature=sym.signature, - 105
doc="", - 106
keywords=sym.keywords, - 107
) - 108
} else { - 109○
sym - 110
} - 111
} - 112
- 113
///| - 114
fn sort_by_priority( - 115
symbols : Array[(SymbolCoord, Int32)], - 116
) -> Array[(SymbolCoord, Int32)] { - 117○
let arr = symbols - 118○
let n = arr.length() - 119
for i in 0..<n { - 120○
for j in 0..<(n - 1 - i) { - 121○
if arr[j].1 < arr[j + 1].1 { - 122○
let tmp = arr[j] - 123
arr[j] = arr[j + 1] - 124
arr[j + 1] = tmp - 125
} - 126
} - 127
} - 128
arr - 129
} - 130
- 131
///| - 132
pub fn ContextBudget::usage_report(self : ContextBudget) -> String { - 133●
let pct = if self.total_quota > 0 { - 134●
self.used * 100 / self.total_quota - 135
} else { - 136○
0 - 137
} - 138
"Budget: \{self.used}/\{self.total_quota} tokens (\{pct}%), remaining: \{self.remaining()}" - 139
}