Harden model results with lossless isolated repair and scoped permissions
This commit is contained in:
+43
-18
@@ -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})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user