Coverage for app / services / economics / attribution.py: 97%

61 statements  

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

1""" 

2Whose a day's spend was, and what it was for — the economics export's two 

3attributions. 

4 

5**Whose:** a workspace (``app/services/workspace.py``), or the platform, for 

6work nobody started: the platform benchmark, the built-in dashboards' tiles, 

7crawls of the built-in sources and indexing of the user guide. A project's 

8rows name its organisation as well, so an organisation's figures can include 

9its projects (as its quotas do). 

10 

11**What for:** a source, in the usage ledger's own vocabulary 

12(``UsageEnum.Source``). The ledger records one. svc-ai's meter records a 

13*purpose* instead, which ``PURPOSE_SOURCES`` maps onto the same vocabulary so 

14that the two can be compared line by line. A purpose the map does not know is 

15reported as ``other``, never folded into a source it might not be. 

16""" 

17 

18from __future__ import annotations 

19 

20from dataclasses import dataclass 

21from enum import StrEnum 

22from typing import Any 

23from uuid import UUID 

24 

25from sqlalchemy.orm import Session 

26 

27from app.db.postgres.enums import UsageEnum 

28from app.services.dashboards.generator import GENERATION_TRACE_PURPOSE 

29from app.services.dashboards.widgets import WIDGET_TRACE_PURPOSE 

30from app.services.evaluation.targets import JUDGE_TRACE_PURPOSE, TRACE_PURPOSE 

31from app.services.skill_runs.runner import TRACE_PURPOSE as SKILL_RUN_PURPOSE 

32 

33Source = UsageEnum.Source 

34 

35#: Spend whose purpose ``PURPOSE_SOURCES`` does not know. 

36OTHER = "other" 

37 

38 

39class Kind(StrEnum): 

40 """What owns a row of the export.""" 

41 

42 personal = "personal" 

43 organisation = "organisation" 

44 project = "project" 

45 #: Nobody: work CHAIN-R does for itself. 

46 platform = "platform" 

47 

48 

49@dataclass(frozen=True) 

50class Owner: 

51 kind: Kind 

52 #: The user, organisation or project; None for the platform. 

53 id: str | None 

54 #: The organisation whose figures include this row: itself, or a 

55 #: project's organisation. None for personal work and personal projects. 

56 organisation_id: str | None 

57 

58 def columns(self) -> dict[str, str | None]: 

59 return { 

60 "workspace_kind": self.kind.value, 

61 "workspace_id": self.id, 

62 "organisation_id": self.organisation_id, 

63 } 

64 

65 

66PLATFORM = Owner(Kind.platform, None, None) 

67 

68 

69def _text(value: Any) -> str | None: 

70 return None if value is None else str(value) 

71 

72 

73class Projects: 

74 """Each project's organisation, read once for a whole export.""" 

75 

76 def __init__(self, db: Session) -> None: 

77 from app.db.postgres.models import Project 

78 

79 self._organisation = { 

80 str(project_id): _text(organisation_id) 

81 for project_id, organisation_id in db.query( 

82 Project.id, Project.owner_organisation_id 

83 ).all() 

84 } 

85 

86 def organisation_of(self, project_id: Any) -> str | None: 

87 return self._organisation.get(str(project_id)) 

88 

89 

90def owner( 

91 projects: Projects, 

92 *, 

93 user_id: Any = None, 

94 organisation_id: Any = None, 

95 project_id: Any = None, 

96) -> Owner: 

97 """The workspace a row was in, from the workspace columns a resource 

98 carries (``workspace_organisation_id`` / ``workspace_project_id``, or an 

99 owner's) and the person it is otherwise theirs: neither set is that 

100 person's Personal; no person either is the platform.""" 

101 if project_id is not None: 

102 return Owner( 

103 Kind.project, 

104 str(project_id), 

105 _text(organisation_id) or projects.organisation_of(project_id), 

106 ) 

107 if organisation_id is not None: 

108 return Owner(Kind.organisation, str(organisation_id), str(organisation_id)) 

109 if user_id is not None: 

110 return Owner(Kind.personal, str(user_id), None) 

111 return PLATFORM 

112 

113 

114def trace_owner(projects: Projects, workspace: str | None, user_id: Any) -> Owner: 

115 """The workspace of a svc-ai trace, from the key svc-core stamped on it 

116 (``user:<id>``, ``organisation:<id>``, ``project:<id>``). A trace with 

117 no key predates workspaces and is its user's Personal; one with no user 

118 either is the platform's.""" 

119 kind, _, ident = (workspace or "").partition(":") 

120 try: 

121 key = UUID(ident) if ident else None 

122 except ValueError: 

123 key = None 

124 if kind == "project" and key: 

125 return owner(projects, project_id=key) 

126 if kind == "organisation" and key: 

127 return owner(projects, organisation_id=key) 

128 if kind == "user" and key: 

129 return owner(projects, user_id=key) 

130 return owner(projects, user_id=user_id) 

131 

132 

133#: svc-ai's purpose for a traced invocation (``LLMInvocation.purpose``) → the 

134#: source whose spend it is. svc-core's flows name their own purpose; svc-ai's 

135#: flows and execution types are its strings, held to its source by 

136#: ``tests/test_economics_export.py``. 

137PURPOSE_SOURCES: dict[str, Source] = { 

138 # Execution types: what svc-ai records when the request names no purpose. 

139 "agent": Source.agent, 

140 "graph": Source.agent, 

141 "worker": Source.agent, 

142 "pipeline": Source.agent, 

143 "model": Source.model, 

144 # svc-core's flows. 

145 SKILL_RUN_PURPOSE: Source.skill, 

146 TRACE_PURPOSE: Source.experiment, 

147 JUDGE_TRACE_PURPOSE: Source.experiment, 

148 WIDGET_TRACE_PURPOSE: Source.dashboard, 

149 GENERATION_TRACE_PURPOSE: Source.generation, 

150 # svc-ai's generative setup routers. 

151 "agent_generation": Source.generation, 

152 "skill_generation": Source.generation, 

153 "skillset_generation": Source.generation, 

154 "toolkit_generation": Source.generation, 

155 "knowledge_generation": Source.generation, 

156 "rule_generation": Source.generation, 

157 # svc-ai's knowledge and simulation endpoints. 

158 "knowledge_retrieval": Source.knowledge, 

159 "knowledge_indexing": Source.knowledge, 

160 "simulation": Source.simulation, 

161} 

162 

163 

164def source_of(purpose: str | None) -> str: 

165 """The source a trace's spend counts toward, or ``other``.""" 

166 source = PURPOSE_SOURCES.get(purpose or "") 

167 return source.value if source else OTHER 

168 

169 

170def sources() -> list[str]: 

171 """Every source the export can name, in a stable order.""" 

172 return [source.value for source in Source] + [OTHER]