Spectaculous-Code avatar

ci-doctor

Diagnose and fix CI/CD pipeline failures, test errors, GitHub Actions issues, and code scanning aler

by Spectaculous-Code|Open Source

CI Doctor

Diagnose and fix CI failures fast. Read logs, identify root cause, fix or delegate.

Context Files (Read First)

For project structure, read from Docs/context/:

  • Docs/context/repo-structure.md - File locations
  • Docs/context/conventions.md - CI/build patterns

Workflow

1. FETCH    → Get failure details (gh cli or logs)
2. DIAGNOSE → Identify error category
3. FIX      → Apply fix or delegate to specialist skill
4. VERIFY   → Run locally to confirm
5. PUSH     → Commit and push fix

Step 1: Fetch Failure Details

# Get recent workflow runs
gh run list --limit 5

# Get failed run details
gh run view <run-id>

# Get job logs
gh run view <run-id> --log-failed

# Get PR checks
gh pr checks

If user provides a GitHub URL, extract info with gh:

gh pr view <url> --json statusCheckRollup
gh run view <url>

Step 2: Diagnose Error Category

Error PatternCategoryAction
tsc errors, "TS2xxx"TypeScriptFix type errors
biome lint, "lint error"LintDelegate to lint-fixer
FAIL src/...test.tsTest failureDelegate to test-writer or fix
npm ci failedDependencyCheck package-lock.json
types.ts changedGenerated typesRegenerate and commit
timeout, ETIMEDOUTFlaky/infraRetry or increase timeout
permission deniedSecrets/authCheck workflow permissions
CodeQL alert, code scanningSecurity vulnerabilitySee "Code Scanning Alerts" section

Step 3: Fix by Category

TypeScript Errors

# Run locally to see all errors
npm run typecheck:ci
# or
npx tsc -p tsconfig.ci.json --noEmit

Fix each error. Common patterns:

  • Missing imports
  • Type mismatches
  • Unused variables — CAUTION: See "Unused Variable Safety" section below

Lint Errors

Delegate: Use the lint-fixer skill

Or quick fix:

npx @biomejs/biome check --write .

Test Failures

  1. Run failing test locally:
npm run test --workspace=apps/raamattu-nyt -- --run <test-file>
  1. If complex, delegate: Use the systematic-debugging skill

  2. If test needs update, delegate: Use the test-writer skill

Generated Types Out of Sync

Supabase types:

# Regenerate (requires SUPABASE_PROJECT_ID and SUPABASE_ACCESS_TOKEN)
npx supabase gen types typescript --project-id "$SUPABASE_PROJECT_ID" > apps/raamattu-nyt/src/integrations/supabase/types.ts
git add apps/raamattu-nyt/src/integrations/supabase/types.ts
git commit -m "Regenerate Supabase types"

OpenAPI types:

npx openapi-typescript ./openapi.yaml -o apps/raamattu-nyt/src/lib/openapi.types.ts
git add apps/raamattu-nyt/src/lib/openapi.types.ts
git commit -m "Regenerate OpenAPI types"

Dependency Issues

# Clear and reinstall
rm -rf node_modules package-lock.json
npm install
git add package-lock.json
git commit -m "Refresh package-lock.json"

Code Scanning Alerts (CodeQL)

GitHub Code Scanning uses CodeQL to find security vulnerabilities.

Step 1: Fetch and Summarize

# Summary by rule type (always start here)
gh api 'repos/{owner}/{repo}/code-scanning/alerts?state=open&per_page=100' \
  --jq '[.[] | .rule.id] | group_by(.) | map({rule: .[0], count: length}) | sort_by(-.count)'

# Summary by file
gh api 'repos/{owner}/{repo}/code-scanning/alerts?state=open&per_page=100' \
  --jq '[.[] | .most_recent_instance.location.path] | group_by(.) | map({file: .[0], count: length}) | sort_by(-.count)'

# Filter to actual security issues (skip noise)
gh api 'repos/{owner}/{repo}/code-scanning/alerts?state=open&per_page=100' \
  --jq '[.[] | select(.rule.id | test("syntax-error|unused-local") | not)] | map({number, rule: .rule.id, severity: .rule.security_severity_level, file: .most_recent_instance.location.path})'

# Get specific alert details
gh api repos/{owner}/{repo}/code-scanning/alerts/<alert-number>

Step 2: TRIAGE FIRST (CRITICAL)

Before fixing anything, categorize each alert. Many CodeQL alerts are false positives in this codebase.

Alert RuleLikely ActionWhy
js/syntax-error on *-types.tsDISMISS as false positiveAuto-generated Supabase type files, not real syntax errors
js/unused-local-variableVERIFY INDIVIDUALLYSee "Unused Variable Safety" section — CodeQL misses JSX usage
js/incomplete-*-sanitization on *.test.tsDISMISS as test codeTest files intentionally test sanitization edge cases
js/bad-tag-filter on *.test.tsDISMISS as test codeTest files intentionally test regex filters
js/xss-through-domINVESTIGATEMay be real — read the code and check if user input reaches innerHTML
js/incomplete-url-substring-sanitizationINVESTIGATEMay be real — check if URL validation is actually vulnerable
js/useless-assignment-to-localINVESTIGATELow priority, cosmetic — check if assignment has side effects

Known false positive patterns in this project:

  • Generated type files (types.ts, bible-schema-types.ts, notifications-schema-types.ts) — CodeQL cannot parse Supabase's generated TypeScript. Dismiss all js/syntax-error alerts on these files.
  • Test files (*.test.ts, *.test.tsx) — Tests intentionally create insecure patterns to verify sanitization works. Dismiss sanitization alerts on test files.
  • JSX icon/component imports — CodeQL flags React component imports as "unused variables" because it doesn't understand <Component /> JSX syntax. See "Unused Variable Safety" section.

Step 3: Handle Each Category

For real security alerts (js/xss, js/sql-injection, js/url-*):

  1. Read the affected file and understand the data flow
  2. Verify untrusted user input actually reaches the vulnerable sink
  3. Apply the appropriate fix (see table below)
  4. Test locally
  5. Commit and push — CodeQL will re-analyze

For false positives:

# Dismiss as false positive (generated code)
gh api -X PATCH 'repos/{owner}/{repo}/code-scanning/alerts/<number>' \
  -f state=dismissed -f 'dismissed_reason=false positive' \
  -f dismissed_comment="Auto-generated Supabase type file"

# Dismiss as test code
gh api -X PATCH 'repos/{owner}/{repo}/code-scanning/alerts/<number>' \
  -f state=dismissed -f 'dismissed_reason=false positive' \
  -f dismissed_comment="Test file intentionally testing sanitization"

# Dismiss unused-local-variable after verifying JSX usage
gh api -X PATCH 'repos/{owner}/{repo}/code-scanning/alerts/<number>' \
  -f state=dismissed -f 'dismissed_reason=false positive' \
  -f dismissed_comment="Import used as JSX component: <ComponentName />"

For js/unused-local-variable: See "Unused Variable Safety" section below. NEVER bulk-remove.

Common real security fixes:

Alert TypeFix
js/xss / js/xss-through-domUse textContent not innerHTML, or sanitize with DOMPurify
js/sql-injectionUse parameterized queries, never concatenate user input
js/path-injectionValidate/sanitize file paths, use path.join with basename
js/incomplete-url-substring-sanitizationUse new URL() parsing instead of substring checks
js/prototype-pollutionUse Object.create(null) or validate object keys
js/insecure-randomnessUse crypto.randomUUID() instead of Math.random()
js/hardcoded-credentialsMove secrets to environment variables

Step 4: Batch-dismiss Known False Positives

For bulk dismissal of generated file alerts, process them in a loop:

# Get all syntax-error alerts on generated type files
gh api 'repos/{owner}/{repo}/code-scanning/alerts?state=open&per_page=100' \
  --jq '.[] | select(.rule.id == "js/syntax-error") | .number' | while read num; do
  gh api -X PATCH "repos/{owner}/{repo}/code-scanning/alerts/$num" \
    -f state=dismissed -f 'dismissed_reason=false positive' \
    -f dismissed_comment="Auto-generated Supabase type file"
done

Unused Variable Safety (CRITICAL)

CodeQL js/unused-local-variable alerts are frequently WRONG for React/JSX codebases.

CodeQL's static analysis cannot detect:

  • JSX component usage: <Heart className="..." />Heart looks "unused" as a JS variable
  • Destructured props used in JSX: const { limit } = usage{limit.toLocaleString()}
  • Hook calls: const { toast } = useToast()toast(...) in event handlers

NEVER bulk-remove CodeQL unused variable alerts. For each flagged variable:

# 1. Grep for ALL usages including JSX
grep -n "variableName" path/to/file.tsx

# 2. Specifically check JSX usage (components/icons)
grep -n "<variableName" path/to/file.tsx

# 3. Check if it's used in template literals
grep -n "{variableName" path/to/file.tsx

Only safe to remove/rename:

  • Imports with genuinely zero references in the file
  • Catch parameters (catch (err)catch (_err)) IF err is not used in catch block
  • Function parameters that are truly unused (and renaming won't break callers)

Never rename with _ prefix if the original name is used anywhere in the file.

Incident reference: Commit df849425 bulk-removed "unused" variables across 24 files. 14 were actually in use, causing runtime crashes. Required 8 fix commits to repair.

Step 4: Verify Locally

Before pushing, run the same checks CI runs:

# TypeScript
npm run typecheck:ci || npx tsc -p tsconfig.ci.json

# Lint
npx @biomejs/biome lint .

# Build
npm run build

# Tests
npm run test --workspace=apps/raamattu-nyt

Step 5: Push Fix

git add -A
git commit -m "Fix CI: <brief description>"
git push

Then monitor: gh run watch

Project CI Structure

This project has these workflows:

WorkflowFileTriggersChecks
CI.github/workflows/ci.ymlPR, push to mainTypeScript, Lint, Build
Tests.github/workflows/tests.ymlPR, push (code changes)Vitest, Playwright smoke
Supabase Sync.github/workflows/supabase-sync.ymlVariousType generation

Delegation Guide

SituationDelegate To
Lint/format errorslint-fixer skill
Test needs rewritingtest-writer skill
Complex bug in testsystematic-debugging skill
Supabase migration issuesupabase-migration-writer skill
Type refactoring neededcode-refactoring skill
RLS/GRANT security gapssecurity-auditor skill
Auth flow vulnerabilitiesauth-shield skill

Quick Commands Reference

# See what's failing
gh pr checks
gh run list --limit 3

# Get logs for failed run
gh run view <id> --log-failed

# Re-run failed jobs
gh run rerun <id> --failed

# Watch current run
gh run watch

References

Read these before diagnosing — they encode hard-won project specifics: