एजेंट कार्रवाइयों के लिए नीति प्रवर्तन
कार्रवाई-पूर्व नीति जाँच, अनुमत/निषिद्ध सूचियाँ और गतिशील नीति नियम।
एजेंट कार्रवाइयों के लिए नीति प्रवर्तन, CoddyKit पर AI एजेंट का एक निःशुल्क पाठ है। यह 4 में से 2वाँ पाठ है। आप नीचे पूरा पाठ निःशुल्क पढ़ सकते हैं—फिर अंतर्निहित कोड संपादक और 24/7 एआई ट्यूटर के साथ ब्राउज़र में इसका व्यावहारिक अभ्यास कर सकते हैं। यह AI एजेंट सीखने के मार्ग का हिस्सा है और आपकी प्रगति वेब तथा CoddyKit ऐप पर सिंक होती रहती है। AI एजेंट पाठ्यक्रम में कुल 4 पाठ शामिल हैं।
एजेंट की नीति प्रवर्तन क्या है
नीति प्रवर्तन वह रनटाइम नियंत्रण-द्वार है जो यह तय करने के लिए एजेंट की हर कार्रवाई से पहले चलता है कि कार्रवाई की अनुमति है या नहीं। इसके बिना एजेंट की एकमात्र सीमा LLM का निर्देशों का पालन करना है — जिसे दरकिनार किया जा सकता है या जिसका गलत अर्थ निकाला जा सकता है।
प्रवर्तन LLM के बाहर, आपके बुनियादी ढाँचे में होना चाहिए।
कार्रवाई-पूर्व जाँच का पैटर्न
किसी भी उपकरण को चलाने से पहले can_agent_do(action, context) को कॉल करें। यह फ़ंक्शन प्रवर्तन का एकमात्र बिंदु है — कार्रवाई चलाने तक पहुँचने वाला हर मार्ग इसी से होकर गुजरता है।
def can_agent_do(action: str, context: dict) -> tuple[bool, str]:
'''
Returns (allowed: bool, reason: str).
Context includes: user_id, agent_id, session_id, parameters, timestamp.
'''
# 1. Check denylist first (fast path for obvious violations)
if action in DENIED_ACTIONS:
return False, f'Action "{action}" is on the global denylist'
# 2. Check allowlist
if action not in ALLOWED_ACTIONS:
return False, f'Action "{action}" is not on the allowlist'
# 3. Context-specific checks
return check_context_policy(action, context)अनुमति-सूची और निषेध-सूची तय करना
अनुमति-सूची में एजेंट द्वारा की जा सकने वाली हर कार्रवाई दर्ज होती है। सूची में न होने वाली हर चीज़ डिफ़ॉल्ट रूप से रोक दी जाती है। निषेध-सूची उन कार्रवाइयों के लिए अतिरिक्त सुरक्षा देती है जिन्हें संदर्भ चाहे जो हो, कभी अनुमति नहीं दी जानी चाहिए।
# Allowlist: tools the agent can use
ALLOWED_ACTIONS = {
'web_search',
'read_file',
'write_file',
'send_email',
'create_calendar_event',
'query_database',
'execute_python_sandbox',
'fetch_url',
'create_ticket'
}
# Denylist: actions that are always blocked, regardless of context
DENIED_ACTIONS = {
'delete_all_records',
'export_entire_database',
'send_mass_email',
'modify_system_config',
'create_admin_user',
'disable_audit_logging'
}
if __name__ == '__main__':
for action in ('web_search', 'send_mass_email'):
print(f"{action}: allowed={action in ALLOWED_ACTIONS} denied={action in DENIED_ACTIONS}")
संदर्भ-विशिष्ट नीति जाँच
साधारण अनुमति या निषेध सूचियों से आगे, नीतियाँ अक्सर संदर्भ पर निर्भर करती हैं: उपयोगकर्ता कौन है, उसकी भूमिका क्या है, समय क्या है और लक्षित संसाधन कौन-सा है? ये संदर्भ-विशिष्ट जाँच हैं।
from datetime import datetime, timezone
def check_context_policy(action: str, context: dict) -> tuple[bool, str]:
user_id = context.get('user_id', '')
params = context.get('parameters', {})
user_role = context.get('user_role', 'user')
# send_email: only agents with email_sender role
if action == 'send_email':
if user_role not in ('email_agent', 'admin'):
return False, f'Role "{user_role}" cannot send emails'
recipient = params.get('to', '')
if not recipient.endswith('@trusted-domain.com'):
return False, 'Email recipient must be in @trusted-domain.com'
# write_file: path restrictions
if action == 'write_file':
path = params.get('path', '')
if not path.startswith('/tmp/') and not path.startswith('/workspace/'):
return False, f'File writes outside /tmp/ and /workspace/ are not allowed'
return True, 'Permitted'
if __name__ == '__main__':
ctx = {'user_id': 'u1', 'user_role': 'user',
'parameters': {'to': 'someone@gmail.com'}}
print('send_email as user:', check_context_policy('send_email', ctx))
ctx2 = {'user_id': 'u1', 'user_role': 'user',
'parameters': {'path': '/etc/passwd'}}
print('write_file outside sandbox:', check_context_policy('write_file', ctx2))
नीति इंजन से गतिशील नीतियाँ
वास्तविक उपयोग में पहले से लिखी गई नीतियों को अपडेट करना कठिन होता है। नीति इंजन (जैसे OPA — ओपन पॉलिसी एजेंट) का उपयोग करें, ताकि नीतियों का मूल्यांकन कोड के बजाय डेटा के रूप में परिभाषित नीतियों पर हो। एजेंट को दोबारा लागू किए बिना नीतियों को अपडेट किया जा सकता है।
import requests
OPA_URL = 'http://localhost:8181/v1/data/agent/allow'
def opa_policy_check(action: str, context: dict) -> tuple[bool, str]:
payload = {
'input': {
'action': action,
'user_id': context.get('user_id'),
'role': context.get('user_role', 'user'),
'params': context.get('parameters', {}),
'time_utc': datetime.now(timezone.utc).isoformat()
}
}
try:
resp = requests.post(OPA_URL, json=payload, timeout=0.5)
result = resp.json().get('result', {})
allowed = result.get('allow', False)
reason = result.get('reason', 'Policy decision')
return allowed, reason
except Exception as e:
# Fail closed: deny if policy engine is unreachable
return False, f'Policy engine unavailable: {e}'विफल-बंद बनाम विफल-खुला
जब नीति इंजन उपलब्ध न हो, तो आपके पास दो विकल्प होते हैं:
- विफल-बंद: सभी कार्रवाइयों को अस्वीकार करें। यह सुरक्षित है, लेकिन एजेंट काम करना बंद कर देता है।
- विफल-खुला: सभी कार्रवाइयों की अनुमति दें। एजेंट काम करता रहता है, लेकिन नीति लागू नहीं रहती।
सुरक्षा-संवेदनशील एजेंटों के लिए हमेशा विफल-बंद अपनाएँ। कम-जोखिम वाली कार्रवाइयों वाले उत्पादकता एजेंटों में विफल-खुला स्वीकार्य हो सकता है।
FAIL_CLOSED = True # Configure per agent
def safe_policy_check(action: str, context: dict) -> tuple[bool, str]:
try:
return can_agent_do(action, context)
except Exception as e:
if FAIL_CLOSED:
return False, f'Policy check failed (fail-closed): {e}'
else:
# Log the failure but allow the action
import logging
logging.warning('Policy check error (fail-open): %s', e)
return True, 'Policy check bypassed due to error (fail-open)'कार्रवाइयों की दर-सीमा तय करना
नीति प्रवर्तन में दर-सीमाएँ शामिल हो सकती हैं: एजेंट को ईमेल भेजने की अनुमति हो सकती है, लेकिन प्रति सत्र केवल 5 ईमेल। सीमा पार होते ही कार्रवाई अस्वीकार कर दी जाती है।
from collections import defaultdict
import time
action_counts: dict[str, dict[str, int]] = defaultdict(lambda: defaultdict(int))
action_window_start: dict[str, float] = defaultdict(float)
ACTION_RATE_LIMITS = {
'send_email': {'limit': 5, 'window_secs': 3600}, # 5/hour
'write_file': {'limit': 50, 'window_secs': 300},
'web_search': {'limit': 20, 'window_secs': 60}
}
def check_rate_limit(action: str, session_id: str) -> tuple[bool, str]:
limit_config = ACTION_RATE_LIMITS.get(action)
if not limit_config:
return True, 'No rate limit defined'
window = limit_config['window_secs']
now = time.time()
key = f'{session_id}:{action}'
if now - action_window_start[key] > window:
action_counts[key] = defaultdict(int)
action_window_start[key] = now
action_counts[key]['count'] += 1
if action_counts[key]['count'] > limit_config['limit']:
return False, f'Rate limit exceeded: {action} ({limit_config["limit"]}/{window}s)'
return True, 'Within rate limit'
if __name__ == '__main__':
for i in range(6):
allowed, reason = check_rate_limit('send_email', 'session-1')
print(f'After 6 send_email calls: allowed={allowed}, reason={reason}')
नीति उल्लंघनों की लॉगिंग
हर नीति-अस्वीकृति को पूरे संदर्भ के साथ लॉग में दर्ज किया जाना चाहिए। एजेंट के अप्रत्याशित व्यवहार में त्रुटि खोजते समय या सुरक्षा घटना की जाँच करते समय सबसे पहले इन्हीं लॉग को देखना चाहिए।
import logging, json, time
policy_logger = logging.getLogger('agent.policy')
def enforced_action(agent_id: str, user_id: str, action: str,
context: dict, audit_log) -> tuple[bool, str]:
allowed, reason = safe_policy_check(action, context)
log_entry = {
'ts': time.time(),
'agent_id': agent_id,
'user_id': user_id,
'action': action,
'allowed': allowed,
'reason': reason,
'params': context.get('parameters', {})
}
if allowed:
policy_logger.info('ALLOWED %s', json.dumps(log_entry))
else:
policy_logger.warning('DENIED %s', json.dumps(log_entry))
audit_log.append(
agent_id, user_id,
f'POLICY_{"ALLOW" if allowed else "DENY"}',
{'action': action},
{'allowed': allowed, 'reason': reason},
context.get('session_id', '')
)
return allowed, reasonसंसाधन का दायरा सीमित करना
अनुमति वाली कार्रवाइयों के लिए भी उन संसाधनों का दायरा सीमित करें जिन तक एजेंट पहुँच सकता है। उपयोगकर्ता A के दस्तावेज़ सँभालने वाले एजेंट को उपयोगकर्ता B की फ़ाइलें पढ़ने में सक्षम नहीं होना चाहिए, भले ही read_file अनुमति-सूची में हो।
def check_resource_scope(action: str, context: dict) -> tuple[bool, str]:
user_id = context.get('user_id', '')
params = context.get('parameters', {})
if action == 'read_file':
path = params.get('path', '')
# Each user's files must be under their own namespace
if not path.startswith(f'/workspace/{user_id}/'):
return False, (
f'User {user_id} cannot read files outside '
f'/workspace/{user_id}/'
)
if action == 'query_database':
table = params.get('table', '')
allowed_tables = {'products', 'public_docs', f'user_{user_id}_data'}
if table not in allowed_tables:
return False, f'Table "{table}" not in scope for user {user_id}'
return True, 'Resource scope check passed'
if __name__ == '__main__':
ctx = {'user_id': 'u1', 'parameters': {'path': '/workspace/u2/secret.txt'}}
print('Cross-user file read:', check_resource_scope('read_file', ctx))
ctx2 = {'user_id': 'u1', 'parameters': {'path': '/workspace/u1/notes.txt'}}
print('Own file read: ', check_resource_scope('read_file', ctx2))
नीति नियमों का परीक्षण
नीति नियम कोड होते हैं — उनका परीक्षण किया जाना चाहिए। हर नियम के लिए इकाई परीक्षण लिखें, ताकि यह सुनिश्चित हो कि अस्वीकृतियाँ और अनुमतियाँ सही ढंग से काम करती हैं और सीमांत स्थितियाँ अनजाने में नीति को दरकिनार न कर दें।
def test_policy_rules():
# Denylist blocks unconditionally
ok, msg = can_agent_do('delete_all_records', {'user_role': 'admin'})
assert not ok, 'Denylist should block even for admin'
# Email requires trusted domain
ok, msg = can_agent_do('send_email', {
'user_role': 'email_agent',
'parameters': {'to': 'attacker@evil.com'}
})
assert not ok, 'Should block untrusted email recipient'
# File write outside allowed paths
ok, msg = can_agent_do('write_file', {
'user_role': 'user',
'parameters': {'path': '/etc/crontab'}
})
assert not ok, 'Should block write to /etc/'
print('All policy tests passed')
test_policy_rules()नीति निर्णयों को कैश करना
हर कार्रवाई पर नीति इंजन को कॉल करने से विलंब बढ़ता है, विशेषकर बाहरी OPA सेवा का उपयोग करते समय। बार-बार होने वाले आवागमन को घटाने के लिए (कार्रवाई, संदर्भ_हैश) युग्मों के हाल के निर्णयों को कम TTL अवधि के लिए कैश करें।
import hashlib, time
policy_cache: dict[str, dict] = {}
POLICY_CACHE_TTL = 10 # seconds — short TTL so policy updates take effect quickly
def cached_policy_check(action: str, context: dict) -> tuple[bool, str]:
ctx_hash = hashlib.md5(
f'{action}:{context.get("user_id")}:{context.get("user_role")}'
.encode()
).hexdigest()
key = f'{action}:{ctx_hash}'
entry = policy_cache.get(key)
if entry and time.time() - entry['ts'] < POLICY_CACHE_TTL:
return entry['result']
result = can_agent_do(action, context)
policy_cache[key] = {'result': result, 'ts': time.time()}
return resultनीति प्रवर्तन में 'विफल-बंद' दृष्टिकोण क्या है
विफल-बंद बनाम विफल-खुला निर्णय किसी भी नीति प्रवर्तन तंत्र के लिए एक मूलभूत सुरक्षा-संतुलन है। यह जानना कि कौन-सा दृष्टिकोण कब उपयुक्त है, शासन की मूल अवधारणा है।
नीति प्रवर्तन की पुनरावृत्ति
एजेंट नीति प्रवर्तन में शामिल हैं: एकमात्र नियंत्रण-द्वार के रूप में कार्रवाई-पूर्व जाँच, मूल निर्णयों के लिए अनुमति-सूचियाँ + निषेध-सूचियाँ, संसाधन का दायरा सीमित करने और भूमिका जाँचने के लिए संदर्भ-विशिष्ट नियम, अपडेट किए जा सकने वाले नियमों के लिए गतिशील नीति इंजन (OPA), प्रति सत्र प्रत्येक कार्रवाई की दर-सीमाएँ, और सुरक्षा-महत्वपूर्ण एजेंटों के लिए डिफ़ॉल्ट रूप से विफल-बंद व्यवहार।
एआई शिक्षक के साथ AI एजेंट सीखें — निःशुल्क
अपने ब्राउज़र में वास्तविक कोड लिखें और चलाएँ, चौबीसों घंटे एआई शिक्षक से तुरंत सहायता पाएँ, और वेब या ऐप पर वहीं से शुरू करें जहाँ आपने छोड़ा था।
- पाठ्यक्रम
- 60
- पाठ
- 239
अक्सर पूछे जाने वाले प्रश्न
क्या “एजेंट कार्रवाइयों के लिए नीति प्रवर्तन” पाठ निःशुल्क है?
हाँ—“एजेंट कार्रवाइयों के लिए नीति प्रवर्तन” का पूरा पाठ यहाँ वेब पर निःशुल्क पढ़ा जा सकता है। इंटरैक्टिव अभ्यास (अंतर्निहित कोड संपादक और 24/7 एआई ट्यूटर) करने और AI एजेंट पाठ्यक्रम का बाकी हिस्सा अनलॉक करने के लिए CoddyKit PRO लें। AI एजेंट पाठ्यक्रम में कुल 4 पाठ शामिल हैं।
“एजेंट कार्रवाइयों के लिए नीति प्रवर्तन” में मैं क्या सीखूँगा?
कार्रवाई-पूर्व नीति जाँच, अनुमत/निषिद्ध सूचियाँ और गतिशील नीति नियम। आप ब्राउज़र में सीधे चलाए जाने वाले व्यावहारिक कोड के साथ AI एजेंट का अभ्यास करते हैं, और पाठ पूरा करते समय 24/7 एआई ट्यूटर आपके प्रश्नों के उत्तर देता है।
क्या AI एजेंट शुरू करने के लिए मुझे किसी अनुभव की आवश्यकता है?
पहले के अनुभव की आवश्यकता नहीं है। CoddyKit पर AI एजेंट शुरुआती से लेकर उन्नत शिक्षार्थियों तक सभी के लिए व्यवस्थित किया गया है, इसलिए आप यहीं से या शुरुआत से सीखना शुरू कर सकते हैं और अपनी गति से आगे बढ़ सकते हैं। यह 4 में से 2वाँ पाठ है।
“एजेंट कार्रवाइयों के लिए नीति प्रवर्तन” पाठ पूरा करने में कितना समय लगता है?
CoddyKit का अधिकांश पाठ लगभग 5–10 मिनट में पूरा हो जाता है। हर पाठ छोटा और संवादात्मक है, इसलिए आप लगातार प्रगति करते हैं और वेब या ऐप पर वहीं से सीखना जारी रख सकते हैं जहाँ आपने छोड़ा था।
क्या मैं इस AI एजेंट पाठ में कोड लिख और चला सकता हूँ?
हाँ। हर AI एजेंट पाठ में एक अंतर्निर्मित कोड संपादक शामिल है, जिससे आप सीधे अपने ब्राउज़र में वास्तविक कोड लिख और चला सकते हैं और तुरंत एआई प्रतिक्रिया पा सकते हैं—स्थानीय सेटअप की आवश्यकता नहीं है।
इस पाठ्यक्रम के सभी पाठ
- एजेंट के लिए अपरिवर्तनीय कार्रवाई लॉगिंग
- एजेंट कार्रवाइयों के लिए नीति प्रवर्तन
- नियामकीय अनुपालन: GDPR और SOC2
- मानव-निगरानी अनुमोदन द्वार