-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.py
More file actions
1090 lines (905 loc) · 39.7 KB
/
agent.py
File metadata and controls
1090 lines (905 loc) · 39.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import json
import re
from codetide import CodeTide
from ...mcp.tools.patch_code import file_exists, open_file, process_patch, remove_file, write_file, parse_patch_blocks
from ...search.code_search import SmartCodeSearch
from ...core.defaults import DEFAULT_STORAGE_PATH
from ...parsers import SUPPORTED_LANGUAGES
from ...autocomplete import AutoComplete
from .models import Steps
from .prompts import (
AGENT_TIDE_SYSTEM_PROMPT, ASSESS_HISTORY_RELEVANCE_PROMPT, CALMNESS_SYSTEM_PROMPT,
CMD_BRAINSTORM_PROMPT, CMD_CODE_REVIEW_PROMPT, CMD_TRIGGER_PLANNING_STEPS,
CMD_WRITE_TESTS_PROMPT, DETERMINE_OPERATION_MODE_PROMPT, DETERMINE_OPERATION_MODE_SYSTEM,
FINALIZE_IDENTIFIERS_PROMPT, GATHER_CANDIDATES_PREFIX, GATHER_CANDIDATES_SYSTEM,
PREFIX_SUMMARY_PROMPT, README_CONTEXT_PROMPT, REJECT_PATCH_FEEDBACK_TEMPLATE,
REPO_TREE_CONTEXT_PROMPT, STAGED_DIFFS_TEMPLATE, STEPS_SYSTEM_PROMPT, WRITE_PATCH_SYSTEM_PROMPT
)
from .defaults import DEFAULT_MAX_HISTORY_TOKENS
from .utils import delete_file, parse_blocks, parse_steps_markdown, trim_to_patch_section
from .consts import AGENT_TIDE_ASCII_ART, REASONING_FINISHED, REASONING_STARTED, ROUND_FINISHED
try:
from aicore.llm import Llm
from aicore.logger import _logger
from .streaming.service import custom_logger_fn
except ImportError as e:
raise ImportError(
"The 'codetide.agents' module requires the 'aicore' package. "
"Install it with: pip install codetide[agents]"
) from e
from pydantic import BaseModel, Field, ConfigDict, model_validator
from prompt_toolkit.key_binding import KeyBindings
from prompt_toolkit import PromptSession
from typing import Dict, List, Optional, Set, Tuple
from typing_extensions import Self
from functools import partial
from datetime import date
from pathlib import Path
from ulid import ulid
import asyncio
import pygit2
import os
# ============================================================================
# Constants
# ============================================================================
FILE_TEMPLATE = """{FILENAME}
{CONTENT}
"""
# Default configuration values
DEFAULT_CONTEXT_WINDOW_SIZE = 3
DEFAULT_MAX_EXPANSION_ITERATIONS = 10
DEFAULT_MAX_CANDIDATE_ITERATIONS = 3
DEFAULT_SEARCH_TOP_K = 15
# Operation modes
OPERATION_MODE_STANDARD = "STANDARD"
OPERATION_MODE_PLAN_STEPS = "PLAN_STEPS"
OPERATION_MODE_PATCH_CODE = "PATCH_CODE"
# Commands to filter from history
COMMAND_PROMPTS = [
CMD_TRIGGER_PLANNING_STEPS,
CMD_WRITE_TESTS_PROMPT,
CMD_BRAINSTORM_PROMPT,
CMD_CODE_REVIEW_PROMPT
]
# ============================================================================
# Data Classes for Identifier Resolution
# ============================================================================
class IdentifierResolutionResult(BaseModel):
"""Result of the two-phase identifier resolution process."""
matches: List[str]
context_identifiers: List[str]
modify_identifiers: List[str]
summary: Optional[str]
all_reasoning: str
iteration_count: int
class OperationModeResult(BaseModel):
"""Result of operation mode extraction."""
operation_mode: str
sufficient_context: bool
expanded_history: list
search_query: Optional[str]
is_new_topic: Optional[bool]=None
topic_title: Optional[str]=None
# ============================================================================
# Helper Classes
# ============================================================================
class GitOperations:
"""Handles Git-related operations."""
def __init__(self, repo: pygit2.Repository, rootpath: Path):
self.repo = repo
self.rootpath = rootpath
def has_staged_changes(self) -> bool:
"""Check if there are staged changes in the repository."""
status = self.repo.status()
result = any([
file_status == pygit2.GIT_STATUS_INDEX_MODIFIED
for file_status in status.values()
])
_logger.logger.debug(f"has_staged_changes result={result}")
return result
async def get_staged_diff(self) -> str:
"""Get the diff of staged changes."""
if not Path(self.rootpath).is_dir():
raise FileNotFoundError(f"Directory not found: {self.rootpath}")
process = await asyncio.create_subprocess_exec(
'git', 'diff', '--staged',
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
cwd=self.rootpath
)
stdout, stderr = await process.communicate()
if process.returncode != 0:
raise Exception(f"Git command failed: {stderr.decode().strip()}")
return stdout.decode()
async def stage_files(self, changed_paths: List[str]) -> str:
"""Stage files and return the diff."""
index = self.repo.index
if not self.has_staged_changes():
for path in changed_paths:
index.add(str(Path(path)))
index.write()
staged_diff = await self.get_staged_diff()
staged_diff = staged_diff.strip()
return staged_diff if staged_diff else (
"No files were staged. Nothing to commit. "
"Tell the user to request some changes so there is something to commit"
)
def commit(self, message: str) -> pygit2.Commit:
"""
Commit all staged files with the given message.
Args:
message: Commit message
Returns:
The created commit object
Raises:
ValueError: If no files are staged for commit
Exception: For other git-related errors
"""
try:
config = self.repo.config
author_name = config._get('user.name')[1].value or 'Unknown Author'
author_email = config._get('user.email')[1].value or 'unknown@example.com'
author = pygit2.Signature(author_name, author_email)
committer = author
tree = self.repo.index.write_tree()
parents = [self.repo.head.target] if self.repo.head else []
commit_oid = self.repo.create_commit(
'HEAD',
author,
committer,
message,
tree,
parents
)
self.repo.index.write()
return self.repo[commit_oid]
except pygit2.GitError as e:
raise Exception(f"Git error: {e}")
except KeyError as e:
raise Exception(f"Configuration error: {e}")
class IdentifierResolver:
"""Handles the two-phase identifier resolution process."""
def __init__(
self,
llm: Llm,
tide: CodeTide,
smart_code_search: SmartCodeSearch,
autocomplete: AutoComplete
):
self.llm = llm
self.tide = tide
self.smart_code_search = smart_code_search
self.autocomplete = autocomplete
@staticmethod
def extract_candidate_identifiers(reasoning: str) -> List[str]:
"""Extract candidate identifiers from reasoning text using regex."""
pattern = r"^\s*-\s*(.+?)$"
matches = re.findall(pattern, reasoning, re.MULTILINE)
return [match.strip() for match in matches]
def validate_identifier(self, identifier: str) -> Optional[str]:
"""Validate and potentially correct an identifier."""
result = self.autocomplete.validate_code_identifier(identifier)
if result.get("is_valid"):
return identifier
elif result.get("matching_identifiers"):
return result.get("matching_identifiers")[0]
return None
async def gather_candidates(
self,
search_query: str,
direct_matches: Set[str],
expanded_history: list,
context_window: Set[str],
today: str
) -> Tuple[Set[str], List[str], Optional[str]]:
"""
Phase 1: Gather candidate identifiers through iterative search and expansion.
Returns:
Tuple of (candidate_pool, all_reasoning, final_search_query)
"""
candidate_pool = set()
all_reasoning = []
iteration_count = 0
previous_response = None
while iteration_count < DEFAULT_MAX_CANDIDATE_ITERATIONS:
iteration_count += 1
# Search for relevant identifiers
search_results = await self.smart_code_search.search_smart(
search_query,
use_variations=False,
top_k=DEFAULT_SEARCH_TOP_K
)
identifiers_from_search = {result[0] for result in search_results}
# Early exit if all direct matches found
# if identifiers_from_search.issubset(direct_matches):
# candidate_pool = identifiers_from_search
# print("All matches found in identifiers from search")
# break
# Build filtered tree view
candidates_to_filter = self.tide._as_file_paths(list(identifiers_from_search))
self.tide.codebase._build_tree_dict(candidates_to_filter, slim=True)
sub_tree = self.tide.codebase.get_tree_view()
# Prepare prompts
prefix_prompt = [
GATHER_CANDIDATES_PREFIX.format(
LAST_SEARCH_QUERY=search_query,
ITERATION_COUNT=iteration_count,
ACCUMULATED_CONTEXT=context_window,
DIRECT_MATCHES=direct_matches,
SEARCH_CANDIDATES=identifiers_from_search,
REPO_TREE=sub_tree
)
]
if previous_response:
prefix_prompt.insert(0, previous_response)
# Get LLM response
phase1_response = await self.llm.acomplete(
expanded_history,
system_prompt=GATHER_CANDIDATES_SYSTEM.format(
DATE=today,
SUPPORTED_LANGUAGES=SUPPORTED_LANGUAGES
),
prefix_prompt=prefix_prompt,
stream=True,
action_id=f"phase_1.{iteration_count}"
)
previous_response = phase1_response
# Parse response
reasoning_blocks = parse_blocks(phase1_response, block_word="Reasoning", multiple=True)
search_query = parse_blocks(phase1_response, block_word="Search Query", multiple=False)
# Extract candidates from reasoning
if reasoning_blocks:
all_reasoning.extend(reasoning_blocks)
for reasoning in reasoning_blocks:
candidate_matches = self.extract_candidate_identifiers(reasoning)
for match in candidate_matches:
if validated := self.validate_identifier(match):
candidate_pool.add(validated)
# Check if we have enough identifiers
if ("ENOUGH_IDENTIFIERS: TRUE" in phase1_response.upper() or
direct_matches.issubset(candidate_pool)):
break
return candidate_pool, all_reasoning, search_query
async def finalize_identifiers(
self,
candidate_pool: Set[str],
all_reasoning: List[str],
expanded_history: list,
today: str
) -> Tuple[Set[str], Set[str], Optional[str]]:
"""
Phase 2: Classify candidates into context and modify identifiers.
Returns:
Tuple of (context_identifiers, modify_identifiers, summary)
"""
all_reasoning_text = "\n\n".join(all_reasoning)
all_candidates_text = "\n".join(sorted(candidate_pool))
phase2_response = await self.llm.acomplete(
expanded_history,
system_prompt=[FINALIZE_IDENTIFIERS_PROMPT.format(
DATE=today,
SUPPORTED_LANGUAGES=SUPPORTED_LANGUAGES,
EXPLORATION_STEPS=all_reasoning_text,
ALL_CANDIDATES=all_candidates_text,
)],
stream=True,
action_id="phase2.finalize"
)
# Parse results
summary = parse_blocks(phase2_response, block_word="Summary", multiple=False)
context_identifiers = parse_blocks(
phase2_response,
block_word="Context Identifiers",
multiple=False
)
modify_identifiers = parse_blocks(
phase2_response,
block_word="Modify Identifiers",
multiple=False
)
# Process and validate identifiers
final_context = set()
final_modify = set()
if context_identifiers:
for ident in context_identifiers.strip().split('\n'):
if validated := self.validate_identifier(ident.strip()):
final_context.add(validated)
if modify_identifiers:
for ident in modify_identifiers.strip().split('\n'):
if validated := self.validate_identifier(ident.strip()):
final_modify.add(validated)
return final_context, final_modify, summary
async def resolve_identifiers(
self,
search_query: Optional[str],
direct_matches: List[str],
expanded_history: list,
context_window: Set[str],
today: str
) -> IdentifierResolutionResult:
"""
Execute the full two-phase identifier resolution process.
Args:
search_query: Initial search query (if None, uses last history item)
direct_matches: Identifiers directly matched from autocomplete
expanded_history: Conversation history to use
context_window: Set of identifiers from recent context
today: Current date string
Returns:
IdentifierResolutionResult with all resolved identifiers
"""
if search_query is None:
search_query = expanded_history[-1]
# Phase 1: Gather candidates
candidate_pool, all_reasoning, _ = await self.gather_candidates(
search_query,
set(direct_matches),
expanded_history,
context_window,
today
)
# Phase 2: Finalize classification
context_ids, modify_ids, summary = await self.finalize_identifiers(
candidate_pool,
all_reasoning,
expanded_history,
today
)
return IdentifierResolutionResult(
matches=direct_matches,
context_identifiers=list(context_ids),
modify_identifiers=self.tide._as_file_paths(list(modify_ids)),
summary=summary,
all_reasoning="\n\n".join(all_reasoning),
iteration_count=len(all_reasoning)
)
class HistoryManager:
"""Manages conversation history expansion and relevance assessment."""
def __init__(self, llm: Llm):
self.llm = llm
@staticmethod
def trim_messages(messages: list, tokenizer_fn, max_tokens: Optional[int] = None):
"""Trim messages to fit within token budget."""
max_tokens = max_tokens or int(
os.environ.get("MAX_HISTORY_TOKENS", DEFAULT_MAX_HISTORY_TOKENS)
)
while messages and sum(len(tokenizer_fn(str(msg))) for msg in messages) > max_tokens:
messages.pop(0)
async def expand_history_if_needed(
self,
history: list,
sufficient_context: bool,
initial_history_count: int,
) -> int:
"""
Iteratively expand history window if more context is needed.
Args:
history: Full conversation history
sufficient_context: Whether initial context is sufficient
initial_history_count: Starting history count
Returns:
Final history count to use
"""
current_count = max(initial_history_count, 1)
if sufficient_context:
return current_count
iteration = 0
while iteration < DEFAULT_MAX_EXPANSION_ITERATIONS and current_count < len(history):
iteration += 1
start_index = max(0, len(history) - current_count)
end_index = len(history)
current_window = history[start_index:end_index]
latest_request = history[-1]
response = await self.llm.acomplete(
current_window,
system_prompt=ASSESS_HISTORY_RELEVANCE_PROMPT.format(
START_INDEX=start_index,
END_INDEX=end_index,
TOTAL_INTERACTIONS=len(history),
CURRENT_WINDOW=str(current_window),
LATEST_REQUEST=str(latest_request)
),
stream=False,
action_id=f"expand_history.iteration_{iteration}"
)
# Extract assessment fields
history_sufficient = self._extract_boolean_field(response, "HISTORY_SUFFICIENT")
requires_more = self._extract_integer_field(response, "REQUIRES_MORE_MESSAGES")
if history_sufficient is None or requires_more is None:
raise ValueError(
f"Failed to extract relevance assessment at iteration {iteration}:\n{response}"
)
if history_sufficient:
return current_count
if requires_more > 0:
current_count = min(current_count + requires_more, len(history))
else:
current_count = len(history)
return min(current_count, len(history))
@staticmethod
def _extract_boolean_field(text: str, field_name: str) -> Optional[bool]:
"""Extract a boolean field from response text."""
match = re.search(rf'{field_name}:\s*\[?(TRUE|FALSE)\]?', text)
if match:
return match.group(1).upper() == "TRUE"
return None
@staticmethod
def _extract_integer_field(text: str, field_name: str) -> Optional[int]:
"""Extract an integer field from response text."""
match = re.search(rf'{field_name}:\s*\[?(\d+)\]?', text)
if match:
return int(match.group(1))
return None
# ============================================================================
# Main Agent Class
# ============================================================================
class AgentTide(BaseModel):
"""Main agent for autonomous code editing and task execution."""
llm: Llm
tide: CodeTide
history: Optional[list] = None
steps: Optional[Steps] = None
session_id: str = Field(default_factory=ulid)
changed_paths: List[str] = Field(default_factory=list)
request_human_confirmation: bool = False
context_identifiers: Optional[List[str]] = None
modify_identifiers: Optional[List[str]] = None
reasoning: Optional[str] = None
# Internal state
_skip_context_retrieval: bool = False
_last_code_identifiers: Optional[Set[str]] = set()
_last_code_context: Optional[str] = None
_has_patch: bool = False
_direct_mode: bool = False
_smart_code_search: Optional[SmartCodeSearch] = None
_context_identifier_window: Optional[list] = None
_git_operations: Optional[GitOperations] = None
_history_manager: Optional[HistoryManager] = None
# Configuration
CONTEXT_WINDOW_SIZE: int = DEFAULT_CONTEXT_WINDOW_SIZE
OPERATIONS: Dict[str, str] = {
OPERATION_MODE_PLAN_STEPS: STEPS_SYSTEM_PROMPT,
OPERATION_MODE_PATCH_CODE: WRITE_PATCH_SYSTEM_PROMPT
}
model_config = ConfigDict(arbitrary_types_allowed=True)
@model_validator(mode="after")
def initialize_components(self) -> Self:
"""Initialize helper components and configure logging."""
self.llm.logger_fn = partial(
custom_logger_fn,
session_id=self.session_id,
filepath=self.patch_path
)
self._git_operations = GitOperations(self.tide.repo, self.tide.rootpath)
self._history_manager = HistoryManager(self.llm)
return self
@property
def patch_path(self) -> Path:
"""Get the path for storing patches."""
storage_dir = self.tide.rootpath / DEFAULT_STORAGE_PATH
storage_dir.mkdir(exist_ok=True)
return storage_dir / f"{self.session_id}.bash"
# ========================================================================
# Patch Management
# ========================================================================
def approve(self):
"""Approve and apply the current patch."""
self._has_patch = False
if not os.path.exists(self.patch_path):
return
changed_paths = process_patch(
self.patch_path,
open_file,
write_file,
remove_file,
file_exists,
root_path=self.tide.rootpath
)
self.changed_paths.extend(changed_paths)
# Clean up patch blocks from history
self._remove_patch_blocks_from_history()
def reject(self, feedback: str):
"""Reject the current patch with feedback."""
self._has_patch = False
self.history.append(REJECT_PATCH_FEEDBACK_TEMPLATE.format(FEEDBACK=feedback))
def _remove_patch_blocks_from_history(self):
"""Remove patch blocks from the last response in history."""
if not self.history:
return
previous_response = self.history[-1]
diff_patches = parse_patch_blocks(previous_response, multiple=True)
if diff_patches:
for patch in diff_patches:
previous_response = previous_response.replace(
f"*** Begin Patch\n{patch}*** End Patch",
""
)
self.history[-1] = previous_response
# ========================================================================
# History Management
# ========================================================================
def _clean_history(self):
"""Convert history messages to plain strings."""
for i, message in enumerate(self.history):
if isinstance(message, dict):
self.history[i] = message.get("content", "")
def _filter_command_prompts_from_history(self, history: list) -> str:
"""Remove command prompts from history string."""
history_str = "\n\n".join(history)
for cmd_prompt in COMMAND_PROMPTS:
history_str = history_str.replace(cmd_prompt, "")
return history_str
# ========================================================================
# Operation Mode and Context Extraction
# ========================================================================
async def extract_operation_mode(
self,
cached_identifiers: Set[str]
) -> OperationModeResult:
"""
Extract operation mode, context sufficiency, and relevant history.
Returns:
OperationModeResult with all extracted information
"""
response = await self.llm.acomplete(
self.history[-3:],
system_prompt=DETERMINE_OPERATION_MODE_SYSTEM,
prefix_prompt=DETERMINE_OPERATION_MODE_PROMPT.format(
INTERACTION_COUNT=len(self.history),
CODE_IDENTIFIERS=cached_identifiers
),
stream=False,
action_id="extract_operation_mode"
)
# Extract fields from response
operation_mode = self._extract_field(response, "OPERATION_MODE", "STANDARD")
sufficient_context = self._extract_field(response, "SUFFICIENT_CONTEXT", "FALSE")
history_count = self._extract_field(response, "HISTORY_COUNT", "2")
is_new_topic = self._extract_field(response, "IS_NEW_TOPIC")
topic_title = self._extract_field(response, "TOPIC_TITLE")
search_query = self._extract_field(response, "SEARCH_QUERY")
# Validate extraction
if operation_mode is None or sufficient_context is None:
raise ValueError(f"Failed to extract required fields from response:\n{response}")
# Parse values
operation_mode = operation_mode.strip()
sufficient_context = sufficient_context.strip().upper() == "TRUE"
history_count = int(history_count) if history_count else len(self.history)
is_new_topic = is_new_topic.strip().upper() == "TRUE" if is_new_topic else False
topic_title = topic_title.strip() if topic_title and topic_title.strip().lower() != "null" else None
search_query = search_query.strip() if search_query and search_query.strip().upper() != "NO" else None
# Expand history if needed
final_history_count = await self._history_manager.expand_history_if_needed(
self.history,
sufficient_context,
min(history_count, int(history_count * 0.2) + 1)
)
expanded_history = self.history[-final_history_count:]
return OperationModeResult(
operation_mode=operation_mode,
sufficient_context=sufficient_context,
expanded_history=expanded_history,
search_query=search_query,
is_new_topic=is_new_topic,
topic_title=topic_title
)
@staticmethod
def _extract_field(text: str, field_name: str, default :Optional[str]=None) -> Optional[str]:
"""Extract a field value from response text."""
pattern = rf'{field_name}:\s*\[?([^\]]+?)\]?(?:\n|$)'
match = re.search(pattern, text)
return match.group(1) if match else default
@staticmethod
def _extract_search_query(response: str) -> Optional[str]:
"""Extract search query by removing known fields from response."""
cleaned = response
for field in ["OPERATION_MODE", "SUFFICIENT_CONTEXT", "HISTORY_COUNT"]:
cleaned = re.sub(rf'{field}:\s*\[?[^\]]+?\]?', '', cleaned)
search_query = cleaned.strip()
return search_query if search_query else None
# ========================================================================
# Context Building
# ========================================================================
async def prepare_search_infrastructure(self):
"""Initialize search components and update codebase."""
await self.tide.check_for_updates(serialize=True, include_cached_ids=True)
self._smart_code_search = SmartCodeSearch(
documents={
codefile.file_path: FILE_TEMPLATE.format(
CONTENT=codefile.raw,
FILENAME=codefile.file_path
)
for codefile in self.tide.codebase.root
}
)
await self._smart_code_search.initialize_async()
async def get_repo_tree_from_user_prompt(
self,
history: list,
include_modules: bool = False,
expand_paths: Optional[List[str]] = None
) -> str:
"""Get a tree view of the repository based on user prompt context."""
self._filter_command_prompts_from_history(history)
self.tide.codebase._build_tree_dict(expand_paths)
return self.tide.codebase.get_tree_view(
include_modules=include_modules,
include_types=True
)
def _build_code_context(
self,
code_identifiers: Optional[List[str]],
matches: Optional[List[str]] = None
) -> Optional[str]:
"""Build code context from identifiers, falling back to tree view if needed."""
if code_identifiers:
### TODO prefix this into:
# As you answer the user's questions, you can use the following context:
return self.tide.get(code_identifiers, as_string=True)
# Fallback to tree view and README
tree_view = REPO_TREE_CONTEXT_PROMPT.format(
REPO_TREE=self.tide.codebase.get_tree_view()
)
readme_files = self.tide.get(
["README.md"] + (matches or []),
as_string_list=True
)
if readme_files:
return "\n".join([
tree_view,
README_CONTEXT_PROMPT.format(README=readme_files)
])
return tree_view
# ========================================================================
# Identifier Resolution
# ========================================================================
async def resolve_identifiers_for_request(
self,
operation_result: OperationModeResult,
autocomplete: AutoComplete,
today: str
) -> Tuple[Optional[List[str]], Optional[str], Optional[str]]:
"""
Resolve code identifiers based on operation mode and context.
Returns:
Tuple of (code_identifiers, code_context, prefilled_summary)
"""
# Initialize context window if needed
if self._context_identifier_window is None:
self._context_identifier_window = []
expanded_history = operation_result.expanded_history
sufficient_context = operation_result.sufficient_context
search_query = operation_result.search_query
# Extract direct matches from last message
autocomplete_result = await autocomplete.async_extract_words_from_text(
self.history[-1] if self.history else "",
max_matches_per_word=1,
timeout=30
)
direct_matches = autocomplete_result["all_found_words"]
print(f"operation_mode={operation_result.operation_mode}")
print(f"direct_matches={direct_matches}")
print(f"search_query={search_query}")
print(f"sufficient_context={sufficient_context}")
# Case 1: Sufficient context with cached identifiers
if sufficient_context or (
direct_matches and set(direct_matches).issubset(self._last_code_identifiers)
):
await self.llm.logger_fn(REASONING_FINISHED)
return list(self._last_code_identifiers), None, None
# Case 2: Direct mode - use only exact matches
if self._direct_mode:
self.context_identifiers = None
self.modify_identifiers = self.tide._as_file_paths(direct_matches)
self._update_context_window(direct_matches)
self._direct_mode = False
await self.llm.logger_fn(REASONING_FINISHED)
return self.modify_identifiers, None, None
# Case 3: Full two-phase identifier resolution
print("Entering two-phase identifier resolution")
await self.llm.logger_fn(REASONING_STARTED)
resolver = IdentifierResolver(
self.llm,
self.tide,
self._smart_code_search,
autocomplete
)
context_window = set()
if self._context_identifier_window:
context_window = set().union(*self._context_identifier_window)
resolution_result = await resolver.resolve_identifiers(
search_query,
direct_matches,
expanded_history,
context_window,
today
)
await self.llm.logger_fn(REASONING_FINISHED)
print(json.dumps(resolution_result.dict(), indent=4))
code_identifiers = (
resolution_result.context_identifiers +
resolution_result.modify_identifiers
)
self._update_context_window(resolution_result.matches)
return code_identifiers, None, resolution_result.summary
def _update_context_window(self, new_identifiers: List[str]):
"""Update the rolling window of context identifiers."""
self._context_identifier_window.append(set(new_identifiers))
if len(self._context_identifier_window) > self.CONTEXT_WINDOW_SIZE:
self._context_identifier_window.pop(0)
# ========================================================================
# Main Agent Loop
# ========================================================================
async def agent_loop(self, code_identifiers: Optional[List[str]] = None):
"""
Main agent execution loop.
Args:
code_identifiers: Optional list of code identifiers to use directly
"""
today = date.today()
operation_mode = None
code_context = None
prefilled_summary = None
# Skip context retrieval if flagged
if self._skip_context_retrieval:
expanded_history = [self.history[-1]]
await self.llm.logger_fn(REASONING_FINISHED)
else:
# Prepare autocomplete and search infrastructure
cached_identifiers = self._last_code_identifiers.copy()
if code_identifiers:
cached_identifiers.update(code_identifiers)
autocomplete = AutoComplete(
self.tide.cached_ids,
mapped_words=self.tide.filenames_mapped
)
# Run preparation and mode extraction in parallel
operation_result, _ = await asyncio.gather(
self.extract_operation_mode(cached_identifiers),
self.prepare_search_infrastructure()
)
operation_mode = operation_result.operation_mode
expanded_history = operation_result.expanded_history
# Resolve identifiers and build context
code_identifiers, _, prefilled_summary = await self.resolve_identifiers_for_request(
operation_result,
autocomplete,
str(today)
)
# Build code context
if code_identifiers:
self._last_code_identifiers = set(code_identifiers)
code_context = self.tide.get(code_identifiers, as_string=True)
if not code_context and not operation_result.sufficient_context:
code_context = self._build_code_context(code_identifiers)
# Store context for potential reuse
self._last_code_context = code_context
await delete_file(self.patch_path)
# Build system prompt
system_prompt = [
AGENT_TIDE_SYSTEM_PROMPT.format(DATE=today),
CALMNESS_SYSTEM_PROMPT
]
if operation_mode in self.OPERATIONS:
system_prompt.insert(1, self.OPERATIONS[operation_mode])
# Build prefix prompt
prefix_prompt = None
if prefilled_summary:
prefix_prompt = [PREFIX_SUMMARY_PROMPT.format(SUMMARY=prefilled_summary)]
# Generate response
history_with_context = (
expanded_history[:-1] + [code_context] + expanded_history[-1:] if code_context else expanded_history
)
response = await self.llm.acomplete(
history_with_context,
system_prompt=system_prompt,
prefix_prompt=prefix_prompt,
action_id="agent_loop.main"
)
# Process response
await self._process_agent_response(response)
self.history.append(response)
await self.llm.logger_fn(ROUND_FINISHED)
async def _process_agent_response(self, response: str):
"""Process the agent's response for patches, commits, and steps."""
await trim_to_patch_section(self.patch_path)
# Handle patches
if not self.request_human_confirmation:
self.approve()
# Handle commits
commit_message = parse_blocks(response, multiple=False, block_word="Commit")
if commit_message:
self.commit(commit_message)
# Handle steps
steps = parse_steps_markdown(response)
if steps:
self.steps = Steps.from_steps(steps)
# Track patches for human confirmation
diff_patches = parse_patch_blocks(response, multiple=True)
if diff_patches:
if self.request_human_confirmation:
self._has_patch = True
else:
# Remove patch blocks from response to keep history clean
for patch in diff_patches:
response = response.replace(
f"*** Begin Patch\n{patch}*** End Patch",
""
)
# ========================================================================
# Git Operations
# ========================================================================
async def prepare_commit(self) -> str:
"""Stage files and prepare commit context."""