---
phase: 05-mvp-deployment
plan: 02
type: execute
wave: 2
depends_on:
- 05-01
files_modified:
- adapter/matrix/handlers/__init__.py
- adapter/matrix/handlers/context_commands.py
- adapter/matrix/routed_platform.py
- tests/adapter/matrix/test_context_commands.py
- tests/adapter/matrix/test_routed_platform.py
autonomous: true
requirements:
- PH05-02
must_haves:
truths:
- "Each working Matrix room uses its own durable `platform_chat_id` as the real agent context boundary."
- "`!clear` resets only the current room by rotating its `platform_chat_id` and disconnecting the old upstream chat."
- "Save, load, context, and routed send paths resolve through room-local platform context, not shared user state."
- "Strict room routing assumes startup reconciliation has already repaired legacy rooms missing `platform_chat_id`."
artifacts:
- path: "adapter/matrix/handlers/context_commands.py"
provides: "Room-local `!clear`, save/load/context resolution, and upstream disconnect behavior"
- path: "adapter/matrix/routed_platform.py"
provides: "Strict room -> agent_id + platform_chat_id routing"
- path: "tests/adapter/matrix/test_context_commands.py"
provides: "Regression coverage for `!clear` and room-local context commands"
key_links:
- from: "adapter/matrix/handlers/__init__.py"
to: "adapter/matrix/handlers/context_commands.py"
via: "IncomingCommand registration for `clear`"
pattern: "\"clear\""
- from: "adapter/matrix/routed_platform.py"
to: "adapter/matrix/store.py"
via: "room metadata lookup"
pattern: "platform_chat_id"
---
Make room-local platform context explicit and user-facing by shipping real `!clear` semantics and strict per-room routing.
Purpose: Phase 05 must preserve Space+rooms UX while giving each room a true upstream context boundary.
Output: Updated command wiring, room-local context reset behavior, and routing regressions tied to `platform_chat_id`.
@/Users/a/.codex/get-shit-done/workflows/execute-plan.md
@/Users/a/.codex/get-shit-done/templates/summary.md
@.planning/PROJECT.md
@.planning/ROADMAP.md
@.planning/STATE.md
@.planning/phases/05-mvp-deployment/05-RESEARCH.md
@.planning/phases/05-mvp-deployment/05-VALIDATION.md
@.planning/phases/04-matrix-mvp-shared-agent-context-and-context-management-comma/04-02-SUMMARY.md
@adapter/matrix/handlers/__init__.py
@adapter/matrix/handlers/context_commands.py
@adapter/matrix/routed_platform.py
@tests/adapter/matrix/test_context_commands.py
@tests/adapter/matrix/test_routed_platform.py
From `adapter/matrix/handlers/__init__.py`:
```python
dispatcher.register(
IncomingCommand,
"reset",
make_handle_reset(store, prototype_state)
if prototype_state is not None
else handle_settings,
)
```
From `adapter/matrix/handlers/context_commands.py`:
```python
async def _resolve_context_scope(
event: IncomingCommand,
store: StateStore,
chat_mgr,
) -> tuple[str, str | None]: ...
```
From `adapter/matrix/routed_platform.py`:
```python
async def _resolve_delegate(self, user_id: str, local_chat_id: str) -> tuple[PlatformClient, str]:
...
```
Task 1: Expand room-local context and clear-command tests
tests/adapter/matrix/test_context_commands.py, tests/adapter/matrix/test_routed_platform.py
tests/adapter/matrix/test_context_commands.py, tests/adapter/matrix/test_routed_platform.py, adapter/matrix/handlers/__init__.py, adapter/matrix/handlers/context_commands.py, adapter/matrix/routed_platform.py, .planning/phases/05-mvp-deployment/05-VALIDATION.md
- Test 1: `!clear` rotates only the current room's `platform_chat_id` and disconnects only the old upstream chat (per PH05-02).
- Test 2: `!clear` is the supported command name; `!reset` may remain as a compatibility alias but must not be the only registered path.
- Test 3: routed send/stream paths fail fast when room metadata lacks `agent_id` or `platform_chat_id` instead of silently sharing context.
- Test 4: routed behavior uses startup-repaired room metadata and does not introduce a second fallback path that invents `platform_chat_id` during message handling.
- Tests explicitly mention `clear` in command registration or command invocation.
- The context-command tests assert old and new `platform_chat_id` values and upstream disconnect behavior.
- The routed-platform tests assert room-local IDs are passed to delegates unchanged.
Extend the current Matrix context-command and routed-platform regressions so Phase 05 has direct coverage for `!clear`, room-local `platform_chat_id` rotation, and fail-fast routing when room bindings are incomplete. Treat startup reconciliation from `05-01` as the only supported repair path for legacy rooms missing `platform_chat_id`; the routed path must consume repaired metadata, not synthesize new room identities on demand. Preserve the Phase 04 prototype-state behavior where it still fits, but anchor new checks on per-room context isolation rather than shared session assumptions.
pytest tests/adapter/matrix/test_context_commands.py tests/adapter/matrix/test_routed_platform.py -v
The tests define the Phase 05 room-local contract for reset/clear and for routed upstream calls.
Task 2: Ship real room-local `!clear` semantics and strict routing
adapter/matrix/handlers/__init__.py, adapter/matrix/handlers/context_commands.py, adapter/matrix/routed_platform.py
adapter/matrix/handlers/__init__.py, adapter/matrix/handlers/context_commands.py, adapter/matrix/routed_platform.py, tests/adapter/matrix/test_context_commands.py, tests/adapter/matrix/test_routed_platform.py, .planning/phases/05-mvp-deployment/05-RESEARCH.md
- Test 1: command registration exposes `!clear` as the real context-reset entrypoint for Matrix rooms.
- Test 2: only the active room's `platform_chat_id` rotates, and only the old upstream chat session is disconnected.
- Test 3: all room-local context commands resolve through recovered room metadata instead of falling back to shared user scope.
- Test 4: strict routing stays strict at runtime because legacy-room repair was already handled at startup by `05-01`, not by hidden message-path fallbacks.
- `adapter/matrix/handlers/__init__.py` registers `clear`; if `reset` remains, it is clearly a compatibility alias.
- `adapter/matrix/handlers/context_commands.py` resolves and rotates room-local platform context without touching other rooms.
- `adapter/matrix/routed_platform.py` keeps explicit `MATRIX_ROUTE_INCOMPLETE` behavior when bindings are missing.
Update the Matrix context command surface to match the Phase 05 contract: real `!clear`, room-local `platform_chat_id` rotation, and upstream disconnect scoped to the old room context. Keep `save`, `load`, and `context` anchored to the same room-local identity. Tighten routed-platform behavior only where needed to preserve fail-fast semantics after startup reconciliation has repaired legacy rooms; do not reintroduce shared chat state, user-level reset behavior, or message-time backfill of missing `platform_chat_id`.
pytest tests/adapter/matrix/test_context_commands.py tests/adapter/matrix/test_routed_platform.py tests/adapter/matrix/test_dispatcher.py -v
Users can clear one working room without affecting others, and all routed upstream calls stay bound to room-local platform context.
Run the context and routed-platform slices plus Matrix dispatcher smoke coverage to confirm the exposed command name and room-local routing behavior are consistent.
Every working Matrix room has an independent upstream context boundary, and `!clear` resets only the room where it is invoked.