Coverage for app / services / withholding.py: 80%

25 statements  

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

1""" 

2Withholding an answer — the Output layer's ``withhold`` action, svc-core's side. 

3 

4A ``withhold`` rule gives people its own message in place of an answer, once 

5every other Output rule has run on it, and sends the answer itself to review 

6(``rule_engine.Withheld``, ``review.record_withheld``) — the one place it is 

7then kept, for the workspace's reviewers. 

8 

9Its conditions read the finished answer, so whether it fires is known only 

10at the end. So while one governs a turn (``rule_engine.may_withhold``), 

11nothing the run writes reaches the person, or anywhere they can read, before 

12the Output rules have run on it: 

13 

14- the chat streams no text: no tokens, no sources until the answer is 

15 checked, and trace frames held to what describes the run rather than what 

16 it wrote (:func:`held_frame`) — shown and stored alike; 

17- what the run wrote along the way — its workers' text, the quality check's 

18 critique — is not stored; 

19- a turn stopped or failed part-way keeps :data:`HELD_TEXT`, never what it 

20 had written. 

21 

22svc-ai, which is sent the rule, keeps nothing of the answer either: no 

23memory of it, no preview in its trace, no tool's arguments 

24(svc-ai ``RulesEnforcer.may_withhold``). 

25""" 

26 

27from __future__ import annotations 

28 

29from typing import Any 

30 

31#: Shown while a held turn's answer is being written and checked. 

32HELD_STATUS = ( 

33 "Writing the answer — it appears once it has been checked against your rules…" 

34) 

35 

36#: Stored for a held turn that stopped or failed before its answer was 

37#: checked: what it had written is not kept. 

38HELD_TEXT = ( 

39 "The answer was not kept: it had not been checked against this workspace's " 

40 "rules when the turn ended." 

41) 

42 

43#: For each trace frame svc-ai streams, the keys a held frame keeps: what 

44#: ran, when (``t_ms``, from the stream's start) and on which pass, on which 

45#: model, and at what cost — never what it read or wrote (a node's input, 

46#: its output preview, the state it set, a tool's arguments or result, a 

47#: system node's result). 

48_KEPT_KEYS: dict[str, frozenset[str]] = { 

49 "graph_plan": frozenset({"type", "stages"}), 

50 "node_start": frozenset({"type", "node_id", "is_system_node", "t_ms", "pass"}), 

51 "node_end": frozenset( 

52 { 

53 "type", 

54 "node_id", 

55 "is_system_node", 

56 "tokens", 

57 "duration_ms", 

58 "t_ms", 

59 "model_name", 

60 "provider", 

61 "history_turns_omitted", 

62 } 

63 ), 

64 "tool_call": frozenset({"type", "node_id", "tool", "status"}), 

65 "system_node_result": frozenset({"type", "node_id"}), 

66} 

67 

68#: Frames that pass as they are: a pause is checked by the Output rules 

69#: before it is sent (``hitl.audit_pause``). 

70_PASSED = frozenset({"hitl_pause"}) 

71 

72#: Frames that are not sent at all: they take back text that was never shown. 

73_DROPPED = frozenset({"token_reset"}) 

74 

75 

76def held_frame(frame: dict[str, Any]) -> dict[str, Any] | None: 

77 """``frame`` as a held turn shows and stores it, or None when it is not 

78 sent at all. A frame of a type not listed keeps only its type and node.""" 

79 kind = str(frame.get("type") or "") 

80 if kind in _DROPPED: 

81 return None 

82 if kind in _PASSED: 

83 return frame 

84 kept = _KEPT_KEYS.get(kind, frozenset({"type", "node_id"})) 

85 return {key: value for key, value in frame.items() if key in kept} 

86 

87 

88def held_trace(frames: list[dict[str, Any]] | None) -> list[dict[str, Any]] | None: 

89 """A trace's frames as a held turn keeps them (:func:`held_frame`).""" 

90 if not frames: 

91 return frames 

92 held = [frame for frame in (held_frame(f) for f in frames) if frame is not None] 

93 return held or None 

94 

95 

96def checked_verdict(eval_result: dict[str, Any] | None) -> dict[str, Any]: 

97 """What a held turn keeps of its quality check: the outcome and score — 

98 not the critique or the gaps, which quote drafts nobody checked.""" 

99 return { 

100 key: value 

101 for key, value in (eval_result or {}).items() 

102 if key in ("outcome", "score") 

103 } 

104 

105 

106def strip_tool_calls(calls: list[dict[str, Any]] | None) -> list[dict[str, Any]]: 

107 """Tool calls of a withheld answer: which tools ran, not what they were 

108 given or returned — a document's body is the answer itself.""" 

109 return [ 

110 {key: value for key, value in call.items() if key in _TOOL_CALL_KEPT} 

111 for call in calls or [] 

112 if isinstance(call, dict) 

113 ] 

114 

115 

116_TOOL_CALL_KEPT = frozenset({"name", "tool", "status", "id"})