@unbrained/pm-cli 2026.5.6 → 2026.5.11

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 (268) hide show
  1. package/.agents/pm/extensions/.managed-extensions.json +2 -2
  2. package/.agents/pm/extensions/beads/runtime.js +4 -4
  3. package/.agents/pm/extensions/beads/runtime.ts +5 -5
  4. package/.agents/pm/extensions/todos/runtime.js +7 -7
  5. package/.agents/pm/extensions/todos/runtime.ts +10 -10
  6. package/.agents/skills/HARNESS_COMPATIBILITY.md +45 -0
  7. package/.agents/skills/README.md +21 -0
  8. package/.agents/skills/pm-developer/SKILL.md +73 -0
  9. package/.agents/skills/pm-developer/references/COMMAND_PLAYBOOK.md +48 -0
  10. package/.agents/skills/pm-developer/references/PROMPTS.md +17 -0
  11. package/.agents/skills/pm-extensions/SKILL.md +57 -0
  12. package/.agents/skills/pm-extensions/references/LIFECYCLE.md +40 -0
  13. package/.agents/skills/pm-extensions/references/TROUBLESHOOTING.md +25 -0
  14. package/.agents/skills/pm-sdk/SKILL.md +50 -0
  15. package/.agents/skills/pm-sdk/references/INTEGRATION_CHECKLIST.md +31 -0
  16. package/.agents/skills/pm-sdk/references/PROMPTS.md +13 -0
  17. package/.agents/skills/pm-user/SKILL.md +59 -0
  18. package/.agents/skills/pm-user/references/PROMPTS.md +17 -0
  19. package/.agents/skills/pm-user/references/WORKFLOWS.md +35 -0
  20. package/.claude-plugin/marketplace.json +38 -0
  21. package/.pi/README.md +35 -0
  22. package/.pi/agents/pm-triage-agent.md +19 -0
  23. package/.pi/agents/pm-verification-agent.md +21 -0
  24. package/.pi/chains/pm-native-delivery.chain.md +11 -0
  25. package/.pi/extensions/pm-cli/index.js +387 -0
  26. package/.pi/prompts/pm-workflow.md +5 -0
  27. package/.pi/skills/pm-native/SKILL.md +44 -0
  28. package/.pi/skills/pm-release/SKILL.md +35 -0
  29. package/AGENTS.md +1 -1
  30. package/CHANGELOG.md +13 -0
  31. package/PRD.md +16 -16
  32. package/README.md +30 -4
  33. package/dist/cli/argv-utils.d.ts +5 -0
  34. package/dist/cli/argv-utils.js +34 -0
  35. package/dist/cli/argv-utils.js.map +1 -0
  36. package/dist/cli/bootstrap-args.d.ts +15 -0
  37. package/dist/cli/bootstrap-args.js +211 -0
  38. package/dist/cli/bootstrap-args.js.map +1 -1
  39. package/dist/cli/commander-usage.js +109 -3
  40. package/dist/cli/commander-usage.js.map +1 -1
  41. package/dist/cli/commands/claim.js +6 -6
  42. package/dist/cli/commands/claim.js.map +1 -1
  43. package/dist/cli/commands/close.js +9 -9
  44. package/dist/cli/commands/close.js.map +1 -1
  45. package/dist/cli/commands/comments.d.ts +2 -0
  46. package/dist/cli/commands/comments.js +57 -8
  47. package/dist/cli/commands/comments.js.map +1 -1
  48. package/dist/cli/commands/completion.js +40 -7
  49. package/dist/cli/commands/completion.js.map +1 -1
  50. package/dist/cli/commands/config.js +6 -3
  51. package/dist/cli/commands/config.js.map +1 -1
  52. package/dist/cli/commands/contracts.d.ts +19 -0
  53. package/dist/cli/commands/contracts.js +36 -1
  54. package/dist/cli/commands/contracts.js.map +1 -1
  55. package/dist/cli/commands/create.d.ts +2 -2
  56. package/dist/cli/commands/create.js +116 -55
  57. package/dist/cli/commands/create.js.map +1 -1
  58. package/dist/cli/commands/docs.js +13 -6
  59. package/dist/cli/commands/docs.js.map +1 -1
  60. package/dist/cli/commands/extension.d.ts +3 -1
  61. package/dist/cli/commands/extension.js +174 -2
  62. package/dist/cli/commands/extension.js.map +1 -1
  63. package/dist/cli/commands/files.js +19 -12
  64. package/dist/cli/commands/files.js.map +1 -1
  65. package/dist/cli/commands/get.js +5 -5
  66. package/dist/cli/commands/get.js.map +1 -1
  67. package/dist/cli/commands/guide.d.ts +55 -0
  68. package/dist/cli/commands/guide.js +260 -0
  69. package/dist/cli/commands/guide.js.map +1 -0
  70. package/dist/cli/commands/health.js +1 -1
  71. package/dist/cli/commands/health.js.map +1 -1
  72. package/dist/cli/commands/history.js +30 -10
  73. package/dist/cli/commands/history.js.map +1 -1
  74. package/dist/cli/commands/index.d.ts +1 -0
  75. package/dist/cli/commands/index.js +1 -0
  76. package/dist/cli/commands/index.js.map +1 -1
  77. package/dist/cli/commands/init.d.ts +2 -0
  78. package/dist/cli/commands/init.js +21 -1
  79. package/dist/cli/commands/init.js.map +1 -1
  80. package/dist/cli/commands/learnings.js +3 -3
  81. package/dist/cli/commands/learnings.js.map +1 -1
  82. package/dist/cli/commands/metadata-normalizers.d.ts +4 -0
  83. package/dist/cli/commands/metadata-normalizers.js +37 -0
  84. package/dist/cli/commands/metadata-normalizers.js.map +1 -0
  85. package/dist/cli/commands/notes.js +3 -3
  86. package/dist/cli/commands/notes.js.map +1 -1
  87. package/dist/cli/commands/reindex.js +180 -156
  88. package/dist/cli/commands/reindex.js.map +1 -1
  89. package/dist/cli/commands/restore.d.ts +2 -2
  90. package/dist/cli/commands/restore.js +44 -24
  91. package/dist/cli/commands/restore.js.map +1 -1
  92. package/dist/cli/commands/search.d.ts +2 -0
  93. package/dist/cli/commands/search.js +45 -26
  94. package/dist/cli/commands/search.js.map +1 -1
  95. package/dist/cli/commands/test-all.d.ts +2 -0
  96. package/dist/cli/commands/test-all.js +2 -0
  97. package/dist/cli/commands/test-all.js.map +1 -1
  98. package/dist/cli/commands/test.d.ts +1 -0
  99. package/dist/cli/commands/test.js +13 -5
  100. package/dist/cli/commands/test.js.map +1 -1
  101. package/dist/cli/commands/update.js +188 -157
  102. package/dist/cli/commands/update.js.map +1 -1
  103. package/dist/cli/commands/validate.js +1 -1
  104. package/dist/cli/commands/validate.js.map +1 -1
  105. package/dist/cli/error-guidance.d.ts +9 -1
  106. package/dist/cli/error-guidance.js +147 -6
  107. package/dist/cli/error-guidance.js.map +1 -1
  108. package/dist/cli/guide-topics.d.ts +25 -0
  109. package/dist/cli/guide-topics.js +283 -0
  110. package/dist/cli/guide-topics.js.map +1 -0
  111. package/dist/cli/help-content.js +25 -1
  112. package/dist/cli/help-content.js.map +1 -1
  113. package/dist/cli/help-json-payload.js +11 -1
  114. package/dist/cli/help-json-payload.js.map +1 -1
  115. package/dist/cli/main.js +69 -6
  116. package/dist/cli/main.js.map +1 -1
  117. package/dist/cli/register-list-query.js +38 -1
  118. package/dist/cli/register-list-query.js.map +1 -1
  119. package/dist/cli/register-mutation.js +17 -4
  120. package/dist/cli/register-mutation.js.map +1 -1
  121. package/dist/cli/register-setup.js +15 -1
  122. package/dist/cli/register-setup.js.map +1 -1
  123. package/dist/cli/telemetry-flush.d.ts +2 -0
  124. package/dist/cli/telemetry-flush.js +4 -0
  125. package/dist/cli/telemetry-flush.js.map +1 -0
  126. package/dist/cli.js +1 -2
  127. package/dist/cli.js.map +1 -1
  128. package/dist/core/extensions/extension-types.d.ts +72 -0
  129. package/dist/core/extensions/extension-types.js +24 -0
  130. package/dist/core/extensions/extension-types.js.map +1 -1
  131. package/dist/core/extensions/loader.d.ts +1 -0
  132. package/dist/core/extensions/loader.js +766 -7
  133. package/dist/core/extensions/loader.js.map +1 -1
  134. package/dist/core/history/history.js +32 -11
  135. package/dist/core/history/history.js.map +1 -1
  136. package/dist/core/item/item-format.d.ts +2 -2
  137. package/dist/core/item/item-format.js +16 -16
  138. package/dist/core/item/item-format.js.map +1 -1
  139. package/dist/core/lock/lock.js +2 -0
  140. package/dist/core/lock/lock.js.map +1 -1
  141. package/dist/core/schema/runtime-field-filters.js +1 -1
  142. package/dist/core/schema/runtime-field-filters.js.map +1 -1
  143. package/dist/core/schema/runtime-field-values.js +2 -2
  144. package/dist/core/schema/runtime-field-values.js.map +1 -1
  145. package/dist/core/schema/runtime-schema.d.ts +1 -1
  146. package/dist/core/schema/runtime-schema.js +3 -3
  147. package/dist/core/schema/runtime-schema.js.map +1 -1
  148. package/dist/core/search/cache.js +7 -21
  149. package/dist/core/search/cache.js.map +1 -1
  150. package/dist/core/search/corpus.d.ts +13 -0
  151. package/dist/core/search/corpus.js +74 -0
  152. package/dist/core/search/corpus.js.map +1 -0
  153. package/dist/core/search/embedding-batches.js +90 -30
  154. package/dist/core/search/embedding-batches.js.map +1 -1
  155. package/dist/core/sentry/instrument.d.ts +18 -1
  156. package/dist/core/sentry/instrument.js +128 -12
  157. package/dist/core/sentry/instrument.js.map +1 -1
  158. package/dist/core/shared/constants.d.ts +1 -1
  159. package/dist/core/shared/constants.js +21 -1
  160. package/dist/core/shared/constants.js.map +1 -1
  161. package/dist/core/shared/errors.d.ts +8 -0
  162. package/dist/core/shared/errors.js.map +1 -1
  163. package/dist/core/shared/levenshtein.d.ts +1 -0
  164. package/dist/core/shared/levenshtein.js +37 -0
  165. package/dist/core/shared/levenshtein.js.map +1 -0
  166. package/dist/core/store/front-matter-cache.d.ts +1 -1
  167. package/dist/core/store/front-matter-cache.js +13 -13
  168. package/dist/core/store/front-matter-cache.js.map +1 -1
  169. package/dist/core/store/item-format-migration.js +5 -2
  170. package/dist/core/store/item-format-migration.js.map +1 -1
  171. package/dist/core/store/item-store.js +16 -15
  172. package/dist/core/store/item-store.js.map +1 -1
  173. package/dist/core/store/paths.js +35 -2
  174. package/dist/core/store/paths.js.map +1 -1
  175. package/dist/core/store/settings.js +216 -2
  176. package/dist/core/store/settings.js.map +1 -1
  177. package/dist/core/telemetry/runtime.d.ts +1 -0
  178. package/dist/core/telemetry/runtime.js +102 -3
  179. package/dist/core/telemetry/runtime.js.map +1 -1
  180. package/dist/core/test/item-test-run-tracking.js +2 -2
  181. package/dist/core/test/item-test-run-tracking.js.map +1 -1
  182. package/dist/mcp/server.d.ts +2 -0
  183. package/dist/mcp/server.js +407 -0
  184. package/dist/mcp/server.js.map +1 -0
  185. package/dist/pi/native.d.ts +5 -0
  186. package/dist/pi/native.js +236 -0
  187. package/dist/pi/native.js.map +1 -0
  188. package/dist/sdk/cli-contracts.d.ts +24 -2
  189. package/dist/sdk/cli-contracts.js +317 -2
  190. package/dist/sdk/cli-contracts.js.map +1 -1
  191. package/dist/sdk/index.d.ts +12 -1
  192. package/dist/sdk/index.js +8 -1
  193. package/dist/sdk/index.js.map +1 -1
  194. package/dist/types.d.ts +51 -2
  195. package/dist/types.js.map +1 -1
  196. package/docs/AGENT_GUIDE.md +15 -0
  197. package/docs/ARCHITECTURE.md +2 -2
  198. package/docs/CLAUDE_CODE_PLUGIN.md +225 -0
  199. package/docs/CODEX_PLUGIN.md +33 -0
  200. package/docs/COMMANDS.md +6 -2
  201. package/docs/CONFIGURATION.md +2 -8
  202. package/docs/EXTENSIONS.md +688 -0
  203. package/docs/MIGRATION_CLI_SIMPLIFICATION.md +64 -0
  204. package/docs/PI_PACKAGE.md +141 -0
  205. package/docs/QUICKSTART.md +1 -0
  206. package/docs/README.md +30 -1
  207. package/docs/RELEASING.md +4 -2
  208. package/docs/SDK.md +444 -2
  209. package/docs/examples/ci/github-actions-pm-extension-gate.yml +53 -0
  210. package/docs/examples/ci/gitlab-ci-pm-extension-gate.yml +41 -0
  211. package/docs/examples/ci/jenkins-pm-extension-gate.Jenkinsfile +45 -0
  212. package/docs/examples/policy-restricted-extension/README.md +74 -0
  213. package/docs/examples/policy-restricted-extension/index.js +21 -0
  214. package/docs/examples/policy-restricted-extension/manifest.json +21 -0
  215. package/docs/examples/policy-restricted-extension/package.json +8 -0
  216. package/docs/examples/sdk-app-embedding/README.md +39 -0
  217. package/docs/examples/sdk-app-embedding/package.json +9 -0
  218. package/docs/examples/sdk-app-embedding/run-embedded-pm.mjs +61 -0
  219. package/docs/examples/sdk-contract-consumer/README.md +57 -0
  220. package/docs/examples/sdk-contract-consumer/inspect-contracts.mjs +47 -0
  221. package/docs/examples/sdk-contract-consumer/package.json +10 -0
  222. package/docs/examples/starter-extension/README.md +57 -42
  223. package/docs/examples/starter-extension/manifest.json +15 -0
  224. package/marketplace.json +34 -0
  225. package/package.json +38 -4
  226. package/plugins/pm-cli-claude/.claude-plugin/plugin.json +23 -0
  227. package/plugins/pm-cli-claude/.mcp.json +12 -0
  228. package/plugins/pm-cli-claude/README.md +225 -0
  229. package/plugins/pm-cli-claude/agents/pm-coordinator.md +48 -0
  230. package/plugins/pm-cli-claude/agents/pm-delivery-chain.md +88 -0
  231. package/plugins/pm-cli-claude/agents/pm-triage-agent.md +83 -0
  232. package/plugins/pm-cli-claude/agents/pm-verification-agent.md +88 -0
  233. package/plugins/pm-cli-claude/commands/pm-audit.md +39 -0
  234. package/plugins/pm-cli-claude/commands/pm-calendar.md +41 -0
  235. package/plugins/pm-cli-claude/commands/pm-close-task.md +20 -0
  236. package/plugins/pm-cli-claude/commands/pm-developer.md +38 -0
  237. package/plugins/pm-cli-claude/commands/pm-init.md +44 -0
  238. package/plugins/pm-cli-claude/commands/pm-list.md +39 -0
  239. package/plugins/pm-cli-claude/commands/pm-new.md +36 -0
  240. package/plugins/pm-cli-claude/commands/pm-planner.md +51 -0
  241. package/plugins/pm-cli-claude/commands/pm-release.md +41 -0
  242. package/plugins/pm-cli-claude/commands/pm-search.md +21 -0
  243. package/plugins/pm-cli-claude/commands/pm-start-task.md +27 -0
  244. package/plugins/pm-cli-claude/commands/pm-status.md +15 -0
  245. package/plugins/pm-cli-claude/commands/pm-triage.md +35 -0
  246. package/plugins/pm-cli-claude/commands/pm-workflow.md +49 -0
  247. package/plugins/pm-cli-claude/hooks/hooks.json +17 -0
  248. package/plugins/pm-cli-claude/hooks/session-start.mjs +120 -0
  249. package/plugins/pm-cli-claude/scripts/pm-mcp-server.mjs +60 -0
  250. package/plugins/pm-cli-claude/skills/pm-audit/SKILL.md +88 -0
  251. package/plugins/pm-cli-claude/skills/pm-developer/SKILL.md +116 -0
  252. package/plugins/pm-cli-claude/skills/pm-planner/SKILL.md +118 -0
  253. package/plugins/pm-cli-claude/skills/pm-release/SKILL.md +83 -0
  254. package/plugins/pm-cli-claude/skills/pm-workflow/SKILL.md +148 -0
  255. package/plugins/pm-cli-codex/.codex-plugin/plugin.json +45 -0
  256. package/plugins/pm-cli-codex/.mcp.json +14 -0
  257. package/plugins/pm-cli-codex/README.md +30 -0
  258. package/plugins/pm-cli-codex/assets/pm-cli-small.svg +4 -0
  259. package/plugins/pm-cli-codex/commands/pm-audit.md +8 -0
  260. package/plugins/pm-cli-codex/commands/pm-close-task.md +9 -0
  261. package/plugins/pm-cli-codex/commands/pm-start-task.md +9 -0
  262. package/plugins/pm-cli-codex/scripts/pm-mcp-server.mjs +54 -0
  263. package/plugins/pm-cli-codex/skills/pm-auditor/SKILL.md +21 -0
  264. package/plugins/pm-cli-codex/skills/pm-auditor/agents/openai.yaml +6 -0
  265. package/plugins/pm-cli-codex/skills/pm-native/SKILL.md +57 -0
  266. package/plugins/pm-cli-codex/skills/pm-native/agents/openai.yaml +6 -0
  267. package/plugins/pm-cli-codex/skills/pm-release/SKILL.md +19 -0
  268. package/plugins/pm-cli-codex/skills/pm-release/agents/openai.yaml +6 -0
@@ -1,9 +1,13 @@
1
1
  import jsonPatch from "fast-json-patch";
2
- import { EMPTY_CANONICAL_DOCUMENT, FRONT_MATTER_KEY_ORDER } from "../shared/constants.js";
2
+ import { FRONT_MATTER_KEY_ORDER } from "../shared/constants.js";
3
3
  import { runActiveServiceOverride } from "../extensions/index.js";
4
4
  import { appendLineAtomic } from "../fs/fs-utils.js";
5
5
  import { canonicalDocument } from "../item/item-format.js";
6
6
  import { orderObject, sha256Hex, stableStringify } from "../shared/serialization.js";
7
+ const EMPTY_LEGACY_HASH_DOCUMENT = {
8
+ front_matter: {},
9
+ body: "",
10
+ };
7
11
  function decodeJsonPointer(path) {
8
12
  if (!path || path === "/") {
9
13
  return [];
@@ -65,38 +69,55 @@ function normalizeHistoryPatchOps(beforeDocument, patch) {
65
69
  return normalized;
66
70
  }
67
71
  function canonicalHashDocument(document) {
68
- const hasFrontMatter = document.front_matter && Object.keys(document.front_matter).length > 0;
69
- if (!hasFrontMatter) {
72
+ const hasMetadata = document.metadata && Object.keys(document.metadata).length > 0;
73
+ if (!hasMetadata) {
70
74
  return {
71
75
  front_matter: {},
72
76
  body: document.body ?? "",
73
77
  };
74
78
  }
75
79
  const canonical = canonicalDocument(document);
76
- const orderedFrontMatter = orderObject(canonical.front_matter, FRONT_MATTER_KEY_ORDER);
80
+ const orderedFrontMatter = orderObject(canonical.metadata, FRONT_MATTER_KEY_ORDER);
77
81
  return {
78
82
  front_matter: orderedFrontMatter,
79
83
  body: canonical.body,
80
84
  };
81
85
  }
86
+ function canonicalPatchDocument(document) {
87
+ const hasMetadata = document.metadata && Object.keys(document.metadata).length > 0;
88
+ if (!hasMetadata) {
89
+ return {
90
+ metadata: {},
91
+ body: document.body ?? "",
92
+ };
93
+ }
94
+ const canonical = canonicalDocument(document);
95
+ const orderedMetadata = orderObject(canonical.metadata, FRONT_MATTER_KEY_ORDER);
96
+ return {
97
+ metadata: orderedMetadata,
98
+ body: canonical.body,
99
+ };
100
+ }
82
101
  export function hashDocument(document) {
83
102
  return sha256Hex(stableStringify(canonicalHashDocument(document)));
84
103
  }
85
104
  export function hashEmptyDocument() {
86
- return sha256Hex(stableStringify(EMPTY_CANONICAL_DOCUMENT));
105
+ return sha256Hex(stableStringify(EMPTY_LEGACY_HASH_DOCUMENT));
87
106
  }
88
107
  export function createHistoryEntry(params) {
89
- const beforeCanonical = canonicalHashDocument(params.before);
90
- const afterCanonical = canonicalHashDocument(params.after);
91
- const rawPatch = jsonPatch.compare(beforeCanonical, afterCanonical);
92
- const patch = normalizeHistoryPatchOps(beforeCanonical, rawPatch);
108
+ const beforeHashCanonical = canonicalHashDocument(params.before);
109
+ const afterHashCanonical = canonicalHashDocument(params.after);
110
+ const beforePatchCanonical = canonicalPatchDocument(params.before);
111
+ const afterPatchCanonical = canonicalPatchDocument(params.after);
112
+ const rawPatch = jsonPatch.compare(beforePatchCanonical, afterPatchCanonical);
113
+ const patch = normalizeHistoryPatchOps(beforePatchCanonical, rawPatch);
93
114
  return {
94
115
  ts: params.nowIso,
95
116
  author: params.author,
96
117
  op: params.op,
97
118
  patch,
98
- before_hash: sha256Hex(stableStringify(beforeCanonical)),
99
- after_hash: sha256Hex(stableStringify(afterCanonical)),
119
+ before_hash: sha256Hex(stableStringify(beforeHashCanonical)),
120
+ after_hash: sha256Hex(stableStringify(afterHashCanonical)),
100
121
  message: params.message === undefined ? undefined : params.message,
101
122
  };
102
123
  }
@@ -1 +1 @@
1
- {"version":3,"file":"history.js","sourceRoot":"/","sources":["core/history/history.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,iBAAiB,CAAC;AACxC,OAAO,EAAE,wBAAwB,EAAE,sBAAsB,EAAE,MAAM,wBAAwB,CAAC;AAC1F,OAAO,EAAE,wBAAwB,EAAE,MAAM,wBAAwB,CAAC;AAClE,OAAO,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AACrD,OAAO,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAC3D,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,eAAe,EAAE,MAAM,4BAA4B,CAAC;AAGrF,SAAS,iBAAiB,CAAC,IAAY;IACrC,IAAI,CAAC,IAAI,IAAI,IAAI,KAAK,GAAG,EAAE,CAAC;QAC1B,OAAO,EAAE,CAAC;IACZ,CAAC;IACD,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC;QAC1B,OAAO,EAAE,CAAC;IACZ,CAAC;IACD,OAAO,IAAI;SACR,KAAK,CAAC,CAAC,CAAC;SACR,KAAK,CAAC,GAAG,CAAC;SACV,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,CAAC,UAAU,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC,UAAU,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC,CAAC;AAC3E,CAAC;AAED,SAAS,oBAAoB,CAAC,QAAiB,EAAE,IAAY;IAC3D,MAAM,QAAQ,GAAG,iBAAiB,CAAC,IAAI,CAAC,CAAC;IACzC,IAAI,QAAQ,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAC1B,OAAO,IAAI,CAAC;IACd,CAAC;IACD,IAAI,MAAM,GAAY,QAAQ,CAAC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MAAM,KAAK,QAAQ,IAAI,QAAQ,CAAC,MAAM,KAAK,IAAI,EAAE,CAAC;YACpE,MAAM,MAAM,GAAG,QAAQ,CAAC,MAKvB,CAAC;YACF,IAAI,MAAM,CAAC,IAAI,KAAK,IAAI,EAAE,CAAC;gBACzB,OAAO;YACT,CAAC;YACD,MAAM,eAAe,GAAG,OAAO,MAAM,CAAC,YAAY,KAAK,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,YAAY,CAAC,CAAC,CAAC,WAAW,CAAC;YACpG,IAAI,OAAO,MAAM,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;gBACpC,MAAM,gBAAgB,CAAC,eAAe,EAAE,MAAM,CAAC,IAAI,CAAC,CAAC;gBACrD,OAAO;YACT,CAAC;YACD,MAAM,SAAS,GAAG,CAAC,MAAM,CAAC,KAAK,IAAI,KAAK,CAAiB,CAAC;YAC1D,MAAM,gBAAgB,CAAC,eAAe,EAAE,IAAI,CAAC,SAAS,CAAC,SAAS,CAAC,CAAC,CAAC;YACnE,OAAO;QACT,CAAC;IACH,CAAC;IACD,MAAM,gBAAgB,CAAC,WAAW,EAAE,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC;AAC7D,CAAC","sourcesContent":["import jsonPatch from \"fast-json-patch\";\nimport { EMPTY_CANONICAL_DOCUMENT, FRONT_MATTER_KEY_ORDER } from \"../shared/constants.js\";\nimport { runActiveServiceOverride } from \"../extensions/index.js\";\nimport { appendLineAtomic } from \"../fs/fs-utils.js\";\nimport { canonicalDocument } from \"../item/item-format.js\";\nimport { orderObject, sha256Hex, stableStringify } from \"../shared/serialization.js\";\nimport type { HistoryEntry, HistoryPatchOp, ItemDocument } from \"../../types/index.js\";\n\nfunction decodeJsonPointer(path: string): string[] {\n if (!path || path === \"/\") {\n return [];\n }\n if (!path.startsWith(\"/\")) {\n return [];\n }\n return path\n .slice(1)\n .split(\"/\")\n .map((segment) => segment.replaceAll(\"~1\", \"/\").replaceAll(\"~0\", \"~\"));\n}\n\nfunction isDefinedPointerPath(document: unknown, path: string): boolean {\n const segments = decodeJsonPointer(path);\n if (segments.length === 0) {\n return true;\n }\n let cursor: unknown = document;\n for (const segment of segments) {\n if (Array.isArray(cursor)) {\n if (segment === \"-\" || !/^(0|[1-9]\\d*)$/.test(segment)) {\n return false;\n }\n const index = Number(segment);\n if (!Number.isInteger(index) || index < 0 || index >= cursor.length) {\n return false;\n }\n const next = cursor[index];\n if (next === undefined) {\n return false;\n }\n cursor = next;\n continue;\n }\n if (typeof cursor !== \"object\" || cursor === null) {\n return false;\n }\n if (!Object.prototype.hasOwnProperty.call(cursor, segment)) {\n return false;\n }\n const next = (cursor as Record<string, unknown>)[segment];\n if (next === undefined) {\n return false;\n }\n cursor = next;\n }\n return true;\n}\n\nfunction normalizeHistoryPatchOps(\n beforeDocument: { front_matter: Record<string, unknown>; body: string },\n patch: HistoryPatchOp[],\n): HistoryPatchOp[] {\n const normalized: HistoryPatchOp[] = [];\n let replayCursor: unknown = structuredClone(beforeDocument);\n for (const operation of patch) {\n const normalizedOperation =\n operation.op === \"replace\" && !isDefinedPointerPath(replayCursor, operation.path)\n ? ({ ...operation, op: \"add\" } as HistoryPatchOp)\n : operation;\n normalized.push(normalizedOperation);\n replayCursor = jsonPatch.applyPatch(\n replayCursor,\n [normalizedOperation as jsonPatch.Operation],\n true,\n true,\n ).newDocument as unknown;\n }\n return normalized;\n}\n\nfunction canonicalHashDocument(document: ItemDocument): { front_matter: Record<string, unknown>; body: string } {\n const hasFrontMatter = document.front_matter && Object.keys(document.front_matter).length > 0;\n if (!hasFrontMatter) {\n return {\n front_matter: {},\n body: document.body ?? \"\",\n };\n }\n const canonical = canonicalDocument(document);\n const orderedFrontMatter = orderObject(\n canonical.front_matter as unknown as Record<string, unknown>,\n FRONT_MATTER_KEY_ORDER,\n );\n return {\n front_matter: orderedFrontMatter,\n body: canonical.body,\n };\n}\n\nexport function hashDocument(document: ItemDocument): string {\n return sha256Hex(stableStringify(canonicalHashDocument(document)));\n}\n\nexport function hashEmptyDocument(): string {\n return sha256Hex(stableStringify(EMPTY_CANONICAL_DOCUMENT));\n}\n\nexport function createHistoryEntry(params: {\n nowIso: string;\n author: string;\n op: string;\n before: ItemDocument;\n after: ItemDocument;\n message?: string;\n}): HistoryEntry {\n const beforeCanonical = canonicalHashDocument(params.before);\n const afterCanonical = canonicalHashDocument(params.after);\n const rawPatch = jsonPatch.compare(beforeCanonical, afterCanonical) as HistoryPatchOp[];\n const patch = normalizeHistoryPatchOps(beforeCanonical, rawPatch);\n\n return {\n ts: params.nowIso,\n author: params.author,\n op: params.op,\n patch,\n before_hash: sha256Hex(stableStringify(beforeCanonical)),\n after_hash: sha256Hex(stableStringify(afterCanonical)),\n message: params.message === undefined ? undefined : params.message,\n };\n}\n\nexport async function appendHistoryEntry(historyPath: string, entry: HistoryEntry): Promise<void> {\n const override = await runActiveServiceOverride(\"history_append\", {\n history_path: historyPath,\n entry,\n });\n if (override.handled) {\n if (override.result === false) {\n return;\n }\n if (typeof override.result === \"string\") {\n await appendLineAtomic(historyPath, override.result);\n return;\n }\n if (typeof override.result === \"object\" && override.result !== null) {\n const record = override.result as {\n history_path?: unknown;\n entry?: unknown;\n line?: unknown;\n skip?: unknown;\n };\n if (record.skip === true) {\n return;\n }\n const nextHistoryPath = typeof record.history_path === \"string\" ? record.history_path : historyPath;\n if (typeof record.line === \"string\") {\n await appendLineAtomic(nextHistoryPath, record.line);\n return;\n }\n const nextEntry = (record.entry ?? entry) as HistoryEntry;\n await appendLineAtomic(nextHistoryPath, JSON.stringify(nextEntry));\n return;\n }\n }\n await appendLineAtomic(historyPath, JSON.stringify(entry));\n}\n"]}
1
+ {"version":3,"file":"history.js","sourceRoot":"/","sources":["core/history/history.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,iBAAiB,CAAC;AACxC,OAAO,EAAE,sBAAsB,EAAE,MAAM,wBAAwB,CAAC;AAChE,OAAO,EAAE,wBAAwB,EAAE,MAA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cesContent":["import jsonPatch from \"fast-json-patch\";\nimport { FRONT_MATTER_KEY_ORDER } from \"../shared/constants.js\";\nimport { runActiveServiceOverride } from \"../extensions/index.js\";\nimport { appendLineAtomic } from \"../fs/fs-utils.js\";\nimport { canonicalDocument } from \"../item/item-format.js\";\nimport { orderObject, sha256Hex, stableStringify } from \"../shared/serialization.js\";\nimport type { HistoryEntry, HistoryPatchOp, ItemDocument } from \"../../types/index.js\";\n\nconst EMPTY_LEGACY_HASH_DOCUMENT = {\n front_matter: {},\n body: \"\",\n};\n\nfunction decodeJsonPointer(path: string): string[] {\n if (!path || path === \"/\") {\n return [];\n }\n if (!path.startsWith(\"/\")) {\n return [];\n }\n return path\n .slice(1)\n .split(\"/\")\n .map((segment) => segment.replaceAll(\"~1\", \"/\").replaceAll(\"~0\", \"~\"));\n}\n\nfunction isDefinedPointerPath(document: unknown, path: string): boolean {\n const segments = decodeJsonPointer(path);\n if (segments.length === 0) {\n return true;\n }\n let cursor: unknown = document;\n for (const segment of segments) {\n if (Array.isArray(cursor)) {\n if (segment === \"-\" || !/^(0|[1-9]\\d*)$/.test(segment)) {\n return false;\n }\n const index = Number(segment);\n if (!Number.isInteger(index) || index < 0 || index >= cursor.length) {\n return false;\n }\n const next = cursor[index];\n if (next === undefined) {\n return false;\n }\n cursor = next;\n continue;\n }\n if (typeof cursor !== \"object\" || cursor === null) {\n return false;\n }\n if (!Object.prototype.hasOwnProperty.call(cursor, segment)) {\n return false;\n }\n const next = (cursor as Record<string, unknown>)[segment];\n if (next === undefined) {\n return false;\n }\n cursor = next;\n }\n return true;\n}\n\nfunction normalizeHistoryPatchOps(\n beforeDocument: { metadata: Record<string, unknown>; body: string },\n patch: HistoryPatchOp[],\n): HistoryPatchOp[] {\n const normalized: HistoryPatchOp[] = [];\n let replayCursor: unknown = structuredClone(beforeDocument);\n for (const operation of patch) {\n const normalizedOperation =\n operation.op === \"replace\" && !isDefinedPointerPath(replayCursor, operation.path)\n ? ({ ...operation, op: \"add\" } as HistoryPatchOp)\n : operation;\n normalized.push(normalizedOperation);\n replayCursor = jsonPatch.applyPatch(\n replayCursor,\n [normalizedOperation as jsonPatch.Operation],\n true,\n true,\n ).newDocument as unknown;\n }\n return normalized;\n}\n\nfunction canonicalHashDocument(document: ItemDocument): { front_matter: Record<string, unknown>; body: string } {\n const hasMetadata = document.metadata && Object.keys(document.metadata).length > 0;\n if (!hasMetadata) {\n return {\n front_matter: {},\n body: document.body ?? \"\",\n };\n }\n const canonical = canonicalDocument(document);\n const orderedFrontMatter = orderObject(canonical.metadata as unknown as Record<string, unknown>, FRONT_MATTER_KEY_ORDER);\n return {\n front_matter: orderedFrontMatter,\n body: canonical.body,\n };\n}\n\nfunction canonicalPatchDocument(document: ItemDocument): { metadata: Record<string, unknown>; body: string } {\n const hasMetadata = document.metadata && Object.keys(document.metadata).length > 0;\n if (!hasMetadata) {\n return {\n metadata: {},\n body: document.body ?? \"\",\n };\n }\n const canonical = canonicalDocument(document);\n const orderedMetadata = orderObject(canonical.metadata as unknown as Record<string, unknown>, FRONT_MATTER_KEY_ORDER);\n return {\n metadata: orderedMetadata,\n body: canonical.body,\n };\n}\n\nexport function hashDocument(document: ItemDocument): string {\n return sha256Hex(stableStringify(canonicalHashDocument(document)));\n}\n\nexport function hashEmptyDocument(): string {\n return sha256Hex(stableStringify(EMPTY_LEGACY_HASH_DOCUMENT));\n}\n\nexport function createHistoryEntry(params: {\n nowIso: string;\n author: string;\n op: string;\n before: ItemDocument;\n after: ItemDocument;\n message?: string;\n}): HistoryEntry {\n const beforeHashCanonical = canonicalHashDocument(params.before);\n const afterHashCanonical = canonicalHashDocument(params.after);\n const beforePatchCanonical = canonicalPatchDocument(params.before);\n const afterPatchCanonical = canonicalPatchDocument(params.after);\n const rawPatch = jsonPatch.compare(beforePatchCanonical, afterPatchCanonical) as HistoryPatchOp[];\n const patch = normalizeHistoryPatchOps(beforePatchCanonical, rawPatch);\n\n return {\n ts: params.nowIso,\n author: params.author,\n op: params.op,\n patch,\n before_hash: sha256Hex(stableStringify(beforeHashCanonical)),\n after_hash: sha256Hex(stableStringify(afterHashCanonical)),\n message: params.message === undefined ? undefined : params.message,\n };\n}\n\nexport async function appendHistoryEntry(historyPath: string, entry: HistoryEntry): Promise<void> {\n const override = await runActiveServiceOverride(\"history_append\", {\n history_path: historyPath,\n entry,\n });\n if (override.handled) {\n if (override.result === false) {\n return;\n }\n if (typeof override.result === \"string\") {\n await appendLineAtomic(historyPath, override.result);\n return;\n }\n if (typeof override.result === \"object\" && override.result !== null) {\n const record = override.result as {\n history_path?: unknown;\n entry?: unknown;\n line?: unknown;\n skip?: unknown;\n };\n if (record.skip === true) {\n return;\n }\n const nextHistoryPath = typeof record.history_path === \"string\" ? record.history_path : historyPath;\n if (typeof record.line === \"string\") {\n await appendLineAtomic(nextHistoryPath, record.line);\n return;\n }\n const nextEntry = (record.entry ?? entry) as HistoryEntry;\n await appendLineAtomic(nextHistoryPath, JSON.stringify(nextEntry));\n return;\n }\n }\n await appendLineAtomic(historyPath, JSON.stringify(entry));\n}\n"]}
@@ -1,11 +1,11 @@
1
- import type { ItemDocument, ItemFrontMatter, RuntimeSchemaSettings } from "../../types/index.js";
1
+ import type { ItemDocument, ItemMetadata, RuntimeSchemaSettings } from "../../types/index.js";
2
2
  import type { ItemFormat } from "../../types/index.js";
3
3
  export interface ItemDocumentFormatOptions {
4
4
  format?: ItemFormat;
5
5
  schema?: RuntimeSchemaSettings;
6
6
  onWarning?: (warning: string) => void;
7
7
  }
8
- export declare function normalizeFrontMatter(frontMatter: ItemFrontMatter, options?: Pick<ItemDocumentFormatOptions, "schema" | "onWarning">): ItemFrontMatter;
8
+ export declare function normalizeFrontMatter(frontMatter: ItemMetadata, options?: Pick<ItemDocumentFormatOptions, "schema" | "onWarning">): ItemMetadata;
9
9
  export declare function splitFrontMatter(content: string): {
10
10
  frontMatter: string;
11
11
  body: string;
@@ -219,25 +219,25 @@ function assertValidFrontMatter(frontMatter, runtimeContext) {
219
219
  }
220
220
  if (runtimeContext?.fieldRegistry) {
221
221
  for (const definition of runtimeContext.fieldRegistry.definitions) {
222
- const fieldValue = record[definition.front_matter_key];
222
+ const fieldValue = record[definition.metadata_key];
223
223
  if (fieldValue === undefined) {
224
224
  if (runtimeFieldRequiredForType(definition, itemType)) {
225
- validationError(`missing required schema field: ${definition.front_matter_key}`);
225
+ validationError(`missing required schema field: ${definition.metadata_key}`);
226
226
  }
227
227
  continue;
228
228
  }
229
229
  try {
230
- record[definition.front_matter_key] = coerceRuntimeFieldValue(definition, fieldValue, `metadata field "${definition.front_matter_key}"`);
230
+ record[definition.metadata_key] = coerceRuntimeFieldValue(definition, fieldValue, `metadata field "${definition.metadata_key}"`);
231
231
  }
232
232
  catch (error) {
233
- validationError(error instanceof Error ? error.message.replace(/^Invalid\s+/u, "") : `invalid ${definition.front_matter_key} value`);
233
+ validationError(error instanceof Error ? error.message.replace(/^Invalid\s+/u, "") : `invalid ${definition.metadata_key} value`);
234
234
  }
235
235
  }
236
236
  }
237
237
  if (runtimeContext && runtimeContext.unknownFieldPolicy !== "allow") {
238
238
  const knownKeys = new Set(STATIC_FRONT_MATTER_FIELD_SET);
239
239
  for (const definition of runtimeContext.fieldRegistry?.definitions ?? []) {
240
- knownKeys.add(definition.front_matter_key);
240
+ knownKeys.add(definition.metadata_key);
241
241
  }
242
242
  const unknownKeys = Object.keys(record).filter((key) => !knownKeys.has(key)).sort((left, right) => left.localeCompare(right));
243
243
  if (unknownKeys.length > 0) {
@@ -693,17 +693,17 @@ export function normalizeFrontMatter(frontMatter, options = {}) {
693
693
  }
694
694
  if (runtimeContext?.fieldRegistry) {
695
695
  for (const definition of runtimeContext.fieldRegistry.definitions) {
696
- const currentValue = normalized[definition.front_matter_key];
696
+ const currentValue = normalized[definition.metadata_key];
697
697
  if (currentValue === undefined) {
698
698
  continue;
699
699
  }
700
- normalized[definition.front_matter_key] = coerceRuntimeFieldValue(definition, currentValue, `metadata field "${definition.front_matter_key}"`);
700
+ normalized[definition.metadata_key] = coerceRuntimeFieldValue(definition, currentValue, `metadata field "${definition.metadata_key}"`);
701
701
  }
702
702
  }
703
703
  if (runtimeContext && runtimeContext.unknownFieldPolicy !== "allow") {
704
704
  const knownKeys = new Set(STATIC_FRONT_MATTER_FIELD_SET);
705
705
  for (const definition of runtimeContext.fieldRegistry?.definitions ?? []) {
706
- knownKeys.add(definition.front_matter_key);
706
+ knownKeys.add(definition.metadata_key);
707
707
  }
708
708
  const unknownKeys = Object.keys(normalized)
709
709
  .filter((key) => !knownKeys.has(key))
@@ -814,7 +814,7 @@ function parseJsonMarkdownItemDocument(content, runtimeContext, options = {}) {
814
814
  }
815
815
  assertValidFrontMatter(parsed, runtimeContext);
816
816
  return {
817
- front_matter: normalizeFrontMatter(parsed, options),
817
+ metadata: normalizeFrontMatter(parsed, options),
818
818
  body: normalizeBody(body),
819
819
  };
820
820
  }
@@ -832,7 +832,7 @@ function parseToonItemDocument(content, runtimeContext, options = {}) {
832
832
  assertValidFrontMatter(record.front_matter, runtimeContext);
833
833
  assertFrontMatterCondition(record.body === undefined || typeof record.body === "string", "TOON item document body must be a string");
834
834
  return {
835
- front_matter: normalizeFrontMatter(record.front_matter, options),
835
+ metadata: normalizeFrontMatter(record.front_matter, options),
836
836
  body: normalizeBody(typeof record.body === "string" ? record.body : ""),
837
837
  };
838
838
  }
@@ -840,12 +840,12 @@ function parseToonItemDocument(content, runtimeContext, options = {}) {
840
840
  assertFrontMatterCondition(body === undefined || typeof body === "string", "TOON item document body must be a string");
841
841
  assertValidFrontMatter(frontMatterRecord, runtimeContext);
842
842
  return {
843
- front_matter: normalizeFrontMatter(frontMatterRecord, options),
843
+ metadata: normalizeFrontMatter(frontMatterRecord, options),
844
844
  body: normalizeBody(typeof body === "string" ? body : ""),
845
845
  };
846
846
  }
847
847
  function serializeJsonMarkdownItemDocument(document, options = {}) {
848
- const normalizedFrontMatter = normalizeFrontMatter(document.front_matter, options);
848
+ const normalizedFrontMatter = normalizeFrontMatter(document.metadata, options);
849
849
  const orderedFrontMatter = orderFrontMatter(normalizedFrontMatter);
850
850
  const serializedFrontMatter = JSON.stringify(orderedFrontMatter, null, 2);
851
851
  const normalizedBody = normalizeBody(document.body ?? "");
@@ -855,7 +855,7 @@ function serializeJsonMarkdownItemDocument(document, options = {}) {
855
855
  return `${serializedFrontMatter}\n\n${normalizedBody}\n`;
856
856
  }
857
857
  function serializeToonItemDocument(document, options = {}) {
858
- const normalizedFrontMatter = normalizeFrontMatter(document.front_matter, options);
858
+ const normalizedFrontMatter = normalizeFrontMatter(document.metadata, options);
859
859
  const orderedFrontMatter = orderFrontMatter(normalizedFrontMatter);
860
860
  const normalizedBody = normalizeBody(document.body ?? "");
861
861
  return `${encodeToon({ ...orderedFrontMatter, body: normalizedBody })}\n`;
@@ -871,19 +871,19 @@ export function parseItemDocument(content, options = {}) {
871
871
  nextSteps: ["Resolve conflicts, save the file, then rerun the pm command."],
872
872
  });
873
873
  }
874
- const format = options.format ?? "json_markdown";
874
+ const format = options.format ?? "toon";
875
875
  const runtimeContext = resolveRuntimeSchemaValidationContext(options);
876
876
  return format === "toon"
877
877
  ? parseToonItemDocument(content, runtimeContext, options)
878
878
  : parseJsonMarkdownItemDocument(content, runtimeContext, options);
879
879
  }
880
880
  export function serializeItemDocument(document, options = {}) {
881
- const format = options.format ?? "json_markdown";
881
+ const format = options.format ?? "toon";
882
882
  return format === "toon" ? serializeToonItemDocument(document, options) : serializeJsonMarkdownItemDocument(document, options);
883
883
  }
884
884
  export function canonicalDocument(document, options = {}) {
885
885
  return {
886
- front_matter: normalizeFrontMatter(document.front_matter, options),
886
+ metadata: normalizeFrontMatter(document.metadata, options),
887
887
  body: normalizeBody(document.body ?? ""),
888
888
  };
889
889
  }