Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Fix approve & remember not applying to parallel tool calls already asking or pending in the same response or a subagent.

## 0.173.2

- Fix the ChatGPT (OpenAI `pro`) login failing when retried: each attempt gets its own callback server, and the browser page tells whether the login actually worked.
Expand Down
2 changes: 1 addition & 1 deletion docs/config/modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ Every chat event also has `:chat-id`, `:agent`, `:model`, `:variant`, and `:pare
| `:chat/ended` | notify | `:chat-id`, `:agent`, `:model` | — |
| `:chat/status-changed` | notify | `:status`, `:pending-approval-ids`, `:running-tool-ids`... | — |
| `:chat/usage-updated` | notify | `:usage`, `:session-tokens`, `:context-limit`, `:context-used`. After each response, and when `/context` refreshes the usage. | `:auto-compact-percentage`: where the chat compacts on its own, in percent of the context limit, shown to clients |
| `:tool/before-call` | transform | `:tool` (full name), `:server`, `:arguments`, `:approval`, `:tool-call-id` | `:arguments`, `:approval`, `:reason`, `:block`, `:stop`, `:additional-context` (added to the call's output for the model) |
| `:tool/before-call` | transform | `:tool` (full name), `:server`, `:arguments`, `:approval`, `:tool-call-id` | `:arguments`, `:approval`, `:reason`, `:block`, `:stop`, `:additional-context` (added to the call's output for the model), `:remembered-approval-covers?` (for a call that asks, a fn of a db and the arguments: whether the remembered approvals now allow it) |
| `:tool/approval` | handle | `:tool` (full name), `:server`, `:arguments`. What a call's approval would be, outside of a chat and without running it: ECA leaves out the tools whose calls it would deny whatever their arguments, and eca/skills the skills the `skill` tool's approval denies. `:eca/approval` answers from the config rules. | `:approval` (`:allow`, `:ask` or `:deny`) |
| `:tool/after-call` | transform | `:tool`, `:server`, `:arguments`, `:result`, `:error?`, `:tool-call-id`. Runs before the model, the editor and postToolCall hooks see the result. | `:result` (a result map or a string), `:additional-context` (for the model only), `:stop` (after this batch of calls) |
| `:tool/called` | transform | `:tool`, `:server`, `:arguments`, `:result`, `:error?`, `:tool-call-id`. Runs once the editor got the result, before the model sees it; postToolCall hooks run here. Not when a `:tool/after-call` handler stopped the turn. | `:result` (what the model sees of it: a result map's `:contents` or a string), `:additional-context`, `:stop` (after this batch of calls) |
Expand Down
5 changes: 4 additions & 1 deletion src/eca/db.clj
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@
:decision-reason {:code :keyword :text :string}
:approved?* :promise
:future-cleanup-complete?* :promise
;; fn of a db: whether the remembered approvals now
;; allow the call, for calls asking only by config rules
:remembered-approval-covers? :fn
:start-time :long
:future :future
:resources ::any-map
Expand Down Expand Up @@ -169,7 +172,7 @@
(:trust chat)
(recur (:parent-chat-id chat) (conj seen id)))))))

(defn ^:private root-chat-id
(defn root-chat-id
"Resolve the top-level chat of `chat-id` by walking up `:parent-chat-id`
while the parent chat exists, so a subagent resolves to the chat that
spawned it. Returns `chat-id` itself for top-level chats."
Expand Down
17 changes: 11 additions & 6 deletions src/eca/features/chat.clj
Original file line number Diff line number Diff line change
Expand Up @@ -565,11 +565,11 @@
:db* db*
:config config
:metrics metrics
:messenger messenger}]
(f.chat.tool-calls/transition-tool-call! db* chat-ctx tool-call-id :user-approve
{:reason {:code :user-choice-allow
:text "Tool call allowed by user choice"}})
(when (= "session" save)
:messenger messenger}
remember? (= "session" save)]
;; Remembered before approving, so the batch's next calls, which run once
;; this one is approved, see it.
(when remember?
;; Remembered for the chat session (the root chat, shared with its
;; subagents) by full tool name, never server-wide. #639
(let [{tool-call-name :name :keys [full-name origin arguments]}
Expand All @@ -584,7 +584,12 @@
(swap! db* db/update-remembered-approvals chat-id
update-in [full-name :remembered-command-keys] (fnil into #{}) keys'))
(swap! db* db/update-remembered-approvals chat-id
assoc-in [full-name :remember-to-approve?] true))))))))
assoc-in [full-name :remember-to-approve?] true))))
(f.chat.tool-calls/transition-tool-call! db* chat-ctx tool-call-id :user-approve
{:reason {:code :user-choice-allow
:text "Tool call allowed by user choice"}})
(when remember?
(f.chat.tool-calls/approve-remembered-waiting-calls! db* (dissoc chat-ctx :chat-id) chat-id))))))

(defn tool-call-reject
[{:keys [chat-id tool-call-id]} db* messenger config metrics]
Expand Down
59 changes: 39 additions & 20 deletions src/eca/features/chat/tool_batch.clj
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@
- :stop-reason (string, for turn stops)
- :ended-by (who stopped the turn, see `f.chat.lifecycle/ended-by`)
- :module-contexts (the modules' contexts for the call's output, `{:module :content :data}`)
- :remembered-approval-covers? (fn of a db, whether the chat session's
remembered approvals now allow the call, when it asks because of the config rules)

`chat-ctx` is the handlers' ctx: the hooks show their actions in the chat."
[{:keys [full-name arguments] tool-call-id :id} all-tools db config agent-name chat-id
Expand Down Expand Up @@ -242,7 +244,11 @@
:else
{:code :user-config-deny
:text "Tool call rejected by user config"})
nil)]
nil)
remembered-approval-covers? (when-let [covers? (and (= :ask final-decision)
(:remembered-approval-covers? event))]
(when (fn? covers?)
(fn [db] (covers? db final-arguments))))]

;; Return the decision plan
(cond-> {:decision final-decision
Expand All @@ -254,7 +260,8 @@
reason (assoc :reason reason)
stop-turn? (assoc :stop-reason (shared/not-blank (:stop event))
:ended-by (f.chat.lifecycle/ended-by event))
(seq module-contexts) (assoc :module-contexts module-contexts))))
(seq module-contexts) (assoc :module-contexts module-contexts)
remembered-approval-covers? (assoc :remembered-approval-covers? remembered-approval-covers?))))

(defn on-tools-called! [{:keys [db* config chat-id agent messenger metrics invalid-arguments-rounds*]
;; One counter per returned fn, i.e. per LLM tool loop:
Expand Down Expand Up @@ -325,7 +332,8 @@
:trust (db/resolve-trust @db* chat-id)
:full-model (:full-model chat-ctx)
:variant (:variant chat-ctx)}))
{:keys [decision tool-call-blocked-by-hook? reason stop-turn? stop-reason ended-by]
{:keys [decision tool-call-blocked-by-hook? reason stop-turn? stop-reason ended-by
remembered-approval-covers?]
before-contexts :module-contexts} decision-plan
;; updatedInput is surfaced inside the real preToolCall
;; hook finished block (see eca.modules.hooks/format-hook-output),
Expand All @@ -341,23 +349,34 @@
{:ask? ask?
:stop-batch? (and tool-call-blocked-by-hook? stop-turn?)
:run! (fn run-tool-call! []
(when-not (#{:stopping :cleanup} (:status (f.chat.tool-calls/get-tool-call-state @db* chat-id id)))
(f.chat.tool-calls/transition-tool-call! db* chat-ctx id :tool-run {:approved?* approved?*
:future-cleanup-complete?* (promise)
:name name
:server server-name
:origin origin
:arguments arguments
:manual-approval ask?
:asks-user? (:asks-user? resolved-tool)
:details details
:summary summary}))
(when-not (#{:stopping :cleanup :rejected} (:status (f.chat.tool-calls/get-tool-call-state @db* chat-id id)))
(case decision
:ask (f.chat.tool-calls/transition-tool-call! db* chat-ctx id :approval-ask {:progress-text f.chat.tool-calls/waiting-approval-progress-text})
:allow (f.chat.tool-calls/transition-tool-call! db* chat-ctx id :approval-allow {:reason reason})
:deny (f.chat.tool-calls/transition-tool-call! db* chat-ctx id :approval-deny {:reason reason})
(logger/warn logger-tag "Unknown value of approval" {:approval decision :tool-call-id id})))
;; An earlier call of the batch may have been approved & remembered
;; since the decision, covering this one.
(let [remembered? (and (= :ask decision)
(f.chat.tool-calls/remembered-approval-covers-call?
@db* {:remembered-approval-covers? remembered-approval-covers?}))
decision (if remembered? :allow decision)
reason (if remembered? f.chat.tool-calls/remembered-approval-reason reason)]
(when-not (#{:stopping :cleanup} (:status (f.chat.tool-calls/get-tool-call-state @db* chat-id id)))
(f.chat.tool-calls/transition-tool-call! db* chat-ctx id :tool-run {:approved?* approved?*
:future-cleanup-complete?* (promise)
:name name
:server server-name
:origin origin
:arguments arguments
:manual-approval (= :ask decision)
:asks-user? (:asks-user? resolved-tool)
:remembered-approval-covers? remembered-approval-covers?
:details details
:summary summary}))
(when-not (#{:stopping :cleanup :rejected} (:status (f.chat.tool-calls/get-tool-call-state @db* chat-id id)))
(case decision
;; Waiting before checking again: an approval remembered meanwhile
;; is then seen either here or by the approval of the other call.
:ask (do (f.chat.tool-calls/transition-tool-call! db* chat-ctx id :approval-ask {:progress-text f.chat.tool-calls/waiting-approval-progress-text})
(f.chat.tool-calls/approve-if-remembered! db* chat-ctx id))
:allow (f.chat.tool-calls/transition-tool-call! db* chat-ctx id :approval-allow {:reason reason})
:deny (f.chat.tool-calls/transition-tool-call! db* chat-ctx id :approval-deny {:reason reason})
(logger/warn logger-tag "Unknown value of approval" {:approval decision :tool-call-id id}))))
(if (and @approved?* (not tool-call-blocked-by-hook?))
(when-not (#{:stopping :cleanup} (:status (f.chat.tool-calls/get-tool-call-state @db* chat-id id)))
(f.chat.lifecycle/assert-chat-not-stopped! chat-ctx)
Expand Down
68 changes: 59 additions & 9 deletions src/eca/features/chat/tool_calls.clj
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,9 @@
;; :status (keyword) is initialized by the state transition machinery
;; :approved?* (promise) is initialized by the :init-approval-promise action
;; :future-cleanup-complete?* (promise) is initialized by the :init-future-cleanup-promise action
;; :arguments (map), :summary, :details, :manual-approval and the
;; tool's :asks-user? are initialized by the :init-arguments action
;; :arguments (map), :summary, :details, :manual-approval, the
;; tool's :asks-user? and :remembered-approval-covers? are initialized
;; by the :init-arguments action
;; :start-time (long) is initialized by the :set-start-time action
;; :future (future) is added by the :add-future action and removed by :remove-future
;; :resources (map) is updated by the :add-resources and :remove-resources actions,
Expand Down Expand Up @@ -432,7 +433,8 @@
(assoc-some :summary (:summary event-data)
:details (:details event-data)
:manual-approval (:manual-approval event-data)
:asks-user? (:asks-user? event-data))))
:asks-user? (:asks-user? event-data)
:remembered-approval-covers? (:remembered-approval-covers? event-data))))

:set-decision-reason
(swap! db* assoc-in [:chats (:chat-id chat-ctx) :tool-calls tool-call-id :decision-reason]
Expand Down Expand Up @@ -465,7 +467,7 @@

:remove-all-promises
(swap! db* update-in [:chats (:chat-id chat-ctx) :tool-calls tool-call-id]
dissoc :approved?* :future-cleanup-complete?*)
dissoc :approved?* :future-cleanup-complete?* :remembered-approval-covers?)

;; Logging actions
:log-rejection
Expand Down Expand Up @@ -497,8 +499,15 @@
Note: The status is updated before any actions are run.
Actions are run in the order specified."
[db* chat-ctx tool-call-id event & [event-data]]
(let [current-state (get-tool-call-state @db* (:chat-id chat-ctx) tool-call-id)
current-status (:status current-state :initial) ; Default to :initial if no state
(let [status-path [:chats (:chat-id chat-ctx) :tool-calls tool-call-id :status]
;; The transition is chosen from the status in the same swap that
;; changes it, so of two concurrent events only one leaves a status.
[old-db] (swap-vals! db* (fn [db]
(if-let [status (:status (get tool-call-state-machine
[(get-in db status-path :initial) event]))]
(assoc-in db status-path status)
db)))
current-status (get-in old-db status-path :initial) ; Default to :initial if no state
transition-key [current-status event]
{:keys [status actions finally-actions] :as transition} (get tool-call-state-machine transition-key)]

Expand All @@ -514,9 +523,6 @@
:tool-call-id tool-call-id
:valid-events valid-events}))))

;; Atomic status update
(swap! db* assoc-in [:chats (:chat-id chat-ctx) :tool-calls tool-call-id :status] status)

(try
(doseq [action actions]
(execute-action! action db* chat-ctx tool-call-id event-data))
Expand All @@ -527,6 +533,50 @@

transition))

(def remembered-approval-reason
"The reason a call allowed by a remembered approval gets, as when
`:eca/approval` allows it up front."
{:code :user-config-allow
:text "Tool call allowed by user config"})

(defn remembered-approval-covers-call?
"Whether the chat session's remembered approvals now allow the tool call with
`state`, for calls that asked only because of the config rules."
[db state]
(boolean (when-let [covers? (:remembered-approval-covers? state)]
(covers? db))))

(defn approve-if-remembered!
"Approves the tool call `tool-call-id` of `chat-ctx`'s chat when it waits for
approval and the chat session's remembered approvals now allow it. Returns
true when it approved the call."
[db* chat-ctx tool-call-id]
(let [state (get-tool-call-state @db* (:chat-id chat-ctx) tool-call-id)]
(when (and (= :waiting-approval (:status state))
(remembered-approval-covers-call? @db* state))
(try
(transition-tool-call! db* chat-ctx tool-call-id :user-approve
{:reason remembered-approval-reason})
true
(catch clojure.lang.ExceptionInfo e
;; Another event (the user's answer, a stop) changed the status first.
(when-not (= "Invalid state transition" (ex-message e))
(throw e)))))))

(defn approve-remembered-waiting-calls!
"Approves the tool calls waiting for approval in `chat-id`'s chat session (its
root chat and subagents) that the session's remembered approvals now allow.
`chat-ctx` is the context for the chats, without :chat-id."
[db* chat-ctx chat-id]
(let [db @db*
root-id (db/root-chat-id db chat-id)]
(doseq [[session-chat-id chat] (:chats db)
:when (= root-id (db/root-chat-id db session-chat-id))
[tool-call-id state] (:tool-calls chat)
:when (= :waiting-approval (:status state))]
(logger/with-chat-context session-chat-id (:parent-chat-id chat)
(approve-if-remembered! db* (assoc chat-ctx :chat-id session-chat-id) tool-call-id)))))

(defn report-tool-call-progress!
"Records the latest progress of an executing tool call and re-sends its
toolCallRunning content with it. Ignored once the call is no longer executing."
Expand Down
Loading
Loading