Compute a principals' records permissions once per metadata snapshot

Merged
FE
1d ago
+321-191

Why

The AI chat rebuilds its tool catalog about four times per turn, and each build took ~145 ms (p50) locally. Profiling put almost all of it in the database tool provider's permission checks: ~45 objects × 5 canAccessSomeRecords calls, about 1 ms each.

The cost was not the checks themselves. canAllRecordsAccessOperation, canRlsAccessOperation and the denial check all recomputed the records permissions of every object (computePrincipalsRecordsPermissions) just to read one entry, so the cost grew with the square of the object count. PermissionsMetadataMapsService already computes the same object once per principals when it loads their permissions.

Part of the AI chat architecture work (prompt caching layout, workspace schema card, one generic tool surface, infrastructure quick wins).

What

The access request now carries the result next to the inputs it was computed from:

type PermissionsObjectAccessRequest = {
  principals;
  metadataMaps;
  computedRecordPermissions; // effective permissions of `principals` on `metadataMaps`
  flatObjectMetadata;
  operationType;
};
  • It is set where a request is built from principals, from the per-principals cache PermissionsMetadataMapsService already keeps. There is no second cache.
  • canAllRecordsAccessOperation, canRlsAccessOperation, areRecordIdsReadableByPrincipals and findRlsRulesAccessingOperation read it instead of recomputing. The field-access and RLS validation utils take it as a parameter.
  • Related record paths narrow metadataMaps per path (a role unable to read the relation field loses access to the source object). buildRelatedRecordPathJoinSQLCondition recomputes the permissions for the narrowed maps and hands both to the entry builder, so an entry never pairs narrowed maps with the request's permissions. A spec covers this.
  • Per-role checks (filterRoleIdsWithoutReadAccess) and record events, which build principals or load maps outside the per-principals cache, compute once explicitly as before.

An earlier version of this PR memoized the getter in a module-level WeakMap keyed on the identity of principals and metadataMaps. It was replaced because it duplicated the service cache and silently fell back to the slow path for any caller passing rebuilt objects.

Measured

Synthetic benchmark of one catalog build (every object × 5 operations), median:

Workspace shapeRecompute on every check (main)WeakMap memoCompute once, pass down
45 objects, 1 role12.8 ms0.094 ms0.080 ms
300 objects, 3 roles3,319 ms1.89 ms1.81 ms

Measured on the memo version against a seeded workspace (MCP integration suites): tool catalog build p50 145 ms to 9.7 ms, database tool provider 238 ms to 21 ms. Not re-measured after the rewrite.

No UI change, so no screenshots.

Tests

  • Specs that build requests by hand now go through withComputedRecordPermissions.
  • New spec: a path source entry receives permissions computed on its narrowed metadata maps.
  • Unit and integration suites are left to CI; they were not run locally after the rewrite.