# Staging, shutdown, and rollback

Use this when moving the Node agent server. MySQL holds the chats. Redis holds only the live reply. Qdrant holds chat-history vectors and can be filled again from MySQL. The counts on `/api/agent/metrics` live in the process and reset on restart.

## Staging

Use the same build as production with staging values in the environment.

- `QDRANT_URL` is the private Qdrant address. `QDRANT_API_KEY` stays empty when that server has no key.
- `QDRANT_COLLECTION` stays `ai_manger_chat_histroy`.
- `RATE_LIMIT_ALLOW_IPS` lists office and probe addresses, comma-separated. Those addresses skip the per-IP cap. The per-user cap still applies.

Start the process, then open `GET /api/agent/health`. Ship traffic only when `mysql`, `redis`, and `qdrant` are all `ok`.

Send one message. The process log must contain a line:

```text
Reply trace runId=... userId=... agentId=... status=ok durationMs=...
```

`GET /api/agent/metrics` then shows `agent_replies_total` of at least 1.

## Shutdown

Stop the Node process. Locally that is Ctrl+C in the `npm run dev` terminal. In production, stop it with the same process manager that started it.

The process does not finish an open reply before it exits. On the next start, every `agentic_runs` row still marked `running` is set to `failed` with the error `The reply was interrupted.` The Redis lock for that reply expires in about 30 seconds. The saved messages stay in MySQL. That reply cannot be resumed. The in-memory metrics start again at zero.

## Rollback

Put the previous release on the host and start it with the same environment file.

Leave MySQL as it is. A code rollback does not undo a SQL change that was already applied. Apply schema changes by hand, and only when the new release is the one you intend to keep.

Do not restore Redis as part of a code rollback. An empty Redis is safe. Existing chats still load from MySQL. A reply that was in progress is gone.

Check `GET /api/agent/health` after the old release is up. Then send one message and confirm a `Reply trace` line.

## Restore check

Run this against a spare database, not the live one.

Dump these tables from the agent database: `agentic_agent_types`, `agentic_agents`, `agentic_agent_messages`, `agentic_runs`, `agentic_tool_events`. Load that dump into an empty database. Point a spare process at it with `MYSQL_DATABASE` set to that database. Open a known agent and confirm its messages are there.

Flushing Redis does not delete those messages. Reload the same agent. The history is still there. A reply that was running is no longer attachable.
