Spaces:
Sleeping
Sleeping
Deploy code agent from GitHub Actions
Browse files
app.py
CHANGED
|
@@ -20,6 +20,8 @@ class CodeRepairRequest(BaseModel):
|
|
| 20 |
error_log: str | None = None
|
| 21 |
root_cause: str | None = None
|
| 22 |
repair_summary: str | None = None
|
|
|
|
|
|
|
| 23 |
|
| 24 |
|
| 25 |
@app.get("/")
|
|
@@ -47,12 +49,77 @@ def load_model():
|
|
| 47 |
return tokenizer, model
|
| 48 |
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
def build_prompt(request: CodeRepairRequest):
|
| 51 |
code = request.code_snippet or "No code snippet provided."
|
| 52 |
error = request.error_log or "No error log provided."
|
| 53 |
root_cause = request.root_cause or "No root cause provided."
|
| 54 |
repair_summary = request.repair_summary or "No repair summary provided."
|
| 55 |
file_path = request.file_path or "Unknown file"
|
|
|
|
|
|
|
| 56 |
|
| 57 |
return f"""
|
| 58 |
Analyze the following code repair context and provide safe code-level guidance.
|
|
@@ -63,6 +130,12 @@ Project type:
|
|
| 63 |
File path:
|
| 64 |
{file_path}
|
| 65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
Root cause:
|
| 67 |
{root_cause}
|
| 68 |
|
|
@@ -81,6 +154,7 @@ Return only these sections:
|
|
| 81 |
3. Suggested code change
|
| 82 |
4. Verification step
|
| 83 |
|
|
|
|
| 84 |
Do not include system/user/assistant labels.
|
| 85 |
Do not repeat the prompt.
|
| 86 |
Do not invent files that are not shown.
|
|
@@ -132,6 +206,11 @@ def call_llm(prompt: str):
|
|
| 132 |
|
| 133 |
|
| 134 |
def fallback_code_guidance(request: CodeRepairRequest):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
if request.error_log:
|
| 136 |
summary = (
|
| 137 |
"A code-level issue may exist based on the provided error log. "
|
|
@@ -163,7 +242,9 @@ def remove_prompt_leak(text: str):
|
|
| 163 |
r"assistant\s*1\.",
|
| 164 |
r"assistant\s*Problem",
|
| 165 |
r"###\s*1\.\s*Problem",
|
| 166 |
-
r"1\.\s*Problem"
|
|
|
|
|
|
|
| 167 |
]
|
| 168 |
|
| 169 |
for pattern in marker_patterns:
|
|
@@ -178,7 +259,8 @@ def remove_prompt_leak(text: str):
|
|
| 178 |
bad_prefixes = [
|
| 179 |
"system You are",
|
| 180 |
"user You are",
|
| 181 |
-
"Analyze the following code repair context"
|
|
|
|
| 182 |
]
|
| 183 |
|
| 184 |
for prefix in bad_prefixes:
|
|
@@ -203,7 +285,17 @@ def clean_output(text: str):
|
|
| 203 |
if len(cleaned) < 30:
|
| 204 |
return None
|
| 205 |
|
| 206 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
return None
|
| 208 |
|
| 209 |
return cleaned
|
|
@@ -211,6 +303,11 @@ def clean_output(text: str):
|
|
| 211 |
|
| 212 |
@app.post("/suggest-code-fix")
|
| 213 |
def suggest_code_fix(request: CodeRepairRequest):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 214 |
fallback_result = fallback_code_guidance(request)
|
| 215 |
|
| 216 |
try:
|
|
@@ -233,5 +330,4 @@ def suggest_code_fix(request: CodeRepairRequest):
|
|
| 233 |
|
| 234 |
except Exception as error:
|
| 235 |
fallback_result["llm_error"] = repr(error)
|
| 236 |
-
return fallback_result
|
| 237 |
-
|
|
|
|
| 20 |
error_log: str | None = None
|
| 21 |
root_cause: str | None = None
|
| 22 |
repair_summary: str | None = None
|
| 23 |
+
failure_type: str | None = None
|
| 24 |
+
help_message: str | None = None
|
| 25 |
|
| 26 |
|
| 27 |
@app.get("/")
|
|
|
|
| 49 |
return tokenizer, model
|
| 50 |
|
| 51 |
|
| 52 |
+
def get_environment_guidance(request: CodeRepairRequest):
|
| 53 |
+
if request.failure_type == "MAVEN_NOT_AVAILABLE":
|
| 54 |
+
summary = (
|
| 55 |
+
"Problem: Maven is not installed or not available in PATH.\n\n"
|
| 56 |
+
"Safe fix approach: This is an environment setup issue, not an application source code issue. "
|
| 57 |
+
"Do not modify Java source files for this failure.\n\n"
|
| 58 |
+
"Suggested code change: No application code change is required. Install Apache Maven and add the Maven bin directory "
|
| 59 |
+
"to PATH, or add Maven Wrapper files (mvnw, mvnw.cmd, .mvn/wrapper) to the project.\n\n"
|
| 60 |
+
"Verification step: Run `mvn -v` or `mvnw.cmd test` after fixing the environment, then rerun Stitch QA."
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
return {
|
| 64 |
+
"agent": "code-agent",
|
| 65 |
+
"mode": "rule-based",
|
| 66 |
+
"summary": summary,
|
| 67 |
+
"risk_level": "LOW",
|
| 68 |
+
"auto_apply": False,
|
| 69 |
+
"suggested_patch": None,
|
| 70 |
+
"verification": "Fix the Maven environment first, then rerun Stitch QA verification."
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
if request.failure_type == "MAVEN_WRAPPER_NOT_AVAILABLE":
|
| 74 |
+
summary = (
|
| 75 |
+
"Problem: Maven Wrapper command is missing or cannot be executed.\n\n"
|
| 76 |
+
"Safe fix approach: This is a project execution setup issue, not a confirmed Java source code issue.\n\n"
|
| 77 |
+
"Suggested code change: No application source code change is required. Check whether mvnw.cmd exists in the project root, "
|
| 78 |
+
"or add Maven Wrapper files to the project.\n\n"
|
| 79 |
+
"Verification step: Run `mvnw.cmd test` from the project root after adding or fixing the wrapper."
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
return {
|
| 83 |
+
"agent": "code-agent",
|
| 84 |
+
"mode": "rule-based",
|
| 85 |
+
"summary": summary,
|
| 86 |
+
"risk_level": "LOW",
|
| 87 |
+
"auto_apply": False,
|
| 88 |
+
"suggested_patch": None,
|
| 89 |
+
"verification": "Fix Maven Wrapper availability first, then rerun Stitch QA verification."
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
if request.failure_type == "COMMAND_TIMEOUT":
|
| 93 |
+
summary = (
|
| 94 |
+
"Problem: The build or test command timed out.\n\n"
|
| 95 |
+
"Safe fix approach: Treat this as an execution/runtime environment issue first. "
|
| 96 |
+
"Do not modify source code until the command behavior is verified manually.\n\n"
|
| 97 |
+
"Suggested code change: No direct code change is recommended from this timeout alone. "
|
| 98 |
+
"Check whether dependency downloads, tests, or build steps are hanging.\n\n"
|
| 99 |
+
"Verification step: Rerun the Maven command manually with a longer timeout and inspect where it stalls."
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
return {
|
| 103 |
+
"agent": "code-agent",
|
| 104 |
+
"mode": "rule-based",
|
| 105 |
+
"summary": summary,
|
| 106 |
+
"risk_level": "MEDIUM",
|
| 107 |
+
"auto_apply": False,
|
| 108 |
+
"suggested_patch": None,
|
| 109 |
+
"verification": "Investigate command timeout first, then rerun Stitch QA."
|
| 110 |
+
}
|
| 111 |
+
|
| 112 |
+
return None
|
| 113 |
+
|
| 114 |
+
|
| 115 |
def build_prompt(request: CodeRepairRequest):
|
| 116 |
code = request.code_snippet or "No code snippet provided."
|
| 117 |
error = request.error_log or "No error log provided."
|
| 118 |
root_cause = request.root_cause or "No root cause provided."
|
| 119 |
repair_summary = request.repair_summary or "No repair summary provided."
|
| 120 |
file_path = request.file_path or "Unknown file"
|
| 121 |
+
failure_type = request.failure_type or "None"
|
| 122 |
+
help_message = request.help_message or "None"
|
| 123 |
|
| 124 |
return f"""
|
| 125 |
Analyze the following code repair context and provide safe code-level guidance.
|
|
|
|
| 130 |
File path:
|
| 131 |
{file_path}
|
| 132 |
|
| 133 |
+
Failure type:
|
| 134 |
+
{failure_type}
|
| 135 |
+
|
| 136 |
+
Help message:
|
| 137 |
+
{help_message}
|
| 138 |
+
|
| 139 |
Root cause:
|
| 140 |
{root_cause}
|
| 141 |
|
|
|
|
| 154 |
3. Suggested code change
|
| 155 |
4. Verification step
|
| 156 |
|
| 157 |
+
If the failure is Maven not available or Maven Wrapper missing, clearly say no application source code change is required.
|
| 158 |
Do not include system/user/assistant labels.
|
| 159 |
Do not repeat the prompt.
|
| 160 |
Do not invent files that are not shown.
|
|
|
|
| 206 |
|
| 207 |
|
| 208 |
def fallback_code_guidance(request: CodeRepairRequest):
|
| 209 |
+
environment_guidance = get_environment_guidance(request)
|
| 210 |
+
|
| 211 |
+
if environment_guidance:
|
| 212 |
+
return environment_guidance
|
| 213 |
+
|
| 214 |
if request.error_log:
|
| 215 |
summary = (
|
| 216 |
"A code-level issue may exist based on the provided error log. "
|
|
|
|
| 242 |
r"assistant\s*1\.",
|
| 243 |
r"assistant\s*Problem",
|
| 244 |
r"###\s*1\.\s*Problem",
|
| 245 |
+
r"1\.\s*Problem",
|
| 246 |
+
r"\*\*Problem:\*\*",
|
| 247 |
+
r"Problem:"
|
| 248 |
]
|
| 249 |
|
| 250 |
for pattern in marker_patterns:
|
|
|
|
| 259 |
bad_prefixes = [
|
| 260 |
"system You are",
|
| 261 |
"user You are",
|
| 262 |
+
"Analyze the following code repair context",
|
| 263 |
+
"Return only these sections"
|
| 264 |
]
|
| 265 |
|
| 266 |
for prefix in bad_prefixes:
|
|
|
|
| 285 |
if len(cleaned) < 30:
|
| 286 |
return None
|
| 287 |
|
| 288 |
+
bad_patterns = [
|
| 289 |
+
"system You are",
|
| 290 |
+
"user You are",
|
| 291 |
+
"Do not include system/user/assistant labels",
|
| 292 |
+
"Do not repeat the prompt",
|
| 293 |
+
"Do not invent files that are not shown",
|
| 294 |
+
"Do not apply changes automatically",
|
| 295 |
+
"Keep the answer concise"
|
| 296 |
+
]
|
| 297 |
+
|
| 298 |
+
if any(pattern.lower() in cleaned.lower() for pattern in bad_patterns):
|
| 299 |
return None
|
| 300 |
|
| 301 |
return cleaned
|
|
|
|
| 303 |
|
| 304 |
@app.post("/suggest-code-fix")
|
| 305 |
def suggest_code_fix(request: CodeRepairRequest):
|
| 306 |
+
environment_guidance = get_environment_guidance(request)
|
| 307 |
+
|
| 308 |
+
if environment_guidance:
|
| 309 |
+
return environment_guidance
|
| 310 |
+
|
| 311 |
fallback_result = fallback_code_guidance(request)
|
| 312 |
|
| 313 |
try:
|
|
|
|
| 330 |
|
| 331 |
except Exception as error:
|
| 332 |
fallback_result["llm_error"] = repr(error)
|
| 333 |
+
return fallback_result
|
|
|