DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.
Python ai-agentsai-tutorclawbotcli-tooldeepresearchinteractive-learninglearningmulti-agent-systemsrag
refactor(multi-user): consolidate partner-turn detection + align MCP grant UI
Bingxi Zhao (Frank)5eafa0c2fb71
diff --git a/deeptutor/agents/chat/agentic_pipeline.py b/deeptutor/agents/chat/agentic_pipeline.pyindex 061e15c92..370684652 100644--- a/deeptutor/agents/chat/agentic_pipeline.py+++ b/deeptutor/agents/chat/agentic_pipeline.py@@ -429,24 +429,36 @@ class AgenticChatPipeline:# ---- deferred tools / tool composition ------------------------------+ @staticmethod+ def _is_partner_turn(context: UnifiedContext) -> bool:+ """Whether this turn runs under a partner's synthetic scope.++ A partner turn executes as a synthetic non-admin user but acts as the+ admin owner's extension. Authorization for these turns travels through+ context metadata (the owner-scoped ``mcp_tools_filter`` / exec gate),+ not the synthetic user's grant file — so callers must bypass real-user+ grant resolution and defer to that metadata whitelist instead.+ """+ return str((context.metadata or {}).get("source") or "") == "partner"+async def _prepare_deferred_tools(self, context: UnifiedContext) -> None:try:from deeptutor.services.mcp import get_mcp_manager, load_loaded_toolsawait get_mcp_manager().ensure_started()# Caller-scoped whitelist (e.g. a partner's configured MCP tools)- # intersected with the current user's admin grant. ``None`` means+ # intersected with the current user's grant. ``None`` means# unrestricted; a set narrows the deferred tools. Real non-admin# users fail closed when no MCP grant is present, while partner- # turns use their owner-scoped metadata whitelist as the authority.+ # turns defer to their owner-scoped metadata whitelist as the+ # authority (see ``_is_partner_turn``).from deeptutor.multi_user.tool_access import allowed_mcp_tools, combine_whitelistsraw_filter = context.metadata.get("mcp_tools_filter")caller_allowed = ({str(name) for name in raw_filter} if isinstance(raw_filter, list) else None)- is_partner = str((context.metadata or {}).get("source")) == "partner"- user_allowed = None if is_partner else allowed_mcp_tools()+ user_allowed = None if self._is_partner_turn(context) else allowed_mcp_tools()allowed: set[str] | None = combine_whitelists(caller_allowed, user_allowed)pool = self.registry.deferred_tools()if allowed is not None:@@ -481,7 +493,7 @@ class AgenticChatPipeline:# owner's extension (partners are anchored to the admin workspace), so# exec follows the owner's authority — not the partner's "user" role.# The owner still gates exec per-partner via the builtin-tool whitelist.- is_partner = str((context.metadata or {}).get("source")) == "partner"+ is_partner = self._is_partner_turn(context)level = await get_sandbox_service().isolation_level()if level is IsolationLevel.SYSTEM:@@ -507,7 +519,7 @@ class AgenticChatPipeline:return Falsedef _compose_enabled_tools(self, context: UnifiedContext) -> list[str]:- is_partner = str((context.metadata or {}).get("source")) == "partner"+ is_partner = self._is_partner_turn(context)composed = compose_enabled_tools(registry=self.registry,requested_tools=context.enabled_tools,@@ -886,7 +898,7 @@ class AgenticChatPipeline:kwargs["_cron_in_context"] = bool(cron_job_id or str(meta.get("source") or "") == "cron")- if str(meta.get("source") or "") == "partner":+ if self._is_partner_turn(context):channel_meta = meta.get("channel_metadata")kwargs["_cron_owner"] = {"kind": "partner",diff --git a/web/features/multi-user/components/GrantEditor.tsx b/web/features/multi-user/components/GrantEditor.tsxindex fd593d256..eb89f0fe3 100644--- a/web/features/multi-user/components/GrantEditor.tsx+++ b/web/features/multi-user/components/GrantEditor.tsx@@ -74,17 +74,24 @@ function CheckRow({);}-/** Default-vs-custom switch for a whitelist field (null = default/all). */+/**+ * Default-vs-custom switch for a whitelist field. ``null`` selects the+ * "default" mode; what that resolves to server-side depends on the field —+ * built-in tools default to *all*, MCP tools default to *none* (deny until+ * explicitly granted) — so the default-mode label is caller-supplied.+ */function ModeSwitch({isCustom,disabled,onDefault,onCustom,+ defaultLabel = "Default · all",}: {isCustom: boolean;disabled: boolean;onDefault: () => void;onCustom: () => void;+ defaultLabel?: string;}) {const base ="rounded-md px-2 py-0.5 text-[11px] font-medium transition-colors disabled:cursor-not-allowed disabled:opacity-45";@@ -100,7 +107,7 @@ function ModeSwitch({: "text-[var(--muted-foreground)] hover:text-[var(--foreground)]"}`}>- Default · all+ {defaultLabel}</button><buttontype="button"@@ -293,8 +300,10 @@ export function GrantEditor({ userId }: { userId: string }) {grant.enabled_tools === null? "all tools": `${grant.enabled_tools.length} tools`;+ // MCP tools deny-by-default for non-admin users: ``null`` grants none until+ // the admin switches to Custom and picks specific tool names.const mcpSummary =- grant.mcp_tools === null ? "all MCP" : `${grant.mcp_tools.length} MCP`;+ grant.mcp_tools === null ? "no MCP" : `${grant.mcp_tools.length} MCP`;const mcpByServer = useMemo(() => {const groups = new Map<string, McpToolOption[]>();@@ -451,6 +460,7 @@ export function GrantEditor({ userId }: { userId: string }) {<ModeSwitchisCustom={grant.mcp_tools !== null}disabled={controlsDisabled}+ defaultLabel="Default · none"onDefault={() => setToolList("mcp_tools", null)}onCustom={() =>setToolList(@@ -459,6 +469,12 @@ export function GrantEditor({ userId }: { userId: string }) {)}/>+ {grant.mcp_tools === null ? (+ <p className="px-1 text-[11px] leading-relaxed text-[var(--muted-foreground)]">+ MCP tools proxy host-side capabilities, so they stay denied by+ default. Switch to Custom to grant specific tools.+ </p>+ ) : null}{grant.mcp_tools !== null &&(resources?.mcp_tools?.length ? (<div className="space-y-2 text-xs">