Skip to content

Commit abc59a2

Browse files
authored
Merge pull request #453 from itkonen/fix/subagent-chat-id-validation
Fix subagent chat ID validation regression
2 parents b7eec37 + abffa00 commit abc59a2

3 files changed

Lines changed: 23 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
- Fix subagent spawning being rejected by reserved server-managed chat ids.
6+
57
## 0.133.1
68

79
- Support client-generated chat ids: clients may now create the `chatId` themselves and send it on the first `chat/prompt`. eca-emacs#231.

src/eca/features/chat.clj

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1246,6 +1246,12 @@
12461246

12471247
(def ^:private max-client-chat-id-length 256)
12481248

1249+
(defn ^:private server-managed-subagent-chat-id?
1250+
[db chat-id]
1251+
(and (string? chat-id)
1252+
(string/starts-with? chat-id "subagent-")
1253+
(some? (get-in db [:chats chat-id :subagent]))))
1254+
12491255
(defn validate-client-chat-id
12501256
"Validates a client-supplied chat id. Returns nil when valid, otherwise an
12511257
error message string.
@@ -1275,7 +1281,8 @@
12751281
(defn prompt
12761282
[{:keys [message agent behavior chat-id contexts variant trust] :as params} db* messenger config metrics]
12771283
(let [provided-chat-id chat-id
1278-
invalid-id-reason (when (some? provided-chat-id)
1284+
invalid-id-reason (when (and (some? provided-chat-id)
1285+
(not (server-managed-subagent-chat-id? @db* provided-chat-id)))
12791286
(validate-client-chat-id provided-chat-id))]
12801287
(if invalid-id-reason
12811288
(do (logger/warn logger-tag "Rejected chat/prompt with invalid chat-id"

test/eca/features/chat_test.clj

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1403,6 +1403,19 @@
14031403
(is (match? {:chat-id "subagent-foo" :status :error} resp))
14041404
(is (= {} (:chats (h/db))))
14051405
(is (nil? (:chat-opened (h/messages))))))
1406+
(testing "Server-managed subagent chat-id is accepted"
1407+
(h/reset-components!)
1408+
(let [chat-id "subagent-foo"]
1409+
(swap! (h/db*) assoc-in [:chats chat-id] {:id chat-id :subagent {:mode "subagent"}})
1410+
(is (= {:chat-id chat-id}
1411+
(prompt!
1412+
{:message "hi" :chat-id chat-id}
1413+
{:all-tools-mock (constantly [])
1414+
:api-mock
1415+
(fn [{:keys [on-first-response-received on-message-received]}]
1416+
(on-first-response-received {:type :text :text "ok"})
1417+
(on-message-received {:type :text :text "ok"})
1418+
(on-message-received {:type :finish}))})))))
14061419
(testing "Excessively long chat-id is rejected"
14071420
(h/reset-components!)
14081421
(let [too-long (apply str (repeat 257 "a"))

0 commit comments

Comments
 (0)