@kb-labs/cli-commands 2.89.0 → 2.93.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/dist/index.js CHANGED
@@ -185,7 +185,7 @@ function ensureManifestLoader(manifest) {
185
185
  }
186
186
  }
187
187
  function createUnavailableManifest(pkgName, error) {
188
- const rawMsg = (error?.message || String(error) || "").toString();
188
+ const rawMsg = (error instanceof Error ? error.message : String(error) || "").toString();
189
189
  let missing = null;
190
190
  const m1 = rawMsg.match(/Cannot find (?:module|package) '([^']+)'/);
191
191
  const m2 = rawMsg.match(/from ['"]([^'"]+)['"]/);
@@ -206,9 +206,13 @@ function createUnavailableManifest(pkgName, error) {
206
206
  requires,
207
207
  loader: async () => {
208
208
  throw new Error(`Cannot load ${pkgName} CLI manifest. ${rawMsg}`);
209
- }
209
+ },
210
+ // Mark as synthetic so saveCache can skip it — synthetic manifests must never
211
+ // be persisted to disk because they represent transient load failures.
212
+ // Caching them would make the error "stick" until the TTL expires even after
213
+ // the underlying problem (missing build artifact, broken dep) is resolved.
214
+ _synthetic: true
210
215
  };
211
- manifest._synthetic = true;
212
216
  return manifest;
213
217
  }
214
218
  function resetInProcCache() {
@@ -294,7 +298,7 @@ async function detectNewWorkspacePackages(cwd, cachedPackages) {
294
298
  }
295
299
  }
296
300
  } catch (error) {
297
- log("debug", `[plugins][cache] Workspace scan skipped: ${error?.message || "unknown error"}`);
301
+ log("debug", `[plugins][cache] Workspace scan skipped: ${error instanceof Error ? error.message : "unknown error"}`);
298
302
  }
299
303
  return false;
300
304
  }
@@ -305,7 +309,7 @@ async function loadManifestWithTimeout(manifestPath, pkgName, pkgRoot) {
305
309
  try {
306
310
  return await Promise.race([loadManifest(manifestPath, pkgName, pkgRoot), timeout]);
307
311
  } catch (err) {
308
- if (err.message === "Timeout") {
312
+ if (err instanceof Error && err.message === "Timeout") {
309
313
  log("warn", `Timeout loading manifest from ${pkgName}`);
310
314
  return [];
311
315
  }
@@ -325,10 +329,12 @@ function getNamespaceFromManifest(manifestV2, packageName) {
325
329
  async function loadManifest(manifestPath, pkgName, pkgRoot) {
326
330
  const fileUrl = pathToFileURL(manifestPath).href;
327
331
  const mod = await import(fileUrl);
328
- const manifest = mod.manifest || mod.default;
329
- if (!manifest || typeof manifest !== "object" || manifest.schema !== "kb.plugin/3") {
332
+ const modTyped = mod;
333
+ const rawManifest = modTyped.manifest || modTyped.default;
334
+ if (!rawManifest || typeof rawManifest !== "object" || rawManifest.schema !== "kb.plugin/3") {
330
335
  throw new Error(`Unsupported manifest format in ${pkgName}. Only kb.plugin/3 schema is supported.`);
331
336
  }
337
+ const manifest = rawManifest;
332
338
  const namespace = getNamespaceFromManifest(manifest, pkgName);
333
339
  const manifestDir = path.dirname(manifestPath);
334
340
  const baseRoot = pkgRoot || manifestDir;
@@ -390,8 +396,9 @@ async function loadConfig(cwd) {
390
396
  }
391
397
  }
392
398
  async function findManifestPath(pkgRoot, pkg) {
393
- if (pkg.kb?.manifest) {
394
- const manifestPath = path.join(pkgRoot, pkg.kb.manifest);
399
+ const kb = pkg.kb;
400
+ if (kb?.manifest) {
401
+ const manifestPath = path.join(pkgRoot, kb.manifest);
395
402
  try {
396
403
  await promises.access(manifestPath);
397
404
  return { path: manifestPath, deprecated: false };
@@ -399,8 +406,9 @@ async function findManifestPath(pkgRoot, pkg) {
399
406
  return { path: null, deprecated: false };
400
407
  }
401
408
  }
402
- if (pkg.exports?.["./kb/commands"]) {
403
- const exportPath = pkg.exports["./kb/commands"];
409
+ const exports$1 = pkg.exports;
410
+ if (exports$1?.["./kb/commands"]) {
411
+ const exportPath = exports$1["./kb/commands"];
404
412
  const manifestPath = typeof exportPath === "string" ? exportPath : exportPath.default || exportPath.import;
405
413
  if (manifestPath) {
406
414
  const resolved = path.resolve(pkgRoot, manifestPath);
@@ -504,20 +512,21 @@ async function discoverProjectLocalPlugins(projectRoot) {
504
512
  }
505
513
  async function loadManifestsForPackages(packageInfos, source, scope) {
506
514
  const loadPromises = packageInfos.map(async ({ pkgRoot, pkg, manifestPath }) => {
515
+ const pkgName = pkg.name;
507
516
  const pkgStart = Date.now();
508
517
  try {
509
- const manifests = await loadManifestWithTimeout(manifestPath, pkg.name, pkgRoot);
518
+ const manifests = await loadManifestWithTimeout(manifestPath, pkgName, pkgRoot);
510
519
  const pkgTime = Date.now() - pkgStart;
511
520
  if (pkgTime > 30) {
512
- log("debug", `[plugins][perf] ${pkg.name} manifest parse: ${pkgTime}ms (budget: 30ms)`);
521
+ log("debug", `[plugins][perf] ${pkgName} manifest parse: ${pkgTime}ms (budget: 30ms)`);
513
522
  }
514
523
  if (manifests.length > 0) {
515
- validateUniqueIds(manifests, pkg.name);
524
+ validateUniqueIds(manifests, pkgName);
516
525
  return {
517
526
  manifests,
518
527
  source,
519
528
  scope,
520
- packageName: pkg.name,
529
+ packageName: pkgName,
521
530
  manifestPath: toPosixPath(manifestPath),
522
531
  pkgRoot: toPosixPath(pkgRoot)
523
532
  };
@@ -525,21 +534,23 @@ async function loadManifestsForPackages(packageInfos, source, scope) {
525
534
  return null;
526
535
  } catch (err) {
527
536
  const pkgTime = Date.now() - pkgStart;
528
- log("debug", `[plugins][perf] ${pkg.name} failed after ${pkgTime}ms`);
537
+ log("debug", `[plugins][perf] ${pkgName} failed after ${pkgTime}ms`);
538
+ const errMsg = err instanceof Error ? err.message : String(err);
539
+ const errCode = err.code ?? "UNKNOWN";
529
540
  log("warn", JSON.stringify({
530
541
  code: "DISCOVERY_MANIFEST_LOAD_FAIL",
531
- packageName: pkg.name,
542
+ packageName: pkgName,
532
543
  manifestPath: toPosixPath(manifestPath),
533
- errorCode: err.code || "UNKNOWN",
534
- errorMessage: err.message,
535
- hint: err.message.includes("Cannot find package") ? "Run: kb devlink apply && pnpm -w build" : "Check manifest syntax and dependencies"
544
+ errorCode: errCode,
545
+ errorMessage: errMsg,
546
+ hint: errMsg.includes("Cannot find package") ? "Run: kb devlink apply && pnpm -w build" : "Check manifest syntax and dependencies"
536
547
  }));
537
- const synthetic = createUnavailableManifest(pkg.name, err);
548
+ const synthetic = createUnavailableManifest(pkgName, err);
538
549
  return {
539
550
  manifests: [synthetic],
540
551
  source,
541
552
  scope,
542
- packageName: pkg.name,
553
+ packageName: pkgName,
543
554
  manifestPath: toPosixPath(manifestPath),
544
555
  pkgRoot: toPosixPath(pkgRoot)
545
556
  };
@@ -560,29 +571,30 @@ async function discoverCurrentPackage(cwd) {
560
571
  if (!pkg) {
561
572
  return null;
562
573
  }
574
+ const pkgName = pkg.name;
563
575
  const manifestInfo = await findManifestPath(cwd, pkg);
564
576
  if (manifestInfo.path) {
565
577
  if (manifestInfo.deprecated) {
566
- log("warn", `[DEPRECATED] ${pkg.name} uses legacy manifest path: ${manifestInfo.path}`);
578
+ log("warn", `[DEPRECATED] ${pkgName} uses legacy manifest path: ${manifestInfo.path}`);
567
579
  log("warn", ` \u2192 Migrate to exports["./kb/commands"] or set kb.commandsManifest in package.json`);
568
580
  }
569
- const manifests = await loadManifestWithTimeout(manifestInfo.path, pkg.name, cwd);
581
+ const manifests = await loadManifestWithTimeout(manifestInfo.path, pkgName, cwd);
570
582
  if (manifests.length > 0) {
571
- validateUniqueIds(manifests, pkg.name);
583
+ validateUniqueIds(manifests, pkgName);
572
584
  return {
573
585
  manifests,
574
586
  source: "workspace",
575
587
  // Current-package fallback fires in installed mode when the user's
576
588
  // project itself exposes a manifest. That maps to the project scope.
577
589
  scope: "project",
578
- packageName: pkg.name,
590
+ packageName: pkgName,
579
591
  manifestPath: toPosixPath(manifestInfo.path),
580
592
  pkgRoot: toPosixPath(cwd)
581
593
  };
582
594
  }
583
595
  }
584
596
  } catch (err) {
585
- log("debug", `No CLI manifest in current package: ${err.message}`);
597
+ log("debug", `No CLI manifest in current package: ${err instanceof Error ? err.message : String(err)}`);
586
598
  }
587
599
  return null;
588
600
  }
@@ -599,7 +611,7 @@ async function discoverNodeModules(cwd) {
599
611
  }
600
612
  const scanEntry = async () => {
601
613
  let pkgRoot;
602
- let pkg;
614
+ let pkg = null;
603
615
  if (entry.name.startsWith("@")) {
604
616
  const scopeDir = path.join(nmDir, entry.name);
605
617
  try {
@@ -610,29 +622,30 @@ async function discoverNodeModules(cwd) {
610
622
  if (!pkg) {
611
623
  continue;
612
624
  }
625
+ const scopedPkgName = pkg.name;
613
626
  const isPlugin = isPluginPackage(pkg);
614
- if (pkg.name?.startsWith("@kb-labs/")) {
627
+ if (scopedPkgName?.startsWith("@kb-labs/")) {
615
628
  const manifestInfo = await findManifestPath(pkgRoot, pkg);
616
629
  if (manifestInfo.path) {
617
630
  packageInfos.push({ pkgRoot, pkg, manifestPath: manifestInfo.path });
618
631
  }
619
632
  } else if (isPlugin) {
620
- if (config.block?.includes(pkg.name)) {
621
- log("debug", `Plugin ${pkg.name} blocked by config`);
633
+ if (config.block?.includes(scopedPkgName ?? "")) {
634
+ log("debug", `Plugin ${scopedPkgName} blocked by config`);
622
635
  return;
623
636
  }
624
- const isAllowlisted = config.allow?.includes(pkg.name) || config.linked?.includes(pkg.name);
637
+ const isAllowlisted = config.allow?.includes(scopedPkgName ?? "") || config.linked?.includes(scopedPkgName ?? "");
625
638
  if (!isAllowlisted) {
626
- log("debug", `Plugin ${pkg.name} skipped (not allowlisted). Add to kb-labs.config.json plugins.allow or enable via 'kb marketplace enable'`);
639
+ log("debug", `Plugin ${scopedPkgName} skipped (not allowlisted). Add to kb-labs.config.json plugins.allow or enable via 'kb marketplace enable'`);
627
640
  return;
628
641
  }
629
642
  const manifestInfo = await findManifestPath(pkgRoot, pkg);
630
643
  if (manifestInfo.path) {
631
644
  if (manifestInfo.deprecated) {
632
- log("warn", `[DEPRECATED] ${pkg.name} uses legacy manifest path: ${manifestInfo.path}`);
645
+ log("warn", `[DEPRECATED] ${scopedPkgName} uses legacy manifest path: ${manifestInfo.path}`);
633
646
  log("warn", ` \u2192 Migrate to exports["./kb/commands"] or set kb.commandsManifest in package.json`);
634
647
  }
635
- const isLinked = config.linked?.includes(pkg.name);
648
+ const isLinked = config.linked?.includes(scopedPkgName ?? "");
636
649
  packageInfos.push({ pkgRoot, pkg, manifestPath: manifestInfo.path, isLinked });
637
650
  }
638
651
  }
@@ -645,24 +658,25 @@ async function discoverNodeModules(cwd) {
645
658
  if (!pkg) {
646
659
  return;
647
660
  }
661
+ const unscopedPkgName = pkg.name;
648
662
  const isPlugin = isPluginPackage(pkg);
649
663
  if (isPlugin) {
650
- if (config.block?.includes(pkg.name)) {
651
- log("debug", `Plugin ${pkg.name} blocked by config`);
664
+ if (config.block?.includes(unscopedPkgName ?? "")) {
665
+ log("debug", `Plugin ${unscopedPkgName} blocked by config`);
652
666
  return;
653
667
  }
654
- const isAllowlisted = config.allow?.includes(pkg.name) || config.linked?.includes(pkg.name);
668
+ const isAllowlisted = config.allow?.includes(unscopedPkgName ?? "") || config.linked?.includes(unscopedPkgName ?? "");
655
669
  if (!isAllowlisted) {
656
- log("debug", `Plugin ${pkg.name} skipped (not allowlisted). Add to kb-labs.config.json plugins.allow or enable via 'kb marketplace enable'`);
670
+ log("debug", `Plugin ${unscopedPkgName} skipped (not allowlisted). Add to kb-labs.config.json plugins.allow or enable via 'kb marketplace enable'`);
657
671
  return;
658
672
  }
659
673
  const manifestInfo = await findManifestPath(pkgRoot, pkg);
660
674
  if (manifestInfo.path) {
661
675
  if (manifestInfo.deprecated) {
662
- log("warn", `[DEPRECATED] ${pkg.name} uses legacy manifest path: ${manifestInfo.path}`);
676
+ log("warn", `[DEPRECATED] ${unscopedPkgName} uses legacy manifest path: ${manifestInfo.path}`);
663
677
  log("warn", ` \u2192 Migrate to exports["./kb/commands"] or set kb.commandsManifest in package.json`);
664
678
  }
665
- const isLinked = config.linked?.includes(pkg.name);
679
+ const isLinked = config.linked?.includes(unscopedPkgName ?? "");
666
680
  packageInfos.push({ pkgRoot, pkg, manifestPath: manifestInfo.path, isLinked });
667
681
  }
668
682
  }
@@ -672,10 +686,11 @@ async function discoverNodeModules(cwd) {
672
686
  }
673
687
  await Promise.allSettled(scanPromises);
674
688
  const loadPromises = packageInfos.map(async ({ pkgRoot, pkg, manifestPath, isLinked }) => {
689
+ const pkgName = pkg.name;
675
690
  try {
676
- const manifests = await loadManifestWithTimeout(manifestPath, pkg.name, pkgRoot);
691
+ const manifests = await loadManifestWithTimeout(manifestPath, pkgName, pkgRoot);
677
692
  if (manifests.length > 0) {
678
- validateUniqueIds(manifests, pkg.name);
693
+ validateUniqueIds(manifests, pkgName);
679
694
  return {
680
695
  manifests,
681
696
  source: isLinked ? "linked" : "node_modules",
@@ -683,27 +698,29 @@ async function discoverNodeModules(cwd) {
683
698
  // platformRoot === projectRoot so either label is correct; we use
684
699
  // platform to reflect where the manifest physically lives.
685
700
  scope: "platform",
686
- packageName: pkg.name,
701
+ packageName: pkgName,
687
702
  manifestPath: toPosixPath(manifestPath),
688
703
  pkgRoot: toPosixPath(pkgRoot)
689
704
  };
690
705
  }
691
706
  return null;
692
707
  } catch (err) {
708
+ const errMsg = err instanceof Error ? err.message : String(err);
709
+ const errCode = err.code ?? "UNKNOWN";
693
710
  log("warn", JSON.stringify({
694
711
  code: "DISCOVERY_MANIFEST_LOAD_FAIL",
695
- packageName: pkg.name,
712
+ packageName: pkgName,
696
713
  manifestPath: toPosixPath(manifestPath),
697
- errorCode: err.code || "UNKNOWN",
698
- errorMessage: err.message,
699
- hint: err.message.includes("Cannot find package") ? "Run: kb devlink apply && pnpm -w build" : "Check manifest syntax and dependencies"
714
+ errorCode: errCode,
715
+ errorMessage: errMsg,
716
+ hint: errMsg.includes("Cannot find package") ? "Run: kb devlink apply && pnpm -w build" : "Check manifest syntax and dependencies"
700
717
  }));
701
- const synthetic = createUnavailableManifest(pkg.name, err);
718
+ const synthetic = createUnavailableManifest(pkgName, err);
702
719
  return {
703
720
  manifests: [synthetic],
704
721
  source: isLinked ? "linked" : "node_modules",
705
722
  scope: "platform",
706
- packageName: pkg.name,
723
+ packageName: pkgName,
707
724
  manifestPath: toPosixPath(manifestPath),
708
725
  pkgRoot: toPosixPath(pkgRoot)
709
726
  };
@@ -718,7 +735,7 @@ async function discoverNodeModules(cwd) {
718
735
  }
719
736
  return results;
720
737
  } catch (err) {
721
- log("debug", `Could not scan node_modules: ${err.message}`);
738
+ log("debug", `Could not scan node_modules: ${err instanceof Error ? err.message : String(err)}`);
722
739
  return [];
723
740
  }
724
741
  }
@@ -842,7 +859,7 @@ async function isPackageCacheStale(entry, options) {
842
859
  return true;
843
860
  }
844
861
  } catch (error) {
845
- log("debug", `Package cache invalidated: missing package.json for ${entry.result.packageName} (${error?.message || "unknown"})`);
862
+ log("debug", `Package cache invalidated: missing package.json for ${entry.result.packageName} (${error instanceof Error ? error.message : "unknown"})`);
846
863
  return true;
847
864
  }
848
865
  let manifestStat;
@@ -853,7 +870,7 @@ async function isPackageCacheStale(entry, options) {
853
870
  return true;
854
871
  }
855
872
  } catch (error) {
856
- log("debug", `Package cache invalidated: manifest deleted for ${entry.result.packageName} (${error?.message || "unknown"})`);
873
+ log("debug", `Package cache invalidated: manifest deleted for ${entry.result.packageName} (${error instanceof Error ? error.message : "unknown"})`);
857
874
  return true;
858
875
  }
859
876
  if (options.validateHash) {
@@ -864,7 +881,7 @@ async function isPackageCacheStale(entry, options) {
864
881
  return true;
865
882
  }
866
883
  } catch (error) {
867
- log("debug", `Package cache hash validation failed for ${entry.result.packageName}: ${error?.message || "unknown"}`);
884
+ log("debug", `Package cache hash validation failed for ${entry.result.packageName}: ${error instanceof Error ? error.message : "unknown"}`);
868
885
  return true;
869
886
  }
870
887
  }
@@ -910,7 +927,7 @@ async function saveCache(cwd, results, roots) {
910
927
  stateHasher.update(result.packageName);
911
928
  stateHasher.update(manifestHash);
912
929
  } catch (err) {
913
- log("debug", `Failed to cache package ${result.packageName}: ${err.message}`);
930
+ log("debug", `Failed to cache package ${result.packageName}: ${err instanceof Error ? err.message : String(err)}`);
914
931
  }
915
932
  }
916
933
  const lockfileHash = await computeLockfileHash(cwd);
@@ -953,7 +970,7 @@ async function saveCache(cwd, results, roots) {
953
970
  try {
954
971
  await promises.writeFile(cachePath, JSON.stringify(cache), "utf8");
955
972
  } catch (err) {
956
- log("debug", `Failed to save cache: ${err.message}`);
973
+ log("debug", `Failed to save cache: ${err instanceof Error ? err.message : String(err)}`);
957
974
  }
958
975
  }
959
976
  async function discoverManifests(cwd, noCache = false, options = {}) {
@@ -1773,11 +1790,12 @@ var diag = defineSystemCommand({
1773
1790
  }
1774
1791
  });
1775
1792
  } catch (err) {
1793
+ const errMsg = err instanceof Error ? err.message : String(err);
1776
1794
  diagnostics.push({
1777
1795
  category: "marketplace",
1778
1796
  status: "error",
1779
- message: `Discovery failed: ${err.message}`,
1780
- details: { error: err.message }
1797
+ message: `Discovery failed: ${errMsg}`,
1798
+ details: { error: errMsg }
1781
1799
  });
1782
1800
  }
1783
1801
  try {
@@ -1806,11 +1824,12 @@ var diag = defineSystemCommand({
1806
1824
  });
1807
1825
  }
1808
1826
  } catch (err) {
1827
+ const errMsg = err instanceof Error ? err.message : String(err);
1809
1828
  diagnostics.push({
1810
1829
  category: "cache",
1811
1830
  status: "warning",
1812
- message: `Cache check failed: ${err.message}`,
1813
- details: { error: err.message }
1831
+ message: `Cache check failed: ${errMsg}`,
1832
+ details: { error: errMsg }
1814
1833
  });
1815
1834
  }
1816
1835
  try {
@@ -1833,11 +1852,12 @@ var diag = defineSystemCommand({
1833
1852
  });
1834
1853
  }
1835
1854
  } catch (err) {
1855
+ const errMsg = err instanceof Error ? err.message : String(err);
1836
1856
  diagnostics.push({
1837
1857
  category: "marketplace-lock",
1838
1858
  status: "error",
1839
- message: `Marketplace lock check failed: ${err.message}`,
1840
- details: { error: err.message }
1859
+ message: `Marketplace lock check failed: ${errMsg}`,
1860
+ details: { error: errMsg }
1841
1861
  });
1842
1862
  }
1843
1863
  const versionIssues = [];
@@ -1877,11 +1897,12 @@ var diag = defineSystemCommand({
1877
1897
  });
1878
1898
  }
1879
1899
  } catch (err) {
1900
+ const errMsg = err instanceof Error ? err.message : String(err);
1880
1901
  diagnostics.push({
1881
1902
  category: "versions",
1882
1903
  status: "error",
1883
- message: `Version check failed: ${err.message}`,
1884
- details: { error: err.message }
1904
+ message: `Version check failed: ${errMsg}`,
1905
+ details: { error: errMsg }
1885
1906
  });
1886
1907
  }
1887
1908
  const summary = {
@@ -1915,7 +1936,7 @@ var diag = defineSystemCommand({
1915
1936
  const icon = diag2.status === "ok" ? safeSymbols.success : diag2.status === "warning" ? safeSymbols.warning : safeSymbols.error;
1916
1937
  const colorize = diag2.status === "ok" ? safeColors.success : diag2.status === "warning" ? safeColors.warning : safeColors.error;
1917
1938
  diagItems.push(`${icon} ${colorize(safeColors.bold(diag2.category))}: ${diag2.message}`);
1918
- if (diag2.status === "warning" && diag2.details?.issues) {
1939
+ if (diag2.status === "warning" && Array.isArray(diag2.details?.issues)) {
1919
1940
  for (const issue of diag2.details.issues) {
1920
1941
  diagItems.push(
1921
1942
  ` ${safeColors.warning(`\u2192 ${issue.plugin}: requires ${issue.required}, found ${issue.current}`)}`
@@ -2492,44 +2513,42 @@ var logsDiagnose = defineSystemCommand({
2492
2513
  ctx.ui.error("Log Diagnose", { sections: [{ header: "Error", items: [result.error ?? "Unknown"] }] });
2493
2514
  return;
2494
2515
  }
2495
- const data = result;
2496
- const { summary, topErrors, bySource, period } = data;
2516
+ const { summary, topErrors, bySource, period } = result;
2497
2517
  const summaryItems = [
2498
- `Period: ${period.from} to ${period.to}`,
2499
- `Total logs: ${summary.total}`,
2500
- `Errors: ${summary.errors}`,
2501
- `Warnings: ${summary.warnings}`,
2502
- `Sources: ${summary.sources.join(", ") || "none"}`
2518
+ `Period: ${period?.from ?? "?"} to ${period?.to ?? "?"}`,
2519
+ `Total logs: ${summary?.total ?? 0}`,
2520
+ `Errors: ${summary?.errors ?? 0}`,
2521
+ `Warnings: ${summary?.warnings ?? 0}`,
2522
+ `Sources: ${summary?.sources.join(", ") || "none"}`
2503
2523
  ];
2504
2524
  const sections = [
2505
2525
  { header: "Summary", items: summaryItems }
2506
2526
  ];
2507
- if (topErrors.length > 0) {
2527
+ if ((topErrors?.length ?? 0) > 0) {
2508
2528
  sections.push({
2509
2529
  header: "Top Errors",
2510
- items: topErrors.slice(0, 5).map(
2530
+ items: (topErrors ?? []).slice(0, 5).map(
2511
2531
  (e, i) => `${i + 1}. "${e.message}" (${e.count}x) [${e.source}]`
2512
2532
  )
2513
2533
  });
2514
2534
  }
2515
2535
  const sourceItems = [];
2516
- for (const [src, breakdown] of Object.entries(bySource)) {
2517
- const b = breakdown;
2518
- if (b.errors > 0 || b.warnings > 0) {
2519
- sourceItems.push(`${src}: ${b.errors} errors, ${b.warnings} warnings`);
2536
+ for (const [src, breakdown] of Object.entries(bySource ?? {})) {
2537
+ if (breakdown.errors > 0 || breakdown.warnings > 0) {
2538
+ sourceItems.push(`${src}: ${breakdown.errors} errors, ${breakdown.warnings} warnings`);
2520
2539
  }
2521
2540
  }
2522
2541
  if (sourceItems.length > 0) {
2523
2542
  sections.push({ header: "By Source", items: sourceItems });
2524
2543
  }
2525
- if (summary.errors === 0 && summary.warnings === 0) {
2544
+ if ((summary?.errors ?? 0) === 0 && (summary?.warnings ?? 0) === 0) {
2526
2545
  ctx.ui.success("Log Diagnosis", { sections });
2527
- } else if (summary.errors > 0) {
2546
+ } else if ((summary?.errors ?? 0) > 0) {
2528
2547
  ctx.ui.error("Log Diagnosis", { sections });
2529
2548
  } else {
2530
2549
  ctx.ui.warn("Log Diagnosis", { sections });
2531
2550
  }
2532
- const raw = data._raw;
2551
+ const raw = result._raw ?? [];
2533
2552
  const errorLogs = raw.filter((l) => l.level === "error" || l.level === "fatal").slice(0, 5);
2534
2553
  if (errorLogs.length > 0) {
2535
2554
  ctx.ui.write("\nRecent Errors:\n");
@@ -2610,8 +2629,9 @@ var logsContext = defineSystemCommand({
2610
2629
  };
2611
2630
  },
2612
2631
  formatter(result, ctx, flags) {
2632
+ const data = result;
2613
2633
  if (flags.json) {
2614
- const { _raw, ...jsonResult } = result;
2634
+ const { _raw: _ignored, ...jsonResult } = data;
2615
2635
  ctx.ui.json(jsonResult);
2616
2636
  return;
2617
2637
  }
@@ -2619,19 +2639,18 @@ var logsContext = defineSystemCommand({
2619
2639
  ctx.ui.error("Log Context", { sections: [{ header: "Error", items: [result.error ?? "Unknown"] }] });
2620
2640
  return;
2621
2641
  }
2622
- const data = result;
2623
2642
  const { timeline, correlationKey, byLevel } = data;
2624
- const raw = data._raw;
2625
- const keyStr = Object.entries(correlationKey).map(([k, v]) => `${k}=${v}`).join(", ");
2626
- const durationStr = timeline.durationMs > 0 ? `${timeline.durationMs}ms` : "instant";
2643
+ const raw = data._raw ?? [];
2644
+ const keyStr = correlationKey ? Object.entries(correlationKey).map(([k, v]) => `${k}=${v}`).join(", ") : "";
2645
+ const durationStr = timeline && timeline.durationMs > 0 ? `${timeline.durationMs}ms` : "instant";
2627
2646
  ctx.ui.success("Execution Context", {
2628
2647
  sections: [{
2629
2648
  header: `${keyStr}`,
2630
2649
  items: [
2631
- `Total events: ${timeline.total}`,
2650
+ `Total events: ${timeline?.total ?? 0}`,
2632
2651
  `Duration: ${durationStr}`,
2633
- `Time: ${timeline.from ?? "N/A"} \u2192 ${timeline.to ?? "N/A"}`,
2634
- `Levels: ${Object.entries(byLevel).map(([k, v]) => `${k}=${v}`).join(", ")}`
2652
+ `Time: ${timeline?.from ?? "N/A"} \u2192 ${timeline?.to ?? "N/A"}`,
2653
+ `Levels: ${byLevel ? Object.entries(byLevel).map(([k, v]) => `${k}=${v}`).join(", ") : ""}`
2635
2654
  ]
2636
2655
  }]
2637
2656
  });
@@ -2801,13 +2820,12 @@ var logsSummarize = defineSystemCommand({
2801
2820
  ctx.ui.error("Log Summarize", { sections: [{ header: "Error", items: [result.error ?? "Unknown"] }] });
2802
2821
  return;
2803
2822
  }
2804
- const data = result;
2805
- ctx.ui.write(`Question: ${data.question}
2823
+ ctx.ui.write(`Question: ${result.question}
2806
2824
  `);
2807
- ctx.ui.write(`${data.llmUsed ? "(AI-powered)" : "(Statistical fallback)"}
2825
+ ctx.ui.write(`${result.llmUsed ? "(AI-powered)" : "(Statistical fallback)"}
2808
2826
 
2809
2827
  `);
2810
- ctx.ui.write(data.summary + "\n");
2828
+ ctx.ui.write((result.summary ?? "") + "\n");
2811
2829
  }
2812
2830
  });
2813
2831
  var logsQuery = defineSystemCommand({
@@ -3002,12 +3020,15 @@ var logsGet = defineSystemCommand({
3002
3020
  const rawLog = result._rawLog;
3003
3021
  const logJson = result.log;
3004
3022
  const keys = result.correlationKeys;
3023
+ if (!rawLog || !logJson || !keys) {
3024
+ return;
3025
+ }
3005
3026
  ctx.ui.write(formatLogLine(rawLog) + "\n\n");
3006
3027
  const details = [
3007
- `ID: ${logJson.id}`,
3008
- `Time: ${logJson.time}`,
3009
- `Level: ${logJson.level}`,
3010
- `Source: ${logJson.source}`
3028
+ `ID: ${logJson["id"]}`,
3029
+ `Time: ${logJson["time"]}`,
3030
+ `Level: ${logJson["level"]}`,
3031
+ `Source: ${logJson["source"]}`
3011
3032
  ];
3012
3033
  if (keys.traceId) {
3013
3034
  details.push(`Trace ID: ${keys.traceId}`);
@@ -3022,18 +3043,18 @@ var logsGet = defineSystemCommand({
3022
3043
  details.push(`Session ID: ${keys.sessionId}`);
3023
3044
  }
3024
3045
  const fieldsToShow = { ...rawLog.fields };
3025
- delete fieldsToShow.level;
3026
- delete fieldsToShow.time;
3027
- delete fieldsToShow.traceId;
3028
- delete fieldsToShow.executionId;
3029
- delete fieldsToShow.requestId;
3030
- delete fieldsToShow.reqId;
3031
- delete fieldsToShow.sessionId;
3046
+ delete fieldsToShow["level"];
3047
+ delete fieldsToShow["time"];
3048
+ delete fieldsToShow["traceId"];
3049
+ delete fieldsToShow["executionId"];
3050
+ delete fieldsToShow["requestId"];
3051
+ delete fieldsToShow["reqId"];
3052
+ delete fieldsToShow["sessionId"];
3032
3053
  if (Object.keys(fieldsToShow).length > 0) {
3033
3054
  details.push(`Fields: ${JSON.stringify(fieldsToShow, null, 2)}`);
3034
3055
  }
3035
3056
  ctx.ui.success("Log Detail", { sections: [{ header: "Info", items: details }] });
3036
- const related = result.related;
3057
+ const { related } = result;
3037
3058
  if (related && related.length > 0) {
3038
3059
  ctx.ui.write(`
3039
3060
  Related Logs (${related.length}):
@@ -3606,7 +3627,7 @@ function checkRequires(manifest, options = {}) {
3606
3627
  if (isInMonorepo) {
3607
3628
  continue;
3608
3629
  }
3609
- const errorMessage = err.message || "";
3630
+ const errorMessage = (err instanceof Error ? err.message : "") || "";
3610
3631
  const hasExportsError = errorMessage.includes("exports") || errorMessage.includes("main");
3611
3632
  if (hasExportsError || errorMessage.includes("Cannot resolve")) {
3612
3633
  const parts = dep.startsWith("@") ? dep.slice(1).split("/") : [dep];
@@ -3783,7 +3804,7 @@ function preflightManifests(discoveryResults, logger) {
3783
3804
  validateManifestStructure(manifest);
3784
3805
  allowed.push(manifest);
3785
3806
  } catch (error) {
3786
- const reason = error?.message ? String(error.message) : "Validation failed";
3807
+ const reason = error instanceof Error ? error.message : "Validation failed";
3787
3808
  const manifestId = manifest?.id || manifest?.group || result.packageName || "unknown";
3788
3809
  skipped.push({
3789
3810
  id: manifestId,
@@ -3828,7 +3849,7 @@ async function registerManifests(discoveryResults, registry2, options = {}) {
3828
3849
  try {
3829
3850
  validateManifestStructure(manifest);
3830
3851
  } catch (err) {
3831
- throw new Error(`Validation failed: ${err.message}`);
3852
+ throw new Error(`Validation failed: ${err instanceof Error ? err.message : String(err)}`);
3832
3853
  }
3833
3854
  const normalizedId = normalizeCommandId(manifest.id, namespace);
3834
3855
  if (normalizedId !== manifest.id) {
@@ -3875,7 +3896,8 @@ async function registerManifests(discoveryResults, registry2, options = {}) {
3875
3896
  cmd._disposeHook = manifestModule.dispose;
3876
3897
  }
3877
3898
  } catch (hookError) {
3878
- log3.debug(`Lifecycle hooks unavailable for ${manifest.id}: ${hookError.message}`);
3899
+ const hookMsg = hookError instanceof Error ? hookError.message : String(hookError);
3900
+ log3.debug(`Lifecycle hooks unavailable for ${manifest.id}: ${hookMsg}`);
3879
3901
  }
3880
3902
  const canonicalKey = manifest.group ? `${manifest.group}:${manifest.id}` : manifest.id;
3881
3903
  const existing = globalIds.get(canonicalKey);
@@ -3945,7 +3967,7 @@ async function registerManifests(discoveryResults, registry2, options = {}) {
3945
3967
  registered.push(cmd);
3946
3968
  } catch (error) {
3947
3969
  errors++;
3948
- const reason = error?.message ? String(error.message) : String(error);
3970
+ const reason = error instanceof Error ? error.message : String(error);
3949
3971
  skipped.push({
3950
3972
  id: manifestId,
3951
3973
  source: result.source,
@@ -3977,11 +3999,9 @@ async function disposeAllPlugins(registry2, logger) {
3977
3999
  const disposeHook = cmd._disposeHook;
3978
4000
  if (disposeHook && typeof disposeHook === "function") {
3979
4001
  disposePromises.push(
3980
- Promise.resolve(disposeHook({
3981
- registry: registry2,
3982
- command: cmd
3983
- })).catch((err) => {
3984
- log3.warn(`Dispose hook failed for ${cmd.manifest.id}: ${err.message}`);
4002
+ Promise.resolve(disposeHook()).catch((err) => {
4003
+ const errMsg = err instanceof Error ? err.message : String(err);
4004
+ log3.warn(`Dispose hook failed for ${cmd.manifest.id}: ${errMsg}`);
3985
4005
  })
3986
4006
  );
3987
4007
  }
@@ -4086,7 +4106,7 @@ async function registerBuiltinCommands(input = {}) {
4086
4106
  registry.markPartial(false);
4087
4107
  }
4088
4108
  } catch (err) {
4089
- log3.warn(`Discovery failed: ${err.message}`);
4109
+ log3.warn(`Discovery failed: ${err instanceof Error ? err.message : String(err)}`);
4090
4110
  registry.markPartial(true);
4091
4111
  _registered = false;
4092
4112
  return;