This commit is contained in:
Mikan
2026-06-19 17:10:17 +03:00
parent 5202b30e0a
commit 81bbf8aa69
4 changed files with 171 additions and 59 deletions

View File

@@ -55,21 +55,11 @@ DEFAULT_SETTINGS = [
]
# Keys whose values come from environment variables (via Settings fields).
# These are re-applied on EVERY startup so .env is the source of truth.
# Admin-panel changes to these keys are runtime overrides that get reset on
# restart unless the operator also updates .env.
ENV_DERIVED_SETTING_KEYS = {
"llm.base_url",
"llm.api_key",
"llm.model",
"embedding.provider",
"embedding.base_url",
"embedding.api_key",
"embedding.model",
"embedding.dim",
"embedding.request_timeout",
}
# NOTE: env-derived defaults (llm.base_url, llm.api_key, llm.model,
# embedding.* etc.) are ONLY applied on the very first run via _seed_settings.
# After that, the admin panel is the source of truth — restarting the
# container will NOT overwrite admin-configured values with .env values.
# To force a re-seed, drop the `settings` table or delete the relevant rows.
async def init_db() -> None:
@@ -91,7 +81,6 @@ async def init_db() -> None:
)
)
await _seed_settings(session)
await _sync_env_derived_settings(session)
await _seed_builtin_presets(session)
await session.commit()
except Exception as e:
@@ -99,7 +88,6 @@ async def init_db() -> None:
log.warning("advisory_lock_unavailable_proceeding", error=f"{type(e).__name__}: {e}")
async with AsyncSessionLocal() as session:
await _seed_settings(session)
await _sync_env_derived_settings(session)
await _seed_builtin_presets(session)
await session.commit()
@@ -167,46 +155,6 @@ async def _seed_settings(session) -> None:
log.info("settings_already_exist")
async def _sync_env_derived_settings(session) -> None:
"""Re-apply env-derived setting values from .env on every startup.
This makes .env the source of truth for these keys: changing .env and
restarting the container takes effect immediately. Admin-panel edits to
these keys are runtime overrides that are reset on the next restart
(unless the operator also updates .env).
Only the env-derived keys (see ENV_DERIVED_SETTING_KEYS) are touched;
other settings (temperature, context params, etc.) are preserved as
configured via the admin panel.
"""
env_values = {key: value for key, value, _desc in DEFAULT_SETTINGS if key in ENV_DERIVED_SETTING_KEYS}
updated = 0
for key, new_value in env_values.items():
result = await session.execute(select(Setting).where(Setting.key == key))
row = result.scalars().first()
if row is None:
# Shouldn't happen (seeded above) but handle defensively.
session.add(Setting(key=key, value=new_value, description="Env-derived"))
updated += 1
else:
if row.value != new_value:
log.info(
"env_setting_resynced",
key=key,
old_value=str(row.value)[:80],
new_value=str(new_value)[:80],
)
row.value = new_value
updated += 1
if updated:
try:
await session.commit()
log.info("env_settings_synced", count=updated)
except IntegrityError:
await session.rollback()
log.warning("env_settings_sync_failed_concurrent")
async def _seed_builtin_presets(session) -> None:
"""Insert built-in presets if none exist yet."""
result = await session.execute(select(Preset).where(Preset.is_builtin.is_(True)))