Files
elizaos--eliza/scripts/security/coverage-source-classifier.mjs
T
wehub-resource-sync 426e9eeabd
Benchmark Bridge Tests / benchmark (bunx @biomejs/biome check packages/lifeops-bench/src, benchmark-lint) (push) Waiting to run
Benchmark Bridge Tests / benchmark (bunx vitest run --config packages/lifeops-bench/vitest.config.ts --root packages/lifeops-bench --passWithNoTests, benchmark-tests) (push) Waiting to run
Build Agent Image / build-and-push (push) Waiting to run
Chat shell gestures / Chat shell gesture + parity e2e (push) Waiting to run
Cloud Gateway Discord / Test (push) Waiting to run
Cloud Gateway Webhook / Test (push) Waiting to run
Cloud Tests / lint-and-types (push) Waiting to run
Cloud Tests / unit-tests (push) Waiting to run
Cloud Tests / integration-tests (push) Waiting to run
Cloud Tests / e2e-tests (push) Blocked by required conditions
CodeQL Advanced / Analyze (javascript-typescript) (push) Waiting to run
Deploy Apps Worker (Product 2) / Determine environment (push) Waiting to run
Deploy Apps Worker (Product 2) / Deploy apps worker to apps-control host (${{ needs.determine-env.outputs.environment }}) (push) Blocked by required conditions
Deploy Eliza Provisioning Worker / Determine environment (push) Waiting to run
Deploy Eliza Provisioning Worker / Deploy worker to Hetzner host (${{ needs.determine-env.outputs.environment }} @ ${{ needs.determine-env.outputs.deployment_sha }}) (push) Blocked by required conditions
Dev Smoke / Classify changed paths (push) Waiting to run
Dev Smoke / bun run dev onboarding chat (push) Blocked by required conditions
Dev Smoke / Vite HMR dependency-level smoke (push) Blocked by required conditions
Electrobun Submodule Guard / electrobun gitlink is fetchable (push) Waiting to run
Publish @elizaos/example-code / check_npm (push) Waiting to run
Publish @elizaos/example-code / publish_npm (push) Blocked by required conditions
Publish @elizaos/plugin-elizacloud / verify_version (push) Waiting to run
Publish @elizaos/plugin-elizacloud / publish_npm (push) Blocked by required conditions
Sandbox Live Smoke / Sandbox live smoke (push) Waiting to run
Snap Build & Test / Build Snap (amd64) (push) Waiting to run
Snap Build & Test / Build Snap (arm64) (push) Waiting to run
supply-chain / sbom (push) Waiting to run
supply-chain / vulnerability-scan (push) Waiting to run
Build, Push & Deploy to Phala Cloud / build-and-push (push) Waiting to run
Test Packaging / Validate Packaging Configs (push) Waiting to run
Test Packaging / PyPI on Python ${{ matrix.python }} (push) Waiting to run
Test Packaging / Pack & Test JS Tarballs (push) Waiting to run
Test Packaging / elizaos CLI global-install smoke (node + bun) (push) Waiting to run
UI Fixture E2E / ui-fixture-e2e (push) Waiting to run
UI Fixture E2E / fixture-e2e (push) Waiting to run
UI Story Gate / story-gate (push) Waiting to run
vault-ci / test (macos-latest) (push) Waiting to run
vault-ci / test (ubuntu-latest) (push) Waiting to run
vault-ci / test (windows-latest) (push) Waiting to run
vault-ci / app-core wiring tests (push) Waiting to run
verify-patches / verify patches/CHECKSUMS.sha256 (push) Waiting to run
Voice Benchmark Smoke / voice-emotion fixture smoke (push) Waiting to run
Voice Benchmark Smoke / voiceagentbench fixture smoke (push) Waiting to run
Voice Benchmark Smoke / voicebench-quality unit smoke (push) Waiting to run
Voice Benchmark Smoke / voicebench TypeScript unit (no audio) (push) Waiting to run
Voice Benchmark Smoke / voice bench smoke summary (push) Blocked by required conditions
Windows CI / windows ([bun run --cwd packages/app-core test bun run --cwd packages/elizaos test bun run --cwd packages/cloud/shared test], app-and-cli) (push) Waiting to run
Windows CI / windows ([bun run --cwd packages/scenario-runner test bun run --cwd packages/vault test bun run --cwd packages/security test bun run --cwd plugins/plugin-coding-tools test], framework-packages) (push) Waiting to run
Windows CI / windows ([bun run --cwd plugins/plugin-elizacloud test bun run --cwd plugins/plugin-discord test bun run --cwd plugins/plugin-anthropic test bun run --cwd plugins/plugin-openai test bun run --cwd plugins/plugin-app-control test bun run --cwd plugins/pl… (push) Waiting to run
Windows CI / windows ([node packages/scripts/run-turbo.mjs run build --filter=@elizaos/core --filter=@elizaos/shared --filter=@elizaos/agent --concurrency=4 node packages/scripts/run-bash-linux-only.mjs scripts/verify-riscv64-buildpaths.sh node packages/scripts/run… (push) Waiting to run
Windows CI / windows ([node packages/scripts/run-turbo.mjs run typecheck --filter=@elizaos/core --filter=@elizaos/shared --filter=@elizaos/cloud-shared --concurrency=4 bun run --cwd packages/core test bun run --cwd packages/shared test], core-runtime, 75) (push) Waiting to run
Test Packaging / Build & Test PyPI Package (push) Waiting to run
Voice Workbench / headless workbench (mocked backends) (push) Has been cancelled
Voice Workbench / real acoustic lane (nightly, provisioned only) (push) Has been cancelled
ci / test (push) Has been cancelled
ci / lint-and-format (push) Has been cancelled
ci / build (push) Has been cancelled
ci / dev-startup (push) Has been cancelled
gitleaks / gitleaks (push) Has been cancelled
Markdown Links / Relative Markdown Links (push) Has been cancelled
Quality (Extended) / Homepage Build (PR smoke) (push) Has been cancelled
Quality (Extended) / Comment-only diff guard (push) Has been cancelled
Quality (Extended) / Format + Type Safety Ratchet (push) Has been cancelled
Quality (Extended) / Develop Gate (secret scan + UI determinism) (push) Has been cancelled
Quality (Extended) / Develop Gate (lint) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:43:05 +08:00

186 lines
6.1 KiB
JavaScript

#!/usr/bin/env node
/** Emits changed modules that retain runtime code after Node strips TypeScript-only syntax. */
import { execFileSync } from "node:child_process";
import { readFileSync } from "node:fs";
import * as nodeModule from "node:module";
import { fileURLToPath } from "node:url";
const bunTypeScriptTranspiler = globalThis.Bun
? new globalThis.Bun.Transpiler({ loader: "ts" })
: undefined;
function eraseTypeScriptSyntax(source) {
if (typeof nodeModule.stripTypeScriptTypes === "function") {
return nodeModule.stripTypeScriptTypes(source, {
mode: "transform",
sourceMap: false,
});
}
if (bunTypeScriptTranspiler) {
return bunTypeScriptTranspiler.transformSync(source);
}
throw new Error("TypeScript syntax erasure is unavailable in this runtime");
}
function runtimeSource(source) {
return eraseTypeScriptSyntax(source).trim();
}
function stripTypeScriptSyntaxPreservingComments(source) {
if (typeof nodeModule.stripTypeScriptTypes !== "function") {
throw new Error("comment-preserving TypeScript erasure is unavailable");
}
return nodeModule.stripTypeScriptTypes(source, {
mode: "strip",
sourceMap: false,
});
}
/**
* Captures every comment-like delimiter with a whitespace-insensitive source
* anchor. False positives inside literals only widen coverage enforcement;
* importantly, tooling directives can never disappear from the proof.
*/
function commentSensitiveRecords(source) {
const stripped = stripTypeScriptSyntaxPreservingComments(source);
const commentLike = /\/\/[^\r\n]*|\/\*[\s\S]*?\*\//g;
const records = [];
let cursor = 0;
let anchor = 0;
for (const match of stripped.matchAll(commentLike)) {
const index = match.index ?? 0;
anchor += stripped.slice(cursor, index).replace(/\s/g, "").length;
records.push([anchor, match[0]]);
anchor += match[0].replace(/\s/g, "").length;
cursor = index + match[0].length;
}
return records;
}
/** V8 emits no line records for a module made entirely of re-export facades. */
function isPureReExportFacade(source) {
const reExport =
/export\s+(?:\*\s*(?:as\s+[A-Za-z_$][\w$]*\s*)?|\{[^}]*\})\s+from\s+["'][^"']+["']\s*;?/gs;
return source.trim().length > 0 && source.replace(reExport, "").trim() === "";
}
/** Returns true only when type erasure leaves coverable runtime statements. */
export function sourceRetainsRuntimeCode(source) {
const emittedSource = runtimeSource(source);
return emittedSource.length > 0 && !isPureReExportFacade(emittedSource);
}
/** Returns true unless emitted JavaScript and comment-sensitive tokens match. */
export function sourceChangesRuntimeCode(baseSource, headSource) {
// Type annotations can alter emitted decorator metadata under repository
// tsconfigs even when Node's transform output is otherwise identical.
if (
baseSource !== headSource &&
(baseSource.includes("@") || headSource.includes("@"))
) {
return true;
}
const emittedHead = runtimeSource(headSource);
if (emittedHead.length === 0 || isPureReExportFacade(emittedHead)) {
return false;
}
if (runtimeSource(baseSource) !== emittedHead) return true;
return (
JSON.stringify(commentSensitiveRecords(baseSource)) !==
JSON.stringify(commentSensitiveRecords(headSource))
);
}
/** Classifier failures retain the file as a coverage target. */
export function pathRetainsRuntimeCode(
path,
warn = (message) => process.stderr.write(message),
) {
try {
const source = readFileSync(path, "utf8");
return sourceRetainsRuntimeCode(source);
} catch (error) {
// A classifier failure must widen enforcement, never hide the source.
warn(
`[coverage-source-classifier] treating unclassifiable module as executable: ${path} (${error instanceof Error ? error.message : String(error)})\n`,
);
return true;
}
}
/** Writes coverable paths and explains independently proven exclusions. */
export function classifyPaths(
paths,
writeOutput = (message) => process.stdout.write(message),
writeError = (message) => process.stderr.write(message),
{ readBaseSource } = {},
) {
for (const path of paths) {
if (!pathRetainsRuntimeCode(path, writeError)) {
writeError(
`[coverage-source-classifier] excluding module without coverable runtime statements: ${path}\n`,
);
continue;
}
if (readBaseSource) {
try {
const baseSource = readBaseSource(path);
if (baseSource !== undefined) {
const headSource = readFileSync(path, "utf8");
if (!sourceChangesRuntimeCode(baseSource, headSource)) {
writeError(
`[coverage-source-classifier] excluding runtime-equivalent source change: ${path}\n`,
);
continue;
}
}
} catch (error) {
// A failed comparison must retain the module just like a failed parse.
writeError(
`[coverage-source-classifier] treating unclassifiable source change as executable: ${path} (${error instanceof Error ? error.message : String(error)})\n`,
);
}
}
writeOutput(`${path}\n`);
}
}
function parseBaseRef(args) {
if (args.length === 0) return undefined;
if (args.length === 2 && args[0] === "--base" && args[1]) return args[1];
throw new Error(
"usage: coverage-source-classifier.mjs [--base <git-revision>]",
);
}
function readSourceAtRevision(revision, path) {
try {
return execFileSync("git", ["cat-file", "blob", `${revision}:${path}`], {
encoding: "utf8",
maxBuffer: 16 * 1024 * 1024,
stdio: ["ignore", "pipe", "ignore"],
});
} catch {
// New and renamed paths have no blob at the merge base. Retaining them is
// conservative, and also fail-widens unexpected Git lookup failures.
return undefined;
}
}
if (process.argv[1] && fileURLToPath(import.meta.url) === process.argv[1]) {
const baseRef = parseBaseRef(process.argv.slice(2));
classifyPaths(
readFileSync(0, "utf8").split("\n").filter(Boolean),
undefined,
undefined,
{
readBaseSource: baseRef
? (path) => readSourceAtRevision(baseRef, path)
: undefined,
},
);
}