-
Notifications
You must be signed in to change notification settings - Fork 202
Expand file tree
/
Copy pathproject_context.py
More file actions
1594 lines (1347 loc) · 62.9 KB
/
project_context.py
File metadata and controls
1594 lines (1347 loc) · 62.9 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Project context utilities for Basic Memory MCP server.
Provides project lookup utilities for MCP tools.
Handles project validation and context management in one place.
Note: This module uses ProjectResolver for unified project resolution.
The resolve_project_parameter function is a thin wrapper for backwards
compatibility with existing MCP tools.
"""
import asyncio
from contextlib import asynccontextmanager, nullcontext
from dataclasses import dataclass, field
from typing import AsyncIterator, Awaitable, Callable, List, Optional, Sequence, Tuple, cast
from uuid import UUID
from httpx import AsyncClient
from httpx._types import (
HeaderTypes,
)
from loguru import logger
from fastmcp import Context
from mcp.server.fastmcp.exceptions import ToolError
import logfire
from basic_memory.config import BasicMemoryConfig, ConfigManager, ProjectMode, has_cloud_credentials
from basic_memory.project_resolver import ProjectResolver
from basic_memory.schemas.cloud import (
WorkspaceInfo,
WorkspaceListResponse,
format_workspace_choices,
format_workspace_selection_choices,
workspace_matches_exact_identifier,
workspace_matches_identifier,
)
from basic_memory.schemas.project_info import ProjectItem, ProjectList
from basic_memory.schemas.v2 import ProjectResolveResponse
from basic_memory.schemas.memory import memory_url_path
from basic_memory.utils import (
build_qualified_permalink_reference,
generate_permalink,
normalize_project_reference,
)
from basic_memory.workspace_context import (
current_workspace_permalink_context,
workspace_permalink_context,
)
# --- Workspace provider injection ---
# Mirrors the set_client_factory() pattern in async_client.py.
# The cloud MCP server sets a provider that queries its own database directly,
# avoiding the control-plane HTTP round-trip that requires local credentials.
_workspace_provider: Optional[Callable[[], Awaitable[list[WorkspaceInfo]]]] = None
_WORKSPACE_PROJECT_INDEX_STATE_KEY = "workspace_project_index"
@dataclass(frozen=True)
class WorkspaceProjectEntry:
"""A cloud project resolved together with the workspace that owns it."""
workspace: WorkspaceInfo
project: ProjectItem
@property
def qualified_name(self) -> str:
return f"{self.workspace.slug}/{self.project.permalink}"
@dataclass(frozen=True)
class WorkspaceProjectIndex:
"""Session-local cloud project lookup index keyed by project permalink and external_id."""
workspaces: tuple[WorkspaceInfo, ...]
entries: tuple[WorkspaceProjectEntry, ...]
entries_by_permalink: dict[str, tuple[WorkspaceProjectEntry, ...]]
entries_by_external_id: dict[str, WorkspaceProjectEntry] = field(default_factory=dict)
failed_workspaces: tuple[WorkspaceInfo, ...] = ()
@dataclass(frozen=True)
class WorkspaceMemoryUrlResolution:
"""Resolved workspace/project route for a workspace-qualified memory URL."""
entry: WorkspaceProjectEntry
canonical_path: str
@property
def project_identifier(self) -> str:
return self.entry.qualified_name
def set_workspace_provider(provider: Callable[[], Awaitable[list[WorkspaceInfo]]]) -> None:
"""Override workspace discovery (for cloud app, testing, etc)."""
global _workspace_provider
_workspace_provider = provider
async def _resolve_default_project_from_api() -> Optional[str]:
"""Query the projects API for the default project.
Used as a fallback when ConfigManager has no local config (cloud mode).
"""
from basic_memory.mcp.async_client import get_client
try:
async with get_client() as client:
response = await client.get("/v2/projects/")
if response.status_code == 200:
project_list = ProjectList.model_validate(response.json())
if project_list.default_project:
return project_list.default_project
# Fallback: find project with is_default=True
for p in project_list.projects:
if p.is_default:
return p.name
except Exception:
pass
return None
async def _get_cached_active_project(context: Optional[Context]) -> Optional[ProjectItem]:
"""Return the cached active project from context when available."""
if not context:
return None
cached_raw = await context.get_state("active_project")
if isinstance(cached_raw, dict):
return ProjectItem.model_validate(cached_raw)
return None
async def _set_cached_active_project(
context: Optional[Context],
active_project: ProjectItem,
) -> None:
"""Persist the active project and known default-project metadata in context."""
if not context:
return
await context.set_state("active_project", active_project.model_dump())
if active_project.is_default:
await context.set_state("default_project_name", active_project.name)
async def _clear_cached_active_project(context: Optional[Context]) -> None:
"""Clear cached project metadata that may no longer match the active route."""
if not context:
return
await context.set_state("active_project", None)
await context.set_state("default_project_name", None)
async def _get_cached_active_workspace(context: Optional[Context]) -> Optional[WorkspaceInfo]:
"""Return the cached active workspace from context when available."""
if not context:
return None
cached_raw = await context.get_state("active_workspace")
if isinstance(cached_raw, dict):
return WorkspaceInfo.model_validate(cached_raw)
return None
async def _set_cached_active_workspace(
context: Optional[Context],
active_workspace: WorkspaceInfo,
) -> None:
"""Persist workspace context and clear project cache when the tenant changes."""
if not context:
return
cached_workspace = await _get_cached_active_workspace(context)
if cached_workspace and cached_workspace.tenant_id != active_workspace.tenant_id:
# Trigger: project routing moved to another workspace
# Why: project names are only unique inside one workspace, so a cached
# ProjectItem from the previous tenant can point at the wrong project
# Outcome: force the next validation call to resolve within the new tenant
await _clear_cached_active_project(context)
await context.set_state("active_workspace", active_workspace.model_dump())
async def _clear_cached_active_workspace_for_local_route(context: Optional[Context]) -> None:
"""Drop tenant workspace metadata before routing through a local project."""
if not context:
return
# Trigger: local routing follows a cloud route in the same MCP session
# Why: active_workspace is tenant metadata, not part of local project identity
# Outcome: memory:// resolution uses project-only local permalinks
await context.set_state("active_workspace", None)
async def _get_cached_default_project(context: Optional[Context]) -> Optional[str]:
"""Return the cached default project name from context when available."""
if not context:
return None
cached_default = await context.get_state("default_project_name")
if isinstance(cached_default, str):
return cached_default
return None
def _canonicalize_project_name(
project_name: Optional[str],
config: BasicMemoryConfig,
) -> Optional[str]:
"""Return the configured project name when the identifier matches by permalink.
Project routing happens before API validation, so we normalize explicit inputs
here to keep local/cloud routing aligned with the database's case-insensitive
project resolver.
"""
if project_name is None:
return None
requested_permalink = generate_permalink(project_name)
for configured_name in config.projects:
if generate_permalink(configured_name) == requested_permalink:
return configured_name
return project_name
def _project_matches_identifier(project_item: ProjectItem, identifier: Optional[str]) -> bool:
"""Return True when the identifier refers to the cached project."""
if identifier is None:
return True
normalized_identifier = generate_permalink(identifier)
return normalized_identifier in {
generate_permalink(project_item.name),
project_item.permalink,
}
async def resolve_project_parameter(
project: Optional[str] = None,
allow_discovery: bool = False,
default_project: Optional[str] = None,
context: Optional[Context] = None,
) -> Optional[str]:
"""Resolve project parameter using unified linear priority chain.
This is a thin wrapper around ProjectResolver for backwards compatibility.
New code should consider using ProjectResolver directly for more detailed
resolution information.
Resolution order:
1. ENV_CONSTRAINT: BASIC_MEMORY_MCP_PROJECT env var (highest priority)
2. EXPLICIT: project parameter passed directly
3. DEFAULT: default_project from config (if set)
4. Fallback: discovery (if allowed) → NONE
Args:
project: Optional explicit project parameter
allow_discovery: If True, allows returning None for discovery mode
(used by tools like recent_activity that can operate across all projects)
default_project: Optional explicit default project. If not provided, reads from ConfigManager.
Returns:
Resolved project name or None if no resolution possible
"""
with logfire.span(
"routing.resolve_project",
requested_project=project,
allow_discovery=allow_discovery,
):
config = ConfigManager().config
# Trigger: project already resolved earlier in the same MCP request
# Why: the active project is request-constant, so re-discovering the
# default project via /v2/projects/ just repeats work
# Outcome: reuse the cached project name as the explicit candidate
if project is None:
cached_project = await _get_cached_active_project(context)
if cached_project is not None:
project = cached_project.name
# Trigger: there is no explicit project after env/context normalization
# Why: default-project discovery is only needed as a fallback; doing it
# for explicit requests adds an avoidable /v2/projects/ round-trip
# Outcome: skip default lookup when the active project is already known
if default_project is None and project is None:
# Load config for any values not explicitly provided.
# ConfigManager reads from the local config file, which doesn't exist in cloud mode.
# When it returns None, fall back to querying the projects API for the is_default flag.
default_project = config.default_project
if default_project is None:
default_project = await _get_cached_default_project(context)
if default_project is None:
default_project = await _resolve_default_project_from_api()
if default_project and context:
await context.set_state("default_project_name", default_project)
# Create resolver with configuration and resolve
resolver = ProjectResolver.from_env(
default_project=default_project,
)
result = resolver.resolve(project=project, allow_discovery=allow_discovery)
return _canonicalize_project_name(result.project, config)
async def get_project_names(client: AsyncClient, headers: HeaderTypes | None = None) -> List[str]:
# Deferred import to avoid circular dependency with tools
from basic_memory.mcp.tools.utils import call_get
response = await call_get(client, "/v2/projects/", headers=headers)
project_list = ProjectList.model_validate(response.json())
return [project.name for project in project_list.projects]
def _workspace_project_index_from_state(raw: object) -> WorkspaceProjectIndex | None:
"""Deserialize a cached workspace project index from MCP context state."""
if not isinstance(raw, dict):
return None
raw_mapping = cast(dict[str, object], raw)
workspaces_raw = raw_mapping.get("workspaces")
entries_raw = raw_mapping.get("entries")
if not isinstance(workspaces_raw, list) or not isinstance(entries_raw, list):
return None
workspaces = tuple(WorkspaceInfo.model_validate(item) for item in workspaces_raw)
failed_workspaces_raw = raw_mapping.get("failed_workspaces")
failed_workspaces = (
tuple(WorkspaceInfo.model_validate(item) for item in failed_workspaces_raw)
if isinstance(failed_workspaces_raw, list)
else ()
)
entries_list: list[WorkspaceProjectEntry] = []
for item in entries_raw:
if not isinstance(item, dict):
continue
item_mapping = cast(dict[str, object], item)
workspace_raw = item_mapping.get("workspace")
project_raw = item_mapping.get("project")
if workspace_raw is None or project_raw is None:
continue
entries_list.append(
WorkspaceProjectEntry(
workspace=WorkspaceInfo.model_validate(workspace_raw),
project=ProjectItem.model_validate(project_raw),
)
)
entries = tuple(entries_list)
return _build_workspace_project_index(
workspaces,
entries,
failed_workspaces=failed_workspaces,
)
def _workspace_project_index_to_state(index: WorkspaceProjectIndex) -> dict:
"""Serialize a workspace project index for MCP context state."""
return {
"workspaces": [workspace.model_dump() for workspace in index.workspaces],
"failed_workspaces": [workspace.model_dump() for workspace in index.failed_workspaces],
"entries": [
{
"workspace": entry.workspace.model_dump(),
"project": entry.project.model_dump(),
}
for entry in index.entries
],
}
def _build_workspace_project_index(
workspaces: tuple[WorkspaceInfo, ...],
entries: tuple[WorkspaceProjectEntry, ...],
*,
failed_workspaces: tuple[WorkspaceInfo, ...] = (),
) -> WorkspaceProjectIndex:
"""Build the permalink and external_id lookup tables for workspace-project entries."""
grouped: dict[str, list[WorkspaceProjectEntry]] = {}
by_external_id: dict[str, WorkspaceProjectEntry] = {}
for entry in entries:
grouped.setdefault(entry.project.permalink, []).append(entry)
by_external_id[entry.project.external_id] = entry
return WorkspaceProjectIndex(
workspaces=workspaces,
entries=entries,
entries_by_permalink={
permalink: tuple(items)
for permalink, items in sorted(grouped.items(), key=lambda item: item[0])
},
entries_by_external_id=by_external_id,
failed_workspaces=failed_workspaces,
)
def _split_qualified_project_identifier(identifier: str) -> tuple[str | None, str]:
"""Split ``<workspace-slug>/<project>`` identifiers for cloud routing."""
cleaned = identifier.strip()
if "/" not in cleaned:
return None, cleaned
workspace_slug, project_identifier = cleaned.split("/", 1)
if not workspace_slug or not project_identifier:
return None, cleaned
return workspace_slug, project_identifier
def _unqualified_project_identifier(identifier: str) -> str:
"""Return the project segment from an optional qualified project identifier."""
_, project_identifier = _split_qualified_project_identifier(identifier)
return project_identifier
def _identifier_path(identifier: str) -> str:
"""Return the routable path portion of a raw identifier or memory URL."""
stripped = identifier.strip()
return memory_url_path(stripped) if stripped.startswith("memory://") else stripped
def _split_workspace_identifier_segments(identifier: str) -> tuple[str, str, str] | None:
"""Split ``<workspace>/<project>/<path>`` identifiers into route segments."""
normalized = normalize_project_reference(_identifier_path(identifier)).strip("/")
parts = normalized.split("/", 2)
if len(parts) != 3:
# Trigger: two-segment identifiers such as `workspace/project` or `project/path`.
# Why: without a remainder, the shape is ambiguous with existing project-prefix routing.
# Outcome: fall through so the normal project-prefix/default-project resolver decides.
return None
workspace_slug, project_identifier, remainder = parts
if not workspace_slug or not project_identifier or not remainder:
return None
return workspace_slug, project_identifier, remainder
def _split_workspace_memory_url_segments(identifier: str) -> tuple[str, str, str] | None:
"""Split ``memory://<workspace>/<project>/<path>`` into route segments."""
if not identifier.strip().startswith("memory://"):
return None
return _split_workspace_identifier_segments(identifier)
def _canonical_memory_path_for_workspace(
*,
workspace_slug: str,
workspace_type: str,
project_permalink: str,
remainder: str,
) -> str:
"""Return the stored canonical path for a workspace-qualified memory URL."""
normalized_remainder = remainder.strip("/")
if workspace_type not in {"organization", "personal"}:
raise ValueError(f"Unsupported workspace_type for memory URL routing: {workspace_type}")
# Trigger: a caller supplied a workspace-qualified memory URL.
# Why: the first two path segments are the global route, even for Personal.
# Outcome: lookups preserve the complete workspace/project canonical permalink.
if not normalized_remainder:
normalized_remainder = project_permalink
return build_qualified_permalink_reference(
project_permalink,
normalized_remainder,
include_project=True,
workspace_permalink=workspace_slug,
)
def _canonical_memory_path_for_active_route(
active_project: ProjectItem,
path: str,
*,
include_project: bool,
cached_workspace: WorkspaceInfo | None = None,
) -> str:
"""Return the canonical permalink path for the currently routed project/workspace."""
project_prefix = active_project.permalink
workspace_remainder = path
if include_project and (path == project_prefix or path.startswith(f"{project_prefix}/")):
# Trigger: the memory URL already names the active project root/prefix
# Why: workspace canonicalization adds the project prefix itself, so
# keeping it in the remainder would produce <workspace>/<project>/<project>
# Outcome: keep project-root and project-prefixed URLs canonical once
workspace_remainder = (
"" if path == project_prefix else path.removeprefix(f"{project_prefix}/")
)
workspace_context = current_workspace_permalink_context()
if workspace_context is not None:
return _canonical_memory_path_for_workspace(
workspace_slug=workspace_context.workspace_slug,
workspace_type=workspace_context.workspace_type,
project_permalink=active_project.permalink,
remainder=workspace_remainder,
)
if cached_workspace is not None:
return _canonical_memory_path_for_workspace(
workspace_slug=cached_workspace.slug,
workspace_type=cached_workspace.workspace_type,
project_permalink=active_project.permalink,
remainder=workspace_remainder,
)
if not include_project:
return path
if path == project_prefix or path.startswith(f"{project_prefix}/"):
return path
return f"{project_prefix}/{path}"
def _cloud_workspace_discovery_available(config: BasicMemoryConfig) -> bool:
"""Return True when workspace discovery can be used without forcing local routing."""
from basic_memory.mcp.async_client import (
_explicit_routing,
_force_local_mode,
is_factory_mode,
)
if _explicit_routing() and _force_local_mode():
return False
# Trigger: local project config is present even though cloud credentials are saved.
# Why: existing local `memory://...` URLs must not depend on workspace discovery.
# Outcome: only factory, explicit cloud, or cloud-only sessions attempt discovery here.
return (
is_factory_mode()
or (_explicit_routing() and not _force_local_mode())
or (not config.projects and has_cloud_credentials(config))
)
def _workspace_identifier_discovery_available(
identifier: str,
config: BasicMemoryConfig,
) -> bool:
"""Return True when an identifier is allowed to consult workspace discovery."""
if _cloud_workspace_discovery_available(config):
return True
from basic_memory.mcp.async_client import (
_explicit_routing,
_force_local_mode,
)
if _explicit_routing() and _force_local_mode():
return False
return (
has_cloud_credentials(config)
and _split_workspace_identifier_segments(identifier) is not None
)
async def resolve_workspace_qualified_memory_url(
identifier: str,
context: Optional[Context] = None,
) -> WorkspaceMemoryUrlResolution | None:
"""Resolve a workspace-qualified memory URL against accessible workspaces."""
segments = _split_workspace_memory_url_segments(identifier)
if segments is None:
return None
return await _resolve_workspace_segments(identifier, segments, context=context)
async def resolve_workspace_qualified_identifier(
identifier: str,
context: Optional[Context] = None,
) -> WorkspaceMemoryUrlResolution | None:
"""Resolve a workspace-qualified permalink or memory URL against accessible workspaces."""
segments = _split_workspace_identifier_segments(identifier)
if segments is None:
return None
return await _resolve_workspace_segments(identifier, segments, context=context)
async def _resolve_workspace_segments(
identifier: str,
segments: tuple[str, str, str],
context: Optional[Context] = None,
) -> WorkspaceMemoryUrlResolution | None:
"""Resolve parsed workspace/project/path segments against accessible workspaces."""
workspace_slug, project_identifier, remainder = segments
index = await _ensure_workspace_project_index(context=context)
workspace = next(
(item for item in index.workspaces if item.slug.casefold() == workspace_slug.casefold()),
None,
)
if workspace is None:
return None
project_permalink = generate_permalink(project_identifier)
matches = [
entry
for entry in index.entries_by_permalink.get(project_permalink, ())
if entry.workspace.tenant_id == workspace.tenant_id
]
if not matches:
if any(
failed_workspace.tenant_id == workspace.tenant_id
for failed_workspace in index.failed_workspaces
):
raise ValueError(
f"Projects for workspace '{workspace.name}' ({workspace.slug}) "
"could not be loaded. Retry after workspace discovery recovers."
)
# Trigger: first segment matches a workspace slug but the second does not
# match a project in that workspace.
# Why: workspace-qualified URLs require both route segments to match; otherwise
# existing project-prefixed URLs like `memory://main/notes/foo` can collide
# with a workspace slug named `main`.
# Outcome: treat this as not workspace-qualified and let the caller use
# the existing project-prefix/default-project resolver.
return None
if len(matches) > 1:
details = ", ".join(
f"{entry.qualified_name} ({entry.project.external_id})" for entry in matches
)
raise ValueError(
f"Project '{project_identifier}' matched multiple projects in workspace "
f"'{workspace.name}' ({workspace.slug}). Project permalinks must be unique. "
f"Matches: {details}"
)
entry = matches[0]
canonical_path = _canonical_memory_path_for_workspace(
workspace_slug=entry.workspace.slug,
workspace_type=entry.workspace.workspace_type,
project_permalink=entry.project.permalink,
remainder=remainder,
)
return WorkspaceMemoryUrlResolution(entry=entry, canonical_path=canonical_path)
def _format_qualified_choices(entries: Sequence[WorkspaceProjectEntry]) -> str:
"""Format qualified project choices for collision errors."""
return " or ".join(entry.qualified_name for entry in entries)
async def get_available_workspaces(context: Optional[Context] = None) -> list[WorkspaceInfo]:
"""Load available cloud workspaces for the current authenticated user."""
if context:
cached_raw = await context.get_state("available_workspaces")
if isinstance(cached_raw, list):
return [WorkspaceInfo.model_validate(item) for item in cached_raw]
# Trigger: workspace provider was injected (e.g., by cloud MCP server)
# Why: the cloud server IS the cloud — it can query its own database
# directly instead of making an HTTP round-trip that requires local credentials
# Outcome: use provider result, cache in context, skip control-plane client
if _workspace_provider is not None:
workspaces = await _workspace_provider()
if context:
await context.set_state(
"available_workspaces",
[ws.model_dump() for ws in workspaces],
)
return workspaces
from basic_memory.mcp.async_client import get_cloud_control_plane_client
from basic_memory.mcp.tools.utils import call_get
async with get_cloud_control_plane_client() as client:
response = await call_get(client, "/workspaces/")
workspace_list = WorkspaceListResponse.model_validate(response.json())
if context:
await context.set_state(
"available_workspaces",
[ws.model_dump() for ws in workspace_list.workspaces],
)
return workspace_list.workspaces
async def invalidate_workspace_project_index(context: Optional[Context] = None) -> None:
"""Invalidate the cached cloud workspace/project lookup index."""
if context:
await context.set_state(_WORKSPACE_PROJECT_INDEX_STATE_KEY, None)
async def _fetch_workspace_project_entries(
workspace: WorkspaceInfo,
context: Optional[Context] = None,
) -> tuple[WorkspaceProjectEntry, ...]:
"""Fetch projects for one workspace and tag each project with workspace metadata."""
from basic_memory.mcp.async_client import get_client, get_cloud_proxy_client, is_factory_mode
from basic_memory.mcp.clients import ProjectClient
client_context = (
get_client(workspace=workspace.tenant_id)
if is_factory_mode()
else get_cloud_proxy_client(workspace=workspace.tenant_id)
)
async with client_context as client:
project_list = await ProjectClient(client).list_projects()
default_permalink = (
generate_permalink(project_list.default_project) if project_list.default_project else None
)
entries: list[WorkspaceProjectEntry] = []
for project in project_list.projects:
entry_project = project
if default_permalink and project.permalink == default_permalink and not project.is_default:
entry_project = project.model_copy(update={"is_default": True})
entries.append(WorkspaceProjectEntry(workspace=workspace, project=entry_project))
if context: # pragma: no cover
await context.info(
f"Discovered {len(entries)} cloud projects in workspace {workspace.slug}"
)
return tuple(entries)
async def _ensure_workspace_project_index(
context: Optional[Context] = None,
) -> WorkspaceProjectIndex:
"""Build or load the session-local workspace/project lookup index."""
if context:
cached_raw = await context.get_state(_WORKSPACE_PROJECT_INDEX_STATE_KEY)
cached_index = _workspace_project_index_from_state(cached_raw)
if cached_index is not None:
return cached_index
workspaces = tuple(await get_available_workspaces(context=context))
if not workspaces:
raise ValueError(
"No accessible workspaces found for this account. "
"Ensure you have an active subscription and tenant access."
)
fetched_results = await asyncio.gather(
*[_fetch_workspace_project_entries(workspace, context=context) for workspace in workspaces],
return_exceptions=True,
)
entries_list: list[WorkspaceProjectEntry] = []
failed_workspaces: list[WorkspaceInfo] = []
successful_fetches = 0
for workspace, result in zip(workspaces, fetched_results, strict=True):
if isinstance(result, BaseException):
if not isinstance(result, Exception):
raise result
# Trigger: one workspace project listing failed during a multi-workspace index.
# Why: a transient or unauthorized tenant should not break qualified routing for
# healthy workspaces, but unqualified routing still needs to know the index is partial.
# Outcome: keep successful workspace entries and record the failed workspace.
failed_workspaces.append(workspace)
logger.warning(
f"Cloud project discovery failed for workspace {workspace.slug} "
f"({workspace.tenant_id}): {result}"
)
if context: # pragma: no cover
await context.info(
f"Cloud project discovery failed for workspace {workspace.slug}; "
"continuing with other workspaces"
)
continue
workspace_entries = result
successful_fetches += 1
entries_list.extend(workspace_entries)
if failed_workspaces and successful_fetches == 0:
failed_labels = ", ".join(workspace.slug for workspace in failed_workspaces)
raise ValueError(
"Unable to discover projects in any accessible workspace. "
f"Failed workspaces: {failed_labels}"
)
entries = tuple(entries_list)
index = _build_workspace_project_index(
workspaces,
entries,
failed_workspaces=tuple(failed_workspaces),
)
if context:
await context.set_state(
_WORKSPACE_PROJECT_INDEX_STATE_KEY,
_workspace_project_index_to_state(index),
)
return index
async def ensure_workspace_project_index(
context: Optional[Context] = None,
) -> WorkspaceProjectIndex:
"""Public wrapper for loading the session-local workspace/project lookup index."""
return await _ensure_workspace_project_index(context=context)
async def resolve_workspace_project_identifier(
project: str,
context: Optional[Context] = None,
) -> WorkspaceProjectEntry:
"""Resolve a project by external_id (UUID), qualified name, or unqualified name."""
index = await _ensure_workspace_project_index(context=context)
# Fast path: direct lookup by external_id when the identifier is a UUID
# Canonicalize via str(UUID(...)) so uppercase, brace-wrapped, or urn:uuid forms
# all hash to the same lowercase-hyphenated key as the stored external_ids.
try:
canonical_external_id = str(UUID(project))
entry = index.entries_by_external_id.get(canonical_external_id)
if entry:
return entry
except ValueError:
pass
workspace_slug, project_identifier = _split_qualified_project_identifier(project)
project_permalink = generate_permalink(project_identifier)
if workspace_slug:
workspace_matches = [
workspace
for workspace in index.workspaces
if workspace.slug.casefold() == workspace_slug.casefold()
]
if not workspace_matches:
available = ", ".join(workspace.slug for workspace in index.workspaces)
raise ValueError(
f"Workspace '{workspace_slug}' was not found. "
f"Available workspace slugs: {available}"
)
workspace = workspace_matches[0]
matches = [
entry
for entry in index.entries_by_permalink.get(project_permalink, ())
if entry.workspace.tenant_id == workspace.tenant_id
]
if not matches:
if any(
failed_workspace.tenant_id == workspace.tenant_id
for failed_workspace in index.failed_workspaces
):
raise ValueError(
f"Projects for workspace '{workspace.name}' ({workspace.slug}) "
"could not be loaded. Retry after workspace discovery recovers."
)
available = ", ".join(
entry.qualified_name
for entry in index.entries
if entry.workspace.tenant_id == workspace.tenant_id
)
raise ValueError(
f"Project '{project_identifier}' was not found in workspace "
f"'{workspace.name}' ({workspace.slug}). Available projects: {available}"
)
if len(matches) > 1:
details = ", ".join(
f"{entry.qualified_name} ({entry.project.external_id})" for entry in matches
)
raise ValueError(
f"Project '{project_identifier}' matched multiple projects in workspace "
f"'{workspace.name}' ({workspace.slug}). Project permalinks must be unique. "
f"Matches: {details}"
)
return matches[0]
matches = list(index.entries_by_permalink.get(project_permalink, ()))
if not matches:
failed_note = ""
if index.failed_workspaces:
failed = ", ".join(workspace.slug for workspace in index.failed_workspaces)
failed_note = (
f" Project discovery failed for workspace(s): {failed}; "
"retry or use a qualified project from an indexed workspace."
)
available = ", ".join(entry.qualified_name for entry in index.entries)
raise ValueError(
f"Project '{project}' was not found in indexed cloud workspaces. "
f"Available projects: {available}.{failed_note}"
)
cached_workspace = await _get_cached_active_workspace(context)
if cached_workspace:
cached_matches = [
entry for entry in matches if entry.workspace.tenant_id == cached_workspace.tenant_id
]
if cached_matches:
return cached_matches[0]
if len(matches) > 1:
# Prefer the project in the default workspace when name is ambiguous
default_match = next((entry for entry in matches if entry.workspace.is_default), None)
if default_match:
return default_match
choices = _format_qualified_choices(matches)
details = "\n".join(
f"- {entry.workspace.name} ({entry.workspace.slug}): {entry.qualified_name}"
for entry in matches
)
raise ValueError(
f"Project '{project}' exists in multiple workspaces. Use: {choices}\n{details}"
)
if index.failed_workspaces:
qualified_name = matches[0].qualified_name
failed = ", ".join(workspace.slug for workspace in index.failed_workspaces)
raise ValueError(
f"Project '{project}' was found as {qualified_name}, but project discovery "
f"failed for workspace(s): {failed}. Use '{qualified_name}' to route "
"explicitly, or retry after discovery recovers."
)
return matches[0]
async def _default_workspace_project_entry(
context: Optional[Context] = None,
) -> WorkspaceProjectEntry | None:
"""Return the default project from the default cloud workspace, when available."""
index = await _ensure_workspace_project_index(context=context)
default_workspace = next(
(workspace for workspace in index.workspaces if workspace.is_default),
None,
)
if default_workspace is None:
return None
default_entries = [
entry
for entry in index.entries
if entry.workspace.tenant_id == default_workspace.tenant_id and entry.project.is_default
]
return default_entries[0] if default_entries else None
async def _workspace_metadata_by_tenant_id(
tenant_id: str,
context: Optional[Context] = None,
) -> WorkspaceInfo | None:
"""Return non-index workspace metadata for a configured tenant id."""
cached_workspace = await _get_cached_active_workspace(context)
if cached_workspace and cached_workspace.tenant_id == tenant_id:
return cached_workspace
if cached_workspace and context:
# Trigger: the configured workspace_id differs from cached workspace metadata.
# Why: tenant_id routes the request, but stale workspace slug/type would corrupt
# memory URL normalization and canonical permalink headers.
# Outcome: drop stale metadata and route without permalink decoration.
await context.set_state("active_workspace", None)
if context:
cached_raw = await context.get_state("available_workspaces")
if isinstance(cached_raw, list):
for item in cached_raw:
if not isinstance(item, dict):
continue
workspace = WorkspaceInfo.model_validate(item)
if workspace.tenant_id == tenant_id:
return workspace
if _workspace_provider is not None:
# Trigger: the hosting runtime can provide workspace metadata directly.
# Why: configured workspace_id is already sufficient for tenant routing, but
# canonical organization permalinks also need slug/type context.
# Outcome: use the injected runtime seam without loading the workspace project index.
workspace = next(
(
workspace
for workspace in await get_available_workspaces(context=context)
if workspace.tenant_id == tenant_id
),
None,
)
if workspace is None:
raise ValueError(
f"Configured workspace_id '{tenant_id}' was not returned by the workspace "
"metadata provider. Reconfigure the project workspace or retry after "
"workspace metadata recovers."
)
return workspace
return None
async def resolve_workspace_parameter(
workspace: Optional[str] = None,
context: Optional[Context] = None,
) -> WorkspaceInfo:
"""Resolve workspace using explicit input, session cache, and cloud discovery."""
with logfire.span(
"routing.resolve_workspace",
workspace_requested=workspace is not None,
has_context=context is not None,
):
if context: