File size: 3,899 Bytes
2bb5af3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""CrewAI memory is off for the book agents, because it never worked.

Across every recorded run:

    memory_save_started     42
    memory_save_completed    0
    memory_save_failed      53

and the store directory - `AppData/Local/CrewAI/api/memory` - is empty. No
embedder is configured, so every save fails and every search reads nothing.

It was not free while doing nothing. Memory attaches `search_memory` and
`save_to_memory` to the agent, and each tool round-trip resends the entire
prompt: 20,564 characters for the writer, three times per subsection. The book
made 298 calls for 75 subsections where 150 would do, about $1.30.

Had it worked it would have been a hazard. The store is machine-wide, so one
client's book could recall passages from another's. What memory was meant to
prevent - a subsection repeating its neighbours - is already prevented by
`avoid_overlap_with`, which the planner writes per subsection and which names
the titles rather than hoping a similarity search finds them.
"""

from __future__ import annotations

import inspect

import pytest

BOOK_AGENTS = [
    ("app.agents.books.book_content", "make_writer_agent"),
    ("app.agents.books.book_content", "make_quality_agent"),
]


@pytest.mark.parametrize("module_name,factory", BOOK_AGENTS)
def test_the_book_agents_do_not_enable_memory(module_name, factory):
    import importlib

    module = importlib.import_module(module_name)
    source = inspect.getsource(getattr(module, factory))
    assert "memory=True" not in source, f"{factory} still enables memory"


def test_the_planner_does_not_enable_memory():
    from app.agents.books.book_planning import agent_task_planning

    source = inspect.getsource(agent_task_planning)
    assert "memory=True" not in source
    assert "memory=False" in source


def test_nothing_on_the_book_path_enables_memory():
    """A third agent appearing on the path later would carry the same cost
    silently, so the whole path is scanned rather than the two known files.

    Both layers are read: the steps import some agents directly, and the
    planner is reached through `planning_services` instead - checking only the
    step module missed it entirely on the first version of this test.
    """
    import pathlib

    api = pathlib.Path(inspect.getfile(
        __import__("app.pipelines.books.steps", fromlist=["x"]))).parents[3]
    callers = "\n".join(
        path.read_text(encoding="utf-8")
        for folder in ("pipelines/books", "services/books")
        for path in (api / "app" / folder).glob("*.py")
    )

    agents = api / "app" / "agents" / "books"
    offenders = [
        path.name
        for path in sorted(agents.glob("*.py"))
        if path.stem in callers and "memory=True" in path.read_text(encoding="utf-8")
    ]
    assert not offenders, f"memory is still on in: {offenders}"


def test_the_reason_is_recorded_against_the_fidelity_guard():
    """Both files are carried over from the original tree, so the switch has to
    be written down as a permitted change rather than pass unnoticed."""
    from tests.test_ported_fidelity import INTENTIONAL

    for key in ("agents/books/book_content.py", "agents/books/book_planning.py"):
        assert key in INTENTIONAL, f"{key} changed without a recorded reason"
        reason, allowed = INTENTIONAL[key]
        assert "memory" in reason
        assert "        memory=True," in allowed


def test_overlap_is_still_prevented_by_the_plan():
    """The thing memory was supposed to do. It is done by a field the planner
    fills per subsection, and it survives this change untouched."""
    from app.services.books.content_services import build_inputs

    inputs = build_inputs(
        {}, {"chapter_intro": {}}, {}, "موضوع",
        {"avoid_overlap_with": ["موضوع سابق", "موضوع آخر"]},
    )
    assert "موضوع سابق" in inputs["avoid_overlap_with"]