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
+13 -4
View File
@@ -15,9 +15,10 @@ const ModelID = "deepseek-v4.1-flash"
// ModelConfig and ModelSpec group the settings/contracts that belong specifically
// to an LLM stage. Other stage types need not have a prompt, provider, or model.
type ModelConfig struct {
Provider string
Model string
Retries int
Provider string
Model string
Retries int
ResultSchema map[string]any
}
type ModelSpec struct {
@@ -53,7 +54,7 @@ func (ModelStage) Spec(def Definition) (ModelSpec, error) {
}
for block := range def.Blocks {
switch block {
case "output", "prompt", "edges", "configs", "tools":
case "output", "prompt", "edges", "configs", "tools", "permissions":
default:
return ModelSpec{}, fmt.Errorf("unexpected block %q for model stage", block)
}
@@ -61,6 +62,9 @@ func (ModelStage) Spec(def Definition) (ModelSpec, error) {
if _, _, err := ToolNames(def); err != nil {
return ModelSpec{}, err
}
if _, err := StagePermissions(def); err != nil {
return ModelSpec{}, err
}
for _, key := range []string{"provider", "model", "retries"} {
if def.Configs[key] == "" {
return ModelSpec{}, fmt.Errorf("missing config %s", key)
@@ -97,6 +101,7 @@ func (m ModelStage) Run(ctx context.Context, def Definition, input Values) (Resu
if err != nil {
return Result{}, err
}
spec.Config.ResultSchema = resultSchema(def.Edges)
prompt, err := modelPrompt(def, spec, input)
if err != nil {
return Result{}, err
@@ -117,6 +122,7 @@ func (m ModelStage) Run(ctx context.Context, def Definition, input Values) (Resu
if tools, explicit, _ := ToolNames(def); explicit {
agent.Tools = tools
}
agent.Policy, _ = StagePermissions(def)
spec.Config.Retries = 0
}
for attempt := 0; attempt <= spec.Config.Retries; attempt++ {
@@ -131,6 +137,9 @@ func (m ModelStage) Run(ctx context.Context, def Definition, input Values) (Resu
if err == nil {
return result, nil
}
// Formatting retries are isolated from execution retries. Never replay
// stage instructions or tool actions to fix malformed final JSON.
return repairModelResult(ctx, client, spec.Config, text, def.Edges, err)
}
if ctx.Err() != nil {
return Result{}, ctx.Err()