@unbrained/pm-cli 2026.5.18 → 2026.5.27
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +953 -472
- package/README.md +4 -11
- package/dist/cli/bootstrap-args.d.ts +18 -1
- package/dist/cli/bootstrap-args.js +143 -3
- package/dist/cli/bootstrap-args.js.map +1 -1
- package/dist/cli/commander-usage.js +147 -10
- package/dist/cli/commander-usage.js.map +1 -1
- package/dist/cli/commands/annotation-command.d.ts +49 -0
- package/dist/cli/commands/annotation-command.js +135 -0
- package/dist/cli/commands/annotation-command.js.map +1 -0
- package/dist/cli/commands/append.js +5 -8
- package/dist/cli/commands/append.js.map +1 -1
- package/dist/cli/commands/calendar.js +3 -6
- package/dist/cli/commands/calendar.js.map +1 -1
- package/dist/cli/commands/claim.js +15 -24
- package/dist/cli/commands/claim.js.map +1 -1
- package/dist/cli/commands/close.js +63 -10
- package/dist/cli/commands/close.js.map +1 -1
- package/dist/cli/commands/comments.d.ts +5 -0
- package/dist/cli/commands/comments.js +27 -117
- package/dist/cli/commands/comments.js.map +1 -1
- package/dist/cli/commands/completion.d.ts +2 -2
- package/dist/cli/commands/completion.js +203 -63
- package/dist/cli/commands/completion.js.map +1 -1
- package/dist/cli/commands/config.d.ts +1 -1
- package/dist/cli/commands/config.js +82 -4
- package/dist/cli/commands/config.js.map +1 -1
- package/dist/cli/commands/context.js +4 -10
- package/dist/cli/commands/context.js.map +1 -1
- package/dist/cli/commands/contracts.js +168 -36
- package/dist/cli/commands/contracts.js.map +1 -1
- package/dist/cli/commands/create.js +53 -313
- package/dist/cli/commands/create.js.map +1 -1
- package/dist/cli/commands/dedupe-audit.js +7 -4
- package/dist/cli/commands/dedupe-audit.js.map +1 -1
- package/dist/cli/commands/delete.d.ts +3 -0
- package/dist/cli/commands/delete.js +11 -9
- package/dist/cli/commands/delete.js.map +1 -1
- package/dist/cli/commands/docs.d.ts +2 -12
- package/dist/cli/commands/docs.js +8 -316
- package/dist/cli/commands/docs.js.map +1 -1
- package/dist/cli/commands/event-validation-messages.d.ts +3 -0
- package/dist/cli/commands/event-validation-messages.js +44 -0
- package/dist/cli/commands/event-validation-messages.js.map +1 -0
- package/dist/cli/commands/extension/bundled-catalog.d.ts +14 -0
- package/dist/cli/commands/extension/bundled-catalog.js +268 -0
- package/dist/cli/commands/extension/bundled-catalog.js.map +1 -0
- package/dist/cli/commands/extension/doctor.d.ts +31 -0
- package/dist/cli/commands/extension/doctor.js +345 -0
- package/dist/cli/commands/extension/doctor.js.map +1 -0
- package/dist/cli/commands/extension/install-sources.d.ts +37 -0
- package/dist/cli/commands/extension/install-sources.js +384 -0
- package/dist/cli/commands/extension/install-sources.js.map +1 -0
- package/dist/cli/commands/extension/managed-state.d.ts +48 -0
- package/dist/cli/commands/extension/managed-state.js +172 -0
- package/dist/cli/commands/extension/managed-state.js.map +1 -0
- package/dist/cli/commands/extension/scaffold.d.ts +14 -0
- package/dist/cli/commands/extension/scaffold.js +169 -0
- package/dist/cli/commands/extension/scaffold.js.map +1 -0
- package/dist/cli/commands/extension/shared.d.ts +14 -0
- package/dist/cli/commands/extension/shared.js +106 -0
- package/dist/cli/commands/extension/shared.js.map +1 -0
- package/dist/cli/commands/extension.d.ts +37 -68
- package/dist/cli/commands/extension.js +157 -1319
- package/dist/cli/commands/extension.js.map +1 -1
- package/dist/cli/commands/files.d.ts +1 -12
- package/dist/cli/commands/files.js +14 -318
- package/dist/cli/commands/files.js.map +1 -1
- package/dist/cli/commands/gc.js +17 -4
- package/dist/cli/commands/gc.js.map +1 -1
- package/dist/cli/commands/get.d.ts +3 -2
- package/dist/cli/commands/get.js +52 -9
- package/dist/cli/commands/get.js.map +1 -1
- package/dist/cli/commands/health.d.ts +10 -0
- package/dist/cli/commands/health.js +269 -76
- package/dist/cli/commands/health.js.map +1 -1
- package/dist/cli/commands/history-redact.d.ts +8 -0
- package/dist/cli/commands/history-redact.js +35 -113
- package/dist/cli/commands/history-redact.js.map +1 -1
- package/dist/cli/commands/history-repair.d.ts +33 -0
- package/dist/cli/commands/history-repair.js +172 -0
- package/dist/cli/commands/history-repair.js.map +1 -0
- package/dist/cli/commands/history.d.ts +4 -4
- package/dist/cli/commands/history.js +10 -88
- package/dist/cli/commands/history.js.map +1 -1
- package/dist/cli/commands/index.d.ts +3 -1
- package/dist/cli/commands/index.js +5 -3
- package/dist/cli/commands/index.js.map +1 -1
- package/dist/cli/commands/init.d.ts +28 -0
- package/dist/cli/commands/init.js +23 -2
- package/dist/cli/commands/init.js.map +1 -1
- package/dist/cli/commands/learnings.js +20 -119
- package/dist/cli/commands/learnings.js.map +1 -1
- package/dist/cli/commands/legacy-none-tokens.d.ts +3 -0
- package/dist/cli/commands/legacy-none-tokens.js +39 -0
- package/dist/cli/commands/legacy-none-tokens.js.map +1 -0
- package/dist/cli/commands/linked-artifacts.d.ts +96 -0
- package/dist/cli/commands/linked-artifacts.js +335 -0
- package/dist/cli/commands/linked-artifacts.js.map +1 -0
- package/dist/cli/commands/linked-test-entry.d.ts +3 -0
- package/dist/cli/commands/linked-test-entry.js +62 -0
- package/dist/cli/commands/linked-test-entry.js.map +1 -0
- package/dist/cli/commands/linked-test-parsers.d.ts +28 -0
- package/dist/cli/commands/linked-test-parsers.js +192 -0
- package/dist/cli/commands/linked-test-parsers.js.map +1 -0
- package/dist/cli/commands/list.js +49 -24
- package/dist/cli/commands/list.js.map +1 -1
- package/dist/cli/commands/normalize.js +4 -3
- package/dist/cli/commands/normalize.js.map +1 -1
- package/dist/cli/commands/notes.js +20 -119
- package/dist/cli/commands/notes.js.map +1 -1
- package/dist/cli/commands/plan.d.ts +3 -0
- package/dist/cli/commands/plan.js +184 -22
- package/dist/cli/commands/plan.js.map +1 -1
- package/dist/cli/commands/recurrence-parsers.d.ts +26 -0
- package/dist/cli/commands/recurrence-parsers.js +98 -0
- package/dist/cli/commands/recurrence-parsers.js.map +1 -0
- package/dist/cli/commands/restore.js +24 -56
- package/dist/cli/commands/restore.js.map +1 -1
- package/dist/cli/commands/schema.d.ts +31 -0
- package/dist/cli/commands/schema.js +98 -0
- package/dist/cli/commands/schema.js.map +1 -0
- package/dist/cli/commands/search.js +154 -42
- package/dist/cli/commands/search.js.map +1 -1
- package/dist/cli/commands/templates.d.ts +4 -0
- package/dist/cli/commands/templates.js +89 -17
- package/dist/cli/commands/templates.js.map +1 -1
- package/dist/cli/commands/test/linked-command-detection.d.ts +37 -0
- package/dist/cli/commands/test/linked-command-detection.js +200 -0
- package/dist/cli/commands/test/linked-command-detection.js.map +1 -0
- package/dist/cli/commands/test-all.js +4 -8
- package/dist/cli/commands/test-all.js.map +1 -1
- package/dist/cli/commands/test.d.ts +2 -2
- package/dist/cli/commands/test.js +12 -357
- package/dist/cli/commands/test.js.map +1 -1
- package/dist/cli/commands/update-many.js +6 -9
- package/dist/cli/commands/update-many.js.map +1 -1
- package/dist/cli/commands/update.js +167 -401
- package/dist/cli/commands/update.js.map +1 -1
- package/dist/cli/commands/validate.d.ts +3 -1
- package/dist/cli/commands/validate.js +23 -71
- package/dist/cli/commands/validate.js.map +1 -1
- package/dist/cli/error-guidance.d.ts +1 -0
- package/dist/cli/error-guidance.js +100 -6
- package/dist/cli/error-guidance.js.map +1 -1
- package/dist/cli/extension-command-help.d.ts +0 -1
- package/dist/cli/extension-command-help.js +2 -13
- package/dist/cli/extension-command-help.js.map +1 -1
- package/dist/cli/extension-command-options.d.ts +1 -0
- package/dist/cli/extension-command-options.js +106 -7
- package/dist/cli/extension-command-options.js.map +1 -1
- package/dist/cli/help-content.d.ts +0 -1
- package/dist/cli/help-content.js +13 -9
- package/dist/cli/help-content.js.map +1 -1
- package/dist/cli/help-json-payload.d.ts +1 -0
- package/dist/cli/help-json-payload.js +33 -3
- package/dist/cli/help-json-payload.js.map +1 -1
- package/dist/cli/main.d.ts +11 -0
- package/dist/cli/main.js +109 -55
- package/dist/cli/main.js.map +1 -1
- package/dist/cli/register-list-query.d.ts +5 -2
- package/dist/cli/register-list-query.js +254 -192
- package/dist/cli/register-list-query.js.map +1 -1
- package/dist/cli/register-mutation.d.ts +1 -1
- package/dist/cli/register-mutation.js +247 -64
- package/dist/cli/register-mutation.js.map +1 -1
- package/dist/cli/register-operations.js +17 -12
- package/dist/cli/register-operations.js.map +1 -1
- package/dist/cli/register-setup.js +33 -16
- package/dist/cli/register-setup.js.map +1 -1
- package/dist/cli/registration-helpers.d.ts +0 -2
- package/dist/cli/registration-helpers.js +14 -40
- package/dist/cli/registration-helpers.js.map +1 -1
- package/dist/cli.js +25 -4
- package/dist/cli.js.map +1 -1
- package/dist/core/config/positional-value.d.ts +44 -0
- package/dist/core/config/positional-value.js +109 -0
- package/dist/core/config/positional-value.js.map +1 -0
- package/dist/core/extensions/extension-capability-aliases.d.ts +14 -0
- package/dist/core/extensions/extension-capability-aliases.js +159 -0
- package/dist/core/extensions/extension-capability-aliases.js.map +1 -0
- package/dist/core/extensions/extension-hook-runtime.d.ts +13 -0
- package/dist/core/extensions/extension-hook-runtime.js +414 -0
- package/dist/core/extensions/extension-hook-runtime.js.map +1 -0
- package/dist/core/extensions/extension-policy.d.ts +69 -0
- package/dist/core/extensions/extension-policy.js +481 -0
- package/dist/core/extensions/extension-policy.js.map +1 -0
- package/dist/core/extensions/extension-registries.d.ts +8 -0
- package/dist/core/extensions/extension-registries.js +52 -0
- package/dist/core/extensions/extension-registries.js.map +1 -0
- package/dist/core/extensions/extension-runtime-helpers.d.ts +6 -0
- package/dist/core/extensions/extension-runtime-helpers.js +29 -0
- package/dist/core/extensions/extension-runtime-helpers.js.map +1 -0
- package/dist/core/extensions/extension-types.d.ts +13 -39
- package/dist/core/extensions/extension-types.js +34 -2
- package/dist/core/extensions/extension-types.js.map +1 -1
- package/dist/core/extensions/index.d.ts +7 -1
- package/dist/core/extensions/index.js +11 -14
- package/dist/core/extensions/index.js.map +1 -1
- package/dist/core/extensions/loader.d.ts +4 -22
- package/dist/core/extensions/loader.js +23 -1146
- package/dist/core/extensions/loader.js.map +1 -1
- package/dist/core/fs/path-utils.d.ts +1 -0
- package/dist/core/fs/path-utils.js +12 -0
- package/dist/core/fs/path-utils.js.map +1 -0
- package/dist/core/history/drift-scan.d.ts +22 -0
- package/dist/core/history/drift-scan.js +149 -0
- package/dist/core/history/drift-scan.js.map +1 -0
- package/dist/core/history/history-rewrite.d.ts +43 -0
- package/dist/core/history/history-rewrite.js +48 -0
- package/dist/core/history/history-rewrite.js.map +1 -0
- package/dist/core/history/history.js +5 -4
- package/dist/core/history/history.js.map +1 -1
- package/dist/core/history/replay.d.ts +82 -0
- package/dist/core/history/replay.js +250 -0
- package/dist/core/history/replay.js.map +1 -0
- package/dist/core/item/item-format.js +11 -8
- package/dist/core/item/item-format.js.map +1 -1
- package/dist/core/item/item-record.d.ts +19 -0
- package/dist/core/item/item-record.js +24 -0
- package/dist/core/item/item-record.js.map +1 -0
- package/dist/core/item/item-type-definition.d.ts +52 -0
- package/dist/core/item/item-type-definition.js +123 -0
- package/dist/core/item/item-type-definition.js.map +1 -0
- package/dist/core/item/parse.js +3 -2
- package/dist/core/item/parse.js.map +1 -1
- package/dist/core/item/priority.d.ts +23 -0
- package/dist/core/item/priority.js +55 -0
- package/dist/core/item/priority.js.map +1 -0
- package/dist/core/item/status.d.ts +14 -1
- package/dist/core/item/status.js +22 -2
- package/dist/core/item/status.js.map +1 -1
- package/dist/core/item/toon-decode.d.ts +19 -0
- package/dist/core/item/toon-decode.js +69 -0
- package/dist/core/item/toon-decode.js.map +1 -0
- package/dist/core/item/type-registry.js +13 -84
- package/dist/core/item/type-registry.js.map +1 -1
- package/dist/core/output/mutation-projection.d.ts +31 -0
- package/dist/core/output/mutation-projection.js +103 -0
- package/dist/core/output/mutation-projection.js.map +1 -0
- package/dist/core/output/output.d.ts +2 -0
- package/dist/core/output/output.js +5 -3
- package/dist/core/output/output.js.map +1 -1
- package/dist/core/packages/manifest.js +3 -9
- package/dist/core/packages/manifest.js.map +1 -1
- package/dist/core/schema/item-types-file.d.ts +85 -0
- package/dist/core/schema/item-types-file.js +243 -0
- package/dist/core/schema/item-types-file.js.map +1 -0
- package/dist/core/schema/runtime-schema.d.ts +2 -1
- package/dist/core/schema/runtime-schema.js +17 -45
- package/dist/core/schema/runtime-schema.js.map +1 -1
- package/dist/core/search/semantic-defaults.js +3 -3
- package/dist/core/search/semantic-defaults.js.map +1 -1
- package/dist/core/search/vector-stores.js +46 -9
- package/dist/core/search/vector-stores.js.map +1 -1
- package/dist/core/sentry/helpers.d.ts +1 -1
- package/dist/core/sentry/helpers.js +20 -3
- package/dist/core/sentry/helpers.js.map +1 -1
- package/dist/core/shared/author.d.ts +1 -0
- package/dist/core/shared/author.js +9 -0
- package/dist/core/shared/author.js.map +1 -0
- package/dist/core/shared/command-types.d.ts +1 -0
- package/dist/core/shared/command-types.js +2 -2
- package/dist/core/shared/command-types.js.map +1 -1
- package/dist/core/shared/constants.d.ts +10 -1
- package/dist/core/shared/constants.js +56 -58
- package/dist/core/shared/constants.js.map +1 -1
- package/dist/core/shared/lazy-module.d.ts +1 -0
- package/dist/core/shared/lazy-module.js +11 -0
- package/dist/core/shared/lazy-module.js.map +1 -0
- package/dist/core/shared/option-alias-visibility.d.ts +44 -0
- package/dist/core/shared/option-alias-visibility.js +76 -0
- package/dist/core/shared/option-alias-visibility.js.map +1 -0
- package/dist/core/shared/primitives.d.ts +23 -0
- package/dist/core/shared/primitives.js +39 -2
- package/dist/core/shared/primitives.js.map +1 -1
- package/dist/core/shared/text-normalization.d.ts +0 -1
- package/dist/core/shared/text-normalization.js +2 -5
- package/dist/core/shared/text-normalization.js.map +1 -1
- package/dist/core/store/front-matter-cache.d.ts +16 -2
- package/dist/core/store/front-matter-cache.js +99 -33
- package/dist/core/store/front-matter-cache.js.map +1 -1
- package/dist/core/store/item-store.d.ts +2 -0
- package/dist/core/store/item-store.js +76 -110
- package/dist/core/store/item-store.js.map +1 -1
- package/dist/core/store/settings-validator.d.ts +106 -0
- package/dist/core/store/settings-validator.js +279 -0
- package/dist/core/store/settings-validator.js.map +1 -0
- package/dist/core/store/settings.js +6 -343
- package/dist/core/store/settings.js.map +1 -1
- package/dist/core/telemetry/runtime.js +5 -3
- package/dist/core/telemetry/runtime.js.map +1 -1
- package/dist/mcp/server.js +138 -39
- package/dist/mcp/server.js.map +1 -1
- package/dist/sdk/cli-contracts/enum-contracts.d.ts +20 -0
- package/dist/sdk/cli-contracts/enum-contracts.js +156 -0
- package/dist/sdk/cli-contracts/enum-contracts.js.map +1 -0
- package/dist/sdk/cli-contracts/tool-option-contracts.d.ts +14 -0
- package/dist/sdk/cli-contracts/tool-option-contracts.js +243 -0
- package/dist/sdk/cli-contracts/tool-option-contracts.js.map +1 -0
- package/dist/sdk/cli-contracts/tool-parameter-tables.d.ts +11 -0
- package/dist/sdk/cli-contracts/tool-parameter-tables.js +901 -0
- package/dist/sdk/cli-contracts/tool-parameter-tables.js.map +1 -0
- package/dist/sdk/cli-contracts.d.ts +18 -33
- package/dist/sdk/cli-contracts.js +96 -1238
- package/dist/sdk/cli-contracts.js.map +1 -1
- package/dist/sdk/package-import-adapters.d.ts +74 -0
- package/dist/sdk/package-import-adapters.js +186 -0
- package/dist/sdk/package-import-adapters.js.map +1 -0
- package/dist/sdk/package-runtime-options.d.ts +26 -0
- package/dist/sdk/package-runtime-options.js +71 -0
- package/dist/sdk/package-runtime-options.js.map +1 -0
- package/dist/sdk/runtime.d.ts +27 -1
- package/dist/sdk/runtime.js +48 -3
- package/dist/sdk/runtime.js.map +1 -1
- package/dist/types.d.ts +6 -0
- package/dist/types.js +10 -2
- package/dist/types.js.map +1 -1
- package/docs/AGENT_GUIDE.md +13 -11
- package/docs/ARCHITECTURE.md +1 -1
- package/docs/CLAUDE_CODE_PLUGIN.md +5 -28
- package/docs/CODEX_PLUGIN.md +5 -5
- package/docs/COMMANDS.md +58 -9
- package/docs/CONFIGURATION.md +16 -1
- package/docs/EXTENSIONS.md +4 -63
- package/docs/RELEASING.md +12 -8
- package/docs/SDK.md +11 -2
- package/marketplace.json +7 -3
- package/package.json +18 -14
- package/packages/pm-beads/extensions/beads/index.js +2 -49
- package/packages/pm-beads/extensions/beads/index.ts +2 -54
- package/packages/pm-beads/extensions/beads/runtime-loader.js +86 -0
- package/packages/pm-beads/extensions/beads/runtime-loader.ts +88 -0
- package/packages/pm-beads/extensions/beads/runtime.js +26 -115
- package/packages/pm-beads/extensions/beads/runtime.ts +33 -132
- package/packages/pm-calendar/README.md +3 -1
- package/packages/pm-calendar/extensions/calendar/index.js +66 -2
- package/packages/pm-calendar/extensions/calendar/index.ts +71 -2
- package/packages/pm-calendar/extensions/calendar/runtime.js +1 -0
- package/packages/pm-calendar/extensions/calendar/runtime.ts +1 -0
- package/packages/pm-governance-audit/extensions/governance-audit/runtime.js +14 -41
- package/packages/pm-governance-audit/extensions/governance-audit/runtime.ts +25 -41
- package/packages/pm-guide-shell/extensions/guide-shell/runtime.js +10 -50
- package/packages/pm-guide-shell/extensions/guide-shell/runtime.ts +17 -50
- package/packages/pm-linked-test-adapters/extensions/linked-test-adapters/runtime.js +8 -40
- package/packages/pm-linked-test-adapters/extensions/linked-test-adapters/runtime.ts +10 -40
- package/packages/pm-search-advanced/README.md +8 -0
- package/packages/pm-search-advanced/extensions/search-advanced/index.js +75 -1
- package/packages/pm-search-advanced/extensions/search-advanced/index.ts +74 -0
- package/packages/pm-search-advanced/extensions/search-advanced/runtime.js +58 -33
- package/packages/pm-search-advanced/extensions/search-advanced/runtime.ts +60 -33
- package/packages/pm-templates/extensions/templates/runtime.js +11 -202
- package/packages/pm-templates/extensions/templates/runtime.ts +38 -230
- package/packages/pm-todos/extensions/todos/index.js +3 -50
- package/packages/pm-todos/extensions/todos/index.ts +3 -55
- package/packages/pm-todos/extensions/todos/runtime-loader.js +86 -0
- package/packages/pm-todos/extensions/todos/runtime-loader.ts +88 -0
- package/packages/pm-todos/extensions/todos/runtime.js +24 -117
- package/packages/pm-todos/extensions/todos/runtime.ts +32 -129
- package/plugins/pm-claude/README.md +2 -2
- package/plugins/pm-claude/commands/pm-planner.md +1 -15
- package/plugins/pm-claude/scripts/pm-mcp-server.mjs +5 -2
- package/plugins/pm-claude/skills/pm-planner/SKILL.md +3 -21
- package/plugins/pm-codex/scripts/pm-mcp-server.mjs +15 -6
- package/plugins/pm-codex/skills/pm-native/SKILL.md +1 -13
- package/PRD.md +0 -1734
- package/dist/core/output/command-aware.d.ts +0 -1
- package/dist/core/output/command-aware.js +0 -397
- package/dist/core/output/command-aware.js.map +0 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function isPathWithinDirectory(directory: string, targetPath: string): boolean;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
|
|
2
|
+
!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]="e8f4f66b-8f68-5a07-8905-ff2602564716")}catch(e){}}();
|
|
3
|
+
import path from "path";
|
|
4
|
+
export function isPathWithinDirectory(directory, targetPath) {
|
|
5
|
+
const relative = path.relative(directory, targetPath);
|
|
6
|
+
if (relative.length === 0) {
|
|
7
|
+
return true;
|
|
8
|
+
}
|
|
9
|
+
return !relative.startsWith("..") && !path.isAbsolute(relative);
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=path-utils.js.map
|
|
12
|
+
//# debugId=e8f4f66b-8f68-5a07-8905-ff2602564716
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"path-utils.js","sources":["core/fs/path-utils.ts"],"sourceRoot":"/","sourcesContent":["import path from \"path\";\n\nexport function isPathWithinDirectory(directory: string, targetPath: string): boolean {\n const relative = path.relative(directory, targetPath);\n if (relative.length === 0) {\n return true;\n }\n return !relative.startsWith(\"..\") && !path.isAbsolute(relative);\n}\n"],"names":[],"mappings":";;AAAA,OAAO,IAAI,MAAM,MAAM,CAAC;AAExB,MAAM,UAAU,qBAAqB,CAAC,SAAiB,EAAE,UAAkB;IACzE,MAAM,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAC,SAAS,EAAE,UAAU,CAAC,CAAC;IACtD,IAAI,QAAQ,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAC1B,OAAO,IAAI,CAAC;IACd,CAAC;IACD,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAC;AAClE,CAAC","debugId":"e8f4f66b-8f68-5a07-8905-ff2602564716"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { ItemMetadata } from "../../types/index.js";
|
|
2
|
+
export interface DriftScanResult {
|
|
3
|
+
missingStreams: string[];
|
|
4
|
+
unreadableStreams: string[];
|
|
5
|
+
hashMismatches: string[];
|
|
6
|
+
chainMismatches: string[];
|
|
7
|
+
driftedItems: string[];
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Scan every item's history stream for drift (missing/unreadable streams, broken
|
|
11
|
+
* hash chains, and item/history hash mismatches).
|
|
12
|
+
*
|
|
13
|
+
* Full chain re-verification of a 17MB+ history tree is the dominant cost of
|
|
14
|
+
* `pm health`. We cache the per-stream verification keyed by the history file's
|
|
15
|
+
* mtime/size: an unchanged file has an identical chain and latest stored hash, so
|
|
16
|
+
* we skip re-reading and re-hashing it. Item-side changes are still caught every
|
|
17
|
+
* run because the current document hash is recomputed and compared to the cached
|
|
18
|
+
* `latest_after_hash`.
|
|
19
|
+
*/
|
|
20
|
+
export declare function scanHistoryDrift(pmRoot: string, items: Array<ItemMetadata & {
|
|
21
|
+
body: string;
|
|
22
|
+
}>): Promise<DriftScanResult>;
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
|
|
2
|
+
!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]="e8559b46-0199-506f-bf98-8f92a4474215")}catch(e){}}();
|
|
3
|
+
import fs from "fs/promises";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { getHistoryPath } from "../store/paths.js";
|
|
6
|
+
import { writeFileAtomic } from "../fs/fs-utils.js";
|
|
7
|
+
import { hashDocument } from "./history.js";
|
|
8
|
+
import { verifyHistoryChain } from "./replay.js";
|
|
9
|
+
const DRIFT_CACHE_VERSION = 1;
|
|
10
|
+
const DRIFT_CACHE_FILENAME = "history-drift-cache.json";
|
|
11
|
+
function getDriftCachePath(pmRoot) {
|
|
12
|
+
return path.join(pmRoot, "runtime", DRIFT_CACHE_FILENAME);
|
|
13
|
+
}
|
|
14
|
+
async function loadDriftCache(pmRoot) {
|
|
15
|
+
try {
|
|
16
|
+
const raw = await fs.readFile(getDriftCachePath(pmRoot), "utf8");
|
|
17
|
+
const parsed = JSON.parse(raw);
|
|
18
|
+
if (parsed.version !== DRIFT_CACHE_VERSION || typeof parsed.entries !== "object" || parsed.entries === null) {
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
return parsed;
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
function isErrno(error, code) {
|
|
28
|
+
return typeof error === "object" && error !== null && "code" in error && error.code === code;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Read and fully verify one history stream's hash chain. Returns null for an
|
|
32
|
+
* empty/missing stream (caller records it as a missing stream).
|
|
33
|
+
*/
|
|
34
|
+
async function verifyHistoryStream(historyPath) {
|
|
35
|
+
const raw = await fs.readFile(historyPath, "utf8");
|
|
36
|
+
if (raw.trim().length === 0) {
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
const entries = [];
|
|
40
|
+
let latestAfterHash = null;
|
|
41
|
+
for (const line of raw.split(/\r?\n/)) {
|
|
42
|
+
const trimmed = line.trim();
|
|
43
|
+
if (trimmed.length === 0) {
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
const parsed = JSON.parse(trimmed);
|
|
47
|
+
if (typeof parsed.after_hash !== "string" || parsed.after_hash.trim().length === 0) {
|
|
48
|
+
throw new Error("missing after_hash");
|
|
49
|
+
}
|
|
50
|
+
entries.push(parsed);
|
|
51
|
+
latestAfterHash = parsed.after_hash;
|
|
52
|
+
}
|
|
53
|
+
/* c8 ignore start -- defensive guard for future history schema changes. */
|
|
54
|
+
if (!latestAfterHash) {
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
/* c8 ignore stop */
|
|
58
|
+
return { latestAfterHash, chainOk: verifyHistoryChain(entries).ok };
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Scan every item's history stream for drift (missing/unreadable streams, broken
|
|
62
|
+
* hash chains, and item/history hash mismatches).
|
|
63
|
+
*
|
|
64
|
+
* Full chain re-verification of a 17MB+ history tree is the dominant cost of
|
|
65
|
+
* `pm health`. We cache the per-stream verification keyed by the history file's
|
|
66
|
+
* mtime/size: an unchanged file has an identical chain and latest stored hash, so
|
|
67
|
+
* we skip re-reading and re-hashing it. Item-side changes are still caught every
|
|
68
|
+
* run because the current document hash is recomputed and compared to the cached
|
|
69
|
+
* `latest_after_hash`.
|
|
70
|
+
*/
|
|
71
|
+
export async function scanHistoryDrift(pmRoot, items) {
|
|
72
|
+
const missingStreams = [];
|
|
73
|
+
const unreadableStreams = [];
|
|
74
|
+
const hashMismatches = [];
|
|
75
|
+
const chainMismatches = [];
|
|
76
|
+
const cache = await loadDriftCache(pmRoot);
|
|
77
|
+
const previousEntries = cache?.entries ?? {};
|
|
78
|
+
const nextEntries = {};
|
|
79
|
+
let cacheDirty = false;
|
|
80
|
+
for (const item of items) {
|
|
81
|
+
const historyPath = getHistoryPath(pmRoot, item.id);
|
|
82
|
+
let stat;
|
|
83
|
+
try {
|
|
84
|
+
stat = await fs.stat(historyPath);
|
|
85
|
+
}
|
|
86
|
+
catch (error) {
|
|
87
|
+
if (isErrno(error, "ENOENT")) {
|
|
88
|
+
missingStreams.push(item.id);
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
unreadableStreams.push(item.id);
|
|
92
|
+
}
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
const cached = previousEntries[item.id];
|
|
96
|
+
let verification;
|
|
97
|
+
if (cached && cached.mtime_ms === stat.mtimeMs && cached.ctime_ms === stat.ctimeMs && cached.size === stat.size) {
|
|
98
|
+
verification = { latestAfterHash: cached.latest_after_hash, chainOk: cached.chain_ok };
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
cacheDirty = true;
|
|
102
|
+
let result;
|
|
103
|
+
try {
|
|
104
|
+
result = await verifyHistoryStream(historyPath);
|
|
105
|
+
}
|
|
106
|
+
catch {
|
|
107
|
+
unreadableStreams.push(item.id);
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
if (!result) {
|
|
111
|
+
missingStreams.push(item.id);
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
114
|
+
verification = result;
|
|
115
|
+
}
|
|
116
|
+
if (!verification.chainOk) {
|
|
117
|
+
chainMismatches.push(item.id);
|
|
118
|
+
}
|
|
119
|
+
nextEntries[item.id] = {
|
|
120
|
+
mtime_ms: stat.mtimeMs,
|
|
121
|
+
ctime_ms: stat.ctimeMs,
|
|
122
|
+
size: stat.size,
|
|
123
|
+
latest_after_hash: verification.latestAfterHash,
|
|
124
|
+
chain_ok: verification.chainOk,
|
|
125
|
+
};
|
|
126
|
+
const { body, ...frontMatter } = item;
|
|
127
|
+
const currentHash = hashDocument({
|
|
128
|
+
metadata: frontMatter,
|
|
129
|
+
body,
|
|
130
|
+
});
|
|
131
|
+
if (currentHash !== verification.latestAfterHash) {
|
|
132
|
+
hashMismatches.push(item.id);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
if (cacheDirty || Object.keys(previousEntries).length !== Object.keys(nextEntries).length) {
|
|
136
|
+
const cachePath = getDriftCachePath(pmRoot);
|
|
137
|
+
try {
|
|
138
|
+
await fs.mkdir(path.dirname(cachePath), { recursive: true });
|
|
139
|
+
await writeFileAtomic(cachePath, JSON.stringify({ version: DRIFT_CACHE_VERSION, entries: nextEntries }));
|
|
140
|
+
}
|
|
141
|
+
catch {
|
|
142
|
+
// Best-effort cache write: a failed persist must never fail a health scan.
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
const driftedItems = [...new Set([...missingStreams, ...unreadableStreams, ...hashMismatches, ...chainMismatches])].sort((a, b) => a.localeCompare(b));
|
|
146
|
+
return { missingStreams, unreadableStreams, hashMismatches, chainMismatches, driftedItems };
|
|
147
|
+
}
|
|
148
|
+
//# sourceMappingURL=drift-scan.js.map
|
|
149
|
+
//# debugId=e8559b46-0199-506f-bf98-8f92a4474215
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"drift-scan.js","sources":["core/history/drift-scan.ts"],"sourceRoot":"/","sourcesContent":["import fs from \"fs/promises\";\nimport path from \"node:path\";\nimport { getHistoryPath } from \"../store/paths.js\";\nimport { writeFileAtomic } from \"../fs/fs-utils.js\";\nimport { hashDocument } from \"./history.js\";\nimport { verifyHistoryChain } from \"./replay.js\";\nimport type { HistoryEntry, ItemMetadata } from \"../../types/index.js\";\n\nexport interface DriftScanResult {\n missingStreams: string[];\n unreadableStreams: string[];\n hashMismatches: string[];\n chainMismatches: string[];\n driftedItems: string[];\n}\n\nconst DRIFT_CACHE_VERSION = 1;\nconst DRIFT_CACHE_FILENAME = \"history-drift-cache.json\";\n\ninterface DriftCacheEntry {\n mtime_ms: number;\n ctime_ms: number;\n size: number;\n latest_after_hash: string;\n chain_ok: boolean;\n}\n\ninterface DriftCacheEnvelope {\n version: number;\n entries: Record<string, DriftCacheEntry>;\n}\n\nfunction getDriftCachePath(pmRoot: string): string {\n return path.join(pmRoot, \"runtime\", DRIFT_CACHE_FILENAME);\n}\n\nasync function loadDriftCache(pmRoot: string): Promise<DriftCacheEnvelope | null> {\n try {\n const raw = await fs.readFile(getDriftCachePath(pmRoot), \"utf8\");\n const parsed = JSON.parse(raw) as DriftCacheEnvelope;\n if (parsed.version !== DRIFT_CACHE_VERSION || typeof parsed.entries !== \"object\" || parsed.entries === null) {\n return null;\n }\n return parsed;\n } catch {\n return null;\n }\n}\n\nfunction isErrno(error: unknown, code: string): boolean {\n return typeof error === \"object\" && error !== null && \"code\" in error && (error as { code?: string }).code === code;\n}\n\ninterface StreamVerification {\n latestAfterHash: string;\n chainOk: boolean;\n}\n\n/**\n * Read and fully verify one history stream's hash chain. Returns null for an\n * empty/missing stream (caller records it as a missing stream).\n */\nasync function verifyHistoryStream(historyPath: string): Promise<StreamVerification | null> {\n const raw = await fs.readFile(historyPath, \"utf8\");\n if (raw.trim().length === 0) {\n return null;\n }\n const entries: HistoryEntry[] = [];\n let latestAfterHash: string | null = null;\n for (const line of raw.split(/\\r?\\n/)) {\n const trimmed = line.trim();\n if (trimmed.length === 0) {\n continue;\n }\n const parsed = JSON.parse(trimmed) as HistoryEntry;\n if (typeof parsed.after_hash !== \"string\" || parsed.after_hash.trim().length === 0) {\n throw new Error(\"missing after_hash\");\n }\n entries.push(parsed);\n latestAfterHash = parsed.after_hash;\n }\n /* c8 ignore start -- defensive guard for future history schema changes. */\n if (!latestAfterHash) {\n return null;\n }\n /* c8 ignore stop */\n return { latestAfterHash, chainOk: verifyHistoryChain(entries).ok };\n}\n\n/**\n * Scan every item's history stream for drift (missing/unreadable streams, broken\n * hash chains, and item/history hash mismatches).\n *\n * Full chain re-verification of a 17MB+ history tree is the dominant cost of\n * `pm health`. We cache the per-stream verification keyed by the history file's\n * mtime/size: an unchanged file has an identical chain and latest stored hash, so\n * we skip re-reading and re-hashing it. Item-side changes are still caught every\n * run because the current document hash is recomputed and compared to the cached\n * `latest_after_hash`.\n */\nexport async function scanHistoryDrift(\n pmRoot: string,\n items: Array<ItemMetadata & { body: string }>,\n): Promise<DriftScanResult> {\n const missingStreams: string[] = [];\n const unreadableStreams: string[] = [];\n const hashMismatches: string[] = [];\n const chainMismatches: string[] = [];\n\n const cache = await loadDriftCache(pmRoot);\n const previousEntries: Record<string, DriftCacheEntry> = cache?.entries ?? {};\n const nextEntries: Record<string, DriftCacheEntry> = {};\n let cacheDirty = false;\n\n for (const item of items) {\n const historyPath = getHistoryPath(pmRoot, item.id);\n\n let stat: Awaited<ReturnType<typeof fs.stat>>;\n try {\n stat = await fs.stat(historyPath);\n } catch (error: unknown) {\n if (isErrno(error, \"ENOENT\")) {\n missingStreams.push(item.id);\n } else {\n unreadableStreams.push(item.id);\n }\n continue;\n }\n\n const cached = previousEntries[item.id];\n let verification: StreamVerification;\n if (cached && cached.mtime_ms === stat.mtimeMs && cached.ctime_ms === stat.ctimeMs && cached.size === stat.size) {\n verification = { latestAfterHash: cached.latest_after_hash, chainOk: cached.chain_ok };\n } else {\n cacheDirty = true;\n let result: StreamVerification | null;\n try {\n result = await verifyHistoryStream(historyPath);\n } catch {\n unreadableStreams.push(item.id);\n continue;\n }\n if (!result) {\n missingStreams.push(item.id);\n continue;\n }\n verification = result;\n }\n\n if (!verification.chainOk) {\n chainMismatches.push(item.id);\n }\n nextEntries[item.id] = {\n mtime_ms: stat.mtimeMs,\n ctime_ms: stat.ctimeMs,\n size: stat.size,\n latest_after_hash: verification.latestAfterHash,\n chain_ok: verification.chainOk,\n };\n\n const { body, ...frontMatter } = item;\n const currentHash = hashDocument({\n metadata: frontMatter as ItemMetadata,\n body,\n });\n if (currentHash !== verification.latestAfterHash) {\n hashMismatches.push(item.id);\n }\n }\n\n if (cacheDirty || Object.keys(previousEntries).length !== Object.keys(nextEntries).length) {\n const cachePath = getDriftCachePath(pmRoot);\n try {\n await fs.mkdir(path.dirname(cachePath), { recursive: true });\n await writeFileAtomic(cachePath, JSON.stringify({ version: DRIFT_CACHE_VERSION, entries: nextEntries }));\n } catch {\n // Best-effort cache write: a failed persist must never fail a health scan.\n }\n }\n\n const driftedItems = [...new Set([...missingStreams, ...unreadableStreams, ...hashMismatches, ...chainMismatches])].sort((a, b) =>\n a.localeCompare(b),\n );\n return { missingStreams, unreadableStreams, hashMismatches, chainMismatches, driftedItems };\n}\n"],"names":[],"mappings":";;AAAA,OAAO,EAAE,MAAM,aAAa,CAAC;AAC7B,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AACnD,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACpD,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAC5C,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAWjD,MAAM,mBAAmB,GAAG,CAAC,CAAC;AAC9B,MAAM,oBAAoB,GAAG,0BAA0B,CAAC;AAexD,SAAS,iBAAiB,CAAC,MAAc;IACvC,OAAO,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE,SAAS,EAAE,oBAAoB,CAAC,CAAC;AAC5D,CAAC;AAED,KAAK,UAAU,cAAc,CAAC,MAAc;IAC1C,IAAI,CAAC;QACH,MAAM,GAAG,GAAG,MAAM,EAAE,CAAC,QAAQ,CAAC,iBAAiB,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC,CAAC;QACjE,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAuB,CAAC;QACrD,IAAI,MAAM,CAAC,OAAO,KAAK,mBAAmB,IAAI,OAAO,MAAM,CAAC,OAAO,KAAK,QAAQ,IAAI,MAAM,CAAC,OAAO,KAAK,IAAI,EAAE,CAAC;YAC5G,OAAO,IAAI,CAAC;QACd,CAAC;QACD,OAAO,MAAM,CAAC;IAChB,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,IAAI,CAAC;IACd,CAAC;AACH,CAAC;AAED,SAAS,OAAO,CAAC,KAAc,EAAE,IAAY;IAC3C,OAAO,OAAO,KAAK,KAAK,QAAQ,IA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AM,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC,MAAM,EAAE,CAAC;QAC1F,MAAM,SAAS,GAAG,iBAAiB,CAAC,MAAM,CAAC,CAAC;QAC5C,IAAI,CAAC;YACH,MAAM,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;YAC7D,MAAM,eAAe,CAAC,SAAS,EAAE,IAAI,CAAC,SAAS,CAAC,EAAE,OAAO,EAAE,mBAAmB,EAAE,OAAO,EAAE,WAAW,EAAE,CAAC,CAAC,CAAC;QAC3G,CAAC;QAAC,MAAM,CAAC;YACP,2EAA2E;QAC7E,CAAC;IACH,CAAC;IAED,MAAM,YAAY,GAAG,CAAC,GAAG,IAAI,GAAG,CAAC,CAAC,GAAG,cAAc,EAAE,GAAG,iBAAiB,EAAE,GAAG,cAAc,EAAE,GAAG,eAAe,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAChI,CAAC,CAAC,aAAa,CAAC,CAAC,CAAC,CACnB,CAAC;IACF,OAAO,EAAE,cAAc,EAAE,iBAAiB,EAAE,cAAc,EAAE,eAAe,EAAE,YAAY,EAAE,CAAC;AAC9F,CAAC","debugId":"e8559b46-0199-506f-bf98-8f92a4474215"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { ItemTypeRegistry } from "../item/type-registry.js";
|
|
2
|
+
import { locateItem, readLocatedItem } from "../store/item-store.js";
|
|
3
|
+
import type { ItemDocument, PmSettings } from "../../types/index.js";
|
|
4
|
+
type LoadedItem = Awaited<ReturnType<typeof readLocatedItem>>;
|
|
5
|
+
export interface HistoryRewriteSubject {
|
|
6
|
+
id: string;
|
|
7
|
+
historyPath: string;
|
|
8
|
+
}
|
|
9
|
+
export interface HistoryRewriteOwnershipParams {
|
|
10
|
+
itemDocument: ItemDocument | null;
|
|
11
|
+
subjectId: string;
|
|
12
|
+
author: string;
|
|
13
|
+
force: boolean | undefined;
|
|
14
|
+
settings: PmSettings;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Apply ownership_enforcement governance to a history-rewriting operation.
|
|
18
|
+
* Returns the warning(s) the caller should append (empty when no conflict or strict-throws).
|
|
19
|
+
* Throws PmCliError(CONFLICT) in strict mode when the assignee conflicts and --force is not set.
|
|
20
|
+
*/
|
|
21
|
+
export declare function checkHistoryRewriteOwnership(params: HistoryRewriteOwnershipParams): string[];
|
|
22
|
+
export interface VerifyHistoryRewriteDriftParams {
|
|
23
|
+
pmRoot: string;
|
|
24
|
+
subject: HistoryRewriteSubject;
|
|
25
|
+
settings: PmSettings;
|
|
26
|
+
typeRegistry: ItemTypeRegistry;
|
|
27
|
+
historyRawBeforeLock: string | null;
|
|
28
|
+
currentItemRawBeforeLock: string | null;
|
|
29
|
+
/** Short operation name used in the conflict message (e.g. "history-redact"). */
|
|
30
|
+
operation: string;
|
|
31
|
+
}
|
|
32
|
+
export interface VerifiedHistoryRewriteState {
|
|
33
|
+
historyRawUnderLock: string | null;
|
|
34
|
+
locatedUnderLock: Awaited<ReturnType<typeof locateItem>>;
|
|
35
|
+
loadedItemUnderLock: LoadedItem | null;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Re-read the history stream and the located item document under the acquired lock and
|
|
39
|
+
* compare both with the pre-lock raw snapshots. Throws PmCliError(CONFLICT) if either
|
|
40
|
+
* diverged while waiting for the lock so the caller surfaces an actionable retry.
|
|
41
|
+
*/
|
|
42
|
+
export declare function verifyHistoryRewriteNoDrift(params: VerifyHistoryRewriteDriftParams): Promise<VerifiedHistoryRewriteState>;
|
|
43
|
+
export {};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
|
|
2
|
+
!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]="6929fbcd-89b7-580d-93fc-f647b086b95f")}catch(e){}}();
|
|
3
|
+
import { readFileIfExists } from "../fs/fs-utils.js";
|
|
4
|
+
import { EXIT_CODE } from "../shared/constants.js";
|
|
5
|
+
import { PmCliError } from "../shared/errors.js";
|
|
6
|
+
import { locateItem, readLocatedItem } from "../store/item-store.js";
|
|
7
|
+
import { resolveGovernanceKnobs } from "../store/settings.js";
|
|
8
|
+
/**
|
|
9
|
+
* Apply ownership_enforcement governance to a history-rewriting operation.
|
|
10
|
+
* Returns the warning(s) the caller should append (empty when no conflict or strict-throws).
|
|
11
|
+
* Throws PmCliError(CONFLICT) in strict mode when the assignee conflicts and --force is not set.
|
|
12
|
+
*/
|
|
13
|
+
export function checkHistoryRewriteOwnership(params) {
|
|
14
|
+
if (!params.itemDocument)
|
|
15
|
+
return [];
|
|
16
|
+
const assigned = params.itemDocument.metadata?.assignee?.trim();
|
|
17
|
+
if (!assigned || assigned === params.author || params.force)
|
|
18
|
+
return [];
|
|
19
|
+
const governance = resolveGovernanceKnobs(params.settings);
|
|
20
|
+
if (governance.ownership_enforcement === "strict") {
|
|
21
|
+
throw new PmCliError(`Item ${params.subjectId} is assigned to ${assigned}. Use --force to override.`, EXIT_CODE.CONFLICT);
|
|
22
|
+
}
|
|
23
|
+
if (governance.ownership_enforcement === "warn") {
|
|
24
|
+
return [`ownership_warning:assignee_conflict:${params.subjectId}:${assigned}`];
|
|
25
|
+
}
|
|
26
|
+
return [];
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Re-read the history stream and the located item document under the acquired lock and
|
|
30
|
+
* compare both with the pre-lock raw snapshots. Throws PmCliError(CONFLICT) if either
|
|
31
|
+
* diverged while waiting for the lock so the caller surfaces an actionable retry.
|
|
32
|
+
*/
|
|
33
|
+
export async function verifyHistoryRewriteNoDrift(params) {
|
|
34
|
+
const historyRawUnderLock = await readFileIfExists(params.subject.historyPath);
|
|
35
|
+
if (historyRawUnderLock !== params.historyRawBeforeLock) {
|
|
36
|
+
throw new PmCliError(`History for ${params.subject.id} changed while waiting for lock; retry ${params.operation}.`, EXIT_CODE.CONFLICT);
|
|
37
|
+
}
|
|
38
|
+
const locatedUnderLock = await locateItem(params.pmRoot, params.subject.id, params.settings.id_prefix, params.settings.item_format, params.typeRegistry.type_to_folder);
|
|
39
|
+
const loadedItemUnderLock = locatedUnderLock
|
|
40
|
+
? await readLocatedItem(locatedUnderLock, { schema: params.settings.schema })
|
|
41
|
+
: null;
|
|
42
|
+
if ((loadedItemUnderLock?.raw ?? null) !== (params.currentItemRawBeforeLock ?? null)) {
|
|
43
|
+
throw new PmCliError(`Item ${params.subject.id} changed while waiting for lock; retry ${params.operation}.`, EXIT_CODE.CONFLICT);
|
|
44
|
+
}
|
|
45
|
+
return { historyRawUnderLock, locatedUnderLock, loadedItemUnderLock };
|
|
46
|
+
}
|
|
47
|
+
//# sourceMappingURL=history-rewrite.js.map
|
|
48
|
+
//# debugId=6929fbcd-89b7-580d-93fc-f647b086b95f
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"history-rewrite.js","sources":["core/history/history-rewrite.ts"],"sourceRoot":"/","sourcesContent":["import { readFileIfExists } from \"../fs/fs-utils.js\";\nimport type { ItemTypeRegistry } from \"../item/type-registry.js\";\nimport { EXIT_CODE } from \"../shared/constants.js\";\nimport { PmCliError } from \"../shared/errors.js\";\nimport { locateItem, readLocatedItem } from \"../store/item-store.js\";\nimport { resolveGovernanceKnobs } from \"../store/settings.js\";\nimport type { ItemDocument, PmSettings } from \"../../types/index.js\";\n\ntype LoadedItem = Awaited<ReturnType<typeof readLocatedItem>>;\n\nexport interface HistoryRewriteSubject {\n id: string;\n historyPath: string;\n}\n\nexport interface HistoryRewriteOwnershipParams {\n itemDocument: ItemDocument | null;\n subjectId: string;\n author: string;\n force: boolean | undefined;\n settings: PmSettings;\n}\n\n/**\n * Apply ownership_enforcement governance to a history-rewriting operation.\n * Returns the warning(s) the caller should append (empty when no conflict or strict-throws).\n * Throws PmCliError(CONFLICT) in strict mode when the assignee conflicts and --force is not set.\n */\nexport function checkHistoryRewriteOwnership(params: HistoryRewriteOwnershipParams): string[] {\n if (!params.itemDocument) return [];\n const assigned = params.itemDocument.metadata?.assignee?.trim();\n if (!assigned || assigned === params.author || params.force) return [];\n\n const governance = resolveGovernanceKnobs(params.settings);\n if (governance.ownership_enforcement === \"strict\") {\n throw new PmCliError(\n `Item ${params.subjectId} is assigned to ${assigned}. Use --force to override.`,\n EXIT_CODE.CONFLICT,\n );\n }\n if (governance.ownership_enforcement === \"warn\") {\n return [`ownership_warning:assignee_conflict:${params.subjectId}:${assigned}`];\n }\n return [];\n}\n\nexport interface VerifyHistoryRewriteDriftParams {\n pmRoot: string;\n subject: HistoryRewriteSubject;\n settings: PmSettings;\n typeRegistry: ItemTypeRegistry;\n historyRawBeforeLock: string | null;\n currentItemRawBeforeLock: string | null;\n /** Short operation name used in the conflict message (e.g. \"history-redact\"). */\n operation: string;\n}\n\nexport interface VerifiedHistoryRewriteState {\n historyRawUnderLock: string | null;\n locatedUnderLock: Awaited<ReturnType<typeof locateItem>>;\n loadedItemUnderLock: LoadedItem | null;\n}\n\n/**\n * Re-read the history stream and the located item document under the acquired lock and\n * compare both with the pre-lock raw snapshots. Throws PmCliError(CONFLICT) if either\n * diverged while waiting for the lock so the caller surfaces an actionable retry.\n */\nexport async function verifyHistoryRewriteNoDrift(\n params: VerifyHistoryRewriteDriftParams,\n): Promise<VerifiedHistoryRewriteState> {\n const historyRawUnderLock = await readFileIfExists(params.subject.historyPath);\n if (historyRawUnderLock !== params.historyRawBeforeLock) {\n throw new PmCliError(\n `History for ${params.subject.id} changed while waiting for lock; retry ${params.operation}.`,\n EXIT_CODE.CONFLICT,\n );\n }\n const locatedUnderLock = await locateItem(\n params.pmRoot,\n params.subject.id,\n params.settings.id_prefix,\n params.settings.item_format,\n params.typeRegistry.type_to_folder,\n );\n const loadedItemUnderLock = locatedUnderLock\n ? await readLocatedItem(locatedUnderLock, { schema: params.settings.schema })\n : null;\n if ((loadedItemUnderLock?.raw ?? null) !== (params.currentItemRawBeforeLock ?? null)) {\n throw new PmCliError(\n `Item ${params.subject.id} changed while waiting for lock; retry ${params.operation}.`,\n EXIT_CODE.CONFLICT,\n );\n }\n return { historyRawUnderLock, locatedUnderLock, loadedItemUnderLock };\n}\n"],"names":[],"mappings":";;AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AAErD,OAAO,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AACnD,OAAO,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AACjD,OAAO,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AACrE,OAAO,EAAE,sBAAsB,EAAE,MAAM,sBAAsB,CAAC;AAkB9D;;;;GAIG;AACH,MAAM,UAAU,4BAA4B,CAAC,MAAqC;IAChF,IAAI,CAAC,MAAM,CAAC,YAAY;QAAE,OAAO,EAAE,CAAC;IACpC,MAAM,QAAQ,GAAG,MAAM,CAAC,YAAY,CAAC,QAAQ,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC;IAChE,IAAI,CAAC,QAAQ,IAAI,QAAQ,KAAK,MAAM,CAAC,MAAM,IAAI,MAAM,CAAC,KAAK;QAAE,OAAO,EAAE,CAAC;IAEvE,MAAM,UAAU,GAAG,sBAAsB,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;IAC3D,IAAI,UAAU,CAAC,qBAAqB,KAAK,QAAQ,EAAE,CAAC;QAClD,MAAM,IAAI,UAAU,CAClB,QAAQ,MAAM,CAAC,SAAS,mBAAmB,QAAQ,4BAA4B,EAC/E,SAAS,CAAC,QAAQ,CACnB,CAAC;IACJ,CAAC;IACD,IAAI,UAAU,CAAC,qBAAqB,KAAK,MAAM,EAAE,CAAC;QAChD,OAAO,CAAC,uCAAuC,MAAM,CAAC,SAAS,IAAI,QAAQ,EAAE,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@
|
|
|
1
1
|
|
|
2
|
-
!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]="
|
|
2
|
+
!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]="9a356560-3dec-5529-a2b5-1b4224116c14")}catch(e){}}();
|
|
3
3
|
import jsonPatch from "fast-json-patch";
|
|
4
4
|
import { FRONT_MATTER_KEY_ORDER } from "../shared/constants.js";
|
|
5
5
|
import { runActiveServiceOverride } from "../extensions/index.js";
|
|
6
6
|
import { appendLineAtomic } from "../fs/fs-utils.js";
|
|
7
7
|
import { canonicalDocument } from "../item/item-format.js";
|
|
8
|
+
import { toItemRecord } from "../item/item-record.js";
|
|
8
9
|
import { orderObject, sha256Hex, stableStringify } from "../shared/serialization.js";
|
|
9
10
|
const EMPTY_LEGACY_HASH_DOCUMENT = {
|
|
10
11
|
front_matter: {},
|
|
@@ -79,7 +80,7 @@ function canonicalHashDocument(document) {
|
|
|
79
80
|
};
|
|
80
81
|
}
|
|
81
82
|
const canonical = canonicalDocument(document);
|
|
82
|
-
const orderedFrontMatter = orderObject(canonical.metadata, FRONT_MATTER_KEY_ORDER);
|
|
83
|
+
const orderedFrontMatter = orderObject(toItemRecord(canonical.metadata), FRONT_MATTER_KEY_ORDER);
|
|
83
84
|
return {
|
|
84
85
|
front_matter: orderedFrontMatter,
|
|
85
86
|
body: canonical.body,
|
|
@@ -94,7 +95,7 @@ function canonicalPatchDocument(document) {
|
|
|
94
95
|
};
|
|
95
96
|
}
|
|
96
97
|
const canonical = canonicalDocument(document);
|
|
97
|
-
const orderedMetadata = orderObject(canonical.metadata, FRONT_MATTER_KEY_ORDER);
|
|
98
|
+
const orderedMetadata = orderObject(toItemRecord(canonical.metadata), FRONT_MATTER_KEY_ORDER);
|
|
98
99
|
return {
|
|
99
100
|
metadata: orderedMetadata,
|
|
100
101
|
body: canonical.body,
|
|
@@ -154,4 +155,4 @@ export async function appendHistoryEntry(historyPath, entry) {
|
|
|
154
155
|
await appendLineAtomic(historyPath, JSON.stringify(entry));
|
|
155
156
|
}
|
|
156
157
|
//# sourceMappingURL=history.js.map
|
|
157
|
-
//# debugId=
|
|
158
|
+
//# debugId=9a356560-3dec-5529-a2b5-1b4224116c14
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"history.js","sources":["core/history/history.ts"],"sourceRoot":"/","sourcesContent":["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"],"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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Id":"bd15b5bc-70d0-57d2-a06d-27b9d518656f"}
|
|
1
|
+
{"version":3,"file":"history.js","sources":["core/history/history.ts"],"sourceRoot":"/","sourcesContent":["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 { toItemRecord } from \"../item/item-record.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(toItemRecord(canonical.metadata), 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(toItemRecord(canonical.metadata), 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"],"names":[],"mappings":";;AAAA,OAAO,SAAS,MAAM,iBAAiB,CAAC;AACxC,OAAO,EAAE,sBAAsB,EAAE,MAAM,wBAAwB,CAAC;AAChE,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,YAAY,EAAE,MAAM,wBAAwB,CAAC;AACtD,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,eAAe,EAAE,MAAM,4BAA4B,CAAC;AAGrF,MAAM,0BAA0B,GAAG;IACjC,YAAY,EAAE,EAAE;IAChB,IAAI,EAAE,EAA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AAK,EAAE,CAAC;YAC9B,OAAO;QACT,CAAC;QACD,IAAI,OAAO,QAAQ,CAAC,MAAM,KAAK,QAAQ,EAAE,CAAC;YACxC,MAAM,gBAAgB,CAAC,WAAW,EAAE,QAAQ,CAAC,MAAM,CAAC,CAAC;YACrD,OAAO;QACT,CAAC;QACD,IAAI,OAAO,QAAQ,CAAC,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","debugId":"9a356560-3dec-5529-a2b5-1b4224116c14"}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import type { HistoryEntry, HistoryPatchOp, ItemDocument } from "../../types/index.js";
|
|
2
|
+
/**
|
|
3
|
+
* Shared history replay/patch mechanics single-sourced for the history, restore,
|
|
4
|
+
* history-redact, and history-repair commands plus the health/validate drift checks.
|
|
5
|
+
*
|
|
6
|
+
* Each command keeps its own thin error-formatting wrapper so the exact CLI error
|
|
7
|
+
* contracts (restore's rich patch-failure context, redact's op tag, history's
|
|
8
|
+
* generic message) are preserved; only the underlying replay/patch logic is shared.
|
|
9
|
+
*/
|
|
10
|
+
export interface ReplayDocument {
|
|
11
|
+
metadata: Record<string, unknown>;
|
|
12
|
+
body: string;
|
|
13
|
+
}
|
|
14
|
+
export declare const EMPTY_REPLAY_DOCUMENT: ReplayDocument;
|
|
15
|
+
export declare function cloneEmptyReplayDocument(): ReplayDocument;
|
|
16
|
+
export declare function replayHash(document: ReplayDocument): string;
|
|
17
|
+
export declare function replayToItemDocument(document: ReplayDocument): ItemDocument;
|
|
18
|
+
/**
|
|
19
|
+
* Canonicalize an item document into the ordered replay form used when comparing
|
|
20
|
+
* a replayed chain against the on-disk item (restore + history-repair reconciliation).
|
|
21
|
+
*/
|
|
22
|
+
export declare function toReplayDocument(document: ItemDocument): ReplayDocument;
|
|
23
|
+
export declare function normalizeReplayPatchPath(path: string): string;
|
|
24
|
+
export declare function normalizeReplayPatchOps(patch: HistoryPatchOp[]): HistoryPatchOp[];
|
|
25
|
+
export type ReplayApplyResult = {
|
|
26
|
+
ok: true;
|
|
27
|
+
document: ReplayDocument;
|
|
28
|
+
} | {
|
|
29
|
+
ok: false;
|
|
30
|
+
error: unknown;
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Strictly apply a history patch (front_matter->metadata normalized) to a replay
|
|
34
|
+
* document. Returns a result envelope rather than throwing so each caller can
|
|
35
|
+
* format its own error contract.
|
|
36
|
+
*/
|
|
37
|
+
export declare function tryApplyReplayPatch(current: ReplayDocument, patch: HistoryPatchOp[]): ReplayApplyResult;
|
|
38
|
+
/**
|
|
39
|
+
* Deterministically verify a history chain: each entry's before_hash must equal
|
|
40
|
+
* the prior replayed after_hash, the patch must strictly apply, and the recorded
|
|
41
|
+
* after_hash must equal the replayed result.
|
|
42
|
+
*/
|
|
43
|
+
export declare function verifyHistoryChain(entries: HistoryEntry[]): {
|
|
44
|
+
ok: boolean;
|
|
45
|
+
errors: string[];
|
|
46
|
+
};
|
|
47
|
+
export interface LenientApplyResult {
|
|
48
|
+
document: ReplayDocument;
|
|
49
|
+
convertedReplaceToAdd: number;
|
|
50
|
+
skippedOps: number;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Apply a legacy patch op-by-op, recovering from drift that strict replay rejects:
|
|
54
|
+
* a `replace` whose path no longer exists is retried as `add`, and any op that
|
|
55
|
+
* still cannot apply against the current replay state is skipped. The resulting
|
|
56
|
+
* document is what the repaired entry's recomputed patch should target.
|
|
57
|
+
*/
|
|
58
|
+
export declare function lenientApplyReplayPatch(current: ReplayDocument, patch: HistoryPatchOp[]): LenientApplyResult;
|
|
59
|
+
export interface ReanchorEntryDetail {
|
|
60
|
+
index: number;
|
|
61
|
+
rehashed: boolean;
|
|
62
|
+
patch_repaired: boolean;
|
|
63
|
+
converted_replace_to_add: number;
|
|
64
|
+
skipped_ops: number;
|
|
65
|
+
}
|
|
66
|
+
export interface ReanchorResult {
|
|
67
|
+
entries: HistoryEntry[];
|
|
68
|
+
finalDocument: ReplayDocument;
|
|
69
|
+
entriesRehashed: number;
|
|
70
|
+
entriesPatchRepaired: number;
|
|
71
|
+
convertedReplaceToAdd: number;
|
|
72
|
+
skippedOps: number;
|
|
73
|
+
details: ReanchorEntryDetail[];
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Re-anchor a drifted history chain: replay every entry from empty, recompute the
|
|
77
|
+
* before/after hashes, and only rewrite a patch when the original op set no longer
|
|
78
|
+
* strictly applies (legacy drift). Clean entries keep their patch verbatim so the
|
|
79
|
+
* on-disk diff stays minimal. The returned chain verifies via verifyHistoryChain.
|
|
80
|
+
*/
|
|
81
|
+
export declare function reanchorHistoryEntries(entries: HistoryEntry[]): ReanchorResult;
|
|
82
|
+
export declare function historyEntriesToRaw(entries: HistoryEntry[]): string;
|