hoi4-agent-tools 4.6.5 → 4.8.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 +40 -5
- package/README.md +32 -13
- package/dist/hoi4_agent_tools/core/engine-client.js +342 -0
- package/dist/hoi4_agent_tools/core/operation-result.js +33 -5
- package/dist/hoi4_agent_tools/core/resident-engine.js +273 -0
- package/dist/hoi4_agent_tools/core/temporary-directories.js +3 -2
- package/dist/hoi4_agent_tools/decision/service.js +7 -0
- package/dist/hoi4_agent_tools/event/service.js +18 -6
- package/dist/hoi4_agent_tools/event/summary.js +9 -0
- 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/game-render.js +72 -0
- package/dist/hoi4_agent_tools/focus/game-view-output.js +160 -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/native-effects.js +4 -4
- 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 +213 -0
- package/dist/hoi4_agent_tools/focus/quality/positions.js +41 -0
- package/dist/hoi4_agent_tools/focus/quality/schematic.js +243 -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/render.js +21 -9
- package/dist/hoi4_agent_tools/focus/tool-operations.js +364 -25
- package/dist/hoi4_agent_tools/focus/tool-support.js +1 -1
- package/dist/hoi4_agent_tools/gui/assets.js +18 -0
- package/dist/hoi4_agent_tools/gui/dds.js +38 -5
- package/dist/hoi4_agent_tools/gui/engine-textures.js +48 -0
- package/dist/hoi4_agent_tools/gui/source-graph.js +7 -0
- package/dist/hoi4_agent_tools/gui/tool-operations.js +23 -4
- package/dist/hoi4_agent_tools/gui/validators.js +31 -2
- package/dist/hoi4_agent_tools/ideas/inspect.js +43 -0
- package/dist/hoi4_agent_tools/localisation/inspect.js +34 -0
- package/dist/hoi4_agent_tools/mcp/server/argument-errors.js +2 -0
- package/dist/hoi4_agent_tools/mcp/server/instructions.js +1 -1
- package/dist/hoi4_agent_tools/mcp/tools/event.js +2 -0
- package/dist/hoi4_agent_tools/mcp/tools/focus.js +76 -4
- package/dist/hoi4_agent_tools/mcp/tools/reference.js +18 -0
- package/dist/hoi4_agent_tools/probability/operations.js +27 -1
- package/dist/hoi4_agent_tools/probability/service.js +91 -11
- package/dist/hoi4_agent_tools/probability/source-analysis.js +2 -2
- package/dist/hoi4_agent_tools/probability/sweep.js +20 -6
- package/dist/hoi4_agent_tools/reference/command-phrases.json +11065 -0
- package/dist/hoi4_agent_tools/reference/error-log.js +24 -0
- package/dist/hoi4_agent_tools/reference/game-text.js +109 -0
- package/dist/hoi4_agent_tools/reference/known-mistakes.js +8 -5
- package/dist/hoi4_agent_tools/reference/known-mistakes.json +4 -4
- package/dist/hoi4_agent_tools/reference/operations.js +68 -2
- package/dist/hoi4_agent_tools/reference/ranking.js +92 -2
- package/dist/hoi4_agent_tools/reference/script-validation.js +46 -6
- package/dist/hoi4_agent_tools/reference/service.js +125 -21
- package/dist/hoi4_agent_tools/reference/source-lookup.js +8 -0
- package/dist/hoi4_agent_tools/schemas/focus-requests.js +68 -7
- package/dist/hoi4_agent_tools/schemas/focus.js +121 -6
- package/dist/hoi4_agent_tools/schemas/ideas.js +142 -0
- package/dist/hoi4_agent_tools/schemas/localisation.js +94 -0
- package/dist/hoi4_agent_tools/schemas/mod-tools.js +11 -0
- package/dist/hoi4_agent_tools/version.js +1 -1
- package/docs/configuration.md +17 -17
- package/docs/focus.md +104 -4
- package/docs/reference.md +1 -1
- package/docs/research/reference-benchmark.md +152 -1
- package/docs/setup.md +1 -1
- package/docs/tools.md +48 -44
- package/docs/troubleshooting.md +13 -12
- package/engine/README.md +284 -0
- package/engine/bin/darwin-arm64/hoi4-engine +0 -0
- package/engine/bin/linux-arm64/hoi4-engine +0 -0
- package/engine/bin/linux-x64/hoi4-engine +0 -0
- package/engine/bin/manifest.json +26 -0
- package/engine/bin/win32-x64/hoi4-engine.exe +0 -0
- package/npm-shrinkwrap.json +2 -2
- package/package.json +5 -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,160 @@
|
|
|
1
|
+
import sharp from 'sharp';
|
|
2
|
+
import { ArtifactStore, boundedSourceHashEvidence, } from '../core/artifacts.js';
|
|
3
|
+
import { canonicalJson, sha256Bytes } from '../core/canonical.js';
|
|
4
|
+
import { PACKAGE_VERSION } from '../version.js';
|
|
5
|
+
/**
|
|
6
|
+
* Artifacts of the game view alone (`view: "game"`): the PNG, a JSON report, an SVG hover map
|
|
7
|
+
* on the same pixels and a self-contained HTML viewer with zoom and hover titles. No schematic is
|
|
8
|
+
* drawn, so this is the fast path an in-app preview calls on every edit.
|
|
9
|
+
*/
|
|
10
|
+
const artifacts = new ArtifactStore();
|
|
11
|
+
/** Longest edge of the inline overview. */
|
|
12
|
+
const INLINE_EDGE = 1500;
|
|
13
|
+
/** Largest inline overview, the same budget as the schematic pictures. */
|
|
14
|
+
const INLINE_BYTES = 700_000;
|
|
15
|
+
function escapeXml(value) {
|
|
16
|
+
return value
|
|
17
|
+
.replaceAll('&', '&')
|
|
18
|
+
.replaceAll('<', '<')
|
|
19
|
+
.replaceAll('>', '>')
|
|
20
|
+
.replaceAll('"', '"')
|
|
21
|
+
.replaceAll("'", ''');
|
|
22
|
+
}
|
|
23
|
+
function safeStem(value) {
|
|
24
|
+
const safe = value
|
|
25
|
+
.replace(/[^A-Za-z0-9._-]+/gu, '_')
|
|
26
|
+
.replace(/^[_-]+/u, '')
|
|
27
|
+
.slice(0, 80);
|
|
28
|
+
return safe.length === 0 ? 'focus-tree' : safe;
|
|
29
|
+
}
|
|
30
|
+
export async function gameViewBundle(plan, layout, png, report, titles, request) {
|
|
31
|
+
const boxes = report.focuses
|
|
32
|
+
.map((box) => {
|
|
33
|
+
const title = titles[box.id]?.text ?? box.id;
|
|
34
|
+
return `<rect class="focus" data-focus-id="${escapeXml(box.id)}" x="${box.x}" y="${box.y}" width="${box.width}" height="${box.height}"><title>${escapeXml(`${title} (${box.id})`)}</title></rect>`;
|
|
35
|
+
})
|
|
36
|
+
.join('\n');
|
|
37
|
+
const svg = `<svg xmlns="http://www.w3.org/2000/svg" width="${report.width}" height="${report.height}" viewBox="0 0 ${report.width} ${report.height}" role="img" aria-label="${escapeXml(plan.id)} focus tree, game view hover map">
|
|
38
|
+
<style>.focus{fill:transparent;stroke:none}.focus:hover{stroke:#66d9ef;stroke-width:2}</style>
|
|
39
|
+
${boxes}
|
|
40
|
+
</svg>
|
|
41
|
+
`;
|
|
42
|
+
const json = `${canonicalJson({
|
|
43
|
+
schemaVersion: 1,
|
|
44
|
+
view: 'game',
|
|
45
|
+
tree: {
|
|
46
|
+
id: plan.id,
|
|
47
|
+
sourcePath: plan.provenance.sourcePath,
|
|
48
|
+
sourceHash: plan.provenance.sourceHash,
|
|
49
|
+
focusCount: plan.focuses.length,
|
|
50
|
+
},
|
|
51
|
+
layout: { hash: layout.layoutHash, nodes: layout.nodes.map(({ id, x, y }) => ({ id, x, y })) },
|
|
52
|
+
render: report,
|
|
53
|
+
titles,
|
|
54
|
+
request,
|
|
55
|
+
})}\n`;
|
|
56
|
+
const html = `<!doctype html>
|
|
57
|
+
<html lang="en">
|
|
58
|
+
<head>
|
|
59
|
+
<meta charset="utf-8">
|
|
60
|
+
<meta name="viewport" content="width=device-width,initial-scale=1">
|
|
61
|
+
<title>${escapeXml(plan.id)} focus tree · game view · HOI4 Agent Tools</title>
|
|
62
|
+
<style>
|
|
63
|
+
html,body{margin:0;height:100%;background:#111;color:#ddd;font-family:system-ui,sans-serif}
|
|
64
|
+
#view{position:absolute;inset:0;overflow:hidden;cursor:grab}
|
|
65
|
+
#stage{position:absolute;transform-origin:0 0}
|
|
66
|
+
#stage img,#stage svg{position:absolute;left:0;top:0;display:block}
|
|
67
|
+
#note{position:fixed;left:8px;bottom:8px;font-size:12px;background:#000a;padding:4px 8px;border-radius:4px}
|
|
68
|
+
</style>
|
|
69
|
+
</head>
|
|
70
|
+
<body>
|
|
71
|
+
<div id="view"><div id="stage"><img alt="${escapeXml(plan.id)} focus tree as the game draws it" width="${report.width}" height="${report.height}" src="data:image/png;base64,${png.toString('base64')}">${svg}</div></div>
|
|
72
|
+
<div id="note">${escapeXml(plan.id)} · ${plan.focuses.length} focuses · wheel to zoom, drag to move, hover for titles. Tool render, not a game screenshot.</div>
|
|
73
|
+
<script>
|
|
74
|
+
const view=document.getElementById('view'),stage=document.getElementById('stage');
|
|
75
|
+
let scale=Math.min(1,view.clientWidth/${report.width}),x=0,y=0,drag;
|
|
76
|
+
const apply=()=>{stage.style.transform='translate('+x+'px,'+y+'px) scale('+scale+')'};
|
|
77
|
+
view.addEventListener('wheel',(e)=>{e.preventDefault();const k=Math.exp(-e.deltaY*0.0015),n=Math.min(8,Math.max(0.05,scale*k));x=e.clientX-(e.clientX-x)*n/scale;y=e.clientY-(e.clientY-y)*n/scale;scale=n;apply()},{passive:false});
|
|
78
|
+
view.addEventListener('pointerdown',(e)=>{drag=[e.clientX-x,e.clientY-y];view.setPointerCapture(e.pointerId)});
|
|
79
|
+
view.addEventListener('pointermove',(e)=>{if(drag){x=e.clientX-drag[0];y=e.clientY-drag[1];apply()}});
|
|
80
|
+
view.addEventListener('pointerup',()=>{drag=undefined});
|
|
81
|
+
apply();
|
|
82
|
+
</script>
|
|
83
|
+
</body>
|
|
84
|
+
</html>
|
|
85
|
+
`;
|
|
86
|
+
// The overview shrinks until it fits the inline byte budget, like the schematic pictures.
|
|
87
|
+
let fit = Math.min(1, INLINE_EDGE / Math.max(report.width, report.height));
|
|
88
|
+
let inlinePng = png;
|
|
89
|
+
for (let attempt = 0; attempt < 6; attempt += 1) {
|
|
90
|
+
inlinePng =
|
|
91
|
+
fit >= 1 && png.length <= INLINE_BYTES
|
|
92
|
+
? png
|
|
93
|
+
: await sharp(png)
|
|
94
|
+
.resize(Math.max(1, Math.round(report.width * fit)), Math.max(1, Math.round(report.height * fit)), { fit: 'fill', kernel: sharp.kernel.lanczos3 })
|
|
95
|
+
.png({ compressionLevel: 9, palette: true, quality: 90 })
|
|
96
|
+
.toBuffer();
|
|
97
|
+
if (inlinePng.length <= INLINE_BYTES)
|
|
98
|
+
break;
|
|
99
|
+
fit *= 0.75;
|
|
100
|
+
}
|
|
101
|
+
return {
|
|
102
|
+
png,
|
|
103
|
+
svg,
|
|
104
|
+
html,
|
|
105
|
+
json,
|
|
106
|
+
hashes: {
|
|
107
|
+
html: sha256Bytes(html),
|
|
108
|
+
svg: sha256Bytes(svg),
|
|
109
|
+
png: sha256Bytes(png),
|
|
110
|
+
json: sha256Bytes(json),
|
|
111
|
+
},
|
|
112
|
+
inline: {
|
|
113
|
+
png: inlinePng,
|
|
114
|
+
width: Math.max(1, Math.round(report.width * fit)),
|
|
115
|
+
height: Math.max(1, Math.round(report.height * fit)),
|
|
116
|
+
},
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
export async function storeGameViewArtifacts(workspace, plan, bundle, renderProfile, signal) {
|
|
120
|
+
const stem = safeStem(plan.id);
|
|
121
|
+
const sourceEvidence = boundedSourceHashEvidence({
|
|
122
|
+
[plan.provenance.sourcePath]: plan.provenance.sourceHash,
|
|
123
|
+
});
|
|
124
|
+
const provenance = {
|
|
125
|
+
toolVersion: PACKAGE_VERSION,
|
|
126
|
+
schemaVersion: 'focus-render.v1',
|
|
127
|
+
sourceHashes: sourceEvidence.sourceHashes,
|
|
128
|
+
renderProfile: { renderer: 'hoi4-engine game view', ...renderProfile },
|
|
129
|
+
metadata: { sourceHashInventory: sourceEvidence.inventory },
|
|
130
|
+
};
|
|
131
|
+
const writes = [
|
|
132
|
+
{
|
|
133
|
+
name: `${stem}.focus.png`,
|
|
134
|
+
mimeType: 'image/png',
|
|
135
|
+
content: bundle.png,
|
|
136
|
+
provenance: { ...provenance, kind: 'focus-png' },
|
|
137
|
+
},
|
|
138
|
+
{
|
|
139
|
+
name: `${stem}.focus.json`,
|
|
140
|
+
mimeType: 'application/json',
|
|
141
|
+
content: bundle.json,
|
|
142
|
+
provenance: { ...provenance, kind: 'focus-json' },
|
|
143
|
+
},
|
|
144
|
+
{
|
|
145
|
+
name: `${stem}.focus.svg`,
|
|
146
|
+
mimeType: 'image/svg+xml',
|
|
147
|
+
content: bundle.svg,
|
|
148
|
+
provenance: { ...provenance, kind: 'focus-svg' },
|
|
149
|
+
description: 'Hover map of the game view: one titled box per focus, on the PNG pixels',
|
|
150
|
+
},
|
|
151
|
+
{
|
|
152
|
+
name: `${stem}.focus.html`,
|
|
153
|
+
mimeType: 'text/html',
|
|
154
|
+
content: bundle.html,
|
|
155
|
+
provenance: { ...provenance, kind: 'focus-html' },
|
|
156
|
+
description: 'The game view with zoom, pan and hover titles',
|
|
157
|
+
},
|
|
158
|
+
];
|
|
159
|
+
return artifacts.withAtomicChunkedWrites(workspace, writes, (stored) => Promise.resolve([...stored]), signal);
|
|
160
|
+
}
|
|
@@ -22,7 +22,9 @@ export function registerFocusJobs(operations, engine) {
|
|
|
22
22
|
signal: context.signal,
|
|
23
23
|
progress: (completed, total, message) => context.progress({ completed, total, message }),
|
|
24
24
|
});
|
|
25
|
-
const wire = wireResult.parse(JSON.parse(JSON.stringify(toolResult(execution.result
|
|
25
|
+
const wire = wireResult.parse(JSON.parse(JSON.stringify(toolResult(execution.result, {
|
|
26
|
+
...(execution.images === undefined ? {} : { images: execution.images }),
|
|
27
|
+
}))));
|
|
26
28
|
await context.stageResult(wire, { sourceRevision: execution.sourceRevision });
|
|
27
29
|
return wire;
|
|
28
30
|
}
|
|
@@ -82,7 +82,8 @@ const LONG_CONNECTOR_MANHATTAN_SPAN = 10;
|
|
|
82
82
|
function coordinateKey(x, y) {
|
|
83
83
|
return `${x},${y}`;
|
|
84
84
|
}
|
|
85
|
-
|
|
85
|
+
/** The lane a focus is laid out in: its own lane, its branch's lane, its branch, or 'default'. */
|
|
86
|
+
export function focusLaneId(plan, focus) {
|
|
86
87
|
if (focus.laneId !== undefined)
|
|
87
88
|
return focus.laneId;
|
|
88
89
|
if (focus.branchId !== undefined) {
|
|
@@ -2228,7 +2229,7 @@ function* layoutFocusTreeSteps(plan, options = {}) {
|
|
|
2228
2229
|
}
|
|
2229
2230
|
focuses.set(focus.id, focus);
|
|
2230
2231
|
}
|
|
2231
|
-
const laneIds = new Map([...focuses.values()].map((focus) => [focus.id,
|
|
2232
|
+
const laneIds = new Map([...focuses.values()].map((focus) => [focus.id, focusLaneId(plan, focus)]));
|
|
2232
2233
|
const discoveredLanes = [...new Set(laneIds.values())].sort((left, right) => compareCodeUnits(left, right));
|
|
2233
2234
|
const nodeSpacing = options.nodeSpacing ?? DEFAULT_NODE_SPACING;
|
|
2234
2235
|
const siblingLayout = automaticSiblingLayout(plan, focuses, laneIds, nodeSpacing);
|
|
@@ -2,10 +2,10 @@
|
|
|
2
2
|
* Native effect identifiers used to distinguish direct scripted-effect calls
|
|
3
3
|
* from engine effects while importing focus completion rewards.
|
|
4
4
|
*
|
|
5
|
-
*
|
|
6
|
-
* fallback list; analysis also accepts every effect the installed
|
|
7
|
-
* documentation/effects_documentation.md, so effects added by later patches
|
|
8
|
-
*
|
|
5
|
+
* Generated by scripts/generate-native-effects.ts from the HOI4 1.19.4.1 effects
|
|
6
|
+
* documentation. This is the fallback list; analysis also accepts every effect the installed
|
|
7
|
+
* game documents in documentation/effects_documentation.md, so effects added by later patches
|
|
8
|
+
* are recognized. The nightly game-update watcher regenerates it for review on a new build.
|
|
9
9
|
*/
|
|
10
10
|
export const nativeEffectKeys = new Set([
|
|
11
11
|
'activate_advisor',
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { compareCodeUnits } from '../../core/canonical.js';
|
|
2
|
+
/** Groups focuses by their declared or derived branch; trunk and shared focuses form no branch. */
|
|
3
|
+
export function summarizeBranches(graph) {
|
|
4
|
+
const groups = new Map();
|
|
5
|
+
for (const { id } of graph.nodes) {
|
|
6
|
+
const branch = graph.branchOf.get(id) ?? '';
|
|
7
|
+
if (branch === '')
|
|
8
|
+
continue;
|
|
9
|
+
const members = groups.get(branch) ?? [];
|
|
10
|
+
members.push(id);
|
|
11
|
+
groups.set(branch, members);
|
|
12
|
+
}
|
|
13
|
+
return [...groups]
|
|
14
|
+
.map(([branchId, focusIds]) => {
|
|
15
|
+
const points = focusIds.map((focusId) => graph.position.get(focusId));
|
|
16
|
+
const xs = points.map(({ x }) => x);
|
|
17
|
+
const ys = points.map(({ y }) => y);
|
|
18
|
+
return {
|
|
19
|
+
id: branchId,
|
|
20
|
+
declared: graph.branchesDeclared,
|
|
21
|
+
focusCount: focusIds.length,
|
|
22
|
+
focusIds,
|
|
23
|
+
minimumX: Math.min(...xs),
|
|
24
|
+
maximumX: Math.max(...xs),
|
|
25
|
+
minimumY: Math.min(...ys),
|
|
26
|
+
maximumY: Math.max(...ys),
|
|
27
|
+
centroidX: xs.reduce((total, x) => total + x, 0) / xs.length,
|
|
28
|
+
centroidY: ys.reduce((total, y) => total + y, 0) / ys.length,
|
|
29
|
+
};
|
|
30
|
+
})
|
|
31
|
+
.sort((a, b) => a.minimumX - b.minimumX || compareCodeUnits(a.id, b.id));
|
|
32
|
+
}
|
|
@@ -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
|
+
}
|