summaryrefslogtreecommitdiff
path: root/internal/editor/editor.go
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2026-06-11 08:34:41 +0300
committerPaul Buetow <paul@buetow.org>2026-06-11 08:34:41 +0300
commite95f3fdf0a66ba05ba2c8fb7e755e107f9cf7991 (patch)
tree412c7e21ec9c317beb99ed7fe0d4d93a7dacbe50 /internal/editor/editor.go
parent73dadb573f92dca310036e8793932e94277abd62 (diff)
Thread context.Context through blocking I/O entry points
Accept ctx as the first parameter on the blocking I/O entry points and propagate it to downstream blocking calls so the work is cancellable from the process entry point: - appconfig.Load / LoadWithOptions: honor ctx before the blocking file reads, returning defaults on a cancelled context. - LSP: lsp.Server.Run(ctx) ties the serve loop to the caller context via a new watchParentContext bridge (cancels the server context, aborting in-flight LLM work). Threaded through hexailsp.Run/RunWithConfig/ RunWithFactory and runtimeconfig.Store.Reload. - MCP: mcp.Server.Run(ctx) stops accepting requests once ctx is cancelled; threaded through hexaimcp.Run/RunWithFactory/RunBackfill. - editor: RunEditor/OpenTempAndEdit/OpenFile take ctx and use exec.CommandContext so a cancelled context kills the editor subprocess; threaded through hexaicli, hexaiaction and askcli call sites. Top-level callers (cmd/hexai-lsp-server, cmd/hexai-mcp-server) now build a signal-cancelled context (SIGINT/SIGTERM) so shutdown tears the run down cleanly. Updated comments to explain the cancellation flow and added cancellation tests for the LSP/MCP loops, editor, and config load. All tests pass with -race; cross-package coverage 86.2%. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Diffstat (limited to 'internal/editor/editor.go')
-rw-r--r--internal/editor/editor.go23
1 files changed, 14 insertions, 9 deletions
diff --git a/internal/editor/editor.go b/internal/editor/editor.go
index 722e336..9a9e737 100644
--- a/internal/editor/editor.go
+++ b/internal/editor/editor.go
@@ -1,6 +1,7 @@
package editor
import (
+ "context"
"errors"
"os"
"os/exec"
@@ -21,9 +22,11 @@ func Resolve() (string, error) {
}
// RunEditor is the seam that invokes the editor on the given file path.
-// Override in tests to avoid launching a real editor.
-var RunEditor = func(editor, path string) error {
- cmd := exec.Command(editor, path)
+// Override in tests to avoid launching a real editor. It uses
+// exec.CommandContext so a cancelled ctx (e.g. process shutdown) kills the
+// editor subprocess instead of leaving it blocking on terminal input.
+var RunEditor = func(ctx context.Context, editor, path string) error {
+ cmd := exec.CommandContext(ctx, editor, path)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
@@ -32,8 +35,9 @@ var RunEditor = func(editor, path string) error {
// OpenTempAndEdit creates a temporary .md file, writes initial content if provided,
// opens it in the resolved editor, then reads the final content and removes the file.
-// Returns the trimmed content.
-func OpenTempAndEdit(initial []byte) (string, error) {
+// Returns the trimmed content. ctx is forwarded to the editor subprocess so it
+// can be cancelled along with the surrounding command.
+func OpenTempAndEdit(ctx context.Context, initial []byte) (string, error) {
ed, err := Resolve()
if err != nil {
return "", err
@@ -59,7 +63,7 @@ func OpenTempAndEdit(initial []byte) (string, error) {
if err := f.Close(); err != nil {
return "", err
}
- if err := RunEditor(ed, path); err != nil {
+ if err := RunEditor(ctx, ed, path); err != nil {
return "", err
}
b, err := os.ReadFile(filepath.Clean(path))
@@ -70,8 +74,9 @@ func OpenTempAndEdit(initial []byte) (string, error) {
}
// OpenFile ensures the parent directory exists, then opens path in the editor
-// from Resolve() (HEXAI_EDITOR or EDITOR).
-func OpenFile(path string) error {
+// from Resolve() (HEXAI_EDITOR or EDITOR). ctx is forwarded to the editor
+// subprocess so it can be cancelled with the surrounding command.
+func OpenFile(ctx context.Context, path string) error {
ed, err := Resolve()
if err != nil {
return err
@@ -86,5 +91,5 @@ func OpenFile(path string) error {
return mkErr
}
}
- return RunEditor(ed, path)
+ return RunEditor(ctx, ed, path)
}