Introduction¶
Executing systemctl --user restart dsh-server directly in a DSH session, or triggering an equivalent effect via a tool call, often terminates the host process while the agent is still outputting answers to the user: the user may not receive a complete reply, and the session may appear abnormally interrupted. If you want to bring the host back up after “changing configuration, upgrading version, or changing the exit method” but do not want to interrupt the currently running agent and subagents, you need to register the restart as an observable, cancellable request and end the process only after it is truly idle.
Below is an introduction to the community plugin dsh-graceful-restart (maintainer unera, categorized as Workflow): registering restart intent from within the session, polling all active agents, recording the reason and exiting after a quiet window; sending a summary to the relevant sessions upon the next startup. See source code at GitHub, and directory at SkillHub.
What is it¶
dsh-graceful-restart is a Cordis plugin for DSH hosts (npm package name @dr-co/dsh-graceful-restart; version 1.0.0 in current docs is a profile example constraint, package metadata is 1.7.3). The core problem it solves is: initiating a host restart from within the session without truncating ongoing answers; waiting for all agents (including subagents) to finish before restarting, optionally waiting for background jobs; writing the cause of each request to event logs and status files; and upon restart completion, waking up and briefly explaining the reason to sessions where need_notify is checked.
Compared to “immediate systemctl restart”, this is a workflow of registration — idle detection — single exit — recovery notification, rather than synchronously blocking until the process is dead.
Core Features¶
Restart Scheduling (Five-Step Method)¶
The behavior described can be summarized as:
- Call
dsh_restartto register the request and return immediately; the agent is told not to wait for the restart to complete. - The watcher polls all surviving agents (including subagents) every second; as long as there is a
runningstatus, exit is not triggered. - After everyone is idle, start a quiet window (config option
quietSeconds, default 10 seconds), checked every second; if a new session appears or work starts again, the timer restarts from zero. - After the quiet window ends, write the cause provided by each agent to the event log and status file, then end the host according to the configured
exitMethod. - On the next startup, read the state, record the cause again, and send a brief explanation to sessions with
need_notify: true, facilitating the continuation of the interrupted work context.
Global Tools¶
Main sessions and each subagent use the same set of tool descriptions and contracts.
dsh_restart¶
| Parameter | Type | Meaning |
|---|---|---|
cause |
string, required | A one-line explanation of “what was changed”; will be recorded in logs, notifications, and startup reports |
need_notify |
boolean | Whether to wake this session after restart and bring the cause along |
Example of return on success (excerpt):
{
"status": "ok",
"message": "The restart is scheduled and will run as soon as the host is idle. Do not wait for it: waiting only lengthens the delay and cannot change the result. Cancel it with dsh_restart_cancel(id) if it is no longer needed.",
"id": "1765432100123",
"pending": 1
}
Multiple calls will register multiple requests (each with an id); the host will restart at most once, but each request needs to be cancelled separately with dsh_restart_cancel.
dsh_restart_cancel¶
| Parameter | Type | Meaning |
|---|---|---|
id |
string, required | The id returned by dsh_restart |
Only when all pending requests are cancelled will the entire restart be cancelled; cancelling one id while others are still pending will not stop the host from restarting as planned.
Configuration¶
Edit in the DSH interface Plugins → dsh-graceful-restart; fields match the config file (the volatile() field is persisted by settings). The interface language supports English, Russian, and Chinese.
| Field | Default | Meaning |
|---|---|---|
exitMethod |
systemctl |
systemctl: systemctl --user restart <serviceUnit>; command: executes exitCommand; kill: sends SIGTERM to this process; exit: process.exit(0) |
serviceUnit |
dsh-server |
The unit name used for systemd restart |
exitCommand |
— | The shell command executed when exitMethod is command (separately launched from the dying process) |
quietSeconds |
10 |
Seconds that must remain idle after the last activity, checked every second |
waitForJobs |
false |
When true, restart is postponed while background jobs (run_in_background, job_*) are running or stopping; default false to avoid a dev server that never ends dragging the restart into eternity |
notifyJobOwners |
true |
If a restart kills a background job, sessions are notified upon startup; this option is greyed out when waitForJobs is true |
reasonFile |
— | Optional: writes the cause to a line file before restart, for the unit’s ExecStartPost startup report to read |
language |
en |
Tool copy and explanation after restart: en, ru, zh |
stateDir |
$DSH_HOME/graceful-restart |
Directory for requests, event logs, and pending notification states |
When the host provides webServer, the settings card displays process uptime, and GET /api/graceful-restart/status (pending count, quiet window progress, busy session count, etc.; does not expose cause and session IDs).
Background Jobs and Persistent Files¶
When notifyJobOwners is enabled by default, a snapshot of running jobs is taken before exit and written to pending.json (including jobLoss). On the next startup, each session will have at most one explanation: it can include the cause and all interrupted jobs under that session along with their runtimes.
| Path | Content |
|---|---|
<stateDir>/events.log |
Requests, cancellations, window events, causes, exit decisions, line by line |
<stateDir>/pending.json |
The just-occurred restart request and list of jobs killed, until the session is woken |
Installation and Enablement¶
Official installation command (from README):
dsh plugin add @dr-co/dsh-graceful-restart
Or fix the bundle in ~/.dsh/profiles/web/package.json of the profile:
{
"dependencies": { "@dr-co/dsh-graceful-restart": "^1.0.0" },
"dsh": { "profile": { "bundles": ["@dr-co/dsh-graceful-restart"] } }
}
The package declares Node >= 22 and license MIT; it runs as a DSH plugin under the main process permissions. Before installation, it is advisable to review the repository source code and license yourself.
Typical Usage¶
Registering a restart in the agent¶
When the agent confirms that the host needs to be restarted (e.g., configuration has been written, dependencies updated), it calls dsh_restart, fills in an auditable cause, and sets need_notify as needed:
- Example semantics for
cause:dsh-im updated to 5.10.0(appears in thepending.jsonexample in the README). - After receiving
status: ok, continue the current task or end the turn; do not poll and wait for the restart; waiting only prolongs the occupied time before the idle detection.
If the plan is cancelled, call dsh_restart_cancel with the returned id; you must cancel every pending id to cancel the restart as a whole.
Coordinating with systemd user services¶
The default exitMethod is systemctl and serviceUnit is dsh-server, suitable for deployments where the “user-level unit starts itself up”. If you want to print the reason in journal or custom startup scripts, you can set reasonFile to point to the path read by the unit report (README example: /home/dsh/.cache/dsh-restart-reason.txt).
Job strategy on development machines¶
Long-running run_in_background tasks (e.g., dev server) will not block the restart by default (waitForJobs: false). If you must wait for the job to end before restarting, explicitly enable waitForJobs, and accept the risk that the restart might be postponed indefinitely; at this point, there is usually no need for notifyJobOwners to explain the job loss.
Use Cases and Notes¶
Suitable for: triggering a rolling restart of the DSH host from within the session/toolchain; avoiding truncation of output when multiple agents and subagents run in parallel; needing cause auditing and automatically waking up specific sessions after restart; needing to clearly inform sessions when the restart interrupts background jobs.
Notes:
- The plugin executes
systemctl, custom commands, orkillwith the current DSH process permissions; production environments should restrict who can calldsh_restart. - Multiple pending requests share “at most one exit”; the collaborative cancellation logic needs to be consistent with team agreements.
- SkillHub and the GitHub repository are maintained by the community and have no official affiliation with DeepSeek/QuantCube; in the DSH ecosystem, “everything is a plugin” is emphasized, and this plugin belongs to the Workflow category of capability extensions.
Conclusion¶
dsh-graceful-restart turns “restarting the host from a session” into a recordable, cancellable, and idle-aware workflow: wait for agents and optional jobs first, then exit through a quiet window, and send the cause and job interruption explanation upon the next startup. For more details on configuration and APIs, please refer to the SkillHub directory page and the GitHub README.