Coverage for app / memory / text.py: 100%
29 statements
« prev ^ index » next coverage.py v7.13.4, created at 2026-10-31 11:23 +0000
« prev ^ index » next coverage.py v7.13.4, created at 2026-10-31 11:23 +0000
1"""The person's own words in a turn's input, and what an episode keeps.
3A turn's input is not only what the person typed. svc-core puts attached
4documents first, as a ``<documents>`` block (svc-core
5``app/util/document.py``), and the question after it. Memory must tell the
6two apart: an episode remembered the first 400 characters of the input, so
7a turn with an attachment was remembered as the start of the document, and
8a fact must be quoted from what the person said — never from a document,
9whose text anyone could have written.
10"""
12from __future__ import annotations
14from collections.abc import Sequence
16from app.util import attachments as attachment_util
18#: What an episode keeps of the request, and of the answer when no model
19#: summarised it.
20TASK_MAX_CHARS = 400
21RESPONSE_MAX_CHARS = 800
22#: What an episode keeps of the attachments' names, and of the answers to a
23#: scope clarification.
24NOTE_MAX_CHARS = 160
27def split_attachments(text: str) -> tuple[str, list[str]]:
28 """``(what the person wrote, the attached documents' names)``, from an
29 input that still carries a pasted ``<documents>`` block
30 (``attachments.split_legacy``)."""
31 words, docs = attachment_util.split_legacy(text)
32 return words, [d.name for d in docs]
35def user_text(text: str) -> str:
36 """What the person wrote in ``text``, without attached documents."""
37 return split_attachments(text)[0]
40def _cut(text: str, limit: int) -> str:
41 text = " ".join((text or "").split())
42 return text if len(text) <= limit else text[: limit - 1].rstrip() + "…"
45def notes(attached: Sequence[str] = (), clarified: Sequence[str] = ()) -> str:
46 """What an episode adds after the question: the attachments' names, then
47 the answers to a scope clarification (`` [attached: a.pdf] [clarified:
48 UK; Last quarter]``), or nothing."""
49 out = ""
50 if attached:
51 out += f" [attached: {_cut(', '.join(attached), NOTE_MAX_CHARS)}]"
52 answers = [a.strip() for a in clarified if a and a.strip()]
53 if answers:
54 out += f" [clarified: {_cut('; '.join(answers), NOTE_MAX_CHARS)}]"
55 return out
58def episode(
59 request: str,
60 response: str | None,
61 clarified: Sequence[str] = (),
62 attached: Sequence[str] = (),
63) -> str:
64 """One turn as an episode: ``Task: …`` then ``Response: …``.
66 ``request`` is the person's request as they wrote it — before a scope
67 clarification folded its scaffolding into the turn's input; attachments
68 (``attached``, or a block still pasted into it) are named, not quoted. ``clarified`` is what they answered to that
69 clarification (``clarify.answer_labels``). ``response`` is the answer's
70 summary (or the answer, cut) — None when the answer is not to be
71 remembered as a conclusion.
72 """
73 asked, pasted = split_attachments(request)
74 task = (_cut(asked, TASK_MAX_CHARS) or "(no question text)") + notes(
75 list(attached) or pasted, clarified
76 )
77 lines = [f"Task: {task}"]
78 if response is not None:
79 lines.append(f"Response: {_cut(response, RESPONSE_MAX_CHARS)}")
80 return "\n".join(lines)