fix: include pre-loaded file contents in ReactAgent system prompt#379
fix: include pre-loaded file contents in ReactAgent system prompt#379
Conversation
The ContextLoader already loaded relevant files into context.loaded_files with token budgeting, but _build_system_prompt() only included file paths, forcing the agent to waste ~30% of its iteration budget re-reading files. Changes: - Add "Relevant Source Files" section to system prompt with file contents - Soften "ALWAYS read before editing" rule to acknowledge pre-loaded files - Update initial user message to not mandate file exploration - Add 4 TDD tests for the new behavior
WalkthroughThe ReAct agent's system prompt now includes preloaded source file contents (from Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant ContextLoader
participant ReactAgent
participant LLM
participant Tools
User->>ReactAgent: Submit task + TaskContext
ReactAgent->>ContextLoader: Request context (file_tree, loaded_files)
ContextLoader-->>ReactAgent: Returns file tree + loaded_files
ReactAgent->>LLM: Build system prompt (includes Relevant Source Files) and send prompt
LLM-->>ReactAgent: Plan / actions
ReactAgent->>Tools: Invoke only-if-needed (read_file / list_files / create_file)
Tools-->>ReactAgent: Tool results
ReactAgent-->>LLM: New observations / next actions
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 inconclusive)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
No actionable comments were generated in the recent review. 🎉 🧹 Recent nitpick comments
Comment |
Include pre-loaded file contents in
|
Code Review - PR #379SummaryThis PR addresses a significant performance issue (issue #373) where the ReactAgent was wasting iterations on redundant file reads. The solution surfaces pre-loaded file contents from ✅ Strengths1. Well-targeted optimization
2. Clean implementation
3. Excellent test coverage
4. Consistent rule updates
🔍 Observations & Considerations1. Potential token budget concerns # codeframe/core/react_agent.py:534-535
file_lines.append(f.content) # No truncation or size checkWhile
Suggestion: Consider adding a sanity check or truncation for very large files in the prompt section (e.g., truncate individual files > 1000 lines with an ellipsis marker). 2. Code block language hint missing file_lines.append("```") # Line 533This is fine for the LLM, but adding language hints ( 3. Test assertion could be more precise assert "Relevant Source Files" in system_promptThis verifies the section header exists, but doesn't verify the actual updated rule text. Consider also checking: assert "unless their contents are already provided" in system_prompt
# or
assert "Read files before editing them unless" in system_promptThis would more directly test that the rule was updated (not just that the section exists). 4. Documentation alignment
🐛 Potential IssuesNone identified - The implementation is solid. The observations above are optimizations, not bugs. 🔒 SecurityNo security concerns. The change surfaces data that was already loaded and trusted. ⚡ PerformanceSignificant positive impact expected:
The trade-off is slightly larger initial prompt, but this is acceptable given the iteration savings. 📋 Test CoverageExcellent coverage:
✅ RecommendationApprove with minor suggestions. This is a well-implemented performance optimization that directly addresses a documented issue. The suggestions above are enhancements, not blockers. Optional follow-ups:
Nice work on the clear PR description and thorough testing! 🎉 |
TestWorkerAgent hardcodes its output to the codeframe repo via Path(__file__), causing this placeholder file to reappear after deletion.
Follow-up ReviewI previously provided a comprehensive review of this PR. The code remains unchanged since that review. Summary: This is a well-implemented performance optimization that addresses issue #373. The PR:
My previous suggestions remain valid but are enhancements, not blockers:
Recommendation: Approve and merge. This is a solid improvement with clear benefits and no blocking issues. |
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
.gitignore (1)
39-48:⚠️ Potential issue | 🟠 MajorAvoid ignoring a concrete test file unless it is generated.
Adding
tests/test_new_feature.pyto.gitignorewill keep that test out of version control and CI coverage, which is risky for a new feature. Unless this file is auto-generated, it should be tracked.
…omment
- Test now verifies actual updated rule text ("Read files before editing
them unless") not just the section header
- Comment on line 56 changed from "verbatim" to "adapted" since rules
are now conditional on pre-loaded context
Final Review - PR #379✅ All Previous Feedback AddressedThe latest commit (73eb480) successfully addresses the suggestions from my initial review:
Code Quality AssessmentArchitecture alignment: ✅ Excellent
Implementation quality: ✅ Strong
Test coverage: ✅ Comprehensive
Performance impact: ✅ Significant improvement
Remaining Considerations1. .gitignore change (out of scope) 2. Token budget (future enhancement) Security & Best Practices
Recommendation✅ APPROVED - Ready to merge This PR successfully resolves issue #373 with a clean, well-tested implementation. The performance improvement is substantial and the code quality is high. All previous review feedback has been addressed. Impact: High-value optimization that directly improves agent efficiency within the iteration budget, a core concern for the v2 architecture. Nice work! 🎉 |
Summary
read_filecallsWhat Changed
## Relevant Source FilessectionRoot Cause
ContextLoader._load_file_contents()already loaded relevant files with token budgeting intocontext.loaded_files, but_build_system_prompt()never included them — only file paths fromcontext.file_tree. Combined with rules mandating file reads, the agent wasted 7-9 iterations before its first write.Expected Impact
Test Plan
TestPreloadedFileContextclasstest_system_prompt_includes_loaded_files— verifies file contents appear in prompttest_system_prompt_no_loaded_files_section_when_empty— edge case: empty listtest_rules_acknowledge_preloaded_context— old rule text removedtest_initial_message_does_not_mandate_reading— no exploration mandatetest_react_agent.pypassruff check)Closes #373
Summary by CodeRabbit
Improvements
Tests
Chore