All files / src/apps/user/pages Rules.tsx

100% Statements 14/14
100% Branches 2/2
100% Functions 6/6
100% Lines 14/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77                                    3x           3x 36x           36x   16x         36x   15x             36x   16x       36x 32x 32x 41x         36x                              
import RulesGrid from "@/apps/user/pages/rules/RulesGrid";
import { isSystemManaged } from "@/objects/Doc";
import { Rule, RuleCatalogue, RuleScopeTargets } from "@/objects/Rule";
import { INCLUDE_SYSTEM, QUERY_KEYS } from "@/services/query_keys";
import { useApiHandler } from "@/services/useApiHandler";
import { useQuery } from "@tanstack/react-query";
import React, { useMemo } from "react";
 
// ─── Rules content (rendered as the Rules tab of Governance) ─────────────────
//
// One list, not two. Rules used to be split across Inference → Models and
// Inference → Agents, filtered by scope, which made a rule read as a property
// of the page it was found on: a global rule appeared twice, a skill rule was
// only reachable from the agents side, and there was nowhere at all for a rule
// covering a whole project. A rule is a policy over the account, so it belongs
// in one place with its scope stated on the row.
 
const INTRO =
  "Rules act on every request at the point where it passes them: whether " +
  "it may run at all, what the model may read, and what people receive. A " +
  "rule's scope decides what it covers — everything, one organisation or " +
  "project, or a single agent, model, skill or worker. Each layer offers " +
  "templates: use one to create a rule of your own.";
 
export const RulesContent: React.FC = () => {
  const { getApiHandler } = useApiHandler();
 
  // The built-in rules come with the user's own; the grid draws them as
  // templates in their layers.
  // Their own key: the Governance header counts rules from the plain
  // listing, and a built-in is not a rule in force.
  const { data: rulesData, isLoading } = useQuery<Rule[] | null>({
    queryKey: [QUERY_KEYS.GET.RULES, INCLUDE_SYSTEM],
    queryFn: () => getApiHandler().rule_router.list({ include_system: true }),
  });
 
  // The scope vocabulary, for the filter's options and each row's label.
  // Cached indefinitely: it describes the backend build, not the user's data.
  const { data: catalogue } = useQuery<RuleCatalogue | null>({
    queryKey: [QUERY_KEYS.GET.RULE_CATALOGUE],
    queryFn: () => getApiHandler().rule_router.catalogue(),
    staleTime: Infinity,
  });
 
  // What each pinned rule actually governs. Kept out of the render path's
  // critical dependencies — the list renders with kind-level labels while this
  // resolves, rather than waiting on a second request to show anything.
  const { data: targets } = useQuery<RuleScopeTargets | null>({
    queryKey: [QUERY_KEYS.GET.RULE_SCOPE_TARGETS],
    queryFn: () => getApiHandler().rule_router.targets(),
    staleTime: 60_000,
  });
 
  const { rules, builtIn } = useMemo(() => {
    const rows: Rule[] = rulesData ?? [];
    return {
      rules: rows.filter((rule) => !isSystemManaged(rule)),
      builtIn: rows.filter(isSystemManaged),
    };
  }, [rulesData]);
 
  return (
    <div>
      <p className="text-sm text-gray-500 dark:text-gray-400 mb-5">{INTRO}</p>
      <RulesGrid
        rules={rules}
        builtIn={builtIn}
        loading={isLoading}
        catalogue={catalogue}
        targets={targets}
      />
    </div>
  );
};
 
export default RulesContent;