|
| 1 | +# Workspaces Drawer Behavior Algorithm |
| 2 | + |
| 3 | +This document locks in the intended behavior for Workspaces drawer actions. |
| 4 | + |
| 5 | +## Goals |
| 6 | + |
| 7 | +- Keep action semantics explicit and predictable. |
| 8 | +- Keep button visibility state-driven and mutually exclusive. |
| 9 | +- Preserve workspace restore behavior by persisting state in IndexedDB. |
| 10 | + |
| 11 | +## Core Terms |
| 12 | + |
| 13 | +- `Local` scope: Workspaces whose `workspaceScope` is `local`. |
| 14 | +- `Repository` scope: Workspaces whose `workspaceScope` is `repository` and whose `repo` matches the selected repository filter. |
| 15 | +- `workspaceKey`: Derived identity key from repository + head branch. Used for matching/preference logic, not for UI policy by itself. |
| 16 | + |
| 17 | +## Required Invariants |
| 18 | + |
| 19 | +1. `Initialize` and `New workspace` must never be visible at the same time. |
| 20 | +2. `Local` scope never shows `Initialize`. |
| 21 | +3. `Initialize` for non-Local empty scope updates the active workspace in place (no fork). |
| 22 | +4. `New workspace` always forks from current editor/runtime state into a new record id. |
| 23 | +5. Fork creation must generate a fresh head branch suffix so `workspaceKey` and visible labels are distinct. |
| 24 | +6. Any workspace created via `New workspace` must persist with `prContextState = "inactive"`. |
| 25 | +7. For `New workspace`, `workspaceScope` is target-dependent: |
| 26 | + - `local` when repository filter is `__local__` |
| 27 | + - `repository` when repository filter is a non-local `owner/repo` |
| 28 | + |
| 29 | +## UI State Machine |
| 30 | + |
| 31 | +State is derived from: |
| 32 | + |
| 33 | +- Selected repository filter (`__local__` vs non-local `owner/repo`) |
| 34 | +- Presence of stored workspaces in the selected scope |
| 35 | + |
| 36 | +States: |
| 37 | + |
| 38 | +1. `local-empty` |
| 39 | + - Show: `New workspace` |
| 40 | + - Hide: `Initialize`, workspace select, `Open`, `Remove` |
| 41 | +2. `local-with-workspaces` |
| 42 | + - Show: `New workspace`, workspace select, `Open`, `Remove` |
| 43 | + - Hide: `Initialize` |
| 44 | +3. `repository-empty` |
| 45 | + - Show: `Initialize` |
| 46 | + - Hide: `New workspace`, workspace select, `Open`, `Remove` |
| 47 | +4. `repository-with-workspaces` |
| 48 | + - Show: `New workspace`, workspace select, `Open`, `Remove` |
| 49 | + - Hide: `Initialize` |
| 50 | + |
| 51 | +## Action Semantics |
| 52 | + |
| 53 | +### A) Local + New workspace |
| 54 | + |
| 55 | +- Action: fork current workspace into a new record. |
| 56 | +- Persisted updates: |
| 57 | + - `workspaceScope = "local"` |
| 58 | + - `prContextState = "inactive"` |
| 59 | + - `repo = ""` |
| 60 | + - fresh `id` |
| 61 | + - fresh local `head` (suffix-appended) |
| 62 | + - `workspaceKey = local::<fresh-head>` |
| 63 | + |
| 64 | +### B) Non-Local + Initialize (no stored workspaces in selected repository) |
| 65 | + |
| 66 | +- Action: update active workspace in place to selected repository scope. |
| 67 | +- Persisted updates on current record: |
| 68 | + - `workspaceScope = "repository"` |
| 69 | + - `repo = <selected owner/repo>` |
| 70 | + - `workspaceKey = <selected owner/repo>::<current head>` |
| 71 | +- Must preserve current record id. |
| 72 | + |
| 73 | +### C) Non-Local + New workspace (stored workspaces exist) |
| 74 | + |
| 75 | +- Action: fork current workspace into a new repository-scoped record. |
| 76 | +- Persisted updates: |
| 77 | + - `workspaceScope = "repository"` |
| 78 | + - `prContextState = "inactive"` |
| 79 | + - `repo = <selected owner/repo>` |
| 80 | + - fresh `id` |
| 81 | + - fresh repository `head` (suffix-appended from current head) |
| 82 | + - `workspaceKey = <selected owner/repo>::<head>` |
| 83 | + |
| 84 | +## Storage Notes |
| 85 | + |
| 86 | +- Canonical workflow state lives in IndexedDB (`prWorkspaces` records). |
| 87 | +- `localStorage` must not own repository/workspace workflow state. |
| 88 | + |
| 89 | +## Regression Coverage Expectations |
| 90 | + |
| 91 | +At minimum, tests should verify: |
| 92 | + |
| 93 | +1. Local `New workspace` creates a new record and distinct local label/key. |
| 94 | +2. Non-local empty scope shows only `Initialize` and updates active record in place. |
| 95 | +3. Non-local scope with records shows only `New workspace` and forks new record. |
| 96 | +4. `Initialize` and `New workspace` never coexist. |
0 commit comments