Harden model results with lossless isolated repair and scoped permissions

This commit is contained in:
2026-10-09 20:02:41 +01:00
parent 0f80ff4e95
commit 67ac79765f
23 changed files with 977 additions and 58 deletions
+5
View File
@@ -52,6 +52,11 @@ func TestStableNativeSessionContractLive(t *testing.T) {
if len(session.Permission) != 3 || session.Permission[2]["permission"] != "bash" {
t.Fatal("scoped native permission contract not retained", string(data))
}
// Validate the real native JSON-schema request without provider inference.
_, err = c.Native(ctx, "post", "/session/"+result.Data.ID+"/message", map[string]any{"noReply": true, "format": map[string]any{"type": "json_schema", "retryCount": 0, "schema": map[string]any{"type": "object", "required": []string{"outcome", "output"}, "additionalProperties": false, "properties": map[string]any{"outcome": map[string]any{"type": "string", "enum": []string{"success", "error"}}, "output": map[string]any{"type": "object"}}}}, "parts": []map[string]string{{"type": "text", "text": "No inference; validate structured format."}}})
if err != nil {
t.Fatal("native schema request rejected", err)
}
ref, err := c.Reference()
if err != nil {
t.Fatal(err)
+43 -18
View File
@@ -4,9 +4,9 @@ import (
"context"
"encoding/json"
"fmt"
"path/filepath"
"regexp"
"strings"
"path/filepath"
)
var sessionID = regexp.MustCompile(`^ses[A-Za-z0-9_-]+$`)
@@ -26,21 +26,28 @@ func Permissions(rules []map[string]string) []map[string]string {
func messageText(data []byte) (string, error) {
var message struct {
Info struct {
Role string `json:"role"`
Finish string `json:"finish"`
Error json.RawMessage `json:"error"`
Time struct {
Role string `json:"role"`
Finish string `json:"finish"`
Error json.RawMessage `json:"error"`
Structured json.RawMessage `json:"structured"`
Time struct {
Completed int64 `json:"completed"`
} `json:"time"`
} `json:"info"`
Parts []struct{ Type, Text string } `json:"parts"`
}
if json.Unmarshal(data, &message) != nil || message.Info.Role != "assistant" || message.Info.Finish != "stop" || message.Info.Time.Completed == 0 {
if json.Unmarshal(data, &message) != nil || message.Info.Role != "assistant" || message.Info.Time.Completed == 0 {
return "", fmt.Errorf("OpenCode returned no completed assistant result")
}
if len(message.Info.Error) > 0 && string(message.Info.Error) != "null" {
return "", fmt.Errorf("OpenCode agent reported an execution error")
}
if len(message.Info.Structured) > 0 && string(message.Info.Structured) != "null" && (message.Info.Finish == "stop" || message.Info.Finish == "tool-calls") {
return string(message.Info.Structured), nil
}
if message.Info.Finish != "stop" {
return "", fmt.Errorf("OpenCode returned no completed assistant result")
}
var text string
for _, part := range message.Parts {
if part.Type == "text" {
@@ -73,11 +80,16 @@ func (c *Connection) Call(ctx context.Context, method, path string, body any) ([
if rules == nil {
rules = []map[string]string{{"action": "*", "resource": "*", "effect": "deny"}}
}
nativeRules:=Permissions(rules)
nativeRules := Permissions(rules)
// V1 tool implementations can match absolute file paths; preserve the exact
// approved scope in both forms, never grant a workspace-wide edit wildcard.
var scoped []map[string]string
for _,rule:=range nativeRules{scoped=append(scoped,rule);if rule["permission"]=="edit"&&rule["action"]=="allow"&&!filepath.IsAbs(rule["pattern"]){scoped=append(scoped,map[string]string{"permission":"edit","action":"allow","pattern":filepath.Join(c.Directory,rule["pattern"])})}}
for _, rule := range nativeRules {
scoped = append(scoped, rule)
if rule["permission"] == "edit" && rule["action"] == "allow" && !filepath.IsAbs(rule["pattern"]) {
scoped = append(scoped, map[string]string{"permission": "edit", "action": "allow", "pattern": filepath.Join(c.Directory, rule["pattern"])})
}
}
payload := map[string]any{"title": request.Title, "permission": scoped, "model": request.Model}
if request.Agent != "" {
payload["agent"] = request.Agent
@@ -110,7 +122,7 @@ func (c *Connection) Call(ctx context.Context, method, path string, body any) ([
return nil, err
}
var confirmed bool
if json.Unmarshal(data,&confirmed)!=nil||!confirmed {
if json.Unmarshal(data, &confirmed) != nil || !confirmed {
return nil, fmt.Errorf("OpenCode session deletion was not confirmed")
}
delete(c.models, id)
@@ -127,21 +139,29 @@ func (c *Connection) Call(ctx context.Context, method, path string, body any) ([
return nil, err
}
var confirmed bool
if json.Unmarshal(data,&confirmed)!=nil||!confirmed {
if json.Unmarshal(data, &confirmed) != nil || !confirmed {
return nil, fmt.Errorf("OpenCode abort was not confirmed")
}
return nil, nil
case "generate", "prompt":
data, _ := json.Marshal(body)
var request map[string]string
var request struct {
Prompt string `json:"prompt"`
Text string `json:"text"`
Format json.RawMessage `json:"format"`
}
if err := json.Unmarshal(data, &request); err != nil {
return nil, err
}
text := request["prompt"]
text := request.Prompt
if text == "" {
text = request["text"]
text = request.Text
}
response, err := c.Native(ctx, "post", "/session/"+id+"/message", map[string]any{"model": c.models[id], "parts": []map[string]string{{"type": "text", "text": text}}})
payload := map[string]any{"model": c.models[id], "parts": []map[string]string{{"type": "text", "text": text}}}
if len(request.Format) > 0 {
payload["format"] = request.Format
}
response, err := c.Native(ctx, "post", "/session/"+id+"/message", payload)
if err != nil {
return nil, err
}
@@ -163,8 +183,8 @@ func (c *Connection) Call(ctx context.Context, method, path string, body any) ([
return nil, err
}
var messages []struct {
Info map[string]any `json:"info"`
Parts []map[string]any `json:"parts"`
Info json.RawMessage `json:"info"`
Parts json.RawMessage `json:"parts"`
}
if json.Unmarshal(data, &messages) != nil {
return nil, fmt.Errorf("invalid native messages")
@@ -172,10 +192,15 @@ func (c *Connection) Call(ctx context.Context, method, path string, body any) ([
var converted []map[string]any
for i := len(messages) - 1; i >= 0; i-- {
m := messages[i]
if m.Info["role"] != "assistant" {
var info struct {
Role, Finish string
Error, Structured json.RawMessage
Time struct{ Completed int64 }
}
if json.Unmarshal(m.Info, &info) != nil || info.Role != "assistant" {
continue
}
converted = append(converted, map[string]any{"finish": m.Info["finish"], "error": m.Info["error"], "content": m.Parts, "native_info": m.Info})
converted = append(converted, map[string]any{"finish": info.Finish, "completed": info.Time.Completed > 0, "error": info.Error, "structured": info.Structured, "content": m.Parts, "native_info": m.Info})
}
return json.Marshal(map[string]any{"data": converted})
}
+7 -3
View File
@@ -30,13 +30,17 @@ func TestNativeSessionModelPermissionsGenerationAndAbort(t *testing.T) {
w.Write([]byte(`{"id":"ses_native"}`))
case "POST /session/ses_native/message":
var body struct {
Model map[string]string
Parts []map[string]string
Model map[string]string
Parts []map[string]string
Format map[string]any
}
json.NewDecoder(r.Body).Decode(&body)
if body.Model["modelID"] != "model" || body.Parts[0]["text"] != "exact prompt" {
t.Error("prompt or message model changed", body)
}
if body.Format["type"] != "json_schema" || body.Format["retryCount"] != float64(0) {
t.Error("structured-output format or no-replay retry count lost", body.Format)
}
prompted = true
w.Write([]byte(`{"info":{"role":"assistant","finish":"stop","time":{"completed":1}},"parts":[{"type":"text","text":"result"}]}`))
case "GET /session/ses_native/message":
@@ -58,7 +62,7 @@ func TestNativeSessionModelPermissionsGenerationAndAbort(t *testing.T) {
if err != nil {
t.Fatal(err)
}
data, err := c.Call(ctx, "post", "/api/session/ses_native/generate", map[string]string{"prompt": "exact prompt"})
data, err := c.Call(ctx, "post", "/api/session/ses_native/generate", map[string]any{"prompt": "exact prompt", "format": map[string]any{"type": "json_schema", "schema": map[string]any{"type": "object"}, "retryCount": 0}})
if err != nil || !strings.Contains(string(data), "result") {
t.Fatal(string(data), err)
}
+47
View File
@@ -0,0 +1,47 @@
package opencode
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestNativeStructuredResultIsPreferredOverPresentationText(t *testing.T) {
data := []byte(`{"info":{"role":"assistant","finish":"stop","time":{"completed":1},"structured":{"outcome":"success","output":{"approval":true}}},"parts":[{"type":"text","text":"review complete"}]}`)
text, err := messageText(data)
if err != nil {
t.Fatal(err)
}
var result map[string]any
if err := json.Unmarshal([]byte(text), &result); err != nil || result["outcome"] != "success" {
t.Fatal("structured result ignored", text, err)
}
if _, err := messageText([]byte(strings.Replace(string(data), `"finish":"stop"`, `"finish":"tool-calls"`, 1))); err != nil {
t.Fatal("completed schema tool result rejected", err)
}
}
func TestStructuredAgentResponseKeepsOriginalToolEvidence(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(`[{"info":{"role":"assistant","finish":"stop","time":{"completed":1},"structured":{"outcome":"success","output":{"approval":true}}},"parts":[{"type":"tool","tool":"bash","state":{"status":"completed","input":{"command":"bash -n install.sh"},"output":"passed"}},{"type":"text","text":"review complete"}]}]`))
}))
defer srv.Close()
c := &Connection{Backend: "v1", URL: srv.URL, Directory: "/repo"}
data, err := c.Call(context.Background(), "get", "/api/session/ses_test/message?order=desc", nil)
if err != nil {
t.Fatal(err)
}
var result struct {
Data []struct {
Structured map[string]any
Content []map[string]any
}
}
if err := json.Unmarshal(data, &result); err != nil || len(result.Data) != 1 || result.Data[0].Structured["outcome"] != "success" || len(result.Data[0].Content) != 2 || result.Data[0].Content[0]["type"] != "tool" {
t.Fatal("structured output erased original tool evidence", string(data), err)
}
}