AQIT 0.1.0
Loading...
Searching...
No Matches
session_mode.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"""LLM session mode — controls which tools exist in a CLI session."""
7from __future__ import annotations
8
9from typing import Literal
10
11SessionMode = Literal["llm"]
12
13# ── Tool registry names ───────────────────────────────────────────────────────
14
15SHARED_TOOLS: frozenset[str] = frozenset({
16 "write_session_memory",
17 "read_session_memory",
18 "run_custom_eval",
19 "run_sae_stats",
20})
21
22# Tools that never touch the loaded LLM weights — skip ensure_model in dispatch.
23MODEL_FREE_TOOLS: frozenset[str] = frozenset({
24 "read_session_memory",
25 "write_session_memory",
26 "list_simulation_runs",
27 "load_simulation_run",
28 "compare_simulations",
29})
30
31
32def tool_requires_model(tool_name: str) -> bool:
33 return tool_name not in MODEL_FREE_TOOLS
34
35LLM_TOOLS: frozenset[str] = frozenset({
36 "run_full_inspection",
37 "run_benchmarks_on_top_feature",
38 "run_find_feature",
39 "run_audit",
40 "run_consistency_eval",
41 "run_suppression_eval",
42 "run_boundary_eval",
43 "get_feature_logits",
44 "get_feature_neighbors",
45 "run_steer_and_show",
46 "extract_steer_vector",
47 "run_multi_steer",
48 "ensure_umap_loaded",
49 "run_layer_analysis",
50 "run_confidence_analysis",
51 "run_weight_diff",
52 "run_merge_analysis",
53 "run_trajectory_analysis",
54 "run_residual_drift",
55 "run_sae_stats",
56 "run_perturbation_sensitivity",
57 "run_attention_routing",
58 "dataset_generate",
59 "run_simulation",
60 "list_simulation_runs",
61 "load_simulation_run",
62 "compare_simulations",
63 "run_red_team",
64 "check_weights",
65})
66
67# CLI-only shortcuts (not in tool registry)
68LLM_CLI_ONLY: frozenset[str] = frozenset({"trace", "feature logit", "feature neighbor"})
69
70# ── CLI verb → tool registry ─────────────────────────────────────────────────
71
72SHARED_CLI_VERBS: dict[str, str] = {
73 "feature locate": "run_find_feature",
74 "feature logit": "get_feature_logits",
75 "feature neighbor": "get_feature_neighbors",
76 "sae-stats": "run_sae_stats",
77 "diff weight": "run_merge_analysis",
78 "check trajectory": "run_trajectory_analysis",
79 "diff residue": "run_residual_drift",
80 "confidence-analysis": "run_confidence_analysis",
81 "eval": "run_custom_eval",
82 "list simulation": "list_simulation_runs",
83 "replay simulation": "load_simulation_run",
84 "compare simulation": "compare_simulations",
85 "list-runs": "list_simulation_runs",
86 "load-run": "load_simulation_run",
87 "compare-runs": "compare_simulations",
88}
89
90LLM_CLI_VERBS: dict[str, str] = {
91 "benchmark": "run_benchmarks_on_top_feature",
92 "benchmarks": "run_benchmarks_on_top_feature",
93 "consistency-eval": "run_consistency_eval",
94 "suppression-eval": "run_suppression_eval",
95 "boundary-eval": "run_boundary_eval",
96 "steer": "run_steer_and_show",
97 "multi-steer": "run_multi_steer",
98 "layer-analysis": "run_layer_analysis",
99 "perturbation": "run_perturbation_sensitivity",
100 "attention": "run_attention_routing",
101 "check-weights": "check_weights",
102 "simulate": "run_simulation",
103 "red-team": "run_red_team",
104}
105
106
107def resolve_legacy_verb(verb: str) -> str:
108 return verb
109
110
111def cli_verbs_for_mode(mode: SessionMode | None) -> dict[str, str]:
112 """CLI verb map for the active session mode."""
113 return {**SHARED_CLI_VERBS, **LLM_CLI_VERBS}
114
116def verb_known(verb: str) -> bool:
117 verb = resolve_legacy_verb(verb)
118 return verb in SHARED_CLI_VERBS or verb in LLM_CLI_VERBS
119
121def normalize_mode(raw: str | None) -> SessionMode:
122 return "llm"
123
124
125def tools_for_mode(mode: SessionMode) -> frozenset[str]:
126 return LLM_TOOLS | SHARED_TOOLS
127
128
129def is_tool_allowed(tool_name: str, mode: SessionMode) -> bool:
130 return tool_name in tools_for_mode(mode)
131
132
133def filter_cli_tool_map(full_map: dict[str, str], mode: SessionMode) -> dict[str, str]:
134 allowed = tools_for_mode(mode)
135 return {verb: tool for verb, tool in full_map.items() if tool in allowed}
136
138def get_filtered_tool_schemas(mode: SessionMode) -> list[dict]:
139 from aquin.engine.tools.registry import get_tool_schemas
140
141 allowed = tools_for_mode(mode)
142 return [s for s in get_tool_schemas() if s.get("function", {}).get("name") in allowed]
143
144
145def mode_label(mode: SessionMode) -> str:
146 return "LLM"
147
148
149def mode_for_model_id(model_id: str) -> SessionMode:
150 """Validate model id and return LLM session mode."""
151 from aquin.compute.model_loader import resolve_model_id
152
153 resolve_model_id(model_id) # raises if unknown LLM
154 return "llm"
155
156
157def mode_for_active_model() -> SessionMode | None:
158 """Infer mode from the model last loaded via `aquin load` (~/.aquin/active_model.txt)."""
159 from aquin.compute.model_loader import get_active_model_id
160
161 model_id = get_active_model_id()
162 if not model_id:
163 return None
164 try:
165 from aquin.compute.model_loader import resolve_model_id
166 resolve_model_id(model_id)
167 return "llm"
168 except ValueError:
169 return None
dict[str, str] cli_verbs_for_mode(SessionMode|None mode)
bool verb_known(str verb)
frozenset[str] tools_for_mode(SessionMode mode)
bool tool_requires_model(str tool_name)
bool is_tool_allowed(str tool_name, SessionMode mode)
SessionMode|None mode_for_active_model()
list[dict] get_filtered_tool_schemas(SessionMode mode)
str mode_label(SessionMode mode)
dict[str, str] filter_cli_tool_map(dict[str, str] full_map, SessionMode mode)
SessionMode mode_for_model_id(str model_id)
str resolve_legacy_verb(str verb)
SessionMode normalize_mode(str|None raw)