Skip to content

feat: Add Mercury 2 (Inception Labs) as primary model in fallback chain#29

Open
aj47 wants to merge 2 commits intomainfrom
mercury-2-integration
Open

feat: Add Mercury 2 (Inception Labs) as primary model in fallback chain#29
aj47 wants to merge 2 commits intomainfrom
mercury-2-integration

Conversation

@aj47
Copy link
Owner

@aj47 aj47 commented Feb 24, 2026

Summary

Adds Mercury Coder by Inception Labs as the first target in the Portkey fallback chain, before the existing Groq → SambaNova → Cerebras → OpenRouter providers.

Changes

  • Uses openai-compatible provider type with base_url: https://api.inceptionlabs.ai/v1
  • Model: mercury-coder
  • Conditionally included — only active when INCEPTION_API_KEY is set in env
  • Falls back gracefully to existing chain if Mercury fails or key is missing

Deployment

Add to Vercel environment variables:

INCEPTION_API_KEY=your_key_here

No breaking changes — safe to deploy without the key.

aj47 added 2 commits March 20, 2025 10:32
- Adds mercury-coder as first target when INCEPTION_API_KEY is set
- Uses openai-compatible provider with base_url https://api.inceptionlabs.ai/v1
- Falls back to existing Groq -> SambaNova -> Cerebras -> OpenRouter chain
- Mercury target is conditionally included so it's safe to deploy without the key
@vercel
Copy link

vercel bot commented Feb 24, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
chaos-coder Building Building Preview, Comment Feb 24, 2026 10:20pm

@augmentcode
Copy link

augmentcode bot commented Feb 24, 2026

🤖 Augment PR Summary

Summary: Introduces Inception Labs Mercury Coder as the first target in the Portkey fallback chain (when INCEPTION_API_KEY is set) and adds credit-system security hardening artifacts.

Changes:

  • Update /api/generate Portkey targets to prefer Mercury Coder, then fall back to existing providers
  • Adjust client auth/credits syncing to avoid direct profiles.credits updates and route deductions through RPC
  • Add CreditsService wrapper for credit RPC operations and refresh logic
  • Add SQL/scripts/docs for tightening Supabase RLS/trigger-based protections and for backing up/restoring before applying fixes

Technical Notes: Mercury is configured as an OpenAI-compatible provider via base_url=https://api.inceptionlabs.ai/v1; the database scripts are intended to be executed with a service-role account.

🤖 Was this summary useful? React with 👍 or 👎

Copy link

@augmentcode augmentcode bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review completed. 8 suggestions posted.

Fix All in Augment

Comment augment review to trigger a new review at any time.

const { data, error } = await supabase
.from("profiles")
.select("credits")
.select("*")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

syncTokensWithDB now does .select("*") but only uses data.credits; this increases payload and can expose extra profile fields to the client unnecessarily. Consider selecting only the columns actually needed (e.g., credits).

Severity: low

Fix This in Augment

🤖 Was this useful? React with 👍 or 👎, or 🚀 if it prevented an incident/outage.

console.error("Error updating tokens in DB:", error);
// ONLY allow decrements (usage), never allow increments
// If local tokens are less than DB credits, sync the lower value
if (localTokens < currentCredits) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The localTokens < currentCredits branch treats any DB increase as “usage” and deducts the delta; if credits are refreshed server-side (daily reset/subscription) while the client is stale, this can unintentionally burn the newly granted credits. This comparison-based deduction seems unsafe without tracking a last-synced baseline.

Severity: high

Fix This in Augment

🤖 Was this useful? React with 👍 or 👎, or 🚀 if it prevented an incident/outage.

case 'free':
return 30;
case 'pro':
return 100;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getBaseCreditsForTier returns 100/1000 for pro/ultra, but the database reset_daily_credits logic elsewhere appears to use different tier amounts in some migrations; a mismatch here can cause repeated/incorrect refresh decisions. It’d be good to ensure these tier constants are consistent with the DB refresh function.

Severity: medium

Fix This in Augment

🤖 Was this useful? React with 👍 or 👎, or 🚀 if it prevented an incident/outage.

if (shouldRefresh) {
try {
// Call the secure RPC function instead of direct update
const { data, error: rpcError } = await supabase.rpc('reset_daily_credits');
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

checkAndRefreshCredits calls reset_daily_credits() as a per-user fallback, but that function is defined as a global daily reset (updates many/all users). Calling it from the browser for one user looks abusable and could unintentionally refresh credits for other accounts.

Severity: high

Fix This in Augment

🤖 Was this useful? React with 👍 or 👎, or 🚀 if it prevented an incident/outage.

type: string = 'adjustment',
description?: string
) {
const supabase = AuthService.createClient();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CreditsService.addCredits uses AuthService.createClient() (browser anon/user client) to call add_user_credits; if that RPC is executable by authenticated, this becomes a direct credit-granting vector. Also, the default type='adjustment' may be rejected by the DB function if it enforces an allowlist.

Severity: high

Fix This in Augment

🤖 Was this useful? React with 👍 or 👎, or 🚀 if it prevented an incident/outage.


-- If credits are being updated directly (not through our functions)
IF OLD.credits IS DISTINCT FROM NEW.credits AND
pg_trigger_depth() <= 1 -- Only check for direct updates, not those triggered by our stored procedures
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The trigger’s pg_trigger_depth() <= 1 check won’t distinguish a “direct UPDATE” from an UPDATE performed inside a stored procedure (both fire the trigger at depth 1). As written, non-service-role updates to credits from SECURITY DEFINER functions can still be reverted unintentionally.

Severity: high

Fix This in Augment

🤖 Was this useful? React with 👍 or 👎, or 🚀 if it prevented an incident/outage.

pg_trigger_depth() <= 1 -- Only check for direct updates, not those triggered by our stored procedures
THEN
-- Reset to old value if someone tries to bypass RLS
NEW.credits := OLD.credits;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In prevent_direct_credits_update, NEW.credits is overwritten with OLD.credits before the audit log entry is written, so the log will lose the attempted new value (it will record old→old). Same issue exists in the subscription_tier logging branch.

Severity: low

Other Locations
  • nextjs-web-app/backups/fix-rls-policies.sql:183

Fix This in Augment

🤖 Was this useful? React with 👍 or 👎, or 🚀 if it prevented an incident/outage.

-- ============================================

-- Revoke all permissions and reapply correctly
REVOKE ALL ON public.profiles FROM authenticated;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This script revokes all privileges on public.profiles from authenticated and only grants SELECT, but it also creates an UPDATE RLS policy for user profile updates. Without re-granting UPDATE on allowed columns, authenticated users won’t be able to update even non-sensitive profile fields.

Severity: medium

Fix This in Augment

🤖 Was this useful? React with 👍 or 👎, or 🚀 if it prevented an incident/outage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant