Generated banner for Wix Replatform
Generated logo for Wix Replatform

Wix Replatform

WixOfficialPublished by Wix

  • Skill
  • Workflow
  • Automation and integrations
  • MIT
  • Low risk
  • Has scripts

From the author

Routes RePlatform source-to-Wix migrations to the next required workflow module by inspecting authoritative migration artifacts. Use when starting, resuming, or recovering a migration run.
— the description by Wix

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

  1. Download the skill as a ZIP:

    https://autogineer.com/agent-skills/wix/wix-replatform-fya3/download
  2. Unzip 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>/ (or REPLATFORM_MIGRATIONS_DIR) before inspecting or creating anything. Only that project's artifacts are authoritative.

  • Preserve EXPECTATIONS.md at 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 review

Delivery 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:

  1. Resolve the project and initialize/load its orchestration artifacts.

  2. Run node skills/wix-replatform/scripts/orchestration-route.js <projectDir>.

  3. Dispatch the nextResource to 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 read state/attempts/write-attempts.ndjson raw; use scripts/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.

  4. 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.

  5. 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.md and mapping-gaps.json, or execution/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 — see rp-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.

  6. 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_rejected is blocked before any setup or import write, and mapping_approval_rejected returns to rp-mapper for 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) is blocked with errors and the producer as nextResource. 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) carry importProgress. interrupted lists recoveryArtifacts: resume it, never a second import. interruptedReport: "missing": first run scripts/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 situationDispatch
project, delivery mode, source mode, or grouped intake is unresolvedresources/rp-project-intake/
destination is absent, conflicting, or needs creation/adoptionresources/rp-destination/
source URL/files/config/acquisition mode/credentials are required, including source credentials said to exist already on another siteresources/rp-source-inputs/
deterministic preflight is missing, repaired, or blocked on CLI checksresources/rp-preflight/
quick mode needs platform resolution, endpoint preflight, plan, extraction, or importselected resources/rp-quick-<platform>/
source schema is missingresources/rp-discovery/ and its selected source adapter
mapping is missing or needs reviewresources/rp-mapper/
Wix requirements are missingresources/rp-setup-discovery/
import code/review is missing or staleresources/rp-import-codegen/
setup verification is missingresources/rp-execute-setup/
backend completion report is missingresources/rp-execute-import/
frontend-only work, or integrated website handoff/build/gap loop/release receipt is incompleteresources/rp-website-continuation/
aggregate completion is missingresources/rp-execution-policy/
aggregate completion exists but the local review publication or current recap is missingresources/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_website mode 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 completed with completion/migration-completion.json, or blocked with 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.

  • website means frontend only; management_and_website means 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 by rp-destination's coordinator and is never published, unpublished or handed to wix-headless-replatform; its website and publication remain operator handoff items.

  • Do not implement a storefront manually when wix-headless-replatform is 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

lib/read-back-contract.js

'use strict';

const fs = require('node:fs');
const path = require('node:path');
const crypto = require('node:crypto');
const {
  hashArtifact
} = require('./artifact-freshness');
const state = require('./read-back-state');
const {
  GUID
} = require('./operator-identity');
const DOMAINS = {
  order: 'ecom',
  member: 'members',
  contact: 'crm',
  product: 'stores'
};
const L = require('./source-location');
const INVENTORY_SCHEMA_VERSION = 2;
// One shared location grammar (lib/source-location.js). Legacy [n] indexes normalize to [].
const canonical = p => L.canonicalize(String(p));
const digest = value => crypto.createHash('sha256').update(JSON.stringify(value)).digest('hex');
const populated = L.populated;
const compareKeys = (a, b) => Buffer.compare(Buffer.from(String(a)), Buffer.from(String(b)));
function entityKey(entity) {
  if (!Object.hasOwn(DOMAINS, entity)) throw new Error('Unsupported read-back entity');
  return entity;
}
function domainPath(entity) {
  return path.join(__dirname, '../resources/rp-target-wix/domains', DOMAINS[entityKey(entity)], 'entities', `${entity}.json`);
}
function domainContract(entity) {
  const file = domainPath(entity);
  const d = JSON.parse(fs.readFileSync(file, 'utf8'));
  return {
    intendedFields: d.intendedFields,
    readBackProjection: d.readBackProjection,
    domainDigest: hashArtifact(path.dirname(file), path.basename(file))
  };
}
function bindingPaths(entity) {
  entityKey(entity);
  return ['mapping/mapping-plan.json', `mapping/entity-decisions/${entity}.json`, 'mapping/mapping-resolution.json', `execution/read-back-inventory/${entity}.json`];
}
function bindings(projectDir, entity) {
  return Object.fromEntries(bindingPaths(entity).map(p => {
    const hash = hashArtifact(projectDir, p);
    if (!hash) throw new Error(`read-back binding missing: ${p}`);
    return [p, hash];
  }));
}
function leafPaths(value, { templates = [] } = {}) {
  return L.leafLocations(value, { templates });
}
const valuesAt = (value, location) => L.valuesAt(value, location);
function planEntity(plan, entity) {
  return require('./mapping-field-contract').entityRows(plan || {}).find(e => [e.entity, e.sourceEntity, e.name, e.target?.entity].includes(entity)) || null;
}
// Selector templates come only from the reviewed coverage's dynamic entries. Two different
// templates for one array are ambiguous and fail closed.
function selectorTemplates(plan, entity) {
  const row = planEntity(plan, entity), byArray = new Map();
  for (const item of row?.discoveryCoverage?.paths || []) {
    if (!item || typeof item !== 'object' || item.dynamic !== true) continue;
    const t = L.template(item), prior = byArray.get(t.array);
    if (prior && (prior.keyPath !== t.keyPath || prior.value !== t.value)) throw Object.assign(new Error(`Ambiguous selector templates for ${t.array}`), { code: 'selector_template_ambiguous' });
    byArray.set(t.array, t);
  }
  return [...byArray.values()].sort((a, b) => compareKeys(a.path, b.path));
}
// Draft helper (read-only): every inventoried location under a selected metadata value that no
// decision claims, grouped per selected value into an UNRESOLVED stub. It never excludes,
// approves or writes; the mapper decides each stub and keeps every leaf as an exact location.
function draftSelectorDescendantStubs(plan, entity, inv) {
  const fields = require('./mapping-field-contract'), row = planEntity(plan, entity);
  const claimed = new Set((row?.fieldMappings || []).flatMap(d => { try { return fields.decisionLocations(d); } catch { return []; } }));
  const groups = new Map();
  for (const p of Object.keys(inv?.sourcePaths || {}).sort(compareKeys)) {
    if (claimed.has(p)) continue;
    const segs = L.parse(p), at = segs.findIndex(s => s.where);
    if (at < 0 || at + 1 >= segs.length) continue;
    const root = L.format(segs.slice(0, at + 2));
    if (!groups.has(root)) groups.set(root, []);
    groups.get(root).push(p);
  }
  return [...groups.entries()].map(([root, sourcePaths]) => ({
    id: `selector-descendants-${fields.digest(root).slice(0, 12)}`,
    disposition: 'unresolved',
    sourcePaths,
    evidence: [{ kind: 'read-back-inventory', ref: `execution/read-back-inventory/${entity}.json` }],
    reason: `Populated locations under ${L.readable(root)} were inventoried but no decision claims them; decide each exactly.`,
    suggestedDecisionRefs: (row?.fieldMappings || []).filter(d => { try { return fields.decisionLocations(d).includes(root); } catch { return false; } }).map(d => d.id).sort(),
    inventoryCounts: Object.fromEntries(sourcePaths.map(p => [p, inv.sourcePaths[p]])),
    draft: true
  }));
}
// The pre-approval builder is a pure record -> request-body function. It is prepared from
// draft decisions before approval, so it must not reach a Wix client, capability, executor,
// network or process API. This is a deterministic static refusal, not a sandbox.
const IMPURE_BUILDER = [
  [/\brequire\s*\(\s*['"](?:node:)?(?:http|https|http2|net|tls|dgram|dns|child_process|worker_threads|cluster)['"]/, 'network or process module'],
  [/\bimport\b[^;]*['"](?:node:)?(?:http|https|http2|net|tls|dgram|dns|child_process|worker_threads|cluster)['"]/, 'network or process module'],
  [/\bfetch\s*\(|\bXMLHttpRequest\b|\bWebSocket\b/, 'network call'],
  [/wix-writers|createWixClient|wix-client|wix-http|dependency-gate|dependency-readiness|execution-capabilit|import-executor|run-import|issueCapability|withCapability|gateBatch|executeCanaries|ReadBackGate|writeFile|appendFile|\bprocess\.env\b/, 'Wix client, capability, executor or write wiring']
];
function assertPureBuilder(sourceText) {
  for (const [pattern, what] of IMPURE_BUILDER) if (pattern.test(sourceText)) throw Object.assign(new Error(`builder is not pure: it references ${what}`), { code: 'builder_not_pure' });
  return true;
}
const templateKey = templates => templates.map(t => ({ path: t.path, keyPath: t.keyPath }));
// Declared output applicability: the exact predicates an inventory was measured
// under. A change to any of them makes that inventory stale.
const applicabilityKey = rows => rows.filter(r => r.appliesWhen !== undefined)
  .map(r => [String(r.canonicalField), canonical(r.writePath), r.appliesWhen]).sort((a, b) => compareKeys(a[0], b[0]));
function assertApplicabilityShape(rows) {
  const A = require('./output-applicability');
  for (const r of rows) if (r.appliesWhen !== undefined) {
    const errors = A.validate(r.appliesWhen, [...A.locations(r.appliesWhen)]);
    if (errors.length) throw Object.assign(new Error(`Invalid output applicability ${r.canonicalField}: ${errors.join('; ')}`), { code: 'applies_when_invalid' });
  }
}
// One record: which outputs the builder must write (applicable and fed by a populated source)
// and which it must not write (every row at that write path declares appliesWhen and none
// applies). Rows without appliesWhen keep the former rule: always applicable. Outputs sharing a
// write path are judged together; writer-guard paths are excluded by the caller.
function outputApplicability(rows, record, expected, { guarded = [], declaredOnly = false } = {}) {
  const A = require('./output-applicability'), groups = new Map(), states = [], missing = [], unexpected = [];
  for (const row of rows) {
    if (guarded.includes(row.writePath)) continue;
    if (!groups.has(row.writePath)) groups.set(row.writePath, []);
    groups.get(row.writePath).push(row);
  }
  for (const [writePath, group] of groups) {
    const at = group.map(row => ({ row, declared: row.appliesWhen !== undefined,
      applies: row.appliesWhen === undefined || A.evaluate(row.appliesWhen, record),
      fed: (row.sourcePaths || []).some(p => valuesAt(record, p).some(populated)) }));
    const emitted = valuesAt(expected, writePath).some(populated);
    const required = at.filter(s => s.applies && s.fed && (!declaredOnly || s.declared));
    if (!emitted && required.length) missing.push(...required.map(s => s.row));
    if (emitted && at.every(s => s.declared && !s.applies)) unexpected.push(...at.map(s => s.row));
    for (const s of at) states.push({ ...s, emitted });
  }
  return { states, missing, unexpected };
}
// A decided hold: the generated pure builder returns `{ status: 'held', holds: [{ decisionId,
// code }] }` for a record it will not write. Returns the sorted `decisionId:code` reasons, or
// null for a writable body. A hold without a reason per entry is refused.
// With `decisionIds`, each hold must name one of those decisions.
function builderHold(body, decisionIds = null) {
  if (!body || typeof body !== 'object' || body.status !== 'held' && !(Array.isArray(body.holds) && body.holds.length)) return null;
  const named = v => typeof v === 'string' && v.trim() !== '';
  const reasons = (Array.isArray(body.holds) ? body.holds : []).map(h => named(h?.decisionId) && named(h?.code) ? `${h.decisionId}:${h.code}` : null);
  if (body.status !== 'held' || !reasons.length || reasons.includes(null)) throw Object.assign(new Error('Builder hold requires status held and a decisionId and code per hold'), { code: 'builder_hold_invalid' });
  if (decisionIds && body.holds.some(h => !decisionIds.has(h.decisionId))) throw Object.assign(new Error('Builder hold names a decision that is not in the current plan for this entity'), { code: 'builder_hold_unknown_decision' });
  return [...new Set(reasons)].sort(compareKeys);
}
// Hold codes the toolkit has retired (target knowledge `retiredHoldCodes`): a shipped route now writes
// these records, so a builder that still holds one is stale generated code, never a decision. The
// inventory refuses it before import.
let retiredCache = null;
function retiredHoldCodes() { return retiredCache ||= require('../resources/rp-target-wix/lib/domain-knowledge.js').retiredHoldCodes(); }
function retiredHolds(body) {
  return (Array.isArray(body?.holds) ? body.holds : []).filter(h => Object.hasOwn(retiredHoldCodes(), h?.code)).map(h => h.code);
}
// The entity's canonical decision IDs in the current exact plan. A plan without fieldContractVersion
// 1 has none, so it cannot hold a record.
function planDecisionIds(plan, entity) {
  return new Set(plan?.fieldContractVersion === 1 ? (planEntity(plan, entity)?.fieldMappings || []).map(d => d.id).filter(Boolean) : []);
}
const HELD_START = entity => `<!-- read-back-held:${entity}:start -->`, HELD_END = entity => `<!-- read-back-held:${entity}:end -->`;
// The held set the approver reviews: one section per entity in both mapping review files, rewritten
// by every inventory (removed when nothing is held). The inventory digest binds the same set.
function writeHeldReview(projectDir, entity, held, inventorySha256) {
  const start = HELD_START(entity), end = HELD_END(entity);
  const block = held ? [start, `## Held records: ${entity}`, '',
    `${held.count} ${entity} records are held by the builder and not written. Each record and its reasons: \`execution/read-back-inventory/${entity}.json\` (\`records[].held\`, sha256 \`${inventorySha256}\`). Approval of this mapping covers this held set.`, '',
    '| Decision:code | Records |', '| --- | --- |', ...Object.entries(held.reasons).map(([r, n]) => `| ${r} | ${n} |`), end].join('\n') : '';
  for (const name of ['mapping-summary.md', 'mapping-plan.md']) {
    const file = path.join(projectDir, 'mapping/review', name);
    let text = fs.existsSync(file) ? fs.readFileSync(file, 'utf8') : '';
    if (!block && !text.includes(start)) continue;
    if (text.includes(start) && text.includes(end)) text = text.slice(0, text.indexOf(start)).trimEnd() + text.slice(text.indexOf(end) + end.length);
    fs.mkdirSync(path.dirname(file), { recursive: true });
    fs.writeFileSync(`${file}.writing`, `${text.trimEnd()}${block ? `\n\n${block}` : ''}\n`);
    fs.renameSync(`${file}.writing`, file);
  }
}
// The held set dependency readiness may treat as settled: the current inventory's
// per-record holds, valid against the current plan (the checks below), named by this inventory's
// sha256 in the reviewed held block, and bound -- inventory and plan digests -- by the current
// identified mapping approval. A missing inventory holds nothing; any other mismatch is not
// current, so no hold is honored.
async function approvedHeldRecords(projectDir, entity, plan) {
  entityKey(entity);
  const file = `execution/read-back-inventory/${entity}.json`, notCurrent = { ok: false, reason: 'held-set-not-current', held: new Map() };
  let inv;
  try { inv = JSON.parse(fs.readFileSync(path.join(projectDir, file), 'utf8')); } catch (e) { return e.code === 'ENOENT' ? { ok: true, held: new Map() } : notCurrent; }
  if (!inv || inv.entity !== entity || !Array.isArray(inv.records) || inv.schemaVersion !== INVENTORY_SCHEMA_VERSION || inv.locationEncoding !== L.ENCODING) return notCurrent;
  if (inv.builder && (!inv.builder.path || hashArtifact(projectDir, inv.builder.path) !== inv.builder.sha256)) return notCurrent;
  const ids = [...planDecisionIds(plan, entity)], held = new Map(), recount = Object.create(null);
  for (const r of inv.records) if (r.held !== undefined) {
    if (!Array.isArray(r.held) || !r.held.length || r.held.some(reason => typeof reason !== 'string' || !ids.some(id => reason.startsWith(`${id}:`)))) return notCurrent;
    held.set(String(r.sourceId), [...r.held]);
    for (const reason of r.held) recount[reason] = (recount[reason] || 0) + 1;
  }
  if (!held.size) return inv.heldRecords ? notCurrent : { ok: true, held };
  const summary = { count: held.size, reasons: Object.fromEntries(Object.keys(recount).sort(compareKeys).map(r => [r, recount[r]])) };
  if (digest(summary) !== digest(inv.heldRecords ?? null)) return notCurrent;
  const inventorySha256 = hashArtifact(projectDir, file);
  let review = '';
  try { review = fs.readFileSync(path.join(projectDir, 'mapping/review/mapping-summary.md'), 'utf8'); } catch {}
  const start = review.indexOf(HELD_START(entity)), end = review.indexOf(HELD_END(entity));
  if (start < 0 || end < start || !review.slice(start, end).includes(`sha256 \`${inventorySha256}\``)) return notCurrent;
  let approval = null, request = null;
  try { approval = (await state.readJson(projectDir, 'orchestration/approvals.json', {})).mapping; } catch { return notCurrent; }
  if (await oneClickHeldApproval(projectDir, approval, file, inv)) return { ok: true, held, inventorySha256 };
  try { request = await require('./user-actions').readUserAction(projectDir, approval?.requestId || ''); } catch { return notCurrent; }
  if (!validApproval(approval, { [file]: inventorySha256, 'mapping/mapping-plan.json': hashArtifact(projectDir, 'mapping/mapping-plan.json') }, request)) return notCurrent;
  return { ok: true, held, inventorySha256 };
}
// Human-approved policy (2026-11-01): in a run the user chose as 1-click (automationMode one_click,
// source user), the agent-decided mapping approval counts for the held set; 1-click asks no
// mid-run questions, so it has no identified request. It binds the held set through what it pins
// instead: it must name this inventory and the plan, its census reference must be current (that
// reconciliation pins the mapping-plan digest), the inventory must pin its builder (checked above)
// and its recorded extract must be unchanged. The held summary and reviewed held block were
// checked above. Normal mode and an inferred one_click still need the identified user approval.
// Omission approvals are unaffected.
async function oneClickHeldApproval(projectDir, approval, file, inv) {
  try {
    if (approval?.status !== 'approved' || approval.decidedBy !== 'agent') return false;
    if (!require('./orchestration-decisions').isExplicitUserOneClick(await state.readJson(projectDir, 'orchestration/decisions.json', {}) || {})) return false;
    const refs = (approval.artifactRefs || []).map(r => typeof r === 'string' ? r : r?.path);
    if (!refs.includes(file) || !refs.includes('mapping/mapping-plan.json') || !inv.builder || !inv.extract?.path) return false;
    if (await require('./extract-ledger').fileDigest(projectDir, inv.extract.path) !== inv.extract.sha256) return false;
    return await require('./census-reconciliation').mappingApprovalBound(projectDir, approval);
  } catch { return false; }
}
const idsDigestOf = value => require('./mapping-field-contract').digest(value);
const idsDigest = ids => require('./mapping-field-contract').digest([...new Set(ids)].sort());
// The generated reader feeds its existing extract stream once. Values are not written to the inventory.
async function inventory(projectDir, entity, records, {
  sourceKey = r => r.id,
  buildWrite = r => r,
  rows = [],
  templates = [],
  builder = null,
  extract = null
} = {}) {
  entityKey(entity);
  assertApplicabilityShape(rows);
  const declared = applicabilityKey(rows), applicability = declared.length ? Object.create(null) : null;
  for (const r of rows) if (applicability && r.appliesWhen !== undefined) applicability[r.canonicalField] = { writePath: canonical(r.writePath), applicable: 0, notApplicable: 0, missing: 0, unexpected: 0 };
  const preparationStarted = performance.now();
  const decisionIds = planDecisionIds(await state.readJson(projectDir, 'mapping/mapping-plan.json', {}), entity);
  const sourcePaths = Object.create(null),
    writePaths = Object.create(null),
    members = new Map(),
    perRecord = [],
    heldReasons = Object.create(null),
    retired = new Map(),
    seen = new Set();
  let heldCount = 0;
  for await (const record of records) {
    const id = sourceKey(record);
    if (id === undefined || id === null || String(id) === '' || seen.has(String(id))) throw new Error('Read-back inventory requires unique stable source keys');
    seen.add(String(id));
    const body = await buildWrite(record),
      source = leafPaths(record, { templates }),
      held = builderHold(body, decisionIds);
    for (const p of source) {
      sourcePaths[p] = (sourcePaths[p] || 0) + 1;
      if (!members.has(p)) members.set(p, []);
      members.get(p).push(String(id));
    }
    // A held record stays in source coverage but is never written: it is outside output
    // applicability and the canary universe, and is listed with its reasons.
    if (held) for (const code of retiredHolds(body)) retired.set(code, [...(retired.get(code) || []), String(id)]);
    if (held) {
      heldCount += 1;
      for (const r of held) heldReasons[r] = (heldReasons[r] || 0) + 1;
      perRecord.push({ sourceId: String(id), writePaths: [], held });
      continue;
    }
    const sent = leafPaths(body);
    // A non-applicable output is outside this record's coverage unless the builder emits it
    // (then the gate refuses that canary, and validation reports it first).
    const judged = applicability ? outputApplicability(rows, record, body) : null;
    const applies = row => !judged || judged.states.find(s => s.row === row)?.applies !== false;
    const fields = [...new Set(rows.filter(row => applies(row) && (row.sourcePaths || []).some(p => valuesAt(record, p).some(populated)) || sent.has(canonical(row.writePath))).map(row => canonical(row.writePath)))].sort(compareKeys);
    if (judged) for (const s of judged.states) {
      const count = applicability[s.row.canonicalField];
      if (!count) continue;
      count[s.applies ? 'applicable' : 'notApplicable'] += 1;
      if (judged.missing.includes(s.row)) count.missing += 1;
      if (judged.unexpected.includes(s.row)) count.unexpected += 1;
    }
    for (const p of fields) writePaths[p] = (writePaths[p] || 0) + 1;
    perRecord.push({
      sourceId: String(id),
      writePaths: fields
    });
  }
  // Refused before anything is written: the inventory file and held review stay as they were.
  if (retired.size) {
    const detail = [...retired].map(([code, ids]) => `${code} (${ids.length}: ${ids.slice(0, 20).join(', ')}${ids.length > 20 ? ', ...' : ''}). ${retiredHoldCodes()[code].remediation}`).join(' ');
    throw Object.assign(new Error(`Builder holds a retired hold code for ${entity}: ${detail}`), { code: 'builder_hold_retired', retired: Object.fromEntries(retired), remediation: Object.fromEntries([...retired.keys()].map(code => [code, retiredHoldCodes()[code].remediation])) });
  }
  perRecord.sort((a, b) => compareKeys(a.sourceId, b.sourceId));
  // Per-location complete membership digest: lets omission review bind each location's
  // measured record set without writing source values or a second source scan.
  const sourceIdsDigests = Object.create(null);
  for (const p of Object.keys(sourcePaths).sort(compareKeys)) sourceIdsDigests[p] = idsDigest(members.get(p));
  const result = {
    schemaVersion: INVENTORY_SCHEMA_VERSION,
    locationEncoding: L.ENCODING,
    entity,
    selectorTemplates: templateKey(templates),
    builder,
    ...(extract ? { extract } : {}),
    sourcePaths,
    sourceIdsDigests,
    writePaths,
    records: perRecord,
    // Present only when a row declares appliesWhen, so an inventory without one is unchanged.
    ...(applicability ? { applicabilityDigest: idsDigestOf(declared), outputApplicability: applicability } : {}),
    ...(heldCount ? { heldRecords: { count: heldCount, reasons: Object.fromEntries(Object.keys(heldReasons).sort(compareKeys).map(r => [r, heldReasons[r]])) } } : {})
  };
  await state.atomic(projectDir, `execution/read-back-inventory/${entity}.json`, result);
  writeHeldReview(projectDir, entity, result.heldRecords, hashArtifact(projectDir, `execution/read-back-inventory/${entity}.json`));
  Object.defineProperty(result, 'preparationMs', {
    value: performance.now() - preparationStarted
  });
  return result;
}
// `exclude`: records that cannot be canaries now (their own parent record is final for this run,
// so the gate would only defer them); they are never selected and are dropped from `existing`.
function cover(inv, {
  ceiling = 10,
  existing = [],
  exclude = []
} = {}) {
  if (!Number.isInteger(ceiling) || ceiling < 10 || ceiling > 30) throw new Error('Canary ceiling must be a recorded user integer from 10 to 30');
  const records = [...inv.records].sort((a, b) => compareKeys(a.sourceId, b.sourceId));
  const universe = Object.keys(inv.writePaths).sort(compareKeys),
    left = new Set(universe),
    selected = [],
    coveredBy = {};
  const add = r => {
    selected.push(r.sourceId);
    for (const p of r.writePaths) if (left.delete(p)) coveredBy[p] = r.sourceId;
  };
  for (const id of existing.filter(id => !exclude.includes(id))) {
    const row = records.find(r => r.sourceId === id);
    if (!row) throw new Error('A previously written canary is missing from the inventory');
    add(row);
  }
  while (left.size) {
    const candidates = records.filter(r => !selected.includes(r.sourceId) && !exclude.includes(r.sourceId)).map(r => ({
      row: r,
      gain: r.writePaths.filter(p => left.has(p)).length
    })).sort((a, b) => b.gain - a.gain || compareKeys(a.row.sourceId, b.row.sourceId));
    if (!candidates[0]?.gain) break;
    add(candidates[0].row);
  }
  return {
    universeDigest: digest({
      universe,
      records
    }),
    ceiling,
    canarySourceIds: selected,
    coveredBy,
    requiredCount: selected.length,
    uncoveredPaths: selected.length > ceiling ? universe.filter(p => !selected.slice(0, ceiling).some(id => records.find(r => r.sourceId === id).writePaths.includes(p))) : [...left],
    status: selected.length > ceiling || left.size ? 'coverage-exceeds-ceiling' : 'covered'
  };
}
function mappingRows(plan, entity) {
  const raw = plan.entityMappings || plan.entityDecisions || plan.mappings || plan.entities || [];
  const entities = Array.isArray(raw) ? raw : Object.entries(raw).map(([key, v]) => ({
    sourceEntity: key,
    ...v
  }));
  const entry = entities.find(e => [e.entity, e.sourceEntity, e.name, e.target?.entity].includes(entity));
  const rows = entry?.fieldMappings || entry?.fields || entry?.mappingRows || [];
  if (plan.fieldContractVersion !== 1) return rows;
  // Derive the approved verification projection from canonical outputs. Never
  // trust a separately authored flat table or infer a remote read path.
  return entry ? require('./mapping-field-contract').projectEntity(entry).verificationMappings || [] : [];
}
function validApproval(approval, current, request) {
  return approval?.status === 'approved' && approval.decidedBy === 'user' && GUID.test(approval.approvedBy?.userId || '') && Boolean(approval.approvedBy?.displayName?.trim()) && approval.requestId === request?.requestId && ['pending', 'answered'].includes(request?.status) && Object.entries(current).every(([p, h]) => approval.bindingDigests?.[p] === h && request.artifactRefs?.some(r => r.path === p && String(r.sha256).replace(/^sha256:/, '') === h));
}
async function validate(projectDir, entity, {
  decision,
  plan,
  inv,
  approval,
  request
} = {}) {
  entityKey(entity);
  decision ||= await state.readJson(projectDir, `mapping/entity-decisions/${entity}.json`);
  plan ||= await state.readJson(projectDir, 'mapping/mapping-plan.json');
  inv ||= await state.readJson(projectDir, `execution/read-back-inventory/${entity}.json`);
  approval ||= (await state.readJson(projectDir, 'orchestration/approvals.json', {})).mapping;
  if (!request && approval?.requestId) {
    try {
      request = await require('./user-actions').readUserAction(projectDir, approval.requestId);
    } catch {/* invalid approval */}
  }
  const errors = [],
    contract = domainContract(entity),
    copied = decision.readBackContract;
  if (!copied || copied.domainDigest !== contract.domainDigest || digest(copied.intendedFields) !== digest(contract.intendedFields) || digest(copied.readBackProjection) !== digest(contract.readBackProjection)) errors.push('read-back domain contract stale or missing');
  if (!inv || inv.entity !== entity || !Array.isArray(inv.records)) errors.push('read-back inventory missing; check not run');
  // Fail closed on an inventory written under another location encoding or selector set,
  // or by a builder that has since changed: its paths/counts no longer mean the same thing.
  else if (inv.schemaVersion !== INVENTORY_SCHEMA_VERSION || inv.locationEncoding !== L.ENCODING) errors.push('read-back inventory stale (location encoding); rerun inventory');
  else {
    let templates = null;
    try { templates = templateKey(selectorTemplates(plan, entity)); } catch (e) { errors.push(e.message); }
    if (templates && digest(templates) !== digest(inv.selectorTemplates || [])) errors.push('read-back inventory stale (selector templates changed); rerun inventory');
    if (inv.builder && (!inv.builder.path || hashArtifact(projectDir, inv.builder.path) !== inv.builder.sha256)) errors.push('read-back inventory stale (builder changed or missing); rerun inventory');
    // The extract the inventory measured must still be the toolkit-recorded version, with no lost fields.
    // The CLI inventory (it pins a builder) always records its extract; one without is not trusted.
    if (inv.builder && !inv.extract) errors.push('read-back inventory has no recorded source extract; rerun inventory');
    // The inventory describes one extract version; a different file needs a fresh inventory, which
    // re-reads and records it (repairing a changed or stripped extract without blocking).
    if (inv.extract) {
      const current = await require('./extract-ledger').fileDigest(projectDir, inv.extract.path).catch(() => null);
      if (current !== inv.extract.sha256) errors.push(`source_extract_changed_since_inventory:${inv.extract.path}: rerun inventory`);
    }
  }
  const loc = p => {
    try { return canonical(p); } catch { errors.push(`Unparseable location: ${JSON.stringify(p)}`); return null; }
  };
  const rows = mappingRows(plan, entity).map(r => ({ ...r, sourcePaths: Array.isArray(r.sourcePaths) ? r.sourcePaths.map(loc) : r.sourcePaths })),
    dispositions = decision.fieldDispositions || [];
  // Output applicability: a closed predicate over the decision's own locations,
  // bound to the inventory it was measured under, and every record honored by the builder.
  const declaredRows = rows.filter(r => r.appliesWhen !== undefined);
  if (declaredRows.length) {
    const A = require('./output-applicability'), fields = require('./mapping-field-contract'), entry = planEntity(plan, entity);
    for (const r of declaredRows) {
      const d = plan.fieldContractVersion === 1 ? (entry?.fieldMappings || []).find(x => x.id === r.decisionRef) : null;
      let own = [];
      try { own = d ? fields.decisionLocations(d) : []; } catch { /* reported by the mapping contract */ }
      if (!d) errors.push(`Invalid output applicability ${r.canonicalField || r.id}: appliesWhen requires a fieldContractVersion 1 decision`);
      else for (const m of A.validate(r.appliesWhen, own)) errors.push(`Invalid output applicability ${r.canonicalField}: ${m}`);
    }
  }
  if (inv && Array.isArray(inv.records) && (declaredRows.length || inv.applicabilityDigest !== undefined)) {
    if (inv.applicabilityDigest !== idsDigestOf(applicabilityKey(rows))) errors.push('read-back inventory stale (output applicability changed); rerun inventory');
    else for (const [field, c] of Object.entries(inv.outputApplicability || {}).sort(([a], [b]) => compareKeys(a, b))) {
      if (c.missing) errors.push(`Builder omits an applicable output: ${field} at ${c.writePath} (${c.missing} records)`);
      if (c.unexpected) errors.push(`Builder emits a non-applicable output: ${field} at ${c.writePath} (${c.unexpected} records)`);
    }
  }
  // Held records (0213 A.2.2): each hold names a decision of this entity in the current plan, and
  // the summary matches the per-record reasons the approval binds.
  let heldRecords = null;
  if (inv && Array.isArray(inv.records)) {
    const ids = planDecisionIds(plan, entity), recount = Object.create(null), unknown = new Map();
    let count = 0;
    for (const r of inv.records) if (r.held !== undefined) {
      count += 1;
      for (const reason of Array.isArray(r.held) ? r.held : [null]) {
        recount[reason] = (recount[reason] || 0) + 1;
        if (typeof reason !== 'string' || ![...ids].some(id => reason.startsWith(`${id}:`))) {
          const id = typeof reason === 'string' ? reason.split(':')[0] : String(reason);
          unknown.set(id, (unknown.get(id) || 0) + 1);
        }
      }
    }
    for (const [id, n] of [...unknown].sort(([a], [b]) => compareKeys(a, b))) errors.push(`Builder hold names an unknown ${entity} decision: ${id} (${n} records); fix the builder, rerun inventory`);
    const summary = count ? { count, reasons: Object.fromEntries(Object.keys(recount).sort(compareKeys).map(r => [r, recount[r]])) } : undefined;
    if (digest(summary ?? null) !== digest(inv.heldRecords ?? null)) errors.push('read-back inventory held summary does not match its records; rerun inventory');
    if (count) heldRecords = { ...summary, sourceIds: `execution/read-back-inventory/${entity}.json records[].held` };
  }
  const gate = await state.load(projectDir),
    history = [...gate.observedLosses, ...Object.values(gate.runs).flatMap(run => Object.values(run.entities || {}).flatMap(e => [...(e.attemptedPaths || []), ...(e.settlements || []).filter(s => s.status !== 'passed')]))];
  const inventoried = Object.keys(inv?.sourcePaths || {});
  const requirements = [...inventoried.map(p => ({
    kind: 'source',
    path: loc(p)
  })), ...(contract.intendedFields || []).filter(f => f.sourcePaths.some(p => inventoried.some(k => L.isPrefix(p, k)))).map(f => ({
    kind: 'intended',
    path: loc(f.targetPath),
    requiredSubpaths: f.requiredSubpaths || []
  }))].filter(r => r.path !== null);
  // An intended item list ([]) is covered by rows writing the list itself or fields inside each item.
  const coversIntended = (writePath, p) => L.within(p, writePath) && (p.endsWith('[]') || L.within(writePath, p));
  const samePath = (x, p) => L.within(p, x) && L.within(x, p);
  const historyHas = (h, p) => [...(h.sourcePaths || []), h.writePath, h.intendedTargetPath].some(x => {
    try { return x !== undefined && x !== null && canonical(x) === p; } catch { return x === p; }
  });
  let current;
  try {
    current = bindings(projectDir, entity);
  } catch (e) {
    errors.push(e.message);
  }
  const omissionMode=require('./mapping-omission-approval').mode(await state.readJson(projectDir,'orchestration/decisions.json',{}));
  for (const requirement of requirements) {
    const matches = dispositions.filter(d => {
      try { return canonical(d.path || d.sourcePath || d.targetPath) === requirement.path && (!d.kind || d.kind === requirement.kind); } catch { return false; }
    });
    // No disposition for the intended item list itself: the item-field dispositions inside it
    // answer for it together, all from one write operation and at most one per item field.
    const inside = requirement.kind === 'intended' && !matches.length ? dispositions.filter(d => d.kind === 'intended' && coversIntended(d.path || d.targetPath, requirement.path)) : [];
    if (!inside.length && matches.length !== 1) {
      errors.push(`Disposition missing or duplicate: ${requirement.kind}:${requirement.path}`);
      continue;
    }
    if (inside.length) {
      if (new Set(inside.map(d => d.writeOperation)).size > 1) errors.push(`Ambiguous intended item fields: ${requirement.path} (more than one write operation)`);
      const seen = new Set();
      for (const d of inside) {
        const p = canonical(d.path || d.targetPath);
        if (seen.has(p)) errors.push(`Disposition missing or duplicate: intended:${p}`);
        seen.add(p);
      }
    }
    const covering = [];
    for (const d of inside.length ? inside : matches) {
      if (d.status === 'mapped') {
        const refs = plan.fieldContractVersion === 1 ? d.mappingRefs : [d.mappingRef];
        const mapped = Array.isArray(refs) ? refs.map(ref => ref === undefined || ref === null ? null : rows.find((r, i) =>
          (plan.fieldContractVersion === 1 ? r.canonicalField === ref : r.id !== undefined && r.id === ref) || plan.fieldContractVersion !== 1 &&
          (r.mappingRef !== undefined && r.mappingRef === ref || String(i) === String(ref)))) : [];
        if (!mapped.length || mapped.some(row => !row ||
          (requirement.kind === 'source' ? !row.sourcePaths?.includes(requirement.path) : !coversIntended(row.writePath, requirement.path) || plan.fieldContractVersion===1 && row.writeOperation!==d.writeOperation) ||
          !row.readPath && !row.unverifiableReason)) errors.push(`Mapping does not cover ${requirement.path}`);
        else covering.push(...mapped);
      } else if (d.status === 'deferred') {
        if (!require('./mapping-recovery').permitsDisposition(projectDir, plan, planEntity(plan, entity), d)) errors.push(`Invalid recoverable deferral: ${requirement.path}`);
        if (history.some(h => (!h.entity || h.entity === entity) && historyHas(h, requirement.path))) errors.push(`Attempted or observed-loss path cannot become a deferred field: ${requirement.path}`);
      } else if (d.status === 'notMigrated') {
        if (history.some(h => (!h.entity || h.entity === entity) && historyHas(h, requirement.path))) errors.push(`Attempted or observed-loss path cannot be omitted: ${requirement.path}`);
        if (!current || !validApproval(approval, current, request) || omissionMode==='one_click' || approval?.omissionApprovalContext?.automationMode!=='interactive' || request?.omissionApprovalContext?.automationMode!=='interactive') errors.push(`Identified exact-binding approval missing: ${requirement.path}`);
      } else errors.push(`Unknown disposition: ${requirement.path}`);
    }
    // The item fields the target contract requires (product images: url). Answered field by field,
    // each needs its own disposition, and must be written whenever any field of the item is.
    for (const sub of requirement.requiredSubpaths || []) {
      const need = canonical(`${requirement.path}.${sub}`);
      if (inside.length) {
        const own = inside.filter(d => samePath(d.path || d.targetPath, need));
        if (!own.length) errors.push(`Disposition missing or duplicate: intended:${need}`);
        else if (inside.some(d => d.status === 'mapped') && own[0].status !== 'mapped') errors.push(`Mapping does not cover ${need}`);
      } else if (covering.length && !covering.some(row => L.within(row.writePath, need) || L.within(need, row.writePath))) errors.push(`Mapping does not cover ${need}`);
    }
  }
  if (plan.fieldContractVersion === 1) {
    const writes = new Map();
    for (const row of rows) {
      const key = canonical(row.writePath), binding = JSON.stringify([row.targetRef,row.writeOperation,row.writeInput]);
      if (writes.has(key) && writes.get(key) !== binding) errors.push(`Ambiguous read-back operation: ${key}`);
      writes.set(key,binding);
    }
  }
  if (plan.fieldContractVersion===1 && require('./mapping-omission-approval').candidates(plan).length) {
    try {require('./mapping-omission-approval').assertAuthorized(projectDir,plan);} catch(e) {errors.push(e.message);}
  }
  const serverOwned = contract.readBackProjection?.serverOwned || [];
  for (const f of contract.intendedFields || []) if (serverOwned.some(p => (typeof p === 'string' ? p : p.path) === f.targetPath)) errors.push(`Intended field cannot be server-owned: ${f.targetPath}`);
  for (const r of rows) if (!r.sourcePaths?.length || !r.writePath || !r.readPath && !r.unverifiableReason) errors.push('Mapping row missing read-back declaration');
  return {
    valid: !errors.length,
    fieldContractVersion:plan.fieldContractVersion,
    errors,
    contract,
    rows,
    inventory: inv,
    decision,
    approval,
    bindingDigests: current,
    heldRecords
  };
}
module.exports = {
  DOMAINS,
  retiredHoldCodes,
  approvedHeldRecords,
  entityKey,
  canonical,
  digest,
  populated,
  compareKeys,
  domainPath,
  domainContract,
  bindingPaths,
  bindings,
  leafPaths,
  valuesAt,
  selectorTemplates,
  draftSelectorDescendantStubs,
  assertPureBuilder,
  templateKey,
  applicabilityKey,
  outputApplicability,
  builderHold,
  planDecisionIds,
  INVENTORY_SCHEMA_VERSION,
  inventory,
  cover,
  mappingRows,
  validApproval,
  validate
};

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