ultra-builder-pro-cli 0.24.2 → 0.25.1
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 +51 -0
- package/adapters/claude.js +104 -0
- package/adapters/codex.js +363 -40
- package/adapters/grok.js +1254 -58
- package/adapters/kimi.js +682 -13
- package/adapters/opencode.js +132 -11
- package/docs/AGENT-CONTEXT.md +14 -0
- package/docs/ARTIFACT-AUTHORITY.md +13 -0
- package/docs/COMMIT-HASH-BACKFILL.md +25 -18
- package/docs/RUNTIME-COMPAT-MATRIX.md +18 -2
- package/mcp-server/lib/artifact-registry.cjs +181 -51
- package/mcp-server/lib/baseline-workflow.cjs +160 -23
- package/mcp-server/lib/change-workflow.cjs +21 -16
- package/mcp-server/lib/context-envelope.cjs +124 -51
- package/mcp-server/lib/decision-records.cjs +164 -78
- package/mcp-server/lib/kernel-change-workflow.cjs +2029 -0
- package/mcp-server/lib/legacy-change-workflow.cjs +294 -71
- package/mcp-server/lib/managed-file-write.cjs +56 -0
- package/mcp-server/lib/prd-parser.cjs +2 -2
- package/mcp-server/lib/runtime-paths.cjs +43 -16
- package/mcp-server/lib/stage-checkpoints.cjs +163 -9
- package/mcp-server/lib/state-db.cjs +186 -8
- package/mcp-server/lib/state-ops.cjs +214 -31
- package/mcp-server/lib/task-contract.cjs +404 -0
- package/mcp-server/lib/task-expander.cjs +2 -2
- package/mcp-server/lib/task-ledger.cjs +175 -62
- package/mcp-server/lib/ultra-facade.cjs +1786 -169
- package/mcp-server/lib/worker-packet.cjs +2 -2
- package/mcp-server/server.cjs +23 -4
- package/orchestrator/parallel-orchestrator.cjs +114 -27
- package/orchestrator/session-runner.cjs +25 -136
- package/package.json +1 -1
- package/skills/ultra-change/SKILL.md +6 -1
- package/skills/ultra-dev/SKILL.md +9 -4
- package/skills/ultra-plan/SKILL.md +13 -4
- package/skills/ultra-plan/references/semantic-preflight.md +6 -4
- package/spec/mcp-tools.yaml +114 -9
- package/spec/schemas/context-file.v4.5.schema.json +2 -2
- package/spec/schemas/state-db.sql +10 -6
- package/spec/schemas/task-ledger.v2.schema.json +23 -8
- package/spec/schemas/tasks.v4.5.schema.json +34 -12
package/adapters/grok.js
CHANGED
|
@@ -5,12 +5,12 @@
|
|
|
5
5
|
const fs = require('node:fs');
|
|
6
6
|
const os = require('node:os');
|
|
7
7
|
const path = require('node:path');
|
|
8
|
+
const crypto = require('node:crypto');
|
|
8
9
|
const { spawnSync } = require('node:child_process');
|
|
9
10
|
|
|
10
11
|
const {
|
|
11
12
|
applyNativeDoctor,
|
|
12
13
|
buildMcpRuntime,
|
|
13
|
-
mcpCommand,
|
|
14
14
|
} = require('./_shared/codex-assets.cjs');
|
|
15
15
|
const {
|
|
16
16
|
copyTree,
|
|
@@ -39,19 +39,33 @@ const PLUGIN_NAME = 'ultra-builder-pro';
|
|
|
39
39
|
const MCP_SERVER_NAME = PLUGIN_NAME;
|
|
40
40
|
const PROVENANCE_FILE = 'provenance.json';
|
|
41
41
|
const COMMAND_NAMES = CORE_PUBLIC_SKILLS;
|
|
42
|
+
const SOURCE_ROOT_PARTS = ['.ubp', 'plugin-sources', PLUGIN_NAME];
|
|
43
|
+
const BACKUP_ROOT_PARTS = ['.ubp', 'backups'];
|
|
42
44
|
|
|
43
45
|
function resolveTarget(ctx = {}) {
|
|
44
46
|
if (ctx.configDir) return path.resolve(ctx.configDir);
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
47
|
+
const configuredHome = [
|
|
48
|
+
ctx.grokHome,
|
|
49
|
+
ctx.grokEnv?.GROK_HOME,
|
|
50
|
+
ctx.env?.GROK_HOME,
|
|
51
|
+
process.env.GROK_HOME,
|
|
52
|
+
].find((candidate) => typeof candidate === 'string' && candidate.trim());
|
|
53
|
+
if (configuredHome) return path.resolve(configuredHome);
|
|
54
|
+
return path.join(ctx.homeDir || os.homedir(), '.grok');
|
|
49
55
|
}
|
|
50
56
|
|
|
51
57
|
function resolvePluginRoot(ctx = {}) {
|
|
52
58
|
return path.join(resolveTarget(ctx), 'plugins', PLUGIN_NAME);
|
|
53
59
|
}
|
|
54
60
|
|
|
61
|
+
function resolveSourceRoot(ctx = {}) {
|
|
62
|
+
return path.join(resolveTarget(ctx), ...SOURCE_ROOT_PARTS);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function resolveBackupRoot(ctx = {}) {
|
|
66
|
+
return path.join(resolveTarget(ctx), ...BACKUP_ROOT_PARTS);
|
|
67
|
+
}
|
|
68
|
+
|
|
55
69
|
function resolveRepoRoot(ctx = {}) {
|
|
56
70
|
return ctx.repoRoot || path.resolve(__dirname, '..');
|
|
57
71
|
}
|
|
@@ -201,7 +215,7 @@ function copyHooks(repoRoot, pluginRoot) {
|
|
|
201
215
|
return copied;
|
|
202
216
|
}
|
|
203
217
|
|
|
204
|
-
function writeManifests(repoRoot, pluginRoot
|
|
218
|
+
function writeManifests(repoRoot, pluginRoot) {
|
|
205
219
|
const pkg = JSON.parse(fs.readFileSync(path.join(repoRoot, 'package.json'), 'utf8'));
|
|
206
220
|
const repository = typeof pkg.repository === 'string'
|
|
207
221
|
? pkg.repository.replace(/^git\+/, '').replace(/\.git$/, '')
|
|
@@ -215,37 +229,472 @@ function writeManifests(repoRoot, pluginRoot, runtime) {
|
|
|
215
229
|
repository,
|
|
216
230
|
license: pkg.license || 'MIT',
|
|
217
231
|
}, null, 2)}\n`);
|
|
218
|
-
const
|
|
232
|
+
const launcher = '${GROK_PLUGIN_ROOT}/runtime/launch.cjs';
|
|
219
233
|
writeAtomic(path.join(pluginRoot, '.mcp.json'), `${JSON.stringify({
|
|
220
234
|
mcpServers: {
|
|
221
235
|
[MCP_SERVER_NAME]: {
|
|
222
236
|
type: 'stdio',
|
|
223
|
-
command:
|
|
224
|
-
args:
|
|
237
|
+
command: process.platform === 'win32' ? 'node.exe' : '/usr/bin/env',
|
|
238
|
+
args: process.platform === 'win32' ? [launcher] : ['node', launcher],
|
|
225
239
|
},
|
|
226
240
|
},
|
|
227
241
|
}, null, 2)}\n`);
|
|
228
242
|
}
|
|
229
243
|
|
|
230
|
-
function
|
|
231
|
-
const
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
const
|
|
244
|
+
function grokFailure(code, message, details = {}) {
|
|
245
|
+
const error = new Error(message);
|
|
246
|
+
error.code = code;
|
|
247
|
+
error.details = details;
|
|
248
|
+
return error;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
function resolveGrokBinary(ctx = {}) {
|
|
252
|
+
const executable = process.platform === 'win32' ? 'grok.exe' : 'grok';
|
|
253
|
+
if (ctx.grokBin) {
|
|
254
|
+
return fs.existsSync(ctx.grokBin) ? path.resolve(ctx.grokBin) : null;
|
|
255
|
+
}
|
|
256
|
+
const candidates = [
|
|
257
|
+
ctx.grokEnv?.GROK_BIN,
|
|
258
|
+
ctx.env?.GROK_BIN,
|
|
259
|
+
process.env.GROK_BIN,
|
|
260
|
+
path.join(resolveTarget(ctx), 'bin', executable),
|
|
261
|
+
].filter(Boolean);
|
|
262
|
+
for (const candidate of candidates) {
|
|
263
|
+
if (fs.existsSync(candidate)) return path.resolve(candidate);
|
|
264
|
+
}
|
|
265
|
+
const probe = spawnSync(executable, ['--version'], {
|
|
266
|
+
encoding: 'utf8',
|
|
267
|
+
timeout: 10_000,
|
|
268
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
269
|
+
});
|
|
270
|
+
return probe.status === 0 ? executable : null;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
function nativeEnvironment(ctx = {}) {
|
|
274
|
+
return {
|
|
275
|
+
...process.env,
|
|
276
|
+
...(ctx.env || {}),
|
|
277
|
+
...(ctx.grokEnv || {}),
|
|
278
|
+
GROK_HOME: resolveTarget(ctx),
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
function runGrok(ctx, args, {
|
|
283
|
+
timeout = 60_000,
|
|
284
|
+
json = false,
|
|
285
|
+
allowFailure = false,
|
|
286
|
+
} = {}) {
|
|
287
|
+
const binary = resolveGrokBinary(ctx);
|
|
288
|
+
if (!binary) {
|
|
289
|
+
throw grokFailure(
|
|
290
|
+
'GROK_CLI_UNAVAILABLE',
|
|
291
|
+
'Grok CLI is required for native plugin registration; install Grok Build or pass grokBin',
|
|
292
|
+
);
|
|
293
|
+
}
|
|
294
|
+
const result = spawnSync(binary, args, {
|
|
295
|
+
cwd: ctx.cwd || process.cwd(),
|
|
296
|
+
env: nativeEnvironment(ctx),
|
|
239
297
|
encoding: 'utf8',
|
|
240
|
-
timeout
|
|
298
|
+
timeout,
|
|
299
|
+
maxBuffer: 32 * 1024 * 1024,
|
|
300
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
241
301
|
});
|
|
242
302
|
if (result.error) throw result.error;
|
|
243
|
-
if (result.status !== 0) {
|
|
244
|
-
|
|
245
|
-
|
|
303
|
+
if (result.status !== 0 && !allowFailure) {
|
|
304
|
+
const detail = (result.stderr || result.stdout || '').trim();
|
|
305
|
+
throw grokFailure(
|
|
306
|
+
'GROK_CLI_COMMAND_FAILED',
|
|
307
|
+
`grok ${args.join(' ')} failed${detail ? `: ${detail}` : ''}`,
|
|
308
|
+
{ args, status: result.status },
|
|
309
|
+
);
|
|
310
|
+
}
|
|
311
|
+
let payload = null;
|
|
312
|
+
if (json && result.status === 0) {
|
|
313
|
+
try {
|
|
314
|
+
payload = JSON.parse(result.stdout || 'null');
|
|
315
|
+
} catch (error) {
|
|
316
|
+
throw grokFailure(
|
|
317
|
+
'GROK_CLI_INVALID_JSON',
|
|
318
|
+
`grok ${args.join(' ')} returned invalid JSON: ${error.message}`,
|
|
319
|
+
);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
return {
|
|
323
|
+
binary,
|
|
324
|
+
status: result.status,
|
|
325
|
+
stdout: result.stdout || '',
|
|
326
|
+
stderr: result.stderr || '',
|
|
327
|
+
payload,
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
function probeNativeCapabilities(ctx = {}) {
|
|
332
|
+
const binary = resolveGrokBinary(ctx);
|
|
333
|
+
if (!binary) return { status: 'unavailable', binary: null, capabilities: {} };
|
|
334
|
+
const commands = [
|
|
335
|
+
{ key: 'plugin_install', args: ['plugin', 'install', '--help'], required: ['--trust'] },
|
|
336
|
+
{ key: 'plugin_list', args: ['plugin', 'list', '--help'], required: ['--json'] },
|
|
337
|
+
{ key: 'plugin_details', args: ['plugin', 'details', '--help'], required: [] },
|
|
338
|
+
{ key: 'plugin_enable', args: ['plugin', 'enable', '--help'], required: [] },
|
|
339
|
+
{
|
|
340
|
+
key: 'plugin_uninstall',
|
|
341
|
+
args: ['plugin', 'uninstall', '--help'],
|
|
342
|
+
required: ['--confirm', '--keep-data'],
|
|
343
|
+
},
|
|
344
|
+
{ key: 'inspect', args: ['inspect', '--help'], required: ['--json'] },
|
|
345
|
+
{ key: 'mcp_doctor', args: ['mcp', 'doctor', '--help'], required: ['--json'] },
|
|
346
|
+
];
|
|
347
|
+
const capabilities = {};
|
|
348
|
+
for (const command of commands) {
|
|
349
|
+
const result = runGrok(ctx, command.args, { timeout: 15_000 });
|
|
350
|
+
const output = `${result.stdout}\n${result.stderr}`;
|
|
351
|
+
const missing = command.required.filter((flag) => !output.includes(flag));
|
|
352
|
+
if (missing.length > 0) {
|
|
353
|
+
throw grokFailure(
|
|
354
|
+
'GROK_CLI_CAPABILITY_MISSING',
|
|
355
|
+
`installed Grok CLI does not expose ${command.key}: missing ${missing.join(', ')}`,
|
|
356
|
+
{ command: command.args, missing },
|
|
357
|
+
);
|
|
358
|
+
}
|
|
359
|
+
capabilities[command.key] = true;
|
|
360
|
+
}
|
|
361
|
+
const version = runGrok(ctx, ['--version'], { timeout: 10_000 }).stdout.trim();
|
|
362
|
+
return { status: 'pass', binary, version, capabilities };
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
function validatePlugin(pluginRoot, ctx = {}) {
|
|
366
|
+
const capabilities = probeNativeCapabilities(ctx);
|
|
367
|
+
if (capabilities.status === 'unavailable') {
|
|
368
|
+
return { status: 'unavailable', binary: null };
|
|
369
|
+
}
|
|
370
|
+
const result = runGrok(ctx, ['plugin', 'validate', pluginRoot], { timeout: 30_000 });
|
|
371
|
+
return {
|
|
372
|
+
status: 'pass',
|
|
373
|
+
binary: result.binary,
|
|
374
|
+
output: result.stdout.trim(),
|
|
375
|
+
version: capabilities.version,
|
|
376
|
+
};
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
function pluginList(ctx = {}) {
|
|
380
|
+
const result = runGrok(ctx, ['plugin', 'list', '--json'], { json: true });
|
|
381
|
+
if (!Array.isArray(result.payload)) {
|
|
382
|
+
throw grokFailure('GROK_PLUGIN_LIST_INVALID', 'grok plugin list --json must return an array');
|
|
383
|
+
}
|
|
384
|
+
return result.payload;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
function installedPlugin(ctx = {}) {
|
|
388
|
+
const matches = pluginList(ctx).filter((entry) => entry?.name === PLUGIN_NAME);
|
|
389
|
+
if (matches.length > 1) {
|
|
390
|
+
throw grokFailure(
|
|
391
|
+
'GROK_PLUGIN_REGISTRY_CONFLICT',
|
|
392
|
+
`Grok registered ${matches.length} Ultra Builder Pro plugin entries`,
|
|
393
|
+
);
|
|
394
|
+
}
|
|
395
|
+
const registered = matches[0] || null;
|
|
396
|
+
if (
|
|
397
|
+
registered
|
|
398
|
+
&& (
|
|
399
|
+
registered.status !== 'installed'
|
|
400
|
+
|| typeof registered.path !== 'string'
|
|
401
|
+
|| !registered.path.trim()
|
|
402
|
+
)
|
|
403
|
+
) {
|
|
404
|
+
throw grokFailure(
|
|
405
|
+
'GROK_PLUGIN_REGISTRY_INVALID',
|
|
406
|
+
'Grok returned an invalid Ultra Builder Pro registry entry',
|
|
407
|
+
);
|
|
408
|
+
}
|
|
409
|
+
return registered;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
function parseActivationArray(section, key, file) {
|
|
413
|
+
const declaration = new RegExp(`(?:^|\\n)\\s*${key}\\s*=`, 'g');
|
|
414
|
+
const assignment = new RegExp(
|
|
415
|
+
`(?:^|\\n)\\s*${key}\\s*=\\s*(\\[[\\s\\S]*?\\])\\s*(?:#.*)?(?=\\n|$)`,
|
|
416
|
+
'g',
|
|
417
|
+
);
|
|
418
|
+
const declarations = [...section.matchAll(declaration)];
|
|
419
|
+
const assignments = [...section.matchAll(assignment)];
|
|
420
|
+
if (declarations.length === 0) return null;
|
|
421
|
+
if (declarations.length !== 1 || assignments.length !== 1) {
|
|
422
|
+
throw grokFailure(
|
|
423
|
+
'GROK_PLUGIN_ACTIVATION_REGISTRY_INVALID',
|
|
424
|
+
`Grok activation registry has an invalid plugins.${key} declaration: ${file}`,
|
|
425
|
+
{ file, key },
|
|
426
|
+
);
|
|
427
|
+
}
|
|
428
|
+
let values;
|
|
429
|
+
try {
|
|
430
|
+
values = JSON.parse(assignments[0][1]);
|
|
431
|
+
} catch (error) {
|
|
432
|
+
throw grokFailure(
|
|
433
|
+
'GROK_PLUGIN_ACTIVATION_REGISTRY_INVALID',
|
|
434
|
+
`Grok activation registry has an unreadable plugins.${key} array: ${file}`,
|
|
435
|
+
{ file, key, cause_code: error.code || null },
|
|
246
436
|
);
|
|
247
437
|
}
|
|
248
|
-
|
|
438
|
+
if (!Array.isArray(values) || values.some((value) => typeof value !== 'string')) {
|
|
439
|
+
throw grokFailure(
|
|
440
|
+
'GROK_PLUGIN_ACTIVATION_REGISTRY_INVALID',
|
|
441
|
+
`Grok activation registry plugins.${key} must be a string array: ${file}`,
|
|
442
|
+
{ file, key },
|
|
443
|
+
);
|
|
444
|
+
}
|
|
445
|
+
return values;
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
function registeredActivationState(ctx) {
|
|
449
|
+
const file = path.join(resolveTarget(ctx), 'config.toml');
|
|
450
|
+
if (!fs.existsSync(file)) return null;
|
|
451
|
+
const text = fs.readFileSync(file, 'utf8');
|
|
452
|
+
const lines = text.split(/\r?\n/);
|
|
453
|
+
const sectionIndexes = lines
|
|
454
|
+
.map((line, index) => (/^\s*\[plugins\]\s*(?:#.*)?$/.test(line) ? index : -1))
|
|
455
|
+
.filter((index) => index >= 0);
|
|
456
|
+
if (sectionIndexes.length === 0) return null;
|
|
457
|
+
if (sectionIndexes.length !== 1) {
|
|
458
|
+
throw grokFailure(
|
|
459
|
+
'GROK_PLUGIN_ACTIVATION_REGISTRY_INVALID',
|
|
460
|
+
`Grok activation registry contains multiple [plugins] sections: ${file}`,
|
|
461
|
+
{ file },
|
|
462
|
+
);
|
|
463
|
+
}
|
|
464
|
+
const start = sectionIndexes[0] + 1;
|
|
465
|
+
let end = lines.length;
|
|
466
|
+
for (let index = start; index < lines.length; index += 1) {
|
|
467
|
+
if (/^\s*\[.+\]\s*(?:#.*)?$/.test(lines[index])) {
|
|
468
|
+
end = index;
|
|
469
|
+
break;
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
const section = lines.slice(start, end).join('\n');
|
|
473
|
+
const enabled = parseActivationArray(section, 'enabled', file);
|
|
474
|
+
const disabled = parseActivationArray(section, 'disabled', file);
|
|
475
|
+
const listedEnabled = enabled?.includes(PLUGIN_NAME) === true;
|
|
476
|
+
const listedDisabled = disabled?.includes(PLUGIN_NAME) === true;
|
|
477
|
+
if (listedEnabled && listedDisabled) {
|
|
478
|
+
throw grokFailure(
|
|
479
|
+
'GROK_PLUGIN_ACTIVATION_REGISTRY_INVALID',
|
|
480
|
+
`Grok activation registry lists ${PLUGIN_NAME} as both enabled and disabled`,
|
|
481
|
+
{ file },
|
|
482
|
+
);
|
|
483
|
+
}
|
|
484
|
+
if (listedEnabled) return true;
|
|
485
|
+
if (listedDisabled) return false;
|
|
486
|
+
return null;
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
function resolveNativeInstallPath(plugin) {
|
|
490
|
+
if (typeof plugin?.path !== 'string' || !plugin.path.trim()) {
|
|
491
|
+
throw grokFailure(
|
|
492
|
+
'GROK_PLUGIN_PATH_MISSING',
|
|
493
|
+
'Grok registry did not return an installed path for Ultra Builder Pro',
|
|
494
|
+
);
|
|
495
|
+
}
|
|
496
|
+
const target = path.resolve(plugin.path);
|
|
497
|
+
if (!fs.existsSync(target)) {
|
|
498
|
+
throw grokFailure('GROK_PLUGIN_PATH_MISSING', `Grok registered plugin path is missing: ${target}`);
|
|
499
|
+
}
|
|
500
|
+
const canonicalTarget = fs.realpathSync(target);
|
|
501
|
+
if (!fs.statSync(canonicalTarget).isDirectory()) {
|
|
502
|
+
throw grokFailure(
|
|
503
|
+
'GROK_PLUGIN_PATH_MISSING',
|
|
504
|
+
`Grok registered plugin path is not a directory: ${canonicalTarget}`,
|
|
505
|
+
);
|
|
506
|
+
}
|
|
507
|
+
return canonicalTarget;
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
function assertRegisteredSource(ctx, plugin, {
|
|
511
|
+
expectedPackageName,
|
|
512
|
+
expectedPackageVersion,
|
|
513
|
+
sourceRoot = resolveSourceRoot(ctx),
|
|
514
|
+
} = {}) {
|
|
515
|
+
if (typeof plugin?.source !== 'string' || !plugin.source.trim()) {
|
|
516
|
+
throw grokFailure(
|
|
517
|
+
'GROK_PLUGIN_SOURCE_MISSING',
|
|
518
|
+
'Grok registry did not return the source used to install Ultra Builder Pro',
|
|
519
|
+
);
|
|
520
|
+
}
|
|
521
|
+
const expectedSource = path.resolve(sourceRoot);
|
|
522
|
+
const registeredSource = path.resolve(plugin.source);
|
|
523
|
+
if (!fs.existsSync(expectedSource) || !fs.existsSync(registeredSource)) {
|
|
524
|
+
throw grokFailure(
|
|
525
|
+
'GROK_PLUGIN_SOURCE_MISSING',
|
|
526
|
+
`Grok registered plugin source is missing: ${registeredSource}`,
|
|
527
|
+
{ expected: expectedSource, actual: registeredSource },
|
|
528
|
+
);
|
|
529
|
+
}
|
|
530
|
+
const canonicalExpected = fs.realpathSync(expectedSource);
|
|
531
|
+
const canonicalSource = fs.realpathSync(registeredSource);
|
|
532
|
+
if (canonicalSource !== canonicalExpected) {
|
|
533
|
+
throw grokFailure(
|
|
534
|
+
'GROK_PLUGIN_SOURCE_MISMATCH',
|
|
535
|
+
`Grok registered ${canonicalSource}, expected managed source ${canonicalExpected}`,
|
|
536
|
+
{ expected: canonicalExpected, actual: canonicalSource },
|
|
537
|
+
);
|
|
538
|
+
}
|
|
539
|
+
const report = assertManagedProvenance(canonicalSource, 'registered Grok source', {
|
|
540
|
+
expectedPackageName,
|
|
541
|
+
expectedPackageVersion,
|
|
542
|
+
});
|
|
543
|
+
return { path: canonicalSource, report };
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
function assertNativeInstallPath(ctx, plugin, {
|
|
547
|
+
expectedPackageName = null,
|
|
548
|
+
expectedPackageVersion = null,
|
|
549
|
+
sourceRoot = resolveSourceRoot(ctx),
|
|
550
|
+
} = {}) {
|
|
551
|
+
const packageInfo = expectedPackageName
|
|
552
|
+
? null
|
|
553
|
+
: provenance.packageSource(resolveRepoRoot(ctx)).packageInfo;
|
|
554
|
+
const packageName = expectedPackageName || packageInfo.name;
|
|
555
|
+
const packageVersion = expectedPackageVersion || plugin?.version;
|
|
556
|
+
if (typeof packageVersion !== 'string' || !packageVersion.trim()) {
|
|
557
|
+
throw grokFailure(
|
|
558
|
+
'GROK_PLUGIN_VERSION_MISSING',
|
|
559
|
+
'Grok registry did not return an Ultra Builder Pro package version',
|
|
560
|
+
);
|
|
561
|
+
}
|
|
562
|
+
if (plugin.version !== packageVersion) {
|
|
563
|
+
throw grokFailure(
|
|
564
|
+
'GROK_PLUGIN_VERSION_MISMATCH',
|
|
565
|
+
`Grok registered Ultra Builder Pro ${plugin.version || '<missing>'}, expected ${packageVersion}`,
|
|
566
|
+
{ expected: packageVersion, actual: plugin.version || null },
|
|
567
|
+
);
|
|
568
|
+
}
|
|
569
|
+
const target = resolveNativeInstallPath(plugin);
|
|
570
|
+
if (!isManaged(target)) {
|
|
571
|
+
throw grokFailure(
|
|
572
|
+
'GROK_PLUGIN_PATH_UNSAFE',
|
|
573
|
+
`Grok registry returned an unmanaged plugin path: ${target}`,
|
|
574
|
+
{ path: target },
|
|
575
|
+
);
|
|
576
|
+
}
|
|
577
|
+
assertManagedProvenance(target, 'registered Grok target', {
|
|
578
|
+
expectedPackageName: packageName,
|
|
579
|
+
expectedPackageVersion: packageVersion,
|
|
580
|
+
});
|
|
581
|
+
assertRegisteredSource(ctx, plugin, {
|
|
582
|
+
expectedPackageName: packageName,
|
|
583
|
+
expectedPackageVersion: packageVersion,
|
|
584
|
+
sourceRoot,
|
|
585
|
+
});
|
|
586
|
+
return target;
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
function assertConsumerPath(expectedTarget, reportedPath, label, ctx = {}) {
|
|
590
|
+
if (typeof reportedPath !== 'string' || !reportedPath.trim()) {
|
|
591
|
+
throw grokFailure(
|
|
592
|
+
'GROK_PLUGIN_CONSUMER_PATH_MISSING',
|
|
593
|
+
`Grok ${label} did not report the registered plugin path`,
|
|
594
|
+
{ expected: expectedTarget, actual: reportedPath || null, label },
|
|
595
|
+
);
|
|
596
|
+
}
|
|
597
|
+
const absolute = path.resolve(ctx.cwd || process.cwd(), reportedPath);
|
|
598
|
+
let canonical;
|
|
599
|
+
try {
|
|
600
|
+
canonical = fs.realpathSync(absolute);
|
|
601
|
+
} catch (error) {
|
|
602
|
+
throw grokFailure(
|
|
603
|
+
'GROK_PLUGIN_PATH_SHADOWED',
|
|
604
|
+
`Grok ${label} resolved a missing path instead of the registered plugin: ${absolute}`,
|
|
605
|
+
{ expected: expectedTarget, actual: absolute, label, cause_code: error.code || null },
|
|
606
|
+
);
|
|
607
|
+
}
|
|
608
|
+
if (canonical !== expectedTarget) {
|
|
609
|
+
throw grokFailure(
|
|
610
|
+
'GROK_PLUGIN_PATH_SHADOWED',
|
|
611
|
+
`Grok ${label} resolved ${canonical}, expected registered target ${expectedTarget}`,
|
|
612
|
+
{ expected: expectedTarget, actual: canonical, label },
|
|
613
|
+
);
|
|
614
|
+
}
|
|
615
|
+
return canonical;
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
function nativeActivationState(ctx, target) {
|
|
619
|
+
const registeredState = registeredActivationState(ctx);
|
|
620
|
+
if (registeredState !== null) return registeredState;
|
|
621
|
+
const inspect = runGrok(ctx, ['inspect', '--json'], { json: true }).payload;
|
|
622
|
+
const matches = (Array.isArray(inspect?.plugins) ? inspect.plugins : [])
|
|
623
|
+
.filter((entry) => entry?.name === PLUGIN_NAME);
|
|
624
|
+
if (matches.length !== 1) {
|
|
625
|
+
throw grokFailure(
|
|
626
|
+
'GROK_PLUGIN_ACTIVATION_STATE_INVALID',
|
|
627
|
+
`Grok inspect returned ${matches.length} Ultra Builder Pro plugin entries`,
|
|
628
|
+
{ expected: 1, actual: matches.length },
|
|
629
|
+
);
|
|
630
|
+
}
|
|
631
|
+
const plugin = matches[0];
|
|
632
|
+
assertConsumerPath(target, plugin.path, 'inspect plugin activation', ctx);
|
|
633
|
+
if (typeof plugin.enabled !== 'boolean') {
|
|
634
|
+
throw grokFailure(
|
|
635
|
+
'GROK_PLUGIN_ACTIVATION_STATE_INVALID',
|
|
636
|
+
'Grok inspect did not report a boolean Ultra Builder Pro activation state',
|
|
637
|
+
{ actual: plugin.enabled ?? null },
|
|
638
|
+
);
|
|
639
|
+
}
|
|
640
|
+
return plugin.enabled;
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
function restoreNativeActivation(ctx, target, enabled) {
|
|
644
|
+
runGrok(ctx, ['plugin', enabled ? 'enable' : 'disable', PLUGIN_NAME]);
|
|
645
|
+
const restored = nativeActivationState(ctx, target);
|
|
646
|
+
if (restored !== enabled) {
|
|
647
|
+
throw grokFailure(
|
|
648
|
+
'GROK_ROLLBACK_ACTIVATION_MISMATCH',
|
|
649
|
+
`Grok restored Ultra Builder Pro with enabled=${restored}, expected enabled=${enabled}`,
|
|
650
|
+
{ expected: enabled, actual: restored },
|
|
651
|
+
);
|
|
652
|
+
}
|
|
653
|
+
return restored;
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
function nativeConsumerState(ctx = {}, registered = null) {
|
|
657
|
+
const registryEntry = registered || installedPlugin(ctx);
|
|
658
|
+
const target = registryEntry ? assertNativeInstallPath(ctx, registryEntry) : null;
|
|
659
|
+
const inspect = runGrok(ctx, ['inspect', '--json'], { json: true }).payload;
|
|
660
|
+
const plugins = Array.isArray(inspect?.plugins) ? inspect.plugins : [];
|
|
661
|
+
const servers = Array.isArray(inspect?.mcpServers) ? inspect.mcpServers : [];
|
|
662
|
+
const matchingPlugins = plugins.filter((entry) => entry?.name === PLUGIN_NAME);
|
|
663
|
+
const matchingServers = servers.filter((entry) => (
|
|
664
|
+
entry?.name === MCP_SERVER_NAME
|
|
665
|
+
&& entry?.source?.type === 'plugin'
|
|
666
|
+
&& entry?.source?.plugin_name === PLUGIN_NAME
|
|
667
|
+
));
|
|
668
|
+
if (target) {
|
|
669
|
+
for (const pluginEntry of matchingPlugins) {
|
|
670
|
+
assertConsumerPath(target, pluginEntry.path, 'inspect plugin', ctx);
|
|
671
|
+
}
|
|
672
|
+
for (const serverEntry of matchingServers) {
|
|
673
|
+
assertConsumerPath(target, serverEntry.source.path, 'inspect plugin MCP source', ctx);
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
const plugin = matchingPlugins[0] || null;
|
|
677
|
+
const mcp = matchingServers[0] || null;
|
|
678
|
+
const doctor = runGrok(
|
|
679
|
+
ctx,
|
|
680
|
+
['mcp', 'doctor', MCP_SERVER_NAME, '--json'],
|
|
681
|
+
{ json: true, timeout: 60_000 },
|
|
682
|
+
).payload;
|
|
683
|
+
const doctorServers = Array.isArray(doctor?.servers) ? doctor.servers : [];
|
|
684
|
+
const doctorServer = doctorServers.find((entry) => {
|
|
685
|
+
if (entry?.name !== MCP_SERVER_NAME) return false;
|
|
686
|
+
if (typeof entry.source === 'string') return entry.source.includes(PLUGIN_NAME);
|
|
687
|
+
return entry?.source?.plugin_name === PLUGIN_NAME;
|
|
688
|
+
}) || null;
|
|
689
|
+
return {
|
|
690
|
+
inspect,
|
|
691
|
+
doctor,
|
|
692
|
+
plugin,
|
|
693
|
+
mcp,
|
|
694
|
+
doctorServer,
|
|
695
|
+
target,
|
|
696
|
+
healthy: Boolean(plugin && mcp && doctorServer?.healthy === true),
|
|
697
|
+
};
|
|
249
698
|
}
|
|
250
699
|
|
|
251
700
|
function buildStaging(repoRoot, staging, ctx) {
|
|
@@ -256,7 +705,7 @@ function buildStaging(repoRoot, staging, ctx) {
|
|
|
256
705
|
hooks: copyHooks(repoRoot, staging),
|
|
257
706
|
};
|
|
258
707
|
const runtime = buildMcpRuntime(repoRoot, staging, { runtime: 'grok' });
|
|
259
|
-
writeManifests(repoRoot, staging
|
|
708
|
+
writeManifests(repoRoot, staging);
|
|
260
709
|
if (fs.existsSync(path.join(repoRoot, 'LICENSE'))) {
|
|
261
710
|
fs.copyFileSync(path.join(repoRoot, 'LICENSE'), path.join(staging, 'LICENSE'));
|
|
262
711
|
}
|
|
@@ -289,56 +738,647 @@ function writeProvenance(repoRoot, pluginRoot) {
|
|
|
289
738
|
return { file, manifest };
|
|
290
739
|
}
|
|
291
740
|
|
|
741
|
+
function managedCopy(source, target) {
|
|
742
|
+
ensureDir(path.dirname(target));
|
|
743
|
+
fs.cpSync(source, target, { recursive: true, force: false, errorOnExist: true });
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
function rebaseProvenanceRoot(pluginRoot) {
|
|
747
|
+
const file = path.join(pluginRoot, PROVENANCE_FILE);
|
|
748
|
+
if (!fs.existsSync(file)) return null;
|
|
749
|
+
const manifest = JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
750
|
+
if (!manifest.roots || typeof manifest.roots !== 'object') {
|
|
751
|
+
throw grokFailure(
|
|
752
|
+
'GROK_ROLLBACK_SOURCE_INVALID',
|
|
753
|
+
`rollback source has invalid provenance roots: ${file}`,
|
|
754
|
+
);
|
|
755
|
+
}
|
|
756
|
+
manifest.roots.plugin = path.resolve(pluginRoot);
|
|
757
|
+
writeAtomic(file, `${JSON.stringify(manifest, null, 2)}\n`);
|
|
758
|
+
return manifest;
|
|
759
|
+
}
|
|
760
|
+
|
|
761
|
+
function sourceMatchesRegistration(source, registered) {
|
|
762
|
+
try {
|
|
763
|
+
const manifest = JSON.parse(fs.readFileSync(path.join(source, 'plugin.json'), 'utf8'));
|
|
764
|
+
return (
|
|
765
|
+
manifest.name === PLUGIN_NAME
|
|
766
|
+
&& (!registered.version || manifest.version === registered.version)
|
|
767
|
+
);
|
|
768
|
+
} catch {
|
|
769
|
+
return false;
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
function ensureRecoverySource(registered, sourceRoot, nativeBackup) {
|
|
774
|
+
const declaredSource = typeof registered.source === 'string'
|
|
775
|
+
? path.resolve(registered.source)
|
|
776
|
+
: null;
|
|
777
|
+
if (
|
|
778
|
+
declaredSource
|
|
779
|
+
&& fs.existsSync(declaredSource)
|
|
780
|
+
&& isManaged(declaredSource)
|
|
781
|
+
&& sourceMatchesRegistration(declaredSource, registered)
|
|
782
|
+
) {
|
|
783
|
+
return declaredSource;
|
|
784
|
+
}
|
|
785
|
+
if (
|
|
786
|
+
fs.existsSync(sourceRoot)
|
|
787
|
+
&& isManaged(sourceRoot)
|
|
788
|
+
&& sourceMatchesRegistration(sourceRoot, registered)
|
|
789
|
+
) {
|
|
790
|
+
return sourceRoot;
|
|
791
|
+
}
|
|
792
|
+
if (fs.existsSync(sourceRoot)) {
|
|
793
|
+
throw grokFailure(
|
|
794
|
+
'GROK_ROLLBACK_SOURCE_CONFLICT',
|
|
795
|
+
`cannot reconstruct the prior Grok source over existing bytes: ${sourceRoot}`,
|
|
796
|
+
);
|
|
797
|
+
}
|
|
798
|
+
if (!nativeBackup || !fs.existsSync(nativeBackup)) {
|
|
799
|
+
throw grokFailure(
|
|
800
|
+
'GROK_ROLLBACK_SOURCE_MISSING',
|
|
801
|
+
'the previous Grok plugin source is unavailable and cannot be reconstructed safely',
|
|
802
|
+
);
|
|
803
|
+
}
|
|
804
|
+
managedCopy(nativeBackup, sourceRoot);
|
|
805
|
+
rebaseProvenanceRoot(sourceRoot);
|
|
806
|
+
return sourceRoot;
|
|
807
|
+
}
|
|
808
|
+
|
|
809
|
+
function nativeUninstall(ctx, { allowMissing = false } = {}) {
|
|
810
|
+
const entry = installedPlugin(ctx);
|
|
811
|
+
if (!entry) {
|
|
812
|
+
if (allowMissing) return null;
|
|
813
|
+
throw grokFailure('GROK_PLUGIN_NOT_REGISTERED', 'Ultra Builder Pro is not registered by Grok');
|
|
814
|
+
}
|
|
815
|
+
runGrok(ctx, ['plugin', 'uninstall', PLUGIN_NAME, '--confirm', '--keep-data']);
|
|
816
|
+
return entry;
|
|
817
|
+
}
|
|
818
|
+
|
|
819
|
+
function restoreNativeRegistration(ctx, source, {
|
|
820
|
+
verifyConsumer = true,
|
|
821
|
+
enabled = true,
|
|
822
|
+
} = {}) {
|
|
823
|
+
if (!source || !fs.existsSync(source)) {
|
|
824
|
+
throw grokFailure(
|
|
825
|
+
'GROK_ROLLBACK_SOURCE_MISSING',
|
|
826
|
+
`cannot restore Grok registration from missing source: ${source || '<none>'}`,
|
|
827
|
+
);
|
|
828
|
+
}
|
|
829
|
+
const current = installedPlugin(ctx);
|
|
830
|
+
if (current) nativeUninstall(ctx, { allowMissing: true });
|
|
831
|
+
runGrok(ctx, ['plugin', 'install', source, '--trust']);
|
|
832
|
+
const registered = installedPlugin(ctx);
|
|
833
|
+
if (!registered) {
|
|
834
|
+
throw grokFailure(
|
|
835
|
+
'GROK_ROLLBACK_NOT_REGISTERED',
|
|
836
|
+
'Grok did not register the restored Ultra Builder Pro source',
|
|
837
|
+
);
|
|
838
|
+
}
|
|
839
|
+
const finalized = finalizeNativeRegistration(
|
|
840
|
+
ctx,
|
|
841
|
+
resolveRepoRoot(ctx),
|
|
842
|
+
source,
|
|
843
|
+
registered,
|
|
844
|
+
{ expectedPackageVersion: registered.version },
|
|
845
|
+
);
|
|
846
|
+
const target = finalized.target;
|
|
847
|
+
if (
|
|
848
|
+
typeof registered.source === 'string'
|
|
849
|
+
&& path.resolve(registered.source) !== path.resolve(source)
|
|
850
|
+
) {
|
|
851
|
+
throw grokFailure(
|
|
852
|
+
'GROK_ROLLBACK_SOURCE_MISMATCH',
|
|
853
|
+
`Grok restored Ultra from ${registered.source}, expected ${source}`,
|
|
854
|
+
);
|
|
855
|
+
}
|
|
856
|
+
runGrok(ctx, ['plugin', 'details', PLUGIN_NAME]);
|
|
857
|
+
restoreNativeActivation(ctx, target, enabled);
|
|
858
|
+
let consumer = null;
|
|
859
|
+
if (verifyConsumer) {
|
|
860
|
+
consumer = nativeConsumerState(ctx, registered);
|
|
861
|
+
if (!consumer.healthy) {
|
|
862
|
+
throw grokFailure(
|
|
863
|
+
'GROK_ROLLBACK_NOT_ACTIVE',
|
|
864
|
+
'restored Grok plugin did not pass native inspect and MCP Doctor',
|
|
865
|
+
);
|
|
866
|
+
}
|
|
867
|
+
}
|
|
868
|
+
return { registered, target, consumer };
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
function combinedRollbackFailure(originalError, rollbackError, recovery = {}) {
|
|
872
|
+
const error = grokFailure(
|
|
873
|
+
'GROK_INSTALL_ROLLBACK_FAILED',
|
|
874
|
+
[
|
|
875
|
+
`Grok install failed: ${originalError.message}`,
|
|
876
|
+
`rollback failed: ${rollbackError.message}`,
|
|
877
|
+
].join('; '),
|
|
878
|
+
{
|
|
879
|
+
original_code: originalError.code || null,
|
|
880
|
+
rollback_code: rollbackError.code || null,
|
|
881
|
+
...recovery,
|
|
882
|
+
},
|
|
883
|
+
);
|
|
884
|
+
error.cause = originalError;
|
|
885
|
+
error.original_error = originalError.message;
|
|
886
|
+
error.rollback_error = rollbackError.message;
|
|
887
|
+
return error;
|
|
888
|
+
}
|
|
889
|
+
|
|
890
|
+
function combinedUninstallRollbackFailure(originalError, rollbackError, recovery = {}) {
|
|
891
|
+
const error = grokFailure(
|
|
892
|
+
'GROK_UNINSTALL_ROLLBACK_FAILED',
|
|
893
|
+
[
|
|
894
|
+
`Grok uninstall failed: ${originalError.message}`,
|
|
895
|
+
`rollback failed: ${rollbackError.message}`,
|
|
896
|
+
].join('; '),
|
|
897
|
+
{
|
|
898
|
+
original_code: originalError.code || null,
|
|
899
|
+
rollback_code: rollbackError.code || null,
|
|
900
|
+
...recovery,
|
|
901
|
+
},
|
|
902
|
+
);
|
|
903
|
+
error.cause = originalError;
|
|
904
|
+
error.original_error = originalError.message;
|
|
905
|
+
error.rollback_error = rollbackError.message;
|
|
906
|
+
return error;
|
|
907
|
+
}
|
|
908
|
+
|
|
909
|
+
function assertOwnedForUninstall(target, label) {
|
|
910
|
+
if (!fs.existsSync(target)) return;
|
|
911
|
+
if (!isManaged(target)) {
|
|
912
|
+
throw grokFailure(
|
|
913
|
+
'GROK_UNINSTALL_OWNERSHIP_CONFLICT',
|
|
914
|
+
`refusing to uninstall because ${label} is not owned by Ultra: ${target}`,
|
|
915
|
+
{ label, path: target },
|
|
916
|
+
);
|
|
917
|
+
}
|
|
918
|
+
}
|
|
919
|
+
|
|
920
|
+
function assertManagedProvenance(target, label, {
|
|
921
|
+
expectedPackageName,
|
|
922
|
+
expectedPackageVersion = null,
|
|
923
|
+
allowMarkerOnly = false,
|
|
924
|
+
} = {}) {
|
|
925
|
+
if (!fs.existsSync(target)) return null;
|
|
926
|
+
const stat = fs.lstatSync(target);
|
|
927
|
+
if (!stat.isDirectory()) {
|
|
928
|
+
throw grokFailure(
|
|
929
|
+
'GROK_MANAGED_ROOT_INVALID',
|
|
930
|
+
`${label} is not a managed directory: ${target}`,
|
|
931
|
+
{ label, path: target },
|
|
932
|
+
);
|
|
933
|
+
}
|
|
934
|
+
if (!isManaged(target)) {
|
|
935
|
+
throw grokFailure(
|
|
936
|
+
'GROK_MANAGED_ROOT_UNOWNED',
|
|
937
|
+
`${label} is not owned by Ultra: ${target}`,
|
|
938
|
+
{ label, path: target },
|
|
939
|
+
);
|
|
940
|
+
}
|
|
941
|
+
const file = path.join(target, PROVENANCE_FILE);
|
|
942
|
+
if (allowMarkerOnly && !fs.existsSync(file)) {
|
|
943
|
+
return { status: 'legacy', path: target };
|
|
944
|
+
}
|
|
945
|
+
const report = provenance.inspectProvenance({
|
|
946
|
+
file,
|
|
947
|
+
expectedAdapter: 'grok',
|
|
948
|
+
expectedPackageVersion,
|
|
949
|
+
});
|
|
950
|
+
if (report.status !== 'healthy' || report.package?.name !== expectedPackageName) {
|
|
951
|
+
throw grokFailure(
|
|
952
|
+
'GROK_MANAGED_ROOT_PROVENANCE_INVALID',
|
|
953
|
+
`${label} failed Ultra provenance verification: ${target}`,
|
|
954
|
+
{ label, path: target, issues: report.issues },
|
|
955
|
+
);
|
|
956
|
+
}
|
|
957
|
+
const manifest = JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
958
|
+
const declaredRoot = path.resolve(manifest.roots?.plugin || '');
|
|
959
|
+
const canonicalDeclaredRoot = fs.existsSync(declaredRoot)
|
|
960
|
+
? fs.realpathSync(declaredRoot)
|
|
961
|
+
: declaredRoot;
|
|
962
|
+
const canonicalTarget = fs.realpathSync(target);
|
|
963
|
+
if (canonicalDeclaredRoot !== canonicalTarget) {
|
|
964
|
+
throw grokFailure(
|
|
965
|
+
'GROK_MANAGED_ROOT_PROVENANCE_INVALID',
|
|
966
|
+
`${label} provenance is bound to a different root: ${target}`,
|
|
967
|
+
{
|
|
968
|
+
label,
|
|
969
|
+
path: target,
|
|
970
|
+
declared_root: manifest.roots?.plugin || null,
|
|
971
|
+
},
|
|
972
|
+
);
|
|
973
|
+
}
|
|
974
|
+
return report;
|
|
975
|
+
}
|
|
976
|
+
|
|
977
|
+
function finalizeNativeRegistration(ctx, repoRoot, sourceRoot, registered, {
|
|
978
|
+
expectedPackageVersion = null,
|
|
979
|
+
} = {}) {
|
|
980
|
+
const packageInfo = provenance.packageSource(repoRoot).packageInfo;
|
|
981
|
+
const packageVersion = expectedPackageVersion || packageInfo.version;
|
|
982
|
+
if (registered.version !== packageVersion) {
|
|
983
|
+
throw grokFailure(
|
|
984
|
+
'GROK_PLUGIN_VERSION_MISMATCH',
|
|
985
|
+
`Grok installed Ultra Builder Pro ${registered.version || '<missing>'}, expected ${packageVersion}`,
|
|
986
|
+
{ expected: packageVersion, actual: registered.version || null },
|
|
987
|
+
);
|
|
988
|
+
}
|
|
989
|
+
const target = resolveNativeInstallPath(registered);
|
|
990
|
+
if (!isManaged(target)) {
|
|
991
|
+
throw grokFailure(
|
|
992
|
+
'GROK_PLUGIN_PATH_UNSAFE',
|
|
993
|
+
`Grok registry returned an unmanaged plugin path: ${target}`,
|
|
994
|
+
{ path: target },
|
|
995
|
+
);
|
|
996
|
+
}
|
|
997
|
+
const source = assertRegisteredSource(ctx, registered, {
|
|
998
|
+
expectedPackageName: packageInfo.name,
|
|
999
|
+
expectedPackageVersion: packageVersion,
|
|
1000
|
+
sourceRoot,
|
|
1001
|
+
});
|
|
1002
|
+
const file = path.join(target, PROVENANCE_FILE);
|
|
1003
|
+
let copiedManifest;
|
|
1004
|
+
try {
|
|
1005
|
+
copiedManifest = JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
1006
|
+
} catch (error) {
|
|
1007
|
+
throw grokFailure(
|
|
1008
|
+
'GROK_PLUGIN_TARGET_PROVENANCE_INVALID',
|
|
1009
|
+
`Grok installed target has invalid copied provenance: ${target}`,
|
|
1010
|
+
{ path: target, cause_code: error.code || null },
|
|
1011
|
+
);
|
|
1012
|
+
}
|
|
1013
|
+
const declaredRoot = path.resolve(copiedManifest.roots?.plugin || '');
|
|
1014
|
+
const canonicalDeclaredRoot = fs.existsSync(declaredRoot)
|
|
1015
|
+
? fs.realpathSync(declaredRoot)
|
|
1016
|
+
: declaredRoot;
|
|
1017
|
+
if (
|
|
1018
|
+
canonicalDeclaredRoot !== source.path
|
|
1019
|
+
&& canonicalDeclaredRoot !== target
|
|
1020
|
+
) {
|
|
1021
|
+
throw grokFailure(
|
|
1022
|
+
'GROK_PLUGIN_TARGET_PROVENANCE_INVALID',
|
|
1023
|
+
`Grok installed target provenance is not derived from its registered source: ${target}`,
|
|
1024
|
+
{ path: target, declared_root: copiedManifest.roots?.plugin || null },
|
|
1025
|
+
);
|
|
1026
|
+
}
|
|
1027
|
+
if (
|
|
1028
|
+
typeof copiedManifest.content?.digest !== 'string'
|
|
1029
|
+
|| copiedManifest.content.digest !== source.report.content_digest
|
|
1030
|
+
) {
|
|
1031
|
+
throw grokFailure(
|
|
1032
|
+
'GROK_PLUGIN_TARGET_PROVENANCE_INVALID',
|
|
1033
|
+
`Grok installed target does not match its registered source provenance: ${target}`,
|
|
1034
|
+
{
|
|
1035
|
+
path: target,
|
|
1036
|
+
expected_digest: source.report.content_digest,
|
|
1037
|
+
actual_digest: copiedManifest.content?.digest || null,
|
|
1038
|
+
},
|
|
1039
|
+
);
|
|
1040
|
+
}
|
|
1041
|
+
const manifest = rebaseProvenanceRoot(target);
|
|
1042
|
+
const targetReport = assertManagedProvenance(target, 'registered Grok target', {
|
|
1043
|
+
expectedPackageName: packageInfo.name,
|
|
1044
|
+
expectedPackageVersion: packageVersion,
|
|
1045
|
+
});
|
|
1046
|
+
if (targetReport.content_digest !== source.report.content_digest) {
|
|
1047
|
+
throw grokFailure(
|
|
1048
|
+
'GROK_PLUGIN_TARGET_PROVENANCE_INVALID',
|
|
1049
|
+
`Grok installed target assets differ from its registered source: ${target}`,
|
|
1050
|
+
{
|
|
1051
|
+
path: target,
|
|
1052
|
+
expected_digest: source.report.content_digest,
|
|
1053
|
+
actual_digest: targetReport.content_digest,
|
|
1054
|
+
},
|
|
1055
|
+
);
|
|
1056
|
+
}
|
|
1057
|
+
assertNativeInstallPath(ctx, registered, {
|
|
1058
|
+
expectedPackageName: packageInfo.name,
|
|
1059
|
+
expectedPackageVersion: packageVersion,
|
|
1060
|
+
sourceRoot,
|
|
1061
|
+
});
|
|
1062
|
+
return { target, provenance: { file, manifest } };
|
|
1063
|
+
}
|
|
1064
|
+
|
|
1065
|
+
function installPreflight(ctx, repoRoot, sourceRoot, legacyRoot) {
|
|
1066
|
+
try {
|
|
1067
|
+
const packageInfo = provenance.packageSource(repoRoot).packageInfo;
|
|
1068
|
+
const registered = installedPlugin(ctx);
|
|
1069
|
+
let installedRoot = null;
|
|
1070
|
+
if (registered) {
|
|
1071
|
+
installedRoot = assertNativeInstallPath(ctx, registered, {
|
|
1072
|
+
expectedPackageName: packageInfo.name,
|
|
1073
|
+
expectedPackageVersion: registered.version,
|
|
1074
|
+
sourceRoot,
|
|
1075
|
+
});
|
|
1076
|
+
const consumer = nativeConsumerState(ctx, registered);
|
|
1077
|
+
if (!consumer.healthy) {
|
|
1078
|
+
throw grokFailure(
|
|
1079
|
+
'GROK_PLUGIN_MCP_INACTIVE',
|
|
1080
|
+
'existing Grok registration did not pass native inspect and MCP Doctor',
|
|
1081
|
+
);
|
|
1082
|
+
}
|
|
1083
|
+
} else {
|
|
1084
|
+
assertManagedProvenance(sourceRoot, 'current Grok source', {
|
|
1085
|
+
expectedPackageName: packageInfo.name,
|
|
1086
|
+
expectedPackageVersion: null,
|
|
1087
|
+
});
|
|
1088
|
+
}
|
|
1089
|
+
assertManagedProvenance(legacyRoot, 'legacy Grok plugin', {
|
|
1090
|
+
expectedPackageName: packageInfo.name,
|
|
1091
|
+
allowMarkerOnly: true,
|
|
1092
|
+
});
|
|
1093
|
+
return { registered, installedRoot };
|
|
1094
|
+
} catch (error) {
|
|
1095
|
+
const failure = grokFailure(
|
|
1096
|
+
'GROK_INSTALL_PREFLIGHT_FAILED',
|
|
1097
|
+
`Grok install preflight failed: ${error.message}`,
|
|
1098
|
+
{ cause_code: error.code || null, ...(error.details || {}) },
|
|
1099
|
+
);
|
|
1100
|
+
failure.cause = error;
|
|
1101
|
+
throw failure;
|
|
1102
|
+
}
|
|
1103
|
+
}
|
|
1104
|
+
|
|
292
1105
|
function install(ctx = {}) {
|
|
293
|
-
const
|
|
1106
|
+
const sourceRoot = resolveSourceRoot(ctx);
|
|
1107
|
+
const legacyRoot = resolvePluginRoot(ctx);
|
|
294
1108
|
const repoRoot = resolveRepoRoot(ctx);
|
|
295
|
-
|
|
296
|
-
|
|
1109
|
+
const capability = probeNativeCapabilities(ctx);
|
|
1110
|
+
if (capability.status !== 'pass') {
|
|
1111
|
+
throw grokFailure(
|
|
1112
|
+
'GROK_CLI_UNAVAILABLE',
|
|
1113
|
+
'Grok CLI is required for formal plugin installation; no files were changed',
|
|
1114
|
+
);
|
|
297
1115
|
}
|
|
298
|
-
|
|
299
|
-
const
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
1116
|
+
const preflight = installPreflight(ctx, repoRoot, sourceRoot, legacyRoot);
|
|
1117
|
+
const sourceParent = path.dirname(sourceRoot);
|
|
1118
|
+
ensureDir(sourceParent);
|
|
1119
|
+
const staging = fs.mkdtempSync(path.join(sourceParent, `${PLUGIN_NAME}-staging-`));
|
|
1120
|
+
const sourceBackup = `${staging}-source-previous`;
|
|
1121
|
+
const backupRoot = resolveBackupRoot(ctx);
|
|
1122
|
+
const transactionId = crypto.randomUUID();
|
|
1123
|
+
const legacyBackup = path.join(backupRoot, `legacy-plugin-${transactionId}`);
|
|
1124
|
+
const nativeBackup = path.join(backupRoot, `native-plugin-${transactionId}`);
|
|
1125
|
+
let sourceMoved = false;
|
|
1126
|
+
let sourcePublished = false;
|
|
1127
|
+
let legacyMoved = false;
|
|
1128
|
+
let nativeSnapshot = false;
|
|
1129
|
+
let registryMutationStarted = false;
|
|
1130
|
+
const originalNative = preflight.registered;
|
|
1131
|
+
let built;
|
|
1132
|
+
|
|
303
1133
|
try {
|
|
304
|
-
|
|
305
|
-
if (fs.existsSync(
|
|
306
|
-
|
|
307
|
-
|
|
1134
|
+
built = buildStaging(repoRoot, staging, ctx);
|
|
1135
|
+
if (fs.existsSync(sourceRoot)) {
|
|
1136
|
+
if (!isManaged(sourceRoot)) {
|
|
1137
|
+
throw grokFailure(
|
|
1138
|
+
'GROK_SOURCE_UNMANAGED',
|
|
1139
|
+
`refusing to replace unmanaged Grok plugin source: ${sourceRoot}`,
|
|
1140
|
+
);
|
|
1141
|
+
}
|
|
1142
|
+
fs.renameSync(sourceRoot, sourceBackup);
|
|
1143
|
+
sourceMoved = true;
|
|
308
1144
|
}
|
|
309
|
-
fs.renameSync(staging,
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
1145
|
+
fs.renameSync(staging, sourceRoot);
|
|
1146
|
+
sourcePublished = true;
|
|
1147
|
+
writeProvenance(repoRoot, sourceRoot);
|
|
1148
|
+
|
|
1149
|
+
if (originalNative) {
|
|
1150
|
+
const installedRoot = preflight.installedRoot;
|
|
1151
|
+
if (!isManaged(installedRoot)) {
|
|
1152
|
+
throw grokFailure(
|
|
1153
|
+
'GROK_PLUGIN_OWNERSHIP_CONFLICT',
|
|
1154
|
+
`refusing to replace an installed Grok plugin not owned by Ultra: ${installedRoot}`,
|
|
1155
|
+
);
|
|
1156
|
+
}
|
|
1157
|
+
ensureDir(backupRoot);
|
|
1158
|
+
managedCopy(installedRoot, nativeBackup);
|
|
1159
|
+
nativeSnapshot = true;
|
|
1160
|
+
}
|
|
1161
|
+
|
|
1162
|
+
if (fs.existsSync(legacyRoot)) {
|
|
1163
|
+
if (!isManaged(legacyRoot)) {
|
|
1164
|
+
throw grokFailure(
|
|
1165
|
+
'GROK_LEGACY_PLUGIN_UNMANAGED',
|
|
1166
|
+
`refusing to migrate unmanaged Grok plugin: ${legacyRoot}`,
|
|
1167
|
+
);
|
|
1168
|
+
}
|
|
1169
|
+
ensureDir(backupRoot);
|
|
1170
|
+
fs.renameSync(legacyRoot, legacyBackup);
|
|
1171
|
+
legacyMoved = true;
|
|
1172
|
+
}
|
|
1173
|
+
|
|
1174
|
+
if (originalNative) {
|
|
1175
|
+
registryMutationStarted = true;
|
|
1176
|
+
nativeUninstall(ctx);
|
|
1177
|
+
}
|
|
1178
|
+
registryMutationStarted = true;
|
|
1179
|
+
runGrok(ctx, ['plugin', 'install', sourceRoot, '--trust']);
|
|
1180
|
+
runGrok(ctx, ['plugin', 'enable', PLUGIN_NAME]);
|
|
1181
|
+
const registered = installedPlugin(ctx);
|
|
1182
|
+
if (!registered) {
|
|
1183
|
+
throw grokFailure(
|
|
1184
|
+
'GROK_PLUGIN_NOT_REGISTERED',
|
|
1185
|
+
'Grok accepted the plugin install command but did not register Ultra Builder Pro',
|
|
1186
|
+
);
|
|
1187
|
+
}
|
|
1188
|
+
const finalized = finalizeNativeRegistration(ctx, repoRoot, sourceRoot, registered);
|
|
1189
|
+
const target = finalized.target;
|
|
1190
|
+
const provenanceReport = finalized.provenance;
|
|
1191
|
+
const consumer = nativeConsumerState(ctx, registered);
|
|
1192
|
+
if (!consumer.healthy) {
|
|
1193
|
+
throw grokFailure(
|
|
1194
|
+
'GROK_PLUGIN_NOT_ACTIVE',
|
|
1195
|
+
'Grok registered Ultra Builder Pro but did not activate its plugin MCP consumer',
|
|
1196
|
+
);
|
|
1197
|
+
}
|
|
1198
|
+
|
|
1199
|
+
if (sourceMoved && fs.existsSync(sourceBackup)) removeTree(sourceBackup);
|
|
1200
|
+
if (nativeSnapshot && fs.existsSync(nativeBackup)) removeTree(nativeBackup);
|
|
316
1201
|
return {
|
|
317
1202
|
target,
|
|
1203
|
+
source: sourceRoot,
|
|
318
1204
|
...built,
|
|
319
1205
|
provenance: provenanceReport,
|
|
320
|
-
|
|
1206
|
+
native_registry: {
|
|
1207
|
+
status: 'pass',
|
|
1208
|
+
repo_key: registered.repo_key || null,
|
|
1209
|
+
path: target,
|
|
1210
|
+
},
|
|
1211
|
+
native_consumer: {
|
|
1212
|
+
status: 'pass',
|
|
1213
|
+
mcp: MCP_SERVER_NAME,
|
|
1214
|
+
healthy: true,
|
|
1215
|
+
},
|
|
1216
|
+
migration: legacyMoved ? {
|
|
1217
|
+
legacy_backup: legacyBackup,
|
|
1218
|
+
legacy_source_removed: true,
|
|
1219
|
+
} : null,
|
|
1220
|
+
trusted: true,
|
|
321
1221
|
reload_required: true,
|
|
322
1222
|
};
|
|
323
1223
|
} catch (error) {
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
1224
|
+
let rollbackError = null;
|
|
1225
|
+
let recoverySource = null;
|
|
1226
|
+
try {
|
|
1227
|
+
if (registryMutationStarted) nativeUninstall(ctx, { allowMissing: true });
|
|
1228
|
+
if (sourcePublished && fs.existsSync(sourceRoot)) removeTree(sourceRoot);
|
|
1229
|
+
else if (fs.existsSync(staging)) removeTree(staging);
|
|
1230
|
+
if (sourceMoved && fs.existsSync(sourceBackup)) fs.renameSync(sourceBackup, sourceRoot);
|
|
1231
|
+
if (legacyMoved && fs.existsSync(legacyBackup)) {
|
|
1232
|
+
ensureDir(path.dirname(legacyRoot));
|
|
1233
|
+
fs.renameSync(legacyBackup, legacyRoot);
|
|
1234
|
+
}
|
|
1235
|
+
if (originalNative && registryMutationStarted) {
|
|
1236
|
+
recoverySource = ensureRecoverySource(originalNative, sourceRoot, nativeBackup);
|
|
1237
|
+
restoreNativeRegistration(ctx, recoverySource);
|
|
1238
|
+
}
|
|
1239
|
+
if (nativeSnapshot && fs.existsSync(nativeBackup)) removeTree(nativeBackup);
|
|
1240
|
+
} catch (caughtRollbackError) {
|
|
1241
|
+
rollbackError = caughtRollbackError;
|
|
1242
|
+
}
|
|
1243
|
+
if (rollbackError) {
|
|
1244
|
+
throw combinedRollbackFailure(error, rollbackError, {
|
|
1245
|
+
recovery_source: recoverySource,
|
|
1246
|
+
native_backup: nativeSnapshot && fs.existsSync(nativeBackup) ? nativeBackup : null,
|
|
1247
|
+
});
|
|
1248
|
+
}
|
|
327
1249
|
throw error;
|
|
328
1250
|
}
|
|
329
1251
|
}
|
|
330
1252
|
|
|
331
1253
|
function doctor(ctx = {}) {
|
|
332
|
-
const target = resolvePluginRoot(ctx);
|
|
333
1254
|
const source = provenance.packageSource(resolveRepoRoot(ctx));
|
|
1255
|
+
const capability = probeNativeCapabilities(ctx);
|
|
1256
|
+
if (capability.status === 'unavailable') {
|
|
1257
|
+
const sourceRoot = resolveSourceRoot(ctx);
|
|
1258
|
+
const report = provenance.inspectProvenance({
|
|
1259
|
+
file: path.join(sourceRoot, PROVENANCE_FILE),
|
|
1260
|
+
expectedAdapter: 'grok',
|
|
1261
|
+
expectedPackageVersion: source.packageInfo.version,
|
|
1262
|
+
});
|
|
1263
|
+
report.checks.native_registry = { status: 'unavailable' };
|
|
1264
|
+
report.checks.native_consumer = { status: 'unavailable' };
|
|
1265
|
+
report.issues.push({
|
|
1266
|
+
code: 'GROK_CLI_UNAVAILABLE',
|
|
1267
|
+
message: 'Grok CLI is unavailable, so native registry and MCP activation were not verified',
|
|
1268
|
+
});
|
|
1269
|
+
report.status = 'degraded';
|
|
1270
|
+
return applyNativeDoctor(report, path.join(sourceRoot, 'runtime'));
|
|
1271
|
+
}
|
|
1272
|
+
|
|
1273
|
+
let registered;
|
|
1274
|
+
try {
|
|
1275
|
+
registered = installedPlugin(ctx);
|
|
1276
|
+
} catch (error) {
|
|
1277
|
+
return {
|
|
1278
|
+
adapter: 'grok',
|
|
1279
|
+
status: 'degraded',
|
|
1280
|
+
manifest_path: null,
|
|
1281
|
+
checks: { native_registry: { status: 'fail' } },
|
|
1282
|
+
issues: [{ code: error.code || 'GROK_PLUGIN_LIST_FAILED', message: error.message }],
|
|
1283
|
+
};
|
|
1284
|
+
}
|
|
1285
|
+
if (!registered) {
|
|
1286
|
+
const legacyRoot = resolvePluginRoot(ctx);
|
|
1287
|
+
return {
|
|
1288
|
+
adapter: 'grok',
|
|
1289
|
+
status: 'degraded',
|
|
1290
|
+
manifest_path: fs.existsSync(path.join(legacyRoot, PROVENANCE_FILE))
|
|
1291
|
+
? path.join(legacyRoot, PROVENANCE_FILE)
|
|
1292
|
+
: null,
|
|
1293
|
+
checks: {
|
|
1294
|
+
native_registry: { status: 'fail' },
|
|
1295
|
+
raw_plugin: {
|
|
1296
|
+
status: fs.existsSync(legacyRoot) ? 'found' : 'missing',
|
|
1297
|
+
path: legacyRoot,
|
|
1298
|
+
},
|
|
1299
|
+
},
|
|
1300
|
+
issues: [{
|
|
1301
|
+
code: 'GROK_PLUGIN_NOT_REGISTERED',
|
|
1302
|
+
message: fs.existsSync(legacyRoot)
|
|
1303
|
+
? 'a raw Grok plugin exists, but the native Grok plugin registry does not contain it'
|
|
1304
|
+
: 'Ultra Builder Pro is not registered in the native Grok plugin registry',
|
|
1305
|
+
}],
|
|
1306
|
+
};
|
|
1307
|
+
}
|
|
1308
|
+
|
|
1309
|
+
let target;
|
|
1310
|
+
try {
|
|
1311
|
+
target = assertNativeInstallPath(ctx, registered, {
|
|
1312
|
+
expectedPackageName: source.packageInfo.name,
|
|
1313
|
+
expectedPackageVersion: source.packageInfo.version,
|
|
1314
|
+
sourceRoot: resolveSourceRoot(ctx),
|
|
1315
|
+
});
|
|
1316
|
+
} catch (error) {
|
|
1317
|
+
return {
|
|
1318
|
+
adapter: 'grok',
|
|
1319
|
+
status: 'degraded',
|
|
1320
|
+
manifest_path: null,
|
|
1321
|
+
checks: {
|
|
1322
|
+
native_registry: {
|
|
1323
|
+
status: 'fail',
|
|
1324
|
+
repo_key: registered.repo_key || null,
|
|
1325
|
+
path: registered.path,
|
|
1326
|
+
},
|
|
1327
|
+
native_consumer: { status: 'unknown' },
|
|
1328
|
+
},
|
|
1329
|
+
issues: [{
|
|
1330
|
+
code: error.code || 'GROK_PLUGIN_PATH_INVALID',
|
|
1331
|
+
message: error.message,
|
|
1332
|
+
...(error.details || {}),
|
|
1333
|
+
}],
|
|
1334
|
+
};
|
|
1335
|
+
}
|
|
334
1336
|
const report = provenance.inspectProvenance({
|
|
335
1337
|
file: path.join(target, PROVENANCE_FILE),
|
|
336
1338
|
expectedAdapter: 'grok',
|
|
337
1339
|
expectedPackageVersion: source.packageInfo.version,
|
|
338
1340
|
});
|
|
1341
|
+
report.checks.native_registry = {
|
|
1342
|
+
status: 'pass',
|
|
1343
|
+
repo_key: registered.repo_key || null,
|
|
1344
|
+
path: target,
|
|
1345
|
+
};
|
|
339
1346
|
try {
|
|
340
|
-
const
|
|
341
|
-
report.checks.
|
|
1347
|
+
const details = runGrok(ctx, ['plugin', 'details', PLUGIN_NAME]);
|
|
1348
|
+
report.checks.plugin_details = {
|
|
1349
|
+
status: 'pass',
|
|
1350
|
+
output: details.stdout.trim(),
|
|
1351
|
+
};
|
|
1352
|
+
} catch (error) {
|
|
1353
|
+
report.checks.plugin_details = { status: 'fail' };
|
|
1354
|
+
report.issues.push({
|
|
1355
|
+
code: 'GROK_PLUGIN_DETAILS_FAILED',
|
|
1356
|
+
message: error.message,
|
|
1357
|
+
});
|
|
1358
|
+
}
|
|
1359
|
+
try {
|
|
1360
|
+
const consumer = nativeConsumerState(ctx, registered);
|
|
1361
|
+
report.checks.native_consumer = {
|
|
1362
|
+
status: consumer.healthy ? 'pass' : 'fail',
|
|
1363
|
+
plugin_discovered: Boolean(consumer.plugin),
|
|
1364
|
+
plugin_mcp_active: Boolean(consumer.mcp),
|
|
1365
|
+
mcp_healthy: consumer.doctorServer?.healthy === true,
|
|
1366
|
+
};
|
|
1367
|
+
if (!consumer.healthy) {
|
|
1368
|
+
report.issues.push({
|
|
1369
|
+
code: 'GROK_PLUGIN_MCP_INACTIVE',
|
|
1370
|
+
message: 'Grok native inspection or MCP Doctor did not confirm the plugin MCP consumer',
|
|
1371
|
+
});
|
|
1372
|
+
}
|
|
1373
|
+
} catch (error) {
|
|
1374
|
+
report.checks.native_consumer = { status: 'fail' };
|
|
1375
|
+
report.issues.push({
|
|
1376
|
+
code: error.code || 'GROK_PLUGIN_MCP_CHECK_FAILED',
|
|
1377
|
+
message: error.message,
|
|
1378
|
+
});
|
|
1379
|
+
}
|
|
1380
|
+
try {
|
|
1381
|
+
report.checks.plugin_validation = validatePlugin(target, ctx);
|
|
342
1382
|
} catch (error) {
|
|
343
1383
|
report.checks.plugin_validation = { status: 'fail' };
|
|
344
1384
|
report.issues.push({ code: 'PLUGIN_MANIFEST_INVALID', message: error.message });
|
|
@@ -347,16 +1387,169 @@ function doctor(ctx = {}) {
|
|
|
347
1387
|
}
|
|
348
1388
|
|
|
349
1389
|
function uninstall(ctx = {}) {
|
|
350
|
-
const
|
|
351
|
-
const
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
1390
|
+
const sourceRoot = resolveSourceRoot(ctx);
|
|
1391
|
+
const legacyRoot = resolvePluginRoot(ctx);
|
|
1392
|
+
const capability = probeNativeCapabilities(ctx);
|
|
1393
|
+
if (capability.status !== 'pass') {
|
|
1394
|
+
throw grokFailure(
|
|
1395
|
+
'GROK_CLI_UNAVAILABLE',
|
|
1396
|
+
'Grok CLI is required for formal plugin uninstall; no files were changed',
|
|
1397
|
+
);
|
|
1398
|
+
}
|
|
1399
|
+
const report = {
|
|
1400
|
+
target: null,
|
|
1401
|
+
source: sourceRoot,
|
|
1402
|
+
removed: {},
|
|
1403
|
+
native_registry: { status: capability.status },
|
|
1404
|
+
};
|
|
1405
|
+
const registered = installedPlugin(ctx);
|
|
1406
|
+
let installedRoot = null;
|
|
1407
|
+
let registeredEnabled = null;
|
|
1408
|
+
if (registered) {
|
|
1409
|
+
installedRoot = resolveNativeInstallPath(registered);
|
|
1410
|
+
report.target = installedRoot;
|
|
1411
|
+
}
|
|
1412
|
+
const declaredSource = registered && typeof registered.source === 'string'
|
|
1413
|
+
? path.resolve(registered.source)
|
|
1414
|
+
: null;
|
|
1415
|
+
const ownershipTargets = [
|
|
1416
|
+
[installedRoot, 'managed target'],
|
|
1417
|
+
[sourceRoot, 'persistent source'],
|
|
1418
|
+
[legacyRoot, 'legacy raw plugin'],
|
|
1419
|
+
[
|
|
1420
|
+
declaredSource
|
|
1421
|
+
&& declaredSource !== path.resolve(sourceRoot)
|
|
1422
|
+
&& declaredSource !== installedRoot
|
|
1423
|
+
? declaredSource
|
|
1424
|
+
: null,
|
|
1425
|
+
'registered source',
|
|
1426
|
+
],
|
|
1427
|
+
];
|
|
1428
|
+
for (const [target, label] of ownershipTargets) {
|
|
1429
|
+
if (target) assertOwnedForUninstall(target, label);
|
|
1430
|
+
}
|
|
1431
|
+
if (registered) {
|
|
1432
|
+
installedRoot = assertNativeInstallPath(ctx, registered);
|
|
1433
|
+
runGrok(ctx, ['plugin', 'details', PLUGIN_NAME]);
|
|
1434
|
+
registeredEnabled = nativeActivationState(ctx, installedRoot);
|
|
1435
|
+
}
|
|
1436
|
+
|
|
1437
|
+
const transactionId = crypto.randomUUID();
|
|
1438
|
+
const sourceBackup = path.join(
|
|
1439
|
+
path.dirname(sourceRoot),
|
|
1440
|
+
`.${path.basename(sourceRoot)}-uninstall-${transactionId}`,
|
|
1441
|
+
);
|
|
1442
|
+
const legacyBackup = path.join(
|
|
1443
|
+
path.dirname(legacyRoot),
|
|
1444
|
+
`.${path.basename(legacyRoot)}-uninstall-${transactionId}`,
|
|
1445
|
+
);
|
|
1446
|
+
const backupRoot = resolveBackupRoot(ctx);
|
|
1447
|
+
const nativeBackup = path.join(
|
|
1448
|
+
backupRoot,
|
|
1449
|
+
`uninstall-native-${transactionId}`,
|
|
1450
|
+
);
|
|
1451
|
+
let sourceStaged = false;
|
|
1452
|
+
let legacyStaged = false;
|
|
1453
|
+
let nativeSnapshot = false;
|
|
1454
|
+
let registryMutationStarted = false;
|
|
1455
|
+
try {
|
|
1456
|
+
if (fs.existsSync(sourceRoot)) {
|
|
1457
|
+
fs.renameSync(sourceRoot, sourceBackup);
|
|
1458
|
+
sourceStaged = true;
|
|
1459
|
+
}
|
|
1460
|
+
if (fs.existsSync(legacyRoot)) {
|
|
1461
|
+
fs.renameSync(legacyRoot, legacyBackup);
|
|
1462
|
+
legacyStaged = true;
|
|
355
1463
|
}
|
|
356
|
-
|
|
357
|
-
|
|
1464
|
+
if (registered) {
|
|
1465
|
+
ensureDir(backupRoot);
|
|
1466
|
+
managedCopy(installedRoot, nativeBackup);
|
|
1467
|
+
nativeSnapshot = true;
|
|
1468
|
+
registryMutationStarted = true;
|
|
1469
|
+
nativeUninstall(ctx);
|
|
1470
|
+
report.removed.native_plugin = true;
|
|
1471
|
+
}
|
|
1472
|
+
const remaining = installedPlugin(ctx);
|
|
1473
|
+
if (remaining) {
|
|
1474
|
+
throw grokFailure(
|
|
1475
|
+
'GROK_UNINSTALL_NOT_REMOVED',
|
|
1476
|
+
'Grok reported uninstall success but the native registry still contains Ultra Builder Pro',
|
|
1477
|
+
);
|
|
1478
|
+
}
|
|
1479
|
+
if (sourceStaged) report.removed.source = true;
|
|
1480
|
+
if (legacyStaged) report.removed.legacy_plugin = true;
|
|
1481
|
+
if (sourceStaged && fs.existsSync(sourceBackup)) removeTree(sourceBackup);
|
|
1482
|
+
if (legacyStaged && fs.existsSync(legacyBackup)) removeTree(legacyBackup);
|
|
1483
|
+
if (nativeSnapshot && fs.existsSync(nativeBackup)) removeTree(nativeBackup);
|
|
1484
|
+
return report;
|
|
1485
|
+
} catch (error) {
|
|
1486
|
+
let rollbackError = null;
|
|
1487
|
+
let recoverySource = null;
|
|
1488
|
+
try {
|
|
1489
|
+
if (sourceStaged && fs.existsSync(sourceBackup)) {
|
|
1490
|
+
if (fs.existsSync(sourceRoot)) {
|
|
1491
|
+
throw grokFailure(
|
|
1492
|
+
'GROK_UNINSTALL_ROLLBACK_SOURCE_CONFLICT',
|
|
1493
|
+
`cannot restore Grok source over existing bytes: ${sourceRoot}`,
|
|
1494
|
+
);
|
|
1495
|
+
}
|
|
1496
|
+
fs.renameSync(sourceBackup, sourceRoot);
|
|
1497
|
+
}
|
|
1498
|
+
if (legacyStaged && fs.existsSync(legacyBackup)) {
|
|
1499
|
+
if (fs.existsSync(legacyRoot)) {
|
|
1500
|
+
throw grokFailure(
|
|
1501
|
+
'GROK_UNINSTALL_ROLLBACK_LEGACY_CONFLICT',
|
|
1502
|
+
`cannot restore legacy Grok plugin over existing bytes: ${legacyRoot}`,
|
|
1503
|
+
);
|
|
1504
|
+
}
|
|
1505
|
+
fs.renameSync(legacyBackup, legacyRoot);
|
|
1506
|
+
}
|
|
1507
|
+
if (registered && registryMutationStarted) {
|
|
1508
|
+
const current = installedPlugin(ctx);
|
|
1509
|
+
if (current) {
|
|
1510
|
+
const currentTarget = assertNativeInstallPath(ctx, current);
|
|
1511
|
+
const originalSource = typeof registered.source === 'string'
|
|
1512
|
+
? path.resolve(registered.source)
|
|
1513
|
+
: null;
|
|
1514
|
+
const currentSource = typeof current.source === 'string'
|
|
1515
|
+
? path.resolve(current.source)
|
|
1516
|
+
: null;
|
|
1517
|
+
if (currentTarget !== installedRoot || currentSource !== originalSource) {
|
|
1518
|
+
throw grokFailure(
|
|
1519
|
+
'GROK_UNINSTALL_ROLLBACK_REGISTRY_MISMATCH',
|
|
1520
|
+
'remaining Grok registration does not match the pre-uninstall registry entry',
|
|
1521
|
+
{
|
|
1522
|
+
expected_path: installedRoot,
|
|
1523
|
+
actual_path: currentTarget,
|
|
1524
|
+
expected_source: originalSource,
|
|
1525
|
+
actual_source: currentSource,
|
|
1526
|
+
},
|
|
1527
|
+
);
|
|
1528
|
+
}
|
|
1529
|
+
runGrok(ctx, ['plugin', 'details', PLUGIN_NAME]);
|
|
1530
|
+
restoreNativeActivation(ctx, currentTarget, registeredEnabled);
|
|
1531
|
+
} else {
|
|
1532
|
+
recoverySource = ensureRecoverySource(registered, sourceRoot, nativeBackup);
|
|
1533
|
+
restoreNativeRegistration(ctx, recoverySource, {
|
|
1534
|
+
verifyConsumer: false,
|
|
1535
|
+
enabled: registeredEnabled,
|
|
1536
|
+
});
|
|
1537
|
+
}
|
|
1538
|
+
}
|
|
1539
|
+
if (nativeSnapshot && fs.existsSync(nativeBackup)) removeTree(nativeBackup);
|
|
1540
|
+
} catch (caughtRollbackError) {
|
|
1541
|
+
rollbackError = caughtRollbackError;
|
|
1542
|
+
}
|
|
1543
|
+
if (rollbackError) {
|
|
1544
|
+
throw combinedUninstallRollbackFailure(error, rollbackError, {
|
|
1545
|
+
recovery_source: recoverySource,
|
|
1546
|
+
native_backup: nativeSnapshot && fs.existsSync(nativeBackup) ? nativeBackup : null,
|
|
1547
|
+
source_backup: fs.existsSync(sourceBackup) ? sourceBackup : null,
|
|
1548
|
+
legacy_backup: fs.existsSync(legacyBackup) ? legacyBackup : null,
|
|
1549
|
+
});
|
|
1550
|
+
}
|
|
1551
|
+
throw error;
|
|
358
1552
|
}
|
|
359
|
-
return report;
|
|
360
1553
|
}
|
|
361
1554
|
|
|
362
1555
|
module.exports = {
|
|
@@ -366,6 +1559,9 @@ module.exports = {
|
|
|
366
1559
|
buildHooksManifest,
|
|
367
1560
|
resolveTarget,
|
|
368
1561
|
resolvePluginRoot,
|
|
1562
|
+
resolveSourceRoot,
|
|
1563
|
+
resolveGrokBinary,
|
|
1564
|
+
probeNativeCapabilities,
|
|
369
1565
|
install,
|
|
370
1566
|
doctor,
|
|
371
1567
|
uninstall,
|