Skip to content

支持自动故障转移的原子化非交互恢复 - #401

Draft
wplct wants to merge 2 commits into
SaladDay:mainfrom
wplct:pr/fix-failover-restart
Draft

wplct wants to merge 2 commits into
SaladDay:mainfrom
wplct:pr/fix-failover-restart

Conversation

@wplct

@wplct wplct commented Aug 7, 2026

Copy link
Copy Markdown

依赖 / Depends on: #400 修复自动故障转移中的供应商凭证串号。该依赖合并后,本分支会 rebase 到最新 main 再提交上游。

问题

非交互环境中没有安全的单命令方式同时启动 proxy worker 和自动故障转移。若脚本先执行 proxy enable、再执行 failover enable,worker 第一次接管发生在 auto_failover_enabled 落库之前,存在按手动模式处理 live 配置的中间窗口。

同时,failover enable 在需要 bootstrap proxy 时会显示交互确认,无法直接用于 systemd ExecStartPost 或自动化脚本。

修复

  • failover enable 增加 --yes,仅跳过 proxy bootstrap 确认;默认交互行为保持不变。
  • 组合启用在启动 daemon worker 前暂存 (enabled=true, auto_failover_enabled=true),使 worker 第一次 takeover 就按自动故障转移语义运行。
  • staging 写入失败或 worker 启动失败时,统一回滚 live backup、failover snapshots、current provider 和 routing flags。
  • EnsureWorker 返回错误后先对账 daemon worker 与 live takeover 状态:
    • worker 和 live takeover 均已生效时,视为响应丢失但操作成功;
    • 只有 worker 存活但 takeover 未完成时,才发送幂等 DropTakeover 补偿;
    • 无 worker 时直接执行本地 rollback。
  • 补偿清理失败和状态回滚失败会分别聚合到错误信息中。

测试

  • failover enable 默认 yes=false
  • failover enable --yes 正确解析为非交互确认。
  • worker 启动前 proxy/auto-failover flags 已同时落库。
  • managed session 启动失败后恢复 live、backup、snapshot、current provider 和 flags。

部署说明

本 PR 不提交任何 systemd unit。部署方可以在 binary 更新后使用:

ExecStartPost=/path/to/cc-switch --app codex failover enable --yes

Depends on the provider credential isolation PR. After that dependency is merged, this branch will be rebased onto the latest main before submitting it upstream.

Problem

There is no safe single-command path for starting both the proxy worker and automatic failover in a non-interactive environment. Running proxy enable followed by failover enable creates an intermediate window where the worker performs its first takeover before auto_failover_enabled is persisted, so the live configuration may be handled with manual-takeover semantics.

In addition, failover enable prompts for confirmation when proxy bootstrap is required, preventing direct use from systemd ExecStartPost and automation scripts.

Fix

  • Add failover enable --yes, which only skips the proxy-bootstrap confirmation. Default interactive behavior remains unchanged.
  • Stage (enabled=true, auto_failover_enabled=true) before starting the daemon worker, so its first takeover uses automatic-failover semantics.
  • Use one rollback path for staging failures and worker-start failures, restoring the live backup, failover snapshots, current provider, and routing flags.
  • Reconcile daemon state after an EnsureWorker error:
    • if both the managed worker and live takeover are active, treat it as a lost response after successful completion;
    • send idempotent DropTakeover compensation only when a worker exists but takeover did not complete;
    • perform local rollback directly when no managed worker exists.
  • Aggregate worker-cleanup and state-rollback failures independently in the returned error.

Tests

  • failover enable defaults to yes=false.
  • failover enable --yes parses as non-interactive confirmation.
  • Proxy and automatic-failover flags are persisted before worker startup.
  • A managed-session startup failure restores live config, backups, snapshots, current provider, and flags.

Deployment note

This PR does not add or modify a systemd unit. Deployments may use the following after updating the binary:

ExecStartPost=/path/to/cc-switch --app codex failover enable --yes

social4hyq pushed a commit to social4hyq/homebrew-core that referenced this pull request Sep 20, 2026
cc-switch-cli 5.10.2

Created-by: HarmonybrewBot
Commit-by: HarmonybrewBot
Merged-by: HarmonybrewBot
Description: Created by `brew bump`

---

Created with `brew bump-formula-pr`.<details>
  <summary>release notes</summary>
  <pre># CC Switch CLI v5.10.2

v5.10.2 is a patch release for skill updates, provider configuration persistence, and upstream-compatible database, pricing, backup, and cloud-sync behavior.

## Highlights

- Manually check and apply updates for repository-backed skills from the CLI or TUI. Use `cc-switch skills check-updates`, `cc-switch skills update <name>`, or `cc-switch skills update --all`.
- Provider quick settings and common-config membership now persist correctly after TUI edits, including inherited Claude and Codex values.
- The database now matches upstream schema v17 and keeps a persistent session-usage deduplication ledger.
- SQL backup/restore and WebDAV/S3 synchronization follow the upstream recovery and locking behavior more closely.
- The built-in price catalog and model aliases are refreshed from upstream CC-Switch v3.20.
- OpenModel is available as a named preset for Claude, Codex, Gemini, OpenCode, Hermes, and OpenClaw.

## Upgrade

The database migrates from schema v16 to v17 after creating the normal pre-migration backup. The migration adds the usage-deduplication ledger without rebuilding or deleting existing usage logs.

If multiple installations share snapshots through WebDAV or S3, upgrade all of them before syncing a v17 snapshot. Older versions intentionally reject newer schemas.

Built-in updater:

```bash
cc-switch update
```

Homebrew:

```bash
brew upgrade cc-switch-cli
```

macOS/Linux installer:

```bash
curl -fsSL https://github.com/SaladDay/cc-switch-cli/releases/latest/download/install.sh | bash
```

## Thank you

Thank you to everyone who submitted an issue, shared diagnostics, followed up in a discussion, proposed a change, or opened a pull request during this cycle:

- Issues and follow-up: [@DoubleLT](https://github.com/DoubleLT) ([#415](SaladDay/cc-switch-cli#415)), [@ShatterDusk](https://github.com/ShatterDusk) ([#413](SaladDay/cc-switch-cli#413)), [@PawnMa](https://github.com/PawnMa) ([#410](SaladDay/cc-switch-cli#410)), [@evelyn-jialin-zhang](https://github.com/evelyn-jialin-zhang) ([#408](SaladDay/cc-switch-cli#408)), [@MartinaBarton6](https://github.com/MartinaBarton6) ([#407](SaladDay/cc-switch-cli#407)), [@Wizard-one](https://github.com/Wizard-one) ([#406](SaladDay/cc-switch-cli#406)), [@Kurayuri](https://github.com/Kurayuri) ([#405](SaladDay/cc-switch-cli#405)), [@coderyangyangyang](https://github.com/coderyangyangyang) ([#404](SaladDay/cc-switch-cli#404)), [@CodeCatMeow](https://github.com/CodeCatMeow) ([#403](SaladDay/cc-switch-cli#403)), [@TaichiLi](https://github.com/TaichiLi) ([#402](SaladDay/cc-switch-cli#402)), [@maxwell-feng](https://github.com/maxwell-feng) ([#370](SaladDay/cc-switch-cli#370)), [@Jerrrry666](https://github.com/Jerrrry666) ([#278](SaladDay/cc-switch-cli#278)), and [@AkaChou](https://github.com/AkaChou) ([#89](SaladDay/cc-switch-cli#89)).
- Pull requests and proposals: [@ShatterDusk](https://github.com/ShatterDusk) ([#414](SaladDay/cc-switch-cli#414)), [@XiaoHuo888-hue](https://github.com/XiaoHuo888-hue) ([#412](SaladDay/cc-switch-cli#412)), [@brushax](https://github.com/brushax) ([#411](SaladDay/cc-switch-cli#411)), [@ChanthMiao](https://github.com/ChanthMiao) ([#409](SaladDay/cc-switch-cli#409)), [@wplct](https://github.com/wplct) ([#399](SaladDay/cc-switch-cli#399), [#400](SaladDay/cc-switch-cli#400), [#401](SaladDay/cc-switch-cli#401)), [@louisneal](https://github.com/louisneal) ([#297](SaladDay/cc-switch-cli#297)), [@zhangyang-crazy-one](https://github.com/zhangyang-crazy-one) ([#277](SaladDay/cc-switch-cli#277)), [@tangaac](https://github.com/tangaac) ([#105](SaladDay/cc-switch-cli#105)), and [@hitsmaxft](https://github.com/hitsmaxft) ([#94](SaladDay/cc-switch-cli#94)).
- Discussion participation: [@ToDayL](https://github.com/ToDayL) ([#297](SaladDay/cc-switch-cli#297)).

We also thank [@farion1231](https://github.com/farion1231) and every upstream CC-Switch contributor, especially [@Komikawayi](https://github.com/Komikawayi), [@mhy1227](https://github.com/mhy1227), [@zayokami](https://github.com/zayokami), and [@YUZHEthefool](https://github.com/YUZHEthefool), whose database, pricing, backup, and sync work was adapted here.

Some acknowledged reports and proposals remain open or were not merged; inclusion here is a thank-you, not a change in their status.

See the [full changelog](https://github.com/SaladDay/cc-switch-cli/blob/v5.10.2/CHANGELOG.md) and [all changes since v5.10.1](SaladDay/cc-switch-cli@v5.10.1...v5.10.2).
</pre>
  <p>View the full release notes at <a href="https://github.com/SaladDay/cc-switch-cli/releases/tag/v5.10.2">https://github.com/SaladDay/cc-switch-cli/releases/tag/v5.10.2</a>.</p>
</details>
<hr>

See merge request: Harmonybrew/homebrew-core!17022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant