hoi4-agent-tools 3.6.0 → 3.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 +11 -0
- package/README.md +122 -28
- package/dist/hoi4_agent_tools/core/engine.js +7 -7
- package/dist/hoi4_agent_tools/core/file-glob.js +164 -0
- package/dist/hoi4_agent_tools/core/index-segments.js +4 -0
- package/dist/hoi4_agent_tools/core/job-operation-registration.js +31 -0
- package/dist/hoi4_agent_tools/core/job-worker-host.js +27 -11
- package/dist/hoi4_agent_tools/core/job-worker.js +8 -16
- package/dist/hoi4_agent_tools/core/persistent-analysis-cache.js +37 -6
- package/dist/hoi4_agent_tools/core/scanner.js +133 -47
- package/dist/hoi4_agent_tools/core/source/cache.js +4 -0
- package/dist/hoi4_agent_tools/map/service.js +2 -2
- package/dist/hoi4_agent_tools/mcp/tools/reference.js +10 -1
- package/dist/hoi4_agent_tools/reference/lines.js +12 -0
- package/dist/hoi4_agent_tools/reference/operations.js +45 -2
- package/dist/hoi4_agent_tools/reference/script-validation.js +277 -0
- package/dist/hoi4_agent_tools/reference/service.js +139 -67
- package/dist/hoi4_agent_tools/reference/source-lookup.js +91 -15
- package/dist/hoi4_agent_tools/reference/source-navigation.js +130 -0
- package/dist/hoi4_agent_tools/schemas/reference.js +55 -0
- package/dist/hoi4_agent_tools/schemas/script-validation.js +69 -0
- package/dist/hoi4_agent_tools/version.js +1 -1
- package/docs/README.md +1 -0
- package/docs/examples.md +1 -11
- package/docs/gui.md +1 -15
- package/docs/reference.md +43 -2
- package/docs/research/reference-benchmark.md +41 -0
- package/docs/tools.md +37 -36
- package/npm-shrinkwrap.json +7088 -0
- package/package.json +7 -6
- 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/event-feature-manifest.schema.json +1 -1
- package/schemas/focus-plan.schema.json +1 -1
- package/schemas/focus-planning-sidecar.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/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 +9 -1
- package/schemas/reference-search-input.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 +92 -0
- package/schemas/shared-scenario.schema.json +1 -1
- package/schemas/source-lookup-input.schema.json +68 -2
- package/server.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,17 @@
|
|
|
2
2
|
|
|
3
3
|
## Unreleased
|
|
4
4
|
|
|
5
|
+
## 3.7.0 - 2026-10-04
|
|
6
|
+
|
|
7
|
+
- Publish an `npm-shrinkwrap.json` so every install reproduces the reviewed dependency closure, including all platforms' optional Sharp binaries (about 300 MB installed).
|
|
8
|
+
- Navigate exact nested source assignments and page through bounded child structure, including repeated keys and same-line blocks.
|
|
9
|
+
- Limit definition-only event/helper lookups to their source family and reuse semantic reference analysis for unchanged snapshots.
|
|
10
|
+
- Check snippet syntax, native command kinds, and declared scopes against cited installed or explicitly selected generated documentation, with unresolved coverage reported separately.
|
|
11
|
+
- Verify source files in bounded parallel batches, reject changing or replaced paths, and bound optional cache persistence without truncating analysis.
|
|
12
|
+
- Load isolated workers' domain modules after authenticating the job, preserving exact operation admission and recorded write policies.
|
|
13
|
+
- Filter reference scans by authority, reuse citation hints, decode supported reference encodings, and preserve byte-bounded continuations and explicit omission counts.
|
|
14
|
+
- Replace the unpatched globbing dependency chain and update vulnerable transitive pins, preserving relative-path, symlink, literal-filename and pattern-complexity checks.
|
|
15
|
+
|
|
5
16
|
## 3.6.0 - 2026-09-27
|
|
6
17
|
|
|
7
18
|
- Preview immediate tooltip text from source localisation with declared position, width and padding, using the source tooltip sprite and font.
|
package/README.md
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
# HOI4 Agent Tools
|
|
2
2
|
|
|
3
|
-
Give your coding agent
|
|
4
|
-
|
|
3
|
+
Give your coding agent one MCP server for Hearts of Iron IV modding.
|
|
4
|
+
It can find local documentation and vanilla examples, inspect a mod, render its trees and interfaces, compare source behavior, and make focused edits to focus trees, GUIs and maps.
|
|
5
|
+
The pictures below use real Chaos Redux or installed HOI4 files; click any image for the full-size version.
|
|
5
6
|
|
|
6
7
|
## Get started
|
|
7
8
|
|
|
@@ -16,37 +17,130 @@ Add the printed configuration to your MCP client and open your mod as its worksp
|
|
|
16
17
|
Keep an installed copy of HOI4 available for vanilla examples, artwork and fonts.
|
|
17
18
|
The [setup guide](docs/setup.md) covers connecting your agent and choosing game or mod paths.
|
|
18
19
|
|
|
19
|
-
|
|
20
|
+
Ask your agent to find a focus tree and render it, locate the installed documentation for a script command, or preview a scripted GUI at different UI scales.
|
|
21
|
+
The examples below show the results.
|
|
20
22
|
|
|
21
|
-
|
|
23
|
+
## Examples
|
|
22
24
|
|
|
23
|
-
|
|
25
|
+
[Focus trees](#focus-trees-and-alternative-prerequisites) · [Technology folders](#technology-folders-and-card-artwork) · [Interfaces](#scripted-interfaces) · [Maps and events](#maps-and-event-chains) · [Local rules and AI](#local-rules-and-ai-weights) · [All tools](#complete-tool-list)
|
|
24
26
|
|
|
25
|
-
|
|
27
|
+
### Focus trees and alternative prerequisites
|
|
26
28
|
|
|
27
|
-
|
|
29
|
+
| Tree or branch | Supplied in-game capture | MCP source preview |
|
|
30
|
+
| ---------------- | --------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------ |
|
|
31
|
+
| Fury | [](docs/images/comparisons/fury-ingame.png) | [](docs/images/comparisons/fury-mcp.png) |
|
|
32
|
+
| Holy Realm | [](docs/images/comparisons/holy-realm-ingame.png) | [](docs/images/comparisons/holy-realm-mcp.png) |
|
|
33
|
+
| Utopia Manifesto | [](docs/images/comparisons/utopia-ingame.png) | [](docs/images/comparisons/utopia-mcp.png) |
|
|
28
34
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
35
|
+
Dotted connectors in the MCP preview mark focuses where either preceding focus satisfies one prerequisite group.
|
|
36
|
+
The tree previews show route structure; the game's focus screen has different framing, labels and icons.
|
|
37
|
+
The [focus guide](docs/focus.md) explains inspection, rendering and edits, and the [comparison details](docs/visual-comparisons.md#focus-trees) retain source revisions.
|
|
38
|
+
|
|
39
|
+
### Technology folders and card artwork
|
|
40
|
+
|
|
41
|
+
> Render a technology folder. Check card sizes, artwork, links and missing placements.
|
|
42
|
+
|
|
43
|
+
| Folder | Supplied in-game capture | MCP source preview |
|
|
44
|
+
| ---------------- | ------------------------------------------------------------------------------------------------------------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------- |
|
|
45
|
+
| Infantry | [](docs/images/comparisons/infantry-ingame.png) | [](docs/images/comparisons/infantry-mcp.png) |
|
|
46
|
+
| Chemical warfare | [](docs/images/comparisons/chemical-recent-ingame.png) | [](docs/images/comparisons/chemical-historical-mcp.png) |
|
|
47
|
+
|
|
48
|
+
The chemical comparison uses the source revision recorded with the screenshot and includes wide equipment cards.
|
|
49
|
+
The [technology comparison](docs/visual-comparisons.md#technology-folders) records its source revision, and the [technology guide](docs/technology.md) covers inspection and doctrine paths.
|
|
50
|
+
|
|
51
|
+
### Scripted interfaces
|
|
52
|
+
|
|
53
|
+
> Preview an interface with its game assets and fonts. Check states, text clipping and clickable areas at 100% and 125% UI scale.
|
|
54
|
+
|
|
55
|
+
| Interface | Supplied in-game capture | MCP source preview |
|
|
56
|
+
| ----------------------- | ----------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------- |
|
|
57
|
+
| Video options | [](docs/images/comparisons/gui/options-video-ingame.png) | [](docs/images/comparisons/gui/options-video-mcp.png) |
|
|
58
|
+
| Škoda Priority | [](docs/images/comparisons/gui/skoda-priority-ingame.png) | [](docs/images/comparisons/gui/skoda-priority-mcp.png) |
|
|
59
|
+
| Chaos Redux Tag Manager | [](docs/images/comparisons/gui/tag-manager-ingame.png) | [](docs/images/comparisons/gui/tag-manager-mcp.png) |
|
|
60
|
+
|
|
61
|
+
The Tag Manager screenshot comes from an older layout.
|
|
62
|
+
The Options preview has no observed gamma value, and Škoda's game capture frames its background differently.
|
|
63
|
+
|
|
64
|
+
| Event Log | Chaos Meter: status | Chaos Meter: deaths |
|
|
65
|
+
| -------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------- |
|
|
66
|
+
| [](docs/images/comparisons/gui/event-log-mcp.png) | [](docs/images/comparisons/gui/chaos-meter-status-mcp.png) | [](docs/images/comparisons/gui/chaos-meter-deaths-mcp.png) |
|
|
67
|
+
|
|
68
|
+
| Vanilla decisions | Vanilla occupation |
|
|
69
|
+
| -------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
70
|
+
| [](docs/images/comparisons/native-ui/countrydecisionview-window.png) | [](docs/images/comparisons/native-ui/countryoccupationview-window.png) |
|
|
71
|
+
|
|
72
|
+
The Event Log, Chaos Meter, decision and occupation previews use declared states or positions for layout review.
|
|
73
|
+
They are source previews, not observations of a running campaign.
|
|
74
|
+
See the [interface gallery](docs/gui-comparisons.md) for full images and fidelity notes, and the [GUI guide](docs/gui.md) for scenario inputs.
|
|
75
|
+
|
|
76
|
+
### Maps and event chains
|
|
77
|
+
|
|
78
|
+
> Find Brandenburg on the map, inspect its provinces and neighbours, and show the affected source files before a change.
|
|
79
|
+
|
|
80
|
+
> Trace an event's options and follow-up events. Point out unresolved calls and link each route to its source.
|
|
81
|
+
|
|
82
|
+
| Source map | Chaos Redux event chain |
|
|
83
|
+
| ------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------- |
|
|
84
|
+
| [](docs/images/comparisons/source/map-mcp.png) | [](docs/images/comparisons/source/event-chain-mcp.png) |
|
|
85
|
+
|
|
86
|
+
The [map guide](docs/map.md) covers connected province, state, supply and railway data.
|
|
87
|
+
The [event guide](docs/events.md) covers routes, scope, timing and unresolved references.
|
|
88
|
+
|
|
89
|
+
### Local rules and AI weights
|
|
90
|
+
|
|
91
|
+
> Find the installed documentation for `save_event_target_as`, read the relevant section, and show a vanilla usage with file and line references.
|
|
92
|
+
|
|
93
|
+
> Compare this decision's `ai_will_do` score before and after a change in peace and war. List missing conditions separately from known results.
|
|
94
|
+
|
|
95
|
+
The [reference example](docs/research/reference-benchmark.md) found the installed effect documentation first and read its relevant 12-line section instead of loading whole wiki pages.
|
|
96
|
+
The [AI guide](docs/probability.md) covers supported weights, chance and timing models; a source score alone does not establish an AI click probability.
|
|
97
|
+
The [examples guide](docs/examples.md) has more prompt details.
|
|
98
|
+
|
|
99
|
+
## Complete tool list
|
|
100
|
+
|
|
101
|
+
The default server exposes 35 tools.
|
|
102
|
+
Your agent can select them from ordinary language; use the live tool schema for call parameters.
|
|
103
|
+
|
|
104
|
+
| Tool | Purpose |
|
|
105
|
+
| --------------------------- | -------------------------------------------------------------------------------------------- |
|
|
106
|
+
| `hoi4.focus_inspect` | Read focus trees, continuous-focus placement, and structural or reference problems. |
|
|
107
|
+
| `hoi4.focus_render` | Produce fast HTML, SVG, JSON, and source-linked layout artifacts. |
|
|
108
|
+
| `hoi4.focus_raster` | Produce a PNG preview with decoded source icons. |
|
|
109
|
+
| `hoi4.focus_rewrite` | Create or update a focus tree. |
|
|
110
|
+
| `hoi4.gui_inspect` | Read a scripted GUI and its linked assets and logic. |
|
|
111
|
+
| `hoi4.gui_render` | Render generated and explicit GUI scenarios, states, resolutions, and layout diagnostics. |
|
|
112
|
+
| `hoi4.gui_rewrite` | Create or update a GUI source package. |
|
|
113
|
+
| `hoi4.map_inspect` | Search, click, navigate, and inspect the complete rendered map and its linked data. |
|
|
114
|
+
| `hoi4.map_render` | Render full-map layers, overlays, names, IDs, coordinates, and source-linked catalogs. |
|
|
115
|
+
| `hoi4.map_rewrite` | Create or update states, provinces, IDs, networks, positions, and connected map data. |
|
|
116
|
+
| `hoi4.reference_context` | Get compact, cited wiki and installed documentation pointers for a modding surface. |
|
|
117
|
+
| `hoi4.reference_search` | Search bounded local documentation sections. |
|
|
118
|
+
| `hoi4.reference_read` | Read one revision-bound section, with line continuation. |
|
|
119
|
+
| `hoi4.source_lookup` | Find exact definitions, overrides, usages, and narrow source blocks. |
|
|
120
|
+
| `hoi4.script_validate` | Check snippet syntax, native command kinds, and declared scopes against local documentation. |
|
|
121
|
+
| `hoi4.event_inspect` | Scan, trace, explain, lint, or assess event chains and their state flow. |
|
|
122
|
+
| `hoi4.event_render` | Render source-linked event routes, options, timing, state, scope, and unresolved edges. |
|
|
123
|
+
| `hoi4.event_compare` | Compare event-chain topology and diagnostics between revisions. |
|
|
124
|
+
| `hoi4.impact_inspect` | Trace symbol and changed-file consumers across source systems and compare proposals. |
|
|
125
|
+
| `hoi4.decision_inspect` | Inventory and evaluate decisions or missions under declared scenarios and source changes. |
|
|
126
|
+
| `hoi4.mechanic_test` | Execute bounded source effects on a copied declared scenario and check assertions. |
|
|
127
|
+
| `hoi4.package_check` | Check declarative package definitions, calls, registrations, assets, and case links. |
|
|
128
|
+
| `hoi4.scenario_test` | Run named source and domain cases in resumable, revision-bound batches. |
|
|
129
|
+
| `hoi4.job_inspect` | Inspect durable background work or retrieve its completed tool result. |
|
|
130
|
+
| `hoi4.job_cancel` | Durably request cancellation of authorized background work. |
|
|
131
|
+
| `hoi4.tech_inspect` | Scan, trace, explain, lint, and assess technology and doctrine systems. |
|
|
132
|
+
| `hoi4.tech_render` | Render source layouts with real item sizes and year guides, plus dependencies and assets. |
|
|
133
|
+
| `hoi4.tech_compare` | Compare technology graphs, placements, references, diagnostics, and source overlays. |
|
|
134
|
+
| `hoi4.probability_inspect` | Locate weighted logic and discover compatible adapters, candidates, and required inputs. |
|
|
135
|
+
| `hoi4.probability_evaluate` | Evaluate supported weights, probability and timing models, and missing inputs. |
|
|
136
|
+
| `hoi4.probability_sweep` | Find sensitivity, breakpoints, cliffs, and rank reversals across declared ranges. |
|
|
137
|
+
| `hoi4.probability_simulate` | Run deterministic sampled analysis with confidence and convergence data. |
|
|
138
|
+
| `hoi4.probability_sequence` | Analyze declared recovery, caps, cooldowns, resets, timers, and terminal states. |
|
|
139
|
+
| `hoi4.probability_compare` | Attribute AI-weight and MTTH changes between real or proposed source. |
|
|
140
|
+
| `hoi4.probability_render` | Render cached rankings, matrices, timing, sensitivity, sequence, and comparisons. |
|
|
141
|
+
|
|
142
|
+
Set `HOI4_AGENT_TOOLS_CHAOSX=1` to expose two optional Chaos Redux workflow tools: `chaosx.focus_country_assets` and `chaosx.visual_revision`.
|
|
143
|
+
The [tool guide](docs/tools.md) explains results, linked artifacts and long operations.
|
|
50
144
|
|
|
51
145
|
## How it fits your workflow
|
|
52
146
|
|
|
@@ -182,13 +182,6 @@ export class CoreEngine {
|
|
|
182
182
|
signal: controller.signal,
|
|
183
183
|
});
|
|
184
184
|
controller.signal.throwIfAborted();
|
|
185
|
-
const persistentAnalysisCache = await this.persistentAnalysisCache;
|
|
186
|
-
const analysisScope = {
|
|
187
|
-
workspaceIdentity: workspace.workspaceIdentity,
|
|
188
|
-
rootFingerprint: transactionRootFingerprint(workspace),
|
|
189
|
-
principal: principal ?? null,
|
|
190
|
-
};
|
|
191
|
-
await persistentAnalysisCache?.hydrate(analysisScope, files, sourceDocuments, this.indexSegments, controller.signal);
|
|
192
185
|
const revision = hashCanonical(files.map(({ absolutePath, displayPath, relativePath, rootKind, loadOrder, shadowedBy, sha256, }) => ({
|
|
193
186
|
absolutePath,
|
|
194
187
|
displayPath,
|
|
@@ -202,6 +195,13 @@ export class CoreEngine {
|
|
|
202
195
|
const cached = this.#scanCache.get(cacheKey);
|
|
203
196
|
if (cached !== undefined)
|
|
204
197
|
return cached;
|
|
198
|
+
const persistentAnalysisCache = await this.persistentAnalysisCache;
|
|
199
|
+
const analysisScope = {
|
|
200
|
+
workspaceIdentity: workspace.workspaceIdentity,
|
|
201
|
+
rootFingerprint: transactionRootFingerprint(workspace),
|
|
202
|
+
principal: principal ?? null,
|
|
203
|
+
};
|
|
204
|
+
await persistentAnalysisCache?.hydrate(analysisScope, files, sourceDocuments, this.indexSegments, controller.signal);
|
|
205
205
|
const index = await SymbolIndex.buildAsync(files, controller.signal, this.indexSegments);
|
|
206
206
|
await persistentAnalysisCache?.persist(analysisScope, files, sourceDocuments, this.indexSegments, controller.signal);
|
|
207
207
|
const snapshot = {
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { escape, globIterate, hasMagic, Ignore, unescape } from 'glob';
|
|
3
|
+
import { lstat, readdir } from 'node:fs/promises';
|
|
4
|
+
import { ServiceError } from './result.js';
|
|
5
|
+
function validatePatterns(patterns) {
|
|
6
|
+
if (patterns.length > 8192)
|
|
7
|
+
throw new ServiceError('SCAN_PATTERN_LIMIT', 'Too many source glob patterns');
|
|
8
|
+
let wildcardCount = 0;
|
|
9
|
+
for (const pattern of patterns) {
|
|
10
|
+
if (Buffer.byteLength(pattern, 'utf8') > 4096)
|
|
11
|
+
throw new ServiceError('SCAN_PATTERN_LIMIT', 'Source glob pattern exceeds its byte limit');
|
|
12
|
+
if (pattern.startsWith('/') ||
|
|
13
|
+
/^[A-Za-z]:/u.test(pattern) ||
|
|
14
|
+
pattern.split('/').includes('..') ||
|
|
15
|
+
pattern.includes('\0'))
|
|
16
|
+
throw new ServiceError('SCAN_PATTERN_ESCAPE', 'Source glob patterns must stay relative to the authorized root');
|
|
17
|
+
let depth = 0;
|
|
18
|
+
const groups = [];
|
|
19
|
+
let expansions = 1;
|
|
20
|
+
for (let index = 0; index < pattern.length; index++) {
|
|
21
|
+
const character = pattern[index];
|
|
22
|
+
if (character === '\\') {
|
|
23
|
+
index++;
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
if (character === '{') {
|
|
27
|
+
if (++depth > 8)
|
|
28
|
+
throw new ServiceError('SCAN_PATTERN_LIMIT', 'Source glob brace nesting exceeds its limit');
|
|
29
|
+
groups.push({ start: index + 1, choices: 1 });
|
|
30
|
+
}
|
|
31
|
+
if (character === ',' && groups.length > 0)
|
|
32
|
+
groups.at(-1).choices++;
|
|
33
|
+
if (character === '}') {
|
|
34
|
+
depth = Math.max(0, depth - 1);
|
|
35
|
+
const group = groups.pop();
|
|
36
|
+
if (group === undefined)
|
|
37
|
+
continue;
|
|
38
|
+
const range = /^(-?\d+|[A-Za-z])\.\.(-?\d+|[A-Za-z])(?:\.\.(-?\d+))?$/u.exec(pattern.slice(group.start, index));
|
|
39
|
+
if (range !== null) {
|
|
40
|
+
const first = /^-?\d+$/u.test(range[1]) ? Number(range[1]) : range[1].charCodeAt(0);
|
|
41
|
+
const last = /^-?\d+$/u.test(range[2]) ? Number(range[2]) : range[2].charCodeAt(0);
|
|
42
|
+
const step = Math.abs(Number(range[3] ?? 1));
|
|
43
|
+
group.choices = Math.floor(Math.abs(last - first) / step) + 1;
|
|
44
|
+
}
|
|
45
|
+
expansions *= group.choices;
|
|
46
|
+
if (!Number.isSafeInteger(expansions) || expansions > 128)
|
|
47
|
+
throw new ServiceError('SCAN_PATTERN_LIMIT', 'Source glob expansion exceeds its limit');
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
// Validate brace complexity before invoking the glob parser, which can expand braces.
|
|
51
|
+
if (hasMagic(pattern, { magicalBraces: true }) && ++wildcardCount > 512)
|
|
52
|
+
throw new ServiceError('SCAN_PATTERN_LIMIT', 'Too many wildcard source glob patterns');
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
/** Relative, cancellable enumeration without traversing symbolic-link directories. */
|
|
56
|
+
export async function* iterateFiles(patterns, options) {
|
|
57
|
+
validatePatterns(patterns);
|
|
58
|
+
validatePatterns(options.ignore ?? []);
|
|
59
|
+
options.signal?.throwIfAborted();
|
|
60
|
+
const nocase = options.caseSensitive === false;
|
|
61
|
+
const excluded = new Ignore([...(options.ignore ?? [])], { nocase });
|
|
62
|
+
const seen = new Set();
|
|
63
|
+
const allowedPatterns = [];
|
|
64
|
+
for (const pattern of new Set(patterns)) {
|
|
65
|
+
let current = options.cwd;
|
|
66
|
+
let allowed = true;
|
|
67
|
+
let literal = true;
|
|
68
|
+
let finalIsFile = false;
|
|
69
|
+
for (const part of pattern.split('/')) {
|
|
70
|
+
if (hasMagic(part, { magicalBraces: true })) {
|
|
71
|
+
literal = false;
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
let name = unescape(part);
|
|
75
|
+
let candidate = path.resolve(current, name);
|
|
76
|
+
const relative = path.relative(options.cwd, candidate).replaceAll('\\', '/');
|
|
77
|
+
if (relative === '..' || relative.startsWith('../') || path.isAbsolute(relative))
|
|
78
|
+
throw new ServiceError('SCAN_PATTERN_ESCAPE', 'Literal glob prefix escaped its authorized root');
|
|
79
|
+
let metadata;
|
|
80
|
+
try {
|
|
81
|
+
metadata = await lstat(candidate);
|
|
82
|
+
}
|
|
83
|
+
catch (error) {
|
|
84
|
+
if (error.code !== 'ENOENT' &&
|
|
85
|
+
error.code !== 'ENOTDIR')
|
|
86
|
+
throw error;
|
|
87
|
+
if (!nocase) {
|
|
88
|
+
allowed = false;
|
|
89
|
+
break;
|
|
90
|
+
}
|
|
91
|
+
try {
|
|
92
|
+
name =
|
|
93
|
+
(await readdir(current)).find((entry) => entry.toLowerCase() === name.toLowerCase()) ??
|
|
94
|
+
name;
|
|
95
|
+
candidate = path.resolve(current, name);
|
|
96
|
+
metadata = await lstat(candidate);
|
|
97
|
+
}
|
|
98
|
+
catch {
|
|
99
|
+
allowed = false;
|
|
100
|
+
break;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
if (metadata.isSymbolicLink()) {
|
|
104
|
+
allowed = false;
|
|
105
|
+
break;
|
|
106
|
+
}
|
|
107
|
+
finalIsFile = metadata.isFile();
|
|
108
|
+
current = candidate;
|
|
109
|
+
}
|
|
110
|
+
if (!allowed)
|
|
111
|
+
continue;
|
|
112
|
+
if (literal) {
|
|
113
|
+
if (!finalIsFile)
|
|
114
|
+
continue;
|
|
115
|
+
const relative = path.relative(options.cwd, current).replaceAll('\\', '/');
|
|
116
|
+
if (excluded.relative.some((rule) => rule.match(relative)) ||
|
|
117
|
+
excluded.absolute.some((rule) => rule.match(current)))
|
|
118
|
+
continue;
|
|
119
|
+
options.signal?.throwIfAborted();
|
|
120
|
+
if (!seen.has(relative)) {
|
|
121
|
+
seen.add(relative);
|
|
122
|
+
yield relative;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
else
|
|
126
|
+
allowedPatterns.push(pattern);
|
|
127
|
+
}
|
|
128
|
+
if (allowedPatterns.length === 0)
|
|
129
|
+
return;
|
|
130
|
+
const matches = globIterate(allowedPatterns, {
|
|
131
|
+
cwd: options.cwd,
|
|
132
|
+
withFileTypes: true,
|
|
133
|
+
nodir: true,
|
|
134
|
+
dot: false,
|
|
135
|
+
follow: false,
|
|
136
|
+
nocase,
|
|
137
|
+
...(options.signal === undefined ? {} : { signal: options.signal }),
|
|
138
|
+
ignore: {
|
|
139
|
+
ignored: (entry) => entry.isSymbolicLink() || excluded.ignored(entry),
|
|
140
|
+
childrenIgnored: (entry) => entry.isSymbolicLink() || excluded.childrenIgnored(entry),
|
|
141
|
+
},
|
|
142
|
+
});
|
|
143
|
+
for await (const entry of matches) {
|
|
144
|
+
options.signal?.throwIfAborted();
|
|
145
|
+
if (!entry.isFile() || entry.isSymbolicLink())
|
|
146
|
+
continue;
|
|
147
|
+
const relative = path.relative(options.cwd, entry.fullpath()).replaceAll('\\', '/');
|
|
148
|
+
if (relative === '..' || relative.startsWith('../') || path.isAbsolute(relative))
|
|
149
|
+
throw new ServiceError('SCAN_PATTERN_ESCAPE', 'Glob result escaped its authorized root');
|
|
150
|
+
if (!seen.has(relative)) {
|
|
151
|
+
seen.add(relative);
|
|
152
|
+
yield relative;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
export async function globFiles(patterns, options) {
|
|
157
|
+
const files = [];
|
|
158
|
+
for await (const file of iterateFiles(patterns, options))
|
|
159
|
+
files.push(file);
|
|
160
|
+
return files;
|
|
161
|
+
}
|
|
162
|
+
export function escapeFileGlob(relativePath) {
|
|
163
|
+
return escape(relativePath, { magicalBraces: true });
|
|
164
|
+
}
|
|
@@ -38,6 +38,10 @@ export class IndexSegmentCache {
|
|
|
38
38
|
this.#entries.set(address, entry);
|
|
39
39
|
return JSON.parse(entry.encoded);
|
|
40
40
|
}
|
|
41
|
+
/** Availability check without decoding or cache-hit accounting. */
|
|
42
|
+
has(address) {
|
|
43
|
+
return this.#entries.has(address);
|
|
44
|
+
}
|
|
41
45
|
/** Import one authenticated canonical entry without counting it as a process-local hit. */
|
|
42
46
|
importEncoded(address, encoded) {
|
|
43
47
|
const segment = JSON.parse(encoded);
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { JobOperations } from './job-executor.js';
|
|
2
|
+
import { ServiceError } from './result.js';
|
|
3
|
+
/** Load one trusted domain after the authenticated job identifies its operation. */
|
|
4
|
+
export async function registerWorkerOperations(engine, toolName) {
|
|
5
|
+
const operations = new JobOperations();
|
|
6
|
+
if (toolName.startsWith('hoi4.event_'))
|
|
7
|
+
(await import('../event/job-operations.js')).registerEventJobs(operations, engine);
|
|
8
|
+
else if (toolName.startsWith('hoi4.focus_'))
|
|
9
|
+
(await import('../focus/job-operations.js')).registerFocusJobs(operations, engine);
|
|
10
|
+
else if (toolName.startsWith('hoi4.gui_'))
|
|
11
|
+
(await import('../gui/job-operations.js')).registerGuiJobs(operations, engine);
|
|
12
|
+
else if (toolName.startsWith('hoi4.map_'))
|
|
13
|
+
(await import('../map/job-operations.js')).registerMapJobs(operations, engine);
|
|
14
|
+
else if (toolName.startsWith('hoi4.probability_'))
|
|
15
|
+
(await import('../probability/job-operations.js')).registerProbabilityJobs(operations, engine);
|
|
16
|
+
else if (toolName.startsWith('hoi4.tech_'))
|
|
17
|
+
(await import('../technology/job-operations.js')).registerTechnologyJobs(operations, engine);
|
|
18
|
+
else if ([
|
|
19
|
+
'hoi4.impact_inspect',
|
|
20
|
+
'hoi4.decision_inspect',
|
|
21
|
+
'hoi4.mechanic_test',
|
|
22
|
+
'hoi4.package_check',
|
|
23
|
+
'hoi4.scenario_test',
|
|
24
|
+
].includes(toolName))
|
|
25
|
+
(await import('./analysis-job-operations.js')).registerAnalysisJobs(operations, engine);
|
|
26
|
+
else
|
|
27
|
+
throw new ServiceError('JOB_OPERATION_UNAVAILABLE', 'No trusted worker domain matches this job');
|
|
28
|
+
// A matching prefix never authorizes an unknown suffix or a client-selected module.
|
|
29
|
+
operations.get(toolName);
|
|
30
|
+
return operations;
|
|
31
|
+
}
|
|
@@ -149,9 +149,11 @@ export class JobWorkerHost {
|
|
|
149
149
|
stdio: ['ignore', 'ignore', 'ignore', 'ipc'],
|
|
150
150
|
windowsHide: true,
|
|
151
151
|
});
|
|
152
|
+
let phase = 'waiting_for_ready';
|
|
153
|
+
let acknowledgmentTimer;
|
|
152
154
|
const initialized = new Promise((resolve, reject) => {
|
|
153
155
|
child.once('error', reject);
|
|
154
|
-
child.once('exit', () => reject(new ServiceError('JOB_WORKER_STARTUP_EXIT',
|
|
156
|
+
child.once('exit', (code, signal) => reject(new ServiceError('JOB_WORKER_STARTUP_EXIT', `The worker exited before completing its startup handshake (phase ${phase}, exit ${code ?? 'none'}, signal ${signal ?? 'none'})`)));
|
|
155
157
|
child.once('message', (value) => {
|
|
156
158
|
void (async () => {
|
|
157
159
|
if (typeof value !== 'object' ||
|
|
@@ -162,7 +164,23 @@ export class JobWorkerHost {
|
|
|
162
164
|
throw new ServiceError('JOB_WORKER_PROTOCOL', 'The worker did not provide its readiness handshake');
|
|
163
165
|
await lease.handoffToProcess(child.pid);
|
|
164
166
|
await new Promise((resolve, reject) => {
|
|
165
|
-
|
|
167
|
+
phase = 'waiting_for_acceptance';
|
|
168
|
+
acknowledgmentTimer = setTimeout(() => reject(new ServiceError('JOB_WORKER_ACCEPT_TIMEOUT', 'The worker did not acknowledge receipt within its startup window')), 30_000);
|
|
169
|
+
child.once('message', (accepted) => {
|
|
170
|
+
if (typeof accepted !== 'object' ||
|
|
171
|
+
accepted === null ||
|
|
172
|
+
!('type' in accepted) ||
|
|
173
|
+
accepted.type !== 'accepted') {
|
|
174
|
+
reject(new ServiceError('JOB_WORKER_PROTOCOL', 'The worker did not acknowledge the dispatched job'));
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
phase = 'accepted';
|
|
178
|
+
// Only close IPC after the child confirms receipt. The persistent job,
|
|
179
|
+
// rather than the launcher's lifetime, owns execution after this point.
|
|
180
|
+
if (child.connected)
|
|
181
|
+
child.disconnect();
|
|
182
|
+
resolve();
|
|
183
|
+
});
|
|
166
184
|
child.send({
|
|
167
185
|
configuration: {
|
|
168
186
|
...configuration,
|
|
@@ -178,14 +196,7 @@ export class JobWorkerHost {
|
|
|
178
196
|
jobId: id,
|
|
179
197
|
...(principal === undefined ? {} : { principal }),
|
|
180
198
|
}, (error) => {
|
|
181
|
-
if (error
|
|
182
|
-
// IPC is startup-only. Closing it after dispatch prevents launcher
|
|
183
|
-
// lifetime from becoming an implicit cancellation channel.
|
|
184
|
-
if (child.connected)
|
|
185
|
-
child.disconnect();
|
|
186
|
-
resolve();
|
|
187
|
-
}
|
|
188
|
-
else
|
|
199
|
+
if (error !== null)
|
|
189
200
|
reject(error);
|
|
190
201
|
});
|
|
191
202
|
});
|
|
@@ -199,7 +210,12 @@ export class JobWorkerHost {
|
|
|
199
210
|
});
|
|
200
211
|
});
|
|
201
212
|
});
|
|
202
|
-
|
|
213
|
+
try {
|
|
214
|
+
await initialized;
|
|
215
|
+
}
|
|
216
|
+
finally {
|
|
217
|
+
clearTimeout(acknowledgmentTimer);
|
|
218
|
+
}
|
|
203
219
|
for (;;) {
|
|
204
220
|
const record = await this.jobs.get(workspaceId, id, principal);
|
|
205
221
|
if (['completed', 'failed', 'cancelled'].includes(record.status))
|
|
@@ -1,16 +1,10 @@
|
|
|
1
1
|
import { z } from 'zod/v4';
|
|
2
2
|
import { serverConfigurationSchema } from './configuration.js';
|
|
3
3
|
import { CoreEngine } from './engine.js';
|
|
4
|
-
import { JobExecutor
|
|
4
|
+
import { JobExecutor } from './job-executor.js';
|
|
5
5
|
import { JobService } from './job-service.js';
|
|
6
6
|
import { WorkspaceResolver } from './workspace.js';
|
|
7
|
-
import {
|
|
8
|
-
import { registerFocusJobs } from '../focus/job-operations.js';
|
|
9
|
-
import { registerGuiJobs } from '../gui/job-operations.js';
|
|
10
|
-
import { registerMapJobs } from '../map/job-operations.js';
|
|
11
|
-
import { registerProbabilityJobs } from '../probability/job-operations.js';
|
|
12
|
-
import { registerTechnologyJobs } from '../technology/job-operations.js';
|
|
13
|
-
import { registerAnalysisJobs } from './analysis-job-operations.js';
|
|
7
|
+
import { registerWorkerOperations } from './job-operation-registration.js';
|
|
14
8
|
// Fixed internal process entry point. It accepts only a trusted host's structured IPC
|
|
15
9
|
// message, never a script path, module name, callback, command, or transport request.
|
|
16
10
|
const inputSchema = z
|
|
@@ -38,19 +32,17 @@ process.once('message', (value) => {
|
|
|
38
32
|
clearTimeout(startup);
|
|
39
33
|
void (async () => {
|
|
40
34
|
const input = inputSchema.parse(value);
|
|
35
|
+
// The send callback confirms enqueueing, not receipt. Acknowledge before the host
|
|
36
|
+
// disconnects; losing that channel after receipt never cancels durable execution.
|
|
37
|
+
if (process.connected)
|
|
38
|
+
process.send?.({ type: 'accepted' }, () => undefined);
|
|
41
39
|
const resolver = await WorkspaceResolver.create(input.configuration);
|
|
42
40
|
const engine = new CoreEngine(resolver);
|
|
43
41
|
// Recovery is targeted by the job executor. Do not sweep or interfere with
|
|
44
42
|
// unrelated live rewrite journals when a worker starts.
|
|
45
43
|
const jobs = await JobService.create(resolver);
|
|
46
|
-
const
|
|
47
|
-
|
|
48
|
-
registerAnalysisJobs(operations, engine);
|
|
49
|
-
registerTechnologyJobs(operations, engine);
|
|
50
|
-
registerProbabilityJobs(operations, engine);
|
|
51
|
-
registerMapJobs(operations, engine);
|
|
52
|
-
registerGuiJobs(operations, engine);
|
|
53
|
-
registerFocusJobs(operations, engine);
|
|
44
|
+
const record = await jobs.get(input.workspaceId, input.jobId, input.principal);
|
|
45
|
+
const operations = await registerWorkerOperations(engine, record.request.toolName);
|
|
54
46
|
await new JobExecutor(engine, jobs, operations).run(input.workspaceId, input.jobId, input.principal);
|
|
55
47
|
})()
|
|
56
48
|
.catch(() => {
|