Skip to content

The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu

22.3 KB
Raw Blame
// Package scenario defines what to ask the agent and what Beacon must then show.
//
// The checking model is deliberately small: plant a unique canary in the prompt, have the
// command leave a sentinel file, then assert Beacon's log contains events matching the
// declared expectations. The canary makes matching exact; the sentinel makes a missing
// event unambiguous -- if the agent never acted there is nothing for Beacon to have missed,
// so the run is INCONCLUSIVE rather than FAIL.
package scenario

import (
	"bytes"
	"errors"
	"fmt"
	"io"
	"os"
	"path"
	"path/filepath"
	"strings"
	"time"

	"gopkg.in/yaml.v3"
)

// Expect is one assertion against the captured event stream.
type Expect struct {
	// Action matches event.action exactly, e.g. "command.executed".
	Action string `yaml:"action"`
	// Contains requires these substrings somewhere in the matched event's JSON. Use
	// {{canary}} to reference the run's canary.
	Contains []string `yaml:"contains,omitempty"`
	// Fields requires these dotted paths to be present and non-empty, e.g.
	// "command.command" or "file.path". This is how we assert the field population that
	// threat rules depend on, rather than just event presence.
	Fields []string `yaml:"fields,omitempty"`
	// MinCount defaults to 1.
	MinCount int `yaml:"min_count,omitempty"`
	// Optional records the expectation without failing the run. Use for signals that
	// are known-missing and tracked in BACKLOG.md, so a regression is still visible.
	Optional bool `yaml:"optional,omitempty"`
	// Why explains what this expectation protects, and is printed on failure.
	Why string `yaml:"why,omitempty"`
}

// Install describes a persistent `beacon endpoint install` to perform before the session,
// instead of the default ephemeral `beacon ci exec` collector.
//
// This is what exercises the parity surface: service manager selection, unit installation,
// status reporting, and whether a real installed collector actually receives telemetry. A
// scenario without it tests capture through the CI path, which is a different shipping path
// and cannot tell you whether `endpoint install` works at all.
type Install struct {
	// Mode is "user" (default) or "system". System mode runs as root.
	Mode string `yaml:"mode,omitempty"`
	// Service selects the service manager: empty auto-detects, or "systemd" / "none".
	Service string `yaml:"service,omitempty"`
	// ExpectStatusRunning requires the collector service to report running after install.
	ExpectStatusRunning bool `yaml:"expect_status_running,omitempty"`
	// ExpectServiceKind requires the endpoint to report this service backend after install.
	//
	// Which backend got selected is the substance of a service-manager scenario, and it is not
	// implied by "running": the supervised fallback also reports running, so a systemd scenario
	// on a host without systemd would otherwise pass while testing the wrong thing.
	ExpectServiceKind string `yaml:"expect_service_kind,omitempty"`
	// VerifyUninstall removes the endpoint after the capture assertions and checks that it went.
	//
	// Worth asking separately from install because uninstall fails in a shape nothing else catches:
	// it reports success while leaving the service registered, so the collector returns at the next
	// reboot after the operator was told it was gone. That shipped once in this repository and was
	// caught by a reviewer reading a diff, because no run had ever asked the question.
	//
	// The removal is real, not a rehearsal. It runs after the runtime log has been collected, so the
	// capture assertions are already settled and cannot be affected by it.
	VerifyUninstall bool `yaml:"verify_uninstall,omitempty"`
	// NeedsRealSystemd runs the install inside a nested privileged container where systemd is
	// genuinely PID 1.
	//
	// The sandbox provider cannot offer that directly: its own init holds PID 1, so systemctl
	// refuses. Rather than make a scenario describe VM lanes and Docker, it states the
	// requirement and the runner arranges it.
	NeedsRealSystemd bool `yaml:"needs_real_systemd,omitempty"`
	// VerifyRestartOnReinstall installs a second time over the running endpoint and checks that the
	// collector process was actually replaced.
	//
	// Load is a no-op on a live service in every backend, so an install that only ever calls Load
	// leaves the previous collector running -- serving the old config, and during a package upgrade
	// a binary the package manager has already deleted. Ports answer, status reports healthy, and
	// the endpoint is a version behind until reboot. Nothing short of a real service manager and a
	// real second install exercises that.
	VerifyRestartOnReinstall bool `yaml:"verify_restart_on_reinstall,omitempty"`
	// VerifyRollbackOnFailedReinstall makes a reinstall fail on purpose and checks what the machine
	// is left with.
	//
	// Rollback is the least exercised path in install and the easiest to get wrong, because it runs
	// only when something else has already gone wrong. Two defects lived here: it disabled a
	// collector that was healthy before the attempt, and -- since the supervised backend's unit
	// file is its pidfile, which rollback restores -- it stopped a pid that was already dead while
	// the collector the failed install started kept running, with a second one started beside it.
	//
	// Neither is visible from a successful install, and both leave a machine that looks installed.
	// The failure is induced by pointing the reinstall at a program that starts and never listens,
	// so the service loads and readiness is what fails, which is the ordering that reaches rollback
	// with a service to undo.
	VerifyRollbackOnFailedReinstall bool `yaml:"verify_rollback_on_failed_reinstall,omitempty"`
	// VerifyUnprivilegedUninstallFails checks that a system uninstall run without privileges
	// reports failure instead of pretending it removed anything.
	//
	// verify_uninstall alone cannot catch this: it runs the removal elevated, which is the path
	// that works. The failure worth catching is the unprivileged one, which returned success while
	// leaving the service registered.
	VerifyUnprivilegedUninstallFails bool `yaml:"verify_unprivileged_uninstall_fails,omitempty"`
	// NSSOnlyUser makes the session account resolvable through NSS but absent from /etc/passwd.
	//
	// This reproduces a directory-backed fleet -- OpenLDAP, SSSD, AD -- where `getent passwd alice`
	// succeeds and `grep alice /etc/passwd` finds nothing. It matters because Beacon is built
	// CGO_ENABLED=0, which selects Go's pure-Go os/user: that implementation parses /etc/passwd
	// directly and never consults NSS, so a root-run system install cannot resolve the human whose
	// runtime it is supposed to configure.
	//
	// The default image creates the same account with `useradd`, which writes /etc/passwd and makes
	// the failure structurally invisible -- a scenario can assert capture, install success, and
	// service health and still pass on a build that configures nobody on a real customer fleet.
	// Only the account's *visibility* changes; the username, home directory and uid are otherwise
	// identical, so everything a scenario asserts stays comparable to the local-account lanes.
	NSSOnlyUser bool `yaml:"nss_only_user,omitempty"`
	// VerifyVectorForwarding runs the bundled Vector against Beacon's Asymptote pack and checks
	// what reached a loopback ingest stand-in.
	//
	// The Linux packages ship Vector at /opt/beacon/bin/vector, and it is the engine behind Beacon
	// Managed and six customer-managed destinations. Nothing else in the suite runs it, so a pack
	// Vector rejects, a secret backend that sends the wrong key, or a binary that does not start
	// on a stripped host would all ship unnoticed. The stand-in serves the ingest contract on
	// 127.0.0.1, so the run needs no account and no egress beyond the model API.
	VerifyVectorForwarding bool `yaml:"verify_vector_forwarding,omitempty"`
}

// Platform selects the guest OS family a scenario is written for.
//
// A scenario is not portable by default and pretending otherwise would be worse than declaring
// it: the prompt names shell commands, the sentinel is an absolute path, and the expectations
// assert what Beacon captured from a particular runtime. So a scenario says which world it
// belongs to, and the suite loader runs only the ones that match the target.
type Platform string

const (
	// PlatformLinux is the default, so every scenario written before Windows existed keeps
	// working unchanged.
	PlatformLinux   Platform = "linux"
	PlatformWindows Platform = "windows"
)

// Scenario is one declarative case.
type Scenario struct {
	ID string `yaml:"id"`
	// Platform defaults to linux when unset.
	Platform Platform `yaml:"platform,omitempty"`
	Prompt   string   `yaml:"prompt"`
	// Install, when set, performs a persistent endpoint install and runs the session against
	// it, with no `beacon ci exec` wrapper anywhere.
	Install *Install `yaml:"install,omitempty"`
	// Sentinel is a path the agent's work must create. Its presence proves the agent
	// acted; its absence makes the run inconclusive.
	Sentinel string `yaml:"sentinel,omitempty"`
	// Setup runs as the agent user before the session, for planting fixtures.
	Setup string `yaml:"setup,omitempty"`
	// Tools restricts Claude Code's tool set, tightening what a run can do.
	Tools []string `yaml:"tools,omitempty"`
	// DisallowedTools forces denials, exercising the approval path.
	DisallowedTools []string `yaml:"disallowed_tools,omitempty"`
	// Cwd is relative to the work dir.
	Cwd string `yaml:"cwd,omitempty"`
	// BudgetUSD caps spend for the session.
	BudgetUSD float64 `yaml:"budget_usd,omitempty"`
	// TimeoutSeconds bounds the session.
	TimeoutSeconds int `yaml:"timeout_seconds,omitempty"`
	// BlockEgress denies all outbound network. Only useful for scenarios that need no
	// model call, since Claude Code needs the API.
	BlockEgress bool     `yaml:"block_egress,omitempty"`
	Expect      []Expect `yaml:"expect"`
}

// Suite is an ordered set of scenarios loaded from a directory.
type Suite struct {
	Name      string
	Scenarios []Scenario
}

// TargetPlatform resolves the platform with its default.
func (s Scenario) TargetPlatform() Platform {
	if s.Platform == "" {
		return PlatformLinux
	}
	return s.Platform
}

// Timeout resolves the session bound with a sane default.
func (s Scenario) Timeout() time.Duration {
	if s.TimeoutSeconds > 0 {
		return time.Duration(s.TimeoutSeconds) * time.Second
	}
	return 10 * time.Minute
}

// Budget resolves the spend cap with a sane default.
func (s Scenario) Budget() float64 {
	if s.BudgetUSD > 0 {
		return s.BudgetUSD
	}
	return 1.0
}

// Validate catches scenario authoring mistakes before a sandbox is paid for.
func (s Scenario) Validate() error {
	if strings.TrimSpace(s.ID) == "" {
		return fmt.Errorf("scenario is missing id")
	}
	if strings.TrimSpace(s.Prompt) == "" {
		return fmt.Errorf("%s: prompt is required", s.ID)
	}
	if len(s.Expect) == 0 {
		return fmt.Errorf("%s: at least one expectation is required, else the run asserts nothing", s.ID)
	}
	switch s.TargetPlatform() {
	case PlatformLinux, PlatformWindows:
	default:
		return fmt.Errorf("%s: platform %q must be linux or windows", s.ID, s.Platform)
	}
	if s.TargetPlatform() == PlatformWindows {
		// Vector is installed by the Linux image. The Windows runner is provisioned by CI and has
		// no bundled Vector to run.
		if s.VerifiesVectorForwarding() {
			return fmt.Errorf("%s: verify_vector_forwarding is Linux-only", s.ID)
		}
		// systemd does not exist on Windows, and the nested-container recipe that provides it is
		// a Linux arrangement. A scenario asking for both would be arranged as neither and would
		// then quietly verify whatever backend happened to be selected.
		if s.NeedsRealSystemd() {
			return fmt.Errorf("%s: needs_real_systemd cannot apply to a windows scenario; "+
				"the Windows service manager is the SCM, which is always present", s.ID)
		}
		// An absolute POSIX sentinel on Windows is the mistake that would be hardest to notice:
		// the agent writes somewhere, the probe looks somewhere else, and the run reports the
		// agent idle rather than the scenario being wrong.
		if strings.HasPrefix(s.Sentinel, "/") {
			return fmt.Errorf("%s: sentinel %q is a POSIX path in a windows scenario; use "+
				"{{workdir}} or a drive-qualified path", s.ID, s.Sentinel)
		}
	}
	for i, e := range s.Expect {
		if strings.TrimSpace(e.Action) == "" {
			return fmt.Errorf("%s: expect[%d] is missing action", s.ID, i)
		}
		// An unrecognised mode or service would reach the guest as a bad flag and fail mid-run,
		// after a sandbox has already been paid for. Catch it here instead.
		if s.Install != nil {
			switch s.Install.Mode {
			case "", "user", "system":
			default:
				return fmt.Errorf("%s: install.mode %q must be user or system", s.ID, s.Install.Mode)
			}
			// A systemd requirement with the supervised backend, or without system mode, would
			// silently exercise something other than what the scenario claims to test.
			if s.Install.NeedsRealSystemd && s.Install.Service == "none" {
				return fmt.Errorf("%s: needs_real_systemd with service=none is contradictory; "+
					"the supervised backend does not use systemd", s.ID)
			}
			if s.Install.NeedsRealSystemd && s.Install.Mode != "system" {
				return fmt.Errorf("%s: needs_real_systemd requires mode=system; a user unit in a "+
					"container has no logind session to attach to", s.ID)
			}
			// A user-mode install configures $HOME directly and never resolves anyone by name, so
			// the NSS gap it exists to expose is unreachable there. Allowing the combination would
			// produce a scenario that passes without testing anything it claims to.
			if s.Install.NSSOnlyUser && s.Install.Mode != "system" {
				return fmt.Errorf("%s: nss_only_user requires mode=system; a user-mode install "+
					"resolves $HOME and never looks the account up by name", s.ID)
			}
			// The probe runs Vector by hand as the agent user, against the user-mode log and secrets
			// file. System mode keeps both under root-owned paths, and the nested lane runs the
			// endpoint somewhere the stand-in is not, so either would need a different probe.
			if s.Install.VerifyVectorForwarding && (s.Install.Mode == "system" || s.Install.NeedsRealSystemd) {
				return fmt.Errorf("%s: verify_vector_forwarding needs a user-mode install outside the "+
					"nested systemd lane, because the probe runs Vector as the agent user", s.ID)
			}
			// A user-mode uninstall needs no privileges, so there is no privilege failure to catch
			// and the probe would assert something that cannot happen.
			if s.Install.VerifyUnprivilegedUninstallFails && s.Install.Mode != "system" {
				return fmt.Errorf("%s: verify_unprivileged_uninstall_fails requires mode=system; "+
					"a user-mode uninstall is supposed to succeed unprivileged", s.ID)
			}
			switch s.Install.ExpectServiceKind {
			case "", "systemd", "launchd", "windows-service", "none":
			default:
				return fmt.Errorf("%s: install.expect_service_kind %q must be systemd, launchd, "+
					"windows-service or none", s.ID, s.Install.ExpectServiceKind)
			}
			switch s.Install.Service {
			case "", "auto", "systemd", "launchd", "windows-service", "none":
			default:
				return fmt.Errorf("%s: install.service %q must be auto, systemd, launchd, "+
					"windows-service, or none", s.ID, s.Install.Service)
			}
		}

		// A setup step that writes the sentinel makes the gate unfalsifiable: the file is present
		// before the agent runs, so INCONCLUSIVE can never fire and an idle agent is misreported as
		// a capture failure. s04 shipped with exactly this mistake, so the rule is enforced here
		// rather than left to review.
		//
		// Checked after expansion, not on the raw text. A first version matched only the literal
		// "{{sentinel}}" placeholder, which a setup writing the sentinel's real path -- or building
		// it from {{workdir}} -- walked straight past. Cursor Bugbot pointed out that the guard was
		// narrower than the rule it claimed to enforce.
		if s.Sentinel != "" && s.Setup != "" {
			if target, why := setupPlantsSentinel(s.Setup, s.Sentinel); target != "" {
				return fmt.Errorf("%s: setup %s, which makes the sentinel present regardless of "+
					"what the agent does; the sentinel must be produced by the agent", s.ID, why)
			}
		}

		// A dotted path with an empty segment silently never resolves, so a typo like
		// "command..command" would make an expectation quietly unsatisfiable rather than
		// failing loudly here.
		for _, f := range e.Fields {
			if strings.TrimSpace(f) == "" || strings.Contains(f, "..") ||
				strings.HasPrefix(f, ".") || strings.HasSuffix(f, ".") {
				return fmt.Errorf("%s: expect[%d] has malformed field path %q", s.ID, i, f)
			}
		}
	}
	return nil
}

// Load reads one scenario file.
func Load(path string) (Scenario, error) {
	var s Scenario
	b, err := os.ReadFile(path)
	if err != nil {
		return s, err
	}
	decoder := yaml.NewDecoder(bytes.NewReader(b))
	decoder.KnownFields(true)
	if err := decoder.Decode(&s); err != nil && !errors.Is(err, io.EOF) {
		return s, fmt.Errorf("%s: %w", filepath.Base(path), err)
	}
	if err := s.Validate(); err != nil {
		return s, err
	}
	return s, nil
}

// LoadSuite reads every *.yaml in dir, sorted by filename so ordering is deterministic.
//
// Every scenario is validated, including ones this platform will not run: a syntax error or a
// stale field in a Windows scenario should fail a Linux contributor's `go test`, not wait for
// somebody to dispatch a Windows run.
func LoadSuite(dir string) (Suite, error) {
	paths, err := filepath.Glob(filepath.Join(dir, "*.yaml"))
	if err != nil {
		return Suite{}, err
	}
	if len(paths) == 0 {
		return Suite{}, fmt.Errorf("no scenarios found in %s", dir)
	}
	suite := Suite{Name: filepath.Base(dir)}
	for _, p := range paths {
		s, err := Load(p)
		if err != nil {
			return Suite{}, err
		}
		suite.Scenarios = append(suite.Scenarios, s)
	}
	return suite, nil
}

// For returns the scenarios written for one platform.
//
// Reports how many were set aside rather than silently narrowing the suite. `run` with no
// --scenario is the "everything" command, and a caller who asked for everything and got two
// thirds of it is entitled to know -- silent truncation reads as full coverage.
func (s Suite) For(p Platform) (matching Suite, skipped int) {
	out := Suite{Name: s.Name}
	for _, sc := range s.Scenarios {
		if sc.TargetPlatform() == p {
			out.Scenarios = append(out.Scenarios, sc)
			continue
		}
		skipped++
	}
	return out, skipped
}

// Expand substitutes run-scoped placeholders into a template string.
func Expand(tmpl string, canary, sentinel, workdir string) string {
	r := strings.NewReplacer(
		"{{canary}}", canary,
		"{{sentinel}}", sentinel,
		"{{workdir}}", workdir,
	)
	return r.Replace(tmpl)
}

// setupPlantsSentinel reports whether a setup step would create the sentinel before the session.
//
// Expansion happens first so the three equivalent spellings are all caught: the {{sentinel}}
// placeholder, the sentinel's literal path, and a path assembled from {{workdir}}. The basename
// is also checked, because "{{workdir}}/x" and a sentinel of "/home/agent/work/x" only agree
// once the real workdir is known, which this package deliberately does not import.
func setupPlantsSentinel(setup, sentinel string) (target, why string) {
	const (
		probeCanary  = "PROBE_CANARY"
		probeWorkdir = "PROBE_WORKDIR"
	)
	sent := Expand(sentinel, probeCanary, "", probeWorkdir)
	exp := Expand(setup, probeCanary, sent, probeWorkdir)

	// Boundary-aware for the full path too, not just the basename: a plain substring test made
	// "> /home/agent/work/out.txt.bak" look like it wrote /home/agent/work/out.txt.
	if sent != "" && containsPathElement(exp, sent) {
		return sent, fmt.Sprintf("writes the sentinel path %q", sent)
	}
	// A trailing path element match catches {{workdir}}-relative spellings of the same file, but
	// it has to respect path boundaries. A bare substring test rejected valid setups: a sentinel
	// named out.txt matched a fixture called timeout.txt or layout.txt, so a legitimate scenario
	// could not be written at all. Cursor Bugbot caught the false positive.
	if base := path.Base(sent); base != "" && base != "." && base != "/" {
		if containsPathElement(exp, base) {
			return base, fmt.Sprintf("writes %q, the sentinel's file name", base)
		}
	}
	return "", ""
}

// containsPathElement reports whether name appears in text as a whole final path element rather
// than as an arbitrary substring, so "out.txt" does not match "timeout.txt".
func containsPathElement(text, name string) bool {
	for i := 0; ; {
		j := strings.Index(text[i:], name)
		if j < 0 {
			return false
		}
		start := i + j
		end := start + len(name)
		// The character before must not extend the file name -- a separator or any
		// non-filename character (space, quote, redirect) is a real boundary.
		leftOK := start == 0 || !isFilenameByte(text[start-1])
		// The character after must not extend it either, so "out.txt" does not match
		// "out.txt.bak".
		rightOK := end == len(text) || !isFilenameByte(text[end])
		if leftOK && rightOK {
			return true
		}
		i = start + 1
	}
}

func isFilenameByte(b byte) bool {
	switch {
	case b >= 'a' && b <= 'z', b >= 'A' && b <= 'Z', b >= '0' && b <= '9':
		return true
	case b == '.' || b == '_' || b == '-':
		return true
	}
	return false
}

// VerifiesRestartOnReinstall reports whether a second install must replace the collector process.
func (s Scenario) VerifiesRestartOnReinstall() bool {
	return s.Install != nil && s.Install.VerifyRestartOnReinstall
}

// VerifiesRollbackOnFailedReinstall reports whether a deliberately failed reinstall must leave the
// machine with exactly the endpoint it had beforehand.
func (s Scenario) VerifiesRollbackOnFailedReinstall() bool {
	return s.Install != nil && s.Install.VerifyRollbackOnFailedReinstall
}

// VerifiesUnprivilegedUninstallFails reports whether an unprivileged removal must be refused.
func (s Scenario) VerifiesUnprivilegedUninstallFails() bool {
	return s.Install != nil && s.Install.VerifyUnprivilegedUninstallFails
}

// NeedsNSSOnlyUser reports whether the session account must be resolvable only through NSS.
func (s Scenario) NeedsNSSOnlyUser() bool {
	return s.Install != nil && s.Install.NSSOnlyUser
}

// VerifiesVectorForwarding reports whether the bundled Vector must forward to a loopback stand-in.
func (s Scenario) VerifiesVectorForwarding() bool {
	return s.Install != nil && s.Install.VerifyVectorForwarding
}

// NeedsRealSystemd reports whether this scenario must run somewhere systemd is genuinely PID 1.
func (s Scenario) NeedsRealSystemd() bool {
	return s.Install != nil && s.Install.NeedsRealSystemd
}