refactor: make cmd_continue idempotent with ensure_* functions #168
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Summary
Simplify kugetsu by making
cmd_continuethe main entry point for session/worktree management. Extract reusable functions socmd_startbecomes a thin wrapper and the daemon can always callcmd_continuewithout existence checks.Problem
Currently
cmd_startandcmd_continuehave duplicate existence-checking logic:cmd_start- creates new worktree + sessioncmd_continue- continues existing session, errors if doesn't existThe daemon must check existence before calling either:
This is hard to maintain and
cmd_continuedoesn't handle the "auto-create" case.Solution
New Functions (in
kugetsu-session.sh)1.
ensure_worktree(issue_ref)2.
ensure_session(issue_ref)3.
fork_agent(session_id, worktree_path, message).kugetsu-msg.txtLOGS_DIR/dev-{session}.log4.
cmd_continue()refactored5.
cmd_start()becomes thin wrapperFiles to Modify
skills/kugetsu/scripts/kugetsu-session.shensure_worktree()functionensure_session()functionfork_agent()functioncmd_continue()to use the abovecmd_start()a thin wrapperskills/kugetsu/scripts/kugetsu-queue-daemon.shcmd_continue "$issue_ref" "$message"CLI Behavior Changes
kugetsu start <issue> "msg"kugetsu continue <issue> "msg"(exists)kugetsu continue <issue> "msg"(not exists)Idempotent Retry Behavior
If any operation fails mid-way, retry will succeed because:
ensure_worktree()detects existing worktree → uses itensure_session()detects missing session → creates itNo manual cleanup needed before retry.
Testing Scenarios
Fresh start:
kugetsu start github.com/user/repo#1 "new task"on issue with no worktree/sessionContinue existing:
kugetsu continue github.com/user/repo#1 "resume"on issue with existing worktree/sessionContinue on fresh issue (new behavior):
kugetsu continue github.com/user/repo#1 "msg"when nothing existsPartial failure recovery: Session creation fails, then retry succeeds
Daemon delegate: Daemon processes a pending task
Inconsistent state recovery: Worktree exists but session missing
ensure_session()detects and recreates both cleanlyDeprecation Path
cmd_startwill eventually be deprecated (not in this PR)kugetsu continuecmd_startis just a thin wrapperAcceptance Criteria
ensure_worktree()returns correct status and is idempotentensure_session()returns correct status and is idempotentfork_agent()correctly starts opencode session in backgroundcmd_startworks exactly as before (thin wrapper)kugetsu continue <issue>auto-creates if session/worktree missingenhancement: make cmd_continue idempotentto refactor: make cmd_continue idempotent with ensure_* functions