diff --git a/CHANGELOG.md b/CHANGELOG.md
index f9ae3a0f8..d68675bb1 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -3,6 +3,7 @@
## Unreleased
- Fix MCP OAuth credentials cache not invalidating when the server URL changes.
+- Add `isSubagent` condition variable for chat system instructions
## 0.112.0
diff --git a/src/eca/features/prompt.clj b/src/eca/features/prompt.clj
index 9b2ea1fd7..5a48b62fd 100644
--- a/src/eca/features/prompt.clj
+++ b/src/eca/features/prompt.clj
@@ -92,17 +92,21 @@
(str "\n\n" startup-ctx "\n\n\n"))
""))
-(defn ^:private ->base-selmer-ctx [all-tools db]
- (merge
- {:workspaceRoots (shared/workspaces-as-str db)}
- (reduce
- (fn [m tool]
- (assoc m (keyword (str "toolEnabled_" (:full-name tool))) true))
- {}
- all-tools)))
+(defn ^:private ->base-selmer-ctx
+ ([all-tools db]
+ (->base-selmer-ctx all-tools nil db))
+ ([all-tools chat-id db]
+ (merge
+ {:workspaceRoots (shared/workspaces-as-str db)
+ :isSubagent (boolean (get-in db [:chats chat-id :subagent]))}
+ (reduce
+ (fn [m tool]
+ (assoc m (keyword (str "toolEnabled_" (:full-name tool))) true))
+ {}
+ all-tools))))
(defn build-chat-instructions [refined-contexts rules skills repo-map* agent-name config chat-id all-tools db]
- (let [selmer-ctx (->base-selmer-ctx all-tools db)]
+ (let [selmer-ctx (->base-selmer-ctx all-tools chat-id db)]
(multi-str
(selmer/render (eca-chat-prompt agent-name config) selmer-ctx)
(when (seq rules)
diff --git a/test/eca/features/prompt_test.clj b/test/eca/features/prompt_test.clj
index 68ec86ca8..aa0de343e 100644
--- a/test/eca/features/prompt_test.clj
+++ b/test/eca/features/prompt_test.clj
@@ -58,3 +58,17 @@
(is (string/includes? result "some-cool-content"))
(is (string/includes? result ""))
(is (string? result)))))
+
+(deftest build-instructions-subagent-condition-test
+ (let [config {:prompts {:chat "{% if isSubagent %}SUBAGENT{% endif %}{% if not isSubagent %}MAIN{% endif %}"}}]
+ (testing "renders subagent-only content for subagent chats"
+ (let [db (assoc-in (h/db) [:chats "sub-chat" :subagent] {:name "explorer"})
+ result (prompt/build-chat-instructions [] [] [] (delay "TREE") "code" config "sub-chat" [] db)]
+ (is (string/includes? result "SUBAGENT"))
+ (is (not (string/includes? result "MAIN")))))
+
+ (testing "renders main-agent-only content for non-subagent chats"
+ (let [db (assoc-in (h/db) [:chats "main-chat"] {:id "main-chat"})
+ result (prompt/build-chat-instructions [] [] [] (delay "TREE") "code" config "main-chat" [] db)]
+ (is (string/includes? result "MAIN"))
+ (is (not (string/includes? result "SUBAGENT")))))))