JuliusBrussee/caveman · error

native session usage

Error message

native session usage: %w

What it means

SessionUsage wraps the error from the aggregate usage QueryRow scan with this message. It means the underlying SQL query or scan failed — typically a database-level problem (locked db, I/O error, corruption) rather than a data problem (no rows just yields status "not_observed").

Solutions

  1. Inspect the wrapped driver error for 'database is locked', 'disk I/O error', or scan-type mismatches.
  2. Retry after ensuring other processes have released the SQLite lock.
  3. Check disk health/space on the machine hosting the DB.
  4. If schema drift is suspected, reopen via store.Open so migrations run, or restore the DB from backup.

Example fix

// before
usage, err := store.SessionUsage(sid)
if err != nil { return err }
// after
usage, err := store.SessionUsage(sid)
if err != nil {
    if strings.Contains(err.Error(), "database is locked") { time.Sleep(250 * time.Millisecond); usage, err = store.SessionUsage(sid) }
    if err != nil { return err }
}
Defensive patterns

Strategy: retry

Try / catch

usage, err := store.SessionUsage(sid)
if err != nil {
    if isLocked(err) { time.Sleep(250*time.Millisecond); usage, err = store.SessionUsage(sid) }
    if err != nil { return err }
}

Prevention

When it happens

Trigger: Calling Store.SessionUsage with a valid session ID when the SELECT fails or the columns cannot be scanned into the ObserveSummary fields (e.g. database locked, disk error, schema drift).

Common situations: Another process holds a write lock on the SQLite file, the db file is on a full/failing disk, or the schema was modified externally so scan targets no longer match.

Understand the failure class

Background: Database query failed: Internal Server Error 500s wrapping SQL, Prisma, and connection failures — what to check first — this error's family across 16 libraries.

Related errors


AI-assisted analysis of JuliusBrussee/caveman@3ee70a1026 (2026-09-20). Data as JSON: /api/errors/2f6b4a8ec769b034. Report an issue: GitHub.

Appendix: source

Thrown at proxy/internal/store/store.go:749

		`SELECT COUNT(*),
		        COALESCE(SUM(CASE WHEN token_usage_basis = 'provider_complete' THEN 1 ELSE 0 END),0),
		        COALESCE(SUM(input_tokens),0), COALESCE(SUM(output_tokens),0),
		        COALESCE(SUM(cached_input_tokens),0), COALESCE(SUM(cache_creation_input_tokens),0),
		        COALESCE(SUM(reasoning_tokens),0), COALESCE(SUM(total_cost_usd),0),
		        COALESCE(SUM(savings_usd),0), COALESCE(SUM(compression_tokens_before),0),
		        COALESCE(SUM(compression_tokens_after),0),
		        COALESCE(GROUP_CONCAT(DISTINCT NULLIF(compression_token_count_basis,'')),''),
		        COALESCE(GROUP_CONCAT(DISTINCT NULLIF(session_correlation_basis,'')),'')
		   FROM requests WHERE session_id = ?`,
		sessionID,
	).Scan(
		&out.Requests, &out.ProviderCompleteRequests, &out.InputTokens, &out.OutputTokens,
		&out.CachedInputTokens, &out.CacheCreationInputTokens, &out.ReasoningTokens,
		&out.CatalogListPriceSubtotalUSD, &out.InferredSavingsUSD,
		&out.CompressionTokensBefore, &out.CompressionTokensAfter, &compressionBases, &correlationBases,
	)
	if err != nil {
		return out, fmt.Errorf("native session usage: %w", err)
	}
	if out.Requests == 0 {
		return out, nil
	}
	out.Status = "correlated"
	if !strings.Contains(correlationBases, ",") {
		out.CorrelationBasis = correlationBases
	} else {
		out.CorrelationBasis = "mixed"
	}
	if out.ProviderCompleteRequests == out.Requests {
		out.TokenUsageCoverage = "provider_complete"
	} else {
		out.TokenUsageCoverage = "mixed_or_unavailable"
	}
	if compressionBases == "estimated_engine_o200k" || compressionBases == "legacy_unspecified" {
		out.CompressionTokenCountBasis = compressionBases
	} else if compressionBases != "" {

View on GitHub (pinned to 3ee70a1026)