Write file content. Creates parent directories if they don't exist. Enforces checkpoint for core file modifications. Args: path: Path to file (relative to workspace or absolute) content: Content to write Returns: Success
(self, path: Union[str, Path], content: str)
| 166 | raise FilesystemAccessError(f"Failed to read {path}: {e}") |
| 167 | |
| 168 | def write(self, path: Union[str, Path], content: str) -> str: |
| 169 | """ |
| 170 | Write file content. |
| 171 | |
| 172 | Creates parent directories if they don't exist. |
| 173 | Enforces checkpoint for core file modifications. |
| 174 | |
| 175 | Args: |
| 176 | path: Path to file (relative to workspace or absolute) |
| 177 | content: Content to write |
| 178 | |
| 179 | Returns: |
| 180 | Success message with file path |
| 181 | |
| 182 | Raises: |
| 183 | FilesystemAccessError: If file cannot be written |
| 184 | """ |
| 185 | try: |
| 186 | path = self._validate_path(path) |
| 187 | |
| 188 | # Check if modifying Codey's own code (requires checkpoint) |
| 189 | is_core = _is_core_file(str(path)) |
| 190 | if is_core: |
| 191 | info(f"Writing to core Codey file: {path}") |
| 192 | # Enforce checkpoint before core file writes |
| 193 | self._require_checkpoint(path) |
| 194 | |
| 195 | # Create parent directories |
| 196 | path.parent.mkdir(parents=True, exist_ok=True) |
| 197 | |
| 198 | # Snapshot existing file so /undo works for write_file too |
| 199 | if path.exists(): |
| 200 | _snapshot(str(path)) |
| 201 | |
| 202 | # Write content |
| 203 | path.write_text(content, encoding='utf-8') |
| 204 | |
| 205 | try: |
| 206 | rel = path.relative_to(self.workspace) |
| 207 | except ValueError: |
| 208 | rel = path |
| 209 | msg = f"Written {rel}" |
| 210 | success(msg) |
| 211 | return msg |
| 212 | |
| 213 | except FilesystemAccessError: |
| 214 | raise |
| 215 | except Exception as e: |
| 216 | raise FilesystemAccessError(f"Failed to write {path}: {e}") |
| 217 | |
| 218 | def patch(self, path: Union[str, Path], old_str: str, new_str: str) -> str: |
| 219 | """ |