Skip to main content

Context Rules

Context rules give you declarative control over what gets included in — or excluded from — the assembled context window. They live in .codebolt/ContextRuleEngine/ as JSON files and are evaluated every turn before memory retrieval begins.

Rule structure

A rule is a condition → action pair:

{
"name": "include-architecture-on-refactor",
"condition": {
"field": "task.text",
"op": "contains",
"value": "refactor"
},
"action": {
"include": "markdown://architecture-decisions"
}
}

If the condition matches, the action fires. Rules are evaluated in the order they appear in the file; the first matching rule for a given memory reference wins.

Condition operators

OperatorMatches when…
containsField string contains the value (case-insensitive)
startsWithField string starts with the value
endsWithField string ends with the value
matchesField matches a regular expression
equalsField equals the value exactly
not_equalsField does not equal the value
inField value is one of a set
not_inField value is not in a set
gtField numeric value is greater than
ltField numeric value is less than
gteField numeric value is greater than or equal
lteField numeric value is less than or equal
existsField is present and non-null

Multiple conditions in one rule combine with AND logic — all must match for the rule to fire.

{
"condition": [
{ "field": "task.text", "op": "contains", "value": "payment" },
{ "field": "task.text", "op": "not_contains", "value": "refund" }
],
"action": { "include": "vector://payment-architecture" }
}

Condition fields

Field pathWhat it refers to
task.textThe current user message / task description
task.typeTask type if set (bug, feature, refactor, etc.)
agent.nameThe name of the running agent
project.nameThe current project name
session.turnCountHow many turns have elapsed this session
memory.sourceThe memory source being evaluated (for per-source cap rules)

Action types

include

Adds a memory reference to the assembly regardless of whether a retrieval pipeline would have fetched it:

{ "action": { "include": "kv://user-preferences" } }
{ "action": { "include": "markdown://coding-standards" } }
{ "action": { "include": "json://project-config" } }

exclude

Suppresses a memory reference even if a retrieval pipeline returns it:

{ "action": { "exclude": "vector://test-history" } }

Useful when a source is relevant in general but noisy for a specific task type.

cap

Limits how many tokens a source can contribute to the context window:

{
"condition": { "field": "task.text", "op": "contains", "value": "quick question" },
"action": { "cap": { "source": "vector://", "tokens": 512 } }
}

setPriority

Moves a section's effective priority up or down relative to the default order:

{ "action": { "setPriority": { "section": "history", "value": "high" } } }

Rule files

Rules can be split across multiple files in .codebolt/ContextRuleEngine/. Files are loaded alphabetically and concatenated before evaluation. Naming files with numeric prefixes gives you explicit ordering:

.codebolt/ContextRuleEngine/
01_base-rules.json
02_project-rules.json
03_agent-overrides.json

Common patterns

Always include a style guide

{
"name": "always-style-guide",
"condition": { "field": "task.text", "op": "exists" },
"action": { "include": "markdown://style-guide" }
}

Suppress test files during production refactors

{
"name": "suppress-tests-on-refactor",
"condition": [
{ "field": "task.text", "op": "contains", "value": "refactor" },
{ "field": "task.text", "op": "not_contains", "value": "test" }
],
"action": { "exclude": "vector://test-suite-history" }
}

Limit history on quick questions

{
"name": "short-history-for-quick-questions",
"condition": { "field": "task.type", "op": "equals", "value": "question" },
"action": { "cap": { "section": "history", "tokens": 1000 } }
}

Inject agent-specific context

{
"name": "code-agent-context",
"condition": { "field": "agent.name", "op": "equals", "value": "code-agent" },
"action": { "include": "json://code-agent-config" }
}

Testing rules

Rules are evaluated on every turn. To verify a rule is firing:

  1. Open the assembly trace panel in the Codebolt UI after a turn
  2. Look for the contextRules section — it lists which rules matched and what action each took
  3. Check the included and excluded lists to confirm the expected memory references are present

See also