@aibyzero/cm-workflow 0.17.1 → 0.18.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.codex-plugin/plugin.json +1 -1
- package/AGENTS.md +23 -0
- package/CHANGELOG.md +224 -0
- package/README.md +14 -3
- package/THIRD_PARTY_NOTICES.md +13 -0
- package/VERSION +1 -1
- package/compat/claude-commands/cm-challenge.md +3 -0
- package/docs/architecture.md +11 -0
- package/docs/cm-control-policy-v2.md +127 -9
- package/docs/installation.md +310 -4
- package/docs/js-workflow-control.md +150 -15
- package/docs/live-evidence-drivers.md +28 -4
- package/docs/out-of-scope/README.md +25 -0
- package/docs/out-of-scope/background-supervisor.md +32 -0
- package/docs/reviewed-integration.md +48 -0
- package/docs/user-guide.md +50 -21
- package/install-codex.sh +331 -18
- package/install.ps1 +28 -2
- package/install.sh +321 -17
- package/package.json +2 -1
- package/runtime/js/cm-ai/batch-location.mjs +55 -0
- package/runtime/js/cm-ai/batch-plan.mjs +242 -0
- package/runtime/js/cm-ai/checkpoint-package-material.mjs +162 -0
- package/runtime/js/cm-ai/claude-developer-proposal.schema.json +55 -1
- package/runtime/js/cm-ai/claude-review-stream.mjs +83 -14
- package/runtime/js/cm-ai/claude-tool-preview.mjs +11 -9
- package/runtime/js/cm-ai/cm-ai-admission.mjs +18 -12
- package/runtime/js/cm-ai/cm-ai-conversation-entry.mjs +91 -6
- package/runtime/js/cm-ai/cm-ai-qa-log.mjs +5 -1
- package/runtime/js/cm-ai/codex-config.mjs +6 -2
- package/runtime/js/cm-ai/codex-developer-basic-output.schema.json +3 -2
- package/runtime/js/cm-ai/codex-developer-output.schema.json +2 -1
- package/runtime/js/cm-ai/codex-reconnect-notice.mjs +6 -0
- package/runtime/js/cm-ai/codex-review-adapter.mjs +27 -7
- package/runtime/js/cm-ai/control-policy.mjs +21 -4
- package/runtime/js/cm-ai/developer-adapter.mjs +19 -5
- package/runtime/js/cm-ai/drive-core.mjs +114 -11
- package/runtime/js/cm-ai/drive-rate-limit.mjs +105 -0
- package/runtime/js/cm-ai/durable-runner-state.mjs +234 -33
- package/runtime/js/cm-ai/effect-contract.mjs +11 -2
- package/runtime/js/cm-ai/execution-policy.mjs +3 -1
- package/runtime/js/cm-ai/execution-snapshot.mjs +4 -2
- package/runtime/js/cm-ai/execution-store-limits.mjs +14 -7
- package/runtime/js/cm-ai/execution-store.mjs +60 -17
- package/runtime/js/cm-ai/fix-code-association.mjs +73 -2
- package/runtime/js/cm-ai/host-check.mjs +11 -5
- package/runtime/js/cm-ai/host-conversation-execution.mjs +70 -28
- package/runtime/js/cm-ai/host-qa-executor.mjs +38 -5
- package/runtime/js/cm-ai/host-qa-fix-owner.mjs +2 -2
- package/runtime/js/cm-ai/host-qa-policy.mjs +19 -2
- package/runtime/js/cm-ai/host-replay.mjs +148 -0
- package/runtime/js/cm-ai/host-session.mjs +1 -1
- package/runtime/js/cm-ai/host-workflow-capabilities.mjs +19 -2
- package/runtime/js/cm-ai/host.mjs +1 -1
- package/runtime/js/cm-ai/implicit-qa-decision.mjs +5 -0
- package/runtime/js/cm-ai/integration-snapshots.mjs +50 -0
- package/runtime/js/cm-ai/live-evidence.mjs +28 -6
- package/runtime/js/cm-ai/operator-guidance.mjs +9 -2
- package/runtime/js/cm-ai/owned-process-cleanup.mjs +27 -9
- package/runtime/js/cm-ai/process-group-exit.mjs +46 -0
- package/runtime/js/cm-ai/provider-review-observation.mjs +3 -2
- package/runtime/js/cm-ai/qa-attachment.mjs +22 -0
- package/runtime/js/cm-ai/qa-config-revision.mjs +7 -2
- package/runtime/js/cm-ai/requirement-evidence.mjs +102 -0
- package/runtime/js/cm-ai/review-package.mjs +79 -7
- package/runtime/js/cm-ai/review-presentation-delta.mjs +166 -0
- package/runtime/js/cm-ai/review-presentation.mjs +177 -8
- package/runtime/js/cm-ai/reviewed-evidence-supersede.mjs +70 -2
- package/runtime/js/cm-ai/reviewed-integration.mjs +260 -0
- package/runtime/js/cm-ai/scope-revision.mjs +38 -0
- package/runtime/js/cm-ai/specification-material.mjs +2 -2
- package/runtime/js/cm-ai/task-runner.mjs +242 -39
- package/runtime/js/cm-ai/verification-auto.mjs +38 -0
- package/runtime/js/cm-ai/worker-claude-developer.mjs +39 -15
- package/runtime/js/cm-ai/worker-claude.mjs +35 -13
- package/runtime/js/cm-ai/worker-codex-developer.mjs +43 -17
- package/runtime/js/cm-ai/worker-codex.mjs +41 -7
- package/runtime/js/cm-fix/cause-invocation.mjs +16 -2
- package/runtime/js/cm-fix/cause-package.mjs +46 -1
- package/runtime/js/cm-fix/command-correction.mjs +37 -0
- package/runtime/js/cm-fix/cooperative.mjs +12 -0
- package/runtime/js/cm-fix/dossier.mjs +8 -2
- package/runtime/js/cm-fix/execution.mjs +578 -100
- package/runtime/js/cm-fix/final-review.mjs +39 -8
- package/runtime/js/cm-fix/finish.mjs +15 -11
- package/runtime/js/cm-fix/host-review.mjs +65 -10
- package/runtime/js/cm-fix/host.mjs +35 -12
- package/runtime/js/cm-fix/jest-command-correction.mjs +69 -0
- package/runtime/js/cm-fix/metrics.mjs +18 -5
- package/runtime/js/cm-fix/regression-evidence.mjs +3 -2
- package/runtime/js/cm-fix/regression.mjs +16 -7
- package/runtime/js/cm-fix/repair.mjs +29 -8
- package/runtime/js/cm-fix/review-verification-command.mjs +135 -0
- package/runtime/js/cm-fix/review-verification-contract.mjs +54 -0
- package/runtime/js/cm-fix/review-verification-diagnostic.mjs +75 -0
- package/runtime/js/cm-fix/review-verification-recovery.mjs +44 -0
- package/runtime/js/cm-fix/review-verification-server.mjs +38 -0
- package/runtime/js/cm-fix/review-verification.mjs +214 -0
- package/runtime/js/cm-fix/source-correction-recovery.mjs +65 -0
- package/runtime/js/cm-fix/start.mjs +6 -3
- package/runtime/js/cm-fix/test-type-correction.mjs +86 -0
- package/runtime/js/cm-fix/test-type-recovery.mjs +51 -0
- package/runtime/js/cm-fix/type-import-correction.mjs +89 -0
- package/runtime/js/cm-prd/accepted-design.mjs +8 -0
- package/runtime/js/cm-prd/analysis.mjs +13 -9
- package/runtime/js/cm-prd/change.mjs +37 -16
- package/runtime/js/cm-prd/draft-save.mjs +13 -6
- package/runtime/js/cm-prd/draft.mjs +31 -3
- package/runtime/js/cm-prd/failed-correction.mjs +19 -3
- package/runtime/js/cm-prd/inputs-replaced.mjs +3 -0
- package/runtime/js/cm-prd/review-correction.mjs +6 -4
- package/runtime/js/cm-prd/review-disposition.mjs +7 -1
- package/runtime/js/cm-prd/self-check-revision.mjs +2 -2
- package/runtime/js/cm-prd/self-check.mjs +125 -6
- package/runtime/js/cm-prd/session.mjs +30 -1
- package/runtime/js/cm-prd/summary.mjs +11 -5
- package/runtime/js/cm-prd/task-scope-record.mjs +97 -0
- package/runtime/js/cm-prd/tree-digest.mjs +34 -0
- package/runtime/js/cm-prd/validation-reason.mjs +8 -4
- package/runtime/js/cm-security/scan.mjs +1 -1
- package/runtime/js/commander/decision.mjs +44 -0
- package/runtime/js/commander/dispatch-registry.mjs +110 -0
- package/runtime/js/commander/ledger.mjs +265 -0
- package/runtime/js/commander/preflight.mjs +107 -0
- package/runtime/js/notify-later.mjs +13 -0
- package/runtime/js/notify-send.mjs +6 -2
- package/runtime/js/notify.mjs +570 -63
- package/runtime/js/review-findings.mjs +38 -0
- package/runtime/js/session-boundary.mjs +56 -0
- package/runtime/js/session-handoff.mjs +68 -0
- package/runtime/js/spec-task-line.mjs +27 -13
- package/runtime/js/specs-status.mjs +1 -1
- package/runtime/js/task-scope.mjs +197 -0
- package/runtime/logging.md +6 -1
- package/runtime/model-efficiency.md +69 -10
- package/runtime/orchestration.md +3 -1
- package/runtime/project-learning.md +2 -0
- package/runtime/review.md +3 -2
- package/runtime/test-contract.md +1 -0
- package/scripts/ci-test-weights.json +3 -0
- package/scripts/cm-ai-admission.mjs +11 -5
- package/scripts/cm-ai-admission.test.mjs +23 -2
- package/scripts/cm-ai-batch-drive.mjs +79 -18
- package/scripts/cm-ai-batch-drive.test.mjs +169 -3
- package/scripts/cm-ai-batch-host.mjs +38 -9
- package/scripts/cm-ai-batch-host.test.mjs +6 -5
- package/scripts/cm-ai-batch-learning-merge.test.mjs +36 -0
- package/scripts/cm-ai-batch-location.test.mjs +57 -0
- package/scripts/cm-ai-batch-plan.mjs +52 -0
- package/scripts/cm-ai-batch-plan.test.mjs +397 -0
- package/scripts/cm-ai-batch-run-fixture.mjs +446 -35
- package/scripts/cm-ai-batch-run-member-stops.test.mjs +16 -0
- package/scripts/cm-ai-batch-run-parallel.test.mjs +28 -1
- package/scripts/cm-ai-batch-run.mjs +471 -57
- package/scripts/cm-ai-batch-run.test.mjs +23 -4
- package/scripts/cm-ai-batch-session-e2e.test.mjs +235 -0
- package/scripts/cm-ai-batch-session-member.test.mjs +293 -0
- package/scripts/cm-ai-batch-session-recovery.test.mjs +423 -0
- package/scripts/cm-ai-check-recheck-cancellation.test.mjs +247 -0
- package/scripts/cm-ai-checklist-coverage.test.mjs +4 -2
- package/scripts/cm-ai-checkpoint-package-material.test.mjs +221 -0
- package/scripts/cm-ai-checkpoint-package-owner.test.mjs +193 -0
- package/scripts/cm-ai-control-launch.test.mjs +21 -0
- package/scripts/cm-ai-control-policy.test.mjs +14 -0
- package/scripts/cm-ai-control-v2-drive.test.mjs +398 -3
- package/scripts/cm-ai-control-v2-supersede-race.mjs +25 -0
- package/scripts/cm-ai-control-v2-supersede.test.mjs +318 -0
- package/scripts/cm-ai-develop-driver-gaps.test.mjs +163 -0
- package/scripts/cm-ai-development-rework.test.mjs +423 -0
- package/scripts/cm-ai-drive.mjs +73 -17
- package/scripts/cm-ai-drive.test.mjs +34 -2
- package/scripts/cm-ai-execution-store-legacy-probe.test.mjs +84 -0
- package/scripts/cm-ai-host.mjs +60 -19
- package/scripts/cm-ai-integration-adoption.test.mjs +223 -0
- package/scripts/cm-ai-integration-independence.test.mjs +23 -0
- package/scripts/cm-ai-integration-launch.test.mjs +240 -0
- package/scripts/cm-ai-integration-preview.mjs +41 -0
- package/scripts/cm-ai-integration-snapshots.test.mjs +197 -0
- package/scripts/cm-ai-nested-execution.test.mjs +29 -1
- package/scripts/cm-ai-operator-guidance.test.mjs +16 -1
- package/scripts/cm-ai-prefinal-delivery.test.mjs +278 -0
- package/scripts/cm-ai-project-qa-gate.test.mjs +56 -0
- package/scripts/cm-ai-qa-fingerprint-config.test.mjs +28 -0
- package/scripts/cm-ai-qa-rerun-timeouts.test.mjs +36 -2
- package/scripts/cm-ai-requirement-evidence.test.mjs +65 -0
- package/scripts/cm-ai-review-failure.test.mjs +150 -5
- package/scripts/cm-ai-run.mjs +88 -21
- package/scripts/cm-ai-scope-revision.test.mjs +341 -0
- package/scripts/cm-ai-spec-rebind.test.mjs +13 -0
- package/scripts/cm-ai-supersede.test.mjs +28 -0
- package/scripts/cm-ai-verification-auto.test.mjs +99 -0
- package/scripts/cm-announce-hook.mjs +35 -8
- package/scripts/cm-announce-hook.test.mjs +25 -0
- package/scripts/cm-challenge.test.mjs +61 -0
- package/scripts/cm-check-drive-files.mjs +1 -1
- package/scripts/cm-check-runtime.sh +58 -1
- package/scripts/cm-claude-developer-worker.test.mjs +56 -7
- package/scripts/cm-claude-install-layout.test.mjs +1 -1
- package/scripts/cm-claude-review-diagnostics.test.mjs +235 -0
- package/scripts/cm-claude-review-stream.test.mjs +13 -0
- package/scripts/cm-codex-developer-reconnect-notice.test.mjs +98 -0
- package/scripts/cm-codex-developer-worker.test.mjs +120 -0
- package/scripts/cm-codex-install-local-edit-guard.test.mjs +436 -0
- package/scripts/cm-codex-prompt-input.test.mjs +153 -0
- package/scripts/cm-codex-reconnect-notice.test.mjs +167 -0
- package/scripts/cm-commander-ledger.test.mjs +260 -0
- package/scripts/cm-commander-p2.test.mjs +295 -0
- package/scripts/cm-commander-preflight.mjs +84 -0
- package/scripts/cm-commander.mjs +116 -0
- package/scripts/cm-config-lock.mjs +35 -0
- package/scripts/cm-controlled-correction.test.mjs +10 -1
- package/scripts/cm-developer-failure-code.test.mjs +62 -0
- package/scripts/cm-display-task-id.test.mjs +41 -0
- package/scripts/cm-efficiency-presentation-delta.test.mjs +218 -0
- package/scripts/cm-execution-snapshot-reuse.test.mjs +115 -0
- package/scripts/cm-external-batch-host.test.mjs +4 -3
- package/scripts/cm-external-process-cleanup.test.mjs +128 -0
- package/scripts/cm-external-qa-fix.test.mjs +89 -7
- package/scripts/cm-external-review-reconciliation.test.mjs +11 -0
- package/scripts/cm-failover.mjs +48 -10
- package/scripts/cm-failover.test.mjs +46 -0
- package/scripts/cm-fix-cause-invocation.test.mjs +19 -4
- package/scripts/cm-fix-command-correction.mjs +17 -0
- package/scripts/cm-fix-command-correction.test.mjs +211 -0
- package/scripts/cm-fix-cooperative-closeout.test.mjs +87 -0
- package/scripts/cm-fix-diagnosis-module-correction.test.mjs +217 -0
- package/scripts/cm-fix-drive.mjs +33 -5
- package/scripts/cm-fix-drive.test.mjs +49 -0
- package/scripts/cm-fix-host-review-assembly.test.mjs +197 -1
- package/scripts/cm-fix-host-review.test.mjs +21 -9
- package/scripts/cm-fix-host.mjs +26 -8
- package/scripts/cm-fix-monorepo-map.test.mjs +92 -0
- package/scripts/cm-fix-observation-task-id.test.mjs +122 -0
- package/scripts/cm-fix-regression-input-budget.test.mjs +111 -0
- package/scripts/cm-fix-repair-input-budget.test.mjs +119 -0
- package/scripts/cm-fix-review-file-paging.test.mjs +91 -0
- package/scripts/cm-fix-review-preflight.mjs +24 -0
- package/scripts/cm-fix-review-storage.test.mjs +233 -0
- package/scripts/cm-fix-review-verification-command.test.mjs +242 -0
- package/scripts/cm-fix-review-verification-evidence.test.mjs +218 -0
- package/scripts/cm-fix-review-verification-integration.test.mjs +81 -0
- package/scripts/cm-fix-review-verification-recovery.test.mjs +198 -0
- package/scripts/cm-fix-review-verification.test.mjs +146 -0
- package/scripts/cm-fix-revision-transport.test.mjs +81 -0
- package/scripts/cm-fix-source-correction.test.mjs +296 -0
- package/scripts/cm-fix-test-type-correction.mjs +18 -0
- package/scripts/cm-fix-test-type-correction.test.mjs +123 -0
- package/scripts/cm-fix-test-type-owner.test.mjs +208 -0
- package/scripts/cm-fix-test-type-verification-cost.test.mjs +66 -0
- package/scripts/cm-fix-type-import-correction.test.mjs +95 -0
- package/scripts/cm-fix-verification-diagnostic.test.mjs +200 -0
- package/scripts/cm-fix-verification-recovery.mjs +33 -0
- package/scripts/cm-host-qa-executor.test.mjs +77 -0
- package/scripts/cm-host-replay.test.mjs +176 -0
- package/scripts/cm-install-local-edit-guard.test.mjs +621 -0
- package/scripts/cm-install-manifest.sh +351 -0
- package/scripts/cm-live-evidence.test.mjs +59 -1
- package/scripts/cm-log-event.mjs +61 -10
- package/scripts/cm-log-event.test.mjs +121 -1
- package/scripts/cm-matt-skills.mjs +576 -0
- package/scripts/cm-matt-skills.test.mjs +550 -0
- package/scripts/cm-notify-command.test.mjs +42 -10
- package/scripts/cm-notify.mjs +14 -10
- package/scripts/cm-notify.test.mjs +575 -59
- package/scripts/cm-patrol.mjs +4 -1
- package/scripts/cm-patrol.test.mjs +23 -5
- package/scripts/cm-prd-answer-gaps.test.mjs +2 -2
- package/scripts/cm-prd-completion.test.mjs +56 -13
- package/scripts/cm-prd-design-phase.test.mjs +78 -8
- package/scripts/cm-prd-draft-save.test.mjs +12 -0
- package/scripts/cm-prd-draft.test.mjs +1 -1
- package/scripts/cm-prd-drive.mjs +13 -6
- package/scripts/cm-prd-drive.test.mjs +45 -1
- package/scripts/cm-prd-gate-diagnostic.test.mjs +2 -2
- package/scripts/cm-prd-host.mjs +10 -5
- package/scripts/cm-prd-host.test.mjs +4 -2
- package/scripts/cm-prd-inputs-replaced.test.mjs +1 -1
- package/scripts/cm-prd-review-disposition.test.mjs +124 -8
- package/scripts/cm-prd-revision.test.mjs +1 -1
- package/scripts/cm-prd-self-check-revision.test.mjs +1 -1
- package/scripts/cm-prd-self-check-rules.test.mjs +239 -0
- package/scripts/cm-prd-self-check.test.mjs +105 -5
- package/scripts/cm-prd-summary.test.mjs +24 -3
- package/scripts/cm-prd-task-grouping.test.mjs +6 -6
- package/scripts/cm-prd-tree-digest.test.mjs +74 -0
- package/scripts/cm-refactor-drive.mjs +2 -1
- package/scripts/cm-refactor-drive.test.mjs +9 -0
- package/scripts/cm-release-smoke.sh +1 -1
- package/scripts/cm-skill-hints-autowire.test.mjs +394 -0
- package/scripts/cm-skill-hints-exam.mjs +149 -0
- package/scripts/cm-skill-hints-install.test.mjs +168 -0
- package/scripts/cm-skill-hints.exam.json +706 -0
- package/scripts/cm-skill-hints.json +119 -0
- package/scripts/cm-skill-hints.mjs +669 -0
- package/scripts/cm-skill-hints.test.mjs +831 -0
- package/scripts/cm-skill-judge.mjs +181 -0
- package/scripts/cm-skill-judge.test.mjs +255 -0
- package/scripts/cm-specs-status.test.mjs +4 -2
- package/scripts/cm-start-check.mjs +208 -0
- package/scripts/cm-start-check.test.mjs +321 -0
- package/scripts/cm-task-gate.mjs +150 -29
- package/scripts/cm-task-gate.test.mjs +73 -1
- package/scripts/cm-task-id-grammar.test.mjs +66 -0
- package/scripts/cm-task-scope.test.mjs +308 -0
- package/scripts/cm-upgrade-compat-batch.test.mjs +172 -0
- package/scripts/cm-upgrade-compat.test.mjs +239 -0
- package/scripts/cm-workflow-config.mjs +12 -2
- package/scripts/fixtures/fix-verification-claude.mjs +44 -0
- package/scripts/fixtures/reviewed-integration.mjs +202 -0
- package/scripts/scan-public-safety.py +1 -0
- package/scripts/session-boundary.test.mjs +170 -0
- package/scripts/test-npm-install.py +14 -3
- package/scripts/test-shell-compat.sh +2 -0
- package/scripts/validate-public-repo.py +1 -0
- package/scripts/validate-test-cases.mjs +3 -2
- package/skills/cm-ai/SKILL.md +5 -2
- package/skills/cm-ai/references/N4-review.md +2 -1
- package/skills/cm-ai/references/N6-qa-eval.md +3 -1
- package/skills/cm-ai/references/N8-finish.md +22 -0
- package/skills/cm-ai/references/js-host-qa-recovery.md +1 -1
- package/skills/cm-ai/references/js-host-recovery.md +79 -3
- package/skills/cm-ai/references/js-host.md +56 -16
- package/skills/cm-challenge/SKILL.md +65 -0
- package/skills/cm-challenge/evals/README.md +18 -0
- package/skills/cm-challenge/evals/trigger-cases.json +20 -0
- package/skills/cm-challenge/evals/trigger-config.json +28 -0
- package/skills/cm-challenge/references/examples.md +15 -0
- package/skills/cm-check/SKILL.md +5 -0
- package/skills/cm-check/references/js-host.md +2 -0
- package/skills/cm-fix/SKILL.md +12 -3
- package/skills/cm-fix/references/command-correction.md +67 -0
- package/skills/cm-fix/references/isolated-review.md +54 -0
- package/skills/cm-fix/references/js-host.md +62 -2
- package/skills/cm-fix/references/test-type-correction.md +72 -0
- package/skills/cm-fix/references/verification-recovery.md +114 -0
- package/skills/cm-idea/SKILL.md +2 -0
- package/skills/cm-idea/references/js-host.md +2 -0
- package/skills/cm-notify/SKILL.md +2 -0
- package/skills/cm-prd/SKILL.md +12 -8
- package/skills/cm-prd/references/brownfield.md +8 -1
- package/skills/cm-prd/references/change-mode.md +6 -0
- package/skills/cm-prd/references/greenfield.md +4 -0
- package/skills/cm-prd/references/js-host.md +4 -2
- package/skills/cm-prd/references/spec-self-check.md +5 -1
- package/skills/cm-refactor/SKILL.md +8 -0
- package/skills/cm-refactor/references/js-host.md +2 -0
- package/skills/cm-test/SKILL.md +4 -0
- package/templates/dashboard/dashboard.html +2 -2
- package/templates/pixel/cm-pixel.html +1 -1
- package/templates/pixel/cm-pixel.sh +1 -1
- package/templates/pixel/dev/template.html +1 -1
- package/templates/statusline/cm-statusline.sh +1 -1
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
// Requirement evidence from the verification precheck, carried into the host
|
|
2
|
+
// handoff so the independent reviewer sees what the developer reported for each
|
|
3
|
+
// written requirement. It is the developer's own record, never host-verified fact.
|
|
4
|
+
import {need,shape,json,validIdentity,arrayItems} from './effect-contract.mjs';
|
|
5
|
+
|
|
6
|
+
export const REQUIREMENT_EVIDENCE_PREFIX='cm-requirement-evidence-v1: ';
|
|
7
|
+
export const REQUIREMENT_ITEM_LIMIT=64;
|
|
8
|
+
export const REQUIREMENT_TEXT_LIMIT=2000;
|
|
9
|
+
// The whole serialized item list. The handoff itself is capped at 256 KiB and
|
|
10
|
+
// also carries checks and Learning evidence, so the requirement record keeps a
|
|
11
|
+
// bound well below it instead of failing the package at publication time.
|
|
12
|
+
export const REQUIREMENT_EVIDENCE_LIMIT=64*1024;
|
|
13
|
+
|
|
14
|
+
const freeze=value=>Object.freeze(value);
|
|
15
|
+
const boundedText=(value,code)=>need(typeof value==='string'&&value.trim().length>0
|
|
16
|
+
&&Buffer.byteLength(value,'utf8')<=REQUIREMENT_TEXT_LIMIT&&!/[\x00-\x08\x0b-\x1f]/.test(value),code);
|
|
17
|
+
|
|
18
|
+
// Validates the precheck answer items: [{requirement,satisfied,evidence}], each
|
|
19
|
+
// optionally tagged with the host run it states (run "red": a test failed before
|
|
20
|
+
// the implementation; run "baseline" with result "passed"|"failed": the run before
|
|
21
|
+
// development). A null tag means none; the tag never reaches the handoff text.
|
|
22
|
+
// result means something only on a baseline item. Elsewhere it is dropped, not an
|
|
23
|
+
// error: the first real run wrote result "failed" on its red item, which is just
|
|
24
|
+
// what a red run is, and that one field must not void the whole report.
|
|
25
|
+
const RUNS=new Set(['red','baseline']),RESULTS=new Set(['passed','failed']);
|
|
26
|
+
export function readRequirementItems(raw,code='verification_precheck_invalid'){
|
|
27
|
+
need(Array.isArray(raw)&&raw.length>0&&raw.length<=REQUIREMENT_ITEM_LIMIT,code);
|
|
28
|
+
const items=raw.map(item=>{
|
|
29
|
+
need(item!==null&&typeof item==='object'&&!Array.isArray(item),code);
|
|
30
|
+
try{shape(item,['requirement','satisfied','evidence',...['run','result'].filter(key=>Object.hasOwn(item,key))]);}catch{need(false,code);}
|
|
31
|
+
need(typeof item.satisfied==='boolean',code);
|
|
32
|
+
boundedText(item.requirement,code);boundedText(item.evidence,code);
|
|
33
|
+
const run=item.run??null,result=item.result??null;
|
|
34
|
+
need(run===null||RUNS.has(run),code);need(result===null||RESULTS.has(result),code);
|
|
35
|
+
return freeze({requirement:item.requirement,satisfied:item.satisfied,evidence:item.evidence,
|
|
36
|
+
...(run===null?{}:{run}),...(result===null||run!=='baseline'?{}:{result})});
|
|
37
|
+
});
|
|
38
|
+
need(Buffer.byteLength(JSON.stringify(items),'utf8')<=REQUIREMENT_EVIDENCE_LIMIT,code);
|
|
39
|
+
return freeze(items);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// One handoff evidence entry, bound to the task identity and attempt it answers.
|
|
43
|
+
export function encodeRequirementEvidence({identity,items}){
|
|
44
|
+
validIdentity(identity);
|
|
45
|
+
const checked=readRequirementItems(items,'invalid_result');
|
|
46
|
+
need(checked.every(item=>item.satisfied===true),'invalid_result');
|
|
47
|
+
return REQUIREMENT_EVIDENCE_PREFIX+JSON.stringify({version:1,identity:{repositoryId:identity.repositoryId,
|
|
48
|
+
runId:identity.runId,taskId:identity.taskId,attempt:identity.attempt},
|
|
49
|
+
items:checked.map(item=>({requirement:item.requirement,evidence:item.evidence}))});
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function readRequirementEvidence(entry){
|
|
53
|
+
need(typeof entry==='string'&&entry.startsWith(REQUIREMENT_EVIDENCE_PREFIX),'invalid_input');
|
|
54
|
+
const body=entry.slice(REQUIREMENT_EVIDENCE_PREFIX.length);
|
|
55
|
+
need(Buffer.byteLength(body,'utf8')<=REQUIREMENT_EVIDENCE_LIMIT+4096,'invalid_input');
|
|
56
|
+
let parsed;
|
|
57
|
+
try{parsed=JSON.parse(body);}catch{need(false,'invalid_input');}
|
|
58
|
+
const value=json(parsed);
|
|
59
|
+
shape(value,['version','identity','items']);need(value.version===1,'invalid_input');
|
|
60
|
+
validIdentity(value.identity);
|
|
61
|
+
const items=arrayItems(value.items).map(item=>{shape(item,['requirement','evidence']);return {...item,satisfied:true};});
|
|
62
|
+
readRequirementItems(items,'invalid_input');
|
|
63
|
+
return value;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// The first rule a report breaks, naming only positions, fixed field names and
|
|
67
|
+
// rules, never the reported text, so the operator can act on an invalid report.
|
|
68
|
+
export function requirementReportProblem(raw){
|
|
69
|
+
if(!Array.isArray(raw))return 'not a list';
|
|
70
|
+
if(raw.length>REQUIREMENT_ITEM_LIMIT)return `more than ${REQUIREMENT_ITEM_LIMIT} items`;
|
|
71
|
+
const known=['requirement','satisfied','evidence','run','result'];
|
|
72
|
+
for(const [index,item] of raw.entries()){
|
|
73
|
+
const at=`item ${index+1}`;
|
|
74
|
+
if(item===null||typeof item!=='object'||Array.isArray(item))return `${at}: not an object`;
|
|
75
|
+
const extra=Object.keys(item).filter(key=>!known.includes(key));
|
|
76
|
+
if(extra.length)return `${at}: ${extra.length} unknown field(s)`;
|
|
77
|
+
for(const key of ['requirement','satisfied','evidence'])if(!Object.hasOwn(item,key))return `${at}: missing ${key}`;
|
|
78
|
+
if(typeof item.satisfied!=='boolean')return `${at}: satisfied is not a boolean`;
|
|
79
|
+
for(const key of ['requirement','evidence']){
|
|
80
|
+
const text=item[key];
|
|
81
|
+
if(typeof text!=='string'||!text.trim())return `${at}: ${key} is empty or not text`;
|
|
82
|
+
if(Buffer.byteLength(text,'utf8')>REQUIREMENT_TEXT_LIMIT)return `${at}: ${key} over ${REQUIREMENT_TEXT_LIMIT} bytes`;
|
|
83
|
+
if(/[\x00-\x08\x0b-\x1f]/.test(text))return `${at}: ${key} has a control character`;
|
|
84
|
+
}
|
|
85
|
+
const run=item.run??null,result=item.result??null;
|
|
86
|
+
if(run!==null&&!RUNS.has(run))return `${at}: run is not red, baseline or null`;
|
|
87
|
+
if(result!==null&&!RESULTS.has(result))return `${at}: result is not passed, failed or null`;
|
|
88
|
+
}
|
|
89
|
+
return Buffer.byteLength(JSON.stringify(raw),'utf8')>REQUIREMENT_EVIDENCE_LIMIT?'report over the byte limit':'unknown';
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// The developer's own per-requirement report travels beside, never inside, the
|
|
93
|
+
// journaled developer value, whose contract stays unchanged. {value, items}:
|
|
94
|
+
// items is the validated list, null when absent or empty, or 'invalid' (with the
|
|
95
|
+
// first broken rule as problem).
|
|
96
|
+
export function splitRequirementReport(value){
|
|
97
|
+
if(!value||typeof value!=='object'||Array.isArray(value)||!Object.hasOwn(value,'requirements'))return {value,items:null};
|
|
98
|
+
const {requirements,...rest}=value;
|
|
99
|
+
if(requirements===null||Array.isArray(requirements)&&requirements.length===0)return {value:rest,items:null};
|
|
100
|
+
try{return {value:rest,items:readRequirementItems(requirements,'invalid_result')};}
|
|
101
|
+
catch{return {value:rest,items:'invalid',problem:requirementReportProblem(requirements)};}
|
|
102
|
+
}
|
|
@@ -412,14 +412,17 @@ function snapshot(root,specsPath=null,projectPaths=null,retainedPaths=[],selecte
|
|
|
412
412
|
}
|
|
413
413
|
return files.sort((a,b)=>a.path<b.path?-1:a.path>b.path?1:0);
|
|
414
414
|
}
|
|
415
|
-
|
|
415
|
+
// Recovery source bindings opt into file type/mode; ordinary checks keep the
|
|
416
|
+
// historical path/content-only projection. Both use the same guarded snapshot.
|
|
417
|
+
export function captureReviewInventory(root,baseline,{withMode=false}={}){
|
|
416
418
|
validBaseline(baseline);
|
|
417
419
|
const stable=baseline.ignorePolicy?.version===2;
|
|
418
420
|
const policy=stable?stableSnapshotPolicy(root,baseline.ignorePolicy)
|
|
419
421
|
:Object.hasOwn(baseline,'ignorePolicy')?ignorePolicy(root):null;
|
|
420
422
|
return snapshot(root,baseline.specsPath??null,baseline.codeProjectPaths??null,
|
|
421
423
|
stable?[]:baseline.files.map(file=>file.path),new Set(),policy)
|
|
422
|
-
.map(file=>({path:file.path,sha256:file.sha256})
|
|
424
|
+
.map(file=>withMode?({path:file.path,type:file.type,mode:file.mode,sha256:file.sha256})
|
|
425
|
+
:({path:file.path,sha256:file.sha256}));
|
|
423
426
|
}
|
|
424
427
|
// withMode also reports permission-only changes; the mode is part of a reviewed
|
|
425
428
|
// file record. The default keeps the historical content-only comparison.
|
|
@@ -465,7 +468,7 @@ const sealed = (data,key) => {
|
|
|
465
468
|
};
|
|
466
469
|
export function captureReviewBaseline(options,legacy=false,policyVersion=2,baselinePolicy=null) {
|
|
467
470
|
const v=plain(options); keys(v,['root','identity','scope','requirements',
|
|
468
|
-
...['specsRoot','codeProjectPaths','bootstrapRequirements','specification','version'].filter(key=>Object.hasOwn(v,key))]); identityCheck(v.identity);
|
|
471
|
+
...['specsRoot','codeProjectPaths','bootstrapRequirements','specification','version','scopeRevisionPaths'].filter(key=>Object.hasOwn(v,key))]); identityCheck(v.identity);
|
|
469
472
|
const version=v.version??2;need([1,2].includes(version));
|
|
470
473
|
const bootstrap=Object.hasOwn(v,'bootstrapRequirements')?v.bootstrapRequirements:null;
|
|
471
474
|
if(bootstrap!==null){
|
|
@@ -501,7 +504,7 @@ export function captureReviewBaseline(options,legacy=false,policyVersion=2,basel
|
|
|
501
504
|
if(fs.existsSync(absolute)) need(fs.lstatSync(absolute).isFile(),'unsupported_file');
|
|
502
505
|
}
|
|
503
506
|
return sealed({version,kind:'cm-review-baseline',identity:v.identity,rootDigest:sha(root),
|
|
504
|
-
scope,requirements,files,...(specsPath===null?{}:{specsPath}),
|
|
507
|
+
scope,requirements,files,...(Object.hasOwn(v,'scopeRevisionPaths')?{scopeRevisionPaths:paths(v.scopeRevisionPaths)}:{}),...(specsPath===null?{}:{specsPath}),
|
|
505
508
|
...(projectPaths===null?{}:{codeProjectPaths:projectPaths}),
|
|
506
509
|
...(bootstrap===null?{}:{bootstrapRequirements:bootstrap}),
|
|
507
510
|
...(specification===null?{}:{specification,specificationRoot:v.specification.specsRoot}),
|
|
@@ -517,6 +520,70 @@ export function readReviewSourceFiles(root,selected){
|
|
|
517
520
|
});
|
|
518
521
|
return freeze(files);
|
|
519
522
|
}
|
|
523
|
+
export const readReviewScope=value=>paths(value);
|
|
524
|
+
// The same frozen exclusions govern source capture and pure journal replay.
|
|
525
|
+
// Pods can be missing from the creation inventory without a Git-ignore row;
|
|
526
|
+
// this narrow recovery API never opens a parent directory named Pods.
|
|
527
|
+
function scopeRevisionAdditions(b,scope){
|
|
528
|
+
need(b.version===2&&b.ignorePolicy?.version!==1,'scope_revision_unsupported_baseline');
|
|
529
|
+
const additions=paths(scope).filter(p=>!b.scope.includes(p));
|
|
530
|
+
const ignoredPaths=new Set((b.ignorePolicy?.ignoredPaths??[]).map(p=>p.toLowerCase()));
|
|
531
|
+
const ignoredDirectories=(b.ignorePolicy?.ignoredDirectories??[]).map(p=>p.toLowerCase());
|
|
532
|
+
for(const p of additions)need(!inDependencyDirectory(p)
|
|
533
|
+
&&!p.split('/').slice(0,-1).some(part=>part.toLowerCase()==='pods')
|
|
534
|
+
&&!p.split('/').some(part=>junkDirectories.has(part.toLowerCase()))&&!junkFile(p.split('/').at(-1))
|
|
535
|
+
&&!ignoredPaths.has(p.toLowerCase())
|
|
536
|
+
&&!ignoredDirectories.some(dir=>p.toLowerCase()===dir||p.toLowerCase().startsWith(dir+'/')),'excluded_snapshot_path');
|
|
537
|
+
return additions;
|
|
538
|
+
}
|
|
539
|
+
// Promote only newly approved sparse inventory entries to exact material.
|
|
540
|
+
// Existing business material and the original inventory hashes are preserved.
|
|
541
|
+
export function extendReviewBaseline(raw,scope,files){
|
|
542
|
+
const b=readReviewBaseline(raw),desired=paths(scope);
|
|
543
|
+
const additions=scopeRevisionAdditions(b,desired);
|
|
544
|
+
need(b.scope.filter(p=>p!=='AGENTS.md').every(p=>desired.includes(p)),'scope_revision_shrink');
|
|
545
|
+
need(Array.isArray(files)&&files.length<=additions.length,'invalid_input');
|
|
546
|
+
const supplied=new Map();
|
|
547
|
+
for(const file of files){fileRecord(file);need(additions.includes(file.path)&&!supplied.has(file.path),'invalid_input');supplied.set(file.path,file);}
|
|
548
|
+
const inventory=new Map(b.files.map(file=>[file.path,file]));
|
|
549
|
+
const aliases=new Set(b.files.map(file=>file.path.toLowerCase()));
|
|
550
|
+
for(const p of additions){
|
|
551
|
+
const old=inventory.get(p),material=supplied.get(p);
|
|
552
|
+
need(old!==undefined||!aliases.has(p.toLowerCase()),'unsupported_path');
|
|
553
|
+
if(old){
|
|
554
|
+
need(material!==undefined,'scope_revision_source_missing');
|
|
555
|
+
const {contentBase64:discard,...oldMeta}=old,{contentBase64:body,...newMeta}=material;
|
|
556
|
+
need(digest(oldMeta)===digest(newMeta),'scope_revision_source_changed');
|
|
557
|
+
inventory.set(p,material);
|
|
558
|
+
}else need(material===undefined,'scope_revision_source_changed');
|
|
559
|
+
}
|
|
560
|
+
const {baselineDigest,...data}=b;
|
|
561
|
+
return readReviewBaseline(sealed({...data,scope:[...new Set([...b.scope,...desired])].sort(),scopeRevisionPaths:additions.sort(),
|
|
562
|
+
files:[...inventory.values()].sort((a,b)=>a.path<b.path?-1:a.path>b.path?1:0)},'baselineDigest'));
|
|
563
|
+
}
|
|
564
|
+
// New paths must be absent now; existing additions must still match the
|
|
565
|
+
// creation inventory. Every parent is checked before a no-follow file read.
|
|
566
|
+
export function captureScopeRevisionSources(root,baseline,scope){
|
|
567
|
+
const b=readReviewBaseline(baseline),real=rootPath(root),files=[];
|
|
568
|
+
const additions=scopeRevisionAdditions(b,scope);
|
|
569
|
+
need(sha(real)===b.rootDigest,'invalid_baseline');
|
|
570
|
+
for(const p of additions){
|
|
571
|
+
let parent=real;
|
|
572
|
+
for(const part of p.split('/').slice(0,-1)){
|
|
573
|
+
parent=path.join(parent,part);
|
|
574
|
+
try{const stat=fs.lstatSync(parent);need(stat.isDirectory()&&!stat.isSymbolicLink()&&fs.realpathSync(parent)===parent,'unsupported_path');}
|
|
575
|
+
catch(error){if(error.code==='ENOENT')break;throw error;}
|
|
576
|
+
}
|
|
577
|
+
const old=b.files.find(file=>file.path===p);
|
|
578
|
+
if(old)files.push(readFile(real,p));
|
|
579
|
+
else{
|
|
580
|
+
try{fs.lstatSync(path.join(real,p));need(false,'scope_revision_source_changed');}
|
|
581
|
+
catch(error){if(error.code!=='ENOENT')throw error;}
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
extendReviewBaseline(b,scope,files);
|
|
585
|
+
return freeze(files);
|
|
586
|
+
}
|
|
520
587
|
function sourceParent(root,p){
|
|
521
588
|
let parent=root;
|
|
522
589
|
for(const part of p.split('/').slice(0,-1)){
|
|
@@ -610,7 +677,7 @@ export function readReviewSourceRecords(raw){
|
|
|
610
677
|
function validBaseline(b) {
|
|
611
678
|
try {
|
|
612
679
|
keys(b,['version','kind','identity','rootDigest','scope','requirements','files','baselineDigest',
|
|
613
|
-
...['specsPath','codeProjectPaths','bootstrapRequirements','specification','specificationRoot','ignorePolicy'].filter(key=>Object.hasOwn(b,key))]);
|
|
680
|
+
...['specsPath','codeProjectPaths','bootstrapRequirements','specification','specificationRoot','ignorePolicy','scopeRevisionPaths'].filter(key=>Object.hasOwn(b,key))]);
|
|
614
681
|
need([1,2].includes(b.version) && b.kind==='cm-review-baseline'); identityCheck(b.identity); hex(b.rootDigest);
|
|
615
682
|
if(Object.hasOwn(b,'ignorePolicy')){
|
|
616
683
|
if(b.ignorePolicy.version===2)validateStablePolicy(b.ignorePolicy);
|
|
@@ -631,6 +698,10 @@ function validBaseline(b) {
|
|
|
631
698
|
}
|
|
632
699
|
const bootstrap=Object.hasOwn(b,'bootstrapRequirements');if(bootstrap)validBootstrapRequirements(b.bootstrapRequirements);
|
|
633
700
|
need(digest(b.scope)===digest(paths(b.scope)) && digest(b.requirements)===digest(requirementPaths(b.requirements,bootstrap||Object.hasOwn(b,'specification'))));
|
|
701
|
+
if(Object.hasOwn(b,'scopeRevisionPaths')){
|
|
702
|
+
need(digest(b.scopeRevisionPaths)===digest(paths(b.scopeRevisionPaths))
|
|
703
|
+
&&b.scopeRevisionPaths.every(p=>b.scope.includes(p)),'invalid_baseline');
|
|
704
|
+
}
|
|
634
705
|
if(b.version===2)inventoryList(b);
|
|
635
706
|
else if(!bootstrap||b.files.length)fileList(b.files);else need(Array.isArray(b.files));
|
|
636
707
|
need(b.requirements.every(p=>b.files.some(f=>f.path===p)));
|
|
@@ -724,7 +795,7 @@ function assemblePackage({b,stable,policy,specification,bootstrap},files,checks,
|
|
|
724
795
|
need(changes.length>0,'empty_changes');
|
|
725
796
|
const changedPaths=new Set(changes.map(change=>change.path));
|
|
726
797
|
const unchangedScope=b.scope.filter(p=>!changedPaths.has(p)&&after.has(p))
|
|
727
|
-
.map(p=>({path:p,sha256:after.get(p).sha256}));
|
|
798
|
+
.map(p=>b.scopeRevisionPaths?.includes(p)?after.get(p):({path:p,sha256:after.get(p).sha256}));
|
|
728
799
|
const requirements=b.requirements.map(p=>{ need(after.has(p),'read_failed'); return after.get(p); });
|
|
729
800
|
const pkg=sealed({version:1,kind:'cm-review-package',identity:b.identity,rootDigest:b.rootDigest,
|
|
730
801
|
baseIdentity:b.baselineDigest,scope:b.scope,changes,unchangedScope,requirements,checks,
|
|
@@ -815,7 +886,8 @@ function validPackage(p) {
|
|
|
815
886
|
if(Object.hasOwn(p,'unchangedScope')){
|
|
816
887
|
need(Array.isArray(p.unchangedScope)&&p.unchangedScope.length<=FILE_COUNT);
|
|
817
888
|
for(const file of p.unchangedScope){
|
|
818
|
-
|
|
889
|
+
if(Object.hasOwn(file,'contentBase64'))fileRecord(file);
|
|
890
|
+
else{keys(file,['path','sha256']);filePath(file.path);hex(file.sha256);}
|
|
819
891
|
need(p.scope.includes(file.path)&&!names.includes(file.path));
|
|
820
892
|
for(const material of [...p.requirements,...(p.instructions??[])])
|
|
821
893
|
if(material.path===file.path)need(material.sha256===file.sha256);
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
// Prompt-only, lossless V2. Never used by the V1 journal storage codec.
|
|
2
|
+
import {createHash} from 'node:crypto';
|
|
3
|
+
import {isDeepStrictEqual} from 'node:util';
|
|
4
|
+
import {compactReviewData,expandReviewData} from './review-presentation.mjs';
|
|
5
|
+
import {digest,json,need,shape,hex} from './effect-contract.mjs';
|
|
6
|
+
|
|
7
|
+
const CODE='review_presentation_delta_invalid',INPUT_LIMIT=10*1024*1024,PHYSICAL_LIMIT=11*1024*1024;
|
|
8
|
+
const ENTRY_PARTS=1024,TOTAL_PARTS=4096,LINE_LIMIT=65536;
|
|
9
|
+
const size=value=>Buffer.byteLength(JSON.stringify(value),'utf8');
|
|
10
|
+
const sha=bytes=>createHash('sha256').update(bytes).digest('hex');
|
|
11
|
+
const integer=value=>Number.isSafeInteger(value)&&value>=0;
|
|
12
|
+
function literal(bytes){
|
|
13
|
+
let text;try{text=new TextDecoder('utf8',{fatal:true}).decode(bytes);}catch{}
|
|
14
|
+
return text!==undefined&&Buffer.from(text,'utf8').equals(bytes)
|
|
15
|
+
?{encoding:'utf8',content:text}:{encoding:'base64',content:bytes.toString('base64')};
|
|
16
|
+
}
|
|
17
|
+
function literalBytes(item){
|
|
18
|
+
shape(item,['encoding','content']);
|
|
19
|
+
need(['utf8','base64'].includes(item.encoding)&&typeof item.content==='string',CODE);
|
|
20
|
+
const bytes=Buffer.from(item.content,item.encoding==='utf8'?'utf8':'base64');
|
|
21
|
+
need(isDeepStrictEqual(literal(bytes),item),CODE);return bytes;
|
|
22
|
+
}
|
|
23
|
+
function pairBases(data){
|
|
24
|
+
const pairs=new Map();
|
|
25
|
+
for(const change of data.reviewPackage?.changes??[]){
|
|
26
|
+
const before=change?.before,after=change?.after;
|
|
27
|
+
if(!before||!after||typeof change.path!=='string'||before.path!==change.path||after.path!==change.path
|
|
28
|
+
||typeof before.contentRef!=='string'||typeof after.contentRef!=='string'||before.contentRef===after.contentRef)continue;
|
|
29
|
+
const bases=pairs.get(after.contentRef)??new Set();bases.add(before.contentRef);pairs.set(after.contentRef,bases);
|
|
30
|
+
}
|
|
31
|
+
return pairs;
|
|
32
|
+
}
|
|
33
|
+
function prefixSuffix(base,target){
|
|
34
|
+
const limit=Math.min(base.length,target.length);let prefix=0,suffix=0;
|
|
35
|
+
while(prefix<limit&&base[prefix]===target[prefix])prefix++;
|
|
36
|
+
while(suffix<limit-prefix&&base[base.length-suffix-1]===target[target.length-suffix-1])suffix++;
|
|
37
|
+
return [...(prefix?[{copy:{offset:0,size:prefix}}]:[]),
|
|
38
|
+
...(target.length>prefix+suffix?[{insert:literal(target.subarray(prefix,target.length-suffix))}]:[]),
|
|
39
|
+
...(suffix?[{copy:{offset:base.length-suffix,size:suffix}}]:[])];
|
|
40
|
+
}
|
|
41
|
+
function lines(bytes){
|
|
42
|
+
const result=[];let start=0;
|
|
43
|
+
while(start<bytes.length){
|
|
44
|
+
if(result.length>=LINE_LIMIT)return null;
|
|
45
|
+
const newline=bytes.indexOf(10,start),end=newline<0?bytes.length:newline+1;
|
|
46
|
+
result.push([start,end]);start=end;
|
|
47
|
+
}
|
|
48
|
+
return result;
|
|
49
|
+
}
|
|
50
|
+
function anchoredParts(base,target){
|
|
51
|
+
const oldLines=lines(base),newLines=lines(target);if(!oldLines||!newLines)return null;
|
|
52
|
+
const anchors=new Map();
|
|
53
|
+
for(const [start,end] of oldLines){
|
|
54
|
+
if(end-start<32)continue;
|
|
55
|
+
const key=base.subarray(start,end).toString('base64');
|
|
56
|
+
anchors.set(key,anchors.has(key)?null:start);
|
|
57
|
+
}
|
|
58
|
+
const parts=[];let insertStart=0,previousEnd=null,lastCopyEnd=0;
|
|
59
|
+
for(const [start,end] of newLines){
|
|
60
|
+
const bytes=target.subarray(start,end);
|
|
61
|
+
const offset=previousEnd!==null&&base.subarray(previousEnd,previousEnd+bytes.length).equals(bytes)
|
|
62
|
+
?previousEnd:anchors.get(bytes.toString('base64'));
|
|
63
|
+
if(offset===undefined||offset===null||offset<lastCopyEnd){previousEnd=null;continue;}
|
|
64
|
+
if(start>insertStart)parts.push({insert:literal(target.subarray(insertStart,start))});
|
|
65
|
+
const last=parts.at(-1);
|
|
66
|
+
if(last?.copy&&last.copy.offset+last.copy.size===offset)last.copy.size+=bytes.length;
|
|
67
|
+
else parts.push({copy:{offset,size:bytes.length}});
|
|
68
|
+
if(parts.length>ENTRY_PARTS)return null;
|
|
69
|
+
insertStart=end;previousEnd=offset+bytes.length;lastCopyEnd=previousEnd;
|
|
70
|
+
}
|
|
71
|
+
if(insertStart<target.length)parts.push({insert:literal(target.subarray(insertStart))});
|
|
72
|
+
return parts.length<=ENTRY_PARTS?parts:null;
|
|
73
|
+
}
|
|
74
|
+
export function compactReviewDataV2(raw){
|
|
75
|
+
const input=json(raw,INPUT_LIMIT),v1=compactReviewData(input),pairs=pairBases(v1),contents={};
|
|
76
|
+
let partCount=0;
|
|
77
|
+
for(const [name,entry] of Object.entries(v1.contents)){
|
|
78
|
+
let selected=entry;
|
|
79
|
+
// One same-path pair, never an all-contents similarity search. The base
|
|
80
|
+
// must already be a full table entry; delta chains are not produced.
|
|
81
|
+
const baseRef=[...(pairs.get(name)??[])].find(ref=>Object.hasOwn(contents,ref)&&contents[ref].encoding!=='delta-v2');
|
|
82
|
+
if(baseRef){
|
|
83
|
+
const base=literalBytes({encoding:contents[baseRef].encoding,content:contents[baseRef].content});
|
|
84
|
+
const target=literalBytes({encoding:entry.encoding,content:entry.content});
|
|
85
|
+
for(const parts of [prefixSuffix(base,target),anchoredParts(base,target)]){
|
|
86
|
+
if(!parts||parts.length>ENTRY_PARTS||partCount+parts.length>TOTAL_PARTS||!parts.some(part=>part.copy))continue;
|
|
87
|
+
const candidate={sha256:entry.sha256,size:entry.size,encoding:'delta-v2',baseRef,parts};
|
|
88
|
+
if(size(candidate)<size(selected))selected=candidate;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
contents[name]=selected;if(selected.encoding==='delta-v2')partCount+=selected.parts.length;
|
|
92
|
+
}
|
|
93
|
+
const {presentation,...data}=v1;
|
|
94
|
+
const result={presentation:{version:2,kind:'complete-inline-content-delta',packageDigest:presentation.packageDigest,
|
|
95
|
+
logicalBytes:size(input),logicalDigest:digest(input)},...data,contents};
|
|
96
|
+
need(isDeepStrictEqual(expandReviewDataV2(result),input),CODE);return result;
|
|
97
|
+
}
|
|
98
|
+
export function expandReviewDataV2(raw){
|
|
99
|
+
const value=json(raw,PHYSICAL_LIMIT),{presentation,contents,...data}=value;
|
|
100
|
+
shape(presentation,['version','kind','packageDigest','logicalBytes','logicalDigest']);
|
|
101
|
+
need(presentation.version===2&&presentation.kind==='complete-inline-content-delta'
|
|
102
|
+
&&integer(presentation.logicalBytes)&&presentation.logicalBytes<=INPUT_LIMIT, CODE);
|
|
103
|
+
hex(presentation.packageDigest);hex(presentation.logicalDigest);
|
|
104
|
+
need(data.reviewPackage?.packageDigest===presentation.packageDigest
|
|
105
|
+
&&contents!==null&&typeof contents==='object'&&!Array.isArray(contents),CODE);
|
|
106
|
+
const pairs=pairBases(data),plans=new Map(),used=new Set();let totalBytes=0,partCount=0;
|
|
107
|
+
for(const [index,[name,entry]] of Object.entries(contents).entries()){
|
|
108
|
+
need(name==='content-'+(index+1)&&entry!==null&&typeof entry==='object'&&!Array.isArray(entry),CODE);
|
|
109
|
+
hex(entry.sha256);need(integer(entry.size)&&entry.size<=INPUT_LIMIT,CODE);
|
|
110
|
+
totalBytes+=entry.size;need(totalBytes<=INPUT_LIMIT,'limit_exceeded');
|
|
111
|
+
if(entry.encoding!=='delta-v2'){
|
|
112
|
+
shape(entry,['sha256','size','encoding','content']);
|
|
113
|
+
const bytes=literalBytes({encoding:entry.encoding,content:entry.content});
|
|
114
|
+
need(bytes.length===entry.size&&sha(bytes)===entry.sha256,CODE);plans.set(name,{bytes});continue;
|
|
115
|
+
}
|
|
116
|
+
shape(entry,['sha256','size','encoding','baseRef','parts']);
|
|
117
|
+
// Only backward references to complete entries from this same path pair.
|
|
118
|
+
const base=plans.get(entry.baseRef);
|
|
119
|
+
need(base?.bytes&&pairs.get(name)?.has(entry.baseRef)&&Array.isArray(entry.parts)
|
|
120
|
+
&&entry.parts.length>0&&entry.parts.length<=ENTRY_PARTS,CODE);
|
|
121
|
+
partCount+=entry.parts.length;need(partCount<=TOTAL_PARTS,CODE);
|
|
122
|
+
let length=0,lastCopyEnd=0,copies=0;const segments=[];
|
|
123
|
+
for(const part of entry.parts){
|
|
124
|
+
need(part!==null&&typeof part==='object'&&!Array.isArray(part),CODE);
|
|
125
|
+
if(Object.hasOwn(part,'copy')){
|
|
126
|
+
shape(part,['copy']);shape(part.copy,['offset','size']);const {offset,size}=part.copy;
|
|
127
|
+
need(integer(offset)&&integer(size)&&size>0&&offset>=lastCopyEnd&&offset+size<=base.bytes.length,CODE);
|
|
128
|
+
lastCopyEnd=offset+size;length+=size;copies++;segments.push({offset,size});
|
|
129
|
+
}else{
|
|
130
|
+
shape(part,['insert']);const bytes=literalBytes(part.insert);need(bytes.length>0,CODE);
|
|
131
|
+
length+=bytes.length;segments.push({bytes});
|
|
132
|
+
}
|
|
133
|
+
need(length<=entry.size,CODE);
|
|
134
|
+
}
|
|
135
|
+
need(length===entry.size&&copies>0,CODE);plans.set(name,{base:base.bytes,segments});
|
|
136
|
+
}
|
|
137
|
+
// Measure every expanded occurrence before allocating reconstructed files or
|
|
138
|
+
// base64 strings. A small repeated reference must not amplify past the cap.
|
|
139
|
+
const measure=node=>{
|
|
140
|
+
if(node===null||typeof node!=='object')return size(node);
|
|
141
|
+
if(Array.isArray(node))return 2+Math.max(0,node.length-1)+node.reduce((n,item)=>n+measure(item),0);
|
|
142
|
+
const ref=Object.hasOwn(node,'contentRef'),entry=ref?contents[node.contentRef]:null;
|
|
143
|
+
if(ref){
|
|
144
|
+
need(typeof node.contentRef==='string'&&Object.hasOwn(contents,node.contentRef)&&!Object.hasOwn(node,'contentBase64'),CODE);
|
|
145
|
+
if(Object.hasOwn(node,'sha256'))need(node.sha256===entry.sha256,CODE);
|
|
146
|
+
if(Object.hasOwn(node,'size'))need(node.size===entry.size,CODE);
|
|
147
|
+
used.add(node.contentRef);
|
|
148
|
+
}
|
|
149
|
+
const entries=Object.entries(node).filter(([key])=>!ref||key!=='contentRef');
|
|
150
|
+
const measured=2+Math.max(0,entries.length+(ref?1:0)-1)
|
|
151
|
+
+entries.reduce((n,[key,item])=>n+size(key)+1+measure(item),0)
|
|
152
|
+
+(ref?size('contentBase64')+3+4*Math.ceil(entry.size/3):0);
|
|
153
|
+
need(measured<=INPUT_LIMIT,'limit_exceeded');return measured;
|
|
154
|
+
};
|
|
155
|
+
need(measure(data)===presentation.logicalBytes&&used.size===plans.size,CODE);
|
|
156
|
+
const expandedContents={};
|
|
157
|
+
for(const [name,plan] of plans){
|
|
158
|
+
const entry=contents[name];
|
|
159
|
+
const bytes=plan.bytes??Buffer.concat(plan.segments.map(part=>part.bytes??plan.base.subarray(part.offset,part.offset+part.size)),entry.size);
|
|
160
|
+
need(bytes.length===entry.size&&sha(bytes)===entry.sha256,CODE);
|
|
161
|
+
expandedContents[name]={sha256:entry.sha256,size:entry.size,...literal(bytes)};
|
|
162
|
+
}
|
|
163
|
+
const expanded=expandReviewData({presentation:{version:1,kind:'complete-inline-content-table',packageDigest:presentation.packageDigest},...data,contents:expandedContents});
|
|
164
|
+
need(size(expanded)===presentation.logicalBytes&&digest(expanded)===presentation.logicalDigest,CODE);
|
|
165
|
+
return expanded;
|
|
166
|
+
}
|
|
@@ -1,15 +1,21 @@
|
|
|
1
1
|
// Lossless presentation inside the same no-tools prompt. Canonical evidence stays unchanged.
|
|
2
2
|
import {createHash} from 'node:crypto';
|
|
3
|
-
import {json,need} from './effect-contract.mjs';
|
|
3
|
+
import {json,need,shape,hex,digest,validIdentity} from './effect-contract.mjs';
|
|
4
|
+
import {readReviewPackage} from './review-package.mjs';
|
|
4
5
|
export function compactReviewData(raw){
|
|
5
|
-
const input=json(raw,10*1024*1024)
|
|
6
|
+
const input=json(raw,10*1024*1024);
|
|
6
7
|
need(!Object.hasOwn(input,'presentation')&&!Object.hasOwn(input,'contents')&&input.reviewPackage?.packageDigest,'review_presentation_invalid');
|
|
7
|
-
|
|
8
|
-
|
|
8
|
+
return compactInlineContent(input,input.reviewPackage.packageDigest);
|
|
9
|
+
}
|
|
10
|
+
function compactInlineContent(input,packageDigest,selected=null){
|
|
11
|
+
const contents={},keys=new Map();
|
|
12
|
+
const visit=(value,route=[])=>{
|
|
13
|
+
if(Array.isArray(value))return value.map((item,i)=>visit(item,[...route,String(i)]));
|
|
9
14
|
if(value===null||typeof value!=='object')return value;
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
15
|
+
const eligible=selected===null||inPackage(route,selected);
|
|
16
|
+
if(eligible)need(!Object.hasOwn(value,'contentRef'),'review_presentation_invalid');
|
|
17
|
+
const out=Object.fromEntries(Object.entries(value).map(([key,item])=>[key,visit(item,[...route,key])]));
|
|
18
|
+
if(!eligible||typeof value.contentBase64!=='string')return out;
|
|
13
19
|
const bytes=Buffer.from(value.contentBase64,'base64');
|
|
14
20
|
need(bytes.toString('base64')===value.contentBase64,'review_presentation_invalid');
|
|
15
21
|
const hash=createHash('sha256').update(bytes).digest('hex');
|
|
@@ -26,7 +32,7 @@ export function compactReviewData(raw){
|
|
|
26
32
|
delete out.contentBase64;out.contentRef=ref;return out;
|
|
27
33
|
};
|
|
28
34
|
return {presentation:{version:1,kind:'complete-inline-content-table',
|
|
29
|
-
packageDigest
|
|
35
|
+
packageDigest},...visit(input),contents};
|
|
30
36
|
}
|
|
31
37
|
export function expandReviewData(raw){
|
|
32
38
|
const {presentation,contents,...data}=raw;
|
|
@@ -43,3 +49,166 @@ export function expandReviewData(raw){
|
|
|
43
49
|
};
|
|
44
50
|
return visit(data);
|
|
45
51
|
}
|
|
52
|
+
|
|
53
|
+
// Physical storage only, for the existing standalone fix owner. The state marker
|
|
54
|
+
// explicitly selects encoded sequences: a legacy small lookalike keeps its meaning.
|
|
55
|
+
const FIX_WORKFLOW='cm-fix-stages-v1',PAYLOAD_LIMIT=1024*1024,INPUT_LIMIT=10*1024*1024,STATE_LIMIT=16*1024*1024;
|
|
56
|
+
const STORAGE_KIND='cm-fix-review-payloads-v1',PAYLOAD_KIND='cm-fix-journal-review-payload';
|
|
57
|
+
const storageCode='review_storage_invalid';
|
|
58
|
+
const sizeOf=value=>Buffer.byteLength(JSON.stringify(value));
|
|
59
|
+
const sameBytes=(a,b)=>JSON.stringify(a)===JSON.stringify(b);
|
|
60
|
+
const object=value=>value!==null&&typeof value==='object'&&!Array.isArray(value);
|
|
61
|
+
export function fixJournalWorkflow(state){
|
|
62
|
+
const first=state.records?.[0];
|
|
63
|
+
return first?.id==='fix-configuration'&&first.kind==='intent'&&first.payload?.workflow===FIX_WORKFLOW
|
|
64
|
+
&&state.fingerprints?.workflow===digest(FIX_WORKFLOW)?FIX_WORKFLOW:null;
|
|
65
|
+
}
|
|
66
|
+
const inPackage=(route,paths)=>paths.some(p=>route.length>=p.length&&p.every((part,i)=>route[i]===part));
|
|
67
|
+
// These are the actual durable fix-owner payload forms. This is not an arbitrary
|
|
68
|
+
// recursive search for a token package hidden inside unrelated application data.
|
|
69
|
+
function packageLocations(value,{validate=true}={}){
|
|
70
|
+
need(object(value),storageCode);let locations;
|
|
71
|
+
if(value.kind==='cm-review-package')locations=[[]];
|
|
72
|
+
else if(Object.hasOwn(value,'writeback')&&Object.hasOwn(value,'reviewPackage')){
|
|
73
|
+
shape(value,['writeback','reviewPackage']);const w=value.writeback;
|
|
74
|
+
shape(w,['identity','learningDigest','retrospectiveDigest','packageDigest','outcome','changed','agentsFile','reason','completionEligible']);
|
|
75
|
+
validIdentity(w.identity);[w.learningDigest,w.retrospectiveDigest,w.packageDigest].forEach(hex);
|
|
76
|
+
need(['written','deduplicated'].includes(w.outcome)&&w.changed===(w.outcome==='written')
|
|
77
|
+
&&w.reason===null&&w.completionEligible===false,storageCode);
|
|
78
|
+
shape(w.agentsFile,['scope','path','sha256']);
|
|
79
|
+
need(w.agentsFile.scope==='project'&&w.agentsFile.path==='AGENTS.md',storageCode);hex(w.agentsFile.sha256);
|
|
80
|
+
locations=[['reviewPackage']];
|
|
81
|
+
}else if(Object.hasOwn(value,'request')){
|
|
82
|
+
shape(value,['request','authorizationAt','registeredAt','grant']);
|
|
83
|
+
const r=value.request;
|
|
84
|
+
shape(r,['version','invocationId','identity','role','provider','requestedModel','contextId','payload','requestDigest']);
|
|
85
|
+
validIdentity(r.identity);hex(r.requestDigest);shape(r.payload,['reviewPackage','priorReview']);
|
|
86
|
+
need(r.version===1&&r.role==='reviewer'&&typeof r.invocationId==='string'&&r.invocationId.startsWith('fix-final.')
|
|
87
|
+
&&['codex','claude'].includes(r.provider)&&typeof r.requestedModel==='string'&&r.requestedModel.trim()
|
|
88
|
+
&&typeof r.contextId==='string'&&r.contextId.trim()&&object(value.grant)
|
|
89
|
+
&&Number.isSafeInteger(value.authorizationAt)&&value.authorizationAt>=0
|
|
90
|
+
&&Number.isSafeInteger(value.registeredAt)&&value.registeredAt>=value.authorizationAt,storageCode);
|
|
91
|
+
if(validate){const {requestDigest,...body}=r;need(digest(body)===requestDigest,storageCode);}
|
|
92
|
+
locations=[['request','payload','reviewPackage']];
|
|
93
|
+
}else need(false,storageCode);
|
|
94
|
+
const packages=locations.map(route=>route.reduce((node,key)=>node[key],value));
|
|
95
|
+
need(packages.every(pkg=>object(pkg)&&pkg.kind==='cm-review-package'),storageCode);
|
|
96
|
+
if(validate){
|
|
97
|
+
packages.forEach(readReviewPackage);
|
|
98
|
+
const identity=value.request?.identity??value.writeback?.identity;
|
|
99
|
+
if(identity)need(packages.every(pkg=>digest(pkg.identity)===digest(identity)),storageCode);
|
|
100
|
+
}
|
|
101
|
+
return {paths:locations,packages};
|
|
102
|
+
}
|
|
103
|
+
export function packFixJournalPayload(raw,{workflow}={}){
|
|
104
|
+
const value=json(raw,INPUT_LIMIT),logicalBytes=sizeOf(value);
|
|
105
|
+
if(logicalBytes<=PAYLOAD_LIMIT)return value;
|
|
106
|
+
need(workflow===FIX_WORKFLOW,'limit_exceeded');
|
|
107
|
+
const {paths,packages}=packageLocations(value);
|
|
108
|
+
// No duplicated anchor package in the temporary input: the canonical package
|
|
109
|
+
// already inside payload supplies the presentation digest to the same visitor.
|
|
110
|
+
const data=compactInlineContent({payload:value},packages[0].packageDigest,paths.map(p=>['payload',...p]));
|
|
111
|
+
const packed={version:1,kind:PAYLOAD_KIND,logicalBytes,logicalDigest:digest(value),data};
|
|
112
|
+
return json(packed,PAYLOAD_LIMIT);
|
|
113
|
+
}
|
|
114
|
+
function payloadPlan(raw,{workflow}={}){
|
|
115
|
+
need(workflow===FIX_WORKFLOW,storageCode);
|
|
116
|
+
const value=json(raw,PAYLOAD_LIMIT);
|
|
117
|
+
shape(value,['version','kind','logicalBytes','logicalDigest','data']);
|
|
118
|
+
need(value.version===1&&value.kind===PAYLOAD_KIND&&Number.isSafeInteger(value.logicalBytes)
|
|
119
|
+
&&value.logicalBytes>PAYLOAD_LIMIT&&value.logicalBytes<=INPUT_LIMIT,storageCode);hex(value.logicalDigest);
|
|
120
|
+
const data=value.data;shape(data,['presentation','payload','contents']);
|
|
121
|
+
shape(data.presentation,['version','kind','packageDigest']);
|
|
122
|
+
need(data.presentation.version===1&&data.presentation.kind==='complete-inline-content-table',storageCode);hex(data.presentation.packageDigest);
|
|
123
|
+
need(object(data.contents),storageCode);
|
|
124
|
+
const {paths}=packageLocations(data.payload,{validate:false});
|
|
125
|
+
const names=Object.keys(data.contents),lengths=new Map(),used=new Set();
|
|
126
|
+
for(const [index,name] of names.entries()){
|
|
127
|
+
need(name==='content-'+(index+1),storageCode);
|
|
128
|
+
const item=data.contents[name];shape(item,['sha256','size','encoding','content']);hex(item.sha256);
|
|
129
|
+
need(Number.isSafeInteger(item.size)&&item.size>=0&&item.size<=INPUT_LIMIT
|
|
130
|
+
&&['utf8','base64'].includes(item.encoding)&&typeof item.content==='string',storageCode);
|
|
131
|
+
// Table bytes are bounded by the physical payload, independently of the
|
|
132
|
+
// claimed size. Never allocate from an attacker-supplied size field.
|
|
133
|
+
const bytes=Buffer.from(item.content,item.encoding==='utf8'?'utf8':'base64');
|
|
134
|
+
need(bytes.length===item.size&&(item.encoding!=='base64'||bytes.toString('base64')===item.content)
|
|
135
|
+
&&createHash('sha256').update(bytes).digest('hex')===item.sha256,storageCode);
|
|
136
|
+
lengths.set(name,4*Math.ceil(bytes.length/3));
|
|
137
|
+
}
|
|
138
|
+
let nodes=0;
|
|
139
|
+
const measure=(node,depth=0,route=[])=>{
|
|
140
|
+
need(depth<=40&&++nodes<=PAYLOAD_LIMIT,storageCode);
|
|
141
|
+
if(node===null||typeof node!=='object')return sizeOf(node);
|
|
142
|
+
if(Array.isArray(node))return 2+Math.max(0,node.length-1)+node.reduce((n,item,i)=>n+measure(item,depth+1,[...route,String(i)]),0);
|
|
143
|
+
const reference=inPackage(route,paths)&&Object.hasOwn(node,'contentRef');
|
|
144
|
+
if(reference)need(typeof node.contentRef==='string'&&lengths.has(node.contentRef)
|
|
145
|
+
&&!Object.hasOwn(node,'contentBase64'),storageCode);
|
|
146
|
+
const entries=Object.entries(node).filter(([key])=>!reference||key!=='contentRef');
|
|
147
|
+
let bytes=2+Math.max(0,entries.length+(reference?1:0)-1);
|
|
148
|
+
for(const [key,item] of entries)bytes+=sizeOf(key)+1+measure(item,depth+1,[...route,key]);
|
|
149
|
+
if(reference){used.add(node.contentRef);bytes+=sizeOf('contentBase64')+1+2+lengths.get(node.contentRef);}
|
|
150
|
+
need(bytes<=INPUT_LIMIT,'limit_exceeded');return bytes;
|
|
151
|
+
};
|
|
152
|
+
const logicalBytes=measure(data.payload);
|
|
153
|
+
need(logicalBytes===value.logicalBytes&&used.size===names.length,storageCode);
|
|
154
|
+
return {value,logicalBytes,paths};
|
|
155
|
+
}
|
|
156
|
+
export function unpackFixJournalPayload(raw,{workflow}={}){
|
|
157
|
+
const physical=json(raw,PAYLOAD_LIMIT);
|
|
158
|
+
if(!object(physical)||physical.kind!==PAYLOAD_KIND)return physical;
|
|
159
|
+
const {value,paths}=payloadPlan(physical,{workflow});
|
|
160
|
+
const visit=(node,route=[])=>{
|
|
161
|
+
if(Array.isArray(node))return node.map((item,i)=>visit(item,[...route,String(i)]));
|
|
162
|
+
if(node===null||typeof node!=='object')return node;
|
|
163
|
+
const out=Object.fromEntries(Object.entries(node).map(([key,item])=>[key,visit(item,[...route,key])]));
|
|
164
|
+
if(inPackage(route,paths)&&Object.hasOwn(node,'contentRef')){
|
|
165
|
+
const entry=value.data.contents[node.contentRef];
|
|
166
|
+
delete out.contentRef;out.contentBase64=Buffer.from(entry.content,entry.encoding==='utf8'?'utf8':'base64').toString('base64');
|
|
167
|
+
}
|
|
168
|
+
return out;
|
|
169
|
+
};
|
|
170
|
+
const payload=json(visit(value.data.payload),INPUT_LIMIT),{packages}=packageLocations(payload);
|
|
171
|
+
need(sizeOf(payload)===value.logicalBytes&&digest(payload)===value.logicalDigest
|
|
172
|
+
&&packages[0].packageDigest===value.data.presentation.packageDigest,storageCode);
|
|
173
|
+
// Exact fixed point rejects alternate encodings, extra tables and collisions.
|
|
174
|
+
need(sameBytes(packFixJournalPayload(payload,{workflow}),value),storageCode);return payload;
|
|
175
|
+
}
|
|
176
|
+
export function packFixJournalState(raw){
|
|
177
|
+
const state=json(raw,STATE_LIMIT);need(!Object.hasOwn(state,'storageEncoding'),storageCode);
|
|
178
|
+
const workflow=fixJournalWorkflow(state),sequences=[];
|
|
179
|
+
const records=state.records.map(record=>{
|
|
180
|
+
const payload=packFixJournalPayload(record.payload,{workflow});
|
|
181
|
+
if(sizeOf(record.payload)>PAYLOAD_LIMIT)sequences.push(record.seq);
|
|
182
|
+
return {...record,payload};
|
|
183
|
+
});
|
|
184
|
+
return json({...state,records,...(sequences.length?{storageEncoding:{version:1,kind:STORAGE_KIND,records:sequences}}:{})},STATE_LIMIT);
|
|
185
|
+
}
|
|
186
|
+
export function unpackFixJournalState(raw){
|
|
187
|
+
const physical=json(raw,STATE_LIMIT);
|
|
188
|
+
if(!Object.hasOwn(physical,'storageEncoding')){
|
|
189
|
+
need(Array.isArray(physical.records),storageCode);
|
|
190
|
+
physical.records.forEach(record=>json(record.payload,PAYLOAD_LIMIT));
|
|
191
|
+
return physical;
|
|
192
|
+
}
|
|
193
|
+
shape(physical,['version','identity','fingerprints','records','revision','storageEncoding']);
|
|
194
|
+
const workflow=fixJournalWorkflow(physical);need(workflow===FIX_WORKFLOW,storageCode);
|
|
195
|
+
const marker=physical.storageEncoding;shape(marker,['version','kind','records']);
|
|
196
|
+
need(marker.version===1&&marker.kind===STORAGE_KIND&&Array.isArray(physical.records)
|
|
197
|
+
&&physical.records.length<=1024&&Array.isArray(marker.records)&&marker.records.length>0
|
|
198
|
+
&&marker.records.every((seq,i)=>Number.isSafeInteger(seq)&&seq>1&&seq<=physical.records.length
|
|
199
|
+
&&(i===0||seq>marker.records[i-1])),storageCode);
|
|
200
|
+
const selected=new Set(marker.records),{storageEncoding,...logical}=physical;
|
|
201
|
+
// Plan every expansion before allocating any expanded payload. Per-payload
|
|
202
|
+
// caps alone would permit many individually valid envelopes to exhaust memory.
|
|
203
|
+
let bytes=sizeOf(logical);
|
|
204
|
+
for(const [index,record] of physical.records.entries()){
|
|
205
|
+
need(record.seq===index+1,storageCode);
|
|
206
|
+
if(selected.has(record.seq))bytes+=payloadPlan(record.payload,{workflow}).logicalBytes-sizeOf(record.payload);
|
|
207
|
+
else json(record.payload,PAYLOAD_LIMIT);
|
|
208
|
+
need(bytes<=STATE_LIMIT,'limit_exceeded');
|
|
209
|
+
}
|
|
210
|
+
logical.records=physical.records.map(record=>selected.has(record.seq)
|
|
211
|
+
?{...record,payload:unpackFixJournalPayload(record.payload,{workflow})}:record);
|
|
212
|
+
need(sizeOf(logical)===bytes&&sameBytes(packFixJournalState(logical),physical),storageCode);
|
|
213
|
+
return json(logical,STATE_LIMIT);
|
|
214
|
+
}
|