# RdkTube Agent Auto Update

V5 includes an external updater. The agent checks a manifest on startup, launches the updater if a valid release exists, and exits so files can be replaced safely.

## Files

- `_update_client.py`: checks the manifest and launches the updater.
- `updater.py`: external updater process.
- `version.json`: local version metadata.
- `logs/updater.log`: updater log under `%USERPROFILE%\.rdk\logs`.

## Why A Separate Updater

Windows locks a running `.exe`. The agent should not overwrite itself. The updater runs as a separate process, waits for the agent PID to exit, replaces files, and restarts the agent.

## Config

Auto update is inactive until `manifest_url` is configured:

```ini
[Update]
enabled = yes
check_on_start = yes
manifest_url = https://www.rdktube.com/downloads/rdk-agent-manifest.json
channel = stable
allow_during_live = no
```

## Manifest

```json
{
  "version": "5.0.4",
  "channel": "stable",
  "url": "https://www.rdktube.com/downloads/Rdk-Agent_5.0.4.zip",
  "sha256": "<ZIP_SHA256>",
  "mandatory": false,
  "rollout_percent": 100,
  "min_supported_version": "5.0.0",
  "notes": "Stability fixes"
}
```

Required fields:

- `version`
- `url`
- `sha256`

Optional fields:

- `channel`: `stable`, `beta`, or `*`
- `mandatory`: ignores rollout percentage
- `rollout_percent`: 0-100 deterministic rollout based on `agent_id`
- `min_supported_version`: blocks silent update from very old versions
- `notes`

## Release Zip

Recommended zip content:

```text
Rdk-Agent.exe
Rdk-Agent-Updater.exe
version.json
_internal/
```

Do not include user state in the release zip:

```text
config.ini
logs/
state.json
.rdk_profiles.json
```

The current implementation also skips those names during copy as a safety measure.

## Build Flow

1. Build `Rdk-Agent.exe`.
2. Build `Rdk-Agent-Updater.exe` from `updater.py`.
3. Zip release files.
4. Compute SHA256:

```powershell
Get-FileHash .\Rdk-Agent_5.0.1.zip -Algorithm SHA256
```

5. Upload zip.
6. Update manifest.

## Runtime Flow

1. Agent starts.
2. Agent reads manifest.
3. If `version` is newer and rollout allows this `agent_id`, it launches updater.
4. Agent exits.
5. Updater waits for the old process.
6. Updater downloads release zip.
7. Updater validates SHA256.
8. Updater replaces app files.
9. Updater restarts the agent.

## Notes

- Updates are checked on startup in this first implementation.
- Do not enable mandatory updates during active live sessions unless a critical fix is required.
- For V4 agents that do not include updater support, ship a bridge installer or bridge V4 build once.
