Use Status.IsTerminal for every terminal run check

Merged
RY
14d ago
+17-21

Summary

Follow-up from #3676, where @yohamta0 pointed out that a few call sites still work out "the run has ended" on their own as status != NotStarted && !IsActive() instead of using ir.Status.IsTerminal(). If a status is added to ir.Status later, those copies and IsTerminal() would disagree. This switches them all to IsTerminal(), so there is one definition.

Changes

  • internal/service/coordinator: drop isTerminalRunStatus and call Status.IsTerminal() at its callers (distributed_attempts.go, handler.go, remote_attempt.go). Also replace the five inline !IsActive() && != NotStarted checks in handler.go.
  • internal/cmd/retry.go: retrySourceMayStillBeFinalizing uses IsTerminal().
  • internal/runtime/agent/agent.go: pushStatus picks the terminal push timeout with IsTerminal().
  • internal/service/worker/coordreport/status_pusher.go: the final-delivery retry check uses IsTerminal().
  • internal/service/coordinator/subflow/runner.go and internal/service/frontend/api/v1/service_dags.go (runEnded): same.

No user-visible change.

Core change

For every defined status the old expression and IsTerminal() give the same answer: both are true for Failed, Aborted, Succeeded, PartiallySucceeded and Rejected, and false for NotStarted, Running, Queued and Waiting. The only difference is a value outside the enum: the old expression treated it as ended, IsTerminal() treats it as not ended (its default branch).

// internal/cmd/retry.go:706
return status.Status.IsTerminal()

// internal/runtime/agent/agent.go:2060
if status.Status.IsTerminal() {

// internal/service/coordinator/distributed_attempts.go:589
return status.IsTerminal() && !status.IsSuccess()

Testing

No new test. TestStatus_IsTerminal in internal/ir/status_test.go already pins the set, and the call sites keep their current tests.

  • go build ./...
  • go vet on the touched packages. internal/runtime/agent reports a lostcancel warning at agent.go:1224, which is on main too and not touched here.
  • go test -count=1 ./internal/ir/ ./internal/cmd/ ./internal/runtime/agent/ ./internal/service/coordinator/... ./internal/service/frontend/api/v1/ ./internal/service/worker/coordreport/: all pass.
  • golangci-lint run --new-from-rev=upstream/main on the touched packages: 0 issues.

Not run: make test (race), make check, conformance, Windows.

Environment: main @ 395de48d2, go1.27.4, darwin/arm64.

Performance

No performance risk: each change swaps one in-memory status comparison for another, on the same paths.

Out of scope

  • runEnded(string) in internal/service/scheduler/restart_scanner.go lists the same five statuses, but it matches event status strings, so switching it means parsing the string first. Left as is.
  • handler.go:2268 keeps latestStatus.Status != ir.NotStarted && !...IsTerminal(). That NotStarted check isn't redundant (NotStarted isn't terminal), and the condition reads as "is active", so I didn't rewrite it.

Summary by cubic

Replaces every hand-rolled terminal-run check (status != ir.NotStarted && !status.IsActive()) with the centralized ir.Status.IsTerminal() so there is a single definition of a terminal status.

Refactors

  • Removes the isTerminalRunStatus helper in internal/service/coordinator and calls Status.IsTerminal() at its call sites.
  • Swaps the inline checks in handler.go, retry.go, agent.go, coordreport/status_pusher.go, subflow/runner.go, and service_dags.go for IsTerminal().
  • No behavior change for defined statuses; an out-of-enum value is now treated as not terminal, where the old expression treated it as terminal.

Summary by CodeRabbit

  • Bug Fixes
    • Run completion is now recognized consistently across workflow coordination and status reporting. This helps prevent non-terminal states from being treated as finished and ensures terminal states receive the appropriate final-status handling, including during cancellation and distributed-run updates.