Skip to content

Add ordered Gemini credential failover - #86

Merged
jserv merged 3 commits into
sysprog21:mainfrom
matthiola0:feat/gemini-key-failover
Sep 25, 2026
Merged

jserv merged 3 commits into
sysprog21:mainfrom
matthiola0:feat/gemini-key-failover

Conversation

@matthiola0

@matthiola0 matthiola0 commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #78.

Summary

A rejected or exhausted Gemini key can end an interview even when another credential is available. Add an ordered key list so the interview can switch credentials while staying in its original LiveKit room and project.

Changes

  • Use provider keys, then global GOOGLE_API_KEYS, then global GOOGLE_API_KEY, resolved into one list at load time. Keys from a provider or the config file replace what they would inherit, and a blank value counts as unset. Reject empty entries and duplicate keys.
  • Share cooldowns across interviews and keep a working key selected for each interview. A rejected key sits out for 60 seconds; a 403 with no reason only for the kind of request that saw it. Failures that aren't about the key keep the existing retry budget on the same credential.
  • When every Live key is out on quota, wait for the first one back within the restart budget, as a sole key would retry. An interview's first open still ends within 30 seconds.
  • Switching Live keys uses cold recovery from retained interview state. Report key changes do not restart Live. Cross-key resumption remains unverified, and keys from the same Google Cloud project still share quota.
  • check-gemini selects keys the same way: it skips rejected keys, tries no more keys than an interview's first open could reach, and reports any other failure at once.
  • Make the integration tests portable so they also pass on Windows.

Testing

Add coverage for selection order, shared and per-surface cooldowns and their expiry, the wait on an all-quota rotation, reason codes in responses and close frames, blank key lists, redaction, bounded retries, a first-open 503 retried on the selected key with and without a backup, and check-gemini failover and its attempt limit.

cargo test passes on Windows: lib 272, cli 45, config 44 and web 126. scripts/test.sh passes on Ubuntu 24.04 (WSL), and cargo-mutants over the changes since the last review leaves no missed mutants. Formatting and Clippy pass.


Summary by cubic

Adds ordered Gemini credential failover so a rejected or quota-exhausted key no longer ends an interview. The interview switches to the next key while staying in its original LiveKit room and project.

  • GOOGLE_API_KEYS takes precedence over legacy GOOGLE_API_KEY; a provider list replaces the global list. Precedence, cooldowns, and session recovery are documented in docs/providers.md.
  • Quota and rejected-key failures move on to the next key; transport errors and 5xx responses keep the existing retry budget on the same key.
  • Quota failures put a key on a shared 60-second cooldown per surface; a sole key is never marked, and invalid keys stay disabled until restart.
  • Live key switches start a cold Gemini session from retained interview state; report key changes do not restart Live.
  • check-gemini selects keys the same way and tries no more than an interview's first open could reach.
  • Keys from the same Google Cloud project still share quota; cross-key resumption is not verified.

The integration tests also pass on Windows: test servers are drained before cleanup, shell scripts run through sh, and the tests use occupied ports and portable fixtures.

Written for commit b17d12e. Summary will update on new commits.

Review in cubic

@jserv

jserv commented Sep 22, 2026

Copy link
Copy Markdown
Contributor

The integration suite is not green on Windows. Config tests use a Unix-only API, two CLI tests fail on platform assumptions, and most Web tests fail, many while deleting open SQLite files.

Resolve the known integration regressions.

@jserv jserv left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check https://cbea.ms/git-commit/ carefully and enforce the rules.

cubic-dev-ai[bot]

This comment was marked as resolved.

@matthiola0
matthiola0 force-pushed the feat/gemini-key-failover branch 2 times, most recently from 2afccb9 to 863eb6f Compare September 23, 2026 07:18
@matthiola0

Copy link
Copy Markdown
Contributor Author

Squashed the fixups into the commits they belong to; the tree is identical to 2afccb9, which passed CI. cargo test now also passes on Windows: cli 45, config 43, web 126.

@matthiola0
matthiola0 requested a review from jserv September 23, 2026 07:27
@jserv
jserv requested a review from ColtenOuO September 23, 2026 07:35

@ColtenOuO ColtenOuO left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this, left some suggestions in comment.

Comment thread src/livekit.rs Resolved
Comment thread src/main.rs Resolved
@matthiola0
matthiola0 force-pushed the feat/gemini-key-failover branch from 863eb6f to 016958f Compare September 23, 2026 14:51
Comment thread src/dispatch.rs Resolved
Comment thread src/gemini.rs Resolved
Comment thread src/gemini.rs Resolved
Comment thread src/gemini.rs Resolved
Comment thread src/gemini/credentials.rs Resolved
Comment thread src/livekit.rs Resolved
Comment thread src/main.rs Resolved
@matthiola0
matthiola0 force-pushed the feat/gemini-key-failover branch from 016958f to 86264d7 Compare September 24, 2026 11:02
@jserv
jserv requested a review from ColtenOuO September 24, 2026 12:32
@matthiola0
matthiola0 force-pushed the feat/gemini-key-failover branch from 86264d7 to 7ca1578 Compare September 24, 2026 12:50
Comment thread src/config.rs Resolved
Comment thread src/livekit.rs Resolved
@matthiola0
matthiola0 force-pushed the feat/gemini-key-failover branch from 7ca1578 to e6a69a3 Compare September 24, 2026 22:11
Comment thread src/config.rs Resolved
Comment thread src/gemini/credentials.rs Resolved
Comment thread src/gemini/credentials.rs Resolved
@matthiola0
matthiola0 force-pushed the feat/gemini-key-failover branch from e6a69a3 to 708c13f Compare September 25, 2026 10:10
@jserv
jserv requested review from ColtenOuO and alanhc September 25, 2026 11:56
Comment thread src/gemini/credentials.rs Resolved
Comment thread src/livekit.rs Resolved
Comment thread src/gemini.rs Resolved
Comment thread src/dispatch.rs Resolved
@ColtenOuO

Copy link
Copy Markdown
Collaborator

The PR description might need to be updated due to recent review changes.

A rejected or quota-exhausted Gemini key ended the interview even when
another key was available. Read an ordered GOOGLE_API_KEYS list and move
to the next key while staying in the original LiveKit room and project.
With backups configured, credential failures cool down across interviews
so each one does not rediscover an exhausted key; a sole key is never
marked, because one failure would otherwise refuse every later room.
When every key is out on quota, an interview waits for the first one
back, as a sole key would retry. Every other failure keeps the existing
retry budget on the same key, and an interview's first open still ends
within the time one attempt always took. A Google key or list set by a
provider or by the config file replaces what it would inherit, so its
rooms never bill another project. A Live key switch starts a cold
session from retained state, because resumption across keys is
unverified. check-gemini selects keys the same way and tries no more
keys than an interview's first open could reach.
Explain credential precedence, cooldowns and session recovery so
operators can configure backup keys and understand their limits.
Windows cannot unlink open SQLite files or launch shell scripts
directly. Drain test servers before cleanup and invoke scripts through
the shell. Use occupied ports and portable file fixtures so the same
assertions run without depending on Unix privileges, line endings or
timestamp precision.
@matthiola0
matthiola0 force-pushed the feat/gemini-key-failover branch from 708c13f to b17d12e Compare September 25, 2026 15:31

@ColtenOuO ColtenOuO left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything looks good to me now, thanks so much for your patience and work!

Let's wait for @jserv to take another look.

@ColtenOuO
ColtenOuO requested a review from jserv September 25, 2026 15:41
@jserv
jserv merged commit aa69b10 into sysprog21:main Sep 25, 2026
9 checks passed
@jserv

jserv commented Sep 25, 2026

Copy link
Copy Markdown
Contributor

Thank @matthiola0 for contributing!

@matthiola0
matthiola0 deleted the feat/gemini-key-failover branch November 9, 2026 10:47
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.

Add ordered Gemini API key failover

3 participants