Skip to content

Commit 3871853

Browse files
authored
Merge branch 'main' into chore/asyncio-optimization
2 parents 2d57414 + e973c69 commit 3871853

File tree

4 files changed

+37
-35
lines changed

4 files changed

+37
-35
lines changed

codeflash/api/cfapi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
from packaging import version
2828

29-
if os.environ.get("CODEFLASH_CFAPI_SERVER", default="prod").lower() == "local":
29+
if os.environ.get("CODEFLASH_CFAPI_SERVER", "prod").lower() == "local":
3030
CFAPI_BASE_URL = "http://localhost:3001"
3131
logger.info(f"Using local CF API at {CFAPI_BASE_URL}.")
3232
console.rule()

codeflash/lsp/lsp_logger.py

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
import logging
44
import sys
55
from dataclasses import dataclass
6-
from typing import Any, Callable, Optional
6+
from typing import Any, Callable
77

88
from codeflash.lsp.helpers import is_LSP_enabled
9-
from codeflash.lsp.lsp_message import LspTextMessage
9+
from codeflash.lsp.lsp_message import LspTextMessage, message_delimiter
1010

1111
root_logger = None
1212

@@ -43,16 +43,19 @@ def add_heading_tags(msg: str, tags: LspMessageTags) -> str:
4343
return msg
4444

4545

46-
def extract_tags(msg: str) -> tuple[Optional[LspMessageTags], str]:
46+
def extract_tags(msg: str) -> tuple[LspMessageTags, str]:
4747
delimiter = "|"
48-
parts = msg.split(delimiter)
49-
if len(parts) == 2:
48+
first_delim_idx = msg.find(delimiter)
49+
if first_delim_idx != -1 and msg.count(delimiter) == 1:
50+
tags_str = msg[:first_delim_idx]
51+
content = msg[first_delim_idx + 1 :]
52+
tags = {tag.strip() for tag in tags_str.split(",")}
5053
message_tags = LspMessageTags()
51-
tags = {tag.strip() for tag in parts[0].split(",")}
52-
if "!lsp" in tags:
53-
message_tags.not_lsp = True
54+
# manually check and set to avoid repeated membership tests
5455
if "lsp" in tags:
5556
message_tags.lsp = True
57+
if "!lsp" in tags:
58+
message_tags.not_lsp = True
5659
if "force_lsp" in tags:
5760
message_tags.force_lsp = True
5861
if "loading" in tags:
@@ -67,9 +70,9 @@ def extract_tags(msg: str) -> tuple[Optional[LspMessageTags], str]:
6770
message_tags.h3 = True
6871
if "h4" in tags:
6972
message_tags.h4 = True
70-
return message_tags, delimiter.join(parts[1:])
73+
return message_tags, content
7174

72-
return None, msg
75+
return LspMessageTags(), msg
7376

7477

7578
supported_lsp_log_levels = ("info", "debug")
@@ -86,31 +89,32 @@ def enhanced_log(
8689
actual_log_fn(msg, *args, **kwargs)
8790
return
8891

89-
is_lsp_json_message = msg.startswith('{"type"')
92+
is_lsp_json_message = msg.startswith(message_delimiter) and msg.endswith(message_delimiter)
9093
is_normal_text_message = not is_lsp_json_message
9194

92-
# extract tags only from the text messages (not the json ones)
93-
tags, clean_msg = extract_tags(msg) if is_normal_text_message else (None, msg)
95+
# Extract tags only from text messages
96+
tags, clean_msg = extract_tags(msg) if is_normal_text_message else (LspMessageTags(), msg)
9497

9598
lsp_enabled = is_LSP_enabled()
96-
lsp_only = tags and tags.lsp
97-
98-
if not lsp_enabled and not lsp_only:
99-
# normal logging
100-
actual_log_fn(clean_msg, *args, **kwargs)
101-
return
102-
103-
#### LSP mode ####
104-
final_tags = tags if tags else LspMessageTags()
105-
10699
unsupported_level = level not in supported_lsp_log_levels
107-
if not final_tags.force_lsp and (final_tags.not_lsp or unsupported_level):
108-
return
109100

101+
# ---- Normal logging path ----
102+
if not tags.lsp:
103+
if not lsp_enabled: # LSP disabled
104+
actual_log_fn(clean_msg, *args, **kwargs)
105+
return
106+
if tags.not_lsp: # explicitly marked as not for LSP
107+
actual_log_fn(clean_msg, *args, **kwargs)
108+
return
109+
if unsupported_level and not tags.force_lsp: # unsupported level
110+
actual_log_fn(clean_msg, *args, **kwargs)
111+
return
112+
113+
# ---- LSP logging path ----
110114
if is_normal_text_message:
111-
clean_msg = add_heading_tags(clean_msg, final_tags)
112-
clean_msg = add_highlight_tags(clean_msg, final_tags)
113-
clean_msg = LspTextMessage(text=clean_msg, takes_time=final_tags.loading).serialize()
115+
clean_msg = add_heading_tags(clean_msg, tags)
116+
clean_msg = add_highlight_tags(clean_msg, tags)
117+
clean_msg = LspTextMessage(text=clean_msg, takes_time=tags.loading).serialize()
114118

115119
actual_log_fn(clean_msg, *args, **kwargs)
116120

codeflash/lsp/lsp_message.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
json_primitive_types = (str, float, int, bool)
1111
max_code_lines_before_collapse = 45
1212

13+
# \u241F is the message delimiter becuase it can be more than one message sent over the same message, so we need something to separate each message
14+
message_delimiter = "\u241f"
15+
1316

1417
@dataclass
1518
class LspMessage:
@@ -32,12 +35,8 @@ def type(self) -> str:
3235

3336
def serialize(self) -> str:
3437
data = self._loop_through(asdict(self))
35-
# Important: keep type as the first key, for making it easy and fast for the client to know if this is a lsp message before parsing it
3638
ordered = {"type": self.type(), **data}
37-
return (
38-
json.dumps(ordered)
39-
+ "\u241f" # \u241F is the message delimiter becuase it can be more than one message sent over the same message, so we need something to separate each message
40-
)
39+
return message_delimiter + json.dumps(ordered) + message_delimiter
4140

4241

4342
@dataclass

codeflash/optimization/function_optimizer.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1865,7 +1865,6 @@ def line_profiler_step(
18651865
self, code_context: CodeOptimizationContext, original_helper_code: dict[Path, str], candidate_index: int
18661866
) -> dict:
18671867
try:
1868-
logger.info("Running line profiling to identify performance bottlenecks…")
18691868
console.rule()
18701869

18711870
test_env = self.get_test_env(

0 commit comments

Comments
 (0)