Coverage for app / services / system_resources / dashboards.py: 100%

38 statements  

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

1""" 

2Built-in dashboards — boards every user can open from their first login. 

3 

4Each is one row shown to everybody, so what is on it is chosen by whose data 

5a tile shows: 

6 

7- **The viewer's own.** Platform and network tiles (``stat``, ``chart``, 

8 ``breakdown``, ``resource_list``, the network views) fetch their data when 

9 they are drawn, with the viewer's session — so the Daily briefing shows each 

10 user their own last 24 hours. Every resource id is left empty, which these 

11 tiles read as "all of mine" or "my newest". 

12- **Nobody's.** A generated tile caches one answer for everybody. The fun fact 

13 is the only one: it asks about no user's data, runs on the deployment's 

14 default model (no agent, so no memory written), and is the platform's 

15 spend, recorded on each refresh's background event. 

16- **Never one user's.** Tiles that keep a user's content on the row — a pinned 

17 or saved message (``WidgetRenderer.stores_user_content``) — cannot go on a 

18 built-in board, or one user's message would be shown to all. 

19 

20``tests/test_built_in_dashboards.py`` holds every board to those three rules 

21and every tile to its renderer's validation. 

22 

23Users cannot change a built-in board; **Copy to my dashboards** makes an 

24ordinary board of their own from it, whose generated tile then runs as them. 

25 

26Ids are fixed, boards ``…000000000501`` onwards and each board's tiles 

27``…0000000501NN``. A tile keeps its id across releases so its cache does. 

28""" 

29 

30from __future__ import annotations 

31 

32import base64 

33from typing import Any 

34from uuid import UUID 

35 

36from app.db.postgres.enums import DashboardEnum 

37 

38from .definition import CHAINR_BOT, SystemDashboardSpec, SystemWidgetSpec 

39 

40WidgetType = DashboardEnum.WidgetType 

41 

42_ID_PREFIX = "c4a1b0e7-0000-4000-8000-" 

43 

44 

45def _board_id(board: int) -> UUID: 

46 return UUID(f"{_ID_PREFIX}{500 + board:012d}") 

47 

48 

49def _widget_id(board: int, widget: int) -> UUID: 

50 return UUID(f"{_ID_PREFIX}{(500 + board) * 100 + widget:012d}") 

51 

52 

53# ── In-app paths ────────────────────────────────────────────────────────────── 

54# 

55# fe-core's ``APP_ROUTES``; ``test_built_in_dashboards.py`` checks each path 

56# still exists there, since a moved page would otherwise leave a dead link. 

57 

58AGENTS = "/user/inference?section=agents" 

59KNOWLEDGE = "/user/knowledge" 

60ACTIVITY = "/user/activity" 

61STATS = "/user/stats" 

62RULES = "/user/governance?section=rules" 

63GOVERNANCE_ACTIVITY = "/user/governance?section=activity" 

64REVIEWS = "/user/governance?section=reviews" 

65EVALUATION = "/user/governance?section=evaluation" 

66ASSURANCE = "/user/governance?section=assurance" 

67DATASETS = "/user/governance?section=datasets" 

68EVALUATION_REPORTS = "/user/governance?section=reports" 

69#: Agent ids travel base64-encoded in fe-core's routes (``useResourceId``). 

70BOT_CHAT = ( 

71 "/user/inference/agents/id/" 

72 f"{base64.b64encode(str(CHAINR_BOT.id).encode()).decode()}/chat" 

73) 

74 

75 

76def _links(*pairs: tuple[str, str]) -> dict[str, Any]: 

77 """A shortcuts config, as ``ShortcutsWidget`` validates it.""" 

78 return { 

79 "links": [ 

80 {"label": label, "href": href, "icon": "ArrowUpRight"} 

81 for label, href in pairs 

82 ] 

83 } 

84 

85 

86def _breakdown( 

87 metric: str, 

88 period: str, 

89 *, 

90 dimension: str | None = None, 

91 display: str = "value", 

92 limit: int = 5, 

93 filters: tuple[str, ...] = (), 

94) -> dict[str, Any]: 

95 """A breakdown config, as ``BreakdownWidget`` validates it.""" 

96 return { 

97 "metric": metric, 

98 "dimension": dimension, 

99 "period": period, 

100 "display": display, 

101 "limit": limit, 

102 "filters": list(filters), 

103 } 

104 

105 

106# ── Daily briefing ──────────────────────────────────────────────────────────── 

107 

108FUN_FACT_PROMPT = ( 

109 "Share one fun fact for today: surprising, true and well established — " 

110 "from science, nature, space, history or language — the kind of thing a " 

111 "curious person enjoys repeating. Put the fact in a bold opening " 

112 "sentence, then explain in two or three sentences why it is so. Choose " 

113 "something settled rather than disputed or recent, and nothing political " 

114 "or upsetting." 

115) 

116 

117DAILY_BRIEFING = SystemDashboardSpec( 

118 id=_board_id(1), 

119 name="Daily briefing", 

120 description=( 

121 "Your last 24 hours on CHAIN-R — what ran, what it cost and what the " 

122 "rules did — with your recent chats and a fun fact to start the day." 

123 ), 

124 icon="Sunrise", 

125 accent="amber", 

126 position=0, 

127 widgets=( 

128 SystemWidgetSpec( 

129 id=_widget_id(1, 1), 

130 widget_type=WidgetType.automation, 

131 title="Fun fact of the day", 

132 config={ 

133 "prompt": FUN_FACT_PROMPT, 

134 "system_prompt": "", 

135 "style": "summary", 

136 "pdf": False, 

137 "target": {"type": "model", "default": True}, 

138 }, 

139 x=0, 

140 y=0, 

141 w=6, 

142 h=6, 

143 refresh_interval_seconds=86_400, 

144 ), 

145 SystemWidgetSpec( 

146 id=_widget_id(1, 2), 

147 widget_type=WidgetType.resource_list, 

148 title="Recent chats", 

149 config={"resource": "conversations", "limit": 6}, 

150 x=6, 

151 y=0, 

152 w=3, 

153 h=6, 

154 ), 

155 SystemWidgetSpec( 

156 id=_widget_id(1, 3), 

157 widget_type=WidgetType.shortcuts, 

158 title="Jump to", 

159 config=_links( 

160 ("Ask CHAINR Bot", BOT_CHAT), 

161 ("Your agents", AGENTS), 

162 ("Knowledge", KNOWLEDGE), 

163 ("Governance", RULES), 

164 ("Activity", ACTIVITY), 

165 ), 

166 x=9, 

167 y=0, 

168 w=3, 

169 h=6, 

170 ), 

171 SystemWidgetSpec( 

172 id=_widget_id(1, 4), 

173 widget_type=WidgetType.breakdown, 

174 title="Invocations", 

175 config=_breakdown("invocations", "last_24_hours"), 

176 x=0, 

177 y=6, 

178 w=3, 

179 h=3, 

180 ), 

181 SystemWidgetSpec( 

182 id=_widget_id(1, 5), 

183 widget_type=WidgetType.breakdown, 

184 title="Spend", 

185 config=_breakdown("estimated_cost_usd", "last_24_hours"), 

186 x=3, 

187 y=6, 

188 w=3, 

189 h=3, 

190 ), 

191 SystemWidgetSpec( 

192 id=_widget_id(1, 6), 

193 widget_type=WidgetType.breakdown, 

194 title="Blocked by rules", 

195 config=_breakdown("blocked", "last_24_hours"), 

196 x=6, 

197 y=6, 

198 w=3, 

199 h=3, 

200 ), 

201 SystemWidgetSpec( 

202 id=_widget_id(1, 7), 

203 widget_type=WidgetType.breakdown, 

204 title="Awaiting review", 

205 config=_breakdown("pending_flags", "all_time"), 

206 x=9, 

207 y=6, 

208 w=3, 

209 h=3, 

210 ), 

211 SystemWidgetSpec( 

212 id=_widget_id(1, 8), 

213 widget_type=WidgetType.breakdown, 

214 title="Your week, by agent", 

215 config=_breakdown( 

216 "invocations", "last_7_days", dimension="agent", display="bar" 

217 ), 

218 x=0, 

219 y=9, 

220 w=6, 

221 h=6, 

222 ), 

223 SystemWidgetSpec( 

224 id=_widget_id(1, 9), 

225 widget_type=WidgetType.breakdown, 

226 title="Where your spend went", 

227 config=_breakdown( 

228 "estimated_cost_usd", 

229 "last_7_days", 

230 dimension="source", 

231 display="list", 

232 limit=6, 

233 ), 

234 x=6, 

235 y=9, 

236 w=6, 

237 h=6, 

238 ), 

239 ), 

240) 

241 

242 

243# ── Agent fleet ─────────────────────────────────────────────────────────────── 

244 

245AGENT_FLEET = SystemDashboardSpec( 

246 id=_board_id(2), 

247 name="Agent fleet", 

248 description=( 

249 "Your agents side by side — what each can reach, what each remembers " 

250 "between chats, and how each is doing on spend, speed and errors." 

251 ), 

252 icon="Bot", 

253 accent="violet", 

254 position=1, 

255 widgets=( 

256 SystemWidgetSpec( 

257 id=_widget_id(2, 1), 

258 widget_type=WidgetType.knowledge_network, 

259 title="What each agent knows", 

260 config={ 

261 "agent_ids": [], 

262 "include_memory": True, 

263 # The fleet view is about the agents; the library's loose ends 

264 # belong on the Knowledge board. 

265 "include_unconnected": False, 

266 }, 

267 x=0, 

268 y=0, 

269 w=7, 

270 h=9, 

271 ), 

272 SystemWidgetSpec( 

273 id=_widget_id(2, 2), 

274 widget_type=WidgetType.agent_memory, 

275 title="What they remember", 

276 config={"agent_id": "", "recent": 10}, 

277 x=7, 

278 y=0, 

279 w=5, 

280 h=9, 

281 ), 

282 SystemWidgetSpec( 

283 id=_widget_id(2, 3), 

284 widget_type=WidgetType.breakdown, 

285 title="Spend by agent", 

286 config=_breakdown( 

287 "estimated_cost_usd", 

288 "this_month", 

289 dimension="agent", 

290 display="bar", 

291 limit=6, 

292 ), 

293 x=0, 

294 y=9, 

295 w=4, 

296 h=6, 

297 ), 

298 SystemWidgetSpec( 

299 id=_widget_id(2, 4), 

300 widget_type=WidgetType.breakdown, 

301 title="Average latency by agent", 

302 config=_breakdown( 

303 "avg_latency_ms", 

304 "last_7_days", 

305 dimension="agent", 

306 display="bar", 

307 limit=6, 

308 ), 

309 x=4, 

310 y=9, 

311 w=4, 

312 h=6, 

313 ), 

314 SystemWidgetSpec( 

315 id=_widget_id(2, 5), 

316 widget_type=WidgetType.breakdown, 

317 title="Error rate by agent", 

318 config=_breakdown( 

319 "error_rate", 

320 "last_30_days", 

321 dimension="agent", 

322 display="bar", 

323 limit=6, 

324 ), 

325 x=8, 

326 y=9, 

327 w=4, 

328 h=6, 

329 ), 

330 ), 

331) 

332 

333 

334# ── Governance ──────────────────────────────────────────────────────────────── 

335 

336GOVERNANCE = SystemDashboardSpec( 

337 id=_board_id(3), 

338 name="Governance", 

339 description=( 

340 "What your rules are doing — blocks, redactions and the review queue, " 

341 "with the rules responsible." 

342 ), 

343 icon="ShieldCheck", 

344 accent="rose", 

345 position=2, 

346 widgets=( 

347 SystemWidgetSpec( 

348 id=_widget_id(3, 1), 

349 widget_type=WidgetType.breakdown, 

350 title="Blocks this month", 

351 config=_breakdown("blocked", "this_month"), 

352 x=0, 

353 y=0, 

354 w=3, 

355 h=3, 

356 ), 

357 SystemWidgetSpec( 

358 id=_widget_id(3, 2), 

359 widget_type=WidgetType.breakdown, 

360 title="Block rate", 

361 config=_breakdown("block_rate", "this_month"), 

362 x=3, 

363 y=0, 

364 w=3, 

365 h=3, 

366 ), 

367 SystemWidgetSpec( 

368 id=_widget_id(3, 3), 

369 widget_type=WidgetType.breakdown, 

370 title="Values redacted", 

371 config=_breakdown("redacted_values", "this_month"), 

372 x=6, 

373 y=0, 

374 w=3, 

375 h=3, 

376 ), 

377 SystemWidgetSpec( 

378 id=_widget_id(3, 4), 

379 widget_type=WidgetType.breakdown, 

380 title="Awaiting review", 

381 config=_breakdown("pending_flags", "all_time"), 

382 x=9, 

383 y=0, 

384 w=3, 

385 h=3, 

386 ), 

387 SystemWidgetSpec( 

388 # The rules that ran and did nothing are the point of the activity 

389 # log, so the board leads with the full outcome split rather than 

390 # with blocks alone: a redaction rule over a clean prompt is 

391 # working. 

392 id=_widget_id(3, 5), 

393 widget_type=WidgetType.breakdown, 

394 title="What the rules did", 

395 config=_breakdown( 

396 "evaluations", 

397 "last_7_days", 

398 dimension="governance_outcome", 

399 display="bar", 

400 limit=6, 

401 ), 

402 x=0, 

403 y=3, 

404 w=6, 

405 h=6, 

406 ), 

407 SystemWidgetSpec( 

408 id=_widget_id(3, 6), 

409 widget_type=WidgetType.breakdown, 

410 title="Busiest rules", 

411 config=_breakdown( 

412 "evaluations", 

413 "last_7_days", 

414 dimension="rule", 

415 display="bar", 

416 limit=6, 

417 ), 

418 x=6, 

419 y=3, 

420 w=6, 

421 h=6, 

422 ), 

423 SystemWidgetSpec( 

424 id=_widget_id(3, 7), 

425 widget_type=WidgetType.breakdown, 

426 title="Redactions by kind", 

427 config=_breakdown( 

428 "redacted_values", 

429 "last_30_days", 

430 dimension="redaction_kind", 

431 display="list", 

432 limit=8, 

433 ), 

434 x=0, 

435 y=9, 

436 w=6, 

437 h=5, 

438 ), 

439 SystemWidgetSpec( 

440 id=_widget_id(3, 8), 

441 widget_type=WidgetType.shortcuts, 

442 title="Governance", 

443 config=_links( 

444 ("Rules", RULES), 

445 ("Activity", GOVERNANCE_ACTIVITY), 

446 ("Reviews", REVIEWS), 

447 ("Evaluation", EVALUATION), 

448 ), 

449 x=6, 

450 y=9, 

451 w=6, 

452 h=5, 

453 ), 

454 ), 

455) 

456 

457 

458# ── Knowledge ───────────────────────────────────────────────────────────────── 

459 

460KNOWLEDGE_BOARD = SystemDashboardSpec( 

461 id=_board_id(4), 

462 name="Knowledge", 

463 description=( 

464 "What your agents can reach and where it comes from — repositories, " 

465 "sources, website link graphs and entity graphs, with the sources no " 

466 "agent reaches called out." 

467 ), 

468 icon="Library", 

469 accent="sky", 

470 position=3, 

471 widgets=( 

472 SystemWidgetSpec( 

473 # The picture the rest of the board zooms into: agents on the 

474 # left, what they are granted in the middle, sources on the right. 

475 id=_widget_id(4, 1), 

476 widget_type=WidgetType.knowledge_network, 

477 title="Who can reach what", 

478 config={ 

479 "agent_ids": [], 

480 "include_memory": False, 

481 "include_unconnected": True, 

482 }, 

483 x=0, 

484 y=0, 

485 w=8, 

486 h=10, 

487 ), 

488 SystemWidgetSpec( 

489 id=_widget_id(4, 2), 

490 widget_type=WidgetType.breakdown, 

491 title="Knowledge searches this month", 

492 config=_breakdown( 

493 "invocations", 

494 "this_month", 

495 filters=("execution_type:retrieval",), 

496 ), 

497 x=8, 

498 y=0, 

499 w=4, 

500 h=3, 

501 ), 

502 SystemWidgetSpec( 

503 id=_widget_id(4, 3), 

504 widget_type=WidgetType.resource_list, 

505 title="Knowledge sources", 

506 config={"resource": "knowledge", "limit": 8}, 

507 x=8, 

508 y=3, 

509 w=4, 

510 h=7, 

511 ), 

512 SystemWidgetSpec( 

513 # Empty ids draw the viewer's newest source of each kind, and each 

514 # tile's caption names it. 

515 id=_widget_id(4, 4), 

516 widget_type=WidgetType.web_link_graph, 

517 title="Website link graph", 

518 config={"knowledge_id": "", "mode": "paths"}, 

519 x=0, 

520 y=10, 

521 w=6, 

522 h=9, 

523 ), 

524 SystemWidgetSpec( 

525 id=_widget_id(4, 5), 

526 widget_type=WidgetType.knowledge_graph, 

527 title="Entity graph", 

528 config={"knowledge_id": ""}, 

529 x=6, 

530 y=10, 

531 w=6, 

532 h=9, 

533 ), 

534 ), 

535) 

536 

537 

538# ── Usage & spend ───────────────────────────────────────────────────────────── 

539 

540USAGE_AND_SPEND = SystemDashboardSpec( 

541 id=_board_id(5), 

542 name="Usage & spend", 

543 description=( 

544 "Spend, tokens and errors at a glance, with where the money went by " 

545 "model and by what you ran." 

546 ), 

547 icon="Activity", 

548 accent="emerald", 

549 position=4, 

550 widgets=( 

551 SystemWidgetSpec( 

552 id=_widget_id(5, 1), 

553 widget_type=WidgetType.stat, 

554 title="Spend this month", 

555 config={"metric": "estimated_cost_usd", "period": "this_month"}, 

556 x=0, 

557 y=0, 

558 w=3, 

559 h=3, 

560 ), 

561 SystemWidgetSpec( 

562 id=_widget_id(5, 2), 

563 widget_type=WidgetType.stat, 

564 title="Tokens this month", 

565 config={"metric": "total_tokens", "period": "this_month"}, 

566 x=3, 

567 y=0, 

568 w=3, 

569 h=3, 

570 ), 

571 SystemWidgetSpec( 

572 id=_widget_id(5, 3), 

573 widget_type=WidgetType.stat, 

574 title="Errors this month", 

575 config={"metric": "error_count", "period": "this_month"}, 

576 x=6, 

577 y=0, 

578 w=3, 

579 h=3, 

580 ), 

581 SystemWidgetSpec( 

582 id=_widget_id(5, 4), 

583 widget_type=WidgetType.stat, 

584 title="Tool calls this month", 

585 config={"metric": "tool_invocation_count", "period": "this_month"}, 

586 x=9, 

587 y=0, 

588 w=3, 

589 h=3, 

590 ), 

591 SystemWidgetSpec( 

592 id=_widget_id(5, 5), 

593 widget_type=WidgetType.chart, 

594 title="Token usage", 

595 config={ 

596 "source": "llm_invocations", 

597 "metric": "total_tokens", 

598 "chart_type": "area", 

599 "limit": 50, 

600 }, 

601 x=0, 

602 y=3, 

603 w=12, 

604 h=6, 

605 ), 

606 SystemWidgetSpec( 

607 id=_widget_id(5, 6), 

608 widget_type=WidgetType.breakdown, 

609 title="Spend by model", 

610 config=_breakdown( 

611 "estimated_cost_usd", 

612 "this_month", 

613 dimension="model", 

614 display="bar", 

615 limit=6, 

616 ), 

617 x=0, 

618 y=9, 

619 w=6, 

620 h=6, 

621 ), 

622 SystemWidgetSpec( 

623 id=_widget_id(5, 7), 

624 widget_type=WidgetType.breakdown, 

625 title="Spend by what you ran", 

626 config=_breakdown( 

627 "estimated_cost_usd", 

628 "this_month", 

629 dimension="source", 

630 display="list", 

631 limit=8, 

632 ), 

633 x=6, 

634 y=9, 

635 w=6, 

636 h=6, 

637 ), 

638 ), 

639) 

640 

641 

642#: In the order the Dashboards page lists them. 

643# ── Model & agent assurance ─────────────────────────────────────────────────── 

644# 

645# The platform benchmark is the same for every user — CHAIN-R runs CHAINR Bot 

646# and each maker's strongest model on the gold datasets — so most of this board shows 

647# everybody one picture. "Your agents" is the viewer's own workspace, like 

648# every other platform tile. 

649 

650 

651def _assurance(view: str) -> dict[str, Any]: 

652 """An assurance config, as ``AssuranceWidget`` validates it.""" 

653 return {"view": view} 

654 

655 

656MODEL_AND_AGENT_ASSURANCE = SystemDashboardSpec( 

657 id=_board_id(6), 

658 name="Model & agent assurance", 

659 description=( 

660 "How CHAINR Bot and each maker's strongest model score on the gold datasets — " 

661 "rankings, risk, scores by dataset and the bot's record by release — " 

662 "and which of your agents have passing evidence behind them." 

663 ), 

664 icon="Scale", 

665 accent="indigo", 

666 position=5, 

667 widgets=( 

668 SystemWidgetSpec( 

669 id=_widget_id(6, 1), 

670 widget_type=WidgetType.assurance, 

671 title="Leaderboard", 

672 config=_assurance("leaderboard"), 

673 x=0, 

674 y=0, 

675 w=7, 

676 h=8, 

677 ), 

678 SystemWidgetSpec( 

679 id=_widget_id(6, 2), 

680 widget_type=WidgetType.assurance, 

681 title="Average against risk", 

682 config=_assurance("risk_return"), 

683 x=7, 

684 y=0, 

685 w=5, 

686 h=8, 

687 ), 

688 SystemWidgetSpec( 

689 id=_widget_id(6, 3), 

690 widget_type=WidgetType.assurance, 

691 title="Scores by gold dataset", 

692 config=_assurance("scores"), 

693 x=0, 

694 y=8, 

695 w=12, 

696 h=10, 

697 ), 

698 SystemWidgetSpec( 

699 id=_widget_id(6, 4), 

700 widget_type=WidgetType.assurance, 

701 title="Your agents", 

702 config=_assurance("fleet"), 

703 x=0, 

704 y=18, 

705 w=6, 

706 h=8, 

707 ), 

708 SystemWidgetSpec( 

709 id=_widget_id(6, 5), 

710 widget_type=WidgetType.assurance, 

711 title="CHAINR Bot, release by release", 

712 config=_assurance("bot_trend"), 

713 x=6, 

714 y=18, 

715 w=6, 

716 h=8, 

717 ), 

718 SystemWidgetSpec( 

719 id=_widget_id(6, 6), 

720 widget_type=WidgetType.shortcuts, 

721 title="Evaluation", 

722 config=_links( 

723 ("Assurance", ASSURANCE), 

724 ("Gold datasets", DATASETS), 

725 ("Reports", EVALUATION_REPORTS), 

726 ("Ask CHAINR Bot", BOT_CHAT), 

727 ), 

728 x=0, 

729 y=26, 

730 w=12, 

731 h=3, 

732 ), 

733 ), 

734) 

735 

736 

737SYSTEM_DASHBOARDS: tuple[SystemDashboardSpec, ...] = ( 

738 DAILY_BRIEFING, 

739 AGENT_FLEET, 

740 GOVERNANCE, 

741 KNOWLEDGE_BOARD, 

742 USAGE_AND_SPEND, 

743 MODEL_AND_AGENT_ASSURANCE, 

744)