Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions kai_mcp_solution_server/src/kai_mcp_solution_server/constants.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
# TODO: More robust logging
import logging
import sys
from datetime import datetime
from typing import Any

# Configure logging with more detail
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
handlers=[
logging.FileHandler("kai_mcp_server.log"),
logging.StreamHandler(sys.stderr),
],
)

logger = logging.getLogger("kai_mcp_solution_server")

# Keep the original log function for compatibility but enhance it
log_file = open("stderr.log", "a+")
log_file.close()


def log(*args: Any, **kwargs: Any) -> None:
print(*args, file=log_file if not log_file.closed else sys.stderr, **kwargs)
"""Legacy log function - enhanced with timestamp and logging level"""
timestamp = datetime.now().isoformat()
message = " ".join(str(arg) for arg in args)
logger.info(message)
print(
f"[{timestamp}] {message}",
file=log_file if not log_file.closed else sys.stderr,
**kwargs,
)
Comment on lines +18 to +32

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix legacy log(): avoid potential TypeError with conflicting 'file' kwarg and remove phantom file handle

  • The current implementation always passes a file= argument to print and also forwards kwargs; if the caller passes file in kwargs, print will raise “got multiple values for argument 'file'”.
  • The module-level log_file is opened and immediately closed; it will never be used. Ruff SIM115 is triggered here as well.

Apply this diff to simplify and harden the legacy logger:

-# Keep the original log function for compatibility but enhance it
-log_file = open("stderr.log", "a+")
-log_file.close()
+# Keep the original log function for compatibility but route to stdlib logging and stderr.

 def log(*args: Any, **kwargs: Any) -> None:
     """Legacy log function - enhanced with timestamp and logging level"""
     timestamp = datetime.now().isoformat()
     message = " ".join(str(arg) for arg in args)
     logger.info(message)
-    print(
-        f"[{timestamp}] {message}",
-        file=log_file if not log_file.closed else sys.stderr,
-        **kwargs,
-    )
+    # Preserve print-like kwargs but avoid conflicting 'file'
+    kwargs = dict(kwargs)
+    kwargs.pop("file", None)
+    print(f"[{timestamp}] {message}", file=sys.stderr, **kwargs)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
# Keep the original log function for compatibility but enhance it
log_file = open("stderr.log", "a+")
log_file.close()
def log(*args: Any, **kwargs: Any) -> None:
print(*args, file=log_file if not log_file.closed else sys.stderr, **kwargs)
"""Legacy log function - enhanced with timestamp and logging level"""
timestamp = datetime.now().isoformat()
message = " ".join(str(arg) for arg in args)
logger.info(message)
print(
f"[{timestamp}] {message}",
file=log_file if not log_file.closed else sys.stderr,
**kwargs,
)
# Keep the original log function for compatibility but route to stdlib logging and stderr.
def log(*args: Any, **kwargs: Any) -> None:
"""Legacy log function - enhanced with timestamp and logging level"""
timestamp = datetime.now().isoformat()
message = " ".join(str(arg) for arg in args)
logger.info(message)
# Preserve print-like kwargs but avoid conflicting 'file'
kwargs = dict(kwargs)
kwargs.pop("file", None)
print(f"[{timestamp}] {message}", file=sys.stderr, **kwargs)
🧰 Tools
🪛 Ruff (0.12.2)

19-19: Use a context manager for opening files

(SIM115)

12 changes: 12 additions & 0 deletions kai_mcp_solution_server/src/kai_mcp_solution_server/db/dao.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import logging
import sys
from datetime import datetime
from typing import Any
Expand Down Expand Up @@ -47,6 +48,8 @@
SolutionChangeSetJSON,
)

logger = logging.getLogger("kai_mcp_solution_server.db")


# https://github.com/pallets-eco/flask-sqlalchemy/issues/722#issuecomment-705672929
def drop_everything(con: Connection) -> None:
Expand Down Expand Up @@ -308,12 +311,21 @@ class DBSolution(Base):
)

def update_solution_status(self) -> None:
previous_status = self.solution_status
for file in self.after:
if file.status != SolutionStatus.ACCEPTED:
self.solution_status = file.status
if previous_status != self.solution_status:
logger.info(
f"[DB_STATE_CHANGE] Solution {self.id} status auto-updated: {previous_status} -> {self.solution_status} (due to file {file.uri} with status {file.status})"
)
return

self.solution_status = SolutionStatus.ACCEPTED
if previous_status != self.solution_status:
logger.info(
f"[DB_STATE_CHANGE] Solution {self.id} status auto-updated: {previous_status} -> ACCEPTED (all files accepted)"
)

def __hash__(self) -> int:
return hash(self.id)
Expand Down
Loading
Loading