Skip to content

Commit ee2a9b8

Browse files
Copilotalexec
andauthored
Add Prompt field to Result struct combining rules and task content (#169)
* Initial plan * Add Prompt field to Result struct with combined rules and task content Co-authored-by: alexec <1142830+alexec@users.noreply.github.com> * Fix output formatting to match original behavior with trailing newline Co-authored-by: alexec <1142830+alexec@users.noreply.github.com> * Fix TestResult_Prompt to actually test Context.Run() behavior The test now creates temporary files and calls Context.Run() to verify that the Prompt field is correctly populated by the context assembly logic, instead of manually setting the Prompt field and checking it equals itself. Co-authored-by: alexec <1142830+alexec@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: alexec <1142830+alexec@users.noreply.github.com> Co-authored-by: Alex Collins <alexec@users.noreply.github.com>
1 parent a315a3c commit ee2a9b8

4 files changed

Lines changed: 73 additions & 7 deletions

File tree

main.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,7 @@ func main() {
166166
fmt.Println("---")
167167
}
168168

169-
// Output all rules
170-
for _, rule := range result.Rules {
171-
fmt.Println(rule.Content)
172-
}
173-
174-
// Output task
175-
fmt.Println(result.Task.Content)
169+
// Output the combined prompt (rules + task)
170+
fmt.Println(result.Prompt)
176171
}
177172
}

pkg/codingcontext/context.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,12 +328,21 @@ func (cc *Context) Run(ctx context.Context, taskName string) (*Result, error) {
328328
// Estimate tokens for task
329329
cc.logger.Info("Total estimated tokens", "tokens", cc.totalTokens)
330330

331+
// Build the combined prompt from all rules and task content
332+
var promptBuilder strings.Builder
333+
for _, rule := range cc.rules {
334+
promptBuilder.WriteString(rule.Content)
335+
promptBuilder.WriteString("\n")
336+
}
337+
promptBuilder.WriteString(cc.task.Content)
338+
331339
// Build and return the result
332340
result := &Result{
333341
Rules: cc.rules,
334342
Task: cc.task,
335343
Tokens: cc.totalTokens,
336344
Agent: cc.agent,
345+
Prompt: promptBuilder.String(),
337346
}
338347

339348
return result, nil

pkg/codingcontext/result.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ type Result struct {
1111
Task markdown.Markdown[markdown.TaskFrontMatter] // Task file with frontmatter and content
1212
Tokens int // Total token count
1313
Agent Agent // The agent used (from task or -a flag)
14+
Prompt string // Combined prompt: all rules and task content
1415
}
1516

1617
// MCPServers returns all MCP server configurations from rules.

pkg/codingcontext/result_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,73 @@
11
package codingcontext
22

33
import (
4+
"context"
5+
"log/slog"
6+
"os"
47
"testing"
58

69
"github.com/kitproj/coding-context-cli/pkg/codingcontext/markdown"
710
"github.com/kitproj/coding-context-cli/pkg/codingcontext/mcp"
811
)
912

13+
func TestResult_Prompt(t *testing.T) {
14+
tests := []struct {
15+
name string
16+
setup func(t *testing.T, dir string)
17+
taskName string
18+
want string
19+
}{
20+
{
21+
name: "task only without rules",
22+
setup: func(t *testing.T, dir string) {
23+
createTask(t, dir, "test-task", "task_name: test-task", "Task content\n")
24+
},
25+
taskName: "test-task",
26+
want: "Task content\n",
27+
},
28+
{
29+
name: "single rule and task",
30+
setup: func(t *testing.T, dir string) {
31+
createTask(t, dir, "test-task", "task_name: test-task", "Task content\n")
32+
createRule(t, dir, ".agents/rules/rule1.md", "", "Rule 1 content\n")
33+
},
34+
taskName: "test-task",
35+
want: "Rule 1 content\n\nTask content\n",
36+
},
37+
{
38+
name: "multiple rules and task",
39+
setup: func(t *testing.T, dir string) {
40+
createTask(t, dir, "test-task", "task_name: test-task", "Task content\n")
41+
createRule(t, dir, ".agents/rules/rule1.md", "", "Rule 1 content\n")
42+
createRule(t, dir, ".agents/rules/rule2.md", "", "Rule 2 content\n")
43+
},
44+
taskName: "test-task",
45+
want: "Rule 1 content\n\nRule 2 content\n\nTask content\n",
46+
},
47+
}
48+
49+
for _, tt := range tests {
50+
t.Run(tt.name, func(t *testing.T) {
51+
tmpDir := t.TempDir()
52+
tt.setup(t, tmpDir)
53+
54+
ctx := New(
55+
WithSearchPaths("file://"+tmpDir),
56+
WithLogger(slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelError}))),
57+
)
58+
59+
result, err := ctx.Run(context.Background(), tt.taskName)
60+
if err != nil {
61+
t.Fatalf("Run() error = %v", err)
62+
}
63+
64+
if result.Prompt != tt.want {
65+
t.Errorf("Result.Prompt = %q, want %q", result.Prompt, tt.want)
66+
}
67+
})
68+
}
69+
}
70+
1071
func TestResult_MCPServers(t *testing.T) {
1172
tests := []struct {
1273
name string

0 commit comments

Comments
 (0)