Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(phrocs): make r (re)run procs in any state, not just running or crashed (#69667)

Marius Andra5d729d724ae9
diff --git a/tools/phrocs/internal/tui/keys.go b/tools/phrocs/internal/tui/keys.go
index f58e4d792af2..b51dbff7a3bc 100644
--- a/tools/phrocs/internal/tui/keys.go
+++ b/tools/phrocs/internal/tui/keys.go
@@ -312,21 +312,17 @@ func (m *Model) updateProcKeys() {
if p.IsStandby() {
m.keys.Start.SetEnabled(true)
m.keys.Stop.SetEnabled(false)
- m.keys.Restart.SetEnabled(false)
+ m.keys.Restart.SetEnabled(true)
m.keys.ClearLogs.SetEnabled(false)
return
}
- // Snapshot status once so running/crashed are derived from the same
- // observation — avoids a second trip through the proc mutex and any
- // chance of inconsistency between the two reads.
- st := p.Status()
- running := st.IsRunning()
- crashed := st == process.StatusCrashed
+ running := p.Status().IsRunning()
m.keys.Start.SetEnabled(!running)
m.keys.Stop.SetEnabled(running)
- // `r` doubles as "restart a running proc" and "kick a crashed proc back to
- // life" — both express the same user intent of "rerun this thing".
- m.keys.Restart.SetEnabled(running || crashed)
+ // `r` always has work to do — restart a running proc, or (re)run one
+ // that crashed, finished, was stopped, or never started. One key, one
+ // intent: "run this thing", regardless of what state it's in.
+ m.keys.Restart.SetEnabled(true)
m.keys.ClearLogs.SetEnabled(running)
}
@@ -519,11 +515,18 @@ func (m Model) handleNormalKey(msg tea.KeyPressMsg, cmds []tea.Cmd) (tea.Model,
if p := m.activeProc(); p != nil {
send := m.mgr.Send()
switch {
+ case p.IsStandby():
+ if real, ok := m.promoteStandby(); ok {
+ m.dbg("restart: promoted standby proc=%s", real.Name)
+ go func() { _ = real.Start(send) }()
+ }
case p.IsRunning():
m.dbg("restart: proc=%s", p.Name)
go p.Restart(send)
- case p.Status() == process.StatusCrashed:
- m.dbg("restart (from crashed): proc=%s", p.Name)
+ default:
+ // Crashed, finished, stopped, or never started — `r` means
+ // "run it (again)", so one-shot tasks rerun on a keypress.
+ m.dbg("restart (from %s): proc=%s", p.Status(), p.Name)
go func() { _ = p.Start(send) }()
}
}
diff --git a/tools/phrocs/internal/tui/restart_failed_test.go b/tools/phrocs/internal/tui/restart_failed_test.go
index 4d7d90734ab1..745720819ff7 100644
--- a/tools/phrocs/internal/tui/restart_failed_test.go
+++ b/tools/phrocs/internal/tui/restart_failed_test.go
@@ -244,10 +244,9 @@ func waitRunning(t *testing.T, p *process.Process, send func(tea.Msg)) {
}
// TestUpdateProcKeys_RestartBindingGating verifies the `r` (Restart) binding
-// is enabled in exactly the states where it has meaningful work: a running
-// proc (existing behavior) or a crashed proc (new behavior). Never-started,
-// cleanly-exited, and manually-stopped procs are all the user's chosen end-
-// state and must leave `r` disabled.
+// is enabled in every proc state — running procs restart, everything else
+// (never-started, crashed, cleanly-exited one-shots, manually-stopped) starts.
+// `r` is the universal "run this thing" key.
func TestUpdateProcKeys_RestartBindingGating(t *testing.T) {
cases := []struct {
name string
@@ -259,7 +258,7 @@ func TestUpdateProcKeys_RestartBindingGating(t *testing.T) {
setup: func(t *testing.T) Model {
return readyModel(t, "backend")
},
- wantEnabled: false,
+ wantEnabled: true,
},
{
name: "running proc",
@@ -286,7 +285,7 @@ func TestUpdateProcKeys_RestartBindingGating(t *testing.T) {
runUntilStatus(t, findProc(t, m, "oneshot"), m.mgr.Send(), process.StatusDone)
return m
},
- wantEnabled: false,
+ wantEnabled: true,
},
{
name: "manually stopped",
@@ -297,7 +296,7 @@ func TestUpdateProcKeys_RestartBindingGating(t *testing.T) {
p.Stop()
return m
},
- wantEnabled: false,
+ wantEnabled: true,
},
}
@@ -312,36 +311,49 @@ func TestUpdateProcKeys_RestartBindingGating(t *testing.T) {
}
}
-// ── `r` keypress on a crashed proc actually restarts it ─────────────────────────
+// ── `r` keypress on a non-running proc actually (re)starts it ────────────────────
-func TestHandleNormalKey_RestartKeyRevivesCrashedProc(t *testing.T) {
- // End-to-end check that pressing `r` on a crashed proc kicks off Start
- // (not just that the binding is enabled). We use `sleep 30` as the shell
- // — once the second start fires, the proc should be running again.
- m := modelWithProcs(t, map[string]string{"flaky": "exit 1"})
- runUntilStatus(t, findProc(t, m, "flaky"), m.mgr.Send(), process.StatusCrashed)
+// End-to-end check that pressing `r` kicks off Start on procs in terminal
+// states (not just that the binding is enabled). Covers the crashed case and
+// the finished one-shot case ("migrate postgres"-style tasks that exit 0).
+func TestHandleNormalKey_RestartKeyRerunsTerminalProc(t *testing.T) {
+ cases := []struct {
+ name string
+ shell string
+ target process.Status
+ }{
+ {name: "crashed", shell: "exit 1", target: process.StatusCrashed},
+ {name: "finished one-shot", shell: "true", target: process.StatusDone},
+ }
- // Swap the shell to a long-running command so the post-`r` start lands on
- // a process that won't immediately crash again — lets us observe the
- // running state without races.
- p := findProc(t, m, "flaky")
- p.Cfg.Shell = "sleep 30"
- t.Cleanup(func() { p.Stop() })
+ for _, tc := range cases {
+ t.Run(tc.name, func(t *testing.T) {
+ m := modelWithProcs(t, map[string]string{"task": tc.shell})
+ runUntilStatus(t, findProc(t, m, "task"), m.mgr.Send(), tc.target)
- m.updateProcKeys()
- if !m.keys.Restart.Enabled() {
- t.Fatal("precondition: Restart binding should be enabled on a crashed proc")
- }
+ // Swap the shell to a long-running command so the post-`r` start
+ // lands on a process that won't immediately exit again — lets us
+ // observe the running state without races.
+ p := findProc(t, m, "task")
+ p.Cfg.Shell = "sleep 30"
+ t.Cleanup(func() { p.Stop() })
- next, _ := m.Update(keypress('r'))
- _ = next
+ m.updateProcKeys()
+ if !m.keys.Restart.Enabled() {
+ t.Fatalf("precondition: Restart binding should be enabled on a %s proc", tc.name)
+ }
- deadline := time.After(2 * time.Second)
- for !p.IsRunning() {
- select {
- case <-deadline:
- t.Fatalf("crashed proc never restarted (status: %s)", p.Status())
- case <-time.After(20 * time.Millisecond):
- }
+ next, _ := m.Update(keypress('r'))
+ _ = next
+
+ deadline := time.After(2 * time.Second)
+ for !p.IsRunning() {
+ select {
+ case <-deadline:
+ t.Fatalf("%s proc never restarted (status: %s)", tc.name, p.Status())
+ case <-time.After(20 * time.Millisecond):
+ }
+ }
+ })
}
}
diff --git a/tools/phrocs/internal/tui/standby_test.go b/tools/phrocs/internal/tui/standby_test.go
index 378e575ca54e..9f99d544eb5a 100644
--- a/tools/phrocs/internal/tui/standby_test.go
+++ b/tools/phrocs/internal/tui/standby_test.go
@@ -272,8 +272,9 @@ func TestShowAll_cursorPreservedOnToggle(t *testing.T) {
// ── Key enablement ──────────────────────────────────────────────────────────────
func TestShowAll_keyEnablementByProcState(t *testing.T) {
- // Expected key enablement for each process state. Ensures standby processes
- // only expose Start, while other states follow the running/stopped rules.
+ // Expected key enablement for each process state. `r` is enabled in every
+ // state (it acts as start on standby/stopped procs); Stop stays gated on
+ // running.
cases := []struct {
name string
procName string // name of the process to focus on (from readyShowAllModel)
@@ -281,8 +282,8 @@ func TestShowAll_keyEnablementByProcState(t *testing.T) {
wantStop bool
wantRestart bool
}{
- {name: "standby", procName: "capture", wantStart: true},
- {name: "stopped", procName: "backend", wantStart: true},
+ {name: "standby", procName: "capture", wantStart: true, wantRestart: true},
+ {name: "stopped", procName: "backend", wantStart: true, wantRestart: true},
}
for _, tc := range cases {