77import logging
88import pathlib
99import shutil
10+ import datetime
1011
11- from git import Repo , Commit
12+ from git import Repo
13+ from llm4papers .config import Settings
1214
1315from llm4papers .editor_agents .EditorAgent import EditorAgent
1416from llm4papers .models import Document , EditRequest
1517from llm4papers .paper_remote import PaperRemote , logger
1618
1719
18- def _line_was_in_last_n_commit (
20+ def _too_close_to_human_edits (
1921 repo : Repo , filename : str , line_number : int , last_n : int = 2
2022) -> bool :
2123 """
@@ -27,6 +29,19 @@ def _line_was_in_last_n_commit(
2729 made without stomping on the user's edits.
2830
2931 """
32+ # Get the date of the nth-back commit:
33+ last_commit_date = repo .head .commit .committed_datetime
34+ now = datetime .datetime .now (tz = datetime .timezone .utc )
35+
36+ print (f"Last commit date: { last_commit_date } " )
37+ print (f"Now: { now } " )
38+
39+ sec_since_last_commit = (now - last_commit_date ).total_seconds ()
40+
41+ # If the last commit was more than 10 seconds ago, any edit is fine:
42+ if sec_since_last_commit > 10 :
43+ return False
44+
3045 # Get the diff for HEAD~n:
3146 total_diff = repo .git .diff (f"HEAD~{ last_n } " , filename , unified = 0 )
3247
@@ -40,22 +55,6 @@ def _line_was_in_last_n_commit(
4055 if current_line in total_diff :
4156 return True
4257
43- # Get the n latest commits, in reverse chronological order
44- # commits: list[Commit] = repo.iter_commits(max_count=last_n)
45- # for commit in commits:
46- # # Get the diffs for this commit
47- # # https://gitpython.readthedocs.io/en/stable/reference.html?highlight=iter_commits#git.diff.Diffable
48- # diffs = commit.diff()
49-
50- # # Check if the line was changed in any of the diffs
51- # for diff in diffs:
52- # print("Path eq", diff.b_blob.path, filename)
53- # if diff.b_blob is None or diff.b_blob.path != filename:
54- # continue
55-
56- # # TODO: Get the line number of the diff... Not clear to me this is
57- # # even doable in a dependable way with "regular" git.
58-
5958 return False
6059
6160
@@ -150,7 +149,7 @@ def get_next_edit_request(self) -> EditRequest:
150149 repo_scoped_file = pathlib .Path (file ).relative_to (
151150 self ._repo .working_tree_dir
152151 )
153- if _line_was_in_last_n_commit (
152+ if _too_close_to_human_edits (
154153 self ._repo , str (repo_scoped_file ), i
155154 ):
156155 logging .info (
@@ -208,7 +207,17 @@ def perform_edit(self, edit: EditRequest, agent_cascade: list[EditorAgent]):
208207 # We replace instead of just setting
209208 # `lines[edit.line_range[0]:edit.line_range[1]] = new_lines`
210209 # because the number of lines may be different.
211- lines = lines [: edit .line_range [0 ]] + [new_lines ] + lines [edit .line_range [1 ] :]
210+ edited_lines = lines [edit .line_range [0 ] : edit .line_range [1 ]]
211+ if Settings ().retain_originals_as_comments :
212+ edited_lines = [f"% { line .split ('@ai' )[0 ]} \n " for line in edited_lines ]
213+ else :
214+ edited_lines = []
215+ lines = (
216+ lines [: edit .line_range [0 ]]
217+ + edited_lines
218+ + [new_lines ]
219+ + lines [edit .line_range [1 ] :]
220+ )
212221
213222 with open (edit .file_path , "w" ) as f :
214223 f .writelines (lines )
0 commit comments