[object Object]

← back to Dw Contact Us Pages

harden-variants: append per-variant preimage ledger BEFORE the first write; run-summary row with failures; rollback ignores summary rows

942c7385dd05f3f3b7a04c567a9cbcacdb5e32dd · 2026-09-19 10:21:18 -0700 · Steve Abrams

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0163KBzeE1R39RSbxNmAjbki

Files touched

Diff

commit 942c7385dd05f3f3b7a04c567a9cbcacdb5e32dd
Author: Steve Abrams <steve@designerwallcoverings.com>
Date:   Sat Sep 19 10:21:18 2026 -0700

    harden-variants: append per-variant preimage ledger BEFORE the first write; run-summary row with failures; rollback ignores summary rows
    
    Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
    Claude-Session: https://claude.ai/code/session_0163KBzeE1R39RSbxNmAjbki
---
 scripts/harden-variants.mjs | 25 +++++++++++++++++--------
 1 file changed, 17 insertions(+), 8 deletions(-)

diff --git a/scripts/harden-variants.mjs b/scripts/harden-variants.mjs
index f2456be..7b6a45c 100644
--- a/scripts/harden-variants.mjs
+++ b/scripts/harden-variants.mjs
@@ -38,7 +38,7 @@ function qty(level, name) {
 // ---------------------------------------------------------------- build plan
 let plan;
 if (a.rollback) {
-  const rows = readJsonl(LEDGER).filter((r) => r.applied);
+  const rows = readJsonl(LEDGER).filter((r) => r.applied && r.variantId);
   const latest = new Map();
   for (const r of rows) if (!latest.has(r.variantId)) latest.set(r.variantId, r); // FIRST write = true preimage
   plan = [...latest.values()].map((r) => ({
@@ -94,6 +94,20 @@ for (const x of plan.slice(0, 3)) {
 if (!a.apply) { console.log('\nDRY-RUN: nothing was written.'); process.exit(0); }
 
 // ---------------------------------------------------------------- apply
+// PREIMAGE FIRST (TK-11925 review fix): every variant's before-state is appended to the
+// ledger BEFORE the first Shopify write, so a crash / kill mid-run still leaves a complete
+// undo record. Restoring a preimage for a variant that was never changed is a no-op.
+if (!a.rollback) {
+  const runId = new Date().toISOString();
+  for (const x of plan) {
+    appendJsonl(LEDGER, {
+      ts: runId, runId, productId: x.productId, handle: x.handle, variantId: x.variantId,
+      sku: x.sku, title: x.title, inventoryItemId: x.inventoryItemId, before: x.before,
+      after: { inventoryPolicy: 'DENY', tracked: true, levels: x.targetLevels }, applied: true, phase: 'preimage',
+    });
+  }
+  console.log(`  preimage: ${plan.length} rows appended to ${LEDGER} BEFORE any write`);
+}
 let ok = 0, fail = 0;
 const failed = [];
 function note(msg) { fail++; failed.push(msg); console.error('  FAIL ' + msg); }
@@ -141,13 +155,8 @@ for (const step of steps) {
 }
 
 if (!a.rollback) {
-  for (const x of plan) {
-    appendJsonl(LEDGER, {
-      ts: new Date().toISOString(), productId: x.productId, handle: x.handle, variantId: x.variantId,
-      sku: x.sku, title: x.title, inventoryItemId: x.inventoryItemId, before: x.before,
-      after: { inventoryPolicy: 'DENY', tracked: true, levels: x.targetLevels }, applied: true,
-    });
-  }
+  // run summary (not a preimage row; --rollback ignores rows without a variantId)
+  appendJsonl(LEDGER, { ts: new Date().toISOString(), phase: 'summary', ok, fail, failed: failed.slice(0, 200), applied: false });
 }
 
 console.log(`\ndone: ${ok} successful ops, ${fail} failures`);

← ef0936e TK-11925: read-only live preimage of the 785 target products  ·  back to Dw Contact Us Pages  ·  Review fixes (Kimi + contrarian): JSON-LD Offer carve-out in 2b12c68 →