repositories
loading repo index
repositories
loading repo index
repository
loading code, commits, and activity
Mirrored from https://github.com/yingqi-z20/Agent-libOS
stars
latest
clone command
git clone gitlawb://did:key:z6MkqRzA...RfoM/yingqi-z20-Agen...git clone gitlawb://did:key:z6MkqRzA.../yingqi-z20-Agen...d98dd2c9IPC1d ago| #1 | from __future__ import annotations |
| #2 | |
| #3 | from agent_libos.utils.ids import new_id, utc_now |
| #4 | from agent_libos.models import Checkpoint, EventType |
| #5 | from agent_libos.runtime.audit_manager import AuditManager |
| #6 | from agent_libos.runtime.event_bus import EventBus |
| #7 | from agent_libos.storage import SQLiteStore |
| #8 | |
| #9 | |
| #10 | class CheckpointManager: |
| #11 | def __init__(self, store: SQLiteStore, audit: AuditManager, events: EventBus): |
| #12 | self.store = store |
| #13 | self.audit = audit |
| #14 | self.events = events |
| #15 | |
| #16 | def checkpoint(self, pid: str, reason: str) -> str: |
| #17 | checkpoint = Checkpoint( |
| #18 | checkpoint_id=new_id("ckpt"), |
| #19 | pid=pid, |
| #20 | reason=reason, |
| #21 | created_at=utc_now(), |
| #22 | ) |
| #23 | snapshot = self.store.snapshot_tables() |
| #24 | self.store.insert_checkpoint(checkpoint, snapshot) |
| #25 | process = self.store.get_process(pid) |
| #26 | if process is not None: |
| #27 | process.checkpoint_head = checkpoint.checkpoint_id |
| #28 | process.updated_at = utc_now() |
| #29 | self.store.update_process(process) |
| #30 | self.events.emit( |
| #31 | EventType.CHECKPOINT_CREATED, |
| #32 | source=pid, |
| #33 | target=pid, |
| #34 | payload={"checkpoint_id": checkpoint.checkpoint_id, "reason": reason}, |
| #35 | ) |
| #36 | self.audit.record( |
| #37 | actor=pid, |
| #38 | action="checkpoint.create", |
| #39 | target=f"checkpoint:{checkpoint.checkpoint_id}", |
| #40 | decision={"reason": reason}, |
| #41 | ) |
| #42 | return checkpoint.checkpoint_id |
| #43 | |
| #44 | def rollback(self, pid: str, checkpoint_id: str) -> dict[str, str]: |
| #45 | found = self.store.get_checkpoint_snapshot(checkpoint_id) |
| #46 | if found is None: |
| #47 | raise KeyError(f"checkpoint not found: {checkpoint_id}") |
| #48 | checkpoint, snapshot = found |
| #49 | self.store.restore_tables(snapshot) |
| #50 | self.events.emit( |
| #51 | EventType.ROLLBACK, |
| #52 | source=pid, |
| #53 | target=checkpoint.pid, |
| #54 | payload={"checkpoint_id": checkpoint_id}, |
| #55 | ) |
| #56 | self.audit.record( |
| #57 | actor=pid, |
| #58 | action="checkpoint.rollback", |
| #59 | target=f"checkpoint:{checkpoint_id}", |
| #60 | decision={"restored_for": checkpoint.pid}, |
| #61 | ) |
| #62 | return {"checkpoint_id": checkpoint_id, "pid": checkpoint.pid, "status": "rolled_back"} |
| #63 | |
| #64 |