# Update, stop, remove and withdraw

Four different things, each with one answer. Updating replaces the program and keeps the device. Stopping ends sharing until you start it again. Removing deletes the files from the machine. Withdrawing ends the device at Coronium for good. None of them does another's work: a removed agent is still an enrolled device, and a withdrawn device still has its files.

Calls go to https://supply.coronium.ai, signed in as in [enrolment](/guides/device-enrol.md), in the folder its step 1 made.

## Update

`/kit/index.json` speaks for the one release this server publishes now. The file names carry no version and a new build replaces the files in place. The agent never downloads or replaces itself. An installed agent keeps working: a relay refuses only a protocol version it no longer speaks, and the agent's log then says so. You learn of a new build by reading the list.

A release states:

- `version`: the version of the agent in every file.
- `files`: each file's `sha256`, `bytes`, `state`, `checked` and `limits`, as [the agent files](/guides/device-files.md) explains.
- `signature`: the signature over the list, or null. While it is null nothing is signed, and a digest is as trustworthy as the HTTPS connection it came over. [The signing contract](/kit/SIGNING.md) says what a signature covers.
- `agreement`: the `version` and `sha256` of the partner agreement the kit carries.

1. Compare what is published with what you installed.

```sh
umask 077
SUPPLY=${SUPPLY:-https://supply.coronium.ai}
cd "${CORONIUM_DIR:-$HOME/coronium}" || exit
curl -fsS "$SUPPLY/kit/index.json" -o coronium-kit.next.json
jq '{schema, version, signature, agreement}' coronium-kit.next.json
[ -e coronium-kit.json ] || cp coronium-kit.next.json coronium-kit.json
jq -r '.files[] | "\(.name) \(.sha256)"' coronium-kit.json | sort > coronium-installed.txt
jq -r '.files[] | "\(.name) \(.sha256)"' coronium-kit.next.json | sort | comm -13 coronium-installed.txt -
```

Expect: `"schema": 2`, the version, then one line for each file whose digest changed since `coronium-kit.json` was saved, which [the files section](/guides/device-files.md) does when you choose a file. No line: the file you run is the file published, and there is nothing to do. Safe to repeat: yes.

2. Decide. Take a changed file only when its `state` and each of its `limits` are ones the device's owner accepts. If `agreement` no longer names the text the account signed, read and sign the text in force first, as [enrolment](/guides/device-enrol.md) steps 2 and 3 do.

3. Replace the program the way it was installed. The device keeps its id and its secret: do not enrol again. Stop the old copy before the new one starts, because one device id takes one agent.

- The Linux kit: download and check the new kit as [its guide](/guides/device-linux-kit.md) does, then run its installer again in the new kit's folder, with a person at the terminal as the first time. It keeps the settings, replaces the program and, with systemd, starts the service again.
- A container: load the new image as [its guide](/guides/device-docker.md) does, stop and remove the old container, and run the new one with the same `agent.env`.
- The program alone: download and check the new file as [the files section](/guides/device-files.md) does, stop the running program, put the new file in its place, and start it as [its guide](/guides/device-program.md) does, with the same `agent.env`.

4. Check, then keep the new list as the installed one.

```sh
umask 077
SUPPLY=${SUPPLY:-https://supply.coronium.ai}
cd "${CORONIUM_DIR:-$HOME/coronium}" || exit
curl -sS -b coronium.cookies "$SUPPLY/app/api/devices" | jq -c 'if .code then {code, cause, fix} else .devices[] | {deviceId, status, state, reason} end'
mv coronium-kit.next.json coronium-kit.json
```

Expect: the log reads `connected to the relay` and the device reads `status` `online`, in the state it had. Any other line of the log is in [troubleshooting](/guides/device-troubleshoot.md). Safe to repeat: yes.

## Stop

Stopping the agent stops sharing, and the device keeps its place: start it and it serves again.

| How it runs | Stop | Start again |
|---|---|---|
| The Linux kit, with systemd | `sudo systemctl disable --now coronium-agent` | `sudo systemctl enable --now coronium-agent` |
| A container | `docker stop coronium-agent` | `docker start coronium-agent` |
| The program alone | end its process, with Ctrl-C or `kill` and its process id | start it again as [its guide](/guides/device-program.md) does |

After a stop the device reads `status` `offline` within a few reads, and its log names no cause, because nothing failed.

## Remove

Stop it first. Then, the way it was installed:

- The Linux kit: `sudo ./uninstall.sh` in the kit's folder removes the service, the program, its user and `/etc/coronium-agent` with the secret. It warns, with the process id, about an agent you started yourself, which it does not stop.
- The program alone: delete the program's file, `agent.env` and `coronium-device.json`.
- A container: the container, the two files that hold the secret, and the image:

```sh
umask 077
cd "${CORONIUM_DIR:-$HOME/coronium}" || exit
VERSION=$(cat coronium-version.txt)
docker rm -f coronium-agent
rm -f agent.env coronium-device.json
docker rmi "coronium-agent:$VERSION"
```

`agent.env` and `coronium-device.json` hold the device's secret. Once they are gone nobody can run this device again, and the device is still enrolled: withdraw it if it is not coming back.

## Withdraw

Withdrawal is for good, and only your person decides it: a withdrawn device never connects or enrols again, relays refuse it (a connection already open stays until it ends), and what it earned stays as recorded. The account withdraws its own device with one call, whose body is `{"confirm": true}`.

```sh
umask 077
SUPPLY=${SUPPLY:-https://supply.coronium.ai}
cd "${CORONIUM_DIR:-$HOME/coronium}" || exit
DEVICE=$(jq -r .deviceId coronium-device.json)
printf '{"confirm": true}' | curl -sS -b coronium.cookies -H 'Content-Type: application/json' -d @- "$SUPPLY/app/api/devices/$DEVICE/withdraw" | jq '{outcome, deviceId, state, reason, withdrawnAt, code, cause, fix}'
```

Expect: `state` `withdrawn` and `reason` `account`; a repeat answers `already_applied`. For another device, set `DEVICE` to its `deviceId` from `GET /app/api/devices`. If refused: `device_not_found` (the id is not a device of this account), `confirmation_required` (send `confirm` as true), `sign_in_required` (sign in again). Then stop and remove the agent as above. Safe to repeat: yes.

A device with no session withdraws itself with its own secret, the `CORONIUM_DEVICE_SECRET` line of `agent.env`, sent as a Bearer header on curl's standard input, never kept in a file of its own, and with no cookie. The reason stored is `user`.

```sh
umask 077
SUPPLY=${SUPPLY:-https://supply.coronium.ai}
cd "${CORONIUM_DIR:-$HOME/coronium}" || exit
id=$(sed -n 's/^CORONIUM_DEVICE=//p' agent.env)
sed -n 's/^CORONIUM_DEVICE_SECRET=/Authorization: Bearer /p' agent.env | curl -sS -H @- -H 'Content-Type: application/json' -d '{"confirm": true}' "$SUPPLY/app/api/devices/$id/self/withdraw" | jq '{outcome, deviceId, state, reason, withdrawnAt, code, cause, fix}'
```

Expect: `state` `withdrawn` and `reason` `user`. `GET /app/api/devices/{deviceId}/self` with the same header reads the device's own state without changing it. A wrong id or secret answers `device_not_found`, and many of them from one address answer `rate_limited` with `retryAt`.

When neither can be done, because the access code and the device's secret are both lost, Coronium withdraws the device on request:

<!-- facts:begin supply.contact -->
The one contact for device owners is support@coronium.ai: a question, more devices, or a withdrawal the account cannot make itself.
<!-- facts:end supply.contact -->

## Sign out

Signing out ends this session: the device keeps running and the account keeps its devices. Sign in again as [enrolment](/guides/device-enrol.md) does.

```sh
umask 077
SUPPLY=${SUPPLY:-https://supply.coronium.ai}
cd "${CORONIUM_DIR:-$HOME/coronium}" || exit
curl -sS -b coronium.cookies -c coronium.cookies -H 'Content-Type: application/json' -d '{}' "$SUPPLY/app/api/logout" | jq '{signedIn, code, cause, fix}'
```

Expect: `"signedIn": false`; a read after it answers `sign_in_required`. Safe to repeat: yes.

## Pause and private

`paused` and `private` are the account's own choices, set by a person in the farmer portal. The call behind them takes a browser's origin and fresh proof, so it is not a call for an agent. To stop sharing for a while, stop the agent.
