Skip to content

Commit 9e50bb4

Browse files
fix: address review findings on text generators
- Unsaved configs can no longer reach an API under a managed provider allow-list. - Saving a generator re-reads its model list, so a corrected key is not masked by the redacted cache key. - The setup form is locked while Connect checks it, so the saved config is the checked one. - Shared selections across environments need the same kind and base URL, not just the same brand. - Review guide fallback resolves against agents the project has enabled. - Placeholder models are deduplicated; docs note localhost runs on the server and the review guide fallback. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 1caf795 commit 9e50bb4

8 files changed

Lines changed: 201 additions & 116 deletions

File tree

‎apps/server/src/textGeneration/TextGenerators.test.ts‎

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { it } from "@effect/vitest";
2-
import { ProviderInstanceId, type TextGeneratorConfig } from "@t3tools/contracts";
2+
import {
3+
type ManagedPolicySummary,
4+
ProviderDriverKind,
5+
ProviderInstanceId,
6+
type TextGeneratorConfig,
7+
} from "@t3tools/contracts";
38
import * as SecretResolver from "@t3tools/secret-provider-core/server/SecretResolver";
49
import * as Effect from "effect/Effect";
510
import * as Layer from "effect/Layer";
@@ -13,14 +18,17 @@ import * as TextGenerators from "./TextGenerators.ts";
1318

1419
const ID = ProviderInstanceId.make("router");
1520

16-
const layer = (generator: Partial<TextGeneratorConfig>) =>
21+
const layer = (generator: Partial<TextGeneratorConfig>, policy?: ManagedPolicySummary) =>
1722
Layer.effect(TextGenerators.TextGenerators, TextGenerators.make).pipe(
1823
Layer.provideMerge(
19-
ServerSettings.layerTest({
20-
textGenerators: {
21-
[ID]: { kind: "openaiCompatible", baseUrl: "https://api.test/v1", ...generator },
24+
ServerSettings.layerTest(
25+
{
26+
textGenerators: {
27+
[ID]: { kind: "openaiCompatible", baseUrl: "https://api.test/v1", ...generator },
28+
},
2229
},
23-
}),
30+
policy,
31+
),
2432
),
2533
Layer.provide(FetchHttpClient.layer),
2634
Layer.provide(Layer.mock(SecretResolver.SecretResolver)({})),
@@ -189,5 +197,39 @@ it.effect("checks unsaved settings, keeping the saved key behind a redaction mar
189197
url: "https://api.test/v1/chat/completions",
190198
auth: "Bearer saved-key",
191199
});
200+
201+
// An allow-list names agent drivers only, so no unsaved config may reach an API either.
202+
const managed = yield* TextGenerators.TextGenerators.pipe(
203+
Effect.provide(
204+
layer(
205+
{ apiKey: "saved-key" },
206+
{
207+
organization: "Example Corp",
208+
lockedSettings: [],
209+
limits: {
210+
providers: {
211+
allowedDrivers: [ProviderDriverKind.make("codex")],
212+
lockedInstanceFields: {},
213+
},
214+
},
215+
},
216+
),
217+
),
218+
);
219+
const requestsBefore = seen.length;
220+
const refused = yield* managed
221+
.listModels({
222+
id: ProviderInstanceId.make("draft"),
223+
config: {
224+
kind: "openaiCompatible",
225+
baseUrl: "https://new.test/v1",
226+
apiKey: "k",
227+
enabled: true,
228+
},
229+
test: true,
230+
})
231+
.pipe(Effect.provideService(FetchHttpClient.Fetch, upstream), Effect.flip);
232+
expect(refused.detail).toContain("policy");
233+
expect(seen).toHaveLength(requestsBefore);
192234
}),
193235
);

‎apps/server/src/textGeneration/TextGenerators.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ export const make = Effect.gen(function* () {
6262
const secrets = yield* SecretResolver.SecretResolver;
6363
const http = yield* HttpClient.HttpClient;
6464
const modelCache = yield* Ref.make(new Map<ProviderInstanceId, CachedModels>());
65+
// Settings already drop saved generators under an allow-list; this stops unsaved ones too.
66+
const allowListed =
67+
settingsService.managedPolicy?.limits?.providers?.allowedDrivers !== undefined;
6568

6669
const readConfig = Effect.fn("TextGenerators.readConfig")(function* (id: ProviderInstanceId) {
6770
const settings = yield* settingsService.getSettings.pipe(
@@ -121,6 +124,11 @@ export const make = Effect.gen(function* () {
121124

122125
const listModels = Effect.fn("TextGenerators.listModels")(
123126
function* (input: ListTextGeneratorModelsInput) {
127+
if (allowListed) {
128+
return yield* new TextGeneratorRequestError({
129+
detail: "Your organization's provider policy turns off text generators.",
130+
});
131+
}
124132
const saved = yield* readConfig(input.id);
125133
const config =
126134
input.config === undefined

‎apps/web/src/components/settings/SourceControlWritingSettings.tsx‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,20 +161,26 @@ export function SourceControlWritingSettingsSection() {
161161
(entry) => entry.instanceId === activeSelection.instanceId,
162162
);
163163
const usesReviewGuideModel = settings.reviewGuideModelSelection !== null;
164+
// Resolve against agents this project can use, so the fallback never lands on one it turned off.
165+
const projectAgentProviders = textGenerationProviders.filter((provider) =>
166+
instanceEntries.some(
167+
(entry) => entry.instanceId === provider.instanceId && entry.enabled && entry.isAvailable,
168+
),
169+
);
164170
const reviewGuideSelection = resolveAppModelSelectionState(
165171
{
166172
...settings,
167173
textGenerationModelSelection: resolveReviewGuideModelSelection(
168174
settings,
169-
textGenerationProviders,
175+
projectAgentProviders,
170176
target?.projectId ?? null,
171177
),
172178
},
173-
textGenerationProviders,
179+
projectAgentProviders,
174180
);
175181
const reviewGuideModelOptionsByInstance = getCustomModelOptionsByInstance(
176182
settings,
177-
textGenerationProviders,
183+
projectAgentProviders,
178184
reviewGuideSelection.instanceId,
179185
reviewGuideSelection.model,
180186
);

‎apps/web/src/components/settings/TextGeneratorsSettings.tsx‎

Lines changed: 101 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { useState } from "react";
1717
import { useEnvironmentSettings, useUpdateEnvironmentSettings } from "../../hooks/useSettings";
1818
import {
1919
checkTextGeneratorConfig,
20+
rereadTextGeneratorModels,
2021
nextTextGeneratorId,
2122
refreshTextGeneratorModels,
2223
testTextGeneratorModel,
@@ -177,7 +178,12 @@ export function TextGeneratorsSettings({
177178
editing={editing}
178179
providers={providers}
179180
onClose={() => setEditing(null)}
180-
onSave={(id, config) => updateSettings({ textGenerators: { [id]: config } })}
181+
onSave={(id, config) =>
182+
updateSettings(
183+
{ textGenerators: { [id]: config } },
184+
{ onSaved: () => rereadTextGeneratorModels(environmentId, id) },
185+
)
186+
}
181187
newId={(preset) => nextTextGeneratorId(preset.id, settings, providers)}
182188
/>
183189
) : null}
@@ -297,6 +303,7 @@ function TextGeneratorWizard({
297303
const id = savedId ?? newId(preset);
298304
setChecking(true);
299305
setCheckError(null);
306+
// The form is disabled while this runs, so the checked config is the one saved below.
300307
const result = await checkTextGeneratorConfig(environmentId, id, config.value);
301308
setChecking(false);
302309
if ("error" in result) {
@@ -337,104 +344,106 @@ function TextGeneratorWizard({
337344
void connect();
338345
}}
339346
>
340-
{existing === null ? (
341-
<div className="grid gap-1.5">
342-
<span className="text-sm font-medium">Service</span>
343-
<div
344-
role="radiogroup"
345-
aria-label="Service"
346-
className="grid grid-cols-2 gap-2 sm:grid-cols-4"
347-
>
348-
{TEXT_GENERATOR_PRESETS.map((candidate) => {
349-
const selected = candidate.id === preset.id;
350-
return (
351-
<button
352-
key={candidate.id}
353-
type="button"
354-
role="radio"
355-
aria-checked={selected}
356-
className={
357-
selected
358-
? "flex flex-col items-center gap-2 rounded-lg border border-primary bg-primary/5 px-2 py-3 text-sm font-medium text-foreground ring-1 ring-primary ring-inset outline-none focus-visible:ring-2 focus-visible:ring-inset"
359-
: "flex flex-col items-center gap-2 rounded-lg border bg-background px-2 py-3 text-sm text-muted-foreground outline-none hover:bg-accent/50 hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-inset"
360-
}
361-
onClick={() => {
362-
setPreset(candidate);
363-
setBaseUrl(candidate.baseUrl);
364-
setCheckError(null);
365-
}}
366-
>
367-
{candidate.id === "custom" ? (
368-
<ServerIcon className="size-5" aria-hidden />
369-
) : (
370-
<ProviderInstanceIcon
371-
driverKind={textGeneratorBrand(candidate)}
372-
displayName={candidate.label}
373-
badgeContent="none"
374-
/>
375-
)}
376-
{candidate.id === "custom" ? "Custom" : candidate.label}
377-
</button>
378-
);
379-
})}
347+
<fieldset disabled={checking} className="contents">
348+
{existing === null ? (
349+
<div className="grid gap-1.5">
350+
<span className="text-sm font-medium">Service</span>
351+
<div
352+
role="radiogroup"
353+
aria-label="Service"
354+
className="grid grid-cols-2 gap-2 sm:grid-cols-4"
355+
>
356+
{TEXT_GENERATOR_PRESETS.map((candidate) => {
357+
const selected = candidate.id === preset.id;
358+
return (
359+
<button
360+
key={candidate.id}
361+
type="button"
362+
role="radio"
363+
aria-checked={selected}
364+
className={
365+
selected
366+
? "flex flex-col items-center gap-2 rounded-lg border border-primary bg-primary/5 px-2 py-3 text-sm font-medium text-foreground ring-1 ring-primary ring-inset outline-none focus-visible:ring-2 focus-visible:ring-inset"
367+
: "flex flex-col items-center gap-2 rounded-lg border bg-background px-2 py-3 text-sm text-muted-foreground outline-none hover:bg-accent/50 hover:text-foreground focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-inset"
368+
}
369+
onClick={() => {
370+
setPreset(candidate);
371+
setBaseUrl(candidate.baseUrl);
372+
setCheckError(null);
373+
}}
374+
>
375+
{candidate.id === "custom" ? (
376+
<ServerIcon className="size-5" aria-hidden />
377+
) : (
378+
<ProviderInstanceIcon
379+
driverKind={textGeneratorBrand(candidate)}
380+
displayName={candidate.label}
381+
badgeContent="none"
382+
/>
383+
)}
384+
{candidate.id === "custom" ? "Custom" : candidate.label}
385+
</button>
386+
);
387+
})}
388+
</div>
380389
</div>
381-
</div>
382-
) : null}
383-
{showBaseUrl ? (
390+
) : null}
391+
{showBaseUrl ? (
392+
<div className="grid gap-1.5">
393+
<Label htmlFor="text-generator-url">Base URL</Label>
394+
<Input
395+
id="text-generator-url"
396+
placeholder="http://localhost:11434/v1"
397+
value={baseUrl}
398+
onChange={(event) => {
399+
setBaseUrl(event.target.value);
400+
setCheckError(null);
401+
}}
402+
spellCheck={false}
403+
autoFocus={existing === null && preset.id === "custom"}
404+
/>
405+
</div>
406+
) : null}
384407
<div className="grid gap-1.5">
385-
<Label htmlFor="text-generator-url">Base URL</Label>
386-
<Input
387-
id="text-generator-url"
388-
placeholder="http://localhost:11434/v1"
389-
value={baseUrl}
408+
<Label htmlFor="text-generator-key">
409+
{keyRequired ? "API key" : "API key (optional)"}
410+
</Label>
411+
<SecretSourceInput
412+
id="text-generator-key"
413+
aria-label="API key"
414+
value={keyDraft}
415+
configured={storedKey !== ""}
416+
sources={secretSources}
390417
onChange={(event) => {
391-
setBaseUrl(event.target.value);
418+
setKeyDraft(event.target.value);
392419
setCheckError(null);
393420
}}
394-
spellCheck={false}
395-
autoFocus={existing === null && preset.id === "custom"}
421+
onSecretChange={(secret) => setKeyDraft(secret ?? "")}
422+
autoFocus={existing === null && preset.id !== "custom"}
423+
onValidityChange={setKeyValid}
396424
/>
397425
</div>
398-
) : null}
399-
<div className="grid gap-1.5">
400-
<Label htmlFor="text-generator-key">
401-
{keyRequired ? "API key" : "API key (optional)"}
402-
</Label>
403-
<SecretSourceInput
404-
id="text-generator-key"
405-
aria-label="API key"
406-
value={keyDraft}
407-
configured={storedKey !== ""}
408-
sources={secretSources}
409-
onChange={(event) => {
410-
setKeyDraft(event.target.value);
411-
setCheckError(null);
412-
}}
413-
onSecretChange={(secret) => setKeyDraft(secret ?? "")}
414-
autoFocus={existing === null && preset.id !== "custom"}
415-
onValidityChange={setKeyValid}
416-
/>
417-
</div>
418-
<div className="grid gap-1.5">
419-
<Label htmlFor="text-generator-label">Name (optional)</Label>
420-
<Input
421-
id="text-generator-label"
422-
placeholder={
423-
existing !== null
424-
? textGeneratorDisplayName(existing)
425-
: preset.id === "custom"
426-
? "e.g. My local model"
427-
: preset.label
428-
}
429-
value={label}
430-
onChange={(event) => setLabel(event.target.value)}
431-
/>
432-
</div>
433-
{checkError ? (
434-
<p role="alert" className="text-sm text-destructive">
435-
{checkError}
436-
</p>
437-
) : null}
426+
<div className="grid gap-1.5">
427+
<Label htmlFor="text-generator-label">Name (optional)</Label>
428+
<Input
429+
id="text-generator-label"
430+
placeholder={
431+
existing !== null
432+
? textGeneratorDisplayName(existing)
433+
: preset.id === "custom"
434+
? "e.g. My local model"
435+
: preset.label
436+
}
437+
value={label}
438+
onChange={(event) => setLabel(event.target.value)}
439+
/>
440+
</div>
441+
{checkError ? (
442+
<p role="alert" className="text-sm text-destructive">
443+
{checkError}
444+
</p>
445+
) : null}
446+
</fieldset>
438447
</form>
439448
) : (
440449
<div className="grid gap-4">

‎apps/web/src/components/settings/useScopedModelAvailability.ts‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66

77
import { mergeEnvironmentSettings } from "../../hooks/useSettings";
88
import { getCustomModelOptionsByInstance } from "../../modelSelection";
9-
import { textGeneratorBrand } from "../../textGenerators";
109
import {
1110
applyProviderInstanceSettings,
1211
deriveProviderInstanceEntries,
@@ -24,16 +23,20 @@ export function useScopedModelDisabledReason(
2423
const providersByEnvironment = useEnvironmentProviders();
2524
return (instanceId: ProviderInstanceId, model: string): string | null => {
2625
const sourceEntry = entries.find((entry) => entry.instanceId === instanceId);
27-
const isTextGenerator = textGeneratorForSelection(settings, instanceId) !== undefined;
26+
const shownGenerator = textGeneratorForSelection(settings, instanceId);
2827
for (const candidate of targets) {
2928
const environment = environments.find(
3029
(entry) => entry.environmentId === candidate.environmentId,
3130
);
3231
const unavailable = `This model is unavailable on ${environment?.label ?? "a selected environment"}. Select that environment to choose its model separately.`;
3332
const generator = textGeneratorForSelection(candidate.settings, instanceId);
34-
if (isTextGenerator || generator !== undefined) {
35-
// Only the shown environment's model list is loaded; elsewhere the generator must match.
36-
if (!generator?.enabled || textGeneratorBrand(generator) !== sourceEntry?.driverKind) {
33+
if (shownGenerator !== undefined || generator !== undefined) {
34+
// Only the shown environment's model list is loaded; elsewhere it must reach the same API.
35+
if (
36+
!generator?.enabled ||
37+
generator.kind !== shownGenerator?.kind ||
38+
generator.baseUrl !== shownGenerator.baseUrl
39+
) {
3740
return unavailable;
3841
}
3942
continue;

0 commit comments

Comments
 (0)