-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdynamic_context_buffer.py
More file actions
907 lines (720 loc) · 28.2 KB
/
Copy pathdynamic_context_buffer.py
File metadata and controls
907 lines (720 loc) · 28.2 KB
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
#!/usr/bin/env python3
"""
Dynamic Context Buffer
A powerful buffer system for AI agents to:
- Hot-reload and dynamically rewrite modules on the fly
- Maintain an in-memory environment for code execution
- Sandbox code execution with proper isolation
- Allow agents to experiment with code changes without affecting the disk
- Persist memory across sessions with serialization
This acts as a virtual filesystem and execution environment
that only becomes "real" when explicitly committed to disk.
"""
import os
import sys
import importlib
import inspect
import ast
import types
import uuid
import json
import pickle
import time
import copy
import logging
from typing import Dict, List, Any, Optional, Tuple, Set, Union, Callable
# Setup logging
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger('dynamic_context')
class VirtualModule:
"""Represents a Python module that exists in memory but not necessarily on disk"""
def __init__(self, name: str, code: str = "", source_path: Optional[str] = None):
"""
Initialize a virtual module
Args:
name: The module name
code: The module source code
source_path: Optional path to the original source file
"""
self.name = name
self.code = code
self.source_path = source_path
self.compiled = None
self.module = None
self.last_modified = time.time()
self.attributes = {}
self.in_memory_only = source_path is None
# Compile if code is provided
if code:
self.compile()
def compile(self) -> bool:
"""
Compile the module code
Returns:
True if successful, False otherwise
"""
try:
self.compiled = compile(self.code, f"<virtual:{self.name}>", 'exec')
return True
except SyntaxError as e:
logger.error(f"Syntax error in module {self.name}: {str(e)}")
return False
def load(self) -> Optional[types.ModuleType]:
"""
Load the module into memory
Returns:
The loaded module object or None if failed
"""
try:
# Create a new module object
module = types.ModuleType(self.name)
# Set __file__ attribute if we have a source path
if self.source_path:
module.__file__ = self.source_path
# Add to sys.modules
sys.modules[self.name] = module
# Execute the compiled code in the module's namespace
if self.compiled:
exec(self.compiled, module.__dict__)
self.module = module
self.attributes = {
name: value for name, value in inspect.getmembers(module)
if not name.startswith('_')
}
return module
except Exception as e:
logger.error(f"Error loading module {self.name}: {str(e)}")
return None
def reload(self) -> Optional[types.ModuleType]:
"""
Reload the module with current code
Returns:
The reloaded module object or None if failed
"""
self.compile()
return self.load()
def get_function(self, function_name: str) -> Optional[Callable]:
"""
Get a function from the module
Args:
function_name: Name of the function to retrieve
Returns:
The function object or None if not found
"""
if not self.module:
self.load()
if not self.module:
return None
return getattr(self.module, function_name, None)
def get_class(self, class_name: str) -> Optional[type]:
"""
Get a class from the module
Args:
class_name: Name of the class to retrieve
Returns:
The class object or None if not found
"""
if not self.module:
self.load()
if not self.module:
return None
return getattr(self.module, class_name, None)
def update_code(self, new_code: str) -> bool:
"""
Update the module's code
Args:
new_code: The new source code
Returns:
True if successful, False otherwise
"""
self.code = new_code
self.last_modified = time.time()
return self.compile() and self.reload() is not None
def serialize(self) -> Dict:
"""
Serialize the module for storage
Returns:
Dictionary representation of the module
"""
return {
"name": self.name,
"code": self.code,
"source_path": self.source_path,
"last_modified": self.last_modified,
"in_memory_only": self.in_memory_only
}
@classmethod
def deserialize(cls, data: Dict) -> 'VirtualModule':
"""
Create a module from serialized data
Args:
data: Dictionary representation of the module
Returns:
VirtualModule instance
"""
module = cls(
name=data["name"],
code=data["code"],
source_path=data["source_path"]
)
module.last_modified = data.get("last_modified", time.time())
module.in_memory_only = data.get("in_memory_only", False)
return module
class DynamicContextBuffer:
"""
Core buffer system for managing dynamic code modules
Provides virtual environment for testing code changes before committing to disk
"""
def __init__(self, persistence_path: Optional[str] = None):
"""
Initialize the dynamic context buffer
Args:
persistence_path: Optional path to save/load buffer state
"""
self.modules: Dict[str, VirtualModule] = {}
self.persistence_path = persistence_path
self.snapshots: Dict[str, List[Dict]] = {} # module_name -> list of snapshots
self.execution_history: List[Dict] = []
self.logger = logger
# Load persistent state if available
if persistence_path and os.path.exists(persistence_path):
self.load_state()
def add_module_from_file(self, file_path: str) -> Optional[VirtualModule]:
"""
Load a module from an existing file
Args:
file_path: Path to the Python file
Returns:
The loaded virtual module or None if failed
"""
try:
with open(file_path, 'r') as f:
code = f.read()
module_name = os.path.splitext(os.path.basename(file_path))[0]
module = VirtualModule(module_name, code, file_path)
self.modules[module_name] = module
# Create initial snapshot
self._create_snapshot(module_name)
return module
except Exception as e:
self.logger.error(f"Error loading file {file_path}: {str(e)}")
return None
def add_module_from_code(self, module_name: str, code: str) -> VirtualModule:
"""
Create a new in-memory module from code
Args:
module_name: Name for the new module
code: Python source code
Returns:
The created virtual module
"""
module = VirtualModule(module_name, code)
self.modules[module_name] = module
# Create initial snapshot
self._create_snapshot(module_name)
return module
def get_module(self, module_name: str) -> Optional[VirtualModule]:
"""
Get a virtual module by name
Args:
module_name: Name of the module to retrieve
Returns:
The module or None if not found
"""
return self.modules.get(module_name)
def update_module(self, module_name: str, new_code: str) -> bool:
"""
Update a module's code
Args:
module_name: Name of the module to update
new_code: New source code
Returns:
True if successful, False otherwise
"""
module = self.get_module(module_name)
if not module:
return False
success = module.update_code(new_code)
if success:
# Create snapshot after successful update
self._create_snapshot(module_name)
return success
def execute_code(self, code: str, globals_dict: Optional[Dict] = None,
locals_dict: Optional[Dict] = None) -> Dict:
"""
Execute code in a managed environment
Args:
code: Python code to execute
globals_dict: Optional globals dictionary
locals_dict: Optional locals dictionary
Returns:
Dictionary with execution results
"""
execution_id = str(uuid.uuid4())
start_time = time.time()
if globals_dict is None:
globals_dict = {}
if locals_dict is None:
locals_dict = {}
# Add access to our modules
for name, module in self.modules.items():
if module.module:
globals_dict[name] = module.module
# Execute the code
stdout_buffer = []
stderr_buffer = []
result = None
error = None
try:
# Redirect stdout/stderr
original_stdout = sys.stdout
original_stderr = sys.stderr
class CaptureBuffer:
def __init__(self, buffer):
self.buffer = buffer
def write(self, text):
self.buffer.append(text)
original_stdout.write(text)
def flush(self):
pass
sys.stdout = CaptureBuffer(stdout_buffer)
sys.stderr = CaptureBuffer(stderr_buffer)
# Execute the code
compiled = compile(code, "<dynamic_context>", "exec")
exec(compiled, globals_dict, locals_dict)
# Check for result variable if it exists
if 'result' in locals_dict:
result = locals_dict['result']
except Exception as e:
error = f"{type(e).__name__}: {str(e)}"
self.logger.error(f"Error executing code: {error}")
finally:
# Restore stdout/stderr
sys.stdout = original_stdout
sys.stderr = original_stderr
execution_time = time.time() - start_time
# Record the execution
execution_record = {
"id": execution_id,
"code": code,
"timestamp": start_time,
"duration": execution_time,
"error": error,
"stdout": "".join(stdout_buffer),
"stderr": "".join(stderr_buffer),
"result": result
}
self.execution_history.append(execution_record)
return execution_record
def execute_function(self, module_name: str, function_name: str,
*args, **kwargs) -> Dict:
"""
Execute a function from a module
Args:
module_name: Name of the module
function_name: Name of the function
*args: Positional arguments
**kwargs: Keyword arguments
Returns:
Dictionary with execution results
"""
execution_id = str(uuid.uuid4())
start_time = time.time()
module = self.get_module(module_name)
if not module:
return {
"id": execution_id,
"error": f"Module {module_name} not found",
"result": None,
"success": False
}
function = module.get_function(function_name)
if not function:
return {
"id": execution_id,
"error": f"Function {function_name} not found in module {module_name}",
"result": None,
"success": False
}
# Execute the function
stdout_buffer = []
stderr_buffer = []
result = None
error = None
try:
# Redirect stdout/stderr
original_stdout = sys.stdout
original_stderr = sys.stderr
class CaptureBuffer:
def __init__(self, buffer):
self.buffer = buffer
def write(self, text):
self.buffer.append(text)
original_stdout.write(text)
def flush(self):
pass
sys.stdout = CaptureBuffer(stdout_buffer)
sys.stderr = CaptureBuffer(stderr_buffer)
# Execute the function
result = function(*args, **kwargs)
except Exception as e:
error = f"{type(e).__name__}: {str(e)}"
self.logger.error(f"Error executing function {function_name}: {error}")
finally:
# Restore stdout/stderr
sys.stdout = original_stdout
sys.stderr = original_stderr
execution_time = time.time() - start_time
# Record the execution
execution_record = {
"id": execution_id,
"module": module_name,
"function": function_name,
"args": args,
"kwargs": kwargs,
"timestamp": start_time,
"duration": execution_time,
"error": error,
"stdout": "".join(stdout_buffer),
"stderr": "".join(stderr_buffer),
"result": result,
"success": error is None
}
self.execution_history.append(execution_record)
return execution_record
def _create_snapshot(self, module_name: str) -> None:
"""
Create a snapshot of a module's current state
Args:
module_name: Name of the module
"""
module = self.get_module(module_name)
if not module:
return
if module_name not in self.snapshots:
self.snapshots[module_name] = []
snapshot = {
"timestamp": time.time(),
"code": module.code,
"hash": hash(module.code)
}
self.snapshots[module_name].append(snapshot)
# Limit snapshots to 20 per module
if len(self.snapshots[module_name]) > 20:
self.snapshots[module_name].pop(0)
def restore_snapshot(self, module_name: str, index: int = -1) -> bool:
"""
Restore a module to a previous snapshot
Args:
module_name: Name of the module
index: Index of the snapshot to restore (-1 for latest)
Returns:
True if successful, False otherwise
"""
if module_name not in self.snapshots:
return False
snapshots = self.snapshots[module_name]
if not snapshots:
return False
if index < 0:
index = len(snapshots) + index
if index < 0 or index >= len(snapshots):
return False
snapshot = snapshots[index]
return self.update_module(module_name, snapshot["code"])
def get_snapshots(self, module_name: str) -> List[Dict]:
"""
Get all snapshots for a module
Args:
module_name: Name of the module
Returns:
List of snapshots
"""
return self.snapshots.get(module_name, [])
def commit_to_disk(self, module_name: str) -> bool:
"""
Write an in-memory module to disk
Args:
module_name: Name of the module
Returns:
True if successful, False otherwise
"""
module = self.get_module(module_name)
if not module:
return False
# If module doesn't have a source path, create one
if not module.source_path:
module.source_path = f"{module_name}.py"
try:
with open(module.source_path, 'w') as f:
f.write(module.code)
# Update module state
module.in_memory_only = False
return True
except Exception as e:
self.logger.error(f"Error writing module {module_name} to disk: {str(e)}")
return False
def save_state(self, path: Optional[str] = None) -> bool:
"""
Save the current state of the buffer
Args:
path: Optional path to save to (defaults to persistence_path)
Returns:
True if successful, False otherwise
"""
save_path = path or self.persistence_path
if not save_path:
self.logger.error("No persistence path specified")
return False
try:
# Serialize all modules
modules_data = {
name: module.serialize()
for name, module in self.modules.items()
}
# Create state object
state = {
"modules": modules_data,
"snapshots": self.snapshots,
"timestamp": time.time()
}
with open(save_path, 'wb') as f:
pickle.dump(state, f)
return True
except Exception as e:
self.logger.error(f"Error saving buffer state: {str(e)}")
return False
def load_state(self, path: Optional[str] = None) -> bool:
"""
Load a previously saved state
Args:
path: Optional path to load from (defaults to persistence_path)
Returns:
True if successful, False otherwise
"""
load_path = path or self.persistence_path
if not load_path or not os.path.exists(load_path):
return False
try:
with open(load_path, 'rb') as f:
state = pickle.load(f)
# Load modules
self.modules = {}
for name, module_data in state.get("modules", {}).items():
self.modules[name] = VirtualModule.deserialize(module_data)
# Load snapshots
self.snapshots = state.get("snapshots", {})
# Load each module
for module in self.modules.values():
module.load()
return True
except Exception as e:
self.logger.error(f"Error loading buffer state: {str(e)}")
return False
def clear(self) -> None:
"""Clear all modules and state"""
self.modules = {}
self.snapshots = {}
self.execution_history = []
def get_module_summary(self, module_name: str) -> Dict:
"""
Get a summary of a module's content
Args:
module_name: Name of the module
Returns:
Dictionary with module summary
"""
module = self.get_module(module_name)
if not module:
return {"error": f"Module {module_name} not found"}
# Parse the code
try:
tree = ast.parse(module.code)
# Extract functions
functions = []
for node in ast.walk(tree):
if isinstance(node, ast.FunctionDef):
func = {
"name": node.name,
"line": node.lineno,
"args": [arg.arg for arg in node.args.args],
"decorators": [ast.unparse(dec).strip() for dec in node.decorator_list]
}
# Extract docstring if available
if (node.body and isinstance(node.body[0], ast.Expr) and
isinstance(node.body[0].value, ast.Str)):
func["docstring"] = node.body[0].value.s
functions.append(func)
# Extract classes
classes = []
for node in ast.walk(tree):
if isinstance(node, ast.ClassDef):
cls = {
"name": node.name,
"line": node.lineno,
"bases": [ast.unparse(base).strip() for base in node.bases],
"methods": []
}
# Extract docstring if available
if (node.body and isinstance(node.body[0], ast.Expr) and
isinstance(node.body[0].value, ast.Str)):
cls["docstring"] = node.body[0].value.s
# Extract methods
for child in node.body:
if isinstance(child, ast.FunctionDef):
method = {
"name": child.name,
"line": child.lineno,
"args": [arg.arg for arg in child.args.args],
"decorators": [ast.unparse(dec).strip() for dec in child.decorator_list]
}
# Extract docstring if available
if (child.body and isinstance(child.body[0], ast.Expr) and
isinstance(child.body[0].value, ast.Str)):
method["docstring"] = child.body[0].value.s
cls["methods"].append(method)
classes.append(cls)
return {
"name": module_name,
"source_path": module.source_path,
"in_memory_only": module.in_memory_only,
"last_modified": module.last_modified,
"functions": functions,
"classes": classes,
"line_count": len(module.code.split('\n'))
}
except SyntaxError as e:
return {
"name": module_name,
"source_path": module.source_path,
"in_memory_only": module.in_memory_only,
"last_modified": module.last_modified,
"error": f"Syntax error: {str(e)}",
"line_count": len(module.code.split('\n'))
}
class HotSwapExecutionContext:
"""
Execution context that allows hot-swapping code at runtime
Provides a safe environment for executing dynamically modified code
"""
def __init__(self, buffer: DynamicContextBuffer):
"""
Initialize the execution context
Args:
buffer: The dynamic context buffer to use
"""
self.buffer = buffer
self.globals = {}
self.locals = {}
self.modules_imported = set()
def reset(self) -> None:
"""Reset the execution context"""
self.globals = {}
self.locals = {}
self.modules_imported = set()
def import_module(self, module_name: str) -> bool:
"""
Import a module from the buffer into the execution context
Args:
module_name: Name of the module to import
Returns:
True if successful, False otherwise
"""
module = self.buffer.get_module(module_name)
if not module or not module.module:
return False
self.globals[module_name] = module.module
self.modules_imported.add(module_name)
return True
def execute(self, code: str) -> Dict:
"""
Execute code in this context
Args:
code: Python code to execute
Returns:
Execution results
"""
return self.buffer.execute_code(code, self.globals, self.locals)
def call_function(self, module_name: str, function_name: str,
*args, **kwargs) -> Dict:
"""
Call a function from a module in this context
Args:
module_name: Name of the module
function_name: Name of the function
*args: Positional arguments
**kwargs: Keyword arguments
Returns:
Execution results
"""
# Ensure the module is imported
if module_name not in self.modules_imported:
self.import_module(module_name)
return self.buffer.execute_function(module_name, function_name, *args, **kwargs)
def hot_reload_module(self, module_name: str) -> bool:
"""
Reload a module and update the context
Args:
module_name: Name of the module to reload
Returns:
True if successful, False otherwise
"""
module = self.buffer.get_module(module_name)
if not module:
return False
# Reload the module
reloaded_module = module.reload()
if not reloaded_module:
return False
# Update in our globals
if module_name in self.modules_imported:
self.globals[module_name] = reloaded_module
return True
# === HELPER FUNCTIONS ===
def create_dynamic_context(persistence_path: Optional[str] = None) -> DynamicContextBuffer:
"""
Create a new dynamic context buffer
Args:
persistence_path: Optional path for persisting buffer state
Returns:
Initialized DynamicContextBuffer
"""
return DynamicContextBuffer(persistence_path)
def create_execution_context(buffer: DynamicContextBuffer) -> HotSwapExecutionContext:
"""
Create a new execution context
Args:
buffer: The dynamic context buffer to use
Returns:
Initialized execution context
"""
return HotSwapExecutionContext(buffer)
if __name__ == "__main__":
# Example usage
import argparse
parser = argparse.ArgumentParser(description="Dynamic Context Buffer")
parser.add_argument("--file", help="Python file to load")
parser.add_argument("--exec", help="Code to execute")
parser.add_argument("--save", help="Path to save state")
parser.add_argument("--load", help="Path to load state")
args = parser.parse_args()
buffer = DynamicContextBuffer()
if args.load:
print(f"Loading state from {args.load}")
buffer.load_state(args.load)
if args.file:
print(f"Loading file {args.file}")
module = buffer.add_module_from_file(args.file)
if module:
print(f"Loaded module {module.name}")
if args.exec:
print(f"Executing: {args.exec}")
result = buffer.execute_code(args.exec)
if result["error"]:
print(f"Error: {result['error']}")
else:
print(f"Result: {result['result']}")
if args.save:
print(f"Saving state to {args.save}")
buffer.save_state(args.save)