hoi4-agent-tools 4.20.2 → 4.21.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 -0
- package/dist/bin/stdio.js +5 -2
- package/dist/hoi4_agent_tools/characters/write-operations.js +2 -2
- package/dist/hoi4_agent_tools/characters/write-plan.js +21 -205
- package/dist/hoi4_agent_tools/core/artifacts-engine.js +272 -0
- package/dist/hoi4_agent_tools/core/configuration.js +6 -0
- package/dist/hoi4_agent_tools/core/engine.js +77 -17
- package/dist/hoi4_agent_tools/core/idle-cache-lifetime.js +7 -0
- package/dist/hoi4_agent_tools/core/operation-tasks.js +3 -1
- package/dist/hoi4_agent_tools/core/transactions.js +6 -0
- package/dist/hoi4_agent_tools/core/workspace.js +130 -19
- package/dist/hoi4_agent_tools/decision/write-operations.js +7 -8
- package/dist/hoi4_agent_tools/decision/write-plan.js +25 -245
- package/dist/hoi4_agent_tools/doctrine/write-operations.js +2 -2
- package/dist/hoi4_agent_tools/doctrine/write-plan.js +21 -180
- package/dist/hoi4_agent_tools/event/graph.js +3 -3
- package/dist/hoi4_agent_tools/event/job-operations.js +2 -2
- package/dist/hoi4_agent_tools/event/write-job-operations.js +2 -2
- package/dist/hoi4_agent_tools/event/write-operations.js +9 -10
- package/dist/hoi4_agent_tools/event/write-plan.js +28 -278
- package/dist/hoi4_agent_tools/focus/job-operations.js +2 -2
- package/dist/hoi4_agent_tools/gui/assets.js +3 -13
- package/dist/hoi4_agent_tools/gui/job-operations.js +2 -2
- package/dist/hoi4_agent_tools/gui/layout.js +2 -4
- package/dist/hoi4_agent_tools/gui/renderer.js +2 -1
- package/dist/hoi4_agent_tools/gui/source-graph.js +16 -0
- package/dist/hoi4_agent_tools/gui/studio.js +31 -0
- package/dist/hoi4_agent_tools/ideas/write-operations.js +7 -8
- package/dist/hoi4_agent_tools/ideas/write-plan.js +24 -304
- package/dist/hoi4_agent_tools/map/engine-validation.js +230 -0
- package/dist/hoi4_agent_tools/map/job-operations.js +2 -2
- package/dist/hoi4_agent_tools/map/model.js +17 -3
- package/dist/hoi4_agent_tools/map/validation.js +12 -18
- package/dist/hoi4_agent_tools/mcp/server/create-modern-operations.js +9 -0
- package/dist/hoi4_agent_tools/mcp/server/request-lifecycle.js +4 -0
- package/dist/hoi4_agent_tools/mcp/server/startup-failure.js +19 -0
- package/dist/hoi4_agent_tools/mcp/transports/negotiated-stdio.js +24 -16
- package/dist/hoi4_agent_tools/probability/job-operations.js +2 -2
- package/dist/hoi4_agent_tools/runtime.js +85 -19
- package/dist/hoi4_agent_tools/schemas/transaction.js +3 -2
- package/dist/hoi4_agent_tools/script-write/engine-plan.js +45 -0
- package/dist/hoi4_agent_tools/technology/engine-graph.js +156 -0
- package/dist/hoi4_agent_tools/technology/job-operations.js +2 -2
- package/dist/hoi4_agent_tools/technology/operations.js +1 -2
- package/dist/hoi4_agent_tools/technology/service.js +30 -59
- package/dist/hoi4_agent_tools/technology/source-analysis.js +3 -1
- package/dist/hoi4_agent_tools/technology/write-job-operations.js +2 -2
- package/dist/hoi4_agent_tools/technology/write-operations.js +2 -2
- package/dist/hoi4_agent_tools/technology/write-plan.js +21 -207
- package/dist/hoi4_agent_tools/version.js +1 -1
- package/docs/configuration.md +11 -10
- package/docs/troubleshooting.md +11 -11
- 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 +8 -8
- package/engine/bin/win32-x64/hoi4-engine.exe +0 -0
- package/npm-shrinkwrap.json +2 -2
- package/package.json +2 -2
- package/schemas/configuration.schema.json +143 -134
- 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 +1 -1
- package/schemas/focus-planning-sidecar.schema.json +1 -1
- 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
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
1
2
|
import { compareCodeUnits, hashCanonical } from './canonical.js';
|
|
2
3
|
import { installedEffectCommands } from './game-documentation.js';
|
|
3
4
|
import { SymbolIndex } from './index.js';
|
|
@@ -107,6 +108,13 @@ export class CoreEngine {
|
|
|
107
108
|
#scanAdmissionQueue = [];
|
|
108
109
|
#activeScanAdmissions = new Set();
|
|
109
110
|
#recovery;
|
|
111
|
+
#startupWork;
|
|
112
|
+
#startup;
|
|
113
|
+
/** Set by {@link recoverOnDemand}: each workspace recovers on its first use. */
|
|
114
|
+
#recoverOnDemand = false;
|
|
115
|
+
#recoveries = new Map();
|
|
116
|
+
/** Marks the work of a recovery, which reads its own workspace and must not wait for itself. */
|
|
117
|
+
#inRecovery = new AsyncLocalStorage();
|
|
110
118
|
constructor(resolver, services = {}) {
|
|
111
119
|
this.resolver = resolver;
|
|
112
120
|
const state = resolver.serverState();
|
|
@@ -142,27 +150,79 @@ export class CoreEngine {
|
|
|
142
150
|
this.#recovery = recovery;
|
|
143
151
|
return recovery;
|
|
144
152
|
}
|
|
145
|
-
/**
|
|
146
|
-
|
|
147
|
-
|
|
153
|
+
/**
|
|
154
|
+
* Set the work that loads the workspaces, to run when {@link recovered} is first asked for. A
|
|
155
|
+
* stdio server answers its handshake and tool list without it; the transport asks for
|
|
156
|
+
* {@link recovered} when the first request that needs a workspace arrives, so an idle server
|
|
157
|
+
* never loads one. The work reports its own failure (for example through
|
|
158
|
+
* `resolver.failStartup`); a rejection here only ends the wait.
|
|
159
|
+
*/
|
|
160
|
+
deferStartup(work) {
|
|
161
|
+
this.#startupWork = work;
|
|
148
162
|
}
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
163
|
+
/**
|
|
164
|
+
* Recover each workspace on its first use instead of every workspace at once: before its
|
|
165
|
+
* first file access through the resolver, before its first job is submitted and before its
|
|
166
|
+
* first rewrite is planned or applied. A failed recovery is recorded as with
|
|
167
|
+
* {@link initialize}: reads go on and rewrites are refused with
|
|
168
|
+
* `TRANSACTION_RECOVERY_REQUIRED`.
|
|
169
|
+
*/
|
|
170
|
+
recoverOnDemand() {
|
|
171
|
+
this.#recoverOnDemand = true;
|
|
172
|
+
this.resolver.onWorkspaceUse((workspaceId) => this.ensureRecovered(workspaceId));
|
|
173
|
+
this.transactions.setRecoveryGate((workspaceId) => {
|
|
153
174
|
try {
|
|
154
|
-
|
|
175
|
+
return this.ensureRecovered(this.resolver.get(workspaceId).id);
|
|
155
176
|
}
|
|
156
|
-
catch
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
level: 'error',
|
|
160
|
-
event: 'transaction_recovery_blocked',
|
|
161
|
-
workspaceId: workspace.id,
|
|
162
|
-
code: error instanceof ServiceError ? error.code : 'TRANSACTION_RECOVERY_FAILED',
|
|
163
|
-
message: error instanceof Error ? error.message : String(error),
|
|
164
|
-
})}\n`);
|
|
177
|
+
catch {
|
|
178
|
+
// An unknown workspace is refused by the caller's own lookup.
|
|
179
|
+
return Promise.resolve();
|
|
165
180
|
}
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Recover `workspaceId` once when {@link recoverOnDemand} is on; otherwise, and for the
|
|
185
|
+
* recovery's own file access, it returns at once. It never rejects.
|
|
186
|
+
*/
|
|
187
|
+
ensureRecovered(workspaceId) {
|
|
188
|
+
if (!this.#recoverOnDemand || this.#inRecovery.getStore() === true)
|
|
189
|
+
return Promise.resolve();
|
|
190
|
+
let recovery = this.#recoveries.get(workspaceId);
|
|
191
|
+
if (recovery === undefined) {
|
|
192
|
+
recovery = this.#inRecovery.run(true, () => this.#recoverWorkspace(workspaceId));
|
|
193
|
+
this.#recoveries.set(workspaceId, recovery);
|
|
194
|
+
}
|
|
195
|
+
return recovery;
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Settles when the deferred startup and the latest {@link initialize} have finished, at once
|
|
199
|
+
* when neither was set. The first call starts the deferred startup.
|
|
200
|
+
*/
|
|
201
|
+
async recovered() {
|
|
202
|
+
if (this.#startupWork !== undefined)
|
|
203
|
+
this.#startup ??= this.#startupWork().catch(() => undefined);
|
|
204
|
+
await this.#startup;
|
|
205
|
+
await this.#recovery;
|
|
206
|
+
}
|
|
207
|
+
async #recoverWorkspaces() {
|
|
208
|
+
for (const workspace of this.resolver.list())
|
|
209
|
+
await this.#recoverWorkspace(workspace.id);
|
|
210
|
+
}
|
|
211
|
+
async #recoverWorkspace(workspaceId) {
|
|
212
|
+
// Recovery repairs an interrupted internal rewrite before the workspace is exposed. A
|
|
213
|
+
// journal that cannot be recovered blocks rewrites of its own workspace, not the server.
|
|
214
|
+
try {
|
|
215
|
+
await this.transactions.recover(workspaceId);
|
|
216
|
+
}
|
|
217
|
+
catch (error) {
|
|
218
|
+
this.transactions.recordRecoveryFailure(workspaceId, error);
|
|
219
|
+
process.stderr.write(`${JSON.stringify({
|
|
220
|
+
level: 'error',
|
|
221
|
+
event: 'transaction_recovery_blocked',
|
|
222
|
+
workspaceId,
|
|
223
|
+
code: error instanceof ServiceError ? error.code : 'TRANSACTION_RECOVERY_FAILED',
|
|
224
|
+
message: error instanceof Error ? error.message : String(error),
|
|
225
|
+
})}\n`);
|
|
166
226
|
}
|
|
167
227
|
}
|
|
168
228
|
status(workspaceId, principal) {
|
|
@@ -96,3 +96,10 @@ export class IdleCacheLifetime {
|
|
|
96
96
|
this.release();
|
|
97
97
|
}
|
|
98
98
|
}
|
|
99
|
+
/**
|
|
100
|
+
* The cache lifetime of one group of operations on `engine`: its caches are released after the
|
|
101
|
+
* configured `idleCacheReleaseSeconds` without a call (default 30 s).
|
|
102
|
+
*/
|
|
103
|
+
export function idleCacheLifetime(engine, releaseCaches) {
|
|
104
|
+
return new IdleCacheLifetime(releaseCaches, engine.resolver.config().idleCacheReleaseSeconds * 1000);
|
|
105
|
+
}
|
|
@@ -131,7 +131,9 @@ export class OperationTaskService {
|
|
|
131
131
|
? validatedArguments.relativePath
|
|
132
132
|
: undefined, requestSignal)
|
|
133
133
|
: await resolveOperationWorkspaceId(this.engine, context, requestedWorkspaceId, requestSignal);
|
|
134
|
-
this
|
|
134
|
+
// A server that recovers on demand repairs this workspace's interrupted rewrite before
|
|
135
|
+
// the call reads or rewrites it; a failure is recorded and refuses rewrites only.
|
|
136
|
+
await this.engine.ensureRecovered(this.engine.resolver.get(workspaceId, context.principal).id);
|
|
135
137
|
requestSignal?.throwIfAborted();
|
|
136
138
|
}
|
|
137
139
|
catch (error) {
|
|
@@ -212,7 +212,13 @@ export class TransactionManager {
|
|
|
212
212
|
recordRecoveryFailure(workspaceId, error) {
|
|
213
213
|
this.#recoveryFailures.set(workspaceId, error instanceof Error ? error.message : 'Rewrite journal recovery failed');
|
|
214
214
|
}
|
|
215
|
+
#recoveryGate;
|
|
216
|
+
/** Run `gate` before a rewrite of a workspace is planned or applied; the engine recovers on demand. */
|
|
217
|
+
setRecoveryGate(gate) {
|
|
218
|
+
this.#recoveryGate = gate;
|
|
219
|
+
}
|
|
215
220
|
async assertRecovered(workspaceId, signal) {
|
|
221
|
+
await this.#recoveryGate?.(workspaceId);
|
|
216
222
|
if (!this.#recoveryFailures.has(workspaceId))
|
|
217
223
|
return;
|
|
218
224
|
try {
|
|
@@ -385,10 +385,27 @@ export class WorkspaceResolver {
|
|
|
385
385
|
#serverStateRoot;
|
|
386
386
|
#serverState;
|
|
387
387
|
#defaultWorkspaceId;
|
|
388
|
+
/** `prepared`: roots checked, no workspace loaded yet. `ready`: every workspace is loaded. */
|
|
389
|
+
#state = 'prepared';
|
|
390
|
+
#population;
|
|
391
|
+
#startupError;
|
|
392
|
+
#useHook;
|
|
388
393
|
constructor(configuration) {
|
|
389
394
|
this.configuration = configuration;
|
|
390
395
|
}
|
|
396
|
+
/** Check the configured roots and load every workspace. */
|
|
391
397
|
static async create(configuration) {
|
|
398
|
+
const resolver = await WorkspaceResolver.prepare(configuration);
|
|
399
|
+
await resolver.populate();
|
|
400
|
+
return resolver;
|
|
401
|
+
}
|
|
402
|
+
/**
|
|
403
|
+
* The quick half of {@link create}: check the configured roots and open the server state. The
|
|
404
|
+
* resolver answers `config()` and `serverState()` at once, and no workspace is loaded until
|
|
405
|
+
* {@link populate} runs. A stdio server uses this to answer its handshake while the slow
|
|
406
|
+
* half, which reads every mod folder, is still running.
|
|
407
|
+
*/
|
|
408
|
+
static async prepare(configuration) {
|
|
392
409
|
const resolver = new WorkspaceResolver(configuration);
|
|
393
410
|
if (configuration.serverStateRoot !== undefined) {
|
|
394
411
|
resolver.#serverStateRoot = await canonicalPath(configuration.serverStateRoot);
|
|
@@ -426,38 +443,119 @@ export class WorkspaceResolver {
|
|
|
426
443
|
}
|
|
427
444
|
}
|
|
428
445
|
}
|
|
446
|
+
if (configuration.serverStateRoot !== undefined) {
|
|
447
|
+
// No key is written while a configured workspace overlaps the state root; the full
|
|
448
|
+
// check of each workspace runs later, in `populate`.
|
|
449
|
+
await resolver.#assertServerStateClearOfConfiguredWorkspaces();
|
|
450
|
+
resolver.#serverState = await ServerState.create(configuration.serverStateRoot);
|
|
451
|
+
resolver.#serverStateRoot = resolver.#serverState.root;
|
|
452
|
+
}
|
|
453
|
+
return resolver;
|
|
454
|
+
}
|
|
455
|
+
/** The cheap part of the registration checks: every configured root against the state root. */
|
|
456
|
+
async #assertServerStateClearOfConfiguredWorkspaces() {
|
|
457
|
+
const stateRoot = this.#serverStateRoot;
|
|
458
|
+
if (stateRoot === undefined)
|
|
459
|
+
return;
|
|
460
|
+
for (const configured of this.configuration.workspaces) {
|
|
461
|
+
const registration = this.withGlobalWorkspaceDefaults(configured);
|
|
462
|
+
const candidates = [
|
|
463
|
+
registration.root,
|
|
464
|
+
registration.gameRoot,
|
|
465
|
+
registration.artifactRoot,
|
|
466
|
+
registration.cacheRoot,
|
|
467
|
+
registration.fixtureRoot,
|
|
468
|
+
registration.wikiRoot,
|
|
469
|
+
registration.scriptDocsRoot,
|
|
470
|
+
...registration.dependencyRoots,
|
|
471
|
+
...registration.dependencies.map(({ root }) => root),
|
|
472
|
+
].filter((candidate) => candidate !== undefined);
|
|
473
|
+
for (const candidate of candidates) {
|
|
474
|
+
if (pathsOverlap(stateRoot, await canonicalPath(candidate))) {
|
|
475
|
+
throw new ServiceError('SERVER_STATE_ROOT_OVERLAP', 'Server state root must not overlap a workspace source or generated-storage root', { workspaceId: registration.id });
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
/**
|
|
481
|
+
* The slow half of {@link create}: resolve every configured workspace and discover the mods
|
|
482
|
+
* under the mod roots. It runs once; later calls return the same promise, which rejects with
|
|
483
|
+
* the same error.
|
|
484
|
+
*/
|
|
485
|
+
populate() {
|
|
486
|
+
if (this.#startupError !== undefined)
|
|
487
|
+
return Promise.reject(this.#startupError);
|
|
488
|
+
this.#population ??= this.#populate().then(() => {
|
|
489
|
+
this.#state = 'ready';
|
|
490
|
+
}, (error) => {
|
|
491
|
+
throw this.failStartup(error);
|
|
492
|
+
});
|
|
493
|
+
return this.#population;
|
|
494
|
+
}
|
|
495
|
+
/**
|
|
496
|
+
* Make every workspace lookup fail with `error`, the reason the server could not start. A
|
|
497
|
+
* stdio server keeps answering its handshake and reports this on the first call that needs
|
|
498
|
+
* a workspace.
|
|
499
|
+
*/
|
|
500
|
+
failStartup(error) {
|
|
501
|
+
this.#startupError ??=
|
|
502
|
+
error instanceof ServiceError
|
|
503
|
+
? error
|
|
504
|
+
: new ServiceError('INTERNAL_ERROR', error instanceof Error ? error.message : String(error));
|
|
505
|
+
return this.#startupError;
|
|
506
|
+
}
|
|
507
|
+
/**
|
|
508
|
+
* Run `hook` with the workspace's ID before each file access through this resolver
|
|
509
|
+
* (`refreshDescriptors`, `resolvePath`, `resolvePathInRoot`). The engine uses it to
|
|
510
|
+
* recover a workspace on its first use.
|
|
511
|
+
*/
|
|
512
|
+
onWorkspaceUse(hook) {
|
|
513
|
+
this.#useHook = hook;
|
|
514
|
+
}
|
|
515
|
+
/** The reason the server could not start, when it could not. */
|
|
516
|
+
startupError() {
|
|
517
|
+
return this.#startupError;
|
|
518
|
+
}
|
|
519
|
+
#assertUsable() {
|
|
520
|
+
if (this.#startupError !== undefined)
|
|
521
|
+
throw this.#startupError;
|
|
522
|
+
if (this.#state !== 'ready')
|
|
523
|
+
throw new ServiceError('WORKSPACE_STARTUP_PENDING', 'The workspaces are still loading; retry in a moment');
|
|
524
|
+
}
|
|
525
|
+
async #populate() {
|
|
526
|
+
const configuration = this.configuration;
|
|
527
|
+
this.#state = 'populating';
|
|
429
528
|
for (const configured of configuration.workspaces) {
|
|
430
529
|
if (configured.id === CURRENT_WORKSPACE_ID) {
|
|
431
530
|
throw new ServiceError('WORKSPACE_ID_RESERVED', `Workspace ID is reserved for the current working mod: ${CURRENT_WORKSPACE_ID}`);
|
|
432
531
|
}
|
|
433
|
-
const registration =
|
|
434
|
-
const workspace = await
|
|
435
|
-
if (
|
|
532
|
+
const registration = this.withGlobalWorkspaceDefaults(configured);
|
|
533
|
+
const workspace = await this.resolveRegistration(registration);
|
|
534
|
+
if (this.#byId.has(workspace.id)) {
|
|
436
535
|
throw new ServiceError('WORKSPACE_DUPLICATE', `Duplicate workspace ID: ${workspace.id}`);
|
|
437
536
|
}
|
|
438
|
-
|
|
439
|
-
|
|
537
|
+
this.assertWorkspaceIsolation(workspace);
|
|
538
|
+
this.#byId.set(workspace.id, workspace);
|
|
440
539
|
}
|
|
441
|
-
await
|
|
540
|
+
await this.discoverModWorkspaces();
|
|
442
541
|
if (configuration.defaultWorkspace !== undefined) {
|
|
443
|
-
const id =
|
|
444
|
-
if (
|
|
542
|
+
const id = this.#resolveWorkspaceId(configuration.defaultWorkspace);
|
|
543
|
+
if (this.#byId.get(id)?.registration.kind !== 'mod') {
|
|
445
544
|
throw new ServiceError('WORKSPACE_DEFAULT_UNKNOWN', `The configured default workspace is not a registered mod workspace: ${configuration.defaultWorkspace}`, { defaultWorkspace: configuration.defaultWorkspace });
|
|
446
545
|
}
|
|
447
|
-
|
|
546
|
+
this.#defaultWorkspaceId = id;
|
|
448
547
|
}
|
|
449
|
-
if (configuration.serverStateRoot !== undefined) {
|
|
450
|
-
resolver.#serverState = await ServerState.create(configuration.serverStateRoot);
|
|
451
|
-
resolver.#serverStateRoot = resolver.#serverState.root;
|
|
452
|
-
}
|
|
453
|
-
return resolver;
|
|
454
548
|
}
|
|
455
549
|
config() {
|
|
456
550
|
return this.configuration;
|
|
457
551
|
}
|
|
458
552
|
/** Resolve the implicit workspace used by local coding-agent calls. */
|
|
459
553
|
resolveCurrentWorkspaceId(principal) {
|
|
460
|
-
|
|
554
|
+
this.#assertUsable();
|
|
555
|
+
return this.#resolveCurrentWorkspaceId(principal);
|
|
556
|
+
}
|
|
557
|
+
#resolveCurrentWorkspaceId(principal) {
|
|
558
|
+
const workspaces = this.#list(principal).filter(({ registration }) => registration.kind === 'mod');
|
|
461
559
|
if (workspaces.length === 0) {
|
|
462
560
|
throw new ServiceError('WORKSPACE_CONTEXT_NOT_FOUND', 'No writable mod workspace is configured for the MCP server', workspaceCandidates(workspaces));
|
|
463
561
|
}
|
|
@@ -475,8 +573,9 @@ export class WorkspaceResolver {
|
|
|
475
573
|
}
|
|
476
574
|
/** Resolve the active mod from filesystem roots advertised by the connected MCP client. */
|
|
477
575
|
async resolveClientWorkspaceId(rootPaths, principal) {
|
|
576
|
+
this.#assertUsable();
|
|
478
577
|
const canonicalRoots = await Promise.all(rootPaths.map((rootPath) => canonicalPath(rootPath)));
|
|
479
|
-
const workspaces = this
|
|
578
|
+
const workspaces = this.#list(principal).filter(({ registration }) => registration.kind === 'mod');
|
|
480
579
|
const matches = workspaces.filter((workspace) => canonicalRoots.some((rootPath) => isWithin(workspace.modRoot, rootPath) || isWithin(rootPath, workspace.modRoot)));
|
|
481
580
|
if (matches.length === 1)
|
|
482
581
|
return matches[0].id;
|
|
@@ -526,14 +625,18 @@ export class WorkspaceResolver {
|
|
|
526
625
|
throw new ServiceError('WORKSPACE_SOURCE_AMBIGUOUS', `The requested source exists in more than one mod workspace: ${relativePath}`, { relativePath, workspaceIds: matches });
|
|
527
626
|
}
|
|
528
627
|
resolveWorkspaceId(workspaceId, principal) {
|
|
628
|
+
this.#assertUsable();
|
|
629
|
+
return this.#resolveWorkspaceId(workspaceId, principal);
|
|
630
|
+
}
|
|
631
|
+
#resolveWorkspaceId(workspaceId, principal) {
|
|
529
632
|
if (workspaceId === CURRENT_WORKSPACE_ID)
|
|
530
|
-
return this
|
|
633
|
+
return this.#resolveCurrentWorkspaceId(principal);
|
|
531
634
|
if (this.#byId.has(workspaceId))
|
|
532
635
|
return workspaceId;
|
|
533
636
|
const alias = requestedWorkspaceAlias(workspaceId);
|
|
534
637
|
// A folder name as the file manager shows it ("My Mod") names the same mod as its slug.
|
|
535
638
|
const slug = workspaceAliasSlug(alias);
|
|
536
|
-
const matches = this
|
|
639
|
+
const matches = this.#list(principal).filter((workspace) => {
|
|
537
640
|
if (workspace.registration.kind !== 'mod')
|
|
538
641
|
return false;
|
|
539
642
|
const aliases = workspaceAliases(workspace);
|
|
@@ -613,6 +716,10 @@ export class WorkspaceResolver {
|
|
|
613
716
|
}
|
|
614
717
|
}
|
|
615
718
|
list(principal) {
|
|
719
|
+
this.#assertUsable();
|
|
720
|
+
return this.#list(principal);
|
|
721
|
+
}
|
|
722
|
+
#list(principal) {
|
|
616
723
|
const allowed = principal === undefined ? undefined : this.allowedWorkspaceIds(principal);
|
|
617
724
|
return [...this.#byId.values()]
|
|
618
725
|
.filter((workspace) => allowed === undefined || allowed.has(workspace.id))
|
|
@@ -625,6 +732,7 @@ export class WorkspaceResolver {
|
|
|
625
732
|
*/
|
|
626
733
|
async refreshDescriptors(workspaceId, principal) {
|
|
627
734
|
const workspace = this.get(workspaceId, principal);
|
|
735
|
+
await this.#useHook?.(workspace.id);
|
|
628
736
|
if (workspace.registration.kind === 'game')
|
|
629
737
|
return;
|
|
630
738
|
const dependencies = workspace.registration.dependencies;
|
|
@@ -711,13 +819,16 @@ export class WorkspaceResolver {
|
|
|
711
819
|
* workspace lookup when it was TypeScript.
|
|
712
820
|
*/
|
|
713
821
|
async getAfterPathCheck(workspaceId, relativePath, principal) {
|
|
822
|
+
let workspace;
|
|
714
823
|
try {
|
|
715
|
-
|
|
824
|
+
workspace = this.get(workspaceId, principal);
|
|
716
825
|
}
|
|
717
826
|
catch (error) {
|
|
718
827
|
await refuseUnsafeRelativePath(relativePath);
|
|
719
828
|
throw error;
|
|
720
829
|
}
|
|
830
|
+
await this.#useHook?.(workspace.id);
|
|
831
|
+
return workspace;
|
|
721
832
|
}
|
|
722
833
|
allowedWorkspaceIds(principal) {
|
|
723
834
|
const token = this.configuration.http.tokens.find((entry) => entry.principal === principal);
|
|
@@ -38,7 +38,7 @@ function newDecisionFilePath(file) {
|
|
|
38
38
|
return path;
|
|
39
39
|
}
|
|
40
40
|
/** The file the operation writes: the one named, the one that defines the decision, or the one with the category. */
|
|
41
|
-
function targetFile(snapshot, input) {
|
|
41
|
+
async function targetFile(snapshot, input) {
|
|
42
42
|
const files = modDecisionFiles(snapshot);
|
|
43
43
|
if (input.file !== undefined) {
|
|
44
44
|
const named = files.find(({ relativePath }) => normalized(relativePath) === normalized(input.file));
|
|
@@ -63,7 +63,8 @@ function targetFile(snapshot, input) {
|
|
|
63
63
|
if (input.category === undefined)
|
|
64
64
|
fail('DECISION_WRITE_CATEGORY_REQUIRED', 'add needs the category block the decision goes in');
|
|
65
65
|
const category = input.category;
|
|
66
|
-
const
|
|
66
|
+
const categoriesByFile = await Promise.all(files.map(({ bytes, relativePath }) => decisionCategories({ bytes, path: relativePath })));
|
|
67
|
+
const holding = files.filter((_, index) => categoriesByFile[index].includes(category));
|
|
67
68
|
if (holding.length === 0) {
|
|
68
69
|
if (input.createCategory === true) {
|
|
69
70
|
if (files.length === 1)
|
|
@@ -74,9 +75,7 @@ function targetFile(snapshot, input) {
|
|
|
74
75
|
}
|
|
75
76
|
fail('DECISION_WRITE_CATEGORY_NOT_FOUND', `No common/decisions file of the mod has a ${category} category; pass createCategory: true to create it`, {
|
|
76
77
|
category,
|
|
77
|
-
categories: [
|
|
78
|
-
...new Set(files.flatMap(({ bytes, relativePath }) => decisionCategories({ bytes, path: relativePath }))),
|
|
79
|
-
],
|
|
78
|
+
categories: [...new Set(categoriesByFile.flat())],
|
|
80
79
|
});
|
|
81
80
|
}
|
|
82
81
|
if (holding.length > 1)
|
|
@@ -121,7 +120,7 @@ function referencesOf(id, body) {
|
|
|
121
120
|
}
|
|
122
121
|
export async function computeDecisionWrite(engine, input, context, applied) {
|
|
123
122
|
const snapshot = await engine.scan(context.workspaceId, { patterns: WRITE_PATTERNS }, context.principal, context.signal);
|
|
124
|
-
const target = targetFile(snapshot, input);
|
|
123
|
+
const target = await targetFile(snapshot, input);
|
|
125
124
|
const planInput = {
|
|
126
125
|
operation: input.operation,
|
|
127
126
|
id: input.id,
|
|
@@ -133,7 +132,7 @@ export async function computeDecisionWrite(engine, input, context, applied) {
|
|
|
133
132
|
let plan;
|
|
134
133
|
if ('file' in target) {
|
|
135
134
|
file = target.file;
|
|
136
|
-
plan = planDecisionWrite({ bytes: file.bytes, path: file.relativePath }, planInput);
|
|
135
|
+
plan = await planDecisionWrite({ bytes: file.bytes, path: file.relativePath }, planInput);
|
|
137
136
|
}
|
|
138
137
|
else {
|
|
139
138
|
const inMod = snapshot.index
|
|
@@ -144,7 +143,7 @@ export async function computeDecisionWrite(engine, input, context, applied) {
|
|
|
144
143
|
id: input.id,
|
|
145
144
|
path: inMod.path,
|
|
146
145
|
});
|
|
147
|
-
plan = planNewDecisionFile(target.newPath, planInput);
|
|
146
|
+
plan = await planNewDecisionFile(target.newPath, planInput);
|
|
148
147
|
}
|
|
149
148
|
const missing = { localisation: [], icon: [], category: [] };
|
|
150
149
|
const diagnostics = [];
|