oficina / equipo Public

fix: keep the service alive through recoverable runner failures

codex agent, operated by paul@keymail.dev pushed by claude agent, operated by paul@keymail.dev 298caffe963897a67233acc0d9e0276f94ab22a9 parent e24b681
6 files changed, +91 −10
  • cmd/equipo-runner/main.go +1 −0
  • internal/runner/config.go +1 −0
  • internal/runner/final_service_test.go +44 −0
  • internal/runner/journal.go +1 −1
  • internal/runner/runtime_workflow.go +1 −1
  • internal/runner/service.go +43 −8
diff --git a/cmd/equipo-runner/main.go b/cmd/equipo-runner/main.go
index 86946212..5db2643a 100644
--- a/cmd/equipo-runner/main.go
+++ b/cmd/equipo-runner/main.go
@@ -49,6 +49,7 @@ func run(ctx context.Context, args []string, input io.Reader, output, errorOutpu
return fail(err)
}
cfg.Copy = equipo.RunnerToolCopy()
+ cfg.Report = func(code string) { _, _ = fmt.Fprintf(errorOutput, equipo.CopyRunnerDiagnostic, code, "-") }
switch args[0] {
case "enrol":
err = runner.Enrol(ctx, cfg, output)
diff --git a/internal/runner/config.go b/internal/runner/config.go
index e3bc111b..8153444c 100644
--- a/internal/runner/config.go
+++ b/internal/runner/config.go
@@ -16,6 +16,7 @@ import (
)
type Config struct {
+ Report func(string)
Origin, StateDir, PaseoHome, NodePath, BridgePath, AmadanCredentialPath, AccountBindingsPath, CodexPath string
GoRoot, GoBinary, WorkspaceRoot, ScratchRoot, GitPath, Executable string
PaseoEndpoint, PaseoServerID, InstanceID string
diff --git a/internal/runner/final_service_test.go b/internal/runner/final_service_test.go
new file mode 100644
index 00000000..5a998396
--- /dev/null
+++ b/internal/runner/final_service_test.go
@@ -0,0 +1,44 @@
+package runner
+
+import (
+ "amadan.net/oficina/equipo/internal/runnerproto"
+ "context"
+ "errors"
+ "testing"
+)
+
+type resilientSource struct {
+ polls int
+ attempt bool
+}
+
+func (s *resilientSource) Poll(context.Context) ([]runnerproto.Assignment, error) {
+ s.polls++
+ if s.polls > 1 {
+ return nil, context.Canceled
+ }
+ if s.attempt {
+ return []runnerproto.Assignment{{ID: "work", Revision: 1}}, nil
+ }
+ return nil, ErrTransient
+}
+func (s *resilientSource) Claim(context.Context, runnerproto.ClaimRequest) (runnerproto.ClaimResponse, error) {
+ return runnerproto.ClaimResponse{}, nil
+}
+func (s *resilientSource) RecoverClaim(context.Context, runnerproto.ClaimRequest) (runnerproto.ClaimResponse, error) {
+ return runnerproto.ClaimResponse{}, ErrNotClaimed
+}
+func TestServiceSurvivesPollAndAttemptErrors(t *testing.T) {
+ for _, attempt := range []bool{false, true} {
+ t.Run(map[bool]string{false: "poll", true: "attempt"}[attempt], func(t *testing.T) {
+ s, _, _, _ := cleanupFixture(t)
+ s.workflow = &BugWorkflow{}
+ source := &resilientSource{attempt: attempt}
+ service := Service{Supervisor: s, Source: source}
+ err := service.Run(context.Background())
+ if !errors.Is(err, context.Canceled) || source.polls != 2 {
+ t.Fatal("service stopped on recoverable error", err, source.polls)
+ }
+ })
+ }
+}
diff --git a/internal/runner/journal.go b/internal/runner/journal.go
index 73482cd5..02fb1430 100644
--- a/internal/runner/journal.go
+++ b/internal/runner/journal.go
@@ -19,7 +19,7 @@ type Event = runnerproto.Event
const MaxJournalBytes int64 = 128 << 20
-var ErrJournal = paseo.ErrUncertain
+var ErrJournal = errors.Join(paseo.ErrUncertain, errors.New("runner.journal"))
type Journal struct {
db *sql.DB
diff --git a/internal/runner/runtime_workflow.go b/internal/runner/runtime_workflow.go
index 1a3fcf9d..ba7ae45c 100644
--- a/internal/runner/runtime_workflow.go
+++ b/internal/runner/runtime_workflow.go
@@ -185,6 +185,6 @@ func (r *Runtime) wire(ctx context.Context) error {
if err != nil {
return err
}
- r.service = &Service{Supervisor: supervisor, Source: r, BeforeRun: func(c RunClaim) { r.mu.Lock(); r.current = &c; r.mu.Unlock() }}
+ r.service = &Service{Supervisor: supervisor, Source: r, Report: r.cfg.Report, BeforeRun: func(c RunClaim) { r.mu.Lock(); r.current = &c; r.mu.Unlock() }}
return nil
}
diff --git a/internal/runner/service.go b/internal/runner/service.go
index 90bbec4a..d96a3bbc 100644
--- a/internal/runner/service.go
+++ b/internal/runner/service.go
@@ -18,25 +18,43 @@ type Service struct {
Supervisor *Supervisor
Source AssignmentSource
BeforeRun func(RunClaim)
+ Report func(string)
}
func (s *Service) Run(ctx context.Context) error {
if s.Supervisor == nil || s.Source == nil {
return paseo.ErrContract
}
- claimErr := s.recoverClaims(ctx)
- cleanupErr := s.Supervisor.Recover(ctx)
- if err := errors.Join(claimErr, cleanupErr); err != nil {
- s.Supervisor.ready = false
- return err
- }
+ needsRecovery := true
for {
if ctx.Err() != nil {
return ctx.Err()
}
+ if needsRecovery {
+ err := errors.Join(s.recoverClaims(ctx), s.Supervisor.Recover(ctx))
+ if err != nil {
+ s.Supervisor.ready = false
+ if fatalServiceError(err) || errors.Is(err, context.Canceled) {
+ return err
+ }
+ s.report("runner.recovery_retry")
+ if err := serviceBackoff(ctx); err != nil {
+ return err
+ }
+ continue
+ }
+ needsRecovery = false
+ }
assignments, err := s.Source.Poll(ctx)
if err != nil {
- return err
+ if fatalServiceError(err) || errors.Is(err, context.Canceled) {
+ return err
+ }
+ s.report("runner.poll_retry")
+ if err := serviceBackoff(ctx); err != nil {
+ return err
+ }
+ continue
}
for _, a := range assignments {
if err := s.Supervisor.journal.AdmissionAllowed(ctx); err != nil {
@@ -70,7 +88,15 @@ func (s *Service) Run(ctx context.Context) error {
s.BeforeRun(RunClaim{Assignment: a, ClaimResponse: claim})
}
if err = s.Supervisor.Run(ctx, RunClaim{Assignment: a, ClaimResponse: claim}); err != nil {
- return err
+ if errors.Is(err, ErrJournal) {
+ return err
+ }
+ needsRecovery = true
+ s.report("runner.attempt_retry")
+ if err := serviceBackoff(ctx); err != nil {
+ return err
+ }
+ break
}
}
}
@@ -150,3 +176,12 @@ func serviceBackoff(ctx context.Context) error {
return nil
}
}
+
+func fatalServiceError(err error) bool {
+ return errors.Is(err, ErrJournal) || errors.Is(err, paseo.ErrContract) || errors.Is(err, paseo.ErrIdentity)
+}
+func (s *Service) report(code string) {
+ if s.Report != nil {
+ s.Report(code)
+ }
+}