feat: persist permission auto-accept on server (#2158)
Move per-session permission auto-accept policy ownership from the UI to the OpenChamber server so enabled sessions continue running when clients disconnect or the server restarts. - persist explicit per-session policies in OpenChamber settings - inherit the nearest explicit policy across subagent session hierarchies - allow child sessions to opt out of an inherited parent policy - immediately accept matching global and directory-scoped pending requests - process future requests without requiring a connected UI client - reconcile pending permissions after startup and event-stream reconnects - deduplicate concurrent requests and retry transient reply failures - synchronize policy updates across connected clients - migrate existing browser-persisted policies to server storage - suppress auto-accepted permission cards before they enter UI state - show deduplicated permission toasts for inactive sessions - preserve foreground-only permission handling in VS Code - integrate directory-aware notification routing from main - add coverage for persistence, inheritance, retries, reconciliation, pending requests, client hydration, and inactive-session toasts
This commit is contained in:
committed by
GitHub
parent
3d90eddcaf
commit
d738d41574
@@ -0,0 +1,48 @@
|
||||
import { describe, expect, test } from 'bun:test';
|
||||
import type { PermissionRequest } from '@/types/permission';
|
||||
import { showPermissionNeededToast } from './permission-toast';
|
||||
|
||||
const permission = {
|
||||
id: 'permission-1',
|
||||
sessionID: 'inactive-session',
|
||||
permission: 'bash',
|
||||
} as PermissionRequest;
|
||||
|
||||
describe('permission needed toast', () => {
|
||||
test('shows for an inactive session and opens that session', () => {
|
||||
const shown: Array<{ title: string; options: Parameters<Parameters<typeof showPermissionNeededToast>[0]['show']>[1] }> = [];
|
||||
const opened: Array<[string, string]> = [];
|
||||
const show: Parameters<typeof showPermissionNeededToast>[0]['show'] = (title, options) => { shown.push({ title, options }); };
|
||||
const openSession = (sessionId: string, directory: string) => { opened.push([sessionId, directory]); };
|
||||
const pendingIds = new Set<string>();
|
||||
|
||||
expect(showPermissionNeededToast({
|
||||
permission,
|
||||
directory: '/project',
|
||||
isViewed: false,
|
||||
pendingIds,
|
||||
show,
|
||||
openSession,
|
||||
})).toBe(true);
|
||||
|
||||
expect(shown.length).toBe(1);
|
||||
const { options } = shown[0];
|
||||
expect(options.id).toBe('permission-inactive-session:permission-1');
|
||||
expect(options.description).toBe('bash');
|
||||
options.action.onClick();
|
||||
expect(opened).toEqual([['inactive-session', '/project']]);
|
||||
});
|
||||
|
||||
test('does not show for the viewed session or duplicate a pending toast', () => {
|
||||
const shown: string[] = [];
|
||||
const show: Parameters<typeof showPermissionNeededToast>[0]['show'] = (title) => { shown.push(title); };
|
||||
const openSession: Parameters<typeof showPermissionNeededToast>[0]['openSession'] = () => {};
|
||||
const pendingIds = new Set<string>();
|
||||
const base = { permission, directory: '/project', pendingIds, show, openSession };
|
||||
|
||||
expect(showPermissionNeededToast({ ...base, isViewed: true })).toBe(false);
|
||||
expect(showPermissionNeededToast({ ...base, isViewed: false })).toBe(true);
|
||||
expect(showPermissionNeededToast({ ...base, isViewed: false })).toBe(false);
|
||||
expect(shown.length).toBe(1);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,45 @@
|
||||
import type { PermissionRequest } from '@/types/permission';
|
||||
|
||||
type PermissionToastOptions = {
|
||||
permission: PermissionRequest;
|
||||
directory: string;
|
||||
isViewed: boolean;
|
||||
pendingIds: Set<string>;
|
||||
show: (title: string, options: {
|
||||
id: string;
|
||||
description: string;
|
||||
action: { label: string; onClick: () => void };
|
||||
}) => void;
|
||||
openSession: (sessionId: string, directory: string) => void;
|
||||
};
|
||||
|
||||
export const getPermissionToastKey = (sessionId?: string, requestId?: string) => {
|
||||
if (!sessionId || !requestId) return null;
|
||||
return `${sessionId}:${requestId}`;
|
||||
};
|
||||
|
||||
export const showPermissionNeededToast = ({
|
||||
permission,
|
||||
directory,
|
||||
isViewed,
|
||||
pendingIds,
|
||||
show,
|
||||
openSession,
|
||||
}: PermissionToastOptions): boolean => {
|
||||
const key = getPermissionToastKey(permission.sessionID, permission.id);
|
||||
if (isViewed || !key || pendingIds.has(key)) return false;
|
||||
|
||||
pendingIds.add(key);
|
||||
const description = typeof permission.permission === 'string' && permission.permission.trim().length > 0
|
||||
? permission.permission
|
||||
: 'Agent needs your approval';
|
||||
show('Permission needed', {
|
||||
id: `permission-${key}`,
|
||||
description,
|
||||
action: {
|
||||
label: 'Open session',
|
||||
onClick: () => openSession(permission.sessionID, directory),
|
||||
},
|
||||
});
|
||||
return true;
|
||||
};
|
||||
@@ -42,6 +42,7 @@ import type { QuestionRequest } from "@/types/question"
|
||||
import * as sessionActions from "./session-actions"
|
||||
import { getSessionMaterializationStatus, materializeSessionSnapshots } from "./materialization"
|
||||
import { openSessionFromToast } from "./session-navigation"
|
||||
import { getPermissionToastKey, showPermissionNeededToast } from "./permission-toast"
|
||||
import { getRuntimeLiveStatusSeed, LIVE_STATUS_TTL_MS } from "./runtime-live-memory"
|
||||
import { getRuntimeKey } from "@/lib/runtime-switch"
|
||||
import { getRegisteredRuntimeAPIs } from "@/contexts/runtimeAPIRegistry"
|
||||
@@ -320,11 +321,6 @@ const getQuestionToastKey = (sessionID?: string, requestID?: string) => {
|
||||
return `${sessionID}:${requestID}`
|
||||
}
|
||||
|
||||
const getPermissionToastKey = (sessionID?: string, requestID?: string) => {
|
||||
if (!sessionID || !requestID) return null
|
||||
return `${sessionID}:${requestID}`
|
||||
}
|
||||
|
||||
type UiNotificationPayload = {
|
||||
title?: unknown
|
||||
body?: unknown
|
||||
@@ -1135,7 +1131,9 @@ export async function resyncBlockingRequestsForDirectory(
|
||||
}
|
||||
|
||||
const permissionStore = usePermissionStore.getState()
|
||||
const autoAcceptingSessionIds = Object.keys(grouped).filter((sessionId) => permissionStore.isSessionAutoAccepting(sessionId))
|
||||
const autoAcceptingSessionIds = isVSCodeRuntime()
|
||||
? Object.keys(grouped).filter((sessionId) => permissionStore.isSessionAutoAccepting(sessionId))
|
||||
: []
|
||||
|
||||
if (autoAcceptingSessionIds.length > 0) {
|
||||
const acceptedIdsBySession = new Map<string, Set<string>>()
|
||||
@@ -1198,19 +1196,13 @@ export async function resyncBlockingRequestsForDirectory(
|
||||
if (isViewed) continue
|
||||
for (const permission of permissions) {
|
||||
if (knownIds.has(permission.id)) continue
|
||||
const toastKey = getPermissionToastKey(sessionId, permission.id)
|
||||
if (!toastKey || pendingPermissionToastIds.has(toastKey)) continue
|
||||
pendingPermissionToastIds.add(toastKey)
|
||||
const description = typeof permission.permission === "string" && permission.permission.trim().length > 0
|
||||
? permission.permission
|
||||
: "Agent needs your approval"
|
||||
toast.info("Permission needed", {
|
||||
id: `permission-${toastKey}`,
|
||||
description,
|
||||
action: {
|
||||
label: "Open session",
|
||||
onClick: () => openSessionFromToast(sessionId, directory),
|
||||
},
|
||||
showPermissionNeededToast({
|
||||
permission,
|
||||
directory,
|
||||
isViewed,
|
||||
pendingIds: pendingPermissionToastIds,
|
||||
show: (title, options) => toast.info(title, options),
|
||||
openSession: openSessionFromToast,
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1336,6 +1328,19 @@ function handleEvent(
|
||||
childStores: ChildStoreManager,
|
||||
routingIndex: EventRoutingIndex,
|
||||
) {
|
||||
if ((payload as { type?: unknown }).type === "openchamber:permission-auto-accept.updated") {
|
||||
const properties = (payload as unknown as { properties?: unknown }).properties
|
||||
if (properties && typeof properties === "object") {
|
||||
const snapshot = properties as { sessions?: unknown }
|
||||
if (snapshot.sessions && typeof snapshot.sessions === "object") {
|
||||
usePermissionStore.getState().applySnapshot({
|
||||
sessions: snapshot.sessions as Record<string, boolean>,
|
||||
})
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
const directory = resolveDirectoryFromRoutingIndex(routingIndex, rawDirectory, payload, childStores)
|
||||
|
||||
if (handleUiNotificationEvent(payload, directory)) {
|
||||
@@ -1421,26 +1426,21 @@ function handleEvent(
|
||||
const permissionStore = usePermissionStore.getState()
|
||||
if (permissionStore.isSessionAutoAccepting(permission.sessionID)) {
|
||||
updateRoutingIndexFromEvent(routingIndex, resolvedDirectory, payload)
|
||||
void sessionActions.respondToPermission(permission.sessionID, permission.id, "once").catch(() => undefined)
|
||||
if (isVSCodeRuntime()) {
|
||||
void sessionActions.respondToPermission(permission.sessionID, permission.id, "once").catch(() => undefined)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
const toastKey = getPermissionToastKey(permission.sessionID, permission.id)
|
||||
const isViewed = isViewedInCurrentSession(resolvedDirectory, permission.sessionID)
|
||||
if (!isViewed && toastKey && !pendingPermissionToastIds.has(toastKey)) {
|
||||
pendingPermissionToastIds.add(toastKey)
|
||||
const description = typeof permission.permission === "string" && permission.permission.trim().length > 0
|
||||
? permission.permission
|
||||
: "Agent needs your approval"
|
||||
toast.info("Permission needed", {
|
||||
id: `permission-${toastKey}`,
|
||||
description,
|
||||
action: {
|
||||
label: "Open session",
|
||||
onClick: () => openSessionFromToast(permission.sessionID, resolvedDirectory),
|
||||
},
|
||||
})
|
||||
}
|
||||
showPermissionNeededToast({
|
||||
permission,
|
||||
directory: resolvedDirectory,
|
||||
isViewed,
|
||||
pendingIds: pendingPermissionToastIds,
|
||||
show: (title, options) => toast.info(title, options),
|
||||
openSession: openSessionFromToast,
|
||||
})
|
||||
}
|
||||
|
||||
if (payload.type === "permission.replied") {
|
||||
@@ -1732,6 +1732,11 @@ export function SyncProvider(props: {
|
||||
}, [childStores, routingIndex])
|
||||
|
||||
// Configure child store manager
|
||||
useEffect(() => {
|
||||
if (isVSCodeRuntime()) return
|
||||
void usePermissionStore.getState().hydrate().catch(() => undefined)
|
||||
}, [props.sdk])
|
||||
|
||||
useEffect(() => {
|
||||
const bootingDirs = new Set<string>()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user