Skip to content

Commit d577ca7

Browse files
Copilotalexec
andauthored
Skip rules file creation when using -w with -r flags (#159)
* Initial plan * Fix: Skip writing rules file when using -w with -r (resume mode) 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>
1 parent 080db58 commit d577ca7

2 files changed

Lines changed: 114 additions & 26 deletions

File tree

integration_test.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1406,3 +1406,88 @@ func TestWriteRulesOptionWithoutAgent(t *testing.T) {
14061406
t.Errorf("expected error message about requiring an agent, got: %s", output)
14071407
}
14081408
}
1409+
1410+
func TestWriteRulesOptionWithResumeMode(t *testing.T) {
1411+
dirs := setupTestDirs(t)
1412+
1413+
// Create a rule file
1414+
ruleFile := filepath.Join(dirs.rulesDir, "test-rule.md")
1415+
ruleContent := `---
1416+
language: go
1417+
---
1418+
# Test Rule
1419+
1420+
This is a test rule that should NOT be written in resume mode.
1421+
`
1422+
if err := os.WriteFile(ruleFile, []byte(ruleContent), 0o644); err != nil {
1423+
t.Fatalf("failed to write rule file: %v", err)
1424+
}
1425+
1426+
// Create a resume task file
1427+
taskFile := filepath.Join(dirs.tasksDir, "test-task-resume.md")
1428+
taskContent := `---
1429+
resume: true
1430+
---
1431+
# Test Task Resume
1432+
1433+
This is the task prompt for resume mode.
1434+
`
1435+
if err := os.WriteFile(taskFile, []byte(taskContent), 0o644); err != nil {
1436+
t.Fatalf("failed to write task file: %v", err)
1437+
}
1438+
1439+
// Create a temporary home directory for this test
1440+
tmpHome := t.TempDir()
1441+
1442+
// Run with -w flag, -r flag (resume mode), and -a copilot
1443+
wd, err := os.Getwd()
1444+
if err != nil {
1445+
t.Fatalf("failed to get working directory: %v", err)
1446+
}
1447+
cmd := exec.Command("go", "run", wd, "-C", dirs.tmpDir, "-a", "copilot", "-w", "-r", "test-task-resume")
1448+
var stdout, stderr bytes.Buffer
1449+
cmd.Stdout = &stdout
1450+
cmd.Stderr = &stderr
1451+
// Build a clean environment that explicitly sets GOMODCACHE outside tmpDir
1452+
// to avoid permission issues during cleanup
1453+
gomodcache := os.Getenv("GOMODCACHE")
1454+
if gomodcache == "" {
1455+
gomodcache = filepath.Join(os.Getenv("HOME"), "go", "pkg", "mod")
1456+
}
1457+
cmd.Env = append(os.Environ(),
1458+
"HOME="+tmpHome,
1459+
"GOMODCACHE="+gomodcache,
1460+
)
1461+
if err := cmd.Run(); err != nil {
1462+
t.Fatalf("failed to run binary: %v\nstdout: %s\nstderr: %s", err, stdout.String(), stderr.String())
1463+
}
1464+
1465+
output := stdout.String()
1466+
stderrOutput := stderr.String()
1467+
1468+
// Verify that the rules were NOT printed to stdout
1469+
if strings.Contains(output, "# Test Rule") {
1470+
t.Errorf("rules should not be in stdout when using -w flag with resume mode")
1471+
}
1472+
1473+
// Verify that the task IS printed to stdout
1474+
if !strings.Contains(output, "# Test Task Resume") {
1475+
t.Errorf("task content not found in stdout")
1476+
}
1477+
if !strings.Contains(output, "This is the task prompt for resume mode.") {
1478+
t.Errorf("task description not found in stdout")
1479+
}
1480+
1481+
// Verify that NO rules file was created in resume mode
1482+
expectedRulesPath := filepath.Join(tmpHome, ".github", "agents", "AGENTS.md")
1483+
if _, err := os.Stat(expectedRulesPath); err == nil {
1484+
t.Errorf("rules file should NOT be created in resume mode with -w flag, but found at %s", expectedRulesPath)
1485+
} else if !os.IsNotExist(err) {
1486+
t.Fatalf("unexpected error checking for rules file: %v", err)
1487+
}
1488+
1489+
// Verify that the logger did NOT report writing rules
1490+
if strings.Contains(stderrOutput, "Rules written") {
1491+
t.Errorf("stderr should NOT contain 'Rules written' message in resume mode")
1492+
}
1493+
}

main.go

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -98,38 +98,41 @@ func main() {
9898
os.Exit(1)
9999
}
100100

101-
relativePath := result.Agent.UserRulePath()
102-
if relativePath == "" {
103-
logger.Error("Error", "error", fmt.Errorf("no user rule path available for agent"))
104-
os.Exit(1)
105-
}
101+
// Skip writing rules file in resume mode since no rules are collected
102+
if !resume {
103+
relativePath := result.Agent.UserRulePath()
104+
if relativePath == "" {
105+
logger.Error("Error", "error", fmt.Errorf("no user rule path available for agent"))
106+
os.Exit(1)
107+
}
106108

107-
// Construct full path by joining with home directory
108-
rulesFile := filepath.Join(homeDir, relativePath)
109-
rulesDir := filepath.Dir(rulesFile)
109+
// Construct full path by joining with home directory
110+
rulesFile := filepath.Join(homeDir, relativePath)
111+
rulesDir := filepath.Dir(rulesFile)
110112

111-
// Create directory if it doesn't exist
112-
if err := os.MkdirAll(rulesDir, 0o755); err != nil {
113-
logger.Error("Error", "error", fmt.Errorf("failed to create rules directory %s: %w", rulesDir, err))
114-
os.Exit(1)
115-
}
113+
// Create directory if it doesn't exist
114+
if err := os.MkdirAll(rulesDir, 0o755); err != nil {
115+
logger.Error("Error", "error", fmt.Errorf("failed to create rules directory %s: %w", rulesDir, err))
116+
os.Exit(1)
117+
}
116118

117-
// Build rules content, trimming each rule and joining with consistent spacing
118-
var rulesContent strings.Builder
119-
for i, rule := range result.Rules {
120-
if i > 0 {
121-
rulesContent.WriteString("\n\n")
119+
// Build rules content, trimming each rule and joining with consistent spacing
120+
var rulesContent strings.Builder
121+
for i, rule := range result.Rules {
122+
if i > 0 {
123+
rulesContent.WriteString("\n\n")
124+
}
125+
rulesContent.WriteString(strings.TrimSpace(rule.Content))
122126
}
123-
rulesContent.WriteString(strings.TrimSpace(rule.Content))
124-
}
125-
rulesContent.WriteString("\n")
127+
rulesContent.WriteString("\n")
126128

127-
if err := os.WriteFile(rulesFile, []byte(rulesContent.String()), 0o644); err != nil {
128-
logger.Error("Error", "error", fmt.Errorf("failed to write rules to %s: %w", rulesFile, err))
129-
os.Exit(1)
130-
}
129+
if err := os.WriteFile(rulesFile, []byte(rulesContent.String()), 0o644); err != nil {
130+
logger.Error("Error", "error", fmt.Errorf("failed to write rules to %s: %w", rulesFile, err))
131+
os.Exit(1)
132+
}
131133

132-
logger.Info("Rules written", "path", rulesFile)
134+
logger.Info("Rules written", "path", rulesFile)
135+
}
133136

134137
// Output only task frontmatter and content
135138
if taskContent := result.Task.FrontMatter.Content; taskContent != nil {

0 commit comments

Comments
 (0)