diff --git a/README.md b/README.md index 6b962255..962ca99f 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,19 @@ a new command, but cannot replace an earlier untracked reply. Replies sent through the chat adapter are tracked; messages a plugin sends directly through Discord's API are not. +## Command evaluation deadline + +Expression evaluation defaults to **five minutes**. Set a different startup +limit with `YETIBOT_COMMAND_TIMEOUT_MS`, or the equivalent EDN configuration +`{:yetibot {:command {:timeout {:ms 300000}}}}`. Values are positive integer +milliseconds, at most 2,147,483,647; missing or invalid values use five minutes. +Restart to apply a configuration change. + +This deadline does not change provider/tool timeouts, generation limits or call +budgets. A timeout does not cancel an already-running external request or prove +that its side effects did not occur; do not automatically retry it. Inherited +execution scopes close on timeout and reject subsequent derived effects. + ## Dev postgres To quickly provision a postgresql database to work against, run: diff --git a/src/yetibot/core/handler.clj b/src/yetibot/core/handler.clj index 16231123..3153358b 100644 --- a/src/yetibot/core/handler.clj +++ b/src/yetibot/core/handler.clj @@ -50,7 +50,20 @@ ;; enabled by default true))) -(def expr-eval-timeout-ms 5000) +(s/def ::expr-eval-timeout-ms + #(try + (and (or (integer? %) (string? %)) + (<= 1 (Long/parseLong (str %)) Integer/MAX_VALUE)) + (catch Exception _ false))) + +(defn configured-expr-eval-timeout-ms + "Startup command deadline in milliseconds; invalid/missing settings use five minutes." + [] + (let [{:keys [value error]} (get-config ::expr-eval-timeout-ms + [:command :timeout :ms])] + (if error 300000 (Long/parseLong (str value))))) + +(def expr-eval-timeout-ms (configured-expr-eval-timeout-ms)) (def ^:private exception-format "👮 %s 👮") diff --git a/test/yetibot/core/test/util/agent_execution.clj b/test/yetibot/core/test/util/agent_execution.clj index f22d4db1..ca1ddce5 100644 --- a/test/yetibot/core/test/util/agent_execution.clj +++ b/test/yetibot/core/test/util/agent_execution.clj @@ -1,6 +1,8 @@ (ns yetibot.core.test.util.agent-execution (:require [midje.sweet :refer [fact => throws]] [clojure.core.async :as async] + [yetibot.core.config :as config] + [yetibot.core.util.config :as config-util] [yetibot.core.util.agent-execution :as execution] [yetibot.core.parser :as parser] [yetibot.core.handler :as handler] @@ -353,6 +355,30 @@ @seen => [[family ""]] @admissions => ["agent"]))))) +(fact "command deadlines default to five minutes and validate configured millisecond values" + (doseq [[value expected] [[nil 300000] [100 100] ["600000" 600000] + [1 1] [2147483647 2147483647] + [0 300000] [-1 300000] [1.5 300000] + [true 300000] ["invalid" 300000] + ["2147483648" 300000]]] + (with-redefs [config/get-config + (partial config-util/get-config {:command {:timeout {:ms value}}})] + (handler/configured-expr-eval-timeout-ms) => expected))) + +(fact "ordinary commands longer than the former five-second limit complete within the default deadline" + (lineage-fixture + (fn [_ _ _] + (hooks/cmd-hook-resolved #"slowordinary" #".*" + (fn [_] (Thread/sleep 5100) "long-ok")) + (with-redefs [config/get-config (partial config-util/get-config {}) + handler/add-user-message-to-history (fn [& _]) + handler/add-bot-response-to-history (fn [& _])] + (with-redefs [handler/expr-eval-timeout-ms + (handler/configured-expr-eval-timeout-ms)] + (:result (first (handler/record-and-run-raw "!slowordinary" nil nil + {:record-yetibot-response? false}))) + => "long-ok"))))) + (fact "concurrent nested aliases cannot starve command evaluation on async dispatchers" (doseq [route [:raw :delivery]] (lineage-fixture