image.vision\aoci\aoci_budget.mbt

50% · 18 / 36 points · 139 lines

Background color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.

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