perf: Agent List and Model Selector at Scale (#14601)

* perf: cut serial round trips from the agent list query path

The agent list was the slowest path on first page load. Three separate
problems compounded:

- `getListAgentsHandler` chained its reads: two ACL lookups, the avatar
  refresh cache probe and the viewer skill scope all resolved serially
  ahead of the list query, and `attachOwnerContacts` added two more hops
  after it. The four independent reads now resolve together, and the
  avatar refresh runs alongside the list query instead of before it -
  refreshed paths reach the response through `urlCache`, not through
  whatever the list query happened to read. Serial hops per request drop
  from 7 to 4 on a warm cache.

- The avatar refresh loaded the user's whole accessible agent set (up to
  MAX_AVATAR_REFRESH_AGENTS) to discover which entries were S3-backed.
  Scoping the query to `avatar.source` means deployments on any other
  file strategy match nothing instead of walking the full set.

- `fetchAllAgentPages` walked cursor pages at the server's default size
  of 100, and callers consume the flattened result, so every extra page
  was a serial round trip for no benefit. It now requests the server
  maximum. Measured over a 2,860 agent account: 29 requests / 1.65s
  before, 3 requests / 0.29s after.

Also parallelizes the conversation file reads in `initializeAgent`. The
convo file refs and the execute_code thread walk share no inputs, and the
two code-file lookups depend only on `threadFileIds`, so the chain of six
serial reads on every turn collapses to two. This one is time to first
token the user waits through.

* perf: virtualize the model selector agent list

Opening the agents submenu with a large agent set froze the tab and could
kill it outright. With ~10k accessible agents the submenu blocked for over
15 seconds and took the heap from 96MB to 911MB. Four per-row costs were
being multiplied by the full list, which rendered unwindowed:

- `useIsActiveItem` allocated a MutationObserver per row (10,016 of them
  for one dropdown). Replaced with an Ariakit store subscription, which
  needs no observer at all and returns a boolean so a row only re-renders
  when its own active state flips.

- `useFavorites` ran per row, opening a jotai subscription, a query
  subscription and a mutation each time. Hoisted to one call per endpoint.

- Each row rescanned `endpoint.models` to recover `isGlobal`, a field the
  parent had already discarded from the array it was mapping. The parent
  now passes it down from a lookup map.

- The list itself is now windowed above 100 rows. Ariakit's composite only
  knows about mounted rows, so arrow-keying to the window edge previously
  found no next item and let focus escape the nested menu, closing it;
  `handleBoundaryNavigation` scrolls the next index in, waits for it to
  mount, then moves the composite onto it. Navigation inside the window is
  left to Ariakit.

Open drops from >15s to 96ms, mounted rows from 10,028 to ~18, DOM nodes
from 123,346 to ~1,000, and the heap no longer grows. Verified in browser:
arrow keys track 1:1 to index 238 and back, and click selection works.

* perf: serve the model selector from the shared VIEW agent query

The model selector asked for EDIT-scoped agents whenever the marketplace
is enabled, while `useAgentsMap` and `useMentions` asked for VIEW. Since
the cache key includes the params, that was two distinct entries, so first
page load ran the paginated walk twice and held two copies of the whole
agent list in memory. Measured against a 10k agent account: 22 list handler
invocations per page load, now 11.

Collapsing the two by asking for the same permission everywhere would have
changed what the selector shows - under the marketplace the EDIT scope is
what makes it "My Agents", with discovery handled by the marketplace entry.
So the list endpoint now marks each row with `isEditable`, resolved from an
ACL read folded into the existing parallel batch (no extra serial hop), and
the selector filters the shared VIEW response instead of refetching. A
VIEW-scoped list for a user with 2861 visible / 361 editable agents returns
exactly 360 rows flagged editable, matching what the EDIT query returned.

`AgentSelect` deliberately keeps its own EDIT query: it reads `skills` and
`skills_enabled`, which `sanitizeViewerSkillScope` strips from VIEW-scoped
responses. It also only mounts when the builder panel is open, so it is not
part of the first-load cost.

The field is set unconditionally rather than omitted when false so that a
client talking to an older server sees `undefined`, keeps every agent, and
degrades to showing too many rather than none.

* fix: address review findings on the agent list at scale

Three issues from review, all confirmed against the code before fixing.

Avatar refresh no longer runs alongside the list query. `updateAgent` writes
through `findOneAndUpdate` on a `timestamps: true` schema, so refreshing an
avatar advances `updatedAt` — the field `getListAgentsByAccess` sorts and
cursors on. A write landing after the first page's snapshot moved that agent
ahead of the returned cursor, dropping it from every later page and silently
truncating the caller's flattened list. This was a regression introduced when
the two were parallelized; serializing them costs nothing on the common path,
because a cache hit returns without issuing any query, so only the
once-per-30-minutes miss pays for the ordering. The new test asserts the write
lands before the list snapshot and fails against the parallel version.

The virtualized list no longer inserts a focusable grid into the combobox.
`List` spreads its props onto `Grid`, whose defaults are `role="grid"`,
`containerRole="row"` and `tabIndex={0}`; inside Ariakit's listbox that added a
tab stop ahead of any row and put grid/row semantics between the listbox and its
options. All three are now neutralized so focus and ARIA stay with the combobox
items.

The list also resets to the top when the filter changes. `Grid` keeps its scroll
offset across prop changes and clamps an out-of-range offset to
`totalRowsHeight - height`, the end of the shorter list. Scrolling deep and then
searching landed on the tail: measured at row 626 of 667 matches, with only
those rows mounted and reachable by keyboard. Keying the list on the search
value restores row 0.

* fix: declare option position and set size for the virtualized model list

Once the model list is windowed, only the mounted slice exists in the listbox,
so a screen reader infers position and total from ~19 elements instead of the
real set — announcing "3 of 19" partway through 10,014 agents.

Model rows now carry aria-posinset and aria-setsize. The marketplace entry and
any model specs share the same numbering, because they are options in the same
listbox: declaring the values on some options while leaving others to be
inferred from the DOM would make the set internally inconsistent. Both are
omitted entirely when the list is short enough to render unwindowed, where the
DOM holds every option and the implicit values are already correct.

Verified against a 10,014 agent account: the marketplace entry reports 1 of
10015, the first models 2 and 3, and after scrolling to row 4999 the leading
mounted model reports 5001 of 10015 with 19 options in the DOM.

* 🩹 fix: Address Follow-Ups on the Agent List at Scale

Corrects residual issues in the agent-list perf work, all inside its own scope.

- Forward `idOnTheSource` through `PermissionService.findAccessibleResources`
  so `getUserPrincipals` skips the user-document read. The list handler resolves
  three permission sets per request and each was paying its own `User.findById`;
  the auth strategies already normalize the field to a value or null.
- Gate the editable-set lookup on its own predicate instead of borrowing
  `canReturnSkillConfig`. The two answer unrelated questions and only coincide
  today, so redefining the skill flag would have marked every agent editable.
- Log mapping failures in the list response instead of swallowing them.
- Apply the walk page size after the caller's params in `fetchAllAgentPages`.
  A caller limit only changed page size, never what the flattened walk returned,
  so `defaultAgentParams`' `limit: 10` would have turned one request into 301.
- Carry `isEditable` on the agent rows the create and update mutations write
  into the list cache. Mutation responses omit the field, so those rows lost it.
- Document `isEditable` as list-only, ACL-derived, and fail-open on absence.
- Restore the truthiness guard on the thread walk in `initializeAgent`. Widening
  it to `!= null` made an empty `parentMessageId` issue a full-conversation read
  against an anchor that can never match.
- Await `getConvoFiles` directly rather than calling `.then()` on it, restoring
  tolerance for synchronous test doubles.
- Correct the avatar-refresh comment: the projection was never full documents,
  and the real reason to filter is that an unfiltered budget is self-reinforcing.

Tests: both new `initialize` tests and both new backend tests are
mutation-verified; the concurrency test fails under either serialization order.

* fix: preserve ACL isEditable when merging agent mutation responses

Mutation responses omit list-only isEditable. Inferring true from write
success promoted VIEW-only rows into the editable subset for MANAGE_AGENTS
callers who can PATCH agents their ACL marks non-editable.

* fix: sort imports in agent mutations test

ESLint import-order check failed on the isEditable cache-preservation test.

* 🧷 fix: Carry isEditable Onto Duplicated Agent List Rows

`useDuplicateAgentMutation` prepended the raw duplicate response to the cached
list, and mutation responses omit the list-only `isEditable` field. The row
survived the "My Agents" filter only by failing open on `undefined`, so it would
disappear the moment a consumer read the flag strictly.

Duplicating grants the caller ownership, so the new row is editable outright;
this is the create case rather than the merge case `mergeAgentListRow` handles.
Last cache write on this path that did not carry the field.

---------

Co-authored-by: Danny Avila <danny@librechat.ai>
This commit is contained in:
Marco Beretta 2026-08-08 03:04:55 +02:00 committed by GitHub
parent a5b10c78cf
commit 39f5f9d846
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
19 changed files with 1107 additions and 179 deletions

View file

@ -1166,6 +1166,12 @@ const getListAgentsHandler = async (req, res) => {
requiredPermission = PermissionBits.VIEW;
}
const canReturnSkillConfig = hasEditBit(requiredPermission);
/**
* Derived from the same bit as `canReturnSkillConfig` but answering a different question:
* skill-config exposure versus edit-permission reporting. An EDIT-scoped request matches
* only editable agents, so it needs no second lookup to know which ones those are.
*/
const needsEditableLookup = !hasEditBit(requiredPermission);
// Base filter
const filter = {};
@ -1188,33 +1194,99 @@ const getListAgentsHandler = async (req, res) => {
filter.$or = [{ name: regex }, { description: regex }];
}
// Get agent IDs the user has VIEW access to via ACL
const accessibleIds = await findAccessibleResources({
userId,
role: req.user.role,
resourceType: ResourceType.AGENT,
requiredPermissions: requiredPermission,
});
const cache = getLogStores(CacheKeys.S3_EXPIRY_INTERVAL);
const refreshKey = `${userId}:agents_avatar_refresh`;
const publiclyAccessibleIds = await findPubliclyAccessibleResources({
resourceType: ResourceType.AGENT,
requiredPermissions: PermissionBits.VIEW,
});
/**
* These reads share no inputs, so they resolve together rather than chaining round
* trips ahead of the list query. The viewer skill scope and the editable set are only
* consumed when the page is non-empty; dispatching them here trades a wasted lookup on
* the (cheap) zero-agent path for one less serial hop on every populated page.
*
* `editableIds` lets a VIEW-scoped response mark which agents the caller may also edit,
* so consumers wanting just the editable subset can filter one shared VIEW fetch rather
* than issuing a second full paginated walk under an EDIT-scoped cache key. Requests
* that already ask for EDIT get it for free: everything they match is editable.
*
* `idOnTheSource` is forwarded so `getUserPrincipals` resolves identity without reading
* the user document; the auth strategies already normalize it to a value or null. Each
* omission would cost this handler another `User.findById`, once per lookup.
*/
const { idOnTheSource } = req.user;
const [
accessibleIds,
publiclyAccessibleIds,
cachedRefreshEntry,
accessibleSkillIds,
editableIds,
] = await Promise.all([
findAccessibleResources({
userId,
role: req.user.role,
idOnTheSource,
resourceType: ResourceType.AGENT,
requiredPermissions: requiredPermission,
}),
findPubliclyAccessibleResources({
resourceType: ResourceType.AGENT,
requiredPermissions: PermissionBits.VIEW,
}),
cache.get(refreshKey),
canReturnSkillConfig
? null
: findAccessibleResources({
userId,
role: req.user.role,
idOnTheSource,
resourceType: ResourceType.SKILL,
requiredPermissions: PermissionBits.VIEW,
}),
needsEditableLookup
? findAccessibleResources({
userId,
role: req.user.role,
idOnTheSource,
resourceType: ResourceType.AGENT,
requiredPermissions: PermissionBits.EDIT,
})
: null,
]);
const isValidCachedRefresh =
cachedRefreshEntry != null &&
typeof cachedRefreshEntry === 'object' &&
cachedRefreshEntry.urlCache != null;
/**
* Refresh all S3 avatars for this user's accessible agent set (not only the current page)
* This addresses page-size limits preventing refresh of agents beyond the first page
* This addresses page-size limits preventing refresh of agents beyond the first page.
*
* Scoped to agents that actually carry an S3 avatar so the `MAX_AVATAR_REFRESH_AGENTS`
* budget is spent on agents that can do work. Unfiltered, that budget is the most
* recently updated accessible agents regardless of avatar, and because a refresh writes
* through `updateAgent` and advances `updatedAt`, the window is self-reinforcing: an
* S3-avatar agent ranked past the budget never enters it and its presigned URL is never
* regenerated. The predicate is not indexed (`avatar` is `Mixed`), so this trades docs
* examined for that coverage.
*
* Must settle BEFORE the list query below, and is deliberately not parallelized with
* it. `updateAgent` writes through `findOneAndUpdate` on a `timestamps: true` schema,
* so refreshing an avatar advances `updatedAt`, the very field
* `getListAgentsByAccess` sorts and cursors on. A refresh landing after the first
* page's snapshot would move that agent ahead of the returned cursor, dropping it
* from every later page and silently truncating the caller's flattened list.
* Serializing costs nothing on the common path: a cache hit returns below without
* issuing any query, so only the once-per-30-minutes miss pays for the ordering.
*/
const cache = getLogStores(CacheKeys.S3_EXPIRY_INTERVAL);
const refreshKey = `${userId}:agents_avatar_refresh`;
let cachedRefresh = await cache.get(refreshKey);
const isValidCachedRefresh =
cachedRefresh != null && typeof cachedRefresh === 'object' && cachedRefresh.urlCache != null;
if (!isValidCachedRefresh) {
const resolveAvatarRefresh = async () => {
if (isValidCachedRefresh) {
logger.debug('[/Agents] S3 avatar refresh already checked, skipping');
return cachedRefreshEntry;
}
try {
const fullList = await db.getListAgentsByAccess({
accessibleIds,
otherParams: {},
otherParams: { 'avatar.source': FileSources.s3 },
limit: MAX_AVATAR_REFRESH_AGENTS,
after: null,
});
@ -1224,14 +1296,16 @@ const getListAgentsHandler = async (req, res) => {
refreshS3Url,
updateAgent: db.updateAgent,
});
cachedRefresh = { urlCache };
await cache.set(refreshKey, cachedRefresh, Time.THIRTY_MINUTES);
const refreshEntry = { urlCache };
await cache.set(refreshKey, refreshEntry, Time.THIRTY_MINUTES);
return refreshEntry;
} catch (err) {
logger.error('[/Agents] Error refreshing avatars for full list: %o', err);
return null;
}
} else {
logger.debug('[/Agents] S3 avatar refresh already checked, skipping');
}
};
const cachedRefresh = await resolveAvatarRefresh();
// Use the new ACL-aware function
const data = await db.getListAgentsByAccess({
@ -1247,20 +1321,13 @@ const getListAgentsHandler = async (req, res) => {
return res.json(data);
}
let accessibleSkillSet = null;
if (!canReturnSkillConfig) {
const accessibleSkillIds = await findAccessibleResources({
userId,
role: req.user.role,
resourceType: ResourceType.SKILL,
requiredPermissions: PermissionBits.VIEW,
});
accessibleSkillSet = new Set(
mergeDeploymentSkillIds(accessibleSkillIds).map((oid) => oid.toString()),
);
}
const accessibleSkillSet = canReturnSkillConfig
? null
: new Set(mergeDeploymentSkillIds(accessibleSkillIds).map((oid) => oid.toString()));
const publicSet = new Set(publiclyAccessibleIds.map((oid) => oid.toString()));
/** Null for EDIT-scoped requests, where every matched agent is editable by definition. */
const editableSet = editableIds ? new Set(editableIds.map((oid) => oid.toString())) : null;
const agentsWithContacts = await attachOwnerContacts(agents);
const urlCache = cachedRefresh?.urlCache;
@ -1272,6 +1339,7 @@ const getListAgentsHandler = async (req, res) => {
if (agent?._id && publicSet.has(agent._id.toString())) {
agent.isPublic = true;
}
agent.isEditable = editableSet == null || editableSet.has(agent?._id?.toString());
if (
urlCache &&
agent?.id &&
@ -1280,9 +1348,8 @@ const getListAgentsHandler = async (req, res) => {
) {
agent.avatar = { ...agent.avatar, filepath: urlCache[agent.id] };
}
} catch (e) {
// Silently ignore mapping errors
void e;
} catch (err) {
logger.warn('[/Agents] Error mapping agent %s for list response: %o', agent?.id, err);
}
return agent;
});

View file

@ -1676,6 +1676,65 @@ describe('Agent Controllers - Mass Assignment Protection', () => {
expect(response.data[0].owner_contact).toBeUndefined();
});
test('should mark isEditable per agent on a VIEW-scoped list', async () => {
mockReq.user.id = userA.toString();
mockReq.query = { requiredPermission: String(PermissionBits.VIEW) };
/** VIEW reaches all three; the EDIT lookup only reaches agentA1. */
findAccessibleResources.mockImplementation(({ resourceType, requiredPermissions }) => {
if (resourceType === 'agent' && requiredPermissions === PermissionBits.EDIT) {
return Promise.resolve([agentA1._id]);
}
if (resourceType === 'agent') {
return Promise.resolve([agentA1._id, agentA2._id, agentA3._id]);
}
return Promise.resolve([]);
});
findPubliclyAccessibleResources.mockResolvedValue([]);
await getListAgentsHandler(mockReq, mockRes);
const byId = Object.fromEntries(
mockRes.json.mock.calls[0][0].data.map((a) => [a.id, a.isEditable]),
);
expect(byId[agentA1.id]).toBe(true);
expect(byId[agentA2.id]).toBe(false);
expect(byId[agentA3.id]).toBe(false);
});
test('should forward idOnTheSource to every ACL lookup', async () => {
/** Without it `getUserPrincipals` reads the user document once per lookup, so the
* handler pays an extra `User.findById` for each permission it resolves. */
mockReq.user.id = userA.toString();
mockReq.user.idOnTheSource = 'external-oid-1';
findAccessibleResources.mockResolvedValue([agentA1._id]);
findPubliclyAccessibleResources.mockResolvedValue([]);
await getListAgentsHandler(mockReq, mockRes);
expect(findAccessibleResources.mock.calls.length).toBeGreaterThan(1);
for (const [args] of findAccessibleResources.mock.calls) {
expect(args.idOnTheSource).toBe('external-oid-1');
}
});
test('should mark every agent editable when the request is already EDIT-scoped', async () => {
mockReq.user.id = userA.toString();
mockReq.query = { requiredPermission: String(PermissionBits.EDIT) };
findAccessibleResources.mockResolvedValue([agentA1._id, agentA2._id]);
findPubliclyAccessibleResources.mockResolvedValue([]);
await getListAgentsHandler(mockReq, mockRes);
const response = mockRes.json.mock.calls[0][0];
expect(response.data.every((a) => a.isEditable === true)).toBe(true);
/** No extra EDIT lookup: an EDIT-scoped match is editable by definition. */
const editCalls = findAccessibleResources.mock.calls.filter(
([args]) =>
args.resourceType === 'agent' && args.requiredPermissions === PermissionBits.EDIT,
);
expect(editCalls).toHaveLength(1);
});
test('should return only expected safe list fields for VIEW callers', async () => {
const hiddenSkillId = new mongoose.Types.ObjectId();
await Agent.findByIdAndUpdate(agentA1._id, {
@ -1721,6 +1780,7 @@ describe('Agent Controllers - Mass Assignment Protection', () => {
'conversation_starters',
'description',
'id',
'isEditable',
'is_promoted',
'name',
'support_contact',
@ -2235,6 +2295,110 @@ describe('Agent Controllers - Mass Assignment Protection', () => {
expect(mockRes.json).toHaveBeenCalled();
});
test('should finish avatar writes before snapshotting the paginated list query', async () => {
/** `updateAgent` bumps `updatedAt`, which is the field `getListAgentsByAccess`
* sorts and cursors on. If the list query snapshots before a refresh write
* lands, that agent jumps ahead of the returned cursor and vanishes from every
* later page. Assert the ordering rather than the symptom, which only shows up
* on multi-page S3 accounts under a specific interleaving. */
const db = require('~/models');
const order = [];
/** Yield a macrotask so a parallelized refresh would lose the race, the way a real
* S3 presign round trip does. */
refreshS3Url.mockImplementation(async () => {
await new Promise((resolve) => setTimeout(resolve, 5));
order.push('avatar-write');
return 'new-s3-path.jpg';
});
const realList = db.getListAgentsByAccess;
const listSpy = jest.spyOn(db, 'getListAgentsByAccess').mockImplementation(async (params) => {
if (params.includeSkillConfig) {
order.push('list-query');
return { object: 'list', data: [], has_more: false, after: null };
}
return realList(params);
});
mockCache.get.mockResolvedValue(false);
findAccessibleResources.mockResolvedValue([agentWithS3Avatar._id]);
findPubliclyAccessibleResources.mockResolvedValue([]);
const mockReq = { user: { id: userA.toString(), role: 'USER' }, query: {} };
const mockRes = { status: jest.fn().mockReturnThis(), json: jest.fn().mockReturnThis() };
try {
await getListAgentsHandler(mockReq, mockRes);
expect(order).toContain('avatar-write');
expect(order.indexOf('avatar-write')).toBeLessThan(order.indexOf('list-query'));
} finally {
listSpy.mockRestore();
refreshS3Url.mockReset();
}
});
test('should serve the refreshed filepath in the same response on cache miss', async () => {
const agentId = agentWithS3Avatar.id;
mockCache.get.mockResolvedValue(false);
findAccessibleResources.mockResolvedValue([agentWithS3Avatar._id]);
findPubliclyAccessibleResources.mockResolvedValue([]);
refreshS3Url.mockResolvedValue('new-s3-path.jpg');
const mockReq = {
user: { id: userA.toString(), role: 'USER' },
query: {},
};
const mockRes = {
status: jest.fn().mockReturnThis(),
json: jest.fn().mockReturnThis(),
};
await getListAgentsHandler(mockReq, mockRes);
const responseData = mockRes.json.mock.calls[0][0];
const agent = responseData.data.find((a) => a.id === agentId);
/** The refresh runs alongside the list query, so the refreshed path must reach the
* response through `urlCache` rather than through what the list query read. */
expect(agent.avatar.filepath).toBe('new-s3-path.jpg');
});
test('should scope the refresh query to S3 avatars without filtering the list query', async () => {
const db = require('~/models');
const listSpy = jest.spyOn(db, 'getListAgentsByAccess');
mockCache.get.mockResolvedValue(false);
findAccessibleResources.mockResolvedValue([agentWithLocalAvatar._id]);
findPubliclyAccessibleResources.mockResolvedValue([]);
const mockReq = {
user: { id: userA.toString(), role: 'USER' },
query: {},
};
const mockRes = {
status: jest.fn().mockReturnThis(),
json: jest.fn().mockReturnThis(),
};
try {
await getListAgentsHandler(mockReq, mockRes);
/** The refresh pass must query only S3-avatar agents `refreshListAvatars`
* skips non-S3 entries anyway, so without this assertion the filter could
* regress to `{}` (reloading the whole accessible set) unnoticed. */
expect(listSpy).toHaveBeenCalledWith(
expect.objectContaining({ otherParams: { 'avatar.source': FileSources.s3 } }),
);
/** The user-facing list query keeps the request filter, not the refresh scope. */
expect(listSpy).toHaveBeenCalledWith(
expect.objectContaining({ includeSkillConfig: true, otherParams: {} }),
);
expect(refreshS3Url).not.toHaveBeenCalled();
const responseData = mockRes.json.mock.calls[0][0];
const agent = responseData.data.find((a) => a.id === agentWithLocalAvatar.id);
expect(agent.avatar.filepath).toBe('local-path.jpg');
} finally {
listSpy.mockRestore();
}
});
test('should refresh avatars for all accessible agents (VIEW permission)', async () => {
mockCache.get.mockResolvedValue(false);
// User A has access to both their own agent and userB's agent

View file

@ -238,11 +238,19 @@ const getResourcePermissionsMap = async ({ userId, role, resourceType, resourceI
* @param {Object} params - Parameters for finding accessible resources
* @param {string|mongoose.Types.ObjectId} params.userId - The ID of the user
* @param {string} [params.role] - Optional user role (if not provided, will query from DB)
* @param {string|null} [params.idOnTheSource] - Optional external member id. `null` means "known to
* be absent" (local user); only `undefined` makes `getUserPrincipals` read the user document.
* @param {string} params.resourceType - Type of resource (e.g., 'agent')
* @param {number} params.requiredPermissions - The minimum permission bits required (e.g., 1 for VIEW, 3 for VIEW+EDIT)
* @returns {Promise<Array>} Array of resource IDs
*/
const findAccessibleResources = async ({ userId, role, resourceType, requiredPermissions }) => {
const findAccessibleResources = async ({
userId,
role,
idOnTheSource,
resourceType,
requiredPermissions,
}) => {
try {
if (typeof requiredPermissions !== 'number' || requiredPermissions < 1) {
throw new Error('requiredPermissions must be a positive number');
@ -251,7 +259,7 @@ const findAccessibleResources = async ({ userId, role, resourceType, requiredPer
validateResourceType(resourceType);
// Get all principals for the user (user + groups + public)
const principalsList = await db.getUserPrincipals({ userId, role });
const principalsList = await db.getUserPrincipals({ userId, role, idOnTheSource });
if (principalsList.length === 0) {
return [];

View file

@ -632,6 +632,24 @@ describe('PermissionService', () => {
});
});
test('should forward idOnTheSource so principal resolution can skip the user lookup', async () => {
getUserPrincipals.mockResolvedValue([
{ principalType: PrincipalType.USER, principalId: userId },
]);
await findAccessibleResources({
userId,
role: 'USER',
idOnTheSource: null,
resourceType: ResourceType.AGENT,
requiredPermissions: 1, // VIEW
});
expect(getUserPrincipals).toHaveBeenCalledWith(
expect.objectContaining({ idOnTheSource: null }),
);
});
test('should find resources user can view', async () => {
// Mock getUserPrincipals to return user principal
getUserPrincipals.mockResolvedValue([