Skip to content

Keep the Switch Trust settings until they change (CHOO-3667) - #1249

Closed
abeldantas wants to merge 1 commit into
mainfrom
trust-settings-cache
Closed

abeldantas wants to merge 1 commit into
mainfrom
trust-settings-cache

Conversation

@abeldantas

Copy link
Copy Markdown
Collaborator

Every message an agent sends is checked against Switch Trust, and each check read the settings row first: a transaction per message for a row that hardly ever changes.

In this PR we:

  • keep the settings each process read until they change (DynamicTrustClient)
  • have the gateway routes that save or clear them tell every process (TrustSettingsSignals), so a change still takes effect on the next message
  • read them again after 30 seconds at most, and when a process reconnects, for a signal that was lost

In a local run of the load harness (200 agents, 2 processes, 5 msg/s), that read was 1 transaction per message, and it is gone.

Every message an agent sends is checked against Switch Trust, and the
check read the settings row each time: a transaction per message for a
row that hardly ever changes. Each process now keeps what it read, and
the gateway's settings routes tell every process when the row changes
(TrustSettingsSignals), so a change still takes effect on the next
message. A process that reconnects forgets the settings, and they are
read again after 30 seconds at most, for a signal that was lost.

Copilot AI 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.

🟡 Changes recommended

Approval is blocked by an unresolved concurrency issue in cache refreshes and a critical test setup defect that prevents notification behavior from being verified.

2 open issues
What changed in this PR

This pull request reduces Switch Trust database load by caching settings per process while preserving timely updates through cross-process signals, reconnect resynchronization, and a 30-second refresh limit.

Changes:

  • Adds DynamicTrustClient caching with invalidation and expiry handling.
  • Adds TrustSettingsSignals for local, remote, and reconnect-based invalidation.
  • Wires gateway settings updates through dependency and application setup.
  • Adds tests and changelog documentation for the new behavior.
File Summary Final review finding
core/​tests/​switch_core/​trust/​test_signals.py Tests local, remote, and reconnect invalidation. No final comment.
core/​tests/​switch_core/​trust/​test_client.py Tests caching, invalidation, expiry, races, and read failures. No final comment.
core/​tests/​switch_core/​gateway/​test_trust_settings.py Tests authorization and post-commit notifications. Critical (1 vote): _client() creates an app that overwrites the monkeypatched callback, so the notification assertions do not exercise the spy. Preserve the callback through app construction or install it after each app is created.
core/​switch_core/​trust/​signals.py Propagates settings changes between processes. No final comment.
core/​switch_core/​trust/​setup.py Builds and exposes the dynamic Trust client. No final comment.
core/​switch_core/​trust/​client.py Caches Trust settings and refreshes them after invalidation or expiry. Moderate (3 votes): Concurrent checks can all observe an expired or missing cache and open separate settings transactions while the first refresh is still pending. Serialize refreshes with a lock or shared in-flight task, then re-check the cache or generation.
core/​switch_core/​main.py Registers Trust signal handling and gateway invalidation wiring. No final comment.
core/​switch_core/​gateway/​trust_settings.py Publishes invalidation signals after committed settings updates and deletions. No final comment.
core/​switch_core/​gateway/​dependencies.py Stores and exposes the Trust settings change callback. No final comment.
core/​switch_core/​gateway/​app.py Passes the callback into gateway dependency initialization. No final comment.
CHANGELOG.md Documents the reduced settings-read frequency and invalidation behavior. No final comment.

Review effort: Lite

Comment on lines +171 to +173
monkeypatch.setitem(
gw_deps._state, "trust_settings_changed", lambda: told.append("changed")
)
Comment on lines +189 to +192
read_at = self._read_at
if read_at is not None and time.monotonic() - read_at < self._max_age_seconds:
return self._checker
generation = self._generation
@abeldantas

Copy link
Copy Markdown
Collaborator Author

Combined into #1307 with the other scaling PRs.

@abeldantas abeldantas closed this Nov 2, 2026
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.

2 participants