Skip to main content

supercov_engine/
javascript_run.rs

1//! Rust-owned JavaScript execution for the public Supercov engine.
2
3use std::{
4    collections::BTreeMap,
5    ffi::OsString,
6    path::{Path, PathBuf},
7    time::{Instant, SystemTime, UNIX_EPOCH},
8};
9
10use serde::{Deserialize, Serialize};
11use supercov_contracts::{
12    AttributionPrecision, ExecutionModel, FrontendAttribution, FrontendLimitation,
13    FrontendLimitationScope, FrontendRunDeclaration, FrontendRunnerDeclaration,
14    LANGUAGE_FRONTEND_PROTOCOL_VERSION, StructuralSource,
15};
16
17use crate::{
18    coverage_report::{PersistedCoverageModel, RawTestResult, javascript_coverage_model},
19    evidence_archive::{
20        EvidenceArchiveEntry, EvidenceArchiveSource, collect_sources, write_archive,
21    },
22    integrity::{FrontendIntegrityInputs, create_run_integrity},
23    javascript_frontend::{
24        javascript_frontend_reuse_paths, load_cached_javascript_frontend,
25        prepare_javascript_frontend, read_javascript_frontend_cache,
26    },
27    lifecycle::{
28        ProjectLock, RunState, RunStateStatus, finalize_published_run, interrupt_run_state,
29        publish_run, recover_abandoned_runs, remove_stored_tree_deferred, update_run_state,
30        write_run_state,
31    },
32    orchestration::{
33        ExecutionPhase, ExecutionPlan, OrchestrationError, PhaseKind, execute_plan_with_supervisor,
34    },
35    process_supervision::{
36        CommandSpec, ForwardedSignal, ProcessSupervisor, SupervisionOptions, positive_milliseconds,
37    },
38    project_discovery::discover_coverage_project,
39    run_store::{RawEvidenceMetadata, RunIntegrity, RunMetadata, RunTimings},
40    workspace::{
41        cached_workspace_path, prepare_cached_workspace, prune_cached_workspace_sources,
42        sync_command_outputs, workspace_output_baseline,
43    },
44};
45
46#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
47#[serde(rename_all = "camelCase", deny_unknown_fields)]
48pub struct DirectJavascriptRunRequest {
49    pub root: PathBuf,
50    pub command: Vec<String>,
51    #[serde(default, skip_serializing_if = "Option::is_none")]
52    pub run_id: Option<String>,
53    #[serde(default, skip_serializing_if = "Option::is_none")]
54    pub started_at: Option<String>,
55    #[serde(skip)]
56    pub watchdog_program: Option<PathBuf>,
57}
58
59#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
60#[serde(rename_all = "camelCase", deny_unknown_fields)]
61pub struct DirectJavascriptRunResult {
62    pub run_id: String,
63    pub run_directory: PathBuf,
64    pub workspace: PathBuf,
65    pub exit_code: i32,
66    pub assertion_calls: usize,
67    pub recovered_runs: Vec<String>,
68    pub metadata: RunMetadata,
69}
70
71#[derive(Debug)]
72pub enum DirectJavascriptRunError {
73    Interrupted {
74        signal: ForwardedSignal,
75        exit_code: i32,
76        timings: RunTimings,
77        total_ms: f64,
78    },
79    Failed(String),
80}
81
82impl std::fmt::Display for DirectJavascriptRunError {
83    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
84        match self {
85            Self::Interrupted { signal, .. } => {
86                write!(formatter, "interrupted by {}", signal_name(*signal))
87            }
88            Self::Failed(message) => formatter.write_str(message),
89        }
90    }
91}
92
93impl std::error::Error for DirectJavascriptRunError {}
94
95impl From<String> for DirectJavascriptRunError {
96    fn from(value: String) -> Self {
97        Self::Failed(value)
98    }
99}
100
101fn signal_name(signal: ForwardedSignal) -> &'static str {
102    match signal {
103        ForwardedSignal::Sighup => "SIGHUP",
104        ForwardedSignal::Sigint => "SIGINT",
105        ForwardedSignal::Sigterm => "SIGTERM",
106    }
107}
108
109fn javascript_runner_declaration(
110    runner: String,
111    results: &[&RawTestResult],
112) -> FrontendRunnerDeclaration {
113    let has_observations = |raw: &&RawTestResult| {
114        !raw.phases.is_empty()
115            || !raw.server.is_empty()
116            || raw.runtime.iter().chain(&raw.browser).any(|snapshot| {
117                !snapshot.decisions.is_empty()
118                    || !snapshot.hits.is_empty()
119                    || !snapshot.events.is_empty()
120            })
121    };
122    let exact_test = results.iter().all(|raw| {
123        raw.test_id
124            .as_deref()
125            .is_some_and(|value| !value.is_empty())
126            && raw
127                .scope
128                .as_ref()
129                .is_none_or(|scope| raw.test_id.as_deref() == Some(scope.test_id.as_str()))
130    });
131    let exact_worker = results.iter().all(|raw| {
132        !has_observations(raw)
133            || raw
134                .scope
135                .as_ref()
136                .is_some_and(|scope| !scope.worker_id.is_empty())
137    });
138    let exact_retry = results.iter().all(|raw| {
139        raw.retry.is_some()
140            && raw
141                .scope
142                .as_ref()
143                .is_none_or(|scope| raw.retry == Some(scope.retry))
144    });
145    let contextual = !results.is_empty() && exact_test && exact_worker && exact_retry;
146    let precision = |exact| {
147        if exact {
148            AttributionPrecision::Exact
149        } else {
150            AttributionPrecision::Unavailable
151        }
152    };
153    let attribution = FrontendAttribution {
154        run: AttributionPrecision::Exact,
155        worker: precision(contextual),
156        test: precision(contextual),
157        retry: precision(contextual),
158        phase: precision(contextual),
159        action: precision(contextual),
160        assertion: precision(contextual),
161    };
162    let mut limitations = Vec::new();
163    if !contextual {
164        for (suffix, scope) in [
165            ("worker", FrontendLimitationScope::Worker),
166            ("test", FrontendLimitationScope::Test),
167            ("retry", FrontendLimitationScope::Retry),
168            ("phase", FrontendLimitationScope::Phase),
169            ("action", FrontendLimitationScope::Action),
170            ("assertion", FrontendLimitationScope::Assertion),
171        ] {
172            limitations.push(FrontendLimitation {
173                id: format!("{runner}-no-{suffix}").replace(':', "-"),
174                scopes: vec![scope],
175                reason: format!(
176                    "Runner {runner} did not expose exact {suffix} identity for every result"
177                ),
178            });
179        }
180    }
181    FrontendRunnerDeclaration {
182        runner,
183        execution_model: if contextual {
184            ExecutionModel::ParallelContextPropagated
185        } else {
186            ExecutionModel::ParallelUnattributed
187        },
188        attribution,
189        limitations,
190    }
191}
192
193fn javascript_archive_entries(
194    mut entries: Vec<EvidenceArchiveEntry>,
195    manifest: &crate::javascript_frontend::JavascriptManifest,
196    run_id: &str,
197    exit_code: i32,
198) -> Result<Vec<EvidenceArchiveEntry>, String> {
199    let mut results = Vec::new();
200    for entry in &entries {
201        if entry.path == "mcdc.json" || entry.path.ends_with("/mcdc.json") {
202            results.push(
203                serde_json::from_slice::<RawTestResult>(&entry.contents)
204                    .map_err(|error| format!("invalid {}: {error}", entry.path))?,
205            );
206        } else if entry.path == "mcdc.jsonl" || entry.path.ends_with(".mcdc.jsonl") {
207            let contents = entry
208                .contents
209                .strip_suffix(b"\n")
210                .ok_or_else(|| format!("{} does not end with a newline", entry.path))?;
211            for (index, line) in contents.split(|byte| *byte == b'\n').enumerate() {
212                if line.is_empty() {
213                    return Err(format!(
214                        "{} contains a blank record at line {}",
215                        entry.path,
216                        index + 1
217                    ));
218                }
219                results.push(
220                    serde_json::from_slice::<RawTestResult>(line).map_err(|error| {
221                        format!(
222                            "invalid {} record at line {}: {error}",
223                            entry.path,
224                            index + 1
225                        )
226                    })?,
227                );
228            }
229        }
230    }
231    if results.is_empty() {
232        let status = match exit_code {
233            0 => "passed",
234            supercov_contracts::COMMAND_TIMEOUT_EXIT_CODE => "timedOut",
235            _ => "failed",
236        };
237        let command_result = RawTestResult {
238            test_id: Some(format!("command:{run_id}")),
239            scope: None,
240            test: "Test command".into(),
241            test_file: None,
242            title: Some("Test command".into()),
243            retry: Some(0),
244            status: Some(status.into()),
245            expected_status: None,
246            flaky: false,
247            provenance: crate::coverage_report::TestProvenance {
248                runner: "command".into(),
249                kind: "setup".into(),
250                project: None,
251                source: "engine".into(),
252            },
253            role: "setup".into(),
254            attribution: crate::coverage_report::ATTRIBUTION_EXACT.into(),
255            phases: vec![],
256            runtime: vec![],
257            browser: vec![],
258            server: vec![],
259        };
260        entries.push(EvidenceArchiveEntry {
261            path: "results/command/mcdc.json".into(),
262            contents: serde_json::to_vec(&command_result).map_err(|error| error.to_string())?,
263        });
264        results.push(command_result);
265    }
266    let mut by_runner = BTreeMap::<String, Vec<&RawTestResult>>::new();
267    for result in &results {
268        by_runner
269            .entry(result.provenance.runner.clone())
270            .or_default()
271            .push(result);
272    }
273    if entries
274        .iter()
275        .any(|entry| entry.path.starts_with("server/background/") && entry.path.ends_with(".jsonl"))
276    {
277        by_runner.entry("background".into()).or_default();
278    }
279    let declaration = FrontendRunDeclaration {
280        protocol_version: LANGUAGE_FRONTEND_PROTOCOL_VERSION,
281        frontend_id: "javascript".into(),
282        frontend_version: "javascript-owned-v1".into(),
283        language: "javascript".into(),
284        structural_source: StructuralSource::OwnedProbes,
285        runners: by_runner
286            .into_iter()
287            .map(|(runner, results)| javascript_runner_declaration(runner, &results))
288            .collect(),
289        structural_limitations: manifest
290            .limitations
291            .iter()
292            .map(|limitation| limitation.id.clone())
293            .collect(),
294    };
295    entries.push(EvidenceArchiveEntry {
296        path: "coverage-model.json".into(),
297        contents: serde_json::to_vec(
298            &PersistedCoverageModel::from_declaration(&javascript_coverage_model())
299                .expect("JavaScript coverage model is contract-valid"),
300        )
301        .map_err(|error| error.to_string())?,
302    });
303    entries.push(EvidenceArchiveEntry {
304        path: "frontend.json".into(),
305        contents: serde_json::to_vec(&declaration).map_err(|error| error.to_string())?,
306    });
307    Ok(entries)
308}
309
310struct RunCleanup {
311    root: PathBuf,
312    run_id: String,
313    started_at: String,
314    lock: ProjectLock,
315    workspace: Option<PathBuf>,
316    state_written: bool,
317    terminal_recorded: bool,
318    /// Leave the instrumented copy in place: its build failed, and what the
319    /// build read is the only evidence of why.
320    keep_workspace: bool,
321}
322
323impl RunCleanup {
324    fn lock(&self) -> &ProjectLock {
325        &self.lock
326    }
327
328    fn set_workspace(&mut self, workspace: PathBuf) {
329        self.workspace = Some(workspace);
330    }
331
332    fn mark_state_written(&mut self) {
333        self.state_written = true;
334    }
335
336    fn mark_terminal(&mut self) {
337        self.terminal_recorded = true;
338    }
339}
340
341impl Drop for RunCleanup {
342    fn drop(&mut self) {
343        if self.state_written && !self.terminal_recorded {
344            let _ = update_run_state(
345                &self.root,
346                &self.run_id,
347                RunStateStatus::Failed,
348                &self.started_at,
349                Some("Rust run exited before reaching a terminal lifecycle state".into()),
350            );
351        }
352        if let Some(workspace) = &self.workspace {
353            let _ = remove_stored_tree_deferred(
354                &self.root,
355                &workspace.join(".supercov/evidence").join(&self.run_id),
356            );
357            let _ = remove_stored_tree_deferred(
358                &self.root,
359                &workspace
360                    .join(".supercov/server-evidence")
361                    .join(&self.run_id),
362            );
363            let keep_workspace = self.keep_workspace
364                || std::env::var("SUPERCOV_KEEP_WORKSPACE").is_ok_and(|value| !value.is_empty());
365            if !keep_workspace {
366                let _ = prune_cached_workspace_sources(&self.root, &self.lock);
367            }
368        }
369        let _ = self.lock.release();
370    }
371}
372
373fn now_nonce() -> u128 {
374    SystemTime::now()
375        .duration_since(UNIX_EPOCH)
376        .unwrap_or_default()
377        .as_millis()
378}
379
380fn elapsed_ms(started: Instant) -> f64 {
381    started.elapsed().as_secs_f64() * 1000.0
382}
383
384fn rounded_millisecond(value: f64) -> f64 {
385    (value * 10.0).round() / 10.0
386}
387
388fn supervision_options() -> Result<SupervisionOptions, String> {
389    let defaults = SupervisionOptions::default();
390    Ok(SupervisionOptions {
391        diagnostic_interval: positive_milliseconds(
392            std::env::var("SUPERCOV_DIAGNOSTIC_INTERVAL_MS")
393                .ok()
394                .as_deref(),
395            "SUPERCOV_DIAGNOSTIC_INTERVAL_MS",
396        )
397        .map_err(|error| error.to_string())?
398        .unwrap_or(defaults.diagnostic_interval),
399        timeout: positive_milliseconds(
400            std::env::var("SUPERCOV_COMMAND_TIMEOUT_MS").ok().as_deref(),
401            "SUPERCOV_COMMAND_TIMEOUT_MS",
402        )
403        .map_err(|error| error.to_string())?,
404        termination_grace: defaults.termination_grace,
405    })
406}
407
408fn remove_derived_pnpm_config(environment: &mut BTreeMap<OsString, OsString>) {
409    // `npx supercov` is itself launched by npm, which exports project `.npmrc`
410    // keys as `npm_config_*`. pnpm-only keys then make every nested npm process
411    // print a second set of "Unknown env config" warnings. They have no npm
412    // semantics (npm 11 reports them as unknown), so do not leak those derived
413    // environment aliases into the user's already-configured test command.
414    const PNPM_ONLY_NPM_CONFIG: &[&str] = &[
415        "npm_config_auto_install_peers",
416        "npm_config_enable_pre_post_scripts",
417        "npm_config_shamefully_hoist",
418    ];
419    environment.retain(|key, _| {
420        let key = key.to_string_lossy().to_ascii_lowercase();
421        !PNPM_ONLY_NPM_CONFIG.contains(&key.as_str())
422    });
423}
424
425fn environment_with(values: BTreeMap<String, String>) -> Vec<(OsString, OsString)> {
426    let mut environment = std::env::vars_os().collect::<BTreeMap<_, _>>();
427    remove_derived_pnpm_config(&mut environment);
428    for (key, value) in values {
429        environment.retain(|existing, _| !existing.to_string_lossy().eq_ignore_ascii_case(&key));
430        environment.insert(key.into(), value.into());
431    }
432    environment.into_iter().collect()
433}
434
435fn node_options(preload: &Path) -> String {
436    // The register import must be ABSOLUTE: node resolves a relative
437    // `--import` against each child process's OWN working directory, and
438    // monorepo runners spawn tasks inside package directories. turbo running
439    // `packages/react#build` resolved `.supercov/register.mjs` against
440    // `packages/react/` and aborted every task with ERR_MODULE_NOT_FOUND.
441    let preload = std::path::absolute(preload).unwrap_or_else(|_| preload.to_path_buf());
442    // And it must be a URL, not a path: Node reads a bare `C:\...` as a URL
443    // with scheme `c:` and refuses it, so on Windows the first Node child died
444    // at startup and the suite reported only that its exit code was 1. The
445    // JavaScript runtime already passes this import as a file URL; so does
446    // this.
447    [
448        std::env::var("NODE_OPTIONS").ok(),
449        Some("--enable-source-maps".into()),
450        Some(format!("--import={}", crate::workspace::file_url(&preload))),
451    ]
452    .into_iter()
453    .flatten()
454    .filter(|value| !value.is_empty())
455    .collect::<Vec<_>>()
456    .join(" ")
457}
458
459/// Fingerprint the current JavaScript project using the same discovery and
460/// runtime-shim inputs as a Rust-owned execution. Query callers deliberately
461/// treat failure as "staleness unavailable", matching the frozen CLI contract.
462pub fn current_javascript_integrity(
463    root: &Path,
464    command: &[String],
465    source_roots: Option<&[String]>,
466) -> Result<RunIntegrity, String> {
467    let mut environment = std::env::vars().collect::<BTreeMap<_, _>>();
468    // The roots the run was measured under, not this shell's. A run recorded
469    // with the variable and queried from a shell without it discovered a
470    // different scope and read as stale -- or, where automatic discovery found
471    // nothing, stopped being checked at all.
472    match source_roots.filter(|roots| !roots.is_empty()) {
473        Some(roots) => {
474            environment.insert(
475                crate::source_discovery::SOURCE_ROOTS_VARIABLE.into(),
476                roots.join(","),
477            );
478        }
479        None => {
480            environment.remove(crate::source_discovery::SOURCE_ROOTS_VARIABLE);
481        }
482    }
483    let project = discover_coverage_project(root, &environment, command)
484        .map_err(|error| error.to_string())?;
485    javascript_integrity_for_project(root, &project)
486}
487
488fn javascript_integrity_for_project(
489    root: &Path,
490    project: &crate::project_discovery::CoverageProject,
491) -> Result<RunIntegrity, String> {
492    let frontend = FrontendIntegrityInputs::embedded_javascript();
493    create_run_integrity(root, project, &frontend).map_err(|error| error.to_string())
494}
495
496/// Execute one JavaScript suite with every language-neutral stage owned by
497/// Rust. Target-language runtime and runner adapters remain generated shims.
498pub fn run_direct_javascript(
499    request: &DirectJavascriptRunRequest,
500    diagnostics: &mut dyn std::io::Write,
501) -> Result<DirectJavascriptRunResult, DirectJavascriptRunError> {
502    if request.command.is_empty() {
503        return Err(DirectJavascriptRunError::Failed(
504            "test command must not be empty".into(),
505        ));
506    }
507    let total_started = Instant::now();
508    let initialization_started = Instant::now();
509    let root = crate::workspace::canonicalize_simplified(&request.root)
510        .map_err(|error| format!("{}: {error}", request.root.display()))?;
511    let nonce = now_nonce();
512    let run_id = request
513        .run_id
514        .clone()
515        .unwrap_or_else(|| format!("rust-{nonce}"));
516    let started_at = request
517        .started_at
518        .clone()
519        .unwrap_or_else(|| format!("unix-ms-{nonce}"));
520    let lock =
521        ProjectLock::acquire(&root, &run_id, &started_at).map_err(|error| error.to_string())?;
522    let mut cleanup = RunCleanup {
523        root: root.clone(),
524        run_id: run_id.clone(),
525        started_at: started_at.clone(),
526        lock,
527        workspace: None,
528        state_written: false,
529        terminal_recorded: false,
530        keep_workspace: false,
531    };
532    let recovered_runs =
533        recover_abandoned_runs(&root, &started_at).map_err(|error| error.to_string())?;
534    if !recovered_runs.is_empty() {
535        writeln!(
536            diagnostics,
537            "[supercov] recovered abandoned run(s): {}",
538            recovered_runs.join(", ")
539        )
540        .map_err(|error| error.to_string())?;
541    }
542    let environment = std::env::vars().collect::<BTreeMap<_, _>>();
543    let source_roots = crate::source_discovery::configured_source_roots(&environment);
544    let project = discover_coverage_project(&root, &environment, &request.command)
545        .map_err(|error| error.to_string())?;
546    let integrity = javascript_integrity_for_project(&root, &project)?;
547    let source_inputs = crate::source_capture::capture(
548        &root,
549        "javascript",
550        crate::integrity::javascript_assertion_paths(&root, &project).map_err(|e| e.to_string())?,
551    )?;
552    // The root too, which a moved project must not reuse. And the files that
553    // are instrumented: the fingerprint digests every first-party file
554    // whatever its classification, so SUPERCOV_SOURCE_ROOTS set on a project
555    // that had already run was silently ignored until the workspace was
556    // deleted.
557    let frontend_cache_key = format!(
558        "{}:{}:{}:{}:{}",
559        integrity.fingerprint.combined,
560        integrity.fingerprint.execution,
561        crate::source_manifest::digest(&request.command),
562        crate::source_manifest::digest(&project.root.to_string_lossy()),
563        crate::source_manifest::digest(&(&project.source_files, &source_roots))
564    );
565    let prior_workspace = cached_workspace_path(&root).map_err(|error| error.to_string())?;
566    let reusable_frontend = read_javascript_frontend_cache(&prior_workspace, &frontend_cache_key);
567    let cached_paths = reusable_frontend
568        .as_ref()
569        .map(javascript_frontend_reuse_paths)
570        .unwrap_or_default();
571    let initialization_ms = elapsed_ms(initialization_started);
572
573    let workspace_started = Instant::now();
574    let workspace_progress = crate::progress::ProgressLine::start("preparing isolated workspace");
575    let workspace = prepare_cached_workspace(&root, cleanup.lock(), &cached_paths)
576        .map_err(|error| error.to_string())?;
577    drop(workspace_progress);
578    cleanup.set_workspace(workspace.clone());
579    // The workspace this one replaced is in the trash now.
580    crate::lifecycle::sweep_trash_in_background(&root);
581    if let Err(error) = crate::workspace::mirror_git_repository(&root, &workspace)
582        && std::env::var_os("SUPERCOV_DEBUG").is_some()
583    {
584        eprintln!("[supercov] the workspace has no git repository of its own: {error}");
585    }
586    let workspace_preparation_ms = elapsed_ms(workspace_started);
587    writeln!(
588        diagnostics,
589        "[supercov] instrumenting isolated workspace {}",
590        workspace.display()
591    )
592    .map_err(|error| error.to_string())?;
593    cleanup.mark_state_written();
594    write_run_state(
595        &root,
596        &RunState {
597            id: run_id.clone(),
598            pid: std::process::id(),
599            root: root.display().to_string(),
600            workspace: workspace.display().to_string(),
601            started_at: started_at.clone(),
602            updated_at: started_at.clone(),
603            status: RunStateStatus::Preparing,
604            signal: None,
605            error: None,
606        },
607    )
608    .map_err(|error| error.to_string())?;
609
610    let adapter_started = Instant::now();
611    let instrumentation_progress = crate::progress::ProgressLine::start("instrumenting sources");
612    let collector_id = format!("collector-{}", integrity.fingerprint.execution);
613    let frontend = if let Some(cache) = &reusable_frontend {
614        load_cached_javascript_frontend(&workspace, cache)
615    } else {
616        prepare_javascript_frontend(
617            &workspace,
618            &project,
619            &collector_id,
620            &frontend_cache_key,
621            &request.command,
622        )
623    }
624    .map_err(|error| error.to_string())?;
625    crate::workspace::hide_rewritten_files(
626        &workspace,
627        &crate::javascript_frontend::rewritten_files(&workspace),
628    );
629    drop(instrumentation_progress);
630    let adapter_setup_ms = elapsed_ms(adapter_started);
631    if let Some(detail) = crate::javascript_frontend::setup_timing_detail() {
632        writeln!(diagnostics, "[supercov] {detail}").map_err(|error| error.to_string())?;
633    }
634
635    let evidence_relative = format!(".supercov/evidence/{run_id}");
636    let evidence_directory = workspace.join(&evidence_relative);
637    let server_evidence_root = workspace.join(".supercov/server-evidence");
638    let diagnostic_owner = workspace.join(format!(".supercov/diagnostic-owner-{run_id}"));
639    let mut overrides: BTreeMap<String, String> = BTreeMap::from([
640        ("NODE_OPTIONS".into(), node_options(&frontend.preload_path)),
641        ("SUPERCOV_CJS_INTERCEPT".into(), "1".into()),
642        ("SUPERCOV_DIRECT_INSTRUMENTATION".into(), "1".into()),
643        // tap measures its own coverage and fails a run short of 100%. Under
644        // Supercov it measures the instrumented copies, whose probes are
645        // branches the suite was never meant to cover: minimatch passed all
646        // 6,236 tests and still failed. Its report stays; only the gate goes.
647        ("TAP_ALLOW_INCOMPLETE_COVERAGE".into(), "1".into()),
648        // Must be absolute: monorepo runners spawn test processes with a
649        // package directory as cwd, and a relative evidence directory made
650        // every per-test record land beside the package, never collected.
651        (
652            "SUPERCOV_EVIDENCE_DIR".into(),
653            evidence_directory.display().to_string(),
654        ),
655        (
656            "SUPERCOV_DIAGNOSTIC_OWNER_FILE".into(),
657            diagnostic_owner.display().to_string(),
658        ),
659        (
660            "SUPERCOV_EXECUTION_FINGERPRINT".into(),
661            integrity.fingerprint.execution.clone(),
662        ),
663        (
664            "SUPERCOV_EXECUTION_LOG".into(),
665            evidence_directory
666                .join("execution.jsonl")
667                .display()
668                .to_string(),
669        ),
670        (
671            "SUPERCOV_MANIFEST".into(),
672            frontend.manifest_path.display().to_string(),
673        ),
674        (
675            "SUPERCOV_PROJECT_ROOT".into(),
676            workspace.display().to_string(),
677        ),
678        ("SUPERCOV_RUN_ID".into(), run_id.clone()),
679        (
680            "SUPERCOV_SERVER_EVIDENCE_ROOT".into(),
681            server_evidence_root.display().to_string(),
682        ),
683        (
684            "SUPERCOV_SOURCE_PROJECT_ROOT".into(),
685            root.display().to_string(),
686        ),
687    ]);
688    // A wrapped npm/pnpm/yarn script can launch either runner several process
689    // generations later. The preload is the discovery boundary, so always
690    // provide both generated configs; each runner ignores the unrelated one.
691    overrides.insert(
692        "SUPERCOV_GENERATED_VITEST_CONFIG".into(),
693        frontend.vitest_config_path.display().to_string(),
694    );
695    overrides.insert(
696        "SUPERCOV_GENERATED_PLAYWRIGHT_CONFIG".into(),
697        frontend.playwright_config_path.display().to_string(),
698    );
699    overrides.insert(
700        "SUPERCOV_PLAYWRIGHT_MODULE".into(),
701        project.playwright_module.clone(),
702    );
703    overrides.insert(
704        "SUPERCOV_PLAYWRIGHT_TEST_EXPORT".into(),
705        project.playwright_test_export.clone(),
706    );
707    overrides.insert(
708        "SUPERCOV_PLAYWRIGHT_WRAPPER".into(),
709        "./.supercov/node_modules/playwright.mjs".into(),
710    );
711    if let Some(original) = project
712        .playwright_config
713        .as_ref()
714        .and_then(|path| path.strip_prefix(&root).ok())
715        .map(|path| workspace.join(path))
716    {
717        overrides.insert(
718            "SUPERCOV_ORIGINAL_PLAYWRIGHT_CONFIG".into(),
719            original.display().to_string(),
720        );
721    }
722    // A tool that judges source as text, started without a package manager
723    // or installed outside the project, is found through PATH.
724    if let Some(path) = crate::source_tools::path_with_launchers(&workspace)
725        && let Some(path) = path.to_str()
726    {
727        overrides.insert("PATH".into(), path.to_owned());
728    }
729    // What bootstrap.cjs installs in a process that runs the workspace without
730    // these settings: one started in a container or VM the workspace is
731    // mounted into. It moves `root` to where it sees the workspace from.
732    let settings = serde_json::json!({
733        "root": workspace.display().to_string(),
734        "environment": overrides
735            .iter()
736            .filter(|(name, _)| name.starts_with("SUPERCOV_"))
737            .collect::<BTreeMap<_, _>>(),
738    });
739    let settings_path = frontend.preload_path.with_file_name("run.json");
740    std::fs::write(
741        &settings_path,
742        serde_json::to_vec(&settings).map_err(|error| error.to_string())?,
743    )
744    .map_err(|error| format!("{}: {error}", settings_path.display()))?;
745    // What the tools of a command before this one changed in the source has
746    // been applied or let go; this command starts from the project as it is.
747    let _ = crate::source_tools::clear_source_changes(&workspace);
748    // The command, and nothing before it.
749    let plan = ExecutionPlan {
750        preparation: Vec::new(),
751        test: ExecutionPhase {
752            name: "test".into(),
753            kind: PhaseKind::Test,
754            command: CommandSpec {
755                program: request.command[0].clone().into(),
756                arguments: request.command[1..].iter().map(OsString::from).collect(),
757                cwd: workspace.clone(),
758                environment: Some(environment_with(overrides)),
759                captured_output: None,
760            },
761        },
762    };
763    let options = supervision_options()?;
764    let watchdog_program = request.watchdog_program.as_ref().ok_or_else(|| {
765        DirectJavascriptRunError::Failed(
766            "the JavaScript run is missing its crash-containment executable".into(),
767        )
768    })?;
769    let supervisor = ProcessSupervisor::new_crash_safe(watchdog_program)
770        .map_err(|error| DirectJavascriptRunError::Failed(error.to_string()))?;
771    let output_baseline = std::cell::RefCell::new(None);
772    let execution = match execute_plan_with_supervisor(
773        &supervisor,
774        &plan,
775        options,
776        diagnostics,
777        |phase, diagnostics| {
778            {
779                // The snapshot boundary sits before the user's command, so
780                // only the command's own effects flow back to the real
781                // project afterwards.
782                *output_baseline.borrow_mut() =
783                    Some(workspace_output_baseline(&workspace).map_err(|error| {
784                        OrchestrationError::PhaseSetup {
785                            phase: phase.name.clone(),
786                            reason: error.to_string(),
787                        }
788                    })?);
789                if frontend.assertion_calls > 0 {
790                    writeln!(
791                        diagnostics,
792                        "[supercov] attributed {} native node:assert call(s)",
793                        frontend.assertion_calls
794                    )
795                    .map_err(|error| OrchestrationError::PhaseSetup {
796                        phase: phase.name.clone(),
797                        reason: error.to_string(),
798                    })?;
799                }
800                writeln!(
801                    diagnostics,
802                    "[supercov] running in isolated workspace: {}",
803                    request.command.join(" ")
804                )
805                .map_err(|error| OrchestrationError::PhaseSetup {
806                    phase: phase.name.clone(),
807                    reason: error.to_string(),
808                })?;
809            }
810            update_run_state(&root, &run_id, RunStateStatus::Testing, &started_at, None).map_err(
811                |error| OrchestrationError::PhaseSetup {
812                    phase: phase.name.clone(),
813                    reason: error.to_string(),
814                },
815            )?;
816            Ok(())
817        },
818    ) {
819        Ok(execution) => execution,
820        Err(error) => {
821            let message = error.to_string();
822            let state_updated = update_run_state(
823                &root,
824                &run_id,
825                RunStateStatus::Failed,
826                &started_at,
827                Some(message.clone()),
828            )
829            .is_ok();
830            if state_updated {
831                cleanup.mark_terminal();
832            }
833            return Err(message.into());
834        }
835    };
836    let test_command_ms = execution
837        .phases
838        .iter()
839        .find(|phase| phase.kind == PhaseKind::Test)
840        .map_or(0.0, |phase| phase.duration_ms as f64);
841    // The copy starts without build output: `dist/`, `build/`, `.next/` and
842    // the like are not copied, since they hold code that was never
843    // instrumented. A suite that needs it fails on a missing file, which
844    // reads as the project's own fault.
845    if execution.exit_code != 0
846        && execution.interrupted_signal.is_none()
847        && !project.unrun_build.is_empty()
848    {
849        let build = project.unrun_build.join(" ");
850        let _ = writeln!(
851            diagnostics,
852            "[supercov] the command failed. It ran in a copy of the project that starts without build output, and Supercov runs only the command it is given: if these tests need `{build}` first, put it in the command, as in `supercov -- sh -c \"{build} && {}\"`",
853            request.command.join(" ")
854        );
855    }
856    if let Some(signal) = execution.interrupted_signal {
857        interrupt_run_state(&root, &run_id, &started_at, signal_name(signal))
858            .map_err(|error| error.to_string())?;
859        cleanup.mark_terminal();
860        return Err(DirectJavascriptRunError::Interrupted {
861            signal,
862            exit_code: execution.exit_code,
863            timings: RunTimings {
864                initialization_ms: rounded_millisecond(initialization_ms),
865                workspace_preparation_ms: rounded_millisecond(workspace_preparation_ms),
866                adapter_setup_ms: rounded_millisecond(adapter_setup_ms),
867                instrumented_build_ms: 0.0,
868                test_command_ms: rounded_millisecond(test_command_ms),
869                evidence_publication_ms: 0.0,
870            },
871            total_ms: rounded_millisecond(elapsed_ms(total_started)),
872        });
873    }
874    // Leave the real working tree as the command alone would have: updated
875    // snapshots, generated fixtures, and reports belong in the repository,
876    // not in the cache. Deletions and instrumented-source rewrites are
877    // reported, never propagated.
878    if let Some(baseline) = output_baseline.borrow().as_ref() {
879        let protected = project
880            .source_files
881            .iter()
882            .map(PathBuf::from)
883            .collect::<std::collections::BTreeSet<_>>();
884        let outputs = sync_command_outputs(&root, &workspace, baseline, &protected)
885            .map_err(|error| error.to_string())?;
886        if outputs.synced > 0 {
887            let _ = writeln!(
888                diagnostics,
889                "[supercov] synced {} file(s) the command created or changed back to the project: {}",
890                outputs.synced,
891                crate::workspace::summarize_paths(&outputs.synced_paths)
892            );
893        }
894        if !outputs.skipped_instrumented.is_empty() {
895            let _ = writeln!(
896                diagnostics,
897                "[supercov] {} file(s) stayed in the isolated workspace: they are instrumented copies, or were built from them, and must not overwrite your project: {}",
898                outputs.skipped_instrumented.len(),
899                crate::workspace::summarize_paths(&outputs.skipped_instrumented)
900            );
901        }
902        // A formatter or a fixer the command ran changed source files. The
903        // tests ran the copy instrumented from the text before, so that is
904        // what this run measured; the project gets the change, as it does
905        // when the command runs alone.
906        match crate::source_tools::apply_source_changes(&root, &workspace) {
907            Ok(changes) => {
908                if !changes.applied.is_empty() {
909                    let _ = writeln!(
910                        diagnostics,
911                        "[supercov] the command's tools changed {} source file(s), now changed in the project too: {}; this run measured them as they were before",
912                        changes.applied.len(),
913                        crate::workspace::summarize_paths(&changes.applied)
914                    );
915                }
916                if !changes.kept_back.is_empty() {
917                    let _ = writeln!(
918                        diagnostics,
919                        "[supercov] the command's tools changed {} source file(s) that were edited in the project while it ran; those changes were not applied: {}",
920                        changes.kept_back.len(),
921                        crate::workspace::summarize_paths(&changes.kept_back)
922                    );
923                }
924            }
925            Err(error) => {
926                let _ = writeln!(
927                    diagnostics,
928                    "[supercov] the source changes the command's tools made could not be applied to the project: {error}"
929                );
930            }
931        }
932        if !outputs.deleted_in_workspace.is_empty() {
933            let _ = writeln!(
934                diagnostics,
935                "[supercov] the command deleted {} file(s) in the isolated workspace; deletions are not propagated to the project: {}",
936                outputs.deleted_in_workspace.len(),
937                crate::workspace::summarize_paths(&outputs.deleted_in_workspace)
938            );
939        }
940    }
941    update_run_state(
942        &root,
943        &run_id,
944        RunStateStatus::Publishing,
945        &started_at,
946        None,
947    )
948    .map_err(|error| error.to_string())?;
949
950    let publication_started = Instant::now();
951    let archive_path = root
952        .join(".supercov/work")
953        .join(&run_id)
954        .join("evidence.raw.gz");
955    let entries = collect_sources(&[
956        EvidenceArchiveSource::File {
957            file: frontend.manifest_path,
958            path: "manifest.json".into(),
959        },
960        EvidenceArchiveSource::File {
961            file: workspace.join(".supercov/statement-exclusions.json"),
962            path: "statement-exclusions.json".into(),
963        },
964        EvidenceArchiveSource::Directory {
965            directory: evidence_directory,
966            prefix: None,
967        },
968        EvidenceArchiveSource::Directory {
969            directory: server_evidence_root.join(&run_id),
970            prefix: Some("server".into()),
971        },
972    ])
973    .map_err(|error| error.to_string())?;
974    let entries =
975        javascript_archive_entries(entries, &frontend.manifest, &run_id, execution.exit_code)?;
976    let entries = crate::source_capture::append(entries, &source_inputs)?;
977    let raw = write_archive(entries, &archive_path).map_err(|error| error.to_string())?;
978    remove_stored_tree_deferred(&root, &workspace.join(".supercov/evidence"))
979        .map_err(|error| error.to_string())?;
980    remove_stored_tree_deferred(&root, &server_evidence_root).map_err(|error| error.to_string())?;
981    let evidence_publication_ms = elapsed_ms(publication_started);
982    let timings = RunTimings {
983        initialization_ms: rounded_millisecond(initialization_ms),
984        workspace_preparation_ms: rounded_millisecond(workspace_preparation_ms),
985        adapter_setup_ms: rounded_millisecond(adapter_setup_ms),
986        instrumented_build_ms: 0.0,
987        test_command_ms: rounded_millisecond(test_command_ms),
988        evidence_publication_ms: rounded_millisecond(evidence_publication_ms),
989    };
990    let metadata = RunMetadata {
991        id: run_id.clone(),
992        started_at: started_at.clone(),
993        duration_ms: rounded_millisecond(elapsed_ms(total_started)),
994        command: request.command.clone(),
995        test_exit_code: Some(execution.exit_code),
996        integrity,
997        raw_evidence: RawEvidenceMetadata {
998            schema_version: raw.schema_version,
999            format: raw.format.into(),
1000            file: raw.file.into(),
1001            files: raw.files,
1002            uncompressed_bytes: raw.uncompressed_bytes,
1003            compressed_bytes: raw.compressed_bytes,
1004        },
1005        isolated_build: Some(true),
1006        instrumented_build_cache: None,
1007        timings: Some(timings),
1008        merged: None,
1009        parents: None,
1010        source_roots: source_roots.clone(),
1011    };
1012    let run_directory =
1013        publish_run(&root, &metadata, &archive_path).map_err(|error| error.to_string())?;
1014    let terminal_status = if execution.exit_code == 0 {
1015        RunStateStatus::Complete
1016    } else {
1017        RunStateStatus::Failed
1018    };
1019    update_run_state(&root, &run_id, terminal_status, &started_at, None)
1020        .map_err(|error| error.to_string())?;
1021    finalize_published_run(&root, &run_id).map_err(|error| error.to_string())?;
1022    cleanup.mark_terminal();
1023    Ok(DirectJavascriptRunResult {
1024        run_id,
1025        run_directory,
1026        workspace,
1027        exit_code: execution.exit_code,
1028        assertion_calls: frontend.assertion_calls,
1029        recovered_runs,
1030        metadata,
1031    })
1032}
1033
1034#[cfg(test)]
1035mod environment_tests {
1036    use super::*;
1037
1038    #[test]
1039    fn nested_npm_drops_only_pnpm_derived_environment_aliases() {
1040        let mut environment = BTreeMap::from([
1041            ("npm_config_auto_install_peers".into(), "true".into()),
1042            ("NPM_CONFIG_SHAMEFULLY_HOIST".into(), "true".into()),
1043            (
1044                "npm_config_registry".into(),
1045                "https://registry.npmjs.org".into(),
1046            ),
1047            ("USER_VALUE".into(), "kept".into()),
1048        ]);
1049        remove_derived_pnpm_config(&mut environment);
1050        assert_eq!(
1051            environment,
1052            BTreeMap::from([
1053                ("USER_VALUE".into(), "kept".into()),
1054                (
1055                    "npm_config_registry".into(),
1056                    "https://registry.npmjs.org".into()
1057                ),
1058            ])
1059        );
1060    }
1061}