Skip to content

secondsky/claude-skills / plugins/multi-ai-consultant/skills/multi-ai-consultant

multi-ai-consultant

Flagged

Consult external AIs (Gemini 2.5 Pro, OpenAI Codex, Claude) for second opinions. Use for debugging failures, architectural decisions, security validation, or need fresh perspective with synthesis.

FileKindSize
SKILL.mdskill md13.9 KB
examples/sample-consultation.mdmarkdown9.6 KB
references/ai-strengths.mdmarkdown6.0 KB
references/commands-reference.mdmarkdown12.8 KB
references/setup-guide.mdmarkdown10.0 KB
references/troubleshooting.mdmarkdown13.8 KB
references/usage-examples.mdmarkdown15.1 KB
scripts/setup-apis.shexecutablescript10.1 KB
templates/.geminiignoreother3.0 KB
templates/GEMINI.mdmarkdown7.1 KB
templates/codex.mdmarkdown7.7 KB
templates/consultation-log-parser.shexecutablescript4.8 KB

examples/sample-consultation.md

View on GitHub
# Sample Consultation: JWT Authentication Bug

This is a real-world example of using `/consult-gemini` to solve a stuck bug.

---

## Initial Problem

**User**: "Fix this 401 error in src/auth/session.ts"

**File** (`src/auth/session.ts`):
```typescript
import jwt from 'jsonwebtoken';

export async function validateToken(token: string, env: Env): Promise<User | null> {
  try {
    // Verify JWT signature
    const secret = process.env.JWT_SECRET; // ← BUG HERE
    const decoded = jwt.verify(token, secret) as JWTPayload;

    // Check expiry
    if (decoded.exp < Date.now() / 1000) {
      return null;
    }

    // Fetch user from database
    const user = await env.DB.prepare(
      'SELECT * FROM users WHERE id = ?'
    ).bind(decoded.userId).first<User>();

    return user;
  } catch (error) {
    console.error('Token validation failed:', error);
    return null;
  }
}
```

---

## Claude's First Attempt

**Analysis**: The token expiry check looks suspicious. Let me fix that.

**Change**:
```typescript
// Before
if (decoded.exp < Date.now() / 1000) {
  return null;
}

// After (Claude's fix)
if (decoded.exp && decoded.exp < Date.now() / 1000) {
  return null;
}
```

**Result**: Still failing, but now with "invalid signature" error instead of 401

---

## Consultation Triggered

**Claude**: "I've tried one approach without success. Should I consult Gemini for a fresh perspective on JWT validation?"

**User**: "yes"

---

## Consultation Execution

**Claude executes** `/consult-gemini` internally:

### Prompt Sent to Gemini

```
Problem: 401 error on /login route after token refresh
Question: Is the JWT validation logic correct?

I've tried: Updating token expiry check to handle undefined exp
Current status: Now getting "invalid signature" error instead of 401

Context: @src/auth/session.ts @src/middleware/jwt.ts
```

### Gemini's Response

```json
{
  "response": {
    "text": "After analyzing the code and searching for latest JWT best practices with Cloudflare Workers, I found two issues:\n\n**Issue 1 (Root Cause): Line 47**\nYou're using `process.env.JWT_SECRET` which is undefined in Cloudflare Workers environment. Workers don't have `process.env` - you need to use `env.JWT_SECRET` from the context binding.\n\nThis explains the \"invalid signature\" error - the signature is being verified against `undefined` instead of your actual secret.\n\n**Issue 2 (Secondary): Line 52**\nToken refresh logic creates a new token but doesn't update the database record's `lastUsedAt` timestamp, which will cause subsequent requests to fail the replay check.\n\n**Web Research**:\nCloudflare Workers documentation (updated 2024-11-05) explicitly states that Workers don't support `process.env`. All environment variables must be accessed via the `env` parameter passed to your handler.\n\nSource: https://developers.cloudflare.com/workers/runtime-apis/environment-variables/\n\n**Recommended Fix**:\n```typescript\n// Line 47\n- const secret = process.env.JWT_SECRET;\n+ const secret = env.JWT_SECRET;\n\n// Line 55 (add after token creation)\n+ await env.DB.prepare(\n+   'UPDATE users SET lastUsedAt = ? WHERE id = ?'\n+ ).bind(new Date().toISOString(), decoded.userId).run();\n```\n\n**Trade-offs**:\n- The primary fix is straightforward (use env binding)\n- The secondary fix adds a database write, slight performance cost\n- Alternative: Move replay check to application logic instead of DB\n\n**Security Note**:\nEnsure `JWT_SECRET` is set in your wrangler.toml under `[vars]` or `.dev.vars` for local development, and in Cloudflare dashboard for production.\n"
  },
  "stats": {
    "inputTokens": 15420,
    "outputTokens": 850
  }
}
```

---

## Claude's Synthesis

### πŸ€– My Analysis

I attempted to fix the token validation by updating the timestamp comparison to handle undefined `exp` values. However, the new error ("invalid signature") suggested the issue was deeper - likely in how the JWT secret is accessed or how the token is being verified.

My focus was on the expiry logic, but I didn't consider the environment-specific aspects of Cloudflare Workers.

### πŸ’Ž Gemini's Analysis

After analyzing the code and searching for latest JWT best practices with Cloudflare Workers, Gemini found two issues:

**Issue 1 (Root Cause) - Line 47:**
Using `process.env.JWT_SECRET` which is `undefined` in Cloudflare Workers. Workers don't have `process.env` - must use `env.JWT_SECRET` from context binding.

**Issue 2 (Secondary) - Line 52:**
Token refresh creates new token but doesn't update `lastUsedAt` timestamp, causing replay check failures.

**Web Research:**
Cloudflare Workers docs (updated 2024-11-05) explicitly state Workers don't support `process.env`. All environment variables via `env` parameter.

Source: https://developers.cloudflare.com/workers/runtime-apis/environment-variables/

### πŸ” Key Differences

**Agreement:**
- Both identified JWT validation as the core issue
- Both recognized the error indicates signature verification failure

**Divergence:**
- I focused on expiry timing logic
- Gemini identified the actual bug (missing env binding)

**Gemini's Catches:**
1. Cloudflare Workers-specific issue (no `process.env`)
2. Web research found recent docs update (Nov 2024)
3. Secondary bug in refresh logic I completely missed

**What I Had Right:**
- Correct file identified (src/auth/session.ts)
- Recognized validation was failing
- Attempted methodical debugging approach

### ⚑ Synthesis

The 401 error is caused by **two issues**:

**Primary (causing current error):**
Using `process.env.JWT_SECRET` instead of `env.JWT_SECRET`. In Cloudflare Workers, `process.env` doesn't exist - the secret resolves to `undefined`, so JWT signature verification fails.

**Secondary (will cause future errors):**
Missing database update in token refresh flow. The `lastUsedAt` timestamp isn't updated, which will fail replay protection checks on subsequent requests.

Gemini's web search found a recent Cloudflare docs update (Nov 2024) that explicitly addresses this Workers environment pattern, which wasn't in my knowledge base.

### βœ… Recommended Action

**Immediate fixes:**

1. **Line 47** - Use Workers env binding:
```typescript
const secret = env.JWT_SECRET; // Not process.env.JWT_SECRET
```

2. **Line 55** - Add database update:
```typescript
await env.DB.prepare(
  'UPDATE users SET lastUsedAt = ? WHERE id = ?'
).bind(new Date().toISOString(), decoded.userId).run();
```

3. **Configuration** - Ensure `JWT_SECRET` is set:
   - `wrangler.toml`: Add to `[vars]` section
   - `.dev.vars`: For local development
   - Cloudflare dashboard: For production

**Expected outcome:**
- Token validation will succeed (correct secret)
- Replay protection will work (updated timestamp)
- Authentication flow complete

**Risk assessment:**
- Low risk: Straightforward environment variable fix
- Database write adds ~10ms latency (acceptable for auth)
- Must deploy both changes together

Should I proceed with these fixes?

---

**Cost**: $0.18 (15,420 input + 850 output tokens)
**Consultation logged**: ~/.claude/ai-consultations/consultations.log

---

## User Approval

**User**: "yes, proceed"

---

## Implementation

**Claude implements both fixes:**

```typescript
// src/auth/session.ts
export async function validateToken(token: string, env: Env): Promise<User | null> {
  try {
    // Verify JWT signature - FIXED: Use env binding
    const secret = env.JWT_SECRET;
    const decoded = jwt.verify(token, secret) as JWTPayload;

    // Check expiry
    if (decoded.exp && decoded.exp < Date.now() / 1000) {
      return null;
    }

    // Fetch user from database
    const user = await env.DB.prepare(
      'SELECT * FROM users WHERE id = ?'
    ).bind(decoded.userId).first<User>();

    // FIXED: Update lastUsedAt for replay protection
    if (user) {
      await env.DB.prepare(
        'UPDATE users SET lastUsedAt = ? WHERE id = ?'
      ).bind(new Date().toISOString(), decoded.userId).run();
    }

    return user;
  } catch (error) {
    console.error('Token validation failed:', error);
    return null;
  }
}
```

**Test result**: βœ… Authentication working

---

## Outcome

**Time saved**:
- Without consultation: ~30-40 minutes (trial-and-error, eventually find Workers docs)
- With consultation: ~3 minutes (consultation + implementation)
- **Savings**: ~27-37 minutes (~90% time saved)

**Token saved**:
- Without consultation: ~20k tokens (multiple debugging attempts)
- With consultation: ~8k tokens (including Gemini's cost)
- **Savings**: ~12k tokens (~60% tokens saved)

**Value added**:
- Gemini's web search found Workers-specific issue Claude missed
- Caught secondary bug (replay protection) proactively
- Found recent docs update (Nov 2024) beyond knowledge cutoff
- Prevented future bug reports

**Cost**: $0.18 (well worth it)

---

## Key Learnings

1. **Fresh perspective matters**: Claude focused on expiry logic, Gemini caught env binding
2. **Web research is powerful**: Found recent Cloudflare docs Claude didn't have
3. **Synthesis adds value**: Don't just accept Gemini's answer, understand why
4. **Secondary issues**: Gemini found bug that would've appeared later
5. **Cost/benefit**: $0.18 to save 30+ minutes is excellent ROI

---

## How to Replicate

```bash
# 1. Set up your project with GEMINI.md template
cp ~/.claude/skills/multi-ai-consultant/templates/GEMINI.md ./

# 2. When stuck after first attempt, ask Claude Code
"I'm stuck on this bug"

# 3. Claude suggests consultation
"Should I consult Gemini for a second opinion?"

# 4. Approve
"yes"

# 5. Review synthesis and decide
# Claude shows 5-part comparison

# 6. Approve implementation
"yes, proceed"
```

---

**Last Updated**: 2025-11-07
**Source**: planning/multi-ai-consultant-summary.md (example workflow)
rea-analysis-optimizationmorluto/reaOptimize REA providers and shared analysis workflows for agent context, latency, CPU, and memory while preserving correctness and Evidence. Use for analysis bottlenecks and cache or query design; CI/test-suite optimization belongs to rea-ci-tests-audit.Stars: 471kUpdated Claude CodeCodexVerifiedInstall
rea-ci-tests-auditmorluto/reaAudit REA CI and test suites for correctness, fixture drift, false passes, and measured performance; use for suite or workflow investigations and efficiency changes, rather than ordinary feature testing or a single test failure.Stars: 471kUpdated Claude CodeCodexVerifiedInstall
rea-tool-designmorluto/reaAudit, investigate, design, or change REA investigation tools, CLI/MCP contracts, provider capabilities, and Evidence semantics.Stars: 471kUpdated Claude CodeCodexVerifiedInstall
ponytail-auditDietrichGebert/ponytailQuality audit of the whole repo: bugs, security, real load, missing tests, speed, and what to delete. Most important first.Stars: 194kUpdated Claude CodeCodexVerifiedInstall