hoi4-agent-tools 4.6.4 → 4.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.
- package/CHANGELOG.md +22 -5
- package/dist/hoi4_agent_tools/core/artifacts.js +9 -2
- package/dist/hoi4_agent_tools/core/changed-summary.js +41 -0
- package/dist/hoi4_agent_tools/core/engine-client.js +273 -0
- package/dist/hoi4_agent_tools/core/job-worker-host.js +5 -1
- package/dist/hoi4_agent_tools/core/operation-result.js +24 -5
- package/dist/hoi4_agent_tools/focus/compact.js +197 -18
- package/dist/hoi4_agent_tools/focus/engine-layout.js +201 -0
- package/dist/hoi4_agent_tools/focus/job-operations.js +3 -1
- package/dist/hoi4_agent_tools/focus/layout.js +3 -2
- package/dist/hoi4_agent_tools/focus/quality/branches.js +32 -0
- package/dist/hoi4_agent_tools/focus/quality/calibration.js +364 -0
- package/dist/hoi4_agent_tools/focus/quality/delta.js +85 -0
- package/dist/hoi4_agent_tools/focus/quality/focus-quality-calibration.json +2265 -0
- package/dist/hoi4_agent_tools/focus/quality/graph.js +170 -0
- package/dist/hoi4_agent_tools/focus/quality/index.js +24 -0
- package/dist/hoi4_agent_tools/focus/quality/inline-images.js +132 -0
- package/dist/hoi4_agent_tools/focus/quality/metrics.js +424 -0
- package/dist/hoi4_agent_tools/focus/quality/orthogonal.js +202 -0
- package/dist/hoi4_agent_tools/focus/quality/positions.js +41 -0
- package/dist/hoi4_agent_tools/focus/quality/schematic.js +241 -0
- package/dist/hoi4_agent_tools/focus/quality/scorecard.js +549 -0
- package/dist/hoi4_agent_tools/focus/quality/stored-calibration.js +7 -0
- package/dist/hoi4_agent_tools/focus/quality/tool-output.js +113 -0
- package/dist/hoi4_agent_tools/focus/quality/types.js +5 -0
- package/dist/hoi4_agent_tools/focus/tool-operations.js +206 -22
- package/dist/hoi4_agent_tools/focus/tool-support.js +1 -1
- package/dist/hoi4_agent_tools/map/references.js +26 -1
- package/dist/hoi4_agent_tools/map/render.js +29 -3
- package/dist/hoi4_agent_tools/map/service.js +3 -0
- package/dist/hoi4_agent_tools/map/tool-operations.js +5 -0
- package/dist/hoi4_agent_tools/mcp/server/instructions.js +1 -1
- package/dist/hoi4_agent_tools/mcp/tools/focus.js +76 -4
- package/dist/hoi4_agent_tools/reference/game-text.js +109 -0
- package/dist/hoi4_agent_tools/reference/ranking.js +60 -2
- package/dist/hoi4_agent_tools/reference/service.js +92 -21
- package/dist/hoi4_agent_tools/schemas/focus-requests.js +64 -7
- package/dist/hoi4_agent_tools/schemas/focus.js +121 -6
- package/dist/hoi4_agent_tools/version.js +1 -1
- package/docs/configuration.md +17 -17
- package/docs/focus.md +100 -4
- package/docs/map.md +5 -2
- package/docs/reference.md +1 -1
- package/docs/research/reference-benchmark.md +111 -1
- package/docs/setup.md +1 -1
- package/docs/tools.md +5 -5
- package/docs/troubleshooting.md +12 -11
- package/npm-shrinkwrap.json +2 -2
- package/package.json +2 -1
- package/schemas/configuration.schema.json +1 -1
- package/schemas/continuous-focus-palette.schema.json +1 -1
- package/schemas/custom-weighted-pool.schema.json +1 -1
- package/schemas/decision-inspect-input.schema.json +1 -1
- package/schemas/error-log-input.schema.json +1 -1
- package/schemas/event-feature-manifest.schema.json +1 -1
- package/schemas/focus-plan.schema.json +146 -120
- package/schemas/focus-planning-sidecar.schema.json +15 -7
- package/schemas/geography-model.schema.json +1 -1
- package/schemas/gui-animation-source.schema.json +1 -1
- package/schemas/gui-helper.schema.json +1 -1
- package/schemas/gui-scenario.schema.json +1 -1
- package/schemas/helper-expansion-request.schema.json +1 -1
- package/schemas/helper-expansion-summary.schema.json +1 -1
- package/schemas/impact-inspect-input.schema.json +1 -1
- package/schemas/map-operation.schema.json +1 -1
- package/schemas/mechanic-test-input.schema.json +1 -1
- package/schemas/mod-index-input.schema.json +1 -1
- package/schemas/operation-result.schema.json +1 -1
- package/schemas/package-check-input.schema.json +1 -1
- package/schemas/package-manifest.schema.json +1 -1
- package/schemas/probability-analysis-result.schema.json +1 -1
- package/schemas/probability-compare-input.schema.json +1 -1
- package/schemas/probability-evaluate-input.schema.json +1 -1
- package/schemas/probability-inspect-input.schema.json +1 -1
- package/schemas/probability-render-input.schema.json +1 -1
- package/schemas/probability-scenario-set.schema.json +1 -1
- package/schemas/probability-sequence-input.schema.json +1 -1
- package/schemas/probability-simulate-input.schema.json +1 -1
- package/schemas/probability-sweep-input.schema.json +1 -1
- package/schemas/reference-context-input.schema.json +1 -1
- package/schemas/reference-read-input.schema.json +1 -1
- package/schemas/reference-search-input.schema.json +1 -1
- package/schemas/reference-sheets.schema.json +1 -1
- package/schemas/scenario-suite.schema.json +1 -1
- package/schemas/scenario-test-input.schema.json +1 -1
- package/schemas/script-validate-input.schema.json +1 -1
- package/schemas/shared-scenario.schema.json +1 -1
- package/schemas/source-lookup-input.schema.json +1 -1
- package/server.json +2 -2
|
@@ -0,0 +1,364 @@
|
|
|
1
|
+
import { CONVERGENCE_MINIMUM_ROWS, MINIMUM_FOCUSES_FOR_SOFT_METRICS } from './metrics.js';
|
|
2
|
+
/** Bumped whenever a metric definition or the way a tree is measured changes. */
|
|
3
|
+
export const QUALITY_METRIC_DEFINITIONS_VERSION = 1;
|
|
4
|
+
/** Everything the rubric measures, in scorecard order. */
|
|
5
|
+
export const QUALITY_METRICS = [
|
|
6
|
+
{
|
|
7
|
+
id: 'H1',
|
|
8
|
+
group: 'H',
|
|
9
|
+
name: 'duplicate coordinates',
|
|
10
|
+
unit: 'focuses',
|
|
11
|
+
direction: 'high',
|
|
12
|
+
calibrated: false,
|
|
13
|
+
connectorModel: false,
|
|
14
|
+
},
|
|
15
|
+
{
|
|
16
|
+
id: 'H2',
|
|
17
|
+
group: 'H',
|
|
18
|
+
name: 'prerequisite not above',
|
|
19
|
+
unit: 'links',
|
|
20
|
+
direction: 'high',
|
|
21
|
+
calibrated: false,
|
|
22
|
+
connectorModel: false,
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
id: 'H3',
|
|
26
|
+
group: 'H',
|
|
27
|
+
name: 'overlapping plaques',
|
|
28
|
+
unit: 'pairs',
|
|
29
|
+
direction: 'high',
|
|
30
|
+
calibrated: false,
|
|
31
|
+
connectorModel: false,
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
id: 'H4',
|
|
35
|
+
group: 'H',
|
|
36
|
+
name: 'connector through a plaque',
|
|
37
|
+
unit: 'per 100 links',
|
|
38
|
+
direction: 'high',
|
|
39
|
+
calibrated: true,
|
|
40
|
+
connectorModel: true,
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
id: 'H5',
|
|
44
|
+
group: 'H',
|
|
45
|
+
name: 'crossing connectors',
|
|
46
|
+
unit: 'per 100 links',
|
|
47
|
+
direction: 'high',
|
|
48
|
+
calibrated: true,
|
|
49
|
+
connectorModel: true,
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
id: 'S1',
|
|
53
|
+
group: 'S',
|
|
54
|
+
name: 'largest empty rectangle',
|
|
55
|
+
unit: 'fraction of the canvas',
|
|
56
|
+
direction: 'high',
|
|
57
|
+
calibrated: true,
|
|
58
|
+
connectorModel: false,
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
id: 'S2',
|
|
62
|
+
group: 'S',
|
|
63
|
+
name: 'side balance',
|
|
64
|
+
unit: 'fraction',
|
|
65
|
+
direction: 'high',
|
|
66
|
+
calibrated: true,
|
|
67
|
+
connectorModel: false,
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
id: 'S3',
|
|
71
|
+
group: 'S',
|
|
72
|
+
name: 'mass skew',
|
|
73
|
+
unit: 'fraction of half width',
|
|
74
|
+
direction: 'high',
|
|
75
|
+
calibrated: true,
|
|
76
|
+
connectorModel: false,
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
id: 'S4',
|
|
80
|
+
group: 'S',
|
|
81
|
+
name: 'fill ratio',
|
|
82
|
+
unit: 'fraction',
|
|
83
|
+
direction: 'low',
|
|
84
|
+
calibrated: true,
|
|
85
|
+
connectorModel: false,
|
|
86
|
+
},
|
|
87
|
+
{
|
|
88
|
+
id: 'S5',
|
|
89
|
+
group: 'S',
|
|
90
|
+
name: 'row-centroid wander',
|
|
91
|
+
unit: 'fraction of width',
|
|
92
|
+
direction: 'high',
|
|
93
|
+
calibrated: true,
|
|
94
|
+
connectorModel: false,
|
|
95
|
+
},
|
|
96
|
+
{
|
|
97
|
+
id: 'S6',
|
|
98
|
+
group: 'S',
|
|
99
|
+
name: 'sideways jogs in single-focus chains',
|
|
100
|
+
unit: 'per 100 links',
|
|
101
|
+
direction: 'high',
|
|
102
|
+
calibrated: true,
|
|
103
|
+
connectorModel: false,
|
|
104
|
+
},
|
|
105
|
+
{
|
|
106
|
+
id: 'S7',
|
|
107
|
+
group: 'S',
|
|
108
|
+
name: 'interleaved sibling subtrees',
|
|
109
|
+
unit: 'fraction of pairs',
|
|
110
|
+
direction: 'high',
|
|
111
|
+
calibrated: true,
|
|
112
|
+
connectorModel: false,
|
|
113
|
+
},
|
|
114
|
+
{
|
|
115
|
+
id: 'S8',
|
|
116
|
+
group: 'S',
|
|
117
|
+
name: 'fan-out off its parent',
|
|
118
|
+
unit: 'columns',
|
|
119
|
+
direction: 'high',
|
|
120
|
+
calibrated: false,
|
|
121
|
+
connectorModel: false,
|
|
122
|
+
},
|
|
123
|
+
{
|
|
124
|
+
id: 'S9',
|
|
125
|
+
group: 'S',
|
|
126
|
+
name: 'merged connector bars',
|
|
127
|
+
unit: 'per 100 links',
|
|
128
|
+
direction: 'high',
|
|
129
|
+
calibrated: true,
|
|
130
|
+
connectorModel: true,
|
|
131
|
+
},
|
|
132
|
+
{
|
|
133
|
+
id: 'S10',
|
|
134
|
+
group: 'S',
|
|
135
|
+
name: 'vertical slack',
|
|
136
|
+
unit: 'rows',
|
|
137
|
+
direction: 'high',
|
|
138
|
+
calibrated: true,
|
|
139
|
+
connectorModel: false,
|
|
140
|
+
},
|
|
141
|
+
{
|
|
142
|
+
id: 'S11',
|
|
143
|
+
group: 'S',
|
|
144
|
+
name: 'sibling cohorts on mixed rows',
|
|
145
|
+
unit: 'fraction of cohorts',
|
|
146
|
+
direction: 'high',
|
|
147
|
+
calibrated: true,
|
|
148
|
+
connectorModel: false,
|
|
149
|
+
},
|
|
150
|
+
{
|
|
151
|
+
id: 'D1',
|
|
152
|
+
group: 'D',
|
|
153
|
+
name: 'longest single-successor chain',
|
|
154
|
+
unit: 'focuses',
|
|
155
|
+
direction: 'high',
|
|
156
|
+
calibrated: true,
|
|
157
|
+
connectorModel: false,
|
|
158
|
+
},
|
|
159
|
+
{
|
|
160
|
+
id: 'D2',
|
|
161
|
+
group: 'D',
|
|
162
|
+
name: 'leaf fraction',
|
|
163
|
+
unit: 'fraction',
|
|
164
|
+
direction: 'high',
|
|
165
|
+
calibrated: true,
|
|
166
|
+
connectorModel: false,
|
|
167
|
+
},
|
|
168
|
+
{
|
|
169
|
+
id: 'D3',
|
|
170
|
+
group: 'D',
|
|
171
|
+
name: 'exclusive choices that fully reconverge',
|
|
172
|
+
unit: 'sets',
|
|
173
|
+
direction: 'high',
|
|
174
|
+
calibrated: false,
|
|
175
|
+
connectorModel: false,
|
|
176
|
+
},
|
|
177
|
+
{
|
|
178
|
+
id: 'D4',
|
|
179
|
+
group: 'D',
|
|
180
|
+
name: 'tall narrow canvas',
|
|
181
|
+
unit: 'rows per plaque column',
|
|
182
|
+
direction: 'high',
|
|
183
|
+
calibrated: false,
|
|
184
|
+
connectorModel: false,
|
|
185
|
+
},
|
|
186
|
+
];
|
|
187
|
+
const METRIC_BY_ID = new Map(QUALITY_METRICS.map((info) => [info.id, info]));
|
|
188
|
+
export function qualityMetricInfo(id) {
|
|
189
|
+
return METRIC_BY_ID.get(id);
|
|
190
|
+
}
|
|
191
|
+
/** Limits that come from the rubric itself, not from vanilla statistics. */
|
|
192
|
+
export const FIXED_QUALITY_LIMITS = {
|
|
193
|
+
H1: { direction: 'high', review: 0, revise: 0 },
|
|
194
|
+
H2: { direction: 'high', review: 0, revise: 0 },
|
|
195
|
+
H3: { direction: 'high', review: 0, revise: 0 },
|
|
196
|
+
S8: { direction: 'high', review: 2, revise: 3 },
|
|
197
|
+
D3: { direction: 'high', review: 0, revise: 0 },
|
|
198
|
+
D4: { direction: 'high', review: 2.5, revise: 2.5 },
|
|
199
|
+
};
|
|
200
|
+
export const QUALITY_BUCKET_BOUNDS = [
|
|
201
|
+
{ id: 'small', minimumFocuses: 0, maximumFocuses: 39 },
|
|
202
|
+
{ id: 'medium', minimumFocuses: 40, maximumFocuses: 120 },
|
|
203
|
+
{ id: 'large', minimumFocuses: 121, maximumFocuses: Number.MAX_SAFE_INTEGER },
|
|
204
|
+
];
|
|
205
|
+
export function bucketIdForFocusCount(focusCount) {
|
|
206
|
+
return QUALITY_BUCKET_BOUNDS.find(({ minimumFocuses, maximumFocuses }) => focusCount >= minimumFocuses && focusCount <= maximumFocuses).id;
|
|
207
|
+
}
|
|
208
|
+
/** Linear interpolation between order statistics (the common "type 7" percentile). */
|
|
209
|
+
export function quantile(sorted, fraction) {
|
|
210
|
+
if (sorted.length === 0)
|
|
211
|
+
return 0;
|
|
212
|
+
const position = (sorted.length - 1) * fraction;
|
|
213
|
+
const lower = Math.floor(position);
|
|
214
|
+
const upper = Math.ceil(position);
|
|
215
|
+
return sorted[lower] + (sorted[upper] - sorted[lower]) * (position - lower);
|
|
216
|
+
}
|
|
217
|
+
/** Derived numbers are stored with four decimals so a recomputation compares exactly. */
|
|
218
|
+
export function roundMetric(value) {
|
|
219
|
+
return Number(value.toFixed(4));
|
|
220
|
+
}
|
|
221
|
+
export function metricStatistics(values) {
|
|
222
|
+
if (values.length === 0)
|
|
223
|
+
return undefined;
|
|
224
|
+
const sorted = [...values].sort((left, right) => left - right);
|
|
225
|
+
return {
|
|
226
|
+
min: roundMetric(sorted[0]),
|
|
227
|
+
p10: roundMetric(quantile(sorted, 0.1)),
|
|
228
|
+
p50: roundMetric(quantile(sorted, 0.5)),
|
|
229
|
+
p90: roundMetric(quantile(sorted, 0.9)),
|
|
230
|
+
max: roundMetric(sorted[sorted.length - 1]),
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* The verdict rules measured to fit the vanilla corpus (leave-one-out over 65 trees of 40 or more
|
|
235
|
+
* focuses: 82% pass, 15% review, 3% revise for an unseen vanilla-quality tree). Review starts at the
|
|
236
|
+
* edge of the vanilla range; the 90th percentile as review limit put 72% of vanilla trees at review
|
|
237
|
+
* and the vanilla maximum as revise limit put 26% at revise, because twelve metrics each cross a
|
|
238
|
+
* limit by chance. The revise limit therefore carries a margin and a count of five is required.
|
|
239
|
+
*/
|
|
240
|
+
export const DEFAULT_VERDICT_RULES = {
|
|
241
|
+
reviseMargin: 1,
|
|
242
|
+
reviseCount: 5,
|
|
243
|
+
advisoryMetrics: ['H4', 'H5', 'S9'],
|
|
244
|
+
convergenceMinimumRows: CONVERGENCE_MINIMUM_ROWS,
|
|
245
|
+
};
|
|
246
|
+
/**
|
|
247
|
+
* Review limit: the edge of the vanilla range (its maximum, or minimum for the fill ratio). Revise
|
|
248
|
+
* limit: that edge plus a margin of the spread above the median (design proxies flag at the edge
|
|
249
|
+
* itself). The margin has a floor so metrics that are zero in most vanilla trees leave some room.
|
|
250
|
+
*/
|
|
251
|
+
export function deriveLimits(id, statistics, rules = DEFAULT_VERDICT_RULES) {
|
|
252
|
+
const fixed = FIXED_QUALITY_LIMITS[id];
|
|
253
|
+
if (fixed !== undefined)
|
|
254
|
+
return fixed;
|
|
255
|
+
const info = qualityMetricInfo(id);
|
|
256
|
+
if (!info.calibrated)
|
|
257
|
+
return undefined;
|
|
258
|
+
if (info.group === 'D')
|
|
259
|
+
return { direction: 'high', review: statistics.max, revise: statistics.max };
|
|
260
|
+
const floor = (extreme) => 0.02 * Math.max(1, Math.abs(extreme));
|
|
261
|
+
return info.direction === 'high'
|
|
262
|
+
? {
|
|
263
|
+
direction: 'high',
|
|
264
|
+
review: statistics.max,
|
|
265
|
+
revise: roundMetric(statistics.max +
|
|
266
|
+
rules.reviseMargin * Math.max(statistics.max - statistics.p50, floor(statistics.max))),
|
|
267
|
+
}
|
|
268
|
+
: {
|
|
269
|
+
direction: 'low',
|
|
270
|
+
review: statistics.min,
|
|
271
|
+
revise: roundMetric(statistics.min -
|
|
272
|
+
rules.reviseMargin * Math.max(statistics.p50 - statistics.min, floor(statistics.min))),
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
const CALIBRATED_IDS = QUALITY_METRICS.filter(({ calibrated }) => calibrated).map(({ id }) => id);
|
|
276
|
+
export function buildCalibrationBuckets(trees, rules = DEFAULT_VERDICT_RULES) {
|
|
277
|
+
return QUALITY_BUCKET_BOUNDS.map((bounds) => {
|
|
278
|
+
const members = trees.filter(({ bucket }) => bucket === bounds.id);
|
|
279
|
+
const statistics = {};
|
|
280
|
+
const limits = {};
|
|
281
|
+
// Trees below 40 focuses are too few in vanilla to calibrate on; they borrow the medium limits.
|
|
282
|
+
for (const id of bounds.id === 'small' ? [] : CALIBRATED_IDS) {
|
|
283
|
+
const connectorModel = qualityMetricInfo(id).connectorModel;
|
|
284
|
+
const values = members
|
|
285
|
+
.filter(({ ambiguousPositions }) => !(connectorModel && ambiguousPositions))
|
|
286
|
+
.flatMap(({ values: measured }) => {
|
|
287
|
+
const value = measured[id];
|
|
288
|
+
return value === undefined ? [] : [value];
|
|
289
|
+
});
|
|
290
|
+
const stats = metricStatistics(values);
|
|
291
|
+
if (stats === undefined)
|
|
292
|
+
continue;
|
|
293
|
+
statistics[id] = stats;
|
|
294
|
+
const derived = deriveLimits(id, stats, rules);
|
|
295
|
+
if (derived !== undefined)
|
|
296
|
+
limits[id] = derived;
|
|
297
|
+
}
|
|
298
|
+
return {
|
|
299
|
+
id: bounds.id,
|
|
300
|
+
minimumFocuses: bounds.minimumFocuses,
|
|
301
|
+
maximumFocuses: bounds.maximumFocuses === Number.MAX_SAFE_INTEGER ? 100000 : bounds.maximumFocuses,
|
|
302
|
+
treeCount: members.length,
|
|
303
|
+
statistics,
|
|
304
|
+
limits,
|
|
305
|
+
};
|
|
306
|
+
});
|
|
307
|
+
}
|
|
308
|
+
/** Weights of the 0 to 100 convenience score; the verdict never depends on them. */
|
|
309
|
+
export const DEFAULT_SCORE_WEIGHTS = {
|
|
310
|
+
H4: 1,
|
|
311
|
+
H5: 1,
|
|
312
|
+
S1: 1.5,
|
|
313
|
+
S2: 1.5,
|
|
314
|
+
S3: 1.5,
|
|
315
|
+
S4: 1,
|
|
316
|
+
S5: 1,
|
|
317
|
+
S6: 1,
|
|
318
|
+
S7: 1,
|
|
319
|
+
S9: 0.5,
|
|
320
|
+
S10: 0.5,
|
|
321
|
+
S11: 0.5,
|
|
322
|
+
};
|
|
323
|
+
/** The statistics of the bucket a tree of this size is compared with. */
|
|
324
|
+
export function calibrationBucketFor(calibration, focusCount) {
|
|
325
|
+
const id = bucketIdForFocusCount(focusCount);
|
|
326
|
+
// Trees below 40 focuses are judged against the medium bucket's review limits only.
|
|
327
|
+
const lookup = id === 'small' ? 'medium' : id;
|
|
328
|
+
return calibration.buckets.find((bucket) => bucket.id === lookup);
|
|
329
|
+
}
|
|
330
|
+
export function limitsFor(bucket, id) {
|
|
331
|
+
return FIXED_QUALITY_LIMITS[id] ?? bucket.limits[id];
|
|
332
|
+
}
|
|
333
|
+
/** True when a value lies beyond a limit, in the direction in which the metric gets worse. */
|
|
334
|
+
export function beyondLimit(direction, value, limit) {
|
|
335
|
+
return direction === 'high' ? value > limit : value < limit;
|
|
336
|
+
}
|
|
337
|
+
/**
|
|
338
|
+
* Where a calibrated soft metric stands against its limits. Limits are stored with four decimals,
|
|
339
|
+
* so the value is compared at the same precision. A tree below 40 focuses and an advisory metric
|
|
340
|
+
* (a connector metric under the unverified orthogonal model) reach `review` at most.
|
|
341
|
+
*/
|
|
342
|
+
export function softMetricStatus(id, value, limits, bucketId, rules) {
|
|
343
|
+
const comparable = Number(value.toFixed(4));
|
|
344
|
+
if (beyondLimit(limits.direction, comparable, limits.revise))
|
|
345
|
+
return bucketId === 'small' || rules.advisoryMetrics.includes(id) ? 'review' : 'revise';
|
|
346
|
+
return beyondLimit(limits.direction, comparable, limits.review) ? 'review' : 'ok';
|
|
347
|
+
}
|
|
348
|
+
/**
|
|
349
|
+
* The verdict the calibrated soft metrics alone give: `revise` when one is beyond its revise limit
|
|
350
|
+
* or enough of them (the advisory ones excluded) are beyond the vanilla range, `review` when any
|
|
351
|
+
* is, otherwise `pass`. Hard defects and design flags are added by the caller.
|
|
352
|
+
*/
|
|
353
|
+
export function softVerdict(statuses, bucketId, rules) {
|
|
354
|
+
const beyond = statuses.filter(({ status }) => status !== 'ok');
|
|
355
|
+
const counted = beyond.filter(({ id }) => !rules.advisoryMetrics.includes(id));
|
|
356
|
+
if (statuses.some(({ status }) => status === 'revise') ||
|
|
357
|
+
(bucketId !== 'small' && counted.length >= rules.reviseCount))
|
|
358
|
+
return 'revise';
|
|
359
|
+
return beyond.length > 0 ? 'review' : 'pass';
|
|
360
|
+
}
|
|
361
|
+
/** True when a tree is large enough for the vanilla-calibrated soft metrics. */
|
|
362
|
+
export function supportsSoftMetrics(focusCount) {
|
|
363
|
+
return focusCount >= MINIMUM_FOCUSES_FOR_SOFT_METRICS;
|
|
364
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { compareCodeUnits, hashCanonical } from '../../core/canonical.js';
|
|
2
|
+
import { qualityMetricInfo } from './calibration.js';
|
|
3
|
+
const LISTED_FOCUS_LIMIT = 100;
|
|
4
|
+
function anchorOf(plan) {
|
|
5
|
+
return new Map(plan.focuses.map((focus) => [
|
|
6
|
+
focus.id,
|
|
7
|
+
focus.position.mode === 'relative' ? focus.position.relativeTo : null,
|
|
8
|
+
]));
|
|
9
|
+
}
|
|
10
|
+
function prerequisiteHashOf(plan) {
|
|
11
|
+
return hashCanonical([...plan.focuses]
|
|
12
|
+
.sort((left, right) => compareCodeUnits(left.id, right.id))
|
|
13
|
+
.map((focus) => [
|
|
14
|
+
focus.id,
|
|
15
|
+
focus.prerequisites.groups
|
|
16
|
+
.map(({ focusIds }) => [...focusIds].sort(compareCodeUnits).join('\0'))
|
|
17
|
+
.sort(compareCodeUnits),
|
|
18
|
+
]));
|
|
19
|
+
}
|
|
20
|
+
function exclusionHashOf(plan) {
|
|
21
|
+
return hashCanonical([...plan.focuses]
|
|
22
|
+
.sort((left, right) => compareCodeUnits(left.id, right.id))
|
|
23
|
+
.map((focus) => [focus.id, [...focus.mutuallyExclusive].sort(compareCodeUnits)]));
|
|
24
|
+
}
|
|
25
|
+
function improvement(id, before, after) {
|
|
26
|
+
const direction = qualityMetricInfo(id).direction === 'high' ? 1 : -1;
|
|
27
|
+
return direction * (before - after);
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Compares two layouts of one tree. `before` is absent for a new tree. The hashes let a caller
|
|
31
|
+
* prove that a layout repair moved only coordinates: the prerequisite and exclusion hashes must be
|
|
32
|
+
* equal, and the unmoved hash must not change when the same repair is applied again.
|
|
33
|
+
*/
|
|
34
|
+
export function describeLayoutDelta(before, after) {
|
|
35
|
+
const afterAnchors = anchorOf(after.plan);
|
|
36
|
+
const beforePositions = new Map(before?.nodes.map(({ id, x, y }) => [id, { x, y }]));
|
|
37
|
+
const beforeAnchors = before === undefined ? undefined : anchorOf(before.plan);
|
|
38
|
+
const moved = [];
|
|
39
|
+
const unmoved = [];
|
|
40
|
+
for (const node of [...after.nodes].sort((left, right) => compareCodeUnits(left.id, right.id))) {
|
|
41
|
+
const original = beforePositions.get(node.id);
|
|
42
|
+
const anchor = afterAnchors.get(node.id) ?? null;
|
|
43
|
+
const sameAnchor = beforeAnchors === undefined || (beforeAnchors.get(node.id) ?? null) === anchor;
|
|
44
|
+
if (before !== undefined && (original?.x !== node.x || original.y !== node.y || !sameAnchor))
|
|
45
|
+
moved.push(node.id);
|
|
46
|
+
else if (before !== undefined)
|
|
47
|
+
unmoved.push([node.id, node.x, node.y, anchor]);
|
|
48
|
+
}
|
|
49
|
+
const prerequisiteHash = prerequisiteHashOf(after.plan);
|
|
50
|
+
const mutualExclusionHash = exclusionHashOf(after.plan);
|
|
51
|
+
const improved = [];
|
|
52
|
+
const regressed = [];
|
|
53
|
+
if (before !== undefined) {
|
|
54
|
+
const beforeRows = new Map(before.report.scorecard.map((row) => [row.id, row]));
|
|
55
|
+
for (const row of after.report.scorecard) {
|
|
56
|
+
const previous = beforeRows.get(row.id);
|
|
57
|
+
if (previous === undefined ||
|
|
58
|
+
row.status === 'not_evaluated' ||
|
|
59
|
+
previous.status === 'not_evaluated')
|
|
60
|
+
continue;
|
|
61
|
+
const gain = improvement(row.id, previous.value, row.value);
|
|
62
|
+
const noise = Math.max(1e-6, 0.02 * Math.max(Math.abs(previous.value), Math.abs(row.value)));
|
|
63
|
+
if (gain > noise)
|
|
64
|
+
improved.push(row.id);
|
|
65
|
+
else if (gain < -noise)
|
|
66
|
+
regressed.push(row.id);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return {
|
|
70
|
+
verdictBefore: before?.report.verdict ?? null,
|
|
71
|
+
verdictAfter: after.report.verdict,
|
|
72
|
+
scoreBefore: before?.report.score ?? null,
|
|
73
|
+
scoreAfter: after.report.score,
|
|
74
|
+
improved,
|
|
75
|
+
regressed,
|
|
76
|
+
movedFocusCount: moved.length,
|
|
77
|
+
movedFocusIds: moved.slice(0, LISTED_FOCUS_LIMIT),
|
|
78
|
+
unmovedFocusCount: unmoved.length,
|
|
79
|
+
unmovedHash: before === undefined ? '' : hashCanonical(unmoved),
|
|
80
|
+
prerequisiteHash,
|
|
81
|
+
prerequisitesUnchanged: before === undefined ? null : prerequisiteHashOf(before.plan) === prerequisiteHash,
|
|
82
|
+
mutualExclusionHash,
|
|
83
|
+
mutualExclusionsUnchanged: before === undefined ? null : exclusionHashOf(before.plan) === mutualExclusionHash,
|
|
84
|
+
};
|
|
85
|
+
}
|