diff --git a/CHANGELOG.md b/CHANGELOG.md index 0596a2c23..9293a4bce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ NOTE: all releases may include dependency updates, not specifically mentioned ## 2025-07-14 - Runtime v0.18.6 - feat: restore agents on demand [#949](https://github.com/hypermodeinc/modus/pull/949) +- fix: handle agent not found [#951](https://github.com/hypermodeinc/modus/pull/951) ## 2025-07-12 - Runtime v0.18.5 diff --git a/runtime/actors/agents.go b/runtime/actors/agents.go index fd0803d4a..6f480f228 100644 --- a/runtime/actors/agents.go +++ b/runtime/actors/agents.go @@ -224,12 +224,11 @@ func SendAgentMessage(ctx context.Context, agentId string, msgName string, data if errors.Is(err, goakt.ErrActorNotFound) { state, err := db.GetAgentState(ctx, agentId) - if err != nil { + if errors.Is(err, db.ErrAgentNotFound) { + return newAgentMessageErrorResponse(fmt.Sprintf("agent %s not found", agentId)), nil + } else if err != nil { return nil, fmt.Errorf("error getting agent state for %s: %w", agentId, err) } - if state == nil { - return newAgentMessageErrorResponse("agent not found"), nil - } switch AgentStatus(state.Status) { case AgentStatusStopping, AgentStatusTerminated: diff --git a/runtime/db/agentstate.go b/runtime/db/agentstate.go index 2185b4272..619065a9c 100644 --- a/runtime/db/agentstate.go +++ b/runtime/db/agentstate.go @@ -7,6 +7,7 @@ package db import ( "context" + "errors" "fmt" "time" @@ -14,9 +15,12 @@ import ( "github.com/hypermodeinc/modus/runtime/utils" "github.com/hypermodeinc/modusgraph" + mg_utils "github.com/hypermodeinc/modusgraph/api/apiutils" "github.com/jackc/pgx/v5" ) +var ErrAgentNotFound = errors.New("agent not found") + type AgentState struct { Gid uint64 `json:"gid,omitempty"` Id string `json:"id" db:"constraint=unique"` @@ -93,6 +97,10 @@ func getAgentStateFromModusDB(ctx context.Context, id string) (*AgentState, erro Key: "id", Value: id, }) + if errors.Is(err, mg_utils.ErrNoObjFound) { + return nil, ErrAgentNotFound + } + if err != nil { return nil, fmt.Errorf("failed to query agent state: %w", err) } @@ -174,6 +182,9 @@ func getAgentStateFromPostgresDB(ctx context.Context, id string) (*AgentState, e err := WithTx(ctx, func(tx pgx.Tx) error { row := tx.QueryRow(ctx, query, id) if err := row.Scan(&a.Id, &a.Name, &a.Status, &a.Data, &ts); err != nil { + if errors.Is(err, pgx.ErrNoRows) { + return ErrAgentNotFound + } return fmt.Errorf("failed to get agent state: %w", err) } a.UpdatedAt = ts.UTC().Format(utils.TimeFormat)