Files

182 lines
6.7 KiB
Go

package staterpc
import (
"context"
"fmt"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
pb "github.com/samcharles93/archie-core/internal/contracts/state/v1"
"github.com/samcharles93/archie-core/internal/domain/org"
"github.com/samcharles93/archie-core/internal/domain/usage"
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
"github.com/samcharles93/archie-core/internal/events"
"github.com/samcharles93/archie-core/internal/taskstate"
)
type executionServer struct {
pb.UnimplementedExecutionServiceServer
*server
}
// StartStep and FinishStep write a step transition and return its event.
func (s *executionServer) StartStep(ctx context.Context, r *pb.StartStepRequest) (*pb.StartStepResponse, error) {
if s.deps.Steps == nil {
return nil, status.Error(codes.Unavailable, "step recorder unavailable")
}
stepID, event, err := s.deps.Steps.StartStep(ctx, task.StepStart{
ExecutionID: r.ExecutionId, Attempt: int(r.Attempt),
ParentID: r.ParentId, Kind: r.Kind, Name: r.Name,
CalledExecutionID: r.CalledExecutionId,
})
if err != nil {
return nil, s.logErr(ctx, "StartStep", err)
}
return &pb.StartStepResponse{StepId: stepID, Event: eventProto(event)}, nil
}
// RecordUsage appends one model call's usage record.
func (s *executionServer) RecordUsage(ctx context.Context, r *pb.RecordUsageRequest) (*pb.RecordUsageResponse, error) {
if s.deps.Usage == nil {
return nil, status.Error(codes.Unavailable, "usage recorder unavailable")
}
if r.Record == nil {
return nil, status.Error(codes.InvalidArgument, "record is required")
}
record := usageValue(r.Record)
// The caller never names the org: a task's is its row's, which the store
// applies; anything else is the calling principal's.
record.Org = string(org.OrgFromContext(ctx))
if err := s.deps.Usage.RecordUsage(ctx, record); err != nil {
return nil, s.logErr(ctx, "RecordUsage", err)
}
return &pb.RecordUsageResponse{}, nil
}
// CancelExecution is the one cancel path:
// the store records the cancellation and returns the callees it cancelled
// with it; the caller delivers it by cancelling the runs' in-memory contexts
// afterwards.
func (s *executionServer) CancelExecution(ctx context.Context, r *pb.CancelExecutionRequest) (*pb.CancelExecutionResponse, error) {
if s.deps.Canceller == nil {
return nil, status.Error(codes.Unavailable, "execution canceller unavailable")
}
ids, err := s.deps.Canceller.CancelExecution(ctx, r.TaskId, r.Reason, r.To)
if err != nil {
return nil, s.logErr(ctx, "CancelExecution", err)
}
return &pb.CancelExecutionResponse{CancelledCalleeIds: ids}, nil
}
func (s *executionServer) FinishStep(ctx context.Context, r *pb.FinishStepRequest) (*pb.FinishStepResponse, error) {
if s.deps.Steps == nil {
return nil, status.Error(codes.Unavailable, "step recorder unavailable")
}
event, err := s.deps.Steps.FinishStep(ctx, task.StepFinish{
StepID: r.StepId, ExecutionID: r.ExecutionId,
From: taskstate.StepStatus(r.From), To: taskstate.StepStatus(r.To),
Detail: r.Detail, TokensUsed: r.TokensUsed, Results: r.Results,
})
if err != nil {
return nil, s.logErr(ctx, "FinishStep", err)
}
return &pb.FinishStepResponse{Event: eventProto(event)}, nil
}
func (s *executionServer) ListSteps(ctx context.Context, r *pb.ListStepsRequest) (*pb.ListStepsResponse, error) {
if s.deps.StepReader == nil {
return nil, status.Error(codes.Unavailable, "step reader unavailable")
}
steps, err := s.deps.StepReader.ListSteps(ctx, r.ExecutionId, int(r.Attempt))
if err != nil {
return nil, s.logErr(ctx, "ListSteps", err)
}
return &pb.ListStepsResponse{Steps: mapValues(steps, stepExecutionProto)}, nil
}
// RecordUsage appends one model call's usage record. The write is durable
// against a State Store outage: when the RPC fails, the record is queued in
// the client's local outbox and retried until the store accepts it, so the
// call returns once the record is written or safely queued. A non-nil error
// reports a deferred write (queued for retry) or a full queue (record
// dropped); it never fails the model call that produced the record.
func (c *Client) RecordUsage(ctx context.Context, r usage.Record) error {
err := c.writeUsage(ctx, r)
if err == nil {
return nil
}
if c.usageOutbox == nil {
return err
}
//nolint:contextcheck // A queued record outlives the call that produced it.
if c.usageOutbox.enqueue(r) {
return fmt.Errorf("usage record queued for retry: %w", err)
}
return fmt.Errorf("usage retry queue full (%d pending), record dropped: %w", usageQueueBound, err)
}
// writeUsage makes the one RecordUsage RPC.
func (c *Client) writeUsage(ctx context.Context, r usage.Record) error {
_, err := c.execution.RecordUsage(ctx, &pb.RecordUsageRequest{Record: usageProto(r)})
return unmapError(err)
}
// StartStep and FinishStep record step executions and return the persisted
// event.
func (c *Client) StartStep(ctx context.Context, s task.StepStart) (int64, events.Event, error) {
r, err := c.execution.StartStep(ctx, &pb.StartStepRequest{
ExecutionId: s.ExecutionID, Attempt: int64(s.Attempt),
ParentId: s.ParentID, Kind: s.Kind, Name: s.Name,
CalledExecutionId: s.CalledExecutionID,
})
if err != nil {
return 0, events.Event{}, unmapError(err)
}
event := events.Event{}
if r.Event != nil {
event = eventValue(r.Event)
}
return r.StepId, event, nil
}
// CancelExecution is the one cancel path:
// the State Store records the cancellation -- every non-terminal step of the
// current attempt plus the execution's own move, and its waited-on callees --
// and returns the callees it cancelled; the caller cancels the runs'
// in-memory contexts afterwards.
func (c *Client) CancelExecution(ctx context.Context, taskID int64, reason, to string) ([]int64, error) {
r, err := c.execution.CancelExecution(ctx, &pb.CancelExecutionRequest{TaskId: taskID, Reason: reason, To: to})
if err != nil {
return nil, unmapError(err)
}
return r.CancelledCalleeIds, nil
}
func (c *Client) FinishStep(ctx context.Context, s task.StepFinish) (events.Event, error) {
r, err := c.execution.FinishStep(ctx, &pb.FinishStepRequest{
ExecutionId: s.ExecutionID, StepId: s.StepID,
From: string(s.From), To: string(s.To), Detail: s.Detail, TokensUsed: s.TokensUsed, Results: s.Results,
})
if err != nil {
return events.Event{}, unmapError(err)
}
event := events.Event{}
if r.Event != nil {
event = eventValue(r.Event)
}
return event, nil
}
func (c *Client) ListSteps(ctx context.Context, executionID int64, attempt int) ([]task.StepExecution, error) {
r, err := c.execution.ListSteps(ctx, &pb.ListStepsRequest{ExecutionId: executionID, Attempt: int64(attempt)})
if err != nil {
return nil, unmapError(err)
}
steps := make([]task.StepExecution, len(r.Steps))
for i, s := range r.Steps {
steps[i] = stepExecutionValue(s)
}
return steps, nil
}