|
| 1 | +package agents_engine.model |
| 2 | + |
| 3 | +import agents_engine.core.MemoryBank |
| 4 | +import agents_engine.core.agent |
| 5 | +import kotlin.test.Test |
| 6 | +import kotlin.test.assertEquals |
| 7 | +import kotlin.test.assertFalse |
| 8 | +import kotlin.test.assertTrue |
| 9 | + |
| 10 | +// Tests for #856 — per-skill memory-tool opt-in. |
| 11 | +// |
| 12 | +// When ANY skill on an agent calls `useMemory()`, the agentic loop respects |
| 13 | +// that opt-in: skills that did NOT opt in must NOT receive memory_* tools in |
| 14 | +// their authorized set. When no skill opts in, the legacy default-on behavior |
| 15 | +// is preserved for single-skill agents that already work today. |
| 16 | +class MemoryToolPerSkillOptInTest { |
| 17 | + |
| 18 | + private fun snoopAuthorizedTools(captureLatest: MutableList<List<String>>): ModelClient { |
| 19 | + // Use the system message that the agentic loop builds — it lists every tool |
| 20 | + // available to the current skill. We can read it back from the captured |
| 21 | + // messages of the first chat() call. |
| 22 | + return ModelClient { msgs -> |
| 23 | + val sys = msgs.firstOrNull { it.role == "system" } |
| 24 | + val tools = sys?.content |
| 25 | + ?.lineSequence() |
| 26 | + ?.filter { it.startsWith("- ") } |
| 27 | + ?.map { it.removePrefix("- ").substringBefore(":") } |
| 28 | + ?.toList() |
| 29 | + ?: emptyList() |
| 30 | + captureLatest.add(tools) |
| 31 | + LlmResponse.Text("done") |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + fun `legacy single-skill agent with memoryBank still auto-gets memory tools (backward compat)`() { |
| 37 | + val captured = mutableListOf<List<String>>() |
| 38 | + val a = agent<String, String>("legacy") { |
| 39 | + model { ollama("test"); client = snoopAuthorizedTools(captured) } |
| 40 | + memory(MemoryBank()) |
| 41 | + skills { skill<String, String>("only", "stub") { tools() } } |
| 42 | + } |
| 43 | + a("hi") |
| 44 | + assertTrue(captured.single().contains("memory_read"), "legacy auto-inject must still work; got: ${captured.single()}") |
| 45 | + assertTrue(captured.single().contains("memory_write")) |
| 46 | + assertTrue(captured.single().contains("memory_search")) |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + fun `when one skill opts in, a non-opted-in skill on the same agent loses memory access`() { |
| 51 | + // The core security guarantee — auto-inject across ALL skills was the bug. |
| 52 | + // With two skills on one agent (one opts in, one doesn't), the non-opted |
| 53 | + // skill's authorized tool set must NOT include memory_*. |
| 54 | + // The mock client first answers the skill-router LLM call (returning a |
| 55 | + // SkillRoute pointing at "read-only"), then snoops the second call's |
| 56 | + // system prompt for the "read-only" skill's authorized tools. |
| 57 | + val captured = mutableListOf<List<String>>() |
| 58 | + var callIdx = 0 |
| 59 | + val client = ModelClient { msgs -> |
| 60 | + callIdx++ |
| 61 | + if (callIdx == 1) { |
| 62 | + // Skill router call — return a JSON SkillRoute. |
| 63 | + LlmResponse.Text("""{"skillName":"read-only","confidence":1.0,"rationale":"x"}""") |
| 64 | + } else { |
| 65 | + // Skill execution call — capture the system prompt's tool listing. |
| 66 | + val sys = msgs.firstOrNull { it.role == "system" } |
| 67 | + captured.add( |
| 68 | + sys?.content?.lineSequence() |
| 69 | + ?.filter { it.startsWith("- ") } |
| 70 | + ?.map { it.removePrefix("- ").substringBefore(":") } |
| 71 | + ?.toList() |
| 72 | + ?: emptyList(), |
| 73 | + ) |
| 74 | + LlmResponse.Text("done") |
| 75 | + } |
| 76 | + } |
| 77 | + val a = agent<String, String>("two-skill") { |
| 78 | + model { ollama("test"); this.client = client } |
| 79 | + memory(MemoryBank()) |
| 80 | + skills { |
| 81 | + skill<String, String>("read-only", "answers questions") { tools() } |
| 82 | + skill<String, String>("memo-writer", "writes notes") { tools(); useMemory() } |
| 83 | + } |
| 84 | + } |
| 85 | + a("input") |
| 86 | + val tools = captured.single() |
| 87 | + assertFalse(tools.contains("memory_read"), "non-opted-in skill must NOT receive memory_read; got: $tools") |
| 88 | + assertFalse(tools.contains("memory_write"), "non-opted-in skill must NOT receive memory_write; got: $tools") |
| 89 | + assertFalse(tools.contains("memory_search"), "non-opted-in skill must NOT receive memory_search; got: $tools") |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + fun `opted-in skill DOES receive memory tools`() { |
| 94 | + val captured = mutableListOf<List<String>>() |
| 95 | + val a = agent<String, String>("writer") { |
| 96 | + model { ollama("test"); client = snoopAuthorizedTools(captured) } |
| 97 | + memory(MemoryBank()) |
| 98 | + skills { |
| 99 | + skill<String, String>("memo-writer", "uses memory") { tools(); useMemory() } |
| 100 | + } |
| 101 | + } |
| 102 | + a("hi") |
| 103 | + val tools = captured.single() |
| 104 | + assertTrue(tools.contains("memory_read"), "opted-in skill must receive memory_read; got: $tools") |
| 105 | + assertTrue(tools.contains("memory_write")) |
| 106 | + assertTrue(tools.contains("memory_search")) |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + fun `agent without memoryBank never injects memory tools regardless of useMemory`() { |
| 111 | + val captured = mutableListOf<List<String>>() |
| 112 | + val a = agent<String, String>("no-memory") { |
| 113 | + model { ollama("test"); client = snoopAuthorizedTools(captured) } |
| 114 | + // no memory(...) call |
| 115 | + skills { |
| 116 | + skill<String, String>("s", "stub") { tools(); useMemory() } |
| 117 | + } |
| 118 | + } |
| 119 | + a("hi") |
| 120 | + val tools = captured.single() |
| 121 | + assertEquals(emptyList(), tools.filter { it.startsWith("memory_") }, "no memoryBank means no memory tools regardless of opt-in") |
| 122 | + } |
| 123 | +} |
0 commit comments