diff options
| author | Paul Buetow <paul@buetow.org> | 2026-06-11 08:34:41 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-06-11 08:34:41 +0300 |
| commit | e95f3fdf0a66ba05ba2c8fb7e755e107f9cf7991 (patch) | |
| tree | 412c7e21ec9c317beb99ed7fe0d4d93a7dacbe50 /internal/hexaicli | |
| parent | 73dadb573f92dca310036e8793932e94277abd62 (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/hexaicli')
| -rw-r--r-- | internal/hexaicli/editor_integration_test.go | 6 | ||||
| -rw-r--r-- | internal/hexaicli/run_editor_behavior_test.go | 2 | ||||
| -rw-r--r-- | internal/hexaicli/runner.go | 8 | ||||
| -rw-r--r-- | internal/hexaicli/runner_test.go | 6 |
4 files changed, 12 insertions, 10 deletions
diff --git a/internal/hexaicli/editor_integration_test.go b/internal/hexaicli/editor_integration_test.go index 7d53d1f..e5580be 100644 --- a/internal/hexaicli/editor_integration_test.go +++ b/internal/hexaicli/editor_integration_test.go @@ -28,7 +28,9 @@ func TestRun_NoArgs_OpensEditor(t *testing.T) { newClientFromApp = func(_ appconfig.App) (llm.Client, error) { return cliFake{}, nil } t.Cleanup(func() { newClientFromApp = oldNew }) oldRun := editor.RunEditor - editor.RunEditor = func(_ string, path string) error { return os.WriteFile(path, []byte("PROMPT"), 0o600) } + editor.RunEditor = func(_ context.Context, _ string, path string) error { + return os.WriteFile(path, []byte("PROMPT"), 0o600) + } t.Cleanup(func() { editor.RunEditor = oldRun }) t.Setenv("HEXAI_EDITOR", "dummy") @@ -50,7 +52,7 @@ func TestRun_WithArgs_DoesNotOpenEditor(t *testing.T) { // Stub editor and detect if called (should not be) called := false oldRun := editor.RunEditor - editor.RunEditor = func(_ string, _ string) error { called = true; return nil } + editor.RunEditor = func(_ context.Context, _ string, _ string) error { called = true; return nil } t.Cleanup(func() { editor.RunEditor = oldRun }) var stdout, stderr bytes.Buffer if err := Run(context.Background(), []string{"ARG"}, bytes.NewBufferString("SEL"), &stdout, &stderr); err != nil { diff --git a/internal/hexaicli/run_editor_behavior_test.go b/internal/hexaicli/run_editor_behavior_test.go index a934473..99a2f2d 100644 --- a/internal/hexaicli/run_editor_behavior_test.go +++ b/internal/hexaicli/run_editor_behavior_test.go @@ -25,7 +25,7 @@ func TestRun_DoesNotOpenEditorWhenStdinPresent(t *testing.T) { // Guard: make editor invocation fatal if called oldRunEd := editor.RunEditor defer func() { editor.RunEditor = oldRunEd }() - editor.RunEditor = func(_ string, _ string) error { + editor.RunEditor = func(_ context.Context, _ string, _ string) error { t.Fatalf("editor should not be invoked when stdin has content") return nil } diff --git a/internal/hexaicli/runner.go b/internal/hexaicli/runner.go index eaae1cd..3929001 100644 --- a/internal/hexaicli/runner.go +++ b/internal/hexaicli/runner.go @@ -18,7 +18,7 @@ import ( type cliConfigLoader func(context.Context, *log.Logger) appconfig.App -type cliEditorOpener func([]byte) (string, error) +type cliEditorOpener func(context.Context, []byte) (string, error) type cliClientFactory func(appconfig.App) (llm.Client, error) @@ -85,7 +85,7 @@ func (r *Runner) Run(ctx context.Context, args []string, stdin io.Reader, stdout } cfgPath = p } - if err := editor.OpenFile(cfgPath); err != nil { + if err := editor.OpenFile(ctx, cfgPath); err != nil { _, _ = fmt.Fprintf(stderr, logging.AnsiBase+"hexai %s: %v"+logging.AnsiReset+"\n", sub, err) return err } @@ -127,7 +127,7 @@ func (r *Runner) Run(ctx context.Context, args []string, stdin io.Reader, stdout input, rerr := readInput(stdin, args) if rerr != nil && len(args) == 0 { - if prompt, eerr := runner.openEditor(nil); eerr == nil && strings.TrimSpace(prompt) != "" { + if prompt, eerr := runner.openEditor(ctx, nil); eerr == nil && strings.TrimSpace(prompt) != "" { args = []string{prompt} input, rerr = readInput(stdin, args) } @@ -182,5 +182,5 @@ func normalizeRunner(r *Runner) Runner { } func loadConfigFromContext(ctx context.Context, logger *log.Logger) appconfig.App { - return appconfig.LoadWithOptions(logger, appconfig.LoadOptions{ConfigPath: configPathFromContext(ctx)}) + return appconfig.LoadWithOptions(ctx, logger, appconfig.LoadOptions{ConfigPath: configPathFromContext(ctx)}) } diff --git a/internal/hexaicli/runner_test.go b/internal/hexaicli/runner_test.go index 009af54..8b52b89 100644 --- a/internal/hexaicli/runner_test.go +++ b/internal/hexaicli/runner_test.go @@ -40,7 +40,7 @@ func TestRunner_UsesInjectedDependencies(t *testing.T) { PromptConfig: appconfig.PromptConfig{PromptCLIDefaultSystem: "SYS"}, } }, - openEditor: func([]byte) (string, error) { return "PROMPT", nil }, + openEditor: func(context.Context, []byte) (string, error) { return "PROMPT", nil }, newClient: func(appconfig.App) (client llm.Client, err error) { return &fakeClient{name: "fake", model: "m", resp: "OUT"}, nil }, @@ -67,7 +67,7 @@ func TestRunner_ConfigSubcommand_OpensConfigFromContext(t *testing.T) { t.Cleanup(func() { editor.RunEditor = old }) t.Setenv("EDITOR", "true") var gotPath string - editor.RunEditor = func(_, path string) error { + editor.RunEditor = func(_ context.Context, _, path string) error { gotPath = path return nil } @@ -89,7 +89,7 @@ func TestRunner_ConfigSubcommand_UsesXDGWhenNoOverride(t *testing.T) { xdg := t.TempDir() t.Setenv("XDG_CONFIG_HOME", xdg) var gotPath string - editor.RunEditor = func(_, path string) error { + editor.RunEditor = func(_ context.Context, _, path string) error { gotPath = path return nil } |
