Wix Replatform
WixOfficialPublished by Wix
- Skill
- Workflow
- Automation and integrations
- MIT
- Low risk
- Has scripts
AutoGineer summary
Written by AutoGineer from the skill’s own files, not by the author
wix-replatform acts as a central supervisor for migrating data and content to Wix. It does not perform migration tasks directly but instead inspects migration artifacts to determine the next required stage in the pipeline. By maintaining a deterministic state, it ensures that migrations can be started, resumed, or recovered reliably without duplicating work or creating redundant destination sites. It manages the flow through various phases including discovery, mapping, setup, and import, while enforcing strict validation and approval gates.
What it does
- Routes migration tasks to appropriate sub-modules based on current project state.
- Ensures resumability by tracking migration progress through authoritative artifacts.
- Enforces mandatory approval gates before performing write operations.
- Dispatches specialized subagents for specific tasks to maintain context boundaries.
- Validates artifacts at each stage to prevent proceeding with corrupted or stale data.
- Handles reruns for incremental data updates without re-scaffolding the entire project.
When to use it
- When initiating a new migration from an external source to a Wix site.
- When resuming a migration that was interrupted or stopped.
- When recovering from a failed migration stage by addressing specific errors.
- When performing incremental imports to sync new or modified records after an initial migration.
Advantages
- Prevents data duplication by enforcing idempotent write operations.
- Maintains a clear audit trail through structured telemetry and completion reports.
- Reduces risk by requiring explicit approvals before critical setup or import steps.
- Supports both standard interactive flows and automated 1-click execution modes.
Usage examples
- “Start a new migration for my project in ./migrations/my-store.”
- The agent resolves the project directory, initializes orchestration artifacts, and routes to the project intake module.
- “Resume the migration in ./migrations/my-store.”
- The agent inspects the existing artifacts, determines the last completed stage, and dispatches the next required module.
Install
Download the skill as a ZIP:
https://autogineer.com/agent-skills/wix/wix-replatform-fya3/downloadUnzip it into the cross-client skills folder of your project:
mkdir -p .agents/skills && unzip wix-replatform.zip -d .agents/skills
Documentation by Wix
wix-replatform
wix-replatform is the compact supervisor for a resumable source-to-Wix migration. It
selects one internal module for the active stage; it does not carry every stage procedure
in its own context.
Always-active contract
Resolve the active project under
migrations/<project>/(orREPLATFORM_MIGRATIONS_DIR) before inspecting or creating anything. Only that project's artifacts are authoritative.Preserve
EXPECTATIONS.mdat that project root. It is the user's free-form record of how they expected the run to behave; initialization creates it once and never overwrites it.Start/resume
resources/rp-telemetry/at the beginning of every run and finalize it at a terminal state. Never hand-write telemetry events.Use orchestration JSON plus validated stage artifacts as resume authority. Logs and chat history are diagnostic context only.
Preserve user data: write idempotently, dedupe by source identity, resume rather than restart, and never create a second destination site for a migration.
Never print secrets, secret-bearing config contents, tokens, or credentials. Report only key names and present/blank/missing status.
Do not declare a migration complete from prose or a single artifact. The deterministic router and aggregate completion artifact own terminal completion.
Preserve the owner's cutover handoff: unresolved data, applicable manual setup, missing apps and changed plugin behavior. Reconcile discovery, mapping decisions, setup receipts and final outcomes; imported records alone do not establish working functionality. The producing resources own this guidance and durable completion/handoff artifacts. Use a supported cutover publisher when available; report missing publication integration explicitly rather than claiming the application received local artifacts.
CLI-launched runs and human actions
When a migration is started through replatform, the terminal is deliberately
non-interactive: do not use an agent question UI, ask for stdin, or wait for a person in the
current agent turn. The runner owns process lifecycle and emits only observed events; it does
not replace this skill's router or completion receipts.
When a human choice, review, approval, or manual prerequisite is needed, create a durable
request with node skills/wix-replatform/scripts/user-action.js create --project <projectDir> --request-file <request.json>, then end the turn. Put only reviewed artifact paths in the
request. The person can later use replatform requests, replatform show, and
replatform answer; a resumed agent reads the answered request from
orchestration/user-actions/, never from reconstructed chat context.
Cancellation is not completion. If the runner has recorded cancel_requested, canceled, interrupted or an explicit stop (blocked,
transition --status blocked --resume-from '<json>'; routes run_blocked until explicit resume or --status running, never answer --continue), preserve partial/recovery
artifacts and let a later explicit replatform resume route recovery. Never create aggregate migration completion from an aborted execution report.
Pipeline
intake → destination → source inputs → preflight → (standard: discovery → mapping → codegen;
quick: adapter preflight → quick plan) → setup discovery → website handoff
→ safety review/approval → setup → import
→ storefront continuation (website only) → aggregate finalization → Migration reviewDelivery modes are management (backend only), website (frontend only), and
management_and_website (the integrated flow above). Accept user input both as
management_and_website and persist the canonical value.
For deliveryMode=management, aggregate finalization requires valid backend import
accounting. For deliveryMode=management_and_website, it additionally requires a current
website/completion.json with completed screenshot review, no unresolved critical/high
gaps, and release evidence. execution/completion-report.json is backend accounting,
never website-migration completion by itself.
Route before acting
On every new or resumed turn:
Resolve the project and initialize/load its orchestration artifacts.
Run
node skills/wix-replatform/scripts/orchestration-route.js <projectDir>.Dispatch the
nextResourceto a Developer subagent instead of loading and following it yourself. Give it the project directory and the resource to run; it follows that resource's contract exactly as written, unchanged. Read back only its result — never its transcript, tool output, or logs.Every dispatch (Developer or QA) is a fresh subagent, never a resumed one; pass only a handoff file and stay within a context budget — follow
resources/rp-subagent-dispatch/. Never readstate/attempts/write-attempts.ndjsonraw; usescripts/write-log-summary.js.A Developer may fan out to sub-subagents only for harmless read-only work with no shared state, and only if the host supports it; writes stay one at a time, as each resource says.
Dispatch a QA subagent to check the Developer's result against that resource's own stated required outputs (its own "artifacts to create or update" or "## Output" section, and any review/gap documents it defines). QA is read-only — it never writes or calls Wix. If something's missing, dispatch a new Developer for the same resource with QA's findings; otherwise the stage is done.
At the end of each pipeline phase (discovery, mapping, setup discovery, import, and so on — see the pipeline above; a phase spans several resource-level stages), dispatch a QA subagent to review that phase's documents as a whole (for example
mapping/review/mapping-summary.mdandmapping-gaps.json, orexecution/completion-report.json) before routing to the next phase. This catches anything a single per-task check could miss. For the import phase specifically, this review must be entity-by-entity against the accepted mapping plan, not a skim of the top-level status string — seerp-execute-import's "Entity-by-entity QA" section. Any run-fixing dispatch mid-phase (a crash fix, a slowness fix, a resumed pass) does not itself satisfy this; run the phase-level QA again once the fix lands, since fixing one entity's defect certifies nothing about its neighbors. Mapping QA also checks exact source coverage, decided operation/path semantics and canonical projections. Codegen QA reads the Developer's independently expected synthetic request evidence and its freshness; it does not execute examples. Semantic defects return to mapper, implementation defects to codegen. One-click tests defer unresolved scope and finish independent work with a typed incomplete result, without questions or repeated redispatch; customer runs retain their existing checkpoints.Route again after every stage boundary. Stale requests use
resources/rp-user-action-recovery/; unreadable request artifacts block until repaired. Never discard pending requests or decide the next stage from memory.
The router (lib/orchestration-router.js) is the only routing and resume authority. It
recomputes the route from decisions, approvals, preflight, run.json status and the phase
outputs on every call; run.json.currentState is diagnostic only, never a routing input.
A recorded rejection stops the route in every mode, one-click included:
execution_approval_rejectedisblockedbefore any setup or import write, andmapping_approval_rejectedreturns torp-mapperfor a new plan.A gated output that exists but is unusable (
source_schema_invalid,mapping_plan_invalid,setup_plan_invalid,setup_requirements_invalid,execution_manifest_invalid) isblockedwitherrorsand the producer asnextResource. Do not re-run the producer automatically; report the errors to the Import Operator and stop.The import routes (
missing_completion_report,import_interrupted= a stopped run's aborted report,quick_mode_import_required) carryimportProgress.interruptedlistsrecoveryArtifacts: resume it, never a second import.interruptedReport: "missing": first runscripts/interrupted-completion-report.js.
If an artifact is malformed, partial, stale, or fails its validator, treat its producing stage as incomplete and route to the producer. Do not make parallel replacement artifacts.
A user action whose reviewed artifacts changed after it was created is refused with
user_action_stale and stays pending. Create a new request from the current artifacts.
Catching up a finished migration is a rerun, not a new project. When the user asks to pull
in records that arrived or changed since the import — "the source kept selling", "these orders
are missing", "re-run it" — reuse the same project, destination, mapping, generated importer
and crosswalk, and route to rp-execute-import → "Rerunning an import for new and modified
records". Do not scaffold a second project or re-approve an unchanged mapping; a
mapping or scope change does go back through the existing gates. A rerun's revision conflicts
are user decisions even in 1-click mode.
Demand-loaded module map
| Router situation | Dispatch |
|---|---|
| project, delivery mode, source mode, or grouped intake is unresolved | resources/rp-project-intake/ |
| destination is absent, conflicting, or needs creation/adoption | resources/rp-destination/ |
| source URL/files/config/acquisition mode/credentials are required, including source credentials said to exist already on another site | resources/rp-source-inputs/ |
| deterministic preflight is missing, repaired, or blocked on CLI checks | resources/rp-preflight/ |
| quick mode needs platform resolution, endpoint preflight, plan, extraction, or import | selected resources/rp-quick-<platform>/ |
| source schema is missing | resources/rp-discovery/ and its selected source adapter |
| mapping is missing or needs review | resources/rp-mapper/ |
| Wix requirements are missing | resources/rp-setup-discovery/ |
| import code/review is missing or stale | resources/rp-import-codegen/ |
| setup verification is missing | resources/rp-execute-setup/ |
| backend completion report is missing | resources/rp-execute-import/ |
| frontend-only work, or integrated website handoff/build/gap loop/release receipt is incomplete | resources/rp-website-continuation/ |
| aggregate completion is missing | resources/rp-execution-policy/ |
| aggregate completion exists but the local review publication or current recap is missing | resources/rp-migration-review/ |
The resource named by the route is the complete stage contract, dispatched as described in "Route before acting" — the main session itself never loads it directly. Only the dispatched Developer loads source-file, secret, scaffolding, or storefront procedures, and only for the stage that actually needs them; nothing about a stage where they're irrelevant is loaded, by the main session or the Developer.
Interaction policy
Normal mode asks only for a missing required input, a genuinely ambiguous active project, or a mandatory write approval. Ask one question at a time unless a module says otherwise.
When the user says add to expectations: <note> (or an unambiguous natural-language
equivalent), append the note to the active project's EXPECTATIONS.md before continuing.
Use scripts/expectations-ledger.js so the entry retains the supplied statement plus safe
current context (stage/state, relevant project-relative artifacts, and known entity/field
labels). Do not treat this as approval or silently change a mapping. Never record secrets,
tokens, raw customer data, or raw source records; redact them if supplied and say so.
Before writes, preserve the mandatory execution-plan approval gate. Read-only discovery, mapping, setup verification, code generation, and review happen before it.
Quick management import mode
When managementImportMode=quick, select the platform adapter only after the deterministic
source probe identifies a compatible platform. Quick mode is a bounded public-data import:
the adapter's versioned quick-mode.json owns its source routes, entity graph, field matrix,
and exclusions. Do not run agentic discovery, mapping, or project-specific code generation for
the quick backend branch. Still run destination setup, application installation, Catalog-V3 and
notification safety checks, execution approval, shared write runtime, crosswalks, and completion
accounting. If no adapter qualifies, persist the unsupported-mode result and ask the user to
select standard mode; never silently fall back.
Halt only for missing/invalid required input, a manual action with no API, or a systemic failure/data-loss risk. A halt must persist the code, evidence, owner, and exact unblock action; never silently stop.
1-click mode
When explicitly requested by the user, record automationMode=one_click with
source=user and apply the following. A missing mode or a one_click value recorded as
deterministic_inference/prior_artifact is normal interactive mode and must not skip a
checkpoint:
collect all still-missing intake values in one grouped request; then do not pause for routine approvals;
record mapping and execution approvals as
approved,decidedBy=agent; skipping a pause without state is invalid;for CSV/file-only runs, validate and accept the required sample preview as the agent; without explicit user-authored one-click, pause for the normal mapping review, sample preview validation, and execution approval;
default to a new isolated site unless an existing destination is explicitly supplied or already pinned, but never default the new site's type (Headless, Studio or Harmony): ask it in the grouped intake; choose website scope deterministically from discovered route families;
retry PATH/tool-resolution failures under the operator's compatible interactive login shell; request required browser/network sandbox escalation and retry before calling it blocked;
retry the obvious recovery before stopping: aligned shell first for PATH/tool failures, then the required escalation retry for sandboxed browser or network/API failures;
continue through the complete selected deliverable. In
management_and_websitemode that includes frontend build, post-build gap analysis, bounded fix loop, and release receipt; when the user selected facelift at intake, it also includes the separate post-clone facelift review;before yielding, re-route. A 1-click run is only
completedwithcompletion/migration-completion.json, orblockedwith a typed durable blocker.
Use the operator's compatible local Node/Yarn shell when recovering from a PATH or tool resolution failure. Machine-specific shell paths belong only in local, git-excluded agent instructions.
Repo-local interactive shell contract: a PATH/tool-resolution failure on first attempt may
mean the command ran under a plain, non-interactive shell that never sourced the operator's
Node/Yarn profile. Resolve the operator's compatible login shell from the local environment or
git-excluded agent instructions, verify that it is executable, and retry the exact same command
through that shell in interactive login mode so the profile that resolves node/yarn/npx
loads. Do not hardcode a machine-specific shell path in this public skill. Only escalate to a
durable blocker if the compatible-shell retry itself fails.
Guardrails
Never bypass a validator, approval-state update, verified notification mute, or Catalog-V3 gate to make a stage appear finished.
websitemeans frontend only;management_and_websitemeans the integrated backend and frontend migration. Never hand the integrated migration destination to the frontend skill or create a second frontend site in that mode.A Studio or Harmony destination (
targetSiteType) is created and verified byrp-destination's coordinator and is never published, unpublished or handed towix-headless-replatform; its website and publication remain operator handoff items.Do not implement a storefront manually when
wix-headless-replatformis unavailable; persist a blocker with the exact required runtime capability.In management mode, still generate
website/handoff.*after setup discovery so a later storefront can start without repeating backend discovery.
Files
- .publish-manifest.json210 B
- ATTRIBUTION.mdAdded by AutoGineer815 B
- CLI.md9.8 KiB
- LICENSELicence file added by AutoGineer1.0 KiB
- README.md11.9 KiB
- SKILL.md16.4 KiB
- VERSION6 B
- console/account.css633 B
- console/app.css5.2 KiB
- console/app.jsScript22.9 KiB
- console/index.html837 B
- console/json.css639 B
- lib/agent-adapters/index.jsScript2.9 KiB
- lib/app-page-routing.jsScript4.3 KiB
- lib/app-requirements.jsScript1.5 KiB
- lib/artifact-freshness.jsScript12.7 KiB
- lib/blocked-data-requests.jsScript21.4 KiB
- lib/cached-json.jsScript1.3 KiB
- lib/catalog-baseline.jsScript23.3 KiB
- lib/census-reconciliation.jsScript22.8 KiB
- lib/cms-collection-compatibility.jsScript2.0 KiB
- lib/completion-report.jsScript124.6 KiB
- lib/config-env.jsScript6.1 KiB
- lib/confirmed-pictures.jsScript6.9 KiB
- lib/crosswalk-adoption.jsScript6.3 KiB
- lib/dependency-proofs.jsScript46.7 KiB
- lib/dependency-readiness.jsScript112.3 KiB
- lib/dependency-recovery.jsScript1.5 KiB
- lib/dependency-waves.jsScript2.1 KiB
- lib/destination-choice.jsScript9.7 KiB
- lib/destination-handoff.jsScript12.5 KiB
- lib/destination-http.jsScript2.7 KiB
- lib/destination-route.jsScript5.1 KiB
- lib/destination.jsScript57.1 KiB
- lib/discovered-census.jsScript11.0 KiB
- lib/discovered-set-status.jsScript15.2 KiB
- lib/document-url-guard.jsScript12.7 KiB
- lib/editor-type.jsScript748 B
- lib/egress-proxy.jsScript20.8 KiB
- lib/entity-qa-attestation.jsScript19.4 KiB
- lib/entity-verification.jsScript1.3 KiB
- lib/execution-capability.jsScript7.3 KiB
- lib/execution-log.jsScript6.4 KiB
- lib/execution-manifest.jsScript10.2 KiB
- lib/execution-state.jsScript5.9 KiB
- lib/expectations-ledger.jsScript3.4 KiB
- lib/extract-ledger.jsScript21.4 KiB
- lib/field-mapping-examples.jsScript7.3 KiB
- lib/generated-plugin-handoff.jsScript8.7 KiB
- lib/import-interruption.jsScript13.6 KiB
- lib/import-readiness-contract.jsScript4.3 KiB
- lib/import-recovery.jsScript54.0 KiB
- lib/import-task-runner.jsScript18.1 KiB
- lib/imported-redirect-targets.jsScript32.5 KiB
- lib/internal-link-reconciliation.jsScript10.7 KiB
- lib/link-audit.jsScript11.1 KiB
- lib/link-census.jsScript11.0 KiB
- lib/link-plan.jsScript9.4 KiB
- lib/link-verification.jsScript18.7 KiB
- lib/link-waivers.jsScript5.8 KiB
- lib/link-write-journal.jsScript1.8 KiB
- lib/local-state.js66.8 KiB
- lib/mapping-field-contract.js54.1 KiB
- lib/mapping-omission-approval.js7.6 KiB
- lib/mapping-recovery.js22.4 KiB
- lib/mapping-resolution.js6.1 KiB
- lib/mapping-target-rules.js7.4 KiB
- lib/member-completion.js31.6 KiB
- lib/member-crosswalk.js2.9 KiB
- lib/member-outcome-registry.js13.6 KiB
- lib/migration-completion.js18.3 KiB
- lib/migration-review-mapping.js21.4 KiB
- lib/migration-review.js73.7 KiB
- lib/multi-step-attempt.js4.4 KiB
- lib/multilingual-public-report.js14.5 KiB
- lib/multilingual-public.js26.3 KiB
- lib/notification-mute.js9.9 KiB
- lib/operation-assessments.js9.8 KiB
- lib/operator-identity.js1.2 KiB
- lib/orchestration-decisions.js8.7 KiB
- lib/orchestration-preflight.js25.1 KiB
- lib/orchestration-router.js50.8 KiB
- lib/orchestration-state.js30.9 KiB
- lib/order-reference-readiness.js2.8 KiB
- lib/output-applicability.js5.0 KiB
- lib/payments-outcome-registry.js9.8 KiB
- lib/plan-order-outcome-registry.js13.1 KiB
- lib/plugin-cms-fallback.js35.1 KiB
- lib/preservation-approval-stamp.js2.8 KiB
- lib/product-media-deferral.js3.8 KiB
- lib/product-videos.js7.1 KiB
- lib/progress-log.js6.6 KiB
- lib/progress-log.sh4.1 KiB
- lib/raw-request-guard.js18.5 KiB
- lib/read-back-approval.js16.1 KiB
- lib/read-back-comparison.js10.5 KiB
- lib/read-back-contract.js37.4 KiB
- lib/read-back-extract.js4.0 KiB
- lib/read-back-gate.js46.6 KiB
- lib/read-back-reads.js2.9 KiB
- lib/read-back-state.js3.4 KiB
- lib/read-back-summary.js6.5 KiB
- lib/rerun-conflicts.js14.5 KiB
- lib/run-controller.js26.3 KiB
- lib/seo-outcome-registry.js3.2 KiB
- lib/setup-producers.js1.3 KiB
- lib/setup-verification.js11.6 KiB
- lib/site-regional-gate.js3.3 KiB
- lib/site-regional-report.js10.3 KiB
- lib/site-regional.js37.0 KiB
- lib/source-access.js11.2 KiB
- lib/source-coverage.js54.0 KiB
- lib/source-location.js10.6 KiB
- lib/source-redirect-capture.js2.6 KiB
- lib/source-regional.js15.5 KiB
- lib/source-secrets.js8.1 KiB
- lib/structured-link-patch.js3.1 KiB
- lib/subagent-handoff.js8.9 KiB
- lib/target-import-support.js18.4 KiB
- lib/task-state.js6.2 KiB
- lib/toolkit-dependencies.js947 B
- lib/url-preservation-report.js20.4 KiB
- lib/url-preservation-state.js27.0 KiB
- lib/url-redirect-plan.js36.3 KiB
- lib/url-verification.js15.5 KiB
- lib/user-action-effects.js2.9 KiB
- lib/user-actions.js18.4 KiB
- lib/vendor-closure.js5.6 KiB
- lib/website-handoff.js60.6 KiB
- lib/wix-auth.js11.0 KiB
- lib/wix-client-callers.js6.7 KiB
- lib/wix-dashboard-url.js1.0 KiB
- lib/write-log-summary.js9.4 KiB
- lib/write-verification-state.js7.9 KiB
- lib/write-verification.js30.0 KiB
- scripts/artifact-freshness.js2.3 KiB
- scripts/census-reconciliation.js781 B
- scripts/cms-fallback.js3.2 KiB
- scripts/cms-item-quota.js3.9 KiB
- scripts/crosswalk-adopt.js3.0 KiB
- scripts/dependency-preflight.js2.2 KiB
- scripts/discovered-census.js867 B
- scripts/discovered-set-status.js1.1 KiB
- scripts/entity-qa-attestation.js4.8 KiB
- scripts/execution-health-check.js19.9 KiB
- scripts/execution-manifest-validate.js1.7 KiB
- scripts/execution-state-prepare.js3.9 KiB
- scripts/expectations-ledger.js1.5 KiB
- scripts/extract-ledger.js1.9 KiB
- scripts/import-contract-verify.js20.4 KiB
- scripts/import-supervise.js16.1 KiB
- scripts/internal-link-audit.js6.5 KiB
- scripts/interrupted-completion-report.js2.7 KiB
- scripts/item-seo-tags-verify.js8.2 KiB
- scripts/link-waiver.js3.8 KiB
- scripts/migration-completion.js3.8 KiB
- scripts/migration-review.js3.7 KiB
- scripts/orchestration-preflight.js3.5 KiB
- scripts/orchestration-route.js3.7 KiB
- scripts/orchestration-state.js18.9 KiB
- scripts/read-back-gate.js11.3 KiB
- scripts/replatform-console.js12.9 KiB
- scripts/replatform.js35.4 KiB
- scripts/site-regional.js2.4 KiB
- scripts/source-secrets.js2.9 KiB
- scripts/subagent-handoff.js2.5 KiB
- scripts/url-redirects.js13.0 KiB
- scripts/user-action.js2.9 KiB
- scripts/vendor-closure.js1.0 KiB
- scripts/website-handoff-generate.js2.3 KiB
- scripts/wix-dashboard-url.js1.6 KiB
- scripts/wix-destination.js6.4 KiB
- scripts/wix-env.js1.5 KiB
- scripts/wix-headless-scaffold.js34.8 KiB
- scripts/write-log-summary.js2.2 KiB
- vendor/README.md604 B
- vendor/commander/LICENSE1.1 KiB
- vendor/commander/index.js711 B
- vendor/commander/lib/argument.js3.1 KiB
- vendor/commander/lib/command.js85.6 KiB
- vendor/commander/lib/error.js1.1 KiB
- vendor/commander/lib/help.js20.3 KiB
- vendor/commander/lib/option.js10.0 KiB
- vendor/commander/lib/suggestSimilar.js2.7 KiB
- vendor/commander/package.json1.6 KiB
lib/migration-review.js
'use strict';
const crypto = require('node:crypto');
const fs = require('node:fs/promises');
const os = require('node:os');
const { performance } = require('node:perf_hooks');
const { readEnvFile } = require('./config-env.js');
const { getDecisionValue } = require('./orchestration-decisions.js');
const path = require('node:path');
const CONTRACT_VERSION = 'migration-review/1';
const GROUPS = ['Data', 'Manual', 'Plugins'];
const STORE_DIR = 'migration-review';
const EVIDENCE_CATEGORIES = ['source_scope', 'mapping_fidelity', 'execution_recovery', 'setup_plugin', 'explanations_checks'];
class MigrationReviewError extends Error {
constructor(code, message, details = {}) {
super(message);
this.name = 'MigrationReviewError';
this.code = code;
this.details = details;
}
}
function canonical(value) {
if (Array.isArray(value)) return `[${value.map(canonical).join(',')}]`;
if (value && typeof value === 'object') {
return `{${Object.keys(value).sort().map((key) => `${JSON.stringify(key)}:${canonical(value[key])}`).join(',')}}`;
}
return JSON.stringify(value);
}
function digest(value) {
return crypto.createHash('sha256').update(typeof value === 'string' ? value : canonical(value)).digest('hex');
}
function clone(value) {
return value === undefined ? undefined : JSON.parse(JSON.stringify(value));
}
function reviewRoot(projectDir) {
return path.join(path.resolve(projectDir), STORE_DIR);
}
function assertToken(value, name) {
if (typeof value !== 'string' || !/^[A-Za-z0-9][A-Za-z0-9._:-]{0,199}$/.test(value)) {
throw new MigrationReviewError('invalid_request', `${name} must be a stable non-empty token`);
}
return value;
}
function assertObject(value, name) {
if (!value || typeof value !== 'object' || Array.isArray(value)) {
throw new MigrationReviewError('invalid_request', `${name} must be an object`);
}
return value;
}
function assertVersion(request) {
if (request.contractVersion !== CONTRACT_VERSION) {
throw new MigrationReviewError('unsupported_contract', `contractVersion must be ${CONTRACT_VERSION}`);
}
}
function safeName(value) {
return digest(value).slice(0, 32);
}
async function assertSafePath(filePath) {
const parts = path.resolve(filePath).split(path.sep);
const boundary = parts.lastIndexOf(STORE_DIR);
if (boundary < 0) return;
for (let index = boundary; index < parts.length; index += 1) {
const current = parts.slice(0, index + 1).join(path.sep) || path.sep;
const stat = await fs.lstat(current).catch((error) => error.code === 'ENOENT' ? null : Promise.reject(error));
if (stat && stat.isSymbolicLink()) throw new MigrationReviewError('unsupported_store_path', 'review storage must not contain symbolic links');
}
}
function bindingKeys(value) {
return { migrationId: value.migrationId, sourceIdentity: value.source.identity, destinationSiteId: value.destination.siteId };
}
async function projectBinding(projectDir) {
const run = await fs.readFile(path.join(projectDir, 'orchestration', 'run.json'), 'utf8').then(JSON.parse).catch((error) => error.code === 'ENOENT' ? null : Promise.reject(error));
const decisions = await fs.readFile(path.join(projectDir, 'orchestration', 'decisions.json'), 'utf8').then(JSON.parse).catch((error) => error.code === 'ENOENT' ? null : Promise.reject(error));
const env = await readEnvFile(path.join(projectDir, 'config', 'wix.env')).catch((error) => error.code === 'ENOENT' ? {} : Promise.reject(error));
const sourceUrl = decisions && getDecisionValue(decisions, 'sourceUrl');
if (!run || !sourceUrl || !env.WIX_SITE_ID || env.WIX_SITE_ID === 'REPLACE_WITH_REAL_VALUE') return null;
// Project identity persists across runner invocations; a runner's runId does not.
return { contractVersion: CONTRACT_VERSION, migrationId: `migration_${digest({ projectId: run.projectId, sourceUrl, siteId: env.WIX_SITE_ID }).slice(0, 24)}`, source: { identity: `source_${digest({ projectId: run.projectId, sourceUrl }).slice(0, 24)}` }, destination: { siteId: env.WIX_SITE_ID } };
}
function validateBinding(request, identity) {
if (request.migrationId !== identity.migrationId) throw new MigrationReviewError('binding_mismatch', 'request belongs to a different migration');
if (request.destination && request.destination.siteId !== identity.destination.siteId) throw new MigrationReviewError('binding_mismatch', 'request belongs to a different destination');
}
async function readJson(filePath, { optional = false } = {}) {
await assertSafePath(filePath);
try {
return JSON.parse(await fs.readFile(filePath, 'utf8'));
} catch (error) {
if (optional && error && error.code === 'ENOENT') return null;
if (error instanceof SyntaxError) throw new MigrationReviewError('corrupt_store', `invalid JSON at ${filePath}`);
throw error;
}
}
async function syncDir(dirPath) {
try {
const handle = await fs.open(dirPath, 'r');
try { await handle.sync(); } finally { await handle.close(); }
} catch (error) {
if (['EINVAL', 'ENOTSUP', 'EOPNOTSUPP'].includes(error.code)) throw new MigrationReviewError('unsupported_filesystem', 'review storage requires local atomic rename and directory fsync support');
throw error;
}
}
async function ensureDurableDir(dirPath) {
const firstCreated = await fs.mkdir(dirPath, { recursive: true, mode: 0o700 });
if (!firstCreated) return;
let current = dirPath;
for (;;) {
await syncDir(current);
if (current === path.dirname(firstCreated)) break;
current = path.dirname(current);
}
}
async function writeAtomic(filePath, value) {
await assertSafePath(filePath);
const dir = path.dirname(filePath);
await ensureDurableDir(dir);
const temporary = `${filePath}.${process.pid}.${crypto.randomUUID()}.tmp`;
const handle = await fs.open(temporary, 'wx', 0o600);
try {
await handle.writeFile(`${canonical(value)}\n`, 'utf8');
await handle.sync();
} finally {
await handle.close();
}
await fs.rename(temporary, filePath);
await syncDir(dir);
}
async function writeImmutable(filePath, value) {
await assertSafePath(filePath);
const bytes = `${canonical(value)}\n`;
await ensureDurableDir(path.dirname(filePath));
const temporary = `${filePath}.${process.pid}.${crypto.randomUUID()}.tmp`;
const handle = await fs.open(temporary, 'wx', 0o600);
try { await handle.writeFile(bytes, 'utf8'); await handle.sync(); } finally { await handle.close(); }
try {
// Publish only complete bytes. A killed writer cannot leave a truncated immutable object.
await fs.link(temporary, filePath);
await syncDir(path.dirname(filePath));
return { created: true };
} catch (error) {
if (!error || error.code !== 'EEXIST') throw error;
const current = await fs.readFile(filePath, 'utf8');
if (current !== bytes) throw new MigrationReviewError('immutable_collision', `immutable object differs at ${filePath}`);
return { created: false };
} finally { await fs.unlink(temporary); }
}
function emptyState(identity) {
return {
contractVersion: CONTRACT_VERSION,
migrationId: identity.migrationId,
destination: clone(identity.destination),
stateVersion: 0,
currentRevisionId: null,
actionIds: [],
committedRequests: {},
scopeFrontier: {},
executions: {},
captures: {},
generation: 0,
latestHandoffId: null,
updatedAt: identity.createdAt,
};
}
async function ensureIdentity(projectDir, request, clock = () => new Date(), hooks = {}) {
assertVersion(request);
const inferred = await projectBinding(projectDir);
const binding = {
contractVersion: CONTRACT_VERSION,
migrationId: request.migrationId || inferred && inferred.migrationId,
source: clone(request.source || inferred && inferred.source),
destination: clone(request.destination || inferred && inferred.destination),
};
assertToken(binding.migrationId, 'migrationId');
assertToken(assertObject(binding.source, 'source').identity, 'source.identity');
assertToken(assertObject(binding.destination, 'destination').siteId, 'destination.siteId');
if (inferred && canonical(bindingKeys(binding)) !== canonical(bindingKeys(inferred))) throw new MigrationReviewError('binding_mismatch', 'review binding differs from the accepted project identity');
const root = reviewRoot(projectDir);
await assertSafePath(root);
await ensureDurableDir(root);
const lock = await acquireLock(root, { operation: 'initialize', acquiredAt: clock().toISOString() });
try {
const filePath = path.join(root, 'identity.json');
const existing = await readJson(filePath, { optional: true });
if (existing && canonical(bindingKeys(existing)) !== canonical(bindingKeys(binding))) throw new MigrationReviewError('binding_mismatch', 'review source/destination identity differs from this project');
const identity = existing || { ...binding, createdAt: clock().toISOString() };
await writeImmutable(filePath, identity);
if (hooks.afterIdentity) await hooks.afterIdentity(clone(identity));
const statePath = path.join(root, 'state.json');
if (!await readJson(statePath, { optional: true })) {
for (const folder of ['receipts', 'revisions', 'actions', 'executions', 'evidence']) {
const rows = await fs.readdir(path.join(root, folder)).catch((error) => error.code === 'ENOENT' ? [] : Promise.reject(error));
if (rows.length) throw new MigrationReviewError('missing_commit_descriptor', 'existing review artifacts need recovery; initialization cannot reset their history');
}
await writeAtomic(statePath, emptyState(identity));
}
return identity;
} finally { await releaseLock(lock); }
}
async function loadIdentityAndState(projectDir) {
const root = reviewRoot(projectDir);
await assertSafePath(root);
const identity = await readJson(path.join(root, 'identity.json'));
const state = await readJson(path.join(root, 'state.json'));
if (identity.contractVersion !== CONTRACT_VERSION || state.contractVersion !== CONTRACT_VERSION) {
throw new MigrationReviewError('unsupported_contract', 'migration review store has an unsupported contract version');
}
if (identity.migrationId !== state.migrationId || canonical(identity.destination) !== canonical(state.destination)) {
throw new MigrationReviewError('corrupt_store', 'identity and committed state binding disagree');
}
const inferred = await projectBinding(projectDir);
if (inferred && canonical(bindingKeys(identity)) !== canonical(bindingKeys(inferred))) throw new MigrationReviewError('binding_mismatch', 'project identity changed after review initialization');
return { root, identity, state };
}
async function lockOwner(lockPath) {
const stat = await fs.lstat(lockPath).catch((error) => error.code === 'ENOENT' ? null : Promise.reject(error));
if (!stat) return null;
return readJson(stat.isDirectory() ? path.join(lockPath, 'owner.json') : lockPath, { optional: true });
}
async function acquireLock(root, owner) {
const lockPath = path.join(root, '.lock');
const record = { token: owner.token || crypto.randomUUID(), operation: owner.operation, pid: process.pid, host: os.hostname(), acquiredAt: owner.acquiredAt };
// Link fully synced bytes exclusively; no ownerless claim is exposed between mkdir/write.
const ownerPath = path.join(root, 'lock-owners', `${safeName(record.token)}.json`);
await writeImmutable(ownerPath, record);
try { await fs.link(ownerPath, lockPath); } catch (error) {
if (error.code === 'EEXIST') throw new MigrationReviewError('lock_held', 'review mutation is owned by another process', { owner: await lockOwner(lockPath) });
if (['EPERM', 'ENOTSUP', 'EOPNOTSUPP'].includes(error.code)) throw new MigrationReviewError('unsupported_filesystem', 'review storage requires local exclusive hard-link claims');
throw error;
}
await syncDir(root);
return { lockPath, record };
}
async function releaseLock(lock) {
const current = await lockOwner(lock.lockPath);
if (!current || current.token !== lock.record.token) return;
await fs.unlink(lock.lockPath);
await syncDir(path.dirname(lock.lockPath));
}
function ownerIsLive(owner) {
if (!owner || !Number.isInteger(owner.pid) || owner.host !== os.hostname()) return null;
try { process.kill(owner.pid, 0); return true; } catch (error) { return error.code === 'ESRCH' ? false : null; }
}
async function acquireRecoveryClaim(root, originalToken) {
const dir = path.join(root, 'recovery-claims');
const token = crypto.randomUUID(), claimPath = path.join(dir, `${safeName(token)}.json`);
const claim = { token, pid: process.pid, host: os.hostname(), originalToken, choosing: true, number: 0 };
await writeImmutable(claimPath, claim);
try {
const readClaims = async () => {
const rows = [];
for (const name of await fs.readdir(dir)) {
if (!/^[a-f0-9]{32}\.json$/.test(name)) continue;
const row = await readJson(path.join(dir, name), { optional: true });
if (row) rows.push(row);
}
return rows;
};
// Publish a choosing marker before taking a ticket. Later entrants take a higher
// ticket; simultaneous choosers arbitrate by token. Dead process records are inert.
claim.number = Math.max(0, ...(await readClaims()).map((row) => row.number || 0)) + 1;
claim.choosing = false;
await writeAtomic(claimPath, claim);
for (const other of await readClaims()) {
if (other.token === token || ownerIsLive(other) === false) continue;
if (ownerIsLive(other) !== true || other.choosing || other.number < claim.number || (other.number === claim.number && other.token < token)) {
throw new MigrationReviewError('lock_recovery_conflict', 'another live or unknown process owns recovery; retry after it finishes', { recoveryOwner: other });
}
}
return claimPath;
} catch (error) { await fs.unlink(claimPath); await syncDir(dir); throw error; }
}
async function recoverLock(projectDir, request, options = {}) {
assertVersion(request);
const root = reviewRoot(projectDir);
await assertSafePath(root);
const identity = await readJson(path.join(root, 'identity.json'), { optional: true });
let state = await readJson(path.join(root, 'state.json'), { optional: true });
if (identity) validateBinding(request, identity);
else assertToken(request.migrationId, 'migrationId');
const lockPath = path.join(root, '.lock');
const owner = await lockOwner(lockPath);
const originalRecord = !owner && request.ownerToken ? await readJson(path.join(root, 'lock-owners', `${safeName(request.ownerToken)}.json`), { optional: true }) : null;
const recoveryOwner = owner || (originalRecord && request.originalOwner && canonical(originalRecord) === canonical(request.originalOwner) ? originalRecord : null);
if (!recoveryOwner || request.ownerToken !== recoveryOwner.token || ownerIsLive(recoveryOwner) !== false || request.expectedStateVersion !== (state ? state.stateVersion : 0)) throw new MigrationReviewError('lock_recovery_refused', 'recovery requires an unchanged descriptor and the original dead local owner', { owner, stateVersion: state && state.stateVersion });
if (!state && recoveryOwner.operation !== 'initialize') throw new MigrationReviewError('missing_commit_descriptor', 'committed descriptor must be recovered before mutation lock recovery');
if (!state) for (const folder of ['receipts', 'revisions', 'actions', 'executions', 'evidence']) {
const files = await fs.readdir(path.join(root, folder)).catch((error) => error.code === 'ENOENT' ? [] : Promise.reject(error));
if (files.length) throw new MigrationReviewError('missing_commit_descriptor', 'existing artifacts cannot be reset during initialization recovery');
}
// Keep the original claim in place until repair ends. Arbitration has no ownerless
// window or singleton recovery lock that another interruption could strand.
const recoveryPath = await acquireRecoveryClaim(root, recoveryOwner.token);
try {
if (options.hooks && options.hooks.afterRecoveryClaim) await options.hooks.afterRecoveryClaim();
if (canonical(await lockOwner(lockPath)) !== canonical(owner)) throw new MigrationReviewError('lock_recovery_conflict', 'lock owner changed during recovery');
const latestState = await readJson(path.join(root, 'state.json'), { optional: true });
if (canonical(latestState) !== canonical(state)) throw new MigrationReviewError('lock_recovery_conflict', 'descriptor changed during recovery');
if (!state && identity) { state = emptyState(identity); await writeAtomic(path.join(root, 'state.json'), state); }
await fs.rm(lockPath, { recursive: true });
await syncDir(root);
return { contractVersion: CONTRACT_VERSION, recovered: true, owner, stateVersion: state ? state.stateVersion : 0 };
} finally { await fs.unlink(recoveryPath); await syncDir(path.dirname(recoveryPath)); }
}
function requestDigest(operation, request) {
const normalized = clone(request);
delete normalized.expectedStateVersion;
return digest({ operation, request: normalized });
}
async function replayIfCommitted(root, state, requestId, requestHash) {
const entry = state.committedRequests[requestId];
if (!entry) return null;
if (entry.requestHash !== requestHash) throw new MigrationReviewError('request_collision', `requestId ${requestId} was already committed with different content`);
const receipt = await readJson(path.join(root, 'receipts', `${safeName(requestId)}.json`));
if (receipt.requestId !== requestId || receipt.requestHash !== requestHash || receipt.committedStateVersion !== entry.stateVersion) throw new MigrationReviewError('corrupt_store', 'receipt differs from its committed request');
return { ...receipt, replayed: true };
}
async function commitMutation(projectDir, operation, request, build, options = {}) {
assertVersion(request);
const requestId = assertToken(request.requestId, 'requestId');
const requestHash = requestDigest(operation, request);
const initial = await loadIdentityAndState(projectDir);
validateBinding(request, initial.identity);
const replay = await replayIfCommitted(initial.root, initial.state, requestId, requestHash);
if (replay) return replay;
if (request.expectedStateVersion !== initial.state.stateVersion) {
throw new MigrationReviewError('state_conflict', `expected state version ${request.expectedStateVersion}, current is ${initial.state.stateVersion}`, { stateVersion: initial.state.stateVersion });
}
const prepared = await build(initial);
if (options.hooks && options.hooks.afterStage) await options.hooks.afterStage(clone(prepared));
const now = (options.clock || (() => new Date()))().toISOString();
const lock = await acquireLock(initial.root, { token: options.lockToken, operation, acquiredAt: now });
try {
const current = await loadIdentityAndState(projectDir);
const committedReplay = await replayIfCommitted(current.root, current.state, requestId, requestHash);
if (committedReplay) return committedReplay;
if (current.state.stateVersion !== request.expectedStateVersion) {
throw new MigrationReviewError('state_conflict', 'committed state changed while the mutation was staged', { stateVersion: current.state.stateVersion });
}
const nextVersion = current.state.stateVersion + 1;
const receiptPath = path.join(current.root, 'receipts', `${safeName(requestId)}.json`);
const stagedReceipt = await readJson(receiptPath, { optional: true });
if (stagedReceipt && stagedReceipt.requestHash !== requestHash) {
throw new MigrationReviewError('request_collision', `requestId ${requestId} has a staged receipt with different content`);
}
const intentPath = path.join(current.root, 'intents', `${safeName(requestId)}.json`);
const priorIntent = await readJson(intentPath, { optional: true });
if (priorIntent && priorIntent.requestHash !== requestHash) throw new MigrationReviewError('request_collision', 'staged request identity was reused with different content');
if (priorIntent && priorIntent.expectedStateVersion !== current.state.stateVersion) throw new MigrationReviewError('state_conflict', 'staged request must be reconciled against changed committed state');
const intent = priorIntent || { requestId, requestHash, expectedStateVersion: current.state.stateVersion, committedAt: stagedReceipt ? stagedReceipt.committedAt : now };
await writeImmutable(intentPath, intent);
if (options.hooks && options.hooks.afterIntent) await options.hooks.afterIntent(clone(intent));
const effectiveNow = intent.committedAt;
const applied = await prepared.apply({ ...current, now: effectiveNow, nextVersion });
if (options.hooks && options.hooks.afterApply) await options.hooks.afterApply(clone(applied.result));
const receipt = stagedReceipt || {
contractVersion: CONTRACT_VERSION,
operation,
requestId,
requestHash,
migrationId: current.identity.migrationId,
destination: clone(current.identity.destination),
committedStateVersion: nextVersion,
evidenceRevision: applied.state.currentRevisionId,
committedAt: effectiveNow,
result: clone(applied.result),
};
if (stagedReceipt && canonical(stagedReceipt.result) !== canonical(applied.result)) {
throw new MigrationReviewError('staged_receipt_conflict', `staged receipt ${requestId} no longer matches the recomputed mutation`);
}
await writeImmutable(receiptPath, receipt);
if (options.hooks && options.hooks.afterReceipt) await options.hooks.afterReceipt(clone(receipt));
const nextState = {
...applied.state,
stateVersion: nextVersion,
updatedAt: effectiveNow,
committedRequests: {
...applied.state.committedRequests,
[requestId]: { operation, requestHash, stateVersion: nextVersion },
},
};
await writeAtomic(path.join(current.root, 'state.json'), nextState);
if (options.hooks && options.hooks.afterCommit) await options.hooks.afterCommit(clone(receipt));
return { ...receipt, replayed: false };
} finally {
await releaseLock(lock);
}
}
async function initialize(projectDir, request, options = {}) {
const identity = await ensureIdentity(projectDir, request, options.clock, options.hooks);
const { state } = await loadIdentityAndState(projectDir);
return { contractVersion: CONTRACT_VERSION, identity, state };
}
async function registerExecution(projectDir, request, options = {}) {
assertToken(request.executionId, 'executionId');
if (request.executionKind !== undefined && !['import', 'probe'].includes(request.executionKind)) throw new MigrationReviewError('invalid_request', 'executionKind must be import or probe');
if (!Array.isArray(request.assessedScope) || request.assessedScope.length === 0 || !request.assessedScope.every((scope) => typeof scope === 'string' && scope.length)) throw new MigrationReviewError('invalid_request', 'assessedScope must name record or capability scopes');
return commitMutation(projectDir, 'register-execution', request, async ({ root, state }) => {
const existing = state.executions && state.executions[request.executionId];
const facts = { contractVersion: CONTRACT_VERSION, executionId: request.executionId, assessedScope: [...new Set(request.assessedScope)].sort(), artifactRefs: clone(request.artifactRefs || []), sourceSnapshot: clone(request.sourceSnapshot || null) };
if (request.producerId !== undefined) facts.producerId = assertToken(request.producerId, 'producerId');
if (request.executionKind !== undefined) facts.executionKind = request.executionKind;
if (existing) {
const stored = await loadExecution(root, state, request.executionId);
const comparable = { ...stored }; delete comparable.generation; delete comparable.registeredAt;
if (canonical(comparable) !== canonical(facts)) throw new MigrationReviewError('execution_collision', 'an execution identity cannot be rebound on resume');
return { apply: async ({ state: current }) => ({ state: current, result: stored }) };
}
const execution = { ...facts, generation: (state.generation || 0) + 1 };
return { apply: async ({ root: lockedRoot, state: current, now }) => {
const stored = { ...execution, registeredAt: now };
const objectHash = digest(stored);
await writeImmutable(path.join(lockedRoot, 'executions', `${objectHash}.json`), stored);
return { state: { ...current, generation: execution.generation, executions: { ...(current.executions || {}), [request.executionId]: { generation: execution.generation, objectHash } } }, result: stored };
} };
}, options);
}
async function loadExecution(root, state, executionId) {
if (!state.executions || !state.executions[executionId]) throw new MigrationReviewError('unknown_execution', 'execution registration is not committed');
const execution = await readJson(path.join(root, 'executions', `${state.executions[executionId].objectHash}.json`));
if (digest(execution) !== state.executions[executionId].objectHash) throw new MigrationReviewError('corrupt_store', 'execution bytes differ from committed hash');
if (execution.executionId !== executionId || state.executions[executionId].generation !== execution.generation) throw new MigrationReviewError('corrupt_store', 'committed execution identity differs');
return execution;
}
async function captureEvidence(projectDir, request, options = {}) {
assertToken(request.executionId, 'executionId'); assertToken(request.captureId, 'captureId');
if (!EVIDENCE_CATEGORIES.includes(request.category)) throw new MigrationReviewError('invalid_request', 'unknown evidence category');
assertObject(request.evidence, 'evidence');
if (!Object.keys(request.evidence).length) throw new MigrationReviewError('invalid_request', 'capture evidence must contain observed facts or explicit unknown areas');
if (request.category === 'source_scope' && request.evidence.acceptedScopes !== undefined && (!Array.isArray(request.evidence.acceptedScopes) || !request.evidence.acceptedScopes.every((scope) => typeof scope === 'string' && scope.length))) throw new MigrationReviewError('invalid_request', 'acceptedScopes must name accepted capability scopes, not record IDs');
if (request.category === 'source_scope') for (const scope of request.evidence.acceptedScopes || []) assertToken(scope, 'acceptedScopes entry');
return commitMutation(projectDir, 'capture-evidence', request, async ({ root, state }) => {
const execution = await loadExecution(root, state, request.executionId);
const scope = request.scope || execution.assessedScope;
if (!Array.isArray(scope) || !scope.length || scope.some((key) => !execution.assessedScope.includes(key))) throw new MigrationReviewError('scope_violation', 'capture scope is outside its committed execution');
const evidenceHash = digest(request.evidence);
const capture = { contractVersion: CONTRACT_VERSION, captureId: request.captureId, executionId: execution.executionId, generation: execution.generation, category: request.category, scope: [...new Set(scope)].sort(), phase: request.phase || 'import', sequence: Number.isInteger(request.sequence) ? request.sequence : 0, observedAt: request.observedAt || null, objectHash: evidenceHash };
if (state.captures && state.captures[request.captureId]) {
const [stored] = await validateCaptures(root, state, execution, [request.captureId]);
const comparable = { ...stored }; delete comparable.capturedAt; delete comparable.evidence;
if (canonical(comparable) !== canonical(capture)) throw new MigrationReviewError('capture_collision', 'a committed capture identity cannot be rebound');
return { apply: async ({ state: current }) => ({ state: current, result: capture }) };
}
return { apply: async ({ root: lockedRoot, state: current, now }) => {
await writeImmutable(path.join(lockedRoot, 'evidence', 'objects', evidenceHash), request.evidence);
const stored = { ...capture, capturedAt: now }, captureHash = digest(stored);
await writeImmutable(path.join(lockedRoot, 'evidence', 'captures', `${captureHash}.json`), stored);
return { state: { ...current, captures: { ...(current.captures || {}), [request.captureId]: { executionId: execution.executionId, category: capture.category, objectHash: evidenceHash, captureHash } } }, result: capture };
} };
}, options);
}
// One queue per importer process. Only producer-owned captures refresh their version;
// operator actions and publication retain the strict compare-and-swap interface.
function createEvidenceCaptureQueue(projectDir, options = {}) {
const lockWaitMs = options.lockWaitMs === undefined ? 2000 : options.lockWaitMs;
if (!Number.isSafeInteger(lockWaitMs) || lockWaitMs < 0 || lockWaitMs > 5000) throw new MigrationReviewError('invalid_request', 'capture lockWaitMs must be between 0 and 5000');
let pending = Promise.resolve();
return function enqueue(request) {
const input = clone(request);
pending = pending.then(async () => {
const deadline = performance.now() + lockWaitMs;
for (;;) {
const { root, state } = await loadIdentityAndState(projectDir);
try {
return await captureEvidence(projectDir, { ...input, expectedStateVersion: state.stateVersion }, options);
} catch (error) {
// These conflicts occur before a durable intent. Interrupted/staged intents
// have no stateVersion detail and must take the explicit recovery path.
if (error.code !== 'lock_held' && !(error.code === 'state_conflict' && Number.isSafeInteger(error.details?.stateVersion))) throw error;
do {
const remaining = deadline - performance.now();
if (remaining <= 0) throw error;
await new Promise((resolve) => setTimeout(resolve, Math.min(25, remaining)));
// Poll without staging or creating a fresh owner record while held.
} while (await lockOwner(path.join(root, '.lock')));
}
}
});
// A failed capture poisons this queue, preventing later batches from proceeding.
return pending;
};
}
// Every generated target write goes through this wrapper on the importer's one capture queue.
// A throw, or an outcome the caller names unknown, commits execution_recovery evidence before
// the error returns, so the next write cannot start ahead of it. A capture failure stops writes.
const PROBE_WRITE_FAILURE = 'probe_injected_write_failure';
function createWriteRecovery({ captureQueue, migrationId, executionId, scope, probeNonce = null }) {
if (typeof captureQueue !== 'function' || !Array.isArray(scope) || !scope.length) throw new MigrationReviewError('invalid_request', 'write recovery needs the importer capture queue and registered scope');
assertToken(migrationId, 'migrationId'); assertToken(executionId, 'executionId');
async function capture(meta, outcome) {
const evidence = { kind: 'write-failure', outcomes: [{ record: meta.sourceStableKey, attemptId: meta.attemptId || null, operation: meta.operation || null, ...outcome }], recovery: 'read-back-before-retry', ...(probeNonce ? { probeNonce } : {}) };
const id = `write-recovery:${digest({ executionId, scope, evidence })}`;
return captureQueue({ contractVersion: CONTRACT_VERSION, migrationId, executionId, requestId: id, captureId: id, category: 'execution_recovery', scope, phase: 'recovery', evidence });
}
async function write(meta, run, { unknownOutcome = () => null } = {}) {
assertToken(meta && meta.sourceStableKey, 'sourceStableKey');
// One identity per call: separate failures of one record never share a capture id.
meta = { ...meta, attemptId: meta.attemptId || crypto.randomUUID() };
let result;
try { result = await run(); } catch (error) {
// Only a readable 4xx rejection (not 408) is known failed; a 5xx, timeout or transport
// error may have written. Message text stays out.
const e = error || {};
const rejected = e.outcomeKnown === true && Number.isInteger(e.status) && e.status >= 400 && e.status < 500 && e.status !== 408;
const code = typeof e.code === 'string' && e.code ? e.code : ['AbortError', 'TimeoutError'].includes(e.name) ? e.name : null;
await capture(meta, { status: rejected ? 'failed' : 'uncertain', reasonCode: String(e.applicationErrorCode || code || (e.status ? `http_${e.status}` : 'unknown_error')), ...(Number.isInteger(e.status) ? { httpStatus: e.status } : {}) })
.catch((captureError) => { throw Object.assign(captureError, { writeError: error }); });
throw error;
}
const reason = unknownOutcome(result);
if (reason) await capture(meta, { status: 'uncertain', reasonCode: String(reason) });
return result;
}
async function probeFailure() {
if (!probeNonce) throw new MigrationReviewError('invalid_request', 'an injected write failure belongs only to the isolated probe');
await write({ sourceStableKey: 'probe-record', attemptId: 'probe-attempt', operation: 'probe' }, async () => { throw Object.assign(new Error('Injected probe write failure'), { code: PROBE_WRITE_FAILURE }); })
.catch((error) => { if (error.code !== PROBE_WRITE_FAILURE) throw error; });
}
return { write, probeFailure };
}
function materialFingerprint(task) {
return digest({
affected: [...(task.affected || [])].sort(),
// An ID preview is not a population count. Keep legacy fingerprints stable
// when a producer has not supplied this additive measurement.
...(Object.prototype.hasOwnProperty.call(task, 'affectedCount') ? { affectedCount: task.affectedCount } : {}),
intendedBehavior: task.intendedBehavior || null,
action: task.action || null,
requirement: task.requirement || (task.required === true ? 'required' : task.required === false ? 'optional' : null),
scopeKey: task.scopeKey,
consequence: task.consequence || null,
verificationPolicy: task.verificationPolicy || 'manual',
prerequisites: [...(task.prerequisites || [])].sort(),
unknownAreas: [...(task.unknownAreas || [])].sort(),
});
}
function normalizeFinding(item, producerId, kind) {
assertObject(item, kind);
const keyName = kind === 'task' ? 'findingKey' : 'resultKey';
const key = assertToken(item[keyName], `${kind}.${keyName}`);
const scopeKey = assertToken(item.scopeKey, `${kind}.scopeKey`);
const normalized = { ...clone(item), producerId, [keyName]: key, scopeKey };
if (kind === 'task') {
if (Object.prototype.hasOwnProperty.call(item, 'affectedCount')) {
if (item.affectedCount !== null && (!Number.isSafeInteger(item.affectedCount) || item.affectedCount < 0)) throw new MigrationReviewError('invalid_request', 'task.affectedCount must be null or a nonnegative safe integer');
if (Array.isArray(item.affected) && item.affectedCount !== null && item.affectedCount < item.affected.length) throw new MigrationReviewError('invalid_request', 'task.affectedCount cannot be smaller than its affected ID preview');
if (item.affectedCount === null) normalized.unknownAreas = [...new Set([...(item.unknownAreas || []), 'affected-count'])].sort();
}
if (!GROUPS.includes(item.group)) throw new MigrationReviewError('invalid_request', `task.group must be one of ${GROUPS.join(', ')}`);
// Applicability (not_applicable / superseded) closes a task; a publisher may only request it
// through applicabilityChanges, which require matching committed evidence. Never accepted inline.
if (producerId !== 'migration-review' && Object.prototype.hasOwnProperty.call(item, 'applicability')) throw new MigrationReviewError('invalid_disposition', 'task applicability is set only through evidence-backed applicabilityChanges');
const derivedTaskId = `task_${digest(`${producerId}:${key}`).slice(0, 20)}`;
// A task id is derived from its producer and finding key; a chosen id could occupy another
// owner's (including a service investigation's) slot and inherit its disposition.
if (item.taskId !== undefined && item.taskId !== derivedTaskId) throw new MigrationReviewError('identity_collision', 'task.taskId must be derived from its producer and finding key');
normalized.taskId = derivedTaskId;
assertToken(normalized.taskId, 'task.taskId');
normalized.materialFingerprint = materialFingerprint({ ...normalized, requirement: item.requirement || (item.required === true ? 'required' : item.required === false ? 'optional' : null) });
normalized.requirement = item.requirement || (item.required === true ? 'required' : item.required === false ? 'optional' : null);
if (!['required', 'optional'].includes(normalized.requirement)) throw new MigrationReviewError('invalid_request', 'task requirement must be explicit');
if (normalized.requirement === 'required' && !normalized.dismissalAcknowledgment) normalized.dismissalAcknowledgment = normalized.consequence;
if (normalized.requirement === 'required' && !normalized.dismissalAcknowledgment) throw new MigrationReviewError('invalid_request', 'required task must describe its consequence');
normalized.verificationPolicy = normalized.verificationPolicy || 'manual';
if (!['manual', 'machine_required'].includes(normalized.verificationPolicy)) throw new MigrationReviewError('invalid_request', 'unknown task verification policy');
normalized.evidenceStatus = normalized.evidenceStatus || 'unassessed';
} else {
normalized.resultId = item.resultId || `result_${digest(`${producerId}:${key}`).slice(0, 20)}`;
assertToken(normalized.resultId, 'result.resultId');
}
return normalized;
}
async function loadRevision(root, revisionId) {
if (!revisionId) return { contractVersion: CONTRACT_VERSION, revisionId: null, tasks: {}, results: {}, coverage: {}, captures: [], scopeFrontier: {} };
const revision = await readJson(path.join(root, 'revisions', safeName(revisionId), 'manifest.json'));
if (revision.revisionId !== revisionId) throw new MigrationReviewError('corrupt_store', 'revision path and identity disagree');
const content = Object.fromEntries(['completionReportDigest', 'tasks', 'results', 'coverage', 'captures', 'scopeFrontier'].map((key) => [key, revision[key]]));
if (`rev_${digest(content).slice(0, 24)}` !== revisionId) throw new MigrationReviewError('corrupt_store', 'revision content differs from its immutable identity');
return revision;
}
async function validateCaptures(root, state, execution, captureIds) {
const captures = [];
for (const captureId of captureIds || []) {
if (!state.captures || !state.captures[captureId] || state.captures[captureId].executionId !== execution.executionId) throw new MigrationReviewError('missing_evidence', 'capture is not committed for this execution');
const row = await readJson(path.join(root, 'evidence', 'captures', `${state.captures[captureId].captureHash}.json`), { optional: true });
if (!row || row.captureId !== captureId || row.executionId !== execution.executionId) {
throw new MigrationReviewError('missing_evidence', `capture ${captureId} does not belong to execution ${execution.executionId}`);
}
if (digest(row) !== state.captures[captureId].captureHash) throw new MigrationReviewError('evidence_integrity', 'capture metadata differs from its committed hash');
const objectPath = path.join(root, 'evidence', 'objects', row.objectHash);
const object = await readJson(objectPath, { optional: true });
if (!object || digest(object) !== row.objectHash) throw new MigrationReviewError('evidence_integrity', `capture ${captureId} has missing or changed evidence bytes`);
captures.push({ ...row, evidence: object });
}
return captures;
}
async function readExecutionEvidence(projectDir, executionId, captureIds) {
const { root, state } = await loadIdentityAndState(projectDir);
const execution = await loadExecution(root, state, executionId);
const ids = captureIds || Object.entries(state.captures || {}).filter(([, row]) => row.executionId === executionId).map(([id]) => id);
return { execution, captures: await validateCaptures(root, state, execution, ids) };
}
async function checkExecutionCaptureBoundaries(projectDir, executionId) {
const { execution, captures } = await readExecutionEvidence(projectDir, executionId);
const missing = execution.assessedScope.flatMap((scope) => missingImportBoundaries(captures, scope).map((phase) => ({ scope, phase })));
if (missing.length) throw new MigrationReviewError('missing_evidence', 'real execution is missing committed import-boundary captures', { executionId, missing });
return { contractVersion: CONTRACT_VERSION, executionId, assessedScope: execution.assessedScope, captureIds: captures.filter((row) => ['before-write', 'write-outcome'].includes(row.phase) && !row.evidence.probeNonce).map((row) => row.captureId) };
}
function missingImportBoundaries(captures, scope) {
return ['before-write', 'write-outcome'].filter((phase) => !captures.some((row) => row.phase === phase && row.scope.includes(scope) && !row.evidence.probeNonce));
}
function identityDestination(state) { return state.destination; }
function validateSetupLinks(task, destination) {
const links = [task.setupUrl, ...(task.links || []).map((link) => link.url)].filter(Boolean);
for (const link of links) {
let url; try { url = new URL(link); } catch { throw new MigrationReviewError('invalid_link', 'task link must be an absolute URL'); }
if (!['https:', 'http:'].includes(url.protocol)) throw new MigrationReviewError('invalid_link', 'task link must use HTTP or HTTPS');
const wixHost = url.hostname === 'wix.com' || url.hostname.endsWith('.wix.com');
const dashboard = /^\/dashboard(?:\/([^/]+))?(?:\/|$)/.exec(url.pathname);
const editor = url.hostname === 'editor.wix.com' || url.hostname === 'editorx.wix.com' || /(?:^|\/)(?:editor|editorx)(?:\/|$)/.test(url.pathname);
let dashboardSite;
try { dashboardSite = dashboard && decodeURIComponent(dashboard[1] || ''); } catch { throw new MigrationReviewError('invalid_link', 'dashboard site identifier is malformed'); }
const siteParams = ['metaSiteId', 'metasiteId', 'siteId'].flatMap((key) => url.searchParams.getAll(key));
if ((url.hostname === 'manage.wix.com' || (wixHost && (dashboard || editor))) &&
(url.protocol !== 'https:' || url.username || url.password ||
(dashboard ? dashboardSite !== destination.siteId : !editor || !siteParams.length) ||
siteParams.some((id) => id !== destination.siteId))) throw new MigrationReviewError('binding_mismatch', 'dashboard/editor link does not match the bound destination');
if ((!wixHost && (url.hostname.includes('wix.com') || siteParams.length && /editor|dashboard/.test(url.pathname)))) throw new MigrationReviewError('invalid_link', 'site management links must use a Wix host');
}
}
function validateVerification(task, captured) {
const proof = captured.find((row) => row.captureId === task.verificationCaptureId && row.category === 'explanations_checks' && row.scope.includes(task.scopeKey));
const check = proof && (proof.evidence.checks || []).find((item) => item.findingKey === task.findingKey && item.materialFingerprint === task.materialFingerprint && item.kind === 'machine' && item.status === 'passed' && item.checkId && Number.isFinite(Date.parse(item.observedAt)));
if (!check) throw new MigrationReviewError('verification_required', 'machine verification requires a committed check matching this finding and material scope');
}
// The committed captures named by a closing disposition still prove it for this task's current
// material facts (same check as applicabilityChanges, against each capture's own execution).
async function closingEvidenceHolds(root, state, task, applicability) {
const ids = Array.isArray(applicability.captureIds) ? applicability.captureIds : [];
for (const captureId of ids) {
const ref = state.captures && state.captures[captureId];
if (!ref) continue;
let capture;
try { [capture] = await validateCaptures(root, state, await loadExecution(root, state, ref.executionId), [captureId]); } catch { continue; }
if (capture && capture.scope.includes(task.scopeKey) && (capture.evidence.dispositions || []).some((item) => item.findingKey === task.findingKey && item.materialFingerprint === task.materialFingerprint && item.status === applicability.status)) return true;
}
return false;
}
async function publishEvidence(projectDir, request, options = {}) {
const producerId = assertToken(request.producerId, 'producerId');
if (producerId === 'migration-review' || producerId.startsWith('migration-review')) throw new MigrationReviewError('invalid_request', 'migration-review is reserved for service-owned scope investigations');
// Task ids are derived from `${producerId}:${findingKey}`; a ':' in the producer id would make
// that boundary ambiguous and let a crafted pair hash to another owner's task id.
if (producerId.includes(':')) throw new MigrationReviewError('invalid_request', 'producerId must not contain ":"');
assertToken(request.executionId, 'executionId');
if (request.completionReportDigest !== undefined) assertToken(request.completionReportDigest, 'completionReportDigest');
if (!Array.isArray(request.assessedScope) || request.assessedScope.length === 0) throw new MigrationReviewError('invalid_request', 'publish assessedScope must not be empty');
if (!request.coverage || !['complete', 'incomplete'].includes(request.coverage.status)) throw new MigrationReviewError('invalid_request', 'coverage.status must be complete or incomplete');
if (request.coverage.unknownAreas !== undefined && !Array.isArray(request.coverage.unknownAreas)) throw new MigrationReviewError('invalid_request', 'coverage unknownAreas must be a list');
return commitMutation(projectDir, 'publish-evidence', request, async ({ root, state }) => {
const execution = await loadExecution(root, state, request.executionId);
if (execution.producerId && execution.producerId !== producerId) throw new MigrationReviewError('scope_violation', 'publication producer differs from the registered execution owner');
const assessed = [...new Set(request.assessedScope)].sort();
for (const scope of assessed) if (!execution.assessedScope.includes(scope)) throw new MigrationReviewError('scope_violation', `execution did not register assessed scope ${scope}`);
const captured = (await validateCaptures(root, state, execution, request.captureIds || [])).filter((row) => execution.executionKind !== 'import' || !row.evidence.probeNonce);
const base = await loadRevision(root, state.currentRevisionId);
const tasks = clone(base.tasks || {});
const results = clone(base.results || {});
const taskByKey = new Map(Object.values(tasks).map((row) => [row.findingKey, row]));
const resultByKey = new Map(Object.values(results).map((row) => [row.resultKey, row]));
const frontier = clone(state.scopeFrontier || {});
for (const scope of assessed) {
const key = `${producerId}\u0000${scope}`;
const prior = frontier[key];
if (prior && Number(prior.generation) > Number(execution.generation)) {
throw new MigrationReviewError('stale_scope', `execution generation ${execution.generation} cannot replace generation ${prior.generation} for ${scope}`);
}
}
for (const scope of assessed) {
const prior = frontier[`${producerId}\u0000${scope}`];
const oldSource = prior && prior.sourceSnapshot;
const nextSource = execution.sourceSnapshot;
if (oldSource && canonical(oldSource) !== canonical(nextSource)) {
const previousAt = Date.parse(oldSource.observedAt), nextAt = Date.parse(nextSource && nextSource.observedAt);
if (!Number.isFinite(previousAt) || !Number.isFinite(nextAt) || nextAt <= previousAt) throw new MigrationReviewError('source_provenance_conflict', 'source observations cannot advance this scope without newer or explicitly reconciled provenance');
}
}
const normalizedTasks = (request.tasks || []).map((item) => normalizeFinding(item, producerId, 'task'));
if (normalizedTasks.some((row) => row.findingKey.startsWith('review-evidence:'))) throw new MigrationReviewError('invalid_request', 'coverage investigation keys are reserved for the service');
const normalizedResults = (request.results || []).map((item) => normalizeFinding(item, producerId, 'result'));
for (const rows of [normalizedTasks, normalizedResults]) {
const keys = rows.map((row) => row.findingKey || row.resultKey);
if (new Set(keys).size !== keys.length) throw new MigrationReviewError('identity_collision', 'publication contains duplicate finding/result keys');
}
for (const item of [...normalizedTasks, ...normalizedResults]) {
if (!assessed.includes(item.scopeKey)) throw new MigrationReviewError('scope_violation', `${item.scopeKey} is outside this publication's assessed scope`);
}
for (const task of normalizedTasks) {
const canonicalExisting = taskByKey.get(task.findingKey);
if (canonicalExisting && canonicalExisting.producerId !== producerId) {
throw new MigrationReviewError('shared_finding_requires_owner', `finding ${task.findingKey} is already owned by ${canonicalExisting.producerId}; reuse that canonical producer instead of duplicating it`, { taskId: canonicalExisting.taskId });
}
validateSetupLinks(task, identityDestination(state));
const earlier = taskByKey.get(task.findingKey);
if (earlier && (earlier.taskId !== task.taskId || earlier.scopeKey !== task.scopeKey)) throw new MigrationReviewError('identity_collision', 'a finding cannot change task identity or owned scope on rerun');
if (task.verificationPolicy === 'machine_required' && task.evidenceStatus === 'verified') validateVerification(task, captured);
const prior = tasks[task.taskId];
if (prior && (prior.producerId !== producerId || prior.findingKey !== task.findingKey)) throw new MigrationReviewError('identity_collision', `task identity collision for ${task.taskId}`);
// A republished task keeps an earlier closing disposition only while its identity and material
// facts are unchanged and the committed evidence that closed it still proves it.
if (prior && prior.applicability && ['not_applicable', 'superseded'].includes(prior.applicability.status)
&& prior.scopeKey === task.scopeKey && prior.materialFingerprint === task.materialFingerprint
&& await closingEvidenceHolds(root, state, task, prior.applicability)) task.applicability = clone(prior.applicability);
tasks[task.taskId] = task;
taskByKey.set(task.findingKey, task);
}
// A carried supersession still needs its replacement finding.
for (const task of normalizedTasks) {
const app = tasks[task.taskId].applicability;
if (app && app.status === 'superseded' && !Object.values(tasks).some((row) => row.findingKey === app.supersededBy && row.producerId === producerId && row.findingKey !== task.findingKey)) delete tasks[task.taskId].applicability;
}
for (const result of normalizedResults) {
const earlier = resultByKey.get(result.resultKey);
if (earlier && (earlier.producerId !== producerId || earlier.resultId !== result.resultId || earlier.scopeKey !== result.scopeKey)) throw new MigrationReviewError('identity_collision', 'a result key already has a canonical owner/identity/scope');
const prior = results[result.resultId];
if (prior && (prior.producerId !== producerId || prior.resultKey !== result.resultKey)) throw new MigrationReviewError('identity_collision', `result identity collision for ${result.resultId}`);
results[result.resultId] = result;
resultByKey.set(result.resultKey, result);
}
for (const scope of assessed) frontier[`${producerId}\u0000${scope}`] = { generation: execution.generation, executionId: execution.executionId, sourceSnapshot: execution.sourceSnapshot };
const coverage = clone(base.coverage || {});
for (const scope of assessed) {
const missing = EVIDENCE_CATEGORIES.filter((category) => !captured.some((row) => row.category === category && row.scope.includes(scope)));
const missingBoundaries = execution.executionKind === 'import' ? missingImportBoundaries(captured, scope) : [];
const sourceUnknown = captured.filter((row) => row.scope.includes(scope)).flatMap((row) => row.evidence.unknownAreas || row.evidence.unassessed || row.evidence.unknown || []);
if (sourceUnknown.some((item) => typeof item !== 'string')) throw new MigrationReviewError('invalid_request', 'unknown evidence areas must name scopes');
const unknownAreas = [...new Set([...(request.coverage.unknownAreas || []), ...sourceUnknown, ...missing.map((category) => `${scope}:${category}`), ...missingBoundaries.map((phase) => `${scope}:import-boundary:${phase}`)])].sort();
const key = `${producerId}\u0000${scope}`;
coverage[key] = { producerId, scopeKey: scope, status: missing.length || unknownAreas.length || request.coverage.status !== 'complete' ? 'incomplete' : 'complete', unknownAreas, missingCategories: missing };
if (execution.executionKind === 'import') Object.assign(coverage[key], { executionId: execution.executionId, missingBoundaries });
const findingKey = `review-evidence:${scope}`;
const investigation = normalizeFinding({ findingKey, scopeKey: scope, group: 'Data', title: `Review missing ${scope} evidence`, summary: 'Evidence coverage is incomplete; inspect the named missing areas before treating the report as complete.', affected: [], affectedCount: null, consequence: 'Unrecorded migration gaps may remain.', action: 'Investigate and capture the missing evidence.', requirement: 'required', verificationPolicy: 'machine_required', evidenceStatus: 'unassessed', unknownAreas }, producerId, 'task');
// The service opens or closes only its own investigation row under that id.
const held = tasks[investigation.taskId];
if (held && held.findingKey !== investigation.findingKey) throw new MigrationReviewError('identity_collision', `task ${investigation.taskId} is not the coverage investigation for ${scope}`);
if (coverage[key].status === 'incomplete') tasks[investigation.taskId] = investigation;
else if (held) tasks[investigation.taskId] = { ...held, applicability: { status: 'not_applicable', reason: 'Required coverage is now evidenced.', captureIds: request.captureIds } };
}
for (const change of request.applicabilityChanges || []) {
// Coverage investigations are service-owned: they close only when coverage is evidenced.
if (typeof change.findingKey === 'string' && change.findingKey.startsWith('review-evidence:')) throw new MigrationReviewError('invalid_disposition', 'coverage investigations close only when their coverage is evidenced');
const task = Object.values(tasks).find((row) => row.findingKey === change.findingKey && row.producerId === producerId);
if (!task || !assessed.includes(task.scopeKey) || !['not_applicable', 'superseded'].includes(change.status) || !change.reason) throw new MigrationReviewError('invalid_disposition', 'explicit disposition must identify an owned assessed finding and reason');
const proof = captured.find((row) => (change.captureIds || []).includes(row.captureId) && row.scope.includes(task.scopeKey) && (row.evidence.dispositions || []).some((item) => item.findingKey === task.findingKey && item.materialFingerprint === task.materialFingerprint && item.status === change.status));
if (!proof) throw new MigrationReviewError('missing_evidence', 'finding disposition requires evidence matching its current material scope');
if (change.status === 'superseded' && !Object.values(tasks).some((row) => row.findingKey === change.supersededBy && row.producerId === producerId && row.findingKey !== task.findingKey)) throw new MigrationReviewError('invalid_disposition', 'supersession requires an existing replacement finding');
tasks[task.taskId] = { ...task, applicability: { status: change.status, reason: change.reason, captureIds: change.captureIds, supersededBy: change.supersededBy || null } };
}
await reconcileExpectedScopes(root, state, coverage, tasks);
const captures = [...new Set([...(base.captures || []), ...(request.captureIds || [])])].sort();
const content = { completionReportDigest: request.completionReportDigest || null, tasks, results, coverage, captures, scopeFrontier: frontier };
const revisionId = `rev_${digest(content).slice(0, 24)}`;
const manifest = { contractVersion: CONTRACT_VERSION, revisionId, parentRevisionId: state.currentRevisionId, producerId, executionId: execution.executionId, generation: execution.generation, assessedScope: assessed, ...content };
return { apply: async ({ root: lockedRoot, state: lockedState, now }) => {
if (lockedState.currentRevisionId !== revisionId) {
const stored = { ...manifest, publishedAt: now };
const manifestPath = path.join(lockedRoot, 'revisions', safeName(revisionId), 'manifest.json');
const existing = await readJson(manifestPath, { optional: true });
if (existing && Object.keys(content).some((key) => canonical(existing[key]) !== canonical(content[key]))) throw new MigrationReviewError('immutable_collision', 'existing revision content differs');
await writeImmutable(manifestPath, existing || stored);
}
return {
state: { ...lockedState, currentRevisionId: revisionId, scopeFrontier: frontier },
result: { revisionId, coverage: clone(coverage), taskCount: Object.keys(tasks).length, resultCount: Object.keys(results).length },
};
} };
}, options);
}
// Account for committed scope declarations even when a publisher omits them. Older
// registrations without an owner require coverage from any producer; new ones bind it.
async function reconcileExpectedScopes(root, state, coverage, tasks) {
const executions = new Map();
const expected = new Map();
function add(execution, scope) {
const owner = execution.producerId || null;
const key = `${owner || '*'}\u0000${scope}`;
if (!expected.has(key) || execution.generation > expected.get(key).execution.generation) expected.set(key, { owner, scope, execution });
}
for (const id of Object.keys(state.executions || {})) {
const execution = await loadExecution(root, state, id);
executions.set(id, execution);
for (const scope of execution.assessedScope) add(execution, scope);
}
for (const [id, ref] of Object.entries(state.captures || {})) {
if (ref.category && ref.category !== 'source_scope') continue;
const [capture] = await validateCaptures(root, state, executions.get(ref.executionId), [id]);
if (capture.category === 'source_scope') for (const scope of capture.evidence.acceptedScopes || []) add(executions.get(ref.executionId), scope);
}
for (const [key, row] of Object.entries(coverage)) if (row.producerId === 'migration-review') delete coverage[key];
const published = Object.values(coverage);
const ownedScopes = new Set([...expected.values()].filter((row) => row.owner).map((row) => row.scope));
for (const { owner, scope, execution } of expected.values()) {
const findingKey = `review-evidence:unpublished:${digest({ owner, scope }).slice(0, 24)}`;
const taskId = `task_${digest(`migration-review:${findingKey}`).slice(0, 20)}`;
// Only the service's own row under this id may be opened or closed here.
if (tasks[taskId] && (tasks[taskId].producerId !== 'migration-review' || tasks[taskId].findingKey !== findingKey)) throw new MigrationReviewError('identity_collision', `task ${taskId} is not the service investigation for ${scope}`);
if (!owner && ownedScopes.has(scope)) {
if (tasks[taskId]) tasks[taskId] = { ...tasks[taskId], applicability: { status: 'not_applicable', reason: 'Coverage for this scope is now tracked by explicitly registered producers.' } };
continue;
}
if (published.some((row) => row.scopeKey === scope && (!owner || row.producerId === owner) && (execution.executionKind !== 'import' || row.executionId === execution.executionId))) {
if (tasks[taskId]) tasks[taskId] = { ...tasks[taskId], applicability: { status: 'not_applicable', reason: 'This scope now has published coverage; any missing categories remain separate investigations.' } };
continue;
}
const unknownAreas = [`${scope}:unpublished${owner ? `:${owner}` : ''}`];
coverage[`migration-review\u0000${owner || '*'}\u0000${scope}`] = { producerId: 'migration-review', expectedProducerId: owner, scopeKey: scope, status: 'incomplete', unknownAreas, missingCategories: [...EVIDENCE_CATEGORIES] };
tasks[taskId] = normalizeFinding({ findingKey, scopeKey: scope, group: 'Data', title: `Publish missing ${scope} evidence`, summary: 'A registered or accepted scope has no published coverage.', affected: [], affectedCount: null, action: 'Capture and publish evidence for this scope in the migration environment.', requirement: 'required', consequence: 'Unrecorded migration gaps may remain.', verificationPolicy: 'machine_required', evidenceStatus: 'unassessed', unknownAreas }, 'migration-review', 'task');
}
}
async function loadActions(root, state) {
const rows = [];
for (const actionId of state.actionIds || []) rows.push(await readJson(path.join(root, 'actions', `${safeName(actionId)}.json`)));
return rows;
}
function projectTask(task, relevant) {
const current = relevant[relevant.length - 1] || null;
let disposition = 'open';
if (current && current.action === 'resolve') disposition = current.materialFingerprint === task.materialFingerprint ? 'resolved' : 'needs_review';
if (current && current.action === 'dismiss') disposition = current.materialFingerprint === task.materialFingerprint ? 'dismissed' : 'needs_review';
if (current && current.action === 'reopen') disposition = 'open';
if (task.verificationPolicy === 'machine_required' && task.evidenceStatus === 'verified' && task.verificationCaptureId) disposition = 'resolved';
if (task.applicability && ['not_applicable', 'superseded'].includes(task.applicability.status)) disposition = 'resolved';
return { ...clone(task), disposition, resolutionSource: task.applicability && ['not_applicable', 'superseded'].includes(task.applicability.status) ? 'reconciled_evidence' : task.verificationPolicy === 'machine_required' && task.evidenceStatus === 'verified' && task.verificationCaptureId ? 'machine_verification' : current ? 'operator_action' : null, latestAction: clone(current), actionHistory: clone(relevant) };
}
async function readReport(projectDir) {
const { root, identity, state } = await loadIdentityAndState(projectDir);
const revision = await loadRevision(root, state.currentRevisionId);
await reconcileExpectedScopes(root, state, revision.coverage, revision.tasks);
const actions = await loadActions(root, state);
const actionsByTask = new Map();
for (const action of actions) {
if (!actionsByTask.has(action.taskId)) actionsByTask.set(action.taskId, []);
actionsByTask.get(action.taskId).push(action);
}
const tasks = Object.values(revision.tasks || {}).map((task) => projectTask(task, actionsByTask.get(task.taskId) || []));
const coverageRows = Object.entries(revision.coverage || {});
const incomplete = coverageRows.filter(([, row]) => row.status !== 'complete');
return {
contractVersion: CONTRACT_VERSION,
migrationId: identity.migrationId,
source: clone(identity.source),
destination: clone(identity.destination),
stateVersion: state.stateVersion,
evidenceRevision: state.currentRevisionId,
completionReportDigest: revision.completionReportDigest || null,
coverage: { status: coverageRows.length > 0 && incomplete.length === 0 ? 'complete' : 'incomplete', producers: clone(revision.coverage || {}), unknownAreas: incomplete.flatMap(([, row]) => row.unknownAreas || []) },
results: Object.values(revision.results || {}).map(clone),
tasks,
updatedAt: state.updatedAt,
};
}
async function listTasks(projectDir, options = {}) {
const report = await readReport(projectDir);
let rows = report.tasks;
if (options.group) rows = rows.filter((task) => task.group === options.group);
if (options.disposition) rows = rows.filter((task) => task.disposition === options.disposition);
rows.sort((a, b) => GROUPS.indexOf(a.group) - GROUPS.indexOf(b.group) || String(a.title).localeCompare(String(b.title)) || a.taskId.localeCompare(b.taskId));
const offset = Math.max(0, Number(options.offset) || 0);
const limit = Math.min(200, Math.max(1, Number(options.limit) || 50));
return { contractVersion: CONTRACT_VERSION, migrationId: report.migrationId, stateVersion: report.stateVersion, evidenceRevision: report.evidenceRevision, total: rows.length, offset, limit, tasks: rows.slice(offset, offset + limit), groupTotals: Object.fromEntries(GROUPS.map((group) => [group, report.tasks.filter((task) => task.group === group).length])) };
}
async function readTask(projectDir, taskId) {
const report = await readReport(projectDir);
const task = report.tasks.find((row) => row.taskId === taskId);
if (!task) throw new MigrationReviewError('unknown_task', `task ${taskId} does not exist in the committed revision`);
return { contractVersion: CONTRACT_VERSION, migrationId: report.migrationId, stateVersion: report.stateVersion, evidenceRevision: report.evidenceRevision, task };
}
async function recordAction(projectDir, request, options = {}) {
if (request.action === 'later') {
assertVersion(request); assertToken(request.requestId, 'requestId'); assertToken(request.taskId, 'taskId');
const report = await readReport(projectDir);
validateBinding(request, report);
if (request.expectedStateVersion !== report.stateVersion) throw new MigrationReviewError('state_conflict', 'review changed before Later');
const task = report.tasks.find((item) => item.taskId === request.taskId);
if (!task) throw new MigrationReviewError('unknown_task', 'Later requires an existing task');
if (task.materialFingerprint !== request.materialFingerprint) throw new MigrationReviewError('stale_task', 'task facts changed before Later');
return { contractVersion: CONTRACT_VERSION, operation: 'record-action', requestId: request.requestId, committedStateVersion: report.stateVersion, replayed: false, changed: false, result: { action: 'later', taskId: request.taskId, disposition: task.disposition } };
}
if (!['resolve', 'dismiss', 'reopen'].includes(request.action)) throw new MigrationReviewError('invalid_request', 'action must be resolve, dismiss, reopen or later');
assertToken(request.taskId, 'taskId');
assertToken(request.actor, 'actor');
return commitMutation(projectDir, 'record-action', request, async ({ root, state }) => {
const revision = await loadRevision(root, state.currentRevisionId);
await reconcileExpectedScopes(root, state, revision.coverage, revision.tasks);
const task = revision.tasks && revision.tasks[request.taskId];
if (!task) throw new MigrationReviewError('unknown_task', `task ${request.taskId} does not exist in the committed revision`);
if (request.materialFingerprint !== task.materialFingerprint) throw new MigrationReviewError('stale_task', 'task material facts changed; re-read before recording an action', { materialFingerprint: task.materialFingerprint });
if (request.action === 'resolve' && task.verificationPolicy === 'machine_required' && task.evidenceStatus !== 'verified') {
throw new MigrationReviewError('verification_required', 'machine-required task cannot be manually resolved without matching verified evidence');
}
if (request.action === 'dismiss' && task.dismissalAcknowledgment && request.consequenceAcknowledgment !== task.dismissalAcknowledgment) {
throw new MigrationReviewError('acknowledgment_required', 'dismissal requires the current consequence acknowledgment');
}
const actionId = `action_${safeName(request.requestId)}`;
return { apply: async ({ root: lockedRoot, state: lockedState, now }) => {
const event = { contractVersion: CONTRACT_VERSION, actionId, taskId: task.taskId, action: request.action, actor: options.actor || 'local_operator', actorLabel: request.actor, actorKind: 'local_operator', materialFingerprint: task.materialFingerprint, evidenceRevision: lockedState.currentRevisionId, note: request.note || null, consequenceAcknowledgment: request.consequenceAcknowledgment || null, recordedAt: now };
await writeImmutable(path.join(lockedRoot, 'actions', `${safeName(actionId)}.json`), event);
return { state: { ...lockedState, actionIds: [...lockedState.actionIds, actionId] }, result: { taskId: task.taskId, actionId, disposition: request.action === 'resolve' ? 'resolved' : request.action === 'dismiss' ? 'dismissed' : 'open' } };
} };
}, options);
}
async function overview(projectDir) {
const report = await readReport(projectDir);
const counts = { open: 0, resolved: 0, dismissed: 0, needs_review: 0 };
for (const task of report.tasks) counts[task.disposition] += 1;
const groups = GROUPS.map((group) => ({ group, tasks: report.tasks.filter((task) => task.group === group && ['open', 'needs_review'].includes(task.disposition)).map((task) => ({ taskId: task.taskId, title: task.title, explanation: task.summary || task.consequence || '', affectedCount: Object.prototype.hasOwnProperty.call(task, 'affectedCount') ? task.affectedCount : (task.unknownAreas || []).includes('affected-count') ? null : Array.isArray(task.affected) ? task.affected.length : null, disposition: task.disposition, dependencies: clone(task.prerequisites || []) })) }));
const recommended = report.tasks.find((task) => task.disposition === 'needs_review') || report.tasks.find((task) => task.disposition === 'open') || null;
return { contractVersion: CONTRACT_VERSION, migrationId: report.migrationId, destination: report.destination, coverage: report.coverage, counts, groups, completeInventory: true, recommendedTaskId: recommended ? recommended.taskId : null, evidenceCoverageComplete: report.coverage.status === 'complete', reviewWorkRemaining: counts.open + counts.needs_review, nextOverallStep: 'Move to Wix (integration deferred)' };
}
async function prepareHandoff(projectDir, request, options = {}) {
const receipt = await commitMutation(projectDir, 'prepare-handoff', request, async ({ state }) => {
const report = await readReport(projectDir);
if (!state.currentRevisionId) throw new MigrationReviewError('missing_publication', 'a review recap requires a committed publication');
if (report.stateVersion !== state.stateVersion) throw new MigrationReviewError('state_conflict', 'review changed during recap preparation');
return { apply: async ({ root, state: current, nextVersion, now }) => {
const recap = { contractVersion: CONTRACT_VERSION, handoffVersion: 1, migrationId: report.migrationId, source: report.source, destination: report.destination, evidenceRevision: report.evidenceRevision, completionReportDigest: report.completionReportDigest, committedStateVersion: nextVersion, coverage: report.coverage, taskDispositions: report.tasks.map((task) => ({ taskId: task.taskId, materialFingerprint: task.materialFingerprint, disposition: task.disposition, consequenceAcknowledgment: task.latestAction && task.latestAction.consequenceAcknowledgment || null })), unresolved: report.tasks.filter((task) => ['open', 'needs_review'].includes(task.disposition)).map((task) => task.taskId), acceptedConsequences: report.tasks.filter((task) => task.disposition === 'dismissed').map((task) => ({ taskId: task.taskId, acknowledgment: task.latestAction.consequenceAcknowledgment || null })), consumerIntegration: { status: 'deferred' } };
const handoffId = `handoff_${digest(recap).slice(0, 24)}`;
const stored = { ...recap, handoffId, preparedAt: now };
await writeImmutable(path.join(root, 'handoffs', `${safeName(handoffId)}.json`), stored);
return { state: { ...current, latestHandoffId: handoffId }, result: stored };
} };
}, options);
return { ...receipt.result, receipt };
}
async function readLatestHandoff(projectDir) {
const { root, identity, state } = await loadIdentityAndState(projectDir);
if (!state.latestHandoffId) return null;
const recap = await readJson(path.join(root, 'handoffs', `${safeName(state.latestHandoffId)}.json`));
if (recap.handoffId !== state.latestHandoffId || recap.migrationId !== identity.migrationId || recap.destination.siteId !== identity.destination.siteId || recap.committedStateVersion > state.stateVersion) throw new MigrationReviewError('corrupt_store', 'committed recap binding differs');
const content = { ...recap }; delete content.handoffId; delete content.preparedAt;
if (`handoff_${digest(content).slice(0, 24)}` !== recap.handoffId) throw new MigrationReviewError('corrupt_store', 'recap content differs from its immutable identity');
return recap;
}
function generatedImporterReviewContract() {
return {
contractVersion: CONTRACT_VERSION,
executionKind: 'import',
phases: ['before-write', 'write-outcome', 'recovery'],
requiredCategories: ['source_scope', 'mapping_fidelity', 'execution_recovery', 'setup_plugin', 'explanations_checks'],
durableBeforeNextWrite: true,
probeFlag: '--migration-review-probe',
};
}
function validateGeneratedImporterReviewContract(value) {
const expected = generatedImporterReviewContract();
const problems = [];
if (!value || typeof value !== 'object' || Array.isArray(value)) return ['migrationReview must declare the durable evidence capture contract'];
if (value.contractVersion !== expected.contractVersion) problems.push(`migrationReview.contractVersion must be "${expected.contractVersion}"`);
if (value.executionKind !== expected.executionKind) problems.push('migrationReview.executionKind must be "import"');
for (const phase of expected.phases) if (!Array.isArray(value.phases) || !value.phases.includes(phase)) problems.push(`migrationReview missing phase: ${phase}`);
for (const category of expected.requiredCategories) if (!Array.isArray(value.requiredCategories) || !value.requiredCategories.includes(category)) problems.push(`migrationReview missing evidence category: ${category}`);
if (value.durableBeforeNextWrite !== true) problems.push('migrationReview.durableBeforeNextWrite must be true');
if (value.probeFlag !== expected.probeFlag) problems.push(`migrationReview.probeFlag must be "${expected.probeFlag}"`);
return problems;
}
module.exports = {
CONTRACT_VERSION,
EVIDENCE_CATEGORIES,
materialFingerprint,
projectBinding,
GROUPS,
STORE_DIR,
MigrationReviewError,
canonical,
digest,
reviewRoot,
initialize,
registerExecution,
captureEvidence,
createEvidenceCaptureQueue,
createWriteRecovery,
PROBE_WRITE_FAILURE,
readExecutionEvidence,
checkExecutionCaptureBoundaries,
publishEvidence,
readReport,
listTasks,
readTask,
recordAction,
overview,
prepareHandoff,
readLatestHandoff,
recoverLock,
generatedImporterReviewContract,
validateGeneratedImporterReviewContract,
};
Requirements and compatibility
Works with: Claude Code, Claude apps, OpenAI Codex, GitHub Copilot, Cursor, Gemini CLI, Kiro, OpenCode, Goose, Amp, Roo Code, JetBrains Junie, Factory Droid, Devin (ex-Windsurf), Mistral Vibe, OpenHands, Letta
“Works with” means the tool reads the open Agent Skills format; a skill written for one tool may use features others lack.
Needs: Needs Node.js
Security
What our scanner noticed· Low risk
- Scripts that your agent may run
This skill includes scripts that your agent may run. Read them before you use it.
A static scan is not a guarantee. Skills are third-party content: review a skill before you let an agent use it.
Attribution and licence
- Published by
- Wix
- Publisher
- Wix (Official)
- Source
- wix/skills · skills/wix-replatform · commit d168bc2
- Licence
- MIT License (redistribution allowed)
- LICENSE (added by AutoGineer from LICENSE)
- ATTRIBUTION.md (written by AutoGineer)
- Synced
AutoGineer is not affiliated with or endorsed by Wix or Wix.
Download ZIP
The ZIP holds the skill’s files, its licence files and an ATTRIBUTION.md. Download wix-replatform.zip