src.core.workspace module#
Workspace isolation and security management for autonomous agents.
This module ensures that client agents only operate within their designated project directories and cannot access the Marcus installation or system files.
The WorkspaceManager provides: - Automatic detection of Marcus installation directory - Configuration-based workspace assignment - Path validation and security enforcement - Audit logging of security violations
Examples
>>> manager = WorkspaceManager()
>>> workspace = manager.assign_agent_workspace("agent-001", "/home/user/project")
>>> manager.validate_path("/home/user/project/src/file.py") # OK
>>> manager.validate_path("/usr/lib/python3/site-packages") # Raises SecurityError
- exception src.core.workspace.WorkspaceSecurityError[source]#
Bases:
ExceptionRaised when a security violation is detected.
This exception indicates an agent attempted to access a forbidden path.
Examples
>>> raise WorkspaceSecurityError("Access denied to system directory")
- class src.core.workspace.WorkspaceConfig[source]#
Bases:
objectConfiguration for a single agent workspace.
- Parameters:
Notes
Paths are automatically expanded and converted to absolute paths during initialization.
- class src.core.workspace.ProjectWorkspaces[source]#
Bases:
objectConfiguration for all project workspaces.
- Parameters:
Examples
>>> config = ProjectWorkspaces( ... main_workspace="/home/user/project", ... agent_workspaces={"agent-001": "/home/user/project/agent1"} ... )
- class src.core.workspace.WorkspaceManager[source]#
Bases:
objectManages workspace isolation and security boundaries for Marcus.
This class enforces security by maintaining a list of forbidden paths (including the Marcus installation) and validating all agent workspace assignments against these restrictions.
- agent_workspaces#
Mapping of agent IDs to their workspace configurations
- Type:
Dict[str, WorkspaceConfig]
- project_config#
Project-level workspace configuration
- Type:
Optional[ProjectWorkspaces]
Examples
>>> manager = WorkspaceManager() >>> workspace = manager.assign_agent_workspace("agent-001") >>> print(workspace.path) # /home/user/project >>> manager.validate_path("/etc/passwd") # Raises WorkspaceSecurityError
Notes
The Marcus installation directory is automatically detected and added to the forbidden paths list to prevent agents from modifying Marcus itself.
- agent_workspaces: Dict[str, WorkspaceConfig]#
- project_config: ProjectWorkspaces | None#
- load_config(config_path)[source]#
Load workspace configuration from file.
- Parameters:
config_path (
str) – Path to JSON configuration file- Raises:
FileNotFoundError – If configuration file doesn’t exist
WorkspaceSecurityError – If configured workspaces overlap with forbidden paths
- Return type:
Notes
Configuration format:
{ "project": { "workspaces": { "main": "/path/to/project", "agents": { "agent-001": "/path/to/agent1/workspace" } } }, "security": { "additional_forbidden_paths": ["/sensitive/data"] } }
- assign_agent_workspace(agent_id, workspace_path=None)[source]#
Assign a workspace to an agent.
- Parameters:
- Returns:
The assigned workspace configuration
- Return type:
- Raises:
ValueError – If no workspace is available for the agent
WorkspaceSecurityError – If the workspace path is forbidden
Examples
>>> workspace = manager.assign_agent_workspace( ... "agent-001", "/home/user/project" ... ) >>> print(workspace.workspace_id) # "agent-001_workspace"
Notes
If workspace_path is not provided, the method tries: 1. Pre-configured workspace for the specific agent 2. The main project workspace as fallback
- get_agent_workspace(agent_id)[source]#
Get the assigned workspace for an agent.
- Parameters:
agent_id (
str) – The agent’s unique identifier- Returns:
The workspace configuration if assigned, None otherwise
- Return type:
- validate_path(path)[source]#
Validate that a path is allowed (not in forbidden paths).
- Parameters:
path (
str) – Path to validate (can be relative or contain ~)- Returns:
The absolute path if valid
- Return type:
- Raises:
WorkspaceSecurityError – If the path is within a forbidden directory
Examples
>>> manager.validate_path("/home/user/project/file.py") # OK >>> manager.validate_path("~/project/file.py") # Expands and validates >>> manager.validate_path("/usr/lib/python3/os.py") # Raises error
- is_path_allowed(path)[source]#
Check if a path is allowed without raising an exception.
- Parameters:
path (
str) – Path to check- Returns:
True if path is allowed, False if forbidden
- Return type:
Examples
>>> if manager.is_path_allowed("/home/user/file.py"): ... print("Path is safe to access")
- add_forbidden_path(path)[source]#
Add a path to the forbidden list.
Examples
>>> manager.add_forbidden_path("/sensitive/data") >>> manager.is_path_allowed("/sensitive/data/file.txt") # False
- get_task_assignment_data(agent_id)[source]#
Get workspace data to include in task assignments.
This data should be sent to agents so they know where to work and what paths to avoid.
- Parameters:
agent_id (
str) – The agent’s identifier- Returns:
Dictionary containing: - workspace_path: Where the agent should work - workspace_id: Unique workspace identifier - forbidden_paths: List of paths to avoid - is_readonly: Whether the workspace is read-only
- Return type:
Examples
>>> data = manager.get_task_assignment_data("agent-001") >>> print(data['workspace_path']) # /home/user/project >>> print(len(data['forbidden_paths'])) # Number of forbidden paths
- log_security_violation(agent_id, attempted_path, operation)[source]#
Log a security violation attempt.
- Parameters:
- Return type:
Examples
>>> manager.log_security_violation( ... "agent-001", ... "/etc/passwd", ... "read" ... )
Notes
Currently logs to stderr. In production, this would write to a security audit log file.