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
+18 -6
View File
@@ -19,6 +19,7 @@ type AgentClient struct {
OnSession func(string, bool) error
SessionID string
Transcript string
Policy ToolPolicy
}
func (c OpenCodeClient) Interrupt(ctx context.Context, id string) error {
@@ -119,10 +120,16 @@ func (c *AgentClient) Generate(ctx context.Context, config ModelConfig, prompt s
if len(c.WritePaths) > 0 {
editPaths = strings.Join(c.WritePaths, ", ")
}
if c.Policy.Edit != nil {
editPaths = "stage patterns: " + strings.Join(c.Policy.Edit, ", ") + "; run scope: " + editPaths
}
}
}
prompt += "\nRun directory: " + c.Directory + ". Edit scope: " + editPaths + ". There is no language, extension, or existing-file restriction. Shell commands are not limited to one language's verification commands. Select verification commands from the actual repository; do not assume a language or absent toolchain based on older generic pipeline boilerplate. Any earlier description of extension restrictions or required runtime file lists is obsolete; this scope describes the actual runtime. Do not invent a missing file-list or manual-approval requirement. Do not infer that later stages lack editing because this stage is read-only. Respect this stage's role and available tools. Work in the run directory; do not modify unrelated locations or Pipelines history/configuration. Do not delegate. Commit/push only when the task explicitly requests it. Final response must be only the stage result JSON."
if _, err = c.call(ctx, "post", "/api/session/"+c.SessionID+"/prompt", map[string]string{"text": prompt}); err != nil {
prompt += "\nRun directory: " + c.Directory + ". Edit scope: " + editPaths + ". There is no implicit language or extension restriction; explicit stage permissions and run scopes are authoritative. Select verification commands from the actual repository and permitted commands. Older generic language-specific boilerplate is not a runtime permission policy. Do not invent a missing file-list or manual-approval requirement. Do not infer that later stages lack editing because this stage is read-only. Respect this stage's role and available tools. Work in the run directory; do not modify unrelated locations or Pipelines history/configuration. Do not delegate. Commit/push only when the task explicitly requests it. Final response must be only the stage result JSON."
if c.Policy.Shell != nil {
prompt += "\nPermitted shell commands: " + strings.Join(c.Policy.Shell, "; ")
}
if _, err = c.call(ctx, "post", "/api/session/"+c.SessionID+"/prompt", generationBody("text", prompt, config, c.Backend)); err != nil {
return "", err
}
if _, err = c.call(ctx, "post", "/api/experimental/session/"+c.SessionID+"/wait", nil); err != nil {
@@ -135,9 +142,11 @@ func (c *AgentClient) Generate(ctx context.Context, config ModelConfig, prompt s
c.Transcript = string(data)
var messages struct {
Data []struct {
Finish string `json:"finish"`
Error json.RawMessage `json:"error"`
Content []struct {
Finish string `json:"finish"`
Completed bool `json:"completed"`
Error json.RawMessage `json:"error"`
Structured json.RawMessage `json:"structured"`
Content []struct {
Type string `json:"type"`
Text string `json:"text"`
} `json:"content"`
@@ -150,7 +159,10 @@ func (c *AgentClient) Generate(ctx context.Context, config ModelConfig, prompt s
if len(m.Error) > 0 && string(m.Error) != "null" {
return "", fmt.Errorf("agent reported an execution error")
}
if m.Finish == "stop" {
if m.Finish == "stop" || (m.Finish == "tool-calls" && m.Completed && len(m.Structured) > 0 && string(m.Structured) != "null") {
if len(m.Structured) > 0 && string(m.Structured) != "null" {
return string(m.Structured), nil
}
for _, part := range m.Content {
if part.Type == "text" {
text += part.Text