Implement an Observation-Action cycle logger that validates proper sequence:
log_observation(): Starts new cycle with observationlog_thought(): Adds thought to current cycle (must have observation first)log_action(): Completes cycle with action (must have thought first)get_cycles(): Returns list of completed cycles
Validation Rules:
- Must log in order: observation → thought → action
- Skip logging and return False if sequence violated
- Complete cycle only when action is logged
- Each cycle:
{'observation': ..., 'thought': ..., 'action': ...}
Return Values:
- log_* methods return True if logged successfully, False if sequence error
Examples
Example 1:
Input:
logger = OALogger(); logger.log_observation('User asks weather'); logger.log_thought('Need weather API'); logger.log_action('call_weather_api()'); len(logger.get_cycles())Output:
1Explanation: Complete cycle logged, one cycle in history
Example 2:
Input:
logger = OALogger(); logger.log_thought('No observation')Output:
FalseExplanation: Thought without observation returns False
Starter Code
class OALogger:
"""
Logger for Observation → Thought → Action cycles.
Validates and records agent reasoning traces.
"""
def __init__(self):
self.cycles = []
self.current_cycle = {}
def log_observation(self, observation):
"""Log observation, starting new cycle"""
# Your implementation here
pass
def log_thought(self, thought):
"""Log thought for current cycle"""
# Your implementation here
pass
def log_action(self, action):
"""Log action and complete cycle"""
# Your implementation here
pass
def get_cycles(self):
"""Return all completed cycles"""
# Your implementation here
passPython3
ReadyLines: 1Characters: 0
Ready