From 08b633400d2f36730150728a9e93f849df1c369a Mon Sep 17 00:00:00 2001 From: shokollm <270575765+shokollm@users.noreply.github.com> Date: Tue, 7 Apr 2026 00:13:06 +0000 Subject: [PATCH] refactor: add create_session() and use --session instead of --continue - Add create_session() function that forks from base session using JSON session detection - cmd_delegate: fork new session from base instead of using pm_agent - cmd_start: use create_session() instead of broken before/after detection - cmd_continue: use --session instead of --continue (no need to continue existing session) - Remove pm_agent check from cmd_start (no longer needed) --- skills/kugetsu/scripts/kugetsu-session.sh | 72 ++++++++++++++--------- 1 file changed, 45 insertions(+), 27 deletions(-) diff --git a/skills/kugetsu/scripts/kugetsu-session.sh b/skills/kugetsu/scripts/kugetsu-session.sh index 16b9b96..644ec9f 100755 --- a/skills/kugetsu/scripts/kugetsu-session.sh +++ b/skills/kugetsu/scripts/kugetsu-session.sh @@ -204,18 +204,52 @@ cmd_delegate() { return fi - # No issue ref detected — delegate directly to PM agent (legacy path) - local pm_session=$(get_pm_agent_session_id) - if [ -z "$pm_session" ] || [ "$pm_session" = "null" ] || [ "$pm_session" = "None" ]; then - echo "Error: PM agent session not found. Run 'kugetsu init' first." >&2 + # No issue ref detected — fork a new session from base session + local base_session=$(get_base_session_id) + if [ -z "$base_session" ] || [ "$base_session" = "null" ]; then + echo "Error: Base session not found. Run 'kugetsu init' first." >&2 exit 1 fi mkdir -p "$LOGS_DIR" local log_file="$LOGS_DIR/delegate-$(date +%s).log" load_agent_env "pm-agent" - nohup sh -c "GITEA_TOKEN='${GITEA_TOKEN:-}' opencode run '$message' --continue --session '$pm_session'" >> "$log_file" 2>&1 & - echo "Delegated to PM agent (logged to $(basename "$log_file"))" + + local new_session=$(create_session "$base_session") + if [ -z "$new_session" ]; then + echo "Error: Failed to create session" >&2 + exit 1 + fi + + nohup sh -c "GITEA_TOKEN='${GITEA_TOKEN:-}' opencode run '$message' --session '$new_session'" >> "$log_file" 2>&1 & + echo "Delegated to new session (logged to $(basename "$log_file"))" +} + +create_session() { + local base_session="${1:-$base_session_id}" + + if [ -z "$base_session" ] || [ "$base_session" = "null" ]; then + echo "Error: base session not found. Run 'kugetsu init' first." >&2 + return 1 + fi + + local before_json=$(opencode session list --format=json 2>/dev/null) + local before_ids=$(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 run --fork --session "$base_session" "new session" 2>/dev/null + + local after_json=$(opencode session list --format=json 2>/dev/null) + local after_ids=$(echo "$after_json" | python3 -c "import sys,json; sessions=json.load(sys.stdin); print(' '.join(s['id'] for s in sessions))" 2>/dev/null || echo "") + + local new_session_id="" + for sess in $after_ids; do + if [[ ! " $before_ids " =~ " $sess " ]] && [[ "$sess" != "$base_session" ]]; then + new_session_id="$sess" + break + fi + done + + echo "$new_session_id" } build_dev_agent_message() { @@ -275,12 +309,6 @@ cmd_start() { exit 1 fi - local pm_agent_session_id=$(get_pm_agent_session_id) - if [ -z "$pm_agent_session_id" ] || [ "$pm_agent_session_id" = "null" ]; then - echo "Error: PM agent session not found. Run 'kugetsu init' first." >&2 - exit 1 - fi - if worktree_exists "$issue_ref"; then echo "Issue '$issue_ref' already has a worktree. Use 'kugetsu continue' instead." exit 1 @@ -301,22 +329,12 @@ cmd_start() { exit 1 fi - local before_sessions=$(opencode session list 2>/dev/null | grep -oP '^ses_\w+' | sort) - local before_set="|$before_sessions|" - create_worktree "$issue_ref" "$WORKTREES_DIR" - local after_sessions=$(opencode session list 2>/dev/null | grep -oP '^ses_\w+' | sort) - local new_session_id="" - while IFS= read -r sess; do - if [[ ! "$before_set" =~ \|${sess}\| ]] && [[ "$sess" != "$base_session_id" ]] && [[ "$sess" != "$pm_agent_session_id" ]]; then - new_session_id="$sess" - break - fi - done <<< "$after_sessions" + local new_session_id=$(create_session "$base_session_id") if [ -z "$new_session_id" ]; then - echo "Error: Could not find newly created session" >&2 + echo "Error: Could not create session" >&2 remove_worktree_for_issue "$issue_ref" exit 1 fi @@ -333,7 +351,7 @@ cmd_start() { load_agent_env "dev" cd "$worktree_path" - nohup sh -c "GITEA_TOKEN='${GITEA_TOKEN:-}' opencode run '$dev_message' --continue --session '$new_session_id'" >> "$LOGS_DIR/dev-$new_session_id.log" 2>&1 & + nohup sh -c "GITEA_TOKEN='${GITEA_TOKEN:-}' opencode run '$dev_message' --session '$new_session_id'" >> "$LOGS_DIR/dev-$new_session_id.log" 2>&1 & echo "Session started for '$issue_ref': $new_session_id" echo "Worktree: $worktree_path" @@ -388,9 +406,9 @@ cmd_continue() { if [ -n "$worktree_path" ] && [ -d "$worktree_path" ]; then cd "$worktree_path" - nohup sh -c "GITEA_TOKEN='${GITEA_TOKEN:-}' opencode run '$message' --continue --session '$opencode_session_id'" >> "$LOGS_DIR/dev-$opencode_session_id.log" 2>&1 & + nohup sh -c "GITEA_TOKEN='${GITEA_TOKEN:-}' opencode run '$message' --session '$opencode_session_id'" >> "$LOGS_DIR/dev-$opencode_session_id.log" 2>&1 & else - nohup sh -c "GITEA_TOKEN='${GITEA_TOKEN:-}' opencode run '$message' --continue --session '$opencode_session_id'" >> "$LOGS_DIR/dev-$opencode_session_id.log" 2>&1 & + nohup sh -c "GITEA_TOKEN='${GITEA_TOKEN:-}' opencode run '$message' --session '$opencode_session_id'" >> "$LOGS_DIR/dev-$opencode_session_id.log" 2>&1 & fi }