@unbrained/pm-cli 2026.3.12 → 2026.5.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.
Files changed (285) hide show
  1. package/.agents/pm/extensions/.managed-extensions.json +42 -0
  2. package/.agents/pm/extensions/beads/index.js +109 -0
  3. package/.agents/pm/extensions/beads/manifest.json +7 -0
  4. package/{dist/cli/commands/beads.js → .agents/pm/extensions/beads/runtime.js} +31 -21
  5. package/.agents/pm/extensions/beads/runtime.ts +702 -0
  6. package/.agents/pm/extensions/todos/index.js +126 -0
  7. package/.agents/pm/extensions/todos/manifest.json +7 -0
  8. package/{dist/extensions/builtins/todos/import-export.js → .agents/pm/extensions/todos/runtime.js} +39 -29
  9. package/.agents/pm/extensions/todos/runtime.ts +568 -0
  10. package/AGENTS.md +196 -92
  11. package/CHANGELOG.md +399 -0
  12. package/CODE_OF_CONDUCT.md +42 -0
  13. package/CONTRIBUTING.md +144 -0
  14. package/PRD.md +512 -164
  15. package/README.md +1053 -2
  16. package/SECURITY.md +51 -0
  17. package/dist/cli/commands/activity.d.ts +5 -0
  18. package/dist/cli/commands/activity.js +66 -3
  19. package/dist/cli/commands/activity.js.map +1 -1
  20. package/dist/cli/commands/aggregate.d.ts +54 -0
  21. package/dist/cli/commands/aggregate.js +181 -0
  22. package/dist/cli/commands/aggregate.js.map +1 -0
  23. package/dist/cli/commands/append.js +4 -1
  24. package/dist/cli/commands/append.js.map +1 -1
  25. package/dist/cli/commands/calendar.d.ts +109 -0
  26. package/dist/cli/commands/calendar.js +797 -0
  27. package/dist/cli/commands/calendar.js.map +1 -0
  28. package/dist/cli/commands/claim.d.ts +5 -1
  29. package/dist/cli/commands/claim.js +42 -21
  30. package/dist/cli/commands/claim.js.map +1 -1
  31. package/dist/cli/commands/close.d.ts +1 -0
  32. package/dist/cli/commands/close.js +54 -5
  33. package/dist/cli/commands/close.js.map +1 -1
  34. package/dist/cli/commands/comments-audit.d.ts +91 -0
  35. package/dist/cli/commands/comments-audit.js +195 -0
  36. package/dist/cli/commands/comments-audit.js.map +1 -0
  37. package/dist/cli/commands/comments.d.ts +1 -0
  38. package/dist/cli/commands/comments.js +70 -21
  39. package/dist/cli/commands/comments.js.map +1 -1
  40. package/dist/cli/commands/completion.d.ts +10 -4
  41. package/dist/cli/commands/completion.js +1184 -137
  42. package/dist/cli/commands/completion.js.map +1 -1
  43. package/dist/cli/commands/config.d.ts +35 -3
  44. package/dist/cli/commands/config.js +968 -13
  45. package/dist/cli/commands/config.js.map +1 -1
  46. package/dist/cli/commands/context.d.ts +86 -0
  47. package/dist/cli/commands/context.js +299 -0
  48. package/dist/cli/commands/context.js.map +1 -0
  49. package/dist/cli/commands/contracts.d.ts +78 -0
  50. package/dist/cli/commands/contracts.js +920 -0
  51. package/dist/cli/commands/contracts.js.map +1 -0
  52. package/dist/cli/commands/create.d.ts +48 -14
  53. package/dist/cli/commands/create.js +1331 -160
  54. package/dist/cli/commands/create.js.map +1 -1
  55. package/dist/cli/commands/dedupe-audit.d.ts +81 -0
  56. package/dist/cli/commands/dedupe-audit.js +330 -0
  57. package/dist/cli/commands/dedupe-audit.js.map +1 -0
  58. package/dist/cli/commands/deps.d.ts +52 -0
  59. package/dist/cli/commands/deps.js +204 -0
  60. package/dist/cli/commands/deps.js.map +1 -0
  61. package/dist/cli/commands/docs.d.ts +19 -0
  62. package/dist/cli/commands/docs.js +212 -13
  63. package/dist/cli/commands/docs.js.map +1 -1
  64. package/dist/cli/commands/extension.d.ts +122 -0
  65. package/dist/cli/commands/extension.js +1850 -0
  66. package/dist/cli/commands/extension.js.map +1 -0
  67. package/dist/cli/commands/files.d.ts +52 -1
  68. package/dist/cli/commands/files.js +443 -13
  69. package/dist/cli/commands/files.js.map +1 -1
  70. package/dist/cli/commands/gc.d.ts +11 -1
  71. package/dist/cli/commands/gc.js +89 -11
  72. package/dist/cli/commands/gc.js.map +1 -1
  73. package/dist/cli/commands/get.d.ts +13 -0
  74. package/dist/cli/commands/get.js +35 -3
  75. package/dist/cli/commands/get.js.map +1 -1
  76. package/dist/cli/commands/health.d.ts +10 -2
  77. package/dist/cli/commands/health.js +774 -23
  78. package/dist/cli/commands/health.js.map +1 -1
  79. package/dist/cli/commands/history.d.ts +20 -0
  80. package/dist/cli/commands/history.js +152 -6
  81. package/dist/cli/commands/history.js.map +1 -1
  82. package/dist/cli/commands/index.d.ts +16 -3
  83. package/dist/cli/commands/index.js +16 -3
  84. package/dist/cli/commands/index.js.map +1 -1
  85. package/dist/cli/commands/init.d.ts +7 -2
  86. package/dist/cli/commands/init.js +137 -5
  87. package/dist/cli/commands/init.js.map +1 -1
  88. package/dist/cli/commands/learnings.d.ts +17 -0
  89. package/dist/cli/commands/learnings.js +129 -0
  90. package/dist/cli/commands/learnings.js.map +1 -0
  91. package/dist/cli/commands/list.d.ts +29 -1
  92. package/dist/cli/commands/list.js +289 -53
  93. package/dist/cli/commands/list.js.map +1 -1
  94. package/dist/cli/commands/normalize.d.ts +51 -0
  95. package/dist/cli/commands/normalize.js +298 -0
  96. package/dist/cli/commands/normalize.js.map +1 -0
  97. package/dist/cli/commands/notes.d.ts +17 -0
  98. package/dist/cli/commands/notes.js +129 -0
  99. package/dist/cli/commands/notes.js.map +1 -0
  100. package/dist/cli/commands/reindex.d.ts +1 -0
  101. package/dist/cli/commands/reindex.js +208 -32
  102. package/dist/cli/commands/reindex.js.map +1 -1
  103. package/dist/cli/commands/restore.js +164 -30
  104. package/dist/cli/commands/restore.js.map +1 -1
  105. package/dist/cli/commands/search.d.ts +14 -1
  106. package/dist/cli/commands/search.js +475 -81
  107. package/dist/cli/commands/search.js.map +1 -1
  108. package/dist/cli/commands/stats.js +26 -10
  109. package/dist/cli/commands/stats.js.map +1 -1
  110. package/dist/cli/commands/templates.d.ts +26 -0
  111. package/dist/cli/commands/templates.js +179 -0
  112. package/dist/cli/commands/templates.js.map +1 -0
  113. package/dist/cli/commands/test-all.d.ts +19 -1
  114. package/dist/cli/commands/test-all.js +161 -13
  115. package/dist/cli/commands/test-all.js.map +1 -1
  116. package/dist/cli/commands/test-runs.d.ts +63 -0
  117. package/dist/cli/commands/test-runs.js +179 -0
  118. package/dist/cli/commands/test-runs.js.map +1 -0
  119. package/dist/cli/commands/test.d.ts +75 -1
  120. package/dist/cli/commands/test.js +1360 -41
  121. package/dist/cli/commands/test.js.map +1 -1
  122. package/dist/cli/commands/update-many.d.ts +57 -0
  123. package/dist/cli/commands/update-many.js +631 -0
  124. package/dist/cli/commands/update-many.js.map +1 -0
  125. package/dist/cli/commands/update.d.ts +30 -0
  126. package/dist/cli/commands/update.js +1393 -84
  127. package/dist/cli/commands/update.js.map +1 -1
  128. package/dist/cli/commands/validate.d.ts +30 -0
  129. package/dist/cli/commands/validate.js +1140 -0
  130. package/dist/cli/commands/validate.js.map +1 -0
  131. package/dist/cli/error-guidance.d.ts +33 -0
  132. package/dist/cli/error-guidance.js +337 -0
  133. package/dist/cli/error-guidance.js.map +1 -0
  134. package/dist/cli/extension-command-options.d.ts +1 -0
  135. package/dist/cli/extension-command-options.js +92 -0
  136. package/dist/cli/extension-command-options.js.map +1 -1
  137. package/dist/cli/help-content.d.ts +20 -0
  138. package/dist/cli/help-content.js +543 -0
  139. package/dist/cli/help-content.js.map +1 -0
  140. package/dist/cli/main.js +3625 -445
  141. package/dist/cli/main.js.map +1 -1
  142. package/dist/core/extensions/index.d.ts +13 -1
  143. package/dist/core/extensions/index.js +108 -1
  144. package/dist/core/extensions/index.js.map +1 -1
  145. package/dist/core/extensions/item-fields.d.ts +2 -0
  146. package/dist/core/extensions/item-fields.js +79 -0
  147. package/dist/core/extensions/item-fields.js.map +1 -0
  148. package/dist/core/extensions/loader.d.ts +322 -9
  149. package/dist/core/extensions/loader.js +911 -20
  150. package/dist/core/extensions/loader.js.map +1 -1
  151. package/dist/core/extensions/runtime-registrations.d.ts +5 -0
  152. package/dist/core/extensions/runtime-registrations.js +51 -0
  153. package/dist/core/extensions/runtime-registrations.js.map +1 -0
  154. package/dist/core/history/history-stream-policy.d.ts +20 -0
  155. package/dist/core/history/history-stream-policy.js +53 -0
  156. package/dist/core/history/history-stream-policy.js.map +1 -0
  157. package/dist/core/history/history.js +90 -1
  158. package/dist/core/history/history.js.map +1 -1
  159. package/dist/core/item/id.js +4 -1
  160. package/dist/core/item/id.js.map +1 -1
  161. package/dist/core/item/index.d.ts +1 -0
  162. package/dist/core/item/index.js +1 -0
  163. package/dist/core/item/index.js.map +1 -1
  164. package/dist/core/item/item-format.d.ts +11 -5
  165. package/dist/core/item/item-format.js +507 -24
  166. package/dist/core/item/item-format.js.map +1 -1
  167. package/dist/core/item/parent-reference-policy.d.ts +6 -0
  168. package/dist/core/item/parent-reference-policy.js +32 -0
  169. package/dist/core/item/parent-reference-policy.js.map +1 -0
  170. package/dist/core/item/parse.d.ts +5 -0
  171. package/dist/core/item/parse.js +216 -19
  172. package/dist/core/item/parse.js.map +1 -1
  173. package/dist/core/item/sprint-release-format.d.ts +6 -0
  174. package/dist/core/item/sprint-release-format.js +33 -0
  175. package/dist/core/item/sprint-release-format.js.map +1 -0
  176. package/dist/core/item/status.d.ts +3 -0
  177. package/dist/core/item/status.js +24 -0
  178. package/dist/core/item/status.js.map +1 -0
  179. package/dist/core/item/type-registry.d.ts +37 -0
  180. package/dist/core/item/type-registry.js +706 -0
  181. package/dist/core/item/type-registry.js.map +1 -0
  182. package/dist/core/lock/lock.d.ts +1 -1
  183. package/dist/core/lock/lock.js +101 -12
  184. package/dist/core/lock/lock.js.map +1 -1
  185. package/dist/core/output/command-aware.d.ts +1 -0
  186. package/dist/core/output/command-aware.js +394 -0
  187. package/dist/core/output/command-aware.js.map +1 -0
  188. package/dist/core/output/output.d.ts +3 -0
  189. package/dist/core/output/output.js +124 -6
  190. package/dist/core/output/output.js.map +1 -1
  191. package/dist/core/schema/runtime-field-filters.d.ts +3 -0
  192. package/dist/core/schema/runtime-field-filters.js +39 -0
  193. package/dist/core/schema/runtime-field-filters.js.map +1 -0
  194. package/dist/core/schema/runtime-field-values.d.ts +8 -0
  195. package/dist/core/schema/runtime-field-values.js +154 -0
  196. package/dist/core/schema/runtime-field-values.js.map +1 -0
  197. package/dist/core/schema/runtime-schema.d.ts +68 -0
  198. package/dist/core/schema/runtime-schema.js +554 -0
  199. package/dist/core/schema/runtime-schema.js.map +1 -0
  200. package/dist/core/search/cache.d.ts +13 -1
  201. package/dist/core/search/cache.js +123 -14
  202. package/dist/core/search/cache.js.map +1 -1
  203. package/dist/core/search/semantic-defaults.d.ts +6 -0
  204. package/dist/core/search/semantic-defaults.js +120 -0
  205. package/dist/core/search/semantic-defaults.js.map +1 -0
  206. package/dist/core/search/vector-stores.js +3 -1
  207. package/dist/core/search/vector-stores.js.map +1 -1
  208. package/dist/core/shared/command-types.d.ts +2 -0
  209. package/dist/core/shared/conflict-markers.d.ts +7 -0
  210. package/dist/core/shared/conflict-markers.js +27 -0
  211. package/dist/core/shared/conflict-markers.js.map +1 -0
  212. package/dist/core/shared/constants.d.ts +15 -4
  213. package/dist/core/shared/constants.js +141 -1
  214. package/dist/core/shared/constants.js.map +1 -1
  215. package/dist/core/shared/errors.d.ts +10 -1
  216. package/dist/core/shared/errors.js +3 -1
  217. package/dist/core/shared/errors.js.map +1 -1
  218. package/dist/core/shared/text-normalization.d.ts +4 -0
  219. package/dist/core/shared/text-normalization.js +33 -0
  220. package/dist/core/shared/text-normalization.js.map +1 -0
  221. package/dist/core/shared/time.d.ts +1 -2
  222. package/dist/core/shared/time.js +98 -11
  223. package/dist/core/shared/time.js.map +1 -1
  224. package/dist/core/store/index.d.ts +1 -0
  225. package/dist/core/store/index.js +1 -0
  226. package/dist/core/store/index.js.map +1 -1
  227. package/dist/core/store/item-format-migration.d.ts +9 -0
  228. package/dist/core/store/item-format-migration.js +87 -0
  229. package/dist/core/store/item-format-migration.js.map +1 -0
  230. package/dist/core/store/item-store.d.ts +13 -4
  231. package/dist/core/store/item-store.js +238 -51
  232. package/dist/core/store/item-store.js.map +1 -1
  233. package/dist/core/store/paths.d.ts +21 -3
  234. package/dist/core/store/paths.js +59 -4
  235. package/dist/core/store/paths.js.map +1 -1
  236. package/dist/core/store/settings.d.ts +14 -1
  237. package/dist/core/store/settings.js +463 -7
  238. package/dist/core/store/settings.js.map +1 -1
  239. package/dist/core/telemetry/consent.d.ts +2 -0
  240. package/dist/core/telemetry/consent.js +79 -0
  241. package/dist/core/telemetry/consent.js.map +1 -0
  242. package/dist/core/telemetry/runtime.d.ts +38 -0
  243. package/dist/core/telemetry/runtime.js +733 -0
  244. package/dist/core/telemetry/runtime.js.map +1 -0
  245. package/dist/core/test/background-runs.d.ts +117 -0
  246. package/dist/core/test/background-runs.js +760 -0
  247. package/dist/core/test/background-runs.js.map +1 -0
  248. package/dist/core/test/item-test-run-tracking.d.ts +9 -0
  249. package/dist/core/test/item-test-run-tracking.js +50 -0
  250. package/dist/core/test/item-test-run-tracking.js.map +1 -0
  251. package/dist/sdk/cli-contracts.d.ts +92 -0
  252. package/dist/sdk/cli-contracts.js +2357 -0
  253. package/dist/sdk/cli-contracts.js.map +1 -0
  254. package/dist/sdk/index.d.ts +34 -0
  255. package/dist/sdk/index.js +23 -0
  256. package/dist/sdk/index.js.map +1 -0
  257. package/dist/types.d.ts +197 -3
  258. package/dist/types.js +48 -1
  259. package/dist/types.js.map +1 -1
  260. package/docs/ARCHITECTURE.md +368 -39
  261. package/docs/EXTENSIONS.md +454 -49
  262. package/docs/RELEASING.md +68 -19
  263. package/docs/SDK.md +123 -0
  264. package/docs/examples/starter-extension/README.md +48 -0
  265. package/docs/examples/starter-extension/index.js +191 -0
  266. package/docs/examples/starter-extension/manifest.json +17 -0
  267. package/docs/examples/starter-extension/package.json +10 -0
  268. package/package.json +33 -6
  269. package/.pi/extensions/pm-cli/index.ts +0 -778
  270. package/dist/cli/commands/beads.d.ts +0 -16
  271. package/dist/cli/commands/beads.js.map +0 -1
  272. package/dist/cli/commands/install.d.ts +0 -18
  273. package/dist/cli/commands/install.js +0 -87
  274. package/dist/cli/commands/install.js.map +0 -1
  275. package/dist/core/extensions/builtins.d.ts +0 -3
  276. package/dist/core/extensions/builtins.js +0 -47
  277. package/dist/core/extensions/builtins.js.map +0 -1
  278. package/dist/extensions/builtins/beads/index.d.ts +0 -8
  279. package/dist/extensions/builtins/beads/index.js +0 -33
  280. package/dist/extensions/builtins/beads/index.js.map +0 -1
  281. package/dist/extensions/builtins/todos/import-export.d.ts +0 -26
  282. package/dist/extensions/builtins/todos/import-export.js.map +0 -1
  283. package/dist/extensions/builtins/todos/index.d.ts +0 -8
  284. package/dist/extensions/builtins/todos/index.js +0 -38
  285. package/dist/extensions/builtins/todos/index.js.map +0 -1
@@ -0,0 +1,1850 @@
1
+ import { execFile } from "node:child_process";
2
+ import fs from "node:fs/promises";
3
+ import os from "node:os";
4
+ import path from "node:path";
5
+ import { fileURLToPath } from "node:url";
6
+ import { promisify } from "node:util";
7
+ import { activateExtensions, loadExtensions } from "../../core/extensions/index.js";
8
+ import { EXTENSION_CAPABILITY_CONTRACT, KNOWN_EXTENSION_CAPABILITIES, parseLegacyExtensionCapabilityAliasWarning, parseUnknownExtensionCapabilityWarning, resolveExtensionRoots, } from "../../core/extensions/loader.js";
9
+ import { pathExists } from "../../core/fs/fs-utils.js";
10
+ import { EXIT_CODE } from "../../core/shared/constants.js";
11
+ import { PmCliError } from "../../core/shared/errors.js";
12
+ import { nowIso } from "../../core/shared/time.js";
13
+ import { resolveGlobalPmRoot, resolvePmRoot } from "../../core/store/paths.js";
14
+ import { readSettings, writeSettings } from "../../core/store/settings.js";
15
+ const execFileAsync = promisify(execFile);
16
+ const DEFAULT_EXTENSION_PRIORITY = 100;
17
+ const MANAGED_EXTENSION_STATE_FILENAME = ".managed-extensions.json";
18
+ const MANAGED_EXTENSION_STATE_VERSION = 1;
19
+ const PM_PACKAGE_ROOT_ENV = "PM_CLI_PACKAGE_ROOT";
20
+ const BUNDLED_EXTENSION_ALIASES = {
21
+ beads: "beads",
22
+ todos: "todos",
23
+ };
24
+ function resolvePackageRootCandidates() {
25
+ const candidates = [];
26
+ const envRoot = process.env[PM_PACKAGE_ROOT_ENV];
27
+ if (typeof envRoot === "string" && envRoot.trim().length > 0) {
28
+ candidates.push(path.resolve(envRoot.trim()));
29
+ }
30
+ const moduleRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../../..");
31
+ candidates.push(moduleRoot);
32
+ return [...new Set(candidates)];
33
+ }
34
+ async function resolveBundledExtensionAliasSource(input) {
35
+ const normalized = input.trim().toLowerCase();
36
+ const alias = BUNDLED_EXTENSION_ALIASES[normalized];
37
+ if (!alias) {
38
+ return null;
39
+ }
40
+ for (const packageRoot of resolvePackageRootCandidates()) {
41
+ const bundledPath = path.join(packageRoot, ".agents", "pm", "extensions", alias);
42
+ if (await pathExists(path.join(bundledPath, "manifest.json"))) {
43
+ return bundledPath;
44
+ }
45
+ }
46
+ return null;
47
+ }
48
+ function normalizeStringList(values) {
49
+ return [...new Set(values.map((value) => value.trim()).filter((value) => value.length > 0))].sort((left, right) => left.localeCompare(right));
50
+ }
51
+ function normalizeManagedDirectoryName(name) {
52
+ const normalized = name
53
+ .trim()
54
+ .toLowerCase()
55
+ .replace(/[^a-z0-9._-]+/g, "-")
56
+ .replace(/^-+|-+$/g, "");
57
+ if (normalized.length === 0) {
58
+ throw new PmCliError("Extension manifest name must resolve to a non-empty directory name.", EXIT_CODE.USAGE);
59
+ }
60
+ return normalized;
61
+ }
62
+ function parseExtensionManifest(raw) {
63
+ if (typeof raw !== "object" || raw === null) {
64
+ return null;
65
+ }
66
+ const candidate = raw;
67
+ if (typeof candidate.name !== "string" || candidate.name.trim().length === 0) {
68
+ return null;
69
+ }
70
+ if (typeof candidate.version !== "string" || candidate.version.trim().length === 0) {
71
+ return null;
72
+ }
73
+ if (typeof candidate.entry !== "string" || candidate.entry.trim().length === 0) {
74
+ return null;
75
+ }
76
+ let priority = DEFAULT_EXTENSION_PRIORITY;
77
+ if (candidate.priority !== undefined && candidate.priority !== null) {
78
+ if (typeof candidate.priority !== "number" || !Number.isInteger(candidate.priority)) {
79
+ return null;
80
+ }
81
+ priority = candidate.priority;
82
+ }
83
+ let capabilities = [];
84
+ if (candidate.capabilities !== undefined && candidate.capabilities !== null) {
85
+ if (!Array.isArray(candidate.capabilities) || candidate.capabilities.some((value) => typeof value !== "string")) {
86
+ return null;
87
+ }
88
+ capabilities = normalizeStringList(candidate.capabilities.map((value) => String(value).toLowerCase()));
89
+ }
90
+ return {
91
+ name: candidate.name.trim(),
92
+ version: candidate.version.trim(),
93
+ entry: candidate.entry.trim(),
94
+ priority,
95
+ capabilities,
96
+ };
97
+ }
98
+ function isPathWithinDirectory(directory, targetPath) {
99
+ const relative = path.relative(directory, targetPath);
100
+ if (relative.length === 0) {
101
+ return true;
102
+ }
103
+ return !relative.startsWith("..") && !path.isAbsolute(relative);
104
+ }
105
+ async function isCanonicalPathWithinDirectory(directory, targetPath) {
106
+ const [resolvedDirectory, resolvedTargetPath] = await Promise.all([fs.realpath(directory), fs.realpath(targetPath)]);
107
+ return isPathWithinDirectory(resolvedDirectory, resolvedTargetPath);
108
+ }
109
+ async function validateExtensionDirectory(directory) {
110
+ const manifestPath = path.join(directory, "manifest.json");
111
+ if (!(await pathExists(manifestPath))) {
112
+ throw new PmCliError(`Extension manifest is missing at "${manifestPath}".`, EXIT_CODE.USAGE);
113
+ }
114
+ let parsedManifest;
115
+ try {
116
+ parsedManifest = JSON.parse(await fs.readFile(manifestPath, "utf8"));
117
+ }
118
+ catch (error) {
119
+ throw new PmCliError(`Failed to parse extension manifest at "${manifestPath}": ${error instanceof Error ? error.message : String(error)}`, EXIT_CODE.USAGE);
120
+ }
121
+ const manifest = parseExtensionManifest(parsedManifest);
122
+ if (!manifest) {
123
+ throw new PmCliError(`Extension manifest at "${manifestPath}" is invalid.`, EXIT_CODE.USAGE);
124
+ }
125
+ const entryPath = path.resolve(directory, manifest.entry);
126
+ if (!isPathWithinDirectory(directory, entryPath)) {
127
+ throw new PmCliError(`Extension entry "${manifest.entry}" resolves outside extension directory "${directory}".`, EXIT_CODE.USAGE);
128
+ }
129
+ if (!(await pathExists(entryPath))) {
130
+ throw new PmCliError(`Extension entry file is missing at "${entryPath}".`, EXIT_CODE.USAGE);
131
+ }
132
+ if (!(await isCanonicalPathWithinDirectory(directory, entryPath))) {
133
+ throw new PmCliError(`Extension entry "${manifest.entry}" resolves outside extension directory after symlink resolution.`, EXIT_CODE.USAGE);
134
+ }
135
+ return {
136
+ directory,
137
+ manifest_path: manifestPath,
138
+ entry_path: entryPath,
139
+ manifest,
140
+ };
141
+ }
142
+ export function resolveManagedExtensionStatePath(extensionsRoot) {
143
+ return path.join(extensionsRoot, MANAGED_EXTENSION_STATE_FILENAME);
144
+ }
145
+ function createEmptyManagedExtensionState() {
146
+ return {
147
+ version: MANAGED_EXTENSION_STATE_VERSION,
148
+ updated_at: nowIso(),
149
+ entries: [],
150
+ };
151
+ }
152
+ function sortManagedEntries(entries) {
153
+ return [...entries].sort((left, right) => {
154
+ const byScope = left.scope.localeCompare(right.scope);
155
+ if (byScope !== 0) {
156
+ return byScope;
157
+ }
158
+ const byName = left.name.localeCompare(right.name);
159
+ if (byName !== 0) {
160
+ return byName;
161
+ }
162
+ return left.directory.localeCompare(right.directory);
163
+ });
164
+ }
165
+ function normalizeManagedState(raw) {
166
+ if (typeof raw !== "object" || raw === null) {
167
+ return null;
168
+ }
169
+ const candidate = raw;
170
+ if (candidate.version !== MANAGED_EXTENSION_STATE_VERSION || !Array.isArray(candidate.entries)) {
171
+ return null;
172
+ }
173
+ const entries = [];
174
+ for (const rawEntry of candidate.entries) {
175
+ if (typeof rawEntry !== "object" || rawEntry === null) {
176
+ continue;
177
+ }
178
+ const entry = rawEntry;
179
+ if (typeof entry.name !== "string" ||
180
+ entry.name.trim().length === 0 ||
181
+ typeof entry.directory !== "string" ||
182
+ entry.directory.trim().length === 0 ||
183
+ (entry.scope !== "project" && entry.scope !== "global") ||
184
+ typeof entry.manifest_version !== "string" ||
185
+ typeof entry.manifest_entry !== "string" ||
186
+ !Array.isArray(entry.capabilities) ||
187
+ entry.capabilities.some((value) => typeof value !== "string") ||
188
+ typeof entry.installed_at !== "string" ||
189
+ typeof entry.updated_at !== "string" ||
190
+ typeof entry.source !== "object" ||
191
+ entry.source === null) {
192
+ continue;
193
+ }
194
+ const source = entry.source;
195
+ if ((source.kind !== "local" && source.kind !== "github") ||
196
+ typeof source.input !== "string" ||
197
+ typeof source.location !== "string") {
198
+ continue;
199
+ }
200
+ entries.push({
201
+ name: entry.name.trim(),
202
+ directory: entry.directory.trim(),
203
+ scope: entry.scope,
204
+ manifest_version: entry.manifest_version,
205
+ manifest_entry: entry.manifest_entry,
206
+ capabilities: normalizeStringList(entry.capabilities),
207
+ installed_at: entry.installed_at,
208
+ updated_at: entry.updated_at,
209
+ source: {
210
+ kind: source.kind,
211
+ input: source.input,
212
+ location: source.location,
213
+ repository: typeof source.repository === "string" ? source.repository : undefined,
214
+ owner: typeof source.owner === "string" ? source.owner : undefined,
215
+ repo: typeof source.repo === "string" ? source.repo : undefined,
216
+ ref: typeof source.ref === "string" ? source.ref : undefined,
217
+ subpath: typeof source.subpath === "string" ? source.subpath : undefined,
218
+ commit: typeof source.commit === "string" ? source.commit : undefined,
219
+ },
220
+ last_update_check_at: typeof entry.last_update_check_at === "string" ? entry.last_update_check_at : undefined,
221
+ last_update_remote_commit: typeof entry.last_update_remote_commit === "string" ? entry.last_update_remote_commit : undefined,
222
+ update_available: typeof entry.update_available === "boolean" || entry.update_available === null
223
+ ? entry.update_available
224
+ : undefined,
225
+ update_error: typeof entry.update_error === "string" ? entry.update_error : undefined,
226
+ });
227
+ }
228
+ return {
229
+ version: MANAGED_EXTENSION_STATE_VERSION,
230
+ updated_at: typeof candidate.updated_at === "string" ? candidate.updated_at : nowIso(),
231
+ entries: sortManagedEntries(entries),
232
+ };
233
+ }
234
+ export async function readManagedExtensionState(extensionsRoot) {
235
+ const statePath = resolveManagedExtensionStatePath(extensionsRoot);
236
+ const fallback = createEmptyManagedExtensionState();
237
+ try {
238
+ const raw = await fs.readFile(statePath, "utf8");
239
+ const parsed = JSON.parse(raw);
240
+ const normalized = normalizeManagedState(parsed);
241
+ if (!normalized) {
242
+ return {
243
+ path: statePath,
244
+ state: fallback,
245
+ warnings: [`extension_manager_state_invalid_schema:${statePath}`],
246
+ };
247
+ }
248
+ return {
249
+ path: statePath,
250
+ state: normalized,
251
+ warnings: [],
252
+ };
253
+ }
254
+ catch (error) {
255
+ if (typeof error === "object" && error !== null && "code" in error && error.code === "ENOENT") {
256
+ return {
257
+ path: statePath,
258
+ state: fallback,
259
+ warnings: [],
260
+ };
261
+ }
262
+ return {
263
+ path: statePath,
264
+ state: fallback,
265
+ warnings: [`extension_manager_state_read_failed:${statePath}`],
266
+ };
267
+ }
268
+ }
269
+ export async function writeManagedExtensionState(extensionsRoot, state) {
270
+ const statePath = resolveManagedExtensionStatePath(extensionsRoot);
271
+ const normalized = {
272
+ version: MANAGED_EXTENSION_STATE_VERSION,
273
+ updated_at: nowIso(),
274
+ entries: sortManagedEntries(state.entries),
275
+ };
276
+ await fs.mkdir(extensionsRoot, { recursive: true });
277
+ await fs.writeFile(statePath, `${JSON.stringify(normalized, null, 2)}\n`, "utf8");
278
+ }
279
+ function normalizeExtensionNameForMatch(value) {
280
+ return value.trim().toLowerCase();
281
+ }
282
+ async function resolveBundledAliasManifestName(input) {
283
+ const bundledAliasSource = await resolveBundledExtensionAliasSource(input);
284
+ if (!bundledAliasSource) {
285
+ return null;
286
+ }
287
+ try {
288
+ const validated = await validateExtensionDirectory(bundledAliasSource);
289
+ return validated.manifest.name;
290
+ }
291
+ catch {
292
+ return null;
293
+ }
294
+ }
295
+ async function resolveInstalledExtensionCandidate(installed, extensionTarget) {
296
+ const lookupValues = [extensionTarget];
297
+ const bundledAliasManifestName = await resolveBundledAliasManifestName(extensionTarget);
298
+ if (bundledAliasManifestName) {
299
+ lookupValues.push(bundledAliasManifestName);
300
+ }
301
+ const normalizedLookups = [...new Set(lookupValues.map((value) => normalizeExtensionNameForMatch(value)).filter((value) => value.length > 0))];
302
+ for (const lookup of normalizedLookups) {
303
+ const byName = installed.find((entry) => normalizeExtensionNameForMatch(entry.name) === lookup);
304
+ if (byName) {
305
+ return byName;
306
+ }
307
+ const byDirectory = installed.find((entry) => normalizeExtensionNameForMatch(entry.directory) === lookup);
308
+ if (byDirectory) {
309
+ return byDirectory;
310
+ }
311
+ }
312
+ return undefined;
313
+ }
314
+ function isExtensionEnabled(settings, name) {
315
+ const normalizedName = name.trim();
316
+ const enabled = new Set(normalizeStringList(settings.extensions.enabled));
317
+ const disabled = new Set(normalizeStringList(settings.extensions.disabled));
318
+ if (disabled.has(normalizedName)) {
319
+ return false;
320
+ }
321
+ if (enabled.size === 0) {
322
+ return true;
323
+ }
324
+ return enabled.has(normalizedName);
325
+ }
326
+ function ensureActivated(settings, name) {
327
+ const normalizedName = name.trim();
328
+ const enabled = new Set(normalizeStringList(settings.extensions.enabled));
329
+ const disabled = new Set(normalizeStringList(settings.extensions.disabled));
330
+ const previousEnabled = [...enabled];
331
+ const previousDisabled = [...disabled];
332
+ disabled.delete(normalizedName);
333
+ if (enabled.size > 0) {
334
+ enabled.add(normalizedName);
335
+ }
336
+ settings.extensions.enabled = [...enabled].sort((left, right) => left.localeCompare(right));
337
+ settings.extensions.disabled = [...disabled].sort((left, right) => left.localeCompare(right));
338
+ return (settings.extensions.enabled.join("\u0000") !== previousEnabled.join("\u0000") ||
339
+ settings.extensions.disabled.join("\u0000") !== previousDisabled.join("\u0000"));
340
+ }
341
+ function ensureDeactivated(settings, name) {
342
+ const normalizedName = name.trim();
343
+ const enabled = new Set(normalizeStringList(settings.extensions.enabled));
344
+ const disabled = new Set(normalizeStringList(settings.extensions.disabled));
345
+ const previousEnabled = [...enabled];
346
+ const previousDisabled = [...disabled];
347
+ enabled.delete(normalizedName);
348
+ disabled.add(normalizedName);
349
+ settings.extensions.enabled = [...enabled].sort((left, right) => left.localeCompare(right));
350
+ settings.extensions.disabled = [...disabled].sort((left, right) => left.localeCompare(right));
351
+ return (settings.extensions.enabled.join("\u0000") !== previousEnabled.join("\u0000") ||
352
+ settings.extensions.disabled.join("\u0000") !== previousDisabled.join("\u0000"));
353
+ }
354
+ function clearExtensionState(settings, name) {
355
+ const normalizedName = name.trim();
356
+ const enabled = new Set(normalizeStringList(settings.extensions.enabled));
357
+ const disabled = new Set(normalizeStringList(settings.extensions.disabled));
358
+ const previousEnabled = [...enabled];
359
+ const previousDisabled = [...disabled];
360
+ enabled.delete(normalizedName);
361
+ disabled.delete(normalizedName);
362
+ settings.extensions.enabled = [...enabled].sort((left, right) => left.localeCompare(right));
363
+ settings.extensions.disabled = [...disabled].sort((left, right) => left.localeCompare(right));
364
+ return (settings.extensions.enabled.join("\u0000") !== previousEnabled.join("\u0000") ||
365
+ settings.extensions.disabled.join("\u0000") !== previousDisabled.join("\u0000"));
366
+ }
367
+ function resolveAction(target, options) {
368
+ const selected = [...new Set([
369
+ options.install ? "install" : null,
370
+ options.uninstall ? "uninstall" : null,
371
+ options.explore ? "explore" : null,
372
+ options.manage ? "manage" : null,
373
+ options.doctor ? "doctor" : null,
374
+ options.init ? "init" : null,
375
+ options.scaffold ? "init" : null,
376
+ options.adopt ? "adopt" : null,
377
+ options.adoptAll ? "adopt-all" : null,
378
+ options.activate ? "activate" : null,
379
+ options.deactivate ? "deactivate" : null,
380
+ ].filter((value) => value !== null))];
381
+ if (selected.length === 0) {
382
+ if (typeof target === "string" && target.trim().toLowerCase() === "doctor") {
383
+ return "doctor";
384
+ }
385
+ if (typeof target === "string" && (target.trim().toLowerCase() === "init" || target.trim().toLowerCase() === "scaffold")) {
386
+ return "init";
387
+ }
388
+ throw new PmCliError("One action flag is required. Use one of: --install, --uninstall, --explore, --manage, --doctor, --init/--scaffold, --adopt, --adopt-all, --activate, --deactivate.", EXIT_CODE.USAGE);
389
+ }
390
+ if (selected.length > 1) {
391
+ throw new PmCliError("Extension action flags are mutually exclusive.", EXIT_CODE.USAGE);
392
+ }
393
+ return selected[0];
394
+ }
395
+ function resolveScope(options) {
396
+ const projectLike = options.project === true || options.local === true;
397
+ const global = options.global === true;
398
+ if (projectLike && global) {
399
+ throw new PmCliError('Options "--project/--local" and "--global" are mutually exclusive.', EXIT_CODE.USAGE);
400
+ }
401
+ return global ? "global" : "project";
402
+ }
403
+ function parseGithubPathSpec(pathSpec, input, refOverride) {
404
+ const segments = pathSpec
405
+ .split("/")
406
+ .map((segment) => segment.trim())
407
+ .filter((segment) => segment.length > 0);
408
+ if (segments.length < 2) {
409
+ return null;
410
+ }
411
+ const owner = segments[0];
412
+ const repo = segments[1].replace(/\.git$/i, "");
413
+ if (owner.length === 0 || repo.length === 0) {
414
+ return null;
415
+ }
416
+ const tail = segments.slice(2);
417
+ let ref;
418
+ let subpath;
419
+ if (tail[0] === "tree" && tail.length >= 2) {
420
+ ref = tail[1];
421
+ subpath = tail.slice(2).join("/");
422
+ }
423
+ else if (tail.length > 0) {
424
+ subpath = tail.join("/");
425
+ }
426
+ if (typeof refOverride === "string" && refOverride.trim().length > 0) {
427
+ ref = refOverride.trim();
428
+ }
429
+ return {
430
+ kind: "github",
431
+ input,
432
+ owner,
433
+ repo,
434
+ repository: `https://github.com/${owner}/${repo}.git`,
435
+ ref,
436
+ subpath: subpath && subpath.length > 0 ? subpath : undefined,
437
+ };
438
+ }
439
+ export function parseExtensionInstallSource(input, options = {}) {
440
+ const normalizedInput = input.trim();
441
+ if (normalizedInput.length === 0) {
442
+ throw new PmCliError("Extension source is required for --install.", EXIT_CODE.USAGE);
443
+ }
444
+ const refOverride = typeof options.ref === "string" && options.ref.trim().length > 0 ? options.ref.trim() : undefined;
445
+ const maybeGithubByUrl = (() => {
446
+ try {
447
+ const parsed = new URL(normalizedInput);
448
+ if (parsed.hostname !== "github.com") {
449
+ return null;
450
+ }
451
+ const pathSpec = parsed.pathname.replace(/^\/+/, "");
452
+ return parseGithubPathSpec(pathSpec, normalizedInput, refOverride);
453
+ }
454
+ catch {
455
+ return null;
456
+ }
457
+ })();
458
+ if (maybeGithubByUrl) {
459
+ return maybeGithubByUrl;
460
+ }
461
+ const strippedDomainInput = normalizedInput.startsWith("github.com/") ? normalizedInput.slice("github.com/".length) : null;
462
+ if (strippedDomainInput) {
463
+ const parsed = parseGithubPathSpec(strippedDomainInput, normalizedInput, refOverride);
464
+ if (!parsed) {
465
+ throw new PmCliError(`Invalid GitHub source "${normalizedInput}".`, EXIT_CODE.USAGE);
466
+ }
467
+ return parsed;
468
+ }
469
+ if (options.forceGithub) {
470
+ const parsed = parseGithubPathSpec(normalizedInput, normalizedInput, refOverride);
471
+ if (!parsed) {
472
+ throw new PmCliError(`Invalid GitHub shorthand "${normalizedInput}".`, EXIT_CODE.USAGE);
473
+ }
474
+ return parsed;
475
+ }
476
+ if (/^https?:\/\//i.test(normalizedInput)) {
477
+ throw new PmCliError(`Unsupported extension source URL "${normalizedInput}". Supported remote source host: github.com.`, EXIT_CODE.USAGE);
478
+ }
479
+ return {
480
+ kind: "local",
481
+ input: normalizedInput,
482
+ absolute_path: path.resolve(process.cwd(), normalizedInput),
483
+ };
484
+ }
485
+ async function runGitCommand(args) {
486
+ try {
487
+ const result = await execFileAsync("git", args, { encoding: "utf8" });
488
+ return (result.stdout ?? "").trim();
489
+ }
490
+ catch (error) {
491
+ const stderr = typeof error === "object" && error !== null && "stderr" in error ? String(error.stderr) : "";
492
+ const message = stderr.trim().length > 0 ? stderr.trim() : error instanceof Error ? error.message : String(error);
493
+ throw new PmCliError(`Git command failed: git ${args.join(" ")}\n${message}`, EXIT_CODE.GENERIC_FAILURE);
494
+ }
495
+ }
496
+ async function listManifestDirectories(parentDirectory) {
497
+ if (!(await pathExists(parentDirectory))) {
498
+ return [];
499
+ }
500
+ const entries = await fs.readdir(parentDirectory, { withFileTypes: true });
501
+ const candidates = [];
502
+ for (const entry of entries) {
503
+ if (!entry.isDirectory()) {
504
+ continue;
505
+ }
506
+ const directory = path.join(parentDirectory, entry.name);
507
+ if (await pathExists(path.join(directory, "manifest.json"))) {
508
+ candidates.push(directory);
509
+ }
510
+ }
511
+ return candidates.sort((left, right) => left.localeCompare(right));
512
+ }
513
+ async function resolveGithubSourceDirectory(cloneDirectory, source) {
514
+ const candidatePaths = [];
515
+ if (source.subpath) {
516
+ candidatePaths.push(source.subpath);
517
+ candidatePaths.push(path.posix.join(".agents/pm/extensions", source.subpath));
518
+ candidatePaths.push(path.posix.join(".custom/pm-extensions", source.subpath));
519
+ candidatePaths.push(path.posix.join(".custom/pm-extension", source.subpath));
520
+ }
521
+ for (const candidate of candidatePaths) {
522
+ const absolute = path.resolve(cloneDirectory, candidate);
523
+ if (await pathExists(path.join(absolute, "manifest.json"))) {
524
+ return { directory: absolute, resolved_subpath: candidate };
525
+ }
526
+ }
527
+ if (await pathExists(path.join(cloneDirectory, "manifest.json"))) {
528
+ return { directory: cloneDirectory, resolved_subpath: "." };
529
+ }
530
+ const defaultRoots = [
531
+ path.join(cloneDirectory, ".agents", "pm", "extensions"),
532
+ path.join(cloneDirectory, ".custom", "pm-extensions"),
533
+ path.join(cloneDirectory, ".custom", "pm-extension"),
534
+ ];
535
+ const discovered = (await Promise.all(defaultRoots.map((defaultRoot) => listManifestDirectories(defaultRoot)))).flat();
536
+ if (discovered.length === 1) {
537
+ return {
538
+ directory: discovered[0],
539
+ resolved_subpath: path.relative(cloneDirectory, discovered[0]).replaceAll(path.sep, "/"),
540
+ };
541
+ }
542
+ if (discovered.length > 1) {
543
+ const choices = discovered
544
+ .map((entry) => path.relative(cloneDirectory, entry).replaceAll(path.sep, "/"))
545
+ .sort((left, right) => left.localeCompare(right));
546
+ throw new PmCliError(`GitHub source "${source.input}" contains multiple extension manifests. Provide an explicit path. Candidates: ${choices.join(", ")}`, EXIT_CODE.USAGE);
547
+ }
548
+ throw new PmCliError(`Unable to locate extension manifest in GitHub source "${source.input}". Provide an explicit extension path.`, EXIT_CODE.USAGE);
549
+ }
550
+ async function resolveInstallSource(source) {
551
+ if (source.kind === "local") {
552
+ if (!(await pathExists(source.absolute_path))) {
553
+ throw new PmCliError(`Local extension source does not exist: "${source.absolute_path}".`, EXIT_CODE.NOT_FOUND);
554
+ }
555
+ const stats = await fs.stat(source.absolute_path);
556
+ if (!stats.isDirectory()) {
557
+ throw new PmCliError(`Local extension source must be a directory: "${source.absolute_path}".`, EXIT_CODE.USAGE);
558
+ }
559
+ return {
560
+ source,
561
+ directory: source.absolute_path,
562
+ };
563
+ }
564
+ const cloneDirectory = await fs.mkdtemp(path.join(os.tmpdir(), "pm-extension-source-"));
565
+ const cloneArgs = ["clone", "--depth", "1"];
566
+ if (source.ref) {
567
+ cloneArgs.push("--branch", source.ref);
568
+ }
569
+ cloneArgs.push(source.repository, cloneDirectory);
570
+ try {
571
+ await runGitCommand(cloneArgs);
572
+ const commit = await runGitCommand(["-C", cloneDirectory, "rev-parse", "HEAD"]);
573
+ const resolved = await resolveGithubSourceDirectory(cloneDirectory, source);
574
+ return {
575
+ source,
576
+ directory: resolved.directory,
577
+ resolved_subpath: resolved.resolved_subpath,
578
+ commit,
579
+ cleanup: async () => {
580
+ await fs.rm(cloneDirectory, { recursive: true, force: true });
581
+ },
582
+ };
583
+ }
584
+ catch (error) {
585
+ await fs.rm(cloneDirectory, { recursive: true, force: true });
586
+ throw error;
587
+ }
588
+ }
589
+ async function areDirectoriesEquivalent(left, right) {
590
+ if (!(await pathExists(left)) || !(await pathExists(right))) {
591
+ return false;
592
+ }
593
+ const [leftRealPath, rightRealPath] = await Promise.all([fs.realpath(left), fs.realpath(right)]);
594
+ return leftRealPath === rightRealPath;
595
+ }
596
+ function upsertManagedEntry(state, entry) {
597
+ const updatedEntries = state.entries.filter((candidate) => normalizeExtensionNameForMatch(candidate.name) !== normalizeExtensionNameForMatch(entry.name) &&
598
+ normalizeExtensionNameForMatch(candidate.directory) !== normalizeExtensionNameForMatch(entry.directory));
599
+ updatedEntries.push(entry);
600
+ return {
601
+ ...state,
602
+ updated_at: nowIso(),
603
+ entries: sortManagedEntries(updatedEntries),
604
+ };
605
+ }
606
+ function resolveUpdateCheckResolution(managedEntry) {
607
+ if (!managedEntry) {
608
+ return {
609
+ status: "skipped_unmanaged",
610
+ reason: "extension_not_managed",
611
+ };
612
+ }
613
+ if (managedEntry.source.kind !== "github") {
614
+ return {
615
+ status: "skipped_non_github",
616
+ reason: `managed_source_kind_${managedEntry.source.kind}`,
617
+ };
618
+ }
619
+ const updateError = typeof managedEntry.update_error === "string" ? managedEntry.update_error.trim() : "";
620
+ if (updateError.length > 0) {
621
+ return {
622
+ status: "failed",
623
+ reason: updateError,
624
+ };
625
+ }
626
+ if (typeof managedEntry.last_update_check_at === "string" && managedEntry.last_update_check_at.trim().length > 0) {
627
+ if (managedEntry.update_available === true) {
628
+ return {
629
+ status: "checked",
630
+ reason: "update_available",
631
+ };
632
+ }
633
+ if (managedEntry.update_available === false) {
634
+ return {
635
+ status: "checked",
636
+ reason: "up_to_date",
637
+ };
638
+ }
639
+ return {
640
+ status: "checked",
641
+ reason: "checked_without_commit_baseline",
642
+ };
643
+ }
644
+ return {
645
+ status: "not_checked",
646
+ reason: "no_update_check_recorded",
647
+ };
648
+ }
649
+ async function listInstalledExtensions(extensionsRoot, scope, settings, state) {
650
+ if (!(await pathExists(extensionsRoot))) {
651
+ return {
652
+ extensions: [],
653
+ warnings: [],
654
+ };
655
+ }
656
+ const entries = await fs.readdir(extensionsRoot, { withFileTypes: true });
657
+ const directories = entries
658
+ .filter((entry) => entry.isDirectory())
659
+ .map((entry) => entry.name)
660
+ .sort((left, right) => left.localeCompare(right));
661
+ const managedByName = new Map();
662
+ const managedByDirectory = new Map();
663
+ for (const managedEntry of state.entries) {
664
+ managedByName.set(normalizeExtensionNameForMatch(managedEntry.name), managedEntry);
665
+ managedByDirectory.set(normalizeExtensionNameForMatch(managedEntry.directory), managedEntry);
666
+ }
667
+ const warnings = [];
668
+ const summaries = [];
669
+ for (const directoryName of directories) {
670
+ const extensionDirectory = path.join(extensionsRoot, directoryName);
671
+ const manifestPath = path.join(extensionDirectory, "manifest.json");
672
+ if (!(await pathExists(manifestPath))) {
673
+ warnings.push(`extension_manifest_missing:${scope}:${directoryName}`);
674
+ const managedEntry = managedByDirectory.get(normalizeExtensionNameForMatch(directoryName));
675
+ const updateCheck = resolveUpdateCheckResolution(managedEntry);
676
+ const enabled = managedEntry ? isExtensionEnabled(settings, managedEntry.name) : false;
677
+ summaries.push({
678
+ name: managedEntry?.name ?? directoryName,
679
+ directory: directoryName,
680
+ version: managedEntry?.manifest_version ?? "unknown",
681
+ entry: managedEntry?.manifest_entry ?? "unknown",
682
+ scope,
683
+ active: enabled,
684
+ enabled,
685
+ runtime_active: null,
686
+ activation_status: "unknown",
687
+ managed: Boolean(managedEntry),
688
+ source: managedEntry?.source,
689
+ update_available: managedEntry?.update_available,
690
+ last_update_check_at: managedEntry?.last_update_check_at,
691
+ last_update_remote_commit: managedEntry?.last_update_remote_commit,
692
+ update_error: managedEntry?.update_error,
693
+ update_check_status: updateCheck.status,
694
+ update_check_reason: updateCheck.reason,
695
+ });
696
+ continue;
697
+ }
698
+ let rawManifest;
699
+ try {
700
+ rawManifest = JSON.parse(await fs.readFile(manifestPath, "utf8"));
701
+ }
702
+ catch {
703
+ warnings.push(`extension_manifest_invalid_json:${scope}:${directoryName}`);
704
+ continue;
705
+ }
706
+ const manifest = parseExtensionManifest(rawManifest);
707
+ if (!manifest) {
708
+ warnings.push(`extension_manifest_invalid:${scope}:${directoryName}`);
709
+ continue;
710
+ }
711
+ const managedEntry = managedByName.get(normalizeExtensionNameForMatch(manifest.name)) ??
712
+ managedByDirectory.get(normalizeExtensionNameForMatch(directoryName));
713
+ const updateCheck = resolveUpdateCheckResolution(managedEntry);
714
+ const enabled = isExtensionEnabled(settings, manifest.name);
715
+ summaries.push({
716
+ name: manifest.name,
717
+ directory: directoryName,
718
+ version: manifest.version,
719
+ entry: manifest.entry,
720
+ scope,
721
+ active: enabled,
722
+ enabled,
723
+ runtime_active: null,
724
+ activation_status: "unknown",
725
+ managed: Boolean(managedEntry),
726
+ source: managedEntry?.source,
727
+ update_available: managedEntry?.update_available,
728
+ last_update_check_at: managedEntry?.last_update_check_at,
729
+ last_update_remote_commit: managedEntry?.last_update_remote_commit,
730
+ update_error: managedEntry?.update_error,
731
+ update_check_status: updateCheck.status,
732
+ update_check_reason: updateCheck.reason,
733
+ });
734
+ }
735
+ return {
736
+ extensions: summaries.sort((left, right) => left.name.localeCompare(right.name)),
737
+ warnings: warnings.sort((left, right) => left.localeCompare(right)),
738
+ };
739
+ }
740
+ function applyDoctorRuntimeActivationState(extensions, loadResult, activationResult) {
741
+ const loadedNames = new Set(loadResult.loaded.map((entry) => normalizeExtensionNameForMatch(entry.name)));
742
+ const loadFailedNames = new Set(loadResult.failed.map((entry) => normalizeExtensionNameForMatch(entry.name)));
743
+ const activationFailedNames = new Set(activationResult.failed.map((entry) => normalizeExtensionNameForMatch(entry.name)));
744
+ return extensions.map((entry) => {
745
+ if (!entry.enabled) {
746
+ return {
747
+ ...entry,
748
+ runtime_active: false,
749
+ activation_status: "not_loaded",
750
+ };
751
+ }
752
+ const normalizedName = normalizeExtensionNameForMatch(entry.name);
753
+ if (loadFailedNames.has(normalizedName) || activationFailedNames.has(normalizedName)) {
754
+ return {
755
+ ...entry,
756
+ runtime_active: false,
757
+ activation_status: "failed",
758
+ };
759
+ }
760
+ if (loadedNames.has(normalizedName)) {
761
+ return {
762
+ ...entry,
763
+ runtime_active: true,
764
+ activation_status: "ok",
765
+ };
766
+ }
767
+ return {
768
+ ...entry,
769
+ runtime_active: false,
770
+ activation_status: "not_loaded",
771
+ };
772
+ });
773
+ }
774
+ async function checkGithubUpdate(source) {
775
+ const checkedAt = nowIso();
776
+ if (source.kind !== "github" || !source.repository) {
777
+ return {
778
+ checked_at: checkedAt,
779
+ available: null,
780
+ error: "not_a_github_managed_source",
781
+ };
782
+ }
783
+ try {
784
+ const ref = source.ref && source.ref.trim().length > 0 ? source.ref.trim() : "HEAD";
785
+ const output = await runGitCommand(["ls-remote", source.repository, ref]);
786
+ const firstLine = output
787
+ .split(/\r?\n/)
788
+ .map((line) => line.trim())
789
+ .find((line) => line.length > 0);
790
+ if (!firstLine) {
791
+ return {
792
+ checked_at: checkedAt,
793
+ available: null,
794
+ error: "no_remote_reference_found",
795
+ };
796
+ }
797
+ const [remoteCommit] = firstLine.split(/\s+/);
798
+ if (typeof remoteCommit !== "string" || remoteCommit.length === 0) {
799
+ return {
800
+ checked_at: checkedAt,
801
+ available: null,
802
+ error: "invalid_remote_reference",
803
+ };
804
+ }
805
+ if (typeof source.commit === "string" && source.commit.trim().length > 0) {
806
+ return {
807
+ checked_at: checkedAt,
808
+ remote_commit: remoteCommit,
809
+ available: remoteCommit !== source.commit.trim(),
810
+ };
811
+ }
812
+ return {
813
+ checked_at: checkedAt,
814
+ remote_commit: remoteCommit,
815
+ available: null,
816
+ error: "missing_installed_commit",
817
+ };
818
+ }
819
+ catch (error) {
820
+ return {
821
+ checked_at: checkedAt,
822
+ available: null,
823
+ error: error instanceof Error ? error.message : String(error),
824
+ };
825
+ }
826
+ }
827
+ async function adoptUnmanagedExtensions(extensionsRoot, scope, installedExtensions, state) {
828
+ const unmanagedCandidates = installedExtensions.filter((entry) => !entry.managed);
829
+ const sortedCandidates = [...unmanagedCandidates].sort((left, right) => {
830
+ const byName = left.name.localeCompare(right.name);
831
+ if (byName !== 0) {
832
+ return byName;
833
+ }
834
+ return left.directory.localeCompare(right.directory);
835
+ });
836
+ let nextState = state;
837
+ const adoptedEntries = [];
838
+ for (const candidate of sortedCandidates) {
839
+ const extensionDirectory = path.join(extensionsRoot, candidate.directory);
840
+ const validated = await validateExtensionDirectory(extensionDirectory);
841
+ const now = nowIso();
842
+ const sourceRecord = {
843
+ kind: "local",
844
+ input: candidate.name,
845
+ location: extensionDirectory,
846
+ };
847
+ nextState = upsertManagedEntry(nextState, {
848
+ name: validated.manifest.name,
849
+ directory: candidate.directory,
850
+ scope,
851
+ manifest_version: validated.manifest.version,
852
+ manifest_entry: validated.manifest.entry,
853
+ capabilities: [...validated.manifest.capabilities],
854
+ installed_at: now,
855
+ updated_at: now,
856
+ source: sourceRecord,
857
+ });
858
+ adoptedEntries.push({
859
+ name: validated.manifest.name,
860
+ directory: candidate.directory,
861
+ version: validated.manifest.version,
862
+ entry: validated.manifest.entry,
863
+ source: sourceRecord,
864
+ });
865
+ }
866
+ if (adoptedEntries.length > 0) {
867
+ await writeManagedExtensionState(extensionsRoot, nextState);
868
+ }
869
+ return {
870
+ state: nextState,
871
+ adopted_entries: adoptedEntries,
872
+ unmanaged_candidates: sortedCandidates,
873
+ already_managed_count: installedExtensions.length - unmanagedCandidates.length,
874
+ };
875
+ }
876
+ function resolveExtensionRootsForScope(scope, global) {
877
+ const pmRoot = resolvePmRoot(process.cwd(), global.path);
878
+ const roots = resolveExtensionRoots(pmRoot, process.cwd());
879
+ const settingsRoot = scope === "global" ? resolveGlobalPmRoot(process.cwd()) : pmRoot;
880
+ const selectedRoot = scope === "global" ? roots.global : roots.project;
881
+ return {
882
+ pm_root: pmRoot,
883
+ scope,
884
+ settings_root: settingsRoot,
885
+ selected_root: selectedRoot,
886
+ roots,
887
+ };
888
+ }
889
+ function resolveGithubOption(options) {
890
+ if (typeof options.gh === "string" && typeof options.github === "string" && options.gh.trim() !== options.github.trim()) {
891
+ throw new PmCliError('Options "--gh" and "--github" must match when both are provided.', EXIT_CODE.USAGE);
892
+ }
893
+ if (typeof options.gh === "string" && options.gh.trim().length > 0) {
894
+ return options.gh.trim();
895
+ }
896
+ if (typeof options.github === "string" && options.github.trim().length > 0) {
897
+ return options.github.trim();
898
+ }
899
+ return undefined;
900
+ }
901
+ function requireTarget(target, action) {
902
+ const normalized = target?.trim();
903
+ if (!normalized) {
904
+ if (action === "init") {
905
+ throw new PmCliError('Action "init" requires a scaffold target path (for example: pm extension --init ./my-extension or pm extension init ./my-extension).', EXIT_CODE.USAGE);
906
+ }
907
+ throw new PmCliError(`Action "${action}" requires an extension name or source target argument.`, EXIT_CODE.USAGE);
908
+ }
909
+ return normalized;
910
+ }
911
+ function buildStarterExtensionScaffoldFiles(extensionName, commandName) {
912
+ const manifest = `${JSON.stringify({
913
+ name: extensionName,
914
+ version: "0.1.0",
915
+ entry: "./index.js",
916
+ capabilities: ["commands"],
917
+ }, null, 2)}\n`;
918
+ const entrypoint = [
919
+ "module.exports = {",
920
+ " activate(api) {",
921
+ " api.registerCommand({",
922
+ ` name: ${JSON.stringify(commandName)},`,
923
+ ' description: "Starter scaffold command. Replace with your own behavior.",',
924
+ " run: async (context) => ({",
925
+ " ok: true,",
926
+ ` source: ${JSON.stringify(extensionName)},`,
927
+ " command: context.command,",
928
+ ' message: "Starter extension scaffold is active.",',
929
+ " }),",
930
+ " });",
931
+ " },",
932
+ "};",
933
+ "",
934
+ ].join("\n");
935
+ const readme = [
936
+ `# ${extensionName}`,
937
+ "",
938
+ "Generated by `pm extension --init`.",
939
+ "",
940
+ "## Included Files",
941
+ "- `manifest.json`: extension metadata and capabilities.",
942
+ "- `index.js`: starter command registration using the `commands` capability.",
943
+ "",
944
+ "## Quick Start",
945
+ "```bash",
946
+ "pm extension --install --project <scaffold-path>",
947
+ `pm ${commandName}`,
948
+ "pm extension --doctor --project --detail summary",
949
+ "```",
950
+ "",
951
+ "## Notes",
952
+ "- This scaffold uses CommonJS (`module.exports`) for zero-config runtime compatibility.",
953
+ "- Update `manifest.json` capabilities and `index.js` command behavior as your extension evolves.",
954
+ "",
955
+ ].join("\n");
956
+ return {
957
+ "manifest.json": manifest,
958
+ "index.js": entrypoint,
959
+ "README.md": readme,
960
+ };
961
+ }
962
+ async function scaffoldExtensionProject(target) {
963
+ const normalizedTarget = target.trim();
964
+ const targetPath = path.resolve(process.cwd(), normalizedTarget);
965
+ const extensionName = normalizeManagedDirectoryName(path.basename(targetPath));
966
+ const commandName = `${extensionName} ping`;
967
+ const scaffoldFiles = buildStarterExtensionScaffoldFiles(extensionName, commandName);
968
+ let createdDirectory = false;
969
+ if (await pathExists(targetPath)) {
970
+ const existingTargetStats = await fs.stat(targetPath);
971
+ if (!existingTargetStats.isDirectory()) {
972
+ throw new PmCliError(`Scaffold target "${targetPath}" exists and is not a directory.`, EXIT_CODE.CONFLICT);
973
+ }
974
+ }
975
+ else {
976
+ await fs.mkdir(targetPath, { recursive: true });
977
+ createdDirectory = true;
978
+ }
979
+ const files = [];
980
+ for (const [relativePath, content] of Object.entries(scaffoldFiles)) {
981
+ const absolutePath = path.join(targetPath, relativePath);
982
+ if (await pathExists(absolutePath)) {
983
+ const existingContent = await fs.readFile(absolutePath, "utf8");
984
+ if (existingContent !== content) {
985
+ throw new PmCliError(`Scaffold file "${relativePath}" already exists with different content in "${targetPath}". Choose a new target path or remove conflicting files.`, EXIT_CODE.CONFLICT);
986
+ }
987
+ files.push({
988
+ path: relativePath,
989
+ status: "unchanged",
990
+ });
991
+ continue;
992
+ }
993
+ await fs.writeFile(absolutePath, content, "utf8");
994
+ files.push({
995
+ path: relativePath,
996
+ status: "created",
997
+ });
998
+ }
999
+ return {
1000
+ extension_name: extensionName,
1001
+ command_name: commandName,
1002
+ target_path: targetPath,
1003
+ created_directory: createdDirectory,
1004
+ files,
1005
+ };
1006
+ }
1007
+ function classifyDoctorLoadFailureWarnings(loadFailures) {
1008
+ const warnings = [];
1009
+ for (const failure of loadFailures) {
1010
+ const normalizedError = failure.error.toLowerCase();
1011
+ if (normalizedError.includes("cannot find package '@unbrained/pm-cli'") ||
1012
+ normalizedError.includes('cannot find module "@unbrained/pm-cli"') ||
1013
+ normalizedError.includes("cannot find module '@unbrained/pm-cli'")) {
1014
+ warnings.push(`extension_load_failed_sdk_dependency_missing:${failure.name}`);
1015
+ }
1016
+ if (normalizedError.includes("cannot use import statement outside a module") ||
1017
+ normalizedError.includes("to load an es module") ||
1018
+ normalizedError.includes("must use import to load es module")) {
1019
+ warnings.push(`extension_load_failed_module_mode_mismatch:${failure.name}`);
1020
+ }
1021
+ }
1022
+ return [...new Set(warnings)].sort((left, right) => left.localeCompare(right));
1023
+ }
1024
+ function buildExtensionTriageSummary(scope, warnings, extensions) {
1025
+ const normalizedWarnings = [...new Set(warnings)].sort((left, right) => left.localeCompare(right));
1026
+ const managedTotal = extensions.filter((entry) => entry.managed).length;
1027
+ const enabledTotal = extensions.filter((entry) => entry.enabled).length;
1028
+ const activeTotal = extensions.filter((entry) => entry.active).length;
1029
+ const updateAvailableTotal = extensions.filter((entry) => entry.update_available === true).length;
1030
+ const unmanagedExtensions = extensions.filter((entry) => entry.managed === false);
1031
+ const unmanagedExpectedExtensions = unmanagedExtensions
1032
+ .filter((entry) => isExpectedUnmanagedExtension(entry))
1033
+ .map((entry) => entry.name)
1034
+ .sort((left, right) => left.localeCompare(right));
1035
+ const unmanagedActionRequiredExtensions = unmanagedExtensions
1036
+ .filter((entry) => !isExpectedUnmanagedExtension(entry))
1037
+ .map((entry) => entry.name)
1038
+ .sort((left, right) => left.localeCompare(right));
1039
+ const updateCheckStatusTotals = {
1040
+ checked: 0,
1041
+ skipped_unmanaged: 0,
1042
+ skipped_non_github: 0,
1043
+ failed: 0,
1044
+ not_checked: 0,
1045
+ };
1046
+ for (const entry of extensions) {
1047
+ updateCheckStatusTotals[entry.update_check_status] += 1;
1048
+ }
1049
+ const updateCheckFailedTotal = updateCheckStatusTotals.failed;
1050
+ const skippedUnmanagedTotal = updateCheckStatusTotals.skipped_unmanaged;
1051
+ const skippedNonGithubTotal = updateCheckStatusTotals.skipped_non_github;
1052
+ const updateHealthPartial = unmanagedActionRequiredExtensions.length > 0;
1053
+ const updateHealthCoverage = updateHealthPartial ? "partial" : "full";
1054
+ const partialCoverageWarnings = updateHealthPartial
1055
+ ? [`extension_update_health_partial_coverage:skipped_unmanaged:${unmanagedActionRequiredExtensions.length}`]
1056
+ : [];
1057
+ const effectiveWarnings = [...new Set([...normalizedWarnings, ...partialCoverageWarnings])].sort((left, right) => left.localeCompare(right));
1058
+ const warningCodes = [...new Set(effectiveWarnings.map((value) => warningCode(value)))].sort((left, right) => left.localeCompare(right));
1059
+ const scopeFlag = scope === "global" ? "--global" : "--project";
1060
+ const remediation = [];
1061
+ if (normalizedWarnings.length > 0) {
1062
+ if (normalizedWarnings.some((warning) => warning.startsWith("extension_manifest_"))) {
1063
+ remediation.push(`Run pm extension --explore ${scopeFlag} to inspect discovered manifests and directories.`);
1064
+ }
1065
+ if (normalizedWarnings.some((warning) => warning.startsWith("extension_capability_unknown:"))) {
1066
+ remediation.push(`Unknown extension capabilities detected. Allowed capabilities: ${KNOWN_EXTENSION_CAPABILITIES.join(", ")}. ` +
1067
+ "Review extension_capability_unknown warning details for suggested replacements.");
1068
+ }
1069
+ if (normalizedWarnings.some((warning) => warning.startsWith("extension_capability_legacy_alias:"))) {
1070
+ remediation.push("Legacy extension capability aliases were auto-remapped to canonical capabilities. " +
1071
+ "Update manifests to canonical names (migration/validation -> schema).");
1072
+ }
1073
+ if (normalizedWarnings.some((warning) => warning.startsWith("extension_command_definition_legacy_handler_alias:"))) {
1074
+ remediation.push("Extension command definitions using legacy handler were auto-remapped. " +
1075
+ "Update command definitions to use run: (context) => ... for forward compatibility.");
1076
+ }
1077
+ if (normalizedWarnings.some((warning) => warning.startsWith("extension_load_failed_sdk_dependency_missing:"))) {
1078
+ remediation.push(`Detected extension load failures caused by missing SDK dependency resolution. ` +
1079
+ `Ensure extension package dependencies include "@unbrained/pm-cli" and reinstall dependencies before running pm extension --doctor ${scopeFlag}.`);
1080
+ }
1081
+ if (normalizedWarnings.some((warning) => warning.startsWith("extension_load_failed_module_mode_mismatch:"))) {
1082
+ remediation.push(`Detected extension module-mode mismatches. For ESM-based extension entries/imports, set package.json "type": "module" ` +
1083
+ `or use an explicit .mjs entry and rerun pm extension --doctor ${scopeFlag}.`);
1084
+ }
1085
+ if (updateCheckFailedTotal > 0) {
1086
+ remediation.push(`Run pm extension --manage ${scopeFlag} after validating network and repository access.`);
1087
+ }
1088
+ if (normalizedWarnings.some((warning) => warning.startsWith("extension_manager_state_"))) {
1089
+ remediation.push(`Review and repair ${scope} managed extension state file if schema/read warnings persist.`);
1090
+ }
1091
+ }
1092
+ if (updateHealthPartial) {
1093
+ remediation.push(`Update-check coverage is partial because unmanaged extensions need adoption. Adopt existing installs via pm extension --manage ${scopeFlag} --fix-managed-state (or pm extension --adopt-all ${scopeFlag}, pm extension --adopt <name> ${scopeFlag}, or reinstall via pm extension --install ${scopeFlag} <source>).`);
1094
+ }
1095
+ else if (skippedUnmanagedTotal > 0) {
1096
+ remediation.push(`Loaded unmanaged extensions are currently treated as informational. Use pm extension --manage ${scopeFlag} --fix-managed-state to adopt them for update checks.`);
1097
+ }
1098
+ if (skippedNonGithubTotal > 0) {
1099
+ remediation.push(`Non-GitHub managed extensions are skipped by update checks. Use doctor output for non-update diagnostics.`);
1100
+ }
1101
+ if (updateAvailableTotal > 0) {
1102
+ remediation.push(`Update available managed extensions via pm extension --install ${scopeFlag} <source>.`);
1103
+ }
1104
+ if (remediation.length === 0) {
1105
+ remediation.push(`No immediate action required. Re-run pm extension --manage ${scopeFlag} after extension changes.`);
1106
+ }
1107
+ return {
1108
+ status: effectiveWarnings.length === 0 ? "ok" : "warn",
1109
+ warning_count: effectiveWarnings.length,
1110
+ warning_codes: warningCodes,
1111
+ warnings: effectiveWarnings,
1112
+ total_extensions: extensions.length,
1113
+ managed_total: managedTotal,
1114
+ enabled_total: enabledTotal,
1115
+ active_total: activeTotal,
1116
+ update_available_total: updateAvailableTotal,
1117
+ update_health_coverage: updateHealthCoverage,
1118
+ update_health_partial: updateHealthPartial,
1119
+ unmanaged_loaded_extension_count: unmanagedExtensions.length,
1120
+ unmanaged_loaded_extensions: unmanagedExtensions
1121
+ .map((entry) => entry.name)
1122
+ .sort((left, right) => left.localeCompare(right)),
1123
+ unmanaged_expected_extension_count: unmanagedExpectedExtensions.length,
1124
+ unmanaged_expected_extensions: unmanagedExpectedExtensions,
1125
+ unmanaged_action_required_extension_count: unmanagedActionRequiredExtensions.length,
1126
+ unmanaged_action_required_extensions: unmanagedActionRequiredExtensions,
1127
+ update_check_status_totals: updateCheckStatusTotals,
1128
+ update_check_failed_total: updateCheckFailedTotal,
1129
+ top_warnings: effectiveWarnings.slice(0, 8),
1130
+ remediation,
1131
+ };
1132
+ }
1133
+ function parseDoctorDetailMode(raw) {
1134
+ if (!raw || raw.trim().length === 0) {
1135
+ return "summary";
1136
+ }
1137
+ const normalized = raw.trim().toLowerCase();
1138
+ if (normalized === "summary" || normalized === "deep") {
1139
+ return normalized;
1140
+ }
1141
+ throw new PmCliError(`Invalid --detail value "${raw}". Expected summary or deep.`, EXIT_CODE.USAGE);
1142
+ }
1143
+ function warningCode(value) {
1144
+ const normalized = value.trim();
1145
+ const separator = normalized.indexOf(":");
1146
+ if (separator === -1) {
1147
+ return normalized;
1148
+ }
1149
+ return normalized.slice(0, separator);
1150
+ }
1151
+ function collectUnknownCapabilityGuidance(warnings) {
1152
+ const seen = new Set();
1153
+ const guidance = [];
1154
+ for (const warning of warnings) {
1155
+ const parsedDetails = (() => {
1156
+ const unknownWarning = parseUnknownExtensionCapabilityWarning(warning);
1157
+ if (unknownWarning) {
1158
+ return [unknownWarning];
1159
+ }
1160
+ return parseLegacyExtensionCapabilityAliasWarning(warning);
1161
+ })();
1162
+ for (const parsed of parsedDetails) {
1163
+ const key = `${parsed.layer}:${parsed.name}:${parsed.capability}`;
1164
+ if (seen.has(key)) {
1165
+ continue;
1166
+ }
1167
+ seen.add(key);
1168
+ guidance.push(parsed);
1169
+ }
1170
+ }
1171
+ return guidance;
1172
+ }
1173
+ function buildCapabilityContractMetadata() {
1174
+ return {
1175
+ version: EXTENSION_CAPABILITY_CONTRACT.version,
1176
+ capabilities: [...EXTENSION_CAPABILITY_CONTRACT.capabilities],
1177
+ legacy_aliases: { ...EXTENSION_CAPABILITY_CONTRACT.legacy_aliases },
1178
+ };
1179
+ }
1180
+ function isExpectedUnmanagedExtension(entry) {
1181
+ const normalizedName = normalizeExtensionNameForMatch(entry.name);
1182
+ const normalizedDirectory = normalizeExtensionNameForMatch(entry.directory);
1183
+ if (normalizedName.startsWith("builtin-")) {
1184
+ return true;
1185
+ }
1186
+ return normalizedDirectory === "beads" || normalizedDirectory === "todos";
1187
+ }
1188
+ function buildDoctorConsistencySummary(scope, installedExtensions, loadedExtensions, failedLoads, disabledByFlag) {
1189
+ if (scope !== "project" || disabledByFlag) {
1190
+ return {
1191
+ warnings: [],
1192
+ summary: {
1193
+ active_project_count: 0,
1194
+ loaded_project_count: 0,
1195
+ active_project_names: [],
1196
+ loaded_project_names: [],
1197
+ missing_active_project_names: [],
1198
+ },
1199
+ };
1200
+ }
1201
+ const activeProjectNames = [
1202
+ ...new Set(installedExtensions
1203
+ .filter((entry) => entry.active)
1204
+ .map((entry) => normalizeExtensionNameForMatch(entry.name))),
1205
+ ].sort((left, right) => left.localeCompare(right));
1206
+ const loadedProjectNames = [
1207
+ ...new Set(loadedExtensions
1208
+ .filter((entry) => entry.layer === "project")
1209
+ .map((entry) => normalizeExtensionNameForMatch(entry.name))),
1210
+ ].sort((left, right) => left.localeCompare(right));
1211
+ const failedLoadNames = new Set(failedLoads.map((entry) => normalizeExtensionNameForMatch(entry.name)));
1212
+ const missingActiveProjectNames = activeProjectNames
1213
+ .filter((name) => !loadedProjectNames.includes(name) && !failedLoadNames.has(name))
1214
+ .sort((left, right) => left.localeCompare(right));
1215
+ const warnings = missingActiveProjectNames.length > 0
1216
+ ? [`extension_doctor_consistency_active_not_loaded:${missingActiveProjectNames.join(",")}`]
1217
+ : [];
1218
+ return {
1219
+ warnings,
1220
+ summary: {
1221
+ active_project_count: activeProjectNames.length,
1222
+ loaded_project_count: loadedProjectNames.length,
1223
+ active_project_names: activeProjectNames,
1224
+ loaded_project_names: loadedProjectNames,
1225
+ missing_active_project_names: missingActiveProjectNames,
1226
+ },
1227
+ };
1228
+ }
1229
+ export async function runExtension(target, options, global) {
1230
+ const action = resolveAction(target, options);
1231
+ if ((options.strictExit === true || options.failOnWarn === true) && action !== "doctor") {
1232
+ throw new PmCliError("--strict-exit and --fail-on-warn are only valid with --doctor.", EXIT_CODE.USAGE);
1233
+ }
1234
+ if (options.trace === true && action !== "doctor") {
1235
+ throw new PmCliError("--trace is only valid with --doctor.", EXIT_CODE.USAGE);
1236
+ }
1237
+ if (options.runtimeProbe === true && action !== "manage") {
1238
+ throw new PmCliError("--runtime-probe is only valid with --manage.", EXIT_CODE.USAGE);
1239
+ }
1240
+ if (options.fixManagedState === true && action !== "manage" && action !== "doctor") {
1241
+ throw new PmCliError("--fix-managed-state is only valid with --manage or --doctor.", EXIT_CODE.USAGE);
1242
+ }
1243
+ const normalizedTarget = (() => {
1244
+ const normalizedInput = target?.trim().toLowerCase();
1245
+ if (action === "doctor" && normalizedInput === "doctor") {
1246
+ return undefined;
1247
+ }
1248
+ const inferredInitAlias = action === "init" &&
1249
+ options.init !== true &&
1250
+ options.scaffold !== true &&
1251
+ (normalizedInput === "init" || normalizedInput === "scaffold");
1252
+ if (inferredInitAlias) {
1253
+ return undefined;
1254
+ }
1255
+ return target;
1256
+ })();
1257
+ const scope = resolveScope(options);
1258
+ const resolvedRoots = resolveExtensionRootsForScope(scope, global);
1259
+ const warnings = [];
1260
+ const withResult = (details) => ({
1261
+ ok: true,
1262
+ action,
1263
+ scope,
1264
+ roots: {
1265
+ project: resolvedRoots.roots.project,
1266
+ global: resolvedRoots.roots.global,
1267
+ selected: resolvedRoots.selected_root,
1268
+ settings_root: resolvedRoots.settings_root,
1269
+ },
1270
+ warnings: [...new Set(warnings)].sort((left, right) => left.localeCompare(right)),
1271
+ details,
1272
+ });
1273
+ if (action === "init") {
1274
+ const githubOption = resolveGithubOption(options);
1275
+ if (githubOption !== undefined || (typeof options.ref === "string" && options.ref.trim().length > 0)) {
1276
+ throw new PmCliError('Action "init" does not accept --gh/--github/--ref options.', EXIT_CODE.USAGE);
1277
+ }
1278
+ const scaffoldTarget = requireTarget(normalizedTarget, action);
1279
+ const scaffold = await scaffoldExtensionProject(scaffoldTarget);
1280
+ const quotedTargetPath = JSON.stringify(scaffold.target_path);
1281
+ return withResult({
1282
+ scaffolded: scaffold.created_directory || scaffold.files.some((entry) => entry.status === "created"),
1283
+ extension: {
1284
+ name: scaffold.extension_name,
1285
+ command: scaffold.command_name,
1286
+ },
1287
+ target_path: scaffold.target_path,
1288
+ created_directory: scaffold.created_directory,
1289
+ files: scaffold.files,
1290
+ next_steps: [
1291
+ `Install the scaffold: pm extension --install --project ${quotedTargetPath}`,
1292
+ `Smoke-test command path: pm ${scaffold.command_name}`,
1293
+ "Run extension diagnostics: pm extension --doctor --project --detail summary",
1294
+ ],
1295
+ });
1296
+ }
1297
+ if (action === "install") {
1298
+ const githubOption = resolveGithubOption(options);
1299
+ const explicitSourceInput = githubOption ?? requireTarget(normalizedTarget, action);
1300
+ const bundledAliasSource = typeof githubOption === "string" ? null : await resolveBundledExtensionAliasSource(explicitSourceInput);
1301
+ const sourceInput = bundledAliasSource ?? explicitSourceInput;
1302
+ const installSource = parseExtensionInstallSource(sourceInput, {
1303
+ forceGithub: typeof githubOption === "string",
1304
+ ref: options.ref,
1305
+ });
1306
+ const resolvedSource = await resolveInstallSource(installSource);
1307
+ const settings = await readSettings(resolvedRoots.settings_root);
1308
+ const managedStateRead = await readManagedExtensionState(resolvedRoots.selected_root);
1309
+ warnings.push(...managedStateRead.warnings);
1310
+ try {
1311
+ const validated = await validateExtensionDirectory(resolvedSource.directory);
1312
+ const destinationDirectoryName = normalizeManagedDirectoryName(validated.manifest.name);
1313
+ const destinationDirectory = path.join(resolvedRoots.selected_root, destinationDirectoryName);
1314
+ const destinationExists = await pathExists(destinationDirectory);
1315
+ const installInPlace = await areDirectoriesEquivalent(validated.directory, destinationDirectory);
1316
+ await fs.mkdir(resolvedRoots.selected_root, { recursive: true });
1317
+ if (!installInPlace) {
1318
+ if (destinationExists) {
1319
+ await fs.rm(destinationDirectory, { recursive: true, force: true });
1320
+ }
1321
+ await fs.cp(validated.directory, destinationDirectory, { recursive: true, force: true });
1322
+ }
1323
+ const sourceRecord = installSource.kind === "local"
1324
+ ? {
1325
+ kind: "local",
1326
+ input: installSource.input,
1327
+ location: installSource.absolute_path,
1328
+ }
1329
+ : {
1330
+ kind: "github",
1331
+ input: installSource.input,
1332
+ location: resolvedSource.resolved_subpath ?? installSource.subpath ?? ".",
1333
+ repository: installSource.repository,
1334
+ owner: installSource.owner,
1335
+ repo: installSource.repo,
1336
+ ref: installSource.ref,
1337
+ subpath: resolvedSource.resolved_subpath ?? installSource.subpath,
1338
+ commit: resolvedSource.commit,
1339
+ };
1340
+ const now = nowIso();
1341
+ const existingManagedEntry = managedStateRead.state.entries.find((entry) => normalizeExtensionNameForMatch(entry.name) === normalizeExtensionNameForMatch(validated.manifest.name));
1342
+ const managedState = upsertManagedEntry(managedStateRead.state, {
1343
+ name: validated.manifest.name,
1344
+ directory: destinationDirectoryName,
1345
+ scope,
1346
+ manifest_version: validated.manifest.version,
1347
+ manifest_entry: validated.manifest.entry,
1348
+ capabilities: [...validated.manifest.capabilities],
1349
+ installed_at: existingManagedEntry?.installed_at ?? now,
1350
+ updated_at: now,
1351
+ source: sourceRecord,
1352
+ });
1353
+ await writeManagedExtensionState(resolvedRoots.selected_root, managedState);
1354
+ const activationChanged = ensureActivated(settings, validated.manifest.name);
1355
+ if (activationChanged) {
1356
+ await writeSettings(resolvedRoots.settings_root, settings, "settings:write");
1357
+ }
1358
+ return withResult({
1359
+ extension: {
1360
+ name: validated.manifest.name,
1361
+ version: validated.manifest.version,
1362
+ entry: validated.manifest.entry,
1363
+ capabilities: validated.manifest.capabilities,
1364
+ directory: destinationDirectoryName,
1365
+ },
1366
+ source: sourceRecord,
1367
+ destination_path: destinationDirectory,
1368
+ overwritten: destinationExists && !installInPlace,
1369
+ installed_in_place: installInPlace,
1370
+ activated: true,
1371
+ settings_changed: activationChanged,
1372
+ });
1373
+ }
1374
+ finally {
1375
+ if (resolvedSource.cleanup) {
1376
+ await resolvedSource.cleanup();
1377
+ }
1378
+ }
1379
+ }
1380
+ if (action === "adopt-all") {
1381
+ if (normalizedTarget !== undefined) {
1382
+ throw new PmCliError('Action "adopt-all" does not accept a target argument.', EXIT_CODE.USAGE);
1383
+ }
1384
+ const githubOption = resolveGithubOption(options);
1385
+ if (githubOption !== undefined || (typeof options.ref === "string" && options.ref.trim().length > 0)) {
1386
+ throw new PmCliError('Action "adopt-all" does not accept --gh/--github/--ref options.', EXIT_CODE.USAGE);
1387
+ }
1388
+ const settings = await readSettings(resolvedRoots.settings_root);
1389
+ const managedStateRead = await readManagedExtensionState(resolvedRoots.selected_root);
1390
+ warnings.push(...managedStateRead.warnings);
1391
+ const installed = await listInstalledExtensions(resolvedRoots.selected_root, scope, settings, managedStateRead.state);
1392
+ warnings.push(...installed.warnings);
1393
+ const adoption = await adoptUnmanagedExtensions(resolvedRoots.selected_root, scope, installed.extensions, managedStateRead.state);
1394
+ const refreshedInstalled = await listInstalledExtensions(resolvedRoots.selected_root, scope, settings, adoption.state);
1395
+ warnings.push(...refreshedInstalled.warnings);
1396
+ const triage = buildExtensionTriageSummary(scope, warnings, refreshedInstalled.extensions);
1397
+ warnings.push(...triage.warnings);
1398
+ const adoptedDetails = adoption.adopted_entries.map((entry) => {
1399
+ const refreshedEntry = refreshedInstalled.extensions.find((candidate) => normalizeExtensionNameForMatch(candidate.name) === normalizeExtensionNameForMatch(entry.name) &&
1400
+ normalizeExtensionNameForMatch(candidate.directory) === normalizeExtensionNameForMatch(entry.directory)) ??
1401
+ refreshedInstalled.extensions.find((candidate) => normalizeExtensionNameForMatch(candidate.directory) === normalizeExtensionNameForMatch(entry.directory));
1402
+ return {
1403
+ ...entry,
1404
+ update_check_status: refreshedEntry?.update_check_status ?? null,
1405
+ update_check_reason: refreshedEntry?.update_check_reason ?? null,
1406
+ };
1407
+ });
1408
+ return withResult({
1409
+ adopted_all: adoptedDetails.length > 0,
1410
+ adopted_count: adoptedDetails.length,
1411
+ already_managed_count: adoption.already_managed_count,
1412
+ extensions: adoptedDetails,
1413
+ triage,
1414
+ warning_codes: triage.warning_codes,
1415
+ update_health_partial: triage.update_health_partial,
1416
+ update_health_coverage: triage.update_health_coverage,
1417
+ });
1418
+ }
1419
+ if (action === "adopt") {
1420
+ const extensionTarget = requireTarget(normalizedTarget, action);
1421
+ const githubOption = resolveGithubOption(options);
1422
+ const settings = await readSettings(resolvedRoots.settings_root);
1423
+ const managedStateRead = await readManagedExtensionState(resolvedRoots.selected_root);
1424
+ warnings.push(...managedStateRead.warnings);
1425
+ const installed = await listInstalledExtensions(resolvedRoots.selected_root, scope, settings, managedStateRead.state);
1426
+ warnings.push(...installed.warnings);
1427
+ const candidate = await resolveInstalledExtensionCandidate(installed.extensions, extensionTarget);
1428
+ if (!candidate) {
1429
+ throw new PmCliError(`Installed extension "${extensionTarget}" was not found in ${scope} scope.`, EXIT_CODE.NOT_FOUND);
1430
+ }
1431
+ if (candidate.managed) {
1432
+ return withResult({
1433
+ adopted: false,
1434
+ already_managed: true,
1435
+ extension: {
1436
+ name: candidate.name,
1437
+ directory: candidate.directory,
1438
+ },
1439
+ });
1440
+ }
1441
+ const extensionDirectory = path.join(resolvedRoots.selected_root, candidate.directory);
1442
+ const validated = await validateExtensionDirectory(extensionDirectory);
1443
+ const now = nowIso();
1444
+ const sourceRecord = githubOption === undefined
1445
+ ? {
1446
+ kind: "local",
1447
+ input: extensionTarget,
1448
+ location: extensionDirectory,
1449
+ }
1450
+ : (() => {
1451
+ const parsed = parseExtensionInstallSource(githubOption, {
1452
+ forceGithub: true,
1453
+ ref: options.ref,
1454
+ });
1455
+ if (parsed.kind !== "github") {
1456
+ throw new PmCliError(`Invalid GitHub shorthand "${githubOption}".`, EXIT_CODE.USAGE);
1457
+ }
1458
+ return {
1459
+ kind: "github",
1460
+ input: parsed.input,
1461
+ location: parsed.subpath ?? ".",
1462
+ repository: parsed.repository,
1463
+ owner: parsed.owner,
1464
+ repo: parsed.repo,
1465
+ ref: parsed.ref,
1466
+ subpath: parsed.subpath,
1467
+ };
1468
+ })();
1469
+ const managedState = upsertManagedEntry(managedStateRead.state, {
1470
+ name: validated.manifest.name,
1471
+ directory: candidate.directory,
1472
+ scope,
1473
+ manifest_version: validated.manifest.version,
1474
+ manifest_entry: validated.manifest.entry,
1475
+ capabilities: [...validated.manifest.capabilities],
1476
+ installed_at: now,
1477
+ updated_at: now,
1478
+ source: sourceRecord,
1479
+ });
1480
+ await writeManagedExtensionState(resolvedRoots.selected_root, managedState);
1481
+ const refreshedInstalled = await listInstalledExtensions(resolvedRoots.selected_root, scope, settings, managedState);
1482
+ warnings.push(...refreshedInstalled.warnings);
1483
+ const refreshedEntry = refreshedInstalled.extensions.find((entry) => normalizeExtensionNameForMatch(entry.name) === normalizeExtensionNameForMatch(validated.manifest.name)) ??
1484
+ refreshedInstalled.extensions.find((entry) => normalizeExtensionNameForMatch(entry.directory) === normalizeExtensionNameForMatch(candidate.directory));
1485
+ return withResult({
1486
+ adopted: true,
1487
+ extension: {
1488
+ name: validated.manifest.name,
1489
+ directory: candidate.directory,
1490
+ version: validated.manifest.version,
1491
+ entry: validated.manifest.entry,
1492
+ },
1493
+ source: sourceRecord,
1494
+ update_check_status: refreshedEntry?.update_check_status ?? null,
1495
+ update_check_reason: refreshedEntry?.update_check_reason ?? null,
1496
+ });
1497
+ }
1498
+ if (action === "uninstall") {
1499
+ const extensionTarget = requireTarget(normalizedTarget, action);
1500
+ const settings = await readSettings(resolvedRoots.settings_root);
1501
+ const managedStateRead = await readManagedExtensionState(resolvedRoots.selected_root);
1502
+ warnings.push(...managedStateRead.warnings);
1503
+ const installed = await listInstalledExtensions(resolvedRoots.selected_root, scope, settings, managedStateRead.state);
1504
+ warnings.push(...installed.warnings);
1505
+ const candidate = await resolveInstalledExtensionCandidate(installed.extensions, extensionTarget);
1506
+ if (!candidate) {
1507
+ throw new PmCliError(`Installed extension "${extensionTarget}" was not found in ${scope} scope.`, EXIT_CODE.NOT_FOUND);
1508
+ }
1509
+ const destinationDirectory = path.join(resolvedRoots.selected_root, candidate.directory);
1510
+ await fs.rm(destinationDirectory, { recursive: true, force: true });
1511
+ const updatedState = {
1512
+ ...managedStateRead.state,
1513
+ updated_at: nowIso(),
1514
+ entries: managedStateRead.state.entries.filter((entry) => normalizeExtensionNameForMatch(entry.name) !== normalizeExtensionNameForMatch(candidate.name) &&
1515
+ normalizeExtensionNameForMatch(entry.directory) !== normalizeExtensionNameForMatch(candidate.directory)),
1516
+ };
1517
+ await writeManagedExtensionState(resolvedRoots.selected_root, updatedState);
1518
+ const stateChanged = clearExtensionState(settings, candidate.name);
1519
+ if (stateChanged) {
1520
+ await writeSettings(resolvedRoots.settings_root, settings, "settings:write");
1521
+ }
1522
+ return withResult({
1523
+ removed: true,
1524
+ extension: {
1525
+ name: candidate.name,
1526
+ directory: candidate.directory,
1527
+ },
1528
+ destination_path: destinationDirectory,
1529
+ settings_changed: stateChanged,
1530
+ });
1531
+ }
1532
+ if (action === "activate" || action === "deactivate") {
1533
+ const extensionTarget = requireTarget(normalizedTarget, action);
1534
+ const settings = await readSettings(resolvedRoots.settings_root);
1535
+ const managedStateRead = await readManagedExtensionState(resolvedRoots.selected_root);
1536
+ warnings.push(...managedStateRead.warnings);
1537
+ const installed = await listInstalledExtensions(resolvedRoots.selected_root, scope, settings, managedStateRead.state);
1538
+ warnings.push(...installed.warnings);
1539
+ const candidate = await resolveInstalledExtensionCandidate(installed.extensions, extensionTarget);
1540
+ if (!candidate) {
1541
+ throw new PmCliError(`Installed extension "${extensionTarget}" was not found in ${scope} scope.`, EXIT_CODE.NOT_FOUND);
1542
+ }
1543
+ const settingsChanged = action === "activate" ? ensureActivated(settings, candidate.name) : ensureDeactivated(settings, candidate.name);
1544
+ if (settingsChanged) {
1545
+ await writeSettings(resolvedRoots.settings_root, settings, "settings:write");
1546
+ }
1547
+ return withResult({
1548
+ extension: {
1549
+ name: candidate.name,
1550
+ directory: candidate.directory,
1551
+ },
1552
+ active: action === "activate",
1553
+ settings_changed: settingsChanged,
1554
+ settings: {
1555
+ enabled: [...settings.extensions.enabled],
1556
+ disabled: [...settings.extensions.disabled],
1557
+ },
1558
+ });
1559
+ }
1560
+ if (action === "doctor") {
1561
+ if (normalizedTarget && normalizedTarget.trim().length > 0) {
1562
+ throw new PmCliError('Action "doctor" does not accept a target argument.', EXIT_CODE.USAGE);
1563
+ }
1564
+ const detailMode = parseDoctorDetailMode(options.detail);
1565
+ const includeTrace = options.trace === true;
1566
+ if (includeTrace && detailMode !== "deep") {
1567
+ throw new PmCliError("--trace requires --detail deep with --doctor.", EXIT_CODE.USAGE);
1568
+ }
1569
+ const settings = await readSettings(resolvedRoots.settings_root);
1570
+ const managedStateRead = await readManagedExtensionState(resolvedRoots.selected_root);
1571
+ warnings.push(...managedStateRead.warnings);
1572
+ const installed = await listInstalledExtensions(resolvedRoots.selected_root, scope, settings, managedStateRead.state);
1573
+ warnings.push(...installed.warnings);
1574
+ let managedState = managedStateRead.state;
1575
+ const managedStateFix = options.fixManagedState === true
1576
+ ? await adoptUnmanagedExtensions(resolvedRoots.selected_root, scope, installed.extensions, managedStateRead.state)
1577
+ : null;
1578
+ if (managedStateFix) {
1579
+ managedState = managedStateFix.state;
1580
+ }
1581
+ const refreshedInstalled = await listInstalledExtensions(resolvedRoots.selected_root, scope, settings, managedState);
1582
+ warnings.push(...refreshedInstalled.warnings);
1583
+ const loadResult = await loadExtensions({
1584
+ pmRoot: resolvedRoots.pm_root,
1585
+ settings,
1586
+ cwd: process.cwd(),
1587
+ noExtensions: global.noExtensions === true,
1588
+ });
1589
+ const activationResult = await activateExtensions({
1590
+ ...loadResult,
1591
+ loaded: loadResult.loaded,
1592
+ });
1593
+ warnings.push(...loadResult.warnings);
1594
+ warnings.push(...classifyDoctorLoadFailureWarnings(loadResult.failed));
1595
+ warnings.push(...activationResult.warnings);
1596
+ const runtimeInstalledExtensions = applyDoctorRuntimeActivationState(refreshedInstalled.extensions, loadResult, activationResult);
1597
+ const doctorConsistency = buildDoctorConsistencySummary(scope, runtimeInstalledExtensions, loadResult.loaded.map((entry) => ({ layer: entry.layer, name: entry.name })), loadResult.failed.map((entry) => ({ name: entry.name })), loadResult.disabled_by_flag);
1598
+ warnings.push(...doctorConsistency.warnings);
1599
+ const updateCheckWarnings = runtimeInstalledExtensions
1600
+ .filter((entry) => entry.update_check_status === "failed")
1601
+ .map((entry) => `extension_update_check_failed:${entry.name}`);
1602
+ warnings.push(...updateCheckWarnings);
1603
+ const triage = buildExtensionTriageSummary(scope, warnings, runtimeInstalledExtensions);
1604
+ warnings.push(...triage.warnings);
1605
+ const normalizedWarnings = [...triage.warnings];
1606
+ const capabilityGuidance = collectUnknownCapabilityGuidance(normalizedWarnings);
1607
+ const capabilityContract = buildCapabilityContractMetadata();
1608
+ const warningCodes = triage.warning_codes;
1609
+ const remediation = [
1610
+ ...new Set([
1611
+ ...triage.remediation,
1612
+ ...(loadResult.failed.length > 0
1613
+ ? ["Run pm extension --explore --project and pm extension --explore --global to inspect load failures."]
1614
+ : []),
1615
+ ...(activationResult.failed.length > 0
1616
+ ? ["Review activation failures in pm extension --doctor --detail deep output."]
1617
+ : []),
1618
+ ...(managedStateFix && managedStateFix.adopted_entries.length > 0
1619
+ ? [`Managed-state fix adopted ${managedStateFix.adopted_entries.length} extension(s).`]
1620
+ : []),
1621
+ ].map((entry) => entry.trim()).filter((entry) => entry.length > 0)),
1622
+ ];
1623
+ const summary = {
1624
+ status: triage.status,
1625
+ scope,
1626
+ warning_count: triage.warning_count,
1627
+ warning_codes: warningCodes,
1628
+ total_extensions: runtimeInstalledExtensions.length,
1629
+ managed_total: runtimeInstalledExtensions.filter((entry) => entry.managed).length,
1630
+ enabled_total: runtimeInstalledExtensions.filter((entry) => entry.enabled).length,
1631
+ active_total: runtimeInstalledExtensions.filter((entry) => entry.active).length,
1632
+ unmanaged_loaded_extension_count: triage.unmanaged_loaded_extension_count,
1633
+ unmanaged_action_required_extension_count: triage.unmanaged_action_required_extension_count,
1634
+ unmanaged_expected_extension_count: triage.unmanaged_expected_extension_count,
1635
+ runtime_active_total: runtimeInstalledExtensions.filter((entry) => entry.runtime_active === true).length,
1636
+ activation_status_totals: {
1637
+ ok: runtimeInstalledExtensions.filter((entry) => entry.activation_status === "ok").length,
1638
+ failed: runtimeInstalledExtensions.filter((entry) => entry.activation_status === "failed").length,
1639
+ not_loaded: runtimeInstalledExtensions.filter((entry) => entry.activation_status === "not_loaded").length,
1640
+ unknown: runtimeInstalledExtensions.filter((entry) => entry.activation_status === "unknown").length,
1641
+ },
1642
+ unknown_capability_count: capabilityGuidance.length,
1643
+ capability_contract_version: capabilityContract.version,
1644
+ update_available_total: runtimeInstalledExtensions.filter((entry) => entry.update_available === true).length,
1645
+ update_health_coverage: triage.update_health_coverage,
1646
+ update_health_partial: triage.update_health_partial,
1647
+ update_check_failed_total: runtimeInstalledExtensions.filter((entry) => entry.update_check_status === "failed").length,
1648
+ load_failure_count: loadResult.failed.length,
1649
+ activation_failure_count: activationResult.failed.length,
1650
+ blocking_failure_count: loadResult.failed.length + activationResult.failed.length,
1651
+ has_blocking_failures: loadResult.failed.length + activationResult.failed.length > 0,
1652
+ consistency_warning_count: doctorConsistency.warnings.length,
1653
+ trace_enabled: includeTrace,
1654
+ remediation,
1655
+ };
1656
+ const managedStateFixSummary = managedStateFix
1657
+ ? {
1658
+ requested: true,
1659
+ applied: managedStateFix.adopted_entries.length > 0,
1660
+ adopted_count: managedStateFix.adopted_entries.length,
1661
+ already_managed_count: managedStateFix.already_managed_count,
1662
+ adopted_extensions: managedStateFix.adopted_entries.map((entry) => entry.name),
1663
+ }
1664
+ : {
1665
+ requested: false,
1666
+ applied: false,
1667
+ adopted_count: 0,
1668
+ already_managed_count: refreshedInstalled.extensions.filter((entry) => entry.managed).length,
1669
+ adopted_extensions: [],
1670
+ };
1671
+ const details = {
1672
+ mode: detailMode,
1673
+ summary,
1674
+ triage,
1675
+ trace_enabled: includeTrace,
1676
+ capability_contract: capabilityContract,
1677
+ capability_guidance: capabilityGuidance,
1678
+ managed_state_fix: managedStateFixSummary,
1679
+ };
1680
+ if (detailMode === "deep") {
1681
+ const activationFailedDetails = includeTrace
1682
+ ? activationResult.failed
1683
+ : activationResult.failed.map((entry) => {
1684
+ const { trace: _trace, ...rest } = entry;
1685
+ return rest;
1686
+ });
1687
+ details.deep = {
1688
+ warnings: normalizedWarnings,
1689
+ warning_codes: warningCodes,
1690
+ capability_contract: capabilityContract,
1691
+ capability_guidance: capabilityGuidance,
1692
+ trace_enabled: includeTrace,
1693
+ managed_state: {
1694
+ path: managedStateRead.path,
1695
+ count: managedState.entries.length,
1696
+ entries: managedState.entries,
1697
+ },
1698
+ installed_extensions: runtimeInstalledExtensions,
1699
+ load: {
1700
+ roots: loadResult.roots,
1701
+ warnings: loadResult.warnings,
1702
+ failed: loadResult.failed,
1703
+ loaded: loadResult.loaded.map((entry) => ({
1704
+ layer: entry.layer,
1705
+ directory: entry.directory,
1706
+ name: entry.name,
1707
+ version: entry.version,
1708
+ entry: entry.entry,
1709
+ priority: entry.priority,
1710
+ })),
1711
+ },
1712
+ activation: {
1713
+ failed: activationFailedDetails,
1714
+ warnings: activationResult.warnings,
1715
+ hook_counts: activationResult.hook_counts,
1716
+ registration_counts: activationResult.registration_counts,
1717
+ parser_override_count: activationResult.parser_override_count,
1718
+ preflight_override_count: activationResult.preflight_override_count,
1719
+ service_override_count: activationResult.service_override_count,
1720
+ renderer_override_count: activationResult.renderer_override_count,
1721
+ },
1722
+ consistency: doctorConsistency.summary,
1723
+ };
1724
+ if (includeTrace) {
1725
+ details.deep.trace = {
1726
+ activation_failures: activationResult.failed
1727
+ .filter((entry) => entry.trace !== undefined)
1728
+ .map((entry) => ({
1729
+ layer: entry.layer,
1730
+ name: entry.name,
1731
+ entry_path: entry.entry_path,
1732
+ error: entry.error,
1733
+ method: entry.trace?.method,
1734
+ command: entry.trace?.command,
1735
+ registration_index: entry.trace?.registration_index,
1736
+ expected_schema: entry.trace?.expected_schema,
1737
+ hint: entry.trace?.hint,
1738
+ received: entry.trace?.received,
1739
+ })),
1740
+ };
1741
+ }
1742
+ }
1743
+ return withResult(details);
1744
+ }
1745
+ if (action === "explore" || action === "manage") {
1746
+ const settings = await readSettings(resolvedRoots.settings_root);
1747
+ const managedStateRead = await readManagedExtensionState(resolvedRoots.selected_root);
1748
+ warnings.push(...managedStateRead.warnings);
1749
+ const installed = await listInstalledExtensions(resolvedRoots.selected_root, scope, settings, managedStateRead.state);
1750
+ warnings.push(...installed.warnings);
1751
+ let managedState = managedStateRead.state;
1752
+ const managedStateFix = action === "manage" && options.fixManagedState === true
1753
+ ? await adoptUnmanagedExtensions(resolvedRoots.selected_root, scope, installed.extensions, managedStateRead.state)
1754
+ : null;
1755
+ if (managedStateFix) {
1756
+ managedState = managedStateFix.state;
1757
+ }
1758
+ if (action === "manage") {
1759
+ const updates = await Promise.all(managedState.entries.map(async (entry) => {
1760
+ if (entry.source.kind !== "github") {
1761
+ return entry;
1762
+ }
1763
+ const updateStatus = await checkGithubUpdate(entry.source);
1764
+ return {
1765
+ ...entry,
1766
+ last_update_check_at: updateStatus.checked_at,
1767
+ last_update_remote_commit: updateStatus.remote_commit,
1768
+ update_available: updateStatus.available,
1769
+ update_error: updateStatus.error,
1770
+ };
1771
+ }));
1772
+ managedState = {
1773
+ ...managedState,
1774
+ updated_at: nowIso(),
1775
+ entries: sortManagedEntries(updates),
1776
+ };
1777
+ await writeManagedExtensionState(resolvedRoots.selected_root, managedState);
1778
+ }
1779
+ const refreshedInstalled = await listInstalledExtensions(resolvedRoots.selected_root, scope, settings, managedState);
1780
+ warnings.push(...refreshedInstalled.warnings);
1781
+ if (action === "manage") {
1782
+ const updateWarnings = refreshedInstalled.extensions
1783
+ .filter((entry) => entry.update_check_status === "failed")
1784
+ .map((entry) => `extension_update_check_failed:${entry.name}`);
1785
+ warnings.push(...updateWarnings);
1786
+ }
1787
+ let runtimeProbeSummary;
1788
+ let runtimeInstalledExtensions = refreshedInstalled.extensions;
1789
+ if (action === "manage" && options.runtimeProbe === true) {
1790
+ const loadResult = await loadExtensions({
1791
+ pmRoot: resolvedRoots.pm_root,
1792
+ settings,
1793
+ cwd: process.cwd(),
1794
+ noExtensions: global.noExtensions === true,
1795
+ });
1796
+ const activationResult = await activateExtensions({
1797
+ ...loadResult,
1798
+ loaded: loadResult.loaded,
1799
+ });
1800
+ warnings.push(...loadResult.warnings);
1801
+ warnings.push(...activationResult.warnings);
1802
+ runtimeInstalledExtensions = applyDoctorRuntimeActivationState(refreshedInstalled.extensions, loadResult, activationResult);
1803
+ runtimeProbeSummary = {
1804
+ requested: true,
1805
+ executed: true,
1806
+ load_failure_count: loadResult.failed.length,
1807
+ activation_failure_count: activationResult.failed.length,
1808
+ warning_count: [...new Set([...loadResult.warnings, ...activationResult.warnings])].length,
1809
+ };
1810
+ }
1811
+ else if (action === "manage") {
1812
+ runtimeProbeSummary = {
1813
+ requested: options.runtimeProbe === true,
1814
+ executed: false,
1815
+ };
1816
+ }
1817
+ const triage = buildExtensionTriageSummary(scope, warnings, runtimeInstalledExtensions);
1818
+ warnings.push(...triage.warnings);
1819
+ const details = {
1820
+ total: runtimeInstalledExtensions.length,
1821
+ managed_total: runtimeInstalledExtensions.filter((entry) => entry.managed).length,
1822
+ enabled_total: runtimeInstalledExtensions.filter((entry) => entry.enabled).length,
1823
+ active_total: runtimeInstalledExtensions.filter((entry) => entry.active).length,
1824
+ extensions: runtimeInstalledExtensions,
1825
+ triage,
1826
+ };
1827
+ if (action === "manage") {
1828
+ details.runtime_probe = runtimeProbeSummary;
1829
+ details.managed_state_fix =
1830
+ managedStateFix !== null
1831
+ ? {
1832
+ requested: true,
1833
+ applied: managedStateFix.adopted_entries.length > 0,
1834
+ adopted_count: managedStateFix.adopted_entries.length,
1835
+ adopted_extensions: managedStateFix.adopted_entries.map((entry) => entry.name),
1836
+ already_managed_count: managedStateFix.already_managed_count,
1837
+ }
1838
+ : {
1839
+ requested: false,
1840
+ applied: false,
1841
+ adopted_count: 0,
1842
+ adopted_extensions: [],
1843
+ already_managed_count: runtimeInstalledExtensions.filter((entry) => entry.managed).length,
1844
+ };
1845
+ }
1846
+ return withResult(details);
1847
+ }
1848
+ throw new PmCliError(`Unsupported extension action "${action}".`, EXIT_CODE.USAGE);
1849
+ }
1850
+ //# sourceMappingURL=extension.js.map