refactor(context): system prompt section 化重构#115
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
|
/review |
| - Base claims on the current workspace state instead of generic advice.`), | ||
| }, | ||
| } | ||
| } |
There was a problem hiding this comment.
Performance: Repeated Allocations
This function allocates a new 6-element slice and calls strings.TrimSpace() 6 times on every Build() call (happens on every ReAct loop iteration, potentially 8+ times per request). The content is static and never changes.
Suggestion: Cache the result as a package-level variable:
var defaultSections = []promptSection{
{id: "agent-identity", title: "Agent Identity", content: "..."},
// ... other sections (already trimmed)
}
func defaultSystemPromptSections() []promptSection {
return defaultSections
}This eliminates 6 string allocations and 6 TrimSpace operations per Build() call.
There was a problem hiding this comment.
已处理。这次把默认 section 提升为包级缓存,defaultSystemPromptSections() 现在直接返回缓存结果,避免每轮 Build() 重建切片和重复 TrimSpace。
| case content == "": | ||
| return "" | ||
| default: | ||
| return "## " + title + "\n\n" + content |
There was a problem hiding this comment.
Performance: Inefficient String Concatenation
String concatenation with + operator creates multiple intermediate string allocations. For large content sections (project rules up to 12000 runes), this is inefficient.
Suggestion: Use strings.Builder:
var builder strings.Builder
builder.Grow(len(section.title) + len(section.content) + 6)
builder.WriteString("## ")
builder.WriteString(section.title)
builder.WriteString("\n\n")
builder.WriteString(section.content)
return builder.String()There was a problem hiding this comment.
已处理。renderPromptSection() 已改为使用 strings.Builder 渲染,减少额外字符串分配。
| return `You are NeoCode, a local coding agent. | ||
| type promptSection struct { | ||
| id string | ||
| title string |
There was a problem hiding this comment.
Code Quality: Unused Field
The id field is set throughout the codebase but never used anywhere. This adds cognitive overhead without providing value.
Suggestion: Either remove the unused field, or add documentation explaining its reserved purpose for future use (e.g., section deduplication, lookup, or ordering).
There was a problem hiding this comment.
已处理。考虑到当前实现还没有 section 去重或查找场景,这里直接删除了未使用的 id 字段,先保持结构最小化。
| - 规则文件只支持大写文件名 `AGENTS.md` | ||
| - 多份命中结果按“从全局到局部”的顺序注入 | ||
| - git 只注入摘要,不注入完整 `git status` | ||
| - 各 section 统一由 `internal/context` 内部 composer 渲染,`runtime` 仍只消费最终字符串 |
There was a problem hiding this comment.
Documentation: Non-existent Term
The term "composer" doesn't exist in the code. The actual functions are renderPromptSection and composeSystemPrompt.
Suggestion: Change to: "各 section 统一由 internal/context 内部的 renderPromptSection 和 composeSystemPrompt 函数渲染"
There was a problem hiding this comment.
已处理。文档里的 composer 表述已经改成当前代码中的实际函数:renderPromptSection 和 composeSystemPrompt。
Code Review SummaryThis PR introduces a well-structured refactoring to make system prompts composable. The changes maintain backward compatibility while improving code organization. However, several noteworthy issues were identified across code quality, performance, security, and documentation areas. Critical Security Issues (Pre-existing, not introduced by this PR)While reviewing this PR, I discovered several critical security vulnerabilities in existing code that should be addressed:
Recommendation: These vulnerabilities should be addressed in a follow-up security PR before deploying to production. Performance OptimizationsTwo performance issues were identified in the new code that compound because
Code Quality & Documentation
Test CoverageThe PR includes excellent test coverage with new tests for section ordering, blank line prevention, and truncation behavior. Well done! |
|
已根据本轮 review 收敛了当前 PR 范围内的问题:缓存默认 sections、将 review 总结里提到的几项安全问题我认同需要跟进,但它们都属于本次重构前就已存在的问题,而且范围已经超出 issue #104,因此这次没有混入当前 PR,后续更适合单独开 issue 或 PR 处理。 |
概要
internal/context中的 system prompt 从单一硬编码字符串重构为可组合的 section 结构。runtime -> provider接口不变,仍由 builder 输出最终SystemPrompt字符串。验证
go test ./...关闭 #104