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