Charles Grandjean commited on
Commit
62963c8
·
1 Parent(s): 56aa261

retry after a fail

Browse files
Files changed (2) hide show
  1. prompts/doc_editor.py +29 -9
  2. subagents/doc_editor.py +14 -12
prompts/doc_editor.py CHANGED
@@ -11,15 +11,17 @@ DOC_EDITOR_SYSTEM_PROMPT = """You are a Document Editing Agent specialized in mo
11
 
12
  2. **NEVER OUTPUT THE FULL DOCUMENT**: Never output the complete document text. The system always has the current document state.
13
 
14
- 3. **EXACT MATCHING**: When using replace/add/delete, you must copy the EXACT HTML block (including all whitespace, tags, attributes, and attributes values). The search/anchor must match exactly.
15
 
16
- 4. **ALWAYS INCLUDE expected_matches**: Always specify the expected_matches parameter (usually 1) to prevent unintended multiple replacements.
17
 
18
- 5. **SMALL, PRECISE EDITS**: Make small, targeted edits. Don't try to replace large blocks - select the smallest unique fragment that identifies what you want to change.
19
 
20
- 6. **VALIDATE YOUR CHOICES**: If a tool returns an error (mismatch or invalid HTML), analyze the error and try again with a more precise search/anchor.
21
 
22
- 7. **CALL attempt_completion WHEN DONE**: Once all modifications are successfully applied, call attempt_completion with a summary message.
 
 
23
 
24
  ## AVAILABLE TOOLS
25
 
@@ -106,11 +108,29 @@ Example:
106
  }
107
  ```
108
 
109
- ## ERROR HANDLING
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
110
 
111
- If you receive an error:
112
- - "Match count mismatch": Your search/anchor didn't match exactly. Check whitespace, tags, attributes, and attribute values. Try a more specific or different fragment.
113
- - "Post-edit validation failed": The replacement broke the HTML structure. Ensure your insert/replace is valid HTML with proper tags.
114
 
115
  ## WORKFLOW
116
 
 
11
 
12
  2. **NEVER OUTPUT THE FULL DOCUMENT**: Never output the complete document text. The system always has the current document state.
13
 
14
+ 3. **DOCUMENT SUMMARIES ARE FOR CONTEXT ONLY**: Document summaries are provided for informational purposes only. Do NOT use them unless the user instruction explicitly references them. Focus on editing the current document based on the instruction. Only incorporate content from summaries if the user specifically asks you to use them.
15
 
16
+ 4. **EXACT MATCHING**: When using replace/add/delete, you must copy the EXACT HTML block (including all whitespace, tags, attributes, and attributes values). The search/anchor must match exactly.
17
 
18
+ 5. **ALWAYS INCLUDE expected_matches**: Always specify the expected_matches parameter (usually 1) to prevent unintended multiple replacements.
19
 
20
+ 6. **SMALL, PRECISE EDITS**: Make small, targeted edits. Don't try to replace large blocks - select the smallest unique fragment that identifies what you want to change.
21
 
22
+ 7. **VALIDATE YOUR CHOICES**: If a tool returns an error (mismatch or invalid HTML), analyze the error and try again with a more precise search/anchor.
23
+
24
+ 8. **CALL attempt_completion WHEN DONE**: Once all modifications are successfully applied, call attempt_completion with a summary message.
25
 
26
  ## AVAILABLE TOOLS
27
 
 
108
  }
109
  ```
110
 
111
+ ## ERROR HANDLING AND RETRY MECHANISM
112
+
113
+ **Important:** When a tool execution fails, you will automatically receive another chance to try. The system will show you the error message and you should analyze it carefully.
114
+
115
+ Common errors and how to fix them:
116
+ - "Match count mismatch": Your search/anchor didn't match exactly in the document.
117
+ - Check whitespace, tags, attributes, and attribute values
118
+ - Try a more specific or different fragment
119
+ - Use the current document content provided in the conversation to find the exact text
120
+
121
+ - "Post-edit validation failed": The replacement broke the HTML structure.
122
+ - Ensure your insert/replace is valid HTML with proper opening and closing tags
123
+ - Check that you're not creating malformed HTML (e.g., unclosed tags)
124
+ - Verify the structure is valid
125
+
126
+ **Workflow on errors:**
127
+ 1. Read the error message carefully
128
+ 2. Look at the current document state in the conversation history
129
+ 3. Understand what went wrong
130
+ 4. Try again with a corrected approach (different search pattern, different anchor, fixed HTML, etc.)
131
+ 5. Continue retrying until you succeed or reach max_iterations
132
 
133
+ You get multiple chances (up to max_iterations) to complete the task, so don't worry about failures - learn from them and try again!
 
 
134
 
135
  ## WORKFLOW
136
 
subagents/doc_editor.py CHANGED
@@ -86,8 +86,9 @@ class DocumentEditorAgent:
86
  Returns:
87
  "continue" if more iterations needed, "end" if complete or max iterations reached
88
  """
89
- # Check if attempt_completion was called
90
  intermediate_steps = state.get("intermediate_steps", [])
 
 
91
  if intermediate_steps:
92
  for msg in reversed(intermediate_steps):
93
  if isinstance(msg, ToolMessage):
@@ -95,6 +96,17 @@ class DocumentEditorAgent:
95
  if msg.name == "attempt_completion":
96
  logger.info("✅ attempt_completion called - ending workflow")
97
  return "end"
 
 
 
 
 
 
 
 
 
 
 
98
 
99
  # Check max iterations
100
  iteration_count = state.get("iteration_count", 0)
@@ -326,17 +338,7 @@ class DocumentEditorAgent:
326
  logger.info("=" * 80)
327
  logger.info("📄 FINAL DOCUMENT CONTENT")
328
  logger.info("=" * 80)
329
- if final_doc_size <= 1000:
330
- # Log full document if small enough
331
- logger.info(final_state["doc_text"])
332
- else:
333
- # Log first 500 and last 500 characters for readability
334
- doc_content = final_state["doc_text"]
335
- logger.info(f"[BEGINNING - first 500 chars]")
336
- logger.info(doc_content[:500])
337
- logger.info(f"...")
338
- logger.info(f"[ENDING - last 500 chars]")
339
- logger.info(doc_content[-500:])
340
  logger.info("=" * 80)
341
 
342
  if not success:
 
86
  Returns:
87
  "continue" if more iterations needed, "end" if complete or max iterations reached
88
  """
 
89
  intermediate_steps = state.get("intermediate_steps", [])
90
+
91
+ # Check if attempt_completion was called successfully
92
  if intermediate_steps:
93
  for msg in reversed(intermediate_steps):
94
  if isinstance(msg, ToolMessage):
 
96
  if msg.name == "attempt_completion":
97
  logger.info("✅ attempt_completion called - ending workflow")
98
  return "end"
99
+
100
+ # Check if the last tool execution failed
101
+ # If so, we should continue to let the agent retry
102
+ if msg.name in ["replace_html", "add_html", "delete_html"]:
103
+ content = msg.content
104
+ # Parse the tool result to check if it was successful
105
+ if isinstance(content, str) and '"ok": false' in content.lower():
106
+ logger.info("⚠️ Last tool execution failed - allowing agent to retry")
107
+ # Continue to give the agent a chance to correct itself
108
+ # Don't check max_iterations yet - we want retries
109
+ return "continue"
110
 
111
  # Check max iterations
112
  iteration_count = state.get("iteration_count", 0)
 
338
  logger.info("=" * 80)
339
  logger.info("📄 FINAL DOCUMENT CONTENT")
340
  logger.info("=" * 80)
341
+ logger.info(final_state["doc_text"])
 
 
 
 
 
 
 
 
 
 
342
  logger.info("=" * 80)
343
 
344
  if not success: