๐ง AI Safety and Risk MonitoringMay 8, 2026โ
Tests passing
AI Action Monitor
This tool tracks and logs actions generated by AI agents in real-time. It monitors for potentially risky operations, such as file deletions or database modifications, and flags or halts them based on a customizable ruleset. This is useful for ensuring safer deployments of AI agents in production environments.
What It Does
- Real-time monitoring of AI agent actions.
- Customizable rules to flag or halt risky operations.
- Detailed audit logs for traceability.
Installation
1. Clone the repository:
git clone <repository_url>
cd ai_action_monitor2. Install the required dependencies:
pip install -r requirements.txtUsage
Run the tool with the following command:
python ai_action_monitor.py --log_file agent_logs.txt --rules_file safety_rules.json --output_file audit_log.txtArguments
--log_file: Path to the AI agent action log file (required).--rules_file: Path to the JSON ruleset file (required).--output_file: Path to the output log file where flagged actions will be recorded (required).
Example
Suppose you have an AI agent log file agent_logs.txt and a ruleset file safety_rules.json:
agent_logs.txt:
{"type": "file", "operation": "delete", "details": "important_file.txt"}
{"type": "database", "operation": "update", "details": "user_records"}safety_rules.json:
[
{"type": "file", "operation": "delete"},
{"type": "database", "operation": "drop"}
]Run the tool:
python ai_action_monitor.py --log_file agent_logs.txt --rules_file safety_rules.json --output_file audit_log.txtOutput:
Flagged risky action: {'type': 'file', 'operation': 'delete', 'details': 'important_file.txt'}The flagged action will also be logged in audit_log.txt.
Source Code
import argparse
import json
import os
import sys
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
class ActionMonitorHandler(FileSystemEventHandler):
def __init__(self, rules, log_file):
self.rules = rules
self.log_file = log_file
def process_action(self, action):
for rule in self.rules:
if rule['type'] == action['type'] and rule['operation'] == action['operation']:
print(f"Flagged risky action: {action}")
self.log_action(action, flagged=True)
return
self.log_action(action, flagged=False)
def log_action(self, action, flagged):
log_entry = {
'action': action,
'flagged': flagged
}
with open(self.log_file, 'a') as f:
f.write(json.dumps(log_entry) + '\n')
def on_modified(self, event):
if event.is_directory:
return
try:
with open(event.src_path, 'r') as f:
for line in f:
try:
action = json.loads(line.strip())
self.process_action(action)
except json.JSONDecodeError:
print(f"Invalid JSON format in log: {line.strip()}")
except Exception as e:
print(f"Error reading file {event.src_path}: {e}")
def main():
parser = argparse.ArgumentParser(description="AI Action Monitor")
parser.add_argument('--log_file', required=True, help="Path to the AI agent action log file")
parser.add_argument('--rules_file', required=True, help="Path to the JSON ruleset file")
parser.add_argument('--output_file', required=True, help="Path to the output log file")
args = parser.parse_args()
if not os.path.exists(args.log_file):
print(f"Error: Log file '{args.log_file}' does not exist.")
sys.exit(1)
if not os.path.exists(args.rules_file):
print(f"Error: Rules file '{args.rules_file}' does not exist.")
sys.exit(1)
try:
with open(args.rules_file, 'r') as f:
rules = json.load(f)
except json.JSONDecodeError:
print(f"Error: Rules file '{args.rules_file}' contains invalid JSON.")
sys.exit(1)
except Exception as e:
print(f"Error reading rules file '{args.rules_file}': {e}")
sys.exit(1)
event_handler = ActionMonitorHandler(rules, args.output_file)
observer = Observer()
observer.schedule(event_handler, path=os.path.dirname(args.log_file) or '.', recursive=False)
observer.start()
print("Monitoring AI agent actions. Press Ctrl+C to stop.")
try:
while True:
pass
except KeyboardInterrupt:
observer.stop()
observer.join()
if __name__ == "__main__":
main()Community
Downloads
ยทยทยท
Rate this tool
No ratings yet โ be the first!
Details
- Tool Name
- ai_action_monitor
- Category
- AI Safety and Risk Monitoring
- Generated
- May 8, 2026
- Tests
- Passing โ
Quick Install
Clone just this tool:
git clone --depth 1 --filter=blob:none --sparse \ https://github.com/ptulin/autoaiforge.git cd autoaiforge git sparse-checkout set generated_tools/2026-05-08/ai_action_monitor cd generated_tools/2026-05-08/ai_action_monitor pip install -r requirements.txt 2>/dev/null || true python ai_action_monitor.py