Skip to content

Commit 730adfb

Browse files
authored
Merge branch 'main' into chore/add-staging/docs
2 parents eb50e80 + 3171bb8 commit 730adfb

20 files changed

+635
-193
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ For detailed installation and usage instructions, visit our documentation at [do
6565

6666
https://github.com/user-attachments/assets/38f44f4e-be1c-4f84-8db9-63d5ee3e61e5
6767

68+
- Optiming a workflow end to end automatically with `codeflash optimize`
69+
70+
71+
https://github.com/user-attachments/assets/355ba295-eb5a-453a-8968-7fb35c70d16c
72+
73+
74+
6875
## Support
6976

7077
Join our community for support and discussions. If you have any questions, feel free to reach out to us using one of the following methods:

SECURITY.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Security Policy
2+
3+
This document outlines Codeflash's vulnerability disclosure policy. For more information about Codeflash's approach to security, please visit [codeflash.ai/security](https://www.codeflash.ai/security).
4+
5+
## Supported Versions
6+
7+
Since Codeflash is moving quickly, we can only commit to fixing security issues for the latest version of codeflash client.
8+
If a vulnerability is discovered in our backend, we will release the fix for all the users.
9+
10+
## Reporting a Vulnerability
11+
12+
13+
Please do not report security vulnerabilities through public GitHub issues.
14+
15+
Instead, please report them to our [GitHub Security page](https://github.com/codeflash-ai/codeflash/security). If you prefer to submit one without using GitHub, you can also email us at [email protected].
16+
17+
We commit to acknowledging vulnerability reports immediately, and will work to fix active vulnerabilities as soon as we can. We will publish resolved vulnerabilities in the form of security advisories on our GitHub security page. Critical incidents will be communicated both on the GitHub security page and via email to all affected users.
18+
19+
We appreciate your help in making Codeflash more secure for everyone. Thank you for your support and responsible disclosure.
Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,2 @@
11
DEFAULT_API_URL = "https://api.galileo.ai/"
22
DEFAULT_APP_URL = "https://app.galileo.ai/"
3-
4-
5-
# function_names: GalileoApiClient.get_console_url
6-
# module_abs_path : /home/mohammed/Work/galileo-python/src/galileo/api_client.py
7-
# preexisting_objects: {('GalileoApiClient', ()), ('_set_destination', ()), ('get_console_url', (FunctionParent(name='GalileoApiClient', type='ClassDef'),))}
8-
# project_root_path: /home/mohammed/Work/galileo-python/src

codeflash/api/aiservice.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from codeflash.code_utils.env_utils import get_codeflash_api_key, is_LSP_enabled
1414
from codeflash.code_utils.git_utils import get_last_commit_author_if_pr_exists, get_repo_owner_and_name
1515
from codeflash.models.ExperimentMetadata import ExperimentMetadata
16-
from codeflash.models.models import AIServiceRefinerRequest, OptimizedCandidate
16+
from codeflash.models.models import AIServiceRefinerRequest, CodeStringsMarkdown, OptimizedCandidate
1717
from codeflash.telemetry.posthog_cf import ph
1818
from codeflash.version import __version__ as codeflash_version
1919

@@ -136,7 +136,7 @@ def optimize_python_code( # noqa: D417
136136
logger.debug(f"Generating optimizations took {end_time - start_time:.2f} seconds.")
137137
return [
138138
OptimizedCandidate(
139-
source_code=opt["source_code"],
139+
source_code=CodeStringsMarkdown.parse_markdown_code(opt["source_code"]),
140140
explanation=opt["explanation"],
141141
optimization_id=opt["optimization_id"],
142142
)
@@ -206,7 +206,7 @@ def optimize_python_code_line_profiler( # noqa: D417
206206
console.rule()
207207
return [
208208
OptimizedCandidate(
209-
source_code=opt["source_code"],
209+
source_code=CodeStringsMarkdown.parse_markdown_code(opt["source_code"]),
210210
explanation=opt["explanation"],
211211
optimization_id=opt["optimization_id"],
212212
)
@@ -263,7 +263,7 @@ def optimize_python_code_refinement(self, request: list[AIServiceRefinerRequest]
263263
console.rule()
264264
return [
265265
OptimizedCandidate(
266-
source_code=opt["source_code"],
266+
source_code=CodeStringsMarkdown.parse_markdown_code(opt["source_code"]),
267267
explanation=opt["explanation"],
268268
optimization_id=opt["optimization_id"][:-4] + "refi",
269269
)

codeflash/api/cfapi.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ def get_user_id() -> Optional[str]:
101101
if min_version and version.parse(min_version) > version.parse(__version__):
102102
msg = "Your Codeflash CLI version is outdated. Please update to the latest version using `pip install --upgrade codeflash`."
103103
console.print(f"[bold red]{msg}[/bold red]")
104+
if console.quiet: # lsp
105+
logger.debug(msg)
106+
return f"Error: {msg}"
104107
sys.exit(1)
105108
return userid
106109

codeflash/code_utils/code_replacer.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from pathlib import Path
2020

2121
from codeflash.discovery.functions_to_optimize import FunctionToOptimize
22-
from codeflash.models.models import CodeOptimizationContext, OptimizedCandidate, ValidCode
22+
from codeflash.models.models import CodeOptimizationContext, CodeStringsMarkdown, OptimizedCandidate, ValidCode
2323

2424
ASTNodeT = TypeVar("ASTNodeT", bound=ast.AST)
2525

@@ -408,16 +408,17 @@ def replace_functions_and_add_imports(
408408

409409
def replace_function_definitions_in_module(
410410
function_names: list[str],
411-
optimized_code: str,
411+
optimized_code: CodeStringsMarkdown,
412412
module_abspath: Path,
413413
preexisting_objects: set[tuple[str, tuple[FunctionParent, ...]]],
414414
project_root_path: Path,
415415
) -> bool:
416416
source_code: str = module_abspath.read_text(encoding="utf8")
417+
code_to_apply = get_optimized_code_for_module(module_abspath.relative_to(project_root_path), optimized_code)
417418
new_code: str = replace_functions_and_add_imports(
418-
add_global_assignments(optimized_code, source_code),
419+
add_global_assignments(code_to_apply, source_code),
419420
function_names,
420-
optimized_code,
421+
code_to_apply,
421422
module_abspath,
422423
preexisting_objects,
423424
project_root_path,
@@ -428,6 +429,19 @@ def replace_function_definitions_in_module(
428429
return True
429430

430431

432+
def get_optimized_code_for_module(relative_path: Path, optimized_code: CodeStringsMarkdown) -> str:
433+
file_to_code_context = optimized_code.file_to_path()
434+
module_optimized_code = file_to_code_context.get(str(relative_path))
435+
if module_optimized_code is None:
436+
logger.warning(
437+
f"Optimized code not found for {relative_path} In the context\n-------\n{optimized_code}\n-------\n"
438+
"re-check your 'markdown code structure'"
439+
f"existing files are {file_to_code_context.keys()}"
440+
)
441+
module_optimized_code = ""
442+
return module_optimized_code
443+
444+
431445
def is_zero_diff(original_code: str, new_code: str) -> bool:
432446
return normalize_code(original_code) == normalize_code(new_code)
433447

codeflash/code_utils/env_utils.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,12 @@ def check_formatter_installed(formatter_cmds: list[str], exit_on_failure: bool =
3434

3535
@lru_cache(maxsize=1)
3636
def get_codeflash_api_key() -> str:
37-
api_key = os.environ.get("CODEFLASH_API_KEY") or read_api_key_from_shell_config()
37+
if console.quiet: # lsp
38+
# prefer shell config over env var in lsp mode
39+
api_key = read_api_key_from_shell_config()
40+
else:
41+
api_key = os.environ.get("CODEFLASH_API_KEY") or read_api_key_from_shell_config()
42+
3843
api_secret_docs_message = "For more information, refer to the documentation at [https://docs.codeflash.ai/getting-started/codeflash-github-actions#add-your-api-key-to-your-repository-secrets]." # noqa
3944
if not api_key:
4045
msg = (

codeflash/code_utils/formatter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def is_diff_line(line: str) -> bool:
104104
def format_code(
105105
formatter_cmds: list[str],
106106
path: Union[str, Path],
107-
optimized_function: str = "",
107+
optimized_code: str = "",
108108
check_diff: bool = False, # noqa
109109
print_status: bool = True, # noqa
110110
exit_on_failure: bool = True, # noqa
@@ -121,7 +121,7 @@ def format_code(
121121

122122
if check_diff and original_code_lines > 50:
123123
# we dont' count the formatting diff for the optimized function as it should be well-formatted
124-
original_code_without_opfunc = original_code.replace(optimized_function, "")
124+
original_code_without_opfunc = original_code.replace(optimized_code, "")
125125

126126
original_temp = Path(test_dir_str) / "original_temp.py"
127127
original_temp.write_text(original_code_without_opfunc, encoding="utf8")

codeflash/code_utils/shell_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def get_shell_rc_path() -> Path:
4242

4343

4444
def get_api_key_export_line(api_key: str) -> str:
45-
return f"{SHELL_RC_EXPORT_PREFIX}{api_key}"
45+
return f'{SHELL_RC_EXPORT_PREFIX}"{api_key}"'
4646

4747

4848
def save_api_key_to_rc(api_key: str) -> Result[str, str]:

codeflash/context/code_context_extractor.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,14 @@ def get_code_optimization_context(
6161
)
6262

6363
# Extract code context for optimization
64-
final_read_writable_code = extract_code_string_context_from_files(
64+
final_read_writable_code = extract_code_markdown_context_from_files(
6565
helpers_of_fto_dict,
6666
{},
6767
project_root_path,
6868
remove_docstrings=False,
6969
code_context_type=CodeContextType.READ_WRITABLE,
70-
).code
70+
)
71+
7172
read_only_code_markdown = extract_code_markdown_context_from_files(
7273
helpers_of_fto_dict,
7374
helpers_of_helpers_dict,
@@ -84,14 +85,14 @@ def get_code_optimization_context(
8485
)
8586

8687
# Handle token limits
87-
final_read_writable_tokens = encoded_tokens_len(final_read_writable_code)
88+
final_read_writable_tokens = encoded_tokens_len(final_read_writable_code.markdown)
8889
if final_read_writable_tokens > optim_token_limit:
8990
raise ValueError("Read-writable code has exceeded token limit, cannot proceed")
9091

9192
# Setup preexisting objects for code replacer
9293
preexisting_objects = set(
9394
chain(
94-
find_preexisting_objects(final_read_writable_code),
95+
*(find_preexisting_objects(codestring.code) for codestring in final_read_writable_code.code_strings),
9596
*(find_preexisting_objects(codestring.code) for codestring in read_only_code_markdown.code_strings),
9697
)
9798
)

0 commit comments

Comments
 (0)