Compare commits
1 Commits
fix/issue-
...
3a0983c28a
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3a0983c28a |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -4,4 +4,3 @@ results/
|
|||||||
*/results/
|
*/results/
|
||||||
*.pyc
|
*.pyc
|
||||||
|
|
||||||
.kugetsu/
|
|
||||||
|
|||||||
@@ -139,77 +139,6 @@ validate_issue_ref() {
|
|||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
read_json_file() {
|
|
||||||
local file_path="$1"
|
|
||||||
if [ -f "$file_path" ]; then
|
|
||||||
cat "$file_path"
|
|
||||||
else
|
|
||||||
echo "{}"
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
write_json_file() {
|
|
||||||
local file_path="$1"
|
|
||||||
local json_content="$2"
|
|
||||||
local temp_file="$file_path.tmp.$$"
|
|
||||||
|
|
||||||
printf '%s' "$json_content" > "$temp_file"
|
|
||||||
|
|
||||||
if ! python3 -c "import json; json.load(open('$temp_file'))" 2>/dev/null; then
|
|
||||||
echo "Error: write_json_file would create malformed JSON: $file_path" >&2
|
|
||||||
rm -f "$temp_file"
|
|
||||||
return 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
mv "$temp_file" "$file_path"
|
|
||||||
}
|
|
||||||
|
|
||||||
get_json_value() {
|
|
||||||
local file_path="$1"
|
|
||||||
local key="$2"
|
|
||||||
local default="${3:-}"
|
|
||||||
|
|
||||||
if [ ! -f "$file_path" ]; then
|
|
||||||
echo "$default"
|
|
||||||
return
|
|
||||||
fi
|
|
||||||
|
|
||||||
python3 -c "import json; print(json.load(open('$file_path')).get('$key', '$default'))" 2>/dev/null || echo "$default"
|
|
||||||
}
|
|
||||||
|
|
||||||
set_json_value() {
|
|
||||||
local file_path="$1"
|
|
||||||
local key="$2"
|
|
||||||
local value="$3"
|
|
||||||
|
|
||||||
if [ ! -f "$file_path" ]; then
|
|
||||||
printf '{"%s": "%s"}\n' "$key" "$value" > "$file_path"
|
|
||||||
return
|
|
||||||
fi
|
|
||||||
|
|
||||||
python3 << PYEOF
|
|
||||||
import json
|
|
||||||
import sys
|
|
||||||
|
|
||||||
file_path = "$file_path"
|
|
||||||
key = "$key"
|
|
||||||
value = "$value"
|
|
||||||
|
|
||||||
try:
|
|
||||||
with open(file_path, 'r') as f:
|
|
||||||
data = json.load(f)
|
|
||||||
except:
|
|
||||||
data = {}
|
|
||||||
|
|
||||||
data[key] = value
|
|
||||||
|
|
||||||
with open(file_path, 'w') as f:
|
|
||||||
json.dump(data, f, indent=2)
|
|
||||||
|
|
||||||
print(f"Set $key = $value in $file_path")
|
|
||||||
PYEOF
|
|
||||||
}
|
|
||||||
|
|
||||||
update_session_pr_url() {
|
update_session_pr_url() {
|
||||||
local issue_ref="$1"
|
local issue_ref="$1"
|
||||||
local pr_url="$2"
|
local pr_url="$2"
|
||||||
|
|||||||
@@ -34,8 +34,6 @@ release_lock() {
|
|||||||
check_task_completion() {
|
check_task_completion() {
|
||||||
local item="$1"
|
local item="$1"
|
||||||
local queue_id=$(basename "$item" .json)
|
local queue_id=$(basename "$item" .json)
|
||||||
|
|
||||||
local item_data=$(read_json_file "$item")
|
|
||||||
local state=$(python3 -c "import json; print(json.load(open('$item')).get('state', ''))" 2>/dev/null)
|
local state=$(python3 -c "import json; print(json.load(open('$item')).get('state', ''))" 2>/dev/null)
|
||||||
|
|
||||||
[ "$state" = "notified" ] || return 0
|
[ "$state" = "notified" ] || return 0
|
||||||
@@ -43,31 +41,6 @@ check_task_completion() {
|
|||||||
local session_id=$(python3 -c "import json; print(json.load(open('$item')).get('opencode_session_id', ''))" 2>/dev/null)
|
local session_id=$(python3 -c "import json; print(json.load(open('$item')).get('opencode_session_id', ''))" 2>/dev/null)
|
||||||
local issue_ref=$(python3 -c "import json; print(json.load(open('$item')).get('issue_ref', ''))" 2>/dev/null)
|
local issue_ref=$(python3 -c "import json; print(json.load(open('$item')).get('issue_ref', ''))" 2>/dev/null)
|
||||||
local pid=$(python3 -c "import json; print(json.load(open('$item')).get('pid', ''))" 2>/dev/null)
|
local pid=$(python3 -c "import json; print(json.load(open('$item')).get('pid', ''))" 2>/dev/null)
|
||||||
local notified_at=$(python3 -c "import json; print(json.load(open('$item')).get('notified_at', ''))" 2>/dev/null)
|
|
||||||
|
|
||||||
local timed_out=false
|
|
||||||
if [ -n "$notified_at" ]; then
|
|
||||||
local notified_epoch=$(date -d "$notified_at" +%s 2>/dev/null || echo "0")
|
|
||||||
local now_epoch=$(date +%s)
|
|
||||||
local hours_elapsed=$(( (now_epoch - notified_epoch) / 3600 ))
|
|
||||||
if [ "$hours_elapsed" -ge "${TASK_TIMEOUT_HOURS:-1}" ]; then
|
|
||||||
timed_out=true
|
|
||||||
log_warn "queue-daemon" "Task $queue_id ($issue_ref) timed out after ${hours_elapsed}h"
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ "$timed_out" = true ]; then
|
|
||||||
if [ -n "$pid" ] && [ "$pid" != "None" ]; then
|
|
||||||
kill "$pid" 2>/dev/null || true
|
|
||||||
fi
|
|
||||||
if [ -n "$session_id" ]; then
|
|
||||||
opencode session stop "$session_id" 2>/dev/null || true
|
|
||||||
fi
|
|
||||||
update_queue_item_state "$queue_id" "error"
|
|
||||||
log_error "queue-daemon" "Task $queue_id ($issue_ref) marked error — timeout after ${hours_elapsed}h"
|
|
||||||
release_lock "$issue_ref"
|
|
||||||
return
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ -n "$pid" ] && [ "$pid" != "None" ]; then
|
if [ -n "$pid" ] && [ "$pid" != "None" ]; then
|
||||||
if ! kill -0 "$pid" 2>/dev/null; then
|
if ! kill -0 "$pid" 2>/dev/null; then
|
||||||
|
|||||||
@@ -239,34 +239,23 @@ create_session() {
|
|||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
local before_file="$KUGETSU_DIR/sessions/before$$.json"
|
local before_json=$(opencode session list --format=json 2>/dev/null)
|
||||||
local after_file="$KUGETSU_DIR/sessions/after$$.json"
|
local before_set=$(echo "$before_json" | python3 -c "import sys,json; sessions=json.load(sys.stdin); print('|'.join(s['id'] for s in sessions))" 2>/dev/null || echo "|")
|
||||||
|
|
||||||
opencode session list --format=json > "$before_file" 2>/dev/null || printf '{}' > "$before_file"
|
|
||||||
|
|
||||||
opencode run --fork --session "$base_session" "new session" >/dev/null 2>&1
|
opencode run --fork --session "$base_session" "new session" >/dev/null 2>&1
|
||||||
|
|
||||||
sleep 1
|
sleep 1
|
||||||
|
|
||||||
opencode session list --format=json > "$after_file" 2>/dev/null || printf '{}' > "$after_file"
|
local after_json=$(opencode session list --format=json 2>/dev/null)
|
||||||
|
local after_sessions=$(echo "$after_json" | python3 -c "import sys,json; sessions=json.load(sys.stdin); [print(s['id']) for s in sessions]" 2>/dev/null || true)
|
||||||
|
|
||||||
local new_session_id=$(python3 << PYEOF
|
local new_session_id=""
|
||||||
import json
|
while IFS= read -r sess; do
|
||||||
|
if [[ -n "$sess" ]] && [[ ! "$before_set" =~ \|${sess}\| ]]; then
|
||||||
with open("$before_file", 'r') as f:
|
new_session_id="$sess"
|
||||||
before = json.load(f)
|
break
|
||||||
with open("$after_file", 'r') as f:
|
fi
|
||||||
after = json.load(f)
|
done <<< "$after_sessions"
|
||||||
|
|
||||||
before_ids = set(s['id'] for s in before)
|
|
||||||
for s in after:
|
|
||||||
if s['id'] not in before_ids:
|
|
||||||
print(s['id'])
|
|
||||||
break
|
|
||||||
PYEOF
|
|
||||||
)
|
|
||||||
|
|
||||||
rm -f "$before_file" "$after_file"
|
|
||||||
|
|
||||||
echo "$new_session_id"
|
echo "$new_session_id"
|
||||||
}
|
}
|
||||||
@@ -281,37 +270,21 @@ build_dev_agent_message() {
|
|||||||
local number=$(echo "$issue_ref" | grep -oE '#[0-9]+$' | tr -d '#')
|
local number=$(echo "$issue_ref" | grep -oE '#[0-9]+$' | tr -d '#')
|
||||||
local worktree_path=$(issue_ref_to_worktree_path "$issue_ref")
|
local worktree_path=$(issue_ref_to_worktree_path "$issue_ref")
|
||||||
|
|
||||||
local conflict_check=""
|
|
||||||
local review_notes=""
|
|
||||||
local delegator_header=""
|
|
||||||
local delegator_footer=""
|
|
||||||
if [ -n "$user_message" ]; then
|
if [ -n "$user_message" ]; then
|
||||||
conflict_check=" - CRITICAL: Check if PR has merge conflicts before asking for review:
|
cat <<EOF
|
||||||
- Use: curl -s \"https://$instance/api/v1/repos/$owner/$repo/pulls/$number\" -H \"Authorization: Bearer \$GITEA_TOKEN\"
|
|
||||||
- If \"mergeable\": false, there ARE conflicts - you MUST resolve them FIRST
|
|
||||||
- To resolve: cd to worktree, git fetch origin, git rebase origin/main, resolve conflicts, git rebase --continue, git push --force-with-lease
|
|
||||||
- Only after resolving conflicts (mergeable: true) can you ask for review"
|
|
||||||
delegator_header="IMPORTANT: Follow the workflow below as your guideline, but prioritize the delegator's message.
|
|
||||||
|
|
||||||
Workflow:"
|
|
||||||
delegator_footer="
|
|
||||||
|
|
||||||
Delegator's message:
|
|
||||||
$user_message"
|
|
||||||
else
|
|
||||||
review_notes=" - IMPORTANT: After listing reviews, READ the review comments and incorporate feedback
|
|
||||||
- Check for review state: \"APPROVED\" means ready to merge, \"COMMENT\" means feedback to address"
|
|
||||||
delegator_header="Workflow:"
|
|
||||||
fi
|
|
||||||
|
|
||||||
cat <<EOF
|
|
||||||
You are assigned to work on $issue_ref.
|
You are assigned to work on $issue_ref.
|
||||||
|
|
||||||
$delegator_header
|
IMPORTANT: Follow the workflow below as your guideline, but prioritize the delegator's message.
|
||||||
|
|
||||||
|
Workflow:
|
||||||
1. Read the issue at $instance/$owner/$repo/issues/$number AND all comments on that issue
|
1. Read the issue at $instance/$owner/$repo/issues/$number AND all comments on that issue
|
||||||
2. Check if a PR already exists for this issue
|
2. Check if a PR already exists for this issue
|
||||||
- If PR exists and is open, review it and learn from it
|
- If PR exists and is open, review it and learn from it
|
||||||
$conflict_check
|
- CRITICAL: Check if PR has merge conflicts before asking for review:
|
||||||
|
- Use: curl -s "https://$instance/api/v1/repos/$owner/$repo/pulls/$number" -H "Authorization: Bearer \$GITEA_TOKEN"
|
||||||
|
- If "mergeable": false, there ARE conflicts - you MUST resolve them FIRST
|
||||||
|
- To resolve: cd to worktree, git fetch origin, git rebase origin/main, resolve conflicts, git rebase --continue, git push --force-with-lease
|
||||||
|
- Only after resolving conflicts (mergeable: true) can you ask for review
|
||||||
- If PR makes sense to continue, work on it instead
|
- If PR makes sense to continue, work on it instead
|
||||||
- If PR is not worth continuing, create a new branch/PR but explain in PR description why you're creating a new one instead of continuing the existing PR
|
- If PR is not worth continuing, create a new branch/PR but explain in PR description why you're creating a new one instead of continuing the existing PR
|
||||||
3. Read README.md (if exists) to understand the general concept of this repository
|
3. Read README.md (if exists) to understand the general concept of this repository
|
||||||
@@ -329,14 +302,52 @@ Tools for PR interaction:
|
|||||||
- Post issue/PR comment: curl -X POST "https://$instance/api/v1/repos/$owner/$repo/issues/$number/comments" -H "Authorization: Bearer \$GITEA_TOKEN" -H "Content-Type: application/json" -d '{"body":"Your comment"}'
|
- Post issue/PR comment: curl -X POST "https://$instance/api/v1/repos/$owner/$repo/issues/$number/comments" -H "Authorization: Bearer \$GITEA_TOKEN" -H "Content-Type: application/json" -d '{"body":"Your comment"}'
|
||||||
- List PR comments: curl -s "https://$instance/api/v1/repos/$owner/$repo/issues/$number/comments" -H "Authorization: Bearer \$GITEA_TOKEN"
|
- List PR comments: curl -s "https://$instance/api/v1/repos/$owner/$repo/issues/$number/comments" -H "Authorization: Bearer \$GITEA_TOKEN"
|
||||||
- List PR reviews: curl -s "https://$instance/api/v1/repos/$owner/$repo/pulls/$number/reviews" -H "Authorization: Bearer \$GITEA_TOKEN"
|
- List PR reviews: curl -s "https://$instance/api/v1/repos/$owner/$repo/pulls/$number/reviews" -H "Authorization: Bearer \$GITEA_TOKEN"
|
||||||
$review_notes
|
|
||||||
- Merge PR (only with approval): tea pr merge --repo $owner/$repo $number --style merge
|
- Merge PR (only with approval): tea pr merge --repo $owner/$repo $number --style merge
|
||||||
- MERGING requires approval first! Check for: approval in reviews, OR "lgtm"/"approved" in comments
|
- MERGING requires approval first! Check for: approval in reviews, OR "lgtm"/"approved" in comments
|
||||||
- If no approval, ask reviewer to approve first before merging
|
- If no approval, ask reviewer to approve first before merging
|
||||||
$delegator_footer
|
|
||||||
|
Delegator's message:
|
||||||
|
$user_message
|
||||||
|
|
||||||
Work directory: $worktree_path
|
Work directory: $worktree_path
|
||||||
EOF
|
EOF
|
||||||
|
else
|
||||||
|
cat <<EOF
|
||||||
|
You are assigned to work on $issue_ref.
|
||||||
|
|
||||||
|
Workflow:
|
||||||
|
1. Read the issue at $instance/$owner/$repo/issues/$number AND all comments on that issue
|
||||||
|
2. Check if a PR already exists for this issue
|
||||||
|
- If PR exists and is open, review it and learn from it
|
||||||
|
- CRITICAL: Check if PR has merge conflicts before asking for review:
|
||||||
|
- Use: curl -s "https://$instance/api/v1/repos/$owner/$repo/pulls/$number" -H "Authorization: Bearer \$GITEA_TOKEN"
|
||||||
|
- If "mergeable": false, there ARE conflicts - you MUST resolve them FIRST
|
||||||
|
- To resolve: cd to worktree, git fetch origin, git rebase origin/main, resolve conflicts, git rebase --continue, git push --force-with-lease
|
||||||
|
- Only after resolving conflicts (mergeable: true) can you ask for review
|
||||||
|
- If PR makes sense to continue, work on it instead
|
||||||
|
- If PR is not worth continuing, create a new branch/PR but explain in PR description why you're creating a new one instead of continuing the existing PR
|
||||||
|
3. Read README.md (if exists) to understand the general concept of this repository
|
||||||
|
4. Read CONTRIBUTING.md (if exists) to understand how to contribute
|
||||||
|
- If CONTRIBUTING.md doesn't exist, follow steps 5-9 as your guideline
|
||||||
|
5. Explore the repository to understand the codebase
|
||||||
|
6. If anything is unclear, post a comment on the issue asking for clarification before implementing
|
||||||
|
7. Implement the solution
|
||||||
|
8. Create a branch named fix/issue-$number and implement the fix
|
||||||
|
9. Create a PR when the implementation is complete using: tea pr create --repo $owner/$repo --title "Your PR title" --body "PR description"
|
||||||
|
- Make sure you are logged in with: tea login add --name gitea --token \$GITEA_TOKEN --url https://$instance
|
||||||
|
- If tea is not available, use: curl -X POST "https://$instance/api/v1/repos/$owner/$repo/pulls" -H "Authorization: Bearer \$GITEA_TOKEN" -H "Content-Type: application/json" -d '{"title":"PR Title","head":"branch-name","base":"main","body":"PR description"}'
|
||||||
|
|
||||||
|
Tools for PR interaction:
|
||||||
|
- Post issue/PR comment: curl -X POST "https://$instance/api/v1/repos/$owner/$repo/issues/$number/comments" -H "Authorization: Bearer \$GITEA_TOKEN" -H "Content-Type: application/json" -d '{"body":"Your comment"}'
|
||||||
|
- List PR comments: curl -s "https://$instance/api/v1/repos/$owner/$repo/issues/$number/comments" -H "Authorization: Bearer \$GITEA_TOKEN"
|
||||||
|
- List PR reviews: curl -s "https://$instance/api/v1/repos/$owner/$repo/pulls/$number/reviews" -H "Authorization: Bearer \$GITEA_TOKEN"
|
||||||
|
- Merge PR (only with approval): tea pr merge --repo $owner/$repo $number --style merge
|
||||||
|
- MERGING requires approval first! Check for: approval in reviews, OR "lgtm"/"approved" in comments
|
||||||
|
- If no approval, ask reviewer to approve first before merging
|
||||||
|
|
||||||
|
Work directory: $worktree_path
|
||||||
|
EOF
|
||||||
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
ensure_worktree() {
|
ensure_worktree() {
|
||||||
@@ -500,8 +511,6 @@ cmd_continue() {
|
|||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
kugetsu_context_dump "$issue_ref" "$message" "$(issue_ref_to_branch_name "$issue_ref")"
|
|
||||||
|
|
||||||
local session_file=$(issue_ref_to_filename "$issue_ref")
|
local session_file=$(issue_ref_to_filename "$issue_ref")
|
||||||
local session_path="$SESSIONS_DIR/$session_file"
|
local session_path="$SESSIONS_DIR/$session_file"
|
||||||
local opencode_session_id=$(python3 -c "import json; print(json.load(open('$session_path')).get('opencode_session_id', ''))" 2>/dev/null || echo "")
|
local opencode_session_id=$(python3 -c "import json; print(json.load(open('$session_path')).get('opencode_session_id', ''))" 2>/dev/null || echo "")
|
||||||
|
|||||||
@@ -145,17 +145,15 @@ check_pr_status() {
|
|||||||
token="${GITEA_TOKEN:-}"
|
token="${GITEA_TOKEN:-}"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
local response_file="$KUGETSU_DIR/.pr_status_response_$$.json"
|
local response
|
||||||
if [ -n "$token" ]; then
|
if [ -n "$token" ]; then
|
||||||
curl -s -H "Authorization: token $token" "$api_url" > "$response_file" 2>/dev/null || printf '{}' > "$response_file"
|
response=$(curl -s -H "Authorization: token $token" "$api_url" 2>/dev/null || echo "{}")
|
||||||
else
|
else
|
||||||
curl -s "$api_url" > "$response_file" 2>/dev/null || printf '{}' > "$response_file"
|
response=$(curl -s "$api_url" 2>/dev/null || echo "{}")
|
||||||
fi
|
fi
|
||||||
|
|
||||||
local state=$(python3 -c "import json; print(json.load(open('$response_file')).get('state', 'unknown'))" 2>/dev/null || echo "unknown")
|
local state=$(echo "$response" | python3 -c "import json, sys; d=json.load(sys.stdin); print(d.get('state', 'unknown'))" 2>/dev/null || echo "unknown")
|
||||||
local merged=$(python3 -c "import json; print('true' if json.load(open('$response_file')).get('merged', False) else 'false')" 2>/dev/null || echo "false")
|
local merged=$(echo "$response" | python3 -c "import json, sys; d=json.load(sys.stdin); print('true' if d.get('merged', False) else 'false')" 2>/dev/null || echo "false")
|
||||||
|
|
||||||
rm -f "$response_file"
|
|
||||||
|
|
||||||
if [ "$merged" = "true" ]; then
|
if [ "$merged" = "true" ]; then
|
||||||
echo "merged"
|
echo "merged"
|
||||||
|
|||||||
Reference in New Issue
Block a user