MCPcopy Create free account
hub / github.com/FSoft-AI4Code/CodeWiki / SessionWorkspace

Class SessionWorkspace

codewiki/mcp/workspace.py:56–110  ·  view source on GitHub ↗

Manages the on-disk workspace for a single MCP session.

Source from the content-addressed store, hash-verified

54 return f"{sanitized}_{hash_suffix}.src"
55
56
57class SessionWorkspace:
58 """Manages the on-disk workspace for a single MCP session."""
59
60 def __init__(self, repo_path: Path, session_id: str) -> None:
61 self.root = repo_path / _WORKSPACE_REL / session_id
62 self.root.mkdir(parents=True, exist_ok=True)
63 (self.root / "sources").mkdir(exist_ok=True)
64 logger.debug("Workspace created at %s", self.root)
65
66 # -- writers ----------------------------------------------------------
67
68 def write_json(self, name: str, data: Any) -> Path:
69 """Write *data* as pretty-printed JSON and return the file path."""
70 p = self.root / name
71 p.write_text(json.dumps(data, indent=2, ensure_ascii=False), encoding="utf-8")
72 return p
73
74 def write_component_source(
75 self,
76 component_id: str,
77 source: str,
78 language: str = "",
79 ) -> Path:
80 """Write a single component's source code to the ``sources/`` dir."""
81 p = self.root / "sources" / _safe_filename(component_id)
82 header = f"// Component: {component_id}\n// Language: {language}\n"
83 p.write_text(header + source, encoding="utf-8")
84 return p
85
86 # -- readers ----------------------------------------------------------
87
88 def read_json(self, name: str) -> Any:
89 """Read a JSON file from the workspace. Returns ``None`` if missing."""
90 p = self.root / name
91 if not p.exists():
92 return None
93 return json.loads(p.read_text(encoding="utf-8"))
94
95 # -- cleanup ----------------------------------------------------------
96
97 def cleanup(self) -> None:
98 """Remove the session directory and try to prune empty parents."""
99 if self.root.exists():
100 shutil.rmtree(self.root, ignore_errors=True)
101 # Walk up and remove empty parent directories
102 try:
103 sessions_dir = self.root.parent # .codewiki/sessions
104 if sessions_dir.exists() and not any(sessions_dir.iterdir()):
105 sessions_dir.rmdir()
106 base_dir = sessions_dir.parent # .codewiki
107 if base_dir.exists() and not any(base_dir.iterdir()):
108 base_dir.rmdir()
109 except OSError:
110 pass
111 logger.debug("Workspace cleaned up: %s", self.root)

Callers 1

handle_analyze_repoFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected