Repository navigation
feat: AI alerts checked by the analyst agent #10231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nishantmonu51
wants to merge
9
commits into
nishant/resolver-props-yaml
Choose a base branch
from
nishant/ai-alerts
base: nishant/resolver-props-yaml
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,933
−289
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
fbe5ea1
fix(admin): write resolver properties of alerts and reports as plain …
dfliess 587d2c3
feat(runtime): evaluate AI alerts with the analyst agent
dfliess 012f126
feat(alerts): add an AI condition to the alert dialog
dfliess 52fbcbf
Merge main into AI alerts, and address the backend review
nishantmonu51 e194251
fix(alerts): convert a measure filter's subquery to the resolver's shape
nishantmonu51 71c2fed
feat(alerts): redo the AI condition on the current alert dialog
nishantmonu51 51d3949
fix(alerts): tighten AI alert data choices, open links and docs after…
nishantmonu51 93771e7
fix(alerts): notify AI alert errors and cut a reason without a summar…
nishantmonu51 14c91f5
fix(alerts): escape an alert's error message in Slack after review
nishantmonu51 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| package ai_test | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/google/jsonschema-go/jsonschema" | ||
| aiv1 "github.com/rilldata/rill/proto/gen/rill/ai/v1" | ||
| "github.com/rilldata/rill/runtime/ai" | ||
| "github.com/rilldata/rill/runtime/drivers" | ||
| "github.com/rilldata/rill/runtime/testruntime" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestAnalystAlert(t *testing.T) { | ||
| alertArgs := &ai.RouterAgentArgs{ | ||
| Prompt: "There are orders from Denmark", | ||
| Agent: ai.AnalystAgentName, | ||
| AnalystAgentArgs: &ai.AnalystAgentArgs{IsAlert: true}, | ||
| } | ||
| analysis := "<summary>Denmark has orders.</summary> Orders come from Denmark." | ||
|
|
||
| cases := []struct { | ||
| verdict string | ||
| want ai.AlertOutcome | ||
| wantErr string | ||
| }{ | ||
| {verdict: `{"outcome": "met"}`, want: ai.AlertOutcomeMet}, | ||
| {verdict: `{"outcome": "not_met"}`, want: ai.AlertOutcomeNotMet}, | ||
| {verdict: `{"outcome": "cannot_determine"}`, want: ai.AlertOutcomeCannotDetermine}, | ||
| {verdict: `{"outcome": "maybe"}`, wantErr: "invalid verdict"}, | ||
| {verdict: `The condition holds.`, wantErr: "verdict"}, | ||
| } | ||
| for _, tc := range cases { | ||
| t.Run(tc.verdict, func(t *testing.T) { | ||
| var schemas []*jsonschema.Schema | ||
| recordSchema := func(turn turnFunc) turnFunc { | ||
| return func(opts *drivers.CompleteOptions) *aiv1.CompletionMessage { | ||
| schemas = append(schemas, opts.OutputSchema) | ||
| return turn(opts) | ||
| } | ||
| } | ||
| script := &scriptedAIService{turns: []turnFunc{recordSchema(textTurn(analysis)), recordSchema(textTurn(tc.verdict))}} | ||
| s := newSkillReferencesSession(t, script) | ||
|
|
||
| var res *ai.RouterAgentResult | ||
| _, err := s.CallTool(t.Context(), ai.RoleUser, ai.RouterAgentName, &res, alertArgs) | ||
| if tc.wantErr != "" { | ||
| require.ErrorContains(t, err, tc.wantErr) | ||
| return | ||
| } | ||
| require.NoError(t, err) | ||
| require.Equal(t, tc.want, res.AlertOutcome) | ||
| require.Equal(t, analysis, res.Response) | ||
|
|
||
| // The analysis runs with tools and the verdict with an output schema, in separate requests, | ||
| // since some providers reject a request that combines them. | ||
| require.Len(t, schemas, 2) | ||
| require.Contains(t, script.tools[0], ai.QueryMetricsViewName) | ||
| require.Nil(t, schemas[0]) | ||
| require.Empty(t, script.tools[1]) | ||
| require.Contains(t, schemas[1].Properties, "outcome") | ||
| // The verdict is given on the analysis | ||
| require.Contains(t, script.inputs[1][len(script.inputs[1])-1].Content[0].GetText(), analysis) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestAnalystAlertEval(t *testing.T) { | ||
| // Eight weeks of daily revenue for three countries. | ||
| // Revenue is around 100 a day for every country, except Spain, which drops to around 30 a day in the last week. | ||
| rt, instanceID := testruntime.NewInstanceWithOptions(t, testruntime.InstanceOptions{ | ||
| AIConnector: "openai", | ||
| Files: map[string]string{ | ||
| "models/orders.yaml": ` | ||
| type: model | ||
| materialize: true | ||
| sql: | | ||
| SELECT | ||
| d::TIMESTAMP AS event_time, | ||
| c AS country, | ||
| CASE WHEN c = 'Spain' AND d >= DATE '2025-10-19' THEN 30 ELSE 100 END + (hash(d::DATE, c) % 7)::INTEGER AS revenue | ||
| FROM range(DATE '2025-08-31', DATE '2025-10-26', INTERVAL 1 DAY) AS t(d), (VALUES ('Spain'), ('France'), ('Germany')) AS countries(c) | ||
| `, | ||
| "metrics/orders.yaml": ` | ||
| type: metrics_view | ||
| model: orders | ||
| timeseries: event_time | ||
| dimensions: | ||
| - column: country | ||
| measures: | ||
| - name: revenue | ||
| expression: SUM(revenue) | ||
| `, | ||
| "explores/orders.yaml": ` | ||
| type: explore | ||
| metrics_view: orders | ||
| `, | ||
| }, | ||
| }) | ||
| testruntime.RequireReconcileState(t, rt, instanceID, 3, 0, 0) | ||
|
|
||
| cases := []struct { | ||
| condition string | ||
| want ai.AlertOutcome | ||
| }{ | ||
| {"Revenue in Spain dropped sharply in the last week compared with the previous weeks", ai.AlertOutcomeMet}, | ||
| {"Spain is the country with the lowest revenue in the last week", ai.AlertOutcomeMet}, | ||
| {"Revenue in France dropped sharply in the last week compared with the previous weeks", ai.AlertOutcomeNotMet}, | ||
| {"There are orders from Italy", ai.AlertOutcomeNotMet}, | ||
| // The data has no profit | ||
| {"The profit margin in Spain fell below 10% in the last week", ai.AlertOutcomeCannotDetermine}, | ||
| } | ||
| for _, tc := range cases { | ||
| t.Run(tc.condition, func(t *testing.T) { | ||
| s := newEval(t, rt, instanceID) | ||
| var res *ai.RouterAgentResult | ||
| _, err := s.CallTool(t.Context(), ai.RoleUser, ai.RouterAgentName, &res, &ai.RouterAgentArgs{ | ||
| Prompt: tc.condition, | ||
| Agent: ai.AnalystAgentName, | ||
| AnalystAgentArgs: &ai.AnalystAgentArgs{Explore: "orders", IsAlert: true}, | ||
| }) | ||
| require.NoError(t, err) | ||
| require.Equal(t, tc.want, res.AlertOutcome, res.Response) | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With
on_errorenabled, acannot_determinereason goes throughSendAlertStatusasExecutionError. That branch puts the analyst’s text straight into Slack markup, so angle-bracketed text can turn into a link or mention. Could we useslackTextEscaper.Replace(s.ExecutionError)forErrorMessage, like the alert-triggered branch does forReason? Slack’s escaping rulesThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 14c91f5: the error branch now escapes
ExecutionErrorwithslackTextEscaper, for every alert, and the Slack notifier test covers it.