Validate providers and retain complete run debugging context

This commit is contained in:
2026-10-09 23:21:01 +01:00
parent f3d829bea5
commit 08b8647d5b
51 changed files with 1036 additions and 159 deletions
+16 -4
View File
@@ -27,6 +27,7 @@ type OpenCodeClient struct {
Backend string
Reference string
FormatAudit string
Operations *OperationLog
OnGenerationSession func(id, backend, reference string, active bool) error
connection *opencode.Connection
command func(context.Context, string, ...string) ([]byte, error)
@@ -60,12 +61,14 @@ var httpStatusPattern = regexp.MustCompile(`HTTP (\d{3})`)
func (c OpenCodeClient) call(ctx context.Context, method, path string, body any) ([]byte, error) {
if c.connection != nil && c.connection.Backend == "v1" {
started := time.Now()
data, err := c.connection.Call(ctx, method, path, body)
if err != nil {
status := 0
if match := httpStatusPattern.FindStringSubmatch(err.Error()); len(match) > 0 {
status, _ = strconv.Atoi(match[1])
}
c.Operations.record(OperationEvidence{Operation: method + " " + path, Backend: c.Backend, Executable: opencode.Executable(), Directory: c.Directory, ExitCode: -1, HTTPStatus: status, Diagnostic: err.Error(), Stdout: string(data)}, started)
if ctx.Err() != nil {
return nil, ctx.Err()
}
@@ -101,14 +104,20 @@ func (c OpenCodeClient) call(ctx context.Context, method, path string, body any)
stderr := &boundedBuffer{limit: 8 << 10}
cmd.Stdout = stdout
cmd.Stderr = stderr
started := time.Now()
if err := cmd.Run(); err != nil {
if ctx.Err() != nil {
return nil, ctx.Err()
}
status := 0
status, code := 0, -1
if match := httpStatusPattern.FindStringSubmatch(stderr.String()); len(match) > 0 {
status, _ = strconv.Atoi(match[1])
}
var exit *exec.ExitError
if errors.As(err, &exit) {
code = exit.ExitCode()
}
c.Operations.record(OperationEvidence{Operation: method + " " + path, Backend: c.Backend, Executable: cmd.Path, Directory: directory, ExitCode: code, HTTPStatus: status, Diagnostic: safeCommandDiagnostic(err, stdout.Bytes()), Stdout: stdout.String(), Stderr: stderr.String()}, started)
if ctx.Err() != nil {
return nil, ctx.Err()
}
// Do not embed provider responses, prompts, credentials, or CLI logs in errors.
return nil, &openCodeError{operation: method + " " + path, status: status, diagnostic: safeCommandDiagnostic(err, stdout.Bytes())}
}
@@ -138,6 +147,9 @@ func safeCommandDiagnostic(err error, data []byte) string {
}
func (c OpenCodeClient) Generate(ctx context.Context, config ModelConfig, prompt string) (text string, err error) {
if c.Operations != nil {
c.Operations.Provider, c.Operations.Model = config.Provider, config.Model
}
if c.command == nil {
connection, err := opencode.Open(ctx, opencode.Executable(), c.Directory, c.Backend)
if err != nil {