xfi-create-rule
End-to-end guide for creating a new X-Fidelity analysis rule. Use when creating rules, adding new checks, or when the user asks about rule development.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
End-to-end guide for creating a new X-Fidelity analysis rule. Use when creating rules, adding new checks, or when the user asks about rule development.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
Multi-purpose utility for CRUX compression workflows. Provides token estimation and checksum calculation. Use when estimating tokens for compression, comparing file sizes, or getting checksums for sourceChecksum tracking.
Guide for creating engineering plans through a structured workflow. Use when planning new features, coordinating multi-agent work, or breaking down complex initiatives.
Guide for executing engineering plans through coordinated subagent work. Use when executing existing plans from knowledge/plans/ directory.
Guide for creating a new package in the X-Fidelity monorepo. Use when adding new packages, setting up monorepo structure, or configuring workspace dependencies.
Guide for ensuring CLI and VSCode extension produce identical analysis results. Use when verifying CLI-Extension parity, debugging output differences, or setting up consistency checks.
Guide for creating a new X-Fidelity archetype configuration. Use when defining project templates, configuring rule sets, or setting up dependency requirements.
| name | xfi-create-rule |
| description | End-to-end guide for creating a new X-Fidelity analysis rule. Use when creating rules, adding new checks, or when the user asks about rule development. |
This skill guides you through creating new analysis rules for X-Fidelity.
| Type | Suffix | When It Runs | Example Use Case |
|---|---|---|---|
| Global | -global | Once per repository | Missing required files, dependency checks |
| Iterative | -iterative | Once per matching file | Code complexity, pattern detection |
Copy this checklist to track progress:
Rule Creation Progress:
- [ ] Step 1: Define rule purpose and type
- [ ] Step 2: Find or create required fact
- [ ] Step 3: Find or create required operator
- [ ] Step 4: Write rule JSON
- [ ] Step 5: Add to archetype
- [ ] Step 6: Test the rule
Determine:
warning (informational) or fatality (blocks CI)Facts collect data. Check existing facts in packages/x-fidelity-plugins/src/.
| Plugin | Facts |
|---|---|
xfiPluginAst | functionComplexity, functionCount, astFact |
xfiPluginDependency | repoDependencyFacts |
xfiPluginFilesystem | repoFilesystemFacts |
xfiPluginPatterns | globalFileAnalysis |
xfiPluginReactPatterns | effectCleanup, hookDependency |
xfiPluginRequiredFiles | missingRequiredFiles |
xfiPluginExtractValues | extractValues |
xfiPluginSimpleExample | customFact |
If no existing fact works, create one in the appropriate plugin:
File: packages/x-fidelity-plugins/src/xfiPlugin{Name}/facts/{factName}Fact.ts
import { FactDefn } from '@x-fidelity/types';
import { pluginLogger } from '@x-fidelity/core';
interface MyFactParams {
resultFact?: string;
threshold?: number;
}
export const myFact: FactDefn = {
name: 'myFact',
description: 'Description of what this fact collects',
type: 'iterative-function', // or 'global-function'
priority: 1,
fn: async (params: unknown, almanac?: unknown): Promise<any> => {
const logger = pluginLogger.createOperationLogger('plugin-name', 'myFact');
const factParams = params as MyFactParams;
// Collect and return data
const result = { /* collected data */ };
// Optionally store result for later reference
if (factParams?.resultFact && almanac?.addRuntimeFact) {
almanac.addRuntimeFact(factParams.resultFact, result);
}
return result;
}
};
Add to plugin index: Export the fact in packages/x-fidelity-plugins/src/xfiPlugin{Name}/index.ts
Operators compare values. Check existing operators in the same plugin locations.
From json-rules-engine:
equal, notEqualgreaterThan, lessThan, greaterThanInclusive, lessThanInclusivein, notIn, contains, doesNotContainFile: packages/x-fidelity-plugins/src/xfiPlugin{Name}/operators/{operatorName}.ts
import { OperatorDefn } from '@x-fidelity/types';
import { pluginLogger } from '@x-fidelity/core';
export const myOperator: OperatorDefn = {
name: 'myOperator',
description: 'Description of comparison logic',
fn: (factValue: any, operatorValue: any): boolean => {
const logger = pluginLogger.createOperationLogger('plugin-name', 'myOperator');
// Compare factValue against operatorValue
// Return true if condition is met (rule triggers)
return factValue > operatorValue;
}
};
Add to plugin index: Export the operator alongside facts.
Create rule file in packages/x-fidelity-democonfig/src/rules/
File: {ruleName}-iterative-rule.json
{
"name": "myRule-iterative",
"conditions": {
"all": [
{
"fact": "fileData",
"path": "$.fileName",
"operator": "notEqual",
"value": "REPO_GLOBAL_CHECK"
},
{
"fact": "myFact",
"params": {
"resultFact": "myRuleResult",
"threshold": 10
},
"operator": "myOperator",
"value": true
}
]
},
"event": {
"type": "warning",
"params": {
"message": "Description of what was detected",
"details": {
"fact": "myRuleResult"
}
}
}
}
File: {ruleName}-global-rule.json
{
"name": "myRule-global",
"conditions": {
"all": [
{
"fact": "fileData",
"path": "$.fileName",
"operator": "equal",
"value": "REPO_GLOBAL_CHECK"
},
{
"fact": "myFact",
"params": {
"resultFact": "myRuleResult"
},
"operator": "myOperator",
"value": true
}
]
},
"event": {
"type": "fatality",
"params": {
"message": "Critical issue detected",
"details": {
"fact": "myRuleResult"
}
}
}
}
| Element | Purpose |
|---|---|
name | Rule identifier with -iterative or -global suffix |
conditions.all | All conditions must be true |
conditions.any | At least one condition must be true |
fact | Name of the fact to evaluate |
params | Parameters passed to the fact |
params.resultFact | Store result for use in event message |
operator | Comparison operator to use |
value | Value to compare against |
event.type | warning or fatality |
event.params.details.fact | Reference stored result in message |
Edit the archetype configuration to include your rule.
File: packages/x-fidelity-democonfig/src/{archetype-name}.json
{
"name": "node-fullstack",
"rules": [
"existingRule-iterative",
"myRule-iterative"
]
}
Note: Rule name in archetype omits the -rule.json suffix but includes -iterative or -global.
# Test the plugin
yarn workspace @x-fidelity/plugins test
# Run full test suite
yarn test
# Run analysis against test fixtures
cd packages/x-fidelity-fixtures/node-fullstack
yarn xfi --configServer local --archetype node-fullstack --debug
| Purpose | Location |
|---|---|
| Facts | packages/x-fidelity-plugins/src/xfiPlugin*/facts/ |
| Operators | packages/x-fidelity-plugins/src/xfiPlugin*/operators/ |
| Rules | packages/x-fidelity-democonfig/src/rules/ |
| Archetypes | packages/x-fidelity-democonfig/src/*.json |
| Plugin registry | packages/x-fidelity-plugins/src/index.ts |