niceeval 0.6.2 → 0.7.0

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 (216) hide show
  1. package/INDEX.md +23 -23
  2. package/README.zh.md +6 -6
  3. package/dist/agents/types.d.ts +2 -2
  4. package/dist/i18n/zh-CN.d.ts +3 -3
  5. package/dist/report/aggregate.d.ts +32 -26
  6. package/dist/report/aggregate.js +157 -76
  7. package/dist/report/built-in/index.d.ts +2 -0
  8. package/dist/report/built-in/index.js +8 -0
  9. package/dist/report/components.d.ts +91 -164
  10. package/dist/report/components.js +377 -114
  11. package/dist/report/compute.d.ts +86 -73
  12. package/dist/report/compute.js +592 -432
  13. package/dist/report/flag.d.ts +28 -17
  14. package/dist/report/flag.js +86 -16
  15. package/dist/report/format.d.ts +11 -11
  16. package/dist/report/format.js +17 -15
  17. package/dist/report/index.d.ts +16 -17
  18. package/dist/report/index.js +20 -22
  19. package/dist/report/load.js +3 -2
  20. package/dist/report/locale.d.ts +49 -34
  21. package/dist/report/locale.js +106 -58
  22. package/dist/report/metrics.d.ts +10 -3
  23. package/dist/report/metrics.js +46 -12
  24. package/dist/report/primitives.d.ts +42 -15
  25. package/dist/report/primitives.js +135 -26
  26. package/dist/report/react/AttemptList.d.ts +10 -8
  27. package/dist/report/react/AttemptList.js +18 -10
  28. package/dist/report/react/DeltaTable.js +19 -18
  29. package/dist/report/react/EvalList.d.ts +3 -3
  30. package/dist/report/react/EvalList.js +0 -0
  31. package/dist/report/react/ExperimentComparison.d.ts +4 -2
  32. package/dist/report/react/ExperimentComparison.js +5 -4
  33. package/dist/report/react/ExperimentList.d.ts +3 -3
  34. package/dist/report/react/ExperimentList.js +16 -15
  35. package/dist/report/react/MetricBars.js +5 -4
  36. package/dist/report/react/MetricLine.js +12 -5
  37. package/dist/report/react/MetricMatrix.js +1 -1
  38. package/dist/report/react/MetricScatter.js +54 -17
  39. package/dist/report/react/MetricTable.js +2 -12
  40. package/dist/report/react/ScopeSummary.d.ts +10 -0
  41. package/dist/report/react/ScopeSummary.js +17 -0
  42. package/dist/report/react/Scoreboard.js +6 -6
  43. package/dist/report/react/cell.js +2 -2
  44. package/dist/report/react/fixtures.d.ts +5 -9
  45. package/dist/report/react/fixtures.js +105 -149
  46. package/dist/report/react/index.d.ts +15 -5
  47. package/dist/report/react/index.js +18 -7
  48. package/dist/report/report.d.ts +137 -20
  49. package/dist/report/report.js +261 -34
  50. package/dist/report/text/faces.d.ts +17 -19
  51. package/dist/report/text/faces.js +225 -157
  52. package/dist/report/text/plot.js +1 -1
  53. package/dist/report/text/table.js +2 -2
  54. package/dist/report/tree.d.ts +90 -40
  55. package/dist/report/tree.js +252 -94
  56. package/dist/report/types.d.ts +245 -300
  57. package/dist/report/types.js +4 -3
  58. package/dist/report/web.d.ts +21 -5
  59. package/dist/report/web.js +42 -16
  60. package/dist/results/select.d.ts +38 -16
  61. package/dist/results/select.js +73 -25
  62. package/dist/results/types.d.ts +38 -14
  63. package/dist/shared/aggregate.d.ts +3 -2
  64. package/dist/shared/aggregate.js +5 -4
  65. package/docs-site/zh/README.md +44 -0
  66. package/docs-site/zh/examples/ai-agent-application.mdx +63 -0
  67. package/docs-site/zh/examples/coding-agent-extensions.mdx +57 -0
  68. package/docs-site/zh/examples/index.mdx +50 -0
  69. package/docs-site/zh/{concepts → explanation}/adapter.mdx +11 -11
  70. package/docs-site/zh/{concepts → explanation}/assert.mdx +7 -7
  71. package/docs-site/zh/{concepts → explanation}/drive.mdx +8 -8
  72. package/docs-site/zh/{concepts → explanation}/evals.mdx +4 -4
  73. package/docs-site/zh/{concepts → explanation}/experiment.mdx +8 -8
  74. package/docs-site/zh/{concepts → explanation}/hitl.mdx +8 -8
  75. package/docs-site/zh/{concepts → explanation}/judge.mdx +5 -5
  76. package/docs-site/zh/{concepts → explanation}/overview.mdx +5 -5
  77. package/docs-site/zh/{guides → explanation}/runner.mdx +1 -1
  78. package/docs-site/zh/{concepts → explanation}/tier.mdx +6 -6
  79. package/docs-site/zh/{guides → how-to}/agent-feedback-loop.mdx +7 -7
  80. package/docs-site/zh/{guides → how-to}/authoring.mdx +2 -2
  81. package/docs-site/zh/{guides → how-to}/connect-otel.mdx +6 -6
  82. package/docs-site/zh/{guides → how-to}/connect-your-agent.mdx +18 -18
  83. package/docs-site/zh/{guides → how-to}/custom-reports.mdx +6 -6
  84. package/docs-site/zh/{guides → how-to}/experiments.mdx +3 -3
  85. package/docs-site/zh/{guides → how-to}/publish-report.mdx +2 -2
  86. package/docs-site/zh/{guides → how-to}/sandbox-agent.mdx +2 -2
  87. package/docs-site/zh/{guides → how-to}/sandbox-providers.mdx +1 -1
  88. package/docs-site/zh/{guides → how-to}/viewing-results.mdx +6 -6
  89. package/docs-site/zh/{guides → how-to}/write-experiment.mdx +3 -3
  90. package/docs-site/zh/{guides → how-to}/write-send.mdx +13 -13
  91. package/docs-site/zh/index.mdx +23 -25
  92. package/docs-site/zh/introduction.mdx +8 -8
  93. package/docs-site/zh/reference/builtin-agents.mdx +5 -5
  94. package/docs-site/zh/reference/capabilities.mdx +6 -6
  95. package/docs-site/zh/reference/cli.mdx +9 -7
  96. package/docs-site/zh/reference/define-agent.mdx +1 -1
  97. package/docs-site/zh/reference/events.mdx +3 -3
  98. package/docs-site/zh/{guides → reference}/official-adapters.mdx +7 -7
  99. package/docs-site/zh/{guides → reference}/report-components.mdx +5 -5
  100. package/docs-site/zh/{guides → reference}/results-data.mdx +5 -5
  101. package/docs-site/zh/{guides → troubleshooting}/debug-sandbox.mdx +2 -2
  102. package/docs-site/zh/{guides → troubleshooting}/debugging.mdx +4 -2
  103. package/docs-site/zh/{quickstart.mdx → tutorials/quickstart.mdx} +5 -17
  104. package/package.json +6 -2
  105. package/src/agents/index.ts +2 -2
  106. package/src/agents/openai-compat.ts +1 -1
  107. package/src/agents/streaming.ts +2 -2
  108. package/src/agents/types.ts +3 -3
  109. package/src/cli.ts +42 -23
  110. package/src/context/context.ts +1 -1
  111. package/src/context/session.test.ts +1 -1
  112. package/src/context/session.ts +1 -1
  113. package/src/i18n/en.ts +18 -16
  114. package/src/i18n/zh-CN.ts +16 -15
  115. package/src/report/aggregate.ts +175 -87
  116. package/src/report/built-in/index.tsx +9 -0
  117. package/src/report/components.tsx +625 -285
  118. package/src/report/compute.ts +717 -515
  119. package/src/report/dual-render.test.tsx +738 -1148
  120. package/src/report/flag.ts +97 -33
  121. package/src/report/format.ts +18 -22
  122. package/src/report/index.ts +113 -58
  123. package/src/report/load.ts +3 -2
  124. package/src/report/locale.ts +120 -69
  125. package/src/report/metrics.ts +42 -12
  126. package/src/report/primitives.tsx +190 -45
  127. package/src/report/react/AttemptList.tsx +32 -20
  128. package/src/report/react/DeltaTable.tsx +63 -45
  129. package/src/report/react/EvalList.tsx +0 -0
  130. package/src/report/react/ExperimentComparison.tsx +12 -7
  131. package/src/report/react/ExperimentList.tsx +38 -26
  132. package/src/report/react/MetricBars.tsx +5 -4
  133. package/src/report/react/MetricLine.tsx +13 -8
  134. package/src/report/react/MetricMatrix.tsx +2 -2
  135. package/src/report/react/MetricScatter.tsx +74 -20
  136. package/src/report/react/MetricTable.tsx +4 -76
  137. package/src/report/react/ScopeSummary.tsx +86 -0
  138. package/src/report/react/Scoreboard.tsx +28 -10
  139. package/src/report/react/cell.tsx +2 -2
  140. package/src/report/react/enhance.js +57 -5
  141. package/src/report/react/fixtures.ts +109 -156
  142. package/src/report/react/index.tsx +24 -39
  143. package/src/report/react/render.test.tsx +139 -104
  144. package/src/report/react/styles.css +181 -91
  145. package/src/report/report.test.ts +761 -1031
  146. package/src/report/report.ts +425 -47
  147. package/src/report/text/faces.ts +257 -164
  148. package/src/report/text/plot.ts +1 -1
  149. package/src/report/text/table.ts +2 -2
  150. package/src/report/tree.ts +362 -104
  151. package/src/report/types.ts +257 -287
  152. package/src/report/web.ts +63 -20
  153. package/src/results/attempt-evidence.test.ts +4 -4
  154. package/src/results/attempt-evidence.ts +5 -5
  155. package/src/results/copy.ts +6 -6
  156. package/src/results/host-equivalence.test.ts +26 -14
  157. package/src/results/index.ts +10 -4
  158. package/src/results/open.ts +8 -4
  159. package/src/results/results.test.ts +4 -3
  160. package/src/results/select.ts +104 -34
  161. package/src/results/types.ts +36 -14
  162. package/src/runner/feedback/human.test.ts +1 -1
  163. package/src/runner/run.ts +1 -1
  164. package/src/sandbox/cli-commands.ts +2 -2
  165. package/src/scoring/judge.test.ts +1 -1
  166. package/src/shared/aggregate.ts +5 -4
  167. package/src/show/compose.ts +50 -67
  168. package/src/show/index.ts +107 -56
  169. package/src/show/render.ts +43 -27
  170. package/src/show/report-host.test.ts +188 -0
  171. package/src/show/report-host.ts +375 -0
  172. package/src/show/show.test.ts +86 -36
  173. package/src/view/app/App.test.tsx +69 -0
  174. package/src/view/app/App.tsx +144 -48
  175. package/src/view/app/components/AttemptModal.tsx +324 -63
  176. package/src/view/app/components/CodeView.tsx +10 -4
  177. package/src/view/app/i18n.ts +31 -17
  178. package/src/view/app/main.tsx +13 -8
  179. package/src/view/app/pages/{RunsPage.tsx → AttemptsPage.tsx} +6 -6
  180. package/src/view/app/types.ts +4 -1
  181. package/src/view/client-dist/app.css +1 -1
  182. package/src/view/client-dist/app.js +14 -14
  183. package/src/view/data.test.ts +9 -3
  184. package/src/view/data.ts +145 -49
  185. package/src/view/index.ts +48 -44
  186. package/src/view/server.ts +35 -15
  187. package/src/view/shared/types.ts +34 -5
  188. package/src/view/styles.css +224 -0
  189. package/src/view/view-report.test.ts +161 -57
  190. package/dist/report/built-ins/experiment-comparison.d.ts +0 -39
  191. package/dist/report/built-ins/experiment-comparison.js +0 -119
  192. package/dist/report/built-ins/index.d.ts +0 -2
  193. package/dist/report/built-ins/index.js +0 -2
  194. package/dist/report/react/GroupSummary.d.ts +0 -8
  195. package/dist/report/react/GroupSummary.js +0 -8
  196. package/dist/report/react/RunOverview.d.ts +0 -8
  197. package/dist/report/react/RunOverview.js +0 -12
  198. package/docs-site/zh/example/ai-agent-application.mdx +0 -152
  199. package/docs-site/zh/example/claude-code-codex-plugin.mdx +0 -167
  200. package/docs-site/zh/example/claude-code-codex-skill.mdx +0 -152
  201. package/docs-site/zh/example/showcase.mdx +0 -39
  202. package/src/report/built-in-user-parity.test.tsx +0 -597
  203. package/src/report/built-ins/experiment-comparison.tsx +0 -179
  204. package/src/report/built-ins/index.ts +0 -7
  205. package/src/report/react/GroupSummary.tsx +0 -66
  206. package/src/report/react/RunOverview.tsx +0 -109
  207. /package/docs-site/zh/{example/tier1-ai-sdk-v7.mdx → examples/integrations/ai-sdk-v7.mdx} +0 -0
  208. /package/docs-site/zh/{example/tier1-claude-sdk.mdx → examples/integrations/claude-sdk.mdx} +0 -0
  209. /package/docs-site/zh/{example/tier1-codex-sdk.mdx → examples/integrations/codex-sdk.mdx} +0 -0
  210. /package/docs-site/zh/{example/tier1-langgraph.mdx → examples/integrations/langgraph.mdx} +0 -0
  211. /package/docs-site/zh/{example/tier1-pi-sdk.mdx → examples/integrations/pi-sdk.mdx} +0 -0
  212. /package/docs-site/zh/{guides → how-to}/ci-integration.mdx +0 -0
  213. /package/docs-site/zh/{guides → how-to}/dataset-fanout.mdx +0 -0
  214. /package/docs-site/zh/{guides → how-to}/fixtures.mdx +0 -0
  215. /package/docs-site/zh/{guides → how-to}/reporters.mdx +0 -0
  216. /package/docs-site/zh/{guides → how-to}/scoring-guide.mdx +0 -0
@@ -1,40 +1,39 @@
1
- // 两级聚合引擎:去重 → 按维度分组 → 组内按 (eval × 快照) 折叠(perEval)→ 跨题折叠(across)→ MetricCell。
1
+ // 两级聚合引擎:去重 → 按维度分组 → 组内按 (experiment × eval) 折叠(perEval)→ 跨题折叠
2
+ // (acrossEvals)→ MetricCell(docs/feature/reports/architecture.md「指标聚合不变量」、
3
+ // docs/feature/reports/library/metrics.md「公开计算模型」)。
2
4
  //
3
5
  // 为什么是两级:earlyExit 默认开,失败的题天然比通过的题样本多;平铺求均值会把分数
4
6
  // 和重试策略纠缠在一起(eval A=[1]、eval B=[0,0,0] 平铺 = 0.25,两级宏平均 = 0.5)。
5
7
  // 自定义维度把同一道题的 attempt 分进不同组时,第一级折叠发生在各组内部。
6
8
 
7
9
  import { dedupeAttempts } from "../results/select.ts";
8
- import type { AttemptHandle, Selection, SelectionWarning, Snapshot } from "../results/types.ts";
10
+ import type { AttemptHandle, Scope, ScopeWarning, Snapshot } from "../results/types.ts";
9
11
  import { encodeAttemptLocator, type AttemptLocator } from "../results/locator.ts";
10
12
  import type {
11
13
  Aggregator,
12
- AxisInput,
13
- ConfigRef,
14
14
  DimensionInput,
15
+ DimensionRef,
15
16
  Metric,
16
17
  MetricCell,
17
18
  MetricColumn,
18
- FlagRef,
19
+ NumericAxis,
20
+ ReportInput,
19
21
  } from "./types.ts";
20
- import { formatMetricValue } from "./format.ts";
22
+ import { flagValueOf, runConfigValueOf } from "./flag.ts";
23
+ import { formatMetricValue, localizedDisplay } from "./format.ts";
24
+ import { localeText, type LocalizedText } from "./locale.ts";
21
25
  import { evalPrefixPredicate } from "../shared/aggregate.ts";
22
26
 
23
27
  // 复合键分隔符:NUL 不会出现在 eval id / experimentId / ISO 时间里,拼接键不会串味
24
28
  const KEY_SEP = "\u0000";
25
29
 
26
- /** flag 未声明时的组名:不猜,如实归一组。 */
27
- export const FLAG_UNSET = "(unset)";
28
-
29
- /** 计算函数的第一参:Selection(warnings 随行)或手工挑的快照数组(没有挑选过程,自然无警告)。 */
30
- export type SnapshotsInput = Selection | Snapshot[];
31
-
32
- export function resolveInput(input: SnapshotsInput): {
33
- snapshots: Snapshot[];
34
- warnings: SelectionWarning[];
30
+ export function resolveInput(input: ReportInput): {
31
+ snapshots: readonly Snapshot[];
32
+ warnings: readonly ScopeWarning[];
35
33
  } {
36
- if (Array.isArray(input)) return { snapshots: input, warnings: [] };
37
- return { snapshots: input.snapshots, warnings: input.warnings };
34
+ if (Array.isArray(input)) return { snapshots: input as readonly Snapshot[], warnings: [] };
35
+ const scope = input as Scope;
36
+ return { snapshots: scope.snapshots, warnings: scope.warnings };
38
37
  }
39
38
 
40
39
  /** 展平后的一条样本:attempt + 它所属的快照(维度解析与题级折叠都需要快照身份)。 */
@@ -51,16 +50,19 @@ export function evalIdOf(item: Item): string {
51
50
  return item.attempt.evalId || item.attempt.result.id;
52
51
  }
53
52
 
54
- /** 快照键:与 view Compare 同口径的 "<experimentId> @ <startedAt>"。 */
53
+ /** 快照键:"<experimentId> @ <startedAt>"("snapshot" 维度与手挑快照数组的对比用)。 */
55
54
  export function snapshotKeyOf(snapshot: Snapshot): string {
56
55
  return `${snapshot.experimentId} @ ${snapshot.startedAt}`;
57
56
  }
58
57
 
58
+ /** 一组 Item 的 eval 全身份键:experimentId + eval id(聚合中的题级身份始终是这一对)。 */
59
+ export function fullEvalKey(item: Item): string {
60
+ return `${experimentIdOf(item)}${KEY_SEP}${evalIdOf(item)}`;
61
+ }
62
+
59
63
  /**
60
64
  * 一条 Item 的 AttemptLocator:真实读取路径(openResults() 产出的 handle)恒有
61
- * `attempt.locator`;手工构造的测试 fixture 若省略它,按当前身份元组兜底算一份
62
- * (与 `loadAttemptEvidence`、`open.ts` 的回填同一口径)——只服务这类场景,不改变真实
63
- * 读取路径的行为。
65
+ * `attempt.locator`;手工构造的测试 fixture 若省略它,按当前身份元组兜底算一份。
64
66
  */
65
67
  export function locatorOf(item: Item): AttemptLocator {
66
68
  return (
@@ -79,7 +81,7 @@ export function locatorOf(item: Item): AttemptLocator {
79
81
  * (experimentId, evalId, attempt, startedAt))。missing-startedAt 的警告不透出:
80
82
  * 官方产出永不缺 startedAt,缺失只可能来自 legacy 落盘,「不去重、如实保留重复」即终稿。
81
83
  */
82
- export function collectItems(snapshots: Snapshot[]): Item[] {
84
+ export function collectItems(snapshots: readonly Snapshot[]): Item[] {
83
85
  const snapshotByAttempt = new Map<AttemptHandle, Snapshot>();
84
86
  const flattened: AttemptHandle[] = [];
85
87
  for (const snapshot of snapshots) {
@@ -95,9 +97,9 @@ export function collectItems(snapshots: Snapshot[]): Item[] {
95
97
 
96
98
  export { evalPrefixPredicate };
97
99
 
98
- export function filterItems(items: Item[], evals?: string | string[]): Item[] {
100
+ export function filterItems(items: Item[], evals?: string | readonly string[]): Item[] {
99
101
  if (evals === undefined) return items;
100
- const match = evalPrefixPredicate(evals);
102
+ const match = evalPrefixPredicate(Array.isArray(evals) ? [...evals] : (evals as string));
101
103
  return items.filter((item) => match(evalIdOf(item)));
102
104
  }
103
105
 
@@ -107,66 +109,52 @@ export function dimensionName(dimension: DimensionInput): string {
107
109
  return typeof dimension === "string" ? dimension : dimension.name;
108
110
  }
109
111
 
110
- /** eval id 的第一段:"algebra/quadratic" → "algebra";没有 "/" 时就是 id 本身。 */
112
+ /** eval id 的完整父路径:"a/b/c" → "a/b";没有 "/" 时取完整 id(与可比组同一条派生规则)。 */
111
113
  export function evalGroupOf(id: string): string {
112
- const slash = id.indexOf("/");
114
+ const slash = id.lastIndexOf("/");
113
115
  return slash === -1 ? id : id.slice(0, slash);
114
116
  }
115
117
 
116
- function isFlagRef(dimension: DimensionInput): dimension is FlagRef {
117
- return typeof dimension === "object" && "kind" in dimension && dimension.kind === "flag";
118
+ function isDimensionRef(dimension: DimensionInput): dimension is DimensionRef {
119
+ return (
120
+ typeof dimension === "object" &&
121
+ "kind" in dimension &&
122
+ ((dimension as DimensionRef).kind === "flag" || (dimension as DimensionRef).kind === "runConfig")
123
+ );
118
124
  }
119
125
 
120
- function isConfigRef(dimension: DimensionInput): dimension is ConfigRef {
121
- return typeof dimension === "object" && "kind" in dimension && dimension.kind === "config";
122
- }
126
+ /** flag / runConfig 未声明时的显示键:内置文案,en / zh-CN 同形。 */
127
+ export const MISSING_GROUP_KEY = localeText("en", "cell.missingValue");
123
128
 
124
- /** experiment 声明的 flags(经 runner 原样透传进持久化字段 ExperimentRunInfo.flags)。 */
125
- function flagsOf(attempt: AttemptHandle): Record<string, unknown> | undefined {
126
- return attempt.result.experiment?.flags;
129
+ /** 对象键递归排序后的稳定 JSON(分组显示键与冲突检测共用)。 */
130
+ function canonicalJson(value: unknown): string {
131
+ if (Array.isArray(value)) return `[${value.map(canonicalJson).join(",")}]`;
132
+ if (typeof value === "object" && value !== null) {
133
+ const keys = Object.keys(value as Record<string, unknown>).sort();
134
+ return `{${keys.map((k) => `${JSON.stringify(k)}:${canonicalJson((value as Record<string, unknown>)[k])}`).join(",")}}`;
135
+ }
136
+ return JSON.stringify(value);
127
137
  }
128
138
 
129
139
  /**
130
- * config() 的取值:读快照的 `ExperimentRunInfo` 投影(快照优先;第三方落盘只把投影拼在
131
- * 条目上时回退 result.experiment),外加桥接到快照顶层权威字段的 `model` / `agent` 两个键
132
- * (与 "model" / "agent" 内置维度同一读法,不另造第二套口径)。未投影 → undefined。
140
+ * flag / runConfig 声明值 → 分组显示键(稳定 JSON 规则):字符串直接显示,其它值用对象键
141
+ * 递归排序后的 JSON,缺失值显示内置文案 `(missing)`。返回 [显示键, 冲突检测用的规范形]。
133
142
  */
134
- function configValueOf(ref: ConfigRef, item: Item): unknown {
135
- if (ref.name === "model") return item.attempt.result.model ?? item.snapshot.model;
136
- if (ref.name === "agent") return item.attempt.result.agent || item.snapshot.agent;
137
- const info = item.snapshot.experiment ?? item.attempt.result.experiment;
138
- // ExperimentRunInfo 是穷尽可序列化投影;按字段名取值,新增投影字段无需改这里
139
- return (info as Record<string, unknown> | undefined)?.[ref.name];
140
- }
141
-
142
- /** flag 声明值 → 组标签:label 函数优先,其余 String();未声明 → FLAG_UNSET。 */
143
- export function flagGroupKey(ref: FlagRef, item: Item): string {
144
- return refGroupKey(ref, flagsOf(item.attempt)?.[ref.name]);
145
- }
146
-
147
- /** config 投影值 → 组标签,规则与 flagGroupKey 一致;未投影 → FLAG_UNSET。 */
148
- export function configGroupKey(ref: ConfigRef, item: Item): string {
149
- return refGroupKey(ref, configValueOf(ref, item));
150
- }
151
-
152
- function refGroupKey(ref: FlagRef | ConfigRef, value: unknown): string {
153
- if (value === undefined) return FLAG_UNSET;
154
- // 声明侧的合法标量就是这三种;非标量投影(sandbox / selectedEvalIds…)如实 JSON 化,不猜
155
- if (typeof ref.label === "function") return ref.label(value as string | number | boolean);
156
- if (typeof value === "object" && value !== null) return JSON.stringify(value);
157
- return String(value);
143
+ export function refDisplayKey(value: unknown): [display: string, canonical: string] {
144
+ if (value === undefined) return [MISSING_GROUP_KEY, "undefined"];
145
+ if (typeof value === "string") return [value, `string:${value}`];
146
+ return [canonicalJson(value), `json:${canonicalJson(value)}`];
158
147
  }
159
148
 
160
- /** flag / config 作轴:要求数值;未声明、未投影或非数值 null(点不画,注脚报数)。 */
161
- export function axisValue(ref: AxisInput, item: Item): number | null {
162
- const value = ref.kind === "flag" ? flagsOf(item.attempt)?.[ref.name] : configValueOf(ref, item);
163
- return typeof value === "number" ? value : null;
149
+ function refValueOf(ref: DimensionRef, item: Item): unknown {
150
+ return ref.kind === "flag"
151
+ ? flagValueOf(item.attempt, ref.name)
152
+ : runConfigValueOf(item.attempt, ref.name as Parameters<typeof runConfigValueOf>[1]);
164
153
  }
165
154
 
166
155
  export function dimensionKey(dimension: DimensionInput, item: Item): string {
167
156
  if (typeof dimension !== "string") {
168
- if (isFlagRef(dimension)) return flagGroupKey(dimension, item);
169
- if (isConfigRef(dimension)) return configGroupKey(dimension, item);
157
+ if (isDimensionRef(dimension)) return refDisplayKey(refValueOf(dimension, item))[0];
170
158
  return dimension.of(item.attempt);
171
159
  }
172
160
  const result = item.attempt.result;
@@ -191,21 +179,42 @@ export function dimensionKey(dimension: DimensionInput, item: Item): string {
191
179
  }
192
180
  }
193
181
 
194
- /** 按维度分组,保持首次出现顺序(无 sort 时表格行序即此序)。 */
182
+ /**
183
+ * 按维度分组;维度 domain 按稳定 key 字典序(Unicode)排列,不让文件扫描顺序渗进报告。
184
+ * flag / runConfig 维度做显示键冲突检测:不同原始值生成同一显示键时报错并要求改用
185
+ * CustomDimension,绝不静默合组。
186
+ */
195
187
  export function groupItems(items: Item[], dimension: DimensionInput): Map<string, Item[]> {
196
188
  const groups = new Map<string, Item[]>();
189
+ const canonicalByDisplay = new Map<string, string>();
190
+ const checkConflicts = typeof dimension !== "string" && isDimensionRef(dimension);
197
191
  for (const item of items) {
198
- const key = dimensionKey(dimension, item);
192
+ let key: string;
193
+ if (checkConflicts) {
194
+ const [display, canonical] = refDisplayKey(refValueOf(dimension as DimensionRef, item));
195
+ const existing = canonicalByDisplay.get(display);
196
+ if (existing === undefined) canonicalByDisplay.set(display, canonical);
197
+ else if (existing !== canonical) {
198
+ throw new Error(
199
+ `Dimension "${dimensionName(dimension)}" maps two different raw values to the same display key "${display}" ` +
200
+ `(e.g. the string "5" and the number 5). Grouping them silently would merge distinct configurations; ` +
201
+ `use a CustomDimension ({ name, of(attempt) }) to define an unambiguous key.`,
202
+ );
203
+ }
204
+ key = display;
205
+ } else {
206
+ key = dimensionKey(dimension, item);
207
+ }
199
208
  const list = groups.get(key);
200
209
  if (list) list.push(item);
201
210
  else groups.set(key, [item]);
202
211
  }
203
- return groups;
212
+ return new Map([...groups.entries()].sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0)));
204
213
  }
205
214
 
206
215
  // ───────────────────────── 聚合 ─────────────────────────
207
216
 
208
- export function applyAggregator(aggregator: Aggregator, values: number[]): number {
217
+ export function applyAggregator(aggregator: Aggregator, values: readonly number[]): number {
209
218
  if (typeof aggregator === "function") return aggregator(values);
210
219
  switch (aggregator) {
211
220
  case "mean":
@@ -219,50 +228,108 @@ export function applyAggregator(aggregator: Aggregator, values: number[]): numbe
219
228
  }
220
229
  }
221
230
 
222
- /** where 不满足 null,语义等价于 value 开头 return null。 */
231
+ function metricError(metric: Metric, step: string, cause: unknown, locator?: AttemptLocator): Error {
232
+ const at = locator === undefined ? "" : ` at attempt ${locator}`;
233
+ return new Error(
234
+ `Metric "${metric.name}" ${step} failed${at}: ${cause instanceof Error ? cause.message : String(cause)}. ` +
235
+ "Computation errors are not disguised as missing data — fix the metric (return null for expected gaps), then re-run.",
236
+ { cause },
237
+ );
238
+ }
239
+
240
+ /** where 不满足 → null,语义等价于 value 开头 return null;抛错与非有限数按完整用户反馈失败。 */
223
241
  export async function evaluateMetric(metric: Metric, attempt: AttemptHandle): Promise<number | null> {
224
- if (metric.where && !metric.where(attempt)) return null;
225
- return metric.value(attempt);
242
+ const locator = attempt.locator;
243
+ if (metric.where) {
244
+ let pass: boolean;
245
+ try {
246
+ pass = metric.where(attempt);
247
+ } catch (e) {
248
+ throw metricError(metric, "where()", e, locator);
249
+ }
250
+ if (!pass) return null;
251
+ }
252
+ let value: number | null;
253
+ try {
254
+ value = await metric.value(attempt);
255
+ } catch (e) {
256
+ throw metricError(metric, "value()", e, locator);
257
+ }
258
+ if (value !== null && !Number.isFinite(value)) {
259
+ throw metricError(metric, "value()", `returned a non-finite number (${String(value)}); return null for "not measurable"`, locator);
260
+ }
261
+ return value;
226
262
  }
227
263
 
228
- export function displayValue(metric: Metric, value: number | null): string {
264
+ /** 单值 LocalizedText display:metric.display 覆盖内置 unit 格式化;null 的兜底归渲染面。 */
265
+ export function displayValue(metric: Metric, value: number | null): LocalizedText {
229
266
  // null 的纯文本兜底;组件把 null 渲染成「缺数据」,绝不画 0
230
267
  if (value === null) return "—";
231
- return metric.display ? metric.display(value) : formatMetricValue(value, metric.unit);
268
+ if (metric.display) {
269
+ const display = metric.display;
270
+ return localizedDisplay((locale) => {
271
+ try {
272
+ return display(value, locale);
273
+ } catch (e) {
274
+ throw metricError(metric, "display()", e);
275
+ }
276
+ });
277
+ }
278
+ return formatMetricValue(value, metric.unit);
279
+ }
280
+
281
+ function foldAggregator(metric: Metric, step: "perEval" | "acrossEvals", values: readonly number[]): number {
282
+ const aggregator = metric.aggregate?.[step] ?? "mean";
283
+ let folded: number;
284
+ try {
285
+ folded = applyAggregator(aggregator, values);
286
+ } catch (e) {
287
+ throw metricError(metric, `${step} aggregator`, e);
288
+ }
289
+ if (!Number.isFinite(folded)) {
290
+ throw metricError(metric, `${step} aggregator`, `returned a non-finite number (${String(folded)})`);
291
+ }
292
+ return folded;
232
293
  }
233
294
 
234
295
  /**
235
296
  * 一个格子:组内 attempt → 两级聚合 → 终值。
236
297
  * null 值不进聚合但计入 total(覆盖率经 samples/total 如实暴露);全 null → value null。
298
+ * refs 跟随覆盖范围(含值为 null 的 attempt),去重后按 locator 字典序。
237
299
  */
238
300
  export async function computeCell(metric: Metric, items: Item[]): Promise<MetricCell> {
239
- // 第一级桶:同一 (eval × 快照) 的 attempt 折成一个题级值
301
+ // 第一级桶:同一 (experiment × eval × 快照) 的 attempt 折成一个题级值
240
302
  const buckets = new Map<string, number[]>();
241
- const refs: AttemptLocator[] = [];
303
+ const refs = new Set<AttemptLocator>();
242
304
  let samples = 0;
243
305
  for (const item of items) {
306
+ refs.add(locatorOf(item));
244
307
  const value = await evaluateMetric(metric, item.attempt);
245
308
  if (value === null) continue;
246
309
  samples += 1;
247
- refs.push(locatorOf(item)); // 证据引用由句柄直供,不反查下标
248
- const bucketKey = `${evalIdOf(item)}${KEY_SEP}${snapshotKeyOf(item.snapshot)}`;
310
+ const bucketKey = `${fullEvalKey(item)}${KEY_SEP}${snapshotKeyOf(item.snapshot)}`;
249
311
  const bucket = buckets.get(bucketKey);
250
312
  if (bucket) bucket.push(value);
251
313
  else buckets.set(bucketKey, [value]);
252
314
  }
253
- const perEval = metric.aggregate?.perEval ?? "mean";
254
- const across = metric.aggregate?.across ?? "mean";
255
- const evalValues = [...buckets.values()].map((values) => applyAggregator(perEval, values));
256
- const value = evalValues.length === 0 ? null : applyAggregator(across, evalValues);
257
- return { value, display: displayValue(metric, value), samples, total: items.length, refs };
315
+ const evalValues = [...buckets.values()].map((values) => foldAggregator(metric, "perEval", values));
316
+ const value = evalValues.length === 0 ? null : foldAggregator(metric, "acrossEvals", evalValues);
317
+ return {
318
+ value,
319
+ display: displayValue(metric, value),
320
+ samples,
321
+ total: items.length,
322
+ refs: [...refs].sort(),
323
+ };
258
324
  }
259
325
 
260
326
  export function toColumn(metric: Metric): MetricColumn {
261
327
  return {
262
328
  key: metric.name,
263
329
  label: metric.label ?? metric.name,
264
- unit: metric.unit,
265
- better: metric.better,
330
+ ...(metric.description !== undefined ? { description: metric.description } : {}),
331
+ ...(metric.unit !== undefined ? { unit: metric.unit } : {}),
332
+ ...(metric.better !== undefined ? { better: metric.better } : {}),
266
333
  };
267
334
  }
268
335
 
@@ -277,3 +344,24 @@ export function assertUniqueMetricNames(metrics: readonly Metric[], where: strin
277
344
  seen.add(metric.name);
278
345
  }
279
346
  }
347
+
348
+ /** NumericAxis 的取值,带完整反馈的非法值检查(NaN / Infinity 一律报错,不静默当缺失)。 */
349
+ export function axisValueOf(axis: NumericAxis, attempt: AttemptHandle): number | null {
350
+ let value: number | null;
351
+ try {
352
+ value = axis.of(attempt);
353
+ } catch (e) {
354
+ throw new Error(
355
+ `Numeric axis "${axis.name}" of() failed${attempt.locator ? ` at attempt ${attempt.locator}` : ""}: ${
356
+ e instanceof Error ? e.message : String(e)
357
+ }`,
358
+ { cause: e },
359
+ );
360
+ }
361
+ if (value !== null && !Number.isFinite(value)) {
362
+ throw new Error(
363
+ `Numeric axis "${axis.name}" of() returned a non-finite number (${String(value)}); return null for attempts without a plottable x value.`,
364
+ );
365
+ }
366
+ return value;
367
+ }
@@ -0,0 +1,9 @@
1
+ // niceeval/report/built-in —— 包里自带的一个报告文件,没有任何私有钩子
2
+ // (docs/feature/reports/library/built-in.md)。裸 `niceeval show` 与 `niceeval view`
3
+ // 装载的就是这份默认导出:一份普通 defineReport,与用户的 --report 文件同层、
4
+ // 走同一条 装载 → resolve → validate → render 管线。「builtin」不是装载逻辑里的类别,
5
+ // 只是宿主默认拿哪个值的事实。
6
+
7
+ import { ExperimentComparison, defineReport } from "../index.ts";
8
+
9
+ export default defineReport(<ExperimentComparison />);