fix(cli): recover unresponsive service restarts
This commit is contained in:
parent
a149a61a89
commit
69d3f7d0d9
13 changed files with 378 additions and 50 deletions
|
|
@ -140,7 +140,7 @@ export type TuiInput = {
|
|||
server: {
|
||||
endpoint: Service.Endpoint
|
||||
reconnect?: (onStatus: (status: Service.Status) => void, signal: AbortSignal) => Promise<Service.Endpoint>
|
||||
reload?: () => Promise<void>
|
||||
reload?: (signal?: AbortSignal) => Promise<void>
|
||||
}
|
||||
args: Args
|
||||
config: Config.Interface
|
||||
|
|
@ -1121,7 +1121,7 @@ function App(props: { pair?: DialogPairCredentials }) {
|
|||
<StartupLoading ready={plugins.ready} />
|
||||
</Show>
|
||||
<Show when={showReconnecting()}>
|
||||
<Reconnecting status={client.connection.service()} />
|
||||
<Reconnecting status={client.connection.service()} restart={client.reload} />
|
||||
</Show>
|
||||
</box>
|
||||
)
|
||||
|
|
|
|||
|
|
@ -1,11 +1,52 @@
|
|||
import type { Service } from "@opencode-ai/client/effect"
|
||||
import { Show } from "solid-js"
|
||||
import { createSignal, onCleanup, Show } from "solid-js"
|
||||
import { Keymap } from "../context/keymap"
|
||||
import { useTheme } from "../context/theme"
|
||||
import { errorMessage } from "../util/error"
|
||||
import { Spinner } from "./spinner"
|
||||
|
||||
export function Reconnecting(props: { status?: Service.Status }) {
|
||||
const restartCommand = "service.restart"
|
||||
|
||||
export function Reconnecting(props: { status?: Service.Status; restart?: (signal?: AbortSignal) => Promise<void> }) {
|
||||
const theme = useTheme().theme
|
||||
const copy = () => reconnectingCopy(props.status)
|
||||
const shortcuts = Keymap.useShortcuts()
|
||||
const [restarting, setRestarting] = createSignal(false)
|
||||
const [failure, setFailure] = createSignal<string>()
|
||||
let controller: AbortController | undefined
|
||||
const copy = () =>
|
||||
restarting()
|
||||
? { loading: true, message: "Restarting background service..." }
|
||||
: reconnectingCopy(props.status, shortcuts.get(restartCommand))
|
||||
|
||||
Keymap.createLayer(() => ({
|
||||
mode: "global",
|
||||
priority: 1000,
|
||||
commands: [
|
||||
{
|
||||
id: restartCommand,
|
||||
bind: "r",
|
||||
title: "Restart service",
|
||||
enabled: props.status?.type === "unresponsive" && !!props.restart,
|
||||
run: () => {
|
||||
if (!props.restart || restarting()) return
|
||||
controller = new AbortController()
|
||||
setFailure(undefined)
|
||||
setRestarting(true)
|
||||
void props
|
||||
.restart(controller.signal)
|
||||
.then(
|
||||
() => setFailure(undefined),
|
||||
(error) => {
|
||||
setFailure(errorMessage(error))
|
||||
setRestarting(false)
|
||||
},
|
||||
)
|
||||
},
|
||||
},
|
||||
],
|
||||
}))
|
||||
|
||||
onCleanup(() => controller?.abort())
|
||||
|
||||
return (
|
||||
<box
|
||||
|
|
@ -37,12 +78,19 @@ export function Reconnecting(props: { status?: Service.Status }) {
|
|||
)}
|
||||
</Show>
|
||||
</Show>
|
||||
<Show when={failure()}>
|
||||
{(message) => (
|
||||
<text fg={theme.error} wrapMode="word">
|
||||
{message()}
|
||||
</text>
|
||||
)}
|
||||
</Show>
|
||||
</box>
|
||||
</box>
|
||||
)
|
||||
}
|
||||
|
||||
export function reconnectingCopy(status?: Service.Status) {
|
||||
export function reconnectingCopy(status?: Service.Status, restart = "r") {
|
||||
if (status?.type === "starting")
|
||||
return {
|
||||
loading: true,
|
||||
|
|
@ -59,7 +107,7 @@ export function reconnectingCopy(status?: Service.Status) {
|
|||
return {
|
||||
loading: false,
|
||||
message: "Background service is not responding",
|
||||
action: "Run `opencode service restart` to recover it.",
|
||||
action: `[${restart}] Restart service`,
|
||||
}
|
||||
return { loading: true, message: "Waiting for background service..." }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ export const { use: useClient, provider: ClientProvider } = createSimpleContext(
|
|||
api: OpenCodeClient
|
||||
reconnect?: (onStatus: (status: Service.Status) => void, signal: AbortSignal) => Promise<{ api: OpenCodeClient }>
|
||||
// Stops and starts the managed service; present only in service mode.
|
||||
reload?: () => Promise<void>
|
||||
reload?: (signal?: AbortSignal) => Promise<void>
|
||||
}) => {
|
||||
const log = useLog({ component: "client" })
|
||||
const abort = new AbortController()
|
||||
|
|
|
|||
73
packages/tui/test/cli/tui/reconnecting-interaction.test.tsx
Normal file
73
packages/tui/test/cli/tui/reconnecting-interaction.test.tsx
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
/** @jsxImportSource @opentui/solid */
|
||||
import { testRender } from "@opentui/solid"
|
||||
import { expect, test } from "bun:test"
|
||||
import { ConfigProvider } from "../../../src/config"
|
||||
import { Reconnecting } from "../../../src/component/reconnecting"
|
||||
import { Keymap } from "../../../src/context/keymap"
|
||||
import { ThemeProvider } from "../../../src/context/theme"
|
||||
import { TestTuiContexts } from "../../fixture/tui-environment"
|
||||
import { createTuiResolvedConfig } from "../../fixture/tui-runtime"
|
||||
|
||||
function render(restart: (signal?: AbortSignal) => Promise<void>) {
|
||||
return testRender(() => (
|
||||
<TestTuiContexts>
|
||||
<ConfigProvider config={createTuiResolvedConfig()}>
|
||||
<Keymap.Provider>
|
||||
<ThemeProvider mode="dark" source={{ discover: () => Promise.resolve({}) }}>
|
||||
<Reconnecting status={{ type: "unresponsive" }} restart={restart} />
|
||||
</ThemeProvider>
|
||||
</Keymap.Provider>
|
||||
</ConfigProvider>
|
||||
</TestTuiContexts>
|
||||
))
|
||||
}
|
||||
|
||||
test("restarts an unresponsive service when r is pressed", async () => {
|
||||
let restarts = 0
|
||||
let complete!: () => void
|
||||
const restarting = new Promise<void>((resolve) => {
|
||||
complete = resolve
|
||||
})
|
||||
const app = await render(() => {
|
||||
restarts += 1
|
||||
return restarting
|
||||
})
|
||||
app.renderer.start()
|
||||
|
||||
try {
|
||||
await app.waitForFrame((frame) => frame.includes("[r] Restart service"))
|
||||
app.mockInput.pressKey("r")
|
||||
await app.waitForFrame((frame) => frame.includes("Restarting background service..."))
|
||||
app.mockInput.pressKey("r")
|
||||
expect(restarts).toBe(1)
|
||||
complete()
|
||||
await Bun.sleep(0)
|
||||
app.mockInput.pressKey("r")
|
||||
expect(restarts).toBe(1)
|
||||
} finally {
|
||||
complete()
|
||||
app.renderer.destroy()
|
||||
}
|
||||
})
|
||||
|
||||
test("cancels recovery when the reconnecting overlay unmounts", async () => {
|
||||
let aborted = false
|
||||
const app = await render(
|
||||
(signal) =>
|
||||
new Promise((_, reject) => {
|
||||
signal?.addEventListener("abort", () => {
|
||||
aborted = true
|
||||
reject(signal.reason)
|
||||
})
|
||||
}),
|
||||
)
|
||||
app.renderer.start()
|
||||
|
||||
await app.waitForFrame((frame) => frame.includes("[r] Restart service"))
|
||||
app.mockInput.pressKey("r")
|
||||
await app.waitForFrame((frame) => frame.includes("Restarting background service..."))
|
||||
app.renderer.destroy()
|
||||
await Bun.sleep(0)
|
||||
|
||||
expect(aborted).toBe(true)
|
||||
})
|
||||
|
|
@ -25,7 +25,7 @@ test("describes service status without transport diagnostics", () => {
|
|||
expect(reconnectingCopy({ type: "unresponsive" })).toEqual({
|
||||
loading: false,
|
||||
message: "Background service is not responding",
|
||||
action: "Run `opencode service restart` to recover it.",
|
||||
action: "[r] Restart service",
|
||||
})
|
||||
expect(JSON.stringify(reconnectingCopy())).not.toMatch(/Attempt|ECONNREFUSED|Event stream disconnected/)
|
||||
})
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue