-
Notifications
You must be signed in to change notification settings - Fork 142
Add CheckPrerequisite to WorkflowStep to validate state machine edges #374
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Zoe Zhao (zoez7)
wants to merge
4
commits into
agent-substrate:main
Choose a base branch
from
zoez7:check-prerequisite
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1795b42
Add a CheckPrerequisite step to each workflow
zoez7 4104a7d
Adopting the new Actor API changes.
zoez7 c968955
Merge branch 'main' into check-prerequisite
zoez7 6d976cf
Refactored the worker eligibility check in workflow_resume.go, to als…
zoez7 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,172 @@ | ||
| // 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" | ||
| "testing" | ||
|
|
||
| "github.com/agent-substrate/substrate/cmd/ateapi/internal/store/storetest" | ||
| "github.com/agent-substrate/substrate/pkg/proto/ateapipb" | ||
| "google.golang.org/grpc/codes" | ||
| "google.golang.org/grpc/status" | ||
| ) | ||
|
|
||
| // TestPauseActorWorkflow_RejectedAndIdempotentPaths covers the two | ||
| // short-circuit paths of the pause workflow: rejection by MarkPausingStep's | ||
| // CheckPrerequisite and the IsComplete idempotent fast-forward. | ||
| func TestPauseActorWorkflow_RejectedAndIdempotentPaths(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| seedStatus ateapipb.Actor_Status | ||
| // wantErr true means PauseActor must fail with FailedPrecondition. | ||
| wantErr bool | ||
| // wantStatus is the stored status after the call. | ||
| wantStatus ateapipb.Actor_Status | ||
| }{ | ||
| { | ||
| // Pausing a SUSPENDED actor is rejected by MarkPausingStep's | ||
| // CheckPrerequisite and the actor's status is left untouched. | ||
| name: "not running rejected", | ||
| seedStatus: ateapipb.Actor_STATUS_SUSPENDED, | ||
| wantErr: true, | ||
| wantStatus: ateapipb.Actor_STATUS_SUSPENDED, | ||
| }, | ||
| { | ||
| // Pausing a PAUSED actor succeeds idempotently via IsComplete | ||
| // fast-forward without calling atelet. | ||
| name: "already paused succeeds", | ||
| seedStatus: ateapipb.Actor_STATUS_PAUSED, | ||
| wantStatus: ateapipb.Actor_STATUS_PAUSED, | ||
| }, | ||
| } | ||
| for _, tc := range tests { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| ctx := context.Background() | ||
| st, cleanup := storetest.SetupTestStore(t) | ||
| defer cleanup() | ||
| w := newTestActorWorkflow(t, st, "ns", "tmpl1") | ||
|
|
||
| seedWorkflowActor(t, ctx, st, "team-a", "id1", "ns", "tmpl1", tc.seedStatus) | ||
|
|
||
| actor, err := w.PauseActor(ctx, "team-a", "id1") | ||
| if tc.wantErr { | ||
| if got := status.Code(err); got != codes.FailedPrecondition { | ||
| t.Fatalf("status.Code(err) = %v, want %v (err: %v)", got, codes.FailedPrecondition, err) | ||
| } | ||
| } else { | ||
| if err != nil { | ||
| t.Fatalf("PauseActor failed: %v", err) | ||
| } | ||
| if actor.GetStatus() != tc.wantStatus { | ||
| t.Errorf("returned status = %v, want %v", actor.GetStatus(), tc.wantStatus) | ||
| } | ||
| } | ||
|
|
||
| got, err := st.GetActor(ctx, "team-a", "id1") | ||
| if err != nil { | ||
| t.Fatalf("GetActor failed: %v", err) | ||
| } | ||
| if got.GetStatus() != tc.wantStatus { | ||
| t.Errorf("stored status = %v, want %v", got.GetStatus(), tc.wantStatus) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // TestPauseSteps_CheckPrerequisite verifies each pause step's CheckPrerequisite | ||
| // against every actor status: nil for the step's allowed statuses, | ||
| // FailedPrecondition for all others. | ||
| func TestPauseSteps_CheckPrerequisite(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| step WorkflowStep[*PauseInput, *PauseState] | ||
| // allowed lists the statuses CheckPrerequisite accepts; nil means | ||
| // every status is accepted. | ||
| allowed map[ateapipb.Actor_Status]bool | ||
| }{ | ||
| { | ||
| // Loading has no prerequisite: it is allowed from every status. | ||
| name: "LoadActorForPauseStep", | ||
| step: &LoadActorForPauseStep{}, | ||
| allowed: nil, | ||
| }, | ||
| { | ||
| // Pausing is allowed only from RUNNING. | ||
| name: "MarkPausingStep", | ||
| step: &MarkPausingStep{}, | ||
| allowed: map[ateapipb.Actor_Status]bool{ | ||
| ateapipb.Actor_STATUS_RUNNING: true, | ||
| }, | ||
| }, | ||
| { | ||
| // The checkpoint call is allowed only from PAUSING (PAUSED is | ||
| // fast-forwarded by IsComplete). | ||
| name: "CallAteletPauseStep", | ||
| step: &CallAteletPauseStep{}, | ||
| allowed: map[ateapipb.Actor_Status]bool{ | ||
| ateapipb.Actor_STATUS_PAUSING: true, | ||
| }, | ||
| }, | ||
| { | ||
| // Finalizing is allowed only from PAUSING: a persisted PAUSED | ||
| // actor always has its worker pod fields cleared and is | ||
| // fast-forwarded by IsComplete. | ||
| name: "FinalizePausedStep", | ||
| step: &FinalizePausedStep{}, | ||
| allowed: map[ateapipb.Actor_Status]bool{ | ||
| ateapipb.Actor_STATUS_PAUSING: true, | ||
| }, | ||
| }, | ||
| } | ||
| for _, tc := range tests { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| ctx := context.Background() | ||
| for _, st := range allActorStatuses { | ||
| // Worker pod fields are populated so CallAteletPauseStep's | ||
| // missing-worker crash branch is not taken; this test only | ||
| // verifies status gating. | ||
| err := tc.step.CheckPrerequisite(ctx, &PauseInput{ActorName: "id1"}, &PauseState{Actor: &ateapipb.Actor{Status: st, AteomPodNamespace: "ns", AteomPodName: "worker-1"}}) | ||
| assertPrerequisiteResult(t, st, err, tc.allowed == nil || tc.allowed[st]) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // TestPauseActor_CrashesWhenPausingActorMissingWorkerPod verifies that a | ||
| // PAUSING actor with no worker pod recorded is moved to CRASHED by | ||
| // CallAteletPauseStep's prerequisite check and the pause fails with | ||
| // FailedPrecondition. | ||
| func TestPauseActor_CrashesWhenPausingActorMissingWorkerPod(t *testing.T) { | ||
| ctx := context.Background() | ||
| st, cleanup := storetest.SetupTestStore(t) | ||
| defer cleanup() | ||
| w := newTestActorWorkflow(t, st, "ns", "tmpl1") | ||
|
|
||
| seedWorkflowActor(t, ctx, st, "team-a", "id1", "ns", "tmpl1", ateapipb.Actor_STATUS_PAUSING) | ||
|
|
||
| _, err := w.PauseActor(ctx, "team-a", "id1") | ||
| if got := status.Code(err); got != codes.FailedPrecondition { | ||
| t.Fatalf("status.Code(err) = %v, want %v (err: %v)", got, codes.FailedPrecondition, err) | ||
| } | ||
|
|
||
| got, err := st.GetActor(ctx, "team-a", "id1") | ||
| if err != nil { | ||
| t.Fatalf("GetActor failed: %v", err) | ||
| } | ||
| if got.GetStatus() != ateapipb.Actor_STATUS_CRASHED { | ||
| t.Errorf("stored status = %v, want %v", got.GetStatus(), ateapipb.Actor_STATUS_CRASHED) | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is a pre-requisite too, however function in current implementation does not support transition to crashActor.
Might be rename the "CheckPrerequisite" to different name? might be allow transition to crash? or might be from the beginning we were not supposed to be in this state, that actor does not have pod or namespace?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should crash the actor here. We can only get here if MarkPausing has succeeded, not having the ateom pod or namespace should crash the actor. I changed the implementation.