Files
PaperClipAI/ui/src/components/BuiltInAgentGate.tsx
T
Constantineandcucurigoo 106955bdfd fix(ui): gate summary built-in requests by feature flag (#10170)
## Thinking Path

> - Paperclip's web UI conditionally exposes experimental summary and
built-in-agent capabilities.
> - Summary cards depend on the built-in Summarizer agent, but the two
capabilities have independent feature flags.
> - `SummarySlotCard` and the reusable `BuiltInAgentGate` enabled
built-in-agent lookups without requiring `enableBuiltInAgents`.
> - When built-in agents were disabled, those surfaces called a server
route that was intentionally unavailable and generated avoidable 404s.
> - The client query should obey both server-side feature gates.
> - This pull request adds the missing gate and a cross-flag regression.
> - The benefit is consistent feature-flag behavior and no request loop
against a disabled endpoint.

## Linked Issues or Issue Description

No exact duplicate found. I searched open PRs for `SummarySlotCard`,
`BuiltInAgentGate`, `enableBuiltInAgents summaries`, and `built-in
agents 404`. Related PR #10116 gates `SidebarAgents`; this PR
deliberately excludes that file and covers the remaining summary/gate
callers.

**What happened?**

`SummarySlotCard` called `builtInAgentsApi.list` whenever summaries were
enabled, and `BuiltInAgentGate` called it whenever a company was
selected. The server rejects that route when `enableBuiltInAgents` is
false, so the disabled configuration produced repeated 404 requests.

**Expected behavior**

Built-in-agent queries should run only when built-in agents are enabled;
the summary-specific query also requires summaries to be enabled.

**Steps to reproduce**

1. Enable summaries.
2. Disable built-in agents.
3. Render a page containing `SummarySlotCard` or `BuiltInAgentGate`.
4. Observe a request to the disabled built-in-agents route.

**Environment**

- Paperclip web UI
- Cross-flag configuration: summaries enabled, built-in agents disabled

- [x] I searched open PRs for the affected component, feature flags, and
404 behavior; no exact duplicate was found.

## What Changed

- Require both `enableSummaries` and `enableBuiltInAgents` in
`SummarySlotCard`.
- Make `BuiltInAgentGate` resolve experimental settings before enabling
its built-in-agent query and fail open when the feature is disabled.
- Add cross-flag regressions for both callers.
- Leave `SidebarAgents` to related PR #10116 rather than duplicating it.

## Verification

- `pnpm exec vitest run ui/src/components/SummarySlotCard.test.tsx
ui/src/components/BuiltInAgentGate.test.tsx` — passed as part of a
41-test built-in UI group.
- `pnpm --filter @paperclipai/ui typecheck` — passed.
- `pnpm --filter @paperclipai/ui build` — passed in combined deployment
staging.
- UI-only local cutover completed with the Paperclip server PID
unchanged.
- The complete UI fix was staged after a summary-only cutover exposed
the remaining reusable-gate caller.

## Risks

- Low risk: this changes only whether one query is enabled under a
feature-flag combination where the server route is unavailable.
- No API, schema, migration, authentication, or persistence changes.
- Rollback is a single commit revert.

> This is a bug fix, not roadmap feature work.

## Model Used

OpenAI Codex `gpt-5.6-sol`, with tool use, code execution, repository
inspection, and read-only review-agent evidence.

## Checklist

- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and described
the search above
- [x] I have described the issue in-PR following the bug template
- [x] I have not referenced internal/instance-local Paperclip issues or
links
- [x] My branch name describes the change and contains no internal
ticket id
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [x] I have considered documentation; no user-facing documentation
change is required
- [x] I have considered and documented risks above
- [x] All Paperclip CI gates are green
- [x] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge

---------

Co-authored-by: cucurigoo <cucurigoo@users.noreply.github.com>
2026-08-12 16:43:22 -07:00

132 lines
4.9 KiB
TypeScript

import { useState, type ReactNode } from "react";
import { Link } from "react-router-dom";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { Bot, Clock3 } from "lucide-react";
import { EmptyState } from "@/components/EmptyState";
import { InlineBanner } from "@/components/InlineBanner";
import { PageSkeleton } from "@/components/PageSkeleton";
import { Button } from "@/components/ui/button";
import { ConfigureBuiltInAgentModal } from "@/components/ConfigureBuiltInAgentModal";
import { builtInAgentsApi, type BuiltInAgentState } from "@/api/builtInAgents";
import { agentsApi } from "@/api/agents";
import { instanceSettingsApi } from "@/api/instanceSettings";
import { queryKeys } from "@/lib/queryKeys";
import { agentUrl } from "@/lib/utils";
import { relativeTime } from "@/lib/utils";
export interface BuiltInAgentGateProps {
/** Registry key of the built-in agent that powers this feature (e.g. "briefs"). */
agentKey: string;
companyId: string | null | undefined;
/** Human label for the gated feature. Defaults to the agent's display name. */
featureLabel?: string;
children: ReactNode;
}
/**
* Wraps a feature surface that depends on a built-in agent and renders the
* right lifecycle state (ux-spec §4):
*
* - loading → skeleton
* - not_provisioned / needs_setup → setup empty-state + configure modal CTA
* - paused → amber banner + Resume over the (stale) children
* - ready → children
*/
export function BuiltInAgentGate({ agentKey, companyId, featureLabel, children }: BuiltInAgentGateProps) {
const queryClient = useQueryClient();
const [configureOpen, setConfigureOpen] = useState(false);
const experimentalQuery = useQuery({
queryKey: queryKeys.instance.experimentalSettings,
queryFn: () => instanceSettingsApi.getExperimental(),
});
const builtInAgentsEnabled = experimentalQuery.data?.enableBuiltInAgents === true;
const { data: states, isLoading: statesLoading } = useQuery({
queryKey: queryKeys.builtInAgents.list(companyId ?? "__none__"),
queryFn: () => builtInAgentsApi.list(companyId!),
enabled: Boolean(companyId && builtInAgentsEnabled),
});
const state: BuiltInAgentState | undefined = states?.find((entry) => entry.definition.key === agentKey);
const resume = useMutation({
mutationFn: (agentId: string) => agentsApi.resume(agentId, companyId ?? undefined),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: queryKeys.builtInAgents.list(companyId ?? "__none__") });
if (companyId) queryClient.invalidateQueries({ queryKey: queryKeys.agents.list(companyId) });
},
});
// Unknown key or still resolving the company — fail open to the feature.
if (!companyId) return <>{children}</>;
if ((experimentalQuery.isLoading || statesLoading) && !states) return <PageSkeleton variant="detail" />;
if (!state) return <>{children}</>;
const label = featureLabel ?? state.definition.displayName;
if (state.status === "not_provisioned" || state.status === "needs_setup") {
return (
<>
<EmptyState
icon={Bot}
title={`Set up the ${state.definition.displayName}`}
message={`${label} is generated by a built-in agent. Configure its model to enable the feature.`}
action={`Set up ${state.definition.displayName}`}
onAction={() => setConfigureOpen(true)}
hideActionIcon
/>
<ConfigureBuiltInAgentModal
companyId={companyId}
state={state}
open={configureOpen}
onOpenChange={setConfigureOpen}
/>
</>
);
}
if (state.status === "pending_approval") {
return (
<EmptyState
icon={Clock3}
title={`${state.definition.displayName} is pending approval`}
message={`${label} will be available after the board approves this built-in agent hire.`}
/>
);
}
if (state.status === "paused" && state.agent) {
const pausedAt = state.agent.pausedAt ? relativeTime(state.agent.pausedAt) : null;
return (
<div className="space-y-4">
<InlineBanner
tone="warning"
title={`${label} is paused.`}
actions={
<>
<Button variant="ghost" size="sm" asChild>
<Link to={agentUrl(state.agent)}>View agent</Link>
</Button>
<Button
size="sm"
onClick={() => state.agent && resume.mutate(state.agent.id)}
disabled={resume.isPending}
>
{resume.isPending ? "Resuming…" : "Resume agent"}
</Button>
</>
}
>
Its built-in agent was paused{pausedAt ? ` ${pausedAt}` : ""}, so new{" "}
{label.toLowerCase()} isn't being generated.
</InlineBanner>
{/* Paused ≠ hidden: keep existing content readable, marked stale. */}
<div className="opacity-70">{children}</div>
</div>
);
}
return <>{children}</>;
}