Coverage for app / tools / web / report.py: 94%

31 statements  

« prev     ^ index     » next       coverage.py v7.13.4, created at 2026-10-31 11:23 +0000

1"""The web search's report, built from what the tool did. 

2 

3The tool gives the model its text (``search.py``) and keeps this beside it 

4as the tool message's artifact, which no model is sent: the stream's 

5``tool_call`` frame, the batch response's context and the stored tool call 

6carry it to svc-core instead. Its shape is ``app/util/web_search_report.py``, 

7which svc-core carries byte for byte; each vocabulary it names is svc-ai's 

8own, held value for value at import. 

9""" 

10 

11from __future__ import annotations 

12 

13from collections.abc import Iterable, Sequence 

14from typing import TYPE_CHECKING, Any 

15 

16from app.tools.web.models import ( 

17 AttemptOutcome, 

18 Freshness, 

19 SearchResponse, 

20 SearchResult, 

21 SearchType, 

22) 

23from app.tools.web.policy import DomainPolicy, Verdict, hostname_of 

24from app.util import web_search_report as wire 

25 

26if TYPE_CHECKING: 

27 from app.tools.web.search import Screened, SearchRound 

28 

29 

30def _same(ours: Iterable[str], theirs: Iterable[str], what: str) -> None: 

31 ours, theirs = set(ours), set(theirs) 

32 if ours != theirs: 

33 raise RuntimeError( 

34 f"web search report: {what} differ — svc-ai {sorted(ours)}, " 

35 f"report {sorted(theirs)}" 

36 ) 

37 

38 

39# What the report says, in svc-ai's own words: a check added to the policy, or 

40# an outcome added to an attempt, is a report the other service cannot read 

41# until the shared module says it too. 

42_same( 

43 (v.value for v in Verdict if v is not Verdict.PERMITTED), 

44 (r.value for r in wire.Removal), 

45 "removal kinds", 

46) 

47_same( 

48 (o.value for o in AttemptOutcome), 

49 (o.value for o in wire.AttemptOutcome), 

50 "attempt outcomes", 

51) 

52 

53 

54def _attempts(rounds: Sequence[SearchRound]) -> list[wire.Attempt]: 

55 """Every engine asked, in order: the first of each search is the 

56 ``primary``, any after it the ``fallback``.""" 

57 return [ 

58 wire.Attempt( 

59 engine=wire.Engine.primary if index == 0 else wire.Engine.fallback, 

60 search_type=attempt.search_type.value, 

61 outcome=wire.AttemptOutcome(attempt.outcome.value), 

62 reason=attempt.reason.value if attempt.reason is not None else None, 

63 returned=attempt.returned, 

64 ) 

65 for search in rounds 

66 for index, attempt in enumerate(search.attempts) 

67 ] 

68 

69 

70def _host(url: str) -> str: 

71 return hostname_of(url).removeprefix("www.") 

72 

73 

74def _put_first(kept: Sequence[SearchResult], policy: DomainPolicy) -> list[str]: 

75 """Hosts of the results given to the model that the rules put first.""" 

76 hosts: list[str] = [] 

77 for result in kept: 

78 host = _host(result.url) 

79 if host and host not in hosts and policy.prefers(result.url): 

80 hosts.append(host) 

81 if len(hosts) >= wire.MAX_HOSTS: 

82 break 

83 return hosts 

84 

85 

86def build( 

87 *, 

88 search_type: SearchType, 

89 freshness: Freshness | None, 

90 outcome: wire.SearchOutcome, 

91 policy: DomainPolicy | None = None, 

92 rounds: Sequence[SearchRound] = (), 

93 response: SearchResponse | None = None, 

94 searched_as: SearchType | None = None, 

95 screened: Screened | None = None, 

96 kept: Sequence[SearchResult] = (), 

97 scoped_to: str | None = None, 

98 retried: SearchRound | None = None, 

99 trusted_search: bool = False, 

100) -> dict[str, Any]: 

101 """One call's report, as the tool's artifact carries it.""" 

102 removed = ( 

103 {verdict.value: n for verdict, n in screened.removed.items() if n} 

104 if screened is not None 

105 else {} 

106 ) 

107 left_out = ( 

108 [ 

109 wire.LeftOut(host=host, by=wire.Removal(verdict.value)) 

110 for host, verdict in screened.left_out 

111 ] 

112 if screened is not None 

113 else [] 

114 ) 

115 report = wire.SearchReport( 

116 search_type=search_type.value, 

117 searched_as=( 

118 searched_as.value 

119 if searched_as is not None and searched_as is not search_type 

120 else None 

121 ), 

122 freshness=freshness.value if freshness is not None else None, 

123 outcome=outcome, 

124 attempts=_attempts(rounds), 

125 returned=len(response.results) if response is not None else 0, 

126 kept=len(kept), 

127 removed=removed, 

128 not_shown=( 

129 max(0, len(screened.permitted) - len(kept)) if screened is not None else 0 

130 ), 

131 left_out=left_out[: wire.MAX_HOSTS], 

132 put_first=_put_first(kept, policy) if policy is not None else [], 

133 scoped_to=wire.ScopedTo(scoped_to) if scoped_to else None, 

134 trusted_search=trusted_search, 

135 date_removed=retried.undated if retried is not None else None, 

136 sources_set_by=policy.preferred_set_by() if policy is not None else None, 

137 cached=bool(response is not None and response.cached), 

138 ) 

139 return report.wire() 

140 

141 

142__all__ = ["build"]