# RESUME — AI Assistant Context — ✅ COMPLETE (2026-07-17)

**Status:** DONE. All WPs shipped on `hazemdev` (BE+FE), verified green. ⛔ Not deployed/merged — owner runs `/fullpush`.
**Ledger:** [LEDGER.md](LEDGER.md) · **Briefs:** [tasks/](tasks/) · **Plan:** https://moonui.elbaset.com/his-analysis/ai-assistant-context-analysis.html

## What shipped
The in-app AI assistant (Core `AiChatService`) no longer runs on a hand-written static prompt. It's now fed a **generated "capability digest"** — every screen/feature of all 13 modules + a "what's new" summary from the CHANGELOG — filtered to the user's permissions, aware of the current screen, plus an owner-editable "extra knowledge" field. The digest is code-derived (nav-items + i18n + PermissionCatalog + LisScreenCatalog + CHANGELOG) so it **stays current automatically** every release. Honors the governing rule: "AI = interface; the code-derived catalog = product." Zero PHI.

| WP | Delivered | Commits |
|---|---|---|
| WP1 | `CapabilityDigestService` — generated bilingual digest (13 mods/122 routes), token-capped, versioned/cached, permission-filterable + generated `capability-map.json` + `gen-capability-map.mjs` | BE `5771755ed` |
| WP2 | Dynamic capability-aware system prompt in `AiChatService`; response-cache key folds `digest.version()` + permission-scope + page (scope-consistent, no cross-scope serve) | BE `132f599e0` |
| WP3 | FE dropped the smuggled hardcoded `SYSTEM_PROMPT`/fake-history; sends `page={route,label}`; BE validates + injects a page-context line | FE `c6b5977` + BE `92aecf6a8` |
| WP4 | Owner "extra knowledge" textarea + "Refresh knowledge" button in `/core/ai-settings`; `ai_settings.custom_knowledge` (migration, ran on dev); folded into the digest + `version()` | BE `882c9636c` + FE `e3e6200` |

(WP5 folded into WP1.)

## Verification
- **BE full AI+digest suite: 62 passed / 191 assertions, 0 failures** (baseline was 27; feature added 35). Zero regression.
- **FE `ng build`: green** (WP4 was the last FE change).
- Migration `2026_07_17_100000_add_custom_knowledge_to_ai_settings_table` ran on `moonui_dev_be` (column present). ⛔ no fresh/wipe.
- Native review per WP (Codex bwrap-blocked on server; advisor disabled → native, incl. a cross-scope no-leak test). Whole-feature seam verified: digest→prompt→cache→page→notes→version all connect.
- CHANGELOG: 2 bilingual `[Unreleased]` bullets (AI Assistant: knows real screens + page-aware · owner can teach notes + refresh). 0 conflict markers.

## Key facts / gotchas (for future work)
- Digest cache key = `ai.digest.{epoch}.{version()}.{locale}.{permFingerprint}`; `refresh()` bumps the epoch (busts all variants, no key enumeration). `version()` = hash of capability-map + CHANGELOG head + app version + **current company's custom_knowledge**.
- Response cache `query_hash = sha256(message | digest.version() | permTag | pageTag)`, symmetric read/write; `AiResponseCache` is `(company_id, query_hash)` → no cross-company/cross-scope leak.
- `resolveUserPermissions()` (super-admin/owner ⇒ null ⇒ unfiltered) is shared by BOTH the digest filter and the cache permTag → prompt-scope ↔ cache-scope always consistent.
- **Regen the capability map after FE nav/i18n changes:** `node Modules/Core/resources/gen-capability-map.mjs Modules/Core/resources/capability-map.json /home/moonui/public_html/moon-erp` (committed JSON; BE reads JSON, never the TS). Also emit at deploy + the owner "Refresh knowledge" button.
- Clinic surfaces only 2 screens in the digest (redirect + settings) — no `ClinicScreenCatalog` analogue to LIS yet. Future enrichment: a Clinic screen catalog would deepen its digest coverage (like LIS's 48).

## ⛔ Next (owner)
Run `/fullpush` to unify hazemdev↔main + migrate dev + deploy `/app`. Then the assistant is digest-driven live; test by asking it about a new feature (clinic lab-results, loyalty) or "where is X" and confirm it answers correctly + deep-links, and try the "Assistant knowledge" field + "Refresh knowledge" on `/core/ai-settings`.

## Deferred
- Clinic screen catalog (deepen clinic digest coverage). · CHANGELOG "what's new" could later switch to an AI-summarized rolling digest if [Unreleased]+2 versions proves too thin.
