pref(prompt-optimizer): handle escaped quotes in JSON parsing#903
Merged
keeees merged 1 commit intorelease/v0.3.0from Apr 15, 2026
Merged
pref(prompt-optimizer): handle escaped quotes in JSON parsing#903keeees merged 1 commit intorelease/v0.3.0from
keeees merged 1 commit intorelease/v0.3.0from
Conversation
Contributor
审阅者指南(在小型 PR 上默认折叠)审阅者指南调整提示优化器的 JSON 解析逻辑,在提取用于提示检测的缓存内容之前,正确处理结尾处经过转义的反斜杠。 文件级更改
技巧与命令与 Sourcery 交互
自定义你的体验前往你的控制面板以:
获取帮助Original review guide in EnglishReviewer's guide (collapsed on small PRs)Reviewer's GuideAdjusts the prompt optimizer’s JSON parsing to correctly handle trailing escaped backslashes before extracting cached content for prompt detection. File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Contributor
There was a problem hiding this comment.
Hey - 我发现了 1 个问题,并给出了一些整体性的反馈:
- 当存在很多结尾反斜杠时,处理反斜杠的循环可能会出现非预期行为(例如,
last_idx可能会变成 0 并继续变为负数,从而改变buffer[:-last_idx]的含义,并可能导致无限循环);建议重写这段逻辑,只计算一次安全的截断索引(或者在已切片的 buffer 上使用类似rstrip('\\')的方法),而不是用递减的负索引不断重新切片。 - 硬编码常量
20和初始的last_idx = 19让截断行为很难推理;将这些提取到命名清晰的变量中,和/或添加简短注释解释协议相关的后缀长度以及调整反斜杠的意图,会提升可维护性。
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The backslash-handling loop can behave unexpectedly when many trailing backslashes are present (e.g., `last_idx` can reach 0 and go negative, changing the meaning of `buffer[:-last_idx]` and potentially causing an infinite loop); consider rewriting this logic to compute a safe cutoff index once (or use something like `rstrip('\\')` on the sliced buffer) instead of repeatedly reslicing with a decreasing negative index.
- The hardcoded constants `20` and initial `last_idx = 19` make the truncation behavior difficult to reason about; extracting these into clearly named variables and/or adding a brief comment explaining the protocol-specific suffix length and the intent of the backslash adjustment would improve maintainability.
## Individual Comments
### Comment 1
<location path="api/app/services/prompt_optimizer_service.py" line_range="238-242" />
<code_context>
logger.error(f"Unsupported content type - {content}")
raise Exception("Unsupported content type")
cache = buffer[:-20]
+ last_idx = 19
+ while cache and cache[-1] == '\\':
+ cache = buffer[:-last_idx]
+ last_idx -= 1
# 尝试找到 "prompt": " 开始位置
</code_context>
<issue_to_address>
**issue (bug_risk):** Trailing backslash trimming loop likely has flawed slicing logic and can behave unexpectedly.
The loop intends to strip trailing backslashes from `cache`, but instead repeatedly re-slices from `buffer` with a *decreasing* `last_idx`, which expands the slice rather than trimming it. This can yield unexpected values and, when `last_idx` reaches 0, `buffer[:-0]` becomes `''`, which is likely unintended. Consider operating directly on `cache` (e.g. `cache = cache.rstrip('\\')` or `cache = cache[:-1]` in a loop) instead of re-deriving it from `buffer`.
</issue_to_address>帮我变得更有用!请对每条评论点 👍 或 👎,我会根据你的反馈改进后续的评审。
Original comment in English
Hey - I've found 1 issue, and left some high level feedback:
- The backslash-handling loop can behave unexpectedly when many trailing backslashes are present (e.g.,
last_idxcan reach 0 and go negative, changing the meaning ofbuffer[:-last_idx]and potentially causing an infinite loop); consider rewriting this logic to compute a safe cutoff index once (or use something likerstrip('\')on the sliced buffer) instead of repeatedly reslicing with a decreasing negative index. - The hardcoded constants
20and initiallast_idx = 19make the truncation behavior difficult to reason about; extracting these into clearly named variables and/or adding a brief comment explaining the protocol-specific suffix length and the intent of the backslash adjustment would improve maintainability.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The backslash-handling loop can behave unexpectedly when many trailing backslashes are present (e.g., `last_idx` can reach 0 and go negative, changing the meaning of `buffer[:-last_idx]` and potentially causing an infinite loop); consider rewriting this logic to compute a safe cutoff index once (or use something like `rstrip('\')` on the sliced buffer) instead of repeatedly reslicing with a decreasing negative index.
- The hardcoded constants `20` and initial `last_idx = 19` make the truncation behavior difficult to reason about; extracting these into clearly named variables and/or adding a brief comment explaining the protocol-specific suffix length and the intent of the backslash adjustment would improve maintainability.
## Individual Comments
### Comment 1
<location path="api/app/services/prompt_optimizer_service.py" line_range="238-242" />
<code_context>
logger.error(f"Unsupported content type - {content}")
raise Exception("Unsupported content type")
cache = buffer[:-20]
+ last_idx = 19
+ while cache and cache[-1] == '\\':
+ cache = buffer[:-last_idx]
+ last_idx -= 1
# 尝试找到 "prompt": " 开始位置
</code_context>
<issue_to_address>
**issue (bug_risk):** Trailing backslash trimming loop likely has flawed slicing logic and can behave unexpectedly.
The loop intends to strip trailing backslashes from `cache`, but instead repeatedly re-slices from `buffer` with a *decreasing* `last_idx`, which expands the slice rather than trimming it. This can yield unexpected values and, when `last_idx` reaches 0, `buffer[:-0]` becomes `''`, which is likely unintended. Consider operating directly on `cache` (e.g. `cache = cache.rstrip('\\')` or `cache = cache[:-1]` in a loop) instead of re-deriving it from `buffer`.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
9746ffc to
ed765b7
Compare
keeees
approved these changes
Apr 15, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary by Sourcery
Bug Fixes:
Original summary in English
Summary by Sourcery
Bug Fixes: