Files
pipelines/internal/opencode/format.go
T
alan 7b72b8f20f stage: retry pre-admission harness reach failures and widen repair deadline
Treat a failure to reach the harness before any prompt or tool call is
admitted (connecting or creating the session) as a distinct retryable
reach failure, so a transport blip at the first stage no longer ends a
mutating run; keep post-admission readback/poll/agent failures retried
only on an explicit read-only stage and never replay admitted tool work.
Preserve the true cause when the context is already done (join ctx.Err
with the typed transport error) and report a sanitized unknown provider
error name/status instead of a blanket summary.

Make the isolated format-repair deadline proportional to the stage
timeout (floored, capped and clamped to remaining minus a margin) so a
slow-but-repairable result is recovered without ever extending the stage
deadline; the bounded ladder, value guard, deny-all formatter session,
cumulative usage accounting and no-tool-replay rule are unchanged.

Update docs/model-results.md, docs/pipeline-performance-review.md and the
packaged skill references; add stage, opencode and server fixtures.
2026-10-17 14:51:09 +01:00

120 lines
4.7 KiB
Go

package opencode
import (
"context"
"encoding/json"
"fmt"
"strings"
)
// FormatProbe is retained, deny-all and no-inference. Some native releases
// accept a format on POST but reject their own stored user message on GET.
type FormatProbe struct {
SessionID string `json:"session_id"`
Supported bool `json:"supported"`
Diagnostic string `json:"diagnostic,omitempty"`
Response string `json:"response,omitempty"`
}
func (c *Connection) supportsFormat(ctx context.Context, format json.RawMessage) (bool, error) {
if c.formatChecked {
return c.formatSupported, nil
}
created, err := c.Native(ctx, "post", "/session", map[string]any{"title": "Pipelines structured-format compatibility probe (no inference)", "permission": []map[string]string{{"permission": "*", "pattern": "*", "action": "deny"}}})
if err != nil {
return false, err
}
var session struct {
ID string `json:"id"`
}
if json.Unmarshal(created, &session) != nil || !sessionID.MatchString(session.ID) {
return false, fmt.Errorf("invalid structured-format probe session")
}
c.FormatProbe = &FormatProbe{SessionID: session.ID}
posted, err := c.Native(ctx, "post", "/session/"+session.ID+"/message", map[string]any{"noReply": true, "format": format, "parts": []map[string]string{{"type": "text", "text": "Compatibility probe only. No reply, inference or tool execution requested."}}})
if err != nil {
c.FormatProbe.Response = string(posted)
return false, err
}
data, err := c.Native(ctx, "get", "/session/"+session.ID+"/message", nil)
if err != nil {
// Downgrade only this specific readback incompatibility, never an auth,
// network or unrelated HTTP failure. Do not alter the stored probe.
c.FormatProbe.Response = string(data)
if !strings.Contains(err.Error(), "HTTP 400") || !strings.Contains(string(data), "Expected OutputFormat") {
return false, err
}
c.FormatProbe.Diagnostic = "native format readback incompatibility; explicit format omitted, strict local JSON validation remains enabled"
c.FormatProbe.Response = string(data)
c.formatChecked = true
return false, nil
}
var messages []json.RawMessage
if json.Unmarshal(data, &messages) != nil || len(messages) == 0 {
return false, fmt.Errorf("invalid structured-format probe readback")
}
c.formatChecked = true
c.formatSupported = true
c.FormatProbe.Supported = true
return true, nil
}
// ExecutionError exposes a safe classification, not arbitrary provider messages,
// prompts or credentials. Full redacted responses belong in failure evidence.
func ExecutionError(data json.RawMessage) error {
var failure struct {
Name string `json:"name"`
Tag string `json:"_tag"`
Data struct {
StatusCode int `json:"statusCode"`
} `json:"data"`
}
if json.Unmarshal(data, &failure) != nil {
return fmt.Errorf("OpenCode agent reported an execution error")
}
name := failure.Name
if name == "" {
name = failure.Tag
}
if name == "StructuredOutputError" {
return fmt.Errorf("StructuredOutputError: Model did not produce structured output; inspect retained response for tool-call markup or result-envelope mismatch")
}
safeNames := map[string]bool{"APIError": true, "ProviderAuthError": true, "ContextOverflowError": true, "MessageAbortedError": true, "OutputLengthError": true, "UnknownError": true, "ServiceUnavailableError": true}
if !safeNames[name] {
// An unrecognized provider error name is still reported, but only as a
// bounded, safe identifier plus any HTTP status. No provider body,
// message, prompt or credential is copied.
if safe := sanitizedExecutionName(name); safe != "" {
if failure.Data.StatusCode >= 400 && failure.Data.StatusCode <= 599 {
return fmt.Errorf("OpenCode agent reported an execution error: %s (HTTP %d)", safe, failure.Data.StatusCode)
}
return fmt.Errorf("OpenCode agent reported an execution error: %s", safe)
}
return fmt.Errorf("OpenCode agent reported an execution error")
}
if failure.Data.StatusCode >= 400 && failure.Data.StatusCode <= 599 {
return fmt.Errorf("OpenCode agent execution error: %s (HTTP %d)", name, failure.Data.StatusCode)
}
return fmt.Errorf("OpenCode agent execution error: %s", name)
}
// sanitizedExecutionName reduces an unknown provider error name/tag to a
// bounded, safe identifier so the diagnostic can name the failure shape without
// copying an arbitrary message, prompt or credential. A name containing any
// other byte returns empty and is reported as a generic execution error.
func sanitizedExecutionName(name string) string {
if len(name) > 64 {
name = name[:64]
}
var b strings.Builder
for _, r := range name {
switch {
case r >= 'a' && r <= 'z', r >= 'A' && r <= 'Z', r >= '0' && r <= '9', r == '_', r == '.', r == '-':
b.WriteRune(r)
default:
return ""
}
}
return b.String()
}