codebee 0.1.74 → 0.1.77

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (119) hide show
  1. package/CHANGELOG.md +628 -607
  2. package/LICENSE +21 -21
  3. package/README.md +507 -508
  4. package/app/.playwright-cli/console-2026-09-23T08-26-03-635Z.log +88 -0
  5. package/app/.playwright-cli/page-2026-09-23T08-26-03-845Z.yml +126 -0
  6. package/app/.playwright-cli/page-2026-09-23T08-29-44-203Z.png +0 -0
  7. package/app/.playwright-cli/page-2026-09-23T08-35-42-046Z.yml +210 -0
  8. package/app/.playwright-cli/page-2026-09-23T08-36-52-858Z.png +0 -0
  9. package/app/.playwright-cli/page-2026-09-23T08-37-33-938Z.png +0 -0
  10. package/app/.playwright-cli/run-detail-after.png +0 -0
  11. package/app/core/acceptance.py +100 -100
  12. package/app/core/aiflavor.py +197 -197
  13. package/app/core/analytics.py +38 -38
  14. package/app/core/artifacts.py +148 -148
  15. package/app/core/assets.py +110 -110
  16. package/app/core/attachments.py +623 -623
  17. package/app/core/audit.py +30 -30
  18. package/app/core/backup.py +470 -470
  19. package/app/core/beekeeper.py +183 -183
  20. package/app/core/benchstore.py +95 -95
  21. package/app/core/board.py +209 -209
  22. package/app/core/bookmeta.py +1036 -921
  23. package/app/core/bookmeta_catalog.py +180 -180
  24. package/app/core/branching.py +137 -137
  25. package/app/core/builtin_agent.py +1680 -1680
  26. package/app/core/capability.py +144 -144
  27. package/app/core/chaptersafe.py +32 -32
  28. package/app/core/cleanup.py +303 -303
  29. package/app/core/compaction.py +201 -201
  30. package/app/core/contracts.py +255 -255
  31. package/app/core/covergen.py +356 -356
  32. package/app/core/defectretro.py +43 -43
  33. package/app/core/diagnostics.py +114 -114
  34. package/app/core/dispatch.py +190 -190
  35. package/app/core/dispatch_log.py +128 -128
  36. package/app/core/env_scrub.py +85 -85
  37. package/app/core/errorlog.py +137 -137
  38. package/app/core/evaluation.py +174 -174
  39. package/app/core/failure_notify.py +290 -290
  40. package/app/core/goal_service.py +167 -167
  41. package/app/core/health.py +389 -387
  42. package/app/core/history.py +38 -38
  43. package/app/core/hooks.py +162 -162
  44. package/app/core/jobs.py +22 -0
  45. package/app/core/manager.py +2232 -2232
  46. package/app/core/mocks.py +64 -64
  47. package/app/core/notify.py +201 -201
  48. package/app/core/novel_quality.py +189 -189
  49. package/app/core/openapi.py +154 -154
  50. package/app/core/operations.py +228 -228
  51. package/app/core/paihang.py +145 -145
  52. package/app/core/pipeline.py +189 -18
  53. package/app/core/planner.py +88 -0
  54. package/app/core/portguard.py +111 -111
  55. package/app/core/presence.py +77 -77
  56. package/app/core/preview.py +293 -293
  57. package/app/core/prompt_guard.py +78 -78
  58. package/app/core/publish/__init__.py +7 -7
  59. package/app/core/publish/browser.py +655 -655
  60. package/app/core/publish/fanqie.py +165 -165
  61. package/app/core/publish/flow.py +640 -640
  62. package/app/core/publish/flows-fanqie-calibrated.json +336 -336
  63. package/app/core/publish/manager.py +1118 -1118
  64. package/app/core/publish/qimao.py +176 -176
  65. package/app/core/publish/ws.py +139 -139
  66. package/app/core/quality_gate.py +194 -194
  67. package/app/core/redact.py +38 -38
  68. package/app/core/repeat_guard.py +129 -129
  69. package/app/core/retrieval.py +137 -137
  70. package/app/core/router.py +398 -398
  71. package/app/core/runner.py +2238 -2225
  72. package/app/core/selfupdate.py +384 -384
  73. package/app/core/session_log.py +161 -161
  74. package/app/core/sessions.py +312 -312
  75. package/app/core/settings_schema.py +270 -270
  76. package/app/core/share_page.py +114 -114
  77. package/app/core/skill_scan.py +139 -139
  78. package/app/core/skillpacks/market/character-bible.md +66 -66
  79. package/app/core/skillpacks/market/code-risk-checklist.md +58 -58
  80. package/app/core/skillpacks/market/git-workflow.md +57 -57
  81. package/app/core/skillpacks/market/release-notes.md +72 -72
  82. package/app/core/skillpacks/market/weekly-report.md +71 -71
  83. package/app/core/skillpacks/market/worldview-consistency.md +70 -70
  84. package/app/core/skills.py +1 -1
  85. package/app/core/step_runner.py +132 -132
  86. package/app/core/story_tracking.py +275 -241
  87. package/app/core/task_compile.py +199 -199
  88. package/app/core/telemetry.py +292 -292
  89. package/app/core/tlsctx.py +64 -64
  90. package/app/core/token_meter.py +147 -147
  91. package/app/core/tracing.py +144 -144
  92. package/app/core/upstream.py +41 -41
  93. package/app/core/usage.py +1035 -1035
  94. package/app/core/volumes.py +389 -389
  95. package/app/core/wxdigest.py +858 -858
  96. package/app/core/zentao.py +2692 -2692
  97. package/app/main.py +59 -0
  98. package/app/mcp_server.py +244 -244
  99. package/app/output/_createbook_body.json +1 -0
  100. package/app/output/_probe_fq_click.py +50 -0
  101. package/app/output/_probe_fq_form.py +42 -0
  102. package/app/output/_probe_fq_modal.py +55 -0
  103. package/app/output/_probe_fq_points.py +71 -0
  104. package/app/output/_probe_fq_submit.py +34 -0
  105. package/app/output/_probe_fq_watch.py +62 -0
  106. package/app/pick_dialog.py +110 -110
  107. package/app/ui/app.js +108 -12
  108. package/app/ui/artdiff.js +74 -74
  109. package/app/ui/board.html +162 -162
  110. package/app/ui/board.js +305 -305
  111. package/app/ui/i18n.js +15 -0
  112. package/app/ui/icons/bee.svg +79 -79
  113. package/app/ui/index.html +4 -0
  114. package/app/ui/manifest.json +16 -16
  115. package/app/ui/qrcode.js +2297 -2297
  116. package/app/ui/style.css +7086 -7070
  117. package/bin/tutti.js +147 -147
  118. package/package.json +48 -48
  119. package/tools/wx_reader.py +261 -261
@@ -1,190 +1,190 @@
1
- # -*- coding: utf-8 -*-
2
- """统一调度评分:任务画像、CLI 亲和度与模型链排序。
3
-
4
- 调用方先完成协议、启停、健康、密钥等硬约束过滤,再把可用候选交给这里评分;
5
- 静态能力与近期用量台账只提供软信号,相同分数保持用户原顺序。
6
- """
7
- from __future__ import annotations
8
-
9
- import math
10
-
11
-
12
- TYPE_DIMENSIONS = {
13
- "direct": "reasoning",
14
- "code": "coding",
15
- "novel": "writing",
16
- "serial_novel": "writing",
17
- "article": "writing",
18
- "video_script": "writing",
19
- "doc": "writing",
20
- "translation": "writing",
21
- "rank_scan": "reasoning",
22
- "research": "reasoning",
23
- "speech": "writing",
24
- "weekly_report": "writing",
25
- "email": "writing",
26
- "tech_proposal": "reasoning",
27
- "resume": "writing",
28
- "bid_doc": "writing", # 标书:正文应答写作量大(合规判断由契约规则约束)
29
- "zentao": "coding",
30
- }
31
-
32
- _KIND_AFFINITY = {
33
- "coding": {"codex": 10, "aider": 8, "opencode": 7, "qwen": 6,
34
- "claude": 5, "generic": 2},
35
- "writing": {"claude": 10, "qwen": 7, "generic": 5, "codex": 3,
36
- "opencode": 2, "aider": 0},
37
- "reasoning": {"claude": 9, "codex": 8, "qwen": 6, "opencode": 4,
38
- "generic": 3, "aider": 1},
39
- "vision": {"codex": 10, "claude": 8, "qwen": 5, "opencode": 2,
40
- "generic": 1, "aider": 0},
41
- }
42
-
43
- _TIER_SCORE = {
44
- "easy": {"budget": 18.0, "standard": 8.0, "premium": -6.0},
45
- "default": {"budget": 2.0, "standard": 6.0, "premium": 5.0},
46
- "hard": {"budget": -8.0, "standard": 4.0, "premium": 12.0},
47
- }
48
-
49
-
50
- def task_dimension(task_type, role=""):
51
- """把预置任务与步骤角色归一为 writing/coding/reasoning/vision。"""
52
- ttype = str(task_type or "").strip().lower()
53
- role = str(role or "").strip().lower()
54
- if ttype in ("writing", "coding", "reasoning", "vision"):
55
- return ttype
56
- if role in ("plan", "review", "critique", "selector", "qa") \
57
- or "review" in role or "critique" in role:
58
- return "reasoning"
59
- return TYPE_DIMENSIONS.get(ttype, "reasoning")
60
-
61
-
62
- def agent_affinity(kind, task_type, role=""):
63
- """CLI 类型对任务维度的温和偏好;只作加分,不覆盖健康与历史信号。"""
64
- dim = task_dimension(task_type, role)
65
- score = float((_KIND_AFFINITY.get(dim) or {}).get(kind, 0))
66
- if role == "review" or "critique" in str(role or ""):
67
- score += {"claude": 4, "codex": 3, "qwen": 2}.get(kind, 0)
68
- return score, "%s 匹配 %+.1f" % (dim, score)
69
-
70
-
71
- def _model_meta(provider, model):
72
- for item in (provider or {}).get("models") or []:
73
- if isinstance(item, dict) and item.get("name") == model:
74
- return item
75
- return {}
76
-
77
-
78
- def _price_score(pricing, model, difficulty):
79
- price = (pricing or {}).get(model) or {}
80
- try:
81
- # 输出通常比输入贵且更影响整步成本,按 2 倍权重估算。
82
- blended = max(0.0, float(price.get("in") or 0.0)
83
- + 2.0 * float(price.get("out") or 0.0))
84
- except (TypeError, ValueError):
85
- return 0.0, "价格未知"
86
- if blended <= 0:
87
- return 0.0, "价格未知"
88
- magnitude = max(0.0, math.log10(blended + 1.0))
89
- score = -min(18.0, magnitude * (7.0 if difficulty == "easy" else 2.0))
90
- return score, "估算价 %.3g(%+.1f)" % (blended, score)
91
-
92
-
93
- def _online_model_bonus(entry, task_type, role):
94
- """把真实运行指标作为模型链软信号,硬约束仍由调用方负责。"""
95
- try:
96
- from . import usage
97
- metrics = usage.routing_stats(
98
- task_type=task_type, role=role,
99
- provider=entry.get("provider_id") or "",
100
- model=entry.get("model") or "")
101
- samples = int(metrics.get("samples") or 0)
102
- if not samples:
103
- return 0.0, ""
104
- rate = float(metrics.get("success_rate") or 0.0)
105
- success_score = max(-6.0, min(6.0, (rate - 0.75) * 18.0))
106
- p95 = max(0.0, float(metrics.get("p95_duration_s") or 0.0))
107
- # 模型链也必须让真实延迟影响排序,不能让 P95 数分钟的链靠静态质量分
108
- # 长期压在十几秒的健康链前面。
109
- latency_score = -min(12.0, max(0.0, (p95 - 30.0) / 8.0))
110
- cost = max(0.0, float(metrics.get("avg_cost_usd") or 0.0))
111
- cost_score = -min(3.0, max(0.0, (cost - 0.01) / 0.01))
112
- total = round(success_score + latency_score + cost_score, 2)
113
- success_samples = int(metrics.get("success_samples") or samples)
114
- reason = ("在线 %d/%d 验收成功(%+.1f),P95 %.1fs(%+.1f),均价 $%.4f(%+.1f)"
115
- % (int(metrics.get("successes") or 0), success_samples,
116
- success_score, p95, latency_score, cost, cost_score))
117
- return total, reason
118
- except Exception:
119
- return 0.0, ""
120
-
121
-
122
- def _bench_bonus(entry):
123
- """评测基准台实测分软信号(promptfoo 式样题榜单;14 天新鲜度,过期记 0)。
124
-
125
- 与在线信号同理念:软加减分,硬约束(健康/冷却/协议)仍由调用方负责;
126
- 无数据不猜分——没上过评测台的候选保持原排序。
127
- """
128
- try:
129
- from . import benchstore
130
- return benchstore.bonus_for(entry.get("provider_id") or "",
131
- entry.get("model") or "")
132
- except Exception:
133
- return 0.0, ""
134
-
135
-
136
- def score_model_entry(entry, providers, pricing, difficulty, task_type="", role=""):
137
- """给已通过硬约束的模型链条目评分,返回 (score, explanation)。"""
138
- provider = (providers or {}).get(entry.get("provider_id")) \
139
- or entry.get("provider") or {}
140
- model = entry.get("model") or ""
141
- meta = _model_meta(provider, model)
142
- try:
143
- priority = max(1, int(meta.get("priority") or 99))
144
- except (TypeError, ValueError):
145
- priority = 99
146
- quality = 0.0 if priority == 99 else max(-8.0, 20.0 - (priority - 1) * 4.0)
147
- tier = meta.get("tier") or provider.get("tier") or "standard"
148
- tier_score = (_TIER_SCORE.get(difficulty) or {}).get(tier, 0.0)
149
- price_score, price_reason = _price_score(pricing, model, difficulty)
150
- dim = task_dimension(task_type, role)
151
- strengths = provider.get("strengths") if isinstance(provider.get("strengths"), list) else []
152
- strength_score = 10.0 if dim in strengths else 0.0
153
- if difficulty == "easy":
154
- quality *= 0.25
155
- elif difficulty != "hard":
156
- # 普通任务兼顾质量、成本与能力匹配;是否真的重排由调用方决定,
157
- # 因而手工链仍可保持原顺序,自动推荐则能使用这组平衡分。
158
- quality *= 0.65
159
- vision_score = 0.0
160
- if dim == "vision":
161
- vision_score = 24.0 if meta.get("image_in") else -24.0
162
- online_score, online_reason = _online_model_bonus(entry, task_type, role)
163
- bench_score, bench_reason = _bench_bonus(entry)
164
- total = (quality + tier_score + price_score + strength_score + vision_score
165
- + online_score + bench_score)
166
- reason = ("质量 %+.1f,档位 %s %+.1f,%s,能力 %s %+.1f%s%s%s%s"
167
- % (quality, tier, tier_score, price_reason, dim,
168
- strength_score + vision_score,
169
- "," if online_reason else "", online_reason,
170
- "," if bench_reason else "", bench_reason))
171
- return round(total, 2), reason
172
-
173
-
174
- def rank_model_entries(entries, providers, pricing, difficulty,
175
- task_type="", role="", force=False):
176
- """稳定排序模型链并返回脱敏决策明细;default 难度保持人工顺序。"""
177
- rows = []
178
- for index, entry in enumerate(entries or []):
179
- score, reason = score_model_entry(
180
- entry, providers, pricing, difficulty, task_type, role)
181
- rows.append((score, index, entry, reason))
182
- if difficulty in ("easy", "hard") or force:
183
- rows.sort(key=lambda row: (-row[0], row[1]))
184
- ranked = [row[2] for row in rows]
185
- decisions = [{"provider_id": row[2].get("provider_id") or "",
186
- "provider": (row[2].get("provider") or {}).get("name") or "",
187
- "model": row[2].get("model") or "",
188
- "score": row[0], "reason": row[3]}
189
- for row in rows]
190
- return ranked, decisions
1
+ # -*- coding: utf-8 -*-
2
+ """统一调度评分:任务画像、CLI 亲和度与模型链排序。
3
+
4
+ 调用方先完成协议、启停、健康、密钥等硬约束过滤,再把可用候选交给这里评分;
5
+ 静态能力与近期用量台账只提供软信号,相同分数保持用户原顺序。
6
+ """
7
+ from __future__ import annotations
8
+
9
+ import math
10
+
11
+
12
+ TYPE_DIMENSIONS = {
13
+ "direct": "reasoning",
14
+ "code": "coding",
15
+ "novel": "writing",
16
+ "serial_novel": "writing",
17
+ "article": "writing",
18
+ "video_script": "writing",
19
+ "doc": "writing",
20
+ "translation": "writing",
21
+ "rank_scan": "reasoning",
22
+ "research": "reasoning",
23
+ "speech": "writing",
24
+ "weekly_report": "writing",
25
+ "email": "writing",
26
+ "tech_proposal": "reasoning",
27
+ "resume": "writing",
28
+ "bid_doc": "writing", # 标书:正文应答写作量大(合规判断由契约规则约束)
29
+ "zentao": "coding",
30
+ }
31
+
32
+ _KIND_AFFINITY = {
33
+ "coding": {"codex": 10, "aider": 8, "opencode": 7, "qwen": 6,
34
+ "claude": 5, "generic": 2},
35
+ "writing": {"claude": 10, "qwen": 7, "generic": 5, "codex": 3,
36
+ "opencode": 2, "aider": 0},
37
+ "reasoning": {"claude": 9, "codex": 8, "qwen": 6, "opencode": 4,
38
+ "generic": 3, "aider": 1},
39
+ "vision": {"codex": 10, "claude": 8, "qwen": 5, "opencode": 2,
40
+ "generic": 1, "aider": 0},
41
+ }
42
+
43
+ _TIER_SCORE = {
44
+ "easy": {"budget": 18.0, "standard": 8.0, "premium": -6.0},
45
+ "default": {"budget": 2.0, "standard": 6.0, "premium": 5.0},
46
+ "hard": {"budget": -8.0, "standard": 4.0, "premium": 12.0},
47
+ }
48
+
49
+
50
+ def task_dimension(task_type, role=""):
51
+ """把预置任务与步骤角色归一为 writing/coding/reasoning/vision。"""
52
+ ttype = str(task_type or "").strip().lower()
53
+ role = str(role or "").strip().lower()
54
+ if ttype in ("writing", "coding", "reasoning", "vision"):
55
+ return ttype
56
+ if role in ("plan", "review", "critique", "selector", "qa") \
57
+ or "review" in role or "critique" in role:
58
+ return "reasoning"
59
+ return TYPE_DIMENSIONS.get(ttype, "reasoning")
60
+
61
+
62
+ def agent_affinity(kind, task_type, role=""):
63
+ """CLI 类型对任务维度的温和偏好;只作加分,不覆盖健康与历史信号。"""
64
+ dim = task_dimension(task_type, role)
65
+ score = float((_KIND_AFFINITY.get(dim) or {}).get(kind, 0))
66
+ if role == "review" or "critique" in str(role or ""):
67
+ score += {"claude": 4, "codex": 3, "qwen": 2}.get(kind, 0)
68
+ return score, "%s 匹配 %+.1f" % (dim, score)
69
+
70
+
71
+ def _model_meta(provider, model):
72
+ for item in (provider or {}).get("models") or []:
73
+ if isinstance(item, dict) and item.get("name") == model:
74
+ return item
75
+ return {}
76
+
77
+
78
+ def _price_score(pricing, model, difficulty):
79
+ price = (pricing or {}).get(model) or {}
80
+ try:
81
+ # 输出通常比输入贵且更影响整步成本,按 2 倍权重估算。
82
+ blended = max(0.0, float(price.get("in") or 0.0)
83
+ + 2.0 * float(price.get("out") or 0.0))
84
+ except (TypeError, ValueError):
85
+ return 0.0, "价格未知"
86
+ if blended <= 0:
87
+ return 0.0, "价格未知"
88
+ magnitude = max(0.0, math.log10(blended + 1.0))
89
+ score = -min(18.0, magnitude * (7.0 if difficulty == "easy" else 2.0))
90
+ return score, "估算价 %.3g(%+.1f)" % (blended, score)
91
+
92
+
93
+ def _online_model_bonus(entry, task_type, role):
94
+ """把真实运行指标作为模型链软信号,硬约束仍由调用方负责。"""
95
+ try:
96
+ from . import usage
97
+ metrics = usage.routing_stats(
98
+ task_type=task_type, role=role,
99
+ provider=entry.get("provider_id") or "",
100
+ model=entry.get("model") or "")
101
+ samples = int(metrics.get("samples") or 0)
102
+ if not samples:
103
+ return 0.0, ""
104
+ rate = float(metrics.get("success_rate") or 0.0)
105
+ success_score = max(-6.0, min(6.0, (rate - 0.75) * 18.0))
106
+ p95 = max(0.0, float(metrics.get("p95_duration_s") or 0.0))
107
+ # 模型链也必须让真实延迟影响排序,不能让 P95 数分钟的链靠静态质量分
108
+ # 长期压在十几秒的健康链前面。
109
+ latency_score = -min(12.0, max(0.0, (p95 - 30.0) / 8.0))
110
+ cost = max(0.0, float(metrics.get("avg_cost_usd") or 0.0))
111
+ cost_score = -min(3.0, max(0.0, (cost - 0.01) / 0.01))
112
+ total = round(success_score + latency_score + cost_score, 2)
113
+ success_samples = int(metrics.get("success_samples") or samples)
114
+ reason = ("在线 %d/%d 验收成功(%+.1f),P95 %.1fs(%+.1f),均价 $%.4f(%+.1f)"
115
+ % (int(metrics.get("successes") or 0), success_samples,
116
+ success_score, p95, latency_score, cost, cost_score))
117
+ return total, reason
118
+ except Exception:
119
+ return 0.0, ""
120
+
121
+
122
+ def _bench_bonus(entry):
123
+ """评测基准台实测分软信号(promptfoo 式样题榜单;14 天新鲜度,过期记 0)。
124
+
125
+ 与在线信号同理念:软加减分,硬约束(健康/冷却/协议)仍由调用方负责;
126
+ 无数据不猜分——没上过评测台的候选保持原排序。
127
+ """
128
+ try:
129
+ from . import benchstore
130
+ return benchstore.bonus_for(entry.get("provider_id") or "",
131
+ entry.get("model") or "")
132
+ except Exception:
133
+ return 0.0, ""
134
+
135
+
136
+ def score_model_entry(entry, providers, pricing, difficulty, task_type="", role=""):
137
+ """给已通过硬约束的模型链条目评分,返回 (score, explanation)。"""
138
+ provider = (providers or {}).get(entry.get("provider_id")) \
139
+ or entry.get("provider") or {}
140
+ model = entry.get("model") or ""
141
+ meta = _model_meta(provider, model)
142
+ try:
143
+ priority = max(1, int(meta.get("priority") or 99))
144
+ except (TypeError, ValueError):
145
+ priority = 99
146
+ quality = 0.0 if priority == 99 else max(-8.0, 20.0 - (priority - 1) * 4.0)
147
+ tier = meta.get("tier") or provider.get("tier") or "standard"
148
+ tier_score = (_TIER_SCORE.get(difficulty) or {}).get(tier, 0.0)
149
+ price_score, price_reason = _price_score(pricing, model, difficulty)
150
+ dim = task_dimension(task_type, role)
151
+ strengths = provider.get("strengths") if isinstance(provider.get("strengths"), list) else []
152
+ strength_score = 10.0 if dim in strengths else 0.0
153
+ if difficulty == "easy":
154
+ quality *= 0.25
155
+ elif difficulty != "hard":
156
+ # 普通任务兼顾质量、成本与能力匹配;是否真的重排由调用方决定,
157
+ # 因而手工链仍可保持原顺序,自动推荐则能使用这组平衡分。
158
+ quality *= 0.65
159
+ vision_score = 0.0
160
+ if dim == "vision":
161
+ vision_score = 24.0 if meta.get("image_in") else -24.0
162
+ online_score, online_reason = _online_model_bonus(entry, task_type, role)
163
+ bench_score, bench_reason = _bench_bonus(entry)
164
+ total = (quality + tier_score + price_score + strength_score + vision_score
165
+ + online_score + bench_score)
166
+ reason = ("质量 %+.1f,档位 %s %+.1f,%s,能力 %s %+.1f%s%s%s%s"
167
+ % (quality, tier, tier_score, price_reason, dim,
168
+ strength_score + vision_score,
169
+ "," if online_reason else "", online_reason,
170
+ "," if bench_reason else "", bench_reason))
171
+ return round(total, 2), reason
172
+
173
+
174
+ def rank_model_entries(entries, providers, pricing, difficulty,
175
+ task_type="", role="", force=False):
176
+ """稳定排序模型链并返回脱敏决策明细;default 难度保持人工顺序。"""
177
+ rows = []
178
+ for index, entry in enumerate(entries or []):
179
+ score, reason = score_model_entry(
180
+ entry, providers, pricing, difficulty, task_type, role)
181
+ rows.append((score, index, entry, reason))
182
+ if difficulty in ("easy", "hard") or force:
183
+ rows.sort(key=lambda row: (-row[0], row[1]))
184
+ ranked = [row[2] for row in rows]
185
+ decisions = [{"provider_id": row[2].get("provider_id") or "",
186
+ "provider": (row[2].get("provider") or {}).get("name") or "",
187
+ "model": row[2].get("model") or "",
188
+ "score": row[0], "reason": row[3]}
189
+ for row in rows]
190
+ return ranked, decisions
@@ -1,128 +1,128 @@
1
- # -*- coding: utf-8 -*-
2
- """调度事件台账:记录脱敏的候选、决策和结果,支持按运行回放。"""
3
- from __future__ import annotations
4
-
5
- import json
6
- import hashlib
7
- import threading
8
- import time
9
-
10
- from . import paths
11
- from .redact import scrub_text
12
-
13
- LOCK = threading.RLock()
14
-
15
-
16
- def _dispatch_dir():
17
- # 读取动态 DATA_DIR,测试和多实例运行可在启动后重定向数据目录。
18
- return paths.DATA_DIR / "dispatch"
19
-
20
-
21
- def _month_file(day):
22
- return _dispatch_dir() / ("dispatch-%s.jsonl" % day[:7].replace("-", ""))
23
-
24
-
25
- def _text(value, limit=240):
26
- value = str(value or "").replace("\r", " ").replace("\n", " ")
27
- return scrub_text(value, limit=limit)
28
-
29
-
30
- def _candidate(row):
31
- if not isinstance(row, dict):
32
- return {}
33
- return {"agent_id": _text(row.get("agent_id"), 64),
34
- "label": _text(row.get("label"), 80),
35
- "kind": _text(row.get("kind"), 32),
36
- "score": row.get("score", 0),
37
- "reason": _text(row.get("reason"), 240),
38
- "order": row.get("order", 0)}
39
-
40
-
41
- def record_event(run_id="", task_id="", task_type="", difficulty="", role="",
42
- phase="selected", selected="", participants=(), candidates=(),
43
- fallback=(), selection_reason="", result="", verify_pass=None,
44
- review_pass=None, trace_id="", span_id="", parent_event_id=""):
45
- """追加一条事件;仅保存路由元数据,不保存正文、提示词、密钥或文件内容。"""
46
- try:
47
- day = time.strftime("%Y-%m-%d")
48
- event = {
49
- "ts": time.strftime("%Y-%m-%d %H:%M:%S"),
50
- "run_id": _text(run_id, 64), "task_id": _text(task_id, 64),
51
- "task_type": _text(task_type, 32), "difficulty": _text(difficulty, 16),
52
- "role": _text(role, 40), "phase": _text(phase, 16),
53
- "selected": _text(selected, 64),
54
- "participants": [_text(x.get("id") if isinstance(x, dict) else x, 64)
55
- for x in (participants or ())],
56
- "candidates": [_candidate(x) for x in (candidates or ()) if isinstance(x, dict)],
57
- "fallback": [_text(x, 64) for x in (fallback or ())],
58
- "selection_reason": _text(selection_reason, 400),
59
- "result": _text(result, 32),
60
- }
61
- # Trace identifiers are routing metadata only. They deliberately do
62
- # not carry prompt/output contents and remain optional for old callers.
63
- if run_id:
64
- from . import tracing
65
- event["trace_id"] = _text(trace_id or tracing.trace_id(run_id), 64)
66
- if span_id:
67
- event["span_id"] = _text(span_id, 32)
68
- if parent_event_id:
69
- event["parent_event_id"] = _text(parent_event_id, 64)
70
- event["event_id"] = hashlib.sha256(
71
- ("%s|%s|%s|%s|%s|%s" % (
72
- time.time_ns(), run_id, phase, role, selected, result
73
- )).encode("utf-8")
74
- ).hexdigest()[:24]
75
- if verify_pass is not None:
76
- event["verify_pass"] = bool(verify_pass)
77
- if review_pass is not None:
78
- event["review_pass"] = bool(review_pass)
79
- with LOCK:
80
- _dispatch_dir().mkdir(parents=True, exist_ok=True)
81
- with open(_month_file(day), "a", encoding="utf-8") as fh:
82
- fh.write(json.dumps(event, ensure_ascii=False) + "\n")
83
- return event
84
- except Exception:
85
- return None
86
-
87
-
88
- def _iter_events(run_id="", task_type="", limit=100):
89
- """从新到旧读取,满足 limit 即停,避免回放请求解析全部历史。"""
90
- try:
91
- directory = _dispatch_dir()
92
- files = sorted(directory.glob("dispatch-*.jsonl"), reverse=True) \
93
- if directory.is_dir() else []
94
- except Exception:
95
- return []
96
- events = []
97
- for path in files:
98
- try:
99
- lines = path.read_text(encoding="utf-8", errors="replace").splitlines()
100
- for line in reversed(lines):
101
- if not line.strip().startswith("{"):
102
- continue
103
- try:
104
- item = json.loads(line)
105
- except Exception:
106
- continue
107
- if not isinstance(item, dict):
108
- continue
109
- if run_id and item.get("run_id") != run_id:
110
- continue
111
- if task_type and item.get("task_type") != task_type:
112
- continue
113
- events.append(item)
114
- if len(events) >= limit:
115
- return events
116
- except Exception:
117
- continue
118
- return events
119
-
120
-
121
- def replay(run_id="", task_type="", limit=100):
122
- """返回最新的脱敏调度事件,支持按 run_id/task_type 筛选。"""
123
- try:
124
- limit = max(1, min(1000, int(limit)))
125
- except (TypeError, ValueError):
126
- limit = 100
127
- rid, ttype = _text(run_id, 64), _text(task_type, 32)
128
- return _iter_events(rid, ttype, limit)
1
+ # -*- coding: utf-8 -*-
2
+ """调度事件台账:记录脱敏的候选、决策和结果,支持按运行回放。"""
3
+ from __future__ import annotations
4
+
5
+ import json
6
+ import hashlib
7
+ import threading
8
+ import time
9
+
10
+ from . import paths
11
+ from .redact import scrub_text
12
+
13
+ LOCK = threading.RLock()
14
+
15
+
16
+ def _dispatch_dir():
17
+ # 读取动态 DATA_DIR,测试和多实例运行可在启动后重定向数据目录。
18
+ return paths.DATA_DIR / "dispatch"
19
+
20
+
21
+ def _month_file(day):
22
+ return _dispatch_dir() / ("dispatch-%s.jsonl" % day[:7].replace("-", ""))
23
+
24
+
25
+ def _text(value, limit=240):
26
+ value = str(value or "").replace("\r", " ").replace("\n", " ")
27
+ return scrub_text(value, limit=limit)
28
+
29
+
30
+ def _candidate(row):
31
+ if not isinstance(row, dict):
32
+ return {}
33
+ return {"agent_id": _text(row.get("agent_id"), 64),
34
+ "label": _text(row.get("label"), 80),
35
+ "kind": _text(row.get("kind"), 32),
36
+ "score": row.get("score", 0),
37
+ "reason": _text(row.get("reason"), 240),
38
+ "order": row.get("order", 0)}
39
+
40
+
41
+ def record_event(run_id="", task_id="", task_type="", difficulty="", role="",
42
+ phase="selected", selected="", participants=(), candidates=(),
43
+ fallback=(), selection_reason="", result="", verify_pass=None,
44
+ review_pass=None, trace_id="", span_id="", parent_event_id=""):
45
+ """追加一条事件;仅保存路由元数据,不保存正文、提示词、密钥或文件内容。"""
46
+ try:
47
+ day = time.strftime("%Y-%m-%d")
48
+ event = {
49
+ "ts": time.strftime("%Y-%m-%d %H:%M:%S"),
50
+ "run_id": _text(run_id, 64), "task_id": _text(task_id, 64),
51
+ "task_type": _text(task_type, 32), "difficulty": _text(difficulty, 16),
52
+ "role": _text(role, 40), "phase": _text(phase, 16),
53
+ "selected": _text(selected, 64),
54
+ "participants": [_text(x.get("id") if isinstance(x, dict) else x, 64)
55
+ for x in (participants or ())],
56
+ "candidates": [_candidate(x) for x in (candidates or ()) if isinstance(x, dict)],
57
+ "fallback": [_text(x, 64) for x in (fallback or ())],
58
+ "selection_reason": _text(selection_reason, 400),
59
+ "result": _text(result, 32),
60
+ }
61
+ # Trace identifiers are routing metadata only. They deliberately do
62
+ # not carry prompt/output contents and remain optional for old callers.
63
+ if run_id:
64
+ from . import tracing
65
+ event["trace_id"] = _text(trace_id or tracing.trace_id(run_id), 64)
66
+ if span_id:
67
+ event["span_id"] = _text(span_id, 32)
68
+ if parent_event_id:
69
+ event["parent_event_id"] = _text(parent_event_id, 64)
70
+ event["event_id"] = hashlib.sha256(
71
+ ("%s|%s|%s|%s|%s|%s" % (
72
+ time.time_ns(), run_id, phase, role, selected, result
73
+ )).encode("utf-8")
74
+ ).hexdigest()[:24]
75
+ if verify_pass is not None:
76
+ event["verify_pass"] = bool(verify_pass)
77
+ if review_pass is not None:
78
+ event["review_pass"] = bool(review_pass)
79
+ with LOCK:
80
+ _dispatch_dir().mkdir(parents=True, exist_ok=True)
81
+ with open(_month_file(day), "a", encoding="utf-8") as fh:
82
+ fh.write(json.dumps(event, ensure_ascii=False) + "\n")
83
+ return event
84
+ except Exception:
85
+ return None
86
+
87
+
88
+ def _iter_events(run_id="", task_type="", limit=100):
89
+ """从新到旧读取,满足 limit 即停,避免回放请求解析全部历史。"""
90
+ try:
91
+ directory = _dispatch_dir()
92
+ files = sorted(directory.glob("dispatch-*.jsonl"), reverse=True) \
93
+ if directory.is_dir() else []
94
+ except Exception:
95
+ return []
96
+ events = []
97
+ for path in files:
98
+ try:
99
+ lines = path.read_text(encoding="utf-8", errors="replace").splitlines()
100
+ for line in reversed(lines):
101
+ if not line.strip().startswith("{"):
102
+ continue
103
+ try:
104
+ item = json.loads(line)
105
+ except Exception:
106
+ continue
107
+ if not isinstance(item, dict):
108
+ continue
109
+ if run_id and item.get("run_id") != run_id:
110
+ continue
111
+ if task_type and item.get("task_type") != task_type:
112
+ continue
113
+ events.append(item)
114
+ if len(events) >= limit:
115
+ return events
116
+ except Exception:
117
+ continue
118
+ return events
119
+
120
+
121
+ def replay(run_id="", task_type="", limit=100):
122
+ """返回最新的脱敏调度事件,支持按 run_id/task_type 筛选。"""
123
+ try:
124
+ limit = max(1, min(1000, int(limit)))
125
+ except (TypeError, ValueError):
126
+ limit = 100
127
+ rid, ttype = _text(run_id, 64), _text(task_type, 32)
128
+ return _iter_events(rid, ttype, limit)