♻️ Quality: internal/config without smells (18 → 0)
- validations as ordered checks (checks.go: problem, firstError, and the lazy firstFailing where a later check must not run after a failure) - Load → applyFile + resolvePaths; Config.validate in model / run / section groups; validation code in validate.go and toolsvalidate.go, parameter coercion in toolparams.go - same error messages; the 152 agent files of demos/ and testing/ load exactly as before Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
fe04469 parent: 5bb7026 modified
.tickets/issues/0283-quality-code-smells-of-the-core-zone-by-zone.yaml +11 -2 | @@ -6,7 +6,7 @@ author: | ||
| 6 | 6 | name: k33g |
| 7 | 7 | email: ph.charriere@gmail.com |
| 8 | 8 | createdAt: 2026-11-28T10:19:47.000Z |
| 9 | -updatedAt: 2026-11-28T10:32:34.000Z | |
| 9 | +updatedAt: 2026-11-28T10:51:44.000Z | |
| 10 | 10 | labels: |
| 11 | 11 | - kind::chore |
| 12 | 12 | milestone: v2.2.0 |
| @@ -27,12 +27,14 @@ tasks: | ||
| 27 | 27 | closedAt: 2026-11-28T10:32:34.000Z |
| 28 | 28 | - id: 2 |
| 29 | 29 | title: "internal/config" |
| 30 | - state: open | |
| 30 | + state: closed | |
| 31 | 31 | priority: medium |
| 32 | 32 | author: |
| 33 | 33 | name: k33g |
| 34 | 34 | email: ph.charriere@gmail.com |
| 35 | 35 | createdAt: 2026-11-28T10:19:47.000Z |
| 36 | + updatedAt: 2026-11-28T10:51:44.000Z | |
| 37 | + closedAt: 2026-11-28T10:51:44.000Z | |
| 36 | 38 | - id: 3 |
| 37 | 39 | title: "main.go and the root wiring files" |
| 38 | 40 | state: open |
| @@ -81,3 +83,10 @@ comments: | ||
| 81 | 83 | createdAt: 2026-11-28T10:32:34.000Z |
| 82 | 84 | body: | |
| 83 | 85 | internal/engine: 28 → 0 smells (total 210 → 182). generate() split into a `turn` (turn.go: wrapModel with one method per hand-back sharing handBack, wrapTool, onChunk, emit, retryableWithoutStreaming); Explain and turnStruggle as ordered tables; one-variable switches (openaiCompat.Explain); extracted steps in Summarize, probeOpenAI, ensureModel / pullDMR, UnloadAgent / unloadKeepAlive, xmlTools, reasoningTap / sseTap.report, normalizeEdit, expectedArgs, the Responses provider (post, input, result, stream); engine.go, responses.go, toolschema.go, xmltools.go, history.go split by theme. Printed texts unchanged. go test ./... green; live: a read_skill turn, LFM3 plain answer, decisionContext, decisionResult. |
| 86 | + - id: 2 | |
| 87 | + author: | |
| 88 | + name: k33g | |
| 89 | + email: ph.charriere@gmail.com | |
| 90 | + createdAt: 2026-11-28T10:51:44.000Z | |
| 91 | + body: | | |
| 92 | + internal/config: 18 → 0 smells (total 182 → 164). The validations use checks.go (problem, firstError, and firstFailing — lazy, for checks that must not run once an earlier one failed, e.g. Command[0] on an empty command); Load → applyFile + resolvePaths; validation code moved to validate.go and toolsvalidate.go, parameter coercion to toolparams.go; hooks' slot a map. Error messages unchanged. Checked: go test ./... green, and the 152 agent files of demos/ and testing/ load to exactly the same result as before the change (old/new comparison, no difference). | |
| @@ -6,7 +6,7 @@ author: | |||
| 6 | name: k33g | 6 | name: k33g |
| 7 | email: ph.charriere@gmail.com | 7 | email: ph.charriere@gmail.com |
| 8 | createdAt: 2026-11-28T10:19:47.000Z | 8 | createdAt: 2026-11-28T10:19:47.000Z |
| 9 | -updatedAt: 2026-11-28T10:32:34.000Z | 9 | +updatedAt: 2026-11-28T10:51:44.000Z |
| 10 | labels: | 10 | labels: |
| 11 | - kind::chore | 11 | - kind::chore |
| 12 | milestone: v2.2.0 | 12 | milestone: v2.2.0 |
| @@ -27,12 +27,14 @@ tasks: | |||
| 27 | closedAt: 2026-11-28T10:32:34.000Z | 27 | closedAt: 2026-11-28T10:32:34.000Z |
| 28 | - id: 2 | 28 | - id: 2 |
| 29 | title: "internal/config" | 29 | title: "internal/config" |
| 30 | - state: open | 30 | + state: closed |
| 31 | priority: medium | 31 | priority: medium |
| 32 | author: | 32 | author: |
| 33 | name: k33g | 33 | name: k33g |
| 34 | email: ph.charriere@gmail.com | 34 | email: ph.charriere@gmail.com |
| 35 | createdAt: 2026-11-28T10:19:47.000Z | 35 | createdAt: 2026-11-28T10:19:47.000Z |
| 36 | + updatedAt: 2026-11-28T10:51:44.000Z | ||
| 37 | + closedAt: 2026-11-28T10:51:44.000Z | ||
| 36 | - id: 3 | 38 | - id: 3 |
| 37 | title: "main.go and the root wiring files" | 39 | title: "main.go and the root wiring files" |
| 38 | state: open | 40 | state: open |
| @@ -81,3 +83,10 @@ comments: | |||
| 81 | createdAt: 2026-11-28T10:32:34.000Z | 83 | createdAt: 2026-11-28T10:32:34.000Z |
| 82 | body: | | 84 | body: | |
| 83 | internal/engine: 28 → 0 smells (total 210 → 182). generate() split into a `turn` (turn.go: wrapModel with one method per hand-back sharing handBack, wrapTool, onChunk, emit, retryableWithoutStreaming); Explain and turnStruggle as ordered tables; one-variable switches (openaiCompat.Explain); extracted steps in Summarize, probeOpenAI, ensureModel / pullDMR, UnloadAgent / unloadKeepAlive, xmlTools, reasoningTap / sseTap.report, normalizeEdit, expectedArgs, the Responses provider (post, input, result, stream); engine.go, responses.go, toolschema.go, xmltools.go, history.go split by theme. Printed texts unchanged. go test ./... green; live: a read_skill turn, LFM3 plain answer, decisionContext, decisionResult. | 85 | internal/engine: 28 → 0 smells (total 210 → 182). generate() split into a `turn` (turn.go: wrapModel with one method per hand-back sharing handBack, wrapTool, onChunk, emit, retryableWithoutStreaming); Explain and turnStruggle as ordered tables; one-variable switches (openaiCompat.Explain); extracted steps in Summarize, probeOpenAI, ensureModel / pullDMR, UnloadAgent / unloadKeepAlive, xmlTools, reasoningTap / sseTap.report, normalizeEdit, expectedArgs, the Responses provider (post, input, result, stream); engine.go, responses.go, toolschema.go, xmltools.go, history.go split by theme. Printed texts unchanged. go test ./... green; live: a read_skill turn, LFM3 plain answer, decisionContext, decisionResult. |
| 86 | + - id: 2 | ||
| 87 | + author: | ||
| 88 | + name: k33g | ||
| 89 | + email: ph.charriere@gmail.com | ||
| 90 | + createdAt: 2026-11-28T10:51:44.000Z | ||
| 91 | + body: | | ||
| 92 | + internal/config: 18 → 0 smells (total 182 → 164). The validations use checks.go (problem, firstError, and firstFailing — lazy, for checks that must not run once an earlier one failed, e.g. Command[0] on an empty command); Load → applyFile + resolvePaths; validation code moved to validate.go and toolsvalidate.go, parameter coercion to toolparams.go; hooks' slot a map. Error messages unchanged. Checked: go test ./... green, and the 152 agent files of demos/ and testing/ load to exactly the same result as before the change (old/new comparison, no difference). | ||
renamed
internal/config/checks.go +18 -3 | similarity index 73% | ||
| rename from internal/config/decisionchecks.go | ||
| rename to internal/config/checks.go | ||
| @@ -10,9 +10,10 @@ import ( | ||
| 10 | 10 | "mm/internal/decide" |
| 11 | 11 | ) |
| 12 | 12 | |
| 13 | -// What the decision blocks (decisionContext, decisionTool, decisionResult) | |
| 14 | -// check alike: a list of problems reported in order, and a decider file that | |
| 15 | -// exists, reads and has questions to ask. | |
| 13 | +// The checks every validation shares: a list of problems reported in order | |
| 14 | +// (problem, firstError, firstFailing), errors prefixed with their key, and — | |
| 15 | +// for the decision blocks (decisionContext, decisionTool, decisionResult) — a | |
| 16 | +// decider file that exists, reads and has questions to ask. | |
| 16 | 17 | |
| 17 | 18 | // problem is err when bad is true, nil otherwise — one line of a block's |
| 18 | 19 | // list of checks. |
| @@ -35,6 +36,20 @@ func firstError(errs ...error) error { | ||
| 35 | 36 | return nil |
| 36 | 37 | } |
| 37 | 38 | |
| 39 | +// firstFailing runs checks in order and returns the first error; the checks | |
| 40 | +// after it do not run — as a cascade of `if err := …; err != nil { return | |
| 41 | +// err }`, when a later check must not run once an earlier one failed. | |
| 42 | +// | |
| 43 | +// err := firstFailing(c.LlamaCpp.validate, c.MCP.Validate, c.validateAgents) | |
| 44 | +func firstFailing(checks ...func() error) error { | |
| 45 | + for _, check := range checks { | |
| 46 | + if err := check(); err != nil { | |
| 47 | + return err | |
| 48 | + } | |
| 49 | + } | |
| 50 | + return nil | |
| 51 | +} | |
| 52 | + | |
| 38 | 53 | // prefixed puts key and ": " before err; nil stays nil. |
| 39 | 54 | func prefixed(key string, err error) error { |
| 40 | 55 | if err == nil { |
| similarity index 73% | |||
| rename from internal/config/decisionchecks.go | |||
| rename to internal/config/checks.go | |||
| @@ -10,9 +10,10 @@ import ( | |||
| 10 | "mm/internal/decide" | 10 | "mm/internal/decide" |
| 11 | ) | 11 | ) |
| 12 | 12 | ||
| 13 | -// What the decision blocks (decisionContext, decisionTool, decisionResult) | 13 | +// The checks every validation shares: a list of problems reported in order |
| 14 | -// check alike: a list of problems reported in order, and a decider file that | 14 | +// (problem, firstError, firstFailing), errors prefixed with their key, and — |
| 15 | -// exists, reads and has questions to ask. | 15 | +// for the decision blocks (decisionContext, decisionTool, decisionResult) — a |
| 16 | +// decider file that exists, reads and has questions to ask. | ||
| 16 | 17 | ||
| 17 | // problem is err when bad is true, nil otherwise — one line of a block's | 18 | // problem is err when bad is true, nil otherwise — one line of a block's |
| 18 | // list of checks. | 19 | // list of checks. |
| @@ -35,6 +36,20 @@ func firstError(errs ...error) error { | |||
| 35 | return nil | 36 | return nil |
| 36 | } | 37 | } |
| 37 | 38 | ||
| 39 | +// firstFailing runs checks in order and returns the first error; the checks | ||
| 40 | +// after it do not run — as a cascade of `if err := …; err != nil { return | ||
| 41 | +// err }`, when a later check must not run once an earlier one failed. | ||
| 42 | +// | ||
| 43 | +// err := firstFailing(c.LlamaCpp.validate, c.MCP.Validate, c.validateAgents) | ||
| 44 | +func firstFailing(checks ...func() error) error { | ||
| 45 | + for _, check := range checks { | ||
| 46 | + if err := check(); err != nil { | ||
| 47 | + return err | ||
| 48 | + } | ||
| 49 | + } | ||
| 50 | + return nil | ||
| 51 | +} | ||
| 52 | + | ||
| 38 | // prefixed puts key and ": " before err; nil stays nil. | 53 | // prefixed puts key and ": " before err; nil stays nil. |
| 39 | func prefixed(key string, err error) error { | 54 | func prefixed(key string, err error) error { |
| 40 | if err == nil { | 55 | if err == nil { |
modified
internal/config/config.go +20 -234 | @@ -436,30 +436,6 @@ func (l LlamaCppConfig) LaunchArgs() []string { | ||
| 436 | 436 | return append(args, l.ServerArgs...) |
| 437 | 437 | } |
| 438 | 438 | |
| 439 | -// validate refuses a value llama-server would refuse at start-up — better | |
| 440 | -// a configuration error naming the key than a server that does not start. | |
| 441 | -func (l LlamaCppConfig) validate() error { | |
| 442 | - for _, f := range []struct { | |
| 443 | - key string | |
| 444 | - value *int | |
| 445 | - min int | |
| 446 | - }{ | |
| 447 | - {"ctxSize", l.CtxSize, 0}, | |
| 448 | - {"modelsMax", l.ModelsMax, 0}, | |
| 449 | - {"modelsMemoryMargin", l.ModelsMemoryMargin, 0}, | |
| 450 | - {"fitTarget", l.FitTarget, 0}, | |
| 451 | - {"sleepIdleSeconds", l.SleepIdleSeconds, -1}, | |
| 452 | - } { | |
| 453 | - if f.value != nil && *f.value < f.min { | |
| 454 | - return fmt.Errorf("llamacpp.%s must be >= %d, not %d", f.key, f.min, *f.value) | |
| 455 | - } | |
| 456 | - } | |
| 457 | - if l.StartTimeout < 0 { | |
| 458 | - return errors.New("llamacpp.startTimeout must be >= 0") | |
| 459 | - } | |
| 460 | - return nil | |
| 461 | -} | |
| 462 | - | |
| 463 | 439 | // DefaultLlamaStartTimeout is llamacpp.startTimeout when the file does not |
| 464 | 440 | // set it: a download of several GB fits in it on a usual connection. |
| 465 | 441 | const DefaultLlamaStartTimeout = 15 * time.Minute |
| @@ -523,19 +499,6 @@ type ClientToolsConfig struct { | ||
| 523 | 499 | Timeout time.Duration `yaml:"timeout"` |
| 524 | 500 | } |
| 525 | 501 | |
| 526 | -// validate refuses the values the ACP front end could not act on. | |
| 527 | -func (c ClientToolsConfig) validate() error { | |
| 528 | - switch { | |
| 529 | - case c.Discovery != DiscoveryMeta && c.Discovery != DiscoveryRequest: | |
| 530 | - return fmt.Errorf("acp.clientTools.discovery must be %q or %q, got %q", DiscoveryMeta, DiscoveryRequest, c.Discovery) | |
| 531 | - case c.Permissions != PermissionsAuto && c.Permissions != PermissionsAsk: | |
| 532 | - return fmt.Errorf("acp.clientTools.permissions must be %q or %q, got %q", PermissionsAuto, PermissionsAsk, c.Permissions) | |
| 533 | - case c.Timeout <= 0: | |
| 534 | - return errors.New("acp.clientTools.timeout must be > 0") | |
| 535 | - } | |
| 536 | - return nil | |
| 537 | -} | |
| 538 | - | |
| 539 | 502 | // SessionsConfig is the `sessions:` block of the YAML. |
| 540 | 503 | // LSPConfig is the `lsp` key. |
| 541 | 504 | type LSPConfig struct { |
| @@ -708,45 +671,6 @@ type MCPAuthConfig struct { | ||
| 708 | 671 | OAuthScopes []string `yaml:"oauthScopes"` |
| 709 | 672 | } |
| 710 | 673 | |
| 711 | -// Validate checks the MCP server configuration. | |
| 712 | -func (s MCPServer) Validate() error { | |
| 713 | - switch { | |
| 714 | - case s.Name == "": | |
| 715 | - return errors.New("mcp server name must not be empty") | |
| 716 | - case s.Transport == "": | |
| 717 | - return errors.New("mcp server transport must be 'http' or 'stdio'") | |
| 718 | - case s.Transport == "http" && s.URL == "": | |
| 719 | - return errors.New("mcp server with http transport must have a url") | |
| 720 | - case s.Transport == "stdio" && s.Command == "": | |
| 721 | - return errors.New("mcp server with stdio transport must have a command") | |
| 722 | - case s.Transport != "http" && s.Transport != "stdio": | |
| 723 | - return fmt.Errorf("mcp server transport must be 'http' or 'stdio', got %q", s.Transport) | |
| 724 | - case s.Auth.Type != "" && s.Auth.Type != "none" && s.Auth.Type != "bearer" && s.Auth.Type != "api_key" && s.Auth.Type != "oauth2": | |
| 725 | - return fmt.Errorf("mcp auth type must be 'none', 'bearer', 'api_key', or 'oauth2', got %q", s.Auth.Type) | |
| 726 | - case s.Auth.Type == "bearer" && s.Auth.TokenEnv == "": | |
| 727 | - return errors.New("mcp bearer auth must specify tokenEnv") | |
| 728 | - case s.Auth.Type == "api_key" && s.Auth.APIKeyEnv == "": | |
| 729 | - return errors.New("mcp api_key auth must specify apiKeyEnv") | |
| 730 | - } | |
| 731 | - return nil | |
| 732 | -} | |
| 733 | - | |
| 734 | -// validate checks the MCP configuration. | |
| 735 | -func (m MCPConfig) Validate() error { | |
| 736 | - if !m.Enabled { | |
| 737 | - return nil // MCP disabled, nothing to validate | |
| 738 | - } | |
| 739 | - if m.Permissions != "" && m.Permissions != PermissionsAuto && m.Permissions != PermissionsAsk { | |
| 740 | - return fmt.Errorf("mcp.permissions must be 'auto' or 'ask', got %q", m.Permissions) | |
| 741 | - } | |
| 742 | - for i, s := range m.Servers { | |
| 743 | - if err := s.Validate(); err != nil { | |
| 744 | - return fmt.Errorf("mcp.servers[%d]: %w", i, err) | |
| 745 | - } | |
| 746 | - } | |
| 747 | - return nil | |
| 748 | -} | |
| 749 | - | |
| 750 | 674 | // TraceConfig is the `trace:` block of the YAML. |
| 751 | 675 | type TraceConfig struct { |
| 752 | 676 | // Enabled turns the trace on. AGENT_TRACE (true/false) overrides it. |
| @@ -1024,31 +948,47 @@ func Load(cliPath string) (string, error) { | ||
| 1024 | 948 | if path == "" { |
| 1025 | 949 | path, explicit = defaultPath(), false |
| 1026 | 950 | } |
| 1027 | - | |
| 1028 | 951 | data, err := os.ReadFile(path) |
| 1029 | 952 | if errors.Is(err, fs.ErrNotExist) && !explicit { |
| 1030 | 953 | return "", nil // no file: the defaults stand |
| 1031 | 954 | } |
| 955 | + if err == nil { | |
| 956 | + err = applyFile(path, data) | |
| 957 | + } | |
| 1032 | 958 | if err != nil { |
| 1033 | 959 | return "", err |
| 1034 | 960 | } |
| 961 | + return path, nil | |
| 962 | +} | |
| 1035 | 963 | |
| 964 | +// applyFile puts the file read from path on top of Cfg's defaults: the | |
| 965 | +// YAML, the deprecated keys, the environment, the provider's sampling, the | |
| 966 | +// relative paths, then the checks. Errors carry the path, except the | |
| 967 | +// environment's, which is not about the file. | |
| 968 | +func applyFile(path string, data []byte) error { | |
| 1036 | 969 | // Unmarshalling INTO Cfg: keys absent from the file keep their default. |
| 1037 | 970 | // The sampling map is decoded into in place: a copy keeps the default map |
| 1038 | 971 | // itself untouched (a second Load, a test, starts from the real defaults). |
| 1039 | 972 | Cfg.Sampling = maps.Clone(Cfg.Sampling) |
| 1040 | 973 | if err := yaml.Unmarshal(data, &Cfg); err != nil { |
| 1041 | - return "", fmt.Errorf("%s: %w", path, err) | |
| 974 | + return fmt.Errorf("%s: %w", path, err) | |
| 1042 | 975 | } |
| 1043 | 976 | own, err := ownSampling(data) |
| 1044 | 977 | if err != nil { |
| 1045 | - return "", fmt.Errorf("%s: %w", path, err) | |
| 978 | + return fmt.Errorf("%s: %w", path, err) | |
| 1046 | 979 | } |
| 1047 | 980 | applyDeprecated() |
| 1048 | 981 | if err := applyEnv(); err != nil { |
| 1049 | - return "", err | |
| 982 | + return err | |
| 1050 | 983 | } |
| 1051 | 984 | applyProviderSampling(own) |
| 985 | + resolvePaths(path) | |
| 986 | + return prefixed(path, firstFailing(Cfg.validate, compileStructuredOutput)) | |
| 987 | +} | |
| 988 | + | |
| 989 | +// resolvePaths makes every relative path of the file absolute against its | |
| 990 | +// directory. | |
| 991 | +func resolvePaths(path string) { | |
| 1052 | 992 | resolveAgentPaths(path) |
| 1053 | 993 | resolveRazorbillPaths(path) |
| 1054 | 994 | resolveHooks(path) |
| @@ -1059,13 +999,6 @@ func Load(cliPath string) (string, error) { | ||
| 1059 | 999 | resolveContextDecider(path) |
| 1060 | 1000 | resolveToolGateDecider(path) |
| 1061 | 1001 | resolveResultDecider(path) |
| 1062 | - if err := Cfg.validate(); err != nil { | |
| 1063 | - return "", fmt.Errorf("%s: %w", path, err) | |
| 1064 | - } | |
| 1065 | - if err := compileStructuredOutput(); err != nil { | |
| 1066 | - return "", fmt.Errorf("%s: %w", path, err) | |
| 1067 | - } | |
| 1068 | - return path, nil | |
| 1069 | 1002 | } |
| 1070 | 1003 | |
| 1071 | 1004 | // applyEnv lets three variables override the file: AGENT_PROVIDER, AGENT_MODEL, |
| @@ -1145,150 +1078,3 @@ func Deprecations() []string { | ||
| 1145 | 1078 | return []string{fmt.Sprintf("permissions: %s is deprecated — use yolo: %t instead (yolo: false asks before every tool call, in every front end)", |
| 1146 | 1079 | Cfg.Permissions, Cfg.Permissions != PermissionsAsk)} |
| 1147 | 1080 | } |
| 1148 | - | |
| 1149 | -// validate checks the language servers: every one named and runnable, every | |
| 1150 | -// extension written with its dot and served once — two servers for ".ts" | |
| 1151 | -// would make the tool's choice arbitrary. | |
| 1152 | -func (l LSPConfig) validate() error { | |
| 1153 | - seen := map[string]string{} | |
| 1154 | - for i, s := range l.Servers { | |
| 1155 | - if err := s.validate(fmt.Sprintf("lsp.servers[%d]", i), seen); err != nil { | |
| 1156 | - return err | |
| 1157 | - } | |
| 1158 | - } | |
| 1159 | - return nil | |
| 1160 | -} | |
| 1161 | - | |
| 1162 | -// validate checks one server and claims its extensions in seen (extension → | |
| 1163 | -// server name), so a second server on the same extension is refused. | |
| 1164 | -func (s LSPServer) validate(where string, seen map[string]string) error { | |
| 1165 | - switch { | |
| 1166 | - case s.Name == "": | |
| 1167 | - return errors.New(where + ".name must not be empty") | |
| 1168 | - case s.Command == "": | |
| 1169 | - return errors.New(where + ".command must not be empty") | |
| 1170 | - case len(s.Extensions) == 0: | |
| 1171 | - return errors.New(where + ".extensions must list at least one extension") | |
| 1172 | - } | |
| 1173 | - for _, e := range s.Extensions { | |
| 1174 | - if err := s.claimExtension(where, e, seen); err != nil { | |
| 1175 | - return err | |
| 1176 | - } | |
| 1177 | - } | |
| 1178 | - return nil | |
| 1179 | -} | |
| 1180 | - | |
| 1181 | -// claimExtension checks one extension's spelling and records the server as | |
| 1182 | -// its owner, refusing an extension another server already owns. | |
| 1183 | -func (s LSPServer) claimExtension(where, e string, seen map[string]string) error { | |
| 1184 | - ext := strings.ToLower(e) | |
| 1185 | - if !strings.HasPrefix(ext, ".") || len(ext) < 2 { | |
| 1186 | - return fmt.Errorf("%s.extensions: %q must start with a dot, like \".go\"", where, e) | |
| 1187 | - } | |
| 1188 | - if other, dup := seen[ext]; dup { | |
| 1189 | - return fmt.Errorf("%s: extension %q is already served by %q", where, e, other) | |
| 1190 | - } | |
| 1191 | - seen[ext] = s.Name | |
| 1192 | - return nil | |
| 1193 | -} | |
| 1194 | - | |
| 1195 | -// validate catches the settings that would break the agent at runtime. | |
| 1196 | -func (c Config) validate() error { | |
| 1197 | - if err := c.LlamaCpp.validate(); err != nil { | |
| 1198 | - return err | |
| 1199 | - } | |
| 1200 | - if err := c.Razorbill.validate(); err != nil { | |
| 1201 | - return err | |
| 1202 | - } | |
| 1203 | - if err := c.A2A.validate(); err != nil { | |
| 1204 | - return err | |
| 1205 | - } | |
| 1206 | - if err := validatePrompt(c.System, c.Variables); err != nil { | |
| 1207 | - return err | |
| 1208 | - } | |
| 1209 | - switch { | |
| 1210 | - case c.Provider == "": | |
| 1211 | - return errors.New("provider must not be empty") | |
| 1212 | - case c.Model == "": | |
| 1213 | - return errors.New("model must not be empty") | |
| 1214 | - case c.ContextWindow < 0: | |
| 1215 | - return errors.New("contextWindow must be >= 0") | |
| 1216 | - case c.ToolFormat != "" && c.ToolFormat != ToolFormatOpenAI && c.ToolFormat != ToolFormatXML: | |
| 1217 | - return fmt.Errorf("toolFormat must be %s or %s, not %q", ToolFormatOpenAI, ToolFormatXML, c.ToolFormat) | |
| 1218 | - case c.FirstTokenTimeout < 0: | |
| 1219 | - return errors.New("firstTokenTimeout must be >= 0") | |
| 1220 | - case c.BashTimeout <= 0: | |
| 1221 | - return errors.New("bashTimeout must be > 0") | |
| 1222 | - case c.BashBackground != BashBackgroundReport && c.BashBackground != BashBackgroundStop && c.BashBackground != BashBackgroundAllow: | |
| 1223 | - return fmt.Errorf("bashBackground must be %q, %q or %q, not %q", BashBackgroundReport, BashBackgroundStop, BashBackgroundAllow, c.BashBackground) | |
| 1224 | - case c.MaxOutput <= 0: | |
| 1225 | - return errors.New("maxOutput must be > 0") | |
| 1226 | - case c.MaxTurns <= 0: | |
| 1227 | - return errors.New("maxTurns must be > 0") | |
| 1228 | - case c.PreviewLines < 0: | |
| 1229 | - return errors.New("previewLines must be >= 0") | |
| 1230 | - case c.Context.Threshold < 1 || c.Context.Threshold > 100: | |
| 1231 | - return errors.New("context.threshold must be between 1 and 100") | |
| 1232 | - case c.Context.KeepLastTurns < 1: | |
| 1233 | - return errors.New("context.keepLastTurns must be >= 1") | |
| 1234 | - case c.Context.SummaryMaxTokens <= 0: | |
| 1235 | - return errors.New("context.summaryMaxTokens must be > 0") | |
| 1236 | - case c.Context.MaxMessages < 0: | |
| 1237 | - return errors.New("context.maxMessages must be >= 0") | |
| 1238 | - case c.Sessions.Dir == "": | |
| 1239 | - return errors.New("sessions.dir must not be empty") | |
| 1240 | - case c.Trace.Enabled && strings.TrimSpace(c.Trace.Dir) == "": | |
| 1241 | - return errors.New("trace.dir must not be empty") | |
| 1242 | - case c.AgentsMd.Enabled && strings.TrimSpace(c.AgentsMd.Path) == "": | |
| 1243 | - return errors.New("agentsMd.path must not be empty") | |
| 1244 | - case c.AgentsMd.MaxChars < 0: | |
| 1245 | - return errors.New("agentsMd.maxChars must be >= 0") | |
| 1246 | - case strings.TrimSpace(c.ACP.Name) == "": | |
| 1247 | - return errors.New("acp.name must not be empty") | |
| 1248 | - case c.Permissions != "" && c.Permissions != PermissionsAuto && c.Permissions != PermissionsAsk: | |
| 1249 | - return errors.New(`permissions must be "auto" or "ask"`) | |
| 1250 | - } | |
| 1251 | - // `enabled: true` with no window and no maxMessages would never trigger — | |
| 1252 | - // but the window may still come from the server's probe at start-up, so | |
| 1253 | - // that case is a warning in main, not an error here. | |
| 1254 | - if err := c.MCP.Validate(); err != nil { | |
| 1255 | - return err | |
| 1256 | - } | |
| 1257 | - if err := c.ACP.ClientTools.validate(); err != nil { | |
| 1258 | - return err | |
| 1259 | - } | |
| 1260 | - if err := c.validateAgents(); err != nil { | |
| 1261 | - return err | |
| 1262 | - } | |
| 1263 | - if err := c.Hooks.validate(); err != nil { | |
| 1264 | - return err | |
| 1265 | - } | |
| 1266 | - if err := c.validateTools(); err != nil { | |
| 1267 | - return err | |
| 1268 | - } | |
| 1269 | - if err := validateMetaPrompts(c.MetaPrompts); err != nil { | |
| 1270 | - return err | |
| 1271 | - } | |
| 1272 | - if err := c.DocSearch.validate(); err != nil { | |
| 1273 | - return err | |
| 1274 | - } | |
| 1275 | - if err := c.validateDecision(); err != nil { | |
| 1276 | - return err | |
| 1277 | - } | |
| 1278 | - if err := c.SkillRouting.validate(); err != nil { | |
| 1279 | - return err | |
| 1280 | - } | |
| 1281 | - if err := c.ToolRouting.validate(); err != nil { | |
| 1282 | - return err | |
| 1283 | - } | |
| 1284 | - if err := c.DecisionContext.validate(c.Decision != nil); err != nil { | |
| 1285 | - return err | |
| 1286 | - } | |
| 1287 | - if err := c.DecisionTool.validate(c.Decision != nil); err != nil { | |
| 1288 | - return err | |
| 1289 | - } | |
| 1290 | - if err := c.DecisionResult.validate(c.Decision != nil); err != nil { | |
| 1291 | - return err | |
| 1292 | - } | |
| 1293 | - return c.LSP.validate() | |
| 1294 | -} | |
| @@ -436,30 +436,6 @@ func (l LlamaCppConfig) LaunchArgs() []string { | |||
| 436 | return append(args, l.ServerArgs...) | 436 | return append(args, l.ServerArgs...) |
| 437 | } | 437 | } |
| 438 | 438 | ||
| 439 | -// validate refuses a value llama-server would refuse at start-up — better | ||
| 440 | -// a configuration error naming the key than a server that does not start. | ||
| 441 | -func (l LlamaCppConfig) validate() error { | ||
| 442 | - for _, f := range []struct { | ||
| 443 | - key string | ||
| 444 | - value *int | ||
| 445 | - min int | ||
| 446 | - }{ | ||
| 447 | - {"ctxSize", l.CtxSize, 0}, | ||
| 448 | - {"modelsMax", l.ModelsMax, 0}, | ||
| 449 | - {"modelsMemoryMargin", l.ModelsMemoryMargin, 0}, | ||
| 450 | - {"fitTarget", l.FitTarget, 0}, | ||
| 451 | - {"sleepIdleSeconds", l.SleepIdleSeconds, -1}, | ||
| 452 | - } { | ||
| 453 | - if f.value != nil && *f.value < f.min { | ||
| 454 | - return fmt.Errorf("llamacpp.%s must be >= %d, not %d", f.key, f.min, *f.value) | ||
| 455 | - } | ||
| 456 | - } | ||
| 457 | - if l.StartTimeout < 0 { | ||
| 458 | - return errors.New("llamacpp.startTimeout must be >= 0") | ||
| 459 | - } | ||
| 460 | - return nil | ||
| 461 | -} | ||
| 462 | - | ||
| 463 | // DefaultLlamaStartTimeout is llamacpp.startTimeout when the file does not | 439 | // DefaultLlamaStartTimeout is llamacpp.startTimeout when the file does not |
| 464 | // set it: a download of several GB fits in it on a usual connection. | 440 | // set it: a download of several GB fits in it on a usual connection. |
| 465 | const DefaultLlamaStartTimeout = 15 * time.Minute | 441 | const DefaultLlamaStartTimeout = 15 * time.Minute |
| @@ -523,19 +499,6 @@ type ClientToolsConfig struct { | |||
| 523 | Timeout time.Duration `yaml:"timeout"` | 499 | Timeout time.Duration `yaml:"timeout"` |
| 524 | } | 500 | } |
| 525 | 501 | ||
| 526 | -// validate refuses the values the ACP front end could not act on. | ||
| 527 | -func (c ClientToolsConfig) validate() error { | ||
| 528 | - switch { | ||
| 529 | - case c.Discovery != DiscoveryMeta && c.Discovery != DiscoveryRequest: | ||
| 530 | - return fmt.Errorf("acp.clientTools.discovery must be %q or %q, got %q", DiscoveryMeta, DiscoveryRequest, c.Discovery) | ||
| 531 | - case c.Permissions != PermissionsAuto && c.Permissions != PermissionsAsk: | ||
| 532 | - return fmt.Errorf("acp.clientTools.permissions must be %q or %q, got %q", PermissionsAuto, PermissionsAsk, c.Permissions) | ||
| 533 | - case c.Timeout <= 0: | ||
| 534 | - return errors.New("acp.clientTools.timeout must be > 0") | ||
| 535 | - } | ||
| 536 | - return nil | ||
| 537 | -} | ||
| 538 | - | ||
| 539 | // SessionsConfig is the `sessions:` block of the YAML. | 502 | // SessionsConfig is the `sessions:` block of the YAML. |
| 540 | // LSPConfig is the `lsp` key. | 503 | // LSPConfig is the `lsp` key. |
| 541 | type LSPConfig struct { | 504 | type LSPConfig struct { |
| @@ -708,45 +671,6 @@ type MCPAuthConfig struct { | |||
| 708 | OAuthScopes []string `yaml:"oauthScopes"` | 671 | OAuthScopes []string `yaml:"oauthScopes"` |
| 709 | } | 672 | } |
| 710 | 673 | ||
| 711 | -// Validate checks the MCP server configuration. | ||
| 712 | -func (s MCPServer) Validate() error { | ||
| 713 | - switch { | ||
| 714 | - case s.Name == "": | ||
| 715 | - return errors.New("mcp server name must not be empty") | ||
| 716 | - case s.Transport == "": | ||
| 717 | - return errors.New("mcp server transport must be 'http' or 'stdio'") | ||
| 718 | - case s.Transport == "http" && s.URL == "": | ||
| 719 | - return errors.New("mcp server with http transport must have a url") | ||
| 720 | - case s.Transport == "stdio" && s.Command == "": | ||
| 721 | - return errors.New("mcp server with stdio transport must have a command") | ||
| 722 | - case s.Transport != "http" && s.Transport != "stdio": | ||
| 723 | - return fmt.Errorf("mcp server transport must be 'http' or 'stdio', got %q", s.Transport) | ||
| 724 | - case s.Auth.Type != "" && s.Auth.Type != "none" && s.Auth.Type != "bearer" && s.Auth.Type != "api_key" && s.Auth.Type != "oauth2": | ||
| 725 | - return fmt.Errorf("mcp auth type must be 'none', 'bearer', 'api_key', or 'oauth2', got %q", s.Auth.Type) | ||
| 726 | - case s.Auth.Type == "bearer" && s.Auth.TokenEnv == "": | ||
| 727 | - return errors.New("mcp bearer auth must specify tokenEnv") | ||
| 728 | - case s.Auth.Type == "api_key" && s.Auth.APIKeyEnv == "": | ||
| 729 | - return errors.New("mcp api_key auth must specify apiKeyEnv") | ||
| 730 | - } | ||
| 731 | - return nil | ||
| 732 | -} | ||
| 733 | - | ||
| 734 | -// validate checks the MCP configuration. | ||
| 735 | -func (m MCPConfig) Validate() error { | ||
| 736 | - if !m.Enabled { | ||
| 737 | - return nil // MCP disabled, nothing to validate | ||
| 738 | - } | ||
| 739 | - if m.Permissions != "" && m.Permissions != PermissionsAuto && m.Permissions != PermissionsAsk { | ||
| 740 | - return fmt.Errorf("mcp.permissions must be 'auto' or 'ask', got %q", m.Permissions) | ||
| 741 | - } | ||
| 742 | - for i, s := range m.Servers { | ||
| 743 | - if err := s.Validate(); err != nil { | ||
| 744 | - return fmt.Errorf("mcp.servers[%d]: %w", i, err) | ||
| 745 | - } | ||
| 746 | - } | ||
| 747 | - return nil | ||
| 748 | -} | ||
| 749 | - | ||
| 750 | // TraceConfig is the `trace:` block of the YAML. | 674 | // TraceConfig is the `trace:` block of the YAML. |
| 751 | type TraceConfig struct { | 675 | type TraceConfig struct { |
| 752 | // Enabled turns the trace on. AGENT_TRACE (true/false) overrides it. | 676 | // Enabled turns the trace on. AGENT_TRACE (true/false) overrides it. |
| @@ -1024,31 +948,47 @@ func Load(cliPath string) (string, error) { | |||
| 1024 | if path == "" { | 948 | if path == "" { |
| 1025 | path, explicit = defaultPath(), false | 949 | path, explicit = defaultPath(), false |
| 1026 | } | 950 | } |
| 1027 | - | ||
| 1028 | data, err := os.ReadFile(path) | 951 | data, err := os.ReadFile(path) |
| 1029 | if errors.Is(err, fs.ErrNotExist) && !explicit { | 952 | if errors.Is(err, fs.ErrNotExist) && !explicit { |
| 1030 | return "", nil // no file: the defaults stand | 953 | return "", nil // no file: the defaults stand |
| 1031 | } | 954 | } |
| 955 | + if err == nil { | ||
| 956 | + err = applyFile(path, data) | ||
| 957 | + } | ||
| 1032 | if err != nil { | 958 | if err != nil { |
| 1033 | return "", err | 959 | return "", err |
| 1034 | } | 960 | } |
| 961 | + return path, nil | ||
| 962 | +} | ||
| 1035 | 963 | ||
| 964 | +// applyFile puts the file read from path on top of Cfg's defaults: the | ||
| 965 | +// YAML, the deprecated keys, the environment, the provider's sampling, the | ||
| 966 | +// relative paths, then the checks. Errors carry the path, except the | ||
| 967 | +// environment's, which is not about the file. | ||
| 968 | +func applyFile(path string, data []byte) error { | ||
| 1036 | // Unmarshalling INTO Cfg: keys absent from the file keep their default. | 969 | // Unmarshalling INTO Cfg: keys absent from the file keep their default. |
| 1037 | // The sampling map is decoded into in place: a copy keeps the default map | 970 | // The sampling map is decoded into in place: a copy keeps the default map |
| 1038 | // itself untouched (a second Load, a test, starts from the real defaults). | 971 | // itself untouched (a second Load, a test, starts from the real defaults). |
| 1039 | Cfg.Sampling = maps.Clone(Cfg.Sampling) | 972 | Cfg.Sampling = maps.Clone(Cfg.Sampling) |
| 1040 | if err := yaml.Unmarshal(data, &Cfg); err != nil { | 973 | if err := yaml.Unmarshal(data, &Cfg); err != nil { |
| 1041 | - return "", fmt.Errorf("%s: %w", path, err) | 974 | + return fmt.Errorf("%s: %w", path, err) |
| 1042 | } | 975 | } |
| 1043 | own, err := ownSampling(data) | 976 | own, err := ownSampling(data) |
| 1044 | if err != nil { | 977 | if err != nil { |
| 1045 | - return "", fmt.Errorf("%s: %w", path, err) | 978 | + return fmt.Errorf("%s: %w", path, err) |
| 1046 | } | 979 | } |
| 1047 | applyDeprecated() | 980 | applyDeprecated() |
| 1048 | if err := applyEnv(); err != nil { | 981 | if err := applyEnv(); err != nil { |
| 1049 | - return "", err | 982 | + return err |
| 1050 | } | 983 | } |
| 1051 | applyProviderSampling(own) | 984 | applyProviderSampling(own) |
| 985 | + resolvePaths(path) | ||
| 986 | + return prefixed(path, firstFailing(Cfg.validate, compileStructuredOutput)) | ||
| 987 | +} | ||
| 988 | + | ||
| 989 | +// resolvePaths makes every relative path of the file absolute against its | ||
| 990 | +// directory. | ||
| 991 | +func resolvePaths(path string) { | ||
| 1052 | resolveAgentPaths(path) | 992 | resolveAgentPaths(path) |
| 1053 | resolveRazorbillPaths(path) | 993 | resolveRazorbillPaths(path) |
| 1054 | resolveHooks(path) | 994 | resolveHooks(path) |
| @@ -1059,13 +999,6 @@ func Load(cliPath string) (string, error) { | |||
| 1059 | resolveContextDecider(path) | 999 | resolveContextDecider(path) |
| 1060 | resolveToolGateDecider(path) | 1000 | resolveToolGateDecider(path) |
| 1061 | resolveResultDecider(path) | 1001 | resolveResultDecider(path) |
| 1062 | - if err := Cfg.validate(); err != nil { | ||
| 1063 | - return "", fmt.Errorf("%s: %w", path, err) | ||
| 1064 | - } | ||
| 1065 | - if err := compileStructuredOutput(); err != nil { | ||
| 1066 | - return "", fmt.Errorf("%s: %w", path, err) | ||
| 1067 | - } | ||
| 1068 | - return path, nil | ||
| 1069 | } | 1002 | } |
| 1070 | 1003 | ||
| 1071 | // applyEnv lets three variables override the file: AGENT_PROVIDER, AGENT_MODEL, | 1004 | // applyEnv lets three variables override the file: AGENT_PROVIDER, AGENT_MODEL, |
| @@ -1145,150 +1078,3 @@ func Deprecations() []string { | |||
| 1145 | return []string{fmt.Sprintf("permissions: %s is deprecated — use yolo: %t instead (yolo: false asks before every tool call, in every front end)", | 1078 | return []string{fmt.Sprintf("permissions: %s is deprecated — use yolo: %t instead (yolo: false asks before every tool call, in every front end)", |
| 1146 | Cfg.Permissions, Cfg.Permissions != PermissionsAsk)} | 1079 | Cfg.Permissions, Cfg.Permissions != PermissionsAsk)} |
| 1147 | } | 1080 | } |
| 1148 | - | ||
| 1149 | -// validate checks the language servers: every one named and runnable, every | ||
| 1150 | -// extension written with its dot and served once — two servers for ".ts" | ||
| 1151 | -// would make the tool's choice arbitrary. | ||
| 1152 | -func (l LSPConfig) validate() error { | ||
| 1153 | - seen := map[string]string{} | ||
| 1154 | - for i, s := range l.Servers { | ||
| 1155 | - if err := s.validate(fmt.Sprintf("lsp.servers[%d]", i), seen); err != nil { | ||
| 1156 | - return err | ||
| 1157 | - } | ||
| 1158 | - } | ||
| 1159 | - return nil | ||
| 1160 | -} | ||
| 1161 | - | ||
| 1162 | -// validate checks one server and claims its extensions in seen (extension → | ||
| 1163 | -// server name), so a second server on the same extension is refused. | ||
| 1164 | -func (s LSPServer) validate(where string, seen map[string]string) error { | ||
| 1165 | - switch { | ||
| 1166 | - case s.Name == "": | ||
| 1167 | - return errors.New(where + ".name must not be empty") | ||
| 1168 | - case s.Command == "": | ||
| 1169 | - return errors.New(where + ".command must not be empty") | ||
| 1170 | - case len(s.Extensions) == 0: | ||
| 1171 | - return errors.New(where + ".extensions must list at least one extension") | ||
| 1172 | - } | ||
| 1173 | - for _, e := range s.Extensions { | ||
| 1174 | - if err := s.claimExtension(where, e, seen); err != nil { | ||
| 1175 | - return err | ||
| 1176 | - } | ||
| 1177 | - } | ||
| 1178 | - return nil | ||
| 1179 | -} | ||
| 1180 | - | ||
| 1181 | -// claimExtension checks one extension's spelling and records the server as | ||
| 1182 | -// its owner, refusing an extension another server already owns. | ||
| 1183 | -func (s LSPServer) claimExtension(where, e string, seen map[string]string) error { | ||
| 1184 | - ext := strings.ToLower(e) | ||
| 1185 | - if !strings.HasPrefix(ext, ".") || len(ext) < 2 { | ||
| 1186 | - return fmt.Errorf("%s.extensions: %q must start with a dot, like \".go\"", where, e) | ||
| 1187 | - } | ||
| 1188 | - if other, dup := seen[ext]; dup { | ||
| 1189 | - return fmt.Errorf("%s: extension %q is already served by %q", where, e, other) | ||
| 1190 | - } | ||
| 1191 | - seen[ext] = s.Name | ||
| 1192 | - return nil | ||
| 1193 | -} | ||
| 1194 | - | ||
| 1195 | -// validate catches the settings that would break the agent at runtime. | ||
| 1196 | -func (c Config) validate() error { | ||
| 1197 | - if err := c.LlamaCpp.validate(); err != nil { | ||
| 1198 | - return err | ||
| 1199 | - } | ||
| 1200 | - if err := c.Razorbill.validate(); err != nil { | ||
| 1201 | - return err | ||
| 1202 | - } | ||
| 1203 | - if err := c.A2A.validate(); err != nil { | ||
| 1204 | - return err | ||
| 1205 | - } | ||
| 1206 | - if err := validatePrompt(c.System, c.Variables); err != nil { | ||
| 1207 | - return err | ||
| 1208 | - } | ||
| 1209 | - switch { | ||
| 1210 | - case c.Provider == "": | ||
| 1211 | - return errors.New("provider must not be empty") | ||
| 1212 | - case c.Model == "": | ||
| 1213 | - return errors.New("model must not be empty") | ||
| 1214 | - case c.ContextWindow < 0: | ||
| 1215 | - return errors.New("contextWindow must be >= 0") | ||
| 1216 | - case c.ToolFormat != "" && c.ToolFormat != ToolFormatOpenAI && c.ToolFormat != ToolFormatXML: | ||
| 1217 | - return fmt.Errorf("toolFormat must be %s or %s, not %q", ToolFormatOpenAI, ToolFormatXML, c.ToolFormat) | ||
| 1218 | - case c.FirstTokenTimeout < 0: | ||
| 1219 | - return errors.New("firstTokenTimeout must be >= 0") | ||
| 1220 | - case c.BashTimeout <= 0: | ||
| 1221 | - return errors.New("bashTimeout must be > 0") | ||
| 1222 | - case c.BashBackground != BashBackgroundReport && c.BashBackground != BashBackgroundStop && c.BashBackground != BashBackgroundAllow: | ||
| 1223 | - return fmt.Errorf("bashBackground must be %q, %q or %q, not %q", BashBackgroundReport, BashBackgroundStop, BashBackgroundAllow, c.BashBackground) | ||
| 1224 | - case c.MaxOutput <= 0: | ||
| 1225 | - return errors.New("maxOutput must be > 0") | ||
| 1226 | - case c.MaxTurns <= 0: | ||
| 1227 | - return errors.New("maxTurns must be > 0") | ||
| 1228 | - case c.PreviewLines < 0: | ||
| 1229 | - return errors.New("previewLines must be >= 0") | ||
| 1230 | - case c.Context.Threshold < 1 || c.Context.Threshold > 100: | ||
| 1231 | - return errors.New("context.threshold must be between 1 and 100") | ||
| 1232 | - case c.Context.KeepLastTurns < 1: | ||
| 1233 | - return errors.New("context.keepLastTurns must be >= 1") | ||
| 1234 | - case c.Context.SummaryMaxTokens <= 0: | ||
| 1235 | - return errors.New("context.summaryMaxTokens must be > 0") | ||
| 1236 | - case c.Context.MaxMessages < 0: | ||
| 1237 | - return errors.New("context.maxMessages must be >= 0") | ||
| 1238 | - case c.Sessions.Dir == "": | ||
| 1239 | - return errors.New("sessions.dir must not be empty") | ||
| 1240 | - case c.Trace.Enabled && strings.TrimSpace(c.Trace.Dir) == "": | ||
| 1241 | - return errors.New("trace.dir must not be empty") | ||
| 1242 | - case c.AgentsMd.Enabled && strings.TrimSpace(c.AgentsMd.Path) == "": | ||
| 1243 | - return errors.New("agentsMd.path must not be empty") | ||
| 1244 | - case c.AgentsMd.MaxChars < 0: | ||
| 1245 | - return errors.New("agentsMd.maxChars must be >= 0") | ||
| 1246 | - case strings.TrimSpace(c.ACP.Name) == "": | ||
| 1247 | - return errors.New("acp.name must not be empty") | ||
| 1248 | - case c.Permissions != "" && c.Permissions != PermissionsAuto && c.Permissions != PermissionsAsk: | ||
| 1249 | - return errors.New(`permissions must be "auto" or "ask"`) | ||
| 1250 | - } | ||
| 1251 | - // `enabled: true` with no window and no maxMessages would never trigger — | ||
| 1252 | - // but the window may still come from the server's probe at start-up, so | ||
| 1253 | - // that case is a warning in main, not an error here. | ||
| 1254 | - if err := c.MCP.Validate(); err != nil { | ||
| 1255 | - return err | ||
| 1256 | - } | ||
| 1257 | - if err := c.ACP.ClientTools.validate(); err != nil { | ||
| 1258 | - return err | ||
| 1259 | - } | ||
| 1260 | - if err := c.validateAgents(); err != nil { | ||
| 1261 | - return err | ||
| 1262 | - } | ||
| 1263 | - if err := c.Hooks.validate(); err != nil { | ||
| 1264 | - return err | ||
| 1265 | - } | ||
| 1266 | - if err := c.validateTools(); err != nil { | ||
| 1267 | - return err | ||
| 1268 | - } | ||
| 1269 | - if err := validateMetaPrompts(c.MetaPrompts); err != nil { | ||
| 1270 | - return err | ||
| 1271 | - } | ||
| 1272 | - if err := c.DocSearch.validate(); err != nil { | ||
| 1273 | - return err | ||
| 1274 | - } | ||
| 1275 | - if err := c.validateDecision(); err != nil { | ||
| 1276 | - return err | ||
| 1277 | - } | ||
| 1278 | - if err := c.SkillRouting.validate(); err != nil { | ||
| 1279 | - return err | ||
| 1280 | - } | ||
| 1281 | - if err := c.ToolRouting.validate(); err != nil { | ||
| 1282 | - return err | ||
| 1283 | - } | ||
| 1284 | - if err := c.DecisionContext.validate(c.Decision != nil); err != nil { | ||
| 1285 | - return err | ||
| 1286 | - } | ||
| 1287 | - if err := c.DecisionTool.validate(c.Decision != nil); err != nil { | ||
| 1288 | - return err | ||
| 1289 | - } | ||
| 1290 | - if err := c.DecisionResult.validate(c.Decision != nil); err != nil { | ||
| 1291 | - return err | ||
| 1292 | - } | ||
| 1293 | - return c.LSP.validate() | ||
| 1294 | -} | ||
modified
internal/config/decision.go +47 -41 | @@ -79,29 +79,36 @@ func (d *DecisionConfig) UnmarshalYAML(node *yaml.Node) error { | ||
| 79 | 79 | return fmt.Errorf("line %d: decision must be a mapping (timeout, display, questions)", node.Line) |
| 80 | 80 | } |
| 81 | 81 | for i := 0; i+1 < len(node.Content); i += 2 { |
| 82 | - key, value := node.Content[i], node.Content[i+1] | |
| 83 | - switch key.Value { | |
| 84 | - case "timeout": | |
| 85 | - if err := value.Decode(&d.Timeout); err != nil { | |
| 86 | - return fmt.Errorf("line %d: decision.timeout: %w", value.Line, err) | |
| 87 | - } | |
| 88 | - case "display": | |
| 89 | - if err := value.Decode(&d.Display); err != nil { | |
| 90 | - return fmt.Errorf("line %d: decision.display: %w", value.Line, err) | |
| 91 | - } | |
| 92 | - case "questions": | |
| 93 | - questions, err := decodeQuestions(value) | |
| 94 | - if err != nil { | |
| 95 | - return err | |
| 96 | - } | |
| 97 | - d.Questions = questions | |
| 98 | - default: | |
| 99 | - return fmt.Errorf("line %d: decision: unknown key %q (timeout, display, questions)", key.Line, key.Value) | |
| 82 | + if err := d.decodeKey(node.Content[i], node.Content[i+1]); err != nil { | |
| 83 | + return err | |
| 100 | 84 | } |
| 101 | 85 | } |
| 102 | 86 | return nil |
| 103 | 87 | } |
| 104 | 88 | |
| 89 | +// decodeKey reads one key of the block. | |
| 90 | +func (d *DecisionConfig) decodeKey(key, value *yaml.Node) error { | |
| 91 | + switch key.Value { | |
| 92 | + case "timeout": | |
| 93 | + return lineError(value, "decision.timeout", value.Decode(&d.Timeout)) | |
| 94 | + case "display": | |
| 95 | + return lineError(value, "decision.display", value.Decode(&d.Display)) | |
| 96 | + case "questions": | |
| 97 | + questions, err := decodeQuestions(value) | |
| 98 | + d.Questions = questions | |
| 99 | + return err | |
| 100 | + } | |
| 101 | + return fmt.Errorf("line %d: decision: unknown key %q (timeout, display, questions)", key.Line, key.Value) | |
| 102 | +} | |
| 103 | + | |
| 104 | +// lineError puts the node's line and key before err; nil stays nil. | |
| 105 | +func lineError(node *yaml.Node, key string, err error) error { | |
| 106 | + if err == nil { | |
| 107 | + return nil | |
| 108 | + } | |
| 109 | + return fmt.Errorf("line %d: %s: %w", node.Line, key, err) | |
| 110 | +} | |
| 111 | + | |
| 105 | 112 | func decodeQuestions(node *yaml.Node) ([]decide.Question, error) { |
| 106 | 113 | if node.Kind != yaml.MappingNode { |
| 107 | 114 | return nil, fmt.Errorf("line %d: decision.questions must be a mapping of name → question", node.Line) |
| @@ -170,10 +177,19 @@ func (c Config) validateDecision() error { | ||
| 170 | 177 | if d == nil { |
| 171 | 178 | return nil |
| 172 | 179 | } |
| 173 | - if len(d.Questions) == 0 { | |
| 174 | - return errors.New("decision.questions: at least one question is needed") | |
| 175 | - } | |
| 176 | - for _, q := range d.Questions { | |
| 180 | + return firstError( | |
| 181 | + problem(len(d.Questions) == 0, "decision.questions: at least one question is needed"), | |
| 182 | + criteriaError(d.Questions), | |
| 183 | + problem(d.Timeout < 0, "decision.timeout must be >= 0"), | |
| 184 | + prefixed("decision", decide.Supports(c.Provider)), | |
| 185 | + problem(strings.TrimSpace(c.StructuredOutput) != "", "decision and structuredOutput cannot be used together: a decision model writes no text"), | |
| 186 | + ) | |
| 187 | +} | |
| 188 | + | |
| 189 | +// criteriaError reports the first question whose criteria do not fit its | |
| 190 | +// type: a mapping for a choice, a list for a score, nothing for a noul. | |
| 191 | +func criteriaError(questions []decide.Question) error { | |
| 192 | + for _, q := range questions { | |
| 177 | 193 | if q.Type == decide.Choice && len(q.Levels) > 0 || q.Type == decide.Score && len(q.Options) > 0 { |
| 178 | 194 | return fmt.Errorf("decision.questions.%s: a choice takes criteria as a mapping, a score as a list", q.Name) |
| 179 | 195 | } |
| @@ -181,15 +197,6 @@ func (c Config) validateDecision() error { | ||
| 181 | 197 | return fmt.Errorf("decision.questions.%s: a noul question takes no criteria", q.Name) |
| 182 | 198 | } |
| 183 | 199 | } |
| 184 | - if d.Timeout < 0 { | |
| 185 | - return errors.New("decision.timeout must be >= 0") | |
| 186 | - } | |
| 187 | - if err := decide.Supports(c.Provider); err != nil { | |
| 188 | - return fmt.Errorf("decision: %w", err) | |
| 189 | - } | |
| 190 | - if strings.TrimSpace(c.StructuredOutput) != "" { | |
| 191 | - return errors.New("decision and structuredOutput cannot be used together: a decision model writes no text") | |
| 192 | - } | |
| 193 | 200 | return nil |
| 194 | 201 | } |
| 195 | 202 | |
| @@ -313,17 +320,16 @@ func ReadDecider(path string) (Config, error) { | ||
| 313 | 320 | return Config{}, err |
| 314 | 321 | } |
| 315 | 322 | var d Config |
| 316 | - if err := yaml.Unmarshal(data, &d); err != nil { | |
| 317 | - return Config{}, fmt.Errorf("%s: %w", path, err) | |
| 318 | - } | |
| 319 | - if d.Provider == "" { | |
| 320 | - return Config{}, fmt.Errorf("%s: provider must be set (llamacpp, llmman, ollama or openai)", path) | |
| 323 | + err = yaml.Unmarshal(data, &d) | |
| 324 | + if err == nil { | |
| 325 | + err = firstError( | |
| 326 | + problem(d.Provider == "", "provider must be set (llamacpp, llmman, ollama or openai)"), | |
| 327 | + problem(strings.TrimSpace(d.Model) == "", "model must not be empty"), | |
| 328 | + decide.Supports(d.Provider), | |
| 329 | + ) | |
| 321 | 330 | } |
| 322 | - if strings.TrimSpace(d.Model) == "" { | |
| 323 | - return Config{}, fmt.Errorf("%s: model must not be empty", path) | |
| 324 | - } | |
| 325 | - if err := decide.Supports(d.Provider); err != nil { | |
| 326 | - return Config{}, fmt.Errorf("%s: %w", path, err) | |
| 331 | + if err != nil { | |
| 332 | + return Config{}, prefixed(path, err) | |
| 327 | 333 | } |
| 328 | 334 | return d, nil |
| 329 | 335 | } |
| @@ -79,29 +79,36 @@ func (d *DecisionConfig) UnmarshalYAML(node *yaml.Node) error { | |||
| 79 | return fmt.Errorf("line %d: decision must be a mapping (timeout, display, questions)", node.Line) | 79 | return fmt.Errorf("line %d: decision must be a mapping (timeout, display, questions)", node.Line) |
| 80 | } | 80 | } |
| 81 | for i := 0; i+1 < len(node.Content); i += 2 { | 81 | for i := 0; i+1 < len(node.Content); i += 2 { |
| 82 | - key, value := node.Content[i], node.Content[i+1] | 82 | + if err := d.decodeKey(node.Content[i], node.Content[i+1]); err != nil { |
| 83 | - switch key.Value { | 83 | + return err |
| 84 | - case "timeout": | ||
| 85 | - if err := value.Decode(&d.Timeout); err != nil { | ||
| 86 | - return fmt.Errorf("line %d: decision.timeout: %w", value.Line, err) | ||
| 87 | - } | ||
| 88 | - case "display": | ||
| 89 | - if err := value.Decode(&d.Display); err != nil { | ||
| 90 | - return fmt.Errorf("line %d: decision.display: %w", value.Line, err) | ||
| 91 | - } | ||
| 92 | - case "questions": | ||
| 93 | - questions, err := decodeQuestions(value) | ||
| 94 | - if err != nil { | ||
| 95 | - return err | ||
| 96 | - } | ||
| 97 | - d.Questions = questions | ||
| 98 | - default: | ||
| 99 | - return fmt.Errorf("line %d: decision: unknown key %q (timeout, display, questions)", key.Line, key.Value) | ||
| 100 | } | 84 | } |
| 101 | } | 85 | } |
| 102 | return nil | 86 | return nil |
| 103 | } | 87 | } |
| 104 | 88 | ||
| 89 | +// decodeKey reads one key of the block. | ||
| 90 | +func (d *DecisionConfig) decodeKey(key, value *yaml.Node) error { | ||
| 91 | + switch key.Value { | ||
| 92 | + case "timeout": | ||
| 93 | + return lineError(value, "decision.timeout", value.Decode(&d.Timeout)) | ||
| 94 | + case "display": | ||
| 95 | + return lineError(value, "decision.display", value.Decode(&d.Display)) | ||
| 96 | + case "questions": | ||
| 97 | + questions, err := decodeQuestions(value) | ||
| 98 | + d.Questions = questions | ||
| 99 | + return err | ||
| 100 | + } | ||
| 101 | + return fmt.Errorf("line %d: decision: unknown key %q (timeout, display, questions)", key.Line, key.Value) | ||
| 102 | +} | ||
| 103 | + | ||
| 104 | +// lineError puts the node's line and key before err; nil stays nil. | ||
| 105 | +func lineError(node *yaml.Node, key string, err error) error { | ||
| 106 | + if err == nil { | ||
| 107 | + return nil | ||
| 108 | + } | ||
| 109 | + return fmt.Errorf("line %d: %s: %w", node.Line, key, err) | ||
| 110 | +} | ||
| 111 | + | ||
| 105 | func decodeQuestions(node *yaml.Node) ([]decide.Question, error) { | 112 | func decodeQuestions(node *yaml.Node) ([]decide.Question, error) { |
| 106 | if node.Kind != yaml.MappingNode { | 113 | if node.Kind != yaml.MappingNode { |
| 107 | return nil, fmt.Errorf("line %d: decision.questions must be a mapping of name → question", node.Line) | 114 | return nil, fmt.Errorf("line %d: decision.questions must be a mapping of name → question", node.Line) |
| @@ -170,10 +177,19 @@ func (c Config) validateDecision() error { | |||
| 170 | if d == nil { | 177 | if d == nil { |
| 171 | return nil | 178 | return nil |
| 172 | } | 179 | } |
| 173 | - if len(d.Questions) == 0 { | 180 | + return firstError( |
| 174 | - return errors.New("decision.questions: at least one question is needed") | 181 | + problem(len(d.Questions) == 0, "decision.questions: at least one question is needed"), |
| 175 | - } | 182 | + criteriaError(d.Questions), |
| 176 | - for _, q := range d.Questions { | 183 | + problem(d.Timeout < 0, "decision.timeout must be >= 0"), |
| 184 | + prefixed("decision", decide.Supports(c.Provider)), | ||
| 185 | + problem(strings.TrimSpace(c.StructuredOutput) != "", "decision and structuredOutput cannot be used together: a decision model writes no text"), | ||
| 186 | + ) | ||
| 187 | +} | ||
| 188 | + | ||
| 189 | +// criteriaError reports the first question whose criteria do not fit its | ||
| 190 | +// type: a mapping for a choice, a list for a score, nothing for a noul. | ||
| 191 | +func criteriaError(questions []decide.Question) error { | ||
| 192 | + for _, q := range questions { | ||
| 177 | if q.Type == decide.Choice && len(q.Levels) > 0 || q.Type == decide.Score && len(q.Options) > 0 { | 193 | if q.Type == decide.Choice && len(q.Levels) > 0 || q.Type == decide.Score && len(q.Options) > 0 { |
| 178 | return fmt.Errorf("decision.questions.%s: a choice takes criteria as a mapping, a score as a list", q.Name) | 194 | return fmt.Errorf("decision.questions.%s: a choice takes criteria as a mapping, a score as a list", q.Name) |
| 179 | } | 195 | } |
| @@ -181,15 +197,6 @@ func (c Config) validateDecision() error { | |||
| 181 | return fmt.Errorf("decision.questions.%s: a noul question takes no criteria", q.Name) | 197 | return fmt.Errorf("decision.questions.%s: a noul question takes no criteria", q.Name) |
| 182 | } | 198 | } |
| 183 | } | 199 | } |
| 184 | - if d.Timeout < 0 { | ||
| 185 | - return errors.New("decision.timeout must be >= 0") | ||
| 186 | - } | ||
| 187 | - if err := decide.Supports(c.Provider); err != nil { | ||
| 188 | - return fmt.Errorf("decision: %w", err) | ||
| 189 | - } | ||
| 190 | - if strings.TrimSpace(c.StructuredOutput) != "" { | ||
| 191 | - return errors.New("decision and structuredOutput cannot be used together: a decision model writes no text") | ||
| 192 | - } | ||
| 193 | return nil | 200 | return nil |
| 194 | } | 201 | } |
| 195 | 202 | ||
| @@ -313,17 +320,16 @@ func ReadDecider(path string) (Config, error) { | |||
| 313 | return Config{}, err | 320 | return Config{}, err |
| 314 | } | 321 | } |
| 315 | var d Config | 322 | var d Config |
| 316 | - if err := yaml.Unmarshal(data, &d); err != nil { | 323 | + err = yaml.Unmarshal(data, &d) |
| 317 | - return Config{}, fmt.Errorf("%s: %w", path, err) | 324 | + if err == nil { |
| 318 | - } | 325 | + err = firstError( |
| 319 | - if d.Provider == "" { | 326 | + problem(d.Provider == "", "provider must be set (llamacpp, llmman, ollama or openai)"), |
| 320 | - return Config{}, fmt.Errorf("%s: provider must be set (llamacpp, llmman, ollama or openai)", path) | 327 | + problem(strings.TrimSpace(d.Model) == "", "model must not be empty"), |
| 328 | + decide.Supports(d.Provider), | ||
| 329 | + ) | ||
| 321 | } | 330 | } |
| 322 | - if strings.TrimSpace(d.Model) == "" { | 331 | + if err != nil { |
| 323 | - return Config{}, fmt.Errorf("%s: model must not be empty", path) | 332 | + return Config{}, prefixed(path, err) |
| 324 | - } | ||
| 325 | - if err := decide.Supports(d.Provider); err != nil { | ||
| 326 | - return Config{}, fmt.Errorf("%s: %w", path, err) | ||
| 327 | } | 333 | } |
| 328 | return d, nil | 334 | return d, nil |
| 329 | } | 335 | } |
modified
internal/config/docsearch.go +14 -16 | @@ -1,9 +1,9 @@ | ||
| 1 | 1 | package config |
| 2 | 2 | |
| 3 | 3 | import ( |
| 4 | - "errors" | |
| 5 | 4 | "fmt" |
| 6 | 5 | "path/filepath" |
| 6 | + "slices" | |
| 7 | 7 | "strconv" |
| 8 | 8 | "strings" |
| 9 | 9 | |
| @@ -96,25 +96,23 @@ func (d DocSearchConfig) validate() error { | ||
| 96 | 96 | if !d.SearchDocs && !d.AutoSearchDocs { |
| 97 | 97 | return nil |
| 98 | 98 | } |
| 99 | - if len(d.Dirs) == 0 { | |
| 100 | - return errors.New("docSearch.dirs must name at least one folder") | |
| 101 | - } | |
| 102 | - for _, dir := range d.Dirs { | |
| 103 | - if strings.TrimSpace(dir) == "" { | |
| 104 | - return errors.New("docSearch.dirs: a folder must not be empty") | |
| 105 | - } | |
| 106 | - } | |
| 107 | - if len(d.Extensions) == 0 { | |
| 108 | - return errors.New("docSearch.extensions must name at least one extension") | |
| 109 | - } | |
| 110 | - for _, e := range d.Extensions { | |
| 99 | + return firstError( | |
| 100 | + problem(len(d.Dirs) == 0, "docSearch.dirs must name at least one folder"), | |
| 101 | + problem(slices.ContainsFunc(d.Dirs, func(dir string) bool { return strings.TrimSpace(dir) == "" }), "docSearch.dirs: a folder must not be empty"), | |
| 102 | + problem(len(d.Extensions) == 0, "docSearch.extensions must name at least one extension"), | |
| 103 | + extensionsError(d.Extensions), | |
| 104 | + problem(d.MaxFileSize <= 0, "docSearch.maxFileSize must be > 0"), | |
| 105 | + ) | |
| 106 | +} | |
| 107 | + | |
| 108 | +// extensionsError reports the first extension that does not start with a | |
| 109 | +// dot. | |
| 110 | +func extensionsError(extensions []string) error { | |
| 111 | + for _, e := range extensions { | |
| 111 | 112 | if !strings.HasPrefix(e, ".") || len(e) < 2 { |
| 112 | 113 | return fmt.Errorf("docSearch.extensions: %q must start with a dot, like \".md\"", e) |
| 113 | 114 | } |
| 114 | 115 | } |
| 115 | - if d.MaxFileSize <= 0 { | |
| 116 | - return errors.New("docSearch.maxFileSize must be > 0") | |
| 117 | - } | |
| 118 | 116 | return nil |
| 119 | 117 | } |
| 120 | 118 | |
| @@ -1,9 +1,9 @@ | |||
| 1 | package config | 1 | package config |
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | - "errors" | ||
| 5 | "fmt" | 4 | "fmt" |
| 6 | "path/filepath" | 5 | "path/filepath" |
| 6 | + "slices" | ||
| 7 | "strconv" | 7 | "strconv" |
| 8 | "strings" | 8 | "strings" |
| 9 | 9 | ||
| @@ -96,25 +96,23 @@ func (d DocSearchConfig) validate() error { | |||
| 96 | if !d.SearchDocs && !d.AutoSearchDocs { | 96 | if !d.SearchDocs && !d.AutoSearchDocs { |
| 97 | return nil | 97 | return nil |
| 98 | } | 98 | } |
| 99 | - if len(d.Dirs) == 0 { | 99 | + return firstError( |
| 100 | - return errors.New("docSearch.dirs must name at least one folder") | 100 | + problem(len(d.Dirs) == 0, "docSearch.dirs must name at least one folder"), |
| 101 | - } | 101 | + problem(slices.ContainsFunc(d.Dirs, func(dir string) bool { return strings.TrimSpace(dir) == "" }), "docSearch.dirs: a folder must not be empty"), |
| 102 | - for _, dir := range d.Dirs { | 102 | + problem(len(d.Extensions) == 0, "docSearch.extensions must name at least one extension"), |
| 103 | - if strings.TrimSpace(dir) == "" { | 103 | + extensionsError(d.Extensions), |
| 104 | - return errors.New("docSearch.dirs: a folder must not be empty") | 104 | + problem(d.MaxFileSize <= 0, "docSearch.maxFileSize must be > 0"), |
| 105 | - } | 105 | + ) |
| 106 | - } | 106 | +} |
| 107 | - if len(d.Extensions) == 0 { | 107 | + |
| 108 | - return errors.New("docSearch.extensions must name at least one extension") | 108 | +// extensionsError reports the first extension that does not start with a |
| 109 | - } | 109 | +// dot. |
| 110 | - for _, e := range d.Extensions { | 110 | +func extensionsError(extensions []string) error { |
| 111 | + for _, e := range extensions { | ||
| 111 | if !strings.HasPrefix(e, ".") || len(e) < 2 { | 112 | if !strings.HasPrefix(e, ".") || len(e) < 2 { |
| 112 | return fmt.Errorf("docSearch.extensions: %q must start with a dot, like \".md\"", e) | 113 | return fmt.Errorf("docSearch.extensions: %q must start with a dot, like \".md\"", e) |
| 113 | } | 114 | } |
| 114 | } | 115 | } |
| 115 | - if d.MaxFileSize <= 0 { | ||
| 116 | - return errors.New("docSearch.maxFileSize must be > 0") | ||
| 117 | - } | ||
| 118 | return nil | 116 | return nil |
| 119 | } | 117 | } |
| 120 | 118 | ||
modified
internal/config/hooks.go +22 -32 | @@ -5,6 +5,7 @@ import ( | ||
| 5 | 5 | "fmt" |
| 6 | 6 | "path/filepath" |
| 7 | 7 | "regexp" |
| 8 | + "slices" | |
| 8 | 9 | "strings" |
| 9 | 10 | "time" |
| 10 | 11 | |
| @@ -126,21 +127,15 @@ func (h *HooksConfig) UnmarshalYAML(node *yaml.Node) error { | ||
| 126 | 127 | // slot returns the list that holds an event's hooks, nil for an unknown |
| 127 | 128 | // event. |
| 128 | 129 | func (h *HooksConfig) slot(event string) *[]Hook { |
| 129 | - switch event { | |
| 130 | - case HookPreGeneration: | |
| 131 | - return &h.PreGeneration | |
| 132 | - case HookPreToolCall: | |
| 133 | - return &h.PreToolCall | |
| 134 | - case HookPostToolCall: | |
| 135 | - return &h.PostToolCall | |
| 136 | - case HookPostGeneration: | |
| 137 | - return &h.PostGeneration | |
| 138 | - case HookPreAsk: | |
| 139 | - return &h.PreAsk | |
| 140 | - case HookPostAsk: | |
| 141 | - return &h.PostAsk | |
| 130 | + slots := map[string]*[]Hook{ | |
| 131 | + HookPreGeneration: &h.PreGeneration, | |
| 132 | + HookPreToolCall: &h.PreToolCall, | |
| 133 | + HookPostToolCall: &h.PostToolCall, | |
| 134 | + HookPostGeneration: &h.PostGeneration, | |
| 135 | + HookPreAsk: &h.PreAsk, | |
| 136 | + HookPostAsk: &h.PostAsk, | |
| 142 | 137 | } |
| 143 | - return nil | |
| 138 | + return slots[event] | |
| 144 | 139 | } |
| 145 | 140 | |
| 146 | 141 | // IsWasm reports whether the hook runs a WebAssembly module, not a command. |
| @@ -235,24 +230,19 @@ func (h HooksConfig) validate() error { | ||
| 235 | 230 | |
| 236 | 231 | func (h Hook) validate(event string) error { |
| 237 | 232 | named := event != HookPreGeneration && event != HookPostGeneration |
| 238 | - if err := h.validateRunnable(); err != nil { | |
| 239 | - return err | |
| 240 | - } | |
| 241 | - switch { | |
| 242 | - case h.Matcher != "" && !named: | |
| 243 | - return fmt.Errorf("matcher is only for %s, %s, %s and %s", HookPreToolCall, HookPostToolCall, HookPreAsk, HookPostAsk) | |
| 244 | - case h.Timeout < 0: | |
| 245 | - return errors.New("timeout must be >= 0") | |
| 246 | - } | |
| 247 | - switch h.OnError { | |
| 248 | - case "", HookOnErrorFail, HookOnErrorWarn, HookOnErrorIgnore: | |
| 249 | - default: | |
| 250 | - return fmt.Errorf("onError must be %q, %q or %q", HookOnErrorFail, HookOnErrorWarn, HookOnErrorIgnore) | |
| 251 | - } | |
| 252 | - if _, err := regexp.Compile(h.Matcher); err != nil { | |
| 253 | - return fmt.Errorf("matcher: %w", err) | |
| 254 | - } | |
| 255 | - return nil | |
| 233 | + _, matcherErr := regexp.Compile(h.Matcher) | |
| 234 | + return firstFailing( | |
| 235 | + h.validateRunnable, | |
| 236 | + func() error { | |
| 237 | + return firstError( | |
| 238 | + problem(h.Matcher != "" && !named, fmt.Sprintf("matcher is only for %s, %s, %s and %s", HookPreToolCall, HookPostToolCall, HookPreAsk, HookPostAsk)), | |
| 239 | + problem(h.Timeout < 0, "timeout must be >= 0"), | |
| 240 | + problem(!slices.Contains([]string{"", HookOnErrorFail, HookOnErrorWarn, HookOnErrorIgnore}, h.OnError), | |
| 241 | + fmt.Sprintf("onError must be %q, %q or %q", HookOnErrorFail, HookOnErrorWarn, HookOnErrorIgnore)), | |
| 242 | + prefixed("matcher", matcherErr), | |
| 243 | + ) | |
| 244 | + }, | |
| 245 | + ) | |
| 256 | 246 | } |
| 257 | 247 | |
| 258 | 248 | // resolveHooks fills what an entry may leave out (timeout, onError) and |
| @@ -5,6 +5,7 @@ import ( | |||
| 5 | "fmt" | 5 | "fmt" |
| 6 | "path/filepath" | 6 | "path/filepath" |
| 7 | "regexp" | 7 | "regexp" |
| 8 | + "slices" | ||
| 8 | "strings" | 9 | "strings" |
| 9 | "time" | 10 | "time" |
| 10 | 11 | ||
| @@ -126,21 +127,15 @@ func (h *HooksConfig) UnmarshalYAML(node *yaml.Node) error { | |||
| 126 | // slot returns the list that holds an event's hooks, nil for an unknown | 127 | // slot returns the list that holds an event's hooks, nil for an unknown |
| 127 | // event. | 128 | // event. |
| 128 | func (h *HooksConfig) slot(event string) *[]Hook { | 129 | func (h *HooksConfig) slot(event string) *[]Hook { |
| 129 | - switch event { | 130 | + slots := map[string]*[]Hook{ |
| 130 | - case HookPreGeneration: | 131 | + HookPreGeneration: &h.PreGeneration, |
| 131 | - return &h.PreGeneration | 132 | + HookPreToolCall: &h.PreToolCall, |
| 132 | - case HookPreToolCall: | 133 | + HookPostToolCall: &h.PostToolCall, |
| 133 | - return &h.PreToolCall | 134 | + HookPostGeneration: &h.PostGeneration, |
| 134 | - case HookPostToolCall: | 135 | + HookPreAsk: &h.PreAsk, |
| 135 | - return &h.PostToolCall | 136 | + HookPostAsk: &h.PostAsk, |
| 136 | - case HookPostGeneration: | ||
| 137 | - return &h.PostGeneration | ||
| 138 | - case HookPreAsk: | ||
| 139 | - return &h.PreAsk | ||
| 140 | - case HookPostAsk: | ||
| 141 | - return &h.PostAsk | ||
| 142 | } | 137 | } |
| 143 | - return nil | 138 | + return slots[event] |
| 144 | } | 139 | } |
| 145 | 140 | ||
| 146 | // IsWasm reports whether the hook runs a WebAssembly module, not a command. | 141 | // IsWasm reports whether the hook runs a WebAssembly module, not a command. |
| @@ -235,24 +230,19 @@ func (h HooksConfig) validate() error { | |||
| 235 | 230 | ||
| 236 | func (h Hook) validate(event string) error { | 231 | func (h Hook) validate(event string) error { |
| 237 | named := event != HookPreGeneration && event != HookPostGeneration | 232 | named := event != HookPreGeneration && event != HookPostGeneration |
| 238 | - if err := h.validateRunnable(); err != nil { | 233 | + _, matcherErr := regexp.Compile(h.Matcher) |
| 239 | - return err | 234 | + return firstFailing( |
| 240 | - } | 235 | + h.validateRunnable, |
| 241 | - switch { | 236 | + func() error { |
| 242 | - case h.Matcher != "" && !named: | 237 | + return firstError( |
| 243 | - return fmt.Errorf("matcher is only for %s, %s, %s and %s", HookPreToolCall, HookPostToolCall, HookPreAsk, HookPostAsk) | 238 | + problem(h.Matcher != "" && !named, fmt.Sprintf("matcher is only for %s, %s, %s and %s", HookPreToolCall, HookPostToolCall, HookPreAsk, HookPostAsk)), |
| 244 | - case h.Timeout < 0: | 239 | + problem(h.Timeout < 0, "timeout must be >= 0"), |
| 245 | - return errors.New("timeout must be >= 0") | 240 | + problem(!slices.Contains([]string{"", HookOnErrorFail, HookOnErrorWarn, HookOnErrorIgnore}, h.OnError), |
| 246 | - } | 241 | + fmt.Sprintf("onError must be %q, %q or %q", HookOnErrorFail, HookOnErrorWarn, HookOnErrorIgnore)), |
| 247 | - switch h.OnError { | 242 | + prefixed("matcher", matcherErr), |
| 248 | - case "", HookOnErrorFail, HookOnErrorWarn, HookOnErrorIgnore: | 243 | + ) |
| 249 | - default: | 244 | + }, |
| 250 | - return fmt.Errorf("onError must be %q, %q or %q", HookOnErrorFail, HookOnErrorWarn, HookOnErrorIgnore) | 245 | + ) |
| 251 | - } | ||
| 252 | - if _, err := regexp.Compile(h.Matcher); err != nil { | ||
| 253 | - return fmt.Errorf("matcher: %w", err) | ||
| 254 | - } | ||
| 255 | - return nil | ||
| 256 | } | 246 | } |
| 257 | 247 | ||
| 258 | // resolveHooks fills what an entry may leave out (timeout, onError) and | 248 | // resolveHooks fills what an entry may leave out (timeout, onError) and |
modified
internal/config/promptvars.go +24 -16 | @@ -106,22 +106,8 @@ func promptEnvNames(text string) []string { | ||
| 106 | 106 | // {{config.NAME}} the section does not have, a {{config.…}} inside a value, |
| 107 | 107 | // and a key that is not a name. |
| 108 | 108 | func validatePrompt(system string, vars map[string]string) error { |
| 109 | - check := func(where, text string) error { | |
| 110 | - for _, name := range promptEnvNames(text) { | |
| 111 | - if secretName.MatchString(name) { | |
| 112 | - return fmt.Errorf("%s: {{env.%s}} looks like a secret — the prompt is sent to the model and kept in the sessions and traces; refused", where, name) | |
| 113 | - } | |
| 114 | - } | |
| 115 | - return nil | |
| 116 | - } | |
| 117 | 109 | for _, key := range sortedKeys(vars) { |
| 118 | - if !variableName.MatchString(key) { | |
| 119 | - return fmt.Errorf("variables: %q is not a valid name (letters, digits, _ and -, not starting with a digit)", key) | |
| 120 | - } | |
| 121 | - if names := promptConfigNames(vars[key]); len(names) > 0 { | |
| 122 | - return fmt.Errorf("variables.%s: {{config.%s}} — a value cannot use another variable of the section", key, names[0]) | |
| 123 | - } | |
| 124 | - if err := check("variables."+key, vars[key]); err != nil { | |
| 110 | + if err := variableError(key, vars[key]); err != nil { | |
| 125 | 111 | return err |
| 126 | 112 | } |
| 127 | 113 | } |
| @@ -130,7 +116,29 @@ func validatePrompt(system string, vars map[string]string) error { | ||
| 130 | 116 | return fmt.Errorf("system: {{config.%s}} — the variables: section has no %q", name, name) |
| 131 | 117 | } |
| 132 | 118 | } |
| 133 | - return check("system", system) | |
| 119 | + return secretEnvError("system", system) | |
| 120 | +} | |
| 121 | + | |
| 122 | +// variableError checks one variable of the variables: section: its name, | |
| 123 | +// no {{config.…}} in its value, no secret {{env.…}}. | |
| 124 | +func variableError(key, value string) error { | |
| 125 | + if !variableName.MatchString(key) { | |
| 126 | + return fmt.Errorf("variables: %q is not a valid name (letters, digits, _ and -, not starting with a digit)", key) | |
| 127 | + } | |
| 128 | + if names := promptConfigNames(value); len(names) > 0 { | |
| 129 | + return fmt.Errorf("variables.%s: {{config.%s}} — a value cannot use another variable of the section", key, names[0]) | |
| 130 | + } | |
| 131 | + return secretEnvError("variables."+key, value) | |
| 132 | +} | |
| 133 | + | |
| 134 | +// secretEnvError refuses an {{env.…}} whose name looks like a secret. | |
| 135 | +func secretEnvError(where, text string) error { | |
| 136 | + for _, name := range promptEnvNames(text) { | |
| 137 | + if secretName.MatchString(name) { | |
| 138 | + return fmt.Errorf("%s: {{env.%s}} looks like a secret — the prompt is sent to the model and kept in the sessions and traces; refused", where, name) | |
| 139 | + } | |
| 140 | + } | |
| 141 | + return nil | |
| 134 | 142 | } |
| 135 | 143 | |
| 136 | 144 | // promptConfigNames lists the {{config.NAME}} of text that are not escaped. |
| @@ -106,22 +106,8 @@ func promptEnvNames(text string) []string { | |||
| 106 | // {{config.NAME}} the section does not have, a {{config.…}} inside a value, | 106 | // {{config.NAME}} the section does not have, a {{config.…}} inside a value, |
| 107 | // and a key that is not a name. | 107 | // and a key that is not a name. |
| 108 | func validatePrompt(system string, vars map[string]string) error { | 108 | func validatePrompt(system string, vars map[string]string) error { |
| 109 | - check := func(where, text string) error { | ||
| 110 | - for _, name := range promptEnvNames(text) { | ||
| 111 | - if secretName.MatchString(name) { | ||
| 112 | - return fmt.Errorf("%s: {{env.%s}} looks like a secret — the prompt is sent to the model and kept in the sessions and traces; refused", where, name) | ||
| 113 | - } | ||
| 114 | - } | ||
| 115 | - return nil | ||
| 116 | - } | ||
| 117 | for _, key := range sortedKeys(vars) { | 109 | for _, key := range sortedKeys(vars) { |
| 118 | - if !variableName.MatchString(key) { | 110 | + if err := variableError(key, vars[key]); err != nil { |
| 119 | - return fmt.Errorf("variables: %q is not a valid name (letters, digits, _ and -, not starting with a digit)", key) | ||
| 120 | - } | ||
| 121 | - if names := promptConfigNames(vars[key]); len(names) > 0 { | ||
| 122 | - return fmt.Errorf("variables.%s: {{config.%s}} — a value cannot use another variable of the section", key, names[0]) | ||
| 123 | - } | ||
| 124 | - if err := check("variables."+key, vars[key]); err != nil { | ||
| 125 | return err | 111 | return err |
| 126 | } | 112 | } |
| 127 | } | 113 | } |
| @@ -130,7 +116,29 @@ func validatePrompt(system string, vars map[string]string) error { | |||
| 130 | return fmt.Errorf("system: {{config.%s}} — the variables: section has no %q", name, name) | 116 | return fmt.Errorf("system: {{config.%s}} — the variables: section has no %q", name, name) |
| 131 | } | 117 | } |
| 132 | } | 118 | } |
| 133 | - return check("system", system) | 119 | + return secretEnvError("system", system) |
| 120 | +} | ||
| 121 | + | ||
| 122 | +// variableError checks one variable of the variables: section: its name, | ||
| 123 | +// no {{config.…}} in its value, no secret {{env.…}}. | ||
| 124 | +func variableError(key, value string) error { | ||
| 125 | + if !variableName.MatchString(key) { | ||
| 126 | + return fmt.Errorf("variables: %q is not a valid name (letters, digits, _ and -, not starting with a digit)", key) | ||
| 127 | + } | ||
| 128 | + if names := promptConfigNames(value); len(names) > 0 { | ||
| 129 | + return fmt.Errorf("variables.%s: {{config.%s}} — a value cannot use another variable of the section", key, names[0]) | ||
| 130 | + } | ||
| 131 | + return secretEnvError("variables."+key, value) | ||
| 132 | +} | ||
| 133 | + | ||
| 134 | +// secretEnvError refuses an {{env.…}} whose name looks like a secret. | ||
| 135 | +func secretEnvError(where, text string) error { | ||
| 136 | + for _, name := range promptEnvNames(text) { | ||
| 137 | + if secretName.MatchString(name) { | ||
| 138 | + return fmt.Errorf("%s: {{env.%s}} looks like a secret — the prompt is sent to the model and kept in the sessions and traces; refused", where, name) | ||
| 139 | + } | ||
| 140 | + } | ||
| 141 | + return nil | ||
| 134 | } | 142 | } |
| 135 | 143 | ||
| 136 | // promptConfigNames lists the {{config.NAME}} of text that are not escaped. | 144 | // promptConfigNames lists the {{config.NAME}} of text that are not escaped. |
added
internal/config/toolparams.go +103 -0 | new file mode 100644 | ||
| @@ -0,0 +1,103 @@ | ||
| 1 | +package config | |
| 2 | + | |
| 3 | +import ( | |
| 4 | + "fmt" | |
| 5 | + "strconv" | |
| 6 | + "strings" | |
| 7 | +) | |
| 8 | + | |
| 9 | +// Coerce turns a value — from the file, or from the model's call — into the | |
| 10 | +// parameter's type, or says why it cannot. Models write numbers and booleans | |
| 11 | +// as strings often enough ("3", "true") that those are accepted; anything | |
| 12 | +// else is refused rather than guessed. | |
| 13 | +// | |
| 14 | +// ToolParameter{Type: "integer"}.Coerce("3") // 3, nil | |
| 15 | +// ToolParameter{Type: "boolean"}.Coerce("yes") // error | |
| 16 | +func (p ToolParameter) Coerce(v any) (any, error) { | |
| 17 | + switch p.Type { | |
| 18 | + case ParamNumber: | |
| 19 | + return coerceNumber(v) | |
| 20 | + case ParamInteger: | |
| 21 | + return coerceInteger(v) | |
| 22 | + case ParamBoolean: | |
| 23 | + return coerceBoolean(v) | |
| 24 | + default: | |
| 25 | + return coerceString(v) | |
| 26 | + } | |
| 27 | +} | |
| 28 | + | |
| 29 | +func coerceString(v any) (any, error) { | |
| 30 | + switch x := v.(type) { | |
| 31 | + case string: | |
| 32 | + return x, nil | |
| 33 | + case int, int64, float64, bool: | |
| 34 | + return fmt.Sprint(x), nil | |
| 35 | + } | |
| 36 | + return nil, fmt.Errorf("%v is not a string", v) | |
| 37 | +} | |
| 38 | + | |
| 39 | +func coerceNumber(v any) (any, error) { | |
| 40 | + switch x := v.(type) { | |
| 41 | + case int: | |
| 42 | + return float64(x), nil | |
| 43 | + case int64: | |
| 44 | + return float64(x), nil | |
| 45 | + case float64: | |
| 46 | + return x, nil | |
| 47 | + case string: | |
| 48 | + if f, err := strconv.ParseFloat(strings.TrimSpace(x), 64); err == nil { | |
| 49 | + return f, nil | |
| 50 | + } | |
| 51 | + } | |
| 52 | + return nil, fmt.Errorf("%q is not a number", fmt.Sprint(v)) | |
| 53 | +} | |
| 54 | + | |
| 55 | +func coerceInteger(v any) (any, error) { | |
| 56 | + f, err := coerceNumber(v) | |
| 57 | + if err != nil { | |
| 58 | + return nil, fmt.Errorf("%q is not an integer", fmt.Sprint(v)) | |
| 59 | + } | |
| 60 | + n := f.(float64) | |
| 61 | + if n != float64(int64(n)) { | |
| 62 | + return nil, fmt.Errorf("%v is not an integer", v) | |
| 63 | + } | |
| 64 | + return int64(n), nil | |
| 65 | +} | |
| 66 | + | |
| 67 | +func coerceBoolean(v any) (any, error) { | |
| 68 | + switch x := v.(type) { | |
| 69 | + case bool: | |
| 70 | + return x, nil | |
| 71 | + case string: | |
| 72 | + switch strings.ToLower(strings.TrimSpace(x)) { | |
| 73 | + case "true": | |
| 74 | + return true, nil | |
| 75 | + case "false": | |
| 76 | + return false, nil | |
| 77 | + } | |
| 78 | + } | |
| 79 | + return nil, fmt.Errorf("%q is not a boolean (true or false)", fmt.Sprint(v)) | |
| 80 | +} | |
| 81 | + | |
| 82 | +// Allows reports whether a coerced value is accepted by the enum (always, | |
| 83 | +// without one). | |
| 84 | +func (p ToolParameter) Allows(v any) bool { | |
| 85 | + if len(p.Enum) == 0 { | |
| 86 | + return true | |
| 87 | + } | |
| 88 | + for _, e := range p.Enum { | |
| 89 | + if allowed, err := p.Coerce(e); err == nil && allowed == v { | |
| 90 | + return true | |
| 91 | + } | |
| 92 | + } | |
| 93 | + return false | |
| 94 | +} | |
| 95 | + | |
| 96 | +// EnumText lists the enum values for a message or a description. | |
| 97 | +func (p ToolParameter) EnumText() string { | |
| 98 | + values := make([]string, len(p.Enum)) | |
| 99 | + for i, e := range p.Enum { | |
| 100 | + values[i] = fmt.Sprint(e) | |
| 101 | + } | |
| 102 | + return strings.Join(values, ", ") | |
| 103 | +} | |
| new file mode 100644 | |||
| @@ -0,0 +1,103 @@ | |||
| 1 | +package config | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "fmt" | ||
| 5 | + "strconv" | ||
| 6 | + "strings" | ||
| 7 | +) | ||
| 8 | + | ||
| 9 | +// Coerce turns a value — from the file, or from the model's call — into the | ||
| 10 | +// parameter's type, or says why it cannot. Models write numbers and booleans | ||
| 11 | +// as strings often enough ("3", "true") that those are accepted; anything | ||
| 12 | +// else is refused rather than guessed. | ||
| 13 | +// | ||
| 14 | +// ToolParameter{Type: "integer"}.Coerce("3") // 3, nil | ||
| 15 | +// ToolParameter{Type: "boolean"}.Coerce("yes") // error | ||
| 16 | +func (p ToolParameter) Coerce(v any) (any, error) { | ||
| 17 | + switch p.Type { | ||
| 18 | + case ParamNumber: | ||
| 19 | + return coerceNumber(v) | ||
| 20 | + case ParamInteger: | ||
| 21 | + return coerceInteger(v) | ||
| 22 | + case ParamBoolean: | ||
| 23 | + return coerceBoolean(v) | ||
| 24 | + default: | ||
| 25 | + return coerceString(v) | ||
| 26 | + } | ||
| 27 | +} | ||
| 28 | + | ||
| 29 | +func coerceString(v any) (any, error) { | ||
| 30 | + switch x := v.(type) { | ||
| 31 | + case string: | ||
| 32 | + return x, nil | ||
| 33 | + case int, int64, float64, bool: | ||
| 34 | + return fmt.Sprint(x), nil | ||
| 35 | + } | ||
| 36 | + return nil, fmt.Errorf("%v is not a string", v) | ||
| 37 | +} | ||
| 38 | + | ||
| 39 | +func coerceNumber(v any) (any, error) { | ||
| 40 | + switch x := v.(type) { | ||
| 41 | + case int: | ||
| 42 | + return float64(x), nil | ||
| 43 | + case int64: | ||
| 44 | + return float64(x), nil | ||
| 45 | + case float64: | ||
| 46 | + return x, nil | ||
| 47 | + case string: | ||
| 48 | + if f, err := strconv.ParseFloat(strings.TrimSpace(x), 64); err == nil { | ||
| 49 | + return f, nil | ||
| 50 | + } | ||
| 51 | + } | ||
| 52 | + return nil, fmt.Errorf("%q is not a number", fmt.Sprint(v)) | ||
| 53 | +} | ||
| 54 | + | ||
| 55 | +func coerceInteger(v any) (any, error) { | ||
| 56 | + f, err := coerceNumber(v) | ||
| 57 | + if err != nil { | ||
| 58 | + return nil, fmt.Errorf("%q is not an integer", fmt.Sprint(v)) | ||
| 59 | + } | ||
| 60 | + n := f.(float64) | ||
| 61 | + if n != float64(int64(n)) { | ||
| 62 | + return nil, fmt.Errorf("%v is not an integer", v) | ||
| 63 | + } | ||
| 64 | + return int64(n), nil | ||
| 65 | +} | ||
| 66 | + | ||
| 67 | +func coerceBoolean(v any) (any, error) { | ||
| 68 | + switch x := v.(type) { | ||
| 69 | + case bool: | ||
| 70 | + return x, nil | ||
| 71 | + case string: | ||
| 72 | + switch strings.ToLower(strings.TrimSpace(x)) { | ||
| 73 | + case "true": | ||
| 74 | + return true, nil | ||
| 75 | + case "false": | ||
| 76 | + return false, nil | ||
| 77 | + } | ||
| 78 | + } | ||
| 79 | + return nil, fmt.Errorf("%q is not a boolean (true or false)", fmt.Sprint(v)) | ||
| 80 | +} | ||
| 81 | + | ||
| 82 | +// Allows reports whether a coerced value is accepted by the enum (always, | ||
| 83 | +// without one). | ||
| 84 | +func (p ToolParameter) Allows(v any) bool { | ||
| 85 | + if len(p.Enum) == 0 { | ||
| 86 | + return true | ||
| 87 | + } | ||
| 88 | + for _, e := range p.Enum { | ||
| 89 | + if allowed, err := p.Coerce(e); err == nil && allowed == v { | ||
| 90 | + return true | ||
| 91 | + } | ||
| 92 | + } | ||
| 93 | + return false | ||
| 94 | +} | ||
| 95 | + | ||
| 96 | +// EnumText lists the enum values for a message or a description. | ||
| 97 | +func (p ToolParameter) EnumText() string { | ||
| 98 | + values := make([]string, len(p.Enum)) | ||
| 99 | + for i, e := range p.Enum { | ||
| 100 | + values[i] = fmt.Sprint(e) | ||
| 101 | + } | ||
| 102 | + return strings.Join(values, ", ") | ||
| 103 | +} | ||
modified
internal/config/toolrouting.go +21 -20 | @@ -116,28 +116,29 @@ func (t ToolRoutingConfig) validate() error { | ||
| 116 | 116 | if !t.Enabled() { |
| 117 | 117 | return nil |
| 118 | 118 | } |
| 119 | - switch { | |
| 120 | - case t.DropBelow < 0 || t.DropBelow > 1: | |
| 121 | - return errors.New("toolRouting.dropBelow must be between 0 and 1") | |
| 122 | - case t.Timeout <= 0: | |
| 123 | - return errors.New("toolRouting.timeout must be > 0") | |
| 124 | - case len(t.Groups) == 0: | |
| 125 | - return errors.New("toolRouting.groups: at least one group is needed") | |
| 126 | - } | |
| 127 | - for _, g := range t.Groups { | |
| 128 | - if strings.TrimSpace(g.When) == "" { | |
| 129 | - return fmt.Errorf("toolRouting.groups.%s: when must not be empty", g.Name) | |
| 130 | - } | |
| 131 | - if strings.TrimSpace(g.Matcher) == "" { | |
| 132 | - return fmt.Errorf("toolRouting.groups.%s: matcher must not be empty", g.Name) | |
| 133 | - } | |
| 134 | - if _, err := regexp.Compile(g.Matcher); err != nil { | |
| 135 | - return fmt.Errorf("toolRouting.groups.%s.matcher: %w", g.Name, err) | |
| 119 | + _, statErr := os.Stat(t.Decider) | |
| 120 | + return firstError( | |
| 121 | + problem(t.DropBelow < 0 || t.DropBelow > 1, "toolRouting.dropBelow must be between 0 and 1"), | |
| 122 | + problem(t.Timeout <= 0, "toolRouting.timeout must be > 0"), | |
| 123 | + problem(len(t.Groups) == 0, "toolRouting.groups: at least one group is needed"), | |
| 124 | + groupsError(t.Groups), | |
| 125 | + prefixed("toolRouting.decider", statErr), | |
| 126 | + ) | |
| 127 | +} | |
| 128 | + | |
| 129 | +// groupsError reports the first tool group without a statement or a | |
| 130 | +// matcher, or whose matcher does not compile. | |
| 131 | +func groupsError(groups []ToolGroup) error { | |
| 132 | + for _, g := range groups { | |
| 133 | + _, matcherErr := regexp.Compile(g.Matcher) | |
| 134 | + if err := firstError( | |
| 135 | + problem(strings.TrimSpace(g.When) == "", fmt.Sprintf("toolRouting.groups.%s: when must not be empty", g.Name)), | |
| 136 | + problem(strings.TrimSpace(g.Matcher) == "", fmt.Sprintf("toolRouting.groups.%s: matcher must not be empty", g.Name)), | |
| 137 | + prefixed(fmt.Sprintf("toolRouting.groups.%s.matcher", g.Name), matcherErr), | |
| 138 | + ); err != nil { | |
| 139 | + return err | |
| 136 | 140 | } |
| 137 | 141 | } |
| 138 | - if _, err := os.Stat(t.Decider); err != nil { | |
| 139 | - return fmt.Errorf("toolRouting.decider: %w", err) | |
| 140 | - } | |
| 141 | 142 | return nil |
| 142 | 143 | } |
| 143 | 144 | |
| @@ -116,28 +116,29 @@ func (t ToolRoutingConfig) validate() error { | |||
| 116 | if !t.Enabled() { | 116 | if !t.Enabled() { |
| 117 | return nil | 117 | return nil |
| 118 | } | 118 | } |
| 119 | - switch { | 119 | + _, statErr := os.Stat(t.Decider) |
| 120 | - case t.DropBelow < 0 || t.DropBelow > 1: | 120 | + return firstError( |
| 121 | - return errors.New("toolRouting.dropBelow must be between 0 and 1") | 121 | + problem(t.DropBelow < 0 || t.DropBelow > 1, "toolRouting.dropBelow must be between 0 and 1"), |
| 122 | - case t.Timeout <= 0: | 122 | + problem(t.Timeout <= 0, "toolRouting.timeout must be > 0"), |
| 123 | - return errors.New("toolRouting.timeout must be > 0") | 123 | + problem(len(t.Groups) == 0, "toolRouting.groups: at least one group is needed"), |
| 124 | - case len(t.Groups) == 0: | 124 | + groupsError(t.Groups), |
| 125 | - return errors.New("toolRouting.groups: at least one group is needed") | 125 | + prefixed("toolRouting.decider", statErr), |
| 126 | - } | 126 | + ) |
| 127 | - for _, g := range t.Groups { | 127 | +} |
| 128 | - if strings.TrimSpace(g.When) == "" { | 128 | + |
| 129 | - return fmt.Errorf("toolRouting.groups.%s: when must not be empty", g.Name) | 129 | +// groupsError reports the first tool group without a statement or a |
| 130 | - } | 130 | +// matcher, or whose matcher does not compile. |
| 131 | - if strings.TrimSpace(g.Matcher) == "" { | 131 | +func groupsError(groups []ToolGroup) error { |
| 132 | - return fmt.Errorf("toolRouting.groups.%s: matcher must not be empty", g.Name) | 132 | + for _, g := range groups { |
| 133 | - } | 133 | + _, matcherErr := regexp.Compile(g.Matcher) |
| 134 | - if _, err := regexp.Compile(g.Matcher); err != nil { | 134 | + if err := firstError( |
| 135 | - return fmt.Errorf("toolRouting.groups.%s.matcher: %w", g.Name, err) | 135 | + problem(strings.TrimSpace(g.When) == "", fmt.Sprintf("toolRouting.groups.%s: when must not be empty", g.Name)), |
| 136 | + problem(strings.TrimSpace(g.Matcher) == "", fmt.Sprintf("toolRouting.groups.%s: matcher must not be empty", g.Name)), | ||
| 137 | + prefixed(fmt.Sprintf("toolRouting.groups.%s.matcher", g.Name), matcherErr), | ||
| 138 | + ); err != nil { | ||
| 139 | + return err | ||
| 136 | } | 140 | } |
| 137 | } | 141 | } |
| 138 | - if _, err := os.Stat(t.Decider); err != nil { | ||
| 139 | - return fmt.Errorf("toolRouting.decider: %w", err) | ||
| 140 | - } | ||
| 141 | return nil | 142 | return nil |
| 142 | } | 143 | } |
| 143 | 144 | ||
modified
internal/config/tools.go +0 -241 | @@ -34,12 +34,8 @@ package config | ||
| 34 | 34 | |
| 35 | 35 | import ( |
| 36 | 36 | "fmt" |
| 37 | - "os" | |
| 38 | 37 | "path/filepath" |
| 39 | 38 | "regexp" |
| 40 | - "slices" | |
| 41 | - "strconv" | |
| 42 | - "strings" | |
| 43 | 39 | "time" |
| 44 | 40 | |
| 45 | 41 | "gopkg.in/yaml.v3" |
| @@ -139,28 +135,6 @@ func sandboxFolders(read, write []string) []ToolFolder { | ||
| 139 | 135 | return folders |
| 140 | 136 | } |
| 141 | 137 | |
| 142 | -// validateSandbox checks what a Wasm module — a tool's or a hook's — is | |
| 143 | -// given: a module file that exists, and folders that stay inside the | |
| 144 | -// working directory, each once. The caller names the entry. | |
| 145 | -func validateSandbox(module string, folders []ToolFolder) error { | |
| 146 | - if info, err := os.Stat(module); err != nil || info.IsDir() { | |
| 147 | - return fmt.Errorf("wasm: no module at %s", module) | |
| 148 | - } | |
| 149 | - seen := map[string]bool{} | |
| 150 | - for _, f := range folders { | |
| 151 | - switch { | |
| 152 | - case filepath.IsAbs(f.Path): | |
| 153 | - return fmt.Errorf("folder %q must be relative to the working directory", f.Path) | |
| 154 | - case f.Path == "." || f.Path == ".." || strings.HasPrefix(f.Path, ".."+string(filepath.Separator)): | |
| 155 | - return fmt.Errorf("folder %q must be a folder inside the working directory, not it or above it", f.Path) | |
| 156 | - case seen[f.Path]: | |
| 157 | - return fmt.Errorf("folder %q is given twice", f.Path) | |
| 158 | - } | |
| 159 | - seen[f.Path] = true | |
| 160 | - } | |
| 161 | - return nil | |
| 162 | -} | |
| 163 | - | |
| 164 | 138 | // ToolParameter is one argument of a declared tool. |
| 165 | 139 | type ToolParameter struct { |
| 166 | 140 | // Name: the key under `parameters:`. |
| @@ -233,67 +207,6 @@ func ReplacePlaceholders(entry string, value func(name string) string) string { | ||
| 233 | 207 | }) |
| 234 | 208 | } |
| 235 | 209 | |
| 236 | -// validateTools checks the `tools:` block: names unique and free, every | |
| 237 | -// command runnable as written, every parameter consistent. | |
| 238 | -func (c Config) validateTools() error { | |
| 239 | - seen := map[string]bool{} | |
| 240 | - for i, t := range c.Tools { | |
| 241 | - where := fmt.Sprintf("tools[%d]", i) | |
| 242 | - if t.Name != "" { | |
| 243 | - where = fmt.Sprintf("tools[%d] (%s)", i, t.Name) | |
| 244 | - } | |
| 245 | - if err := t.validate(where); err != nil { | |
| 246 | - return err | |
| 247 | - } | |
| 248 | - if seen[t.Name] { | |
| 249 | - return fmt.Errorf("%s: another tool is already called %q", where, t.Name) | |
| 250 | - } | |
| 251 | - seen[t.Name] = true | |
| 252 | - } | |
| 253 | - return nil | |
| 254 | -} | |
| 255 | - | |
| 256 | -// validate checks one declared tool. | |
| 257 | -func (t DeclaredTool) validate(where string) error { | |
| 258 | - switch { | |
| 259 | - case !toolNamePattern.MatchString(t.Name): | |
| 260 | - return fmt.Errorf("%s.name %q must start with a letter and hold only letters, digits, _ or - (64 characters at most)", where, t.Name) | |
| 261 | - case slices.Contains(BuiltinToolNames, t.Name): | |
| 262 | - return fmt.Errorf("%s.name %q is the name of a built-in tool", where, t.Name) | |
| 263 | - case strings.TrimSpace(t.Description) == "": | |
| 264 | - return fmt.Errorf("%s.description must not be empty: the model picks a tool from its description", where) | |
| 265 | - case t.Timeout < 0: | |
| 266 | - return fmt.Errorf("%s.timeout must be >= 0", where) | |
| 267 | - } | |
| 268 | - if err := t.validateParameters(where); err != nil { | |
| 269 | - return err | |
| 270 | - } | |
| 271 | - if t.IsWasm() { | |
| 272 | - return t.validateWasm(where) | |
| 273 | - } | |
| 274 | - switch { | |
| 275 | - case len(t.Read)+len(t.Write) > 0: | |
| 276 | - return fmt.Errorf("%s: read and write only apply to a wasm tool — a command already sees every file", where) | |
| 277 | - case len(t.Command) == 0 || strings.TrimSpace(t.Command[0]) == "": | |
| 278 | - return fmt.Errorf("%s.command must name a program, e.g. [\"./scripts/run.sh\", \"{{arg}}\"] (or wasm: a module)", where) | |
| 279 | - case len(Placeholders(t.Command[0])) > 0: | |
| 280 | - return fmt.Errorf("%s.command: the program (%q) cannot come from a parameter", where, t.Command[0]) | |
| 281 | - } | |
| 282 | - return t.validatePlaceholders(where) | |
| 283 | -} | |
| 284 | - | |
| 285 | -// validateWasm checks a wasm tool: no command beside it, a module that | |
| 286 | -// exists, and folders that stay inside the working directory. | |
| 287 | -func (t DeclaredTool) validateWasm(where string) error { | |
| 288 | - if len(t.Command) > 0 { | |
| 289 | - return fmt.Errorf("%s: give command or wasm, not both", where) | |
| 290 | - } | |
| 291 | - if err := validateSandbox(t.Wasm, t.Folders()); err != nil { | |
| 292 | - return fmt.Errorf("%s: %w", where, err) | |
| 293 | - } | |
| 294 | - return nil | |
| 295 | -} | |
| 296 | - | |
| 297 | 210 | // resolveToolPaths makes every relative wasm module path absolute against |
| 298 | 211 | // the BaseDir of the agent file: the module ships with the agent, whoever |
| 299 | 212 | // starts mm and from wherever. |
| @@ -308,157 +221,3 @@ func resolveToolPaths(configPath string) { | ||
| 308 | 221 | } |
| 309 | 222 | } |
| 310 | 223 | } |
| 311 | - | |
| 312 | -// validateParameters checks each parameter's type, enum and default. | |
| 313 | -func (t DeclaredTool) validateParameters(where string) error { | |
| 314 | - seen := map[string]bool{} | |
| 315 | - for _, p := range t.Parameters { | |
| 316 | - at := where + ".parameters." + p.Name | |
| 317 | - if !toolNamePattern.MatchString(p.Name) { | |
| 318 | - return fmt.Errorf("%s: a parameter name must start with a letter and hold only letters, digits, _ or -", at) | |
| 319 | - } | |
| 320 | - if seen[p.Name] { | |
| 321 | - return fmt.Errorf("%s: declared twice", at) | |
| 322 | - } | |
| 323 | - seen[p.Name] = true | |
| 324 | - if err := p.validate(at); err != nil { | |
| 325 | - return err | |
| 326 | - } | |
| 327 | - } | |
| 328 | - return nil | |
| 329 | -} | |
| 330 | - | |
| 331 | -// validate checks one parameter (an absent type was read as string). | |
| 332 | -func (p ToolParameter) validate(at string) error { | |
| 333 | - if !slices.Contains(paramTypes, p.Type) { | |
| 334 | - return fmt.Errorf("%s.type %q must be one of %s", at, p.Type, strings.Join(paramTypes, ", ")) | |
| 335 | - } | |
| 336 | - for _, v := range p.Enum { | |
| 337 | - if _, err := p.Coerce(v); err != nil { | |
| 338 | - return fmt.Errorf("%s.enum: %v", at, err) | |
| 339 | - } | |
| 340 | - } | |
| 341 | - if p.Default == nil { | |
| 342 | - return nil | |
| 343 | - } | |
| 344 | - if p.IsRequired() { | |
| 345 | - return fmt.Errorf("%s.default only makes sense with required: false", at) | |
| 346 | - } | |
| 347 | - normal, err := p.Coerce(p.Default) | |
| 348 | - if err != nil { | |
| 349 | - return fmt.Errorf("%s.default: %v", at, err) | |
| 350 | - } | |
| 351 | - if !p.Allows(normal) { | |
| 352 | - return fmt.Errorf("%s.default %v is not one of the enum values", at, p.Default) | |
| 353 | - } | |
| 354 | - return nil | |
| 355 | -} | |
| 356 | - | |
| 357 | -// validatePlaceholders refuses a {{name}} that no parameter declares — a | |
| 358 | -// typo there would otherwise send the literal text to the program. | |
| 359 | -func (t DeclaredTool) validatePlaceholders(where string) error { | |
| 360 | - for _, entry := range t.Command { | |
| 361 | - for _, name := range Placeholders(entry) { | |
| 362 | - if _, ok := t.Parameters.Find(name); !ok { | |
| 363 | - return fmt.Errorf("%s.command: {{%s}} is not a declared parameter", where, name) | |
| 364 | - } | |
| 365 | - } | |
| 366 | - } | |
| 367 | - return nil | |
| 368 | -} | |
| 369 | - | |
| 370 | -// Coerce turns a value — from the file, or from the model's call — into the | |
| 371 | -// parameter's type, or says why it cannot. Models write numbers and booleans | |
| 372 | -// as strings often enough ("3", "true") that those are accepted; anything | |
| 373 | -// else is refused rather than guessed. | |
| 374 | -// | |
| 375 | -// ToolParameter{Type: "integer"}.Coerce("3") // 3, nil | |
| 376 | -// ToolParameter{Type: "boolean"}.Coerce("yes") // error | |
| 377 | -func (p ToolParameter) Coerce(v any) (any, error) { | |
| 378 | - switch p.Type { | |
| 379 | - case ParamNumber: | |
| 380 | - return coerceNumber(v) | |
| 381 | - case ParamInteger: | |
| 382 | - return coerceInteger(v) | |
| 383 | - case ParamBoolean: | |
| 384 | - return coerceBoolean(v) | |
| 385 | - default: | |
| 386 | - return coerceString(v) | |
| 387 | - } | |
| 388 | -} | |
| 389 | - | |
| 390 | -func coerceString(v any) (any, error) { | |
| 391 | - switch x := v.(type) { | |
| 392 | - case string: | |
| 393 | - return x, nil | |
| 394 | - case int, int64, float64, bool: | |
| 395 | - return fmt.Sprint(x), nil | |
| 396 | - } | |
| 397 | - return nil, fmt.Errorf("%v is not a string", v) | |
| 398 | -} | |
| 399 | - | |
| 400 | -func coerceNumber(v any) (any, error) { | |
| 401 | - switch x := v.(type) { | |
| 402 | - case int: | |
| 403 | - return float64(x), nil | |
| 404 | - case int64: | |
| 405 | - return float64(x), nil | |
| 406 | - case float64: | |
| 407 | - return x, nil | |
| 408 | - case string: | |
| 409 | - if f, err := strconv.ParseFloat(strings.TrimSpace(x), 64); err == nil { | |
| 410 | - return f, nil | |
| 411 | - } | |
| 412 | - } | |
| 413 | - return nil, fmt.Errorf("%q is not a number", fmt.Sprint(v)) | |
| 414 | -} | |
| 415 | - | |
| 416 | -func coerceInteger(v any) (any, error) { | |
| 417 | - f, err := coerceNumber(v) | |
| 418 | - if err != nil { | |
| 419 | - return nil, fmt.Errorf("%q is not an integer", fmt.Sprint(v)) | |
| 420 | - } | |
| 421 | - n := f.(float64) | |
| 422 | - if n != float64(int64(n)) { | |
| 423 | - return nil, fmt.Errorf("%v is not an integer", v) | |
| 424 | - } | |
| 425 | - return int64(n), nil | |
| 426 | -} | |
| 427 | - | |
| 428 | -func coerceBoolean(v any) (any, error) { | |
| 429 | - switch x := v.(type) { | |
| 430 | - case bool: | |
| 431 | - return x, nil | |
| 432 | - case string: | |
| 433 | - switch strings.ToLower(strings.TrimSpace(x)) { | |
| 434 | - case "true": | |
| 435 | - return true, nil | |
| 436 | - case "false": | |
| 437 | - return false, nil | |
| 438 | - } | |
| 439 | - } | |
| 440 | - return nil, fmt.Errorf("%q is not a boolean (true or false)", fmt.Sprint(v)) | |
| 441 | -} | |
| 442 | - | |
| 443 | -// Allows reports whether a coerced value is accepted by the enum (always, | |
| 444 | -// without one). | |
| 445 | -func (p ToolParameter) Allows(v any) bool { | |
| 446 | - if len(p.Enum) == 0 { | |
| 447 | - return true | |
| 448 | - } | |
| 449 | - for _, e := range p.Enum { | |
| 450 | - if allowed, err := p.Coerce(e); err == nil && allowed == v { | |
| 451 | - return true | |
| 452 | - } | |
| 453 | - } | |
| 454 | - return false | |
| 455 | -} | |
| 456 | - | |
| 457 | -// EnumText lists the enum values for a message or a description. | |
| 458 | -func (p ToolParameter) EnumText() string { | |
| 459 | - values := make([]string, len(p.Enum)) | |
| 460 | - for i, e := range p.Enum { | |
| 461 | - values[i] = fmt.Sprint(e) | |
| 462 | - } | |
| 463 | - return strings.Join(values, ", ") | |
| 464 | -} | |
| @@ -34,12 +34,8 @@ package config | |||
| 34 | 34 | ||
| 35 | import ( | 35 | import ( |
| 36 | "fmt" | 36 | "fmt" |
| 37 | - "os" | ||
| 38 | "path/filepath" | 37 | "path/filepath" |
| 39 | "regexp" | 38 | "regexp" |
| 40 | - "slices" | ||
| 41 | - "strconv" | ||
| 42 | - "strings" | ||
| 43 | "time" | 39 | "time" |
| 44 | 40 | ||
| 45 | "gopkg.in/yaml.v3" | 41 | "gopkg.in/yaml.v3" |
| @@ -139,28 +135,6 @@ func sandboxFolders(read, write []string) []ToolFolder { | |||
| 139 | return folders | 135 | return folders |
| 140 | } | 136 | } |
| 141 | 137 | ||
| 142 | -// validateSandbox checks what a Wasm module — a tool's or a hook's — is | ||
| 143 | -// given: a module file that exists, and folders that stay inside the | ||
| 144 | -// working directory, each once. The caller names the entry. | ||
| 145 | -func validateSandbox(module string, folders []ToolFolder) error { | ||
| 146 | - if info, err := os.Stat(module); err != nil || info.IsDir() { | ||
| 147 | - return fmt.Errorf("wasm: no module at %s", module) | ||
| 148 | - } | ||
| 149 | - seen := map[string]bool{} | ||
| 150 | - for _, f := range folders { | ||
| 151 | - switch { | ||
| 152 | - case filepath.IsAbs(f.Path): | ||
| 153 | - return fmt.Errorf("folder %q must be relative to the working directory", f.Path) | ||
| 154 | - case f.Path == "." || f.Path == ".." || strings.HasPrefix(f.Path, ".."+string(filepath.Separator)): | ||
| 155 | - return fmt.Errorf("folder %q must be a folder inside the working directory, not it or above it", f.Path) | ||
| 156 | - case seen[f.Path]: | ||
| 157 | - return fmt.Errorf("folder %q is given twice", f.Path) | ||
| 158 | - } | ||
| 159 | - seen[f.Path] = true | ||
| 160 | - } | ||
| 161 | - return nil | ||
| 162 | -} | ||
| 163 | - | ||
| 164 | // ToolParameter is one argument of a declared tool. | 138 | // ToolParameter is one argument of a declared tool. |
| 165 | type ToolParameter struct { | 139 | type ToolParameter struct { |
| 166 | // Name: the key under `parameters:`. | 140 | // Name: the key under `parameters:`. |
| @@ -233,67 +207,6 @@ func ReplacePlaceholders(entry string, value func(name string) string) string { | |||
| 233 | }) | 207 | }) |
| 234 | } | 208 | } |
| 235 | 209 | ||
| 236 | -// validateTools checks the `tools:` block: names unique and free, every | ||
| 237 | -// command runnable as written, every parameter consistent. | ||
| 238 | -func (c Config) validateTools() error { | ||
| 239 | - seen := map[string]bool{} | ||
| 240 | - for i, t := range c.Tools { | ||
| 241 | - where := fmt.Sprintf("tools[%d]", i) | ||
| 242 | - if t.Name != "" { | ||
| 243 | - where = fmt.Sprintf("tools[%d] (%s)", i, t.Name) | ||
| 244 | - } | ||
| 245 | - if err := t.validate(where); err != nil { | ||
| 246 | - return err | ||
| 247 | - } | ||
| 248 | - if seen[t.Name] { | ||
| 249 | - return fmt.Errorf("%s: another tool is already called %q", where, t.Name) | ||
| 250 | - } | ||
| 251 | - seen[t.Name] = true | ||
| 252 | - } | ||
| 253 | - return nil | ||
| 254 | -} | ||
| 255 | - | ||
| 256 | -// validate checks one declared tool. | ||
| 257 | -func (t DeclaredTool) validate(where string) error { | ||
| 258 | - switch { | ||
| 259 | - case !toolNamePattern.MatchString(t.Name): | ||
| 260 | - return fmt.Errorf("%s.name %q must start with a letter and hold only letters, digits, _ or - (64 characters at most)", where, t.Name) | ||
| 261 | - case slices.Contains(BuiltinToolNames, t.Name): | ||
| 262 | - return fmt.Errorf("%s.name %q is the name of a built-in tool", where, t.Name) | ||
| 263 | - case strings.TrimSpace(t.Description) == "": | ||
| 264 | - return fmt.Errorf("%s.description must not be empty: the model picks a tool from its description", where) | ||
| 265 | - case t.Timeout < 0: | ||
| 266 | - return fmt.Errorf("%s.timeout must be >= 0", where) | ||
| 267 | - } | ||
| 268 | - if err := t.validateParameters(where); err != nil { | ||
| 269 | - return err | ||
| 270 | - } | ||
| 271 | - if t.IsWasm() { | ||
| 272 | - return t.validateWasm(where) | ||
| 273 | - } | ||
| 274 | - switch { | ||
| 275 | - case len(t.Read)+len(t.Write) > 0: | ||
| 276 | - return fmt.Errorf("%s: read and write only apply to a wasm tool — a command already sees every file", where) | ||
| 277 | - case len(t.Command) == 0 || strings.TrimSpace(t.Command[0]) == "": | ||
| 278 | - return fmt.Errorf("%s.command must name a program, e.g. [\"./scripts/run.sh\", \"{{arg}}\"] (or wasm: a module)", where) | ||
| 279 | - case len(Placeholders(t.Command[0])) > 0: | ||
| 280 | - return fmt.Errorf("%s.command: the program (%q) cannot come from a parameter", where, t.Command[0]) | ||
| 281 | - } | ||
| 282 | - return t.validatePlaceholders(where) | ||
| 283 | -} | ||
| 284 | - | ||
| 285 | -// validateWasm checks a wasm tool: no command beside it, a module that | ||
| 286 | -// exists, and folders that stay inside the working directory. | ||
| 287 | -func (t DeclaredTool) validateWasm(where string) error { | ||
| 288 | - if len(t.Command) > 0 { | ||
| 289 | - return fmt.Errorf("%s: give command or wasm, not both", where) | ||
| 290 | - } | ||
| 291 | - if err := validateSandbox(t.Wasm, t.Folders()); err != nil { | ||
| 292 | - return fmt.Errorf("%s: %w", where, err) | ||
| 293 | - } | ||
| 294 | - return nil | ||
| 295 | -} | ||
| 296 | - | ||
| 297 | // resolveToolPaths makes every relative wasm module path absolute against | 210 | // resolveToolPaths makes every relative wasm module path absolute against |
| 298 | // the BaseDir of the agent file: the module ships with the agent, whoever | 211 | // the BaseDir of the agent file: the module ships with the agent, whoever |
| 299 | // starts mm and from wherever. | 212 | // starts mm and from wherever. |
| @@ -308,157 +221,3 @@ func resolveToolPaths(configPath string) { | |||
| 308 | } | 221 | } |
| 309 | } | 222 | } |
| 310 | } | 223 | } |
| 311 | - | ||
| 312 | -// validateParameters checks each parameter's type, enum and default. | ||
| 313 | -func (t DeclaredTool) validateParameters(where string) error { | ||
| 314 | - seen := map[string]bool{} | ||
| 315 | - for _, p := range t.Parameters { | ||
| 316 | - at := where + ".parameters." + p.Name | ||
| 317 | - if !toolNamePattern.MatchString(p.Name) { | ||
| 318 | - return fmt.Errorf("%s: a parameter name must start with a letter and hold only letters, digits, _ or -", at) | ||
| 319 | - } | ||
| 320 | - if seen[p.Name] { | ||
| 321 | - return fmt.Errorf("%s: declared twice", at) | ||
| 322 | - } | ||
| 323 | - seen[p.Name] = true | ||
| 324 | - if err := p.validate(at); err != nil { | ||
| 325 | - return err | ||
| 326 | - } | ||
| 327 | - } | ||
| 328 | - return nil | ||
| 329 | -} | ||
| 330 | - | ||
| 331 | -// validate checks one parameter (an absent type was read as string). | ||
| 332 | -func (p ToolParameter) validate(at string) error { | ||
| 333 | - if !slices.Contains(paramTypes, p.Type) { | ||
| 334 | - return fmt.Errorf("%s.type %q must be one of %s", at, p.Type, strings.Join(paramTypes, ", ")) | ||
| 335 | - } | ||
| 336 | - for _, v := range p.Enum { | ||
| 337 | - if _, err := p.Coerce(v); err != nil { | ||
| 338 | - return fmt.Errorf("%s.enum: %v", at, err) | ||
| 339 | - } | ||
| 340 | - } | ||
| 341 | - if p.Default == nil { | ||
| 342 | - return nil | ||
| 343 | - } | ||
| 344 | - if p.IsRequired() { | ||
| 345 | - return fmt.Errorf("%s.default only makes sense with required: false", at) | ||
| 346 | - } | ||
| 347 | - normal, err := p.Coerce(p.Default) | ||
| 348 | - if err != nil { | ||
| 349 | - return fmt.Errorf("%s.default: %v", at, err) | ||
| 350 | - } | ||
| 351 | - if !p.Allows(normal) { | ||
| 352 | - return fmt.Errorf("%s.default %v is not one of the enum values", at, p.Default) | ||
| 353 | - } | ||
| 354 | - return nil | ||
| 355 | -} | ||
| 356 | - | ||
| 357 | -// validatePlaceholders refuses a {{name}} that no parameter declares — a | ||
| 358 | -// typo there would otherwise send the literal text to the program. | ||
| 359 | -func (t DeclaredTool) validatePlaceholders(where string) error { | ||
| 360 | - for _, entry := range t.Command { | ||
| 361 | - for _, name := range Placeholders(entry) { | ||
| 362 | - if _, ok := t.Parameters.Find(name); !ok { | ||
| 363 | - return fmt.Errorf("%s.command: {{%s}} is not a declared parameter", where, name) | ||
| 364 | - } | ||
| 365 | - } | ||
| 366 | - } | ||
| 367 | - return nil | ||
| 368 | -} | ||
| 369 | - | ||
| 370 | -// Coerce turns a value — from the file, or from the model's call — into the | ||
| 371 | -// parameter's type, or says why it cannot. Models write numbers and booleans | ||
| 372 | -// as strings often enough ("3", "true") that those are accepted; anything | ||
| 373 | -// else is refused rather than guessed. | ||
| 374 | -// | ||
| 375 | -// ToolParameter{Type: "integer"}.Coerce("3") // 3, nil | ||
| 376 | -// ToolParameter{Type: "boolean"}.Coerce("yes") // error | ||
| 377 | -func (p ToolParameter) Coerce(v any) (any, error) { | ||
| 378 | - switch p.Type { | ||
| 379 | - case ParamNumber: | ||
| 380 | - return coerceNumber(v) | ||
| 381 | - case ParamInteger: | ||
| 382 | - return coerceInteger(v) | ||
| 383 | - case ParamBoolean: | ||
| 384 | - return coerceBoolean(v) | ||
| 385 | - default: | ||
| 386 | - return coerceString(v) | ||
| 387 | - } | ||
| 388 | -} | ||
| 389 | - | ||
| 390 | -func coerceString(v any) (any, error) { | ||
| 391 | - switch x := v.(type) { | ||
| 392 | - case string: | ||
| 393 | - return x, nil | ||
| 394 | - case int, int64, float64, bool: | ||
| 395 | - return fmt.Sprint(x), nil | ||
| 396 | - } | ||
| 397 | - return nil, fmt.Errorf("%v is not a string", v) | ||
| 398 | -} | ||
| 399 | - | ||
| 400 | -func coerceNumber(v any) (any, error) { | ||
| 401 | - switch x := v.(type) { | ||
| 402 | - case int: | ||
| 403 | - return float64(x), nil | ||
| 404 | - case int64: | ||
| 405 | - return float64(x), nil | ||
| 406 | - case float64: | ||
| 407 | - return x, nil | ||
| 408 | - case string: | ||
| 409 | - if f, err := strconv.ParseFloat(strings.TrimSpace(x), 64); err == nil { | ||
| 410 | - return f, nil | ||
| 411 | - } | ||
| 412 | - } | ||
| 413 | - return nil, fmt.Errorf("%q is not a number", fmt.Sprint(v)) | ||
| 414 | -} | ||
| 415 | - | ||
| 416 | -func coerceInteger(v any) (any, error) { | ||
| 417 | - f, err := coerceNumber(v) | ||
| 418 | - if err != nil { | ||
| 419 | - return nil, fmt.Errorf("%q is not an integer", fmt.Sprint(v)) | ||
| 420 | - } | ||
| 421 | - n := f.(float64) | ||
| 422 | - if n != float64(int64(n)) { | ||
| 423 | - return nil, fmt.Errorf("%v is not an integer", v) | ||
| 424 | - } | ||
| 425 | - return int64(n), nil | ||
| 426 | -} | ||
| 427 | - | ||
| 428 | -func coerceBoolean(v any) (any, error) { | ||
| 429 | - switch x := v.(type) { | ||
| 430 | - case bool: | ||
| 431 | - return x, nil | ||
| 432 | - case string: | ||
| 433 | - switch strings.ToLower(strings.TrimSpace(x)) { | ||
| 434 | - case "true": | ||
| 435 | - return true, nil | ||
| 436 | - case "false": | ||
| 437 | - return false, nil | ||
| 438 | - } | ||
| 439 | - } | ||
| 440 | - return nil, fmt.Errorf("%q is not a boolean (true or false)", fmt.Sprint(v)) | ||
| 441 | -} | ||
| 442 | - | ||
| 443 | -// Allows reports whether a coerced value is accepted by the enum (always, | ||
| 444 | -// without one). | ||
| 445 | -func (p ToolParameter) Allows(v any) bool { | ||
| 446 | - if len(p.Enum) == 0 { | ||
| 447 | - return true | ||
| 448 | - } | ||
| 449 | - for _, e := range p.Enum { | ||
| 450 | - if allowed, err := p.Coerce(e); err == nil && allowed == v { | ||
| 451 | - return true | ||
| 452 | - } | ||
| 453 | - } | ||
| 454 | - return false | ||
| 455 | -} | ||
| 456 | - | ||
| 457 | -// EnumText lists the enum values for a message or a description. | ||
| 458 | -func (p ToolParameter) EnumText() string { | ||
| 459 | - values := make([]string, len(p.Enum)) | ||
| 460 | - for i, e := range p.Enum { | ||
| 461 | - values[i] = fmt.Sprint(e) | ||
| 462 | - } | ||
| 463 | - return strings.Join(values, ", ") | ||
| 464 | -} | ||
added
internal/config/toolsvalidate.go +165 -0 | new file mode 100644 | ||
| @@ -0,0 +1,165 @@ | ||
| 1 | +package config | |
| 2 | + | |
| 3 | +import ( | |
| 4 | + "fmt" | |
| 5 | + "os" | |
| 6 | + "path/filepath" | |
| 7 | + "slices" | |
| 8 | + "strings" | |
| 9 | +) | |
| 10 | + | |
| 11 | +// validateSandbox checks what a Wasm module — a tool's or a hook's — is | |
| 12 | +// given: a module file that exists, and folders that stay inside the | |
| 13 | +// working directory, each once. The caller names the entry. | |
| 14 | +func validateSandbox(module string, folders []ToolFolder) error { | |
| 15 | + if info, err := os.Stat(module); err != nil || info.IsDir() { | |
| 16 | + return fmt.Errorf("wasm: no module at %s", module) | |
| 17 | + } | |
| 18 | + seen := map[string]bool{} | |
| 19 | + for _, f := range folders { | |
| 20 | + switch { | |
| 21 | + case filepath.IsAbs(f.Path): | |
| 22 | + return fmt.Errorf("folder %q must be relative to the working directory", f.Path) | |
| 23 | + case f.Path == "." || f.Path == ".." || strings.HasPrefix(f.Path, ".."+string(filepath.Separator)): | |
| 24 | + return fmt.Errorf("folder %q must be a folder inside the working directory, not it or above it", f.Path) | |
| 25 | + case seen[f.Path]: | |
| 26 | + return fmt.Errorf("folder %q is given twice", f.Path) | |
| 27 | + } | |
| 28 | + seen[f.Path] = true | |
| 29 | + } | |
| 30 | + return nil | |
| 31 | +} | |
| 32 | + | |
| 33 | +// validateTools checks the `tools:` block: names unique and free, every | |
| 34 | +// command runnable as written, every parameter consistent. | |
| 35 | +func (c Config) validateTools() error { | |
| 36 | + seen := map[string]bool{} | |
| 37 | + for i, t := range c.Tools { | |
| 38 | + where := fmt.Sprintf("tools[%d]", i) | |
| 39 | + if t.Name != "" { | |
| 40 | + where = fmt.Sprintf("tools[%d] (%s)", i, t.Name) | |
| 41 | + } | |
| 42 | + if err := t.validate(where); err != nil { | |
| 43 | + return err | |
| 44 | + } | |
| 45 | + if seen[t.Name] { | |
| 46 | + return fmt.Errorf("%s: another tool is already called %q", where, t.Name) | |
| 47 | + } | |
| 48 | + seen[t.Name] = true | |
| 49 | + } | |
| 50 | + return nil | |
| 51 | +} | |
| 52 | + | |
| 53 | +// validate checks one declared tool. | |
| 54 | +func (t DeclaredTool) validate(where string) error { | |
| 55 | + return firstFailing( | |
| 56 | + func() error { return t.declarationError(where) }, | |
| 57 | + func() error { return t.validateParameters(where) }, | |
| 58 | + func() error { | |
| 59 | + if t.IsWasm() { | |
| 60 | + return t.validateWasm(where) | |
| 61 | + } | |
| 62 | + return t.commandError(where) | |
| 63 | + }, | |
| 64 | + ) | |
| 65 | +} | |
| 66 | + | |
| 67 | +// declarationError checks the name, the description and the timeout. | |
| 68 | +func (t DeclaredTool) declarationError(where string) error { | |
| 69 | + return firstError( | |
| 70 | + problem(!toolNamePattern.MatchString(t.Name), fmt.Sprintf("%s.name %q must start with a letter and hold only letters, digits, _ or - (64 characters at most)", where, t.Name)), | |
| 71 | + problem(slices.Contains(BuiltinToolNames, t.Name), fmt.Sprintf("%s.name %q is the name of a built-in tool", where, t.Name)), | |
| 72 | + problem(strings.TrimSpace(t.Description) == "", fmt.Sprintf("%s.description must not be empty: the model picks a tool from its description", where)), | |
| 73 | + problem(t.Timeout < 0, fmt.Sprintf("%s.timeout must be >= 0", where)), | |
| 74 | + ) | |
| 75 | +} | |
| 76 | + | |
| 77 | +// commandError checks a command tool: no sandbox folders, a program that | |
| 78 | +// is not a parameter, and placeholders that name declared parameters. | |
| 79 | +func (t DeclaredTool) commandError(where string) error { | |
| 80 | + switch { | |
| 81 | + case len(t.Read)+len(t.Write) > 0: | |
| 82 | + return fmt.Errorf("%s: read and write only apply to a wasm tool — a command already sees every file", where) | |
| 83 | + case len(t.Command) == 0 || strings.TrimSpace(t.Command[0]) == "": | |
| 84 | + return fmt.Errorf("%s.command must name a program, e.g. [\"./scripts/run.sh\", \"{{arg}}\"] (or wasm: a module)", where) | |
| 85 | + case len(Placeholders(t.Command[0])) > 0: | |
| 86 | + return fmt.Errorf("%s.command: the program (%q) cannot come from a parameter", where, t.Command[0]) | |
| 87 | + } | |
| 88 | + return t.validatePlaceholders(where) | |
| 89 | +} | |
| 90 | + | |
| 91 | +// validateWasm checks a wasm tool: no command beside it, a module that | |
| 92 | +// exists, and folders that stay inside the working directory. | |
| 93 | +func (t DeclaredTool) validateWasm(where string) error { | |
| 94 | + if len(t.Command) > 0 { | |
| 95 | + return fmt.Errorf("%s: give command or wasm, not both", where) | |
| 96 | + } | |
| 97 | + if err := validateSandbox(t.Wasm, t.Folders()); err != nil { | |
| 98 | + return fmt.Errorf("%s: %w", where, err) | |
| 99 | + } | |
| 100 | + return nil | |
| 101 | +} | |
| 102 | + | |
| 103 | +// validateParameters checks each parameter's type, enum and default. | |
| 104 | +func (t DeclaredTool) validateParameters(where string) error { | |
| 105 | + seen := map[string]bool{} | |
| 106 | + for _, p := range t.Parameters { | |
| 107 | + at := where + ".parameters." + p.Name | |
| 108 | + if !toolNamePattern.MatchString(p.Name) { | |
| 109 | + return fmt.Errorf("%s: a parameter name must start with a letter and hold only letters, digits, _ or -", at) | |
| 110 | + } | |
| 111 | + if seen[p.Name] { | |
| 112 | + return fmt.Errorf("%s: declared twice", at) | |
| 113 | + } | |
| 114 | + seen[p.Name] = true | |
| 115 | + if err := p.validate(at); err != nil { | |
| 116 | + return err | |
| 117 | + } | |
| 118 | + } | |
| 119 | + return nil | |
| 120 | +} | |
| 121 | + | |
| 122 | +// validate checks one parameter (an absent type was read as string). | |
| 123 | +func (p ToolParameter) validate(at string) error { | |
| 124 | + if !slices.Contains(paramTypes, p.Type) { | |
| 125 | + return fmt.Errorf("%s.type %q must be one of %s", at, p.Type, strings.Join(paramTypes, ", ")) | |
| 126 | + } | |
| 127 | + for _, v := range p.Enum { | |
| 128 | + if _, err := p.Coerce(v); err != nil { | |
| 129 | + return fmt.Errorf("%s.enum: %v", at, err) | |
| 130 | + } | |
| 131 | + } | |
| 132 | + if p.Default == nil { | |
| 133 | + return nil | |
| 134 | + } | |
| 135 | + return p.defaultError(at) | |
| 136 | +} | |
| 137 | + | |
| 138 | +// defaultError checks a parameter's default: only on an optional parameter, | |
| 139 | +// of the parameter's type, one of the enum values. | |
| 140 | +func (p ToolParameter) defaultError(at string) error { | |
| 141 | + if p.IsRequired() { | |
| 142 | + return fmt.Errorf("%s.default only makes sense with required: false", at) | |
| 143 | + } | |
| 144 | + normal, err := p.Coerce(p.Default) | |
| 145 | + if err != nil { | |
| 146 | + return fmt.Errorf("%s.default: %v", at, err) | |
| 147 | + } | |
| 148 | + if !p.Allows(normal) { | |
| 149 | + return fmt.Errorf("%s.default %v is not one of the enum values", at, p.Default) | |
| 150 | + } | |
| 151 | + return nil | |
| 152 | +} | |
| 153 | + | |
| 154 | +// validatePlaceholders refuses a {{name}} that no parameter declares — a | |
| 155 | +// typo there would otherwise send the literal text to the program. | |
| 156 | +func (t DeclaredTool) validatePlaceholders(where string) error { | |
| 157 | + for _, entry := range t.Command { | |
| 158 | + for _, name := range Placeholders(entry) { | |
| 159 | + if _, ok := t.Parameters.Find(name); !ok { | |
| 160 | + return fmt.Errorf("%s.command: {{%s}} is not a declared parameter", where, name) | |
| 161 | + } | |
| 162 | + } | |
| 163 | + } | |
| 164 | + return nil | |
| 165 | +} | |
| new file mode 100644 | |||
| @@ -0,0 +1,165 @@ | |||
| 1 | +package config | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "fmt" | ||
| 5 | + "os" | ||
| 6 | + "path/filepath" | ||
| 7 | + "slices" | ||
| 8 | + "strings" | ||
| 9 | +) | ||
| 10 | + | ||
| 11 | +// validateSandbox checks what a Wasm module — a tool's or a hook's — is | ||
| 12 | +// given: a module file that exists, and folders that stay inside the | ||
| 13 | +// working directory, each once. The caller names the entry. | ||
| 14 | +func validateSandbox(module string, folders []ToolFolder) error { | ||
| 15 | + if info, err := os.Stat(module); err != nil || info.IsDir() { | ||
| 16 | + return fmt.Errorf("wasm: no module at %s", module) | ||
| 17 | + } | ||
| 18 | + seen := map[string]bool{} | ||
| 19 | + for _, f := range folders { | ||
| 20 | + switch { | ||
| 21 | + case filepath.IsAbs(f.Path): | ||
| 22 | + return fmt.Errorf("folder %q must be relative to the working directory", f.Path) | ||
| 23 | + case f.Path == "." || f.Path == ".." || strings.HasPrefix(f.Path, ".."+string(filepath.Separator)): | ||
| 24 | + return fmt.Errorf("folder %q must be a folder inside the working directory, not it or above it", f.Path) | ||
| 25 | + case seen[f.Path]: | ||
| 26 | + return fmt.Errorf("folder %q is given twice", f.Path) | ||
| 27 | + } | ||
| 28 | + seen[f.Path] = true | ||
| 29 | + } | ||
| 30 | + return nil | ||
| 31 | +} | ||
| 32 | + | ||
| 33 | +// validateTools checks the `tools:` block: names unique and free, every | ||
| 34 | +// command runnable as written, every parameter consistent. | ||
| 35 | +func (c Config) validateTools() error { | ||
| 36 | + seen := map[string]bool{} | ||
| 37 | + for i, t := range c.Tools { | ||
| 38 | + where := fmt.Sprintf("tools[%d]", i) | ||
| 39 | + if t.Name != "" { | ||
| 40 | + where = fmt.Sprintf("tools[%d] (%s)", i, t.Name) | ||
| 41 | + } | ||
| 42 | + if err := t.validate(where); err != nil { | ||
| 43 | + return err | ||
| 44 | + } | ||
| 45 | + if seen[t.Name] { | ||
| 46 | + return fmt.Errorf("%s: another tool is already called %q", where, t.Name) | ||
| 47 | + } | ||
| 48 | + seen[t.Name] = true | ||
| 49 | + } | ||
| 50 | + return nil | ||
| 51 | +} | ||
| 52 | + | ||
| 53 | +// validate checks one declared tool. | ||
| 54 | +func (t DeclaredTool) validate(where string) error { | ||
| 55 | + return firstFailing( | ||
| 56 | + func() error { return t.declarationError(where) }, | ||
| 57 | + func() error { return t.validateParameters(where) }, | ||
| 58 | + func() error { | ||
| 59 | + if t.IsWasm() { | ||
| 60 | + return t.validateWasm(where) | ||
| 61 | + } | ||
| 62 | + return t.commandError(where) | ||
| 63 | + }, | ||
| 64 | + ) | ||
| 65 | +} | ||
| 66 | + | ||
| 67 | +// declarationError checks the name, the description and the timeout. | ||
| 68 | +func (t DeclaredTool) declarationError(where string) error { | ||
| 69 | + return firstError( | ||
| 70 | + problem(!toolNamePattern.MatchString(t.Name), fmt.Sprintf("%s.name %q must start with a letter and hold only letters, digits, _ or - (64 characters at most)", where, t.Name)), | ||
| 71 | + problem(slices.Contains(BuiltinToolNames, t.Name), fmt.Sprintf("%s.name %q is the name of a built-in tool", where, t.Name)), | ||
| 72 | + problem(strings.TrimSpace(t.Description) == "", fmt.Sprintf("%s.description must not be empty: the model picks a tool from its description", where)), | ||
| 73 | + problem(t.Timeout < 0, fmt.Sprintf("%s.timeout must be >= 0", where)), | ||
| 74 | + ) | ||
| 75 | +} | ||
| 76 | + | ||
| 77 | +// commandError checks a command tool: no sandbox folders, a program that | ||
| 78 | +// is not a parameter, and placeholders that name declared parameters. | ||
| 79 | +func (t DeclaredTool) commandError(where string) error { | ||
| 80 | + switch { | ||
| 81 | + case len(t.Read)+len(t.Write) > 0: | ||
| 82 | + return fmt.Errorf("%s: read and write only apply to a wasm tool — a command already sees every file", where) | ||
| 83 | + case len(t.Command) == 0 || strings.TrimSpace(t.Command[0]) == "": | ||
| 84 | + return fmt.Errorf("%s.command must name a program, e.g. [\"./scripts/run.sh\", \"{{arg}}\"] (or wasm: a module)", where) | ||
| 85 | + case len(Placeholders(t.Command[0])) > 0: | ||
| 86 | + return fmt.Errorf("%s.command: the program (%q) cannot come from a parameter", where, t.Command[0]) | ||
| 87 | + } | ||
| 88 | + return t.validatePlaceholders(where) | ||
| 89 | +} | ||
| 90 | + | ||
| 91 | +// validateWasm checks a wasm tool: no command beside it, a module that | ||
| 92 | +// exists, and folders that stay inside the working directory. | ||
| 93 | +func (t DeclaredTool) validateWasm(where string) error { | ||
| 94 | + if len(t.Command) > 0 { | ||
| 95 | + return fmt.Errorf("%s: give command or wasm, not both", where) | ||
| 96 | + } | ||
| 97 | + if err := validateSandbox(t.Wasm, t.Folders()); err != nil { | ||
| 98 | + return fmt.Errorf("%s: %w", where, err) | ||
| 99 | + } | ||
| 100 | + return nil | ||
| 101 | +} | ||
| 102 | + | ||
| 103 | +// validateParameters checks each parameter's type, enum and default. | ||
| 104 | +func (t DeclaredTool) validateParameters(where string) error { | ||
| 105 | + seen := map[string]bool{} | ||
| 106 | + for _, p := range t.Parameters { | ||
| 107 | + at := where + ".parameters." + p.Name | ||
| 108 | + if !toolNamePattern.MatchString(p.Name) { | ||
| 109 | + return fmt.Errorf("%s: a parameter name must start with a letter and hold only letters, digits, _ or -", at) | ||
| 110 | + } | ||
| 111 | + if seen[p.Name] { | ||
| 112 | + return fmt.Errorf("%s: declared twice", at) | ||
| 113 | + } | ||
| 114 | + seen[p.Name] = true | ||
| 115 | + if err := p.validate(at); err != nil { | ||
| 116 | + return err | ||
| 117 | + } | ||
| 118 | + } | ||
| 119 | + return nil | ||
| 120 | +} | ||
| 121 | + | ||
| 122 | +// validate checks one parameter (an absent type was read as string). | ||
| 123 | +func (p ToolParameter) validate(at string) error { | ||
| 124 | + if !slices.Contains(paramTypes, p.Type) { | ||
| 125 | + return fmt.Errorf("%s.type %q must be one of %s", at, p.Type, strings.Join(paramTypes, ", ")) | ||
| 126 | + } | ||
| 127 | + for _, v := range p.Enum { | ||
| 128 | + if _, err := p.Coerce(v); err != nil { | ||
| 129 | + return fmt.Errorf("%s.enum: %v", at, err) | ||
| 130 | + } | ||
| 131 | + } | ||
| 132 | + if p.Default == nil { | ||
| 133 | + return nil | ||
| 134 | + } | ||
| 135 | + return p.defaultError(at) | ||
| 136 | +} | ||
| 137 | + | ||
| 138 | +// defaultError checks a parameter's default: only on an optional parameter, | ||
| 139 | +// of the parameter's type, one of the enum values. | ||
| 140 | +func (p ToolParameter) defaultError(at string) error { | ||
| 141 | + if p.IsRequired() { | ||
| 142 | + return fmt.Errorf("%s.default only makes sense with required: false", at) | ||
| 143 | + } | ||
| 144 | + normal, err := p.Coerce(p.Default) | ||
| 145 | + if err != nil { | ||
| 146 | + return fmt.Errorf("%s.default: %v", at, err) | ||
| 147 | + } | ||
| 148 | + if !p.Allows(normal) { | ||
| 149 | + return fmt.Errorf("%s.default %v is not one of the enum values", at, p.Default) | ||
| 150 | + } | ||
| 151 | + return nil | ||
| 152 | +} | ||
| 153 | + | ||
| 154 | +// validatePlaceholders refuses a {{name}} that no parameter declares — a | ||
| 155 | +// typo there would otherwise send the literal text to the program. | ||
| 156 | +func (t DeclaredTool) validatePlaceholders(where string) error { | ||
| 157 | + for _, entry := range t.Command { | ||
| 158 | + for _, name := range Placeholders(entry) { | ||
| 159 | + if _, ok := t.Parameters.Find(name); !ok { | ||
| 160 | + return fmt.Errorf("%s.command: {{%s}} is not a declared parameter", where, name) | ||
| 161 | + } | ||
| 162 | + } | ||
| 163 | + } | ||
| 164 | + return nil | ||
| 165 | +} | ||
added
internal/config/validate.go +200 -0 | new file mode 100644 | ||
| @@ -0,0 +1,200 @@ | ||
| 1 | +package config | |
| 2 | + | |
| 3 | +import ( | |
| 4 | + "errors" | |
| 5 | + "fmt" | |
| 6 | + "strings" | |
| 7 | +) | |
| 8 | + | |
| 9 | +// validate refuses a value llama-server would refuse at start-up — better | |
| 10 | +// a configuration error naming the key than a server that does not start. | |
| 11 | +func (l LlamaCppConfig) validate() error { | |
| 12 | + for _, f := range []struct { | |
| 13 | + key string | |
| 14 | + value *int | |
| 15 | + min int | |
| 16 | + }{ | |
| 17 | + {"ctxSize", l.CtxSize, 0}, | |
| 18 | + {"modelsMax", l.ModelsMax, 0}, | |
| 19 | + {"modelsMemoryMargin", l.ModelsMemoryMargin, 0}, | |
| 20 | + {"fitTarget", l.FitTarget, 0}, | |
| 21 | + {"sleepIdleSeconds", l.SleepIdleSeconds, -1}, | |
| 22 | + } { | |
| 23 | + if f.value != nil && *f.value < f.min { | |
| 24 | + return fmt.Errorf("llamacpp.%s must be >= %d, not %d", f.key, f.min, *f.value) | |
| 25 | + } | |
| 26 | + } | |
| 27 | + if l.StartTimeout < 0 { | |
| 28 | + return errors.New("llamacpp.startTimeout must be >= 0") | |
| 29 | + } | |
| 30 | + return nil | |
| 31 | +} | |
| 32 | + | |
| 33 | +// validate refuses the values the ACP front end could not act on. | |
| 34 | +func (c ClientToolsConfig) validate() error { | |
| 35 | + switch { | |
| 36 | + case c.Discovery != DiscoveryMeta && c.Discovery != DiscoveryRequest: | |
| 37 | + return fmt.Errorf("acp.clientTools.discovery must be %q or %q, got %q", DiscoveryMeta, DiscoveryRequest, c.Discovery) | |
| 38 | + case c.Permissions != PermissionsAuto && c.Permissions != PermissionsAsk: | |
| 39 | + return fmt.Errorf("acp.clientTools.permissions must be %q or %q, got %q", PermissionsAuto, PermissionsAsk, c.Permissions) | |
| 40 | + case c.Timeout <= 0: | |
| 41 | + return errors.New("acp.clientTools.timeout must be > 0") | |
| 42 | + } | |
| 43 | + return nil | |
| 44 | +} | |
| 45 | + | |
| 46 | +// Validate checks the MCP server configuration. | |
| 47 | +func (s MCPServer) Validate() error { | |
| 48 | + return firstError( | |
| 49 | + problem(s.Name == "", "mcp server name must not be empty"), | |
| 50 | + problem(s.Transport == "", "mcp server transport must be 'http' or 'stdio'"), | |
| 51 | + problem(s.Transport == "http" && s.URL == "", "mcp server with http transport must have a url"), | |
| 52 | + problem(s.Transport == "stdio" && s.Command == "", "mcp server with stdio transport must have a command"), | |
| 53 | + problem(s.Transport != "http" && s.Transport != "stdio", fmt.Sprintf("mcp server transport must be 'http' or 'stdio', got %q", s.Transport)), | |
| 54 | + problem(!knownAuthType(s.Auth.Type), fmt.Sprintf("mcp auth type must be 'none', 'bearer', 'api_key', or 'oauth2', got %q", s.Auth.Type)), | |
| 55 | + problem(s.Auth.Type == "bearer" && s.Auth.TokenEnv == "", "mcp bearer auth must specify tokenEnv"), | |
| 56 | + problem(s.Auth.Type == "api_key" && s.Auth.APIKeyEnv == "", "mcp api_key auth must specify apiKeyEnv"), | |
| 57 | + ) | |
| 58 | +} | |
| 59 | + | |
| 60 | +// knownAuthType tells whether an MCP auth type is one mm knows; "" is none. | |
| 61 | +func knownAuthType(t string) bool { | |
| 62 | + switch t { | |
| 63 | + case "", "none", "bearer", "api_key", "oauth2": | |
| 64 | + return true | |
| 65 | + } | |
| 66 | + return false | |
| 67 | +} | |
| 68 | + | |
| 69 | +// validate checks the MCP configuration. | |
| 70 | +func (m MCPConfig) Validate() error { | |
| 71 | + if !m.Enabled { | |
| 72 | + return nil // MCP disabled, nothing to validate | |
| 73 | + } | |
| 74 | + if m.Permissions != "" && m.Permissions != PermissionsAuto && m.Permissions != PermissionsAsk { | |
| 75 | + return fmt.Errorf("mcp.permissions must be 'auto' or 'ask', got %q", m.Permissions) | |
| 76 | + } | |
| 77 | + for i, s := range m.Servers { | |
| 78 | + if err := s.Validate(); err != nil { | |
| 79 | + return fmt.Errorf("mcp.servers[%d]: %w", i, err) | |
| 80 | + } | |
| 81 | + } | |
| 82 | + return nil | |
| 83 | +} | |
| 84 | + | |
| 85 | +// validate checks the language servers: every one named and runnable, every | |
| 86 | +// extension written with its dot and served once — two servers for ".ts" | |
| 87 | +// would make the tool's choice arbitrary. | |
| 88 | +func (l LSPConfig) validate() error { | |
| 89 | + seen := map[string]string{} | |
| 90 | + for i, s := range l.Servers { | |
| 91 | + if err := s.validate(fmt.Sprintf("lsp.servers[%d]", i), seen); err != nil { | |
| 92 | + return err | |
| 93 | + } | |
| 94 | + } | |
| 95 | + return nil | |
| 96 | +} | |
| 97 | + | |
| 98 | +// validate checks one server and claims its extensions in seen (extension → | |
| 99 | +// server name), so a second server on the same extension is refused. | |
| 100 | +func (s LSPServer) validate(where string, seen map[string]string) error { | |
| 101 | + switch { | |
| 102 | + case s.Name == "": | |
| 103 | + return errors.New(where + ".name must not be empty") | |
| 104 | + case s.Command == "": | |
| 105 | + return errors.New(where + ".command must not be empty") | |
| 106 | + case len(s.Extensions) == 0: | |
| 107 | + return errors.New(where + ".extensions must list at least one extension") | |
| 108 | + } | |
| 109 | + for _, e := range s.Extensions { | |
| 110 | + if err := s.claimExtension(where, e, seen); err != nil { | |
| 111 | + return err | |
| 112 | + } | |
| 113 | + } | |
| 114 | + return nil | |
| 115 | +} | |
| 116 | + | |
| 117 | +// claimExtension checks one extension's spelling and records the server as | |
| 118 | +// its owner, refusing an extension another server already owns. | |
| 119 | +func (s LSPServer) claimExtension(where, e string, seen map[string]string) error { | |
| 120 | + ext := strings.ToLower(e) | |
| 121 | + if !strings.HasPrefix(ext, ".") || len(ext) < 2 { | |
| 122 | + return fmt.Errorf("%s.extensions: %q must start with a dot, like \".go\"", where, e) | |
| 123 | + } | |
| 124 | + if other, dup := seen[ext]; dup { | |
| 125 | + return fmt.Errorf("%s: extension %q is already served by %q", where, e, other) | |
| 126 | + } | |
| 127 | + seen[ext] = s.Name | |
| 128 | + return nil | |
| 129 | +} | |
| 130 | + | |
| 131 | +// validate catches the settings that would break the agent at runtime. | |
| 132 | +func (c Config) validate() error { | |
| 133 | + // `enabled: true` with no window and no maxMessages would never trigger — | |
| 134 | + // but the window may still come from the server's probe at start-up, so | |
| 135 | + // that case is a warning in main, not an error here. | |
| 136 | + return firstFailing( | |
| 137 | + c.LlamaCpp.validate, | |
| 138 | + c.Razorbill.validate, | |
| 139 | + c.A2A.validate, | |
| 140 | + func() error { return validatePrompt(c.System, c.Variables) }, | |
| 141 | + c.modelError, | |
| 142 | + c.runError, | |
| 143 | + c.sectionError, | |
| 144 | + c.MCP.Validate, | |
| 145 | + c.ACP.ClientTools.validate, | |
| 146 | + c.validateAgents, | |
| 147 | + c.Hooks.validate, | |
| 148 | + c.validateTools, | |
| 149 | + func() error { return validateMetaPrompts(c.MetaPrompts) }, | |
| 150 | + c.DocSearch.validate, | |
| 151 | + c.validateDecision, | |
| 152 | + c.SkillRouting.validate, | |
| 153 | + c.ToolRouting.validate, | |
| 154 | + func() error { return c.DecisionContext.validate(c.Decision != nil) }, | |
| 155 | + func() error { return c.DecisionTool.validate(c.Decision != nil) }, | |
| 156 | + func() error { return c.DecisionResult.validate(c.Decision != nil) }, | |
| 157 | + c.LSP.validate, | |
| 158 | + ) | |
| 159 | +} | |
| 160 | + | |
| 161 | +// modelError checks the keys that say which model answers, and how. | |
| 162 | +func (c Config) modelError() error { | |
| 163 | + return firstError( | |
| 164 | + problem(c.Provider == "", "provider must not be empty"), | |
| 165 | + problem(c.Model == "", "model must not be empty"), | |
| 166 | + problem(c.ContextWindow < 0, "contextWindow must be >= 0"), | |
| 167 | + problem(c.ToolFormat != "" && c.ToolFormat != ToolFormatOpenAI && c.ToolFormat != ToolFormatXML, | |
| 168 | + fmt.Sprintf("toolFormat must be %s or %s, not %q", ToolFormatOpenAI, ToolFormatXML, c.ToolFormat)), | |
| 169 | + problem(c.FirstTokenTimeout < 0, "firstTokenTimeout must be >= 0"), | |
| 170 | + ) | |
| 171 | +} | |
| 172 | + | |
| 173 | +// runError checks the limits of a turn and of its commands. | |
| 174 | +func (c Config) runError() error { | |
| 175 | + return firstError( | |
| 176 | + problem(c.BashTimeout <= 0, "bashTimeout must be > 0"), | |
| 177 | + problem(c.BashBackground != BashBackgroundReport && c.BashBackground != BashBackgroundStop && c.BashBackground != BashBackgroundAllow, | |
| 178 | + fmt.Sprintf("bashBackground must be %q, %q or %q, not %q", BashBackgroundReport, BashBackgroundStop, BashBackgroundAllow, c.BashBackground)), | |
| 179 | + problem(c.MaxOutput <= 0, "maxOutput must be > 0"), | |
| 180 | + problem(c.MaxTurns <= 0, "maxTurns must be > 0"), | |
| 181 | + problem(c.PreviewLines < 0, "previewLines must be >= 0"), | |
| 182 | + ) | |
| 183 | +} | |
| 184 | + | |
| 185 | +// sectionError checks the context, sessions, trace, agentsMd, acp and | |
| 186 | +// permissions keys. | |
| 187 | +func (c Config) sectionError() error { | |
| 188 | + return firstError( | |
| 189 | + problem(c.Context.Threshold < 1 || c.Context.Threshold > 100, "context.threshold must be between 1 and 100"), | |
| 190 | + problem(c.Context.KeepLastTurns < 1, "context.keepLastTurns must be >= 1"), | |
| 191 | + problem(c.Context.SummaryMaxTokens <= 0, "context.summaryMaxTokens must be > 0"), | |
| 192 | + problem(c.Context.MaxMessages < 0, "context.maxMessages must be >= 0"), | |
| 193 | + problem(c.Sessions.Dir == "", "sessions.dir must not be empty"), | |
| 194 | + problem(c.Trace.Enabled && strings.TrimSpace(c.Trace.Dir) == "", "trace.dir must not be empty"), | |
| 195 | + problem(c.AgentsMd.Enabled && strings.TrimSpace(c.AgentsMd.Path) == "", "agentsMd.path must not be empty"), | |
| 196 | + problem(c.AgentsMd.MaxChars < 0, "agentsMd.maxChars must be >= 0"), | |
| 197 | + problem(strings.TrimSpace(c.ACP.Name) == "", "acp.name must not be empty"), | |
| 198 | + problem(c.Permissions != "" && c.Permissions != PermissionsAuto && c.Permissions != PermissionsAsk, `permissions must be "auto" or "ask"`), | |
| 199 | + ) | |
| 200 | +} | |
| new file mode 100644 | |||
| @@ -0,0 +1,200 @@ | |||
| 1 | +package config | ||
| 2 | + | ||
| 3 | +import ( | ||
| 4 | + "errors" | ||
| 5 | + "fmt" | ||
| 6 | + "strings" | ||
| 7 | +) | ||
| 8 | + | ||
| 9 | +// validate refuses a value llama-server would refuse at start-up — better | ||
| 10 | +// a configuration error naming the key than a server that does not start. | ||
| 11 | +func (l LlamaCppConfig) validate() error { | ||
| 12 | + for _, f := range []struct { | ||
| 13 | + key string | ||
| 14 | + value *int | ||
| 15 | + min int | ||
| 16 | + }{ | ||
| 17 | + {"ctxSize", l.CtxSize, 0}, | ||
| 18 | + {"modelsMax", l.ModelsMax, 0}, | ||
| 19 | + {"modelsMemoryMargin", l.ModelsMemoryMargin, 0}, | ||
| 20 | + {"fitTarget", l.FitTarget, 0}, | ||
| 21 | + {"sleepIdleSeconds", l.SleepIdleSeconds, -1}, | ||
| 22 | + } { | ||
| 23 | + if f.value != nil && *f.value < f.min { | ||
| 24 | + return fmt.Errorf("llamacpp.%s must be >= %d, not %d", f.key, f.min, *f.value) | ||
| 25 | + } | ||
| 26 | + } | ||
| 27 | + if l.StartTimeout < 0 { | ||
| 28 | + return errors.New("llamacpp.startTimeout must be >= 0") | ||
| 29 | + } | ||
| 30 | + return nil | ||
| 31 | +} | ||
| 32 | + | ||
| 33 | +// validate refuses the values the ACP front end could not act on. | ||
| 34 | +func (c ClientToolsConfig) validate() error { | ||
| 35 | + switch { | ||
| 36 | + case c.Discovery != DiscoveryMeta && c.Discovery != DiscoveryRequest: | ||
| 37 | + return fmt.Errorf("acp.clientTools.discovery must be %q or %q, got %q", DiscoveryMeta, DiscoveryRequest, c.Discovery) | ||
| 38 | + case c.Permissions != PermissionsAuto && c.Permissions != PermissionsAsk: | ||
| 39 | + return fmt.Errorf("acp.clientTools.permissions must be %q or %q, got %q", PermissionsAuto, PermissionsAsk, c.Permissions) | ||
| 40 | + case c.Timeout <= 0: | ||
| 41 | + return errors.New("acp.clientTools.timeout must be > 0") | ||
| 42 | + } | ||
| 43 | + return nil | ||
| 44 | +} | ||
| 45 | + | ||
| 46 | +// Validate checks the MCP server configuration. | ||
| 47 | +func (s MCPServer) Validate() error { | ||
| 48 | + return firstError( | ||
| 49 | + problem(s.Name == "", "mcp server name must not be empty"), | ||
| 50 | + problem(s.Transport == "", "mcp server transport must be 'http' or 'stdio'"), | ||
| 51 | + problem(s.Transport == "http" && s.URL == "", "mcp server with http transport must have a url"), | ||
| 52 | + problem(s.Transport == "stdio" && s.Command == "", "mcp server with stdio transport must have a command"), | ||
| 53 | + problem(s.Transport != "http" && s.Transport != "stdio", fmt.Sprintf("mcp server transport must be 'http' or 'stdio', got %q", s.Transport)), | ||
| 54 | + problem(!knownAuthType(s.Auth.Type), fmt.Sprintf("mcp auth type must be 'none', 'bearer', 'api_key', or 'oauth2', got %q", s.Auth.Type)), | ||
| 55 | + problem(s.Auth.Type == "bearer" && s.Auth.TokenEnv == "", "mcp bearer auth must specify tokenEnv"), | ||
| 56 | + problem(s.Auth.Type == "api_key" && s.Auth.APIKeyEnv == "", "mcp api_key auth must specify apiKeyEnv"), | ||
| 57 | + ) | ||
| 58 | +} | ||
| 59 | + | ||
| 60 | +// knownAuthType tells whether an MCP auth type is one mm knows; "" is none. | ||
| 61 | +func knownAuthType(t string) bool { | ||
| 62 | + switch t { | ||
| 63 | + case "", "none", "bearer", "api_key", "oauth2": | ||
| 64 | + return true | ||
| 65 | + } | ||
| 66 | + return false | ||
| 67 | +} | ||
| 68 | + | ||
| 69 | +// validate checks the MCP configuration. | ||
| 70 | +func (m MCPConfig) Validate() error { | ||
| 71 | + if !m.Enabled { | ||
| 72 | + return nil // MCP disabled, nothing to validate | ||
| 73 | + } | ||
| 74 | + if m.Permissions != "" && m.Permissions != PermissionsAuto && m.Permissions != PermissionsAsk { | ||
| 75 | + return fmt.Errorf("mcp.permissions must be 'auto' or 'ask', got %q", m.Permissions) | ||
| 76 | + } | ||
| 77 | + for i, s := range m.Servers { | ||
| 78 | + if err := s.Validate(); err != nil { | ||
| 79 | + return fmt.Errorf("mcp.servers[%d]: %w", i, err) | ||
| 80 | + } | ||
| 81 | + } | ||
| 82 | + return nil | ||
| 83 | +} | ||
| 84 | + | ||
| 85 | +// validate checks the language servers: every one named and runnable, every | ||
| 86 | +// extension written with its dot and served once — two servers for ".ts" | ||
| 87 | +// would make the tool's choice arbitrary. | ||
| 88 | +func (l LSPConfig) validate() error { | ||
| 89 | + seen := map[string]string{} | ||
| 90 | + for i, s := range l.Servers { | ||
| 91 | + if err := s.validate(fmt.Sprintf("lsp.servers[%d]", i), seen); err != nil { | ||
| 92 | + return err | ||
| 93 | + } | ||
| 94 | + } | ||
| 95 | + return nil | ||
| 96 | +} | ||
| 97 | + | ||
| 98 | +// validate checks one server and claims its extensions in seen (extension → | ||
| 99 | +// server name), so a second server on the same extension is refused. | ||
| 100 | +func (s LSPServer) validate(where string, seen map[string]string) error { | ||
| 101 | + switch { | ||
| 102 | + case s.Name == "": | ||
| 103 | + return errors.New(where + ".name must not be empty") | ||
| 104 | + case s.Command == "": | ||
| 105 | + return errors.New(where + ".command must not be empty") | ||
| 106 | + case len(s.Extensions) == 0: | ||
| 107 | + return errors.New(where + ".extensions must list at least one extension") | ||
| 108 | + } | ||
| 109 | + for _, e := range s.Extensions { | ||
| 110 | + if err := s.claimExtension(where, e, seen); err != nil { | ||
| 111 | + return err | ||
| 112 | + } | ||
| 113 | + } | ||
| 114 | + return nil | ||
| 115 | +} | ||
| 116 | + | ||
| 117 | +// claimExtension checks one extension's spelling and records the server as | ||
| 118 | +// its owner, refusing an extension another server already owns. | ||
| 119 | +func (s LSPServer) claimExtension(where, e string, seen map[string]string) error { | ||
| 120 | + ext := strings.ToLower(e) | ||
| 121 | + if !strings.HasPrefix(ext, ".") || len(ext) < 2 { | ||
| 122 | + return fmt.Errorf("%s.extensions: %q must start with a dot, like \".go\"", where, e) | ||
| 123 | + } | ||
| 124 | + if other, dup := seen[ext]; dup { | ||
| 125 | + return fmt.Errorf("%s: extension %q is already served by %q", where, e, other) | ||
| 126 | + } | ||
| 127 | + seen[ext] = s.Name | ||
| 128 | + return nil | ||
| 129 | +} | ||
| 130 | + | ||
| 131 | +// validate catches the settings that would break the agent at runtime. | ||
| 132 | +func (c Config) validate() error { | ||
| 133 | + // `enabled: true` with no window and no maxMessages would never trigger — | ||
| 134 | + // but the window may still come from the server's probe at start-up, so | ||
| 135 | + // that case is a warning in main, not an error here. | ||
| 136 | + return firstFailing( | ||
| 137 | + c.LlamaCpp.validate, | ||
| 138 | + c.Razorbill.validate, | ||
| 139 | + c.A2A.validate, | ||
| 140 | + func() error { return validatePrompt(c.System, c.Variables) }, | ||
| 141 | + c.modelError, | ||
| 142 | + c.runError, | ||
| 143 | + c.sectionError, | ||
| 144 | + c.MCP.Validate, | ||
| 145 | + c.ACP.ClientTools.validate, | ||
| 146 | + c.validateAgents, | ||
| 147 | + c.Hooks.validate, | ||
| 148 | + c.validateTools, | ||
| 149 | + func() error { return validateMetaPrompts(c.MetaPrompts) }, | ||
| 150 | + c.DocSearch.validate, | ||
| 151 | + c.validateDecision, | ||
| 152 | + c.SkillRouting.validate, | ||
| 153 | + c.ToolRouting.validate, | ||
| 154 | + func() error { return c.DecisionContext.validate(c.Decision != nil) }, | ||
| 155 | + func() error { return c.DecisionTool.validate(c.Decision != nil) }, | ||
| 156 | + func() error { return c.DecisionResult.validate(c.Decision != nil) }, | ||
| 157 | + c.LSP.validate, | ||
| 158 | + ) | ||
| 159 | +} | ||
| 160 | + | ||
| 161 | +// modelError checks the keys that say which model answers, and how. | ||
| 162 | +func (c Config) modelError() error { | ||
| 163 | + return firstError( | ||
| 164 | + problem(c.Provider == "", "provider must not be empty"), | ||
| 165 | + problem(c.Model == "", "model must not be empty"), | ||
| 166 | + problem(c.ContextWindow < 0, "contextWindow must be >= 0"), | ||
| 167 | + problem(c.ToolFormat != "" && c.ToolFormat != ToolFormatOpenAI && c.ToolFormat != ToolFormatXML, | ||
| 168 | + fmt.Sprintf("toolFormat must be %s or %s, not %q", ToolFormatOpenAI, ToolFormatXML, c.ToolFormat)), | ||
| 169 | + problem(c.FirstTokenTimeout < 0, "firstTokenTimeout must be >= 0"), | ||
| 170 | + ) | ||
| 171 | +} | ||
| 172 | + | ||
| 173 | +// runError checks the limits of a turn and of its commands. | ||
| 174 | +func (c Config) runError() error { | ||
| 175 | + return firstError( | ||
| 176 | + problem(c.BashTimeout <= 0, "bashTimeout must be > 0"), | ||
| 177 | + problem(c.BashBackground != BashBackgroundReport && c.BashBackground != BashBackgroundStop && c.BashBackground != BashBackgroundAllow, | ||
| 178 | + fmt.Sprintf("bashBackground must be %q, %q or %q, not %q", BashBackgroundReport, BashBackgroundStop, BashBackgroundAllow, c.BashBackground)), | ||
| 179 | + problem(c.MaxOutput <= 0, "maxOutput must be > 0"), | ||
| 180 | + problem(c.MaxTurns <= 0, "maxTurns must be > 0"), | ||
| 181 | + problem(c.PreviewLines < 0, "previewLines must be >= 0"), | ||
| 182 | + ) | ||
| 183 | +} | ||
| 184 | + | ||
| 185 | +// sectionError checks the context, sessions, trace, agentsMd, acp and | ||
| 186 | +// permissions keys. | ||
| 187 | +func (c Config) sectionError() error { | ||
| 188 | + return firstError( | ||
| 189 | + problem(c.Context.Threshold < 1 || c.Context.Threshold > 100, "context.threshold must be between 1 and 100"), | ||
| 190 | + problem(c.Context.KeepLastTurns < 1, "context.keepLastTurns must be >= 1"), | ||
| 191 | + problem(c.Context.SummaryMaxTokens <= 0, "context.summaryMaxTokens must be > 0"), | ||
| 192 | + problem(c.Context.MaxMessages < 0, "context.maxMessages must be >= 0"), | ||
| 193 | + problem(c.Sessions.Dir == "", "sessions.dir must not be empty"), | ||
| 194 | + problem(c.Trace.Enabled && strings.TrimSpace(c.Trace.Dir) == "", "trace.dir must not be empty"), | ||
| 195 | + problem(c.AgentsMd.Enabled && strings.TrimSpace(c.AgentsMd.Path) == "", "agentsMd.path must not be empty"), | ||
| 196 | + problem(c.AgentsMd.MaxChars < 0, "agentsMd.maxChars must be >= 0"), | ||
| 197 | + problem(strings.TrimSpace(c.ACP.Name) == "", "acp.name must not be empty"), | ||
| 198 | + problem(c.Permissions != "" && c.Permissions != PermissionsAuto && c.Permissions != PermissionsAsk, `permissions must be "auto" or "ask"`), | ||
| 199 | + ) | ||
| 200 | +} | ||