Validate providers and retain complete run debugging context
This commit is contained in:
@@ -0,0 +1,118 @@
|
||||
package stage
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Failure evidence is bounded and credential-redacted; request bodies (which
|
||||
// contain prompts) and the subprocess environment are never recorded here.
|
||||
type OperationEvidence struct {
|
||||
Operation string `json:"operation"`
|
||||
Backend string `json:"backend"`
|
||||
Executable string `json:"executable"`
|
||||
Directory string `json:"directory"`
|
||||
Provider string `json:"provider"`
|
||||
Model string `json:"model"`
|
||||
StartedAt string `json:"started_at"`
|
||||
DurationMS int64 `json:"duration_ms"`
|
||||
ExitCode int `json:"exit_code"`
|
||||
HTTPStatus int `json:"http_status,omitempty"`
|
||||
Diagnostic string `json:"diagnostic"`
|
||||
Stdout string `json:"stdout,omitempty"`
|
||||
Stderr string `json:"stderr,omitempty"`
|
||||
}
|
||||
|
||||
type OperationLog struct {
|
||||
Provider, Model string
|
||||
Failures []OperationEvidence
|
||||
}
|
||||
|
||||
var sensitiveKey = regexp.MustCompile(`(?i)^(authorization|password|secret|api[-_]?key|access[-_]?token|refresh[-_]?token|token|cookie|set-cookie|client[-_]?secret)$`)
|
||||
var credentialText = regexp.MustCompile(`(?i)(bearer\s+)[^\s"'<>]+|\b(?:sk-[a-zA-Z0-9_-]+|tk_[a-zA-Z0-9]{29})\b|((?:api[-_]?key|access[-_]?token|refresh[-_]?token|password|client[-_]?secret|authorization|token)\s*[=:]\s*["']?)[^\s"'&,<>]+`)
|
||||
var authorizationLine = regexp.MustCompile(`(?im)(authorization\s*[:=]\s*)[^\r\n]+`)
|
||||
var credentialURL = regexp.MustCompile(`(https?://)[^/\s:@]+:[^/\s@]+@`)
|
||||
|
||||
func RedactEvidence(text string) string {
|
||||
var data any
|
||||
if json.Unmarshal([]byte(text), &data) == nil {
|
||||
var clean func(any)
|
||||
clean = func(v any) {
|
||||
switch x := v.(type) {
|
||||
case map[string]any:
|
||||
for k, v := range x {
|
||||
if sensitiveKey.MatchString(k) {
|
||||
x[k] = "[REDACTED]"
|
||||
} else {
|
||||
clean(v)
|
||||
}
|
||||
}
|
||||
case []any:
|
||||
for _, v := range x {
|
||||
clean(v)
|
||||
}
|
||||
}
|
||||
}
|
||||
clean(data)
|
||||
encoded, _ := json.Marshal(data)
|
||||
text = string(encoded)
|
||||
}
|
||||
text = credentialURL.ReplaceAllString(text, "${1}[REDACTED]@")
|
||||
if !json.Valid([]byte(text)) {
|
||||
text = authorizationLine.ReplaceAllString(text, "${1}[REDACTED]")
|
||||
}
|
||||
text = credentialText.ReplaceAllStringFunc(text, func(match string) string {
|
||||
parts := credentialText.FindStringSubmatch(match)
|
||||
if parts[1] != "" {
|
||||
return parts[1] + "[REDACTED]"
|
||||
}
|
||||
if parts[2] != "" {
|
||||
return parts[2] + "[REDACTED]"
|
||||
}
|
||||
return "[REDACTED]"
|
||||
})
|
||||
// Keep terminal control sequences out of diagnostic presentations.
|
||||
text = strings.Map(func(r rune) rune {
|
||||
if r < 32 && r != '\n' && r != '\t' {
|
||||
return -1
|
||||
}
|
||||
return r
|
||||
}, text)
|
||||
if len(text) > 16<<10 {
|
||||
text = text[:16<<10] + "\n[truncated]"
|
||||
}
|
||||
return text
|
||||
}
|
||||
|
||||
func (l *OperationLog) record(e OperationEvidence, started time.Time) {
|
||||
if l == nil {
|
||||
return
|
||||
}
|
||||
e.Provider, e.Model = l.Provider, l.Model
|
||||
e.StartedAt = started.UTC().Format(time.RFC3339Nano)
|
||||
e.DurationMS = time.Since(started).Milliseconds()
|
||||
e.Diagnostic = RedactEvidence(e.Diagnostic)
|
||||
e.Stdout = RedactEvidence(e.Stdout)
|
||||
e.Stderr = RedactEvidence(e.Stderr)
|
||||
if len(l.Failures) < 32 {
|
||||
l.Failures = append(l.Failures, e)
|
||||
}
|
||||
}
|
||||
|
||||
func (l *OperationLog) Transcript(original string) string {
|
||||
if l == nil || len(l.Failures) == 0 {
|
||||
return original
|
||||
}
|
||||
var data map[string]any
|
||||
if json.Unmarshal([]byte(original), &data) != nil || data == nil {
|
||||
data = map[string]any{}
|
||||
if original != "" {
|
||||
data["original_transcript"] = original
|
||||
}
|
||||
}
|
||||
data["opencode_failures"] = l.Failures
|
||||
encoded, _ := json.Marshal(data)
|
||||
return string(encoded)
|
||||
}
|
||||
Reference in New Issue
Block a user