147 lines
4.4 KiB
Go
147 lines
4.4 KiB
Go
package stage
|
|
|
|
import (
|
|
"encoding/json"
|
|
"git.a-wan.net/alan/pipelines/internal/opencode"
|
|
"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"`
|
|
Transport string `json:"transport,omitempty"`
|
|
NativeOperation string `json:"native_operation,omitempty"`
|
|
ExitKind string `json:"exit_kind,omitempty"`
|
|
}
|
|
|
|
type OperationLog struct {
|
|
Provider, Model string
|
|
Failures []OperationEvidence
|
|
FormatChecks []opencode.FormatProbe
|
|
}
|
|
|
|
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
|
|
decoder := json.NewDecoder(strings.NewReader(text))
|
|
decoder.UseNumber()
|
|
if json.Valid([]byte(text)) && decoder.Decode(&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
|
|
if e.Backend == "v1" {
|
|
e.Transport = "native_http"
|
|
e.ExitKind = "not a subprocess exit"
|
|
} else {
|
|
e.Transport = "cli_subprocess"
|
|
e.ExitKind = "subprocess exit code"
|
|
}
|
|
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 && len(l.FormatChecks) == 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
|
|
}
|
|
}
|
|
if len(l.Failures) > 0 {
|
|
data["opencode_failures"] = l.Failures
|
|
}
|
|
if len(l.FormatChecks) > 0 {
|
|
data["native_format_checks"] = l.FormatChecks
|
|
}
|
|
encoded, _ := json.Marshal(data)
|
|
return string(encoded)
|
|
}
|
|
|
|
func (l *OperationLog) recordFormatProbe(probe opencode.FormatProbe) {
|
|
if l == nil || len(l.FormatChecks) >= 4 {
|
|
return
|
|
}
|
|
probe.Response = RedactEvidence(probe.Response)
|
|
probe.Diagnostic = RedactEvidence(probe.Diagnostic)
|
|
l.FormatChecks = append(l.FormatChecks, probe)
|
|
}
|