File size: 25,520 Bytes
4b03eed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
# -*- coding: utf-8 -*-
"""Unit tests for AgenticMemoryMiddleware with real Agent execution."""
import os
import shutil
import tempfile
from typing import Any, Type
from unittest.async_case import IsolatedAsyncioTestCase

from pydantic import BaseModel
from utils import AnyString, AnyValue, MockModel

from agentscope.agent import Agent
from agentscope.message import (
    HintBlock,
    Msg,
    TextBlock,
    ToolCallBlock,
    UserMsg,
)
from agentscope.middleware import AgenticMemoryMiddleware
from agentscope.model import ChatResponse, StructuredResponse
from agentscope.permission import (
    PermissionBehavior,
    PermissionContext,
    PermissionDecision,
)
from agentscope.tool import ToolBase, ToolChunk, Toolkit


class _RecordingMockModel(MockModel):
    """A ``MockModel`` that records chat and structured-output calls."""

    def __init__(self, **kwargs: Any) -> None:
        """Initialize the recording mock model.



        Args:

            **kwargs (`Any`):

                Keyword arguments forwarded to :class:`MockModel`.

        """
        kwargs.setdefault("context_size", 100_000)
        super().__init__(**kwargs)
        self.chat_messages: list[list[Msg]] = []
        self.structured_messages: list[list[Msg]] = []

    async def _call_api(

        self,

        *args: Any,

        **kwargs: Any,

    ) -> ChatResponse:
        """Record the chat messages and delegate to ``MockModel``.



        Args:

            *args (`Any`):

                Positional arguments forwarded to ``MockModel``.

            **kwargs (`Any`):

                Keyword arguments forwarded to ``MockModel``.



        Returns:

            `ChatResponse`:

                The configured mock chat response.

        """
        self.chat_messages.append(kwargs["messages"])
        return await super()._call_api(*args, **kwargs)

    async def _call_api_with_structured_output(

        self,

        model_name: str,

        messages: list[Msg],

        structured_model: Type[BaseModel] | dict,

        **kwargs: Any,

    ) -> StructuredResponse:
        """Record structured-output messages and delegate to ``MockModel``.



        Args:

            model_name (`str`):

                The model name.

            messages (`list[Msg]`):

                The structured-output prompt messages.

            structured_model (`Type[BaseModel] | dict`):

                The expected structured-output schema.

            **kwargs (`Any`):

                Extra keyword arguments forwarded to ``MockModel``.



        Returns:

            `StructuredResponse`:

                The configured mock structured response.

        """
        self.structured_messages.append(messages)
        return await super()._call_api_with_structured_output(
            model_name,
            messages,
            structured_model,
            **kwargs,
        )


class _DummyTool(ToolBase):
    """A minimal tool that forces a second Agent reasoning iteration."""

    name: str = "dummy"
    description: str = "A dummy tool for middleware tests."
    input_schema: dict[str, Any] = {"type": "object", "properties": {}}
    is_concurrency_safe: bool = True
    is_read_only: bool = True
    is_external_tool: bool = False
    is_mcp: bool = False

    async def check_permissions(

        self,

        tool_input: dict[str, Any],

        context: PermissionContext,

    ) -> PermissionDecision:
        """Allow every dummy tool call.



        Args:

            tool_input (`dict[str, Any]`):

                The tool input.

            context (`PermissionContext`):

                The permission context.



        Returns:

            `PermissionDecision`:

                The allow decision.

        """
        return PermissionDecision(
            behavior=PermissionBehavior.ALLOW,
            decision_reason="Dummy tool always allows.",
            message="Dummy tool always allows.",
        )

    async def __call__(self, **kwargs: Any) -> ToolChunk:
        """Return a fixed tool result.



        Args:

            **kwargs (`Any`):

                Ignored tool arguments.



        Returns:

            `ToolChunk`:

                The fixed tool output.

        """
        return ToolChunk(content=[TextBlock(text="tool result")])


def _text_response(text: str) -> ChatResponse:
    """Build a text-only chat response.



    Args:

        text (`str`):

            The response text.



    Returns:

        `ChatResponse`:

            A complete chat response with one text block.

    """
    return ChatResponse(content=[TextBlock(text=text)], is_last=True)


def _tool_response() -> ChatResponse:
    """Build a chat response that calls the dummy tool.



    Returns:

        `ChatResponse`:

            A complete chat response with one tool call block.

    """
    return ChatResponse(
        content=[
            ToolCallBlock(
                id="call_dummy",
                name="dummy",
                input="{}",
            ),
        ],
        is_last=True,
    )


def _structured_response(selected_files: list[str]) -> StructuredResponse:
    """Build a structured memory-selection response.



    Args:

        selected_files (`list[str]`):

            The selected memory filenames.



    Returns:

        `StructuredResponse`:

            The structured response consumed by the middleware.

    """
    return StructuredResponse(content={"selected_files": selected_files})


def _block_to_dict(block: Any) -> dict:
    """Convert a message block into a stable assertion dictionary.



    Args:

        block (`Any`):

            The message block to convert.



    Returns:

        `dict`:

            The stable block representation.

    """
    if isinstance(block, TextBlock):
        return {
            "type": "text",
            "text": block.text,
            "id": AnyString(),
        }
    if isinstance(block, HintBlock):
        return {
            "type": "hint",
            "hint": block.hint,
            "id": AnyString(),
            "source": block.source,
        }
    if isinstance(block, ToolCallBlock):
        return {
            "type": "tool_call",
            "id": AnyString(),
            "name": block.name,
            "input": block.input,
            "state": block.state,
            "suggested_rules": block.suggested_rules,
        }
    return block.model_dump()


def _message_to_dict(msg: Msg) -> dict:
    """Convert a message into a stable assertion dictionary.



    Args:

        msg (`Msg`):

            The message to convert.



    Returns:

        `dict`:

            The stable message representation.

    """
    return {
        "id": AnyString(),
        "name": msg.name,
        "role": msg.role,
        "content": [_block_to_dict(block) for block in msg.content],
        "metadata": msg.metadata,
    }


def _hint_texts(agent: Agent) -> list[str]:
    """Collect hint texts from an agent context.



    Args:

        agent (`Agent`):

            The agent whose context is inspected.



    Returns:

        `list[str]`:

            The hint texts in context order.

    """
    return [
        block.hint
        for msg in agent.state.context
        for block in msg.content
        if isinstance(block, HintBlock)
    ]


def _write_memory_file(

    memory_dir: str,

    filename: str,

    description: str,

    memory_type: str,

    body: str,

) -> None:
    """Write one Markdown memory file with frontmatter.



    Args:

        memory_dir (`str`):

            The memory directory.

        filename (`str`):

            The memory filename relative to ``memory_dir``.

        description (`str`):

            The frontmatter description.

        memory_type (`str`):

            The frontmatter memory type.

        body (`str`):

            The Markdown body.

    """
    path = os.path.join(memory_dir, filename)
    os.makedirs(os.path.dirname(path), exist_ok=True)
    with open(path, "w", encoding="utf-8") as f:
        f.write(
            "---\n"
            f"name: {filename}\n"
            f"description: {description}\n"
            f"type: {memory_type}\n"
            "---\n\n"
            f"{body}\n",
        )


class AgenticMemoryMiddlewareTest(IsolatedAsyncioTestCase):
    """Agent-level tests for :class:`AgenticMemoryMiddleware`."""

    async def asyncSetUp(self) -> None:
        """Create a temporary workspace for each test."""
        self.temp_dir = tempfile.mkdtemp()

    async def asyncTearDown(self) -> None:
        """Remove the temporary workspace after each test."""
        shutil.rmtree(self.temp_dir, ignore_errors=True)

    def _make_agent(

        self,

        model: _RecordingMockModel,

        middleware: AgenticMemoryMiddleware,

        toolkit: Toolkit | None = None,

    ) -> Agent:
        """Build an Agent with the filesystem memory middleware attached.



        Args:

            model (`_RecordingMockModel`):

                The mock model used by the agent.

            middleware (`AgenticMemoryMiddleware`):

                The middleware under test.

            toolkit (`Toolkit | None`, optional):

                The toolkit for the agent. Defaults to an empty toolkit.



        Returns:

            `Agent`:

                The configured agent.

        """
        return Agent(
            name="assistant",
            system_prompt="You are helpful.",
            model=model,
            toolkit=toolkit or Toolkit(),
            middlewares=[middleware],
        )

    async def test_agent_reply_creates_layout_and_injects_memory_prompt(

        self,

    ) -> None:
        """Agent reply should create layout and inject memory instructions."""
        model = _RecordingMockModel()
        model.set_responses([_text_response("done")])
        middleware = AgenticMemoryMiddleware(workdir=self.temp_dir)
        agent = self._make_agent(model, middleware)

        reply = await agent.reply(UserMsg("user", "hello"))
        memory_dir = os.path.join(self.temp_dir, "Memory")
        system_prompt = model.chat_messages[0][0].get_text_content()

        self.assertDictEqual(
            {
                "reply": _message_to_dict(reply),
                "memory_dir_exists": os.path.isdir(memory_dir),
                "memory_md_exists": os.path.isfile(
                    os.path.join(memory_dir, "MEMORY.md"),
                ),
                "system_prompt": {
                    "has_memory_dir": memory_dir in system_prompt,
                    "has_placeholder": "{memory_dir}" in system_prompt,
                    "has_memory_header": "## MEMORY.md" in system_prompt,
                    "has_empty_memory_text": (
                        "Your MEMORY.md is currently empty" in system_prompt
                    ),
                },
            },
            {
                "reply": {
                    "id": AnyString(),
                    "name": "assistant",
                    "role": "assistant",
                    "content": [
                        {
                            "type": "text",
                            "text": "done",
                            "id": AnyString(),
                        },
                    ],
                    "metadata": {},
                },
                "memory_dir_exists": True,
                "memory_md_exists": True,
                "system_prompt": {
                    "has_memory_dir": True,
                    "has_placeholder": False,
                    "has_memory_header": True,
                    "has_empty_memory_text": True,
                },
            },
        )

    async def test_agent_reasoning_injects_selected_memory_hint(

        self,

    ) -> None:
        """Agent reasoning should inject content selected by structured

        output."""
        memory_dir = os.path.join(self.temp_dir, "Memory")
        os.makedirs(memory_dir)
        with open(
            os.path.join(memory_dir, "MEMORY.md"),
            "w",
            encoding="utf-8",
        ) as f:
            f.write("- [User profile](user_profile.md) — User profile.\n")
        _write_memory_file(
            memory_dir,
            "user_profile.md",
            "User profile details",
            "user",
            "The user prefers concise Chinese answers.",
        )

        model = _RecordingMockModel()
        model.set_structured_response(
            _structured_response(["user_profile.md"]),
        )
        model.set_responses([_tool_response(), _text_response("final answer")])
        middleware = AgenticMemoryMiddleware(workdir=self.temp_dir)
        agent = self._make_agent(
            model,
            middleware,
            toolkit=Toolkit(tools=[_DummyTool()]),
        )

        reply = await agent.reply(UserMsg("user", "what do you remember?"))
        hint_texts = _hint_texts(agent)

        self.assertDictEqual(
            {
                "reply": _message_to_dict(reply),
                "hints": [
                    {
                        "has_selected_content": (
                            "The user prefers concise Chinese answers." in hint
                        ),
                        "has_selected_path": "user_profile.md" in hint,
                    }
                    for hint in hint_texts
                ],
                "context": [
                    _message_to_dict(msg) for msg in agent.state.context
                ],
                "structured_call_count": len(model.structured_messages),
            },
            {
                "reply": {
                    "id": AnyString(),
                    "name": "assistant",
                    "role": "assistant",
                    "content": [
                        {
                            "type": "text",
                            "text": "final answer",
                            "id": AnyString(),
                        },
                    ],
                    "metadata": {},
                },
                "hints": [
                    {
                        "has_selected_content": True,
                        "has_selected_path": True,
                    },
                ],
                "context": [
                    {
                        "id": AnyString(),
                        "name": "user",
                        "role": "user",
                        "content": [
                            {
                                "type": "text",
                                "text": "what do you remember?",
                                "id": AnyString(),
                            },
                        ],
                        "metadata": {},
                    },
                    {
                        "id": AnyString(),
                        "name": "assistant",
                        "role": "assistant",
                        "content": [
                            {
                                "type": "tool_call",
                                "id": AnyString(),
                                "name": "dummy",
                                "input": "{}",
                                "state": "finished",
                                "suggested_rules": [],
                            },
                            AnyValue(),
                            {
                                "type": "hint",
                                "hint": AnyString(),
                                "id": AnyString(),
                                "source": None,
                            },
                            {
                                "type": "text",
                                "text": "final answer",
                                "id": AnyString(),
                            },
                        ],
                        "metadata": {},
                    },
                ],
                "structured_call_count": 1,
            },
        )

    async def test_agent_filters_hallucinated_memory_filenames(self) -> None:
        """Agent retrieval should ignore filenames not present in memory."""
        memory_dir = os.path.join(self.temp_dir, "Memory")
        os.makedirs(memory_dir)
        with open(
            os.path.join(memory_dir, "MEMORY.md"),
            "w",
            encoding="utf-8",
        ) as f:
            f.write("- [User profile](user_profile.md) — User profile.\n")
        _write_memory_file(
            memory_dir,
            "user_profile.md",
            "User profile details",
            "user",
            "Only this real memory should be injected.",
        )

        model = _RecordingMockModel()
        model.set_structured_response(
            _structured_response(["user_profile.md", "missing.md"]),
        )
        model.set_responses([_tool_response(), _text_response("filtered")])
        middleware = AgenticMemoryMiddleware(workdir=self.temp_dir)
        agent = self._make_agent(
            model,
            middleware,
            toolkit=Toolkit(tools=[_DummyTool()]),
        )

        await agent.reply(UserMsg("user", "recall memory"))
        hint_texts = _hint_texts(agent)

        self.assertListEqual(
            [
                {
                    "has_real_memory": (
                        "Only this real memory should be injected." in hint
                    ),
                    "has_missing_memory": "missing.md" in hint,
                }
                for hint in hint_texts
            ],
            [
                {
                    "has_real_memory": True,
                    "has_missing_memory": False,
                },
            ],
        )

    async def test_agent_does_not_inject_hint_when_no_file_selected(

        self,

    ) -> None:
        """Agent retrieval should inject no hint when selection is empty."""
        memory_dir = os.path.join(self.temp_dir, "Memory")
        os.makedirs(memory_dir)
        with open(
            os.path.join(memory_dir, "MEMORY.md"),
            "w",
            encoding="utf-8",
        ) as f:
            f.write("- [User profile](user_profile.md) — User profile.\n")
        _write_memory_file(
            memory_dir,
            "user_profile.md",
            "User profile details",
            "user",
            "This memory is available but not selected.",
        )

        model = _RecordingMockModel()
        model.set_structured_response(_structured_response([]))
        model.set_responses([_tool_response(), _text_response("no hint")])
        middleware = AgenticMemoryMiddleware(workdir=self.temp_dir)
        agent = self._make_agent(
            model,
            middleware,
            toolkit=Toolkit(tools=[_DummyTool()]),
        )

        reply = await agent.reply(UserMsg("user", "ignore memories"))

        self.assertDictEqual(
            {
                "reply": _message_to_dict(reply),
                "hints": _hint_texts(agent),
                "structured_call_count": len(model.structured_messages),
            },
            {
                "reply": {
                    "id": AnyString(),
                    "name": "assistant",
                    "role": "assistant",
                    "content": [
                        {
                            "type": "text",
                            "text": "no hint",
                            "id": AnyString(),
                        },
                    ],
                    "metadata": {},
                },
                "hints": [],
                "structured_call_count": 1,
            },
        )

    async def test_agent_does_not_retrieve_when_only_memory_index_exists(

        self,

    ) -> None:
        """Agent retrieval should skip structured output without topic

        files."""
        model = _RecordingMockModel()
        model.set_structured_response(_structured_response(["missing.md"]))
        model.set_responses([_tool_response(), _text_response("index only")])
        middleware = AgenticMemoryMiddleware(workdir=self.temp_dir)
        agent = self._make_agent(
            model,
            middleware,
            toolkit=Toolkit(tools=[_DummyTool()]),
        )

        reply = await agent.reply(UserMsg("user", "hello"))

        self.assertDictEqual(
            {
                "reply": _message_to_dict(reply),
                "hints": _hint_texts(agent),
                "structured_call_count": len(model.structured_messages),
                "memory_md_exists": os.path.isfile(
                    os.path.join(self.temp_dir, "Memory", "MEMORY.md"),
                ),
            },
            {
                "reply": {
                    "id": AnyString(),
                    "name": "assistant",
                    "role": "assistant",
                    "content": [
                        {
                            "type": "text",
                            "text": "index only",
                            "id": AnyString(),
                        },
                    ],
                    "metadata": {},
                },
                "hints": [],
                "structured_call_count": 0,
                "memory_md_exists": True,
            },
        )

    async def test_agent_system_prompt_contains_truncation_reminder(

        self,

    ) -> None:
        """Agent system prompt should contain reminder for truncated index."""
        memory_dir = os.path.join(self.temp_dir, "Memory")
        os.makedirs(memory_dir)
        with open(
            os.path.join(memory_dir, "MEMORY.md"),
            "w",
            encoding="utf-8",
        ) as f:
            f.write("0123456789" * 80)

        model = _RecordingMockModel()
        model.set_responses([_text_response("truncated")])
        middleware = AgenticMemoryMiddleware(
            workdir=self.temp_dir,
            parameters=AgenticMemoryMiddleware.Parameters(
                memory_max_tokens=10,
                retrieval_async=False,
            ),
        )
        agent = self._make_agent(model, middleware)

        await agent.reply(UserMsg("user", "hello"))
        system_prompt = model.chat_messages[0][0].get_text_content()

        self.assertDictEqual(
            {
                "has_truncated_marker": "<<<TRUNCATED>>>" in system_prompt,
                "has_offset_reminder": "Use the `Read` tool with offset"
                in system_prompt,
                "has_memory_path": os.path.join(memory_dir, "MEMORY.md")
                in system_prompt,
            },
            {
                "has_truncated_marker": True,
                "has_offset_reminder": True,
                "has_memory_path": True,
            },
        )

    async def test_agent_skips_retrieval_when_async_retrieval_disabled(

        self,

    ) -> None:
        """Agent should not run retrieval when ``retrieval_async`` is false."""
        memory_dir = os.path.join(self.temp_dir, "Memory")
        os.makedirs(memory_dir)
        with open(
            os.path.join(memory_dir, "MEMORY.md"),
            "w",
            encoding="utf-8",
        ) as f:
            f.write("- [User profile](user_profile.md) — User profile.\n")
        _write_memory_file(
            memory_dir,
            "user_profile.md",
            "User profile details",
            "user",
            "This memory should not be retrieved.",
        )

        model = _RecordingMockModel()
        model.set_structured_response(
            _structured_response(["user_profile.md"]),
        )
        model.set_responses([_tool_response(), _text_response("disabled")])
        middleware = AgenticMemoryMiddleware(
            workdir=self.temp_dir,
            parameters=AgenticMemoryMiddleware.Parameters(
                retrieval_async=False,
            ),
        )
        agent = self._make_agent(
            model,
            middleware,
            toolkit=Toolkit(tools=[_DummyTool()]),
        )

        reply = await agent.reply(UserMsg("user", "remember?"))

        self.assertDictEqual(
            {
                "reply": _message_to_dict(reply),
                "hints": _hint_texts(agent),
                "structured_call_count": len(model.structured_messages),
            },
            {
                "reply": {
                    "id": AnyString(),
                    "name": "assistant",
                    "role": "assistant",
                    "content": [
                        {
                            "type": "text",
                            "text": "disabled",
                            "id": AnyString(),
                        },
                    ],
                    "metadata": {},
                },
                "hints": [],
                "structured_call_count": 0,
            },
        )