Commit aefa778
fix: escape curly braces in agent instructions to prevent context variable errors (#1394)
## Summary
Fixes #1382
When an agent's instructions contain curly braces (e.g., `{repo}`,
`{branch}`), ADK's `inject_session_state` tries to resolve them as
context variable references. Since kagent doesn't use ADK context
variables, this causes a `KeyError`.
**Root cause:** ADK's `LlmAgent.canonical_instruction()` returns
`bypass_state_injection=False` when `instruction` is a string,
triggering `inject_session_state` which uses regex `{+[^{}]*}+` to find
and resolve `{text}` patterns as state variables.
**Fix:** Wrap the instruction string in a callable
(`InstructionProvider`) before passing to the ADK `Agent` constructor.
When instruction is callable, ADK sets `bypass_state_injection=True`,
completely skipping the template processing. The `_configure_memory`
method is also updated to properly handle callable instructions when
appending memory usage instructions.
## Changes
- `python/packages/kagent-adk/src/kagent/adk/types.py`: Wrap
`self.instruction` in an `InstructionProvider` callable in `to_agent()`.
Update `_configure_memory()` to compose callables when appending memory
instructions.
-
`python/packages/kagent-adk/tests/unittests/test_instruction_escaping.py`:
7 unit tests covering curly braces, JSON-like content, nested braces,
empty instructions, and verifying the callable bypass mechanism.
## Test plan
- [x] All 7 new unit tests pass
- [x] Existing test suite unaffected (pre-existing env import issues for
other tests are unrelated)
- [ ] CI pipeline validation
Signed-off-by: opspawn <agent@opspawn.com>
---------
Signed-off-by: opspawn <agent@opspawn.com>
Signed-off-by: fl-sean03 <jmhbh@users.noreply.github.com>
Signed-off-by: Sean Florez <sean@opspawn.com>
Signed-off-by: fl-sean03 <fl-sean03@users.noreply.github.com>
Co-authored-by: fl-sean03 <jmhbh@users.noreply.github.com>
Co-authored-by: Sean Florez <sean@opspawn.com>
Co-authored-by: fl-sean03 <fl-sean03@users.noreply.github.com>1 parent fcb405d commit aefa778
File tree
2 files changed
+153
-4
lines changed- python/packages/kagent-adk
- src/kagent/adk
- tests/unittests
2 files changed
+153
-4
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
369 | 369 | | |
370 | 370 | | |
371 | 371 | | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
372 | 378 | | |
373 | 379 | | |
374 | 380 | | |
375 | 381 | | |
376 | | - | |
| 382 | + | |
377 | 383 | | |
378 | 384 | | |
379 | 385 | | |
| |||
399 | 405 | | |
400 | 406 | | |
401 | 407 | | |
402 | | - | |
403 | | - | |
404 | | - | |
| 408 | + | |
| 409 | + | |
405 | 410 | | |
406 | 411 | | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
407 | 420 | | |
408 | 421 | | |
409 | 422 | | |
| |||
Lines changed: 136 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
0 commit comments