mirror of
https://github.com/agent-substrate/substrate.git
synced 2026-11-12 10:35:06 +08:00
Fixes #554 Each workflow becomes a plain method orchestrating typed, idempotent ensureX steps, e.g.: ```go func (w *ActorWorkflow) ResumeActor(ctx, ref, boot) (*Actor, bool, error) { // acquire actor lock, metrics defer — unchanged actor, tmpl, snapSrc, err := w.loadActorForResume(ctx, ref, boot) if actor.Status == RUNNING { return actor, false, nil } // explicit no-op actor, err = w.ensureVolumesCreated(ctx, actor, tmpl) actor, worker, err = w.ensureWorkerAssigned(ctx, actor, tmpl) err = w.ensureVolumesAttached(ctx, actor, worker, tmpl) tele, err = w.ensureAteletRestored(ctx, actor, worker, tmpl, snapSrc) actor, err = w.finalizeRunning(ctx, ref) return actor, true, nil } ``` Each `ensureX` - early-returns if its postcondition already holds, which is derived from persisted state only, replacing `IsComplete` - validates the state machine edge internally, replacing `CheckPrerequisite` - takes explicit typed inputs and returns what it produced. - persists what it changed before returning, returning the stored copy so the optimistic lock version threads forward. No shared `*ResumeState`. There is one behavioral change: worker ownership/eligibility validation for a re-entered resume now runs before volumes are attached (it used to run in `CallAteletRestoreStep`'s prerequisite, after attach), so a resume that is about to crash on a reassigned/ineligible worker no longer attaches volumes to it first. - [X] Tests pass - [ ] Appropriate changes to documentation are included in the PR
68 lines
2.1 KiB
Go
68 lines
2.1 KiB
Go
// Copyright 2026 Google LLC
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package controlapi
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/agent-substrate/substrate/internal/ateattr"
|
|
"github.com/agent-substrate/substrate/internal/resources"
|
|
"github.com/agent-substrate/substrate/pkg/proto/ateapipb"
|
|
"go.opentelemetry.io/otel/attribute"
|
|
"k8s.io/apimachinery/pkg/util/validation/field"
|
|
)
|
|
|
|
func (s *Service) DeleteActor(ctx context.Context, req *ateapipb.DeleteActorRequest) (deleted *ateapipb.Actor, err error) {
|
|
if errs := validateDeleteActorRequest(req); len(errs) > 0 {
|
|
return nil, toGRPCStatusError(errs)
|
|
}
|
|
start := time.Now()
|
|
// Template dims only once the record resolved: the request names only the
|
|
// actor, so failures before the load carry none.
|
|
defer func() {
|
|
var attrs []attribute.KeyValue
|
|
if deleted != nil {
|
|
attrs = append(attrs,
|
|
ateattr.TemplateNameKey.String(deleted.GetActorTemplateName()),
|
|
ateattr.TemplateNamespaceKey.String(deleted.GetActorTemplateNamespace()),
|
|
)
|
|
}
|
|
s.instruments.recordLifecycleOp(ctx, ateattr.OperationDelete, start, err, attrs...)
|
|
}()
|
|
actorRef := resources.ActorRefFromObjectRef(req.GetActor())
|
|
setSpanActorRefAttributes(ctx, actorRef)
|
|
|
|
deleted, err = s.actorWorkflow.DeleteActor(ctx, actorRef)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return deleted, nil
|
|
}
|
|
|
|
func validateDeleteActorRequest(req *ateapipb.DeleteActorRequest) field.ErrorList {
|
|
var fldPath *field.Path
|
|
var errs field.ErrorList
|
|
|
|
if val, fldPath := req.Actor, fldPath.Child("actor"); val == nil {
|
|
errs = append(errs, field.Required(fldPath, ""))
|
|
} else {
|
|
errs = append(errs, resources.ValidateObjectRef(val, fldPath)...)
|
|
}
|
|
|
|
return errs
|
|
}
|