agent-zero/tests/test_history.py
Alessandro cc098d3e98
Some checks failed
Build And Publish Docker Images / plan (push) Has been cancelled
Build And Publish Docker Images / build (push) Has been cancelled
Speed up chat restoration and bound embedding warm-up
Reuse Git version metadata within each chat-load operation and pass saved
message token counts into reconstruction. Preserve per-agent settings and
credential reads, initialization hooks, profiles and legacy token behavior.

Give the existing deferred embedding preload up to 10 extra seconds after
chat restoration, within extensible init_a0. Continue startup on timeout or
task failure while allowing unfinished model loading to complete.

Across five trials per version with the same chat workload and a cached
embedding model, median HTTP readiness after the restart command returned
fell from 38.1 to 27.8 seconds: 10.3 seconds saved, or 27% less waiting.
Boot-to-first-token fell from 42.7 to 33.4 seconds. Post-send first-token
latency did not improve in this sample (4.709 to 5.108 seconds).

Validation: 146 focused framework tests passed, followed by 20 preload tests
including seven new edge cases. Real ten-second stalled-loading/inference
checks and offline missing/cached-model checks passed. Five DeferredTask
lifecycle tests passed separately; an existing test-order failure when
combined with preload imports also reproduces without the new cases.

Fresh-install performance was not measured; the full repository suite was
not run.
2026-10-11 19:27:39 +02:00

56 lines
1.8 KiB
Python

import pytest
from helpers import history
from helpers.history import output_langchain
from langchain_core.messages import AIMessage, HumanMessage
def test_output_langchain_omits_leading_assistant_messages():
messages = output_langchain(
[
{"ai": True, "content": "Welcome"},
{"ai": False, "content": "Hello"},
{"ai": True, "content": "Hi"},
]
)
assert messages == [HumanMessage("Hello"), AIMessage("Hi")]
def test_restored_message_reuses_saved_tokens_and_summary(monkeypatch):
data = {
"_cls": "Message", "ai": False, "content": "original content",
"id": "message", "sequence": 2, "metadata": {"custom": True},
"summary": "saved summary", "tokens": 42,
}
def recount(_text):
pytest.fail("Restoring a saved count must not tokenize the message again")
monkeypatch.setattr(history.tokens, "approximate_tokens", recount)
restored = history.Message.from_dict(data, history=None)
assert restored.to_dict() == data
assert restored.get_tokens() == 42
assert restored.output()[0]["content"] == "saved summary"
@pytest.mark.parametrize("saved", [{}, {"tokens": 0}, {"tokens": None}])
def test_restored_message_without_token_count_recounts_using_summary(monkeypatch, saved):
counted = []
def count(text):
counted.append(text)
return 7
monkeypatch.setattr(history.tokens, "approximate_tokens", count)
restored = history.Message.from_dict(
{"ai": False, "content": "original", "summary": "summary", **saved},
history=None,
)
assert restored.tokens == saved.get("tokens", 0)
assert restored.get_tokens() == 7
assert counted == ["user: original", "user: summary"]
assert restored.get_tokens() == 7
assert len(counted) == 2