Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

Abuse Guard auto-selection prefers a provider whose window fits a PR (#12843) (#12848)

[._.]/184eba24cbba
diff --git a/server/lib/untrustedContent.js b/server/lib/untrustedContent.js
index 7873436f79..d8f7dae6e7 100644
--- a/server/lib/untrustedContent.js
+++ b/server/lib/untrustedContent.js
@@ -79,6 +79,9 @@ export function isUntrustedContentProvider(provider, source) {
const PROVIDER_UNAVAILABLE = Object.freeze({ code: 'untrusted-content-provider-unavailable', message: 'Configure an enabled text API provider in Models > LLMs > Abuse Guard. Private messages require a local API endpoint. CLI agents and provider fallback are disabled for external content.' });
+/** Smallest window automatic selection treats as able to hold a real PR. */
+export const MIN_VIABLE_CONTEXT_TOKENS = 16384;
+
/**
* Resolve the provider and model an untrusted-content analysis will use.
*
@@ -87,8 +90,14 @@ const PROVIDER_UNAVAILABLE = Object.freeze({ code: 'untrusted-content-provider-u
* provider without a default model never shadows a usable one. This is an
* eligibility rule at selection time, not a fallback after a failed call: a
* pinned or explicit provider that cannot resolve a model fails closed.
+ *
+ * When the caller supplies `windowOf(provider, model)` (the same budget ladder
+ * the analysis uses), automatic selection also prefers a candidate whose window
+ * reaches MIN_VIABLE_CONTEXT_TOKENS, so an unconfigured local endpoint floored
+ * at 4096 never shadows a provider that can hold a real PR. If none qualifies
+ * the first candidate is kept: single-provider installs behave as before.
*/
-export function selectUntrustedContentProvider({ providers, policy, source, provider = null, model = null }) {
+export function selectUntrustedContentProvider({ providers, policy, source, provider = null, model = null, windowOf = null }) {
if (provider || policy.providerId) {
const selected = provider || (providers || []).find(item => item.id === policy.providerId);
if (!isUntrustedContentProvider(selected, source)) return PROVIDER_UNAVAILABLE;
@@ -99,9 +108,13 @@ export function selectUntrustedContentProvider({ providers, policy, source, prov
}
const eligible = (providers || []).filter(item => isUntrustedContentProvider(item, source));
if (!eligible.length) return PROVIDER_UNAVAILABLE;
+ const candidates = [];
for (const candidate of eligible) {
const effectiveModel = model || policy.model || candidate.defaultModel;
- if (effectiveModel) return { provider: candidate, model: effectiveModel };
+ if (effectiveModel) candidates.push({ provider: candidate, model: effectiveModel });
+ }
+ if (candidates.length) {
+ return (windowOf && candidates.find(item => windowOf(item.provider, item.model) >= MIN_VIABLE_CONTEXT_TOKENS)) || candidates[0];
}
return { code: 'untrusted-content-model-required', message: 'No eligible API provider has a default model. Select an installed text model in Models > LLMs > Abuse Guard.' };
}
diff --git a/server/services/prReviewerPipeline.js b/server/services/prReviewerPipeline.js
index 75fb53c846..61072f182a 100644
--- a/server/services/prReviewerPipeline.js
+++ b/server/services/prReviewerPipeline.js
@@ -306,7 +306,10 @@ export async function runPrReviewerSecurityPreflight(taskType, app, metadata, ta
const reports = securityScanReports(scan);
if (!scan.ok && !reports.length) {
const reason = scan.code || 'security-scan-not-passed';
- emitLog('warn', `Skipping pr-reviewer for ${app.name}: ${reason}`, { appId: app.id, analysisType: taskType });
+ // `message` is server-authored guard text (provider, window, fix hint), never contribution content.
+ const detail = typeof scan.message === 'string' ? scan.message.replace(/[\r\n]+/g, ' ').trim().slice(0, 400) : '';
+ const hint = detail ? ` — ${detail}` : '';
+ emitLog('warn', `Skipping pr-reviewer for ${app.name}: ${reason}${hint}`, { appId: app.id, analysisType: taskType });
return { skipped: true, reason };
}
diff --git a/server/services/prReviewerSecurity.js b/server/services/prReviewerSecurity.js
index ac6bbb27d3..26f27ad89b 100644
--- a/server/services/prReviewerSecurity.js
+++ b/server/services/prReviewerSecurity.js
@@ -430,7 +430,7 @@ export async function runPrReviewerSecurityScan({ app, target = null, largeInput
? { screening: structuralVerdict(structural) }
: await screenUntrustedContent({ content, source: 'github-pr' });
const verdict = screened.screening || screened;
- if (!verdict.ok) return failure(verdict.code || 'security-scan-verdict-unavailable', { reviewedPrs, scanKey });
+ if (!verdict.ok) return failure(verdict.code || 'security-scan-verdict-unavailable', { reviewedPrs, scanKey, ...(typeof verdict.message === 'string' ? { message: verdict.message } : {}) });
guardModel = verdict.model || guardModel;
guardRevision = verdict.revision ?? null;
diff --git a/server/services/untrustedContent.js b/server/services/untrustedContent.js
index c9358eb87d..9abfcae196 100644
--- a/server/services/untrustedContent.js
+++ b/server/services/untrustedContent.js
@@ -299,7 +299,7 @@ export async function getContributionAssessmentReadiness() {
const sources = {};
for (const source of ['github-pr', 'github-issue']) {
const policy = resolveUntrustedContentPolicy(state.settings.untrustedContent, source);
- const selection = policy ? selectUntrustedContentProvider({ providers, policy, source }) : { code: 'untrusted-content-policy-invalid' };
+ const selection = policy ? selectProvider({ providers, policy, source }) : { code: 'untrusted-content-policy-invalid' };
sources[source] = selection.provider
? { ready: true, providerId: selection.provider.id, model: selection.model }
: { ready: false, code: selection.code };
@@ -319,7 +319,7 @@ export async function getContributionAssessmentFingerprint(source = 'github-pr')
if (state.corrupt) return 'settings-unreadable';
const policy = resolveUntrustedContentPolicy(state.settings.untrustedContent, source);
const providers = (await getAllProviders()).providers || [];
- const selection = policy ? selectUntrustedContentProvider({ providers, policy, source }) : null;
+ const selection = policy ? selectProvider({ providers, policy, source }) : null;
const chosen = selection?.provider;
return createHash('sha256').update(JSON.stringify({
policy,
@@ -329,40 +329,43 @@ export async function getContributionAssessmentFingerprint(source = 'github-pr')
})).digest('hex');
}
+/**
+ * The token budget an analysis against this provider/model gets.
+ *
+ * The runtime projection folds in the ambient `OLLAMA_CONTEXT_LENGTH` ceiling
+ * before the shared ladder resolves anything, so an Ollama daemon configured
+ * through the env var alone still budgets at what it serves (#7472).
+ * `knownContextWindow` clamps on `numCtx` only for Ollama-backed providers;
+ * other OpenAI-compatible local endpoints ignore `num_ctx` and must not be
+ * shrunk by it. A local endpoint with no explicit `numCtx` keeps this guard's
+ * own 4096 floor: this is a security guard, and the floor only ever LOWERS the
+ * shared ladder's answer.
+ */
+function resolveUntrustedContentWindow(selected, effectiveModel) {
+ const runtimeProvider = withOllamaRuntimeContextWindow(selected);
+ const claimedWindow = knownContextWindow(runtimeProvider, effectiveModel) ?? 4096;
+ const local = isUntrustedContentProvider(selected, 'messages');
+ return local && !(Number(runtimeProvider.numCtx) > 0) ? Math.min(claimedWindow, 4096) : claimedWindow;
+}
+const selectProvider = (args) => selectUntrustedContentProvider({ ...args, windowOf: resolveUntrustedContentWindow });
+
// Private transport entry: only callers above can supply a completed screen.
async function analyzeScreenedContent({ provider, model, content, prompt, source, responseSchema, screened }) {
const config = screened.policy;
const providers = provider ? [provider] : (await getAllProviders()).providers || [];
- const selection = selectUntrustedContentProvider({ providers, policy: config, source, provider, model });
+ const selection = selectProvider({ providers, policy: config, source, provider, model });
if (!selection.provider) return failure(selection.code, selection.message);
const { provider: selected, model: effectiveModel } = selection;
const taskPrompt = `${UNTRUSTED_CONTENT_INSTRUCTIONS}\n\nTRUSTED TASK:\n${prompt}`;
const evidence = formatUntrustedContent(content);
- const local = isUntrustedContentProvider(selected, 'messages');
- // The runtime projection folds in the ambient `OLLAMA_CONTEXT_LENGTH`
- // ceiling before the shared ladder resolves anything, so an Ollama daemon
- // configured through the env var alone (numCtx unset) still budgets at
- // what it actually serves rather than an unenforced catalog window (#7472).
- // `knownContextWindow` then resolves through the one shared `num_ctx`
- // clamp (`aiToolkit/internal/ollamaBacked.js#clampToRuntimeContextWindow`)
- // instead of this guard's own Math.min pair, so it picks up the provider's
- // own `/models` catalog window and only clamps on `numCtx` for an
- // Ollama-backed provider — every other OpenAI-compatible local endpoint
- // (LM Studio, vLLM, …) ignores `num_ctx` entirely and must not be shrunk
- // by it.
- const runtimeProvider = withOllamaRuntimeContextWindow(selected);
- const claimedWindow = knownContextWindow(runtimeProvider, effectiveModel) ?? 4096;
- // A local endpoint with no explicit `numCtx` keeps this guard's own harder
- // floor rather than trusting a declared/catalog window an unconfigured
- // local endpoint has no confirmed way to honor — this is a security guard
- // that must stay conservative, not a budgeting nicety. Preserved verbatim
- // from the pre-#7473 behavior; it only ever LOWERS the shared ladder's
- // answer, so it never re-opens the catalog-window blind spot #7472 closed.
- const contextWindow = local && !(Number(runtimeProvider.numCtx) > 0) ? Math.min(claimedWindow, 4096) : claimedWindow;
+ const contextWindow = resolveUntrustedContentWindow(selected, effectiveModel);
const maxTokens = Math.min(8192, config.maxOutputChars, Math.floor(contextWindow / 4));
// UTF-8 bytes are a conservative upper bound for byte-fallback text tokens.
// Never clip evidence to make an undersized context appear successful.
- if (Buffer.byteLength(taskPrompt + evidence, 'utf8') + maxTokens + 128 > contextWindow) return failure('untrusted-content-context-too-small', 'The complete evidence does not fit this provider context. Increase its context size or analyze a smaller complete batch.');
+ const needed = Buffer.byteLength(taskPrompt + evidence, 'utf8');
+ if (needed + maxTokens + 128 > contextWindow) {
+ return failure('untrusted-content-context-too-small', `The complete evidence (about ${needed} bytes) does not fit ${selected.id} / ${effectiveModel}, whose resolved window is ${contextWindow} tokens. Set numCtx on that provider or pin a larger-window provider in Models > LLMs > Abuse Guard; or analyze a smaller complete batch.`);
+ }
const endpointPolicy = selected.apiKey ? evaluateSecretEndpoint(selected.endpoint, { allowCustomEndpoint: selected.allowCustomEndpoint === true }) : { allowed: true };
if (!endpointPolicy.allowed) return failure('untrusted-content-endpoint-blocked', 'The configured API endpoint cannot receive this provider credential.');
const { ensureProviderReadyForExecution } = await import('./providerExecutionReadiness.js');
diff --git a/server/services/untrustedContent.test.js b/server/services/untrustedContent.test.js
index a05bf98cd2..cd8756b0ce 100644
--- a/server/services/untrustedContent.test.js
+++ b/server/services/untrustedContent.test.js
@@ -5,7 +5,7 @@ vi.mock('./modelAbuseGuard.js', () => ({ runModelAbuseScan: mocks.scan }));
vi.mock('./settings.js', () => ({ readSettingsStrict: mocks.read }));
vi.mock('./providers.js', () => ({ getAllProviders: mocks.providers }));
vi.mock('./providerExecutionReadiness.js', () => ({ ensureProviderReadyForExecution: async () => ({ success: true }) }));
-import { getContributionAssessmentReadiness, runUntrustedContentAnalysis, screenUntrustedContent } from './untrustedContent.js';
+import { getContributionAssessmentFingerprint, getContributionAssessmentReadiness, runUntrustedContentAnalysis, screenUntrustedContent } from './untrustedContent.js';
const local = { id: 'local', type: 'api', enabled: true, endpoint: 'http://127.0.0.1:11434/v1', defaultModel: 'example-text' };
const cloud = { ...local, id: 'cloud', contextWindow: 32768, endpoint: 'https://api.example.com/v1' };
const args = { content: 'Example sender asks about a meeting.', prompt: 'Return {"action":"review"}.', source: 'messages', responseSchema: z.object({ action: z.literal('review') }).strict() };
@@ -236,4 +236,31 @@ describe('automatic provider selection requires a resolvable model', () => {
expect(mocks.fetch).not.toHaveBeenCalled();
expect(await getContributionAssessmentReadiness()).toMatchObject({ ready: false, code: 'untrusted-content-model-required' });
});
+ describe('window-aware automatic selection', () => {
+ const floored = { ...local, id: 'floored', defaultModel: 'example-floored' };
+ const roomy = { ...cloud, id: 'roomy', defaultModel: 'example-roomy' };
+ const pr = { source: 'github-pr', content: 'x'.repeat(6000) };
+ it('prefers a candidate with a viable window over an earlier 4096-floored one', async () => {
+ mocks.providers.mockResolvedValue({ providers: [floored, roomy] });
+ expect(await getContributionAssessmentReadiness()).toMatchObject({ sources: { 'github-pr': { ready: true, providerId: 'roomy' } } });
+ const both = await getContributionAssessmentFingerprint();
+ mocks.providers.mockResolvedValue({ providers: [roomy] });
+ expect(await getContributionAssessmentFingerprint()).toBe(both);
+ mocks.providers.mockResolvedValue({ providers: [floored, roomy] });
+ const run = await runUntrustedContentAnalysis({ ...args, ...pr });
+ expect(run).toMatchObject({ ok: true, providerId: 'roomy' });
+ });
+ it('keeps the first candidate when none qualifies and names provider, model and window on refusal', async () => {
+ mocks.providers.mockResolvedValue({ providers: [floored] });
+ const result = await runUntrustedContentAnalysis({ ...args, ...pr });
+ expect(result).toMatchObject({ ok: false, code: 'untrusted-content-context-too-small' });
+ expect(result.message).toMatch(/floored \/ example-floored.*4096 tokens.*numCtx.*Abuse Guard/);
+ expect(mocks.fetch).not.toHaveBeenCalled();
+ });
+ it('does not reselect when a provider is pinned', async () => {
+ mocks.providers.mockResolvedValue({ providers: [floored, roomy] });
+ mocks.read.mockResolvedValue({ corrupt: false, settings: { untrustedContent: { defaults: { providerId: 'floored' } } } });
+ expect(await runUntrustedContentAnalysis({ ...args, ...pr })).toMatchObject({ ok: false, code: 'untrusted-content-context-too-small' });
+ });
+ });
});