GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — feat: service restart approval when agents are running by agustif · Pull Request #55967 · anomalyco/opencode Conversation
|
|
The following comment was made by an LLM, it may be inaccurate: |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
The approach makes sense: one server-side request with first-wins resolution, shared by the CLI and the TUI. A few behaviours undercut the fix for the scenario in #43415, though. I ran the new CLI and TUI tests (all pass), type-checked server, cli and tui (clean), and called the new endpoints on a server started from this branch. I didn't run a full restart against a running agent.
Public API (all additions, backwards compatible): POST /api/server/restart/request and POST /api/server/restart/resolve (server.restart.request / server.restart.resolve in both generated clients); the events server.restart.requested and server.restart.resolved; the --force flag on opencode service restart. The CLI behaviour changes too: with active sessions, service restart now prompts, or waits up to 120s and then fails, where it used to restart straight away. Scripts that rely on the old behaviour need --force.
Concerns (details inline):
--force hint: both abort messages tell the caller to re-run with --force. In #43415 an agent ran the restart itself, and it will simply follow that hint.restart.request() has published the request, so a decision made in between is lost.packages/tui/src/context/location.tsx and location-error.test.tsx). Please rebase it out so this PR holds only the restart feature.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| catch: (cause) => | ||
| cause instanceof RestartApprovalTimeout | ||
| ? new Error( | ||
| "Timed out waiting for restart approval; no connected client approved the restart. Re-run with --force to restart immediately.", |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
This message (and the cancel one at line 122) tells the caller to re-run with --force. In #43415 the restart came from an agent's shell tool, and an agent that reads this will retry with --force and wipe the running sessions anyway. Consider leaving the hint out of the non-interactive path, or otherwise keeping --force out of an agent's easy reach. Otherwise the gate mostly protects against humans.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Agreed on both counts. Fixed in 80b3628:
--force hint removed from the non-interactive path: restartAborted and the timeout error now split by mode. Interactive keeps the --force hint (a human at a terminal asking for it); non-interactive reports "Restart cancelled with active session(s) still running." / "Timed out waiting for restart approval with sessions still running." with no bypass named. The CLI test asserts the timeout message does not contain --force.--force. It still can pass it if it already knows the flag, but the default path no longer hands it over. A stricter version (per-command policy denying service restart --force to agents) needs the permissions layer, and I'd rather not widen this PR's scope — happy to follow up separately if you want it.Covered by 11 CLI tests, all green.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| if (ids.length === 0) return "now" | ||
| if (deps.force) return "now" | ||
| yield* Effect.tryPromise({ | ||
| try: () => client.server.restart.request(), |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
In the non-TTY path, restart.request() publishes server.restart.requested before waitForResolvedEvent subscribes to the event stream. If a client resolves before the subscription is up, the CLI misses server.restart.resolved and waits out the full 120s before failing. Subscribe first and then request, or check the request's state after subscribing.
When an agent runs service restart from its shell tool, its own session is in session.active and is blocked waiting on this command. If someone picks "When idle", waitForIdle polls forever: that session can never go idle, and the loop has no timeout or limit. Either leave out the calling session, or give the idle wait a deadline.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Good catch on both. Fixed in 80b3628:
watchResolved) and the TUI command now open the event subscription before POSTing the intent, and only count server.restart.resolved events received after the request (armed). A decision that lands during the request window is buffered rather than missed. The CLI test "the event subscription connects before the intent request" asserts SSE connects before the request POST.waitForIdle now filters process.env.OPENCODE_SESSION_ID (set on shell tool invocations in packages/core/src/tool/plugin/shell.ts) out of the drain set. The test "the idle wait excludes the requesting session itself" pins a session that only ever reports its own ID and asserts the wait settles in one poll instead of spinning.Covered by 11 CLI tests, all green.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| pending: false, | ||
| }) | ||
| return Service.of({ | ||
| request: () => Ref.update(state, (current) => (current.pending ? current : { pending: true })), |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Nothing ever clears a pending request except a resolve. When the CLI times out, is interrupted or loses its connection, pending stays true and no server.restart.resolved event is sent, so every TUI's dialog stays open. A later "Restart now" there succeeds and broadcasts a decision nobody is waiting for. Also, once settled, resolve returns the previous last decision with a 200 instead of an error (curl: request → cancel → now returned cancel twice). Consider giving requests an expiry, or letting the requester cancel or release on exit, and returning an error when nothing is pending.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
All three points were real. Fixed in 80b3628:
cancel on timeout and on connection loss, so connected TUIs dismiss instead of waiting on a dead request. RestartIntent.resolve also reports whether the call settled the intent, and the handler only broadcasts server.restart.resolved when it did — a fresh settlement is the only thing that reaches the stream.last replayed with a 200: resolve now returns undefined when nothing is pending and nothing has settled, which the handler maps to InvalidRequestError (400). A settled intent replays its winner with fresh: false and no rebroadcast, so request → cancel → now yields cancel then cancel without a second stream event. Clients treat a 400 as "someone else won" and adopt the broadcast winner (CLI) or dismiss silently (TUI) rather than surfacing an error.New server coverage in packages/server/test/restart-intent.test.ts (3 tests): first-wins plus late replay, undefined when nothing is pending, and a second request() not clobbering the pending intent.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
- Release pending intents: CLI posts cancel on timeout and disconnect so connected TUIs never stay stuck on a dead request. - Resolve only broadcasts on a fresh settlement; late resolvers replay the winner without re-emitting, and nothing pending returns 400. - Subscribe to the resolved event before requesting an intent in both the CLI and the TUI, so a fast decision cannot be missed. - Exclude the requesting session from the idle wait; it blocks on this command and could never drain. - Keep the --force hint out of the non-interactive path so a blocked agent is not handed its own bypass.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the follow-up. I re-ran the CLI, server and TUI tests (all pass) and the type checks for server, cli and tui (clean). I still haven't tried a live restart with a running agent.
Resolved since my last review:
--force. Keeping agents from using --force at the permission level can be a separate PR.OPENCODE_SESSION_ID).cancel, so other TUIs' dialogs close.Still open: the request isn't released when the CLI is interrupted or killed (details inline). The branch also still carries 3f47f52 from #55940 (location.tsx and its test); please rebase it out.
Public API is the same as before (all additions, backwards compatible): the server.restart.request / server.restart.resolve endpoints and generated clients, the server.restart.requested / server.restart.resolved events, and service restart --force. Without --force, service restart now asks or waits when sessions are active.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| ? new Error("Timed out waiting for restart approval with sessions still running.") | ||
| : new Error("Lost connection to the server while waiting for restart approval", { cause }), | ||
| }).pipe( | ||
| Effect.tapError(() => |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
This releases the request on timeout or a lost connection. It doesn't run when the fiber is interrupted (Ctrl-C) or the process is killed, for example by the agent shell tool's own timeout, which is the agent-invoked case. The request then stays pending forever, the TUI dialogs stay open, and a later "Restart now" there broadcasts a decision nobody acts on. A server-side expiry on the request in RestartIntent (e.g. matching the 120s wait), plus a resolved/expired event so TUIs close the dialog, would cover every way the requester can go away.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
You're right — the release path only covers unwinding, not interruption or SIGKILL, which is exactly the agent-invoked case. Done in 8a5d47c:
Server owns release. Each server.restart.request now schedules a detached expiry (Effect.forkDetach, so it outlives the request scope) at a 2-minute TTL matching the CLI wait. When it fires and the intent is still pending, it clears it and publishes server.restart.expired. Generation tokens guard the race: expire(generation) is a no-op if a newer request has since been made or the intent already settled, so a stale timer can't free someone else's request.
Approvers dismiss on expiry. The TUI closes the dialog on server.restart.expired, so no dialog is left open for a decision nobody will act on.
One release path. I removed the CLI's own cancel-on-error rather than keeping two mechanisms; the CLI now treats expired as the terminal outcome and fails with the same timeout message. Its local wait is 150s against the server's 120s TTL, so the server always ends an unanswered wait and the local timer only covers a stream that goes silent.
Coverage:
packages/server/test/restart-intent.test.ts (6 tests) — expiry releases the current generation; a stale token leaves a newer request pending; expiring a settled intent is a no-op; plus the earlier first-wins/replay cases."an expired request aborts the wait" and the timeout test now asserts the CLI posts no cancel of its own."an expired request dismisses the dialog".All green: server 6, CLI 12, TUI 4; tsgo -b clean in all three packages; oxlint clean.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
A requester that is interrupted or SIGKILLed (for example by the agent shell tool's own timeout) never reaches its release path, so the pending intent lived forever and every approver dialog stayed open, with a later decision broadcast to nobody. The server now owns release: each request schedules a detached expiry that clears the intent and emits server.restart.expired, which approvers treat as a dismissal. Generation tokens keep a stale timer from expiring a newer request. The CLI no longer posts its own cancel; expiry is the single release path, and its local wait is longer than the server TTL so the server always ends an unanswered wait.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I re-ran the server, CLI and TUI tests (all pass) and the type checks for server, cli and tui (clean). I still haven't tried a live restart with a running agent.
Resolved: the last open point. A request nobody answers now expires on the server after 2 minutes, which also covers a CLI that is interrupted or killed. A leftover deadline can't clear a newer request, TUIs close the dialog on server.restart.expired, and the CLI's 150s wait leaves it to the server to end the wait. Everything from the earlier reviews is resolved, apart from one thing: the branch still includes 3f47f52 from #55940 (location.tsx and its test). Please rebase it out.
One minor point, not blocking: someone at a terminal who takes more than 2 minutes at the prompt gets a timeout error after making their choice. That's safe, but confusing.
Public API (all additions, backwards compatible): POST /api/server/restart/request and POST /api/server/restart/resolve (with generated clients); the events server.restart.requested, server.restart.resolved and the new server.restart.expired; the --force flag on opencode service restart. Without --force, service restart now asks or waits when sessions are active.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Automated PR Cleanup Thank you for contributing to opencode. Due to the high volume of PRs from users and AI agents, we periodically close older PRs using automated criteria so maintainers can focus review time on the most active and community-supported contributions. This PR was closed because it matched the following cleanup criteria:
PRs created within the last month are not affected by this cleanup. If you believe this PR was closed incorrectly, or if you are still actively working on it, please leave a comment explaining why it should be reopened. A maintainer can review and reopen it if appropriate. Thanks again for taking the time to contribute. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
tui: agent keeps running while user is trapped in false location-unavailable modal
There was an error while loading. Please reload this page.