fix(workflow): require a workflow from triage when a code change is needed
The selection criteria alone left workflow optional and defaulted an omitted or unrecognised value to implement, so a decide call could still skip the decision and send an unsettled capability straight to the builder. Omission is now a retryable rejection: CaptureTool gains RequiredWhenTrue, a conditional requirement a flat RequiredFields entry cannot express, because the close-without-change branch has no workflow to name and must not be made to invent one. A rejected call is not recorded, so it does not consume MaxCalls and the model retries with the field. A value outside the enum now fails the stage rather than degrading to implement: reaching OnResult with one means the model ignored its own schema. Splits validateCaptureArgs into per-constraint checks (cyclop) and carries the regenerated contracts.json.
This commit is contained in:
1 parent
af56021267
commit
fe7ee44dcc
6 files changed
+184
-24
No files matched your search
@@ -0,0 +1,62 @@
|
||||
package agentexec
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// A capture tool sometimes needs a field only under a condition the same call
|
||||
// carries: triage must name a workflow when it says a code change is needed,
|
||||
// and must not be made to invent one when it says the opposite. A flat
|
||||
// RequiredFields entry cannot say that, so the choice used to be between a
|
||||
// meaningless forced answer and a silent default.
|
||||
func TestValidateCaptureArgsConditionalRequirement(t *testing.T) {
|
||||
spec := CaptureTool{
|
||||
Name: "decide",
|
||||
RequiredFields: []string{"needs_code_change"},
|
||||
RequiredWhenTrue: map[string][]string{
|
||||
"needs_code_change": {"workflow"},
|
||||
},
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
args string
|
||||
wantOK bool
|
||||
wantRejected string
|
||||
}{
|
||||
{
|
||||
name: "trigger true and field present",
|
||||
args: `{"needs_code_change": true, "workflow": "feasibility"}`,
|
||||
wantOK: true,
|
||||
},
|
||||
{
|
||||
name: "trigger true and field missing",
|
||||
args: `{"needs_code_change": true}`,
|
||||
wantRejected: "workflow",
|
||||
},
|
||||
{
|
||||
name: "trigger true and field empty",
|
||||
args: `{"needs_code_change": true, "workflow": " "}`,
|
||||
wantRejected: "workflow",
|
||||
},
|
||||
{
|
||||
name: "trigger false, field not required",
|
||||
args: `{"needs_code_change": false}`,
|
||||
wantOK: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
rejection, ok := validateCaptureArgs(spec, json.RawMessage(tt.args))
|
||||
if ok != tt.wantOK {
|
||||
t.Fatalf("validateCaptureArgs ok = %v, want %v (rejection %q)", ok, tt.wantOK, rejection)
|
||||
}
|
||||
if tt.wantRejected != "" && !strings.Contains(rejection, tt.wantRejected) {
|
||||
t.Fatalf("rejection %q does not name %q", rejection, tt.wantRejected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"log/slog"
|
||||
"maps"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"github.com/samcharles93/ai-sdk/agentloop"
|
||||
@@ -283,32 +284,78 @@ func makeCaptureHandler(spec CaptureTool, captures map[string][]json.RawMessage)
|
||||
|
||||
// validateCaptureArgs checks that the capture tool arguments satisfy all
|
||||
// constraints. Returns a rejection message and false on failure, or "" and
|
||||
// true on success.
|
||||
// true on success. Each constraint is its own check so adding one does not
|
||||
// grow a single branching function.
|
||||
func validateCaptureArgs(spec CaptureTool, value json.RawMessage) (string, bool) {
|
||||
var object map[string]json.RawMessage
|
||||
if err := json.Unmarshal(value, &object); err != nil {
|
||||
return spec.Name + " rejected: arguments must be a JSON object", false //nolint:nilerr // the agent loop must see malformed tool arguments as feedback it can correct, not as a failed tool call
|
||||
}
|
||||
for _, check := range []func(CaptureTool, map[string]json.RawMessage) (string, bool){
|
||||
checkRequiredFields,
|
||||
checkNonEmptyStrings,
|
||||
checkBooleanFields,
|
||||
checkRequiredWhenTrue,
|
||||
} {
|
||||
if rejection, ok := check(spec, object); !ok {
|
||||
return rejection, false
|
||||
}
|
||||
}
|
||||
return "", true
|
||||
}
|
||||
|
||||
func checkRequiredFields(spec CaptureTool, object map[string]json.RawMessage) (string, bool) {
|
||||
for _, field := range spec.RequiredFields {
|
||||
if _, ok := object[field]; !ok {
|
||||
return fmt.Sprintf("%s rejected: %s is required", spec.Name, field), false
|
||||
}
|
||||
}
|
||||
return "", true
|
||||
}
|
||||
|
||||
func checkNonEmptyStrings(spec CaptureTool, object map[string]json.RawMessage) (string, bool) {
|
||||
for _, field := range spec.NonEmptyStrings {
|
||||
var text string
|
||||
if raw, ok := object[field]; !ok || json.Unmarshal(raw, &text) != nil || strings.TrimSpace(text) == "" {
|
||||
return fmt.Sprintf("%s rejected: %s must be a non-empty string", spec.Name, field), false //nolint:nilerr // same: a rejection message lets the model retry with a valid argument
|
||||
}
|
||||
}
|
||||
for _, field := range spec.BooleanFields {
|
||||
var val bool
|
||||
if raw, ok := object[field]; !ok || json.Unmarshal(raw, &val) != nil {
|
||||
return fmt.Sprintf("%s rejected: %s must be a boolean", spec.Name, field), false //nolint:nilerr // same: a rejection message lets the model retry with a valid argument
|
||||
if !isNonEmptyString(object, field) {
|
||||
return fmt.Sprintf("%s rejected: %s must be a non-empty string", spec.Name, field), false
|
||||
}
|
||||
}
|
||||
return "", true
|
||||
}
|
||||
|
||||
func checkBooleanFields(spec CaptureTool, object map[string]json.RawMessage) (string, bool) {
|
||||
for _, field := range spec.BooleanFields {
|
||||
var val bool
|
||||
if raw, ok := object[field]; !ok || json.Unmarshal(raw, &val) != nil {
|
||||
return fmt.Sprintf("%s rejected: %s must be a boolean", spec.Name, field), false
|
||||
}
|
||||
}
|
||||
return "", true
|
||||
}
|
||||
|
||||
// checkRequiredWhenTrue enforces the conditional requirements. Triggers are
|
||||
// visited in sorted order so a call violating two of them names the same one
|
||||
// every run, rather than a different one on each retry.
|
||||
func checkRequiredWhenTrue(spec CaptureTool, object map[string]json.RawMessage) (string, bool) {
|
||||
for _, trigger := range slices.Sorted(maps.Keys(spec.RequiredWhenTrue)) {
|
||||
var on bool
|
||||
if raw, ok := object[trigger]; !ok || json.Unmarshal(raw, &on) != nil || !on {
|
||||
continue
|
||||
}
|
||||
for _, field := range spec.RequiredWhenTrue[trigger] {
|
||||
if !isNonEmptyString(object, field) {
|
||||
return fmt.Sprintf("%s rejected: %s is required when %s is true", spec.Name, field, trigger), false
|
||||
}
|
||||
}
|
||||
}
|
||||
return "", true
|
||||
}
|
||||
|
||||
func isNonEmptyString(object map[string]json.RawMessage, field string) bool {
|
||||
var text string
|
||||
raw, ok := object[field]
|
||||
return ok && json.Unmarshal(raw, &text) == nil && strings.TrimSpace(text) != ""
|
||||
}
|
||||
|
||||
// scriptToolSet exposes run_go_script: interpreting a Yaegi Go script
|
||||
// bundled with a skill (.agents/skills/<skill>/scripts/*.go) or living
|
||||
// anywhere else in the workspace, and returning what it printed. This is
|
||||
|
||||
@@ -52,7 +52,15 @@ type CaptureTool struct {
|
||||
RequiredFields []string `json:"required_fields,omitempty"`
|
||||
NonEmptyStrings []string `json:"non_empty_strings,omitempty"`
|
||||
BooleanFields []string `json:"boolean_fields,omitempty"`
|
||||
MaxCalls int `json:"max_calls,omitempty"`
|
||||
// RequiredWhenTrue makes fields required only when a boolean field in
|
||||
// the same call is true, keyed by that boolean's name. It exists because
|
||||
// a flat RequiredFields entry cannot express "name a workflow, but only
|
||||
// when you have said one is needed": the alternatives were forcing a
|
||||
// meaningless answer on the other branch, or accepting the omission and
|
||||
// defaulting it, which lets the model skip the decision entirely.
|
||||
// A named field must also be a non-empty string when the trigger is true.
|
||||
RequiredWhenTrue map[string][]string `json:"required_when_true,omitempty"`
|
||||
MaxCalls int `json:"max_calls,omitempty"`
|
||||
}
|
||||
|
||||
// Request is the complete, serializable input for one agent stage. The
|
||||
|
||||
Reference in new issue
Block a user