Skip to content

مراقبة وكيل البرمجة المباشر

راقب وكلاء البرمجة أثناء عملهم في الوقت الفعلي مع الإيقاف المؤقت والتراجع والتفريع. ثلاث واجهات خلفية مدعومة: Ollama للنماذج المحلية، وAnthropic API، وClaude Agent SDK.

جديد في الإصدار v2.4.0

يخبرك التعليق التوضيحي على التتبعات الساكنة بما فعله الوكيل. أما المراقبة المباشرة فتخبرك بما يفعله الوكيل استجابةً للتوجيه البشري. يتيح وضع وكيل البرمجة المباشر في Potato للمعلّقين مراقبة وكيل برمجة أثناء عمله في الوقت الفعلي -- يقرأ الملفات، ويحرر الكود، ويشغّل الاختبارات -- والتدخل في أي لحظة. أوقف الوكيل مؤقتاً، أو أرسل تعليمات جديدة، أو تراجع إلى نقطة تحقق سابقة، أو فرّع المسار لاستكشاف مقاربات بديلة.

ينتج هذا بيانات تعليق توضيحي أغنى مما تنتجه التتبعات الساكنة وحدها. تحصل على المسار الكامل بطوابعه الزمنية، وتدخلات المعلّق، ونقاط قرار التفريع، وبيانات مقارنة من المسارات البديلة. وهذه البيانات مفيدة مباشرةً في تدريب نماذج مكافأة العملية، ونماذج التفضيل، ومقيّمات اتباع التعليمات.

المتطلبات

  • Python 3.10+
  • Git (يعتمد نظام نقاط التحقق على commits في git)
  • إحدى الواجهات الخلفية التالية للوكيل:
    • Ollama للاستدلال بنماذج محلية (لا يلزم مفتاح API)
    • ANTHROPIC_API_KEY للوصول إلى Anthropic API
    • Claude Agent SDK لتجربة وكيل Claude Code الكاملة

الواجهات الخلفية

يدعم Potato ثلاث واجهات خلفية لتشغيل وكلاء البرمجة. تشغّل كل واجهة الوكيل في عملية فرعية وتبث إجراءاته إلى واجهة التعليق التوضيحي في الوقت الفعلي.

1. Ollama (نماذج محلية)

شغّل وكلاء البرمجة محلياً من دون مفتاح API. يوفر Ollama استدلالاً سريعاً للنماذج مفتوحة الأوزان. وهو الأنسب للتطوير والاختبار والحالات التي لا يجوز فيها للبيانات مغادرة الجهاز المحلي.

الإعداد:

bash
# Install Ollama
curl -fsSL https://ollama.com/install.sh | sh
 
# Pull a coding-capable model
ollama pull qwen2.5-coder:7b
 
# Or a larger model for better performance
ollama pull deepseek-coder-v2:16b

التهيئة:

yaml
agentic:
  enabled: true
  display_type: coding_trace
  live_agent:
    enabled: true
    backend: ollama
    model: qwen2.5-coder:7b
 
    ollama:
      host: "http://localhost:11434"    # Ollama server URL
      temperature: 0.2
      num_ctx: 8192                     # context window size
      num_predict: 2048                 # max tokens per response
      keep_alive: "5m"                  # keep model loaded in memory
 
    # Agent capabilities
    tools:
      - read_file
      - edit_file
      - write_file
      - bash
      - glob
      - grep
    max_steps: 50
    step_timeout_seconds: 60

2. Anthropic API

استخدم نماذج Claude عبر Anthropic API. يعطي أداءً قوياً في البرمجة مع قدرات استخدام الأدوات. ويتطلب مفتاح API.

الإعداد:

bash
# Set your API key
export ANTHROPIC_API_KEY="sk-ant-..."
 
# Or add to .env file
echo "ANTHROPIC_API_KEY=sk-ant-..." >> .env

التهيئة:

yaml
agentic:
  enabled: true
  display_type: coding_trace
  live_agent:
    enabled: true
    backend: anthropic
    model: claude-sonnet-4-20250514
 
    anthropic:
      api_key: ${ANTHROPIC_API_KEY}
      max_tokens: 4096
      temperature: 0.2
      system_prompt: |
        You are a coding assistant working on a software project.
        Read files before editing them. Run tests after making changes.
        Explain your reasoning before each action.
 
    # Agent capabilities
    tools:
      - read_file
      - edit_file
      - write_file
      - bash
      - glob
      - grep
    max_steps: 100
    step_timeout_seconds: 120

3. Claude Agent SDK

يوفر Claude Agent SDK تجربة وكيل Claude Code الكاملة، بما فيها تنسيق الأدوات التلقائي، وإدارة السياق، والتفكير عبر ملفات متعددة. وهذه أقدر الواجهات الخلفية، لكنها تتطلب تثبيت الـ SDK.

الإعداد:

bash
# Install the Claude Agent SDK
pip install claude-agent-sdk
 
# Set your API key
export ANTHROPIC_API_KEY="sk-ant-..."

التهيئة:

yaml
agentic:
  enabled: true
  display_type: coding_trace
  live_agent:
    enabled: true
    backend: claude_agent_sdk
 
    claude_agent_sdk:
      api_key: ${ANTHROPIC_API_KEY}
      model: claude-sonnet-4-20250514
      max_turns: 100
      permission_mode: auto           # auto-approve tool use
      enable_thinking: true           # show extended thinking
 
    max_steps: 100
    step_timeout_seconds: 180

عناصر التحكم

توفر واجهة التعليق التوضيحي أربعة إجراءات تحكم تتيح للمعلّقين توجيه سلوك الوكيل.

إيقاف مؤقت / استئناف

انقر Pause لإيقاف الوكيل بين الخطوات. ينهي الوكيل خطوته الحالية ثم ينتظر. ويمكن للمعلّق مراجعة الحالة الراهنة، وفحص الملفات، وتقرير ما إذا كان سيدع الوكيل يواصل أم سيتدخل. انقر Resume ليتابع الوكيل.

yaml
live_agent:
  controls:
    pause_resume:
      enabled: true
      auto_pause_on_error: true      # pause when a command fails
      auto_pause_after_steps: 0      # pause after N steps (0 = disabled)
      keyboard_shortcut: "Space"

إرسال التعليمات

أثناء إيقاف الوكيل مؤقتاً، يمكن للمعلّقين إرسال تعليمات جديدة تعيد توجيهه. وهذا مفيد حين يسلك الوكيل مساراً خاطئاً، أو حين يريد المعلّق اختبار استجابته للتوجيه.

yaml
live_agent:
  controls:
    send_instructions:
      enabled: true
      placeholder: "Type instructions for the agent..."
      inject_as: system_message      # "system_message" or "user_message"
      keyboard_shortcut: "Enter"
      presets:
        - "Try a different approach"
        - "Read the error message more carefully"
        - "Check the test file for expected behavior"
        - "Revert your last change and try again"

تُحقن التعليمات في سياق محادثة الوكيل. ويتحكم خيار inject_as في ظهورها كرسالة نظام (تعليمة آمرة) أو كرسالة مستخدم (توجيه حواري).

التراجع

يعيد التراجع المشروع إلى نقطة تحقق سابقة في git. فكل تغيير يجريه الوكيل على الملفات يُودَع تلقائياً، ما يتيح للمعلّق النقر على أي خطوة سابقة في الشريط الزمني والعودة إلى تلك الحالة بالضبط. ويُقتطع سياق محادثة الوكيل بما يوافقها.

yaml
live_agent:
  controls:
    rollback:
      enabled: true
      show_checkpoint_diff: true     # show what will be undone
      require_confirmation: true     # "Are you sure?" dialog
      keyboard_shortcut: "Ctrl+Z"

التفريع وإعادة التشغيل

يجمع التفريع وإعادة التشغيل بين التراجع وإرسال التعليمات. يعود المعلّق إلى نقطة تحقق ويرسل تعليمات مختلفة، فينشأ مسار متفرع. وهذا يفيد عند جمع بيانات التفضيل: إذ يمكنك استكشاف مقاربتين مختلفتين من نقطة البداية نفسها ومقارنة النتائج.

yaml
live_agent:
  controls:
    branch:
      enabled: true
      max_branches: 5                # maximum branches from any checkpoint
      branch_naming: auto            # "auto" or "manual"
      compare_view: true             # side-by-side branch comparison
      keyboard_shortcut: "Ctrl+B"

يعرض عرض مقارنة الفروع فرعين جنباً إلى جنب مع إبراز موضع تباعدهما. ويمكن للمعلّقين تقييم أي فرع أنتج نتائج أفضل، فتتولد أزواج تفضيل لتدريب DPO.

نظام نقاط التحقق في Git

يستخدم وضع الوكيل المباشر git لتتبع كل تغيير في الملفات. وهذا يوفر تراجعاً موثوقاً وتفريعاً وسجلّ تغييرات كاملاً.

كيف يعمل

  1. قبل أن يبدأ الوكيل، ينشئ Potato فرع git جديداً باسم potato-session-{session_id}
  2. بعد كل تغيير في الملفات (تحرير، كتابة، إنشاء، حذف)، يودع Potato تلقائياً برسالة وصفية
  3. يُوسَم كل إيداع نقطةَ تحقق تظهر في الشريط الزمني
  4. يستخدم التراجع git checkout لاستعادة مجلد العمل إلى أي نقطة تحقق
  5. ينشئ التفريع فرع git جديداً من إيداع نقطة التحقق

التهيئة

yaml
live_agent:
  git_checkpoints:
    enabled: true
    branch_prefix: "potato-session"
    commit_message_format: "Step {step}: {tool} {file_path}"
    auto_commit: true
    cleanup_on_complete: false       # delete session branches when done
    require_clean_working_dir: true  # fail if there are uncommitted changes

إدارة نقاط التحقق يدوياً

bash
# List all Potato session branches
git branch | grep potato-session
 
# View checkpoints for a session
git log potato-session-abc123 --oneline
 
# Clean up old session branches
python -m potato.cleanup_sessions --older-than 7d

صيغة البيانات

تحدد بيانات الإدخال لمهام وكيل البرمجة المباشر وصف المهمة، واختيارياً ملف أو مجلد بداية:

json
{
  "id": "task_001",
  "task_description": "Fix the bug in src/parser.py where empty input causes a crash",
  "project_dir": "/path/to/project",
  "start_file": "src/parser.py",
  "test_command": "python -m pytest tests/test_parser.py -v",
  "context_files": [
    "src/parser.py",
    "tests/test_parser.py"
  ]
}
الحقلمطلوبالوصف
idنعممعرّف فريد للمهمة
task_descriptionنعمما ينبغي للوكيل فعله
project_dirنعممسار مجلد المشروع
start_fileلاالملف الذي يُعرض للوكيل في البداية
test_commandلاالأمر الذي يتحقق من الإصلاح
context_filesلاملفات تُحمَّل مسبقاً في سياق الوكيل

مرجع التهيئة

تهيئة كاملة لمهمة مراقبة وكيل برمجة مباشر:

yaml
task_name: "Live Coding Agent Observation"
task_dir: "."
 
data_files:
  - "data/coding_tasks.jsonl"
 
item_properties:
  id_key: id
  text_key: task_description
 
agentic:
  enabled: true
  display_type: coding_trace
 
  coding_trace_display:
    diff_style: unified
    diff_context_lines: 3
    syntax_highlight: true
    show_line_numbers: true
    terminal_theme: dark
    file_tree:
      enabled: true
      position: left
      click_to_navigate: true
 
  live_agent:
    enabled: true
    backend: anthropic
    model: claude-sonnet-4-20250514
 
    anthropic:
      api_key: ${ANTHROPIC_API_KEY}
      max_tokens: 4096
      temperature: 0.2
 
    tools:
      - read_file
      - edit_file
      - write_file
      - bash
      - glob
      - grep
 
    max_steps: 100
    step_timeout_seconds: 120
 
    controls:
      pause_resume:
        enabled: true
        auto_pause_on_error: true
        keyboard_shortcut: "Space"
      send_instructions:
        enabled: true
        inject_as: system_message
        presets:
          - "Try a different approach"
          - "Read the error message carefully"
          - "Run the tests first"
      rollback:
        enabled: true
        require_confirmation: true
      branch:
        enabled: true
        max_branches: 5
        compare_view: true
 
    git_checkpoints:
      enabled: true
      branch_prefix: "potato-session"
      auto_commit: true
      cleanup_on_complete: false
 
annotation_schemes:
  # Per-step ratings during observation
  - annotation_type: per_turn_rating
    name: step_quality
    description: "Rate each agent step as you observe it"
    target: agentic_steps
    rating_type: radio
    labels:
      - "Good"
      - "Acceptable"
      - "Unnecessary"
      - "Incorrect"
 
  # Overall task completion after agent finishes
  - annotation_type: radio
    name: task_completion
    description: "Did the agent complete the task?"
    labels:
      - "Fully Complete"
      - "Partially Complete"
      - "Failed"
 
  # Branch comparison (when branching is used)
  - annotation_type: radio
    name: branch_preference
    description: "Which branch produced a better result?"
    labels:
      - "Branch A"
      - "Branch B"
      - "Both Equal"
      - "Both Failed"
 
  # Notes on the observation
  - annotation_type: text
    name: observation_notes
    description: "Describe what you observed and any interventions you made"
    label_requirement:
      required: false
 
output_annotation_dir: "output/"
output_annotation_format: "jsonl"

تصدير المسارات المتفرعة

حين يستخدم المعلّقون التفريع وإعادة التشغيل، تتضمن المخرجات شجرة التفريع كاملة. وقد صُممت هذه الصيغة لتدريب نماذج التفضيل ونماذج مكافأة العملية من مسارات قابلة للمقارنة.

json
{
  "id": "task_001",
  "annotator": "observer_01",
  "root_branch": {
    "branch_id": "main",
    "steps": [
      {"step": 0, "type": "file_read", "file": "src/parser.py", "rating": "Good"},
      {"step": 1, "type": "edit", "file": "src/parser.py", "rating": "Incorrect"}
    ],
    "children": [
      {
        "branch_id": "branch_1",
        "branch_point": 1,
        "instruction": "Try a different approach -- use a try/except block instead",
        "steps": [
          {"step": 2, "type": "edit", "file": "src/parser.py", "rating": "Good"},
          {"step": 3, "type": "terminal", "command": "pytest", "rating": "Good"}
        ],
        "outcome": "Fully Complete",
        "children": []
      },
      {
        "branch_id": "branch_2",
        "branch_point": 1,
        "instruction": "Read the test file first to understand expected behavior",
        "steps": [
          {"step": 2, "type": "file_read", "file": "tests/test_parser.py", "rating": "Good"},
          {"step": 3, "type": "edit", "file": "src/parser.py", "rating": "Good"},
          {"step": 4, "type": "terminal", "command": "pytest", "rating": "Good"}
        ],
        "outcome": "Fully Complete",
        "children": []
      }
    ]
  },
  "branch_preference": "Branch B",
  "observation_notes": "Both branches solved the problem, but branch B produced cleaner code by reading the tests first."
}

صدّر المسارات المتفرعة لتعلّم التفضيل:

bash
# Export as DPO preference pairs from branch comparisons
python -m potato.export \
  -i output/ \
  -f branching_dpo \
  -o results/branch_preferences.jsonl
 
# Export full trajectory trees
python -m potato.export \
  -i output/ \
  -f trajectory_tree \
  -o results/trajectory_trees.jsonl

الأمان

يعمل الوكيل المباشر داخل مجلد المشروع المحدد في بيانات المهمة، وله صلاحية قراءة الملفات وكتابتها وتنفيذها ضمن ذلك المجلد. خذ في الحسبان الممارسات الأمنية التالية:

  • العزل في بيئة معزولة: للكود غير الموثوق أو نماذج الوكلاء غير الموثوقة، شغّل Potato داخل حاوية Docker أو آلة افتراضية. فالوكيل قادر على تنفيذ أوامر shell عشوائية، والعزل مهم هنا.
  • وضع القراءة فقط: عطّل أداتَي bash وwrite_file إذا كنت تريد من الوكيل تحليل الكود فقط من دون تعديله.
  • قيود الشبكة: استخدم الراية --network none في Docker لمنع الوكيل من إجراء طلبات شبكية.
  • حدود الموارد: اضبط max_steps وstep_timeout_seconds لمنع الوكلاء من الانفلات.
yaml
# Restricted tool set for analysis-only tasks
live_agent:
  tools:
    - read_file
    - glob
    - grep
  # No edit_file, write_file, or bash

معالجة المشكلات

Ollama لا يعمل

text
Error: Connection refused at http://localhost:11434

شغّل خادم Ollama:

bash
ollama serve

وتحقق من أنه يعمل:

bash
ollama list

مفتاح API مفقود

text
Error: ANTHROPIC_API_KEY environment variable not set

اضبط متغير البيئة:

bash
export ANTHROPIC_API_KEY="sk-ant-..."

أو أضفه إلى ملف .env في مشروعك. يحمّل Potato ملفات .env تلقائياً.

Git غير مهيّأ

text
Error: Project directory is not a git repository

يتطلب نظام نقاط التحقق git. هيّئ مستودعاً في مجلد المشروع:

bash
cd /path/to/project
git init
git add -A
git commit -m "Initial commit"

الوكيل عالق في حلقة

إذا كرر الوكيل الإجراء نفسه مرات عدة، فقد يكون عالقاً. يكتشف Potato الحلقات حين يتكرر استدعاء الأداة نفسه بالمعاملات نفسها 3 مرات، فيوقف الوكيل مؤقتاً تلقائياً. ويمكنك تعديل هذه العتبة:

yaml
live_agent:
  loop_detection:
    enabled: true
    threshold: 3                     # pause after N identical consecutive steps
    action: pause                    # "pause" or "terminate"

تنظيف فروع الجلسات

تتراكم فروع الجلسات مع الوقت. نظّفها دورياً:

bash
# Remove branches older than 7 days
python -m potato.cleanup_sessions --older-than 7d
 
# Remove all session branches
python -m potato.cleanup_sessions --all
 
# Dry run (show what would be deleted)
python -m potato.cleanup_sessions --older-than 7d --dry-run

انظر أيضاً

للاطلاع على تفاصيل التنفيذ، راجع الوثائق المصدرية.