惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

B
Blog
D
Docker
J
Java Code Geeks
腾讯CDC
Blog — PlanetScale
Blog — PlanetScale
G
Google Developers Blog
M
MIT News - Artificial intelligence
L
LangChain Blog
T
The Blog of Author Tim Ferriss
P
Proofpoint News Feed
MyScale Blog
MyScale Blog
博客园 - Franky
GbyAI
GbyAI
Hugging Face - Blog
Hugging Face - Blog
aimingoo的专栏
aimingoo的专栏
Last Week in AI
Last Week in AI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 聂微东
N
Netflix TechBlog - Medium
B
Blog RSS Feed
Y
Y Combinator Blog
阮一峰的网络日志
阮一峰的网络日志
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google DeepMind News
Google DeepMind News

Recent Commits to openclaw:main

test: merge chat side-result checks · openclaw/openclaw@ddd2c2a test: merge cron history checks · openclaw/openclaw@f7eb746 test: merge responsive navigation shell checks · openclaw/openclaw@c2e4b47 docs(changelog): add codex oauth fixes · openclaw/openclaw@628e6cd test: merge navigation routing cases · openclaw/openclaw@5d8cecb Tests: mock channel registry bundled fallback · openclaw/openclaw@2b08233 Secrets: avoid broad web search discovery for single plugin config · openclaw/openclaw@a464f59 test: merge config view browser checks · openclaw/openclaw@20cf511 fix(status): align oauth health with runtime · openclaw/openclaw@eed7116 feat: add macOS screen snapshots for monitor preview (#67954) thanks … · openclaw/openclaw@f377db1 fix: report shared auth scopes in hello-ok (#67810) thanks @BunsDev · openclaw/openclaw@0b6c39b Auto-reply: avoid eager bundled route fallback · openclaw/openclaw@3ea1bf4 Tests: narrow session binding contract setup · openclaw/openclaw@54e4e16 fix(macOS): enable undo/redo in webchat composer text input (#34962) · openclaw/openclaw@00951dc Tests: speed up channel setup promotion · openclaw/openclaw@82b529a Docs: refresh agent instructions · openclaw/openclaw@5775fe2 fix(auth): serialize OAuth refresh across agents to fix #26322 (#67876) · openclaw/openclaw@8e79080 test: allow ollama public surface boundary test · openclaw/openclaw@7d4f1a6 Docs: add test performance guardrails · openclaw/openclaw@89706d3 Tests: restore context-engine usage proof · openclaw/openclaw@e4c4f95 Tests: slim context engine runtime coverage · openclaw/openclaw@74c198f ci: retry failed custom checkouts · openclaw/openclaw@0ee5baf test: trim duplicate provider auth onboarding cases · openclaw/openclaw@1ffc02e matrix: fix sessions_spawn --thread subagent session spawning (#67643) · openclaw/openclaw@1ce2596 test: reduce auth choice fixture churn · openclaw/openclaw@857b9cd test: mock health status config boundaries · openclaw/openclaw@9d5ab4a test: mock onboard config io boundary · openclaw/openclaw@299694d test: mock legacy state plugin boundaries · openclaw/openclaw@2713089 test: mock channel install boundaries · openclaw/openclaw@b945248 test: mock doctor preview channel boundaries · openclaw/openclaw@b1a3ad4
fix(model-usage): coerce numeric-string costs and ignore ...
coder9999999 · 2026-06-23 · via Recent Commits to openclaw:main

@@ -7,7 +7,14 @@

77

from datetime import date, timedelta

88

from unittest import TestCase, main

9910-

from model_usage import filter_by_days, positive_int

10+

from model_usage import (

11+

aggregate_costs,

12+

coerce_finite_cost,

13+

filter_by_days,

14+

latest_day_cost,

15+

pick_current_model,

16+

positive_int,

17+

)

111812191320

class TestModelUsage(TestCase):

@@ -35,6 +42,111 @@ def test_filter_by_days_keeps_recent_entries(self):

3542

self.assertEqual(filtered[0]["date"], (today - timedelta(days=1)).strftime("%Y-%m-%d"))

3643

self.assertEqual(filtered[1]["date"], today.strftime("%Y-%m-%d"))

374445+

def test_coerce_finite_cost_accepts_numbers_and_numeric_strings(self):

46+

self.assertEqual(coerce_finite_cost(2), 2.0)

47+

self.assertEqual(coerce_finite_cost(1.75), 1.75)

48+

self.assertEqual(coerce_finite_cost("1.75"), 1.75)

49+

self.assertEqual(coerce_finite_cost(" 2.5 "), 2.5)

50+51+

def test_coerce_finite_cost_rejects_booleans(self):

52+

# bool is a subclass of int in Python, but is never a valid cost.

53+

self.assertIsNone(coerce_finite_cost(True))

54+

self.assertIsNone(coerce_finite_cost(False))

55+56+

def test_coerce_finite_cost_rejects_non_finite(self):

57+

self.assertIsNone(coerce_finite_cost(float("nan")))

58+

self.assertIsNone(coerce_finite_cost(float("inf")))

59+

self.assertIsNone(coerce_finite_cost(float("-inf")))

60+

self.assertIsNone(coerce_finite_cost("NaN"))

61+

self.assertIsNone(coerce_finite_cost("Infinity"))

62+63+

def test_coerce_finite_cost_rejects_unusable_values(self):

64+

self.assertIsNone(coerce_finite_cost("not-a-number"))

65+

self.assertIsNone(coerce_finite_cost(""))

66+

self.assertIsNone(coerce_finite_cost(None))

67+

self.assertIsNone(coerce_finite_cost({}))

68+69+

def test_aggregate_costs_includes_numeric_strings(self):

70+

entries = [

71+

{

72+

"date": "2026-05-25",

73+

"modelBreakdowns": [

74+

{"modelName": "claude-sonnet-4-6", "cost": 1.50},

75+

{"modelName": "claude-sonnet-4-6", "cost": "1.75"},

76+

],

77+

}

78+

]

79+

self.assertEqual(aggregate_costs(entries), {"claude-sonnet-4-6": 3.25})

80+81+

def test_aggregate_costs_ignores_bool_and_non_finite(self):

82+

entries = [

83+

{

84+

"date": "2026-05-25",

85+

"modelBreakdowns": [

86+

{"modelName": "claude-sonnet-4-6", "cost": 1.50},

87+

{"modelName": "claude-sonnet-4-6", "cost": "1.75"},

88+

{"modelName": "claude-sonnet-4-6", "cost": True},

89+

{"modelName": "claude-sonnet-4-6", "cost": float("nan")},

90+

{"modelName": "claude-sonnet-4-6", "cost": float("inf")},

91+

],

92+

}

93+

]

94+

totals = aggregate_costs(entries)

95+

# NaN/Infinity must not poison the total; bool must not add 1.0.

96+

self.assertEqual(totals, {"claude-sonnet-4-6": 3.25})

97+98+

def test_pick_current_model_scores_numeric_string_costs(self):

99+

# model-b's cost is a numeric string; it must still win on highest cost.

100+

entries = [

101+

{

102+

"date": "2026-05-25",

103+

"modelBreakdowns": [

104+

{"modelName": "model-a", "cost": 1.0},

105+

{"modelName": "model-b", "cost": "5.0"},

106+

],

107+

}

108+

]

109+

model, day = pick_current_model(entries)

110+

self.assertEqual(model, "model-b")

111+

self.assertEqual(day, "2026-05-25")

112+113+

def test_pick_current_model_ignores_bool_and_non_finite(self):

114+

# Only model-a has a usable cost; bool and NaN must not be scored.

115+

entries = [

116+

{

117+

"date": "2026-05-25",

118+

"modelBreakdowns": [

119+

{"modelName": "model-a", "cost": 2.0},

120+

{"modelName": "model-b", "cost": True},

121+

{"modelName": "model-c", "cost": float("nan")},

122+

],

123+

}

124+

]

125+

model, _day = pick_current_model(entries)

126+

self.assertEqual(model, "model-a")

127+128+

def test_latest_day_cost_accepts_numeric_string(self):

129+

entries = [

130+

{

131+

"date": "2026-05-25",

132+

"modelBreakdowns": [{"modelName": "model-a", "cost": "2.50"}],

133+

}

134+

]

135+

day, cost = latest_day_cost(entries, "model-a")

136+

self.assertEqual(day, "2026-05-25")

137+

self.assertEqual(cost, 2.50)

138+139+

def test_latest_day_cost_rejects_non_finite(self):

140+

entries = [

141+

{

142+

"date": "2026-05-25",

143+

"modelBreakdowns": [{"modelName": "model-a", "cost": float("inf")}],

144+

}

145+

]

146+

day, cost = latest_day_cost(entries, "model-a")

147+

self.assertEqual(day, "2026-05-25")

148+

self.assertIsNone(cost)

149+3815039151

if __name__ == "__main__":

40152

main()