fix(sync): scope auto-approve to session tree and restore request UX
This commit is contained in:
@@ -3,13 +3,12 @@ import { devtools, persist, createJSONStorage } from "zustand/middleware";
|
||||
import type { Session } from "@opencode-ai/sdk/v2/client";
|
||||
import {
|
||||
autoRespondsPermission,
|
||||
normalizeDirectory,
|
||||
sessionAcceptKey,
|
||||
type PermissionAutoAcceptMap,
|
||||
} from "./utils/permissionAutoAccept";
|
||||
import { getSafeStorage } from "./utils/safeStorage";
|
||||
import { getAllSyncSessions } from "@/sync/sync-refs";
|
||||
import { getAllSyncSessions, getSyncChildStores } from "@/sync/sync-refs";
|
||||
import { opencodeClient } from "@/lib/opencode/client";
|
||||
import { respondToPermission } from "@/sync/session-actions";
|
||||
import { useSessionUIStore } from "@/sync/session-ui-store";
|
||||
|
||||
interface PermissionState {
|
||||
@@ -23,45 +22,120 @@ interface PermissionActions {
|
||||
|
||||
type PermissionStore = PermissionState & PermissionActions;
|
||||
|
||||
const resolveLineage = (sessionID: string, sessions: Session[]): string[] => {
|
||||
const map = new Map<string, Session>();
|
||||
for (const session of sessions) {
|
||||
map.set(session.id, session);
|
||||
const coerceAutoAcceptValue = (value: unknown): boolean => {
|
||||
if (typeof value === "boolean") {
|
||||
return value;
|
||||
}
|
||||
|
||||
const result: string[] = [];
|
||||
const seen = new Set<string>();
|
||||
let current: string | undefined = sessionID;
|
||||
while (current && !seen.has(current)) {
|
||||
seen.add(current);
|
||||
result.push(current);
|
||||
current = map.get(current)?.parentID;
|
||||
if (typeof value === "string") {
|
||||
const normalized = value.trim().toLowerCase();
|
||||
if (normalized === "true") {
|
||||
return true;
|
||||
}
|
||||
if (normalized === "false") {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (typeof value === "number") {
|
||||
return value === 1;
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
const isLegacyDirectoryAutoAcceptKey = (key: string): boolean => key.endsWith("/*");
|
||||
|
||||
const extractSessionIdFromLegacyKey = (key: string): string | null => {
|
||||
const trimmed = key.trim();
|
||||
if (!trimmed) {
|
||||
return null;
|
||||
}
|
||||
const lastSlash = trimmed.lastIndexOf("/");
|
||||
if (lastSlash === -1 || lastSlash === trimmed.length - 1) {
|
||||
return trimmed;
|
||||
}
|
||||
return trimmed.slice(lastSlash + 1);
|
||||
};
|
||||
|
||||
const resolveSessionScope = (sessionID: string, sessions: Session[]): Set<string> => {
|
||||
const map = new Map<string, Session>();
|
||||
const children = new Map<string, string[]>();
|
||||
for (const session of sessions) {
|
||||
map.set(session.id, session);
|
||||
if (session.parentID) {
|
||||
const list = children.get(session.parentID);
|
||||
if (list) {
|
||||
list.push(session.id);
|
||||
} else {
|
||||
children.set(session.parentID, [session.id]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!map.has(sessionID)) {
|
||||
return new Set([sessionID]);
|
||||
}
|
||||
|
||||
const result = new Set<string>();
|
||||
const seen = new Set<string>();
|
||||
const queue = [sessionID];
|
||||
while (queue.length > 0) {
|
||||
const current = queue.shift();
|
||||
if (!current || seen.has(current)) {
|
||||
continue;
|
||||
}
|
||||
seen.add(current);
|
||||
result.add(current);
|
||||
const nextChildren = children.get(current);
|
||||
if (!nextChildren || nextChildren.length === 0) {
|
||||
continue;
|
||||
}
|
||||
for (const child of nextChildren) {
|
||||
if (!seen.has(child)) {
|
||||
queue.push(child);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
};
|
||||
|
||||
const normalizeDirectoryCandidate = (value: unknown): string | null => {
|
||||
if (typeof value !== "string") return null;
|
||||
const trimmed = value.trim();
|
||||
return trimmed.length > 0 ? trimmed : null;
|
||||
};
|
||||
|
||||
const collectPendingFromSyncStores = (sessionScope: Set<string>): Array<{ id: string; sessionID: string }> => {
|
||||
try {
|
||||
const stores = getSyncChildStores();
|
||||
const pending: Array<{ id: string; sessionID: string }> = [];
|
||||
for (const store of stores.children.values()) {
|
||||
const permissionMap = store.getState().permission ?? {};
|
||||
for (const [sessionId, entries] of Object.entries(permissionMap)) {
|
||||
if (!sessionScope.has(sessionId)) continue;
|
||||
for (const permission of entries ?? []) {
|
||||
if (!permission?.id) continue;
|
||||
pending.push({ id: permission.id, sessionID: permission.sessionID || sessionId });
|
||||
}
|
||||
}
|
||||
}
|
||||
return pending;
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
};
|
||||
|
||||
const autoRespondsPermissionBySession = (
|
||||
autoAccept: PermissionAutoAcceptMap,
|
||||
sessions: Session[],
|
||||
sessionID: string,
|
||||
): boolean => {
|
||||
const targetSession = sessions.find((session) => session.id === sessionID);
|
||||
const mappedDirectory = useSessionUIStore.getState().getDirectoryForSession(sessionID);
|
||||
const directory = normalizeDirectory(mappedDirectory ?? (targetSession as Session & { directory?: string | null })?.directory ?? null);
|
||||
if (!directory) {
|
||||
for (const id of resolveLineage(sessionID, sessions)) {
|
||||
if (id in autoAccept) {
|
||||
return autoAccept[id] === true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
return autoRespondsPermission({
|
||||
autoAccept,
|
||||
sessions,
|
||||
sessionID,
|
||||
directory,
|
||||
sessions,
|
||||
});
|
||||
};
|
||||
|
||||
@@ -88,31 +162,55 @@ export const usePermissionStore = create<PermissionStore>()(
|
||||
}
|
||||
|
||||
const sessions = getAllSyncSessions();
|
||||
const targetSession = sessions.find((session) => session.id === sessionId);
|
||||
const mappedDirectory = useSessionUIStore.getState().getDirectoryForSession(sessionId);
|
||||
const directory = normalizeDirectory(mappedDirectory ?? (targetSession as Session & { directory?: string | null })?.directory ?? null);
|
||||
const key = directory ? sessionAcceptKey(sessionId, directory) : sessionId;
|
||||
|
||||
set((state) => {
|
||||
const autoAccept = { ...state.autoAccept };
|
||||
if (directory) {
|
||||
delete autoAccept[sessionId];
|
||||
}
|
||||
autoAccept[key] = enabled;
|
||||
autoAccept[sessionId] = enabled;
|
||||
return { autoAccept };
|
||||
});
|
||||
|
||||
if (!enabled || !directory) {
|
||||
if (!enabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
const pending = await opencodeClient.listPendingPermissions({ directories: [directory] });
|
||||
const client = opencodeClient.getScopedSdkClient(directory);
|
||||
const sessionLineage = new Set(resolveLineage(sessionId, sessions));
|
||||
const sessionScope = resolveSessionScope(sessionId, sessions);
|
||||
const sessionDirectory = useSessionUIStore.getState().getDirectoryForSession(sessionId);
|
||||
const directories = new Set<string>();
|
||||
const currentDirectory = normalizeDirectoryCandidate(opencodeClient.getDirectory());
|
||||
if (currentDirectory) {
|
||||
directories.add(currentDirectory);
|
||||
}
|
||||
const mappedSessionDirectory = normalizeDirectoryCandidate(sessionDirectory);
|
||||
if (mappedSessionDirectory) {
|
||||
directories.add(mappedSessionDirectory);
|
||||
}
|
||||
for (const scopedSessionId of sessionScope) {
|
||||
const mapped = normalizeDirectoryCandidate(useSessionUIStore.getState().getDirectoryForSession(scopedSessionId));
|
||||
if (mapped) {
|
||||
directories.add(mapped);
|
||||
}
|
||||
}
|
||||
|
||||
const pendingFromStores = collectPendingFromSyncStores(sessionScope);
|
||||
const pendingFromApi = await opencodeClient.listPendingPermissions({ directories: Array.from(directories) });
|
||||
const mergedPending = new Map<string, { id: string; sessionID: string }>();
|
||||
|
||||
for (const permission of pendingFromStores) {
|
||||
mergedPending.set(permission.id, permission);
|
||||
}
|
||||
for (const permission of pendingFromApi) {
|
||||
if (!permission?.id || !permission?.sessionID) {
|
||||
continue;
|
||||
}
|
||||
if (!sessionScope.has(permission.sessionID)) {
|
||||
continue;
|
||||
}
|
||||
mergedPending.set(permission.id, { id: permission.id, sessionID: permission.sessionID });
|
||||
}
|
||||
|
||||
await Promise.all(
|
||||
pending
|
||||
.filter((permission) => sessionLineage.has(permission.sessionID))
|
||||
.map((permission) => client.permission.reply({ requestID: permission.id, reply: "once" }).catch(() => undefined)),
|
||||
Array.from(mergedPending.values())
|
||||
.map((permission) => respondToPermission(permission.sessionID, permission.id, "once").catch(() => undefined)),
|
||||
);
|
||||
},
|
||||
}),
|
||||
@@ -126,12 +224,36 @@ export const usePermissionStore = create<PermissionStore>()(
|
||||
...(persistedState as Partial<PermissionStore>),
|
||||
};
|
||||
|
||||
const nextAutoAccept = Object.fromEntries(
|
||||
Object.entries(merged.autoAccept || {}).map(([sessionId, enabled]) => [
|
||||
sessionId,
|
||||
Boolean(enabled),
|
||||
]),
|
||||
);
|
||||
const persisted = Object.entries(merged.autoAccept || {});
|
||||
const nextAutoAccept: PermissionAutoAcceptMap = {};
|
||||
|
||||
for (const [rawKey, rawEnabled] of persisted) {
|
||||
if (rawKey.includes("/") || isLegacyDirectoryAutoAcceptKey(rawKey)) {
|
||||
continue;
|
||||
}
|
||||
nextAutoAccept[rawKey] = coerceAutoAcceptValue(rawEnabled);
|
||||
}
|
||||
|
||||
for (const [rawKey, rawEnabled] of persisted) {
|
||||
if (isLegacyDirectoryAutoAcceptKey(rawKey)) {
|
||||
continue;
|
||||
}
|
||||
if (!rawKey.includes("/")) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const sessionId = extractSessionIdFromLegacyKey(rawKey);
|
||||
if (!sessionId) {
|
||||
continue;
|
||||
}
|
||||
if (Object.prototype.hasOwnProperty.call(nextAutoAccept, sessionId)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const normalized = coerceAutoAcceptValue(rawEnabled);
|
||||
const existing = nextAutoAccept[sessionId];
|
||||
nextAutoAccept[sessionId] = existing === true ? true : normalized;
|
||||
}
|
||||
|
||||
return {
|
||||
...merged,
|
||||
|
||||
@@ -2,86 +2,43 @@ import type { Session } from "@opencode-ai/sdk/v2/client";
|
||||
|
||||
export type PermissionAutoAcceptMap = Record<string, boolean>;
|
||||
|
||||
const DIRECTORY_WILDCARD = "*";
|
||||
|
||||
const encodeBase64 = (value: string): string => {
|
||||
try {
|
||||
const bytes = new TextEncoder().encode(value);
|
||||
let binary = "";
|
||||
for (const byte of bytes) {
|
||||
binary += String.fromCharCode(byte);
|
||||
}
|
||||
return btoa(binary);
|
||||
} catch {
|
||||
return btoa(value);
|
||||
}
|
||||
const buildSessionMap = (sessions: Session[]): Map<string, Session> => {
|
||||
const map = new Map<string, Session>();
|
||||
for (const session of sessions) {
|
||||
map.set(session.id, session);
|
||||
}
|
||||
return map;
|
||||
};
|
||||
|
||||
export const normalizeDirectory = (value: string | null | undefined): string | null => {
|
||||
if (typeof value !== "string") {
|
||||
return null;
|
||||
}
|
||||
const trimmed = value.trim();
|
||||
if (!trimmed) {
|
||||
return null;
|
||||
}
|
||||
const normalized = trimmed.replace(/\\/g, "/");
|
||||
if (normalized === "/") {
|
||||
return "/";
|
||||
}
|
||||
return normalized.length > 1 ? normalized.replace(/\/+$/g, "") : normalized;
|
||||
};
|
||||
|
||||
export const directoryAcceptKey = (directory: string): string => `${encodeBase64(directory)}/${DIRECTORY_WILDCARD}`;
|
||||
|
||||
export const sessionAcceptKey = (sessionID: string, directory: string): string => `${encodeBase64(directory)}/${sessionID}`;
|
||||
|
||||
const resolveLineage = (sessionID: string, sessions: Session[]): string[] => {
|
||||
const map = new Map<string, Session>();
|
||||
for (const session of sessions) {
|
||||
map.set(session.id, session);
|
||||
}
|
||||
const map = buildSessionMap(sessions);
|
||||
const result: string[] = [];
|
||||
const seen = new Set<string>();
|
||||
let current: string | undefined = sessionID;
|
||||
|
||||
const result: string[] = [];
|
||||
const seen = new Set<string>();
|
||||
let current: string | undefined = sessionID;
|
||||
while (current && !seen.has(current)) {
|
||||
seen.add(current);
|
||||
result.push(current);
|
||||
current = map.get(current)?.parentID;
|
||||
}
|
||||
return result;
|
||||
while (current && !seen.has(current)) {
|
||||
seen.add(current);
|
||||
result.push(current);
|
||||
current = map.get(current)?.parentID;
|
||||
}
|
||||
|
||||
return result;
|
||||
};
|
||||
|
||||
export const autoRespondsPermission = (input: {
|
||||
autoAccept: PermissionAutoAcceptMap;
|
||||
sessions: Session[];
|
||||
sessionID: string;
|
||||
directory: string;
|
||||
autoAccept: PermissionAutoAcceptMap;
|
||||
sessions: Session[];
|
||||
sessionID: string;
|
||||
}): boolean => {
|
||||
const { autoAccept, sessions, sessionID, directory } = input;
|
||||
const { autoAccept, sessions, sessionID } = input;
|
||||
const lineage = resolveLineage(sessionID, sessions);
|
||||
|
||||
for (const id of resolveLineage(sessionID, sessions)) {
|
||||
const key = sessionAcceptKey(id, directory);
|
||||
if (key in autoAccept) {
|
||||
return autoAccept[key] === true;
|
||||
}
|
||||
|
||||
// Legacy fallback for pre-directory keys.
|
||||
if (id in autoAccept) {
|
||||
return autoAccept[id] === true;
|
||||
}
|
||||
for (const id of lineage) {
|
||||
if (!Object.prototype.hasOwnProperty.call(autoAccept, id)) {
|
||||
continue;
|
||||
}
|
||||
return autoAccept[id] === true;
|
||||
}
|
||||
|
||||
const directoryKey = directoryAcceptKey(directory);
|
||||
if (directoryKey in autoAccept) {
|
||||
return autoAccept[directoryKey] === true;
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
export const isDirectoryAutoAccepting = (autoAccept: PermissionAutoAcceptMap, directory: string): boolean => {
|
||||
const key = directoryAcceptKey(directory);
|
||||
return autoAccept[key] === true;
|
||||
return false;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user