@getmikie/cli 1.1.113 → 1.1.114

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/bin/mikie.mjs CHANGED
@@ -18,6 +18,7 @@ import {
18
18
  resolveDependencyPackageJson,
19
19
  resolveNativeBinary,
20
20
  selectPlatformPackage,
21
+ shouldReconcileInstalledClients,
21
22
  translateArguments,
22
23
  } from "../lib/launcher.mjs";
23
24
  import {
@@ -26,9 +27,9 @@ import {
26
27
  createFilesystemAdapter,
27
28
  createRegistryAdapter,
28
29
  detectUpdateTopology,
29
- externalRecoveryInvocation,
30
30
  parseUpdateHandoff,
31
31
  parseTrajectoryReconciliationReceipt,
32
+ recoverPendingNpmUpdate,
32
33
  runUpdateTransaction,
33
34
  } from "../lib/update.mjs";
34
35
  import {
@@ -88,33 +89,29 @@ All commands execute the compiled Mikie native engine.`);
88
89
  throw new Error("global npm installation authority is unavailable");
89
90
  }
90
91
  const topology = detectUpdateTopology({ globalNodeModules: npmRoot.stdout.trim() });
92
+ const selected = selectPlatformPackage();
93
+ const platformPackageJson = require.resolve(`${selected.packageName}/package.json`);
94
+ const wrapperPackageJson = fileURLToPath(new URL("../package.json", import.meta.url));
95
+ const binary = await resolveNativeBinary();
96
+ const globalPrefix = npmPrefix.stdout.trim();
97
+ const globalShims =
98
+ process.platform === "win32"
99
+ ? ["mikie", "mikie.cmd", "mikie.ps1"].map((name) => path.join(globalPrefix, name))
100
+ : [path.join(globalPrefix, "bin", "mikie")];
91
101
  const customerHome = homedir();
92
102
  const transactionRoot = path.join(customerHome, ".mikie", "npm-update-v1");
93
103
  const recoveryJournal = path.join(transactionRoot, "journal.json");
94
104
  if (topology === UpdateTopology.NpmGlobal) {
95
105
  try {
96
106
  await access(recoveryJournal);
97
- const invocation = await externalRecoveryInvocation(transactionRoot);
98
- const recovered = await commands.run(invocation.executable, invocation.args, {
99
- shell: false,
100
- capture: true,
107
+ await recoverPendingNpmUpdate(transactionRoot, {
108
+ commands,
109
+ supersedingInstallation: { version: packageMetadata.version, binary },
101
110
  });
102
- if (recovered.code !== 0) {
103
- throw new Error("pending Mikie update recovery failed");
104
- }
105
111
  } catch (error) {
106
112
  if (error?.code !== "ENOENT") throw error;
107
113
  }
108
114
  }
109
- const selected = selectPlatformPackage();
110
- const platformPackageJson = require.resolve(`${selected.packageName}/package.json`);
111
- const wrapperPackageJson = fileURLToPath(new URL("../package.json", import.meta.url));
112
- const binary = await resolveNativeBinary();
113
- const globalPrefix = npmPrefix.stdout.trim();
114
- const globalShims =
115
- process.platform === "win32"
116
- ? ["mikie", "mikie.cmd", "mikie.ps1"].map((name) => path.join(globalPrefix, name))
117
- : [path.join(globalPrefix, "bin", "mikie")];
118
115
  const handoffResult = await launchNativeCaptured(binary, ["update"], { topology });
119
116
  if (handoffResult.code !== 0) {
120
117
  if (handoffResult.stderr) process.stderr.write(handoffResult.stderr);
@@ -247,8 +244,10 @@ All commands execute the compiled Mikie native engine.`);
247
244
  }
248
245
  } else {
249
246
  try {
250
- await reconcileInstalledClients({ version: packageMetadata.version });
251
247
  const nativeArguments = translateArguments(arguments_);
248
+ if (shouldReconcileInstalledClients(nativeArguments)) {
249
+ await reconcileInstalledClients({ version: packageMetadata.version });
250
+ }
252
251
  const binary = await prepareNativeLaunch(await resolveNativeBinary(), nativeArguments, {
253
252
  version: packageMetadata.version,
254
253
  });
package/lib/launcher.mjs CHANGED
@@ -424,6 +424,11 @@ export function shouldStageWindowsRuntime(arguments_) {
424
424
  return arguments_[0] === "control" && arguments_[1] === "run";
425
425
  }
426
426
 
427
+ /** Return whether a native runtime entrypoint needs installed client launchers reconciled first. */
428
+ export function shouldReconcileInstalledClients(arguments_) {
429
+ return ["serve", "run-relay", "run-codex-hook", "run-claude-hook"].includes(arguments_[0]);
430
+ }
431
+
427
432
  async function materializeRuntime(
428
433
  binary,
429
434
  { version, runtimeRoot, executableName, cleanupHistorical },
package/lib/update.mjs CHANGED
@@ -19,7 +19,7 @@ import { isDeepStrictEqual } from "node:util";
19
19
 
20
20
  const WRAPPER_PACKAGE = "@getmikie/cli";
21
21
  const HANDOFF_SCHEMA = "mikie.update-handoff.v1";
22
- const REPAIR_SCHEMA = "mikie.claude-hook-repair.v1";
22
+ const REPAIR_SCHEMA = "mikie.claude-hook-repair.v2";
23
23
  const TRAJECTORY_RECONCILIATION_SCHEMA = "mikie.trajectory-store-reconciliation.v1";
24
24
  const RERUN_COMMAND = "npx -y @getmikie/cli@latest update";
25
25
  const MAX_COMMAND_OUTPUT_BYTES = 1024 * 1024;
@@ -331,11 +331,70 @@ export async function externalRecoveryInvocation(transactionRoot) {
331
331
  "command",
332
332
  ]) ||
333
333
  recovery.descriptor !== path.join(transactionRoot, "recovery.json") ||
334
- !/^[a-f0-9]{64}$/.test(recovery.descriptor_sha256)
334
+ recovery.launcher !== path.join(transactionRoot, "recover.mjs") ||
335
+ recovery.runtime !== path.join(transactionRoot, "recovery-runtime.mjs") ||
336
+ recovery.node !== process.execPath ||
337
+ ![
338
+ recovery.descriptor_sha256,
339
+ recovery.launcher_sha256,
340
+ recovery.runtime_sha256,
341
+ ].every((digest) => /^[a-f0-9]{64}$/.test(digest))
335
342
  ) {
336
343
  throw new Error("invalid Mikie update recovery authority");
337
344
  }
338
- return authenticatedRecoveryInvocation(transactionRoot, recovery.descriptor_sha256);
345
+ return {
346
+ ...authenticatedRecoveryInvocation(transactionRoot, recovery.descriptor_sha256),
347
+ authority: {
348
+ descriptorSha256: recovery.descriptor_sha256,
349
+ launcherSha256: recovery.launcher_sha256,
350
+ runtimeSha256: recovery.runtime_sha256,
351
+ },
352
+ };
353
+ }
354
+
355
+ /** Recover with frozen transaction code first, then the authenticated current implementation. */
356
+ export async function recoverPendingNpmUpdate(
357
+ transactionRoot,
358
+ {
359
+ commands = createCommandAdapter(),
360
+ recoverCurrent = recoverExternalNpmUpdate,
361
+ supersedingInstallation,
362
+ } = {},
363
+ ) {
364
+ const invocation = await externalRecoveryInvocation(transactionRoot);
365
+ const frozen = await commands.run(invocation.executable, invocation.args, {
366
+ shell: false,
367
+ capture: true,
368
+ });
369
+ if (frozen.code === 0) return { status: "recovered", method: "frozen" };
370
+ try {
371
+ let authenticatedSupersedingInstallation;
372
+ if (supersedingInstallation !== undefined) {
373
+ if (
374
+ !exactPublicVersion(supersedingInstallation?.version) ||
375
+ typeof supersedingInstallation?.binary !== "string" ||
376
+ !path.isAbsolute(supersedingInstallation.binary)
377
+ ) {
378
+ throw new Error("invalid superseding Mikie installation authority");
379
+ }
380
+ authenticatedSupersedingInstallation = {
381
+ version: supersedingInstallation.version,
382
+ binarySha256: await executableDigest(supersedingInstallation.binary),
383
+ };
384
+ }
385
+ await recoverCurrent(transactionRoot, invocation.authority, {
386
+ supersedingInstallation: authenticatedSupersedingInstallation,
387
+ });
388
+ return { status: "recovered", method: "current" };
389
+ } catch (error) {
390
+ const message = error instanceof Error ? error.message : "";
391
+ const reason = /tampered|invalid|unsafe|authority|corrupt|changed/i.test(message)
392
+ ? "recovery authority could not be authenticated"
393
+ : /settlement|configured client/i.test(message)
394
+ ? "configured client state could not be settled"
395
+ : "authenticated recovery could not complete";
396
+ throw new Error(`pending Mikie update recovery failed: ${reason}; transaction preserved`);
397
+ }
339
398
  }
340
399
 
341
400
  function expectedHandoff(topology) {
@@ -632,12 +691,17 @@ function parseRepairReceipt(stdout) {
632
691
  receipt.after === "current" &&
633
692
  receipt.action === "already_current" &&
634
693
  receipt.restart_required === false;
694
+ const absent =
695
+ receipt.before === "absent" &&
696
+ receipt.after === "absent" &&
697
+ receipt.action === "not_configured" &&
698
+ receipt.restart_required === false;
635
699
  const skipped =
636
700
  ["foreign_conflict", "unavailable"].includes(receipt.before) &&
637
701
  receipt.after === receipt.before &&
638
702
  receipt.action === "refused" &&
639
703
  receipt.restart_required === false;
640
- if (!migrated && !current && !skipped) {
704
+ if (!migrated && !current && !absent && !skipped) {
641
705
  throw new Error("candidate owned-adapter repair returned an invalid receipt");
642
706
  }
643
707
  return { receipt, skipped };
@@ -779,6 +843,7 @@ export async function runUpdateTransaction({
779
843
  MIKIE_DISTRIBUTION_MODE: "npm",
780
844
  MIKIE_NPM_UPDATE_TOPOLOGY: UpdateTopology.NpmGlobal,
781
845
  MIKIE_NPM_UPDATE_REPAIR_PHASE: "prepare",
846
+ MIKIE_NPM_CLIENT_REPAIR_SCHEMA: REPAIR_SCHEMA,
782
847
  MIKIE_UPDATE_REPAIR_LAUNCHER_STATE: "prior",
783
848
  MIKIE_UPDATE_REPAIR_CANDIDATE: candidate.binary,
784
849
  MIKIE_UPDATE_REPAIR_LAUNCHER: finalLauncher,
@@ -816,8 +881,10 @@ export async function runUpdateTransaction({
816
881
  await files.activate(activatedCandidate);
817
882
  await files.checkpointCandidate(snapshot, { provider: true });
818
883
  const settlementRequired =
819
- repairResult.receipt.action === "migrated" ||
820
- files.requiresFinalLauncherSettlement?.() === true;
884
+ !repairResult.skipped &&
885
+ repairResult.receipt.action !== "not_configured" &&
886
+ (repairResult.receipt.action === "migrated" ||
887
+ files.requiresFinalLauncherSettlement?.() === true);
821
888
  // A skipped receipt does not prove that no client still references a
822
889
  // historical launcher. In particular, Claude can report unavailable
823
890
  // when hooks/ownership are absent while an MCP-only registration remains.
@@ -840,6 +907,7 @@ export async function runUpdateTransaction({
840
907
  MIKIE_DISTRIBUTION_MODE: "npm",
841
908
  MIKIE_NPM_UPDATE_TOPOLOGY: UpdateTopology.NpmGlobal,
842
909
  MIKIE_NPM_UPDATE_REPAIR_PHASE: "commit",
910
+ MIKIE_NPM_CLIENT_REPAIR_SCHEMA: REPAIR_SCHEMA,
843
911
  MIKIE_UPDATE_REPAIR_LAUNCHER_STATE: "activated",
844
912
  MIKIE_UPDATE_REPAIR_CANDIDATE: candidate.binary,
845
913
  MIKIE_UPDATE_REPAIR_LAUNCHER: finalLauncher,
@@ -861,7 +929,7 @@ export async function runUpdateTransaction({
861
929
  ? "skipped"
862
930
  : repairResult.receipt.action === "already_current"
863
931
  ? "verified"
864
- : "migrated",
932
+ : repairResult.receipt.action,
865
933
  foreignSettings: verification.foreignSettings ?? [],
866
934
  };
867
935
  } catch (error) {
@@ -875,6 +943,7 @@ export async function runUpdateTransaction({
875
943
  MIKIE_DISTRIBUTION_MODE: "npm",
876
944
  MIKIE_NPM_UPDATE_TOPOLOGY: UpdateTopology.NpmGlobal,
877
945
  MIKIE_NPM_UPDATE_REPAIR_PHASE: "rollback",
946
+ MIKIE_NPM_CLIENT_REPAIR_SCHEMA: REPAIR_SCHEMA,
878
947
  MIKIE_UPDATE_REPAIR_LAUNCHER_STATE:
879
948
  files.preparedLauncherState?.(snapshot) ?? "prior",
880
949
  MIKIE_UPDATE_REPAIR_CANDIDATE: candidate.binary,
@@ -1338,6 +1407,7 @@ export function createFilesystemAdapter({
1338
1407
  prepareFinalLauncher,
1339
1408
  retireFinalLauncher,
1340
1409
  abandonFinalLauncher,
1410
+ supersedingInstallation,
1341
1411
  }) {
1342
1412
  const currentRoots = validateInstallationLayout(currentLayout);
1343
1413
  const currentShims = currentRoots.shims;
@@ -1354,7 +1424,10 @@ export function createFilesystemAdapter({
1354
1424
  (activationBoundary !== undefined && typeof activationBoundary !== "function") ||
1355
1425
  (prepareFinalLauncher !== undefined && typeof prepareFinalLauncher !== "function") ||
1356
1426
  (retireFinalLauncher !== undefined && typeof retireFinalLauncher !== "function") ||
1357
- (abandonFinalLauncher !== undefined && typeof abandonFinalLauncher !== "function")
1427
+ (abandonFinalLauncher !== undefined && typeof abandonFinalLauncher !== "function") ||
1428
+ (supersedingInstallation !== undefined &&
1429
+ (!exactPublicVersion(supersedingInstallation?.version) ||
1430
+ !/^[a-f0-9]{64}$/.test(supersedingInstallation?.binarySha256 ?? "")))
1358
1431
  ) {
1359
1432
  throw new Error("unsafe Mikie update filesystem path");
1360
1433
  }
@@ -2359,6 +2432,86 @@ export function createFilesystemAdapter({
2359
2432
  return owner?.activationState === "activated" ? "activated" : "prior";
2360
2433
  };
2361
2434
 
2435
+ const committedActivationMatches = async (snapshot) => {
2436
+ for (const entry of snapshot.installation) {
2437
+ if (
2438
+ !entry.expectedFingerprint ||
2439
+ (await directoryFingerprint(entry.root)) !== entry.expectedFingerprint
2440
+ ) {
2441
+ return false;
2442
+ }
2443
+ }
2444
+ for (const entry of snapshot.shims) {
2445
+ if (
2446
+ !entry.expectedFingerprint ||
2447
+ (await readShimState(entry.path)).fingerprint !== entry.expectedFingerprint
2448
+ ) {
2449
+ return false;
2450
+ }
2451
+ }
2452
+ return true;
2453
+ };
2454
+
2455
+ const authenticateSupersedingInstallation = async () => {
2456
+ if (!supersedingInstallation) return false;
2457
+ try {
2458
+ const [wrapper, platform, binarySha256] = await Promise.all([
2459
+ readFile(currentLayout.wrapperPackageJson, "utf8"),
2460
+ readFile(currentLayout.platformPackageJson, "utf8"),
2461
+ executableDigest(currentLayout.binary),
2462
+ ]);
2463
+ return (
2464
+ JSON.parse(wrapper)?.version === supersedingInstallation.version &&
2465
+ JSON.parse(platform)?.version === supersedingInstallation.version &&
2466
+ binarySha256 === supersedingInstallation.binarySha256
2467
+ );
2468
+ } catch {
2469
+ return false;
2470
+ }
2471
+ };
2472
+
2473
+ const cleanupSupersededCommittedActivation = async (snapshot) => {
2474
+ for (const entry of snapshot.installation) {
2475
+ const priorFingerprint = await directoryFingerprint(entry.priorQuarantine);
2476
+ const stagedFingerprint = await directoryFingerprint(entry.activationStaging);
2477
+ if (
2478
+ (priorFingerprint !== "absent" && priorFingerprint !== entry.sourceFingerprint) ||
2479
+ (stagedFingerprint !== "absent" && stagedFingerprint !== entry.expectedFingerprint)
2480
+ ) {
2481
+ throw new Error("tampered Mikie superseded activation state");
2482
+ }
2483
+ }
2484
+ for (const entry of snapshot.shims) {
2485
+ const prior = await readShimState(entry.priorQuarantine);
2486
+ const staged = await readShimState(entry.activationStaging);
2487
+ if (
2488
+ (prior.existed && prior.fingerprint !== entry.sourceFingerprint) ||
2489
+ (staged.existed && staged.fingerprint !== entry.expectedFingerprint)
2490
+ ) {
2491
+ throw new Error("tampered Mikie superseded activation state");
2492
+ }
2493
+ }
2494
+ for (const entry of snapshot.installation) {
2495
+ await rm(entry.priorQuarantine, { recursive: true, force: true });
2496
+ await rm(entry.activationStaging, { recursive: true, force: true });
2497
+ }
2498
+ for (const entry of snapshot.shims) {
2499
+ await rm(entry.priorQuarantine, { force: true });
2500
+ await rm(entry.activationStaging, { force: true });
2501
+ }
2502
+ };
2503
+
2504
+ const providerConfigurationMatchesSnapshot = async (snapshot) => {
2505
+ for (const entry of snapshot.providerConfig) {
2506
+ await assertNoLinkedAncestors(entry.path);
2507
+ const current = await readOptionalFile(entry.path);
2508
+ if (bytesFingerprint(current.existed, current.bytes) !== entry.sourceFingerprint) {
2509
+ return false;
2510
+ }
2511
+ }
2512
+ return true;
2513
+ };
2514
+
2362
2515
  const recoverInterrupted = async (lease, expectedJournalToken) => {
2363
2516
  try {
2364
2517
  const metadata = await lstat(transactionRoot);
@@ -2370,6 +2523,33 @@ export function createFilesystemAdapter({
2370
2523
  throw error;
2371
2524
  }
2372
2525
  const snapshot = await hydrateJournal(lease, expectedJournalToken);
2526
+ if (snapshot.phase === "commit_decided_retain_runtime") {
2527
+ const activationMatches = await committedActivationMatches(snapshot);
2528
+ if (!activationMatches && (await authenticateSupersedingInstallation())) {
2529
+ await cleanupSupersededCommittedActivation(snapshot);
2530
+ await rm(transactionRoot, { recursive: true, force: true });
2531
+ return;
2532
+ }
2533
+ }
2534
+ if (
2535
+ snapshot.phase === "commit_decided_retain_runtime" &&
2536
+ (await providerConfigurationMatchesSnapshot(snapshot))
2537
+ ) {
2538
+ if (!reconcileTrajectoryStore) {
2539
+ throw new Error("trajectory-store reconciliation is unavailable");
2540
+ }
2541
+ await reconcileTrajectoryStore(snapshot.preparedCandidate.finalLauncher, snapshot);
2542
+ const activationMatches = await committedActivationMatches(snapshot);
2543
+ if (!activationMatches && !(await authenticateSupersedingInstallation())) {
2544
+ throw new Error("committed Mikie package changed before cleanup");
2545
+ }
2546
+ snapshot.phase = "committed_retain_runtime";
2547
+ await writeJournal(snapshot);
2548
+ if (activationMatches) await cleanupCommittedActivation(snapshot);
2549
+ else await cleanupSupersededCommittedActivation(snapshot);
2550
+ await rm(transactionRoot, { recursive: true, force: true });
2551
+ return;
2552
+ }
2373
2553
  if (["commit_decided", "commit_decided_retain_runtime"].includes(snapshot.phase)) {
2374
2554
  const retainHistoricalRuntimes = snapshot.phase === "commit_decided_retain_runtime";
2375
2555
  if (!reconcileTrajectoryStore) {
@@ -2412,7 +2592,12 @@ export function createFilesystemAdapter({
2412
2592
  return;
2413
2593
  }
2414
2594
  if (snapshot.phase === "committed_retain_runtime") {
2415
- await cleanupCommittedActivation(snapshot);
2595
+ const activationMatches = await committedActivationMatches(snapshot);
2596
+ if (!activationMatches && !(await authenticateSupersedingInstallation())) {
2597
+ throw new Error("committed Mikie package changed before cleanup");
2598
+ }
2599
+ if (activationMatches) await cleanupCommittedActivation(snapshot);
2600
+ else await cleanupSupersededCommittedActivation(snapshot);
2416
2601
  await rm(transactionRoot, { recursive: true, force: true });
2417
2602
  return;
2418
2603
  }
@@ -3079,7 +3264,11 @@ export function createFilesystemAdapter({
3079
3264
  return adapter;
3080
3265
  }
3081
3266
 
3082
- export async function recoverExternalNpmUpdate(transactionRoot, expectedAuthority) {
3267
+ export async function recoverExternalNpmUpdate(
3268
+ transactionRoot,
3269
+ expectedAuthority,
3270
+ { supersedingInstallation } = {},
3271
+ ) {
3083
3272
  if (typeof transactionRoot !== "string" || !path.isAbsolute(transactionRoot)) {
3084
3273
  throw new Error("unsafe Mikie update recovery root");
3085
3274
  }
@@ -3172,6 +3361,7 @@ export async function recoverExternalNpmUpdate(transactionRoot, expectedAuthorit
3172
3361
  },
3173
3362
  providerPaths: descriptor.provider_paths,
3174
3363
  transactionRoot,
3364
+ supersedingInstallation,
3175
3365
  settleProviderRepair: async (phase, snapshot) => {
3176
3366
  if (
3177
3367
  snapshot?.preparedCandidate?.binary !== descriptor.candidate_binary ||
@@ -3189,6 +3379,7 @@ export async function recoverExternalNpmUpdate(transactionRoot, expectedAuthorit
3189
3379
  MIKIE_DISTRIBUTION_MODE: "npm",
3190
3380
  MIKIE_NPM_UPDATE_TOPOLOGY: UpdateTopology.NpmGlobal,
3191
3381
  MIKIE_NPM_UPDATE_REPAIR_PHASE: phase,
3382
+ MIKIE_NPM_CLIENT_REPAIR_SCHEMA: REPAIR_SCHEMA,
3192
3383
  MIKIE_UPDATE_REPAIR_CANDIDATE: descriptor.candidate_binary,
3193
3384
  MIKIE_UPDATE_REPAIR_LAUNCHER: descriptor.final_launcher,
3194
3385
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@getmikie/cli",
3
- "version": "1.1.113",
3
+ "version": "1.1.114",
4
4
  "description": "Native Mikie relay launcher for MCP clients",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -24,10 +24,10 @@
24
24
  "node": ">=22"
25
25
  },
26
26
  "optionalDependencies": {
27
- "@getmikie/darwin-arm64": "1.1.113",
28
- "@getmikie/darwin-x64": "1.1.113",
29
- "@getmikie/linux-x64-gnu": "1.1.113",
30
- "@getmikie/win32-x64": "1.1.113"
27
+ "@getmikie/darwin-arm64": "1.1.114",
28
+ "@getmikie/darwin-x64": "1.1.114",
29
+ "@getmikie/linux-x64-gnu": "1.1.114",
30
+ "@getmikie/win32-x64": "1.1.114"
31
31
  },
32
32
  "publishConfig": {
33
33
  "access": "public",
@@ -18,7 +18,7 @@ import {
18
18
  const RECONCILIATION_ARGUMENTS = ["reconcile-trajectory-store-v2", "--json"];
19
19
  const CLIENT_REPAIR_ARGUMENTS = ["repair-claude-hooks-v1", "--json"];
20
20
  const CLIENT_RECONCILIATION_SCHEMA = "mikie.npm-client-reconciliation.v2";
21
- const CLIENT_REPAIR_SCHEMA = "mikie.claude-hook-repair.v1";
21
+ const CLIENT_REPAIR_SCHEMA = "mikie.claude-hook-repair.v2";
22
22
  const CLIENT_SETTLEMENT_SCHEMA = "mikie.claude-hook-repair-settlement.v1";
23
23
  const TRANSACTION_TOKEN = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
24
24
  const PUBLIC_VERSION = /^[0-9]+\.[0-9]+\.[0-9]+(?:-[0-9A-Za-z.-]+)?$/;
@@ -453,11 +453,12 @@ export async function reconcileInstalledClients({
453
453
  preserveSha256,
454
454
  }),
455
455
  commands = createCommandAdapter({ environment, platform }),
456
+ allowDeferred = false,
456
457
  } = {}) {
457
458
  if (isTransactionStagedInstall(environment, platform)) {
458
459
  return { status: "staged_candidate" };
459
460
  }
460
- if (!PUBLIC_VERSION.test(version ?? "")) {
461
+ if (!PUBLIC_VERSION.test(version ?? "") || typeof allowDeferred !== "boolean") {
461
462
  throw new Error("installed client reconciliation authority is invalid");
462
463
  }
463
464
 
@@ -513,7 +514,9 @@ export async function reconcileInstalledClients({
513
514
  const preserved = explicitMarker
514
515
  ? [authority.runtime_sha256]
515
516
  : await retainedRuntimeDigests(markerRoot, platform);
516
- await retireLauncher(launcher, authority.runtime_sha256, preserved);
517
+ if (!allowDeferred) {
518
+ await retireLauncher(launcher, authority.runtime_sha256, preserved);
519
+ }
517
520
  return { status: "current" };
518
521
  }
519
522
  const finalLauncher = await prepareLauncher(binary);
@@ -523,6 +526,7 @@ export async function reconcileInstalledClients({
523
526
  const baseEnvironment = {
524
527
  MIKIE_DISTRIBUTION_MODE: "npm",
525
528
  MIKIE_NPM_UPDATE_TOPOLOGY: "npm_global",
529
+ MIKIE_NPM_CLIENT_REPAIR_SCHEMA: CLIENT_REPAIR_SCHEMA,
526
530
  MIKIE_UPDATE_REPAIR_CANDIDATE: binary,
527
531
  MIKIE_UPDATE_REPAIR_LAUNCHER: finalLauncher,
528
532
  };
@@ -540,6 +544,9 @@ export async function reconcileInstalledClients({
540
544
  });
541
545
  parseClientSettlement(committed, settlementPhase);
542
546
  if (preparedReceipt.action === "refused") {
547
+ if (allowDeferred) {
548
+ return { status: "deferred", providerState: preparedReceipt.before };
549
+ }
543
550
  throw new Error(
544
551
  "Mikie could not authenticate every configured client; close active clients and run `mikie update` before reinstalling",
545
552
  );
@@ -554,7 +561,9 @@ export async function reconcileInstalledClients({
554
561
  const preserved = explicitMarker
555
562
  ? [authority.runtime_sha256]
556
563
  : await retainedRuntimeDigests(markerRoot, platform);
557
- await retireLauncher(finalLauncher, authority.runtime_sha256, preserved);
564
+ if (!allowDeferred) {
565
+ await retireLauncher(finalLauncher, authority.runtime_sha256, preserved);
566
+ }
558
567
  return { status: "reconciled", finalLauncher };
559
568
  }
560
569
 
@@ -566,7 +575,13 @@ if (scriptIdentity !== null && scriptIdentity === invokedIdentity) {
566
575
  const packageMetadata = JSON.parse(
567
576
  await readFile(fileURLToPath(new URL("../package.json", import.meta.url)), "utf8"),
568
577
  );
569
- await reconcileInstalledClients({ version: packageMetadata.version });
578
+ const clients = await reconcileInstalledClients({
579
+ version: packageMetadata.version,
580
+ allowDeferred: true,
581
+ });
582
+ if (clients.status === "deferred") {
583
+ console.warn("Mikie install client repair deferred; run `mikie setup` or `mikie doctor`.");
584
+ }
570
585
  } catch (error) {
571
586
  console.error(`Mikie install reconciliation failed: ${error.message}`);
572
587
  process.exitCode = 1;