Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions backend/pkg/database/msgchains.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions backend/pkg/database/querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 16 additions & 5 deletions backend/pkg/providers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,19 @@ func (fp *flowProvider) getTaskMsgLogsSummary(
return summary, nil
}

func scopedMsgChainLookupParams(
flowID int64,
taskID, subtaskID *int64,
msgChainType database.MsgchainType,
) database.GetFlowTaskSubtaskTypeLastMsgChainParams {
return database.GetFlowTaskSubtaskTypeLastMsgChainParams{
FlowID: flowID,
TaskID: database.Int64ToNullInt64(taskID),
SubtaskID: database.Int64ToNullInt64(subtaskID),
Type: msgChainType,
}
}

func (fp *flowProvider) restoreChain(
ctx context.Context,
taskID, subtaskID *int64,
Expand All @@ -454,11 +467,9 @@ func (fp *flowProvider) restoreChain(
ctx, observation := obs.Observer.NewObservation(ctx)

// Get raw chain from DB for observation input
msgChain, err := fp.db.GetFlowTaskTypeLastMsgChain(ctx, database.GetFlowTaskTypeLastMsgChainParams{
FlowID: fp.flowID,
TaskID: database.Int64ToNullInt64(taskID),
Type: msgChainType,
})
msgChain, err := fp.db.GetFlowTaskSubtaskTypeLastMsgChain(ctx, scopedMsgChainLookupParams(
fp.flowID, taskID, subtaskID, msgChainType,
))

var rawChain []llms.MessageContent
if err == nil && !isEmptyChain(msgChain.Chain) {
Expand Down
28 changes: 28 additions & 0 deletions backend/pkg/providers/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"pentagi/pkg/cast"
"pentagi/pkg/config"
"pentagi/pkg/database"

"github.com/stretchr/testify/assert"
"github.com/vxcontrol/langchaingo/llms"
Expand Down Expand Up @@ -1096,6 +1097,33 @@ func TestExecutionMonitorDetector_ShouldInvokeAdviser(t *testing.T) {
}
}

func TestScopedMsgChainLookupParams(t *testing.T) {
taskID := int64(12)
subtaskID := int64(34)

tests := []struct {
name string
taskID *int64
subtaskID *int64
wantTask *int64
wantSub *int64
}{
{name: "assistant scope keeps null identifiers"},
{name: "task scope keeps a null subtask", taskID: &taskID, wantTask: &taskID},
{name: "subtask scope includes both identifiers", taskID: &taskID, subtaskID: &subtaskID, wantTask: &taskID, wantSub: &subtaskID},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
params := scopedMsgChainLookupParams(7, tt.taskID, tt.subtaskID, database.MsgchainTypeCoder)
assert.Equal(t, int64(7), params.FlowID)
assert.Equal(t, database.Int64ToNullInt64(tt.wantTask), params.TaskID)
assert.Equal(t, database.Int64ToNullInt64(tt.wantSub), params.SubtaskID)
assert.Equal(t, database.MsgchainTypeCoder, params.Type)
})
}
}

func TestExecutionMonitorDetector_Reset(t *testing.T) {
emd := &executionMonitor{
enabled: true,
Expand Down
11 changes: 11 additions & 0 deletions backend/sqlc/models/msgchains.sql
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,17 @@ WHERE mc.flow_id = $1 AND (mc.task_id = $2 OR $2 IS NULL) AND mc.type = $3
ORDER BY mc.created_at DESC
LIMIT 1;

-- name: GetFlowTaskSubtaskTypeLastMsgChain :one
SELECT
mc.*
FROM msgchains mc
WHERE mc.flow_id = $1
AND mc.task_id IS NOT DISTINCT FROM $2
AND mc.subtask_id IS NOT DISTINCT FROM $3
AND mc.type = $4
ORDER BY mc.created_at DESC
LIMIT 1;

-- name: GetMsgChain :one
SELECT
mc.*
Expand Down