Rules Files Getting Messier? These Writing Practices Actually Work
This article explains why rules files lose their effectiveness as teams grow and offers practical engineering methods for keeping them manageable. It introduces the concept of "catastrophic memory," shows how to enrich each rule with a three-part structure of incident, root cause, and outcome, and describes how to separate human-readable evidence from machine-executable instructions using block comments. It also covers a four-layer structure for matching rules to their scope and timing, clarifies which constraints should be automated and which must retain human review, and outlines four rollout steps with metrics for measuring whether the rules are genuinely working.
1. The Problem Is Not Too Many Rules—It Is That Rules Lose Their Origins
Almost every team has experienced the same curve: at the start of a project, the rules file contains only a handful of entries, such as “run formatting before committing” or “do not modify the generated directory.” Six months later, it has grown to dozens of items. No one can tell which rules can still be deleted, and no one dares to delete any. New members treat it as a bible; experienced members treat it as background noise.
A common observation across nearly two thousand code repositories in the industry suggests that the root cause of this phenomenon can be summarized in one term: catastrophic memory. It does not mean that “the rules are still valid,” but rather that:
- The origin of the rule has been lost, and no one remembers what failure it was originally meant to address;
- The cost of the rule is hidden, and no one knows how much model context it consumes every day;
- The risk of deletion is subjectively amplified, with “what if we delete the wrong one and who is responsible” becoming the default answer.
As a result, the rules file degrades from a set of “constraints” into a layer of “sediment.” Writing rules that are truly effective is fundamentally an engineering approach that makes rules auditable, traceable, and layered.
2. Complete a Three-Part Structure for Each Rule: Incident, Root Cause, Outcome
A rule should not only state “what should happen” but also “why it must happen.” Each rule is recommended to include three elements:
- Incident: an error that actually occurred in the past;
- Root Cause: the underlying mechanism that caused the failure;
- Outcome: an observable improvement after adopting the rule.
A sample format:
Rule: Do not manually edit auto-generated directoriesIncident: The 2024-xx release had inconsistent API definitions, and rollback took 3 hoursRoot Cause: Generated files were partially modified by hand, overwritten during the next generation, and the differences were hiddenOutcome: After introducing generation checks, the same issue did not recur within 6 months
This approach offers three benefits: first, rules change from “slogans” into “decisions backed by evidence”; second, during review, teams can directly determine whether a rule has already been fixed upstream and can then delete it safely; third, the learning cost for new members drops significantly. A rule without an incident record should be treated as an item to be verified by default, rather than a permanent constitution.
3. Use Block Comments to Carry Human Evidence Without Consuming Model Context
The three-part structure has limited value for model reasoning but very high value for human maintenance. The two need to be separated: models only see the streamlined executable instructions, while the human-readable evidence chain is placed in block comments. Most rules files and configuration formats support block comments, for example:
<!--[Evidence] Incident: Inconsistent APIs in the release version[Root Cause] Generated files were modified by hand and then overwritten[Outcome] No recurrence after introducing the check[Review] This rule can be removed if the upstream supports read-only mounting-->Do not modify any files in auto-generated directories.
With this approach, the rule itself stays within one to two lines, while the evidence and review conditions are “folded” into comments. Context budget is saved, but audit clues are not lost, effectively creating a lightweight “rules ledger” in the comment layer.
4. Layered Structure: Put Different Types of Constraints in the Right Place
The other half of rules chaos comes from layer mismatch: writing a constraint that only applies to a specific directory into a global file, or expressing a multi-step process as a single principle. It is recommended to split rules into four layers:
- Project-level information: tech stack, directory conventions, build and test entry points, and baseline code style, placed in the rules file at the project root;
- Local constraints: rules that apply only to specific paths should be declared using path-scoped rules (matching by directory or file pattern) to avoid polluting the global scope;
- Multi-step processes: ordered operations such as release, migration, canary rollout, and rollback should be abstracted into reusable “skill/process templates” that include pre-checks, execution steps, and failure handling;
- Mandatory commands: format checks, static analysis, and required pre-commit checks should be handled by hook mechanisms that trigger automatically at fixed times, rather than relying on the model to “remember.”
The criterion for layering is simple: how broad is the rule’s scope, and how certain is its execution timing. The smaller the scope, the lower the layer; the more certain the timing, the more it should be automated.
5. The Boundary Between What Must Be Automated and What Must Stay Manual
It is important to note that not all constraints are suitable for automation. Deletion or modification operations involving payments, funds, security policies, and compliance audits must retain human review and secondary confirmation, even if they can technically be intercepted by hooks. There are three reasons:
- The cost of error for these operations is irreversible, with no rollback window;
- Compliance requirements often emphasize a “traceable responsible person” rather than an “executed script”;
- Automation rules themselves may fail due to configuration errors and require a human backstop.
A practical approach is to let automation handle “interception and alerts” and let humans handle “approval and record-keeping,” linking the two in sequence rather than replacing one with the other.
6. Rollout Steps and Metrics
It is recommended to proceed in four steps to avoid the controversy caused by rewriting everything at once:
- Inventory: export all rules and mark whether each has an incident record;
- Archive: move rules without evidence and without an owner into a to-be-verified area and observe them for one iteration;
- Layer: reassign rules to the four-layer structure based on scope and timing;
- Measure: track the number of rules, average context usage, and the number of rework incidents caused by missing rules.
If the number of rules decreases while rework does not increase, the simplification is effective; otherwise, you should roll back and add supporting evidence.
7. Conclusion
The reliability of a rules file does not depend on quantity but on explainability and layering clarity. Write down “why it exists,” fold “evidence for humans” into comments, match “scope” and “execution timing” to the right layer, and return high-risk deletions to human review—these four points form a concise and sustainable way of writing rules. In engineering practice at platforms such as Baidu AI Cloud, the same idea of “constraints as code, evidence as comments” also applies to configuration governance and process automation scenarios.
