jens-l commited on
Commit
8f63e88
·
1 Parent(s): 4725f58

REFACTOR: Simplify and correct test_app_py tool logic

Browse files
Files changed (1) hide show
  1. kiss_agent.py +22 -24
kiss_agent.py CHANGED
@@ -336,47 +336,45 @@ def file_viewer(filename: str) -> str:
336
  @tool
337
  def test_app_py() -> str:
338
  """
339
- Test the app.py file by running it as a subprocess and checking its output.
340
- This tool will automatically find a free port to run the app on.
 
341
  """
 
342
  try:
343
  app_path = Path("sandbox") / "app.py"
344
  if not app_path.exists():
345
  return "Error: app.py not found in sandbox directory."
346
 
347
- # Find a free port to avoid conflicts
348
- free_port = _find_free_port()
349
- if free_port is None:
350
- return "Error: Could not find any free ports to run the test."
351
-
352
- print(f"Found free port {free_port}, running test...")
353
-
354
- # Run the app as a subprocess with a timeout
355
  process = subprocess.Popen(
356
- ["python", str(app_path), "--server-port", str(free_port)],
357
  stdout=subprocess.PIPE,
358
  stderr=subprocess.PIPE,
359
  text=True,
360
  )
361
- stdout, stderr = process.communicate(timeout=15)
362
 
363
- if "Running on local URL" in stdout:
364
- return "✅ Test passed: App launched successfully."
365
- elif process.returncode != 0:
 
 
366
  error_message = (
367
- f"❌ Test failed: App exited with code {process.returncode}\n"
 
368
  f"---STDERR---\n{stderr}\n---STDOUT---\n{stdout}"
369
  )
370
  return error_message
371
- else:
372
- inconclusive_message = (
373
- f"⚠️ Test inconclusive: App ran without clear success message.\n"
374
- f"---STDERR---\n{stderr}\n---STDOUT---\n{stdout}"
375
- )
376
- return inconclusive_message
 
 
 
377
 
378
- except subprocess.TimeoutExpired:
379
- return "❌ Test failed: App ran for over 15 seconds and was terminated."
380
  except Exception as e:
381
  return f"❌ An unexpected error occurred during testing: {str(e)}"
382
 
 
336
  @tool
337
  def test_app_py() -> str:
338
  """
339
+ Test the app.py file by running it as a subprocess.
340
+ This test uses a hardcoded port (7865) to avoid conflicts.
341
+ A successful test means the app launches and runs without crashing.
342
  """
343
+ TEST_PORT = 7865
344
  try:
345
  app_path = Path("sandbox") / "app.py"
346
  if not app_path.exists():
347
  return "Error: app.py not found in sandbox directory."
348
 
349
+ print(f"--- Starting test on port {TEST_PORT} ---")
 
 
 
 
 
 
 
350
  process = subprocess.Popen(
351
+ ["python", str(app_path), "--server-port", str(TEST_PORT)],
352
  stdout=subprocess.PIPE,
353
  stderr=subprocess.PIPE,
354
  text=True,
355
  )
 
356
 
357
+ # The key is to see if the process crashes quickly.
358
+ # If it runs for a few seconds, it's considered a success.
359
+ try:
360
+ # Wait for 5 seconds. If it exits, it's a failure.
361
+ stdout, stderr = process.communicate(timeout=5)
362
  error_message = (
363
+ f"❌ Test failed: App exited unexpectedly before timeout.\n"
364
+ f"---EXIT CODE---\n{process.returncode}\n"
365
  f"---STDERR---\n{stderr}\n---STDOUT---\n{stdout}"
366
  )
367
  return error_message
368
+ except subprocess.TimeoutExpired:
369
+ # This is the SUCCESS case! The app ran for 5s without crashing.
370
+ print(" Test successful: App process is stable.")
371
+ process.terminate() # Clean up the process
372
+ try:
373
+ process.wait(timeout=2) # Wait for graceful shutdown
374
+ except subprocess.TimeoutExpired:
375
+ process.kill() # Force kill if it doesn't respond
376
+ return "✅ Test passed: App launched successfully."
377
 
 
 
378
  except Exception as e:
379
  return f"❌ An unexpected error occurred during testing: {str(e)}"
380