Skip to content

feat: run auto-switch as realtime watcher#24

Merged
Loongphy merged 16 commits intomainfrom
feat/realtime-auto-switch
Mar 22, 2026
Merged

feat: run auto-switch as realtime watcher#24
Loongphy merged 16 commits intomainfrom
feat/realtime-auto-switch

Conversation

@Loongphy
Copy link
Copy Markdown
Owner

@Loongphy Loongphy commented Mar 20, 2026

Summary

  • run auto-switch as a persistent realtime watcher across supported platforms
  • prefer local rollout events for daemon refresh and throttle API fallback to once per minute
  • update docs and tests to match the new watcher-based service model

Validation

  • zig build run -- list

Note

Run auto-switch as a persistent realtime watcher daemon instead of a per-minute oneshot

  • Replaces the periodic oneshot scheduler model with a long-running watcher: the daemon loop now polls at 1s (watch_poll_interval_ns) and rate-limits API refreshes to 60s (api_refresh_interval_ns).
  • Adds CandidateIndex, a heap-based in-memory ranking of non-active candidate accounts, updated incrementally and rebuilt lazily when usage windows reset.
  • The daemon now validates top candidates via API before switching, refreshes candidate usage during upkeep, and returns a structured AutoSwitchAttempt result instead of a bool.
  • Linux systemd installation switches from a .timer + oneshot unit to a persistent Type=simple service with Restart=always and ExecStart using daemon --watch; Windows switches from a per-minute schtasks trigger to a logon-triggered scheduled task registered via PowerShell with restart-on-failure semantics.
  • Extends sessions.zig scanning with a RolloutScanCache to avoid redundant filesystem scans, and adds fallback logic so the daemon uses the last usable local snapshot when the newest rollout event lacks rate limit windows, then falls back to the usage API.
  • usage_api transport layer now captures HTTP status codes (via --write-out for curl and base64 output for PowerShell), enabling callers to distinguish auth failures (403) from missing auth or timeouts.
  • Risk: existing Linux installs with a legacy codex-auth-autoswitch.timer unit will have that timer removed and replaced by the new service on next enable or reconciliation.

Macroscope summarized f00a6ff.

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Mar 20, 2026

Open in StackBlitz

@loongphy/codex-auth-darwin-arm64

npx https://pkg.pr.new/@loongphy/codex-auth-darwin-arm64@24

@loongphy/codex-auth-darwin-x64

npx https://pkg.pr.new/@loongphy/codex-auth-darwin-x64@24

@loongphy/codex-auth-linux-x64

npx https://pkg.pr.new/@loongphy/codex-auth-linux-x64@24

@loongphy/codex-auth-win32-x64

npx https://pkg.pr.new/@loongphy/codex-auth-win32-x64@24

@loongphy/codex-auth

npx https://pkg.pr.new/@loongphy/codex-auth@24

commit: f00a6ff

@Loongphy Loongphy merged commit 397d06b into main Mar 22, 2026
10 checks passed
@Loongphy Loongphy deleted the feat/realtime-auto-switch branch March 22, 2026 03:45
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