Coverage for app / agents / effort.py: 95%

39 statements  

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

1"""What each cost profile does to a turn (cost-control plan, B2). 

2 

3A cost profile (``app/util/cost_profiles.py``, shared with svc-core) is 

4chosen per agent; this is the one place that says what it changes. Every 

5pipeline setting a profile may move is a field of :class:`PipelineEffort`, 

6and the graph reads it from here — never a module constant of its own — so a 

7profile is one entry in :data:`EFFORT` and nothing else. 

8 

9``thorough`` is the pipeline exactly as it ran before profiles existed: each 

10field's value is the constant the graph used to hold 

11(``tests/agents/test_cost_profiles.py`` pins that). ``balanced`` does less of 

12it, and ``economy`` is balanced with a lighter model doing the lighter work 

13(the model svc-core sends as ``lighter_model``). Both are offered as 

14unmeasured (``cost_profiles.MEASURED``) until the gold datasets show they 

15answer as well, and are never anyone's default before then. 

16 

17The graph carries only the profile's *key* (``state.context["cost_profile"]``), 

18never this object, so a checkpointed run stays serialisable. 

19""" 

20 

21from __future__ import annotations 

22 

23import logging 

24from dataclasses import dataclass, replace 

25from typing import Any 

26 

27from app.util.cost_profiles import DEFAULT, LIGHTER_MODEL, CostProfile, parse 

28 

29logger = logging.getLogger(__name__) 

30 

31#: The context key a run's profile travels under. 

32CONTEXT_KEY = "cost_profile" 

33 

34 

35@dataclass(frozen=True) 

36class PipelineEffort: 

37 """How much work one turn's pipeline does.""" 

38 

39 #: Earlier turns an answering node replays verbatim. 

40 history_turns: int 

41 #: How many times the quality check may send a draft back for revision, 

42 #: and how many for a task triage calls simple. 

43 max_eval_retries: int 

44 simple_eval_retries: int 

45 #: How many memories a turn recalls, and the most characters they take. 

46 memory_recall_limit: int 

47 memory_block_max_chars: int 

48 #: A cap of the profile's own on a call's history, in tokens, beside the 

49 #: model's window and price tier (``token_budget``). None: no cap. 

50 history_token_cap: int | None = None 

51 #: Drop old turns in blocks of this many rather than one at a time, so 

52 #: the replayed history starts at the same turn for several turns and a 

53 #: provider can serve it from cache (``prompt_cache.mark_last``). It 

54 #: shows the model up to this many turns more than ``history_turns``. 

55 #: None: one at a time, as the pipeline always has. 

56 history_block_turns: int | None = None 

57 #: Whether a request triage calls simple gets a quality check. False 

58 #: answers it unchecked, recorded as ``skipped`` (never as a pass). 

59 check_simple_tasks: bool = True 

60 #: Whether the lighter model (``lighter_model``) runs the steps nobody 

61 #: reads: triage, the planner, retrieval's query planning and reranking, 

62 #: the quality check, memory summaries. Synthesis and the retrieval 

63 #: workers keep the agent's own model. 

64 lighter_steps: bool = False 

65 #: Whether the lighter model answers a request triage calls simple, on a 

66 #: plain agent (never a stored skill's graph), when the conversation fits 

67 #: it whole. The answer names the model that wrote it. 

68 lighter_simple_answers: bool = False 

69 #: Whether a turn's mix searches its supporting kinds of knowledge beside 

70 #: the primary (``app/knowledge/mix.py``). False searches the primary and 

71 #: any method alone: fewer candidates to judge, less evidence beside. 

72 supporting_lanes: bool = True 

73 

74 

75#: Each profile's settings. Complete: every profile in the vocabulary has one. 

76EFFORT: dict[CostProfile, PipelineEffort] = { 

77 CostProfile.thorough: PipelineEffort( 

78 history_turns=10, 

79 max_eval_retries=2, 

80 simple_eval_retries=1, 

81 memory_recall_limit=8, 

82 memory_block_max_chars=6_000, 

83 history_token_cap=None, 

84 history_block_turns=None, 

85 check_simple_tasks=True, 

86 ), 

87 # Unmeasured (``cost_profiles.MEASURED``). Each setting is one of the 

88 # proportionate-effort levers in agent-graph-review.md: a simple request 

89 # answered without the quality check (one call in three on a chat that 

90 # retrieves nothing), one revision rather than two, six to nine earlier 

91 # turns read from cache rather than ten re-sent, half the memory, and a 

92 # history held to 24K tokens. 

93 CostProfile.balanced: PipelineEffort( 

94 history_turns=6, 

95 max_eval_retries=1, 

96 simple_eval_retries=0, 

97 memory_recall_limit=4, 

98 memory_block_max_chars=3_000, 

99 history_token_cap=24_000, 

100 history_block_turns=4, 

101 check_simple_tasks=False, 

102 ), 

103} 

104# Unmeasured too. Balanced, with the one proportionate-effort lever that 

105# changes the model rather than how much is sent to it: the agent's maker's 

106# lightest model in the same region does the work nobody reads and answers 

107# what triage calls simple. Kept a profile of its own so the measurement can 

108# tell its lever apart from Balanced's. 

109EFFORT[CostProfile.economy] = replace( 

110 EFFORT[CostProfile.balanced], 

111 lighter_steps=True, 

112 lighter_simple_answers=True, 

113 supporting_lanes=False, 

114) 

115 

116if set(EFFORT) != set(CostProfile): 

117 raise RuntimeError("Every cost profile needs its pipeline settings") 

118for _profile, _effort in EFFORT.items(): 

119 if (_effort.lighter_steps or _effort.lighter_simple_answers) != LIGHTER_MODEL[ 

120 _profile 

121 ]: 

122 raise RuntimeError( 

123 f"Cost profile {_profile} uses a lighter model in one service and not " 

124 "the other (cost_profiles.LIGHTER_MODEL)" 

125 ) 

126 

127THOROUGH = EFFORT[CostProfile.thorough] 

128 

129 

130def effort_of(profile: Any) -> PipelineEffort: 

131 """The settings for ``profile`` — a key, a ``CostProfile``, or None for 

132 the default. One the vocabulary does not know is the default, logged: 

133 a turn is never refused for it.""" 

134 if profile is None or profile == "": 

135 return EFFORT[DEFAULT] 

136 found = parse(profile) 

137 if found is None: 

138 logger.warning("[effort] unknown cost profile %r — running thorough", profile) 

139 return EFFORT[DEFAULT] 

140 return EFFORT[found] 

141 

142 

143def of_state(state: Any) -> PipelineEffort: 

144 """The settings of the run ``state`` belongs to.""" 

145 context = getattr(state, "context", None) or {} 

146 return effort_of(context.get(CONTEXT_KEY))