# The chat loops

Two conversations wrap the automated pipeline — the same two capabilities Taaweel 1.0 users loved.

## 1. Refine → lock → run (before research)

```mermaid
sequenceDiagram
  participant U as You
  participant R as Refiner (LLM)
  U->>R: "dreams in the quran"
  R->>U: Which do you mean — nature, interpretation, or the vocabulary (ruʾyā, ḥulm, manām)?
  U->>R: The vocabulary, and how the terms differ.
  R->>U: Proposed: "What do ruʾyā, ḥulm and manām signify, and how do they differ across their occurrences?" (ready)
  U->>U: edit if you wish, then LOCK
  U->>R: start run — the pipeline researches the locked question only
```

- The chat lives in a per-question workspace (`/dashboard/questions/:id`): messages, the proposed
  refined question, an editable lock box.
- Locking stores the refined text; the run's **plan stage researches the locked question**, not the
  rough original. API: `POST /api/questions/:id/chat | lock | run`.

## 2. Discuss → regenerate from an angle (after research)

```mermaid
sequenceDiagram
  participant U as You
  participant RC as Report chat (LLM)
  participant C as Compose stage
  U->>RC: "in simple words, nafs vs ruh?"
  RC->>U: grounded answer from the report's own sections, with [E-n] citations
  U->>C: ↻ "regenerate from angle: the three states of nafs as a moral journey"
  C->>U: new report version — same ledger & conclusions, new emphasis
```

- **Discuss** is grounded: the chat receives the report's answer, entity verdicts, parallel
  architectures, and objections ledger — and answers only from that material.
- **Regenerate** re-runs only the *compose* stage (~90 seconds, ~$0.02): same conclusions, new
  emphasis, opening with one sentence that names the angle. The report's version number increments;
  old versions stay on disk.

## Under the hood

| Piece | Where |
| --- | --- |
| Refiner prompt (ask-one-question / propose-when-ready) | `src/prompts/refine.md` |
| Report-chat prompt (grounded, ≤250 words, points to angle-regeneration) | `src/prompts/report_chat.md` |
| Chat service (history, persistence, LLM turn) | `src/services/chat.ts` |
| Tables | `question_chats`, `report_chats`, `questions.locked/refined`, `runs.angle` |
| API | `POST|GET /api/questions/:id/chat`, `/lock`, `/run`; `POST|GET /api/runs/:id/report-chat`; `POST /api/runs/:id/regenerate` |
| Dashboard | `/dashboard/questions/:id` workspace; chat + angle forms on every run page |
