# Todo 53.1: EventPrompt Order Preservation ## Problem Commit `cf481ab` correctly made EventPrompt derived state, but `removeEventPromptItems()` deletes it and `ensureEventPromptItem()` appends the replacement. Tool changes, mode round trips, and restored prompt lists therefore move EventPrompt to the end and change serialized system-message order. ## Scope - Preserve the first generated EventPrompt's position relative to retained custom prompts. - Remember that insertion position while `chat_agent_tools` hides EventPrompt. - Restore the saved typed/legacy EventPrompt position when loading prompt items. - Continue removing duplicate generated EventPrompt items and preserving ordinary custom prompts. - Do not add frontend dependencies or change backend contracts. ## TDD Steps 1. Add a failing regression in `tests/test_websocket_api.py` that requires an explicit EventPrompt insertion-position state/helper, captures position before removal, and reinserts with `insertBefore` instead of unconditional append. 2. Run the focused test and confirm it fails for the current append-only implementation. 3. Implement the smallest position-preserving logic in `src/agent_lab/presentation/static/app.js`. 4. Verify tool refresh, `dual_agent -> chat_agent_tools -> dual_agent`, and restore paths use the same remembered position. 5. Run: ```bash uv run pytest tests/test_websocket_api.py -k "event_prompt or prompt_restore or tool_mode" -q node --check src/agent_lab/presentation/static/app.js git diff --check ``` 6. Commit with the solved problem and ordering risk, then repeat spec and code-quality reviews. ## Acceptance - EventPrompt content and visibility remain mode/tool-derived. - At most one EventPrompt exists. - Refreshing tools does not move EventPrompt. - Hiding and showing EventPrompt restores its previous insertion position. - Restoring typed or legacy prompt data derives the insertion position from the first generated EventPrompt while discarding duplicates. - Custom prompt order and outbound request contracts remain unchanged.