
# x kimi until --until "确定所有的内容都符合 XXX.rule.txt 以及 YYY 要求" "请处理 lib 以下的文件， 使其满足 XXX.rule.txt，直至所有完成。"

# x kimi until "请处理 lib 以下的文件， 使其满足 XXX.rule.txt，直至所有都完成。"

___x_cmd_kimi_until(){
    local prompt=""
    local until_prompt=""
    local ncheck=1

    while [ $# -gt 0 ]; do
        case "$1" in
            -h|--help)      ___x_cmd help -m kimi until "$@" ; return 0 ;;
            --prompt)       prompt="$2";    arg:2:shift ;;
            --until)        until="$2";     arg:2:shift ;;
            --ncheck)       ncheck="$2" ;   arg:2:shfit ;;
            *)              break ;;
        esac
    done

    local IFS=" "
    [ $# -eq 0 ] || prompt="$prompt $*"

    local fp="$___X_CMD_ROOT_TMP/kimi/until/mission/$$"

    ___x_cmd ensurefp "$fp"

    local x_=""
    ___x_cmd_kimi_retry___checkprompt_ "$fp"

    if [ -z "$until" ]; then
        until_prompt="
# This is mission
${prompt}

${x_}
"
    else
        until_prompt="${until}

${x_}
"
    fi

    ___x_cmd kimi "$prompt"

    local workprompt="$prompt"

    local icheck=0
    while true; do
        icheck=0
        while true; do
            ___x_cmd kimi -C "$until_prompt" || return $?
            if [ ! -f "$fp.DONE" ]; then
                workprompt="$prompt

## This is the suggestion from the reveiewer:

$(___x_cmd_cmds cat "$fp.TODO")
"
                break
            else
                icheck=$((icheck + 1))
                if [ "$icheck" -ge "$ncheck" ]; then
                    kimi:info "Task is completed after $icheck times."
                    return 0
                fi
                ___x_cmd rmrf "$fp.DONE"
                workprompt="$prompt"
            fi
        done

        ___x_cmd kimi -C "$workprompt"
    done
}

