44 lines
2.3 KiB
Python
44 lines
2.3 KiB
Python
"""Apply a narrow, reversible DB-GPT 0.8.2 reasoning-format compatibility fix.
|
|
|
|
Reproducer: removing a vis-thinking block with an empty string can join
|
|
`Thought: ` to the following `Action:` line, making valid tool calls unparseable.
|
|
The fix preserves a logical newline. It does not change tool choice or answers.
|
|
"""
|
|
from pathlib import Path
|
|
import hashlib,json,sys
|
|
|
|
ROOT=Path(__file__).resolve().parents[1]
|
|
TRIAL=ROOT/'.runtime/dbgpt-trial'
|
|
TARGET=TRIAL/'venv/lib/python3.11/site-packages/dbgpt/agent/util/react_parser.py'
|
|
OLD='return re.sub(pattern, "", text, flags=re.DOTALL)'
|
|
NEW='return re.sub(pattern, "\\n", text, flags=re.DOTALL)'
|
|
|
|
def apply():
|
|
source=TARGET.read_text()
|
|
if NEW in source:
|
|
print('Reasoning newline compatibility fix already present');return
|
|
if source.count(OLD)!=1:raise RuntimeError('Unexpected parser version; no files modified')
|
|
backup=TRIAL/'compat/react_parser.py.original'
|
|
backup.parent.mkdir(parents=True,exist_ok=True)
|
|
if backup.exists() and backup.read_text()!=source:raise RuntimeError('Original parser backup differs; no files modified')
|
|
backup.write_text(source)
|
|
patched=source.replace(OLD,NEW)
|
|
TARGET.write_text(patched)
|
|
(backup.parent/'patch.json').write_text(json.dumps({'package':'dbgpt','version':'0.8.2','change':'Preserve newline when removing vis-thinking blocks','original_sha256':hashlib.sha256(source.encode()).hexdigest(),'patched_sha256':hashlib.sha256(patched.encode()).hexdigest()},indent=2))
|
|
print('Applied reasoning newline compatibility fix; original preserved')
|
|
|
|
def check():
|
|
from dbgpt.agent.util.react_parser import ReActOutputParser
|
|
raw=(TRIAL/'mimo-round-raw.txt').read_text()
|
|
parsed=ReActOutputParser().parse_current_step(raw)
|
|
assert len(parsed)==1 and parsed[0].action=='sql_query', 'Recorded real response must remain an executable SQL action'
|
|
assert isinstance(parsed[0].action_input,dict) and 'sql' in parsed[0].action_input
|
|
fenced='Thought: explain\n```text\nAction: terminate\nAction Input: {"result":"bad"}\n```\nAction: sql_query\nAction Input: {"sql":"SELECT 1"}'
|
|
steps=ReActOutputParser().parse_current_step(fenced)
|
|
assert len(steps)==1 and steps[0].action=='sql_query', 'Code-fenced labels must remain inert'
|
|
print('PASS: real MiMo response and fenced-label regression checks')
|
|
|
|
if __name__=='__main__':
|
|
if len(sys.argv)>1 and sys.argv[1]=='check':check()
|
|
else:apply()
|