@unbrained/pm-cli 2026.9.7 → 2026.9.9

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 (746) hide show
  1. package/.agents/skills/pm-developer/SKILL.md +12 -4
  2. package/.agents/skills/pm-developer/references/MULTI_AGENT_MERGE.md +19 -10
  3. package/.agents/skills/pm-extensions/SKILL.md +6 -0
  4. package/.agents/skills/pm-sdk/SKILL.md +11 -2
  5. package/.agents/skills/pm-user/SKILL.md +7 -2
  6. package/.agents/skills/pm-user/references/ECOSYSTEM_PLANNING.md +95 -0
  7. package/.claude-plugin/marketplace.json +2 -2
  8. package/CHANGELOG.md +231 -182
  9. package/dist/cli/after-command-context-usage.js +1 -48
  10. package/dist/cli/argv-utils.js +1 -40
  11. package/dist/cli/bootstrap-args.js +1 -9
  12. package/dist/cli/bundle-integrity.js +2 -110
  13. package/dist/cli/commander-usage.js +11 -771
  14. package/dist/cli/commands/activity.js +1 -9
  15. package/dist/cli/commands/aggregate.js +1 -5
  16. package/dist/cli/commands/append.js +1 -5
  17. package/dist/cli/commands/claim.js +1 -5
  18. package/dist/cli/commands/close-many.js +1 -5
  19. package/dist/cli/commands/close.js +1 -9
  20. package/dist/cli/commands/comments.js +1 -5
  21. package/dist/cli/commands/config.js +1 -9
  22. package/dist/cli/commands/context.js +1 -9
  23. package/dist/cli/commands/contracts.js +1 -5
  24. package/dist/cli/commands/copy.js +1 -9
  25. package/dist/cli/commands/create.js +1 -9
  26. package/dist/cli/commands/delete.js +1 -9
  27. package/dist/cli/commands/deps.js +1 -9
  28. package/dist/cli/commands/docs.js +1 -9
  29. package/dist/cli/commands/duplicates.js +1 -9
  30. package/dist/cli/commands/eval.js +1 -9
  31. package/dist/cli/commands/extension/describe.js +1 -5
  32. package/dist/cli/commands/extension/scaffold.js +1 -5
  33. package/dist/cli/commands/extension/shared.js +1 -5
  34. package/dist/cli/commands/extension.js +1 -9
  35. package/dist/cli/commands/files.js +1 -9
  36. package/dist/cli/commands/focus.js +1 -9
  37. package/dist/cli/commands/gc.js +1 -9
  38. package/dist/cli/commands/get.js +1 -9
  39. package/dist/cli/commands/graph.js +1 -10
  40. package/dist/cli/commands/health.js +1 -9
  41. package/dist/cli/commands/history-compact.js +1 -10
  42. package/dist/cli/commands/history-redact.js +1 -11
  43. package/dist/cli/commands/history-repair.js +1 -10
  44. package/dist/cli/commands/history.js +1 -5
  45. package/dist/cli/commands/index.js +1 -58
  46. package/dist/cli/commands/init-agent-guidance.js +1 -9
  47. package/dist/cli/commands/init.js +1 -9
  48. package/dist/cli/commands/learnings.js +1 -5
  49. package/dist/cli/commands/lifecycle-transitions.js +1 -5
  50. package/dist/cli/commands/list.js +1 -9
  51. package/dist/cli/commands/merge.js +1 -11
  52. package/dist/cli/commands/next.js +1 -9
  53. package/dist/cli/commands/notes.js +1 -5
  54. package/dist/cli/commands/plan.js +1 -9
  55. package/dist/cli/commands/profile.js +1 -9
  56. package/dist/cli/commands/restore.js +1 -9
  57. package/dist/cli/commands/scheduling-shortcuts.js +1 -9
  58. package/dist/cli/commands/schema.js +1 -10
  59. package/dist/cli/commands/search.js +1 -9
  60. package/dist/cli/commands/stats.js +1 -9
  61. package/dist/cli/commands/telemetry.js +1 -9
  62. package/dist/cli/commands/test-all.js +1 -9
  63. package/dist/cli/commands/test-runs.js +1 -9
  64. package/dist/cli/commands/test.js +1 -9
  65. package/dist/cli/commands/update-many.js +1 -5
  66. package/dist/cli/commands/update.js +1 -9
  67. package/dist/cli/commands/upgrade.js +1 -5
  68. package/dist/cli/commands/validate.js +1 -15
  69. package/dist/cli/context-intent-invocation.js +1 -20
  70. package/dist/cli/description-stdin.js +1 -17
  71. package/dist/cli/error-guidance.js +1 -1353
  72. package/dist/cli/extension-command-help.js +5 -636
  73. package/dist/cli/extension-command-options.js +1 -427
  74. package/dist/cli/extension-recovery.js +1 -126
  75. package/dist/cli/help-content.js +2 -1001
  76. package/dist/cli/help-json-payload.js +4 -570
  77. package/dist/cli/main.d.ts +4 -0
  78. package/dist/cli/main.js +5 -2573
  79. package/dist/cli/migration-gates.js +1 -289
  80. package/dist/cli/public.js +1 -20
  81. package/dist/cli/register-annotations.js +1 -270
  82. package/dist/cli/register-assurance.js +1 -62
  83. package/dist/cli/register-files-lookup.js +1 -56
  84. package/dist/cli/register-history-activity.d.ts +8 -0
  85. package/dist/cli/register-history-activity.js +2 -0
  86. package/dist/cli/register-history-author.js +1 -38
  87. package/dist/cli/register-history-maintenance.d.ts +9 -0
  88. package/dist/cli/register-history-maintenance.js +2 -0
  89. package/dist/cli/register-list-query.js +10 -943
  90. package/dist/cli/register-mutation-options.js +1 -9
  91. package/dist/cli/register-mutation.d.ts +0 -5
  92. package/dist/cli/register-mutation.js +19 -1893
  93. package/dist/cli/register-operations.js +1 -920
  94. package/dist/cli/register-setup.js +1 -716
  95. package/dist/cli/register-structured-mutation.js +1 -196
  96. package/dist/cli/registration-helpers.js +1 -5
  97. package/dist/cli/runtime-compatibility-boundary.js +4 -42
  98. package/dist/cli/schema-registration-helpers.js +1 -73
  99. package/dist/cli/search-refresh.js +1 -13
  100. package/dist/cli/stats-analytics-json.js +1 -68
  101. package/dist/cli/telemetry-flush.js +1 -10
  102. package/dist/cli-bundle/bundle-manifest.json +164 -156
  103. package/dist/cli-bundle/chunks/{chunk-NPN5I67J.js → chunk-3IPRM75M.js} +3 -3
  104. package/dist/cli-bundle/chunks/chunk-4VL5P22L.js +2 -0
  105. package/dist/cli-bundle/chunks/chunk-AAFBV3U3.js +29 -0
  106. package/dist/cli-bundle/chunks/chunk-D3YVILJJ.js +20 -0
  107. package/dist/cli-bundle/chunks/{chunk-WYRZZ5ZE.js → chunk-DEZPZOQ7.js} +2 -2
  108. package/dist/cli-bundle/chunks/chunk-ECBLPN7B.js +3 -0
  109. package/dist/cli-bundle/chunks/chunk-J6ERHBV2.js +235 -0
  110. package/dist/cli-bundle/chunks/chunk-JUNJ5EKI.js +2 -0
  111. package/dist/cli-bundle/chunks/chunk-L5RCDKQ2.js +2 -0
  112. package/dist/cli-bundle/chunks/chunk-NSAPNEYH.js +2 -0
  113. package/dist/cli-bundle/chunks/chunk-OHNLSGGV.js +79 -0
  114. package/dist/cli-bundle/chunks/chunk-WQKMSFBV.js +5 -0
  115. package/dist/cli-bundle/chunks/{chunk-BAX2BYUW.js → chunk-XZ6T7YPP.js} +256 -150
  116. package/dist/cli-bundle/chunks/{chunk-JT6XF6GE.js → chunk-Z3XVXUVF.js} +4 -4
  117. package/dist/cli-bundle/chunks/register-list-query-UIHRCS6D.js +11 -0
  118. package/dist/cli-bundle/chunks/register-mutation-WHWMGRVX.js +20 -0
  119. package/dist/cli-bundle/chunks/register-operations-64YZYTC5.js +2 -0
  120. package/dist/cli-bundle/chunks/register-setup-PERI45EM.js +2 -0
  121. package/dist/cli-bundle/focused-chunks/chunk-2W4XIYOB.js +2 -0
  122. package/dist/cli-bundle/focused-chunks/chunk-2WSBXIGF.js +12 -0
  123. package/dist/cli-bundle/focused-chunks/chunk-5FHTCOHS.js +3 -0
  124. package/dist/cli-bundle/focused-chunks/chunk-7N6DOREF.js +26 -0
  125. package/dist/cli-bundle/focused-chunks/{chunk-3BZEOXLT.js → chunk-7PC4R5QB.js} +2 -2
  126. package/dist/cli-bundle/focused-chunks/chunk-BUCJUZVG.js +6 -0
  127. package/dist/cli-bundle/focused-chunks/chunk-FE3BABLJ.js +2 -0
  128. package/dist/cli-bundle/focused-chunks/chunk-IWITF53W.js +2 -0
  129. package/dist/cli-bundle/focused-chunks/chunk-OQUGRHKV.js +10 -0
  130. package/dist/cli-bundle/focused-chunks/{chunk-M6PBRFSH.js → chunk-PNKIANFZ.js} +2 -2
  131. package/dist/cli-bundle/focused-chunks/{chunk-LIJIA4TY.js → chunk-QQQ4SAUO.js} +3 -3
  132. package/dist/cli-bundle/focused-chunks/chunk-TTYXF5YD.js +177 -0
  133. package/dist/cli-bundle/focused-chunks/chunk-XNR3DM6J.js +2 -0
  134. package/dist/cli-bundle/focused-chunks/chunk-XSS3APUF.js +9 -0
  135. package/dist/cli-bundle/focused-chunks/chunk-YCDQ2N23.js +8 -0
  136. package/dist/cli-bundle/focused-chunks/chunk-YJUXGS57.js +20 -0
  137. package/dist/cli-bundle/focused-chunks/chunk-ZTCE4UO6.js +58 -0
  138. package/dist/cli-bundle/main.js +20 -20
  139. package/dist/cli-bundle/sdk-authoring.js +1 -1
  140. package/dist/cli-bundle/sdk-contracts.js +2 -2
  141. package/dist/cli-bundle/sdk-core.js +37 -37
  142. package/dist/cli-bundle/sdk-governance.js +1 -1
  143. package/dist/cli-bundle/sdk-graph.js +1 -1
  144. package/dist/cli-bundle/sdk-merge.js +42 -42
  145. package/dist/cli-bundle/sdk-query.js +1 -1
  146. package/dist/cli-bundle/sdk-runtime.js +1 -1
  147. package/dist/cli-bundle/sdk-testing.js +1 -1
  148. package/dist/cli-bundle/sdk.js +11 -11
  149. package/dist/cli.js +1 -104
  150. package/dist/core/checkpoint/checkpoint-gc.js +1 -169
  151. package/dist/core/checkpoint/mutation-checkpoint.js +2 -133
  152. package/dist/core/config/nested-settings.js +1 -534
  153. package/dist/core/config/positional-value.js +1 -109
  154. package/dist/core/diagnostics/remediation.js +1 -506
  155. package/dist/core/extensions/activation-summary-markdown.js +2 -145
  156. package/dist/core/extensions/activation-summary.js +1 -92
  157. package/dist/core/extensions/activation-transaction.js +1 -46
  158. package/dist/core/extensions/capability-usage.js +1 -173
  159. package/dist/core/extensions/command-hook-context.js +1 -13
  160. package/dist/core/extensions/command-metadata-contract.js +1 -22
  161. package/dist/core/extensions/command-visibility-tier.js +1 -29
  162. package/dist/core/extensions/contribution-inventory.js +1 -173
  163. package/dist/core/extensions/exporter-output-contract.js +1 -66
  164. package/dist/core/extensions/extension-candidate.js +1 -18
  165. package/dist/core/extensions/extension-capability-aliases.js +1 -158
  166. package/dist/core/extensions/extension-hook-runtime.js +1 -699
  167. package/dist/core/extensions/extension-policy.js +1 -599
  168. package/dist/core/extensions/extension-registries.js +1 -62
  169. package/dist/core/extensions/extension-runtime-helpers.js +1 -33
  170. package/dist/core/extensions/extension-types.js +1 -102
  171. package/dist/core/extensions/flag-definition-validation.js +1 -52
  172. package/dist/core/extensions/flag-value-types.js +1 -64
  173. package/dist/core/extensions/index.js +1 -526
  174. package/dist/core/extensions/item-field-types.js +1 -39
  175. package/dist/core/extensions/item-fields.js +1 -261
  176. package/dist/core/extensions/loader.js +1 -2223
  177. package/dist/core/extensions/manifest-schema.js +1 -69
  178. package/dist/core/extensions/module-graph-snapshot.js +1 -33
  179. package/dist/core/extensions/preflight-ownership.js +1 -41
  180. package/dist/core/extensions/registration-validation.js +1 -82
  181. package/dist/core/extensions/registration-values.js +1 -209
  182. package/dist/core/extensions/reserved-host-flags.js +1 -36
  183. package/dist/core/extensions/runtime-registrations.js +1 -67
  184. package/dist/core/extensions/service-ownership.js +1 -19
  185. package/dist/core/extensions/version-compat.js +1 -216
  186. package/dist/core/fs/fs-utils.js +2 -158
  187. package/dist/core/fs/host-environment-errors.js +1 -141
  188. package/dist/core/fs/index.js +1 -9
  189. package/dist/core/fs/path-utils.js +1 -17
  190. package/dist/core/governance/content-fields.js +1 -160
  191. package/dist/core/governance/issue-codes.js +1 -151
  192. package/dist/core/governance/metadata-coverage.js +1 -358
  193. package/dist/core/history/drift-cache.js +3 -72
  194. package/dist/core/history/drift-scan.js +1 -342
  195. package/dist/core/history/event-classification.js +1 -109
  196. package/dist/core/history/event-index.js +8 -784
  197. package/dist/core/history/history-compact-bulk.js +1 -104
  198. package/dist/core/history/history-diff.js +1 -158
  199. package/dist/core/history/history-rewrite.d.ts +6 -1
  200. package/dist/core/history/history-rewrite.js +1 -107
  201. package/dist/core/history/history-storage-stats.js +1 -102
  202. package/dist/core/history/history-stream-policy.js +1 -62
  203. package/dist/core/history/history.js +2 -617
  204. package/dist/core/history/identity.js +1 -30
  205. package/dist/core/history/index.js +1 -9
  206. package/dist/core/history/projection.js +1 -196
  207. package/dist/core/history/read.d.ts +9 -0
  208. package/dist/core/history/read.js +1 -57
  209. package/dist/core/history/replay.js +2 -439
  210. package/dist/core/history/version-address.js +1 -32
  211. package/dist/core/history/workspace-history.js +2 -433
  212. package/dist/core/io/body-file.js +1 -62
  213. package/dist/core/io/bulk-ids-input.js +1 -112
  214. package/dist/core/item/actionability.js +1 -327
  215. package/dist/core/item/dependency-reference.js +1 -20
  216. package/dist/core/item/id.js +1 -112
  217. package/dist/core/item/index.js +1 -12
  218. package/dist/core/item/item-format-version.js +1 -78
  219. package/dist/core/item/item-format.js +5 -1473
  220. package/dist/core/item/item-record.js +1 -23
  221. package/dist/core/item/item-type-definition.js +1 -133
  222. package/dist/core/item/parent-reference-policy.js +1 -57
  223. package/dist/core/item/parse.js +2 -603
  224. package/dist/core/item/plan-lifecycle.js +1 -19
  225. package/dist/core/item/priority.js +1 -61
  226. package/dist/core/item/sprint-release-format.js +1 -38
  227. package/dist/core/item/status-filter.js +1 -115
  228. package/dist/core/item/status.js +1 -43
  229. package/dist/core/item/toon-decode.js +1 -29
  230. package/dist/core/item/type-registry.js +1 -715
  231. package/dist/core/item/type-synonyms.js +1 -38
  232. package/dist/core/lock/index.js +1 -9
  233. package/dist/core/lock/lock-gc.js +1 -240
  234. package/dist/core/lock/lock.js +3 -590
  235. package/dist/core/output/mutation-projection.js +1 -217
  236. package/dist/core/output/output-control.js +1 -25
  237. package/dist/core/output/output.js +11 -494
  238. package/dist/core/output/query-summary.js +1 -58
  239. package/dist/core/output/tabular.js +1 -77
  240. package/dist/core/packages/manifest.js +1 -244
  241. package/dist/core/packages/root.js +1 -84
  242. package/dist/core/policy/workflow-policy-schema.d.ts +312 -0
  243. package/dist/core/policy/workflow-policy-schema.js +2 -0
  244. package/dist/core/policy/workflow-policy-store.d.ts +24 -0
  245. package/dist/core/policy/workflow-policy-store.js +2 -0
  246. package/dist/core/policy/workflow-policy.d.ts +139 -0
  247. package/dist/core/policy/workflow-policy.js +2 -0
  248. package/dist/core/profile/profile-describe.js +1 -83
  249. package/dist/core/profile/profile-lint.js +1 -415
  250. package/dist/core/profile/profile-plan.js +0 -0
  251. package/dist/core/profile/profile-presets.js +1 -370
  252. package/dist/core/reproducibility/context.js +1 -82
  253. package/dist/core/schema/fields-file.js +2 -288
  254. package/dist/core/schema/item-types-file.js +2 -296
  255. package/dist/core/schema/runtime-field-filters.js +1 -49
  256. package/dist/core/schema/runtime-field-values.js +1 -313
  257. package/dist/core/schema/runtime-schema.js +2 -826
  258. package/dist/core/schema/status-defs-file.js +2 -227
  259. package/dist/core/schema/status-token.js +1 -31
  260. package/dist/core/schema/type-inference.js +1 -95
  261. package/dist/core/schema/type-presets.js +1 -87
  262. package/dist/core/schema/type-workflows.js +1 -104
  263. package/dist/core/search/background-refresh.js +2 -286
  264. package/dist/core/search/bm25.js +1 -146
  265. package/dist/core/search/cache.js +2 -534
  266. package/dist/core/search/corpus.js +1 -311
  267. package/dist/core/search/embedding-batches.js +1 -163
  268. package/dist/core/search/eval.js +1 -225
  269. package/dist/core/search/http-client.js +1 -75
  270. package/dist/core/search/providers.js +1 -242
  271. package/dist/core/search/relevance.js +1 -244
  272. package/dist/core/search/semantic-defaults.js +1 -199
  273. package/dist/core/search/staleness.js +1 -30
  274. package/dist/core/search/vector-stores.js +2 -664
  275. package/dist/core/search/vectorization-metadata.js +1 -81
  276. package/dist/core/sentry/helpers.js +1 -272
  277. package/dist/core/sentry/instrument.js +1 -387
  278. package/dist/core/session/session-state.js +1 -260
  279. package/dist/core/shared/agent-session-context.js +1 -133
  280. package/dist/core/shared/author.js +1 -1172
  281. package/dist/core/shared/command-types.js +1 -4
  282. package/dist/core/shared/conflict-markers.js +1 -31
  283. package/dist/core/shared/constants.js +1 -405
  284. package/dist/core/shared/errors.js +1 -20
  285. package/dist/core/shared/html-entity-decode.js +1 -131
  286. package/dist/core/shared/index.js +1 -12
  287. package/dist/core/shared/item-metadata-contract.js +1 -89
  288. package/dist/core/shared/lazy-module.js +1 -16
  289. package/dist/core/shared/levenshtein.js +1 -61
  290. package/dist/core/shared/memo.js +1 -19
  291. package/dist/core/shared/numeric-parsers.js +1 -20
  292. package/dist/core/shared/option-alias-visibility.js +1 -67
  293. package/dist/core/shared/primitives.js +1 -127
  294. package/dist/core/shared/serial-queue.js +1 -79
  295. package/dist/core/shared/serialization.js +1 -210
  296. package/dist/core/shared/split-comma-list.js +1 -30
  297. package/dist/core/shared/text-normalization.js +1 -38
  298. package/dist/core/shared/time.js +1 -306
  299. package/dist/core/store/index.js +1 -12
  300. package/dist/core/store/item-format-migration.js +1 -125
  301. package/dist/core/store/item-metadata-cache.js +1 -1116
  302. package/dist/core/store/item-metadata-query-index.js +9 -394
  303. package/dist/core/store/item-metadata-read-work.js +1 -43
  304. package/dist/core/store/item-store.js +1 -693
  305. package/dist/core/store/paths.d.ts +1 -1
  306. package/dist/core/store/paths.js +1 -214
  307. package/dist/core/store/settings-read-cache.js +1 -85
  308. package/dist/core/store/settings-validator.d.ts +1 -0
  309. package/dist/core/store/settings-validator.js +1 -421
  310. package/dist/core/store/settings.js +3 -1309
  311. package/dist/core/store/tracker-preflight.js +1 -141
  312. package/dist/core/telemetry/consent.js +5 -108
  313. package/dist/core/telemetry/observability.js +1 -246
  314. package/dist/core/telemetry/runtime.js +4 -1988
  315. package/dist/core/test/background-runs.js +4 -945
  316. package/dist/core/test/item-test-run-tracking.js +1 -63
  317. package/dist/core/test/run-selectors.js +1 -120
  318. package/dist/core/validate/estimate-defaults.js +1 -89
  319. package/dist/core/validate/fix-planning.js +1 -189
  320. package/dist/core/validate/linked-artifact-reference.js +1 -36
  321. package/dist/core/validate/missing-by-type.js +1 -34
  322. package/dist/core/validate/missing-link-owners.js +1 -95
  323. package/dist/core/validate/stale-file-classification.js +1 -82
  324. package/dist/mcp/http-server.js +5 -448
  325. package/dist/mcp/legacy-adapter.js +1 -77
  326. package/dist/mcp/runtime-capabilities.js +1 -200
  327. package/dist/mcp/server.js +4 -1233
  328. package/dist/mcp/tool-definitions.js +1 -804
  329. package/dist/sdk/actionability.js +1 -13
  330. package/dist/sdk/agent/closed-domain-contracts.js +1 -161
  331. package/dist/sdk/agent/command-recovery.js +1 -59
  332. package/dist/sdk/agent/command-suggestions.js +1 -66
  333. package/dist/sdk/agent/create-option-policy.js +1 -31
  334. package/dist/sdk/agent/identity-config.js +1 -106
  335. package/dist/sdk/agent/item-addressing.js +1 -207
  336. package/dist/sdk/agent/refusal-closure-census.js +1 -151
  337. package/dist/sdk/agent/refusal-closure.js +1 -133
  338. package/dist/sdk/agent/refusal-corpus-contracts.js +1 -193
  339. package/dist/sdk/agent/refusal-reachability.js +1 -593
  340. package/dist/sdk/agent/subcommand-recovery.js +1 -83
  341. package/dist/sdk/agent/task-transcript-contracts.js +1 -197
  342. package/dist/sdk/agent/tracker-preflight-contracts.js +1 -126
  343. package/dist/sdk/agent-capability-contracts.js +1 -428
  344. package/dist/sdk/agent-session-context.js +1 -9
  345. package/dist/sdk/annotations.js +1 -448
  346. package/dist/sdk/author-attribution.js +1 -468
  347. package/dist/sdk/authoring.js +1 -20
  348. package/dist/sdk/cli-bootstrap.js +1 -838
  349. package/dist/sdk/cli-contracts/agent-output-contracts.d.ts +1 -1
  350. package/dist/sdk/cli-contracts/agent-output-contracts.js +4 -476
  351. package/dist/sdk/cli-contracts/bootstrap-command-scanner.js +1 -79
  352. package/dist/sdk/cli-contracts/command-aliases.d.ts +4 -0
  353. package/dist/sdk/cli-contracts/command-aliases.js +1 -107
  354. package/dist/sdk/cli-contracts/command-exit-contracts.js +1 -151
  355. package/dist/sdk/cli-contracts/commander-mutation-options.js +1 -1081
  356. package/dist/sdk/cli-contracts/commander-types.js +1 -159
  357. package/dist/sdk/cli-contracts/completeness.js +1 -393
  358. package/dist/sdk/cli-contracts/enum-contracts.d.ts +1 -1
  359. package/dist/sdk/cli-contracts/enum-contracts.js +1 -222
  360. package/dist/sdk/cli-contracts/flag-contracts.js +1 -1947
  361. package/dist/sdk/cli-contracts/flag-lexicon-contracts.js +1 -347
  362. package/dist/sdk/cli-contracts/grammar-contracts.js +1 -986
  363. package/dist/sdk/cli-contracts/registration-helpers.js +7 -990
  364. package/dist/sdk/cli-contracts/runtime-contracts.d.ts +5 -0
  365. package/dist/sdk/cli-contracts/runtime-contracts.js +1 -2090
  366. package/dist/sdk/cli-contracts/schemas/assurance-definition.js +1 -324
  367. package/dist/sdk/cli-contracts/string-lists.js +1 -16
  368. package/dist/sdk/cli-contracts/tool-option-contracts.js +1 -461
  369. package/dist/sdk/cli-contracts/tool-parameter-tables.js +1 -2167
  370. package/dist/sdk/cli-contracts/tool-schema.d.ts +3 -1
  371. package/dist/sdk/cli-contracts/tool-schema.js +1 -1664
  372. package/dist/sdk/cli-contracts/typed-action-inputs.js +1 -4
  373. package/dist/sdk/cli-contracts.d.ts +1 -1
  374. package/dist/sdk/cli-contracts.js +1 -17
  375. package/dist/sdk/cli-program.js +1 -49
  376. package/dist/sdk/command-line.d.ts +2 -5
  377. package/dist/sdk/command-line.js +1 -78
  378. package/dist/sdk/comments.js +1 -133
  379. package/dist/sdk/compile-cache.js +1 -73
  380. package/dist/sdk/completion.js +174 -839
  381. package/dist/sdk/compose.js +1 -1090
  382. package/dist/sdk/config.js +1 -1204
  383. package/dist/sdk/context/recency.js +1 -33
  384. package/dist/sdk/context/semantic-session-attribution.js +1 -82
  385. package/dist/sdk/context/usage-egress.js +1 -29
  386. package/dist/sdk/context/usage-ledger.js +3 -185
  387. package/dist/sdk/context-intent-contracts.js +1 -709
  388. package/dist/sdk/context-intent-runtime.js +1 -115
  389. package/dist/sdk/context-packing.js +1 -218
  390. package/dist/sdk/context-relevance.js +1 -503
  391. package/dist/sdk/context-signal-store.js +2 -426
  392. package/dist/sdk/context-usage.js +1 -367
  393. package/dist/sdk/contracts.js +1 -23
  394. package/dist/sdk/core-governance.js +1 -14
  395. package/dist/sdk/core.js +1 -14
  396. package/dist/sdk/corpus-shape.js +1 -401
  397. package/dist/sdk/define.js +1 -220
  398. package/dist/sdk/dependencies.js +1 -718
  399. package/dist/sdk/dependency-flag-validation.js +1 -156
  400. package/dist/sdk/dependency-provenance.js +1 -135
  401. package/dist/sdk/docs.js +1 -14
  402. package/dist/sdk/duplicates.js +1 -40
  403. package/dist/sdk/environment/agent-environment.js +1 -466
  404. package/dist/sdk/environment/host-environment-errors.js +1 -8
  405. package/dist/sdk/environment/project-runtime-compatibility.js +1 -481
  406. package/dist/sdk/environment/source-context.js +1 -28
  407. package/dist/sdk/environment/tracker-preflight.js +1 -9
  408. package/dist/sdk/error-code-catalog.js +1 -153
  409. package/dist/sdk/error-runtime.js +1 -58
  410. package/dist/sdk/errors.js +1 -58
  411. package/dist/sdk/eval.js +1 -190
  412. package/dist/sdk/extension/author-manifest.js +1 -92
  413. package/dist/sdk/extension/bundled-catalog.js +1 -332
  414. package/dist/sdk/extension/concurrency.js +1 -25
  415. package/dist/sdk/extension/copy-scope.d.ts +2 -0
  416. package/dist/sdk/extension/copy-scope.js +2 -0
  417. package/dist/sdk/extension/custom-field-collisions.js +1 -30
  418. package/dist/sdk/extension/describe.js +3 -189
  419. package/dist/sdk/extension/doctor.js +1 -606
  420. package/dist/sdk/extension/install-plan.d.ts +69 -0
  421. package/dist/sdk/extension/install-plan.js +2 -0
  422. package/dist/sdk/extension/install-receipts.d.ts +7 -0
  423. package/dist/sdk/extension/install-receipts.js +2 -0
  424. package/dist/sdk/extension/install-runtime.js +3 -313
  425. package/dist/sdk/extension/install-snapshot.js +1 -66
  426. package/dist/sdk/extension/install-sources.js +5 -1057
  427. package/dist/sdk/extension/managed-state.js +2 -252
  428. package/dist/sdk/extension/migrations.js +2 -174
  429. package/dist/sdk/extension/output-ownership.js +1 -25
  430. package/dist/sdk/extension/runtime-summary.js +1 -18
  431. package/dist/sdk/extension/scaffold.js +4 -2308
  432. package/dist/sdk/extension/shared.js +1 -147
  433. package/dist/sdk/extension/source-resolution.js +1 -65
  434. package/dist/sdk/extension/update-check.js +1 -67
  435. package/dist/sdk/extension-command-context.js +1 -44
  436. package/dist/sdk/extension-contracts.js +1 -73
  437. package/dist/sdk/extension.d.ts +7 -2
  438. package/dist/sdk/extension.js +1 -2196
  439. package/dist/sdk/files.js +1 -535
  440. package/dist/sdk/flag-invocation-contracts.d.ts +0 -5
  441. package/dist/sdk/flag-invocation-contracts.js +1 -344
  442. package/dist/sdk/generated/generated-error-code-catalog-part-1.js +1 -2224
  443. package/dist/sdk/generated/generated-error-code-catalog-part-2.js +1 -2236
  444. package/dist/sdk/generated-error-code-catalog.js +1 -16
  445. package/dist/sdk/governance/assurance-action-contracts.js +1 -4
  446. package/dist/sdk/governance/assurance-action.js +1 -411
  447. package/dist/sdk/governance/assurance-limits.js +1 -10
  448. package/dist/sdk/governance/assurance-mutation-error.js +1 -81
  449. package/dist/sdk/governance/assurance-presets.js +1 -135
  450. package/dist/sdk/governance/assurance-runtime.js +1 -238
  451. package/dist/sdk/governance/assurance.js +4 -1182
  452. package/dist/sdk/governance/boundary-fixtures.js +1 -200
  453. package/dist/sdk/governance/coverage-assurance.js +1 -60
  454. package/dist/sdk/governance/defect-recurrence-coverage.js +1 -188
  455. package/dist/sdk/governance/defect-recurrence-graph.js +1 -120
  456. package/dist/sdk/governance/defect-recurrence-signals.js +1 -24
  457. package/dist/sdk/governance/defect-recurrence.js +1 -623
  458. package/dist/sdk/governance/extension-host-version.js +1 -115
  459. package/dist/sdk/governance/gc.js +1 -368
  460. package/dist/sdk/governance/health.js +1 -2367
  461. package/dist/sdk/governance/hierarchy-validation.js +1 -108
  462. package/dist/sdk/governance/linked-file-report.js +1 -71
  463. package/dist/sdk/governance/merge-receipt-history.js +1 -206
  464. package/dist/sdk/governance/provenance-health.js +1 -282
  465. package/dist/sdk/governance/reindex.js +4 -761
  466. package/dist/sdk/governance/stale-work.js +1 -82
  467. package/dist/sdk/governance/status-role-diagnostics.js +1 -35
  468. package/dist/sdk/governance/storage-integrity.js +1 -280
  469. package/dist/sdk/governance/tracked-runtime-cache.js +1 -106
  470. package/dist/sdk/governance/upgrade.js +2 -327
  471. package/dist/sdk/governance/validate-history-drift.js +1 -81
  472. package/dist/sdk/governance/validate-item-reader.js +1 -17
  473. package/dist/sdk/governance/validate-normalization.js +1 -36
  474. package/dist/sdk/governance/validate.d.ts +3 -1
  475. package/dist/sdk/governance/validate.js +1 -2261
  476. package/dist/sdk/governance/workflow-completeness.d.ts +32 -0
  477. package/dist/sdk/governance/workflow-completeness.js +2 -0
  478. package/dist/sdk/governance/workflow-policy.d.ts +47 -0
  479. package/dist/sdk/governance/workflow-policy.js +4 -0
  480. package/dist/sdk/governance/workspace-position.js +1 -163
  481. package/dist/sdk/governance.d.ts +2 -0
  482. package/dist/sdk/governance.js +1 -31
  483. package/dist/sdk/graph/adapter.js +1 -181
  484. package/dist/sdk/graph/analytics.js +1 -474
  485. package/dist/sdk/graph/assembly.js +1 -377
  486. package/dist/sdk/graph/cache.js +1 -160
  487. package/dist/sdk/graph/centrality.js +1 -258
  488. package/dist/sdk/graph/conformance.js +1 -130
  489. package/dist/sdk/graph/deadline-scheduling.d.ts +90 -0
  490. package/dist/sdk/graph/deadline-scheduling.js +2 -0
  491. package/dist/sdk/graph/durable-cache.d.ts +1 -1
  492. package/dist/sdk/graph/durable-cache.js +1 -361
  493. package/dist/sdk/graph/governance-contracts.js +1 -34
  494. package/dist/sdk/graph/governance.d.ts +13 -1
  495. package/dist/sdk/graph/governance.js +1 -735
  496. package/dist/sdk/graph/hierarchy-integrity.js +1 -310
  497. package/dist/sdk/graph/index.d.ts +3 -2
  498. package/dist/sdk/graph/index.js +1 -26
  499. package/dist/sdk/graph/lifecycle-coverage.d.ts +32 -0
  500. package/dist/sdk/graph/lifecycle-coverage.js +2 -0
  501. package/dist/sdk/graph/mutation-advisory.js +1 -176
  502. package/dist/sdk/graph/remediation.js +1 -197
  503. package/dist/sdk/graph/run.d.ts +15 -1
  504. package/dist/sdk/graph/run.js +1 -768
  505. package/dist/sdk/graph/scheduling.js +1 -124
  506. package/dist/sdk/graph/traversal.js +1 -284
  507. package/dist/sdk/graph/workspace-relationship-kind-registry.js +1 -27
  508. package/dist/sdk/graph.js +1 -15
  509. package/dist/sdk/guide-topics.js +1 -707
  510. package/dist/sdk/guide.js +1 -279
  511. package/dist/sdk/history/maintenance.d.ts +71 -0
  512. package/dist/sdk/history/maintenance.js +2 -0
  513. package/dist/sdk/history/salvage.js +2 -247
  514. package/dist/sdk/history/subject.d.ts +19 -0
  515. package/dist/sdk/history/subject.js +2 -0
  516. package/dist/sdk/history-analytics.js +1 -282
  517. package/dist/sdk/history-compact.js +1 -527
  518. package/dist/sdk/history-mcp.js +1 -68
  519. package/dist/sdk/history-provenance.js +1 -182
  520. package/dist/sdk/history-read.js +1 -96
  521. package/dist/sdk/history-redact.d.ts +1 -14
  522. package/dist/sdk/history-redact.js +1 -643
  523. package/dist/sdk/history-repair.d.ts +5 -0
  524. package/dist/sdk/history-repair.js +1 -657
  525. package/dist/sdk/improvement-ledger-validation.js +1 -62
  526. package/dist/sdk/improvement-ledger.js +2 -257
  527. package/dist/sdk/index.d.ts +4 -1
  528. package/dist/sdk/index.js +1 -183
  529. package/dist/sdk/init-agent-guidance.js +2 -496
  530. package/dist/sdk/init.js +2 -820
  531. package/dist/sdk/invocation-author.js +1 -39
  532. package/dist/sdk/item-children.js +1 -90
  533. package/dist/sdk/item-metadata-index.js +1 -15
  534. package/dist/sdk/item-schedule.js +1 -27
  535. package/dist/sdk/item-transaction.js +1 -396
  536. package/dist/sdk/learnings.js +1 -29
  537. package/dist/sdk/lifecycle/append.js +2 -53
  538. package/dist/sdk/lifecycle/claim.d.ts +2 -0
  539. package/dist/sdk/lifecycle/claim.js +1 -242
  540. package/dist/sdk/lifecycle/close-many.js +1 -438
  541. package/dist/sdk/lifecycle/close.js +1 -468
  542. package/dist/sdk/lifecycle/copy.js +1 -212
  543. package/dist/sdk/lifecycle/create.js +1 -1949
  544. package/dist/sdk/lifecycle/delete.js +1 -45
  545. package/dist/sdk/lifecycle/event-validation-messages.js +1 -91
  546. package/dist/sdk/lifecycle/focus.js +1 -91
  547. package/dist/sdk/lifecycle/hierarchy-mutation-lock.js +1 -24
  548. package/dist/sdk/lifecycle/legacy-none-tokens.js +1 -149
  549. package/dist/sdk/lifecycle/lifecycle-transitions.js +1 -40
  550. package/dist/sdk/lifecycle/mcp-actions.d.ts +2 -0
  551. package/dist/sdk/lifecycle/mcp-actions.js +1 -51
  552. package/dist/sdk/lifecycle/metadata-normalizers.js +1 -46
  553. package/dist/sdk/lifecycle/mutation-command-options.js +1 -9
  554. package/dist/sdk/lifecycle/plan.js +1 -1606
  555. package/dist/sdk/lifecycle/recurrence-parsers.d.ts +1 -1
  556. package/dist/sdk/lifecycle/recurrence-parsers.js +1 -147
  557. package/dist/sdk/lifecycle/reopen.js +1 -68
  558. package/dist/sdk/lifecycle/repeatable-metadata-parsers.js +1 -143
  559. package/dist/sdk/lifecycle/restore.d.ts +1 -0
  560. package/dist/sdk/lifecycle/restore.js +1 -330
  561. package/dist/sdk/lifecycle/shared-unset-fields.js +1 -295
  562. package/dist/sdk/lifecycle/update-many.js +1 -861
  563. package/dist/sdk/lifecycle/update.js +1 -2136
  564. package/dist/sdk/lifecycle-completion.js +1 -92
  565. package/dist/sdk/lifecycle-policy.js +1 -120
  566. package/dist/sdk/linked-artifacts.js +1 -530
  567. package/dist/sdk/mcp/apps.js +4 -133
  568. package/dist/sdk/mcp/authorization.js +1 -404
  569. package/dist/sdk/mcp/discovery.js +1 -494
  570. package/dist/sdk/mcp/interactions.js +1 -336
  571. package/dist/sdk/mcp/protocol.d.ts +7 -0
  572. package/dist/sdk/mcp/protocol.js +1 -184
  573. package/dist/sdk/mcp/skills.js +1 -389
  574. package/dist/sdk/mcp/subscriptions.js +1 -211
  575. package/dist/sdk/mcp/tasks.js +2 -430
  576. package/dist/sdk/mcp/transport.js +1 -260
  577. package/dist/sdk/merge/abandoned-receipts.js +1 -46
  578. package/dist/sdk/merge/driver.js +1 -212
  579. package/dist/sdk/merge/index.js +1 -16
  580. package/dist/sdk/merge/install.js +3 -561
  581. package/dist/sdk/merge/receipt-file-boundary.js +1 -46
  582. package/dist/sdk/merge/receipt-operation.js +1 -96
  583. package/dist/sdk/merge/receipt-schema.js +1 -397
  584. package/dist/sdk/merge/receipts.js +4 -525
  585. package/dist/sdk/merge/reconcile.js +1 -352
  586. package/dist/sdk/merge/three-way.js +3 -775
  587. package/dist/sdk/merge.js +1 -10
  588. package/dist/sdk/mutation-events.js +1 -292
  589. package/dist/sdk/mutation-guard.js +1 -216
  590. package/dist/sdk/notes.js +1 -139
  591. package/dist/sdk/output-contracts.js +1 -172
  592. package/dist/sdk/output-projection.js +1 -430
  593. package/dist/sdk/output-token-accounting.js +1 -140
  594. package/dist/sdk/output.js +1 -64
  595. package/dist/sdk/ownership-source.js +1 -21
  596. package/dist/sdk/package-import-adapters.js +1 -442
  597. package/dist/sdk/package-migrations.js +1 -20
  598. package/dist/sdk/package-runtime-options.js +1 -80
  599. package/dist/sdk/pagination.js +1 -162
  600. package/dist/sdk/profile.js +1 -454
  601. package/dist/sdk/provenance.js +1 -258
  602. package/dist/sdk/public-surface.json +1 -1
  603. package/dist/sdk/query/activity.js +1 -401
  604. package/dist/sdk/query/aggregate.d.ts +16 -0
  605. package/dist/sdk/query/aggregate.js +1 -390
  606. package/dist/sdk/query/calendar.js +1 -964
  607. package/dist/sdk/query/complete-list.js +1 -215
  608. package/dist/sdk/query/context.js +1 -1702
  609. package/dist/sdk/query/duplicate-candidates.js +1 -70
  610. package/dist/sdk/query/get.js +1 -450
  611. package/dist/sdk/query/history.js +1 -186
  612. package/dist/sdk/query/item-filter-options.js +1 -9
  613. package/dist/sdk/query/light-metadata.js +1 -18
  614. package/dist/sdk/query/list-filter-shared.js +1 -81
  615. package/dist/sdk/query/list.js +1 -1304
  616. package/dist/sdk/query/multi-value-filters.js +1 -78
  617. package/dist/sdk/query/next.js +1 -513
  618. package/dist/sdk/query/parsers.js +1 -44
  619. package/dist/sdk/query/projection-contracts.js +1 -184
  620. package/dist/sdk/query/search-contracts.d.ts +27 -0
  621. package/dist/sdk/query/search-contracts.js +1 -230
  622. package/dist/sdk/query/search-pagination.js +1 -35
  623. package/dist/sdk/query/search-rendering.js +1 -105
  624. package/dist/sdk/query/search.d.ts +6 -4
  625. package/dist/sdk/query/search.js +2 -1987
  626. package/dist/sdk/query.js +1 -35
  627. package/dist/sdk/read-output/continuation.js +1 -194
  628. package/dist/sdk/read-output-budget.js +1 -135
  629. package/dist/sdk/read-output-contracts.d.ts +1 -1
  630. package/dist/sdk/read-output-contracts.js +1 -1225
  631. package/dist/sdk/read-output-rows.js +1 -186
  632. package/dist/sdk/read-output-session.js +1 -208
  633. package/dist/sdk/relationship-analytics.js +1 -410
  634. package/dist/sdk/relationship-context.js +1 -284
  635. package/dist/sdk/relationship-history.js +4 -519
  636. package/dist/sdk/relationship-kinds/contract.js +1 -22
  637. package/dist/sdk/relationship-migration.js +1 -77
  638. package/dist/sdk/relationships.js +1 -579
  639. package/dist/sdk/reproducibility/process.js +1 -87
  640. package/dist/sdk/runtime-action-aliases.js +1 -59
  641. package/dist/sdk/runtime-extended-actions.js +1 -177
  642. package/dist/sdk/runtime-input.js +1 -614
  643. package/dist/sdk/runtime-primitives.js +1 -84
  644. package/dist/sdk/runtime-public-contracts.d.ts +2 -1
  645. package/dist/sdk/runtime-public-contracts.js +1 -4
  646. package/dist/sdk/runtime-stats-options.js +1 -38
  647. package/dist/sdk/runtime.d.ts +4 -0
  648. package/dist/sdk/runtime.js +1 -1928
  649. package/dist/sdk/scheduling-shortcuts.js +1 -63
  650. package/dist/sdk/schema-migration.js +3 -856
  651. package/dist/sdk/schema.d.ts +2 -1
  652. package/dist/sdk/schema.js +1 -1376
  653. package/dist/sdk/search-refresh-worker.js +1 -18
  654. package/dist/sdk/similarity-scoring.js +1 -53
  655. package/dist/sdk/similarity.js +1 -326
  656. package/dist/sdk/start-task-status.js +1 -14
  657. package/dist/sdk/stats.js +1 -270
  658. package/dist/sdk/structured-mutations.js +1 -580
  659. package/dist/sdk/telemetry-flush.js +1 -13
  660. package/dist/sdk/telemetry.js +1 -405
  661. package/dist/sdk/templates.js +2 -292
  662. package/dist/sdk/test/batch.js +2 -509
  663. package/dist/sdk/test/entry.js +1 -70
  664. package/dist/sdk/test/execution.js +6 -2211
  665. package/dist/sdk/test/linked-command-detection.js +1 -546
  666. package/dist/sdk/test/measurements.js +1 -91
  667. package/dist/sdk/test/parsers.js +1 -545
  668. package/dist/sdk/test/runs.js +1 -193
  669. package/dist/sdk/test/trust.js +2 -206
  670. package/dist/sdk/testing.js +4 -1391
  671. package/dist/sdk/traceability/runtime-files-lookup.js +1 -32
  672. package/dist/sdk/traceability/source-traceability.js +1 -292
  673. package/dist/sdk/tracker-root-discovery.js +1 -9
  674. package/dist/sdk/workspace-contracts-cache.js +1 -28
  675. package/dist/sdk/workspace-contracts.js +1 -33
  676. package/dist/sdk/workspace-memory.js +2 -318
  677. package/dist/sdk/workspace-recipe.js +1 -71
  678. package/dist/sdk/workspace-snapshot.js +4 -613
  679. package/dist/sdk/workspace-transaction-gc.js +1 -149
  680. package/dist/sdk/workspace-transaction.js +2 -363
  681. package/dist/sdk/workspace.js +3 -127
  682. package/dist/types/index.js +1 -9
  683. package/dist/types.d.ts +4 -0
  684. package/dist/types.js +1 -245
  685. package/docs/AGENT_GUIDE.md +1 -1
  686. package/docs/COMMANDS.md +10 -0
  687. package/docs/CONFIGURATION.md +12 -1
  688. package/docs/CONTEXT_ALGORITHM_PORTFOLIO.md +167 -0
  689. package/docs/DIAGNOSTIC_OUTPUT_CONTRACTS.md +9 -3
  690. package/docs/EXTENSIONS.md +1 -0
  691. package/docs/HISTORY_MAINTENANCE.md +125 -0
  692. package/docs/MCP_2026_07_28_CONFORMANCE.md +20 -10
  693. package/docs/PACKAGE_EVIDENCE.md +92 -0
  694. package/docs/PLANNING_MEASUREMENTS.md +183 -0
  695. package/docs/README.md +4 -0
  696. package/docs/READ_OUTPUT_CONTRACTS.md +5 -0
  697. package/docs/SCRIPTING.md +21 -0
  698. package/docs/TESTING.md +33 -0
  699. package/docs/WORKFLOW_POLICIES.md +129 -0
  700. package/docs/generated/FLAG_LEXICON_BUDGETS.md +7 -7
  701. package/docs/generated/REFUSAL_CLOSURE_CENSUS.md +11 -5
  702. package/marketplace.json +2 -2
  703. package/package.json +5 -5
  704. package/packages/pm-beads/package.json +1 -1
  705. package/packages/pm-calendar/package.json +1 -1
  706. package/packages/pm-command-kit/package.json +1 -1
  707. package/packages/pm-digital-twin/package.json +1 -1
  708. package/packages/pm-governance-audit/package.json +1 -1
  709. package/packages/pm-guide-shell/package.json +1 -1
  710. package/packages/pm-kanban/package.json +1 -1
  711. package/packages/pm-lifecycle-hooks/package.json +1 -1
  712. package/packages/pm-linked-test-adapters/package.json +1 -1
  713. package/packages/pm-search-advanced/package.json +1 -1
  714. package/packages/pm-templates/package.json +1 -1
  715. package/packages/pm-todos/package.json +1 -1
  716. package/packages/pm-vcs/package.json +1 -1
  717. package/plugins/pm-claude/.claude-plugin/plugin.json +1 -1
  718. package/plugins/pm-codex/.codex-plugin/plugin.json +1 -1
  719. package/scripts/finalize-build.mjs +45 -3
  720. package/dist/cli-bundle/chunks/chunk-AZDFJGH6.js +0 -9
  721. package/dist/cli-bundle/chunks/chunk-CKOL5U7J.js +0 -77
  722. package/dist/cli-bundle/chunks/chunk-JX4DH4WC.js +0 -3
  723. package/dist/cli-bundle/chunks/chunk-OF76PSDQ.js +0 -5
  724. package/dist/cli-bundle/chunks/chunk-QUWFABDM.js +0 -232
  725. package/dist/cli-bundle/chunks/chunk-QWRRFYK3.js +0 -2
  726. package/dist/cli-bundle/chunks/chunk-RWWBOWJ5.js +0 -2
  727. package/dist/cli-bundle/chunks/chunk-WGAOYSYH.js +0 -40
  728. package/dist/cli-bundle/chunks/chunk-WI2AYPA7.js +0 -2
  729. package/dist/cli-bundle/chunks/register-list-query-5JL52D33.js +0 -11
  730. package/dist/cli-bundle/chunks/register-mutation-ZPG7KMVP.js +0 -20
  731. package/dist/cli-bundle/chunks/register-operations-I5W3KMGB.js +0 -2
  732. package/dist/cli-bundle/chunks/register-setup-H4SVAVX7.js +0 -2
  733. package/dist/cli-bundle/focused-chunks/chunk-AL5ON6K6.js +0 -2
  734. package/dist/cli-bundle/focused-chunks/chunk-CHC2IDXA.js +0 -6
  735. package/dist/cli-bundle/focused-chunks/chunk-CK6QFAPZ.js +0 -20
  736. package/dist/cli-bundle/focused-chunks/chunk-EZR3GHFE.js +0 -2
  737. package/dist/cli-bundle/focused-chunks/chunk-H2WJWK7L.js +0 -56
  738. package/dist/cli-bundle/focused-chunks/chunk-NTPY5GSO.js +0 -3
  739. package/dist/cli-bundle/focused-chunks/chunk-P5O7MP5Y.js +0 -2
  740. package/dist/cli-bundle/focused-chunks/chunk-PDMW2E36.js +0 -177
  741. package/dist/cli-bundle/focused-chunks/chunk-PEFABF7E.js +0 -26
  742. package/dist/cli-bundle/focused-chunks/chunk-QTVCLMSZ.js +0 -6
  743. package/dist/cli-bundle/focused-chunks/chunk-QTZRBUYA.js +0 -8
  744. package/dist/cli-bundle/focused-chunks/chunk-SGQST3PQ.js +0 -12
  745. package/dist/cli-bundle/focused-chunks/chunk-UKHASMMP.js +0 -10
  746. package/dist/cli-bundle/focused-chunks/chunk-ZPEKQHOC.js +0 -2
@@ -1,1988 +1,3 @@
1
- /**
2
- * @module sdk/query/search
3
- *
4
- * Implements the pm search command surface and its agent-facing runtime behavior.
5
- */
6
-
7
- !function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="a4117e19-af3e-50f5-b3cc-86c39aa4b439")}catch(e){}}();
8
- import { assertInitializedTracker } from "../environment/tracker-preflight.js";
9
- import fs from "node:fs/promises";
10
- import path from "node:path";
11
- import { toNonEmptyStringOrUndefined } from "../../core/shared/primitives.js";
12
- import { isPathWithinDirectory } from "../../core/fs/path-utils.js";
13
- import { getActiveExtensionRegistrations, runActiveOnReadHooks, } from "../../core/extensions/index.js";
14
- import { collectRegisteredItemFieldNames } from "../../core/extensions/item-fields.js";
15
- import { resolveRegisteredSearchProvider, resolveRegisteredVectorStoreAdapter, } from "../../core/extensions/runtime-registrations.js";
16
- import { resolveItemTypeRegistry, } from "../../core/item/type-registry.js";
17
- import { parseLimit } from "./parsers.js";
18
- import { parsePriorityFilterSet, parseStringFilterSet, parseTypeFilterSet, } from "./multi-value-filters.js";
19
- import { executeEmbeddingRequest, resolveEmbeddingProviders, } from "../../core/search/providers.js";
20
- import { buildDeterministicQueryExpansions, mergeQueryExpansions, normalizeQueryExpansionOutput, normalizeRerankOutput, rerankCandidatesWithEmbeddings, resolveQueryExpansionConfig, resolveRerankConfig, } from "../../core/search/relevance.js";
21
- import { resolveSettingsWithSemanticRuntimeDefaults } from "../../core/search/semantic-defaults.js";
22
- import { executeVectorQuery, resolveVectorStores, } from "../../core/search/vector-stores.js";
23
- import { readVectorizationStatusLedger } from "../../core/search/cache.js";
24
- import { buildEventCorpus, buildPlanFlatCorpus, buildReminderCorpus, buildSearchCorpus, resolveSearchCorpusFields, } from "../../core/search/corpus.js";
25
- import { buildBm25Index, flattenSearchCorpusText, resolveBm25Params, scoreBm25Query, tokenizeBm25, } from "../../core/search/bm25.js";
26
- import { collectStaleVectorizationIds } from "../../core/search/staleness.js";
27
- import { parseItemDocument } from "../../core/item/item-format.js";
28
- import { toItemRecord } from "../../core/item/item-record.js";
29
- import { isTerminalStatus } from "../../core/item/status.js";
30
- import { parseStatusFilterCsv } from "../../core/item/status-filter.js";
31
- import { collectRuntimeFilterValues, matchesRuntimeFilters, } from "../../core/schema/runtime-field-filters.js";
32
- import { hasMissingMetadataFilter, itemMatchesMissingMetadata, lifecycleClassifierFromStatusRegistry, } from "../../core/governance/metadata-coverage.js";
33
- import { hasContentFieldFilter, itemMatchesContentFilters, } from "../../core/governance/content-fields.js";
34
- import { resolveContentFieldFilters, resolveMissingMetadataFilters, } from "./list.js";
35
- import { buildCompactSearchFilterSummary, buildVerboseSearchFilters, } from "./search-rendering.js";
36
- import { resolveRuntimeFieldRegistry, resolveRuntimeStatusRegistry, } from "../../core/schema/runtime-schema.js";
37
- import { EXIT_CODE } from "../../core/shared/constants.js";
38
- import { PmCliError } from "../../core/shared/errors.js";
39
- import { tokenizeAlphaNumeric } from "../../core/shared/text-normalization.js";
40
- import { compareTimestampStrings, matchesTimestampFilters, nowIso, } from "../../core/shared/time.js";
41
- import { listAllDocumentCandidatesCached } from "../../core/store/item-metadata-cache.js";
42
- import { listAllItemMetadata } from "../../core/store/item-store.js";
43
- import { getItemPath, resolveGlobalPmRoot, resolvePmRoot, } from "../../core/store/paths.js";
44
- import { readSettings } from "../../core/store/settings.js";
45
- import { resolveSearchPage } from "./search-pagination.js";
46
- import { normalizeSearchPhrase, parseMinScoreOverride, parseSearchBoolean, parseSearchDeadline, parseSearchMatchMode, parseSearchMode, parseSearchProjection, parseSearchTokens, parseSemanticWeightOverride, parseTimestampWindow, resolveHybridSemanticWeight, resolveSearchMaxResults, resolveSearchScoreThreshold, resolveSearchTuning, validateSearchProjectionFields, } from "./search-contracts.js";
47
- import { readWorkspaceMemory, searchWorkspaceMemoryReadResult, } from "../workspace-memory.js";
48
- export { resolveHybridSemanticWeight, resolveSearchMaxResults, resolveSearchScoreThreshold, resolveSearchTuning, } from "./search-contracts.js";
49
- const LONG_QUERY_TOKEN_THRESHOLD = 2;
50
- const LONG_QUERY_TITLE_EXACT_BONUS = 120;
51
- const LONG_QUERY_PHRASE_MULTIPLIER = 6;
52
- // GH-181: in default (OR) match mode, multi-token queries that match EVERY
53
- // distinct query token in some searchable field get an additive ranking bonus so
54
- // items covering all terms outrank items matching only a subset. This is a
55
- // RANKING preference, not a hard filter (use --match-mode and for that).
56
- const ALL_TERMS_COVERAGE_BONUS = 40;
57
- const EXACT_ID_MATCH_SCORE = 1_000;
58
- const SHORT_ID_MATCH_SCORE = 900;
59
- // GH-157 matched-text highlighting (--highlight): markers wrapping each matching
60
- // token run, and the number of characters of surrounding context retained on
61
- // each side of the first match in a field. The «…» guillemets are single
62
- // characters with negligible collision risk against item text, keeping the
63
- // snippet token-cheap while staying visually unambiguous for agents.
64
- const HIGHLIGHT_OPEN = "«";
65
- const HIGHLIGHT_CLOSE = "»";
66
- const HIGHLIGHT_SNIPPET_RADIUS = 60;
67
- // GH-157 inline query syntax: bare `field:value` tokens parsed out of the query
68
- // string and applied as the equivalent filter. The value may itself contain
69
- // colons (e.g. `tag:area:search`), so only the FIRST colon delimits field from
70
- // value. Explicit --field flags take precedence over an inline token (see
71
- // resolveInlineQueryFilters).
72
- const INLINE_QUERY_FILTER_FIELDS = [
73
- "tag",
74
- "status",
75
- "type",
76
- "priority",
77
- ];
78
- /**
79
- * Aggregate the `code` strings found along an error's `cause` chain.
80
- *
81
- * undici (Node's fetch) collapses connection errors to the generic message
82
- * "fetch failed" and stashes the real syscall code (e.g. `ECONNREFUSED`) on
83
- * `error.cause.code`. Walking the chain (bounded depth) lets the fallback
84
- * classifier label a downed/unreachable Ollama backend as "connection" rather
85
- * than the catch-all "error".
86
- */
87
- export function collectErrorCauseCodes(error) {
88
- const codes = [];
89
- let current = error;
90
- for (let depth = 0; depth < 5 && current && typeof current === "object"; depth += 1) {
91
- const code = current.code;
92
- if (typeof code === "string") {
93
- codes.push(code.toLowerCase());
94
- }
95
- current = current.cause;
96
- }
97
- return codes.join(" ");
98
- }
99
- /**
100
- * Classify why an implicit/explicit semantic search degraded to keyword mode,
101
- * inspecting both the error message and the {@link collectErrorCauseCodes} chain
102
- * so undici's generic "fetch failed" is recognised as a connection failure.
103
- */
104
- export function classifyImplicitSemanticFallbackReason(error) {
105
- const message = (error instanceof Error ? error.message : String(error)).toLowerCase();
106
- const causeCodes = collectErrorCauseCodes(error);
107
- const haystack = `${message} ${causeCodes}`;
108
- if (haystack.includes("timed out") ||
109
- haystack.includes("timeout") ||
110
- haystack.includes("etimedout")) {
111
- return "timeout";
112
- }
113
- if (haystack.includes("econnrefused") ||
114
- haystack.includes("connection refused") ||
115
- haystack.includes("connect ") ||
116
- haystack.includes("enotfound") ||
117
- haystack.includes("eai_again") ||
118
- haystack.includes("econnreset") ||
119
- haystack.includes("fetch failed")) {
120
- return "connection";
121
- }
122
- return "error";
123
- }
124
- // Explicit --semantic/--hybrid searches must never hard-fail an agent when the
125
- // embedding/vector backend is unreachable or unconfigured: degrade to keyword
126
- // search and surface a machine-readable warning instead of an unknown_error.
127
- function buildExplicitSemanticFallbackWarning(requestedMode, error) {
128
- const reason = classifyImplicitSemanticFallbackReason(error);
129
- return `search_${requestedMode}_fallback:${reason}:using_keyword_mode`;
130
- }
131
- /** Resolve an executable reindex path from the active extension capability surface. */
132
- export function resolveVectorIndexRecovery(registrations) {
133
- const hasReindex = registrations?.commands?.some((registration) => registration.command.trim() === "reindex") === true;
134
- return hasReindex
135
- ? {
136
- command: "pm reindex --mode hybrid",
137
- args: ["reindex", "--mode", "hybrid"],
138
- }
139
- : {
140
- command: "pm install search-advanced --project",
141
- args: ["install", "search-advanced", "--project"],
142
- follow_up_command: "pm reindex --mode hybrid",
143
- follow_up_args: ["reindex", "--mode", "hybrid"],
144
- };
145
- }
146
- /** Compare the vectorization-status ledger against the filtered corpus and, when the vector index is behind, emit a one-line stderr warning plus a structured `vector_index_stale:N` entry in the JSON warnings array. Best-effort: ledger read failures fall through silently — the existing semantic/hybrid fallback paths cover backend errors. */
147
- async function maybeEmitVectorIndexStaleWarning(pmRoot, filteredDocuments, warnings) {
148
- try {
149
- const ledger = await readVectorizationStatusLedger(pmRoot);
150
- if (ledger.warnings.length > 0) {
151
- warnings.push(...ledger.warnings);
152
- }
153
- const staleIds = collectStaleVectorizationIds(filteredDocuments.map((document) => ({
154
- id: document.metadata.id,
155
- updated_at: document.metadata.updated_at,
156
- })), ledger.entries);
157
- if (staleIds.length === 0) {
158
- return;
159
- }
160
- const recovery = resolveVectorIndexRecovery(getActiveExtensionRegistrations());
161
- warnings.push(...formatVectorIndexRecoveryWarnings(staleIds.length, recovery));
162
- /* c8 ignore start -- singular/plural warning text branches are cosmetic and validated in integration UX tests */
163
- process.stderr.write(`[pm] warning: ${staleIds.length} item${staleIds.length === 1 ? " is" : "s are"} new or modified since the last reindex and ${staleIds.length === 1 ? "is" : "are"} NOT in the semantic index yet — run '${recovery.command}'${recovery.follow_up_command ? `, then '${recovery.follow_up_command}'` : ""}. (Write-time embedding is governed by search.mutation_refresh_policy; staleness means the embed was skipped, failed, or the backend was unreachable.)\n`);
164
- /* c8 ignore stop */
165
- }
166
- catch {
167
- // Best-effort: missing/unreadable ledger is not a query-blocking concern.
168
- }
169
- }
170
- /** Format token-efficient vector-index staleness and executable recovery warnings. */
171
- export function formatVectorIndexRecoveryWarnings(staleCount, recovery) {
172
- return [
173
- `vector_index_stale:${staleCount}`,
174
- `vector_index_recovery:${recovery.args.join(" ")}`,
175
- ...(recovery.follow_up_args
176
- ? [`vector_index_recovery_follow_up:${recovery.follow_up_args.join(" ")}`]
177
- : []),
178
- ];
179
- }
180
- /** Public contract for test only search command, shared by SDK and presentation-layer consumers. */
181
- export const _testOnlySearchCommand = {
182
- applyFilters,
183
- applyExactQueryFilters,
184
- applyInlineQueryFilters,
185
- buildHitHighlights,
186
- buildVerboseSearchFilters,
187
- buildExplicitSemanticFallbackWarning,
188
- buildCompactSearchFilterSummary,
189
- buildHybridLexicalScore,
190
- buildRerankCorpus,
191
- buildSemanticHits,
192
- classifyImplicitSemanticFallbackReason,
193
- collectErrorCauseCodes,
194
- collectExactPhraseFields,
195
- collectLinkedPaths,
196
- combineHybridHits,
197
- computeBuiltInBm25Hits,
198
- computeSemanticOrHybridHits,
199
- resolveBuiltInBm25Mode,
200
- countOccurrences,
201
- dependencyEntries,
202
- documentContainsExactPhrase,
203
- emptySearchResult,
204
- highlightFieldSnippet,
205
- loadDocuments,
206
- markTokenRuns,
207
- parseInlineQueryFilters,
208
- maybeEmitVectorIndexStaleWarning,
209
- loadLinkedCorpus,
210
- mergeVectorHitsById,
211
- normalizeExtensionProviderHits,
212
- normalizeScoreMap,
213
- parseProjectionConfig: parseSearchProjection,
214
- parseTimestampWindow,
215
- parseTokens: parseSearchTokens,
216
- projectSearchHits,
217
- readSearchFieldValue,
218
- requireSemanticDependencies,
219
- resolveExtensionSearchProvider,
220
- resolveExtensionSearchProviderByName,
221
- resolveExtensionVectorAdapter,
222
- resolveLinkedCorpusRoots,
223
- scoreDocument,
224
- sortHits,
225
- stringArray,
226
- textEntries,
227
- validateSearchProjectionFields,
228
- };
229
- /**
230
- * Split a raw search query into its residual keyword text and any inline
231
- * `field:value` filter tokens (GH-157). Recognized fields are
232
- * {@link INLINE_QUERY_FILTER_FIELDS}; the value runs to the end of the token so
233
- * colon-bearing values like `tag:area:search` parse as `{ tag: "area:search" }`.
234
- * Only the first occurrence of each field is captured — later duplicates are left
235
- * in the residual query so they are never silently dropped. Tokens whose prefix
236
- * is not a recognized field (`foo:bar`) stay in the residual query verbatim.
237
- */
238
- function parseInlineQueryFilters(query) {
239
- const inlineFilters = {};
240
- const residualTokens = [];
241
- for (const token of query.split(/\s+/).filter((entry) => entry.length > 0)) {
242
- const separatorIndex = token.indexOf(":");
243
- const field = separatorIndex > 0 ? token.slice(0, separatorIndex).toLowerCase() : "";
244
- const value = separatorIndex > 0 ? token.slice(separatorIndex + 1) : "";
245
- const matchedField = INLINE_QUERY_FILTER_FIELDS.includes(field)
246
- ? field
247
- : undefined;
248
- if (matchedField &&
249
- value.length > 0 &&
250
- inlineFilters[matchedField] === undefined) {
251
- inlineFilters[matchedField] = value;
252
- continue;
253
- }
254
- residualTokens.push(token);
255
- }
256
- return {
257
- residualQuery: residualTokens.join(" "),
258
- inlineFilters,
259
- };
260
- }
261
- /** Merge inline `field:value` filters into a search options object (GH-157). Explicit `--field` flags always win: an inline token is applied only when the corresponding option is not already set, and a conflicting inline token is recorded as a `search_inline_filter_ignored:<field>:flag_takes_precedence` warning so the override is observable rather than silent. Returns a fresh options object — the caller's input is never mutated. */
262
- function applyInlineQueryFilters(options, inlineFilters, warnings) {
263
- const merged = { ...options };
264
- for (const field of INLINE_QUERY_FILTER_FIELDS) {
265
- const inlineValue = inlineFilters[field];
266
- if (inlineValue === undefined) {
267
- continue;
268
- }
269
- if (toNonEmptyStringOrUndefined(merged[field]) !== undefined) {
270
- warnings.push(`search_inline_filter_ignored:${field}:flag_takes_precedence`);
271
- continue;
272
- }
273
- merged[field] = inlineValue;
274
- }
275
- return merged;
276
- }
277
- function stringArray(value) {
278
- return Array.isArray(value)
279
- ? value.filter((entry) => typeof entry === "string")
280
- : [];
281
- }
282
- function textEntries(value) {
283
- return Array.isArray(value)
284
- ? value.filter((entry) => typeof entry === "object" &&
285
- entry !== null &&
286
- typeof entry.text === "string")
287
- : [];
288
- }
289
- function dependencyEntries(value) {
290
- return Array.isArray(value)
291
- ? value.filter((entry) => typeof entry === "object" &&
292
- entry !== null &&
293
- typeof entry.id === "string" &&
294
- typeof entry.kind === "string")
295
- : [];
296
- }
297
- function collectExactPhraseFields(document) {
298
- const item = document.metadata;
299
- return [
300
- item.title,
301
- item.description,
302
- item.status,
303
- stringArray(item.tags).join(" "),
304
- document.body,
305
- textEntries(item.comments)
306
- .map((entry) => entry.text)
307
- .join(" "),
308
- textEntries(item.notes)
309
- .map((entry) => entry.text)
310
- .join(" "),
311
- textEntries(item.learnings)
312
- .map((entry) => entry.text)
313
- .join(" "),
314
- buildReminderCorpus(item).join(" "),
315
- buildEventCorpus(item).join(" "),
316
- dependencyEntries(item.dependencies)
317
- .map((entry) => `${entry.id} ${entry.kind}`)
318
- .join(" "),
319
- buildPlanFlatCorpus(item),
320
- ];
321
- }
322
- function documentContainsExactPhrase(document, normalizedQuery) {
323
- return collectExactPhraseFields(document).some((fieldValue) => normalizeSearchPhrase(fieldValue).includes(normalizedQuery));
324
- }
325
- function applyExactQueryFilters(items, normalizedQuery, options) {
326
- if (!options.titleExact && !options.phraseExact) {
327
- return items;
328
- }
329
- return items.filter((document) => {
330
- if (options.titleExact &&
331
- normalizeSearchPhrase(document.metadata.title) !== normalizedQuery) {
332
- return false;
333
- }
334
- if (options.phraseExact &&
335
- !documentContainsExactPhrase(document, normalizedQuery)) {
336
- return false;
337
- }
338
- return true;
339
- });
340
- }
341
- function assertSearchAssigneeFilter(assigneeFilter) {
342
- // Match pm list: --assignee no longer accepts none/null (unassigned filtering
343
- // belongs to a dedicated flag there; pm search has no presence flag so reject
344
- // the sentinel values explicitly rather than silently matching a literal).
345
- if (assigneeFilter &&
346
- [...assigneeFilter].some((value) => value.toLowerCase() === "none" || value.toLowerCase() === "null")) {
347
- throw new PmCliError('--assignee no longer accepts "none" or "null".', EXIT_CODE.USAGE);
348
- }
349
- }
350
- function resolveSearchMetadataFilterSet(options, typeRegistry, statusFilter) {
351
- const assigneeFilter = parseStringFilterSet(options.assignee, {
352
- label: "--assignee",
353
- });
354
- assertSearchAssigneeFilter(assigneeFilter);
355
- const missingMetadataFilters = resolveMissingMetadataFilters(options);
356
- const contentFieldFilters = resolveContentFieldFilters(options);
357
- return {
358
- typeFilter: parseTypeFilterSet(options.type, typeRegistry),
359
- tagFilter: parseStringFilterSet(options.tag, {
360
- label: "--tag",
361
- normalize: (tag) => tag.toLowerCase(),
362
- }),
363
- priorityFilter: parsePriorityFilterSet(options.priority),
364
- deadlineBefore: parseSearchDeadline(options.deadlineBefore, "deadline-before"),
365
- deadlineAfter: parseSearchDeadline(options.deadlineAfter, "deadline-after"),
366
- updatedAfter: parseTimestampWindow(options.updatedAfter, "updated-after"),
367
- updatedBefore: parseTimestampWindow(options.updatedBefore, "updated-before"),
368
- createdAfter: parseTimestampWindow(options.createdAfter, "created-after"),
369
- createdBefore: parseTimestampWindow(options.createdBefore, "created-before"),
370
- assigneeFilter,
371
- sprintFilter: parseStringFilterSet(options.sprint, { label: "--sprint" }),
372
- releaseFilter: parseStringFilterSet(options.release, { label: "--release" }),
373
- parentFilter: options.parent?.trim(),
374
- statusSet: statusFilter && statusFilter.length > 0
375
- ? new Set(statusFilter)
376
- : undefined,
377
- missingMetadataFilters,
378
- missingMetadataActive: hasMissingMetadataFilter(missingMetadataFilters),
379
- contentFieldFilters,
380
- contentFiltersActive: hasContentFieldFilter(contentFieldFilters),
381
- };
382
- }
383
- function matchesScalarSearchFilters(item, filters) {
384
- return (matchesIdentitySearchFilters(item, filters) &&
385
- matchesTimestampSearchFilters(item, filters) &&
386
- matchesOwnerSearchFilters(item, filters));
387
- }
388
- function matchesIdentitySearchFilters(item, filters) {
389
- if (filters.statusSet && !filters.statusSet.has(item.status))
390
- return false;
391
- if (filters.typeFilter && !filters.typeFilter.has(item.type))
392
- return false;
393
- const tagFilter = filters.tagFilter;
394
- if (tagFilter && !stringArray(item.tags).some((tag) => tagFilter.has(tag)))
395
- return false;
396
- if (filters.priorityFilter !== undefined &&
397
- !filters.priorityFilter.has(item.priority))
398
- return false;
399
- return true;
400
- }
401
- function matchesTimestampSearchFilters(item, filters) {
402
- return matchesTimestampFilters(item, filters);
403
- }
404
- function matchesOwnerSearchFilters(item, filters) {
405
- if (filters.assigneeFilter !== undefined &&
406
- (item.assignee === undefined || !filters.assigneeFilter.has(item.assignee)))
407
- return false;
408
- if (filters.sprintFilter !== undefined &&
409
- (item.sprint === undefined || !filters.sprintFilter.has(item.sprint)))
410
- return false;
411
- /* c8 ignore start -- release/parent metadata filter combinations are covered by integration search fixtures */
412
- if (filters.releaseFilter !== undefined &&
413
- (item.release === undefined || !filters.releaseFilter.has(item.release)))
414
- return false;
415
- if (filters.parentFilter !== undefined &&
416
- item.parent !== filters.parentFilter)
417
- return false;
418
- /* c8 ignore stop */
419
- return true;
420
- }
421
- function matchesSearchMetadataFilters(document, filters, runtimeFieldFilters, lifecycleClassifier) {
422
- const item = document.metadata;
423
- if (!matchesScalarSearchFilters(item, filters)) {
424
- return false;
425
- }
426
- if (!matchesRuntimeFilters(item, runtimeFieldFilters)) {
427
- return false;
428
- }
429
- if (filters.missingMetadataActive &&
430
- !itemMatchesMissingMetadata(item, filters.missingMetadataFilters, lifecycleClassifier)) {
431
- return false;
432
- }
433
- if (filters.contentFiltersActive &&
434
- !itemMatchesContentFilters(item, filters.contentFieldFilters)) {
435
- return false;
436
- }
437
- return true;
438
- }
439
- function applyFilters(items, options, typeRegistry, runtimeFieldFilters, statusFilter, lifecycleClassifier) {
440
- const filters = resolveSearchMetadataFilterSet(options, typeRegistry, statusFilter);
441
- return items.filter((document) => matchesSearchMetadataFilters(document, filters, runtimeFieldFilters, lifecycleClassifier));
442
- }
443
- function countOccurrences(haystack, needle) {
444
- let count = 0;
445
- let index = 0;
446
- while (true) {
447
- const foundAt = haystack.indexOf(needle, index);
448
- if (foundAt < 0) {
449
- return count;
450
- }
451
- count += 1;
452
- index = foundAt + needle.length;
453
- }
454
- }
455
- function tokenizeForExactTokenMatch(value) {
456
- return tokenizeAlphaNumeric(value);
457
- }
458
- function collectLinkedPaths(item) {
459
- const fromFiles = (item.files ?? []).map((entry) => ({
460
- scope: entry.scope,
461
- path: entry.path.trim(),
462
- }));
463
- const fromDocs = (item.docs ?? []).map((entry) => ({
464
- scope: entry.scope,
465
- path: entry.path.trim(),
466
- }));
467
- const fromTests = (item.tests ?? [])
468
- .filter((entry) => typeof entry.path === "string" && entry.path.trim().length > 0)
469
- .map((entry) => ({
470
- scope: entry.scope,
471
- path: entry.path.trim(),
472
- }));
473
- const sorted = [...fromFiles, ...fromDocs, ...fromTests]
474
- .filter((entry) => entry.path.length > 0)
475
- .sort((a, b) => a.scope.localeCompare(b.scope) || a.path.localeCompare(b.path));
476
- const deduped = new Map();
477
- for (const entry of sorted) {
478
- deduped.set(`${entry.scope}:${entry.path}`, entry);
479
- }
480
- return [...deduped.values()];
481
- }
482
- async function resolveContainmentRoot(root) {
483
- const resolved = path.resolve(root);
484
- try {
485
- const realpathRoot = await fs.realpath(resolved);
486
- return {
487
- resolved,
488
- realpath: realpathRoot,
489
- };
490
- }
491
- catch {
492
- return null;
493
- }
494
- }
495
- async function resolveLinkedCorpusRoots(projectRoot, globalRoot) {
496
- const [projectContainmentRoot, globalContainmentRoot] = await Promise.all([
497
- resolveContainmentRoot(projectRoot),
498
- resolveContainmentRoot(globalRoot),
499
- ]);
500
- return {
501
- projectContainmentRoot,
502
- globalContainmentRoot,
503
- };
504
- }
505
- async function loadLinkedCorpus(document, roots) {
506
- const linkedPaths = collectLinkedPaths(document.metadata);
507
- const chunks = [];
508
- for (const linkedPath of linkedPaths) {
509
- const containmentRoot = linkedPath.scope === "global"
510
- ? roots.globalContainmentRoot
511
- : roots.projectContainmentRoot;
512
- if (!containmentRoot) {
513
- continue;
514
- }
515
- const resolved = path.resolve(containmentRoot.resolved, linkedPath.path);
516
- if (!isPathWithinDirectory(containmentRoot.resolved, resolved)) {
517
- continue;
518
- }
519
- let linkedRealpath;
520
- try {
521
- linkedRealpath = await fs.realpath(resolved);
522
- }
523
- catch {
524
- continue;
525
- }
526
- if (!isPathWithinDirectory(containmentRoot.realpath, linkedRealpath)) {
527
- continue;
528
- }
529
- try {
530
- await runActiveOnReadHooks({
531
- path: resolved,
532
- scope: linkedPath.scope,
533
- });
534
- chunks.push(await fs.readFile(resolved, "utf8"));
535
- }
536
- catch {
537
- // Best-effort linked-content indexing: unreadable paths are ignored.
538
- }
539
- }
540
- return chunks.join("\n");
541
- }
542
- /**
543
- * Canonical definition of the document-derived searchable fields shared by the
544
- * lexical scorer ({@link scoreDocument}) and the matched-text highlighter
545
- * ({@link buildHitHighlights}). Keeping a single source of field name → text
546
- * extractor → tuning weight guarantees the highlighter can only ever produce
547
- * snippets for fields the scorer actually inspected. The `linked_content` field
548
- * is appended separately by the scorer because its text is supplied per-query
549
- * rather than derived from the document.
550
- */
551
- const SEARCHABLE_FIELD_BUILDERS = [
552
- {
553
- name: "title",
554
- weightKey: "title_weight",
555
- value: (document) => document.metadata.title,
556
- },
557
- {
558
- name: "description",
559
- weightKey: "description_weight",
560
- value: (document) => document.metadata.description,
561
- },
562
- {
563
- name: "tags",
564
- weightKey: "tags_weight",
565
- value: (document) => stringArray(document.metadata.tags).join(" "),
566
- },
567
- {
568
- name: "status",
569
- weightKey: "status_weight",
570
- value: (document) => typeof document.metadata.status === "string"
571
- ? document.metadata.status
572
- : "",
573
- },
574
- {
575
- name: "body",
576
- weightKey: "body_weight",
577
- value: (document) => document.body,
578
- },
579
- {
580
- name: "comments",
581
- weightKey: "comments_weight",
582
- value: (document) => textEntries(document.metadata.comments)
583
- .map((entry) => entry.text)
584
- .join(" "),
585
- },
586
- {
587
- name: "notes",
588
- weightKey: "notes_weight",
589
- value: (document) => textEntries(document.metadata.notes)
590
- .map((entry) => entry.text)
591
- .join(" "),
592
- },
593
- {
594
- name: "learnings",
595
- weightKey: "learnings_weight",
596
- value: (document) => textEntries(document.metadata.learnings)
597
- .map((entry) => entry.text)
598
- .join(" "),
599
- },
600
- {
601
- name: "reminders",
602
- weightKey: "reminders_weight",
603
- value: (document) => buildReminderCorpus(document.metadata).join(" "),
604
- },
605
- {
606
- name: "events",
607
- weightKey: "events_weight",
608
- value: (document) => buildEventCorpus(document.metadata).join(" "),
609
- },
610
- {
611
- name: "dependencies",
612
- weightKey: "dependencies_weight",
613
- value: (document) => dependencyEntries(document.metadata.dependencies)
614
- .map((entry) => `${entry.id} ${entry.kind}`)
615
- .join(" "),
616
- },
617
- {
618
- name: "plan",
619
- weightKey: "body_weight",
620
- value: (document) => buildPlanFlatCorpus(document.metadata),
621
- },
622
- ];
623
- // Name → builder index so the highlighter can resolve a matched field to its
624
- // text extractor in O(1) and evaluate ONLY the matched fields' values, instead
625
- // of materializing all twelve (several of which join/build corpora) per hit.
626
- const SEARCHABLE_FIELD_BUILDER_BY_NAME = new Map(SEARCHABLE_FIELD_BUILDERS.map((builder) => [builder.name, builder]));
627
- function escapeRegExpLiteral(value) {
628
- return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
629
- }
630
- /**
631
- * Wrap every case-insensitive occurrence of any query token in `text` with the
632
- * {@link HIGHLIGHT_OPEN}/{@link HIGHLIGHT_CLOSE} markers. Token boundaries follow
633
- * the same substring semantics as the lexical scorer (a token highlights inside
634
- * longer words, matching how it scored), and overlapping matches are coalesced
635
- * by the single combined alternation so markers never nest.
636
- */
637
- function markTokenRuns(text, tokens) {
638
- // Sort by length descending before building the alternation so a longer token
639
- // wins over a shorter token that is its prefix (regex alternation is greedy
640
- // left-to-right, so `auth|authority` would mark «auth»ority instead of
641
- // «authority»). `.filter` already returns a fresh array, so the in-place sort
642
- // never mutates the caller's token list.
643
- const escaped = tokens
644
- .filter((token) => token.length > 0)
645
- .sort((left, right) => right.length - left.length)
646
- .map(escapeRegExpLiteral);
647
- if (escaped.length === 0) {
648
- return text;
649
- }
650
- const pattern = new RegExp(escaped.join("|"), "gi");
651
- return text.replace(pattern, (match) => `${HIGHLIGHT_OPEN}${match}${HIGHLIGHT_CLOSE}`);
652
- }
653
- /**
654
- * Build a single highlighted snippet for one field value (GH-157), or null when
655
- * no token matches. The snippet is a window of {@link HIGHLIGHT_SNIPPET_RADIUS}
656
- * characters on each side of the first match with the matching token runs
657
- * wrapped in markers, prefixed/suffixed with an ellipsis when the field text was
658
- * trimmed. Returning null lets the caller skip fields with no concrete textual
659
- * match (e.g. a field flagged purely by the phrase-coverage bonus).
660
- */
661
- function highlightFieldSnippet(text, tokens) {
662
- if (text.length === 0) {
663
- return null;
664
- }
665
- const lowerText = text.toLowerCase();
666
- let firstMatchIndex = -1;
667
- let firstMatchLength = 0;
668
- for (const token of tokens) {
669
- if (token.length === 0) {
670
- continue;
671
- }
672
- const index = lowerText.indexOf(token);
673
- if (index >= 0 && (firstMatchIndex < 0 || index < firstMatchIndex)) {
674
- firstMatchIndex = index;
675
- firstMatchLength = token.length;
676
- }
677
- }
678
- if (firstMatchIndex < 0) {
679
- return null;
680
- }
681
- const windowStart = Math.max(0, firstMatchIndex - HIGHLIGHT_SNIPPET_RADIUS);
682
- const windowEnd = Math.min(text.length, firstMatchIndex + firstMatchLength + HIGHLIGHT_SNIPPET_RADIUS);
683
- const marked = markTokenRuns(text.slice(windowStart, windowEnd), tokens);
684
- const prefix = windowStart > 0 ? "…" : "";
685
- const suffix = windowEnd < text.length ? "…" : "";
686
- return `${prefix}${marked}${suffix}`;
687
- }
688
- /** Produce per-field matched-text snippets for a hit (GH-157, `--highlight`). Iterates the hit's already-sorted `matched_fields`, retains only the document-derived searchable fields (skipping synthetic markers like `id`, `semantic`, `rerank`, and `linked_content`), and emits a snippet for each field that still contains a concrete token match. */
689
- function buildHitHighlights(document, matchedFields, tokens) {
690
- const highlights = [];
691
- for (const field of matchedFields) {
692
- const builder = SEARCHABLE_FIELD_BUILDER_BY_NAME.get(field);
693
- if (builder === undefined) {
694
- continue;
695
- }
696
- const snippet = highlightFieldSnippet(builder.value(document), tokens);
697
- if (snippet !== null) {
698
- highlights.push({ field, snippet });
699
- }
700
- }
701
- return highlights;
702
- }
703
- function buildSearchableScoringFields(document, linkedCorpus, tuning) {
704
- return [
705
- ...SEARCHABLE_FIELD_BUILDERS.map((builder) => ({
706
- name: builder.name,
707
- value: builder.value(document),
708
- weight: tuning[builder.weightKey],
709
- })),
710
- {
711
- name: "linked_content",
712
- value: linkedCorpus,
713
- weight: tuning.linked_content_weight,
714
- },
715
- ];
716
- }
717
- function buildExactIdSearchHit(item, normalizedQuery, idPrefix) {
718
- const normalizedId = normalizeSearchPhrase(item.id);
719
- const normalizedIdPrefix = typeof idPrefix === "string" ? idPrefix.trim().toLowerCase() : "";
720
- const normalizedIdPrefixPhrase = normalizeSearchPhrase(normalizedIdPrefix);
721
- const normalizedShortId = normalizedIdPrefixPhrase.length > 0 &&
722
- normalizedId.startsWith(normalizedIdPrefixPhrase)
723
- ? normalizedId.slice(normalizedIdPrefixPhrase.length).trim()
724
- : normalizedId;
725
- if (normalizedQuery !== normalizedId &&
726
- normalizedQuery !== normalizedShortId) {
727
- return null;
728
- }
729
- return {
730
- item,
731
- score: normalizedQuery === normalizedId
732
- ? EXACT_ID_MATCH_SCORE
733
- : SHORT_ID_MATCH_SCORE,
734
- matched_fields: ["id"],
735
- matched_all_terms: true,
736
- exact_id_match: true,
737
- };
738
- }
739
- function scoreTokenMatches(tokens, titleTokenCounts, searchableFields, tuning, state) {
740
- for (const token of tokens) {
741
- const exactTitleMatches = titleTokenCounts.get(token) ?? 0;
742
- if (exactTitleMatches > 0) {
743
- state.score += exactTitleMatches * tuning.title_exact_bonus;
744
- state.matched.add("title");
745
- state.matchedTokens.add(token);
746
- }
747
- for (const field of searchableFields) {
748
- const occurrences = countOccurrences(field.value.toLowerCase(), token);
749
- if (occurrences > 0) {
750
- state.score += occurrences * field.weight;
751
- state.matched.add(field.name);
752
- state.matchedTokens.add(token);
753
- }
754
- }
755
- }
756
- }
757
- function scorePhraseMatches(item, normalizedQuery, searchableFields, state) {
758
- const normalizedTitle = normalizeSearchPhrase(item.title);
759
- if (normalizedTitle === normalizedQuery) {
760
- state.score += LONG_QUERY_TITLE_EXACT_BONUS;
761
- state.matched.add("title");
762
- }
763
- for (const field of searchableFields) {
764
- const phraseOccurrences = countOccurrences(normalizeSearchPhrase(field.value), normalizedQuery);
765
- if (phraseOccurrences > 0) {
766
- state.score +=
767
- phraseOccurrences * field.weight * LONG_QUERY_PHRASE_MULTIPLIER;
768
- state.matched.add(field.name);
769
- }
770
- }
771
- }
772
- function scoreDocument(document, tokens, normalizedQuery, linkedCorpus, tuning, idPrefix, applyCoverageBonus = false) {
773
- const item = document.metadata;
774
- const exactIdHit = buildExactIdSearchHit(item, normalizedQuery, idPrefix);
775
- if (exactIdHit) {
776
- return exactIdHit;
777
- }
778
- const titleTokenCounts = new Map();
779
- for (const token of tokenizeForExactTokenMatch(item.title)) {
780
- titleTokenCounts.set(token, (titleTokenCounts.get(token) ?? 0) + 1);
781
- }
782
- const searchableFields = buildSearchableScoringFields(document, linkedCorpus, tuning);
783
- const state = {
784
- score: 0,
785
- matched: new Set(),
786
- matchedTokens: new Set(),
787
- };
788
- scoreTokenMatches(tokens, titleTokenCounts, searchableFields, tuning, state);
789
- const distinctTokens = new Set(tokens);
790
- // matchedTokens only ever holds entries drawn from `tokens`, so it is always a
791
- // subset of distinctTokens — exact size equality means every distinct term matched.
792
- const matchedAllTerms = distinctTokens.size > 0 && state.matchedTokens.size === distinctTokens.size;
793
- // GH-181 default-mode all-terms ranking bonus: only meaningful for multi-token
794
- // queries where every distinct token matched somewhere.
795
- if (applyCoverageBonus && distinctTokens.size > 1 && matchedAllTerms) {
796
- state.score += ALL_TERMS_COVERAGE_BONUS;
797
- }
798
- const isLongPhraseQuery = tokens.length >= LONG_QUERY_TOKEN_THRESHOLD &&
799
- normalizedQuery.includes(" ");
800
- if (isLongPhraseQuery) {
801
- scorePhraseMatches(item, normalizedQuery, searchableFields, state);
802
- }
803
- if (state.score <= 0) {
804
- return null;
805
- }
806
- return {
807
- item,
808
- score: state.score,
809
- matched_fields: [...state.matched].sort((a, b) => a.localeCompare(b)),
810
- matched_all_terms: matchedAllTerms,
811
- };
812
- }
813
- function sortHits(items, statusRegistry) {
814
- return [...items].sort((a, b) => {
815
- const byScore = b.score - a.score;
816
- if (byScore !== 0)
817
- return byScore;
818
- const aTerminal = isTerminalStatus(a.item.status, statusRegistry);
819
- const bTerminal = isTerminalStatus(b.item.status, statusRegistry);
820
- if (aTerminal !== bTerminal) {
821
- return aTerminal ? 1 : -1;
822
- }
823
- const byPriority = a.item.priority - b.item.priority;
824
- if (byPriority !== 0)
825
- return byPriority;
826
- const byUpdated = compareTimestampStrings(b.item.updated_at, a.item.updated_at);
827
- if (byUpdated !== 0)
828
- return byUpdated;
829
- return a.item.id.localeCompare(b.item.id);
830
- });
831
- }
832
- function buildHybridLexicalScore(document, tokens, normalizedQuery, includeLinked, linkedCorpusById, tuning, idPrefix, applyCoverageBonus = false) {
833
- /* c8 ignore start -- linked corpus presence branch is covered by keyword/hybrid integration query tests */
834
- return scoreDocument(document, tokens, normalizedQuery, includeLinked ? (linkedCorpusById.get(document.metadata.id) ?? "") : "", tuning, idPrefix, applyCoverageBonus);
835
- /* c8 ignore stop */
836
- }
837
- function normalizeScoreMap(scoreById) {
838
- if (scoreById.size === 0) {
839
- return new Map();
840
- }
841
- const values = [...scoreById.values()];
842
- const minScore = Math.min(...values);
843
- const maxScore = Math.max(...values);
844
- if (maxScore === minScore) {
845
- return new Map([...scoreById.keys()].map((id) => [id, 1]));
846
- }
847
- const normalized = new Map();
848
- for (const [id, score] of scoreById) {
849
- normalized.set(id, (score - minScore) / (maxScore - minScore));
850
- }
851
- return normalized;
852
- }
853
- /* c8 ignore start -- empty-result projection/count/warnings shape matrix is validated by integration response-contract tests */
854
- function emptySearchResult(query, mode, matchMode, options, includeLinked, scoreThreshold, hybridSemanticWeight, queryExpansion, rerank, projection, warnings, runtimeFieldFilters, countOnly = false) {
855
- // --count consistency: a count-only query that matches nothing must still
856
- // carry the same { count_only: true, total } shape as the non-empty path.
857
- const countExtras = countOnly ? { total: 0, count_only: true } : {};
858
- const compactSummaryMode = projection.mode === "compact" && options.compact === true;
859
- if (compactSummaryMode) {
860
- const compactFilters = buildCompactSearchFilterSummary({
861
- mode,
862
- matchMode,
863
- options,
864
- includeLinked,
865
- titleExact: options.titleExact === true,
866
- phraseExact: options.phraseExact === true,
867
- scoreThreshold,
868
- hybridSemanticWeight,
869
- runtimeFieldFilters,
870
- });
871
- return {
872
- query: query.trim(),
873
- mode,
874
- items: [],
875
- count: 0,
876
- ...countExtras,
877
- filters: compactFilters,
878
- ...(warnings.length > 0 ? { warnings } : {}),
879
- };
880
- }
881
- const projectionFields = projection.mode === "full" ? null : [...projection.fields];
882
- return {
883
- query: query.trim(),
884
- mode,
885
- items: [],
886
- count: 0,
887
- ...countExtras,
888
- filters: buildVerboseSearchFilters({
889
- effectiveMode: mode,
890
- matchMode,
891
- options,
892
- includeLinked,
893
- titleExact: options.titleExact === true,
894
- phraseExact: options.phraseExact === true,
895
- scoreThreshold,
896
- hybridSemanticWeight,
897
- queryExpansion,
898
- rerank,
899
- runtimeFieldFilters: runtimeFieldFilters ?? {},
900
- }),
901
- projection: {
902
- mode: projection.mode,
903
- fields: projectionFields,
904
- },
905
- now: nowIso(),
906
- ...(warnings.length > 0 ? { warnings } : {}),
907
- };
908
- }
909
- /* c8 ignore stop */
910
- function requireSemanticDependencies(requestedMode, providerResolution, vectorResolution, hasExtensionVectorQuery) {
911
- if (!providerResolution.active) {
912
- throw new PmCliError(`Search mode '${requestedMode}' requires a configured embedding provider in settings.providers.openai or settings.providers.ollama`, EXIT_CODE.USAGE);
913
- }
914
- if (!vectorResolution.active && !hasExtensionVectorQuery) {
915
- throw new PmCliError(`Search mode '${requestedMode}' requires a configured vector store in settings.vector_store.qdrant/settings.vector_store.lancedb or an extension adapter selected by settings.vector_store.adapter`, EXIT_CODE.USAGE);
916
- }
917
- return {
918
- provider: providerResolution.active,
919
- vectorStore: vectorResolution.active ?? null,
920
- };
921
- }
922
- const toOptionalNonEmptyString = toNonEmptyStringOrUndefined;
923
- /* c8 ignore start -- extension search-provider registration permutations are covered by extension integration suites */
924
- function resolveExtensionSearchProviderByName(providerName) {
925
- const registrations = getActiveExtensionRegistrations();
926
- const resolved = resolveRegisteredSearchProvider(registrations, providerName);
927
- if (!resolved) {
928
- return null;
929
- }
930
- const runtimeDefinition = resolved.runtime_definition ?? resolved.definition;
931
- const query = runtimeDefinition.query;
932
- const queryExpansion = runtimeDefinition.queryExpansion ??
933
- runtimeDefinition.query_expansion;
934
- const rerank = runtimeDefinition.rerank;
935
- const registeredName = toOptionalNonEmptyString(runtimeDefinition.name) ??
936
- toOptionalNonEmptyString(resolved.definition.name) ??
937
- providerName;
938
- /* c8 ignore next 2 -- providerName is required for lookup and remains a non-empty fallback */
939
- if (!registeredName) {
940
- return null;
941
- }
942
- const hooks = {
943
- providerName: registeredName,
944
- ...(typeof query === "function"
945
- ? { query: query }
946
- : {}),
947
- ...(typeof queryExpansion === "function"
948
- ? {
949
- queryExpansion: queryExpansion,
950
- }
951
- : {}),
952
- ...(typeof rerank === "function"
953
- ? { rerank: rerank }
954
- : {}),
955
- };
956
- if (!hooks.query && !hooks.queryExpansion && !hooks.rerank) {
957
- return null;
958
- }
959
- return hooks;
960
- }
961
- /* c8 ignore stop */
962
- function resolveExtensionSearchProvider(settings) {
963
- const providerName = toOptionalNonEmptyString(settings.search?.provider);
964
- const resolved = resolveExtensionSearchProviderByName(providerName);
965
- if (!resolved?.query) {
966
- return null;
967
- }
968
- return {
969
- providerName: resolved.providerName,
970
- query: resolved.query,
971
- };
972
- }
973
- /* c8 ignore start -- vector adapter runtime definition permutations are covered by extension integration suites */
974
- function resolveExtensionVectorAdapter(settings) {
975
- const registrations = getActiveExtensionRegistrations();
976
- const adapterName = toOptionalNonEmptyString(settings.vector_store?.adapter);
977
- const resolved = resolveRegisteredVectorStoreAdapter(registrations, adapterName);
978
- if (!resolved) {
979
- return null;
980
- }
981
- const runtimeDefinition = resolved.runtime_definition ?? resolved.definition;
982
- const query = runtimeDefinition.query;
983
- if (typeof query !== "function") {
984
- return null;
985
- }
986
- const upsert = runtimeDefinition.upsert;
987
- const runtimeAdapterName = toOptionalNonEmptyString(runtimeDefinition.name) ??
988
- toOptionalNonEmptyString(resolved.definition.name) ??
989
- adapterName ??
990
- "extension";
991
- return {
992
- adapterName: runtimeAdapterName,
993
- query: query,
994
- ...(typeof upsert === "function"
995
- ? { upsert: upsert }
996
- : {}),
997
- };
998
- }
999
- /* c8 ignore stop */
1000
- function normalizeExtensionProviderHits(providerName, raw, filteredById) {
1001
- const rawHits = Array.isArray(raw)
1002
- ? raw
1003
- : raw?.hits;
1004
- if (!Array.isArray(rawHits)) {
1005
- throw new PmCliError(`Extension search provider "${providerName}" must return an array of hits or { hits: [...] }`, EXIT_CODE.GENERIC_FAILURE);
1006
- }
1007
- const seen = new Set();
1008
- const hits = [];
1009
- for (const rawHit of rawHits) {
1010
- if (typeof rawHit !== "object" || rawHit === null) {
1011
- continue;
1012
- }
1013
- const id = toOptionalNonEmptyString(rawHit.id);
1014
- const score = rawHit.score;
1015
- if (!id ||
1016
- typeof score !== "number" ||
1017
- !Number.isFinite(score) ||
1018
- seen.has(id)) {
1019
- continue;
1020
- }
1021
- const document = filteredById.get(id);
1022
- if (!document) {
1023
- continue;
1024
- }
1025
- const matchedFieldsRaw = rawHit
1026
- .matched_fields;
1027
- const matchedFields = Array.isArray(matchedFieldsRaw) &&
1028
- matchedFieldsRaw.every((entry) => typeof entry === "string")
1029
- ? [
1030
- ...new Set(matchedFieldsRaw
1031
- .map((entry) => entry.trim())
1032
- .filter((entry) => entry.length > 0)),
1033
- ].sort((a, b) => a.localeCompare(b))
1034
- : [`provider:${providerName}`];
1035
- seen.add(id);
1036
- hits.push({
1037
- item: document.metadata,
1038
- score,
1039
- matched_fields: matchedFields,
1040
- });
1041
- }
1042
- return hits;
1043
- }
1044
- function buildSemanticHits(vectorHits, filteredById) {
1045
- const semanticHits = [];
1046
- const semanticScores = new Map();
1047
- for (const vectorHit of vectorHits) {
1048
- if (semanticScores.has(vectorHit.id)) {
1049
- continue;
1050
- }
1051
- const document = filteredById.get(vectorHit.id);
1052
- if (!document) {
1053
- continue;
1054
- }
1055
- semanticScores.set(vectorHit.id, vectorHit.score);
1056
- semanticHits.push({
1057
- item: document.metadata,
1058
- score: vectorHit.score,
1059
- matched_fields: ["semantic"],
1060
- });
1061
- }
1062
- return {
1063
- semanticHits,
1064
- semanticScores,
1065
- };
1066
- }
1067
- // GH-281: in semantic & hybrid mode the blended/vector scores live on an
1068
- // arbitrary scale (blended hybrid scores are [0,1]; raw vector similarities are
1069
- // provider-defined), so an exact full-ID or short-ID keyword match can be
1070
- // out-ranked by a high-semantic body mention. This reattaches the exact-ID
1071
- // keyword hit(s) to the top of the ranked set in a reserved band ABOVE every
1072
- // other hit, preserving matched_fields:["id"] and keeping full-ID above
1073
- // short-ID. The reserved scores are derived from the current max so the
1074
- // guarantee holds no matter what scale the backend returns, and the hit is
1075
- // re-inserted even if it was dropped from `rankedHits` (e.g. it had no semantic
1076
- // vector match) so it is never lost to the threshold or the result-limit slice.
1077
- function forceExactIdHitsToTop(rankedHits, keywordHits) {
1078
- const exactIdHits = keywordHits.filter((hit) => hit.exact_id_match === true);
1079
- if (exactIdHits.length === 0) {
1080
- return rankedHits;
1081
- }
1082
- const exactIdIds = new Set(exactIdHits.map((hit) => hit.item.id));
1083
- const remaining = rankedHits.filter((hit) => !exactIdIds.has(hit.item.id));
1084
- // reduce (not Math.max(...spread)) so a very large pre-truncation candidate
1085
- // set can never overflow the call stack.
1086
- const maxRemainingScore = remaining.reduce((max, hit) => Math.max(max, hit.score), 0);
1087
- // Full-ID hits (score 1000) must rank above short-ID hits (score 900): rank
1088
- // within the exact-ID band by the original keyword score (descending) so a
1089
- // full-ID match always precedes a short-ID match for the same query, then
1090
- // offset the whole band above every remaining hit. ids are unique, so two
1091
- // exact-ID hits can never tie on score — sorting on score alone is total.
1092
- // exactIdHits is already a fresh array from keywordHits.filter(...), so sort
1093
- // it in place — no defensive copy needed.
1094
- const orderedExactHits = exactIdHits.sort((left, right) => right.score - left.score);
1095
- const bandBase = maxRemainingScore + orderedExactHits.length + 1;
1096
- const promoted = orderedExactHits.map((hit, index) => ({
1097
- // Spread the original keyword hit so flags like matched_all_terms /
1098
- // exact_id_match are preserved; only the band-slot score and the id-only
1099
- // matched_fields are overridden.
1100
- ...hit,
1101
- // Higher band slot for earlier (higher original keyword score) hits.
1102
- score: bandBase - index,
1103
- matched_fields: ["id"],
1104
- }));
1105
- return [...promoted, ...remaining];
1106
- }
1107
- function combineHybridHits(filteredById, semanticScores, keywordHits, hybridSemanticWeight,
1108
- // Matched-field label for the dense-ranking component. Defaults to "semantic"
1109
- // (vector retrieval); the offline BM25 hybrid path passes "bm25" so hits carry
1110
- // an honest provenance marker instead of implying a vector match (pm-75k9).
1111
- semanticFieldLabel = "semantic") {
1112
- const keywordScores = new Map(keywordHits.map((entry) => [entry.item.id, entry.score]));
1113
- const keywordMatches = new Map(keywordHits.map((entry) => [entry.item.id, entry.matched_fields]));
1114
- const normalizedSemantic = normalizeScoreMap(semanticScores);
1115
- const normalizedKeyword = normalizeScoreMap(keywordScores);
1116
- const candidateIds = new Set([
1117
- ...semanticScores.keys(),
1118
- ...keywordScores.keys(),
1119
- ]);
1120
- const keywordWeight = 1 - hybridSemanticWeight;
1121
- return [...candidateIds]
1122
- .map((id) => {
1123
- const document = filteredById.get(id);
1124
- const semanticScore = normalizedSemantic.get(id) ?? 0;
1125
- const keywordScore = normalizedKeyword.get(id) ?? 0;
1126
- const combinedScore = semanticScore * hybridSemanticWeight + keywordScore * keywordWeight;
1127
- if (combinedScore <= 0) {
1128
- return null;
1129
- }
1130
- const matchedFields = new Set();
1131
- if (semanticScores.has(id)) {
1132
- matchedFields.add(semanticFieldLabel);
1133
- }
1134
- for (const field of keywordMatches.get(id) ?? []) {
1135
- matchedFields.add(field);
1136
- }
1137
- return {
1138
- item: document.metadata,
1139
- score: combinedScore,
1140
- matched_fields: [...matchedFields].sort((a, b) => a.localeCompare(b)),
1141
- };
1142
- })
1143
- .filter((entry) => entry !== null);
1144
- }
1145
- function resolveBuiltInBm25Mode(params) {
1146
- const normalized = params.configuredProvider?.trim().toLowerCase();
1147
- if (normalized === "bm25") {
1148
- return "explicit";
1149
- }
1150
- if (normalized === "auto" &&
1151
- !params.hasEmbeddingProvider &&
1152
- !params.hasExtensionSearchProvider) {
1153
- return "auto-fallback";
1154
- }
1155
- return null;
1156
- }
1157
- /**
1158
- * Rank the filtered corpus with the offline BM25 provider (pm-75k9). Builds a
1159
- * BM25 index over each document's resolved search corpus (honoring
1160
- * `search.corpus_fields`), scores the query, and shapes hits for the requested
1161
- * mode: semantic mode returns pure BM25-ranked hits tagged
1162
- * `matched_fields: ["bm25"]`, while hybrid mode blends the BM25 scores with the
1163
- * locally computed keyword scores via {@link combineHybridHits}. The exact
1164
- * full-ID / short-ID guarantee is preserved in both modes via
1165
- * {@link forceExactIdHitsToTop}. No network, embedding service, or vector store
1166
- * is involved, so this path cannot fail on a backend error.
1167
- */
1168
- function computeBuiltInBm25Hits(params) {
1169
- const bm25Documents = params.filteredDocuments.map((document) => ({
1170
- id: document.metadata.id,
1171
- text: flattenSearchCorpusText(buildSearchCorpus(document, { fields: params.corpusFields })),
1172
- }));
1173
- const index = buildBm25Index(bm25Documents);
1174
- const bm25Scores = scoreBm25Query(index, tokenizeBm25(params.query), params.bm25Params);
1175
- const filteredById = new Map(params.filteredDocuments.map((document) => [
1176
- document.metadata.id,
1177
- document,
1178
- ]));
1179
- if (params.requestedMode === "semantic") {
1180
- const semanticHits = [...bm25Scores].map(([id, score]) => ({
1181
- item: filteredById.get(id).metadata,
1182
- score,
1183
- matched_fields: ["bm25"],
1184
- }));
1185
- return forceExactIdHitsToTop(semanticHits, params.keywordHits);
1186
- }
1187
- const hybridHits = combineHybridHits(filteredById, bm25Scores, params.keywordHits, params.hybridSemanticWeight, "bm25");
1188
- return forceExactIdHitsToTop(hybridHits, params.keywordHits);
1189
- }
1190
- function mergeVectorHitsById(vectorHitGroups) {
1191
- const bestById = new Map();
1192
- for (const group of vectorHitGroups) {
1193
- for (const hit of group) {
1194
- const existing = bestById.get(hit.id);
1195
- if (!existing || hit.score > existing.score) {
1196
- bestById.set(hit.id, hit);
1197
- }
1198
- }
1199
- }
1200
- const merged = [...bestById.values()];
1201
- merged.sort((left, right) => {
1202
- if (left.score !== right.score) {
1203
- return right.score - left.score;
1204
- }
1205
- return left.id.localeCompare(right.id);
1206
- });
1207
- return merged;
1208
- }
1209
- /* c8 ignore start -- rerank corpus metadata-shape permutations are covered by semantic integration tests */
1210
- function buildRerankCorpus(document) {
1211
- const metadata = document
1212
- .metadata;
1213
- const tags = Array.isArray(metadata?.tags) ? metadata.tags.join(" ") : "";
1214
- return [
1215
- metadata?.title,
1216
- metadata?.description,
1217
- metadata?.type,
1218
- metadata?.status,
1219
- tags,
1220
- document.body,
1221
- ]
1222
- .map((entry) => (typeof entry === "string" ? entry.trim() : ""))
1223
- .filter((entry) => entry.length > 0)
1224
- .join("\n");
1225
- }
1226
- /* c8 ignore stop */
1227
- /* c8 ignore start -- semantic expansion/rerank fallback matrices are covered by end-to-end semantic search tests */
1228
- async function resolveExpandedSemanticQueries(context, queryTrimmed) {
1229
- const baseExpandedQueries = context.queryExpansion.enabled
1230
- ? buildDeterministicQueryExpansions(queryTrimmed, context.queryExpansion.max_queries)
1231
- : [queryTrimmed];
1232
- const expandedQueries = baseExpandedQueries.length > 0 ? baseExpandedQueries : [queryTrimmed];
1233
- if (!context.queryExpansion.enabled) {
1234
- return expandedQueries;
1235
- }
1236
- if (context.queryExpansionExtension?.expand) {
1237
- try {
1238
- const rawExpansion = await Promise.resolve(context.queryExpansionExtension.expand({
1239
- query: queryTrimmed,
1240
- mode: context.requestedMode,
1241
- settings: context.settings,
1242
- }));
1243
- return mergeQueryExpansions(expandedQueries, normalizeQueryExpansionOutput(rawExpansion), context.queryExpansion.max_queries);
1244
- }
1245
- catch {
1246
- context.warnings.push(`search_query_expansion_provider_failed:${context.queryExpansionExtension.providerName}:using_builtin`);
1247
- return expandedQueries;
1248
- }
1249
- }
1250
- if (context.queryExpansion.provider &&
1251
- context.queryExpansion.provider !== "openai" &&
1252
- context.queryExpansion.provider !== "ollama") {
1253
- context.warnings.push(`search_query_expansion_provider_unavailable:${context.queryExpansion.provider}:using_builtin`);
1254
- }
1255
- return expandedQueries;
1256
- }
1257
- async function executeSemanticVectorQuery(context, semanticVector, semanticLimit, vectorQueryOptions) {
1258
- if (context.extensionVectorAdapter?.query) {
1259
- try {
1260
- return await Promise.resolve(context.extensionVectorAdapter.query({
1261
- vector: semanticVector,
1262
- limit: semanticLimit,
1263
- settings: context.settings,
1264
- }));
1265
- }
1266
- catch (error) {
1267
- if (!context.vectorStore) {
1268
- throw new PmCliError(`Extension vector adapter query failed and no built-in fallback store is configured (${error instanceof Error ? error.message : String(error)})`, EXIT_CODE.GENERIC_FAILURE);
1269
- }
1270
- context.warnings.push(`search_vector_adapter_failed:${context.extensionVectorAdapter.adapterName}:using_builtin`);
1271
- return await executeVectorQuery(context.vectorStore, semanticVector, semanticLimit, vectorQueryOptions);
1272
- }
1273
- }
1274
- if (context.vectorStore) {
1275
- return await executeVectorQuery(context.vectorStore, semanticVector, semanticLimit, vectorQueryOptions);
1276
- }
1277
- throw new PmCliError("Semantic search requires either a configured vector store or an extension vector adapter query handler", EXIT_CODE.USAGE);
1278
- }
1279
- function buildRerankCandidateContexts(hybridHits, filteredById, topK) {
1280
- const sortedForCandidates = [...hybridHits].sort((left, right) => {
1281
- if (left.score !== right.score) {
1282
- return right.score - left.score;
1283
- }
1284
- return left.item.id.localeCompare(right.item.id);
1285
- });
1286
- return sortedForCandidates
1287
- .slice(0, topK)
1288
- .map((hit) => {
1289
- const document = filteredById.get(hit.item.id);
1290
- return document ? { hit, text: buildRerankCorpus(document) } : null;
1291
- })
1292
- .filter((entry) => entry !== null);
1293
- }
1294
- async function resolveExtensionRerankScores(context, queryTrimmed, candidateContexts) {
1295
- if (!context.rerankExtension?.rerank) {
1296
- return null;
1297
- }
1298
- try {
1299
- const rawRerank = await Promise.resolve(context.rerankExtension.rerank({
1300
- query: queryTrimmed,
1301
- mode: "hybrid",
1302
- model: context.rerank.model,
1303
- top_k: context.rerank.top_k,
1304
- settings: context.settings,
1305
- candidates: candidateContexts.map((entry) => ({
1306
- id: entry.hit.item.id,
1307
- text: entry.text,
1308
- score: entry.hit.score,
1309
- })),
1310
- }));
1311
- const normalizedRerank = normalizeRerankOutput(rawRerank);
1312
- if (normalizedRerank.length > 0) {
1313
- return new Map(normalizedRerank.map((entry) => [entry.id, entry.score]));
1314
- }
1315
- context.warnings.push(`search_rerank_provider_invalid_response:${context.rerankExtension.providerName}:using_builtin`);
1316
- return null;
1317
- }
1318
- catch {
1319
- context.warnings.push(`search_rerank_provider_failed:${context.rerankExtension.providerName}:using_builtin`);
1320
- return null;
1321
- }
1322
- }
1323
- async function resolveRerankScores(context, queryTrimmed, candidateContexts) {
1324
- const extensionScores = await resolveExtensionRerankScores(context, queryTrimmed, candidateContexts);
1325
- if (extensionScores) {
1326
- return extensionScores;
1327
- }
1328
- const rerankCandidates = candidateContexts.map((entry) => ({
1329
- id: entry.hit.item.id,
1330
- text: entry.text,
1331
- }));
1332
- try {
1333
- return await rerankCandidatesWithEmbeddings(context.provider, context.rerank.model, queryTrimmed, rerankCandidates, context.embeddingTimeoutMs);
1334
- }
1335
- catch {
1336
- context.warnings.push("search_rerank_failed:using_hybrid_scores");
1337
- return null;
1338
- }
1339
- }
1340
- function applyRerankScores(hybridHits, rerankScores) {
1341
- const rerankedIds = new Set(rerankScores.keys());
1342
- const rerankedHits = hybridHits.map((hit) => {
1343
- const rerankScore = rerankScores.get(hit.item.id);
1344
- if (rerankScore === undefined) {
1345
- return hit;
1346
- }
1347
- const matchedFields = new Set(hit.matched_fields);
1348
- matchedFields.add("rerank");
1349
- return {
1350
- ...hit,
1351
- score: rerankScore,
1352
- matched_fields: [...matchedFields].sort((left, right) => left.localeCompare(right)),
1353
- };
1354
- });
1355
- rerankedHits.sort((left, right) => {
1356
- const leftWasReranked = rerankedIds.has(left.item.id);
1357
- const rightWasReranked = rerankedIds.has(right.item.id);
1358
- if (leftWasReranked !== rightWasReranked) {
1359
- return leftWasReranked ? -1 : 1;
1360
- }
1361
- if (left.score !== right.score) {
1362
- return right.score - left.score;
1363
- }
1364
- return left.item.id.localeCompare(right.item.id);
1365
- });
1366
- return rerankedHits;
1367
- }
1368
- async function applyHybridRerank(context, queryTrimmed, hybridHits, filteredById) {
1369
- if (!context.rerank.enabled || hybridHits.length <= 1) {
1370
- return hybridHits;
1371
- }
1372
- const candidateContexts = buildRerankCandidateContexts(hybridHits, filteredById, context.rerank.top_k);
1373
- const rerankScores = await resolveRerankScores(context, queryTrimmed, candidateContexts);
1374
- return rerankScores && rerankScores.size > 0
1375
- ? applyRerankScores(hybridHits, rerankScores)
1376
- : hybridHits;
1377
- }
1378
- async function computeSemanticOrHybridHits(context) {
1379
- const semanticLimit = Math.max(context.limit ?? context.maxResults, context.maxResults);
1380
- const embeddingOptions = context.embeddingTimeoutMs !== undefined
1381
- ? { timeout_ms: context.embeddingTimeoutMs }
1382
- : {};
1383
- const vectorQueryOptions = context.vectorQueryTimeoutMs !== undefined
1384
- ? { timeout_ms: context.vectorQueryTimeoutMs }
1385
- : {};
1386
- const queryTrimmed = context.query.trim();
1387
- const expandedQueries = await resolveExpandedSemanticQueries(context, queryTrimmed);
1388
- const queryVectors = await executeEmbeddingRequest(context.provider, expandedQueries, embeddingOptions);
1389
- const queryVectorGroups = await Promise.all(queryVectors.map(async (semanticVector) => await executeSemanticVectorQuery(context, semanticVector, semanticLimit, vectorQueryOptions)));
1390
- const vectorHits = mergeVectorHitsById(queryVectorGroups);
1391
- const filteredById = new Map(context.filteredDocuments.map((document) => [
1392
- document.metadata.id,
1393
- document,
1394
- ]));
1395
- const { semanticHits, semanticScores } = buildSemanticHits(vectorHits, filteredById);
1396
- const vectorMatchCount = semanticScores.size;
1397
- if (context.requestedMode === "semantic") {
1398
- // GH-281: guarantee an exact full-ID / short-ID match ranks #1 even in pure
1399
- // semantic mode, where it otherwise carries no vector hit at all.
1400
- return {
1401
- hits: forceExactIdHitsToTop(semanticHits, context.keywordHits),
1402
- vectorMatchCount,
1403
- };
1404
- }
1405
- const hybridHits = await applyHybridRerank(context, queryTrimmed, combineHybridHits(filteredById, semanticScores, context.keywordHits, context.hybridSemanticWeight), filteredById);
1406
- return {
1407
- // GH-281: applied LAST (after any rerank reordering) so an exact full-ID /
1408
- // short-ID keyword match always ranks #1 in hybrid mode regardless of the
1409
- // semantic blend weight or rerank scores.
1410
- hits: forceExactIdHitsToTop(hybridHits, context.keywordHits),
1411
- vectorMatchCount,
1412
- };
1413
- }
1414
- /* c8 ignore stop */
1415
- /* c8 ignore start -- item body fallback read-path combinations are covered by document-cache integration tests */
1416
- async function loadDocuments(pmRoot, itemFormat, typeToFolder, schema) {
1417
- const extensionFieldNames = collectRegisteredItemFieldNames(getActiveExtensionRegistrations());
1418
- const readDocumentBody = async (metadata, preferredPath, preferredFormat) => {
1419
- const tryRead = async (targetPath, format) => {
1420
- await runActiveOnReadHooks({ path: targetPath, scope: "project" });
1421
- const raw = await fs.readFile(targetPath, "utf8");
1422
- const parsed = parseItemDocument(raw, {
1423
- format,
1424
- schema,
1425
- extensionFieldNames,
1426
- onWarning: (warning) => listWarnings.push(warning),
1427
- });
1428
- return parsed.body;
1429
- };
1430
- try {
1431
- return await tryRead(preferredPath, preferredFormat);
1432
- }
1433
- catch {
1434
- const alternateFormat = preferredFormat === "toon" ? "json_markdown" : "toon";
1435
- const alternatePath = getItemPath(pmRoot, metadata.type, metadata.id, alternateFormat, typeToFolder);
1436
- try {
1437
- return await tryRead(alternatePath, alternateFormat);
1438
- }
1439
- catch {
1440
- listWarnings.push(`item_list_item_read_failed:${path.relative(pmRoot, alternatePath)}`);
1441
- return "";
1442
- }
1443
- }
1444
- };
1445
- const listWarnings = [];
1446
- const cachedDocuments = await listAllDocumentCandidatesCached(pmRoot, itemFormat, typeToFolder, listWarnings, schema);
1447
- const documents = [];
1448
- if (cachedDocuments.length === 0) {
1449
- const itemMetadataDocuments = await listAllItemMetadata(pmRoot, itemFormat, typeToFolder, listWarnings, schema);
1450
- for (const metadata of itemMetadataDocuments) {
1451
- const preferredPath = getItemPath(pmRoot, metadata.type, metadata.id, itemFormat, typeToFolder);
1452
- const body = await readDocumentBody(metadata, preferredPath, itemFormat);
1453
- documents.push({ metadata, body });
1454
- }
1455
- return {
1456
- documents,
1457
- warnings: [...new Set(listWarnings)].sort((left, right) => left.localeCompare(right)),
1458
- };
1459
- }
1460
- for (const cachedDocument of cachedDocuments) {
1461
- if (typeof cachedDocument.body === "string") {
1462
- documents.push({
1463
- metadata: cachedDocument.metadata,
1464
- body: cachedDocument.body,
1465
- });
1466
- continue;
1467
- }
1468
- const body = await readDocumentBody(cachedDocument.metadata, cachedDocument.item_path, cachedDocument.item_format);
1469
- documents.push({
1470
- metadata: cachedDocument.metadata,
1471
- body,
1472
- });
1473
- }
1474
- return {
1475
- documents,
1476
- warnings: [...new Set(listWarnings)].sort((left, right) => left.localeCompare(right)),
1477
- };
1478
- }
1479
- /* c8 ignore stop */
1480
- /* c8 ignore start -- field projection lookup precedence is validated by output-shaping integration tests */
1481
- function readSearchFieldValue(hit, field) {
1482
- const normalized = field.trim();
1483
- if (normalized.length === 0) {
1484
- return null;
1485
- }
1486
- if (normalized === "score") {
1487
- return hit.score;
1488
- }
1489
- if (normalized === "matched_fields") {
1490
- return hit.matched_fields;
1491
- }
1492
- if (normalized.startsWith("item.")) {
1493
- const itemKey = normalized.slice("item.".length);
1494
- if (itemKey.length === 0) {
1495
- return null;
1496
- }
1497
- const itemRecord = toItemRecord(hit.item);
1498
- return itemRecord[itemKey] ?? null;
1499
- }
1500
- const hitRecord = hit;
1501
- const itemRecord = toItemRecord(hit.item);
1502
- if (Object.prototype.hasOwnProperty.call(itemRecord, normalized)) {
1503
- return itemRecord[normalized] ?? null;
1504
- }
1505
- if (Object.prototype.hasOwnProperty.call(hitRecord, normalized)) {
1506
- return hitRecord[normalized] ?? null;
1507
- }
1508
- return null;
1509
- }
1510
- /* c8 ignore stop */
1511
- function projectSearchHits(hits, projection) {
1512
- if (projection.mode === "full") {
1513
- // matched_all_terms is an internal ranking signal (GH-181); strip it from
1514
- // full-mode output rows so the public hit shape stays { item, score, matched_fields }.
1515
- return hits.map(({ matched_all_terms: _matchedAllTerms, exact_id_match: _exactIdMatch, ...hit }) => hit);
1516
- }
1517
- return hits.map((hit) => {
1518
- const projected = {};
1519
- for (const field of projection.fields) {
1520
- projected[field] = readSearchFieldValue(hit, field);
1521
- }
1522
- return projected;
1523
- });
1524
- }
1525
- function resolveQueryExpansionExtension(queryExpansion) {
1526
- const provider = resolveExtensionSearchProviderByName(queryExpansion.provider ?? undefined);
1527
- return provider?.queryExpansion
1528
- ? { providerName: provider.providerName, expand: provider.queryExpansion }
1529
- : null;
1530
- }
1531
- function resolveRerankExtension(settings) {
1532
- const provider = resolveExtensionSearchProviderByName(toOptionalNonEmptyString(settings.search?.provider));
1533
- return provider?.rerank
1534
- ? { providerName: provider.providerName, rerank: provider.rerank }
1535
- : null;
1536
- }
1537
- function prepareSearchInput(rawQuery, rawOptions) {
1538
- const inlineWarnings = [];
1539
- const inlineParse = parseInlineQueryFilters(rawQuery);
1540
- const hasInlineFilters = Object.keys(inlineParse.inlineFilters).length > 0;
1541
- if (hasInlineFilters && inlineParse.residualQuery.trim().length === 0) {
1542
- throw new PmCliError("Inline field:value tokens consumed the entire query, leaving no search terms.", EXIT_CODE.USAGE, {
1543
- examples: [
1544
- "pm search auth tag:area:search",
1545
- "pm list --tag area:search --status open",
1546
- ],
1547
- nextSteps: [
1548
- "Add keyword terms alongside the inline filters, or use pm list for pure tag/status/type/priority filtering.",
1549
- ],
1550
- });
1551
- }
1552
- const query = hasInlineFilters ? inlineParse.residualQuery : rawQuery;
1553
- const options = applyInlineQueryFilters(rawOptions, inlineParse.inlineFilters, inlineWarnings);
1554
- return {
1555
- query,
1556
- options,
1557
- inlineWarnings,
1558
- highlight: options.highlight === true,
1559
- includeLinked: parseSearchBoolean(options.includeLinked),
1560
- titleExact: parseSearchBoolean(options.titleExact),
1561
- phraseExact: parseSearchBoolean(options.phraseExact),
1562
- matchMode: parseSearchMatchMode(typeof options.matchMode === "string" ? options.matchMode : undefined),
1563
- minScoreOverride: parseMinScoreOverride(options.minScore),
1564
- countOnly: options.count === true,
1565
- tokens: parseSearchTokens(query),
1566
- normalizedQuery: normalizeSearchPhrase(query),
1567
- limit: parseLimit(options.limit),
1568
- projection: parseSearchProjection(options, rawQuery),
1569
- modeWasExplicit: typeof options.mode === "string" && options.mode.trim().length > 0,
1570
- };
1571
- }
1572
- async function resolveSearchRuntimeContext(prepared, global) {
1573
- const pmRoot = resolvePmRoot(process.cwd(), global.path);
1574
- await assertInitializedTracker(pmRoot);
1575
- const storedSettings = await readSettings(pmRoot);
1576
- const settings = resolveSettingsWithSemanticRuntimeDefaults(storedSettings).settings;
1577
- const statusRegistry = resolveRuntimeStatusRegistry(settings.schema);
1578
- const runtimeFieldRegistry = resolveRuntimeFieldRegistry(settings.schema);
1579
- validateSearchProjectionFields(prepared.projection, runtimeFieldRegistry, prepared.query);
1580
- const runtimeFieldFilters = collectRuntimeFilterValues(prepared.options, runtimeFieldRegistry, "search");
1581
- const typeRegistry = resolveItemTypeRegistry(settings, getActiveExtensionRegistrations());
1582
- const providerResolution = resolveEmbeddingProviders(settings);
1583
- const vectorResolution = resolveVectorStores(settings, pmRoot);
1584
- const extensionSearchProvider = resolveExtensionSearchProvider(settings);
1585
- const extensionVectorAdapter = resolveExtensionVectorAdapter(settings);
1586
- const semanticWeightOverride = parseSemanticWeightOverride(prepared.options.semanticWeight);
1587
- const queryExpansion = resolveQueryExpansionConfig(settings, providerResolution.active?.name ?? null);
1588
- const rerank = resolveRerankConfig(settings, providerResolution.active?.model ??
1589
- toOptionalNonEmptyString(settings.search?.embedding_model) ??
1590
- "text-embedding-3-small");
1591
- return {
1592
- pmRoot,
1593
- settings,
1594
- statusRegistry,
1595
- runtimeFieldRegistry,
1596
- runtimeFieldFilters,
1597
- typeRegistry,
1598
- maxResults: resolveSearchMaxResults(settings),
1599
- scoreThreshold: prepared.minScoreOverride ?? resolveSearchScoreThreshold(settings),
1600
- semanticWeightProvided: prepared.options.semanticWeight !== undefined,
1601
- semanticWeightOverride,
1602
- hybridSemanticWeight: semanticWeightOverride ?? resolveHybridSemanticWeight(settings),
1603
- tuning: resolveSearchTuning(settings),
1604
- providerResolution,
1605
- vectorResolution,
1606
- extensionSearchProvider,
1607
- extensionVectorAdapter,
1608
- bm25Mode: resolveBuiltInBm25Mode({
1609
- configuredProvider: toOptionalNonEmptyString(settings.search?.provider),
1610
- hasEmbeddingProvider: providerResolution.active !== null,
1611
- hasExtensionSearchProvider: extensionSearchProvider !== null,
1612
- }),
1613
- queryExpansion,
1614
- queryExpansionExtension: resolveQueryExpansionExtension(queryExpansion),
1615
- rerank,
1616
- rerankExtension: resolveRerankExtension(settings),
1617
- effectiveMode: parseSearchMode(prepared.options.mode),
1618
- };
1619
- }
1620
- async function loadFilteredSearchCorpus(prepared, runtime) {
1621
- const loadedDocuments = await loadDocuments(runtime.pmRoot, runtime.settings.item_format ?? "toon", runtime.typeRegistry.type_to_folder, runtime.settings.schema);
1622
- const statusFilter = parseStatusFilterCsv(typeof prepared.options.status === "string"
1623
- ? prepared.options.status
1624
- : undefined, runtime.statusRegistry, { strict: true });
1625
- const lifecycleClassifier = lifecycleClassifierFromStatusRegistry(runtime.statusRegistry);
1626
- const metadataFilteredDocuments = applyFilters(loadedDocuments.documents, prepared.options, runtime.typeRegistry, runtime.runtimeFieldFilters, statusFilter, lifecycleClassifier);
1627
- return {
1628
- warnings: loadedDocuments.warnings,
1629
- allDocuments: loadedDocuments.documents,
1630
- filteredDocuments: applyExactQueryFilters(metadataFilteredDocuments, prepared.normalizedQuery, {
1631
- titleExact: prepared.titleExact,
1632
- phraseExact: prepared.phraseExact || prepared.matchMode === "exact",
1633
- }),
1634
- };
1635
- }
1636
- async function collectLinkedCorpusById(includeLinked, effectiveMode, filteredDocuments) {
1637
- const linkedCorpusById = new Map();
1638
- if (!includeLinked ||
1639
- (effectiveMode !== "keyword" && effectiveMode !== "hybrid")) {
1640
- return linkedCorpusById;
1641
- }
1642
- const projectRoot = process.cwd();
1643
- const linkedCorpusRoots = await resolveLinkedCorpusRoots(projectRoot, resolveGlobalPmRoot(projectRoot));
1644
- const linkedCorpusEntries = await Promise.all(filteredDocuments.map(async (document) => [
1645
- document.metadata.id,
1646
- await loadLinkedCorpus(document, linkedCorpusRoots),
1647
- ]));
1648
- for (const [id, corpus] of linkedCorpusEntries) {
1649
- linkedCorpusById.set(id, corpus);
1650
- }
1651
- return linkedCorpusById;
1652
- }
1653
- async function computeKeywordSearchHits(prepared, runtime, filteredDocuments) {
1654
- const linkedCorpusById = await collectLinkedCorpusById(prepared.includeLinked, runtime.effectiveMode, filteredDocuments);
1655
- const applyCoverageBonus = prepared.matchMode === "or";
1656
- return filteredDocuments
1657
- .map((document) => buildHybridLexicalScore(document, prepared.tokens, prepared.normalizedQuery, prepared.includeLinked, linkedCorpusById, runtime.tuning, runtime.settings.id_prefix, applyCoverageBonus))
1658
- .filter((entry) => entry !== null)
1659
- .filter((entry) => prepared.matchMode !== "and" || entry.matched_all_terms === true);
1660
- }
1661
- function buildEmptySearchResultFromContext(response, countOnly) {
1662
- return emptySearchResult(response.query, response.effectiveMode, response.matchMode, response.options, response.includeLinked, response.scoreThreshold, response.hybridSemanticWeight, response.queryExpansion, response.rerank, response.projection, response.warnings, response.runtimeFieldFilters, countOnly);
1663
- }
1664
- async function executeBuiltInBm25Search(prepared, runtime, filteredDocuments, keywordHits, warnings) {
1665
- if (runtime.bm25Mode === null || runtime.effectiveMode === "keyword") {
1666
- return null;
1667
- }
1668
- const hits = computeBuiltInBm25Hits({
1669
- requestedMode: runtime.effectiveMode,
1670
- query: prepared.query,
1671
- filteredDocuments,
1672
- keywordHits,
1673
- corpusFields: resolveSearchCorpusFields(runtime.settings),
1674
- bm25Params: resolveBm25Params(runtime.settings),
1675
- hybridSemanticWeight: runtime.hybridSemanticWeight,
1676
- });
1677
- if (runtime.bm25Mode === "auto-fallback") {
1678
- warnings.push(`search_${runtime.effectiveMode}_offline_bm25:no_embedding_provider:using_lexical_bm25`);
1679
- }
1680
- return hits;
1681
- }
1682
- async function executeExtensionSearchProvider(prepared, runtime, filteredDocuments, filteredById) {
1683
- if (!runtime.extensionSearchProvider) {
1684
- return null;
1685
- }
1686
- const canUseBuiltInSemantic = runtime.providerResolution.active !== null &&
1687
- (runtime.vectorResolution.active !== null ||
1688
- runtime.extensionVectorAdapter !== null);
1689
- try {
1690
- const providerResponse = await Promise.resolve(runtime.extensionSearchProvider.query({
1691
- query: prepared.query,
1692
- mode: runtime.effectiveMode,
1693
- tokens: prepared.tokens,
1694
- options: prepared.options,
1695
- settings: runtime.settings,
1696
- documents: filteredDocuments,
1697
- }));
1698
- return normalizeExtensionProviderHits(runtime.extensionSearchProvider.providerName, providerResponse, filteredById);
1699
- }
1700
- catch (error) {
1701
- if (!canUseBuiltInSemantic) {
1702
- throw new PmCliError(`Extension search provider "${runtime.extensionSearchProvider.providerName}" failed: ${error instanceof Error ? error.message : String(error)}`, EXIT_CODE.GENERIC_FAILURE);
1703
- }
1704
- return null;
1705
- }
1706
- }
1707
- function maybeApplyNoVectorFallback(effectiveMode, semanticResult, keywordHits, warnings) {
1708
- if (semanticResult.vectorMatchCount > 0) {
1709
- return semanticResult.hits;
1710
- }
1711
- warnings.push(`search_${effectiveMode}_degraded:no_vector_matches:results_are_lexical`);
1712
- return effectiveMode === "semantic" ? keywordHits : semanticResult.hits;
1713
- }
1714
- async function prepareBuiltInSemanticSearch(runtime, effectiveMode, filteredDocuments, warnings, modeWasExplicit) {
1715
- const builtInSemanticWillRun = !runtime.extensionSearchProvider &&
1716
- runtime.providerResolution.active !== null &&
1717
- (runtime.vectorResolution.active !== null ||
1718
- runtime.extensionVectorAdapter !== null);
1719
- if (modeWasExplicit && builtInSemanticWillRun) {
1720
- await maybeEmitVectorIndexStaleWarning(runtime.pmRoot, filteredDocuments, warnings);
1721
- }
1722
- if (!runtime.extensionSearchProvider) {
1723
- requireSemanticDependencies(effectiveMode, runtime.providerResolution, runtime.vectorResolution, runtime.extensionVectorAdapter !== null);
1724
- }
1725
- }
1726
- async function executeSemanticSearch(input) {
1727
- const { prepared, runtime, filteredDocuments, keywordHits, warnings, modeWasExplicit, } = input;
1728
- let hits = keywordHits;
1729
- const bm25Hits = await executeBuiltInBm25Search(prepared, runtime, filteredDocuments, keywordHits, warnings);
1730
- const semanticMode = runtime.effectiveMode;
1731
- if (bm25Hits !== null || semanticMode === "keyword") {
1732
- return { effectiveMode: runtime.effectiveMode, hits: bm25Hits ?? hits };
1733
- }
1734
- try {
1735
- await prepareBuiltInSemanticSearch(runtime, semanticMode, filteredDocuments, warnings, modeWasExplicit);
1736
- if (filteredDocuments.length === 0 || prepared.limit === 0) {
1737
- return { effectiveMode: runtime.effectiveMode, hits: [] };
1738
- }
1739
- const filteredById = new Map(filteredDocuments.map((document) => [document.metadata.id, document]));
1740
- hits =
1741
- (await executeExtensionSearchProvider(prepared, runtime, filteredDocuments, filteredById)) ?? hits;
1742
- if (hits === keywordHits) {
1743
- const { provider, vectorStore } = requireSemanticDependencies(semanticMode, runtime.providerResolution, runtime.vectorResolution, runtime.extensionVectorAdapter !== null);
1744
- const semanticResult = await computeSemanticOrHybridHits({
1745
- requestedMode: semanticMode,
1746
- query: prepared.query,
1747
- filteredDocuments,
1748
- keywordHits,
1749
- hybridSemanticWeight: runtime.hybridSemanticWeight,
1750
- limit: prepared.limit,
1751
- maxResults: runtime.maxResults,
1752
- provider,
1753
- vectorStore,
1754
- extensionVectorAdapter: runtime.extensionVectorAdapter,
1755
- queryExpansion: runtime.queryExpansion,
1756
- queryExpansionExtension: runtime.queryExpansionExtension,
1757
- rerank: runtime.rerank,
1758
- rerankExtension: runtime.rerankExtension,
1759
- warnings,
1760
- settings: runtime.settings,
1761
- });
1762
- hits = maybeApplyNoVectorFallback(semanticMode, semanticResult, keywordHits, warnings);
1763
- }
1764
- return { effectiveMode: runtime.effectiveMode, hits };
1765
- }
1766
- catch (error) {
1767
- warnings.push(buildExplicitSemanticFallbackWarning(runtime.effectiveMode, error));
1768
- return { effectiveMode: "keyword", hits: keywordHits };
1769
- }
1770
- }
1771
- function buildCountOnlySearchResult(response, total) {
1772
- if (response.projection.mode === "compact" &&
1773
- response.options.compact === true) {
1774
- return withSearchWarnings({
1775
- query: response.query.trim(),
1776
- mode: response.effectiveMode,
1777
- items: [],
1778
- count: total,
1779
- total,
1780
- count_only: true,
1781
- filters: buildCompactSearchFilterSummary({
1782
- mode: response.effectiveMode,
1783
- matchMode: response.matchMode,
1784
- options: response.options,
1785
- includeLinked: response.includeLinked,
1786
- titleExact: response.titleExact,
1787
- phraseExact: response.phraseExact,
1788
- scoreThreshold: response.scoreThreshold,
1789
- hybridSemanticWeight: response.hybridSemanticWeight,
1790
- runtimeFieldFilters: response.runtimeFieldFilters,
1791
- }),
1792
- }, response.warnings);
1793
- }
1794
- const projectionFields = response.projection.mode === "full"
1795
- ? null
1796
- : [...response.projection.fields];
1797
- return withSearchWarnings({
1798
- query: response.query.trim(),
1799
- mode: response.effectiveMode,
1800
- items: [],
1801
- count: total,
1802
- total,
1803
- count_only: true,
1804
- filters: buildVerboseSearchFilters({
1805
- effectiveMode: response.effectiveMode,
1806
- matchMode: response.matchMode,
1807
- options: response.options,
1808
- includeLinked: response.includeLinked,
1809
- titleExact: response.titleExact,
1810
- phraseExact: response.phraseExact,
1811
- scoreThreshold: response.scoreThreshold,
1812
- hybridSemanticWeight: response.hybridSemanticWeight,
1813
- queryExpansion: response.queryExpansion,
1814
- rerank: response.rerank,
1815
- runtimeFieldFilters: response.runtimeFieldFilters,
1816
- }),
1817
- projection: {
1818
- mode: response.projection.mode,
1819
- fields: projectionFields,
1820
- },
1821
- now: nowIso(),
1822
- }, response.warnings);
1823
- }
1824
- function withSearchWarnings(result, warnings) {
1825
- if (warnings.length === 0) {
1826
- return result;
1827
- }
1828
- return { ...result, warnings };
1829
- }
1830
- function withSearchWorkspaceMemory(result, memory) {
1831
- return memory ? { ...result, workspace_memory: memory } : result;
1832
- }
1833
- function attachSearchHighlights(prepared, filteredDocuments, limited) {
1834
- if (!prepared.highlight) {
1835
- return { hits: limited, projection: prepared.projection };
1836
- }
1837
- const documentById = new Map(filteredDocuments.map((document) => [document.metadata.id, document]));
1838
- const hits = limited.map((hit) => ({
1839
- ...hit,
1840
- highlights: buildHitHighlights(documentById.get(hit.item.id), hit.matched_fields, prepared.tokens),
1841
- }));
1842
- if (prepared.projection.mode === "full" ||
1843
- prepared.projection.fields.includes("highlights")) {
1844
- return { hits, projection: prepared.projection };
1845
- }
1846
- return {
1847
- hits,
1848
- projection: {
1849
- mode: prepared.projection.mode,
1850
- fields: [...prepared.projection.fields, "highlights"],
1851
- },
1852
- };
1853
- }
1854
- function buildSearchResultForHits(response, projectedItems, total, limitedCount, effectiveProjection, pageExtras) {
1855
- const truncationExtras = limitedCount < total ? { total } : {};
1856
- if (response.projection.mode === "compact" &&
1857
- response.options.compact === true) {
1858
- return withSearchWarnings({
1859
- query: response.query.trim(),
1860
- mode: response.effectiveMode,
1861
- items: projectedItems,
1862
- count: projectedItems.length,
1863
- ...truncationExtras,
1864
- ...pageExtras,
1865
- filters: buildCompactSearchFilterSummary({
1866
- mode: response.effectiveMode,
1867
- matchMode: response.matchMode,
1868
- options: response.options,
1869
- includeLinked: response.includeLinked,
1870
- titleExact: response.titleExact,
1871
- phraseExact: response.phraseExact,
1872
- scoreThreshold: response.scoreThreshold,
1873
- hybridSemanticWeight: response.hybridSemanticWeight,
1874
- runtimeFieldFilters: response.runtimeFieldFilters,
1875
- }),
1876
- }, response.warnings);
1877
- }
1878
- const finalProjectionFields = effectiveProjection.mode === "full"
1879
- ? null
1880
- : [...effectiveProjection.fields];
1881
- return {
1882
- query: response.query.trim(),
1883
- mode: response.effectiveMode,
1884
- items: projectedItems,
1885
- count: projectedItems.length,
1886
- ...truncationExtras,
1887
- ...pageExtras,
1888
- filters: buildVerboseSearchFilters({
1889
- effectiveMode: response.effectiveMode,
1890
- matchMode: response.matchMode,
1891
- options: response.options,
1892
- includeLinked: response.includeLinked,
1893
- titleExact: response.titleExact,
1894
- phraseExact: response.phraseExact,
1895
- scoreThreshold: response.scoreThreshold,
1896
- hybridSemanticWeight: response.hybridSemanticWeight,
1897
- queryExpansion: response.queryExpansion,
1898
- rerank: response.rerank,
1899
- runtimeFieldFilters: response.runtimeFieldFilters,
1900
- }),
1901
- projection: {
1902
- mode: effectiveProjection.mode,
1903
- fields: finalProjectionFields,
1904
- },
1905
- now: nowIso(),
1906
- ...(response.warnings.length > 0 ? { warnings: response.warnings } : {}),
1907
- };
1908
- }
1909
- function assertSearchPagingOptions(options) {
1910
- if (options.after !== undefined && options.count === true) {
1911
- throw new PmCliError("Search --after cannot be combined with --count.", EXIT_CODE.USAGE);
1912
- }
1913
- }
1914
- /** Implements run search for the public runtime surface of this module. */
1915
- export async function runSearch(rawQuery, rawOptions, global) {
1916
- assertSearchPagingOptions(rawOptions);
1917
- const prepared = prepareSearchInput(rawQuery, rawOptions);
1918
- const runtime = await resolveSearchRuntimeContext(prepared, global);
1919
- const corpus = await loadFilteredSearchCorpus(prepared, runtime);
1920
- const warnings = corpus.warnings;
1921
- warnings.push(...prepared.inlineWarnings);
1922
- const workspaceMemory = await readWorkspaceMemory(corpus.allDocuments.map((document) => document.metadata), {
1923
- pmRoot: runtime.pmRoot,
1924
- statusRegistry: runtime.statusRegistry,
1925
- now: nowIso(),
1926
- });
1927
- warnings.push(...workspaceMemory.warnings);
1928
- const workspaceMemoryResult = searchWorkspaceMemoryReadResult(workspaceMemory, prepared.query);
1929
- if (runtime.effectiveMode === "hybrid" &&
1930
- runtime.semanticWeightProvided &&
1931
- runtime.semanticWeightOverride === undefined) {
1932
- warnings.push("search_hybrid_semantic_weight_override_invalid:using_settings_default");
1933
- }
1934
- const responseBase = {
1935
- query: prepared.query,
1936
- matchMode: prepared.matchMode,
1937
- options: prepared.options,
1938
- includeLinked: prepared.includeLinked,
1939
- titleExact: prepared.titleExact,
1940
- phraseExact: prepared.phraseExact,
1941
- scoreThreshold: runtime.scoreThreshold,
1942
- hybridSemanticWeight: runtime.hybridSemanticWeight,
1943
- queryExpansion: runtime.queryExpansion,
1944
- rerank: runtime.rerank,
1945
- projection: prepared.projection,
1946
- warnings,
1947
- runtimeFieldFilters: runtime.runtimeFieldFilters,
1948
- };
1949
- if (runtime.effectiveMode === "keyword" &&
1950
- (corpus.filteredDocuments.length === 0 ||
1951
- (prepared.limit === 0 && !prepared.countOnly))) {
1952
- return withSearchWorkspaceMemory(buildEmptySearchResultFromContext({ ...responseBase, effectiveMode: runtime.effectiveMode }, prepared.countOnly), workspaceMemoryResult);
1953
- }
1954
- const keywordHits = await computeKeywordSearchHits(prepared, runtime, corpus.filteredDocuments);
1955
- const modeResult = await executeSemanticSearch({
1956
- prepared,
1957
- runtime,
1958
- filteredDocuments: corpus.filteredDocuments,
1959
- keywordHits,
1960
- warnings,
1961
- modeWasExplicit: prepared.modeWasExplicit,
1962
- });
1963
- if (modeResult.effectiveMode !== "keyword" &&
1964
- modeResult.hits.length === 0 &&
1965
- (corpus.filteredDocuments.length === 0 ||
1966
- (prepared.limit === 0 && !prepared.countOnly))) {
1967
- return withSearchWorkspaceMemory(buildEmptySearchResultFromContext({ ...responseBase, effectiveMode: modeResult.effectiveMode }, prepared.countOnly), workspaceMemoryResult);
1968
- }
1969
- const thresholded = modeResult.hits.filter((entry) => entry.exact_id_match === true || entry.score >= runtime.scoreThreshold);
1970
- const sorted = sortHits(thresholded, runtime.statusRegistry);
1971
- const total = sorted.length;
1972
- const page = resolveSearchPage({
1973
- sorted,
1974
- query: prepared.query,
1975
- mode: modeResult.effectiveMode,
1976
- searchOptions: prepared.options,
1977
- limit: prepared.limit ?? runtime.maxResults,
1978
- });
1979
- const response = { ...responseBase, effectiveMode: modeResult.effectiveMode };
1980
- if (prepared.countOnly) {
1981
- return withSearchWorkspaceMemory(buildCountOnlySearchResult(response, total), workspaceMemoryResult);
1982
- }
1983
- const { hits: projectedHits, projection: effectiveProjection } = attachSearchHighlights(prepared, corpus.filteredDocuments, page.limited);
1984
- const projectedItems = projectSearchHits(projectedHits, effectiveProjection);
1985
- return withSearchWorkspaceMemory(buildSearchResultForHits(response, projectedItems, total, page.limited.length, effectiveProjection, page.pageExtras), workspaceMemoryResult);
1986
- }
1
+ !(function(){try{var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:{},n=new e.Error().stack;n&&(e._sentryDebugIds=e._sentryDebugIds||{},e._sentryDebugIds[n]="c1f323c4-410f-5e51-ba94-29caaccfbac7")}catch(e2){}})();import{assertInitializedTracker}from"../environment/tracker-preflight.js";import fs from"node:fs/promises";import path from"node:path";import{toNonEmptyStringOrUndefined}from"../../core/shared/primitives.js";import{isPathWithinDirectory}from"../../core/fs/path-utils.js";import{getActiveExtensionRegistrations,runActiveOnReadHooks}from"../../core/extensions/index.js";import{collectRegisteredItemFieldNames}from"../../core/extensions/item-fields.js";import{resolveRegisteredSearchProvider,resolveRegisteredVectorStoreAdapter}from"../../core/extensions/runtime-registrations.js";import{resolveItemTypeRegistry}from"../../core/item/type-registry.js";import{parseLimit}from"./parsers.js";import{parsePriorityFilterSet,parseStringFilterSet,parseTypeFilterSet}from"./multi-value-filters.js";import{resolveProviderConfigSource,executeEmbeddingRequest,resolveEmbeddingProviders}from"../../core/search/providers.js";import{buildDeterministicQueryExpansions,mergeQueryExpansions,normalizeQueryExpansionOutput,normalizeRerankOutput,rerankCandidatesWithEmbeddings,resolveQueryExpansionConfig,resolveRerankConfig}from"../../core/search/relevance.js";import{resolveSettingsWithSemanticRuntimeDefaults}from"../../core/search/semantic-defaults.js";import{executeVectorQuery,resolveVectorStores}from"../../core/search/vector-stores.js";import{readVectorizationStatusLedger}from"../../core/search/cache.js";import{buildEventCorpus,buildPlanFlatCorpus,buildReminderCorpus,buildSearchCorpus,resolveSearchCorpusFields}from"../../core/search/corpus.js";import{buildBm25Index,flattenSearchCorpusText,resolveBm25Params,scoreBm25Query,tokenizeBm25}from"../../core/search/bm25.js";import{collectStaleVectorizationIds}from"../../core/search/staleness.js";import{parseItemDocument}from"../../core/item/item-format.js";import{toItemRecord}from"../../core/item/item-record.js";import{isTerminalStatus}from"../../core/item/status.js";import{parseStatusFilterCsv}from"../../core/item/status-filter.js";import{collectRuntimeFilterValues,matchesRuntimeFilters}from"../../core/schema/runtime-field-filters.js";import{hasMissingMetadataFilter,itemMatchesMissingMetadata,lifecycleClassifierFromStatusRegistry}from"../../core/governance/metadata-coverage.js";import{hasContentFieldFilter,itemMatchesContentFilters}from"../../core/governance/content-fields.js";import{resolveContentFieldFilters,resolveMissingMetadataFilters}from"./list.js";import{buildCompactSearchFilterSummary,buildVerboseSearchFilters}from"./search-rendering.js";import{resolveRuntimeFieldRegistry,resolveRuntimeStatusRegistry}from"../../core/schema/runtime-schema.js";import{EXIT_CODE}from"../../core/shared/constants.js";import{PmCliError}from"../../core/shared/errors.js";import{tokenizeAlphaNumeric}from"../../core/shared/text-normalization.js";import{compareTimestampStrings,matchesTimestampFilters,nowIso}from"../../core/shared/time.js";import{listAllDocumentCandidatesCached}from"../../core/store/item-metadata-cache.js";import{listAllItemMetadata}from"../../core/store/item-store.js";import{getItemPath,resolveGlobalPmRoot,resolvePmRoot}from"../../core/store/paths.js";import{readSettings}from"../../core/store/settings.js";import{resolveSearchPage}from"./search-pagination.js";import{normalizeSearchPhrase,parseMinScoreOverride,parseSearchBoolean,parseSearchDeadline,parseSearchMatchMode,parseSearchProjection,parseSearchTokens,parseSemanticWeightOverride,parseTimestampWindow,resolveEffectiveSearchMode,resolveHybridSemanticWeight,resolveSearchMaxResults,resolveSearchScoreThreshold,resolveSearchTuning,validateSearchProjectionFields}from"./search-contracts.js";import{readWorkspaceMemory,searchWorkspaceMemoryReadResult}from"../workspace-memory.js";import{resolveEffectiveSearchMode as resolveEffectiveSearchMode2,resolveHybridSemanticWeight as resolveHybridSemanticWeight2,resolveSearchMaxResults as resolveSearchMaxResults2,resolveSearchScoreThreshold as resolveSearchScoreThreshold2,resolveSearchTuning as resolveSearchTuning2}from"./search-contracts.js";const LONG_QUERY_TOKEN_THRESHOLD=2;const LONG_QUERY_TITLE_EXACT_BONUS=120;const LONG_QUERY_PHRASE_MULTIPLIER=6;const ALL_TERMS_COVERAGE_BONUS=40;const EXACT_ID_MATCH_SCORE=1e3;const SHORT_ID_MATCH_SCORE=900;const HIGHLIGHT_OPEN="\xAB";const HIGHLIGHT_CLOSE="\xBB";const HIGHLIGHT_SNIPPET_RADIUS=60;const INLINE_QUERY_FILTER_FIELDS=["tag","status","type","priority"];function collectErrorCauseCodes(error){const codes=[];let current=error;for(let depth=0;depth<5&&current&&typeof current==="object";depth+=1){const code=current.code;if(typeof code==="string"){codes.push(code.toLowerCase())}current=current.cause}return codes.join(" ")}function classifyImplicitSemanticFallbackReason(error){const message=(error instanceof Error?error.message:String(error)).toLowerCase();const causeCodes=collectErrorCauseCodes(error);const haystack=`${message} ${causeCodes}`;if(haystack.includes("timed out")||haystack.includes("timeout")||haystack.includes("etimedout")){return"timeout"}if(haystack.includes("econnrefused")||haystack.includes("connection refused")||haystack.includes("connect ")||haystack.includes("enotfound")||haystack.includes("eai_again")||haystack.includes("econnreset")||haystack.includes("fetch failed")){return"connection"}return"error"}function buildExplicitSemanticFallbackWarning(requestedMode,error){const reason=classifyImplicitSemanticFallbackReason(error);return`search_${requestedMode}_fallback:${reason}:using_keyword_mode`}function resolveVectorIndexRecovery(registrations){const hasReindex=registrations?.commands?.some(registration=>registration.command.trim()==="reindex")===true;return hasReindex?{command:"pm reindex --mode hybrid",args:["reindex","--mode","hybrid"]}:{command:"pm install search-advanced --project",args:["install","search-advanced","--project"],follow_up_command:"pm reindex --mode hybrid",follow_up_args:["reindex","--mode","hybrid"]}}async function maybeEmitVectorIndexStaleWarning(pmRoot,filteredDocuments,warnings){try{const ledger=await readVectorizationStatusLedger(pmRoot);if(ledger.warnings.length>0){warnings.push(...ledger.warnings)}const staleIds=collectStaleVectorizationIds(filteredDocuments.map(document=>({id:document.metadata.id,updated_at:document.metadata.updated_at})),ledger.entries);if(staleIds.length===0){return}const recovery=resolveVectorIndexRecovery(getActiveExtensionRegistrations());warnings.push(...formatVectorIndexRecoveryWarnings(staleIds.length,recovery));process.stderr.write(`[pm] warning: ${staleIds.length} item${staleIds.length===1?" is":"s are"} new or modified since the last reindex and ${staleIds.length===1?"is":"are"} NOT in the semantic index yet \u2014 run '${recovery.command}'${recovery.follow_up_command?`, then '${recovery.follow_up_command}'`:""}. (Write-time embedding is governed by search.mutation_refresh_policy; staleness means the embed was skipped, failed, or the backend was unreachable.)
2
+ `)}catch{}}function formatVectorIndexRecoveryWarnings(staleCount,recovery){return[`vector_index_stale:${staleCount}`,`vector_index_recovery:${recovery.args.join(" ")}`,...recovery.follow_up_args?[`vector_index_recovery_follow_up:${recovery.follow_up_args.join(" ")}`]:[]]}const _testOnlySearchCommand={applyFilters,applyExactQueryFilters,applyInlineQueryFilters,buildHitHighlights,buildVerboseSearchFilters,buildExplicitSemanticFallbackWarning,buildCompactSearchFilterSummary,buildHybridLexicalScore,buildRerankCorpus,buildSemanticHits,classifyImplicitSemanticFallbackReason,collectErrorCauseCodes,collectExactPhraseFields,collectLinkedPaths,combineHybridHits,computeBuiltInBm25Hits,computeSemanticOrHybridHits,resolveBuiltInBm25Mode,countOccurrences,dependencyEntries,documentContainsExactPhrase,emptySearchResult,highlightFieldSnippet,loadDocuments,markTokenRuns,parseInlineQueryFilters,maybeEmitVectorIndexStaleWarning,loadLinkedCorpus,mergeVectorHitsById,normalizeExtensionProviderHits,normalizeScoreMap,parseProjectionConfig:parseSearchProjection,parseTimestampWindow,parseTokens:parseSearchTokens,projectSearchHits,readSearchFieldValue,requireSemanticDependencies,resolveExtensionSearchProvider,resolveExtensionSearchProviderByName,resolveExtensionVectorAdapter,resolveLinkedCorpusRoots,scoreDocument,sortHits,stringArray,textEntries,validateSearchProjectionFields};function parseInlineQueryFilters(query){const inlineFilters={};const residualTokens=[];for(const token of query.split(/\s+/).filter(entry=>entry.length>0)){const separatorIndex=token.indexOf(":");const field=separatorIndex>0?token.slice(0,separatorIndex).toLowerCase():"";const value=separatorIndex>0?token.slice(separatorIndex+1):"";const matchedField=INLINE_QUERY_FILTER_FIELDS.includes(field)?field:void 0;if(matchedField&&value.length>0&&inlineFilters[matchedField]===void 0){inlineFilters[matchedField]=value;continue}residualTokens.push(token)}return{residualQuery:residualTokens.join(" "),inlineFilters}}function applyInlineQueryFilters(options,inlineFilters,warnings){const merged={...options};for(const field of INLINE_QUERY_FILTER_FIELDS){const inlineValue=inlineFilters[field];if(inlineValue===void 0){continue}if(toNonEmptyStringOrUndefined(merged[field])!==void 0){warnings.push(`search_inline_filter_ignored:${field}:flag_takes_precedence`);continue}merged[field]=inlineValue}return merged}function stringArray(value){return Array.isArray(value)?value.filter(entry=>typeof entry==="string"):[]}function textEntries(value){return Array.isArray(value)?value.filter(entry=>typeof entry==="object"&&entry!==null&&typeof entry.text==="string"):[]}function dependencyEntries(value){return Array.isArray(value)?value.filter(entry=>typeof entry==="object"&&entry!==null&&typeof entry.id==="string"&&typeof entry.kind==="string"):[]}function collectExactPhraseFields(document){const item=document.metadata;return[item.title,item.description,item.status,stringArray(item.tags).join(" "),document.body,textEntries(item.comments).map(entry=>entry.text).join(" "),textEntries(item.notes).map(entry=>entry.text).join(" "),textEntries(item.learnings).map(entry=>entry.text).join(" "),buildReminderCorpus(item).join(" "),buildEventCorpus(item).join(" "),dependencyEntries(item.dependencies).map(entry=>`${entry.id} ${entry.kind}`).join(" "),buildPlanFlatCorpus(item)]}function documentContainsExactPhrase(document,normalizedQuery){return collectExactPhraseFields(document).some(fieldValue=>normalizeSearchPhrase(fieldValue).includes(normalizedQuery))}function applyExactQueryFilters(items,normalizedQuery,options){if(!options.titleExact&&!options.phraseExact){return items}return items.filter(document=>{if(options.titleExact&&normalizeSearchPhrase(document.metadata.title)!==normalizedQuery){return false}if(options.phraseExact&&!documentContainsExactPhrase(document,normalizedQuery)){return false}return true})}function assertSearchAssigneeFilter(assigneeFilter){if(assigneeFilter&&[...assigneeFilter].some(value=>value.toLowerCase()==="none"||value.toLowerCase()==="null")){throw new PmCliError('--assignee no longer accepts "none" or "null".',EXIT_CODE.USAGE)}}function resolveSearchMetadataFilterSet(options,typeRegistry,statusFilter){const assigneeFilter=parseStringFilterSet(options.assignee,{label:"--assignee"});assertSearchAssigneeFilter(assigneeFilter);const missingMetadataFilters=resolveMissingMetadataFilters(options);const contentFieldFilters=resolveContentFieldFilters(options);return{typeFilter:parseTypeFilterSet(options.type,typeRegistry),tagFilter:parseStringFilterSet(options.tag,{label:"--tag",normalize:tag=>tag.toLowerCase()}),priorityFilter:parsePriorityFilterSet(options.priority),deadlineBefore:parseSearchDeadline(options.deadlineBefore,"deadline-before"),deadlineAfter:parseSearchDeadline(options.deadlineAfter,"deadline-after"),updatedAfter:parseTimestampWindow(options.updatedAfter,"updated-after"),updatedBefore:parseTimestampWindow(options.updatedBefore,"updated-before"),createdAfter:parseTimestampWindow(options.createdAfter,"created-after"),createdBefore:parseTimestampWindow(options.createdBefore,"created-before"),assigneeFilter,sprintFilter:parseStringFilterSet(options.sprint,{label:"--sprint"}),releaseFilter:parseStringFilterSet(options.release,{label:"--release"}),parentFilter:options.parent?.trim(),statusSet:statusFilter&&statusFilter.length>0?new Set(statusFilter):void 0,missingMetadataFilters,missingMetadataActive:hasMissingMetadataFilter(missingMetadataFilters),contentFieldFilters,contentFiltersActive:hasContentFieldFilter(contentFieldFilters)}}function matchesScalarSearchFilters(item,filters){return matchesIdentitySearchFilters(item,filters)&&matchesTimestampSearchFilters(item,filters)&&matchesOwnerSearchFilters(item,filters)}function matchesIdentitySearchFilters(item,filters){if(filters.statusSet&&!filters.statusSet.has(item.status))return false;if(filters.typeFilter&&!filters.typeFilter.has(item.type))return false;const tagFilter=filters.tagFilter;if(tagFilter&&!stringArray(item.tags).some(tag=>tagFilter.has(tag)))return false;if(filters.priorityFilter!==void 0&&!filters.priorityFilter.has(item.priority))return false;return true}function matchesTimestampSearchFilters(item,filters){return matchesTimestampFilters(item,filters)}function matchesOwnerSearchFilters(item,filters){if(filters.assigneeFilter!==void 0&&(item.assignee===void 0||!filters.assigneeFilter.has(item.assignee)))return false;if(filters.sprintFilter!==void 0&&(item.sprint===void 0||!filters.sprintFilter.has(item.sprint)))return false;if(filters.releaseFilter!==void 0&&(item.release===void 0||!filters.releaseFilter.has(item.release)))return false;if(filters.parentFilter!==void 0&&item.parent!==filters.parentFilter)return false;return true}function matchesSearchMetadataFilters(document,filters,runtimeFieldFilters,lifecycleClassifier){const item=document.metadata;if(!matchesScalarSearchFilters(item,filters)){return false}if(!matchesRuntimeFilters(item,runtimeFieldFilters)){return false}if(filters.missingMetadataActive&&!itemMatchesMissingMetadata(item,filters.missingMetadataFilters,lifecycleClassifier)){return false}if(filters.contentFiltersActive&&!itemMatchesContentFilters(item,filters.contentFieldFilters)){return false}return true}function applyFilters(items,options,typeRegistry,runtimeFieldFilters,statusFilter,lifecycleClassifier){const filters=resolveSearchMetadataFilterSet(options,typeRegistry,statusFilter);return items.filter(document=>matchesSearchMetadataFilters(document,filters,runtimeFieldFilters,lifecycleClassifier))}function countOccurrences(haystack,needle){let count=0;let index=0;while(true){const foundAt=haystack.indexOf(needle,index);if(foundAt<0){return count}count+=1;index=foundAt+needle.length}}function tokenizeForExactTokenMatch(value){return tokenizeAlphaNumeric(value)}function collectLinkedPaths(item){const fromFiles=(item.files??[]).map(entry=>({scope:entry.scope,path:entry.path.trim()}));const fromDocs=(item.docs??[]).map(entry=>({scope:entry.scope,path:entry.path.trim()}));const fromTests=(item.tests??[]).filter(entry=>typeof entry.path==="string"&&entry.path.trim().length>0).map(entry=>({scope:entry.scope,path:entry.path.trim()}));const sorted=[...fromFiles,...fromDocs,...fromTests].filter(entry=>entry.path.length>0).sort((a,b)=>a.scope.localeCompare(b.scope)||a.path.localeCompare(b.path));const deduped=new Map;for(const entry of sorted){deduped.set(`${entry.scope}:${entry.path}`,entry)}return[...deduped.values()]}async function resolveContainmentRoot(root){const resolved=path.resolve(root);try{const realpathRoot=await fs.realpath(resolved);return{resolved,realpath:realpathRoot}}catch{return null}}async function resolveLinkedCorpusRoots(projectRoot,globalRoot){const[projectContainmentRoot,globalContainmentRoot]=await Promise.all([resolveContainmentRoot(projectRoot),resolveContainmentRoot(globalRoot)]);return{projectContainmentRoot,globalContainmentRoot}}async function loadLinkedCorpus(document,roots){const linkedPaths=collectLinkedPaths(document.metadata);const chunks=[];for(const linkedPath of linkedPaths){const containmentRoot=linkedPath.scope==="global"?roots.globalContainmentRoot:roots.projectContainmentRoot;if(!containmentRoot){continue}const resolved=path.resolve(containmentRoot.resolved,linkedPath.path);if(!isPathWithinDirectory(containmentRoot.resolved,resolved)){continue}let linkedRealpath;try{linkedRealpath=await fs.realpath(resolved)}catch{continue}if(!isPathWithinDirectory(containmentRoot.realpath,linkedRealpath)){continue}try{await runActiveOnReadHooks({path:resolved,scope:linkedPath.scope});chunks.push(await fs.readFile(resolved,"utf8"))}catch{}}return chunks.join("\n")}const SEARCHABLE_FIELD_BUILDERS=[{name:"title",weightKey:"title_weight",value:document=>document.metadata.title},{name:"description",weightKey:"description_weight",value:document=>document.metadata.description},{name:"tags",weightKey:"tags_weight",value:document=>stringArray(document.metadata.tags).join(" ")},{name:"status",weightKey:"status_weight",value:document=>typeof document.metadata.status==="string"?document.metadata.status:""},{name:"body",weightKey:"body_weight",value:document=>document.body},{name:"comments",weightKey:"comments_weight",value:document=>textEntries(document.metadata.comments).map(entry=>entry.text).join(" ")},{name:"notes",weightKey:"notes_weight",value:document=>textEntries(document.metadata.notes).map(entry=>entry.text).join(" ")},{name:"learnings",weightKey:"learnings_weight",value:document=>textEntries(document.metadata.learnings).map(entry=>entry.text).join(" ")},{name:"reminders",weightKey:"reminders_weight",value:document=>buildReminderCorpus(document.metadata).join(" ")},{name:"events",weightKey:"events_weight",value:document=>buildEventCorpus(document.metadata).join(" ")},{name:"dependencies",weightKey:"dependencies_weight",value:document=>dependencyEntries(document.metadata.dependencies).map(entry=>`${entry.id} ${entry.kind}`).join(" ")},{name:"plan",weightKey:"body_weight",value:document=>buildPlanFlatCorpus(document.metadata)}];const SEARCHABLE_FIELD_BUILDER_BY_NAME=new Map(SEARCHABLE_FIELD_BUILDERS.map(builder=>[builder.name,builder]));function escapeRegExpLiteral(value){return value.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}function markTokenRuns(text,tokens){const escaped=tokens.filter(token=>token.length>0).sort((left,right)=>right.length-left.length).map(escapeRegExpLiteral);if(escaped.length===0){return text}const pattern=new RegExp(escaped.join("|"),"gi");return text.replace(pattern,match=>`${HIGHLIGHT_OPEN}${match}${HIGHLIGHT_CLOSE}`)}function highlightFieldSnippet(text,tokens){if(text.length===0){return null}const lowerText=text.toLowerCase();let firstMatchIndex=-1;let firstMatchLength=0;for(const token of tokens){if(token.length===0){continue}const index=lowerText.indexOf(token);if(index>=0&&(firstMatchIndex<0||index<firstMatchIndex)){firstMatchIndex=index;firstMatchLength=token.length}}if(firstMatchIndex<0){return null}const windowStart=Math.max(0,firstMatchIndex-HIGHLIGHT_SNIPPET_RADIUS);const windowEnd=Math.min(text.length,firstMatchIndex+firstMatchLength+HIGHLIGHT_SNIPPET_RADIUS);const marked=markTokenRuns(text.slice(windowStart,windowEnd),tokens);const prefix=windowStart>0?"\u2026":"";const suffix=windowEnd<text.length?"\u2026":"";return`${prefix}${marked}${suffix}`}function buildHitHighlights(document,matchedFields,tokens){const highlights=[];for(const field of matchedFields){const builder=SEARCHABLE_FIELD_BUILDER_BY_NAME.get(field);if(builder===void 0){continue}const snippet=highlightFieldSnippet(builder.value(document),tokens);if(snippet!==null){highlights.push({field,snippet})}}return highlights}function buildSearchableScoringFields(document,linkedCorpus,tuning){return[...SEARCHABLE_FIELD_BUILDERS.map(builder=>({name:builder.name,value:builder.value(document),weight:tuning[builder.weightKey]})),{name:"linked_content",value:linkedCorpus,weight:tuning.linked_content_weight}]}function buildExactIdSearchHit(item,normalizedQuery,idPrefix){const normalizedId=normalizeSearchPhrase(item.id);const normalizedIdPrefix=typeof idPrefix==="string"?idPrefix.trim().toLowerCase():"";const normalizedIdPrefixPhrase=normalizeSearchPhrase(normalizedIdPrefix);const normalizedShortId=normalizedIdPrefixPhrase.length>0&&normalizedId.startsWith(normalizedIdPrefixPhrase)?normalizedId.slice(normalizedIdPrefixPhrase.length).trim():normalizedId;if(normalizedQuery!==normalizedId&&normalizedQuery!==normalizedShortId){return null}return{item,score:normalizedQuery===normalizedId?EXACT_ID_MATCH_SCORE:SHORT_ID_MATCH_SCORE,matched_fields:["id"],matched_all_terms:true,exact_id_match:true}}function scoreTokenMatches(tokens,titleTokenCounts,searchableFields,tuning,state){for(const token of tokens){const exactTitleMatches=titleTokenCounts.get(token)??0;if(exactTitleMatches>0){state.score+=exactTitleMatches*tuning.title_exact_bonus;state.matched.add("title");state.matchedTokens.add(token)}for(const field of searchableFields){const occurrences=countOccurrences(field.value.toLowerCase(),token);if(occurrences>0){state.score+=occurrences*field.weight;state.matched.add(field.name);state.matchedTokens.add(token)}}}}function scorePhraseMatches(item,normalizedQuery,searchableFields,state){const normalizedTitle=normalizeSearchPhrase(item.title);if(normalizedTitle===normalizedQuery){state.score+=LONG_QUERY_TITLE_EXACT_BONUS;state.matched.add("title")}for(const field of searchableFields){const phraseOccurrences=countOccurrences(normalizeSearchPhrase(field.value),normalizedQuery);if(phraseOccurrences>0){state.score+=phraseOccurrences*field.weight*LONG_QUERY_PHRASE_MULTIPLIER;state.matched.add(field.name)}}}function scoreDocument(document,tokens,normalizedQuery,linkedCorpus,tuning,idPrefix,applyCoverageBonus=false){const item=document.metadata;const exactIdHit=buildExactIdSearchHit(item,normalizedQuery,idPrefix);if(exactIdHit){return exactIdHit}const titleTokenCounts=new Map;for(const token of tokenizeForExactTokenMatch(item.title)){titleTokenCounts.set(token,(titleTokenCounts.get(token)??0)+1)}const searchableFields=buildSearchableScoringFields(document,linkedCorpus,tuning);const state={score:0,matched:new Set,matchedTokens:new Set};scoreTokenMatches(tokens,titleTokenCounts,searchableFields,tuning,state);const distinctTokens=new Set(tokens);const matchedAllTerms=distinctTokens.size>0&&state.matchedTokens.size===distinctTokens.size;if(applyCoverageBonus&&distinctTokens.size>1&&matchedAllTerms){state.score+=ALL_TERMS_COVERAGE_BONUS}const isLongPhraseQuery=tokens.length>=LONG_QUERY_TOKEN_THRESHOLD&&normalizedQuery.includes(" ");if(isLongPhraseQuery){scorePhraseMatches(item,normalizedQuery,searchableFields,state)}if(state.score<=0){return null}return{item,score:state.score,matched_fields:[...state.matched].sort((a,b)=>a.localeCompare(b)),matched_all_terms:matchedAllTerms}}function sortHits(items,statusRegistry){return[...items].sort((a,b)=>{const byScore=b.score-a.score;if(byScore!==0)return byScore;const aTerminal=isTerminalStatus(a.item.status,statusRegistry);const bTerminal=isTerminalStatus(b.item.status,statusRegistry);if(aTerminal!==bTerminal){return aTerminal?1:-1}const byPriority=a.item.priority-b.item.priority;if(byPriority!==0)return byPriority;const byUpdated=compareTimestampStrings(b.item.updated_at,a.item.updated_at);if(byUpdated!==0)return byUpdated;return a.item.id.localeCompare(b.item.id)})}function buildHybridLexicalScore(document,tokens,normalizedQuery,includeLinked,linkedCorpusById,tuning,idPrefix,applyCoverageBonus=false){return scoreDocument(document,tokens,normalizedQuery,includeLinked?linkedCorpusById.get(document.metadata.id)??"":"",tuning,idPrefix,applyCoverageBonus)}function normalizeScoreMap(scoreById){if(scoreById.size===0){return new Map}const values=[...scoreById.values()];const minScore=Math.min(...values);const maxScore=Math.max(...values);if(maxScore===minScore){return new Map([...scoreById.keys()].map(id=>[id,1]))}const normalized=new Map;for(const[id,score]of scoreById){normalized.set(id,(score-minScore)/(maxScore-minScore))}return normalized}function emptySearchResult(query,mode,matchMode,options,includeLinked,scoreThreshold,hybridSemanticWeight,queryExpansion,rerank,projection,warnings,runtimeFieldFilters,countOnly,modeSource){const countExtras=countOnly?{total:0,count_only:true}:{};const compactSummaryMode=projection.mode==="compact"&&options.compact===true;if(compactSummaryMode){const compactFilters=buildCompactSearchFilterSummary({mode,matchMode,options,includeLinked,titleExact:options.titleExact===true,phraseExact:options.phraseExact===true,scoreThreshold,hybridSemanticWeight,runtimeFieldFilters});return{query:query.trim(),mode,...compactModeSource(modeSource),items:[],count:0,...countExtras,filters:compactFilters,...warnings.length>0?{warnings}:{}}}const projectionFields=projection.mode==="full"?null:[...projection.fields];return{query:query.trim(),mode,mode_source:modeSource,items:[],count:0,...countExtras,filters:buildVerboseSearchFilters({effectiveMode:mode,matchMode,options,includeLinked,titleExact:options.titleExact===true,phraseExact:options.phraseExact===true,scoreThreshold,hybridSemanticWeight,queryExpansion,rerank,runtimeFieldFilters:runtimeFieldFilters??{}}),projection:{mode:projection.mode,fields:projectionFields},now:nowIso(),...warnings.length>0?{warnings}:{}}}function requireSemanticDependencies(requestedMode,providerResolution,vectorResolution,hasExtensionVectorQuery){if(!providerResolution.active){throw new PmCliError(`Search mode '${requestedMode}' requires a configured embedding provider in settings.providers.openai or settings.providers.ollama`,EXIT_CODE.USAGE)}if(!vectorResolution.active&&!hasExtensionVectorQuery){throw new PmCliError(`Search mode '${requestedMode}' requires a configured vector store in settings.vector_store.qdrant/settings.vector_store.lancedb or an extension adapter selected by settings.vector_store.adapter`,EXIT_CODE.USAGE)}return{provider:providerResolution.active,vectorStore:vectorResolution.active??null}}const toOptionalNonEmptyString=toNonEmptyStringOrUndefined;function resolveExtensionSearchProviderByName(providerName){const registrations=getActiveExtensionRegistrations();const resolved=resolveRegisteredSearchProvider(registrations,providerName);if(!resolved){return null}const runtimeDefinition=resolved.runtime_definition??resolved.definition;const query=runtimeDefinition.query;const queryExpansion=runtimeDefinition.queryExpansion??runtimeDefinition.query_expansion;const rerank=runtimeDefinition.rerank;const registeredName=toOptionalNonEmptyString(runtimeDefinition.name)??toOptionalNonEmptyString(resolved.definition.name)??providerName;if(!registeredName){return null}const hooks={providerName:registeredName,...typeof query==="function"?{query}:{},...typeof queryExpansion==="function"?{queryExpansion}:{},...typeof rerank==="function"?{rerank}:{}};if(!hooks.query&&!hooks.queryExpansion&&!hooks.rerank){return null}return hooks}function resolveExtensionSearchProvider(settings){const providerName=toOptionalNonEmptyString(settings.search?.provider);const resolved=resolveExtensionSearchProviderByName(providerName);if(!resolved?.query){return null}return{providerName:resolved.providerName,query:resolved.query}}function resolveExtensionVectorAdapter(settings){const registrations=getActiveExtensionRegistrations();const adapterName=toOptionalNonEmptyString(settings.vector_store?.adapter);const resolved=resolveRegisteredVectorStoreAdapter(registrations,adapterName);if(!resolved){return null}const runtimeDefinition=resolved.runtime_definition??resolved.definition;const query=runtimeDefinition.query;if(typeof query!=="function"){return null}const upsert=runtimeDefinition.upsert;const runtimeAdapterName=toOptionalNonEmptyString(runtimeDefinition.name)??toOptionalNonEmptyString(resolved.definition.name)??adapterName??"extension";return{adapterName:runtimeAdapterName,query,...typeof upsert==="function"?{upsert}:{}}}function normalizeExtensionProviderHits(providerName,raw,filteredById){const rawHits=Array.isArray(raw)?raw:raw?.hits;if(!Array.isArray(rawHits)){throw new PmCliError(`Extension search provider "${providerName}" must return an array of hits or { hits: [...] }`,EXIT_CODE.GENERIC_FAILURE)}const seen=new Set;const hits=[];for(const rawHit of rawHits){if(typeof rawHit!=="object"||rawHit===null){continue}const id=toOptionalNonEmptyString(rawHit.id);const score=rawHit.score;if(!id||typeof score!=="number"||!Number.isFinite(score)||seen.has(id)){continue}const document=filteredById.get(id);if(!document){continue}const matchedFieldsRaw=rawHit.matched_fields;const matchedFields=Array.isArray(matchedFieldsRaw)&&matchedFieldsRaw.every(entry=>typeof entry==="string")?[...new Set(matchedFieldsRaw.map(entry=>entry.trim()).filter(entry=>entry.length>0))].sort((a,b)=>a.localeCompare(b)):[`provider:${providerName}`];seen.add(id);hits.push({item:document.metadata,score,matched_fields:matchedFields})}return hits}function buildSemanticHits(vectorHits,filteredById){const semanticHits=[];const semanticScores=new Map;for(const vectorHit of vectorHits){if(semanticScores.has(vectorHit.id)){continue}const document=filteredById.get(vectorHit.id);if(!document){continue}semanticScores.set(vectorHit.id,vectorHit.score);semanticHits.push({item:document.metadata,score:vectorHit.score,matched_fields:["semantic"]})}return{semanticHits,semanticScores}}function forceExactIdHitsToTop(rankedHits,keywordHits){const exactIdHits=keywordHits.filter(hit=>hit.exact_id_match===true);if(exactIdHits.length===0){return rankedHits}const exactIdIds=new Set(exactIdHits.map(hit=>hit.item.id));const remaining=rankedHits.filter(hit=>!exactIdIds.has(hit.item.id));const maxRemainingScore=remaining.reduce((max,hit)=>Math.max(max,hit.score),0);const orderedExactHits=exactIdHits.sort((left,right)=>right.score-left.score);const bandBase=maxRemainingScore+orderedExactHits.length+1;const promoted=orderedExactHits.map((hit,index)=>({...hit,score:bandBase-index,matched_fields:["id"]}));return[...promoted,...remaining]}function combineHybridHits(filteredById,semanticScores,keywordHits,hybridSemanticWeight,semanticFieldLabel="semantic"){const keywordScores=new Map(keywordHits.map(entry=>[entry.item.id,entry.score]));const keywordMatches=new Map(keywordHits.map(entry=>[entry.item.id,entry.matched_fields]));const normalizedSemantic=normalizeScoreMap(semanticScores);const normalizedKeyword=normalizeScoreMap(keywordScores);const candidateIds=new Set([...semanticScores.keys(),...keywordScores.keys()]);const keywordWeight=1-hybridSemanticWeight;return[...candidateIds].map(id=>{const document=filteredById.get(id);const semanticScore=normalizedSemantic.get(id)??0;const keywordScore=normalizedKeyword.get(id)??0;const combinedScore=semanticScore*hybridSemanticWeight+keywordScore*keywordWeight;if(combinedScore<=0){return null}const matchedFields=new Set;if(semanticScores.has(id)){matchedFields.add(semanticFieldLabel)}for(const field of keywordMatches.get(id)??[]){matchedFields.add(field)}return{item:document.metadata,score:combinedScore,matched_fields:[...matchedFields].sort((a,b)=>a.localeCompare(b))}}).filter(entry=>entry!==null)}function resolveBuiltInBm25Mode(params){const normalized=params.configuredProvider?.trim().toLowerCase();if(normalized==="bm25"){return"explicit"}if(normalized==="auto"&&!params.hasEmbeddingProvider&&!params.hasExtensionSearchProvider){return"auto-fallback"}return null}function computeBuiltInBm25Hits(params){const bm25Documents=params.filteredDocuments.map(document=>({id:document.metadata.id,text:flattenSearchCorpusText(buildSearchCorpus(document,{fields:params.corpusFields}))}));const index=buildBm25Index(bm25Documents);const bm25Scores=scoreBm25Query(index,tokenizeBm25(params.query),params.bm25Params);const filteredById=new Map(params.filteredDocuments.map(document=>[document.metadata.id,document]));if(params.requestedMode==="semantic"){const semanticHits=[...bm25Scores].map(([id,score])=>({item:filteredById.get(id).metadata,score,matched_fields:["bm25"]}));return forceExactIdHitsToTop(semanticHits,params.keywordHits)}const hybridHits=combineHybridHits(filteredById,bm25Scores,params.keywordHits,params.hybridSemanticWeight,"bm25");return forceExactIdHitsToTop(hybridHits,params.keywordHits)}function mergeVectorHitsById(vectorHitGroups){const bestById=new Map;for(const group of vectorHitGroups){for(const hit of group){const existing=bestById.get(hit.id);if(!existing||hit.score>existing.score){bestById.set(hit.id,hit)}}}const merged=[...bestById.values()];merged.sort((left,right)=>{if(left.score!==right.score){return right.score-left.score}return left.id.localeCompare(right.id)});return merged}function buildRerankCorpus(document){const metadata=document.metadata;const tags=Array.isArray(metadata?.tags)?metadata.tags.join(" "):"";return[metadata?.title,metadata?.description,metadata?.type,metadata?.status,tags,document.body].map(entry=>typeof entry==="string"?entry.trim():"").filter(entry=>entry.length>0).join("\n")}async function resolveExpandedSemanticQueries(context,queryTrimmed){const baseExpandedQueries=context.queryExpansion.enabled?buildDeterministicQueryExpansions(queryTrimmed,context.queryExpansion.max_queries):[queryTrimmed];const expandedQueries=baseExpandedQueries.length>0?baseExpandedQueries:[queryTrimmed];if(!context.queryExpansion.enabled){return expandedQueries}if(context.queryExpansionExtension?.expand){try{const rawExpansion=await Promise.resolve(context.queryExpansionExtension.expand({query:queryTrimmed,mode:context.requestedMode,settings:context.settings}));return mergeQueryExpansions(expandedQueries,normalizeQueryExpansionOutput(rawExpansion),context.queryExpansion.max_queries)}catch{context.warnings.push(`search_query_expansion_provider_failed:${context.queryExpansionExtension.providerName}:using_builtin`);return expandedQueries}}if(context.queryExpansion.provider&&context.queryExpansion.provider!=="openai"&&context.queryExpansion.provider!=="ollama"){context.warnings.push(`search_query_expansion_provider_unavailable:${context.queryExpansion.provider}:using_builtin`)}return expandedQueries}async function executeSemanticVectorQuery(context,semanticVector,semanticLimit,vectorQueryOptions){if(context.extensionVectorAdapter?.query){try{return await Promise.resolve(context.extensionVectorAdapter.query({vector:semanticVector,limit:semanticLimit,settings:context.settings}))}catch(error){if(!context.vectorStore){throw new PmCliError(`Extension vector adapter query failed and no built-in fallback store is configured (${error instanceof Error?error.message:String(error)})`,EXIT_CODE.GENERIC_FAILURE)}context.warnings.push(`search_vector_adapter_failed:${context.extensionVectorAdapter.adapterName}:using_builtin`);return await executeVectorQuery(context.vectorStore,semanticVector,semanticLimit,vectorQueryOptions)}}if(context.vectorStore){return await executeVectorQuery(context.vectorStore,semanticVector,semanticLimit,vectorQueryOptions)}throw new PmCliError("Semantic search requires either a configured vector store or an extension vector adapter query handler",EXIT_CODE.USAGE)}function buildRerankCandidateContexts(hybridHits,filteredById,topK){const sortedForCandidates=[...hybridHits].sort((left,right)=>{if(left.score!==right.score){return right.score-left.score}return left.item.id.localeCompare(right.item.id)});return sortedForCandidates.slice(0,topK).map(hit=>{const document=filteredById.get(hit.item.id);return document?{hit,text:buildRerankCorpus(document)}:null}).filter(entry=>entry!==null)}async function resolveExtensionRerankScores(context,queryTrimmed,candidateContexts){if(!context.rerankExtension?.rerank){return null}try{const rawRerank=await Promise.resolve(context.rerankExtension.rerank({query:queryTrimmed,mode:"hybrid",model:context.rerank.model,top_k:context.rerank.top_k,settings:context.settings,candidates:candidateContexts.map(entry=>({id:entry.hit.item.id,text:entry.text,score:entry.hit.score}))}));const normalizedRerank=normalizeRerankOutput(rawRerank);if(normalizedRerank.length>0){return new Map(normalizedRerank.map(entry=>[entry.id,entry.score]))}context.warnings.push(`search_rerank_provider_invalid_response:${context.rerankExtension.providerName}:using_builtin`);return null}catch{context.warnings.push(`search_rerank_provider_failed:${context.rerankExtension.providerName}:using_builtin`);return null}}async function resolveRerankScores(context,queryTrimmed,candidateContexts){const extensionScores=await resolveExtensionRerankScores(context,queryTrimmed,candidateContexts);if(extensionScores){return extensionScores}const rerankCandidates=candidateContexts.map(entry=>({id:entry.hit.item.id,text:entry.text}));try{return await rerankCandidatesWithEmbeddings(context.provider,context.rerank.model,queryTrimmed,rerankCandidates,context.embeddingTimeoutMs)}catch{context.warnings.push("search_rerank_failed:using_hybrid_scores");return null}}function applyRerankScores(hybridHits,rerankScores){const rerankedIds=new Set(rerankScores.keys());const rerankedHits=hybridHits.map(hit=>{const rerankScore=rerankScores.get(hit.item.id);if(rerankScore===void 0){return hit}const matchedFields=new Set(hit.matched_fields);matchedFields.add("rerank");return{...hit,score:rerankScore,matched_fields:[...matchedFields].sort((left,right)=>left.localeCompare(right))}});rerankedHits.sort((left,right)=>{const leftWasReranked=rerankedIds.has(left.item.id);const rightWasReranked=rerankedIds.has(right.item.id);if(leftWasReranked!==rightWasReranked){return leftWasReranked?-1:1}if(left.score!==right.score){return right.score-left.score}return left.item.id.localeCompare(right.item.id)});return rerankedHits}async function applyHybridRerank(context,queryTrimmed,hybridHits,filteredById){if(!context.rerank.enabled||hybridHits.length<=1){return hybridHits}const candidateContexts=buildRerankCandidateContexts(hybridHits,filteredById,context.rerank.top_k);const rerankScores=await resolveRerankScores(context,queryTrimmed,candidateContexts);return rerankScores&&rerankScores.size>0?applyRerankScores(hybridHits,rerankScores):hybridHits}async function computeSemanticOrHybridHits(context){const semanticLimit=Math.max(context.limit??context.maxResults,context.maxResults);const embeddingOptions=context.embeddingTimeoutMs!==void 0?{timeout_ms:context.embeddingTimeoutMs}:{};const vectorQueryOptions=context.vectorQueryTimeoutMs!==void 0?{timeout_ms:context.vectorQueryTimeoutMs}:{};const queryTrimmed=context.query.trim();const expandedQueries=await resolveExpandedSemanticQueries(context,queryTrimmed);const queryVectors=await executeEmbeddingRequest(context.provider,expandedQueries,embeddingOptions);const queryVectorGroups=await Promise.all(queryVectors.map(async semanticVector=>await executeSemanticVectorQuery(context,semanticVector,semanticLimit,vectorQueryOptions)));const vectorHits=mergeVectorHitsById(queryVectorGroups);const filteredById=new Map(context.filteredDocuments.map(document=>[document.metadata.id,document]));const{semanticHits,semanticScores}=buildSemanticHits(vectorHits,filteredById);const vectorMatchCount=semanticScores.size;if(context.requestedMode==="semantic"){return{hits:forceExactIdHitsToTop(semanticHits,context.keywordHits),vectorMatchCount}}const hybridHits=await applyHybridRerank(context,queryTrimmed,combineHybridHits(filteredById,semanticScores,context.keywordHits,context.hybridSemanticWeight),filteredById);return{hits:forceExactIdHitsToTop(hybridHits,context.keywordHits),vectorMatchCount}}async function loadDocuments(pmRoot,itemFormat,typeToFolder,schema){const extensionFieldNames=collectRegisteredItemFieldNames(getActiveExtensionRegistrations());const readDocumentBody=async(metadata,preferredPath,preferredFormat)=>{const tryRead=async(targetPath,format)=>{await runActiveOnReadHooks({path:targetPath,scope:"project"});const raw=await fs.readFile(targetPath,"utf8");const parsed=parseItemDocument(raw,{format,schema,extensionFieldNames,onWarning:warning=>listWarnings.push(warning)});return parsed.body};try{return await tryRead(preferredPath,preferredFormat)}catch{const alternateFormat=preferredFormat==="toon"?"json_markdown":"toon";const alternatePath=getItemPath(pmRoot,metadata.type,metadata.id,alternateFormat,typeToFolder);try{return await tryRead(alternatePath,alternateFormat)}catch{listWarnings.push(`item_list_item_read_failed:${path.relative(pmRoot,alternatePath)}`);return""}}};const listWarnings=[];const cachedDocuments=await listAllDocumentCandidatesCached(pmRoot,itemFormat,typeToFolder,listWarnings,schema);const documents=[];if(cachedDocuments.length===0){const itemMetadataDocuments=await listAllItemMetadata(pmRoot,itemFormat,typeToFolder,listWarnings,schema);for(const metadata of itemMetadataDocuments){const preferredPath=getItemPath(pmRoot,metadata.type,metadata.id,itemFormat,typeToFolder);const body=await readDocumentBody(metadata,preferredPath,itemFormat);documents.push({metadata,body})}return{documents,warnings:[...new Set(listWarnings)].sort((left,right)=>left.localeCompare(right))}}for(const cachedDocument of cachedDocuments){if(typeof cachedDocument.body==="string"){documents.push({metadata:cachedDocument.metadata,body:cachedDocument.body});continue}const body=await readDocumentBody(cachedDocument.metadata,cachedDocument.item_path,cachedDocument.item_format);documents.push({metadata:cachedDocument.metadata,body})}return{documents,warnings:[...new Set(listWarnings)].sort((left,right)=>left.localeCompare(right))}}function readSearchFieldValue(hit,field){const normalized=field.trim();if(normalized.length===0){return null}if(normalized==="score"){return hit.score}if(normalized==="matched_fields"){return hit.matched_fields}if(normalized.startsWith("item.")){const itemKey=normalized.slice("item.".length);if(itemKey.length===0){return null}const itemRecord2=toItemRecord(hit.item);return itemRecord2[itemKey]??null}const hitRecord=hit;const itemRecord=toItemRecord(hit.item);if(Object.prototype.hasOwnProperty.call(itemRecord,normalized)){return itemRecord[normalized]??null}if(Object.prototype.hasOwnProperty.call(hitRecord,normalized)){return hitRecord[normalized]??null}return null}function projectSearchHits(hits,projection){if(projection.mode==="full"){return hits.map(({matched_all_terms:_matchedAllTerms,exact_id_match:_exactIdMatch,...hit})=>hit)}return hits.map(hit=>{const projected={};for(const field of projection.fields){projected[field]=readSearchFieldValue(hit,field)}return projected})}function resolveQueryExpansionExtension(queryExpansion){const provider=resolveExtensionSearchProviderByName(queryExpansion.provider??void 0);return provider?.queryExpansion?{providerName:provider.providerName,expand:provider.queryExpansion}:null}function resolveRerankExtension(settings){const provider=resolveExtensionSearchProviderByName(toOptionalNonEmptyString(settings.search?.provider));return provider?.rerank?{providerName:provider.providerName,rerank:provider.rerank}:null}function prepareSearchInput(rawQuery,rawOptions){const inlineWarnings=[];const inlineParse=parseInlineQueryFilters(rawQuery);const hasInlineFilters=Object.keys(inlineParse.inlineFilters).length>0;if(hasInlineFilters&&inlineParse.residualQuery.trim().length===0){throw new PmCliError("Inline field:value tokens consumed the entire query, leaving no search terms.",EXIT_CODE.USAGE,{examples:["pm search auth tag:area:search","pm list --tag area:search --status open"],nextSteps:["Add keyword terms alongside the inline filters, or use pm list for pure tag/status/type/priority filtering."]})}const query=hasInlineFilters?inlineParse.residualQuery:rawQuery;const options=applyInlineQueryFilters(rawOptions,inlineParse.inlineFilters,inlineWarnings);return{query,options,inlineWarnings,highlight:options.highlight===true,includeLinked:parseSearchBoolean(options.includeLinked),titleExact:parseSearchBoolean(options.titleExact),phraseExact:parseSearchBoolean(options.phraseExact),matchMode:parseSearchMatchMode(typeof options.matchMode==="string"?options.matchMode:void 0),minScoreOverride:parseMinScoreOverride(options.minScore),countOnly:options.count===true,tokens:parseSearchTokens(query),normalizedQuery:normalizeSearchPhrase(query),limit:parseLimit(options.limit),projection:parseSearchProjection(options,rawQuery),modeWasExplicit:typeof options.mode==="string"&&options.mode.trim().length>0}}async function resolveSearchRuntimeContext(prepared,global2){const pmRoot=resolvePmRoot(process.cwd(),global2.path);await assertInitializedTracker(pmRoot);const storedSettings=await readSettings(pmRoot);const settings=resolveSettingsWithSemanticRuntimeDefaults(storedSettings).settings;const statusRegistry=resolveRuntimeStatusRegistry(settings.schema);const runtimeFieldRegistry=resolveRuntimeFieldRegistry(settings.schema);validateSearchProjectionFields(prepared.projection,runtimeFieldRegistry,prepared.query);const runtimeFieldFilters=collectRuntimeFilterValues(prepared.options,runtimeFieldRegistry,"search");const typeRegistry=resolveItemTypeRegistry(settings,getActiveExtensionRegistrations());const providerResolution=resolveEmbeddingProviders(settings);const vectorResolution=resolveVectorStores(settings,pmRoot);const extensionSearchProvider=resolveExtensionSearchProvider(settings);const extensionVectorAdapter=resolveExtensionVectorAdapter(settings);const semanticWeightOverride=parseSemanticWeightOverride(prepared.options.semanticWeight);const queryExpansion=resolveQueryExpansionConfig(settings,providerResolution.active?.name??null);const rerank=resolveRerankConfig(settings,providerResolution.active?.model??toOptionalNonEmptyString(settings.search?.embedding_model)??"text-embedding-3-small");return{pmRoot,settings,statusRegistry,runtimeFieldRegistry,runtimeFieldFilters,typeRegistry,maxResults:resolveSearchMaxResults(settings),scoreThreshold:prepared.minScoreOverride??resolveSearchScoreThreshold(settings),semanticWeightProvided:prepared.options.semanticWeight!==void 0,semanticWeightOverride,hybridSemanticWeight:semanticWeightOverride??resolveHybridSemanticWeight(settings),tuning:resolveSearchTuning(settings),providerResolution,vectorResolution,extensionSearchProvider,extensionVectorAdapter,bm25Mode:resolveBuiltInBm25Mode({configuredProvider:toOptionalNonEmptyString(settings.search?.provider),hasEmbeddingProvider:providerResolution.active!==null,hasExtensionSearchProvider:extensionSearchProvider!==null}),queryExpansion,queryExpansionExtension:resolveQueryExpansionExtension(queryExpansion),rerank,rerankExtension:resolveRerankExtension(settings),...resolveRuntimeSearchMode({requested:prepared.options.mode,settings,configuredProvider:storedSettings.search?.provider??null,providerResolution,vectorResolution,extensionSearchProvider,extensionVectorAdapter})}}function resolveRuntimeSearchMode(params){const providerConfigured=resolveProviderConfigSource(params.providerResolution.active?.name,params.configuredProvider)==="configured";const builtInRunnable=providerConfigured&&(params.vectorResolution.active!==null||params.extensionVectorAdapter!==null);const semanticRunnable=params.extensionSearchProvider!==null||builtInRunnable;const resolution=resolveEffectiveSearchMode({requested:params.requested,settings:params.settings,semanticRunnable});return{effectiveMode:resolution.mode,modeSource:resolution.source}}async function loadFilteredSearchCorpus(prepared,runtime){const loadedDocuments=await loadDocuments(runtime.pmRoot,runtime.settings.item_format??"toon",runtime.typeRegistry.type_to_folder,runtime.settings.schema);const statusFilter=parseStatusFilterCsv(typeof prepared.options.status==="string"?prepared.options.status:void 0,runtime.statusRegistry,{strict:true});const lifecycleClassifier=lifecycleClassifierFromStatusRegistry(runtime.statusRegistry);const metadataFilteredDocuments=applyFilters(loadedDocuments.documents,prepared.options,runtime.typeRegistry,runtime.runtimeFieldFilters,statusFilter,lifecycleClassifier);return{warnings:loadedDocuments.warnings,allDocuments:loadedDocuments.documents,filteredDocuments:applyExactQueryFilters(metadataFilteredDocuments,prepared.normalizedQuery,{titleExact:prepared.titleExact,phraseExact:prepared.phraseExact||prepared.matchMode==="exact"})}}async function collectLinkedCorpusById(includeLinked,effectiveMode,filteredDocuments){const linkedCorpusById=new Map;if(!includeLinked||effectiveMode!=="keyword"&&effectiveMode!=="hybrid"){return linkedCorpusById}const projectRoot=process.cwd();const linkedCorpusRoots=await resolveLinkedCorpusRoots(projectRoot,resolveGlobalPmRoot(projectRoot));const linkedCorpusEntries=await Promise.all(filteredDocuments.map(async document=>[document.metadata.id,await loadLinkedCorpus(document,linkedCorpusRoots)]));for(const[id,corpus]of linkedCorpusEntries){linkedCorpusById.set(id,corpus)}return linkedCorpusById}async function computeKeywordSearchHits(prepared,runtime,filteredDocuments){const linkedCorpusById=await collectLinkedCorpusById(prepared.includeLinked,runtime.effectiveMode,filteredDocuments);const applyCoverageBonus=prepared.matchMode==="or";return filteredDocuments.map(document=>buildHybridLexicalScore(document,prepared.tokens,prepared.normalizedQuery,prepared.includeLinked,linkedCorpusById,runtime.tuning,runtime.settings.id_prefix,applyCoverageBonus)).filter(entry=>entry!==null).filter(entry=>prepared.matchMode!=="and"||entry.matched_all_terms===true)}function buildEmptySearchResultFromContext(response,countOnly){return emptySearchResult(response.query,response.effectiveMode,response.matchMode,response.options,response.includeLinked,response.scoreThreshold,response.hybridSemanticWeight,response.queryExpansion,response.rerank,response.projection,response.warnings,response.runtimeFieldFilters,countOnly,response.modeSource)}async function executeBuiltInBm25Search(prepared,runtime,filteredDocuments,keywordHits,warnings){if(runtime.bm25Mode===null||runtime.effectiveMode==="keyword"){return null}const hits=computeBuiltInBm25Hits({requestedMode:runtime.effectiveMode,query:prepared.query,filteredDocuments,keywordHits,corpusFields:resolveSearchCorpusFields(runtime.settings),bm25Params:resolveBm25Params(runtime.settings),hybridSemanticWeight:runtime.hybridSemanticWeight});if(runtime.bm25Mode==="auto-fallback"){warnings.push(`search_${runtime.effectiveMode}_offline_bm25:no_embedding_provider:using_lexical_bm25`)}return hits}async function executeExtensionSearchProvider(prepared,runtime,filteredDocuments,filteredById){if(!runtime.extensionSearchProvider){return null}const canUseBuiltInSemantic=runtime.providerResolution.active!==null&&(runtime.vectorResolution.active!==null||runtime.extensionVectorAdapter!==null);try{const providerResponse=await Promise.resolve(runtime.extensionSearchProvider.query({query:prepared.query,mode:runtime.effectiveMode,tokens:prepared.tokens,options:prepared.options,settings:runtime.settings,documents:filteredDocuments}));return normalizeExtensionProviderHits(runtime.extensionSearchProvider.providerName,providerResponse,filteredById)}catch(error){if(!canUseBuiltInSemantic){throw new PmCliError(`Extension search provider "${runtime.extensionSearchProvider.providerName}" failed: ${error instanceof Error?error.message:String(error)}`,EXIT_CODE.GENERIC_FAILURE)}return null}}function maybeApplyNoVectorFallback(effectiveMode,semanticResult,keywordHits,warnings){if(semanticResult.vectorMatchCount>0){return semanticResult.hits}warnings.push(`search_${effectiveMode}_degraded:no_vector_matches:results_are_lexical`);return effectiveMode==="semantic"?keywordHits:semanticResult.hits}async function prepareBuiltInSemanticSearch(runtime,effectiveMode,filteredDocuments,warnings,modeWasExplicit){const builtInSemanticWillRun=!runtime.extensionSearchProvider&&runtime.providerResolution.active!==null&&(runtime.vectorResolution.active!==null||runtime.extensionVectorAdapter!==null);if(modeWasExplicit&&builtInSemanticWillRun){await maybeEmitVectorIndexStaleWarning(runtime.pmRoot,filteredDocuments,warnings)}if(!runtime.extensionSearchProvider){requireSemanticDependencies(effectiveMode,runtime.providerResolution,runtime.vectorResolution,runtime.extensionVectorAdapter!==null)}}async function executeSemanticSearch(input){const{prepared,runtime,filteredDocuments,keywordHits,warnings,modeWasExplicit}=input;let hits=keywordHits;const bm25Hits=await executeBuiltInBm25Search(prepared,runtime,filteredDocuments,keywordHits,warnings);const semanticMode=runtime.effectiveMode;if(bm25Hits!==null||semanticMode==="keyword"){return{effectiveMode:runtime.effectiveMode,hits:bm25Hits??hits}}try{await prepareBuiltInSemanticSearch(runtime,semanticMode,filteredDocuments,warnings,modeWasExplicit);if(filteredDocuments.length===0||prepared.limit===0){return{effectiveMode:runtime.effectiveMode,hits:[]}}const filteredById=new Map(filteredDocuments.map(document=>[document.metadata.id,document]));hits=await executeExtensionSearchProvider(prepared,runtime,filteredDocuments,filteredById)??hits;if(hits===keywordHits){const{provider,vectorStore}=requireSemanticDependencies(semanticMode,runtime.providerResolution,runtime.vectorResolution,runtime.extensionVectorAdapter!==null);const semanticResult=await computeSemanticOrHybridHits({requestedMode:semanticMode,query:prepared.query,filteredDocuments,keywordHits,hybridSemanticWeight:runtime.hybridSemanticWeight,limit:prepared.limit,maxResults:runtime.maxResults,provider,vectorStore,extensionVectorAdapter:runtime.extensionVectorAdapter,queryExpansion:runtime.queryExpansion,queryExpansionExtension:runtime.queryExpansionExtension,rerank:runtime.rerank,rerankExtension:runtime.rerankExtension,warnings,settings:runtime.settings});hits=maybeApplyNoVectorFallback(semanticMode,semanticResult,keywordHits,warnings)}return{effectiveMode:runtime.effectiveMode,hits}}catch(error){warnings.push(buildExplicitSemanticFallbackWarning(runtime.effectiveMode,error));return{effectiveMode:"keyword",hits:keywordHits}}}function buildCountOnlySearchResult(response,total){if(response.projection.mode==="compact"&&response.options.compact===true){return withSearchWarnings({query:response.query.trim(),mode:response.effectiveMode,...compactModeSource(response.modeSource),items:[],count:total,total,count_only:true,filters:buildCompactSearchFilterSummary({mode:response.effectiveMode,matchMode:response.matchMode,options:response.options,includeLinked:response.includeLinked,titleExact:response.titleExact,phraseExact:response.phraseExact,scoreThreshold:response.scoreThreshold,hybridSemanticWeight:response.hybridSemanticWeight,runtimeFieldFilters:response.runtimeFieldFilters})},response.warnings)}const projectionFields=response.projection.mode==="full"?null:[...response.projection.fields];return withSearchWarnings({query:response.query.trim(),mode:response.effectiveMode,mode_source:response.modeSource,items:[],count:total,total,count_only:true,filters:buildVerboseSearchFilters({effectiveMode:response.effectiveMode,matchMode:response.matchMode,options:response.options,includeLinked:response.includeLinked,titleExact:response.titleExact,phraseExact:response.phraseExact,scoreThreshold:response.scoreThreshold,hybridSemanticWeight:response.hybridSemanticWeight,queryExpansion:response.queryExpansion,rerank:response.rerank,runtimeFieldFilters:response.runtimeFieldFilters}),projection:{mode:response.projection.mode,fields:projectionFields},now:nowIso()},response.warnings)}function compactModeSource(source){return source==="settings"?{mode_source:source}:{}}function withSearchWarnings(result,warnings){if(warnings.length===0){return result}return{...result,warnings}}function withSearchWorkspaceMemory(result,memory){return memory?{...result,workspace_memory:memory}:result}function attachSearchHighlights(prepared,filteredDocuments,limited){if(!prepared.highlight){return{hits:limited,projection:prepared.projection}}const documentById=new Map(filteredDocuments.map(document=>[document.metadata.id,document]));const hits=limited.map(hit=>({...hit,highlights:buildHitHighlights(documentById.get(hit.item.id),hit.matched_fields,prepared.tokens)}));if(prepared.projection.mode==="full"||prepared.projection.fields.includes("highlights")){return{hits,projection:prepared.projection}}return{hits,projection:{mode:prepared.projection.mode,fields:[...prepared.projection.fields,"highlights"]}}}function buildSearchResultForHits(response,projectedItems,total,limitedCount,effectiveProjection,pageExtras){const truncationExtras=limitedCount<total?{total}:{};if(response.projection.mode==="compact"&&response.options.compact===true){return withSearchWarnings({query:response.query.trim(),mode:response.effectiveMode,...compactModeSource(response.modeSource),items:projectedItems,count:projectedItems.length,...truncationExtras,...pageExtras,filters:buildCompactSearchFilterSummary({mode:response.effectiveMode,matchMode:response.matchMode,options:response.options,includeLinked:response.includeLinked,titleExact:response.titleExact,phraseExact:response.phraseExact,scoreThreshold:response.scoreThreshold,hybridSemanticWeight:response.hybridSemanticWeight,runtimeFieldFilters:response.runtimeFieldFilters})},response.warnings)}const finalProjectionFields=effectiveProjection.mode==="full"?null:[...effectiveProjection.fields];return{query:response.query.trim(),mode:response.effectiveMode,mode_source:response.modeSource,items:projectedItems,count:projectedItems.length,...truncationExtras,...pageExtras,filters:buildVerboseSearchFilters({effectiveMode:response.effectiveMode,matchMode:response.matchMode,options:response.options,includeLinked:response.includeLinked,titleExact:response.titleExact,phraseExact:response.phraseExact,scoreThreshold:response.scoreThreshold,hybridSemanticWeight:response.hybridSemanticWeight,queryExpansion:response.queryExpansion,rerank:response.rerank,runtimeFieldFilters:response.runtimeFieldFilters}),projection:{mode:effectiveProjection.mode,fields:finalProjectionFields},now:nowIso(),...response.warnings.length>0?{warnings:response.warnings}:{}}}function assertSearchPagingOptions(options){if(options.after!==void 0&&options.count===true){throw new PmCliError("Search --after cannot be combined with --count.",EXIT_CODE.USAGE)}}async function runSearch(rawQuery,rawOptions,global2){assertSearchPagingOptions(rawOptions);const prepared=prepareSearchInput(rawQuery,rawOptions);const runtime=await resolveSearchRuntimeContext(prepared,global2);const corpus=await loadFilteredSearchCorpus(prepared,runtime);const warnings=corpus.warnings;warnings.push(...prepared.inlineWarnings);const workspaceMemory=await readWorkspaceMemory(corpus.allDocuments.map(document=>document.metadata),{pmRoot:runtime.pmRoot,statusRegistry:runtime.statusRegistry,now:nowIso()});warnings.push(...workspaceMemory.warnings);const workspaceMemoryResult=searchWorkspaceMemoryReadResult(workspaceMemory,prepared.query);if(runtime.effectiveMode==="hybrid"&&runtime.semanticWeightProvided&&runtime.semanticWeightOverride===void 0){warnings.push("search_hybrid_semantic_weight_override_invalid:using_settings_default")}const responseBase={query:prepared.query,modeSource:runtime.modeSource,matchMode:prepared.matchMode,options:prepared.options,includeLinked:prepared.includeLinked,titleExact:prepared.titleExact,phraseExact:prepared.phraseExact,scoreThreshold:runtime.scoreThreshold,hybridSemanticWeight:runtime.hybridSemanticWeight,queryExpansion:runtime.queryExpansion,rerank:runtime.rerank,projection:prepared.projection,warnings,runtimeFieldFilters:runtime.runtimeFieldFilters};if(runtime.effectiveMode==="keyword"&&(corpus.filteredDocuments.length===0||prepared.limit===0&&!prepared.countOnly)){return withSearchWorkspaceMemory(buildEmptySearchResultFromContext({...responseBase,effectiveMode:runtime.effectiveMode},prepared.countOnly),workspaceMemoryResult)}const keywordHits=await computeKeywordSearchHits(prepared,runtime,corpus.filteredDocuments);const modeResult=await executeSemanticSearch({prepared,runtime,filteredDocuments:corpus.filteredDocuments,keywordHits,warnings,modeWasExplicit:prepared.modeWasExplicit});if(modeResult.effectiveMode!=="keyword"&&modeResult.hits.length===0&&(corpus.filteredDocuments.length===0||prepared.limit===0&&!prepared.countOnly)){return withSearchWorkspaceMemory(buildEmptySearchResultFromContext({...responseBase,effectiveMode:modeResult.effectiveMode},prepared.countOnly),workspaceMemoryResult)}const thresholded=modeResult.hits.filter(entry=>entry.exact_id_match===true||entry.score>=runtime.scoreThreshold);const sorted=sortHits(thresholded,runtime.statusRegistry);const total=sorted.length;const page=resolveSearchPage({sorted,query:prepared.query,mode:modeResult.effectiveMode,searchOptions:prepared.options,limit:prepared.limit??runtime.maxResults});const response={...responseBase,effectiveMode:modeResult.effectiveMode};if(prepared.countOnly){return withSearchWorkspaceMemory(buildCountOnlySearchResult(response,total),workspaceMemoryResult)}const{hits:projectedHits,projection:effectiveProjection}=attachSearchHighlights(prepared,corpus.filteredDocuments,page.limited);const projectedItems=projectSearchHits(projectedHits,effectiveProjection);return withSearchWorkspaceMemory(buildSearchResultForHits(response,projectedItems,total,page.limited.length,effectiveProjection,page.pageExtras),workspaceMemoryResult)}export{_testOnlySearchCommand,classifyImplicitSemanticFallbackReason,collectErrorCauseCodes,formatVectorIndexRecoveryWarnings,resolveEffectiveSearchMode2 as resolveEffectiveSearchMode,resolveHybridSemanticWeight2 as resolveHybridSemanticWeight,resolveSearchMaxResults2 as resolveSearchMaxResults,resolveSearchScoreThreshold2 as resolveSearchScoreThreshold,resolveSearchTuning2 as resolveSearchTuning,resolveVectorIndexRecovery,runSearch};
1987
3
  //# sourceMappingURL=search.js.map
1988
- //# debugId=a4117e19-af3e-50f5-b3cc-86c39aa4b439