File size: 16,985 Bytes
8c10cf2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# 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_<UUID>.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)