Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(scripts): evaluate the cfg predicate instead of matching the feature name\n\nCodex and CodeRabbit found this independently, and they are right. Matching\n`feature = "x"` anywhere inside a `#[cfg(...)]` accepted two predicates that do\nnot require the feature, and the first is wrong in the most dangerous\ndirection:\n\n #[cfg(not(feature = "e2e-test-support"))] compiles when it is OFF\n #[cfg(any(feature = "e2e-test-support", unix))] compiles on unix regardless\n\nEither would have let a namespace that is always present be accepted as always\nabsent, and its controllers would have left the denominator in silence — the\nexact outcome this check exists to prevent.\n\nReplaced with a small recursive-descent parser over the predicate and an\nimplication test that is conservative by construction: it answers "no" whenever\nit cannot prove "yes". `all(...)` is proved by any one child requiring the\nfeature; `any(...)` only if every branch does; `not(...)` and anything\nunrecognised prove nothing.\n\nAttributes are now collected by matching brackets backwards from the `mod`\ndeclaration rather than by reading one line, so a predicate split across lines\nis one attribute rather than a fragment that parses to nothing, and a doc\ncomment between the attribute and the item no longer hides it.\n\nNine cases cover the semantics — four predicates that must be rejected, five\nthat must still be accepted, including the multi-line and doc-comment forms.\n\nAlso, per CodeRabbit: the fixture now writes `test_support/mod.rs` with\n`mod schemas;`, so every test drives a faithful module tree and the gate is\nfound on a grandparent declaration the way it is in the real source.\n

Shanu4ec25ef5b421
diff --git a/scripts/__tests__/domain-e2e-coverage-gate.test.mjs b/scripts/__tests__/domain-e2e-coverage-gate.test.mjs
index bbde9f87b3..513032a33a 100644
--- a/scripts/__tests__/domain-e2e-coverage-gate.test.mjs
+++ b/scripts/__tests__/domain-e2e-coverage-gate.test.mjs
@@ -72,7 +72,7 @@ function fixture(t, options = {}) {
featureGraph = {},
withExcludedNamespaces = true,
declareExcludedFeatures = true,
- gateExcludedModule = true,
+ excludedModuleCfg = '#[cfg(feature = "e2e-test-support")]',
} = options;
const root = fs.mkdtempSync(join(tmpdir(), 'domain-e2e-gate-'));
t.after(() => fs.rmSync(root, { recursive: true, force: true }));
@@ -88,15 +88,16 @@ function fixture(t, options = {}) {
'src/openhuman/test_support/schemas.rs',
controller('test', 'reset') + controller('test_support', 'workspace_root'),
);
- // The `#[cfg]` the exclusion claims. It lives on the `mod` declaration in
- // the PARENT, which is where the gate looks for it — a fixture without this
- // is a module that compiles unconditionally.
+ // A faithful module tree: `schemas.rs` is reached through `mod schemas;`,
+ // and `test_support` through the declaration in its parent. The `#[cfg]`
+ // the exclusion claims lives on the SECOND of those — which is the whole
+ // reason the gate walks the chain instead of reading the file it found the
+ // controller in.
+ write(root, 'src/openhuman/test_support/mod.rs', 'mod schemas;\n');
write(
root,
'src/openhuman/mod.rs',
- gateExcludedModule
- ? '#[cfg(feature = "e2e-test-support")]\npub mod test_support;\n'
- : 'pub mod test_support;\n',
+ `${excludedModuleCfg ? `${excludedModuleCfg}\n` : ''}pub mod test_support;\n`,
);
}
return root;
@@ -400,7 +401,7 @@ test('reads a single-quoted TOML feature array', (t) => {
// module now compiles unconditionally and its controllers dispatch. An
// exclusion is a claim about the source, so it is checked against the source.
test('fails when the module is no longer behind the #[cfg] the exclusion claims', (t) => {
- const root = fixture(t, { gateExcludedModule: false });
+ const root = fixture(t, { excludedModuleCfg: null });
write(root, 'src/openhuman/widgets/schemas_part_01.rs', controller('widgets', 'list'));
write(root, 'tests/widgets_e2e.rs', 'let m = "openhuman.widgets_list";');
@@ -425,7 +426,6 @@ test('fails when the module is no longer behind the #[cfg] the exclusion claims'
// in between — the arrangement `src/openhuman/test_support/` actually has.
test('finds the gate on an ancestor mod declaration, not just the immediate parent', (t) => {
const root = fixture(t);
- write(root, 'src/openhuman/test_support/mod.rs', 'mod schemas;\n');
write(root, 'src/openhuman/widgets/schemas_part_01.rs', controller('widgets', 'list'));
write(root, 'tests/widgets_e2e.rs', 'let m = "openhuman.widgets_list";');
@@ -472,3 +472,63 @@ test('resolves the gate for a namespace declared in mod.rs itself', (t) => {
`the exclusion must still apply; got:\n${result.stdout}`,
);
});
+
+// Codex and CodeRabbit, PR #6092, independently. A `#[cfg]` that CONTAINS the
+// feature name does not necessarily REQUIRE it, and the containing-the-name
+// reading is wrong in the most dangerous direction — `not(feature = "x")`
+// compiles precisely when the feature is OFF, so a namespace that is always
+// present would have been accepted as always absent.
+const NOT_A_GATE = [
+ ['a negated feature', '#[cfg(not(feature = "e2e-test-support"))]'],
+ ['an any() with an unrelated true branch', '#[cfg(any(feature = "e2e-test-support", unix))]'],
+ ['an any() with another feature', '#[cfg(any(feature = "e2e-test-support", feature = "other"))]'],
+ ['a negation nested in an all()', '#[cfg(all(unix, not(feature = "e2e-test-support")))]'],
+];
+
+for (const [description, attribute] of NOT_A_GATE) {
+ test(`rejects ${description} as proof of the claimed gate`, (t) => {
+ const root = fixture(t, { excludedModuleCfg: attribute });
+ write(root, 'src/openhuman/widgets/schemas_part_01.rs', controller('widgets', 'list'));
+ write(root, 'tests/widgets_e2e.rs', 'let m = "openhuman.widgets_list";');
+
+ const result = runGate(root);
+
+ assert.match(
+ result.stderr,
+ /no longer behind the gate they claim/,
+ `${attribute} does not require the feature and must not count as a gate; got:\n${result.stderr}`,
+ );
+ });
+}
+
+// The other direction: a predicate whose truth genuinely implies the feature
+// must still be accepted, or the check would fail every real gate that is not
+// spelled in the simplest possible form.
+const IS_A_GATE = [
+ ['a bare feature', '#[cfg(feature = "e2e-test-support")]'],
+ ['an all() alongside an unrelated term', '#[cfg(all(feature = "e2e-test-support", unix))]'],
+ ['an any() where every branch requires it', '#[cfg(any(all(feature = "e2e-test-support", unix), all(feature = "e2e-test-support", windows)))]'],
+ ['an attribute split across lines', '#[cfg(all(\n feature = "e2e-test-support",\n unix\n))]'],
+ ['a cfg below a doc comment', '/// Gated.\n#[cfg(feature = "e2e-test-support")]'],
+];
+
+for (const [description, attribute] of IS_A_GATE) {
+ test(`accepts ${description} as proof of the claimed gate`, (t) => {
+ const root = fixture(t, { excludedModuleCfg: attribute });
+ write(root, 'src/openhuman/widgets/schemas_part_01.rs', controller('widgets', 'list'));
+ write(root, 'tests/widgets_e2e.rs', 'let m = "openhuman.widgets_list";');
+
+ const result = runGate(root);
+
+ assert.doesNotMatch(
+ result.stderr,
+ /no longer behind the gate they claim/,
+ `${attribute} does require the feature and must count as a gate; got:\n${result.stderr}`,
+ );
+ assert.match(
+ result.stdout,
+ /Excluded 2 controller\(s\)/,
+ `the exclusion must still apply; got:\n${result.stdout}`,
+ );
+ });
+}
diff --git a/scripts/check-domain-e2e-coverage.mjs b/scripts/check-domain-e2e-coverage.mjs
index 9a5bda3141..02982356f7 100644
--- a/scripts/check-domain-e2e-coverage.mjs
+++ b/scripts/check-domain-e2e-coverage.mjs
@@ -209,7 +209,7 @@ function collectInvokedMethods() {
function collectSchemaMethods() {
const methodsByNamespace = new Map();
// Which file declared each namespace, so an exclusion can be checked against
- // the `#[cfg]` chain that actually reaches it — see `moduleGatesFor`.
+ // the `#[cfg]` chain that actually reaches it — see `moduleGateProves`.
const filesByNamespace = new Map();
for (const root of SCHEMA_ROOTS) {
@@ -235,8 +235,140 @@ function collectSchemaMethods() {
}
/**
- * The Cargo gates on the chain of `mod` declarations that must compile for
- * `file` to exist in the build.
+ * Parse the inside of a `cfg(...)` predicate into `all` / `any` / `not` / atom.
+ *
+ * Deliberately a parser and not a regex. `#[cfg(not(feature = "x"))]` and
+ * `#[cfg(any(feature = "x", unix))]` both CONTAIN the feature name while
+ * neither requires it — the first compiles precisely when the feature is OFF —
+ * so anything that only looks for the name reads them backwards.
+ */
+function parseCfgPredicate(text) {
+ let at = 0;
+ const skipSpace = () => {
+ while (at < text.length && /\s/.test(text[at])) at++;
+ };
+
+ function parseNode() {
+ skipSpace();
+ const start = at;
+ while (at < text.length && /[A-Za-z0-9_]/.test(text[at])) at++;
+ const ident = text.slice(start, at);
+ skipSpace();
+
+ if (text[at] === '(') {
+ at++;
+ const children = [];
+ for (;;) {
+ skipSpace();
+ if (at >= text.length || text[at] === ')') {
+ at++;
+ break;
+ }
+ children.push(parseNode());
+ skipSpace();
+ if (text[at] === ',') at++;
+ }
+ return { kind: ident, children };
+ }
+
+ if (text[at] === '=') {
+ at++;
+ skipSpace();
+ const quote = text[at];
+ if (quote === '"' || quote === "'") {
+ at++;
+ const from = at;
+ while (at < text.length && text[at] !== quote) at++;
+ const value = text.slice(from, at);
+ at++;
+ return { kind: 'atom', key: ident, value };
+ }
+ }
+
+ // A bare atom such as `unix` or `test`: true or false on its own terms,
+ // and never a statement about a feature.
+ return { kind: 'atom', key: ident };
+ }
+
+ return parseNode();
+}
+
+/**
+ * Does this predicate being TRUE prove `feature` is enabled?
+ *
+ * Conservative by construction — it answers "no" whenever it cannot prove
+ * "yes", which is the safe direction here: a gate wrongly believed to protect a
+ * namespace is what removes reachable controllers from the denominator.
+ *
+ * - `all(...)`: true means every child is true, so ONE child requiring the
+ * feature is enough.
+ * - `any(...)`: true means at least one child is true, so the feature is
+ * implied only if EVERY branch requires it. `any(feature = "x", unix)` is
+ * satisfied on unix with `x` off.
+ * - `not(...)`: proves nothing about the feature being on, and
+ * `not(feature = "x")` is true exactly when it is off.
+ */
+function cfgProvesFeature(node, feature) {
+ if (!node) return false;
+ switch (node.kind) {
+ case 'atom':
+ return node.key === 'feature' && node.value === feature;
+ case 'all':
+ return node.children.some((child) => cfgProvesFeature(child, feature));
+ case 'any':
+ return node.children.length > 0 && node.children.every((child) => cfgProvesFeature(child, feature));
+ default:
+ // `not`, and anything unrecognised, prove nothing.
+ return false;
+ }
+}
+
+/**
+ * The `#[...]` attribute bodies attached to the item starting at `index`.
+ *
+ * Walks backwards over stacked attributes and any doc comments between them,
+ * matching brackets rather than reading a line at a time — a `#[cfg(all(
+ * feature = "x",
+ * unix))]` split across lines is one attribute, and a line-based scan would
+ * see only its last line and parse nothing.
+ */
+function attributesBefore(text, index) {
+ const attributes = [];
+ let end = index;
+
+ for (;;) {
+ let cursor = end - 1;
+ for (;;) {
+ while (cursor >= 0 && /\s/.test(text[cursor])) cursor--;
+ const lineStart = text.lastIndexOf('\n', cursor) + 1;
+ if (/^\s*\/\//.test(text.slice(lineStart, cursor + 1))) {
+ cursor = lineStart - 1;
+ continue;
+ }
+ break;
+ }
+
+ if (cursor < 0 || text[cursor] !== ']') break;
+ let depth = 0;
+ let open = cursor;
+ for (; open >= 0; open--) {
+ if (text[open] === ']') depth++;
+ else if (text[open] === '[') {
+ depth--;
+ if (depth === 0) break;
+ }
+ }
+ if (open <= 0 || text[open - 1] !== '#') break;
+
+ attributes.push(text.slice(open + 1, cursor));
+ end = open - 1;
+ }
+
+ return attributes;
+}
+
+/**
+ * Is `file` reachable only when `feature` is enabled?
*
* `#[cfg]` sits on the `mod` declaration in the PARENT, never in the file
* itself, so this walks upward: `src/openhuman/test_support/schemas.rs` is
@@ -244,10 +376,9 @@ function collectSchemaMethods() {
* `pub mod test_support;` in `openhuman/mod.rs` — and only the second carries
* the gate. A file pulled in by `include!` has no `mod` of its own and simply
* contributes nothing at its own level, which is why a missing declaration is
- * not itself an error here.
+ * not an error here; one gated ancestor anywhere on the chain is enough.
*/
-function moduleGatesFor(file) {
- const gates = new Set();
+function moduleGateProves(file, feature) {
let segments = path.relative(ROOT, file).split(path.sep);
while (segments.length > 1) {
@@ -260,13 +391,13 @@ function moduleGatesFor(file) {
const declaringFile = path.join(ROOT, ...parentDir, 'mod.rs');
if (fs.existsSync(declaringFile)) {
- const lines = read(declaringFile).split('\n');
- for (let i = 0; i < lines.length; i++) {
- if (!new RegExp(`^\\s*(?:pub(?:\\([^)]*\\))?\\s+)?mod\\s+${name}\\s*;`).test(lines[i])) continue;
- // Attributes stack above the declaration; read back through all of them.
- for (let j = i - 1; j >= 0 && /^\s*#\[/.test(lines[j]); j--) {
- const gate = lines[j].match(/#\[cfg\(.*?feature\s*=\s*"([a-z0-9-]+)"/)?.[1];
- if (gate) gates.add(gate);
+ const text = read(declaringFile);
+ const declaration = new RegExp(`^[ \\t]*(?:pub(?:\\([^)]*\\))?[ \\t]+)?mod[ \\t]+${name}[ \\t]*;`, 'm');
+ const found = declaration.exec(text);
+ if (found) {
+ for (const attribute of attributesBefore(text, found.index)) {
+ const inner = attribute.match(/^\s*cfg\s*\(([\s\S]*)\)\s*$/)?.[1];
+ if (inner && cfgProvesFeature(parseCfgPredicate(inner), feature)) return true;
}
}
}
@@ -275,7 +406,7 @@ function moduleGatesFor(file) {
if (segments[segments.length - 1] === 'src') break;
}
- return gates;
+ return false;
}
/**
@@ -389,7 +520,7 @@ function checkExclusions(discovered, declaringFiles, labelForNamespace) {
const files = declaringFiles.get(namespace);
if (!files) continue; // Already reported by (2); nothing to check against.
const unguarded = [...files]
- .filter((file) => !moduleGatesFor(file).has(entry.feature))
+ .filter((file) => !moduleGateProves(file, entry.feature))
.map((file) => path.relative(ROOT, file))
.sort();
if (unguarded.length > 0) {