Skip to content

feat: tail-load global RULES.md into per-turn prompt#3

Open
benhoverter wants to merge 5 commits into
mainfrom
feat/rules-md-tail-load
Open

feat: tail-load global RULES.md into per-turn prompt#3
benhoverter wants to merge 5 commits into
mainfrom
feat/rules-md-tail-load

Conversation

@benhoverter
Copy link
Copy Markdown
Owner

Summary

Adds a per-turn loader for ~/.openfang/RULES.md that appends user-defined global rules to every agent's system prompt. Hot-reloadable (re-read each turn), 2KB cap, missing file = no-op.

Commit chain

  • d1271c4 feat(types): add read_global_rules() loader with 2KB cap
  • 3746232 feat(runtime): add rules_md field to PromptContext
  • 20e022f feat(runtime): render RULES.md section at position 14.5 (after Workspace Context, before Safety override line)
  • 7ba629b feat(kernel): wire RULES.md loader into per-turn prompt build
  • fd497d5 docs: document global RULES.md mechanism + example template (docs/global-rules.md, docs/templates/RULES.md.example)

Behavior

  • Loader runs each turn during prompt build — edits to ~/.openfang/RULES.md take effect on the next message, no daemon restart.
  • Files larger than 2KB are silently truncated.
  • Missing file renders nothing (no error, no section).
  • Position 14.5 places rules after workspace context but before the Safety section, so Safety still overrides.

Test plan

  • Manual smoke test: dropped - Always end responses with 🦊 in ~/.openfang/RULES.md, sent message to coder-openfang, rule was respected on next turn.
  • 2KB cap test (oversized file truncates without error)
  • Missing-file test (deletion reverts behavior cleanly)

🤖 Generated with Claude Code

Introduce read_global_rules() in openfang-types::config that reads
$OPENFANG_HOME/RULES.md, trims it, and returns None if absent or blank.
Output is truncated to RULES_MD_MAX_CHARS (2000) so a runaway file can't
balloon the system prompt.

Companion to the per-turn reload path: the kernel calls this on every
turn so edits to RULES.md land on the next message without restart.

Includes 4 unit tests: missing file, empty/whitespace, present, oversized.
Adds Option<String> rules_md alongside context_md. Field is per-turn
reloadable; the kernel will populate it from read_global_rules() on
each turn. Default is None so existing call sites keep compiling via
#[derive(Default)].
Insert a new ## Global Rules (RULES.md) section into build_system_prompt
between Workspace Context (14) and Live Context (15). The framing makes
the rules authoritative over earlier guidance, with one explicit
carveout: the Safety section above remains non-negotiable.

Section is elided when ctx.rules_md is None or trims to empty so the
prompt stays clean for users who never write a RULES.md.

Includes 4 unit tests covering presence, blank/None elision, ordering
relative to Safety + Live Context, and subagent inheritance.
Populate PromptContext.rules_md via openfang_types::config::read_global_rules()
at both PromptContext construction sites in kernel.rs. Re-read on every turn
alongside context_md so edits to ~/.openfang/RULES.md take effect on the
very next message — no daemon restart required.
- docs/global-rules.md: full reference for the per-turn RULES.md
  overlay — contract, position in the system prompt (14.5), authority
  + Safety carveout, subagent inheritance, gotchas, worked example.
- docs/templates/RULES.md.example: starter template with common
  patterns (style preferences, channel etiquette, escalation rules)
  all commented out so users uncomment what they want.
- docs/README.md: index updated under Getting Started and Important
  Paths.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant