Persisting Coding Rules Across Long LLM Sessions
Long coding sessions compress chat history and drop constraints. This guide shows how to separate task-state from project-state rules, keep must-follow policies in versioned repo docs, and reduce rule drift after context compression.
AI coding assistants put multi-turn chat, code snippets, and tool results in one context window. The window is finite, so long tasks almost always trigger compression: earlier discussion is reduced to a short summary that mainly records what is still in progress. Compression keeps the task going, but it also drops tone, exceptions, and agreements that never made it into code. If critical rules live only in the chat log, the assistant later rewrites style by default, ignores boundaries, or retries rejected designs. Persistence is not about making the model remember everything. It is about keeping constraints that must apply across turns alive independent of the summary.
Distinguish two kinds of information
Not every detail belongs in a permanent rule. Split by lifetime.
Task-state information: current branch goal, the module being changed, a temporary error, a one-off tradeoff. These expire when the task ends. Leave them in the conversation so the compressed summary can keep “how far we got.”
Project-state rules: directory conventions, naming and comment standards, forbidden areas, test and release gates, security and compliance red lines, compatibility promises for public APIs. These must apply after every turn and every compression. Put them in in-repo project notes, not in the hope that the model will “also remember” them in a summary.
A simple test: if a new session or a colleague taking over must still honor the constraint, it is project-state. If it only matters for today’s change, it is task-state.
What compression actually drops
Compression usually keeps the goal, completed steps, and open questions. It often loses four kinds of detail.
- Negative constraints. “Do not add a new dependency” or “do not change public protocol fields” often become “try to stay compatible,” then get relaxed at execution time.
- Priority. When “fix the bug quickly” conflicts with “do not reduce test coverage,” the assistant follows the path it knows unless order is explicit.
- Format and location. Which layer a file belongs in, how config keys are named, how error codes are partitioned: summaries keep conclusions, not examples.
- Exception lists. Global rules almost always have exemptions—generated code, third-party lockfiles, historical migration scripts. If exceptions are not persisted, the assistant rewrites exempt areas to the main spec.
Project notes earn their keep by encoding “must keep” as short, searchable clauses, and by naming those clauses as non-omissible in the compression policy.
How to organize project notes
Put notes at the repo root or each submodule root, in a team-standard plain-text format, and change them in the same reviews as code. Keep structure short, actionable, and light on adjectives. A skeleton (illustrative, not a product-specific format):
# Project constraints## Always in force- Language and runtime versions follow the lockfile; do not upgrade on your own.- Public APIs are additive only; breaking changes need a dedicated note and a migration path.- Business-logic changes need matching tests; do not change implementation without assertions.## Layout and naming- Keep interface definitions, implementations, and tests in separate directories.- Config keys are lowercase_with_underscores; inject environment differences, do not hard-code them.## Forbidden- Do not commit secrets, tokens, or internal network addresses.- Do not hand-edit generated directories or lockfiles and then commit.- Do not disable existing security checks just to “make it compile.”## Must survive compression- Current goal and definition of done.- Rejected approaches and why they were rejected.- The full “Always in force / Forbidden” sections above.- Remaining work and the next verification commands.## Exceptions- Leave historical migration scripts as-is unless the task explicitly requires migration.
“Must survive compression” is a meta-instruction for the assistant: when context is summarized, these items should enter the summary verbatim, not as one more paraphrase. It does not replace engineering process, but it cuts rule drift in long sessions.
Subdirectories can hold narrower notes for local conventions (frontend assets or data scripts only). On conflict, more specific wins, except security and compliance red lines, which local notes cannot override. State that priority in the parent file so two notes do not fight.
Write rules that actually get followed
Effective clauses share four traits.
Observable: a reader can tell if they were broken. Prefer “must pass existing checks before commit” over “pay attention to code quality.”
Locatable: name a path, layer, or artifact type, not “related files.”
Bounded: state language, module, and out of scope so the assistant does not over-generalize.
A short reason: one sentence is enough, e.g. “public fields are consumed externally; renaming breaks compatibility.” Long background belongs in design docs; the notes keep the constraint itself.
Counterexamples matter as much as examples. A minimal good snippet and a bad one next to a naming or error-handling rule survive compression better than abstract prose. Keep samples short; dumping whole business files makes the notes hard to maintain and harder to inject in full.
Division of labor: chat, code, and checks
Persistent rules are not the only source of truth. Align them with three other places.
Chat owns this task’s goal, scope, and temporary decisions. Opening with a few sentences that point at the relevant sections—“this change is payment callbacks only; follow Forbidden and API compatibility”—is more stable than pasting the whole file and uses less context.
Code and automated checks own what can be executed. If a formatter, linter, test, or pipeline can express the constraint, do not leave it only in prose. Notes explain why and the judgments checks cannot cover: architecture boundaries, whether new outbound calls are allowed, which directories generated code must not overwrite.
Review owns changes to the notes themselves. Rule changes go through the same review as code. That stops someone from adding “always do it this way” in chat, then watching it vanish after the next compression, or applying only to one session with no repo record.
If you use cloud IDEs or containerized builds, keep the same notes in the repository, not only on a laptop or a bookmark. Local and cloud workspaces then see the same constraints. Logs and monitoring can show whether the assistant keeps violating the same clause: if a ban is broken every week, turn it into an automated check rather than harsher wording.
Cadence for long tasks
Collaborate in this order instead of stacking all history.
- Start: confirm goal, definition of done, and directories you may change, and explicitly cite the relevant project-note sections.
- Mid-task decisions: as soon as something becomes “we always do this,” write it back into the notes or a design record, then keep coding. Do not wait for the session to end.
- Before compression: check that “must survive compression” is still accurate; drop stale goals so the summary does not carry a wrong premise.
- After compression: in one or two turns, verify key bans still hold, e.g. “are new dependencies allowed?” If the answer diverges from the file, correct context before more edits.
- End: promote still-valid temporary agreements to project rules, or mark them as one-off and state when they expire.
This moves “memory” from model context into version control. Compression affects narrative, not constraints.
Common failures and fixes
Notes too long. Huge files can still be truncated on inject. Keep the main file to cross-module red lines; push detail into subdirectories; one rule per line; periodically delete text that tools already enforce.
Contradictory rules. “Zero external dependencies” plus “use this shared capability.” State priority and scenarios; split into defaults versus an exception path that needs review.
Only positive requirements, no bans. Models fill in the vague parts. Use explicit negatives for high-risk acts: secrets, protocol breaks, turning tests off.
One-off debugging steps written as permanent rules. Keep debug commands in the task record; notes keep only stable constraints.
Everyone writes their own dialect. One main file, merged in review; subdirectories add increments only. Do not copy the whole file, rewrite it privately, and skip writing back.
Landing checklist
Before you rely on this, self-check without depending on any product’s toggle names.
- Is there a project note versioned with the code that a new teammate can follow without reading chat history?
- Are security, compatibility, and testing red lines written as negatives?
- Is there a list of information that must be kept verbatim on compression or handoff?
- Is priority between local and global notes unambiguous?
- Have automatable constraints moved into checks, not only prose?
- After the last long task, can you still find why rejected approaches were rejected, in notes or the task record?
If most answers are no, rework in long sessions is usually not “the model is not smart enough.” Constraints never left the chat lifecycle. Put project-state rules in versioned notes, leave task-state for the summary, and name non-omissible sections in compression instructions. Norms then hold across turns without a larger window. Pilot one module: freeze a dozen observable rules, watch rework for two weeks, then expand. Few hard rules beat a long, unmaintained document.
