Message Consistency Monitor: Design Specification
Sherlock's Analysis : As a detective of data and dialogue, I've designed this Message Consistency Monitor (MCM) to detect drifts in conversational integrity. It scans message streams for inconsistencies—like a liar tripping over their own alibi—using diffing for precise change detection, red flags for anomaly flagging, and fixes for automated remediation. Ideal for AI chats, support bots, or multi-turn dialogues.
The system processes messages in real-time or batch mode, maintaining a conversation state (facts, entities, tone, context) in a lightweight backend.
1. Architecture Overview
code Copy code
[Message Stream] → [Preprocessor] → [State Extractor] → [Diff Engine] → [Red Flag Detector] → [Fix Generator] → [Output: Alert + Fixed Message]
↓
[Persistent State Store]
2. Diffing Mechanism
Compares new message against historical state using multi-layer diffing.
Diff Types
Layer Method Output Lexical Token diff (difflib/Levenshtein) Insertions/Deletions/Substitutions (e.g., "red car" → "blue car") Semantic Cosine similarity on embeddings (threshold: 0.85) + LLM entailment Contradiction score (0-1); e.g., "I'm in NY" vs. prior "I'm in LA" → 0.92 contradict Structural Entity alignment (spaCy/NER) + Graph diff Changed relations: e.g., User.location: NY → LA Temporal Timeline check (e.g., event order violations) Anomaly: "Yesterday I won" after "I lost last week"
Diff Algorithm (pseudocode):
python Copy code
def compute_diff(new_msg, state):
facts_new = extract_facts(new_msg) # LLM: "Extract [entity, rel, val]"
lexical_diff = levenshtein(new_msg.text, state.last_text)
semantic_diff = 1 - cosine_emb(new_msg.embed, state.embed_avg)
graph_diff = kg_diff(state.graph, facts_new) # Neo4j Cypher query
return { 'score': weighted_avg([lexical, semantic, graph]), 'deltas': [...] }
Example Diff Output :
code Copy code
Deltas:
- Entity: car.color → "red" (old) vs "blue" (new) [Contradict: High]
- Tone: neutral → aggressive [Drift: Medium]
Score: 0.78 (inconsistent)
3. Red Flags
Threshold-based alerts (configurable: Low/Med/High). Scanned post-diff.
Red Flag Trigger Severity Example Fact Contradiction Semantic score > 0.7 + entity mismatch High "Age: 30" → "Age: 40" Persona Drift Tone/persona embed shift > 0.6 Med Sherlock-like wit → casual slang Logical Inconsistency LLM verdict: "Does A entail B?" = No High "All cats fly" after "Cats don't fly" Repetition Abuse Lexical overlap > 80% in last N msgs Low Copy-paste loops Timeline Break Event seq violation (e.g., future ref before past) Med "Tomorrow I did X" Off-Topic Jump Topic embed drift > 0.75 Low Finance chat → recipes Hallucination Spike New facts w/ low confidence (<0.5 from LLM extractor) High Invented user details Length Anomaly ±2σ from session avg Low Sudden novella response
Flag Pipeline :
Diff score > 0.5 → Run 8 checks.
Aggregate score → Alert level.
Log to dashboard (e.g., Grafana).
4. Fixes
Automated corrections with human-in-loop option. Fix Modes : Suggest, Auto-Apply, Reject.
Fix Strategies
Issue Type Fix Method Example Output Contradiction LLM Rewrite: "Resolve [delta] while preserving intent" Original: "I'm 40 now." → Fixed: "Earlier I said 30? Must've misspoken—I'm 30." Drift Style Transfer: Prompt w/ original persona embed Aggressive → Witty: "Blue car? My dear Watson, it was crimson last eve!" Logical Inference Injection: Add bridging facts "Cats fly? Only in your dreams—grounded felines, as before." Repetition Paraphrase + Diversify: Use T5/BART model "Red car again? Indeed, still scarlet." Timeline Normalize: LLM timeline sort + rephrase "Tomorrow? I meant next week for X." Off-Topic Bridge + Redirect: "Interesting, but tying back to [topic]..." Hallucination Fact-Check API (e.g., Perplexity) + Strip Remove unverified claims. Length Truncate/Expand w/ summarizer
Fix Generator Pseudocode :
python Copy code
def generate_fix(msg, diffs, flags):
prompt = f"""
Original: {msg.text}
Issues: {diffs}
Flags: {flags}
Rewrite to fix ALL issues, stay consistent with state: {state_summary}
"""
fixed_msg = llm.generate(prompt, max_tokens=msg.len)
confidence = llm.self_eval(fixed_msg, state) # 0-1
return fixed_msg if confidence > 0.9 else "REJECT: Manual review"
Deployment Hooks :
Pre-Response : Intercept bot outputs.
Post-User : Flag user lies for context.
Batch Audit : Retro-scan histories.
Implementation Roadmap
MVP (1 week) : Lexical/Semantic diff + 3 red flags + LLM fixes (LangChain prototype).
V1 (2 weeks) : Add graph store + dashboard.
Scale : Async streaming, multi-convo support.
This monitor ensures dialogues stay watertight—no escapes for inconsistencies. Questions, Holmes?