File size: 12,930 Bytes
b19d31a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
Atomic Dataset Persistence for OpenClaw AI
Save state to Hugging Face Dataset with atomic operations
"""

import os
import sys
import json
import hashlib
import time
import tarfile
import tempfile
import shutil
from datetime import datetime
from pathlib import Path
from typing import Dict, Any, Optional, List
import requests
import logging

from huggingface_hub import HfApi, CommitOperationAdd
from huggingface_hub.utils import RepositoryNotFoundError
from huggingface_hub import hf_hub_download

# Configure structured logging
logging.basicConfig(
    level=logging.INFO,
    format='{"timestamp": "%(asctime)s", "level": "%(levelname)s", "module": "atomic-save", "message": "%(message)s"}'
)
logger = logging.getLogger(__name__)

class AtomicDatasetSaver:
    """Atomic dataset persistence with proper error handling and retries"""
    
    def __init__(self, repo_id: str, dataset_path: str = "state"):
        self.repo_id = repo_id
        self.dataset_path = Path(dataset_path)
        self.api = HfApi()
        self.max_retries = 3
        self.base_delay = 1.0
        self.max_backups = 3
        
        logger.info("init", {
            "repo_id": repo_id,
            "dataset_path": dataset_path,
            "max_retries": self.max_retries,
            "max_backups": self.max_backups
        })
    
    def calculate_checksum(self, file_path: Path) -> str:
        """Calculate SHA256 checksum of file"""
        sha256_hash = hashlib.sha256()
        with open(file_path, "rb") as f:
            for chunk in iter(lambda: f.read(4096), b""):
                sha256_hash.update(chunk)
        return sha256_hash.hexdigest()
    
    def create_backup(self, current_commit: Optional[str] = None) -> Optional[str]:
        """Create backup of current state before overwriting"""
        try:
            if not current_commit:
                return None
            
            # List current files in dataset
            files = self.api.list_repo_files(
                repo_id=self.repo_id,
                repo_type="dataset",
                revision=current_commit
            )
            
            # Only backup if there are existing state files
            state_files = [f for f in files if f.startswith(str(self.dataset_path))]
            if not state_files:
                return None
            
            # Create backup with timestamp
            timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
            backup_path = f"backups/state_{timestamp}"
            
            logger.info("creating_backup", {
                "current_commit": current_commit,
                "backup_path": backup_path,
                "files_count": len(state_files)
            })
            
            # Download and create backup
            with tempfile.TemporaryDirectory() as tmpdir:
                tmpdir_path = Path(tmpdir)
                
                # Download all state files
                for file_path in state_files:
                    file_content = hf_hub_download(
                        repo_id=self.repo_id,
                        repo_type="dataset",
                        filename=file_path,
                        revision=current_commit,
                        local_files_only=False
                    )
                    if file_content:
                        shutil.copy2(file_content, tmpdir_path / Path(file_path).name)
                
                # Create backup structure
                backup_files = []
                for file_path in state_files:
                    local_path = tmpdir_path / file_path
                    if local_path.exists():
                        backup_file_path = f"{backup_path}/{Path(file_path).name}"
                        backup_files.append(
                            CommitOperationAdd(
                                path_in_repo=backup_file_path,
                                path_or_fileobj=str(local_path)
                            )
                        )
                
                if backup_files:
                    # Commit backup
                    commit_info = self.api.create_commit(
                        repo_id=self.repo_id,
                        repo_type="dataset",
                        operations=backup_files,
                        commit_message=f"Backup state before update - {timestamp}",
                        parent_commit=current_commit
                    )
                    
                    logger.info("backup_created", {
                        "backup_commit": commit_info.oid,
                        "backup_path": backup_path
                    })
                    
                    return commit_info.oid
                
        except Exception as e:
            logger.error("backup_failed", {"error": str(e), "current_commit": current_commit})
            return None
    
    def cleanup_old_backups(self, current_commit: Optional[str] = None) -> None:
        """Clean up old backups, keeping only the most recent ones"""
        try:
            if not current_commit:
                return
            
            # List all files to find backups
            files = self.api.list_repo_files(
                repo_id=self.repo_id,
                repo_type="dataset",
                revision=current_commit
            )
            
            # Find backup directories
            backup_dirs = set()
            for file_path in files:
                if file_path.startswith("backups/state_"):
                    backup_dir = file_path.split("/")[1]  # Extract backup directory name
                    backup_dirs.add(backup_dir)
            
            # Keep only the most recent backups
            backup_list = sorted(backup_dirs)
            if len(backup_list) > self.max_backups:
                backups_to_remove = backup_list[:-self.max_backups]
                
                logger.info("cleaning_old_backups", {
                    "total_backups": len(backup_list),
                    "keeping": self.max_backups,
                    "removing": len(backups_to_remove),
                    "old_backups": backups_to_remove
                })
                
                # Note: In a real implementation, we would delete these files
                # For now, we just log what would be cleaned up
                
        except Exception as e:
            logger.error("backup_cleanup_failed", {"error": str(e)})
    
    def save_state_atomic(self, state_data: Dict[str, Any], source_paths: List[str]) -> Dict[str, Any]:
        """
        Save state to dataset atomically
        
        Args:
            state_data: Dictionary containing state information
            source_paths: List of file paths to include in the state
            
        Returns:
            Dictionary with operation result
        """
        operation_id = f"save_{int(time.time())}"
        
        logger.info("starting_atomic_save", {
            "operation_id": operation_id,
            "state_keys": list(state_data.keys()),
            "source_paths": source_paths
        })
        
        try:
            # Get current commit to use as parent
            try:
                repo_info = self.api.repo_info(
                    repo_id=self.repo_id,
                    repo_type="dataset"
                )
                current_commit = repo_info.sha
                logger.info("current_commit_found", {"commit": current_commit})
            except RepositoryNotFoundError:
                current_commit = None
                logger.info("repository_not_found", {"action": "creating_new_repo"})
            
            # Create backup before making changes
            backup_commit = self.create_backup(current_commit)
            
            # Create temporary directory for state files
            with tempfile.TemporaryDirectory() as tmpdir:
                tmpdir_path = Path(tmpdir)
                state_dir = tmpdir_path / self.dataset_path
                state_dir.mkdir(parents=True, exist_ok=True)
                
                # Save state metadata
                metadata = {
                    "timestamp": datetime.now().isoformat(),
                    "operation_id": operation_id,
                    "checksum": None,
                    "backup_commit": backup_commit,
                    "state_data": state_data
                }
                
                metadata_path = state_dir / "metadata.json"
                with open(metadata_path, "w") as f:
                    json.dump(metadata, f, indent=2)
                
                # Copy source files to state directory
                operations = [CommitOperationAdd(path_in_repo=f"state/metadata.json", path_or_fileobj=str(metadata_path))]
                
                for source_path in source_paths:
                    source = Path(source_path)
                    if source.exists():
                        dest_path = state_dir / source.name
                        shutil.copy2(source, dest_path)
                        
                        # Calculate checksum for integrity
                        checksum = self.calculate_checksum(dest_path)
                        
                        operations.append(
                            CommitOperationAdd(
                                path_in_repo=f"state/{source.name}",
                                path_or_fileobj=str(dest_path)
                            )
                        )
                        
                        logger.info("file_added", {
                            "source": source_path,
                            "checksum": checksum,
                            "operation_id": operation_id
                        })
                
                # Create final metadata with checksums
                final_metadata = metadata.copy()
                final_metadata["checksum"] = hashlib.sha256(
                    json.dumps(state_data, sort_keys=True).encode()
                ).hexdigest()
                
                # Update metadata file
                with open(metadata_path, "w") as f:
                    json.dump(final_metadata, f, indent=2)
                
                # Atomic commit to dataset
                commit_info = self.api.create_commit(
                    repo_id=self.repo_id,
                    repo_type="dataset",
                    operations=operations,
                    commit_message=f"Atomic state update - {operation_id}",
                    parent_commit=current_commit
                )
                
                # Clean up old backups
                self.cleanup_old_backups(commit_info.oid)
                
                result = {
                    "success": True,
                    "operation_id": operation_id,
                    "commit_id": commit_info.oid,
                    "backup_commit": backup_commit,
                    "timestamp": datetime.now().isoformat(),
                    "files_count": len(source_paths)
                }
                
                logger.info("atomic_save_completed", result)
                return result
                
        except Exception as e:
            error_result = {
                "success": False,
                "operation_id": operation_id,
                "error": str(e),
                "timestamp": datetime.now().isoformat()
            }
            
            logger.error("atomic_save_failed", error_result)
            raise Exception(f"Atomic save failed: {str(e)}")

def main():
    """Main function for command line usage"""
    if len(sys.argv) < 3:
        print(json.dumps({
            "error": "Usage: python save_to_dataset_atomic.py <repo_id> <source_path1> [source_path2...]",
            "status": "error"
        }, indent=2))
        sys.exit(1)
    
    repo_id = sys.argv[1]
    source_paths = sys.argv[2:]
    
    # Validate source paths
    for path in source_paths:
        if not os.path.exists(path):
            print(json.dumps({
                "error": f"Source path does not exist: {path}",
                "status": "error"
            }, indent=2))
            sys.exit(1)
    
    try:
        # Create state data (can be enhanced to read from environment or config)
        state_data = {
            "environment": "production",
            "version": "1.0.0",
            "platform": "huggingface-spaces",
            "timestamp": datetime.now().isoformat()
        }
        
        saver = AtomicDatasetSaver(repo_id)
        result = saver.save_state_atomic(state_data, source_paths)
        
        print(json.dumps(result, indent=2))
        
    except Exception as e:
        print(json.dumps({
            "error": str(e),
            "status": "error"
        }, indent=2))
        sys.exit(1)

if __name__ == "__main__":
    main()