feat: tail-load global RULES.md into per-turn prompt#1196
Open
benhoverter wants to merge 5 commits into
Open
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds an optional
~/.openfang/RULES.mdfile 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: newread_global_rules()loader with 2 KB cap, UTF-8-safe truncation, andNoneon missing file. Unit-tested (present / missing / oversize / non-UTF-8-boundary).openfang-runtime:PromptContextgains anOption<String> rules_mdfield; renderer emits the Global Rules section at slot 14.5 when present.openfang-kernel: both prompt-build sites callread_global_rules()per turn and populatePromptContext.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 --checkcleancargo clippy --workspace --all-targets -- -D warningspassescargo test --workspacepasses (2,707 tests, 0 failures)Security
None(no panic, no error surface to the agent).