-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathworkspace_context.py
More file actions
242 lines (213 loc) · 8.04 KB
/
Copy pathworkspace_context.py
File metadata and controls
242 lines (213 loc) · 8.04 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
"""Workspace determination ceremony — single orchestrator for shared maps."""
from __future__ import annotations
import os
import sqlite3
from dataclasses import dataclass, replace
from typing import Any
from models import Bubble
from services.workspace_db import (
COMPOSER_ROWS_WITH_HEADERS_SQL,
build_composer_id_to_workspace_id,
build_composer_id_to_workspace_id_cached,
collect_invalid_workspace_ids,
collect_workspace_entries,
global_storage_db_path,
load_bubble_map,
load_project_layouts_map,
safe_fetchall,
)
from services.workspace_resolver import (
create_project_name_to_workspace_id_map,
create_workspace_path_to_id_map,
infer_invalid_workspace_aliases,
)
@dataclass(frozen=True)
class WorkspaceContext:
"""Precomputed workspace-resolution maps for conversation assignment."""
workspace_entries: list[dict[str, Any]]
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[str]]
bubble_map: dict[str, Bubble]
invalid_workspace_aliases: dict[str, str] | None = None
def _entries(
workspace_path: str,
workspace_entries: list[dict[str, Any]] | None,
) -> list[dict[str, Any]]:
if workspace_entries is not None:
return workspace_entries
return collect_workspace_entries(workspace_path)
def _assemble_context(
entries: list[dict[str, Any]],
*,
invalid_workspace_ids: set[str],
workspace_path_to_id: dict[str, str],
composer_id_to_workspace_id: dict[str, str],
) -> WorkspaceContext:
return WorkspaceContext(
workspace_entries=entries,
invalid_workspace_ids=invalid_workspace_ids,
composer_id_to_workspace_id=composer_id_to_workspace_id,
project_name_to_workspace_id=create_project_name_to_workspace_id_map(entries),
workspace_path_to_id=workspace_path_to_id,
project_layouts_map={},
bubble_map={},
)
def resolve_workspace_context(
workspace_path: str,
*,
workspace_entries: list[dict[str, Any]] | None = None,
) -> WorkspaceContext:
"""Full workspace maps with an uncached composer→workspace scan (CLI export)."""
entries = _entries(workspace_path, workspace_entries)
return _assemble_context(
entries,
invalid_workspace_ids=collect_invalid_workspace_ids(entries),
workspace_path_to_id=create_workspace_path_to_id_map(entries),
composer_id_to_workspace_id=build_composer_id_to_workspace_id(
workspace_path, entries,
),
)
def resolve_workspace_context_cached(
workspace_path: str,
rules: list[Any],
*,
workspace_entries: list[dict[str, Any]] | None = None,
nocache: bool = False,
) -> WorkspaceContext:
"""Full workspace maps with a mtime-keyed composer map (listing / tabs)."""
entries = _entries(workspace_path, workspace_entries)
return _assemble_context(
entries,
invalid_workspace_ids=collect_invalid_workspace_ids(entries),
workspace_path_to_id=create_workspace_path_to_id_map(entries),
composer_id_to_workspace_id=build_composer_id_to_workspace_id_cached(
workspace_path, entries, rules, nocache=nocache,
),
)
def resolve_workspace_context_minimal(
workspace_path: str,
*,
workspace_entries: list[dict[str, Any]] | None = None,
) -> WorkspaceContext:
"""Entries, project-name, and composer maps only (HTTP export).
Args:
workspace_path: Cursor ``workspaceStorage`` root.
workspace_entries: Pre-collected entries; when ``None``, scanned from disk.
"""
entries = _entries(workspace_path, workspace_entries)
return _assemble_context(
entries,
invalid_workspace_ids=set(),
workspace_path_to_id={},
composer_id_to_workspace_id=build_composer_id_to_workspace_id(
workspace_path, entries,
),
)
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[str, Any] = {}
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)
def resolve_invalid_workspace_aliases_cached(
ctx: WorkspaceContext,
global_db: sqlite3.Connection,
workspace_path: str,
rules: list[Any],
*,
nocache: bool = False,
project_layouts_map: dict[str, list[str]] | None = None,
) -> dict[str, str]:
"""Return invalid-workspace alias map, using the summary-cache fingerprint.
Computes ``infer_invalid_workspace_aliases`` at most once per storage
fingerprint (same mtime key as composer-map / tab-summary caches). When
*ctx* already carries a populated ``invalid_workspace_aliases`` field,
that value is returned without touching disk or the global DB roster.
Args:
ctx: Workspace maps from :func:`resolve_workspace_context_cached`.
global_db: Open global ``state.vscdb`` connection.
workspace_path: Cursor ``workspaceStorage`` root.
rules: Exclusion rule token lists (fingerprint input).
nocache: When ``True``, bypass disk cache reads and writes.
project_layouts_map: Pre-loaded layouts; loaded from *global_db* when
``None``.
Returns:
``{invalid_id: replacement_id}``, or ``{}`` when every workspace is valid.
"""
if ctx.invalid_workspace_aliases is not None:
return ctx.invalid_workspace_aliases
if not ctx.invalid_workspace_ids:
return {}
from services.summary_cache import (
fingerprint_workspace_storage,
get_cached_invalid_workspace_aliases,
nocache_enabled,
set_cached_invalid_workspace_aliases,
)
from utils.workspace_path import get_cli_chats_path
gdb = global_storage_db_path(workspace_path)
cli_path = get_cli_chats_path()
fingerprint = fingerprint_workspace_storage(
workspace_path,
ctx.workspace_entries,
global_db_path=gdb if os.path.isfile(gdb) else None,
rules=rules,
cli_chats_path=cli_path if os.path.isdir(cli_path) else None,
)
if not nocache_enabled(request_nocache=nocache):
cached = get_cached_invalid_workspace_aliases(fingerprint)
if cached is not None:
return cached
layouts = (
project_layouts_map
if project_layouts_map is not None
else load_project_layouts_map(global_db)
)
composer_rows = safe_fetchall(global_db, COMPOSER_ROWS_WITH_HEADERS_SQL)
aliases = infer_invalid_workspace_aliases(
composer_rows=composer_rows,
project_layouts_map=layouts,
project_name_map=ctx.project_name_to_workspace_id,
workspace_path_map=ctx.workspace_path_to_id,
workspace_entries=ctx.workspace_entries,
bubble_map={},
composer_id_to_ws=ctx.composer_id_to_workspace_id,
invalid_workspace_ids=ctx.invalid_workspace_ids,
)
if not nocache_enabled(request_nocache=nocache):
set_cached_invalid_workspace_aliases(fingerprint, aliases)
return aliases
def with_invalid_workspace_aliases(
ctx: WorkspaceContext,
global_db: sqlite3.Connection,
workspace_path: str,
rules: list[Any],
*,
nocache: bool = False,
project_layouts_map: dict[str, list[str]] | None = None,
) -> WorkspaceContext:
"""Return *ctx* with ``invalid_workspace_aliases`` populated from cache."""
if ctx.invalid_workspace_aliases is not None:
return ctx
aliases = resolve_invalid_workspace_aliases_cached(
ctx,
global_db,
workspace_path,
rules,
nocache=nocache,
project_layouts_map=project_layouts_map,
)
return replace(ctx, invalid_workspace_aliases=aliases)