Skip to content

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

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

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

Conversation

@benhoverter
Copy link
Copy Markdown
Contributor

Summary

Adds an optional ~/.openfang/RULES.md file whose contents are appended near the tail of every per-turn agent prompt (position 14.5, just before recalled memories). Lets operators set workspace-wide directives — output conventions, safety rails, formatting rules — without editing each agent's identity or rebuilding the binary.

If the file is absent, behavior is unchanged. If present, contents are loaded fresh on every prompt build (no caching), UTF-8-safe-truncated at a 2 KB cap, and rendered under a ## Global Rules (RULES.md) section with a one-line note that these rules override earlier guidance except Safety.

Changes

  • openfang-types: new read_global_rules() loader with 2 KB cap, UTF-8-safe truncation, and None on missing file. Unit-tested (present / missing / oversize / non-UTF-8-boundary).
  • openfang-runtime: PromptContext gains an Option<String> rules_md field; renderer emits the Global Rules section at slot 14.5 when present.
  • openfang-kernel: both prompt-build sites call read_global_rules() per turn and populate PromptContext.rules_md.
  • docs/global-rules.md: mechanism, precedence, and cap documented.
  • docs/templates/RULES.md.example: starter template operators can copy.
  • docs/README.md: link to the new doc.

No changes to existing functionality outside the two wire-in points. Diff is +399/-0 across 8 files.

Testing

  • cargo fmt --all --check clean
  • cargo clippy --workspace --all-targets -- -D warnings passes
  • cargo test --workspace passes (2,707 tests, 0 failures)
  • Live integration tested: end-to-end smoke confirmed directive lands when present, truncation kills rules past the 2 KB cap, missing file produces no section (context-pattern caveat noted during dev — loader behavior verified at source).

Security

  • No new unsafe code
  • No secrets or API keys in diff
  • User input validated at boundaries — RULES.md size capped at 2 KB, truncation respects UTF-8 char boundaries, non-existent file returns None (no panic, no error surface to the agent).

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