Skip to content

Commit 3ca1435

Browse files
authored
Merge pull request #1198 from gpt-engineer-org/1197-failed-to-update-new-codes-from-gpte
1197 failed to update new codes from gpte
2 parents c1f4a9d + acad5fa commit 3ca1435

File tree

6 files changed

+200
-2
lines changed

6 files changed

+200
-2
lines changed

gpt_engineer/core/chat_to_files.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,14 @@ def parse_diffs(diff_string: str) -> dict:
142142
diff_block = block.group()
143143

144144
# Parse individual diff blocks and update the diffs dictionary
145-
diffs.update(parse_diff_block(diff_block))
145+
diff = parse_diff_block(diff_block)
146+
for filename, diff_obj in diff.items():
147+
if filename not in diffs:
148+
diffs[filename] = diff_obj
149+
else:
150+
print(
151+
f"\nMultiple diffs found for {filename}. Only the first one is kept."
152+
)
146153
except TimeoutError:
147154
print("gpt-engineer timed out while parsing git diff")
148155

gpt_engineer/preprompts/file_format_diff

+3-1
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@ Example of a git diff creating a new file:
3030
+
3131
+Last example line
3232
```
33+
34+
RULES:
3335
-A program will apply the diffs you generate exactly to the code, so diffs must be precise and unambiguous!
3436
-Every diff must be fenced with triple backtick ```.
3537
-The file names at the beginning of a diff, (lines starting with --- and +++) is the relative path to the file before and after the diff.
3638
-LINES TO BE REMOVED (starting with single -) AND LINES TO BE RETAIN (no starting symbol) HAVE TO REPLICATE THE DIFFED HUNK OF THE CODE EXACTLY LINE BY LINE. KEEP THE NUMBER OF RETAIN LINES SMALL IF POSSIBLE.
3739
-EACH LINE IN THE SOURCE FILES STARTS WITH A LINE NUMBER, WHICH IS NOT PART OF THE SOURCE CODE. NEVER TRANSFER THESE LINE NUMBERS TO THE DIFF HUNKS.
3840
-AVOID STARTING A HUNK WITH AN EMPTY LINE.
39-
-ALL DIFFS WILL BE APPLIED SIMULTANEOUSLY, AVOID SEQUENTIAL HUNKS.
41+
-ENSURE ALL CHANGES ARE PROVIDED IN A SINGLE DIFF CHUNK PER FILE TO PREVENT MULTIPLE DIFFS ON THE SAME FILE.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
To achieve the separation of environments (stage and local) and to use H2 DB for the local environment, we need to make the following changes:
2+
3+
1. Create a new `application-local.yml` configuration file for the local environment.
4+
2. Modify the existing `application.yml` to be used for the stage environment.
5+
3. Update the `run.sh` script to handle both environments.
6+
7+
Let's start with the changes:
8+
9+
### 1. Create `application-local.yml` for the local environment
10+
11+
```diff
12+
--- /dev/null
13+
+++ src/main/resources/application-local.yml
14+
@@ -0,0 +1,13 @@
15+
+spring:
16+
+ datasource:
17+
+ url: jdbc:h2:mem:testdb
18+
+ driver-class-name: org.h2.Driver
19+
+ username: sa
20+
+ password: password
21+
+ h2:
22+
+ console:
23+
+ enabled: true
24+
+ jpa:
25+
+ hibernate:
26+
+ ddl-auto: update
27+
+ show-sql: true
28+
```
29+
30+
### 2. Modify `application.yml` to be used for the stage environment
31+
32+
```diff
33+
--- src/main/resources/application.yml
34+
+++ src/main/resources/application.yml
35+
@@ -1,13 +1,13 @@
36+
spring:
37+
datasource:
38+
- url: jdbc:postgresql://<RDS_ENDPOINT>:5432/<DB_NAME>
39+
- username: <DB_USERNAME>
40+
- password: <DB_PASSWORD>
41+
+ url: jdbc:postgresql://stage-rds-endpoint:5432/stage_db
42+
+ username: stage_user
43+
+ password: stage_password
44+
driver-class-name: org.postgresql.Driver
45+
jpa:
46+
hibernate:
47+
ddl-auto: update
48+
show-sql: true
49+
properties:
50+
hibernate:
51+
format_sql: true
52+
```
53+
54+
55+
56+
### 6. Update `application.yml` to include profile-specific configurations
57+
58+
```diff
59+
--- src/main/resources/application.yml
60+
+++ src/main/resources/application.yml
61+
@@ -1,13 +1,15 @@
62+
spring:
63+
profiles:
64+
- active: stage
65+
+ active: stage
66+
datasource:
67+
- url: jdbc:postgresql://<RDS_ENDPOINT>:5432/<DB_NAME>
68+
- username: <DB_USERNAME>
69+
- password: <DB_PASSWORD>
70+
+ url: jdbc:postgresql://stage-rds-endpoint:5432/stage_db
71+
+ username: stage_user
72+
+ password: stage_password
73+
driver-class-name: org.postgresql.Driver
74+
jpa:
75+
hibernate:
76+
ddl-auto: update
77+
show-sql: true
78+
properties:
79+
hibernate:
80+
format_sql: true
81+
```
82+
83+
### 7. Update `application-local.yml` to include profile-specific configurations
84+
85+
```diff
86+
--- src/main/resources/application-local.yml
87+
+++ src/main/resources/application-local.yml
88+
@@ -1,13 +1,15 @@
89+
spring:
90+
profiles:
91+
- active: local
92+
+ active: local
93+
datasource:
94+
- url: jdbc:h2:mem:testdb
95+
- driver-class-name: org.h2.Driver
96+
- username: sa
97+
- password: password
98+
+ url: jdbc:h2:mem:testdb
99+
+ driver-class-name: org.h2.Driver
100+
+ username: sa
101+
+ password: password
102+
h2:
103+
console:
104+
enabled: true
105+
jpa:
106+
hibernate:
107+
ddl-auto: update
108+
show-sql: true
109+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
spring:
2+
datasource:
3+
url: jdbc:postgresql://<RDS_ENDPOINT>:5432/<DB_NAME>
4+
username: <DB_USERNAME>
5+
password: <DB_PASSWORD>
6+
driver-class-name: org.postgresql.Driver
7+
jpa:
8+
hibernate:
9+
ddl-auto: update
10+
show-sql: true
11+
properties:
12+
hibernate:
13+
format_sql: true

tests/core/test_chat_to_files.py

+56
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,32 @@ class Calculator:
147147
Conclusion: ***
148148
"""
149149

150+
single_diff = """
151+
```
152+
--- a/file1.txt
153+
+++ a/file1.txt
154+
@@ -1,3 +1,3 @@
155+
-old line
156+
+new line
157+
```
158+
"""
159+
multi_diff = """
160+
```
161+
--- a/file1.txt
162+
+++ a/file1.txt
163+
@@ -1,3 +1,3 @@
164+
-old line
165+
+new line
166+
```
167+
```
168+
--- a/file1.txt
169+
+++ a/file1.txt
170+
@@ -2,3 +2,3 @@
171+
-another old line
172+
+another new line
173+
```
174+
"""
175+
150176

151177
# Single function tests
152178
def test_basic_similarity():
@@ -289,6 +315,36 @@ def parse_chats_with_regex(
289315
return diff_content, code_content, diffs
290316

291317

318+
def capture_print_output(func):
319+
import io
320+
import sys
321+
322+
captured_output = io.StringIO()
323+
sys.stdout = captured_output
324+
func()
325+
sys.stdout = sys.__stdout__
326+
return captured_output
327+
328+
329+
def test_single_diff():
330+
diffs = parse_diffs(single_diff)
331+
correct_diff = "\n".join(single_diff.strip().split("\n")[1:-1])
332+
assert diffs["a/file1.txt"].diff_to_string() == correct_diff
333+
334+
335+
def test_multi_diff_discard():
336+
captured_output = capture_print_output(lambda: parse_diffs(multi_diff))
337+
diffs = parse_diffs(multi_diff)
338+
correct_diff = "\n".join(multi_diff.strip().split("\n")[1:8]).replace(
339+
"```\n```", ""
340+
)
341+
assert (
342+
"Multiple diffs found for a/file1.txt. Only the first one is kept."
343+
in captured_output.getvalue()
344+
)
345+
assert diffs["a/file1.txt"].diff_to_string().strip() == correct_diff.strip()
346+
347+
292348
# test parse diff
293349
def test_controller_diff():
294350
parse_chats_with_regex("controller_chat", "controller_code")

tests/core/test_salvage_correct_hunks.py

+11
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,17 @@ def test_theo_case():
8989
print(updated_files["run.py"])
9090

9191

92+
def test_zbf_yml_missing():
93+
files = FilesDict(
94+
{"src/main/resources/application.yml": get_file_content("zbf_yml_missing_code")}
95+
)
96+
updated_files, _ = salvage_correct_hunks(
97+
message_builder("zbf_yml_missing_chat"), files, memory
98+
)
99+
print(updated_files["src/main/resources/application.yml"])
100+
print(updated_files["src/main/resources/application-local.yml"])
101+
102+
92103
def test_clean_up_folder(clean_up_folder):
93104
# The folder should be deleted after the test is run
94105
assert True

0 commit comments

Comments
 (0)