From 998f7a4f441aac9878ea0d106f073b07ee27bf8d Mon Sep 17 00:00:00 2001 From: shokollm <270575765+shokollm@users.noreply.github.com> Date: Tue, 7 Apr 2026 00:35:38 +0000 Subject: [PATCH] refactor: remove duplicate functions from kugetsu, use kugetsu-index.sh exclusively - Remove duplicate write_index, get_base_session_id, get_pm_agent_session_id, get_session_for_issue, set_base_in_index, set_pm_agent_in_index, add_issue_to_index, remove_issue_from_index from kugetsu - These functions are already defined in kugetsu-index.sh which is sourced earlier - The kugetsu versions were shadowing the kugetsu-index.sh ones unnecessarily - This removes code duplication and ensures consistent behavior --- skills/kugetsu/scripts/kugetsu | 91 ------------------------- skills/kugetsu/scripts/kugetsu-index.sh | 7 ++ 2 files changed, 7 insertions(+), 91 deletions(-) diff --git a/skills/kugetsu/scripts/kugetsu b/skills/kugetsu/scripts/kugetsu index 3e847d0..87fbccb 100755 --- a/skills/kugetsu/scripts/kugetsu +++ b/skills/kugetsu/scripts/kugetsu @@ -475,97 +475,6 @@ read_index() { fi } -write_index() { - local base="$1" - local pm_agent="$2" - local issues_json="$3" - local temp_file="$INDEX_FILE.tmp.$$" - printf '{"base": %s, "pm_agent": %s, "issues": %s}\n' "$base" "$pm_agent" "$issues_json" > "$temp_file" - mv "$temp_file" "$INDEX_FILE" -} - -get_base_session_id() { - local index=$(read_index) - echo "$index" | python3 -c "import sys, json; d=json.load(sys.stdin); print(d.get('base') or '')" -} - -get_pm_agent_session_id() { - local index=$(read_index) - echo "$index" | python3 -c "import sys, json; d=json.load(sys.stdin); print(d.get('pm_agent') or '')" -} - -get_session_for_issue() { - local issue_ref="$1" - local index=$(read_index) - echo "$index" | python3 -c "import sys, json; d=json.load(sys.stdin); print(d['issues'].get('$issue_ref') or '')" -} - -set_base_in_index() { - local base_session_id="$1" - local pm_agent=$(get_pm_agent_session_id) - local issues_json=$(read_index | python3 -c "import sys, json; d=json.load(sys.stdin); print(json.dumps(d['issues']))") - if [ -z "$pm_agent" ] || [ "$pm_agent" = "null" ]; then - write_index "\"$base_session_id\"" "null" "$issues_json" - else - write_index "\"$base_session_id\"" "\"$pm_agent\"" "$issues_json" - fi -} - -set_pm_agent_in_index() { - local pm_agent_session_id="$1" - local base=$(get_base_session_id) - local issues_json=$(read_index | python3 -c "import sys, json; d=json.load(sys.stdin); print(json.dumps(d['issues']))") - if [ -z "$base" ] || [ "$base" = "null" ]; then - write_index "null" "\"$pm_agent_session_id\"" "$issues_json" - else - write_index "\"$base\"" "\"$pm_agent_session_id\"" "$issues_json" - fi -} - -add_issue_to_index() { - local issue_ref="$1" - local session_file="$2" - local index=$(read_index) - local base=$(get_base_session_id) - local pm_agent=$(get_pm_agent_session_id) - local issues=$(echo "$index" | python3 -c "import sys, json; d=json.load(sys.stdin); print(json.dumps(d['issues']))") - local new_issues=$(echo "$issues" | python3 -c "import sys, json; d=json.load(sys.stdin); d['$issue_ref']='$session_file'; print(json.dumps(d))") - if [ -z "$base" ] || [ "$base" = "null" ]; then - if [ -z "$pm_agent" ] || [ "$pm_agent" = "null" ]; then - write_index "null" "null" "$new_issues" - else - write_index "null" "\"$pm_agent\"" "$new_issues" - fi - else - if [ -z "$pm_agent" ] || [ "$pm_agent" = "null" ]; then - write_index "\"$base\"" "null" "$new_issues" - else - write_index "\"$base\"" "\"$pm_agent\"" "$new_issues" - fi - fi -} - -remove_issue_from_index() { - local issue_ref="$1" - local index=$(read_index) - local base=$(get_base_session_id) - local pm_agent=$(get_pm_agent_session_id) - local new_issues=$(echo "$index" | python3 -c "import sys, json; d=json.load(sys.stdin); d['issues'].pop('$issue_ref', None); print(json.dumps(d['issues']))") - if [ -z "$base" ] || [ "$base" = "null" ]; then - if [ -z "$pm_agent" ] || [ "$pm_agent" = "null" ]; then - write_index "null" "null" "$new_issues" - else - write_index "null" "\"$pm_agent\"" "$new_issues" - fi - else - if [ -z "$pm_agent" ] || [ "$pm_agent" = "null" ]; then - write_index "\"$base\"" "null" "$new_issues" - else - write_index "\"$base\"" "\"$pm_agent\"" "$new_issues" - fi - fi -} - validate_issue_ref() { local issue_ref="$1" if [[ ! "$issue_ref" =~ ^[^/]+/[^/]+/[^#]+#[0-9]+$ ]]; then diff --git a/skills/kugetsu/scripts/kugetsu-index.sh b/skills/kugetsu/scripts/kugetsu-index.sh index 8eec8c2..f1e6695 100755 --- a/skills/kugetsu/scripts/kugetsu-index.sh +++ b/skills/kugetsu/scripts/kugetsu-index.sh @@ -15,6 +15,13 @@ write_index() { local issues_json="$3" local temp_file="$INDEX_FILE.tmp.$$" printf '{"base": %s, "pm_agent": %s, "issues": %s}\n' "$base" "$pm_agent" "$issues_json" > "$temp_file" + + if ! python3 -c "import json; json.load(open('$temp_file'))" 2>/dev/null; then + echo "Error: write_index would create malformed JSON, aborting. base=$base, pm_agent=$pm_agent, issues_json=$issues_json" >&2 + rm -f "$temp_file" + return 1 + fi + mv "$temp_file" "$INDEX_FILE" }