# Master Prompt — OS Self-Reflection Phase (ALL-IN)

> **Use this prompt to generate the entire implementation for the OS Self-Reflection Layer.**
> Context: builds on completed Awareness Layer (POINTER = OS-AWARENESS-PHASE-003)

---

You are a senior systems engineer. Generate the FULL implementation for the **OS Self-Reflection Phase** that builds on an already-finished **Self-Awareness Layer**.

## Context
- POINTER: OS-AWARENESS-PHASE-003
- Stack: Next.js + TypeScript + Vitest (already in repo).
- Completed Awareness Layer provides:
  - Awareness Events: DESKTOP_LOADED, WINDOW_OPENED, WINDOW_FOCUSED, WINDOW_CLOSED, TELEMETRY_METRIC.
  - Event Bus (publish/subscribe + resetBus).
  - Snapshot (openWindowIds, activeWindowId, windowCount + resetSnapshot).
  - Telemetry (conditional logging via flags).
  - Feature flags exist: AWARENESS_ENABLED, TELEMETRY_ENABLED.

## Goal of this Phase
Transform raw events/telemetry into **insights** and a **self-feedback loop**:
1) **Introspection Engine**: derive signals (WINDOW_CHURN, FOCUS_STABILITY, LATENCY_SUMMARY, IDLE_RATIO, WINDOW_LIFETIME).
2) **Behavior Evaluator**: compare signals vs targets (SLO-like) and detect anomalies (optional).
3) **Feedback Loop**: propose → validate → optionally apply **policy adjustments** (e.g., THROTTLE_WINDOWS, DEFER_ANIM, ADJUST_PREFETCH, TUNE_FOCUS_TIMEOUT).
4) **Reflection Memory**: rolling storage (ring buffer) for signals/evaluations/decisions + policy snapshots.
5) **Policy/Strategy Manager**: versioned policy application + revert; DRY_RUN by default.
6) **Observability**: emit REFLECTION_SIGNAL / REFLECTION_EVAL / REFLECTION_DECISION / POLICY_CHANGED events.

## Directives
- Create new folder: lib/reflection/.
- **Implement files** (clean, documented, testable):
  - lib/reflection/flags.ts
  - lib/reflection/types.ts
  - lib/reflection/introspection.ts
  - lib/reflection/evaluator.ts
  - lib/reflection/feedback.ts
  - lib/reflection/policy.ts
  - lib/reflection/memory.ts
  - lib/reflection/observe.ts
  - lib/reflection/index.ts
  - tests/reflection/*.spec.ts
- Integration with Awareness Bus and minimal Debug hooks.
- Config defaults, feature flags, safety, and performance limits.
- Output: file tree + code + tests + README + local verification steps.

## Acceptance Criteria
- Tests passing (≥ Awareness baseline coverage).
- DRY_RUN mode works safely.
- Observability logs emitted correctly.
- Cooldowns, thresholds, and rollbacks implemented.
- Deterministic and performant.

---
