## Implementation Plan: Fix Duplicate Chat Message Correction Inconsistency ### Background The investigation confirms that the root cause is `temperature=0.1` in `gateway_complete()` (`correction.py:73`), which forces non-deterministic LLM sampling. Identical inputs sent in rapid succession produce different corrected outputs. No dedup mechanism exists, but fixing the determinism at the source is sufficient to satisfy the ticket’s requirement: “identical input messages should produce identical AI-corrected output”. ### Changes Required **1. Correction service – make LLM call deterministic** File: `services/correction.py` Line: 73 Change: `temperature=0.1` → `temperature=0` Rationale: Zero temperature eliminates sampling variance, guaranteeing that the same prompt always yields the same output. This is the minimal, safe change that directly addresses the inconsistency without adding new infrastructure, caching, or dedup logic that could introduce latency or bugs. **2. Add a unit test to enforce determinism** File: `tests/unit/services/correction/test_correction.py` (new file) Content: A single test that mocks the HTTP POST to the gateway and asserts that the request body includes `"temperature": 0`. This ensures the change is not accidentally reverted and documents the deterministic requirement. We place it under the existing test infrastructure – the `tests/unit/services/openai_gateway/` directory already holds gateway tests; adding a `correction.py` test there is consistent. **3. No other code changes** - No alterations to persistence, routing, or relay logic. - No new dedup caches or rate limiters. The temperature change alone satisfies the expected behaviour. ### Environment & Verification Notes The project’s verification commands (`pip install -e '.[dev]' -q && make test-unit` and `pip install -q ruff && ruff check .`) are written for a standard environment. On this system, PEP 668 may block bare `pip install` outside a virtual environment. **Before executing the verification commands, the agent must first create and activate a virtual environment** (e.g., `python -m venv .venv && source .venv/bin/activate`). Alternatively, if venv creation is impractical, prepend `PIP_REQUIRE_VIRTUALENV=0` to each `pip install` command. The plan assumes the agent will handle this environment setup. ### Definition of Done - `temperature=0` is set in `gateway_complete()` (`services/correction.py:73`). - A new unit test file `tests/unit/services/correction/test_correction.py` verifies the request payload contains `"temperature": 0`. - `make test-unit` passes (all existing tests + new test) when run inside a properly configured virtual environment. - `ruff check .` passes with zero errors. - No other files are modified.