Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .claude/skills/codetrial-verify/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,28 @@ in `docs/interview-contract-versions.md`; the prompt and report goldens in
output changed on purpose, comes with that version bump, and is explained in
the commit body. The other fixtures there answer to the tests that own them.

The C and C++ harnesses are helpers inside a template string in
`web/compiler-explorer.js`, so no tool reads them as source.
`node scripts/dump-harness.mjs [--cpp] [judge-id]` prints a prelude or one
function judge's whole program to read or compile by hand, formatted when
`clang-format` is installed. The
`c-cpp-harness-audit` gate (`tests/harness/harness-audit.test.js`, in the
portable lane, not a browser shard) compiles each prelude with warnings as
errors, then builds about 25 function judges per language, enough to show every
parameter type, return type and special field once, with a stub candidate under
AddressSanitizer and UBSan. C runs inputs past the sizes its helpers assume
(4097 items, 100000 for trees and lists); C++ stops at 129, because g++ takes
tens of seconds over a longer literal and its helpers grow vectors. It lists
each fixed-capacity array and each judge it knows to fail, and fails when
either is fixed, so delete the entry with the fix. Class judges are not
audited. A change to a harness helper runs it with
`node --test tests/harness/harness-audit.test.js`. It costs about 200 CPU
seconds, so a four-core runner takes about a minute, which
`taskset -c 0-3 node --test tests/harness/harness-audit.test.js` shows
locally. On CI a missing compiler or sanitizer fails the gate; elsewhere it
skips with its reason. Building every distinct shape instead took
three times that, and a C++ program alone costs about five CPU seconds.
Comment thread
jserv marked this conversation as resolved.
Resolved

A test that passes without running anything is the failure mode this tree has
already been bitten by, hence commits like "Prove an empty test run is not a
pass". Assert on the count as well as the content when a suite discovers its
Expand Down
94 changes: 94 additions & 0 deletions scripts/dump-harness.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#!/usr/bin/env node
// Prints the C or C++ program the page builds for a judge, so the helpers that
// web/compiler-explorer.js keeps inside a template string can be read, compiled
// and run on their own. With no judge it prints the shared prelude, the text
// before the candidate.
//
// node scripts/dump-harness.mjs [--cpp] [judge-id] > harness.c
// node scripts/dump-harness.mjs --cpp | clang-format --style=LLVM
//
// The candidate is a stub that returns zero, so the output compiles as it is:
// with -std=c17 for C, and -std=c++20 for C++. Class judges are C++-only and
// have no stub here.
import { execFileSync } from "node:child_process";
import { readFileSync } from "node:fs";
import { pathToFileURL } from "node:url";
import { compilerType, generateHarness } from "../web/compiler-explorer.js";

const webFile = (path) => new URL(`../web/${path}`, import.meta.url);
const MARKER = "CODETRIAL_CANDIDATE_CODE";

/// A candidate that compiles against any signature the harness calls, so this
/// file does not mirror how the harness builds its arguments. In C an empty
/// parameter list is an unprototyped definition, which C23 reads as `(void)`,
/// so compile the result with -std=c17. In C++ the method takes any arguments.
export function stubCandidate(spec, language = "c") {
const type = Number.isInteger(spec.outputPrefixParam)
? "int"
: compilerType(spec.returnType, language);
if (language === "cpp") {
const body = type === "void" ? "{}" : "{ return {}; }";
return `class Solution { public: template <class... A> ${type} ${spec.entry}(A&&...) ${body} };`;
}
if (type === "void") return `void ${spec.entry}() {}`;
return `${type} ${spec.entry}() { static ${type} zero; return zero; }`;
}

let judges;

/// Every judge spec by id, read once.
export function loadJudges() {
if (!judges) {
const pages = JSON.parse(
readFileSync(webFile("problem-pages.json"), "utf8"),
);
judges = Object.fromEntries(
Object.entries(pages).map(([id, { page }]) => [
id,
JSON.parse(readFileSync(webFile(`judges/${page}.json`), "utf8")),
]),
);
}
return judges;
}

const shared = {};

/// The helpers every harness carries: everything before the candidate.
export function prelude(language = "c") {
if (!shared[language]) {
const spec = Object.values(loadJudges()).find((s) => s.kind === "function");
const source = generateHarness(language, spec, MARKER);
shared[language] = source.slice(0, source.indexOf(MARKER));
}
return shared[language];
}

function main() {
const args = process.argv.slice(2);
const language = args.includes("--cpp") ? "cpp" : "c";
const id = args.find((arg) => !arg.startsWith("--"));
const spec = id ? loadJudges()[id] : null;
if (id && !spec) throw new Error(`No judge named ${id}`);
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
if (spec && spec.kind !== "function")
throw new Error(`${id} is a class judge; only function judges have a stub`);
let text = spec
? generateHarness(language, spec, stubCandidate(spec, language))
: prelude(language);
try {
text = execFileSync("clang-format", ["--style=LLVM"], {
input: text,
encoding: "utf8",
maxBuffer: 1 << 26,
});
} catch (error) {
// Unformatted text is still the program, but a formatter that ran and
// failed is worth a word, where one that is not installed is not.
if (error.code !== "ENOENT")
process.stderr.write(`clang-format failed: ${error.message}\n`);
}
process.stdout.write(text);
}

if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href)
main();
5 changes: 5 additions & 0 deletions scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,11 @@ gate agent-guard-tests unittest_gate "$ROOT/tests/test_agent_guard.py"
gate agent-workflow-hooks-tests unittest_gate "$ROOT/tests/test_agent_workflow_hooks.py"
gate diffmodel-tests unittest_gate "$ROOT/tests/test_diffmodel.py"
gate --lane browser browser-tests browser_tests static

# Not a browser test, so not in a browser shard: those are cut by file size, and
# this is CPU-heavy for its size. The portable lane runs well inside the time
# the slowest browser shard takes.
gate c-cpp-harness-audit node --test "$ROOT/tests/harness/harness-audit.test.js"
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
gate test-timing node "$ROOT/scripts/check-test-timing.mjs"
gate test-timing-tests unittest_gate "$ROOT/tests/test_check_test_timing.py"
gate browser-stress-tests unittest_gate "$ROOT/tests/test_browser_stress.py"
Expand Down
Loading
Loading