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.
Files changed (89) hide show
  1. package/CHANGELOG.md +22 -5
  2. package/dist/hoi4_agent_tools/core/artifacts.js +9 -2
  3. package/dist/hoi4_agent_tools/core/changed-summary.js +41 -0
  4. package/dist/hoi4_agent_tools/core/engine-client.js +273 -0
  5. package/dist/hoi4_agent_tools/core/job-worker-host.js +5 -1
  6. package/dist/hoi4_agent_tools/core/operation-result.js +24 -5
  7. package/dist/hoi4_agent_tools/focus/compact.js +197 -18
  8. package/dist/hoi4_agent_tools/focus/engine-layout.js +201 -0
  9. package/dist/hoi4_agent_tools/focus/job-operations.js +3 -1
  10. package/dist/hoi4_agent_tools/focus/layout.js +3 -2
  11. package/dist/hoi4_agent_tools/focus/quality/branches.js +32 -0
  12. package/dist/hoi4_agent_tools/focus/quality/calibration.js +364 -0
  13. package/dist/hoi4_agent_tools/focus/quality/delta.js +85 -0
  14. package/dist/hoi4_agent_tools/focus/quality/focus-quality-calibration.json +2265 -0
  15. package/dist/hoi4_agent_tools/focus/quality/graph.js +170 -0
  16. package/dist/hoi4_agent_tools/focus/quality/index.js +24 -0
  17. package/dist/hoi4_agent_tools/focus/quality/inline-images.js +132 -0
  18. package/dist/hoi4_agent_tools/focus/quality/metrics.js +424 -0
  19. package/dist/hoi4_agent_tools/focus/quality/orthogonal.js +202 -0
  20. package/dist/hoi4_agent_tools/focus/quality/positions.js +41 -0
  21. package/dist/hoi4_agent_tools/focus/quality/schematic.js +241 -0
  22. package/dist/hoi4_agent_tools/focus/quality/scorecard.js +549 -0
  23. package/dist/hoi4_agent_tools/focus/quality/stored-calibration.js +7 -0
  24. package/dist/hoi4_agent_tools/focus/quality/tool-output.js +113 -0
  25. package/dist/hoi4_agent_tools/focus/quality/types.js +5 -0
  26. package/dist/hoi4_agent_tools/focus/tool-operations.js +206 -22
  27. package/dist/hoi4_agent_tools/focus/tool-support.js +1 -1
  28. package/dist/hoi4_agent_tools/map/references.js +26 -1
  29. package/dist/hoi4_agent_tools/map/render.js +29 -3
  30. package/dist/hoi4_agent_tools/map/service.js +3 -0
  31. package/dist/hoi4_agent_tools/map/tool-operations.js +5 -0
  32. package/dist/hoi4_agent_tools/mcp/server/instructions.js +1 -1
  33. package/dist/hoi4_agent_tools/mcp/tools/focus.js +76 -4
  34. package/dist/hoi4_agent_tools/reference/game-text.js +109 -0
  35. package/dist/hoi4_agent_tools/reference/ranking.js +60 -2
  36. package/dist/hoi4_agent_tools/reference/service.js +92 -21
  37. package/dist/hoi4_agent_tools/schemas/focus-requests.js +64 -7
  38. package/dist/hoi4_agent_tools/schemas/focus.js +121 -6
  39. package/dist/hoi4_agent_tools/version.js +1 -1
  40. package/docs/configuration.md +17 -17
  41. package/docs/focus.md +100 -4
  42. package/docs/map.md +5 -2
  43. package/docs/reference.md +1 -1
  44. package/docs/research/reference-benchmark.md +111 -1
  45. package/docs/setup.md +1 -1
  46. package/docs/tools.md +5 -5
  47. package/docs/troubleshooting.md +12 -11
  48. package/npm-shrinkwrap.json +2 -2
  49. package/package.json +2 -1
  50. package/schemas/configuration.schema.json +1 -1
  51. package/schemas/continuous-focus-palette.schema.json +1 -1
  52. package/schemas/custom-weighted-pool.schema.json +1 -1
  53. package/schemas/decision-inspect-input.schema.json +1 -1
  54. package/schemas/error-log-input.schema.json +1 -1
  55. package/schemas/event-feature-manifest.schema.json +1 -1
  56. package/schemas/focus-plan.schema.json +146 -120
  57. package/schemas/focus-planning-sidecar.schema.json +15 -7
  58. package/schemas/geography-model.schema.json +1 -1
  59. package/schemas/gui-animation-source.schema.json +1 -1
  60. package/schemas/gui-helper.schema.json +1 -1
  61. package/schemas/gui-scenario.schema.json +1 -1
  62. package/schemas/helper-expansion-request.schema.json +1 -1
  63. package/schemas/helper-expansion-summary.schema.json +1 -1
  64. package/schemas/impact-inspect-input.schema.json +1 -1
  65. package/schemas/map-operation.schema.json +1 -1
  66. package/schemas/mechanic-test-input.schema.json +1 -1
  67. package/schemas/mod-index-input.schema.json +1 -1
  68. package/schemas/operation-result.schema.json +1 -1
  69. package/schemas/package-check-input.schema.json +1 -1
  70. package/schemas/package-manifest.schema.json +1 -1
  71. package/schemas/probability-analysis-result.schema.json +1 -1
  72. package/schemas/probability-compare-input.schema.json +1 -1
  73. package/schemas/probability-evaluate-input.schema.json +1 -1
  74. package/schemas/probability-inspect-input.schema.json +1 -1
  75. package/schemas/probability-render-input.schema.json +1 -1
  76. package/schemas/probability-scenario-set.schema.json +1 -1
  77. package/schemas/probability-sequence-input.schema.json +1 -1
  78. package/schemas/probability-simulate-input.schema.json +1 -1
  79. package/schemas/probability-sweep-input.schema.json +1 -1
  80. package/schemas/reference-context-input.schema.json +1 -1
  81. package/schemas/reference-read-input.schema.json +1 -1
  82. package/schemas/reference-search-input.schema.json +1 -1
  83. package/schemas/reference-sheets.schema.json +1 -1
  84. package/schemas/scenario-suite.schema.json +1 -1
  85. package/schemas/scenario-test-input.schema.json +1 -1
  86. package/schemas/script-validate-input.schema.json +1 -1
  87. package/schemas/shared-scenario.schema.json +1 -1
  88. package/schemas/source-lookup-input.schema.json +1 -1
  89. package/server.json +2 -2
@@ -0,0 +1,170 @@
1
+ import { compareCodeUnits } from '../../core/canonical.js';
2
+ function sortedUnique(values) {
3
+ return [...new Set(values)].sort(compareCodeUnits);
4
+ }
5
+ /** A focus whose game position depends on an `offset` block the offline tools do not evaluate. */
6
+ function hasOffsetBlock(focus) {
7
+ return focus.rawPassthrough.some((entry) => entry.key === 'offset');
8
+ }
9
+ function declaredBranches(plan) {
10
+ const declared = new Map();
11
+ for (const group of plan.branchGroups)
12
+ for (const id of group.focusIds)
13
+ declared.set(id, group.id);
14
+ for (const focus of plan.focuses) {
15
+ if (focus.branchId !== undefined && !declared.has(focus.id))
16
+ declared.set(focus.id, focus.branchId);
17
+ }
18
+ return declared.size === 0 ? undefined : declared;
19
+ }
20
+ /**
21
+ * Derives branches when none are declared. The trunk of every root runs down its single-successor
22
+ * chain to the first fork; the fork's children are branch roots, and every focus reached from
23
+ * exactly one branch root belongs to it. Focuses reached from several are shared.
24
+ */
25
+ function derivedBranches(nodes, parents, children) {
26
+ const position = new Map(nodes.map((node) => [node.id, node]));
27
+ const roots = nodes
28
+ .filter(({ id }) => (parents.get(id)?.length ?? 0) === 0)
29
+ .sort((left, right) => left.x - right.x || compareCodeUnits(left.id, right.id));
30
+ const branchRoots = [];
31
+ const trunk = new Set();
32
+ for (const root of roots) {
33
+ let current = root.id;
34
+ const walked = new Set();
35
+ while (!walked.has(current)) {
36
+ walked.add(current);
37
+ const next = children.get(current) ?? [];
38
+ if (next.length !== 1)
39
+ break;
40
+ const only = next[0];
41
+ if ((parents.get(only)?.length ?? 0) !== 1)
42
+ break;
43
+ trunk.add(current);
44
+ current = only;
45
+ }
46
+ const fork = children.get(current) ?? [];
47
+ if (fork.length >= 2) {
48
+ trunk.add(current);
49
+ branchRoots.push(...[...fork].sort((left, right) => (position.get(left)?.x ?? 0) - (position.get(right)?.x ?? 0) ||
50
+ compareCodeUnits(left, right)));
51
+ }
52
+ else
53
+ branchRoots.push(current);
54
+ }
55
+ const reach = new Map();
56
+ for (const rootId of branchRoots) {
57
+ const seen = new Set();
58
+ const stack = [rootId];
59
+ while (stack.length > 0) {
60
+ const id = stack.pop();
61
+ if (seen.has(id))
62
+ continue;
63
+ seen.add(id);
64
+ for (const child of children.get(id) ?? [])
65
+ stack.push(child);
66
+ }
67
+ for (const id of seen) {
68
+ const owners = reach.get(id) ?? new Set();
69
+ owners.add(rootId);
70
+ reach.set(id, owners);
71
+ }
72
+ }
73
+ const assignment = new Map();
74
+ for (const node of nodes) {
75
+ const owners = reach.get(node.id);
76
+ const owner = owners?.size === 1 && !trunk.has(node.id) ? [...owners][0] : '';
77
+ assignment.set(node.id, owner);
78
+ }
79
+ return assignment;
80
+ }
81
+ /** Builds the graph the quality metrics read from a plan and the resolved focus positions. */
82
+ export function buildQualityGraph(plan, positions) {
83
+ const nodes = [...positions]
84
+ .map(({ id, x, y }) => ({ id, x, y }))
85
+ .sort((left, right) => compareCodeUnits(left.id, right.id));
86
+ const position = new Map(nodes.map((node) => [node.id, { x: node.x, y: node.y }]));
87
+ const edges = [];
88
+ const exclusionKeys = new Set();
89
+ const exclusions = [];
90
+ const offsetDependent = new Set();
91
+ for (const focus of [...plan.focuses].sort((left, right) => compareCodeUnits(left.id, right.id))) {
92
+ if (!position.has(focus.id))
93
+ continue;
94
+ if (hasOffsetBlock(focus))
95
+ offsetDependent.add(focus.id);
96
+ for (const [groupIndex, group] of focus.prerequisites.groups.entries()) {
97
+ for (const parent of [...group.focusIds].sort(compareCodeUnits)) {
98
+ if (!position.has(parent))
99
+ continue;
100
+ edges.push({ parent, child: focus.id, group: groupIndex, or: group.focusIds.length > 1 });
101
+ }
102
+ }
103
+ for (const other of focus.mutuallyExclusive) {
104
+ if (!position.has(other))
105
+ continue;
106
+ const pair = compareCodeUnits(focus.id, other) < 0 ? [focus.id, other] : [other, focus.id];
107
+ const key = pair.join('\0');
108
+ if (exclusionKeys.has(key))
109
+ continue;
110
+ exclusionKeys.add(key);
111
+ exclusions.push(pair);
112
+ }
113
+ }
114
+ exclusions.sort((left, right) => compareCodeUnits(left.join('\0'), right.join('\0')));
115
+ // A focus placed relative to a shifted or unresolved focus moves with it, so the uncertainty is inherited.
116
+ const anchors = new Map();
117
+ for (const focus of plan.focuses)
118
+ if (focus.position.mode === 'relative')
119
+ anchors.set(focus.id, focus.position.relativeTo);
120
+ const uncertain = (id, seen = new Set()) => {
121
+ if (offsetDependent.has(id))
122
+ return true;
123
+ const anchor = anchors.get(id);
124
+ if (anchor === undefined || seen.has(id))
125
+ return false;
126
+ // An anchor outside this tree (a shared focus) leaves the real position unknown.
127
+ if (!position.has(anchor))
128
+ return true;
129
+ seen.add(id);
130
+ return uncertain(anchor, seen);
131
+ };
132
+ for (const { id } of nodes)
133
+ if (uncertain(id))
134
+ offsetDependent.add(id);
135
+ const conditional = new Set();
136
+ for (const focus of plan.focuses)
137
+ if (position.has(focus.id) &&
138
+ (focus.allowBranch !== undefined || focus.visibility !== 'normal'))
139
+ conditional.add(focus.id);
140
+ for (let changed = true; changed;) {
141
+ changed = false;
142
+ for (const edge of edges)
143
+ if (conditional.has(edge.parent) && !conditional.has(edge.child)) {
144
+ conditional.add(edge.child);
145
+ changed = true;
146
+ }
147
+ }
148
+ const parentSets = new Map(nodes.map(({ id }) => [id, new Set()]));
149
+ const childSets = new Map(nodes.map(({ id }) => [id, new Set()]));
150
+ for (const edge of edges) {
151
+ parentSets.get(edge.child).add(edge.parent);
152
+ childSets.get(edge.parent).add(edge.child);
153
+ }
154
+ const parents = new Map([...parentSets].map(([id, set]) => [id, sortedUnique(set)]));
155
+ const children = new Map([...childSets].map(([id, set]) => [id, sortedUnique(set)]));
156
+ const declared = declaredBranches(plan);
157
+ const branchOf = declared ?? derivedBranches(nodes, parents, children);
158
+ return {
159
+ nodes,
160
+ position,
161
+ edges,
162
+ parents,
163
+ children,
164
+ exclusions,
165
+ offsetDependent,
166
+ conditional,
167
+ branchOf: new Map(nodes.map(({ id }) => [id, branchOf.get(id) ?? ''])),
168
+ branchesDeclared: declared !== undefined,
169
+ };
170
+ }
@@ -0,0 +1,24 @@
1
+ import { buildQualityGraph } from './graph.js';
2
+ import { measureLayoutQuality } from './metrics.js';
3
+ import { reportLayoutQuality } from './scorecard.js';
4
+ import { STORED_QUALITY_CALIBRATION } from './stored-calibration.js';
5
+ export * from './branches.js';
6
+ export * from './calibration.js';
7
+ export * from './graph.js';
8
+ export * from './metrics.js';
9
+ export * from './orthogonal.js';
10
+ export * from './positions.js';
11
+ export * from './scorecard.js';
12
+ export * from './stored-calibration.js';
13
+ export * from './types.js';
14
+ /** Measures one layout of a plan against the vanilla calibration. */
15
+ export function evaluateFocusLayoutQuality(plan, nodes, calibration = STORED_QUALITY_CALIBRATION) {
16
+ const graph = buildQualityGraph(plan, nodes);
17
+ const measurement = measureLayoutQuality(graph);
18
+ const report = reportLayoutQuality(graph, measurement, calibration, { treeId: plan.id });
19
+ return { graph, measurement, report };
20
+ }
21
+ export * from './delta.js';
22
+ export * from './tool-output.js';
23
+ export * from './inline-images.js';
24
+ export * from './schematic.js';
@@ -0,0 +1,132 @@
1
+ import { summarizeBranches } from './branches.js';
2
+ import { qualityMetricInfo } from './calibration.js';
3
+ import { INLINE_IMAGE_LIMIT, regionAround, renderSchematicPng, sortedUnique, treeRegion, } from './schematic.js';
4
+ const SEVERITY_RANK = { review: 0, revise: 1, blocked: 2 };
5
+ function highlightMap(graph, report) {
6
+ const highlight = new Map();
7
+ for (const row of report.scorecard) {
8
+ if (!row.inVerdict)
9
+ continue;
10
+ const severity = row.status === 'blocked'
11
+ ? 'blocked'
12
+ : row.status === 'revise'
13
+ ? 'revise'
14
+ : row.status === 'review'
15
+ ? 'review'
16
+ : undefined;
17
+ if (severity === undefined)
18
+ continue;
19
+ for (const id of row.where) {
20
+ if (!graph.position.has(id))
21
+ continue;
22
+ const known = highlight.get(id);
23
+ if (known === undefined || SEVERITY_RANK[severity] > SEVERITY_RANK[known])
24
+ highlight.set(id, severity);
25
+ }
26
+ }
27
+ return highlight;
28
+ }
29
+ function sameRegion(left, right) {
30
+ return (left.minimumX === right.minimumX &&
31
+ left.maximumX === right.maximumX &&
32
+ left.minimumY === right.minimumY &&
33
+ left.maximumY === right.maximumY);
34
+ }
35
+ /** Which images to draw: the overview, then requested branches and focuses, then the worst findings. */
36
+ export function planInlineImages(graph, report, request) {
37
+ const diagnostics = [];
38
+ const plans = [];
39
+ const add = (plan) => {
40
+ if (plan === undefined || plans.length >= INLINE_IMAGE_LIMIT)
41
+ return;
42
+ if (plans.some(({ region }) => sameRegion(region, plan.region)))
43
+ return;
44
+ plans.push(plan);
45
+ };
46
+ add({
47
+ kind: 'overview',
48
+ label: 'overview',
49
+ focusIds: [],
50
+ region: treeRegion(graph),
51
+ });
52
+ const branches = summarizeBranches(graph);
53
+ for (const branchId of request.cropBranchIds ?? []) {
54
+ const branch = branches.find(({ id }) => id === branchId);
55
+ if (branch === undefined) {
56
+ diagnostics.push({
57
+ code: 'FOCUS_CROP_BRANCH_UNKNOWN',
58
+ severity: 'warning',
59
+ category: 'rendering',
60
+ message: `No branch ${branchId} in this tree; branches are ${sortedUnique(branches.map(({ id }) => id))
61
+ .slice(0, 12)
62
+ .join(', ') || 'not declared'}`,
63
+ details: { branchId, branchIds: sortedUnique(branches.map(({ id }) => id)).slice(0, 50) },
64
+ });
65
+ continue;
66
+ }
67
+ add({
68
+ kind: 'branch',
69
+ label: `branch ${branch.id}`,
70
+ focusIds: branch.focusIds.slice(0, 50),
71
+ region: regionAround(graph, branch.focusIds, { columns: 2, rows: 1 }, { columns: 14, rows: 3 }),
72
+ });
73
+ }
74
+ for (const focusId of request.cropFocusIds ?? []) {
75
+ add({
76
+ kind: 'focus',
77
+ label: `around ${focusId}`,
78
+ focusIds: [focusId],
79
+ region: regionAround(graph, [focusId]),
80
+ });
81
+ }
82
+ const explicit = (request.cropBranchIds?.length ?? 0) + (request.cropFocusIds?.length ?? 0) > 0;
83
+ if (!explicit) {
84
+ // Without a request, show the places the scorecard blames, worst first.
85
+ for (const row of report.scorecard) {
86
+ if (plans.length >= 4)
87
+ break;
88
+ if (!row.inVerdict || !['blocked', 'revise', 'review'].includes(row.status))
89
+ continue;
90
+ const focusIds = row.where.filter((id) => graph.position.has(id));
91
+ const region = regionAround(graph, focusIds.slice(0, 12));
92
+ if (region === undefined)
93
+ continue;
94
+ add({
95
+ kind: 'problem',
96
+ label: `${row.id} ${qualityMetricInfo(row.id).name}`,
97
+ focusIds: focusIds.slice(0, 12),
98
+ region,
99
+ });
100
+ }
101
+ }
102
+ return { plans, diagnostics };
103
+ }
104
+ /** Draws the planned images; each is a PNG of at most about 700 KB. */
105
+ export async function renderInlineImages(graph, report, request, title) {
106
+ if (graph.nodes.length === 0)
107
+ return { images: [], descriptions: [], diagnostics: [] };
108
+ const { plans, diagnostics } = planInlineImages(graph, report, request);
109
+ const highlight = highlightMap(graph, report);
110
+ const images = [];
111
+ const descriptions = [];
112
+ for (const plan of plans) {
113
+ const rendered = await renderSchematicPng(graph, {
114
+ region: plan.region,
115
+ highlight,
116
+ title: plan.kind === 'overview' ? title : `${title} · ${plan.label}`,
117
+ ...(request.budget === undefined ? {} : { budget: request.budget }),
118
+ maximumEdge: plan.kind === 'overview' ? 1500 : 1000,
119
+ });
120
+ images.push({ data: rendered.png.toString('base64'), mimeType: 'image/png' });
121
+ descriptions.push({
122
+ kind: plan.kind,
123
+ label: plan.label,
124
+ focusIds: plan.focusIds,
125
+ region: rendered.region,
126
+ width: rendered.width,
127
+ height: rendered.height,
128
+ bytes: rendered.png.length,
129
+ });
130
+ }
131
+ return { images, descriptions, diagnostics };
132
+ }