|
| 1 | +"""Workspace determination ceremony — single orchestrator for shared maps.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from dataclasses import dataclass, replace |
| 6 | +from typing import TYPE_CHECKING |
| 7 | + |
| 8 | +from services.workspace_db import ( |
| 9 | + build_composer_id_to_workspace_id, |
| 10 | + build_composer_id_to_workspace_id_cached, |
| 11 | + collect_invalid_workspace_ids, |
| 12 | + collect_workspace_entries, |
| 13 | + load_bubble_map, |
| 14 | + load_project_layouts_map, |
| 15 | +) |
| 16 | +from services.workspace_resolver import ( |
| 17 | + create_project_name_to_workspace_id_map, |
| 18 | + create_workspace_path_to_id_map, |
| 19 | +) |
| 20 | + |
| 21 | +if TYPE_CHECKING: |
| 22 | + import sqlite3 |
| 23 | + |
| 24 | + |
| 25 | +@dataclass(frozen=True) |
| 26 | +class WorkspaceContext: |
| 27 | + """Precomputed workspace-resolution maps for conversation assignment.""" |
| 28 | + |
| 29 | + workspace_path: str |
| 30 | + workspace_entries: list[dict] |
| 31 | + invalid_workspace_ids: set[str] |
| 32 | + composer_id_to_workspace_id: dict[str, str] |
| 33 | + project_name_to_workspace_id: dict[str, str] |
| 34 | + workspace_path_to_id: dict[str, str] |
| 35 | + project_layouts_map: dict[str, list] |
| 36 | + bubble_map: dict[str, dict] |
| 37 | + |
| 38 | + |
| 39 | +def resolve_workspace_context( |
| 40 | + workspace_path: str, |
| 41 | + *, |
| 42 | + workspace_entries: list[dict] | None = None, |
| 43 | + rules: list | None = None, |
| 44 | + nocache: bool = False, |
| 45 | + use_composer_cache: bool = False, |
| 46 | + include_invalid_workspace_ids: bool = True, |
| 47 | + include_workspace_path_map: bool = True, |
| 48 | + global_db: sqlite3.Connection | None = None, |
| 49 | + populate_project_layouts: bool = False, |
| 50 | + populate_bubble_map: bool = False, |
| 51 | +) -> WorkspaceContext: |
| 52 | + """Run the workspace-determination ceremony and return a typed context. |
| 53 | +
|
| 54 | + Always resolves ``workspace_entries`` (when not supplied), composer and |
| 55 | + project-name maps. Optional pieces are controlled by flags so lightweight |
| 56 | + consumers (e.g. HTTP export) can omit unused maps. |
| 57 | +
|
| 58 | + Args: |
| 59 | + workspace_path: Cursor ``workspaceStorage`` root. |
| 60 | + workspace_entries: Pre-collected entries; when ``None``, scanned from disk. |
| 61 | + rules: Exclusion rules; required when ``use_composer_cache`` is ``True``. |
| 62 | + nocache: Skip the mtime-keyed composer-map disk cache. |
| 63 | + use_composer_cache: Use :func:`build_composer_id_to_workspace_id_cached`. |
| 64 | + include_invalid_workspace_ids: When ``False``, ``invalid_workspace_ids`` is empty. |
| 65 | + include_workspace_path_map: When ``False``, ``workspace_path_to_id`` is empty. |
| 66 | + global_db: Open global ``state.vscdb`` connection for optional KV loads. |
| 67 | + populate_project_layouts: Populate ``project_layouts_map`` from *global_db*. |
| 68 | + populate_bubble_map: Populate ``bubble_map`` from *global_db*. |
| 69 | +
|
| 70 | + Returns: |
| 71 | + :class:`WorkspaceContext` with all requested maps populated. |
| 72 | + """ |
| 73 | + entries = ( |
| 74 | + workspace_entries |
| 75 | + if workspace_entries is not None |
| 76 | + else collect_workspace_entries(workspace_path) |
| 77 | + ) |
| 78 | + invalid_ids = ( |
| 79 | + collect_invalid_workspace_ids(entries) |
| 80 | + if include_invalid_workspace_ids |
| 81 | + else set() |
| 82 | + ) |
| 83 | + project_name_map = create_project_name_to_workspace_id_map(entries) |
| 84 | + workspace_path_map = ( |
| 85 | + create_workspace_path_to_id_map(entries) |
| 86 | + if include_workspace_path_map |
| 87 | + else {} |
| 88 | + ) |
| 89 | + if use_composer_cache: |
| 90 | + if rules is None: |
| 91 | + raise ValueError("rules is required when use_composer_cache=True") |
| 92 | + composer_id_to_ws = build_composer_id_to_workspace_id_cached( |
| 93 | + workspace_path, entries, rules, nocache=nocache, |
| 94 | + ) |
| 95 | + else: |
| 96 | + composer_id_to_ws = build_composer_id_to_workspace_id(workspace_path, entries) |
| 97 | + |
| 98 | + project_layouts: dict[str, list] = {} |
| 99 | + bubble_map: dict[str, dict] = {} |
| 100 | + if global_db is not None: |
| 101 | + if populate_project_layouts: |
| 102 | + project_layouts = load_project_layouts_map(global_db) |
| 103 | + if populate_bubble_map: |
| 104 | + bubble_map = load_bubble_map(global_db) |
| 105 | + |
| 106 | + return WorkspaceContext( |
| 107 | + workspace_path=workspace_path, |
| 108 | + workspace_entries=entries, |
| 109 | + invalid_workspace_ids=invalid_ids, |
| 110 | + composer_id_to_workspace_id=composer_id_to_ws, |
| 111 | + project_name_to_workspace_id=project_name_map, |
| 112 | + workspace_path_to_id=workspace_path_map, |
| 113 | + project_layouts_map=project_layouts, |
| 114 | + bubble_map=bubble_map, |
| 115 | + ) |
| 116 | + |
| 117 | + |
| 118 | +def enrich_workspace_context_from_global_db( |
| 119 | + ctx: WorkspaceContext, |
| 120 | + global_db: sqlite3.Connection, |
| 121 | + *, |
| 122 | + populate_project_layouts: bool = False, |
| 123 | + populate_bubble_map: bool = False, |
| 124 | +) -> WorkspaceContext: |
| 125 | + """Return *ctx* with global KV maps loaded from an open global DB connection.""" |
| 126 | + updates: dict = {} |
| 127 | + if populate_project_layouts: |
| 128 | + updates["project_layouts_map"] = load_project_layouts_map(global_db) |
| 129 | + if populate_bubble_map: |
| 130 | + updates["bubble_map"] = load_bubble_map(global_db) |
| 131 | + if not updates: |
| 132 | + return ctx |
| 133 | + return replace(ctx, **updates) |
0 commit comments