refine rule criteria, move mode and message to db config - #1396
Conversation
PR Summary by QodoRefine agent rule criteria and move mode/message into persisted rule config
AI Description
Diagram
High-Level Assessment
Files changed (8)
|
Code Review by Qodo
1. Rule mode needs options
|
| var evaluator = ResolveCriteriaEvaluator(rule.Criteria?.Mode) ?? criteriaEvaluator; | ||
| if (evaluator != null && options?.Criteria != null) |
There was a problem hiding this comment.
1. Rule mode needs options 🐞 Bug ≡ Correctness
RuleEngine resolves an evaluator from rule.Criteria.Mode but only evaluates criteria when options.Criteria is non-null, so rule-configured criteria can be silently ignored and the rule executes unconditionally.
Agent Prompt
## Issue description
RuleEngine selects an evaluator from `rule.Criteria?.Mode`, but the criteria evaluation block is gated by `options?.Criteria != null`. This prevents rule-owned criteria from being enforced unless the caller also sends a Criteria options object.
## Issue Context
This directly contradicts the comment that “the rule's own mode wins … without the caller knowing”, and it can cause rules to trigger even when their criteria should block them.
## Fix Focus Areas
- src/Infrastructure/BotSharp.Core.Rules/Engines/RuleEngine.cs[53-69]
### Suggested implementation direction
- Change the guard to evaluate criteria when `evaluator != null`.
- When `options?.Criteria` is null, pass a default `CriteriaOptions` into `RuleCriteriaContext` (so evaluators can still read settings as default/null) e.g. `Options = options?.Criteria ?? new CriteriaOptions()`.
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
| /// Selects which <c>IRuleCriteriaEvaluator</c> handles this criteria. | ||
| /// </summary> | ||
| public string Type { get; set; } = BuiltInRuleCriteria.Code; | ||
| public string? Mode { get; set; } |
There was a problem hiding this comment.
2. Missing default criteria mode 🐞 Bug ≡ Correctness
CriteriaOptions no longer has a default mode, and ResolveCriteriaEvaluator returns null for blank mode; when callers omit the mode (or still send legacy fields), the engine skips EvaluateCriteria and still triggers the rule.
Agent Prompt
## Issue description
`CriteriaOptions` changed from a non-null `Type` with a default value to a nullable `Mode` with no default. In RuleEngine, a null/blank mode resolves no evaluator, and criteria evaluation is skipped while rule triggering continues.
## Issue Context
Previously, `CriteriaOptions.Type` defaulted to the built-in code criteria type. Now, `Mode` can be omitted by callers (including older clients), and the system can silently stop applying criteria.
## Fix Focus Areas
- src/Infrastructure/BotSharp.Abstraction/Rules/Options/RuleTriggerOptions.cs[20-33]
- src/Infrastructure/BotSharp.Core.Rules/Engines/RuleEngine.cs[32-41]
- src/Infrastructure/BotSharp.Core.Rules/Engines/RuleEngine.cs[79-88]
### Suggested implementation direction
Choose at least one:
1) Give `CriteriaOptions.Mode` a safe default (e.g. `BuiltInRuleCriteria.Llm` or `BuiltInRuleCriteria.PythonScript`, depending on intended behavior).
2) Add backward-compatible JSON aliasing (e.g., keep an obsolete `Type` property mapped from `"type"` and translate it into `Mode`).
3) If Mode is absent, fail closed (do not trigger) when criteria evaluation is expected.
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
| /// Evaluate a code script (e.g. Python) that returns a boolean result. | ||
| /// </summary> | ||
| public const string Code = "code"; | ||
| public const string PythonScript = "python_script"; |
There was a problem hiding this comment.
3. Criteria identifier renamed 🐞 Bug ☼ Reliability
The built-in criteria identifier changed from "code" to "python_script" and the evaluator now advertises only python_script; any persisted/requested mode value "code" will no longer resolve, causing criteria to be skipped.
Agent Prompt
## Issue description
`BuiltInRuleCriteria` removed/renamed the prior identifier and `CodeCriteriaEvaluator` now reports `Type = "python_script"`. Evaluator resolution is strict string matching, so legacy `mode: "code"` configurations stop working.
## Issue Context
RuleEngine resolves evaluators by comparing evaluator.Type to the configured mode (`x.Type.IsEqualTo(mode)`). With the rename, older stored configs or API callers using `"code"` will resolve no evaluator.
## Fix Focus Areas
- src/Infrastructure/BotSharp.Abstraction/Rules/Constants/BuiltInRuleCriteria.cs[8-19]
- src/Infrastructure/BotSharp.Core.Rules/Criteria/Code/CodeCriteriaEvaluator.cs[24-34]
- src/Infrastructure/BotSharp.Core.Rules/Engines/RuleEngine.cs[79-88]
### Suggested implementation direction
- Add a legacy alias constant (e.g. `public const string Code = "code";`) and/or map `"code"` -> `"python_script"` inside `ResolveCriteriaEvaluator`.
- Alternatively, let the evaluator advertise both identifiers (e.g. by matching on a small set) via resolver mapping logic.
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
| public string? Message { get; set; } | ||
| public RuleCriteriaMongoModel? Criteria { get; set; } |
There was a problem hiding this comment.
4. Mongo schema not migrated 🐞 Bug ☼ Reliability
Mongo AgentRule storage renamed Config -> Criteria and added Message without a backward-compatible read path; existing documents with Config will deserialize without Criteria, dropping stored criteria/mode and altering triggering behavior.
Agent Prompt
## Issue description
Mongo persistence changed the stored shape from `Config` to `Message` + `Criteria`. With no legacy field mapping, rules persisted under the previous schema will load with `Criteria == null` and lose their stored criteria/mode.
## Issue Context
`AgentRuleMongoElement` no longer has a `Config` property. With `BsonIgnoreExtraElements`, unknown fields are ignored rather than mapped, so old data is effectively dropped on read.
## Fix Focus Areas
- src/Plugins/BotSharp.Plugin.MongoStorage/Models/AgentRuleMongoElement.cs[5-33]
- src/Infrastructure/BotSharp.Abstraction/Agents/Models/AgentRule.cs[3-20]
### Suggested implementation direction
- Add a legacy `Config` property (marked obsolete) and translate it into `Criteria` during `ToDomainElement`.
- Or use BSON aliases (e.g., `[BsonElement("Config")]`) / custom deserialization to read old `Config` into new `Criteria`.
- Consider a one-time migration to rewrite stored documents from `Config` to `Criteria` and add `Message`.
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
No description provided.