Compare commits

..

2 Commits

Author SHA1 Message Date
shokollm
6e9472b5e2 fix(kugetsu): detect session via DB query instead of opencode session list
opencode session list doesn't show sessions in ~/.kugetsu-worktrees/ directories.
This caused detection to fail even though sessions were being created.

Now we query the database directly for sessions matching the worktree path.
Also fixed database path in fix_session_permissions (was ~/.opencode/, should be ~/.local/share/opencode/).
2026-04-02 11:45:35 +00:00
shokollm
775f73348a fix(kugetsu): update forked session permissions after detection
Previously we only fixed base session permissions before forking.
But permissions are NOT inherited from parent to child.

Now we update the newly created session's permissions immediately
after detection, ensuring the forked session can access external
directories like ~/.kugetsu/worktrees/.
2026-04-02 11:15:27 +00:00

View File

@@ -876,7 +876,7 @@ cmd_doctor() {
} }
fix_session_permissions() { fix_session_permissions() {
local opencode_db="${OPENCODE_DB:-$HOME/.opencode/opencode.db}" local opencode_db="${OPENCODE_DB:-$HOME/.local/share/opencode/opencode.db}"
if [ ! -f "$opencode_db" ]; then if [ ! -f "$opencode_db" ]; then
echo "[ERROR] opencode database not found: $opencode_db" echo "[ERROR] opencode database not found: $opencode_db"
@@ -1226,12 +1226,6 @@ cmd_start() {
local session_file="$(issue_ref_to_filename "$issue_ref").json" local session_file="$(issue_ref_to_filename "$issue_ref").json"
# Get list of sessions before fork to compare against after
declare -a before_sessions=()
while IFS= read -r sess; do
before_sessions+=("$sess")
done < <(opencode session list 2>/dev/null | grep -oP '^ses_\w+')
echo "Forking session for '$issue_ref'..." echo "Forking session for '$issue_ref'..."
# Session-counting: count actual dev sessions, reject if at limit # Session-counting: count actual dev sessions, reject if at limit
@@ -1264,25 +1258,17 @@ cmd_start() {
while [ $attempt -le $max_attempts ]; do while [ $attempt -le $max_attempts ]; do
sleep 1 sleep 1
while IFS= read -r sess; do new_session_id=$(python3 -c "
[ "$sess" = "$base_session_id" ] && continue import sqlite3
[ "$sess" = "$pm_agent_session_id" ] && continue conn = sqlite3.connect('$opencode_db')
cursor = conn.cursor()
local existed_before=false cursor.execute(\"SELECT id FROM session WHERE directory = '$worktree_path' ORDER BY time_created DESC LIMIT 1\")
for before_sess in "${before_sessions[@]}"; do result = cursor.fetchone()
if [ "$sess" = "$before_sess" ]; then if result:
existed_before=true print(result[0])
break " 2>/dev/null || echo "")
fi
done
if [ "$existed_before" = false ]; then
new_session_id="$sess"
break
fi
done < <(opencode session list 2>/dev/null | grep -oP '^ses_\w+')
if [ -n "$new_session_id" ]; then if [ -n "$new_session_id" ] && [ "$new_session_id" != "$base_session_id" ] && [ "$new_session_id" != "$pm_agent_session_id" ]; then
break break
fi fi
@@ -1295,21 +1281,6 @@ cmd_start() {
done done
if [ -z "$new_session_id" ]; then if [ -z "$new_session_id" ]; then
if [ -f "$opencode_db" ]; then
local db_sessions=$(python3 -c "
import sqlite3
conn = sqlite3.connect('$opencode_db')
cursor = conn.cursor()
cursor.execute(\"SELECT id FROM session WHERE parent_id IS NOT NULL ORDER BY time_created DESC LIMIT 5\")
for row in cursor.fetchall():
print(row[0])
" 2>/dev/null || echo "")
if [ -n "$db_sessions" ]; then
echo "Recent forked sessions in DB:" >&2
echo "$db_sessions" >&2
fi
fi
echo "Error: Could not find newly created session after ${max_attempts}s" >&2 echo "Error: Could not find newly created session after ${max_attempts}s" >&2
if [ -n "$fork_log_output" ]; then if [ -n "$fork_log_output" ]; then
echo "Fork log output:" >&2 echo "Fork log output:" >&2
@@ -1319,6 +1290,17 @@ for row in cursor.fetchall():
exit 1 exit 1
fi fi
echo "Updating permissions for new session: $new_session_id"
python3 -c "
import sqlite3
conn = sqlite3.connect('$opencode_db')
cursor = conn.cursor()
PERMISSION_JSON = '[{\"permission\":\"question\",\"pattern\":\"*\",\"action\":\"deny\"},{\"permission\":\"plan_enter\",\"pattern\":\"*\",\"action\":\"deny\"},{\"permission\":\"plan_exit\",\"pattern\":\"*\",\"action\":\"deny\"},{\"permission\":\"external_directory\",\"pattern\":\"*\",\"action\":\"allow\"}]'
cursor.execute('UPDATE session SET permission = ? WHERE id = ?', (PERMISSION_JSON, '$new_session_id'))
conn.commit()
print('[OK] Session permissions updated')
"
if [ "$DEBUG_MODE" = true ]; then if [ "$DEBUG_MODE" = true ]; then
echo "[DEBUG] Forked session permissions check:" echo "[DEBUG] Forked session permissions check:"
python3 -c " python3 -c "