llm: fallback to legacy nearai.session key when loading DB session - #111
Conversation
Summary of ChangesHello @lawyered0, 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 introduces a backward-compatible mechanism for loading NEAR AI sessions from the database. It aims to prevent authentication regressions for older installations by ensuring that sessions persisted under a legacy key can still be retrieved, thereby facilitating smoother upgrades without altering the current write key for new sessions. Highlights
Changelog
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
|
|
Added as a follow-up split PR from PR #109 for clean audit scope. Scope is limited to legacy-session compatibility in DB loader:
No other behavior changes included. |
|
This PR is intended as a fix for issue #108 (legacy session key migration path), scoped only to session read compatibility. |
There was a problem hiding this comment.
Code Review
This pull request adds a fallback mechanism to load legacy NEAR AI sessions from the database, which is a good improvement for backward compatibility. The implementation is correct, and I have a suggestion to improve the code's readability and make it more idiomatic.
| let value = match store | ||
| .get_setting(&user_id, "nearai.session_token") | ||
| .await | ||
| .map_err(|e| LlmError::SessionRenewalFailed { | ||
| provider: "nearai".to_string(), | ||
| reason: format!("DB query failed: {}", e), | ||
| })? | ||
| .ok_or_else(|| LlmError::SessionRenewalFailed { | ||
| provider: "nearai".to_string(), | ||
| reason: "No session in DB".to_string(), | ||
| })?; | ||
| })? { | ||
| Some(value) => value, | ||
| None => { | ||
| tracing::warn!( | ||
| "nearai.session_token missing; falling back to legacy nearai.session for backwards compatibility" | ||
| ); | ||
| store | ||
| .get_setting(&user_id, "nearai.session") | ||
| .await | ||
| .map_err(|e| LlmError::SessionRenewalFailed { | ||
| provider: "nearai".to_string(), | ||
| reason: format!("DB query failed: {}", e), | ||
| })? | ||
| .ok_or_else(|| LlmError::SessionRenewalFailed { | ||
| provider: "nearai".to_string(), | ||
| reason: "No session in DB".to_string(), | ||
| })? | ||
| } | ||
| }; |
There was a problem hiding this comment.
The match statement can be expressed more idiomatically as an if let expression for this case. Additionally, ok_or_else can be simplified to ok_or since the error value is not computed dynamically.
let value = if let Some(value) = store
.get_setting(&user_id, "nearai.session_token")
.await
.map_err(|e| LlmError::SessionRenewalFailed {
provider: "nearai".to_string(),
reason: format!("DB query failed: {}", e),
})? {
value
} else {
tracing::warn!(
"nearai.session_token missing; falling back to legacy nearai.session for backwards compatibility"
);
store
.get_setting(&user_id, "nearai.session")
.await
.map_err(|e| LlmError::SessionRenewalFailed {
provider: "nearai".to_string(),
reason: format!("DB query failed: {}", e),
})?
.ok_or(LlmError::SessionRenewalFailed {
provider: "nearai".to_string(),
reason: "No session in DB".to_string(),
})?
};|
Implemented Gemini’s style suggestion on the code path (if-let + ok_or form) in commit a21d271. No behavior change; just readability/idiomatic cleanup. This should clear that review nit. |
ilblackdragon
left a comment
There was a problem hiding this comment.
LGTM. Clean, well-scoped change for backward compatibility.
What I checked:
-
Correctness: The fallback logic is sound. When
nearai.session_tokenreturnsNone, it triesnearai.sessionbefore failing. The write path (save_session) still writes tonearai.session_tokenonly (line 408), so after a successful save-then-load cycle the legacy key becomes unused -- which is the right migration behavior. -
Error handling: Both DB query errors and missing-key errors are handled identically between the primary and fallback paths. The
map_err+ok_orchain is consistent with the rest of the file. Usingok_orinstead ofok_or_elseis fine here since the error construction is cheap (just twoStringallocations). -
Format assumption: Both keys feed into the same
serde_json::from_value::<SessionData>()call (line 443-447). This assumes the legacynearai.sessionkey stores the sameSessionDataJSON shape (session_token,created_at,auth_provider). If any legacy deployment stored a different format (e.g., a bare token string), this would fail at deserialization rather than silently misbehaving -- which is the correct failure mode since the error message says "Failed to parse DB session". -
Code style: The
if letform adopted in the second commit is idiomatic and reads well. Thetracing::warn!log message clearly identifies what happened and why, which will help with debugging in production. -
No regressions: The write path is untouched, so current deployments continue writing to
nearai.session_token. The fallback is read-only and only triggers when the primary key is absent.
Minor observation (non-blocking): The duplicated map_err closure for DB query failures appears in both the primary and fallback paths. A small helper like fn db_query_err(e: impl Display) -> LlmError could reduce repetition, but that is a broader pattern across this file (see lines 186, 231, 261, 320, etc.) and not worth addressing in this PR alone.
Batch 2 PR Review: nearai/ironclaw PRs 111, 110, 109, 103, 95, 74Reviewer: AI Sub-Agent PR #111: Fix backwards compatibility for nearai.session_tokenSummaryThis PR adds backwards compatibility for the nearai session management by implementing a fallback mechanism. When Pros
Concerns
Suggestions
PR #110: Add env docs for local LLM providersSummaryThis PR updates the Pros
Concerns
Suggestions
PR #109: Normalize memory search query and update marked.jsSummaryThis PR addresses two security and stability issues in the web interface. First, it adds input normalization for memory search queries to prevent excessive query lengths and invalid input types. Second, it updates the marked.js dependency to a specific version with integrity hashing for supply chain security. The changes are defensive in nature, preventing potential DoS attacks and ensuring the integrity of third-party JavaScript dependencies. Pros
Concerns
Suggestions
PR #103: Per-request model override for OpenAI-compatible APISummaryThis is a significant feature PR that adds per-request model override capability across the entire LLM provider ecosystem. Previously, all requests used the active model, but now clients can specify a different model per request. The changes span multiple modules: request structs now include optional Pros
Concerns
Suggestions
PR #95: Add Venice AI provider and embeddingsSummaryThis PR adds comprehensive support for Venice AI as both an LLM provider and an embeddings provider. It introduces a new Pros
Concerns
Suggestions
PR #74: Security fix: Enhanced HTTP response size validationSummaryThis PR strengthens the HTTP tool's defense against OOM attacks by implementing two-stage response size validation. First, it checks the Pros
Concerns
Suggestions
Overall RecommendationsHigh Priority
Medium Priority
Low Priority
General Observations
|
…earai#111) * llm: fallback to legacy nearai.session when loading DB session * llm: simplify session fallback load with if-let form --------- Co-authored-by: Clawyered <clawyered@macbookair.home> Co-authored-by: Illia Polosukhin <ilblackdragon@gmail.com>
…earai#111) * llm: fallback to legacy nearai.session when loading DB session * llm: simplify session fallback load with if-let form --------- Co-authored-by: Clawyered <clawyered@macbookair.home> Co-authored-by: Illia Polosukhin <ilblackdragon@gmail.com>
Summary
Addresses auth regressions for legacy installations by adding a backward-compatible read path when loading NEAR AI sessions from DB settings.
Change
nearai.session_tokenas primary key.nearai.sessionkey.Rationale