@@ -37,13 +37,15 @@ use tracing::warn;
3737/// and workspace-scoped profile writers hold their workspace key exclusively;
3838/// sandbox-scoped settings and policy writers hold only their sandbox key
3939/// exclusively. A new cross-object writer needs a scope from the same table.
40+ /// Profile writers pick theirs with [`MutationScope::profiles`].
4041#[ derive( Clone , Copy , Debug ) ]
4142pub enum MutationScope < ' a > {
4243 /// Global policy/settings and platform-scope profiles. Excludes every
4344 /// other scope fleet-wide.
4445 Global ,
45- /// Provider and workspace-scoped profile mutations. `""` (platform)
46- /// behaves as `Global`.
46+ /// Provider and workspace-scoped profile mutations in a named workspace.
47+ /// The platform scope is `Global`: an empty name trips a debug assertion
48+ /// and locks as `Global` in release builds.
4749 Workspace ( & ' a str ) ,
4850 /// Any mutation of one sandbox's records, admin or supervisor.
4951 Sandbox {
@@ -60,23 +62,42 @@ impl<'a> MutationScope<'a> {
6062 }
6163 }
6264
65+ /// Scope of a provider profile write. Platform-scope profiles (`""`)
66+ /// feed every workspace's catalog and are validated against sandboxes in
67+ /// all workspaces, so they take `Global`; a named workspace takes
68+ /// `Workspace(name)`.
69+ pub ( crate ) const fn profiles ( workspace : & ' a str ) -> Self {
70+ if workspace. is_empty ( ) {
71+ Self :: Global
72+ } else {
73+ Self :: Workspace ( workspace)
74+ }
75+ }
76+
6377 /// Keys and modes of this scope:
6478 ///
65- /// - `Global` and `Workspace("")`: X(global).
66- /// - `Workspace(ws)`: S(global) X(workspace).
79+ /// - `Global`: X(global).
80+ /// - `Workspace(ws)`: S(global) X(workspace). An empty name is a caller
81+ /// bug that debug builds catch; release builds take X(global), which
82+ /// over-locks rather than under-locks.
6783 /// - `Sandbox`: S(global) S(workspace) X(sandbox). A legacy sandbox with
6884 /// an empty workspace locks the default workspace, where its providers
6985 /// resolve.
7086 pub ( crate ) fn lock_set ( & self ) -> MutationLockSet {
7187 let mut set = MutationLockSet :: default ( ) ;
7288 match * self {
7389 Self :: Global => set. insert ( MutationLockKey :: Global , LockMode :: Exclusive ) ,
74- Self :: Workspace ( "" ) => {
75- set. insert ( MutationLockKey :: Global , LockMode :: Exclusive ) ;
76- }
7790 Self :: Workspace ( workspace) => {
78- set. insert ( MutationLockKey :: Global , LockMode :: Shared ) ;
79- set. insert ( MutationLockKey :: Workspace ( workspace) , LockMode :: Exclusive ) ;
91+ debug_assert ! (
92+ !workspace. is_empty( ) ,
93+ "the platform scope is MutationScope::Global; use MutationScope::profiles"
94+ ) ;
95+ if workspace. is_empty ( ) {
96+ set. insert ( MutationLockKey :: Global , LockMode :: Exclusive ) ;
97+ } else {
98+ set. insert ( MutationLockKey :: Global , LockMode :: Shared ) ;
99+ set. insert ( MutationLockKey :: Workspace ( workspace) , LockMode :: Exclusive ) ;
100+ }
80101 }
81102 Self :: Sandbox {
82103 workspace,
@@ -460,7 +481,11 @@ mod tests {
460481
461482 let cases = [
462483 ( MutationScope :: Global , keys ( & [ ( Global , Exclusive ) ] ) ) ,
463- ( MutationScope :: Workspace ( "" ) , keys ( & [ ( Global , Exclusive ) ] ) ) ,
484+ ( MutationScope :: profiles ( "" ) , keys ( & [ ( Global , Exclusive ) ] ) ) ,
485+ (
486+ MutationScope :: profiles ( "team-a" ) ,
487+ keys ( & [ ( Global , Shared ) , ( Workspace ( "team-a" ) , Exclusive ) ] ) ,
488+ ) ,
464489 (
465490 MutationScope :: Workspace ( "team-a" ) ,
466491 keys ( & [ ( Global , Shared ) , ( Workspace ( "team-a" ) , Exclusive ) ] ) ,
@@ -504,9 +529,13 @@ mod tests {
504529 }
505530
506531 #[ test]
507- fn scope_labels_map_platform_workspace_to_global ( ) {
532+ fn scope_labels_map_platform_profiles_to_global ( ) {
508533 assert_eq ! ( MutationScope :: Global . lock_scope( ) , LockScope :: Global ) ;
509- assert_eq ! ( MutationScope :: Workspace ( "" ) . lock_scope( ) , LockScope :: Global ) ;
534+ assert_eq ! ( MutationScope :: profiles( "" ) . lock_scope( ) , LockScope :: Global ) ;
535+ assert_eq ! (
536+ MutationScope :: profiles( "team-a" ) . lock_scope( ) ,
537+ LockScope :: Workspace
538+ ) ;
510539 assert_eq ! (
511540 MutationScope :: Workspace ( "team-a" ) . lock_scope( ) ,
512541 LockScope :: Workspace
@@ -570,7 +599,7 @@ mod tests {
570599
571600 let mut waiting_guards = vec ! [
572601 spawn_guard( & runtime, MutationScope :: Global ) ,
573- spawn_guard( & runtime, MutationScope :: Workspace ( "" ) ) ,
602+ spawn_guard( & runtime, MutationScope :: profiles ( "" ) ) ,
574603 spawn_guard( & runtime, MutationScope :: Workspace ( "w1" ) ) ,
575604 spawn_guard( & runtime, MutationScope :: sandbox( "w1" , "a" ) ) ,
576605 ] ;
@@ -1031,6 +1060,13 @@ mod tests {
10311060 run_random_scope_mix ( & [ runtime] , "" , 64 , 50 , Duration :: from_secs ( 20 ) ) . await ;
10321061 }
10331062
1063+ #[ cfg( debug_assertions) ]
1064+ #[ test]
1065+ #[ should_panic( expected = "platform scope" ) ]
1066+ fn empty_workspace_scope_panics_in_debug_builds ( ) {
1067+ let _ = MutationScope :: Workspace ( "" ) . lock_set ( ) ;
1068+ }
1069+
10341070 #[ cfg( debug_assertions) ]
10351071 #[ tokio:: test]
10361072 #[ should_panic( expected = "mutation lock ordering violated" ) ]
0 commit comments