Skip to content

Commit b795083

Browse files
committed
refactor(cli): orchestrate data-liberation compare instead of Studio's own visual parity check
Removes cli/lib/visual-parity.ts (839 lines) and its SSI-oracle eval script. That code re-measured captured-vs-imported section geometry itself and fed static-site-importer's own oracle class through a bundled PHP eval script — duplicating a concern data-liberation-agent already owns end to end as `data-liberation compare <dir>` (readiness, geometry, and the comparison against the live source). Static site import now shells out to the same compare command via a new compareLiberatedCapture() client (reusing the existing data-liberation-client.ts subprocess pattern) and relays its verdict: a disagreement still fails the import, a pass still reports success. What stayed Studio's job: resolving which on-disk directory is the DLA capture for a given import source, and invoking the gate on every create — fresh or resumed. The old gate read site.url directly (only ever set in memory on a fresh create), so it silently skipped on every resume; the replacement gates on the capture directory alone, which is resolved fresh from the source path on both paths and needs no site URL at all, since DLA compares the capture to its live source rather than to this running site. Also gone: mapping captured source URLs to imported permalinks, and the layout-baseline/v1 serialization for SSI's oracle contract. Both existed only to feed the now-removed measure-the-imported-render step and have no replacement need.
1 parent fc5ca49 commit b795083

8 files changed

Lines changed: 259 additions & 1954 deletions

File tree

‎apps/cli/commands/site/create.ts‎

Lines changed: 66 additions & 161 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,13 @@ import {
7272
} from 'cli/lib/cli-config/core';
7373
import { getSiteUrl, removeSiteFromConfig } from 'cli/lib/cli-config/sites';
7474
import { connectToDaemon, disconnectFromDaemon, emitCliEvent } from 'cli/lib/daemon-client';
75-
import { liberateWebsite, type PartialCaptureReport } from 'cli/lib/data-liberation-client';
75+
import {
76+
compareLiberatedCapture,
77+
liberateWebsite,
78+
type PartialCaptureReport,
79+
} from 'cli/lib/data-liberation-client';
7680
import {
7781
getAiInstructionsPath,
78-
getBundledVisualParityEvalScriptPath,
7982
getWordPressVersionPath,
8083
} from 'cli/lib/dependency-management/paths';
8184
import { updateServerFiles } from 'cli/lib/dependency-management/setup';
@@ -98,11 +101,6 @@ import { getTracksOrigin, recordTracksEvent, TRACKS_EVENTS } from 'cli/lib/track
98101
import { StatsGroup } from 'cli/lib/types/bump-stats';
99102
import { untildify } from 'cli/lib/utils';
100103
import { ValidationError } from 'cli/lib/validation-error';
101-
import {
102-
buildVisualParityValidationArtifacts,
103-
visualParityGateFailure,
104-
type VisualParityEvaluation,
105-
} from 'cli/lib/visual-parity';
106104
import { runBlueprint, startWordPressServer } from 'cli/lib/wordpress-server-manager';
107105
import {
108106
CLI_AUTO_UPDATE_WP_VERSION,
@@ -128,9 +126,6 @@ const DEFAULT_STATIC_SITE_IMPORTER_PLUGIN_URL =
128126
const SSI_PLUGIN_SLUG = 'static-site-importer';
129127
const STATIC_SITE_IMPORT_DIR = '.studio-import';
130128
const STATIC_SITE_IMPORT_REQUEST_FILE = 'request.json';
131-
const STATIC_SITE_IMPORT_VISUAL_PARITY_SCRIPT_FILE = 'visual-parity-eval.php';
132-
const STATIC_SITE_IMPORT_VISUAL_PARITY_INPUT_FILE = 'visual-parity-input.json';
133-
const STATIC_SITE_IMPORT_VISUAL_PARITY_OUTPUT_FILE = 'visual-parity-output.json';
134129
const STATIC_SITE_IMPORT_PROGRESS_INTERVAL_MS = 30_000;
135130
const DATA_LIBERATION_CAPTURE_RECEIPT_SCHEMA = 'data-liberation/capture-receipt/v1';
136131
const PARTIAL_CAPTURE_REPORTED_ROUTES = 10;
@@ -163,7 +158,7 @@ type StaticSiteImporterSource = {
163158
payload: Record< string, unknown >;
164159
stagedSourcePath?: string;
165160
stagedReportFiles?: Array< { name: string; from: string } >;
166-
sectionsPath?: string;
161+
captureDirectory?: string;
167162
};
168163

169164
type StaticSiteImporterPlugin = string | { path: string };
@@ -185,7 +180,7 @@ export type CreateCommandOptions = {
185180
bundlePath?: string;
186181
sourcePath?: string;
187182
reportFiles?: Array< { name: string; from: string } >;
188-
sectionsPath?: string;
183+
captureDirectory?: string;
189184
};
190185
};
191186
adminUsername?: string;
@@ -272,26 +267,20 @@ function resolveDataLiberationWebsiteRoot( sourceDir: string ): string {
272267
return websiteRoot;
273268
}
274269

275-
// Data Liberation writes a `sections/*.json` per-page geometry record alongside `website/`
276-
// (sibling to `capture-receipt.json`). `sourcePath` may already be that capture root, or (in
277-
// the real `--from <url>` flow) the `website/` directory `liberateWebsite()` returns directly
278-
// — so this checks both the given directory and its parent for the receipt, matching however
279-
// `resolveDataLiberationWebsiteRoot` ends up locating it. Returns `undefined` (rather than
280-
// throwing) when no DLA capture is present, since this data is optional: visual parity simply
281-
// stays unmeasured, exactly like today, for any non-DLA import source.
282-
function resolveDataLiberationSectionsDir( sourcePath: string ): string | undefined {
283-
for ( const candidateRoot of [ sourcePath, path.dirname( sourcePath ) ] ) {
284-
const receiptPath = path.join( candidateRoot, 'capture-receipt.json' );
285-
const sectionsDir = path.join( candidateRoot, 'sections' );
286-
if (
287-
fs.existsSync( receiptPath ) &&
288-
fs.existsSync( sectionsDir ) &&
289-
fs.statSync( sectionsDir ).isDirectory()
290-
) {
291-
return sectionsDir;
292-
}
293-
}
294-
return undefined;
270+
// `sourcePath` may already be the DLA capture root (containing `capture-receipt.json` and a
271+
// nested `website/`), or — in the real `--from <url>` flow — the `website/` directory
272+
// `liberateWebsite()` returns directly, one level below the receipt. `data-liberation
273+
// compare` resolves both shapes itself (`resolveCheckDirectory` in DLA's
274+
// `lib/fidelity/check.ts` checks the given directory, then its parent), so this only has to
275+
// confirm a receipt is reachable from one of the two and hand back whichever directory the
276+
// caller already has — not resolve it to a single canonical shape. Returns `undefined`
277+
// (rather than throwing) when no DLA capture is present: `data-liberation compare` simply
278+
// does not run for any other import source, exactly like today.
279+
function resolveDataLiberationCaptureDirectory( sourcePath: string ): string | undefined {
280+
const hasCapture = [ sourcePath, path.dirname( sourcePath ) ].some( ( candidateRoot ) =>
281+
isDataLiberationCaptureRoot( candidateRoot )
282+
);
283+
return hasCapture ? sourcePath : undefined;
295284
}
296285

297286
function isDataLiberationCaptureRoot( directory: string ): boolean {
@@ -348,7 +337,7 @@ function collectArtifactRootReports(
348337
): Array< { name: string; from: string } > {
349338
// `--from <url>` stages `website/` (liberateWebsite's return value). Sidecars
350339
// live on the capture root, one directory up — the same parent lookup
351-
// `resolveDataLiberationSectionsDir` already performs.
340+
// `resolveDataLiberationCaptureDirectory` already performs.
352341
for ( const candidateRoot of [ sourcePath, path.dirname( sourcePath ) ] ) {
353342
if (
354343
path.resolve( candidateRoot ) === path.resolve( websiteRoot ) ||
@@ -403,7 +392,7 @@ function resolveStaticSiteImporterSource( sourcePath: string ): StaticSiteImport
403392
payload: {},
404393
stagedSourcePath,
405394
stagedReportFiles: collectArtifactRootReports( sourcePath, stagedSourcePath ),
406-
sectionsPath: resolveDataLiberationSectionsDir( sourcePath ),
395+
captureDirectory: resolveDataLiberationCaptureDirectory( sourcePath ),
407396
};
408397
}
409398

@@ -549,7 +538,7 @@ export function buildCreateFromSourceBlueprint(
549538
bundlePath?: string;
550539
sourcePath?: string;
551540
reportFiles?: Array< { name: string; from: string } >;
552-
sectionsPath?: string;
541+
captureDirectory?: string;
553542
};
554543
} {
555544
const source = resolveStaticSiteImporterSource( sourcePath );
@@ -597,7 +586,7 @@ export function buildCreateFromSourceBlueprint(
597586
bundlePath: tempDir,
598587
sourcePath: source.stagedSourcePath,
599588
reportFiles: source.stagedReportFiles,
600-
sectionsPath: source.sectionsPath,
589+
captureDirectory: source.captureDirectory,
601590
},
602591
};
603592
}
@@ -700,23 +689,6 @@ function staticSiteImportResult(
700689
return response as Record< string, unknown >;
701690
}
702691

703-
function themeImportReportPath(
704-
site: SiteData,
705-
receipt: Record< string, unknown > | undefined
706-
): string | undefined {
707-
const result = staticSiteImportResult( receipt );
708-
const themeDir = result?.theme_dir;
709-
if ( typeof themeDir !== 'string' || ! themeDir.trim() ) {
710-
return undefined;
711-
}
712-
const reportPath = path.join( themeDir, 'import-report.json' );
713-
const relative = path.relative( site.path, reportPath );
714-
if ( relative && ! relative.startsWith( '..' ) && ! path.isAbsolute( relative ) ) {
715-
return relative.split( path.sep ).join( '/' );
716-
}
717-
return reportPath.split( path.sep ).join( '/' );
718-
}
719-
720692
function staticSiteImportQualityFailure(
721693
receipt: Record< string, unknown > | undefined
722694
): string | undefined {
@@ -787,105 +759,50 @@ function staticSiteImportQualityFailure(
787759
);
788760
}
789761

790-
// Measures captured-vs-imported section geometry and evaluates it through SSI's own oracle
791-
// class (`Static_Site_Importer_Visual_Parity_Oracle`). Returns a human-readable failure
792-
// detail when the oracle reports a disagreement, or when Studio sent a real payload and the
793-
// oracle answers `not_verified` (a contract bug). `undefined` when the check passed or itself
794-
// could not run — a failure to *measure* never surfaces as an import failure.
795-
async function runVisualParityCheck(
796-
site: SiteData,
797-
siteUrl: string,
798-
sectionsPath: string,
799-
logger: Logger< LoggerAction >,
800-
reportPath?: string
762+
// Runs DLA's own fidelity gate — `data-liberation compare`, the same command
763+
// `packages/data-liberation-agent`'s own docs point at for verifying a capture — against the
764+
// capture directory Studio imported from, and relays its verdict. Studio does not re-measure
765+
// fidelity itself; DLA already owns readiness, geometry, and the comparison (see
766+
// `compareLiberatedCapture` in `cli/lib/data-liberation-client.ts`). Returns the verdict text
767+
// as a failure detail when the gate disagrees; `undefined` when it passed or could not run at
768+
// all — a failure to *run* the check never surfaces as an import failure.
769+
async function runDataLiberationCompareCheck(
770+
captureDirectory: string,
771+
logger: Logger< LoggerAction >
801772
): Promise< string | undefined > {
802773
logger.reportStart(
803774
LoggerAction.IMPORT_SITE,
804-
__( 'Measuring imported pages for visual parity…' )
775+
__( 'Comparing the capture against its source with data-liberation compare…' )
805776
);
806-
let artifacts;
777+
let result;
807778
try {
808-
artifacts = await buildVisualParityValidationArtifacts( {
809-
sectionsDir: sectionsPath,
810-
importedOrigin: siteUrl,
811-
logger: { warn: ( message ) => logger.reportWarning( message ) },
779+
result = await compareLiberatedCapture( captureDirectory, {
780+
onProgress: ( message ) => logger.reportProgress( message ),
812781
} );
813782
} catch ( error ) {
814783
logger.reportError(
815784
new LoggerError(
816-
__( 'Visual parity check could not run. Import quality was not affected.' ),
785+
__( 'data-liberation compare could not run. Import quality was not affected.' ),
817786
error
818787
),
819788
false
820789
);
821790
return undefined;
822791
}
823792

824-
const importDir = path.join( site.path, STATIC_SITE_IMPORT_DIR );
825-
const inputPath = path.join( importDir, STATIC_SITE_IMPORT_VISUAL_PARITY_INPUT_FILE );
826-
const outputPath = path.join( importDir, STATIC_SITE_IMPORT_VISUAL_PARITY_OUTPUT_FILE );
827-
const scriptPath = path.join( importDir, STATIC_SITE_IMPORT_VISUAL_PARITY_SCRIPT_FILE );
828-
fs.mkdirSync( importDir, { recursive: true } );
829-
fs.writeFileSync( inputPath, JSON.stringify( { visual_parity: artifacts }, null, 2 ) );
830-
fs.copyFileSync( getBundledVisualParityEvalScriptPath(), scriptPath );
831-
832-
const evalArgs = [
833-
'eval-file',
834-
path.posix.join( STATIC_SITE_IMPORT_DIR, STATIC_SITE_IMPORT_VISUAL_PARITY_SCRIPT_FILE ),
835-
path.posix.join( STATIC_SITE_IMPORT_DIR, STATIC_SITE_IMPORT_VISUAL_PARITY_INPUT_FILE ),
836-
path.posix.join( STATIC_SITE_IMPORT_DIR, STATIC_SITE_IMPORT_VISUAL_PARITY_OUTPUT_FILE ),
837-
];
838-
if ( reportPath ) {
839-
evalArgs.push( reportPath );
840-
}
841-
const result = await runWpCli( site, evalArgs );
842-
if ( result.exitCode !== 0 || ! fs.existsSync( outputPath ) ) {
843-
logger.reportError(
844-
new LoggerError(
845-
__( 'Visual parity check could not run. Import quality was not affected.' ),
846-
new Error( wpCliFailureDetail( result ) )
847-
),
848-
false
849-
);
850-
return undefined;
793+
if ( result.report ) {
794+
console.log( result.report );
851795
}
852796

853-
let evaluation: VisualParityEvaluation;
854-
try {
855-
evaluation = JSON.parse( fs.readFileSync( outputPath, 'utf-8' ) );
856-
} catch ( error ) {
857-
logger.reportError(
858-
new LoggerError(
859-
__( 'Visual parity check produced an unreadable result. Import quality was not affected.' ),
860-
error
861-
),
862-
false
863-
);
797+
if ( result.pass ) {
798+
logger.reportSuccess( __( 'data-liberation compare passed' ) );
864799
return undefined;
865800
}
866-
867-
if ( evaluation.visual_parity_artifacts ) {
868-
console.log(
869-
JSON.stringify( { visual_parity_artifacts: evaluation.visual_parity_artifacts }, null, 2 )
870-
);
871-
}
872-
873-
const failure = visualParityGateFailure( artifacts, evaluation );
874-
if ( failure ) {
875-
logger.reportError(
876-
new LoggerError( __( 'Visual parity disagreed with the layout baseline.' ) ),
877-
false
878-
);
879-
return failure;
880-
}
881-
if ( artifacts.status === 'ready' && evaluation.status === 'passed' ) {
882-
logger.reportSuccess( __( 'Visual parity matched the layout baseline' ) );
883-
} else {
884-
logger.reportWarning(
885-
evaluation.reason || __( 'Visual parity could not be verified against the layout baseline.' )
886-
);
887-
}
888-
return undefined;
801+
logger.reportError(
802+
new LoggerError( __( 'data-liberation compare found fidelity issues.' ) ),
803+
false
804+
);
805+
return result.report || __( 'data-liberation compare failed.' );
889806
}
890807

891808
async function runStaticSiteImport(
@@ -895,7 +812,7 @@ async function runStaticSiteImport(
895812
resume = false,
896813
logger: Logger< LoggerAction > = defaultLogger,
897814
reportFiles: Array< { name: string; from: string } > = [],
898-
sectionsPath?: string
815+
captureDirectory?: string
899816
): Promise< boolean > {
900817
const requestPath = staticSiteImportRequestPath( site.path );
901818
if ( resume ) {
@@ -961,32 +878,20 @@ async function runStaticSiteImport(
961878
}
962879
const qualityFailure = staticSiteImportQualityFailure( receipt );
963880

964-
// Section geometry can only be measured once the imported content is actually live on
965-
// this running site, so it happens here — after materialization, before the plugin (and
966-
// its visual-parity oracle class) is removed — rather than as part of the request above.
967-
// See `cli/lib/visual-parity.ts` for how Studio builds `source_reports.layout_baseline`
968-
// and `imported_render`. Run even when other quality gates already failed: parity
969-
// evidence is most valuable on a broken import.
970-
//
971-
// Use `getSiteUrl()`, not the raw `site.url` field: `url` is only ever populated in
972-
// memory on a fresh `create` run (set on `siteDetails` right before this function is
973-
// called) and is never written back to the persisted CLI config. Reading `site.url`
974-
// directly made this check silently `undefined`, and therefore skipped, on every
975-
// resumed import — the CLI's own documented recovery path after a failure — so a
976-
// resume could accept content that would have failed this same gate on a fresh run.
977-
const siteUrl = getSiteUrl( site );
978-
if ( sectionsPath && siteUrl ) {
979-
const parityFailure = await runVisualParityCheck(
980-
site,
981-
siteUrl,
982-
sectionsPath,
983-
logger,
984-
themeImportReportPath( site, receipt )
985-
);
986-
if ( parityFailure ) {
881+
// `captureDirectory` is resolved from the source path afresh on every `create` run —
882+
// fresh or resumed — inside `resolveStaticSiteImporterSource`, not read back from
883+
// persisted site state the way an earlier version of this gate read `site.url` (only
884+
// ever set in memory on a fresh run, so the gate silently skipped on every resumed
885+
// import; see 4ecbcc965 / aff1e33fa). Gating on it alone keeps that same guarantee — a
886+
// resumed import still gets a verdict — without needing the site's own URL at all: DLA
887+
// compares the capture to its live source, not to this running site. Run even when other
888+
// quality gates already failed: a compare verdict is most valuable on a broken import.
889+
if ( captureDirectory ) {
890+
const compareFailure = await runDataLiberationCompareCheck( captureDirectory, logger );
891+
if ( compareFailure ) {
987892
throw new LoggerError(
988-
__( 'Static site import failed visual parity validation' ),
989-
new Error( parityFailure )
893+
__( 'Static site import failed data-liberation compare' ),
894+
new Error( compareFailure )
990895
);
991896
}
992897
}
@@ -1176,7 +1081,7 @@ export async function runCommand(
11761081
true,
11771082
logger,
11781083
staticSiteImport.reportFiles,
1179-
staticSiteImport.sectionsPath
1084+
staticSiteImport.captureDirectory
11801085
);
11811086
importOutcome = cleanupSucceeded ? 'succeeded' : 'attempted';
11821087
} catch ( error ) {
@@ -1378,7 +1283,7 @@ export async function runCommand(
13781283
false,
13791284
logger,
13801285
staticSiteImport.reportFiles,
1381-
staticSiteImport.sectionsPath
1286+
staticSiteImport.captureDirectory
13821287
);
13831288
importOutcome = cleanupSucceeded ? 'succeeded' : 'attempted';
13841289
}
@@ -1434,7 +1339,7 @@ export async function runCommand(
14341339
false,
14351340
logger,
14361341
staticSiteImport.reportFiles,
1437-
staticSiteImport.sectionsPath
1342+
staticSiteImport.captureDirectory
14381343
);
14391344
importOutcome = cleanupSucceeded ? 'succeeded' : 'attempted';
14401345
}

0 commit comments

Comments
 (0)