From 65c58993939c614d33be1b669b892f366c0b36e0 Mon Sep 17 00:00:00 2001 From: Gabby Morgan Date: Thu, 1 Oct 2026 01:43:36 +0000 Subject: [PATCH] remove reasoning from llm calls for title and description generation --- .gitignore | 4 +++- AGENTS.md | 15 ++++++++++++++ config.example.json | 5 +++-- data/codegen-watch.jsonl | 0 semif_agent/cli.py | 1 + semif_agent/llm.py | 9 +++++++++ semif_agent/provider.py | 11 +++++++++++ tests/test_llm.py | 42 ++++++++++++++++++++++++++++++++++++++++ 8 files changed, 84 insertions(+), 3 deletions(-) create mode 100644 data/codegen-watch.jsonl diff --git a/.gitignore b/.gitignore index e39875b..78989ad 100644 --- a/.gitignore +++ b/.gitignore @@ -14,4 +14,6 @@ config.json *.log # Machine-specific experiments/recipes (GPU offload probe, benchmark harness). # Deliberately not part of the shipped agent. -local/ \ No newline at end of file +local/ +*.egg-info/ +*.bak.* \ No newline at end of file diff --git a/AGENTS.md b/AGENTS.md index 7bb7cbf..87e81b3 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -447,6 +447,21 @@ CLI, unit tests (24) + box integration tests (2). that model is pulled; `config.json` `llm.base_url` points here. - `bootstrap.sh` installs **no ollama**: it expects the target box to already run one for `llm` (and pulls `llm.model` into it), while codegen is remote. +- **The local `llm` model is a reasoning model — thinking must be off.** The + default `qwen3.5:4b` has ollama's `thinking` capability: it emits hidden + chain-of-thought and puts the answer in `content`. Through the `/v1` compat + endpoint the CoT consumes the entire reply budget, so + `generate_category`/`generate_skill`'s `max_tokens=128` returns + `finish_reason: length` with `content: ""` → `JSONDecodeError` and a traced + `draft_failed` (no category/skill is ever authored — the whole create branch + is silently dead). `LLMClient` now sends `reasoning_effort: "none"` + (`llm.disable_thinking`, **default on**) so the model answers the ~30-token + JSON directly (~2.5s). Ollama's native `think:false` is **not** plumbed + through `/v1` (silently ignored), and `/no_think` in the prompt is ignored + too; only `reasoning_effort` works there. Codegen *wants* the reasoning, so + the knob is `llm`-only — `CodegenClient` never sets it. Unbounded (no + `max_tokens`) does not help: the model loops on this prompt (2048 tokens of + reasoning, still empty `content`, 3+ min). ### jarvis (staging) diff --git a/config.example.json b/config.example.json index a7a3b91..fbd745a 100644 --- a/config.example.json +++ b/config.example.json @@ -21,7 +21,7 @@ "threads": 8 }, "llm": { - "_comment": "Small OpenAI-compatible model that authors a new category/skill title + description (never the runnable body — that is `codegen` below). Deliberately its own endpoint/model, so it can be local and fast even when codegen is remote. Runs on THIS machine's local ollama; bootstrap sets base_url from --llm-url (default http://127.0.0.1:11434) and pulls `model` if it is missing. Uses the same provider machinery as codegen (real SSE stream, token budget, idle watchdog); short JSON replies, so the sampler defaults are conservative (low temperature, no presence penalty). The draft is authored asynchronously by a single-slot worker and the request re-runs when it lands; an unreachable endpoint is graceful (traced, user notified), not fatal.", + "_comment": "Small OpenAI-compatible model that authors a new category/skill title + description (never the runnable body — that is `codegen` below). Deliberately its own endpoint/model, so it can be local and fast even when codegen is remote. Runs on THIS machine's local ollama; bootstrap sets base_url from --llm-url (default http://127.0.0.1:11434) and pulls `model` if it is missing. Uses the same provider machinery as codegen (real SSE stream, token budget, idle watchdog); short JSON replies, so the sampler defaults are conservative (low temperature, no presence penalty). The draft is authored asynchronously by a single-slot worker and the request re-runs when it lands; an unreachable endpoint is graceful (traced, user notified), not fatal. disable_thinking sends reasoning_effort:none so a reasoning model answers the short JSON directly instead of spending the reply budget on hidden chain-of-thought (set false only for an endpoint that rejects the parameter).", "base_url": "http://localhost:11434/v1", "model": "qwen3.5:4b", "timeout": 600, @@ -32,7 +32,8 @@ "temperature": 0.2, "top_p": 0.9, "presence_penalty": 0.0, - "frequency_penalty": 0.0 + "frequency_penalty": 0.0, + "disable_thinking": true }, "codegen": { "_comment": "OpenAI-compatible model that writes runnable skill bodies. Runs on a REMOTE ollama host (bootstrap sets base_url from --codegen-url, default guppy http://192.168.8.181:11434); the local small model is `llm` above. Bodies perform real actions against the user's configured services via stdlib transports, read every operational value from ctx.config, and declare their integration (INTEGRATION: service/transport/config_vars). Larger/slower than the decision or title/description model. The transport always streams so the client-side token budget (output cap / peak total fill) can abort a runaway in real time; stream only toggles console echo of tokens. timeout is total seconds; idle_warn/idle_timeout surface a silent (wedged) stream. The context window is auto-detected via ollama /api/show; context_window is the manual fallback (0 = auto/default 100000). smart_limit/warn_limit and max_fill_ratio/warn_fill_ratio bound total fill; max_output caps streamed output (>=1 absolute tokens, 0 tuple[Scheduler, dict]: top_p=float(llm_cfg.get("top_p", 0.9)), presence_penalty=float(llm_cfg.get("presence_penalty", 0.0)), frequency_penalty=float(llm_cfg.get("frequency_penalty", 0.0)), + disable_thinking=bool(llm_cfg.get("disable_thinking", True)), ) log = DecisionLog(config.get("log", "data/decisions.jsonl")) trace = TraceLog(config.get("trace", "data/runs.jsonl")) diff --git a/semif_agent/llm.py b/semif_agent/llm.py index b6f76c9..b9c9da8 100644 --- a/semif_agent/llm.py +++ b/semif_agent/llm.py @@ -26,6 +26,13 @@ class LLMClient(OpenAICompatClient): Short JSON replies, so the defaults are conservative: low temperature, no presence penalty, a modest timeout, and single-shot generation. Every value is overridable from the `llm` block in `config.json`. + + `disable_thinking` defaults **on**: the small local model is often a + reasoning model (e.g. `qwen3.5:4b`) whose hidden chain-of-thought consumes + the short reply budget and leaves `content` empty — the same cap problem + codegen documents, but here the reply is a ~30-token JSON object, so the + thinking is pure overhead. Set `llm.disable_thinking: false` for a + non-reasoning endpoint that rejects the parameter. """ error_class = LLMError @@ -54,6 +61,7 @@ class LLMClient(OpenAICompatClient): top_p: float = 0.9, presence_penalty: float = 0.0, frequency_penalty: float = 0.0, + disable_thinking: bool = True, ): super().__init__( base_url=base_url, @@ -76,6 +84,7 @@ class LLMClient(OpenAICompatClient): top_p=top_p, presence_penalty=presence_penalty, frequency_penalty=frequency_penalty, + disable_thinking=disable_thinking, ) @staticmethod diff --git a/semif_agent/provider.py b/semif_agent/provider.py index e5707c2..2e18d72 100644 --- a/semif_agent/provider.py +++ b/semif_agent/provider.py @@ -88,6 +88,13 @@ class OpenAICompatClient: `frequency_penalty` are per-`chat` overridable; the `generate_skill_body` escalation ladder bumps presence toward the card's max (2.0) and lowers temperature on retries. `max_attempts` bounds that ladder (default 3). + + `disable_thinking` sends `reasoning_effort: "none"` so a reasoning model + answers directly instead of spending the (short) reply budget on hidden + chain-of-thought. It is off by default because codegen *wants* the + reasoning; the `llm` title/description author turns it on. Ollama honors it + on the `/v1` compat endpoint (its native `think:false` is not plumbed + through `/v1`). """ error_class = ProviderError @@ -117,6 +124,7 @@ class OpenAICompatClient: presence_penalty: float = 1.5, frequency_penalty: float = 0.2, max_attempts: int = 3, + disable_thinking: bool = False, ): self.base_url = base_url.rstrip("/") self.model = model @@ -139,6 +147,7 @@ class OpenAICompatClient: self.presence_penalty = presence_penalty self.frequency_penalty = frequency_penalty self.max_attempts = max_attempts + self.disable_thinking = disable_thinking self._window: int | None = None def chat( @@ -169,6 +178,8 @@ class OpenAICompatClient: "stream": True, "stream_options": {"include_usage": True}, } + if self.disable_thinking: + payload["reasoning_effort"] = "none" if max_tokens is not None: payload["max_tokens"] = max_tokens body = json.dumps(payload).encode("utf-8") diff --git a/tests/test_llm.py b/tests/test_llm.py index 16117e5..3d64408 100644 --- a/tests/test_llm.py +++ b/tests/test_llm.py @@ -123,6 +123,48 @@ def test_generate_skill_via_provider(): httpd.server_close() +def test_llm_disables_thinking_by_default(): + """The title/description author sends reasoning_effort:none so a reasoning + model answers the short JSON directly (its CoT otherwise eats the 128-token + reply budget and leaves `content` empty).""" + httpd, base = _fake_server('{"title": "x", "description": "y"}') + try: + LLMClient(base_url=base, model="test", timeout=10).chat( + [{"role": "user", "content": "name this"}] + ) + assert httpd.RequestHandlerClass.received[0]["reasoning_effort"] == "none" + finally: + httpd.shutdown() + httpd.server_close() + + +def test_llm_thinking_can_be_re_enabled(): + httpd, base = _fake_server('{"title": "x", "description": "y"}') + try: + LLMClient(base_url=base, model="test", timeout=10, disable_thinking=False).chat( + [{"role": "user", "content": "name this"}] + ) + assert "reasoning_effort" not in httpd.RequestHandlerClass.received[0] + finally: + httpd.shutdown() + httpd.server_close() + + +def test_codegen_keeps_thinking(): + """Codegen wants the chain-of-thought: the knob must not leak into it.""" + from semif_agent.codegen import CodegenClient + + httpd, base = _fake_server("def predict(ctx, request):\n pass\n") + try: + CodegenClient(base_url=base, model="test", timeout=10).chat( + [{"role": "user", "content": "write a body"}] + ) + assert "reasoning_effort" not in httpd.RequestHandlerClass.received[0] + finally: + httpd.shutdown() + httpd.server_close() + + def test_unreachable_endpoint_raises_llm_error(): client = LLMClient(base_url="http://127.0.0.1:1/v1", model="test", timeout=2) with pytest.raises(LLMError):