# Cross-module UI invariants

Breaking a rule changes this list first, then the code. Each rule should have a pinning test under `tests/unit/`.

1. **Change** model or provider toggle custom provider deleted
2. **Forget** `_clear_stale_defaults` last used, chat, exec, default agent
3. **Broadcast** `agent_settings_changed` through the event bus
4. **Refetch** agent settings: chips models-enabled: dropdowns

Rules 1 to 3. A new entry point that changes the set (config import, bulk operations, CLI writes) hooks the same broadcast; Settings may be in another tab.

| Rule | Holds through |
| --- | --- |
| 1. The enabled set is the only model gate | `list_enabled_models()`: spec rows in the providers config. Disabled means no picker, no default, no chip |
| 2. Enabled-set changes broadcast | Every consumer reacts, as drawn |
| 3. Disabling the last used model forgets it | New conversations start on `last_used_model` in `config.json`; there is no default. Hiding would keep it serving background work and bring it back on re-enable |
| 4. The frontend store can express cleared | `setAgentSettings` field: object replaces, `null` clears, `undefined` keeps. `??` alone cannot overwrite a stale value; any field the backend can report gone needs this |
| 5. Backend-to-frontend frames go through the event bus | `emit_ws_frame({"type": ..., "data": ...})`; one server subscriber forwards to the sockets ([event layer](https://fzkuji.github.io/OpenProgram/docs/reference/design/proactive/event-layer.html)). `_s._broadcast` hides a frame from bus consumers (proactive, logging) and audit. Exempt, carrying session routing: `_broadcast_envelope`, `_broadcast_chat_response` |
| 6. The three spawn entry points share one semantics | Clean mode: `spawn_caller` makes the branch root's `caller` the opening turn, not ROOT. Change and test together |
| 7. The chat sibling switcher appears only on real forks | `< N/M >` sets group by fork point (`predecessor`, else `caller`; ROOT means none) and hold conversation turns only |
| 8. Instant interaction feedback | [Interaction feedback](https://fzkuji.github.io/OpenProgram/docs/reference/design/ui/interaction-feedback.html). Function runs pay about 1s of subprocess cold start, which must never feel like a dead click |
| 9. Display order may change; data order may not | The attach pointer stays at the tail for head movement, yet the Spawned card renders before the reply it fed. Solved in the server projection (`transcript/children.py`), never in persisted order or head semantics |
| 10. SSR boundary: no window at module scope | `apps/web/lib/runtime-bridge/*` may read `window` at module scope (`session/chat-handlers.ts`); a static import from a prerendered page breaks. New modules keep this or defer window access into function bodies |
| 11. A transcript row draws what the server sent | The main chat, a split pane and an expanded sub-agent all draw through `TranscriptRows`. Each row dispatches on its `rowKind` with no default branch and takes its conversation from the row list, never from the focused one |

**Rule 1: surfaces**

| Surface | Source |
| --- | --- |
| Top-bar chat and exec chips | the gated `GET /api/agent_settings` |
| Reads of that endpoint | One request in flight per conversation and branch (`lib/agents/agent-settings-request.ts`): the socket connecting, the conversation loading and the composer mounting share its answer, and nothing is kept after it. `agent_settings_changed` or this page's own write makes later callers ask again |
| Model picker dropdowns | `/api/models/enabled` |
| Default resolution on send | `_resolve_session_provider_model`, including the `agent.json` model path |
| Function execution | the exec runtime |

Empty picker, no model in the top bar and the enable-a-model send error hold together.

**Rule 3: what is cleared**

| Record | Cleared when |
| --- | --- |
| `last_used_model` (`openprogram/models/providers/catalog/last_used.py`) | It leaves the enabled set |
| `runtime_management` chat global, default agent's `model` in `agent.json` | The chat global's model leaves the set |
| `runtime_management` exec global | The exec global's model leaves the set |

All in `_clear_stale_defaults`.

**Rule 6: entry points and budgets**

| Entry | Path |
| --- | --- |
| `agent()`, synchronous | `openprogram/programs/tools/agents/agent/agent/agent.py` |
| Asynchronous runner | `openprogram/runtime/jobs/runner/` |
| `send_message` | `openprogram/programs/tools/agents/send_message/` |

| Setting | Default | Counts |
| --- | --- | --- |
| `agent.max_spawn_depth` | 1 | Generations of new agents; only creating one counts. At 1 a spawned agent's `agent()` spawn is refused and it does the work itself |
| `agent.max_messages` | 0, no limit | Messages in the chain, whatever the tool, replies included |

Tool exposure is binary: `agent` leaves the listing when messages or generations run out, except in a fork, which keeps its caller's tool list for the shared prompt cache and is refused at run time. `read_conversation` stays. An agent reading a worker's result keeps its own generation count, so it can spawn the next wave. See [agent collaboration](https://fzkuji.github.io/OpenProgram/docs/reference/design/runtime/collaboration/agent-collaboration.html#limits).

**Rule 7: never joins**

| Row | Why |
| --- | --- |
| Tool and code sub-call rows, ROOT | They have no predecessor and would pollute the root set |
| `source=agent_spawn` branch roots | Grouped apart from the user's turns: an agent-opened branch is not a pageable alternative |
| `display=runtime` cards | Function-run cards have their own navigation |

**Rule 10: how pages load it**

| Importer | Load |
| --- | --- |
| App Router pages and the shell | `next/dynamic` with `ssr: false` |
| Settings components | Dynamic `import()`, as `refreshAgentChip` does |
| Components inside the shell | May import statically: never prerendered |

## Implementation status

Not yet: rule 5 holds for external sources and part of the server. Other routes and WebSocket actions (`routes/chat.py`, `routes/catalog/`, `ws_actions/`) still call `_broadcast` directly.
