# Full reference trajectory config - BookWriter-MCP project # # Version: ideal trajectory derived from the code design (applies to all models) # Principle: include only steps explicitly required by the design, not based on test statistics # # Code design analysis: # 1. Three main stages executed in order (orchestrator.py): # - OutlineGenerator: generate book outline (2 agents: Researcher + Outliner) # - WriteBookChapterCrew: write multiple chapters concurrently (2 agents per chapter: Researcher + Writer) # - ReviewBookCrew: review the full book (1 agent: Editor) # # 2. Tool usage analysis: # - OutlineGenerator: # - Researcher: bocha_websearch_tool, extract_keywords # - Outliner: no tools (reasoning only) # - WriteBookChapterCrew (per chapter): # - Researcher: bocha_websearch_tool, extract_keywords # - Writer: count_words, analyze_chapter_quality, validate_markdown_structure # - ReviewBookCrew: # - Editor: count_book_words, analyze_book_quality, validate_book_markdown, extract_book_keywords # # 3. Execution flow: # - orchestrator creates top-level SPAN: book_writing_orchestrator # - orchestrator creates crew_execution SPAN with retry logic # - three child SPANs: generate_outline, write_chapters, review_book # - each Crew contains one Chain and multiple Agents # - chapter writing is executed in concurrent batches (batch_size=4) # # 4. SPAN hierarchy: # - book_writing_orchestrator (top-level, orchestrator.py line 574) # └─ crew_execution (contains retry logic, orchestrator.py line 597) # ├─ generate_outline (Crew 1, orchestrator.py line 138) # ├─ write_chapters (Crew 2 concurrent batches, orchestrator.py line 268) # └─ review_book (Crew 3, orchestrator.py line 342) # Project name project_name: "BookWriter-MCP" # Trajectory extraction types extract_types: - "SPAN" # Multi-level SPAN hierarchy - "Chain" # kickoff chain for each Crew - "Agent" # agents within each Crew - "LLM" # LLM calls - "Tool" # MCP tool calls # Repeatable pattern config (for handling varying chapter counts) # Format: {"start": start step index (0-based), "end": end step index, "min": min repeats, "max": max repeats} # Reference trajectory indices: 0-11 Outline, 12 write_chapters SPAN, 13-27 single-chapter pattern (full), 28+ Review repeatable_patterns: - start: 13 # Chain: Crew***.kickoff (step 1 of the single-chapter pattern) end: 27 # LLM: * (last step of the single-chapter pattern, after validate_markdown_structure) min: 3 # at least 3 chapters (business requirement) max: 5 # at most 5 chapters name: "chapter_writing_pattern" # pattern name (for debugging) # ============================================================================ # Reference trajectory (ground truth) - derived from the code design # ============================================================================ # # Diagram (ideal execution path; the single-chapter pattern repeats 3-5 times): # # [SPAN] book_writing_orchestrator # └─ [SPAN] crew_execution # ├─ [SPAN] generate_outline # │ └─ [Chain] Crew***.kickoff # │ ├─ [Agent] Expert Research Agent for Book Outline Planning # │ │ ├─ [LLM] * # │ │ ├─ [Tool] bocha_websearch_tool # │ │ ├─ [LLM] * # │ │ ├─ [Tool] extract_keywords # │ │ └─ [LLM] * # │ └─ [Agent] Strategic Book Structure Architect # │ └─ [LLM] * # ├─ [SPAN] write_chapters # │ └─ [Repeat the following pattern per chapter, 3-5 times] # │ └─ [Chain] Crew***.kickoff # │ ├─ [Agent] Specialized Chapter Research Agent # │ │ ├─ [LLM] * # │ │ ├─ [Tool] bocha_websearch_tool # │ │ ├─ [LLM] * # │ │ ├─ [Tool] extract_keywords # │ │ └─ [LLM] * # │ └─ [Agent] Professional Chapter Content Writer # │ ├─ [LLM] * # │ ├─ [Tool] count_words # │ ├─ [LLM] * # │ ├─ [Tool] analyze_chapter_quality # │ ├─ [LLM] * # │ ├─ [Tool] validate_markdown_structure # │ └─ [LLM] * # └─ [SPAN] review_book # └─ [Chain] Crew***.kickoff # └─ [Agent] Senior Book Editor and Quality Assurance Specialist # ├─ [LLM] * # ├─ [Tool] count_book_words # ├─ [LLM] * # ├─ [Tool] analyze_book_quality # ├─ [LLM] * # ├─ [Tool] validate_book_markdown # ├─ [LLM] * # ├─ [Tool] extract_book_keywords # └─ [LLM] * # # Design basis: # - orchestrator.py: runs 3 main stages sequentially # - OutlineGenerator has 2 agents (Researcher + Outliner) # - WriteBookChapterCrew has 2 agents per chapter (Researcher + Writer) # - ReviewBookCrew has 1 agent (Editor) # - all agent roles are defined in each crew's config/agents.yaml # - tools are obtained from the MCP server via MCPServerAdapter # - chapter count is produced from the outline, usually 3-5 chapters # # Ideal trajectory notes: # - Top-level SPAN: book_writing_orchestrator # - Second-level SPAN: crew_execution (contains retry logic) # - 3 child SPANs run in order: generate_outline → write_chapters → review_book # - Outline stage: Researcher uses 2 tools; Outliner uses reasoning only # - Chapter stage: per chapter, Researcher uses 2 tools; Writer uses 3 tools # - Review stage: Editor uses 4 tools for full-book quality checks # - This trajectory represents an ideal path with no retries and no redundant steps (4 chapters as a baseline) # # Notes: # - "LLM: *" means any LLM model (wildcard) # - agent names must match the role field in agents.yaml exactly (full name) # - tool names must match the MCP server tool names exactly # - actual chapter count may be 3-5, which affects exact_match but not in-order match # - tool calls may be skipped or repeated depending on agent decisions # ============================================================================ reference_trajectory: # ===== Top-level SPAN ===== - "SPAN: book_writing_orchestrator" # ===== Second-level SPAN (contains retry logic) ===== - "SPAN: crew_execution" # ===== Stage 1: Generate Outline ===== - "SPAN: generate_outline" - "Chain: Crew***.kickoff" # Agent 1: Researcher - "Agent: Expert Research Agent for Book Outline Planning" - "LLM: *" # decide research strategy - "Tool: bocha_websearch_tool" # web search topics - "LLM: *" # process search results - "Tool: extract_keywords" # extract keywords - "LLM: *" # summarize research findings # Agent 2: Outliner - "Agent: Strategic Book Structure Architect" - "LLM: *" # generate the book outline # ===== Stage 2: Write Chapters (at least 1; typically 3-5 chapters) ===== - "SPAN: write_chapters" # Standard per-chapter pattern (appears at least once; repeats 3-5 times) - "Chain: Crew***.kickoff" - "Agent: Specialized Chapter Research Agent" - "LLM: *" - "Tool: bocha_websearch_tool" - "LLM: *" - "Tool: extract_keywords" - "LLM: *" - "Agent: Professional Chapter Content Writer" - "LLM: *" - "Tool: count_words" - "LLM: *" - "Tool: analyze_chapter_quality" - "LLM: *" - "Tool: validate_markdown_structure" - "LLM: *" # ===== Stage 3: Review Book ===== - "SPAN: review_book" - "Chain: Crew***.kickoff" - "Agent: Senior Book Editor and Quality Assurance Specialist" - "LLM: *" # start review - "Tool: count_book_words" # count words - "LLM: *" # analyze word count - "Tool: analyze_book_quality" # quality analysis - "LLM: *" # assess quality - "Tool: validate_book_markdown" # format validation - "LLM: *" # check formatting - "Tool: extract_book_keywords" # extract keywords - "LLM: *" # generate review report # Target tool list (for the single-tool use metric) # List all tools that are expected to be used target_tools: # Outline generation stage - "Tool: bocha_websearch_tool" - "Tool: extract_keywords" # Chapter writing stage (per chapter) - "Tool: count_words" - "Tool: analyze_chapter_quality" - "Tool: validate_markdown_structure" # Book review stage - "Tool: count_book_words" - "Tool: analyze_book_quality" - "Tool: validate_book_markdown" - "Tool: extract_book_keywords" # ============================================================================ # Dynamic tool permutation configuration # ============================================================================ # Define permutable tool groups for some agents, to generate reference trajectories with different tool orders. # During evaluation, all permutations are tried and the best-matching reference is chosen. # # Design basis: # - Chapter Writer's 3 validation tools (count_words, analyze_chapter_quality, validate_markdown_structure) # depend only on the chapter draft and are independent of each other, so they can be executed in any order. # - Book Editor's 4 analysis tools (count_book_words, analyze_book_quality, # validate_book_markdown, extract_book_keywords) depend only on book files and are fully independent, # so they can be executed in any order. # # Number of permutations: # - Chapter Writer: 3! = 6 permutations # - Book Editor: 4! = 24 permutations # - Total: 6 permutations per chapter, and 24 permutations for the Book Editor # # Evaluation strategy: # - For each sample, generate all possible permuted reference trajectories # - Compute a matching score for each permutation (exact_match * 3 + in_order_match * 2 + any_order_match * 1) # - Choose the highest-scoring permutation as the reference trajectory for that sample # - This fairly evaluates models that may call tools in different orders permutable_tool_groups: # Chapter Writer validation tools can be in any order # These 3 tools are used within "Agent: Professional Chapter Content Writer" chapter_writer_validation_tools: - "Tool: count_words" - "Tool: analyze_chapter_quality" - "Tool: validate_markdown_structure" # Book Editor analysis tools can be in any order # These 4 tools are used within "Agent: Senior Book Editor and Quality Assurance Specialist" book_editor_analysis_tools: - "Tool: count_book_words" - "Tool: analyze_book_quality" - "Tool: validate_book_markdown" - "Tool: extract_book_keywords" # Models to evaluate models: - "GPT-5" - "GPT-4o-mini" - "DeepSeek-V3-1" - "DeepSeek-R1" - "Gemini-2.5-flash" - "Gemini-2.5-flash-nothinking" - "Qwen3-235b" # ============================================================================ # Usage # ============================================================================ # # 1. Trajectory hierarchy: # - Level 1: SPAN (book_writing_orchestrator) # - Level 2: SPAN (crew_execution) # - Level 3: SPAN (generate_outline, write_chapters, review_book) # - Level 4: Chain (Crew***.kickoff, wildcard for UUID) # - Level 5: AGENT (exact match of agent role) # - Level 6: LLM (wildcard match for any model) # - Level 6: Tool (exact match of tool name) # # 2. Wildcard handling: # - Crew_.kickoff -> Crew***.kickoff (handled automatically by evaluate_trajectory.py) # - LLM: * -> matches any model name (e.g., gpt-5-chat-latest, deepseek-reasoner) # - Agent names must match exactly (including any prefixes/suffixes) # # 3. Exact matching requirements: # - SPAN names: "book_writing_orchestrator", "crew_execution", "generate_outline", # "write_chapters", "review_book" # - Chain name: "Crew***.kickoff" (wildcard match) # - Agent names (case-sensitive): "Agent: Expert Research Agent for Book Outline Planning", # "Agent: Strategic Book Structure Architect", "Agent: Specialized Chapter Research Agent", # "Agent: Professional Chapter Content Writer", # "Agent: Senior Book Editor and Quality Assurance Specialist" # (must match the role field in agents.yaml exactly) # - LLM name: "LLM: *" (wildcard match) # - Tool names: must match MCP tool names exactly (bocha_websearch_tool, extract_keywords, # count_words, analyze_chapter_quality, validate_markdown_structure, # count_book_words, analyze_book_quality, validate_book_markdown, extract_book_keywords) # # 4. Metric definitions: # - Exact Match: requires identical trajectories (including number of LLM calls; the per-chapter pattern is repeatable) # - In-order Match: allows extra calls, but required steps must appear in order # - Any-order Match: requires all required steps regardless of order # - Precision: fraction of correct steps among predicted steps # - Recall: fraction of reference steps covered by predicted steps # - Single-tool Use: checks usage of all 9 required tools # - unique_path_ratio: path diversity (unique full trajectories / number of samples) # - path_entropy: path entropy (Shannon entropy over trajectory frequencies, normalized to 0-1) # - Note: chapter count (3-5) does not affect any metric because the per-chapter pattern is repeatable # # 5. Run command: # cd /Users/wzr/TOSEM-2025/RESULTS/RQ-Failure_Breakdown/BookWriter-MCP # python3 evaluate_trajectory.py --config reference_trajectory.yaml # # 6. Design notes: # - This reference represents an ideal execution path (with a per-chapter pattern, matching 3-5 chapters) # - The 3 main stages run in order: Outline → Chapters → Review # - Stage 1 (Outline): Researcher uses 2 tools + Outliner uses reasoning only = 4 LLM calls + 2 tool calls # - Stage 2 (Chapters): per chapter, Researcher uses 2 tools + Writer uses 3 tools = 8 LLM calls + 5 tool calls per chapter # - Stage 3 (Review): Editor uses 4 tools = 5 LLM calls + 4 tool calls # - Per-chapter pattern: 1 Chain + 2 Agents + 8 LLM calls + 5 tool calls (repeated within write_chapters) # - In-order Match and Recall are often more appropriate for evaluating real performance # - Note: the reference defines a single-chapter pattern that repeats 3-5 times to avoid fixed-chapter-count mismatch # # 7. Differences vs. actual trajectories: # - The reference defines a per-chapter pattern; actual runs repeat it 3-5 times (depending on outline-generated chapters) # - Actual trajectories may include more LLM calls (thinking/planning/executing/summarizing; especially for reasoning models) # - Some models may call tools multiple times (retries or extra checks) # - Writer may repeatedly call count_words or analyze_chapter_quality for iterative improvement # - Some models (e.g., Gemini) may skip certain tool calls and still produce results # - These differences do not necessarily indicate errors; they reflect different execution strategies # - The repeatable per-chapter pattern ensures in_order_match works regardless of the number of chapters # # 8. MCP variant characteristics: # - All tools are provided by an MCP server over SSE # - Tools are retrieved via MCPServerAdapter # - Includes a multi-level SPAN hierarchy (orchestrator + crew_execution + 3 stage SPANs) # - The 3 stages collaborate to complete the book-writing task # - Each stage has clear responsibilities and expected tool usage # - OutlineGenerator: 2 agents to research topics and produce an outline # - WriteBookChapterCrew: 2 agents to write chapters in concurrent batches # - ReviewBookCrew: 1 agent using 4 tools to review full-book quality # - Tool categories: search (bocha_websearch_tool), analysis (extract_keywords), # quality checks (count_words, analyze_*_quality), format validation (validate_*) # # 9. Dynamic tool permutation notes: # - Background: based on tool dependency analysis (see tool_sequence_analysis.md) # * Chapter Writer's 3 validation tools are independent and can run in any order # * Book Editor's 4 analysis tools are fully independent and can run in any order # - Implementation: # * permutable_tool_groups defines permutable tool groups # * Chapter Writer: 3! = 6 permutations # * Book Editor: 4! = 24 permutations # * Total per sample: 6 × 24 = 144 reference trajectory permutations # - Selection strategy: # * the evaluation script generates all permutations per sample # * computes a matching score for each permutation (exact*3 + in_order*2 + any_order*1) # * selects the highest-scoring permutation as that sample's reference # - Benefits: # * fairly evaluates models that use different tool orders # * improves the accuracy of exact_match and in_order_match # * reflects realistic flexibility of tool ordering (no forced dependencies) # - Performance: # * permutation generation and scoring are done in-memory and are efficient # * evaluation time per sample increases by ~2-3x (acceptable)