Run executes normalize → policy → (agent stub?) → admission → route → (RAG?) → backend → finalize.
(ctx context.Context, in RunInput)
| 43 | |
| 44 | // Run executes normalize → policy → (agent stub?) → admission → route → (RAG?) → backend → finalize. |
| 45 | func (o *Orchestrator) Run(ctx context.Context, in RunInput) *Result { |
| 46 | _ = ExecutionPlanForRequestType(in.Req.RequestType) |
| 47 | |
| 48 | req := in.Req |
| 49 | requestID := in.RequestID |
| 50 | sw := in.SW |
| 51 | emit := in.EmitRequestTrace |
| 52 | if emit == nil { |
| 53 | emit = func(string, string) {} |
| 54 | } |
| 55 | |
| 56 | ledgerAt := time.Now() |
| 57 | if o.Ledger != nil { |
| 58 | o.Ledger.CreateRequest(ctx, in.TraceID, requestID, *req, ledgerAt) |
| 59 | } |
| 60 | |
| 61 | _ = sw.Run(ctx, execution.StepNormalize, "", map[string]any{ |
| 62 | "request_type": req.RequestType, "pipeline_ref": req.PipelineRef, |
| 63 | }, func() (map[string]any, error) { |
| 64 | return map[string]any{"ok": true}, nil |
| 65 | }) |
| 66 | |
| 67 | policyDecision, err := o.Policy.Evaluate(ctx, *req) |
| 68 | if err != nil { |
| 69 | o.failLedger(ctx, requestID) |
| 70 | if st, ec, ok := InferBudgetHTTPStatus(ctx, err); ok { |
| 71 | if o.NoteTimeout != nil { |
| 72 | o.NoteTimeout(err) |
| 73 | } |
| 74 | emit("", "gateway_timeout") |
| 75 | return &Result{Failure: &Failure{HTTPStatus: st, ErrorCode: ec, Message: "inference request deadline exceeded", TraceResult: "gateway_timeout"}} |
| 76 | } |
| 77 | emit("", "policy_error") |
| 78 | return &Result{Failure: &Failure{HTTPStatus: http.StatusInternalServerError, ErrorCode: "policy_error", Message: err.Error(), TraceResult: "policy_error"}} |
| 79 | } |
| 80 | |
| 81 | if err := sw.Run(ctx, execution.StepPolicyCheck, "", map[string]any{"tenant_id": req.TenantID}, func() (map[string]any, error) { |
| 82 | if !policyDecision.Allowed { |
| 83 | return map[string]any{"allowed": false, "reason": policyDecision.Reason}, fmt.Errorf("policy rejected: %s", policyDecision.Reason) |
| 84 | } |
| 85 | return map[string]any{"allowed": true, "reason": policyDecision.Reason}, nil |
| 86 | }); err != nil { |
| 87 | log.Printf("event=policy_rejected request_id=%s tenant_id=%s reason=%q", requestID, req.TenantID, policyDecision.Reason) |
| 88 | o.failLedger(ctx, requestID) |
| 89 | emit("", "policy_rejected") |
| 90 | return &Result{Failure: &Failure{HTTPStatus: http.StatusTooManyRequests, ErrorCode: "policy_rejected", Message: policyDecision.Reason, TraceResult: "policy_rejected"}} |
| 91 | } |
| 92 | *req = policyDecision.Normalized |
| 93 | |
| 94 | if req.RequestType == types.RequestTypeAgent { |
| 95 | _ = sw.Run(ctx, execution.StepAgentStub, "", map[string]any{"pipeline": req.PipelineRef}, func() (map[string]any, error) { |
| 96 | return nil, fmt.Errorf("agent execution not implemented") |
| 97 | }) |
| 98 | o.failLedger(ctx, requestID) |
| 99 | emit("", "agent_not_implemented") |
| 100 | return &Result{Failure: &Failure{HTTPStatus: http.StatusNotImplemented, ErrorCode: "agent_not_implemented", Message: "agent execution is not implemented in this release", TraceResult: "agent_not_implemented"}} |
| 101 | } |
| 102 |