Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

fix(agent-vault): batch variable key lookups and list an exact match first

Scott Wilsond64c33958aa3
diff --git a/frontend/src/components/agent-vault/service-sheet/VariableSuggestions.tsx b/frontend/src/components/agent-vault/service-sheet/VariableSuggestions.tsx
index d691fbc778..b71844c9e3 100644
--- a/frontend/src/components/agent-vault/service-sheet/VariableSuggestions.tsx
+++ b/frontend/src/components/agent-vault/service-sheet/VariableSuggestions.tsx
@@ -14,7 +14,6 @@ import {
useAgentVaultVariableKeyStatus,
useListAgentVaultVariables
} from "@app/hooks/api/agentVault";
-import { TAgentVaultVariable } from "@app/hooks/api/agentVault/types";
import { useServiceVariables } from "./ServiceVariablesContext";
@@ -23,7 +22,7 @@ type TOpenReference = { start: number; query: string };
const SUGGESTION_LIMIT = 50;
type TSuggestion =
- | { type: "variable"; variable: TAgentVaultVariable }
+ | { type: "variable"; key: string }
| { type: "create"; key: string; isKeyValid: boolean };
export const useVariableAutocomplete = ({
@@ -60,16 +59,24 @@ export const useVariableAutocomplete = ({
const suggestions = useMemo((): TSuggestion[] => {
if (!openReference || !data) return [];
const key = query;
- const matches = data.variables
- .filter((variable) => variable.key.includes(key))
- .sort(
- (a, b) =>
- Number(b.key.startsWith(key)) - Number(a.key.startsWith(key)) ||
- a.key.localeCompare(b.key)
- )
- .map((variable) => ({ type: "variable" as const, variable }));
+ // The search returns the first page of keys containing the text, which may not include the key itself, so
+ // one the lookup has found is added and listed first either way.
+ const keyExists = existing.has(key) || data.variables.some((variable) => variable.key === key);
+ const rank = (candidate: string) => {
+ if (candidate === key) return 0;
+ return candidate.startsWith(key) ? 1 : 2;
+ };
+ const matches = [
+ ...new Set([
+ ...(keyExists ? [key] : []),
+ ...data.variables
+ .map((variable) => variable.key)
+ .filter((candidate) => candidate.includes(key))
+ ])
+ ]
+ .sort((a, b) => rank(a) - rank(b) || a.localeCompare(b))
+ .map((candidate) => ({ type: "variable" as const, key: candidate }));
- const keyExists = matches.some(({ variable }) => variable.key === key) || existing.has(key);
if (!requestVariable || keyExists) return matches;
return [...matches, { type: "create", key, isKeyValid: VARIABLE_KEY_RE.test(key) }];
}, [openReference, query, data, existing, requestVariable]);
@@ -130,7 +137,7 @@ export const useVariableAutocomplete = ({
};
if (suggestion.type === "variable") {
- insert(suggestion.variable.key);
+ insert(suggestion.key);
return;
}
@@ -208,7 +215,7 @@ export const VariableSuggestions = ({
const renderOption = (suggestion: TSuggestion, index: number) => (
// eslint-disable-next-line jsx-a11y/click-events-have-key-events
<div
- key={suggestion.type === "variable" ? suggestion.variable.id : "create"}
+ key={suggestion.type === "variable" ? suggestion.key : "create"}
id={optionId(index)}
role="option"
tabIndex={-1}
@@ -222,7 +229,7 @@ export const VariableSuggestions = ({
onClick={() => choose(suggestion)}
>
{suggestion.type === "variable" ? (
- <span className="flex-1 truncate font-mono text-xs">{suggestion.variable.key}</span>
+ <span className="flex-1 truncate font-mono text-xs">{suggestion.key}</span>
) : (
<>
<PlusIcon className="size-3.5 shrink-0 text-muted" />
diff --git a/frontend/src/hooks/api/agentVault/queries.tsx b/frontend/src/hooks/api/agentVault/queries.tsx
index 96267813d5..862f3ed97a 100644
--- a/frontend/src/hooks/api/agentVault/queries.tsx
+++ b/frontend/src/hooks/api/agentVault/queries.tsx
@@ -1,6 +1,8 @@
import { useMemo, useRef, useState } from "react";
import {
InfiniteData,
+ SkipToken,
+ skipToken,
useInfiniteQuery,
useQueries,
useQuery,
@@ -561,17 +563,21 @@ export const useListAgentVaultVariables = (
});
};
-// Whether each key exists, one cached query per key, so a key checked once is never asked about again until a
-// variable changes. Only keys whose answer has arrived are in either set: a pending one is neither.
+const KEY_LOOKUP_BATCH_SIZE = 100;
+
+// Whether each key exists. Each key's answer is cached on its own, so a key checked once isn't asked about again
+// until a variable changes, but the keys still to check go out together through the keys filter, in batches of
+// the API's maximum, rather than a request each. Only keys whose answer has arrived are in either set.
export const useAgentVaultVariableKeyStatus = (keys: readonly string[], enabled = true) => {
const { currentOrg } = useOrganization();
+ const queryClient = useQueryClient();
+ const statusKey = (key: string) => agentVaultKeys.variableKeyStatus(currentOrg.id, key);
+ // Read-only: these never fetch, the batch below fills them.
const { existing, missing } = useQueries({
queries: keys.map((key) => ({
- queryKey: agentVaultKeys.variableKeyStatus(currentOrg.id, key),
- queryFn: async () =>
- (await fetchAgentVaultVariables({ keys: [key], limit: 1 })).totalCount > 0,
- enabled
+ queryKey: statusKey(key),
+ queryFn: skipToken as SkipToken
})),
combine: (results) => ({
existing: keys.filter((_, index) => results[index]?.data === true).join(","),
@@ -579,6 +585,32 @@ export const useAgentVaultVariableKeyStatus = (keys: readonly string[], enabled
})
});
+ // Unanswered keys, and answered ones a variable change has invalidated since.
+ const unchecked = [...keys]
+ .filter((key) => {
+ const state = queryClient.getQueryState(statusKey(key));
+ return state?.data === undefined || state.isInvalidated;
+ })
+ .sort();
+
+ useQuery({
+ queryKey: [...agentVaultKeys.variables(currentOrg.id), "key-status-batch", unchecked],
+ queryFn: async () => {
+ const found = new Set<string>();
+ for (let i = 0; i < unchecked.length; i += KEY_LOOKUP_BATCH_SIZE) {
+ // eslint-disable-next-line no-await-in-loop
+ const page = await fetchAgentVaultVariables({
+ keys: unchecked.slice(i, i + KEY_LOOKUP_BATCH_SIZE),
+ limit: KEY_LOOKUP_BATCH_SIZE
+ });
+ page.variables.forEach((variable) => found.add(variable.key));
+ }
+ unchecked.forEach((key) => queryClient.setQueryData<boolean>(statusKey(key), found.has(key)));
+ return unchecked.length;
+ },
+ enabled: enabled && unchecked.length > 0
+ });
+
return useMemo(
() => ({
existing: new Set(existing ? existing.split(",") : []) as ReadonlySet<string>,