Coverage for app / util / attachment_wire.py: 100%
39 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-10-31 11:21 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-10-31 11:21 +0000
1"""What a turn's attached documents are, on the wire between the services.
3A chat's attachments travel from svc-core to svc-ai beside the person's
4words (``ExecuteRequest.attachments``), never pasted into them: pasted, every
5step of a run read a document as the person's own words — the 24 October
62026 refusal, where a report's file name forced a knowledge search that
7then told the agent the report did not exist.
9svc-core resolves which documents a turn may use and sends them; svc-ai
10decides how the model reads them. The vocabulary and the limits are the two
11services' shared contract, so this file is the same in both
12(``tools/test_shared_modules.py``).
13"""
15from __future__ import annotations
17from enum import StrEnum
18from typing import Any
21class AttachedIn(StrEnum):
22 """When the person attached a document, relative to the turn."""
24 #: With the message being answered.
25 turn = "turn"
26 #: With an earlier message of the same conversation.
27 earlier = "earlier"
30class Availability(StrEnum):
31 """Whether a document's text travels with the turn."""
33 #: Its text is here.
34 available = "available"
35 #: Deleted from the library, or no longer readable: named, never quoted.
36 removed = "removed"
37 #: Left out for room (``MAX_ATTACHMENTS`` / ``MAX_ATTACHMENTS_CHARS``):
38 #: named, never quoted.
39 omitted = "omitted"
42#: Documents one message may attach.
43MAX_TURN_ATTACHMENTS = 10
44#: Documents one turn carries, this message's and earlier ones'.
45MAX_ATTACHMENTS = 20
46#: Text one document may carry; more is cut and marked ``truncated``.
47MAX_ATTACHMENT_CHARS = 1_000_000
48#: Text a turn's documents may carry together.
49MAX_ATTACHMENTS_CHARS = 3_000_000
51#: The sources one document may carry. A document that is an answer — a
52#: swarm's piece, an earlier request's answer — cites its sources ``[1]``,
53#: ``[2]``…; carried beside it, svc-ai gives each one an id of the run's, so
54#: a step that reads the document cites the source itself, not the document
55#: (``svc-ai app/util/attachments.gather``).
56MAX_ATTACHMENT_SOURCES = 100
57#: What a carried source keeps of a citation (svc-ai's ``cited_citations``):
58#: the number the document's text cites it by, and what names it.
59ATTACHED_SOURCE_KEYS: tuple[str, ...] = (
60 "marker",
61 "kind",
62 "tool",
63 "title",
64 "page_title",
65 "source",
66 "url",
67 "kb_name",
68 "kb_id",
69 "document_id",
70 "page_number",
71 "published",
72 "author",
73 "attribution",
74 "publisher",
75 "licence_url",
76)
79def _marker(value: Any) -> int | None:
80 if isinstance(value, bool):
81 return None
82 if isinstance(value, int):
83 return value if value >= 1 else None
84 if isinstance(value, str) and value.strip().isdigit():
85 return int(value.strip()) or None
86 return None
89def carried_sources(citations: Any) -> list[dict[str, Any]]:
90 """``citations`` as a document carries them: each with a whole ``marker``
91 from 1, once per marker, keeping its :data:`ATTACHED_SOURCE_KEYS` that
92 hold a value — :data:`MAX_ATTACHMENT_SOURCES` at most. Anything else is
93 left out, never an error."""
94 out: list[dict[str, Any]] = []
95 seen: set[int] = set()
96 for citation in citations if isinstance(citations, (list, tuple)) else []:
97 if not isinstance(citation, dict):
98 continue
99 marker = _marker(citation.get("marker"))
100 if marker is None or marker in seen:
101 continue
102 seen.add(marker)
103 kept = {
104 key: citation[key]
105 for key in ATTACHED_SOURCE_KEYS
106 if citation.get(key) not in (None, "")
107 }
108 out.append({**kept, "marker": marker})
109 if len(out) >= MAX_ATTACHMENT_SOURCES:
110 break
111 return out