[Bug]: health_storage_stats panics with SQLx decode error on empty user
Is there an existing issue for the same bug?
Branch Name
main
Actual Behavior
When calling GET /v1/health/storage for a user with no memories, the endpoint returns HTTP 500:
Database error: error occurred while decoding column 1: unexpected null; try decoding as an `Option`
Expected Behavior
The endpoint should return a valid JSON response with zero counts:
{"total": 0, "active": 0, "inactive": 0, "avg_content_size": 0.0}
Root Cause
In memoria-storage/src/store.rs, health_storage_stats() uses:
let row: (i64, i64, f64) = sqlx::query_as(
"SELECT COUNT(*) as total, \
SUM(CASE WHEN is_active = 1 THEN 1 ELSE 0 END) as active, \
AVG(LENGTH(content)) as avg_content_size \
FROM mem_memories WHERE user_id = ?",
)
Per SQL standard, SUM() and AVG() return NULL on empty result sets (unlike COUNT(*) which returns 0). The non-Option types i64 and f64 cannot decode NULL, causing the SQLx decode panic.
Fix
Change the type to (i64, Option<i64>, Option<f64>) and unwrap with defaults:
let row: (i64, Option<i64>, Option<f64>) = sqlx::query_as(...);
let total = row.0;
let active = row.1.unwrap_or(0);
let avg_content_size = row.2.unwrap_or(0.0);
Steps to Reproduce
- Start Memoria with a fresh database
- Create a user (or use any user_id that has no memories)
- Call
GET /v1/health/storage with that user's auth
- Observe HTTP 500 with decode error
Additional information
Discovered during astra-engine E2E testing with mock embedding provider. The health_hygiene endpoint is not affected because it only uses COUNT(*) queries.
[Bug]: health_storage_stats panics with SQLx decode error on empty user
Is there an existing issue for the same bug?
Branch Name
main
Actual Behavior
When calling
GET /v1/health/storagefor a user with no memories, the endpoint returns HTTP 500:Expected Behavior
The endpoint should return a valid JSON response with zero counts:
{"total": 0, "active": 0, "inactive": 0, "avg_content_size": 0.0}Root Cause
In
memoria-storage/src/store.rs,health_storage_stats()uses:Per SQL standard,
SUM()andAVG()returnNULLon empty result sets (unlikeCOUNT(*)which returns0). The non-Option typesi64andf64cannot decode NULL, causing the SQLx decode panic.Fix
Change the type to
(i64, Option<i64>, Option<f64>)and unwrap with defaults:Steps to Reproduce
GET /v1/health/storagewith that user's authAdditional information
Discovered during astra-engine E2E testing with mock embedding provider. The
health_hygieneendpoint is not affected because it only usesCOUNT(*)queries.