fix(config): unify ChannelsConfig resolution to env > settings > default#1124
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request refactors the configuration resolution for various channels (HTTP, Gateway, CLI, WASM, Signal) to establish a clear precedence: environment variables, then Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
ChannelsConfig::resolve() ignored most ChannelSettings fields, reading exclusively from env vars. This made `config set` ineffective for gateway, HTTP, CLI, and WASM channel settings — a prerequisite blocker for nearai#86 (hot-reload) and CLI management commands. - Add gateway and CLI fields to ChannelSettings with correct defaults - Rewrite resolve() to fall back to settings when env var is unset - Keep strict boolean validation via parse_bool_env for all bool fields - Fix GATEWAY_PORT default divergence (3001 -> 3000) in extension manager - Export DEFAULT_GATEWAY_PORT constant as single source of truth - Add 8 tests: settings fallback, env override, DB roundtrip, invalid bool rejection Part of nearai#1119 (Phase 1: Channels pilot) [skip-regression-check]
5ac8ffc to
3cd9487
Compare
There was a problem hiding this comment.
Code Review
This pull request effectively unifies the configuration resolution for channels to follow the env > settings > default priority, which is a great improvement for consistency and usability. The changes correctly implement this fallback logic for gateway, CLI, and WASM channel settings. The introduction of the DEFAULT_GATEWAY_PORT constant and fixing the port divergence are also valuable changes. The new tests are comprehensive and cover the new logic well.
I've added one suggestion to improve the test suite's maintainability and robustness by refactoring the duplicated environment variable cleanup logic into a shared helper function, ensuring proper SAFETY documentation for unsafe operations.
| unsafe { | ||
| std::env::remove_var("GATEWAY_ENABLED"); | ||
| std::env::remove_var("GATEWAY_HOST"); | ||
| std::env::remove_var("GATEWAY_PORT"); | ||
| std::env::remove_var("GATEWAY_AUTH_TOKEN"); | ||
| std::env::remove_var("GATEWAY_USER_ID"); | ||
| std::env::remove_var("HTTP_PORT"); | ||
| std::env::remove_var("HTTP_HOST"); | ||
| std::env::remove_var("SIGNAL_HTTP_URL"); | ||
| std::env::remove_var("CLI_ENABLED"); | ||
| std::env::remove_var("WASM_CHANNELS_DIR"); | ||
| std::env::remove_var("WASM_CHANNELS_ENABLED"); | ||
| std::env::remove_var("TELEGRAM_OWNER_ID"); | ||
| } |
There was a problem hiding this comment.
This block for clearing environment variables is duplicated in several tests (resolve_gateway_from_settings, resolve_cli_enabled_from_settings, etc.). There's also an inconsistency: this block is missing HTTP_WEBHOOK_SECRET and HTTP_USER_ID, which are cleared in resolve_http_from_settings and could potentially affect other tests if not cleared.
To improve consistency and maintainability, consider extracting this logic into a single helper function that clears all relevant environment variables. This ensures a clean slate for each test.
Here's an example of what that could look like:
const TEST_ENV_VARS: &[&str] = &[
"GATEWAY_ENABLED",
"GATEWAY_HOST",
"GATEWAY_PORT",
"GATEWAY_AUTH_TOKEN",
"GATEWAY_USER_ID",
"HTTP_PORT",
"HTTP_HOST",
"HTTP_WEBHOOK_SECRET",
"HTTP_USER_ID",
"SIGNAL_HTTP_URL",
"CLI_ENABLED",
"WASM_CHANNELS_DIR",
"WASM_CHANNELS_ENABLED",
"TELEGRAM_OWNER_ID",
];
fn clear_all_test_vars() {
// SAFETY: This function is called only within test contexts, which are guaranteed
// to be single-threaded before any asynchronous runtime (like Tokio) is initialized.
// Modifying environment variables is safe in a single-threaded context.
unsafe {
for var in TEST_ENV_VARS {
std::env::remove_var(var);
}
}
}Then, this block and others can be replaced with a single call to clear_all_test_vars();.
| unsafe { | |
| std::env::remove_var("GATEWAY_ENABLED"); | |
| std::env::remove_var("GATEWAY_HOST"); | |
| std::env::remove_var("GATEWAY_PORT"); | |
| std::env::remove_var("GATEWAY_AUTH_TOKEN"); | |
| std::env::remove_var("GATEWAY_USER_ID"); | |
| std::env::remove_var("HTTP_PORT"); | |
| std::env::remove_var("HTTP_HOST"); | |
| std::env::remove_var("SIGNAL_HTTP_URL"); | |
| std::env::remove_var("CLI_ENABLED"); | |
| std::env::remove_var("WASM_CHANNELS_DIR"); | |
| std::env::remove_var("WASM_CHANNELS_ENABLED"); | |
| std::env::remove_var("TELEGRAM_OWNER_ID"); | |
| } | |
| unsafe { | |
| std::env::remove_var("GATEWAY_ENABLED"); | |
| std::env::remove_var("GATEWAY_HOST"); | |
| std::env::remove_var("GATEWAY_PORT"); | |
| std::env::remove_var("GATEWAY_AUTH_TOKEN"); | |
| std::env::remove_var("GATEWAY_USER_ID"); | |
| std::env::remove_var("HTTP_PORT"); | |
| std::env::remove_var("HTTP_HOST"); | |
| std::env::remove_var("HTTP_WEBHOOK_SECRET"); | |
| std::env::remove_var("HTTP_USER_ID"); | |
| std::env::remove_var("SIGNAL_HTTP_URL"); | |
| std::env::remove_var("CLI_ENABLED"); | |
| std::env::remove_var("WASM_CHANNELS_DIR"); | |
| std::env::remove_var("WASM_CHANNELS_ENABLED"); | |
| std::env::remove_var("TELEGRAM_OWNER_ID"); | |
| } |
References
- The use of
unsafe std::env::set_var(and by extensionremove_var) is permissible during application startup or in single-threaded contexts like tests, but this safety invariant must be documented in aSAFETYcomment.
zmanian
left a comment
There was a problem hiding this comment.
Good fix with thorough tests. The env > settings > default pattern is well-implemented and consistent across all channel fields. The explicit Default impl for ChannelSettings correctly defaults gateway/cli/wasm to true. LGTM.
|
Tool-calling blocker triage: this looks merge-ready (approved + clean + green checks). Merging this should reduce config-source misrouting that can surface as false tool-calling failures. |
|
This PR is approved and CI is green — could you help merge it? Thanks! |
…ult (nearai#1124) ChannelsConfig::resolve() ignored most ChannelSettings fields, reading exclusively from env vars. This made `config set` ineffective for gateway, HTTP, CLI, and WASM channel settings — a prerequisite blocker for nearai#86 (hot-reload) and CLI management commands. - Add gateway and CLI fields to ChannelSettings with correct defaults - Rewrite resolve() to fall back to settings when env var is unset - Keep strict boolean validation via parse_bool_env for all bool fields - Fix GATEWAY_PORT default divergence (3001 -> 3000) in extension manager - Export DEFAULT_GATEWAY_PORT constant as single source of truth - Add 8 tests: settings fallback, env override, DB roundtrip, invalid bool rejection Part of nearai#1119 (Phase 1: Channels pilot) [skip-regression-check]
…ult (nearai#1124) ChannelsConfig::resolve() ignored most ChannelSettings fields, reading exclusively from env vars. This made `config set` ineffective for gateway, HTTP, CLI, and WASM channel settings — a prerequisite blocker for nearai#86 (hot-reload) and CLI management commands. - Add gateway and CLI fields to ChannelSettings with correct defaults - Rewrite resolve() to fall back to settings when env var is unset - Keep strict boolean validation via parse_bool_env for all bool fields - Fix GATEWAY_PORT default divergence (3001 -> 3000) in extension manager - Export DEFAULT_GATEWAY_PORT constant as single source of truth - Add 8 tests: settings fallback, env override, DB roundtrip, invalid bool rejection Part of nearai#1119 (Phase 1: Channels pilot) [skip-regression-check]
Summary
ChannelsConfig::resolve() ignored most ChannelSettings fields, reading
exclusively from env vars. This made
config setineffective for gateway,HTTP, CLI, and WASM channel settings — a prerequisite blocker for #86
(hot-reload) and CLI management commands.
Change Type
Linked Issue
Part of #1119 (Phase 1: Channels pilot)
Validation
cargo fmtcargo clippy --all --benches --tests --examples --all-featuresSecurity Impact
Database Impact
Blast Radius
Rollback Plan
Review track: