-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add mailman stop command for graceful daemon shutdown #34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| package cli | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "io" | ||
| "os" | ||
|
|
||
| "agentmail/internal/daemon" | ||
| "agentmail/internal/mail" | ||
| ) | ||
|
|
||
| // MailmanStopOptions configures the MailmanStop command behavior. | ||
| type MailmanStopOptions struct { | ||
| RepoRoot string // Repository root (defaults to finding git root) | ||
| } | ||
|
|
||
| // MailmanStop implements the agentmail mailman stop command. | ||
| // Creates a .stop file to signal the daemon to shut down. | ||
| // | ||
| // Exit codes: | ||
| // - 0: Success (stop signal sent) | ||
| // - 1: Error (file exists or filesystem error) | ||
| func MailmanStop(stdout, stderr io.Writer, opts MailmanStopOptions) int { | ||
| // Find repository root | ||
| repoRoot := opts.RepoRoot | ||
| if repoRoot == "" { | ||
| var err error | ||
| repoRoot, err = mail.FindGitRoot() | ||
| if err != nil { | ||
| repoRoot, _ = os.Getwd() | ||
| } | ||
| } | ||
|
|
||
| stopPath := daemon.StopFilePath(repoRoot) | ||
|
|
||
| // Atomic create - fails if file exists (O_CREATE|O_EXCL) | ||
| f, err := os.OpenFile(stopPath, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0600) // #nosec G304 - stopPath is constructed from constants | ||
| if err != nil { | ||
| if os.IsExist(err) { | ||
| fmt.Fprintln(stderr, "Stop already pending") | ||
| return 1 | ||
| } | ||
| fmt.Fprintf(stderr, "Failed to send stop signal: %v\n", err) | ||
| return 1 | ||
| } | ||
| _ = f.Close() // G104: file was just created successfully, close error is non-critical | ||
|
|
||
| fmt.Fprintln(stdout, "Stop signal sent") | ||
| return 0 | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| package cli | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "agentmail/internal/daemon" | ||
| ) | ||
|
|
||
| // ============================================================================= | ||
| // T003-T004: Tests for MailmanStop - successful stop file creation | ||
| // ============================================================================= | ||
|
|
||
| func TestMailmanStop_CreatesStopFile(t *testing.T) { | ||
| // Create temp directory for test | ||
| tmpDir, err := os.MkdirTemp("", "agentmail-stop-test-*") | ||
| if err != nil { | ||
| t.Fatalf("Failed to create temp dir: %v", err) | ||
| } | ||
| defer os.RemoveAll(tmpDir) | ||
|
|
||
| // Create .agentmail directory (simulating existing daemon setup) | ||
| agentmailDir := filepath.Join(tmpDir, ".agentmail") | ||
| if err := os.MkdirAll(agentmailDir, 0755); err != nil { | ||
| t.Fatalf("Failed to create .agentmail dir: %v", err) | ||
| } | ||
|
|
||
| var stdout, stderr bytes.Buffer | ||
|
|
||
| // Run stop command | ||
| exitCode := MailmanStop(&stdout, &stderr, MailmanStopOptions{ | ||
| RepoRoot: tmpDir, | ||
| }) | ||
|
|
||
| // Verify exit code 0 | ||
| if exitCode != 0 { | ||
| t.Errorf("Expected exit code 0, got %d. stderr: %s", exitCode, stderr.String()) | ||
| } | ||
|
|
||
| // Verify stop file was created | ||
| stopPath := daemon.StopFilePath(tmpDir) | ||
| if _, err := os.Stat(stopPath); os.IsNotExist(err) { | ||
| t.Error("Stop file was not created") | ||
| } | ||
| } | ||
|
|
||
| func TestMailmanStop_OutputsSuccessMessage(t *testing.T) { | ||
| // Create temp directory for test | ||
| tmpDir, err := os.MkdirTemp("", "agentmail-stop-test-*") | ||
| if err != nil { | ||
| t.Fatalf("Failed to create temp dir: %v", err) | ||
| } | ||
| defer os.RemoveAll(tmpDir) | ||
|
|
||
| // Create .agentmail directory | ||
| agentmailDir := filepath.Join(tmpDir, ".agentmail") | ||
| if err := os.MkdirAll(agentmailDir, 0755); err != nil { | ||
| t.Fatalf("Failed to create .agentmail dir: %v", err) | ||
| } | ||
|
|
||
| var stdout, stderr bytes.Buffer | ||
|
|
||
| // Run stop command | ||
| exitCode := MailmanStop(&stdout, &stderr, MailmanStopOptions{ | ||
| RepoRoot: tmpDir, | ||
| }) | ||
|
|
||
| // Verify exit code 0 | ||
| if exitCode != 0 { | ||
| t.Errorf("Expected exit code 0, got %d", exitCode) | ||
| } | ||
|
|
||
| // Verify success message | ||
| expectedMsg := "Stop signal sent\n" | ||
| if stdout.String() != expectedMsg { | ||
| t.Errorf("Expected stdout %q, got %q", expectedMsg, stdout.String()) | ||
| } | ||
|
|
||
| // Verify no stderr output | ||
| if stderr.String() != "" { | ||
| t.Errorf("Expected empty stderr, got %q", stderr.String()) | ||
| } | ||
| } | ||
|
|
||
| // ============================================================================= | ||
| // T015-T016: Tests for MailmanStop - stop already pending | ||
| // ============================================================================= | ||
|
|
||
| func TestMailmanStop_StopAlreadyPending_ReturnsError(t *testing.T) { | ||
| // Create temp directory for test | ||
| tmpDir, err := os.MkdirTemp("", "agentmail-stop-test-*") | ||
| if err != nil { | ||
| t.Fatalf("Failed to create temp dir: %v", err) | ||
| } | ||
| defer os.RemoveAll(tmpDir) | ||
|
|
||
| // Create .agentmail directory | ||
| agentmailDir := filepath.Join(tmpDir, ".agentmail") | ||
| if err := os.MkdirAll(agentmailDir, 0755); err != nil { | ||
| t.Fatalf("Failed to create .agentmail dir: %v", err) | ||
| } | ||
|
|
||
| // Pre-create the stop file to simulate pending stop | ||
| stopPath := daemon.StopFilePath(tmpDir) | ||
| if err := os.WriteFile(stopPath, []byte{}, 0600); err != nil { | ||
| t.Fatalf("Failed to create stop file: %v", err) | ||
| } | ||
|
|
||
| var stdout, stderr bytes.Buffer | ||
|
|
||
| // Run stop command | ||
| exitCode := MailmanStop(&stdout, &stderr, MailmanStopOptions{ | ||
| RepoRoot: tmpDir, | ||
| }) | ||
|
|
||
| // Verify exit code 1 | ||
| if exitCode != 1 { | ||
| t.Errorf("Expected exit code 1, got %d", exitCode) | ||
| } | ||
|
|
||
| // Verify error message | ||
| expectedMsg := "Stop already pending\n" | ||
| if stderr.String() != expectedMsg { | ||
| t.Errorf("Expected stderr %q, got %q", expectedMsg, stderr.String()) | ||
| } | ||
|
|
||
| // Verify no stdout output | ||
| if stdout.String() != "" { | ||
| t.Errorf("Expected empty stdout, got %q", stdout.String()) | ||
| } | ||
| } | ||
|
|
||
| func TestMailmanStop_FilesystemError_ReturnsError(t *testing.T) { | ||
| // Create temp directory for test | ||
| tmpDir, err := os.MkdirTemp("", "agentmail-stop-test-*") | ||
| if err != nil { | ||
| t.Fatalf("Failed to create temp dir: %v", err) | ||
| } | ||
| defer os.RemoveAll(tmpDir) | ||
|
|
||
| // Do NOT create .agentmail directory - this should cause a filesystem error | ||
| // when trying to create the stop file | ||
|
|
||
| var stdout, stderr bytes.Buffer | ||
|
|
||
| // Run stop command | ||
| exitCode := MailmanStop(&stdout, &stderr, MailmanStopOptions{ | ||
| RepoRoot: tmpDir, | ||
| }) | ||
|
|
||
| // Verify exit code 1 | ||
| if exitCode != 1 { | ||
| t.Errorf("Expected exit code 1, got %d", exitCode) | ||
| } | ||
|
|
||
| // Verify error message contains expected prefix | ||
| expectedPrefix := "Failed to send stop signal:" | ||
| if len(stderr.String()) < len(expectedPrefix) || stderr.String()[:len(expectedPrefix)] != expectedPrefix { | ||
| t.Errorf("Expected stderr to start with %q, got %q", expectedPrefix, stderr.String()) | ||
| } | ||
|
Comment on lines
+158
to
+162
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧹 Nitpick | 🔵 Trivial Use The manual string slicing works but is less readable than the idiomatic helper. ♻️ Suggested improvement+import "strings"
+
// Verify error message contains expected prefix
expectedPrefix := "Failed to send stop signal:"
-if len(stderr.String()) < len(expectedPrefix) || stderr.String()[:len(expectedPrefix)] != expectedPrefix {
+if !strings.HasPrefix(stderr.String(), expectedPrefix) {
t.Errorf("Expected stderr to start with %q, got %q", expectedPrefix, stderr.String())
}🤖 Prompt for AI Agents |
||
|
|
||
| // Verify no stdout output | ||
| if stdout.String() != "" { | ||
| t.Errorf("Expected empty stdout, got %q", stdout.String()) | ||
| } | ||
| } | ||
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧹 Nitpick | 🔵 Trivial
Consider logging or documenting the silent Getwd fallback behavior.
If both
FindGitRootandGetwdfail (rare but possible), the emptyrepoRootwill cause a predictable OpenFile error. The current behavior is acceptable since the error will surface at file creation, but documenting this edge case in the function comment could help future maintainers.🤖 Prompt for AI Agents