Skip to content

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

Tighten CI collector shutdown handling

Cursor Agent87019276d4fb
diff --git a/cli/beacon/cmd/ci.go b/cli/beacon/cmd/ci.go
index 707aaede..fff238ae 100644
--- a/cli/beacon/cmd/ci.go
+++ b/cli/beacon/cmd/ci.go
@@ -5,6 +5,8 @@ import (
"encoding/json"
"fmt"
"os"
+ "os/signal"
+ "syscall"
"time"
"github.com/spf13/cobra"
@@ -71,8 +73,11 @@ func init() {
}
func runCIExec(cmd *cobra.Command, args []string) error {
- if ciOpts.requireHarness == "" {
- ciOpts.requireHarness = ciOpts.harness
+ runCtx, stopSignals := signal.NotifyContext(cmd.Context(), os.Interrupt, syscall.SIGTERM)
+ defer stopSignals()
+ requireHarness := ciOpts.requireHarness
+ if requireHarness == "" {
+ requireHarness = ciOpts.harness
}
session, err := beaconci.Provision(beaconci.Options{
BaseDir: ciOpts.baseDir,
@@ -88,10 +93,10 @@ func runCIExec(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
- if err := session.Start(cmd.Context(), os.Stdout, os.Stderr); err != nil {
+ if err := session.Start(runCtx, os.Stdout, os.Stderr); err != nil {
return err
}
- childExit, childErr := session.RunChild(cmd.Context(), args, os.Stdout, os.Stderr)
+ childExit, childErr := session.RunChild(runCtx, args, os.Stdout, os.Stderr)
stopCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
stopErr := session.Stop(stopCtx)
cancel()
@@ -104,7 +109,7 @@ func runCIExec(cmd *cobra.Command, args []string) error {
result := beaconci.Validate(beaconci.ValidationOptions{
LogPath: session.LogPath,
MinEvents: ciOpts.minEvents,
- RequireHarness: ciOpts.requireHarness,
+ RequireHarness: requireHarness,
})
execResult := beaconci.ExecResult{
Session: *session,
@@ -112,6 +117,12 @@ func runCIExec(cmd *cobra.Command, args []string) error {
Validation: result,
ArtifactMessage: fmt.Sprintf("Beacon CI artifacts: log=%s config=%s", session.LogPath, session.ConfigPath),
}
+ if !ciOpts.keepArtifacts && result.Status != "fail" && childExit == 0 && ciOpts.baseDir == "" && ciOpts.logPath == "" {
+ if err := os.RemoveAll(session.BaseDir); err != nil {
+ return err
+ }
+ execResult.ArtifactMessage = "Beacon CI artifacts cleaned"
+ }
if ciOpts.jsonOutput {
_ = json.NewEncoder(os.Stdout).Encode(execResult)
} else {
diff --git a/cli/beacon/internal/ci/session.go b/cli/beacon/internal/ci/session.go
index 588b6525..beb21613 100644
--- a/cli/beacon/internal/ci/session.go
+++ b/cli/beacon/internal/ci/session.go
@@ -28,7 +28,8 @@ var (
resolveCollectorBinary = endpointcollector.ResolveBinary
writeCollectorConfig = endpointcollector.WriteConfig
waitCollectorReady = endpointcollector.WaitUntilReady
- commandContext = exec.CommandContext
+ collectorCommand = exec.Command
+ childCommandContext = exec.CommandContext
)
type Options struct {
@@ -55,10 +56,9 @@ type Session struct {
StartedAt string `json:"started_at"`
Run *schema.RunInfo `json:"run,omitempty"`
- cfg endpointconfig.Config
- cancel context.CancelFunc
- cmd *exec.Cmd
- done chan error
+ cfg endpointconfig.Config
+ cmd *exec.Cmd
+ done chan error
}
type ExecResult struct {
@@ -139,21 +139,21 @@ func (s *Session) Start(ctx context.Context, stdout, stderr io.Writer) error {
if s == nil {
return errors.New("ci session is nil")
}
+ if err := ctx.Err(); err != nil {
+ return err
+ }
if stdout == nil {
stdout = io.Discard
}
if stderr == nil {
stderr = io.Discard
}
- runCtx, cancel := context.WithCancel(ctx)
- cmd := commandContext(runCtx, s.CollectorBinary, "--config", s.ConfigPath)
+ cmd := collectorCommand(s.CollectorBinary, "--config", s.ConfigPath)
cmd.Stdout = stdout
cmd.Stderr = stderr
if err := cmd.Start(); err != nil {
- cancel()
return err
}
- s.cancel = cancel
s.cmd = cmd
s.done = make(chan error, 1)
go func() {
@@ -173,9 +173,6 @@ func (s *Session) Stop(ctx context.Context) error {
if s.cmd.Process != nil {
_ = terminateProcess(s.cmd.Process)
}
- if s.cancel != nil {
- s.cancel()
- }
select {
case err := <-s.done:
if err != nil && !isExpectedStopError(err) {
@@ -194,7 +191,7 @@ func (s *Session) RunChild(ctx context.Context, args []string, stdout, stderr io
if len(args) == 0 {
return 0, errors.New("child command is required after --")
}
- cmd := commandContext(ctx, args[0], args[1:]...)
+ cmd := childCommandContext(ctx, args[0], args[1:]...)
cmd.Dir = s.WorkDir
cmd.Env = append(ClaudeEnv(os.Environ(), s.GRPCEndpoint, s.cfg.ContentRetention),
"BEACON_CI_BASE_DIR="+s.BaseDir,