Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
fix(control-ui): preserve only assigned orphan scopes
Miguelon Peluchon7bf0e9e10f8f
diff --git a/control-ui/app/profile-admin/teams/[teamId]/page.tsx b/control-ui/app/profile-admin/teams/[teamId]/page.tsxindex 4a775ad38..685c3dd73 100644--- a/control-ui/app/profile-admin/teams/[teamId]/page.tsx+++ b/control-ui/app/profile-admin/teams/[teamId]/page.tsx@@ -10,6 +10,7 @@ import { TeamRolePermissionEditor } from '@components/TeamRolePermissionEditor'import { useToast } from '@components/Toast'import { CONTROL_ROUTES } from '@constants/routes'import { partitionVisibleAccess } from '@lib/accessVisibility'+import { planAgentAccessUpdate } from '@lib/agentAccessCompatibility'import { getAgentDisplayName } from '@lib/agentName'import { ConnectorCountCell } from '../../../../components/ConnectorCountCell'import { InviteMemberDialog } from '../../../../components/InviteMemberDialog'@@ -531,33 +532,13 @@ export default function TeamDetailsPage() {setBusy(true)setError('')try {- const normalizedAgents = Array.from(- new Set(nextGrantedAgents.map(v => v.trim()).filter(Boolean))- ).sort((a, b) => a.localeCompare(b))-- const nextRefs = new Set<string>()- for (const agentName of normalizedAgents) {- const ref = agentContextRef(agentName)- if (ref) nextRefs.add(ref)- }- const nextContextIds = availableContextIds.filter(contextId => {- const owned = [...hostsByName.values()].some(- host =>- String((host.spec as { contextRef?: string } | undefined)?.contextRef || '').trim() ===- contextId- )- return owned ? nextRefs.has(contextId) : true- })- for (const ref of nextRefs) {- if (!nextContextIds.includes(ref)) nextContextIds.push(ref)- }+ const updatePlan = planAgentAccessUpdate(assignedContextIds, nextGrantedAgents, [+ ...hostsByName.values(),+ ])const [updatedAgents, updatedContexts] = await Promise.all([- updateAdminTeamAgents(teamId, normalizedAgents, observedAgentNames),- updateAdminTeamContexts(- teamId,- nextContextIds.sort((a, b) => a.localeCompare(b))- ),+ updateAdminTeamAgents(teamId, updatePlan.agentNames, observedAgentNames),+ updateAdminTeamContexts(teamId, updatePlan.contextIds),])const hostNames = [...hostsByName.keys()]diff --git a/control-ui/app/profile-admin/users/[userId]/page.tsx b/control-ui/app/profile-admin/users/[userId]/page.tsxindex 05e52011d..ec3db72ae 100644--- a/control-ui/app/profile-admin/users/[userId]/page.tsx+++ b/control-ui/app/profile-admin/users/[userId]/page.tsx@@ -12,6 +12,7 @@ import { CheckboxField } from '@components/ui'import { CONTROL_ROUTES } from '@constants/routes'import { accessScopeLabeler } from '@lib/accessScopeLabels'import { partitionVisibleAccess } from '@lib/accessVisibility'+import { planAgentAccessUpdate } from '@lib/agentAccessCompatibility'import { getAgentDisplayName } from '@lib/agentName'import type { DeleteCandidateTeam } from '@lib/profileAdminDelete'import { formatTeamNames, getSoloMemberTeamsForUser } from '@lib/profileAdminDelete'@@ -455,38 +456,13 @@ export default function UserDetailsPage() {setBusy(true)setError('')try {- const normalizedAgents = Array.from(- new Set(nextGrantedAgents.map(v => v.trim()).filter(Boolean))- ).sort((a, b) => a.localeCompare(b))-- // Scopes: keep legacy ids that map to no known agent; add the scopes of- // newly granted agents; drop the scopes of revoked agents unless- // another still-granted agent shares them.- const nextRefs = new Set<string>()- for (const agentName of normalizedAgents) {- const ref = agentContextRef(agentName)- if (ref) nextRefs.add(ref)- }- const nextContextIds = availableContextIds.filter(contextId => {- const owner = [...hostsByName.values()].some(- host =>- String((host.spec as { contextRef?: string } | undefined)?.contextRef || '').trim() ===- contextId- )- // Keep legacy scope-only mappings untouched; managed ones follow the- // agent grant state.- return owner ? nextRefs.has(contextId) : true- })- for (const ref of nextRefs) {- if (!nextContextIds.includes(ref)) nextContextIds.push(ref)- }+ const updatePlan = planAgentAccessUpdate(assignedContextIds, nextGrantedAgents, [+ ...hostsByName.values(),+ ])const [updatedAgents, updatedContexts] = await Promise.all([- updateAdminUserAgents(userId, normalizedAgents, observedAgentNames),- updateAdminUserContexts(- userId,- nextContextIds.sort((a, b) => a.localeCompare(b))- ),+ updateAdminUserAgents(userId, updatePlan.agentNames, observedAgentNames),+ updateAdminUserContexts(userId, updatePlan.contextIds),])const hostNames = [...hostsByName.keys()]diff --git a/control-ui/lib/__tests__/agentAccessCompatibility.test.ts b/control-ui/lib/__tests__/agentAccessCompatibility.test.tsnew file mode 100644index 000000000..90fe66d5a--- /dev/null+++ b/control-ui/lib/__tests__/agentAccessCompatibility.test.ts@@ -0,0 +1,49 @@+import { describe, expect, it } from 'vitest'+import { planAgentAccessUpdate } from '../agentAccessCompatibility'+import type { HostResource } from '../api'++const hosts: HostResource[] = [+ { metadata: { name: 'agent-alpha' }, spec: { contextRef: 'ctx-alpha' } },+ { metadata: { name: 'agent-beta' }, spec: { contextRef: 'ctx-beta' } },+]++describe('planAgentAccessUpdate', () => {+ it('keeps only the member’s already-assigned unowned scopes', () => {+ const result = planAgentAccessUpdate(['member-private-scope'], ['agent-alpha'], hosts)++ expect(result).toEqual({+ agentNames: ['agent-alpha'],+ contextIds: ['ctx-alpha', 'member-private-scope'],+ })+ expect(result.contextIds).not.toContain('unrelated-unowned-scope')+ })++ it('keeps only the team’s already-assigned unowned scopes', () => {+ const result = planAgentAccessUpdate([], ['agent-beta'], hosts)++ expect(result).toEqual({+ agentNames: ['agent-beta'],+ contextIds: ['ctx-beta'],+ })+ expect(result.contextIds).not.toContain('unrelated-unowned-scope')+ })++ it('drops a revoked Agent scope while retaining a shared selected scope once', () => {+ const sharedHosts: HostResource[] = [+ { metadata: { name: 'agent-alpha' }, spec: { contextRef: 'ctx-shared' } },+ { metadata: { name: 'agent-beta' }, spec: { contextRef: 'ctx-shared' } },+ { metadata: { name: 'agent-revoked' }, spec: { contextRef: 'ctx-revoked' } },+ ]++ expect(+ planAgentAccessUpdate(+ ['ctx-shared', 'ctx-revoked'],+ ['agent-beta', 'agent-alpha'],+ sharedHosts+ )+ ).toEqual({+ agentNames: ['agent-alpha', 'agent-beta'],+ contextIds: ['ctx-shared'],+ })+ })+})diff --git a/control-ui/lib/agentAccessCompatibility.ts b/control-ui/lib/agentAccessCompatibility.tsnew file mode 100644index 000000000..a7cb42abf--- /dev/null+++ b/control-ui/lib/agentAccessCompatibility.ts@@ -0,0 +1,50 @@+import type { HostResource } from './api'++export type AgentAccessUpdatePlan = {+ agentNames: string[]+ contextIds: string[]+}++function hostName(host: HostResource): string {+ return String(host.metadata?.name ?? '').trim()+}++function hostContextRef(host: HostResource): string {+ return String(host.spec?.contextRef ?? '').trim()+}++/**+ * Plans the compatibility payload for an Agent-centric access update.+ *+ * Context mappings remain a runtime compatibility write, but the UI no longer+ * manages arbitrary Contexts. Preserve only unowned scopes that were already+ * assigned, then derive every Host-owned scope from the requested Agent set.+ * Never copy unrelated unowned scopes from the global Context inventory.+ */+export function planAgentAccessUpdate(+ assignedContextIds: readonly string[],+ nextGrantedAgentNames: readonly string[],+ hosts: readonly HostResource[]+): AgentAccessUpdatePlan {+ const normalizedAgents = Array.from(+ new Set(nextGrantedAgentNames.map(value => value.trim()).filter(Boolean))+ ).sort((left, right) => left.localeCompare(right))+ const ownedContextIds = new Set(hosts.map(hostContextRef).filter(Boolean))+ const preservedUnownedContextIds = assignedContextIds.filter(+ contextId => contextId && !ownedContextIds.has(contextId)+ )+ const hostByName = new Map(+ hosts.map(host => [hostName(host), host] as const).filter(([name]) => Boolean(name))+ )+ const selectedAgentContextIds = normalizedAgents+ .map(agentName => hostByName.get(agentName))+ .map(host => (host ? hostContextRef(host) : ''))+ .filter(Boolean)++ return {+ agentNames: normalizedAgents,+ contextIds: Array.from(+ new Set([...preservedUnownedContextIds, ...selectedAgentContextIds])+ ).sort((left, right) => left.localeCompare(right)),+ }+}diff --git a/control-ui/package-lock.json b/control-ui/package-lock.jsonindex 28b0f3e26..f8484046b 100644--- a/control-ui/package-lock.json+++ b/control-ui/package-lock.json@@ -1,12 +1,12 @@{"name": "clerum-control-ui",- "version": "0.1.489",+ "version": "0.1.490","lockfileVersion": 3,"requires": true,"packages": {"": {"name": "clerum-control-ui",- "version": "0.1.489",+ "version": "0.1.490","dependencies": {"@clerum/display-field": "file:../packages/display-field","@clerum/llm-providers": "file:../packages/llm-providers",diff --git a/control-ui/package.json b/control-ui/package.jsonindex bb8fb301e..8ec8bbf01 100644--- a/control-ui/package.json+++ b/control-ui/package.json@@ -1,6 +1,6 @@{"name": "clerum-control-ui",- "version": "0.1.489",+ "version": "0.1.490","private": true,"scripts": {"dev": "NEXT_IGNORE_INCORRECT_LOCKFILE=1 next dev",