# 5. Deployment and Production

## Current state in this repo

`deploy_rule()` hien tai **da ghi file that**, khong con chi log DB.

Khi deploy:

1. Lay rule co `status = approved`
2. Validate lai rule
3. Resolve target file tu `module_name`
4. Backup file hien tai
5. Update file JSON
6. Ghi row vao bang `deployments`
7. Mark rule la `deployed`

---

## Target file hien tai

Trong repo nay:

| module_name | Target file | Ghi chu |
|---|---|---|
| `fashion_rules.json` | `backend/worker/fashion_rules.json` | Da duoc support |

Nhung file sau **chua duoc support trong code hien tai**:

- `prompt_v2.txt`
- `stylist_guidelines.md`

---

## Tai sao khong append vao `rules[]`

`backend/worker/fashion_rules.json` trong repo nay khong phai mot file chi chua mang `rules[]`.

No la mot JSON config lon phuc vu worker/stylist engine. Neu ep append vao `rules[]` theo doc cu, rat de pha schema hien tai.

Vi vay Auto-Train dang append vao:

```json
{
  "auto_train_rules": [
    {
      "rule_id": "RUL-1000",
      "feedback_id": "FB-1000",
      "rule_type": "prompt_update",
      "rule": "...",
      "reasoning": "...",
      "confidence": 0.85,
      "deployed_at": "..."
    }
  ]
}
```

Day la cach it rui ro nhat de luu rule moi ma khong dong vao phan schema worker dang dung.

---

## Deploy flow

Pseudo-flow:

```python
async def deploy_rule(rule_id: str):
    # 1. approved rule only
    # 2. validate rule
    # 3. backup target file
    # 4. append rule vao auto_train_rules[]
    # 5. write file
    # 6. insert deployment log
    # 7. update generated_rules.status = deployed
```

Response hien tai:

```json
{
  "status": "deployed",
  "rule_id": "RUL-1001",
  "module": "fashion_rules.json",
  "deployed_at": "2026-05-08T09:39:55",
  "target_file": "D:/.../backend/worker/fashion_rules.json",
  "backup_file": "D:/.../backend/worker/fashion_rules.20260508_093955.backup.json"
}
```

---

## Backup behavior

Moi lan deploy:

- file target duoc copy thanh 1 file backup
- ten file backup co timestamp
- backup duoc tao truoc khi write

Mau ten file:

```text
fashion_rules.20260508_093955.backup.json
```

Luu y:

- du lieu backup hien tai chi nam tren file system
- bang `deployments` chua luu `backup_path`
- rollback endpoint chua co

---

## Hot reload

Auto-Train hien tai **khong** tu reload chatbot config.

Trang thai dung:

- file rules da duoc update
- viec chatbot co doc lai file ngay hay khong phu thuoc vao subsystem khac

Cac huong nang cap hop ly:

1. Polling: chatbot doc lai file theo chu ky
2. Signal-based reload
3. Redis pub/sub cho config update

Nhung cac co che nay chua thuoc implementation hien tai cua Auto-Train route.

---

## Rollback strategy

Hien tai:

- co backup file
- chua co endpoint rollback
- rollback chi lam thu cong

Rollback thu cong:

1. Tim backup file vua tao
2. Copy backup de ghi de lai file goc
3. Tu cap nhat trang thai / ghi log neu can

Route de lam sau:

```text
POST /autotrain/rollback/{rule_id}
```

---

## Safety checks truoc deploy

Nhung check dang co:

- rule phai `approved`
- rule phai pass validation
- toxic-word filter co chay
- backup file duoc tao truoc khi write

Nhung check chua co:

- conflict detection giua rule moi va rule da deploy
- confidence threshold policy nhu `> 0.7`
- staging gate
- notify team

---

## Production roadmap hop ly

Thu tu nen lam:

1. Luu `backup_path` vao bang `deployments`
2. Them rollback endpoint
3. Support them file text/markdown neu can
4. Them metrics deployment
5. Them hot reload

---

## File structure thuc te

```text
backend/
  api/
    autotrain/
      autotrain_route.py
  database/
    autotrain.db
  worker/
    fashion_rules.json
    fashion_rules.<timestamp>.backup.json
```

---

## Next steps

- [x] Real file deployment vao `fashion_rules.json`
- [x] Backup file truoc khi deploy
- [ ] Rollback endpoint
- [ ] Persist `backup_path` vao DB
- [ ] Hot-reload mechanism
- [ ] Deployment metrics
- [ ] Alerting / auto rollback
