Skip to content

feat(daemon): qq daemon run --detach, status, stop and restart - #440

Merged
lg2m merged 3 commits into
refactor/qq-host-compositionfrom
feat/daemon-lifecycle
Oct 25, 2026
Merged

lg2m merged 3 commits into
refactor/qq-host-compositionfrom
feat/daemon-lifecycle

Conversation

@lg2m

@lg2m lg2m commented Oct 23, 2026

Copy link
Copy Markdown
Collaborator

Stacked on #431. This is the first half of P1 task 2 (daemon lifecycle). Bare qq is unchanged: it still embeds its server. Switching it to start or attach to the daemon comes in the next stacked PR, after the trust route lands.

What changed for users

  • qq daemon run [--bind] [--allow-origin]: the same server as qq serve, under the daemon name.
  • qq daemon run --detach: starts this binary as a background process and returns once the daemon serves. If a daemon is already running, it reports that one instead. The background process:
    • is detached from the terminal and its process group;
    • runs from / rather than the directory you started it in;
    • writes to daemon.log in the server state directory.
  • qq daemon status: prints the address, pid, version, protocol, server id and log path. Exits 1 when no daemon is running.
  • qq daemon stop: asks the daemon to stop, then waits until it has closed the session store, not just until the listener is gone. When it returns, qq run can open the store. It refuses to stop a server embedded in an interactive qq and tells you to quit that qq instead.
  • qq daemon restart: stop, then a detached start of the current binary. The store identity (server_id) is kept.
  • qq serve and qq daemon run now stop on SIGTERM, not only SIGINT or Ctrl-C, and settle runs the same way in every case. Before this, SIGTERM killed qq serve without settling.
  • Approval timeout: qq serve and the daemon read approval_timeout_seconds from global and managed configuration only, not from the directory they were started in.
  • Read-image approvals (--approve-self-contained-read-image) are accepted by daemon run and restart, and passed on to the detached process.

How it works

  • New route POST /v1/daemon/shutdown:
    • It requires the bearer token. The server answers 202 with its ServerInfo, then the host stops.
    • Only hosts that opt in with ServerOptions::with_stop_route serve it. A TUI's embedded server answers 404.
    • The route is additive, so PROTOCOL_VERSION is unchanged, and older servers answer 404 too.
  • Client side: SessionClient::request_stop calls the route.
  • Readiness: a detached start waits for the existing discovery check, with metadata published only after the store opens, plus a health probe. It also checks that the pid is the child it started, so a lost start race reports the existing daemon.
  • Host lock: a new host.lock is held from before the runtime opens until after it closes.
    • wait_until_released lets stop return only once the store is closed.
    • acquire_host_lock lets a new host wait for a previous one that is still closing, instead of failing on a busy store.
    • This covers the window where the listener has stopped but the runtime is still settling.
  • qq serve and qq daemon run share a single host loop in the new src/daemon.rs.

Verification

  • cargo xtask check passes against refactor(host): move runtime composition from the binary into qq-host #431.
  • Tests:
    • tests/daemon_lifecycle.rs drives the real binary in an isolated home:
      • detached start, a second start that attaches, status, stop (asserting the store lock is released), and stop when nothing is running;
      • restart replacing the pid while keeping the server_id;
      • SIGTERM on a foreground daemon;
      • stop refusing an embedded server.
    • qq-server unit tests cover:
      • the route's opt-in, authentication, and that it signals without stopping the listener;
      • the host lock outliving the listener.
    • The qq-client test checks request_stop decoding and the 404 from an embedded server.
    • The test for passing arguments to the detached process round-trips read-image approvals through clap.
  • cargo xtask smoke: 11/11, including serve-sse.
  • The qq-client wasm32 build compiles.

Known gaps (next PR)

  • Bare qq still embeds its server, and quitting it still cancels its runs.
  • Moving it onto ensure_daemon needs the TUI's "trust for this session" answer to reach the daemon, so that comes with the trust route.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ⚠️ Failed 2026-10-25T04:35:27.870225Z
We couldn’t complete this review. Comment @codex review to try again.
3dc777d New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ffe3d22df4

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/daemon.rs Resolved
Comment thread src/daemon.rs Resolved
@lg2m
lg2m added this pull request to stack #433 October 23, 2026 17:11
@lg2m lg2m closed this Oct 23, 2026
@lg2m lg2m reopened this Oct 23, 2026
@lg2m
lg2m force-pushed the feat/daemon-lifecycle branch from acf891c to 8109667 Compare October 24, 2026 00:57
@lg2m
lg2m force-pushed the feat/daemon-lifecycle branch from 8109667 to be9b934 Compare October 24, 2026 05:08

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: be9b934550

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/daemon.rs Resolved
lg2m added 3 commits October 24, 2026 21:07
…losing

A second foreground host waited on the running host's lock and failed; it now reports the running server. qq daemon stop returned while a host that had stopped listening still held the store; it now waits for the host lock.
@lg2m
lg2m force-pushed the feat/daemon-lifecycle branch from be9b934 to 3dc777d Compare October 25, 2026 04:35
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Repo admins can enable using credits for code reviews in their settings.

1 similar comment
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Repo admins can enable using credits for code reviews in their settings.

@lg2m
lg2m merged commit 77cd903 into main Oct 25, 2026
15 of 18 checks passed
@lg2m
lg2m deleted the feat/daemon-lifecycle branch October 25, 2026 04:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant