# Investibook Scripts

## sync-autox-skill.sh

同步 AutoX SQL tuning skill 到 Investibook 自有 skill 目录。

### 功能

1. 从 `AilinKid/AutoX` 拉取指定 ref，默认 `main`
2. 将 `autox-optimize-sql` 落到 `agentkit/skills/tidb-query-tuning-autox`
3. 保留 upstream `LICENSE`、README、spec 和 helper skill 文档
4. 自动加入 Investibook adapter boundary，限制该 skill 只消费已有 diagnosis evidence
5. 写入 `UPSTREAM.json`，记录 upstream repo/ref/commit 和本地适配说明

### 使用方法

```bash
# 从项目根目录运行
make sync-autox-skill

# 同步指定 ref
AUTOX_REF=main ./scripts/sync-autox-skill.sh
AUTOX_REF=<commit-sha> ./scripts/sync-autox-skill.sh
```

### 注意事项

- AutoX 不放在 `agentkit/skills/nutshell-skills` 下；它是 Investibook diagnosis specialist。
- 同步脚本会重建 `agentkit/skills/tidb-query-tuning-autox` 目录。
- `diagnosis-analyst` 中只使用 AutoX 的 SQL 深诊断逻辑，不运行 AutoX collection、fleet discovery 或生产变更命令。
- helper skills 会以 reference 形式落在 `references/helpers/`，不会作为额外 top-level skills 自动暴露。

## sync-pod-credentials.sh

同步本地凭据到 EKS 运行中的 Investibook backend pod。

### 功能

1. **自动发现 Pod**: 自动查找 `investibook` namespace 中的 backend pod
2. **同步 Clinic 凭据**: 将本地的 `opencode.json` 同步到 pod 的 template 目录
3. **同步 Grafana 认证**: 将本地的 `grafana_storage_state.json` 同步到 diagnostic 服务

### 使用方法

```bash
# 从项目根目录运行
./platform/investibook/scripts/sync-pod-credentials.sh
```

### 前置条件

1. `kubectl` 已配置并可以访问 EKS 集群
2. 有 `investibook` namespace 的访问权限
3. backend pod 正在运行
4. 本地文件存在:
   - `platform/investibook/backend/templates/example/.opencode/opencode.json`
   - `aiops/service/diagnostic/config/grafana_storage_state.json`

### 注意事项

⚠️ **这些更改是临时的!**

- 同步的凭据只在当前 pod 中生效
- 如果 pod 重启或重新部署,更改会丢失
- 需要在每次 pod 重启后重新运行脚本

### 永久性解决方案

如果需要永久性更改,应该:

1. 更新本地的配置文件
2. 提交到 git 仓库
3. 重新构建 Docker 镜像
4. 重新部署到 EKS

### 示例输出

```
==========================================
Sync Credentials to Investibook Pod
==========================================

🔍 Finding backend pod...
✅ Found pod: backend-565dc5b887-bb6pb

📋 Pod status: Running

📝 Step 1: Syncing template opencode.json...
   ✅ Template opencode.json synced successfully

📝 Step 2: Syncing grafana_storage_state.json...
   ✅ Grafana storage state synced successfully

==========================================
✅ All credentials synced successfully!
==========================================
```

### 故障排查

**错误: No backend pod found**
```bash
# 检查 pod 是否运行
kubectl get pods -n investibook

# 检查 namespace 是否正确
kubectl get namespaces | grep investibook
```

**错误: Pod is not running**
```bash
# 查看 pod 状态
kubectl describe pod -n investibook <pod-name>

# 查看 pod 日志
kubectl logs -n investibook <pod-name>
```

**错误: Local file not found**
```bash
# 确保从项目根目录运行脚本
cd /path/to/aiops
./platform/investibook/scripts/sync-pod-credentials.sh
```
