Improve listener failures and share configuration diagnostics
This commit is contained in:
@@ -250,6 +250,9 @@ swapped cache fields are detectable, and production imports no longer reference
|
|||||||
|
|
||||||
## Stage 7: Improve Listener Explanations And Simplify CLI Diagnostics
|
## Stage 7: Improve Listener Explanations And Simplify CLI Diagnostics
|
||||||
|
|
||||||
|
**Status: complete.** Listener failures identify the selected address and safe
|
||||||
|
error category, and run/serve share application-configuration diagnostics.
|
||||||
|
|
||||||
**Findings:** the local portion of 8, and 15.
|
**Findings:** the local portion of 8, and 15.
|
||||||
**Files:** `internal/adapter/cli/server.go`, `native_run.go`, `outcomes.go`, and
|
**Files:** `internal/adapter/cli/server.go`, `native_run.go`, `outcomes.go`, and
|
||||||
focused lifecycle/diagnostic tests.
|
focused lifecycle/diagnostic tests.
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
|
"syscall"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -358,6 +359,9 @@ func TestServeListenAndConfigurationFailures(t *testing.T) {
|
|||||||
if outcome.Status != 1 || outcome.Cause == nil || output != "" || diagnostics == "" {
|
if outcome.Status != 1 || outcome.Cause == nil || output != "" || diagnostics == "" {
|
||||||
t.Fatalf("serve failure: %+v %q %q", outcome, output, diagnostics)
|
t.Fatalf("serve failure: %+v %q %q", outcome, output, diagnostics)
|
||||||
}
|
}
|
||||||
|
if strings.Contains(args[len(args)-1], "invalid:address:syntax") && (!strings.Contains(diagnostics, "invalid:address:syntax") || !strings.Contains(diagnostics, "address")) {
|
||||||
|
t.Fatalf("missing listener context: %q", diagnostics)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
writeNativeFile(t, "invalid.yml", "server:\n max_request_bytes: -1\n")
|
writeNativeFile(t, "invalid.yml", "server:\n max_request_bytes: -1\n")
|
||||||
outcome, _, diagnostics := executeNative(t, "serve", "--config=invalid.yml")
|
outcome, _, diagnostics := executeNative(t, "serve", "--config=invalid.yml")
|
||||||
@@ -379,6 +383,35 @@ func TestServeListenAndConfigurationFailures(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestServeListenerFailureDetails(t *testing.T) {
|
||||||
|
nativePromptFixture(t, "http://127.0.0.1:1/v1")
|
||||||
|
for _, tc := range []struct {
|
||||||
|
name, category string
|
||||||
|
cause error
|
||||||
|
}{
|
||||||
|
{"address", "address", &net.AddrError{Err: "private detail", Addr: "bad"}},
|
||||||
|
{"dns", "address", &net.DNSError{Err: "private detail", Name: "bad"}},
|
||||||
|
{"in use", "in use", fmt.Errorf("private detail: %w", syscall.EADDRINUSE)},
|
||||||
|
{"permission", "permission", fmt.Errorf("private detail: %w", syscall.EACCES)},
|
||||||
|
{"other", "listener setup", errors.New("private detail")},
|
||||||
|
} {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
listen := func(context.Context, string, string) (net.Listener, error) { return nil, tc.cause }
|
||||||
|
app, err := commandkit.New(commandkit.Identity{Name: "scriptorium"}, commandkit.NewCommand(commandkit.CommandSpec[serveInput]{Name: "serve", Flags: serveFlags(), Run: func(inv commandkit.Invocation, input serveInput) commandkit.Outcome {
|
||||||
|
return serveWithListener(inv, input, listen)
|
||||||
|
}}))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
var output, diagnostics bytes.Buffer
|
||||||
|
outcome := app.Execute(context.Background(), []string{"serve", "--config=app.yml", "--addr=127.0.0.1:1234", "--quiet"}, commandkit.Streams{Out: &output, Err: &diagnostics})
|
||||||
|
if outcome.Status != 1 || !errors.Is(outcome.Cause, tc.cause) || output.Len() != 0 || !strings.Contains(diagnostics.String(), "127.0.0.1:1234") || !strings.Contains(diagnostics.String(), tc.category) || strings.Contains(diagnostics.String(), "private detail") {
|
||||||
|
t.Fatalf("listener failure: %+v output=%q diagnostics=%q", outcome, output.String(), diagnostics.String())
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestServeCaptureFailureDoesNotListen(t *testing.T) {
|
func TestServeCaptureFailureDoesNotListen(t *testing.T) {
|
||||||
nativePromptFixture(t, "http://127.0.0.1:1/v1")
|
nativePromptFixture(t, "http://127.0.0.1:1/v1")
|
||||||
writeNativeFile(t, "framework.yml", `prompts:
|
writeNativeFile(t, "framework.yml", `prompts:
|
||||||
|
|||||||
@@ -1,14 +1,12 @@
|
|||||||
package cli
|
package cli
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"text/template"
|
"text/template"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"go.maximumdirect.net/ckconfig"
|
"go.maximumdirect.net/ckconfig"
|
||||||
"go.maximumdirect.net/ckprompt"
|
"go.maximumdirect.net/ckprompt"
|
||||||
"go.maximumdirect.net/commandkit"
|
"go.maximumdirect.net/commandkit"
|
||||||
"go.maximumdirect.net/configkit"
|
|
||||||
"go.maximumdirect.net/promptkit"
|
"go.maximumdirect.net/promptkit"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -75,17 +73,7 @@ func requestFromRunInput(input runInput) promptkit.RunRequest {
|
|||||||
|
|
||||||
func runPrompt(invocation commandkit.Invocation, input runInput) commandkit.Outcome {
|
func runPrompt(invocation commandkit.Invocation, input runInput) commandkit.Outcome {
|
||||||
result, err := loadApplication(invocation.Context, input.Selection, applicationInput{}, struct{}{})
|
result, err := loadApplication(invocation.Context, input.Selection, applicationInput{}, struct{}{})
|
||||||
var sources []configkit.SourceInfo
|
explanation := applicationConfigurationDiagnostics(invocation.Reporter(), result, err)
|
||||||
var records []configkit.Diagnostic
|
|
||||||
if result != nil {
|
|
||||||
sources, records = result.Sources(), result.Diagnostics()
|
|
||||||
}
|
|
||||||
var native *configkit.LoadError
|
|
||||||
if errors.As(err, &native) && native != nil {
|
|
||||||
sources, records = native.Sources(), native.Diagnostics()
|
|
||||||
}
|
|
||||||
diagnostics, explanation := ckconfig.AdaptDiagnostics("Application configuration:\n", sources, records)
|
|
||||||
queueDiagnostics(invocation.Reporter(), diagnostics)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return requestFailure(err, explanation)
|
return requestFailure(err, explanation)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,12 +4,28 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
|
appconfig "gitea.maximumdirect.net/eric/scriptorium/internal/config"
|
||||||
"go.maximumdirect.net/ckconfig"
|
"go.maximumdirect.net/ckconfig"
|
||||||
"go.maximumdirect.net/commandkit"
|
"go.maximumdirect.net/commandkit"
|
||||||
"go.maximumdirect.net/configkit"
|
"go.maximumdirect.net/configkit"
|
||||||
"go.maximumdirect.net/promptkit"
|
"go.maximumdirect.net/promptkit"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func applicationConfigurationDiagnostics(reporter *commandkit.Reporter, result *configkit.Result[appconfig.Document], err error) commandkit.Explanation {
|
||||||
|
var sources []configkit.SourceInfo
|
||||||
|
var records []configkit.Diagnostic
|
||||||
|
if result != nil {
|
||||||
|
sources, records = result.Sources(), result.Diagnostics()
|
||||||
|
}
|
||||||
|
var native *configkit.LoadError
|
||||||
|
if errors.As(err, &native) && native != nil {
|
||||||
|
sources, records = native.Sources(), native.Diagnostics()
|
||||||
|
}
|
||||||
|
diagnostics, explanation := ckconfig.AdaptDiagnostics("Application configuration:\n", sources, records)
|
||||||
|
queueDiagnostics(reporter, diagnostics)
|
||||||
|
return explanation
|
||||||
|
}
|
||||||
|
|
||||||
func completedRunOutcome(result *promptkit.RunResult) commandkit.Outcome {
|
func completedRunOutcome(result *promptkit.RunResult) commandkit.Outcome {
|
||||||
if result == nil {
|
if result == nil {
|
||||||
return commandkit.Failure("Prompt execution returned no result.").WithCause(errors.New("engine returned neither a result nor an error"))
|
return commandkit.Failure("Prompt execution returned no result.").WithCause(errors.New("engine returned neither a result nor an error"))
|
||||||
@@ -39,30 +55,26 @@ func requestFailure(err error, explanation commandkit.Explanation) commandkit.Ou
|
|||||||
message = "The operation was canceled."
|
message = "The operation was canceled."
|
||||||
case errors.Is(err, context.DeadlineExceeded):
|
case errors.Is(err, context.DeadlineExceeded):
|
||||||
message = "A request deadline expired."
|
message = "A request deadline expired."
|
||||||
default:
|
case ckconfig.ClassifyConfigurationError(err) == commandkit.OutcomeUsage:
|
||||||
if ckconfig.ClassifyConfigurationError(err) == commandkit.OutcomeUsage {
|
return commandkit.Usage("Invalid configuration.", explanation.Details...).WithCause(err)
|
||||||
return commandkit.Usage("Invalid configuration.", explanation.Details...).WithCause(err)
|
case errors.Is(err, promptkit.ErrCapacityExceeded):
|
||||||
}
|
message = "Model backend capacity is exhausted."
|
||||||
switch {
|
case errors.Is(err, promptkit.ErrLLMGenerate):
|
||||||
case errors.Is(err, promptkit.ErrCapacityExceeded):
|
message = "Model generation failed."
|
||||||
message = "Model backend capacity is exhausted."
|
case errors.Is(err, promptkit.ErrValidation):
|
||||||
case errors.Is(err, promptkit.ErrLLMGenerate):
|
message = "The output validation operation failed."
|
||||||
message = "Model generation failed."
|
case errors.Is(err, promptkit.ErrPromptNotFound):
|
||||||
case errors.Is(err, promptkit.ErrValidation):
|
message = "The selected prompt was not found."
|
||||||
message = "The output validation operation failed."
|
case errors.Is(err, promptkit.ErrProfileNotFound):
|
||||||
case errors.Is(err, promptkit.ErrPromptNotFound):
|
message = "The selected profile was not found."
|
||||||
message = "The selected prompt was not found."
|
case errors.Is(err, promptkit.ErrArtifactLoad):
|
||||||
case errors.Is(err, promptkit.ErrProfileNotFound):
|
message = "Could not load request input artifacts."
|
||||||
message = "The selected profile was not found."
|
case errors.Is(err, promptkit.ErrPromptRender):
|
||||||
case errors.Is(err, promptkit.ErrArtifactLoad):
|
message = "Could not render the selected prompt."
|
||||||
message = "Could not load request input artifacts."
|
case errors.Is(err, promptkit.ErrInvalidRequest):
|
||||||
case errors.Is(err, promptkit.ErrPromptRender):
|
message = "The prompt request is invalid."
|
||||||
message = "Could not render the selected prompt."
|
case errors.Is(err, promptkit.ErrInvalidConfig):
|
||||||
case errors.Is(err, promptkit.ErrInvalidRequest):
|
message = "Could not initialize PromptKit configuration."
|
||||||
message = "The prompt request is invalid."
|
|
||||||
case errors.Is(err, promptkit.ErrInvalidConfig):
|
|
||||||
message = "Could not initialize PromptKit configuration."
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return commandkit.Failure(message, explanation.Details...).WithCause(err)
|
return commandkit.Failure(message, explanation.Details...).WithCause(err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,12 +6,11 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"syscall"
|
||||||
|
|
||||||
httpadapter "gitea.maximumdirect.net/eric/scriptorium/internal/adapter/http"
|
httpadapter "gitea.maximumdirect.net/eric/scriptorium/internal/adapter/http"
|
||||||
"gitea.maximumdirect.net/eric/scriptorium/internal/defaults"
|
"gitea.maximumdirect.net/eric/scriptorium/internal/defaults"
|
||||||
"go.maximumdirect.net/ckconfig"
|
|
||||||
"go.maximumdirect.net/commandkit"
|
"go.maximumdirect.net/commandkit"
|
||||||
"go.maximumdirect.net/configkit"
|
|
||||||
"go.maximumdirect.net/promptkit"
|
"go.maximumdirect.net/promptkit"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -21,17 +20,7 @@ func serveHTTP(invocation commandkit.Invocation, input serveInput) commandkit.Ou
|
|||||||
|
|
||||||
func serveWithListener(invocation commandkit.Invocation, input serveInput, listen func(context.Context, string, string) (net.Listener, error)) commandkit.Outcome {
|
func serveWithListener(invocation commandkit.Invocation, input serveInput, listen func(context.Context, string, string) (net.Listener, error)) commandkit.Outcome {
|
||||||
result, err := loadApplication(invocation.Context, input.Selection, input.Application, struct{}{})
|
result, err := loadApplication(invocation.Context, input.Selection, input.Application, struct{}{})
|
||||||
var sources []configkit.SourceInfo
|
explanation := applicationConfigurationDiagnostics(invocation.Reporter(), result, err)
|
||||||
var records []configkit.Diagnostic
|
|
||||||
if result != nil {
|
|
||||||
sources, records = result.Sources(), result.Diagnostics()
|
|
||||||
}
|
|
||||||
var native *configkit.LoadError
|
|
||||||
if errors.As(err, &native) && native != nil {
|
|
||||||
sources, records = native.Sources(), native.Diagnostics()
|
|
||||||
}
|
|
||||||
diagnostics, explanation := ckconfig.AdaptDiagnostics("Application configuration:\n", sources, records)
|
|
||||||
queueDiagnostics(invocation.Reporter(), diagnostics)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return requestFailure(err, explanation)
|
return requestFailure(err, explanation)
|
||||||
}
|
}
|
||||||
@@ -51,7 +40,7 @@ func serveWithListener(invocation commandkit.Invocation, input serveInput, liste
|
|||||||
}
|
}
|
||||||
listener, err := listen(invocation.Context, "tcp", settings.Addr)
|
listener, err := listen(invocation.Context, "tcp", settings.Addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return commandkit.Failure("Could not listen for HTTP requests.").WithCause(err)
|
return commandkit.Failure(fmt.Sprintf("Could not listen on %q: %s.", settings.Addr, listenerFailureCategory(err))).WithCause(err)
|
||||||
}
|
}
|
||||||
_ = invocation.Reporter().Info(fmt.Sprintf("Serving HTTP on %s.", listener.Addr()))
|
_ = invocation.Reporter().Info(fmt.Sprintf("Serving HTTP on %s.", listener.Addr()))
|
||||||
closed := make(chan struct{})
|
closed := make(chan struct{})
|
||||||
@@ -67,3 +56,18 @@ func serveWithListener(invocation commandkit.Invocation, input serveInput, liste
|
|||||||
}
|
}
|
||||||
return commandkit.Failure("HTTP server stopped unexpectedly.").WithCause(errors.Join(serveErr, closeErr))
|
return commandkit.Failure("HTTP server stopped unexpectedly.").WithCause(errors.Join(serveErr, closeErr))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func listenerFailureCategory(err error) string {
|
||||||
|
var address *net.AddrError
|
||||||
|
var dns *net.DNSError
|
||||||
|
switch {
|
||||||
|
case errors.As(err, &address), errors.As(err, &dns):
|
||||||
|
return "invalid or unresolvable address"
|
||||||
|
case errors.Is(err, syscall.EADDRINUSE):
|
||||||
|
return "address already in use"
|
||||||
|
case errors.Is(err, syscall.EACCES), errors.Is(err, syscall.EPERM):
|
||||||
|
return "permission denied"
|
||||||
|
default:
|
||||||
|
return "listener setup failed"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user