Skip to content
This repository has been archived by the owner on Jan 7, 2025. It is now read-only.

Commit

Permalink
Disable sampler on error (#426)
Browse files Browse the repository at this point in the history
  • Loading branch information
granawkins authored Jan 1, 2024
1 parent 8145c6a commit 808109e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
1 change: 1 addition & 0 deletions mentat/sampler/sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def parse_message(message: ChatCompletionMessageParam) -> dict[str, str]:


class Sampler:
active: bool = True
diff_active: str | None = None
commit_active: str | None = None
last_sample_id: str | None = None
Expand Down
5 changes: 5 additions & 0 deletions mentat/sampler/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ def get_active_snapshot_commit(repo: Repo) -> str | None:
"""Returns the commit hash of the current active snapshot, or None if there are no active changes."""
if not repo.is_dirty():
return None
if not repo.config_reader().has_option("user", "name"):
raise SampleError(
"ERROR: Git user.name not set. Please run 'git config --global user.name"
' "Your Name"\'.'
)
try:
# Stash active changes and record the current position
for file in get_non_gitignored_files(Path(repo.working_dir)):
Expand Down
20 changes: 17 additions & 3 deletions mentat/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@
from mentat.conversation import Conversation
from mentat.cost_tracker import CostTracker
from mentat.ctags import ensure_ctags_installed
from mentat.errors import ContextSizeInsufficient, MentatError, SessionExit, UserError
from mentat.errors import (
ContextSizeInsufficient,
MentatError,
SampleError,
SessionExit,
UserError,
)
from mentat.git_handler import get_git_root_for_path
from mentat.llm_api_handler import LlmApiHandler, is_test_environment
from mentat.logging_config import setup_logging
Expand Down Expand Up @@ -179,8 +185,16 @@ async def _main(self):
for file_edit in file_edits:
file_edit.resolve_conflicts()

if session_context.sampler:
session_context.sampler.set_active_diff()
if session_context.sampler and session_context.sampler.active:
try:
session_context.sampler.set_active_diff()
except SampleError as e:
stream.send(
f"Sampler error setting active diff: {e}. Disabling"
" sampler.",
color="red",
)
session_context.sampler.active = False

applied_edits = await code_file_manager.write_changes_to_files(
file_edits
Expand Down

0 comments on commit 808109e

Please sign in to comment.