-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentrypoint.py
More file actions
212 lines (177 loc) · 7.71 KB
/
entrypoint.py
File metadata and controls
212 lines (177 loc) · 7.71 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
#!/usr/bin/env python3
"""Container entrypoint — reads WORKFLOW.md, instantiates adapters, runs session.
No hardcoded adapter imports. Adapter selection is driven by WORKFLOW.md.
"""
import json
import logging
import os
import subprocess
import sys
from pathlib import Path
from adapters.notifiers.composite import CompositeNotifier
from adapters.trackers.static import StaticTracker
from core.config import (
load_workflow, create_agent, create_tracker,
create_workspace_mgr, create_notifiers,
OverflowConfig,
)
from core.constants import MERGE_NEEDED_FILENAME
from core.prompts import render_template, build_initial_prompt
from core.protocols import Workspace
from core.state import StateManager
from core.session import SessionRunner
from core.search import search_related_issues
logging.basicConfig(level=logging.INFO,
format="%(asctime)s [%(name)s] %(levelname)s: %(message)s")
log = logging.getLogger("entrypoint")
def _current_branch(repo_dir: str) -> str:
try:
return subprocess.check_output(
["git", "branch", "--show-current"],
cwd=repo_dir, stderr=subprocess.DEVNULL,
).decode().strip()
except (subprocess.CalledProcessError, FileNotFoundError):
return "unknown"
def _read_diff() -> str:
"""Read pre-generated diff from session dir (created by host-side _prepare_review_session)."""
diff_path = Path("/session/diff.patch")
if diff_path.exists():
return diff_path.read_text().strip()
return "N/A"
def _read_merge_instructions(session_dir: str) -> str | None:
"""Read and consume merge-needed.txt if it exists.
Returns merge instructions string, or None if no merge is needed.
The file is deleted after reading so it is only consumed once.
"""
merge_path = Path(session_dir) / MERGE_NEEDED_FILENAME
if not merge_path.exists():
return None
content = merge_path.read_text().strip()
merge_path.unlink()
log.info("Consumed %s — agent will be instructed to merge", MERGE_NEEDED_FILENAME)
# Parse the merge target from the file
lines = content.split("\n")
base_branch = ""
for line in lines:
if line.startswith("base_branch:"):
base_branch = line.split(":", 1)[1].strip()
break
conflict_details = ""
sep_idx = content.find("---\n")
if sep_idx != -1:
conflict_details = content[sep_idx + 4:].strip()
return (
f"MERGE NEEDED: The base branch ({base_branch}) has advanced while you were "
f"working. Before continuing your task, you must merge the latest changes.\n\n"
f"Conflict details from the host-side merge attempt:\n"
f"{conflict_details}\n\n"
f"Please:\n"
f"1. Run `git fetch origin {base_branch}` to get latest changes\n"
f"2. Run `git merge origin/{base_branch}` (or `git merge {base_branch}`)\n"
f"3. Resolve any merge conflicts\n"
f"4. Commit the merge\n"
f"5. Run the test suite to confirm no regressions\n"
f"6. Then continue with your original task\n"
)
def _build_prompt(config, issue, related, workspace, state_mgr, tracker,
issue_id, resume, step):
"""Build or load the agent prompt."""
if resume and (p := state_mgr.read_resume_prompt()):
tracker.add_comment(issue_id, f"🤖 Resuming from step {state_mgr.load_state().step}...")
# Prepend merge instructions if merge-needed.txt exists
merge_instructions = _read_merge_instructions("/session")
if merge_instructions:
p = merge_instructions + "\n---\n\n" + p
return p
# On resume without a resume-prompt, still check for merge instructions
if resume:
merge_instructions = _read_merge_instructions("/session")
if merge_instructions:
tracker.add_comment(issue_id, f"🤖 Resuming with merge instructions...")
state_mgr.update_status("working")
base_prompt = ""
if config.prompt_template:
base_prompt = render_template(
config.prompt_template, issue=issue,
related_context=related, attempt=None,
agent_kind=config.agent.kind,
)
else:
base_prompt = build_initial_prompt(issue.title, issue.body, related)
return merge_instructions + "\n---\n\n" + base_prompt
tracker.add_label(issue_id, "agent-in-progress")
tracker.add_comment(issue_id, f"🤖 Starting on {issue.identifier}")
state_mgr.update_status("working")
if config.prompt_template:
extra_vars = {"agent_kind": config.agent.kind}
if step == "review":
extra_vars["diff"] = _read_diff()
extra_vars["base_branch"] = os.environ.get("BASE_BRANCH", "master")
extra_vars["agent_branch"] = workspace.branch
return render_template(
config.prompt_template, issue=issue,
related_context=related, attempt=None,
**extra_vars,
)
return build_initial_prompt(issue.title, issue.body, related)
def _create_adapters(config, overflow: OverflowConfig | None = None):
"""Instantiate all adapters from config.
Args:
config: The workflow configuration
overflow: Optional overflow config. If provided, use overflow.agent_kind
for the agent (for overflow mode).
"""
tracker = StaticTracker(session_dir="/session")
agent = create_agent(config, overflow)
workspace_mgr = create_workspace_mgr(config, repo_root=Path("/workspace"))
workspace = Workspace(
path=Path("/workspace"),
branch=_current_branch("/workspace"),
is_new=False,
)
state_mgr = StateManager("/session")
notifiers = create_notifiers(config, tracker=tracker)
notifier = CompositeNotifier(notifiers)
return tracker, agent, workspace_mgr, workspace, state_mgr, notifier
def main():
issue_id = os.environ["ISSUE_ID"]
resume = os.environ.get("RESUME") == "--resume"
step = os.environ.get("STEP", "")
config = load_workflow(Path("/workspace/WORKFLOW.md"))
# Extend agent extra_args with overflow args injected by host
overflow_args_raw = os.environ.get("OVERFLOW_EXTRA_ARGS", "")
if overflow_args_raw:
try:
overflow_args = json.loads(overflow_args_raw)
config.agent.extra_args = list(overflow_args) + config.agent.extra_args
except json.JSONDecodeError as e:
log.error(f"Failed to parse OVERFLOW_EXTRA_ARGS: {e}")
max_turns = int(os.environ.get("MAX_TURNS", config.agent.max_turns))
# Check if we're in overflow mode (OVERFLOW_ACTIVE set by host)
overflow_active = os.environ.get("OVERFLOW_ACTIVE") == "1"
overflow_config = config.overflow if overflow_active else None
tracker, agent, workspace_mgr, workspace, state_mgr, notifier = _create_adapters(config, overflow_config)
notifier.start()
issue = tracker.get_issue(issue_id)
if not issue:
log.error(f"Issue {issue_id} not found")
sys.exit(1)
related = search_related_issues(issue, tracker.list_issues(), tracker)
prompt = _build_prompt(config, issue, related, workspace, state_mgr,
tracker, issue_id, resume, step)
runner = SessionRunner(
agent=agent, tracker=tracker, notifier=notifier,
workspace_mgr=workspace_mgr, state_mgr=state_mgr,
issue=issue, prompt=prompt, max_turns=max_turns,
terminal_statuses=tuple(config.terminal_statuses),
merge_config=config.merge,
hooks_config=config.hooks,
workspace_config=config.workspace,
is_review=(step == "review"),
)
try:
runner.run(workspace=workspace)
finally:
notifier.stop()
if __name__ == "__main__":
main()