feat(work): gate agent canary dispatch
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
package agent_execution_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.cin.su/m/Assistant/contracts/work"
|
||||
"git.cin.su/m/Assistant/modules/work/application"
|
||||
)
|
||||
|
||||
type canaryStore struct{ writes int }
|
||||
|
||||
func (s *canaryStore) SaveDispatchIntent(context.Context, work.AgentDispatchIntent) error {
|
||||
s.writes++
|
||||
return nil
|
||||
}
|
||||
func (s *canaryStore) SaveAgentLease(context.Context, work.AgentLease) error { s.writes++; return nil }
|
||||
func (s *canaryStore) AcceptAgentCallback(context.Context, work.AgentCallback) error {
|
||||
s.writes++
|
||||
return nil
|
||||
}
|
||||
|
||||
func canaryIntent() work.AgentDispatchIntent {
|
||||
return work.AgentDispatchIntent{DispatchID: "dispatch-1", IdempotencyKey: "idem-1", DelegationID: "delegation-1", RunID: "run-1", ImplementationID: "fake-worker", ImplementationRevision: 1, SourceRevision: "source-1", WorktreeID: "worktree-1", VerificationPlanRef: "verify-1", CreatedAtUnixMillis: 1_700_000_000_000}
|
||||
}
|
||||
|
||||
func TestCanaryRequiresExplicitEnablementAndAllowlist(t *testing.T) {
|
||||
store := &canaryStore{}
|
||||
config := application.CanaryConfig{Enabled: false, ImplementationID: "fake-worker", MaxDispatches: 1}
|
||||
_, err := application.ExecuteCanaryDryRun(context.Background(), store, config, 0, canaryIntent(), "worker-1", 7, time.UnixMilli(1_700_000_000_000), time.Minute)
|
||||
if err != application.ErrCanaryDisabled || store.writes != 0 {
|
||||
t.Fatalf("disabled canary wrote state or returned wrong error: %v/%d", err, store.writes)
|
||||
}
|
||||
config.Enabled = true
|
||||
config.ImplementationID = "other-worker"
|
||||
_, err = application.ExecuteCanaryDryRun(context.Background(), store, config, 0, canaryIntent(), "worker-1", 7, time.UnixMilli(1_700_000_000_000), time.Minute)
|
||||
if err != application.ErrCanaryNotAllowed || store.writes != 0 {
|
||||
t.Fatalf("unallowed canary wrote state or returned wrong error: %v/%d", err, store.writes)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCanaryHonorsDispatchLimit(t *testing.T) {
|
||||
store := &canaryStore{}
|
||||
config := application.CanaryConfig{Enabled: true, ImplementationID: "fake-worker", MaxDispatches: 1}
|
||||
_, err := application.ExecuteCanaryDryRun(context.Background(), store, config, 1, canaryIntent(), "worker-1", 7, time.UnixMilli(1_700_000_000_000), time.Minute)
|
||||
if err != application.ErrCanaryLimitReached || store.writes != 0 {
|
||||
t.Fatalf("limited canary wrote state or returned wrong error: %v/%d", err, store.writes)
|
||||
}
|
||||
_, err = application.ExecuteCanaryDryRun(context.Background(), store, config, 0, canaryIntent(), "worker-1", 7, time.UnixMilli(1_700_000_000_000), time.Minute)
|
||||
if err != nil || store.writes != 4 {
|
||||
t.Fatalf("enabled canary did not persist expected cycle: %v/%d", err, store.writes)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user