AQIT 0.1.0
Loading...
Searching...
No Matches
sync_slim.py
Go to the documentation of this file.
1# Copyright (c) 2025-present Aquin Labs Private Limited. All Rights Reserved.
2# This file is part of the Aquin Engine. Unauthorized copying, modification,
3# distribution, or use of this file, via any medium, is strictly prohibited.
4# Proprietary and confidential. See LICENSE for terms.
5
6"""Trim large tool payloads before cloud sync."""
7from __future__ import annotations
8
9import random
10
11UMAP_WEB_MAX_POINTS = 8192
12
13
14def downsample_umap_points(points: list[dict], max_points: int = UMAP_WEB_MAX_POINTS) -> list[dict]:
15 n = len(points)
16 if n <= max_points:
17 return points
18 idx = sorted(random.Random(42).sample(range(n), max_points))
19 return [points[i] for i in idx]
20
21
22def slim_tool_result_for_sync(tool_name: str, result: dict, card: dict | None = None) -> dict:
23 """Drop bulky fields from synced tool.result (full data lives on card only)."""
24 if tool_name == "ensure_umap_loaded":
25 card_points = []
26 if card and isinstance(card.get("data"), dict):
27 card_points = card["data"].get("points") or []
28 return {
29 "model_id": result.get("model_id"),
30 "layer": result.get("layer"),
31 "n_features": result.get("n_features"),
32 "n_points": len(card_points) or result.get("n_points"),
33 }
34 if tool_name == "run_sae_diff" and card is not None:
35 return {
36 "baseModelId": result.get("baseModelId"),
37 "ftCheckpointName": result.get("ftCheckpointName"),
38 "layer": result.get("layer"),
39 "nFeatures": result.get("nFeatures"),
40 "nChanged": result.get("nChanged"),
41 "meanAbsDelta": result.get("meanAbsDelta"),
42 "maxAbsDelta": result.get("maxAbsDelta"),
43 }
44 if tool_name == "run_sae_align" and card is not None:
45 return {
46 "mean_cosine": result.get("mean_cosine"),
47 "n_pairs": result.get("n_pairs"),
48 }
49 if tool_name == "run_sae_train" and card is not None:
50 return {
51 "model_id": result.get("model_id"),
52 "layer": result.get("layer"),
53 "quick": result.get("quick"),
54 "name": result.get("name"),
55 "output_path": result.get("output_path"),
56 "status": result.get("status"),
57 }
58 if tool_name == "run_weight_diff" and card is not None:
59 return {
60 "baseModelId": result.get("baseModelId"),
61 "ftCheckpointName": result.get("ftCheckpointName"),
62 "modelMode": result.get("modelMode"),
63 "deltaMode": result.get("deltaMode"),
64 "nMatrices": result.get("nMatrices"),
65 "totalDeltaL2": result.get("totalDeltaL2"),
66 "maxDeltaL2": result.get("maxDeltaL2"),
67 }
68 if tool_name == "run_merge_analysis" and card is not None:
69 return {
70 "baseModelId": result.get("baseModelId"),
71 "ftCheckpointName": result.get("ftCheckpointName"),
72 "mergeVerdict": result.get("mergeVerdict"),
73 "nWarnings": len(result.get("warnings") or []),
74 "totalDeltaL2": result.get("totalDeltaL2"),
75 }
76 if tool_name == "run_trajectory_analysis" and card is not None:
77 return {
78 "baseModelId": result.get("baseModelId"),
79 "nCheckpoints": result.get("nCheckpoints"),
80 "nAnalyzed": result.get("nAnalyzed"),
81 "peakStep": result.get("peakStep"),
82 "peakTotalDeltaL2": result.get("peakTotalDeltaL2"),
83 }
84 if tool_name == "run_residual_drift" and card is not None:
85 return {
86 "baseModelId": result.get("baseModelId"),
87 "ftCheckpointName": result.get("ftCheckpointName"),
88 "modelMode": result.get("modelMode"),
89 "nProbes": result.get("nProbes"),
90 "meanDrift": result.get("meanDrift"),
91 "maxDrift": result.get("maxDrift"),
92 "peakLayer": result.get("peakLayer"),
93 }
94 if tool_name == "run_capture_activations" and card is not None:
95 return {
96 "model_id": result.get("model_id"),
97 "model_mode": result.get("model_mode"),
98 "n_probes": result.get("n_probes"),
99 "layers": result.get("layers"),
100 "position": result.get("position"),
101 "encode_sae": result.get("encode_sae"),
102 "manifest_path": result.get("manifest_path"),
103 "status": result.get("status"),
104 }
105 if tool_name == "run_find_feature" and card is not None:
106 return {
107 "model_id": result.get("model_id"),
108 "layer": result.get("layer"),
109 "scorer": result.get("scorer"),
110 "n_honest": result.get("n_honest"),
111 "n_deceptive": result.get("n_deceptive"),
112 "chosen_feature_idx": result.get("chosen_feature_idx"),
113 "chosen_delta": result.get("chosen_delta"),
114 "persisted_key": result.get("persisted_key"),
115 "status": result.get("status"),
116 }
117 return result
list[dict] downsample_umap_points(list[dict] points, int max_points=UMAP_WEB_MAX_POINTS)
Definition sync_slim.py:18
dict slim_tool_result_for_sync(str tool_name, dict result, dict|None card=None)
Definition sync_slim.py:26