# Operating Taaweel

## Run it

```powershell
cd taaweel
npm install
copy .env.example .env      # provider + keys
npm run data -- import <folder with the source DBs>
npm run dev                 # http://127.0.0.1:3901/dashboard
```

For a production install (build, `npm start`, health checks, shutdown) see the README's
*Production* section; for deploying (Docker, plain Node, cPanel), backups, monitoring and
retention, see `deploy/README.md`.

## Providers and models

`TAAWEEL_PROVIDER` = `mock | deepseek | zai | openai | openrouter`. Keys resolve: admin-saved key (Models → API keys) →
`TAAWEEL_<PROVIDER>_API_KEY` → `<PROVIDER>_API_KEY`, from this app's own `.env`. Per-stage overrides:
`TAAWEEL_MODEL_PLAN / _EXTRACT / _REASON / _COMPOSE` (each accepts `provider:model`).

Cheap models work — the pipeline decomposes reasoning into small, grounded, code-checked calls.
Typical full-run cost on `deepseek-chat`: **$0.6–1.0**; a compose-only regeneration: **~$0.02**.

## The two workflows

**Dashboard**: *New research* → refine chat → lock → run (live stage progress) → report →
discuss chat / regenerate-from-angle / science sources / rebuild-from-stage → behind-the-scenes page.

**API**:

| Method | Path | Purpose |
| --- | --- | --- |
| POST | `/api/questions` | create question (+ first run) |
| POST/GET | `/api/questions/:id/chat` | refinement chat |
| POST | `/api/questions/:id/lock` | lock the refined question |
| POST | `/api/questions/:id/run` | create a run for a locked question |
| POST | `/api/runs/:id/start` · `/stop` | run control |
| GET | `/api/runs/:id/events?since=` | NDJSON live events |
| GET | `/api/runs/:id` · `/ledger` · `/report.md` | state, ledger, report |
| POST/GET | `/api/runs/:id/report-chat` | discuss the report |
| POST | `/api/runs/:id/regenerate` | regenerate from an angle |
| POST | `/api/runs/:id/sources` | attach user-supplied empirical sources |
| POST | `/api/objections/:id/resolve` | resolve a ledger objection |
| GET | `/dashboard/runs/:id/meta` | the provenance page |
| GET | `/docs` · `/docs/:slug` · `/docs/:slug.md` · `/docs/export` | this documentation |

## Offline safety net

`npm run smoke` runs the entire pipeline against the **mock provider** (56 checks, no network, no
keys) — the same plumbing the live runs use. Use it after every code change.

## Cost discipline

- Deterministic code does everything countable; LLM calls are small and JSON-validated.
- Every stage records tokens and cost (`pipeline_jobs`); run pages show totals.
- Rebuild-from lets you re-run only what a change invalidates (e.g. `--rebuild-from audit` ≈ $0.05).

## Known boundaries (honest)

- GLM/Z.AI is fully wired but needs account balance to execute live runs.
- Rāzī's *Mafātīḥ al-Ghayb* is not yet in any fetchable catalogue; al-Wahīdī's *al-Wasīṭ* is
  available (`fetchTafsir.ts wasit`).
- With very cheap models, judgment-call stages can vary between runs (e.g. how far the cosmic
  reading of Ard goes); the code gates (blinding, scope partition, L7, terminology) bound that
  variance, and the report shows its own audit trail either way.
