carlos / platform Public

activator: held-leg guard takes only actState's own constants (#432)

Human

Prompt

task 2 re-review (T2-rereview.md) left P2 #3 open: a
package-level constant alias of actStopping bypassed the guard.

From this commit's own message. The prompt ledger has no entries for it — which is what a squashed, rebased or amended commit looks like, and what every commit looks like in a repo without a ledger.

Summary

- An actEntry.state assignment (or literal state key) must be exactly
  one of the constants declared in actState's own const block
  (actWaking, actAwake, actStopping), resolved as *types.Const objects;
  any other constant, variable, call or conversion fails.
- Any package-level constant of type actState outside that block fails
  where it is declared.
- Fixtures: a package-level alias and its use, and actState(2).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev 1595be9f7830fde2601642c639b83be84da7bdb9 parent 21241fb
1 file changed, +108 −23
  • internal/activator/heldlegs_guard_test.go +108 −23
diff --git a/internal/activator/heldlegs_guard_test.go b/internal/activator/heldlegs_guard_test.go
index 3d9bf6d59..3e1a22511 100644
--- a/internal/activator/heldlegs_guard_test.go
+++ b/internal/activator/heldlegs_guard_test.go
@@ -227,20 +227,6 @@ func heldGuardIsBuiltin(info *types.Info, call *ast.CallExpr, name string) bool
return builtin
}
-// heldGuardIsConst reports whether x is an identifier naming a package
-// constant, and which.
-func heldGuardIsConst(info *types.Info, x ast.Expr) (string, bool) {
- id, ok := x.(*ast.Ident)
- if !ok {
- return "", false
- }
- c, ok := info.Uses[id].(*types.Const)
- if !ok || c.Pkg() == nil || c.Parent() != c.Pkg().Scope() {
- return "", false
- }
- return c.Name(), true
-}
-
// heldGuardReport is the guard's result over a set of functions.
type heldGuardReport struct {
violations []string
@@ -251,8 +237,20 @@ type heldGuardReport struct {
bindClaimSeen, refuseSeen bool
}
-func heldGuardRun(fset *token.FileSet, info *types.Info, funcs []heldGuardFunc, tg heldGuardTargets) heldGuardReport {
+func heldGuardRun(fset *token.FileSet, info *types.Info, funcs []heldGuardFunc, tg heldGuardTargets, canon map[*types.Const]bool) heldGuardReport {
var r heldGuardReport
+ if len(canon) == 0 {
+ r.violations = append(r.violations, "the state type's own constants (actState's const block) were not resolved; see "+heldGuardSpec)
+ }
+ // isCanon: x is exactly one of the state type's own constants.
+ isCanon := func(x ast.Expr) bool {
+ id, ok := x.(*ast.Ident)
+ if !ok {
+ return false
+ }
+ c, ok := info.Uses[id].(*types.Const)
+ return ok && canon[c]
+ }
at := func(fn heldGuardFunc, pos token.Pos, what string) {
p := fset.Position(pos)
r.violations = append(r.violations, fmt.Sprintf("%s:%d (%s): %s; see %s", fn.file, p.Line, fn.name, what, heldGuardSpec))
@@ -271,7 +269,8 @@ func heldGuardRun(fset *token.FileSet, info *types.Info, funcs []heldGuardFunc,
// beginStopLocked's one assignment. Any other use (an
// alias, a helper's argument, a literal) could move an
// entry to stopping without the cancel.
- if name, ok := heldGuardIsConst(info, v); !ok || name != "actStopping" {
+ c, ok := info.Uses[v].(*types.Const)
+ if !ok || !canon[c] || c.Name() != "actStopping" {
return
}
switch p := parent.(type) {
@@ -302,8 +301,8 @@ func heldGuardRun(fset *token.FileSet, info *types.Info, funcs []heldGuardFunc,
if heldGuardField(info, se, "actEntry", "state") {
if len(v.Rhs) != len(v.Lhs) {
at(fn, v.Pos(), "assigns actEntry.state from a multi-value expression")
- } else if _, ok := heldGuardIsConst(info, v.Rhs[i]); !ok {
- at(fn, v.Pos(), "assigns actEntry.state something other than a named constant")
+ } else if !isCanon(v.Rhs[i]) {
+ at(fn, v.Pos(), "assigns actEntry.state something other than one of the state type's own constants")
}
}
// The proc field of an actEntry; other types' proc
@@ -351,8 +350,8 @@ func heldGuardRun(fset *token.FileSet, info *types.Info, funcs []heldGuardFunc,
continue
}
if id, ok := kv.Key.(*ast.Ident); ok && id.Name == "state" {
- if _, ok := heldGuardIsConst(info, kv.Value); !ok {
- at(fn, v.Pos(), "builds an actEntry whose state is not a named constant")
+ if !isCanon(kv.Value) {
+ at(fn, v.Pos(), "builds an actEntry whose state is not one of the state type's own constants")
}
}
}
@@ -511,6 +510,64 @@ func heldGuardRun(fset *token.FileSet, info *types.Info, funcs []heldGuardFunc,
return r
}
+// heldGuardCanonStates is the state type's own constants: those declared
+// in the const block that declares actStopping and whose type is actState
+// (actWaking, actAwake, actStopping), resolved in info.
+func heldGuardCanonStates(info *types.Info, files map[string]*ast.File) map[*types.Const]bool {
+ canon := map[*types.Const]bool{}
+ for _, f := range files {
+ for _, d := range f.Decls {
+ gd, ok := d.(*ast.GenDecl)
+ if !ok || gd.Tok != token.CONST {
+ continue
+ }
+ var block []*types.Const
+ hasStopping := false
+ for _, spec := range gd.Specs {
+ for _, id := range spec.(*ast.ValueSpec).Names {
+ c, ok := info.Defs[id].(*types.Const)
+ if !ok || !heldGuardNamed(c.Type(), "actState") {
+ continue
+ }
+ block = append(block, c)
+ hasStopping = hasStopping || c.Name() == "actStopping"
+ }
+ }
+ if hasStopping {
+ for _, c := range block {
+ canon[c] = true
+ }
+ }
+ }
+ }
+ return canon
+}
+
+// heldGuardStrayStateConsts refuses every package-level constant of the
+// state type outside its own const block: an alias (const x =
+// actStopping) could carry an entry to stopping past the guard.
+func heldGuardStrayStateConsts(fset *token.FileSet, info *types.Info, files map[string]*ast.File, canon map[*types.Const]bool) []string {
+ var out []string
+ for base, f := range files {
+ for _, d := range f.Decls {
+ gd, ok := d.(*ast.GenDecl)
+ if !ok || gd.Tok != token.CONST {
+ continue
+ }
+ for _, spec := range gd.Specs {
+ for _, id := range spec.(*ast.ValueSpec).Names {
+ c, ok := info.Defs[id].(*types.Const)
+ if ok && heldGuardNamed(c.Type(), "actState") && !canon[c] {
+ out = append(out, fmt.Sprintf("%s:%d: declares %s, a constant of the state type outside its own const block; see %s", base, fset.Position(id.Pos()).Line, id.Name, heldGuardSpec))
+ }
+ }
+ }
+ }
+ }
+ slices.Sort(out)
+ return out
+}
+
// heldGuardRealFiles is the package's own non-test files, guard fixtures
// left out.
func heldGuardRealFiles(pkg *packages.Package) map[string]*ast.File {
@@ -529,10 +586,16 @@ func TestHeldLegTransitionSites(t *testing.T) {
if err != nil {
t.Fatal(err)
}
- r := heldGuardRun(pkg.Fset, pkg.TypesInfo, heldGuardFuncs(pkg.Fset, heldGuardRealFiles(pkg)), heldGuardReal)
+ realFiles := heldGuardRealFiles(pkg)
+ canon := heldGuardCanonStates(pkg.TypesInfo, realFiles)
+ r := heldGuardRun(pkg.Fset, pkg.TypesInfo, heldGuardFuncs(pkg.Fset, realFiles), heldGuardReal, canon)
+ r.violations = append(r.violations, heldGuardStrayStateConsts(pkg.Fset, pkg.TypesInfo, realFiles, canon)...)
for _, v := range r.violations {
t.Error(v)
}
+ if len(canon) != 3 {
+ t.Errorf("the state type's own constants resolved to %d, want actWaking, actAwake, actStopping", len(canon))
+ }
// A guard that resolved nothing proves nothing.
if r.stoppingSites < 1 || !r.beginStopFirst {
t.Errorf("the guard saw %d actStopping assignments (beginStopLocked first-statement cancel: %v): it resolves nothing", r.stoppingSites, r.beginStopFirst)
@@ -559,6 +622,13 @@ func TestHeldLegTransitionSites(t *testing.T) {
// row names, must be refused with the given text; the clean ones must
// pass.
info, files := heldGuardFixturePackage(t, pkg)
+ fixCanon := heldGuardCanonStates(info, realFiles)
+ // A package-level constant of the state type outside its own const
+ // block is refused where it is declared.
+ stray := heldGuardStrayStateConsts(pkg.Fset, info, files, fixCanon)
+ if !slices.ContainsFunc(stray, func(v string) bool { return strings.Contains(v, "heldFixStoppingConst") }) {
+ t.Errorf("the guard did not refuse the package-level alias heldFixStoppingConst; it reported:\n%s", strings.Join(stray, "\n"))
+ }
byName := map[string]heldGuardFunc{}
for _, fn := range heldGuardFuncs(pkg.Fset, files) {
byName[fn.name] = fn
@@ -569,7 +639,7 @@ func TestHeldLegTransitionSites(t *testing.T) {
if !ok {
t.Fatalf("fixture function %s not found", fx.fn)
}
- got := heldGuardRun(pkg.Fset, info, []heldGuardFunc{fn}, fx.tg).violations
+ got := heldGuardRun(pkg.Fset, info, []heldGuardFunc{fn}, fx.tg, fixCanon).violations
if fx.want == "" {
if len(got) > 0 {
t.Fatalf("the guard refused a clean shape:\n%s", strings.Join(got, "\n"))
@@ -638,7 +708,12 @@ var heldGuardFixtures = []heldGuardFixture{
// through a local and through a helper's argument.
{"Service.heldFixStoppingAlias", heldGuardAs("beginStop", "Service.heldFixStoppingAlias"), "uses actStopping outside"},
{"Service.heldFixStoppingHelper", heldGuardReal, "uses actStopping outside"},
- {"heldFixSetState", heldGuardReal, "assigns actEntry.state something other than a named constant"},
+ {"heldFixSetState", heldGuardReal, "assigns actEntry.state something other than one of the state type's own constants"},
+ // A package-level alias of actStopping (refused where it is declared,
+ // heldGuardStrayStateConsts) and its use.
+ {"Service.heldFixStoppingConstAlias", heldGuardReal, "assigns actEntry.state something other than one of the state type's own constants"},
+ // Another constant of the same value, and a conversion.
+ {"Service.heldFixStoppingConversion", heldGuardReal, "assigns actEntry.state something other than one of the state type's own constants"},
{"Service.heldFixEntriesAlias", heldGuardReal, "uses a.entries other than"},
{"Service.heldFixEntriesHelper", heldGuardReal, "uses a.entries other than"},
}
@@ -776,6 +851,16 @@ func heldFixSetState(e *actEntry, s actState) {
e.state = s
}
+const heldFixStoppingConst = actStopping
+
+func (a *Service) heldFixStoppingConstAlias(e *actEntry) {
+ e.state = heldFixStoppingConst
+}
+
+func (a *Service) heldFixStoppingConversion(e *actEntry) {
+ e.state = actState(2)
+}
+
func (a *Service) heldFixEntriesAlias(host string) {
entries := a.entries
delete(entries, host)