fix: persist agent permission edits
Prevents permission changes from being overwritten by other agent fields Writes built-in and custom agent permissions to the correct config source Refreshes agent state after permission saves
This commit is contained in:
@@ -122,6 +122,10 @@ const filterRulesAgainstGlobal = (ruleset: PermissionRule[], globalAction: Permi
|
||||
);
|
||||
|
||||
const permissionConfigToRuleset = (value: unknown): PermissionRule[] => {
|
||||
if (Array.isArray(value)) {
|
||||
return normalizeRuleset(value as PermissionRule[]);
|
||||
}
|
||||
|
||||
if (isPermissionAction(value)) {
|
||||
return [{ permission: '*', pattern: '*', action: value }];
|
||||
}
|
||||
@@ -162,9 +166,7 @@ const buildPermissionConfigWithGlobal = (
|
||||
(grouped[rule.permission] ||= {})[rule.pattern] = rule.action;
|
||||
}
|
||||
|
||||
const result: Record<string, PermissionConfigValue> = {
|
||||
'*': globalAction,
|
||||
};
|
||||
const result: Record<string, PermissionConfigValue> = {};
|
||||
|
||||
for (const [permissionName, patterns] of Object.entries(grouped)) {
|
||||
if (permissionName === '*') {
|
||||
@@ -179,6 +181,14 @@ const buildPermissionConfigWithGlobal = (
|
||||
result[permissionName] = patterns;
|
||||
}
|
||||
|
||||
if (Object.keys(result).length === 0) {
|
||||
return globalAction;
|
||||
}
|
||||
|
||||
if (globalAction !== 'allow') {
|
||||
result['*'] = globalAction;
|
||||
}
|
||||
|
||||
return result as AgentConfig['permission'];
|
||||
};
|
||||
|
||||
@@ -273,7 +283,7 @@ export const AgentsPage: React.FC = () => {
|
||||
const names = new Set<string>();
|
||||
|
||||
for (const agent of agents) {
|
||||
const rules = normalizeRuleset(Array.isArray(agent.permission) ? agent.permission as PermissionRule[] : []);
|
||||
const rules = normalizeRuleset(permissionConfigToRuleset(agent.permission));
|
||||
for (const rule of rules) {
|
||||
if (rule.permission && rule.permission !== '*' && rule.permission !== 'invalid') {
|
||||
names.add(rule.permission);
|
||||
@@ -509,7 +519,7 @@ export const AgentsPage: React.FC = () => {
|
||||
setPrompt(promptValue);
|
||||
|
||||
const permissionState = applyPermissionState(
|
||||
Array.isArray(selectedAgent.permission) ? selectedAgent.permission as PermissionRule[] : [],
|
||||
permissionConfigToRuleset(selectedAgent.permission),
|
||||
);
|
||||
|
||||
initialStateRef.current = {
|
||||
|
||||
@@ -70,12 +70,24 @@ const getAgentsCacheKey = (directory: string | null): string => {
|
||||
return directory?.trim() || DEFAULT_AGENTS_CACHE_KEY;
|
||||
};
|
||||
|
||||
const invalidateAgentsLoadCache = (directory: string | null = getConfigDirectory()) => {
|
||||
agentsLastLoadedAt.delete(getAgentsCacheKey(directory));
|
||||
};
|
||||
|
||||
const buildAgentsSignature = (agents: Agent[]): string => {
|
||||
return agents
|
||||
.map((agent) => {
|
||||
const extended = agent as AgentWithExtras;
|
||||
return [
|
||||
agent.name,
|
||||
extended.mode ?? '',
|
||||
typeof extended.model === 'object' && extended.model
|
||||
? `${extended.model.providerID ?? ''}/${extended.model.modelID ?? ''}`
|
||||
: String(extended.model ?? ''),
|
||||
String(extended.temperature ?? ''),
|
||||
String((extended as { topP?: unknown; top_p?: unknown }).topP ?? (extended as { topP?: unknown; top_p?: unknown }).top_p ?? ''),
|
||||
extended.prompt ?? '',
|
||||
JSON.stringify(extended.permission ?? null),
|
||||
extended.scope ?? '',
|
||||
extended.group ?? '',
|
||||
extended.description ?? '',
|
||||
@@ -345,6 +357,7 @@ export const useAgentsStore = create<AgentsStore>()(
|
||||
}
|
||||
|
||||
const needsReload = payload?.requiresReload ?? true;
|
||||
invalidateAgentsLoadCache(configDirectory);
|
||||
if (needsReload) {
|
||||
requiresReload = true;
|
||||
await refreshAfterOpenCodeRestart({
|
||||
@@ -406,6 +419,7 @@ export const useAgentsStore = create<AgentsStore>()(
|
||||
}
|
||||
|
||||
const needsReload = payload?.requiresReload ?? true;
|
||||
invalidateAgentsLoadCache(configDirectory);
|
||||
if (needsReload) {
|
||||
requiresReload = true;
|
||||
await refreshAfterOpenCodeRestart({
|
||||
@@ -452,6 +466,7 @@ export const useAgentsStore = create<AgentsStore>()(
|
||||
}
|
||||
|
||||
const needsReload = payload?.requiresReload ?? true;
|
||||
invalidateAgentsLoadCache(configDirectory);
|
||||
if (needsReload) {
|
||||
requiresReload = true;
|
||||
await refreshAfterOpenCodeRestart({
|
||||
@@ -629,6 +644,7 @@ async function performConfigRefresh(options: {
|
||||
|
||||
const uiRefreshTasks: Promise<void>[] = [];
|
||||
if (refreshAgentConfigs) {
|
||||
invalidateAgentsLoadCache(currentDirectory);
|
||||
uiRefreshTasks.push(agentConfigStore.loadAgents().then(() => undefined));
|
||||
}
|
||||
if (refreshCommands) {
|
||||
|
||||
Reference in New Issue
Block a user