# AI Conversation System - P2 Backlog

Issues identified during the March 2026 audit. Implement as needed.

## ~~P2-1: Tool definitions not counted in token budget~~ FIXED (da597d4d)
**File:** `ai_response.ts`
✅ `estimateToolTokens()` now subtracts tool definition cost from available budget before compaction/trimming.

## ~~P2-2: Prompt template duplication~~ FIXED (a2298587)
**File:** `ai_response.ts`
✅ Extracted shared footer (time context, affinity scoring, roleplay consistency) into `promptFooter`. Each variant now only defines its personality block.

## ~~P2-3: No summary size capping~~ FIXED (5b50e132)
**File:** `ai_response.ts`
✅ After each summarization, checks if summary exceeds ~500 tokens. If so, re-condenses it aggressively with a 400-token cap.

## ~~P2-4: Time gap system messages preserved during trimming~~ FIXED (a2298587)
**File:** `ai_response.ts`
✅ Added `isTimeGapMessage()` helper. Time gap system messages are now excluded from the preserve filter during trimming.

## ~~P2-5: frequency_penalty still aggressive~~ FIXED (009ae1f7)
**File:** `ai_response.ts`
✅ Reduced from 0.6 → 0.35 across all 3 API call sites.

## ~~P2-6: Memory reinforcement is dead code~~ FIXED (00dbc40d)
**File:** `character_memory.ts`
✅ `reinforceMemory()` now called when facts are used in prompts. Decay uses the more recent of `learned_date` or `last_accessed` as anchor, so frequently-accessed memories resist decay.

## ~~P2-7: SQL injection pattern in pruneDecayedMemories~~ FIXED (009ae1f7)
**File:** `character_memory.ts`
✅ Now uses parameterized placeholders for the `IN` clause.

## ~~P2-8: Flatter event gives free affinity (fixed partially)~~ FIXED (a2298587)
**File:** `events.ts`, `types.ts`
✅ Removed `affinityChange` from all conversation check results and `ConversationCheckResult` interface. AI determines the actual delta; static values were dead code.

## ~~P2-9: JSON regex doesn't handle escaped quotes~~ FIXED (bf9e214b)
**File:** `ai_response.ts`
✅ Regex updated to `((?:[^"\\]|\\.)*)` to handle escaped quotes in AI responses.

## ~~P2-10: createAnswerOptions is a stub~~ FIXED (009ae1f7)
**File:** `types.ts`, `ai_response.ts`, `Player.ts`
✅ Removed dead stub method and both call sites. iOS uses free-text input.

## ~~P2-11: Summarization triggers only on message count, not token count~~ FIXED (bf9e214b)
**File:** `ai_response.ts`
✅ Token-based compaction now summarizes old messages when short conversations exceed the budget, instead of silently dropping them.
