feat(worktree-lifecycle): add PR tracking and safe destroy #138

Merged
shoko merged 4 commits from feat/worktree-lifecycle into main 2026-04-05 06:00:15 +02:00
Owner

Overview

Implements #135 - Worktree lifecycle management with PR check.

Changes

New Configuration

  • WORKTREE_CHECK_PR_STATUS=true (default) - Check PR status before destroy

Session File Enhancement

Session files now include pr_url and branch_name:

{
  "type": "forked",
  "issue_ref": "github.com/shoko/kugetsu#14",
  "opencode_session_id": "ses_xyz",
  "worktree_path": "~/.kugetsu/worktrees/...",
  "created_at": "2026-04-05T10:00:00+02:00",
  "state": "idle",
  "branch_name": "fix/issue-14",
  "pr_url": null
}

New Command

kugetsu update-pr <issue-ref> <pr-url>

Updates the PR URL in the session file. Dev agents should call this when they create a PR.

PR Status Check on Destroy

  • kugetsu destroy <issue> checks PR status via API
  • If PR is still open: refuses to destroy (shows error)
  • If PR is merged/closed: proceeds with destroy
  • kugetsu destroy <issue> --force bypasses the check

API Support

  • Gitea (git.fbrns.co)
  • GitHub (github.com)

Closes #135

## Overview Implements #135 - Worktree lifecycle management with PR check. ## Changes ### New Configuration - `WORKTREE_CHECK_PR_STATUS=true` (default) - Check PR status before destroy ### Session File Enhancement Session files now include `pr_url` and `branch_name`: ```json { "type": "forked", "issue_ref": "github.com/shoko/kugetsu#14", "opencode_session_id": "ses_xyz", "worktree_path": "~/.kugetsu/worktrees/...", "created_at": "2026-04-05T10:00:00+02:00", "state": "idle", "branch_name": "fix/issue-14", "pr_url": null } ``` ### New Command ```bash kugetsu update-pr <issue-ref> <pr-url> ``` Updates the PR URL in the session file. Dev agents should call this when they create a PR. ### PR Status Check on Destroy - `kugetsu destroy <issue>` checks PR status via API - If PR is still open: refuses to destroy (shows error) - If PR is merged/closed: proceeds with destroy - `kugetsu destroy <issue> --force` bypasses the check ### API Support - Gitea (git.fbrns.co) - GitHub (github.com) ## Related Issues Closes #135
shoko added 1 commit 2026-04-05 04:50:42 +02:00
- Add WORKTREE_CHECK_PR_STATUS config (default: true)
- Add pr_url and branch_name fields to session files
- Add check_pr_status() to query PR status via API (Gitea/GitHub)
- Add update_session_pr_url() to update PR URL in session
- Add kugetsu update-pr command to set PR URL
- Modify cmd_destroy to check PR status before destroying worktree

Closes #135
First-time contributor

Updates the PR URL in the session file. Dev agents should call this when they create a PR.

why do we have to create a specific command to create a PR? what if the PR already exist? should we handle it as well? what if we need it for different instance, how can user update it without changing the script?

> Updates the PR URL in the session file. Dev agents should call this when they create a PR. why do we have to create a specific command to create a PR? what if the PR already exist? should we handle it as well? what if we need it for different instance, how can user update it without changing the script?
han requested changes 2026-04-05 05:13:36 +02:00
Dismissed
han left a comment
First-time contributor

please adjust

please adjust
@@ -13,6 +13,7 @@ VERBOSITY_DIR="$KUGETSU_DIR/verbosity"
MAX_CONCURRENT_AGENTS="${MAX_CONCURRENT_AGENTS:-3}"
KUGETSU_VERBOSITY="${KUGETSU_VERBOSITY:-default}"
WORKTREE_CHECK_PR_STATUS="${WORKTREE_CHECK_PR_STATUS:-true}"
First-time contributor

what is the default value of this?

what is the default value of this?
@@ -78,2 +79,3 @@
kugetsu destroy --pm-agent [-y] Delete pm-agent session (not recommended)
kugetsu destroy --base [-y] Delete base session
kugetsu destroy --base [-y] Delete base session
kugetsu update-pr <issue-ref> <pr-url> Update PR URL for session (for PR tracking)
First-time contributor

do we really have this update-pr command?

do we really have this update-pr command?
@@ -259,0 +269,4 @@
local api_url=""
local token=""
if [[ "$pr_url" =~ git\.fbrns\.co ]]; then
First-time contributor

I don't want this to be in the code. this should be a custom configurable. don't we have repos.json or anything? or servers.json? I forget. the place where we configure which instances are we using (github, gitlab, selfhosted gitea, etc)

I don't want this to be in the code. this should be a custom configurable. don't we have repos.json or anything? or servers.json? I forget. the place where we configure which instances are we using (github, gitlab, selfhosted gitea, etc)
@@ -259,0 +276,4 @@
api_url=$(echo "$pr_url" | sed -E 's|https://github\.com/([^/]+)/([^/]+)/pulls/([0-9]+)|https://api.github.com/repos/\1/\2/pulls/\3|')
token="${GITHUB_TOKEN:-}"
else
echo "unknown_server"
First-time contributor

this is totally killing the feature in a way that the git instance should be registered here wont work in the open internet

this is totally killing the feature in a way that the git instance should be registered here wont work in the open internet
@@ -259,0 +287,4 @@
response=$(curl -s "$api_url" 2>/dev/null || echo "{}")
fi
if [[ "$pr_url" =~ git\.fbrns\.co ]]; then
First-time contributor

same with this, I dont want to be hardcoded here the domain. it should be configurable in a way user could always add later, by not changing this script

same with this, I dont want to be hardcoded here the domain. it should be configurable in a way user could always add later, by not changing this script
@@ -1890,0 +2026,4 @@
local pr_url="${2:-}"
if [ -z "$issue_ref" ] || [ -z "$pr_url" ]; then
echo "Usage: kugetsu update-pr <issue-ref> <pr-url>" >&2
echo "Example: kugetsu update-pr github.com/shoko/kugetsu#14 https://git.fbrns.co/shoko/kugetsu/pulls/123" >&2
First-time contributor

dont put git.fbrns.co here

dont put git.fbrns.co here
shoko added 1 commit 2026-04-05 05:16:14 +02:00
- Rename update-pr to set-pr for clarity (it's setting the PR URL, not updating PR)
- Add optional pr-url argument to kugetsu start command
  Usage: kugetsu start <issue-ref> <message> [pr-url]
- If pr-url is provided at start, it's stored directly in session file
shoko added 1 commit 2026-04-05 05:41:50 +02:00
- Extract hostname from pr_url instead of hardcoding domains
- Look up server base URL from GIT_SERVERS config
- Append /api/v1 to derive API URL (configurable per server)
- Works with any server configured in GIT_SERVERS
han reviewed 2026-04-05 05:51:24 +02:00
@@ -1890,0 +2022,4 @@
local pr_url="${2:-}"
if [ -z "$issue_ref" ] || [ -z "$pr_url" ]; then
echo "Usage: kugetsu set-pr <issue-ref> <pr-url>" >&2
echo "Example: kugetsu set-pr github.com/shoko/kugetsu#14 https://git.fbrns.co/shoko/kugetsu/pulls/123" >&2
First-time contributor

remove git.fbrns.co here. just use github as example

remove git.fbrns.co here. just use github as example
shoko added 1 commit 2026-04-05 05:52:43 +02:00
shoko force-pushed feat/worktree-lifecycle from ce4116bcb1 to ac850869fd 2026-04-05 05:57:07 +02:00 Compare
han approved these changes 2026-04-05 05:59:40 +02:00
han left a comment
First-time contributor

lgtm

lgtm
shoko merged commit b76a9b883a into main 2026-04-05 06:00:15 +02:00
Sign in to join this conversation.