fix: background tasks follow the live model selection

TaskWorker resolved its model from LLM_MODEL env (frozen at MCP spawn),
so after a live set_model switch in the UI, dispatched background tasks
still asked for the old model. On local-vl-models (llama-swap
--models-max 1) that forces a swap against the loaded conversation
model; a force-kill mid-load surfaces as 400 'model failed to load'.

The agent now writes the selected model to <task_dir>/model.txt at
session start and on every switch; TaskWorker reads it with the env
value as fallback.
This commit is contained in:
snowspeeder committed 2026-09-12 07:16:08 -04:00
1 parent 436b3fc8b1
commit b01ee6478d
3 files changed
+37 -1

No files matched your search

+17 -1
View File
@@ -45,6 +45,22 @@ def _html_to_text(html: str) -> str:
return text.strip()
def _live_model(default: str) -> str:
"""Follow the conversation's live model selection when it is fresher.
The main agent writes the UI-selected model to the session's task dir
(shared with this MCP subprocess) on every set_model. Env is the
fallback for tasks dispatched before any switch happened.
"""
env = os.environ.get("TASKS_DIR", "/tmp/tasks")
try:
with open(os.path.join(env, "model.txt")) as f:
chosen = f.read().strip()
except OSError:
return default
return chosen or default
class TaskWorker:
"""Runs a background research/lookup task using the LLM + tools."""
@@ -65,7 +81,7 @@ class TaskWorker:
or os.environ.get("GEMMA_BASE_URL")
or "http://192.168.86.2:3005/api"
)
model = (
model = _live_model(
os.environ.get("LLM_MODEL")
or os.environ.get("GEMMA_MODEL")
or "qwen2.5-vl-3b"