Skip to content

Validate published errors against API-owned response contracts - #1038

Merged
sidneyswift merged 3 commits into
mainfrom
codex/published-response-contracts
Oct 17, 2026
Merged

sidneyswift merged 3 commits into
mainfrom
codex/published-response-contracts

Conversation

@sidneyswift

@sidneyswift sidneyswift commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

Projects authentication can return {status, message}, while the published 401 schema required error. This preserves the existing wire behavior, derives the shared error schemas from API-owned Zod types, and validates selected published schemas against actual Artist Profile and Project GET exports. Shared auth helpers now use typed response constructors. Projects' authentication union is limited to 401; its 400, 404 and 500 responses still require error.

Merge this API change before recoupable/marketing#175, then the generated copy recoupable/docs#376. Marketing pins API 4dd3a1debfed685930cbb50e83d9a08e3ecc5818; the API fixture records Marketing source-file hashes rather than a circular commit pin. The cohesive generated schema artifact and existing shared fixture remain single files so the pinned export/check boundary stays atomic.

Validation: 105 focused contract/auth/project/networking/artist tests and strict response type checks pass; changed TypeScript lint passes and repository lint/format/OAuth compatibility CI are green. Tests reject the historical Artist schema and missing producer fields. The distinct project/account fixture rejects swapped access arguments. All 28 published error responses across seven Projects operations accept message-only bodies only at 401. Deliberately dropping resolver required-fields or exporter method identity still exports fixtures but makes the API contract gate fail; restoring the code passes. Profile, project-access and credential dependencies are mocked in local route tests.

On the exact Vercel deployment for dcd8022ceecc0cea09a61c0791ad9444ee0c7814, a Projects GET without credentials returned the expected 401 {status, error}, and malformed Authorization: invalid-token returned the expected 401 {status, message}. The auth helpers, response constructors, access function, handlers and route exports remain byte-identical to that observed revision. No valid identity, customer access or paid operation was used.

Scope note: the existing token-verification catch can return a caught Error.message to the caller. That behavior predates this change and is deliberately preserved; sanitization requires a separately scoped compatibility/security change and is not claimed fixed here.

@vercel

vercel Bot commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

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

Project Deployment Actions Updated
api Ready Ready Preview Oct 17, 2026 12:49am UTC

Request Review

@coderabbitai

coderabbitai Bot commented Oct 17, 2026 •

Copy link
Copy Markdown

Important

  • 🔍 Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

⚙️ Run configuration
  • Configuration used: Repository: recoupable/api/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 8c6e3d5a-84a1-419f-8b0d-7fc7e3bd2a03
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

2 issues found and verified against the latest diff

Confidence score: 3/5

  • getAuthenticatedAccountId.ts returns raw error messages in 401 responses, which can expose database details to callers. Return a generic message instead.
  • published-docs.json is 285 lines and exceeds the 100-line style limit. Split it into smaller, cohesive fixtures.
Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="lib/auth/getAuthenticatedAccountId.ts">

<violation number="1" location="lib/auth/getAuthenticatedAccountId.ts:48">
P2: The catch branch returns raw `error.message` in the 401 body. A DB failure during account provisioning (for example, `insertAccount` rethrowing `Failed to insert account: <Postgres message>`) is exposed to the caller. This predates the PR, but the touched line still carries it. Log the full error (already done) and return a generic message for non-auth failures, or add a test asserting internal error text is not returned.</violation>
</file>

<file name="lib/contracts/fixtures/published-docs.json">

<violation number="1" location="lib/contracts/fixtures/published-docs.json:6">
P2: Custom agent: **Enforce Clear Code Style and Maintainability Practices**

This fixture is now 285 lines, well above the rule’s 100-line limit. Split its response-schema data and other cohesive fixture sections into smaller files.</violation>
</file>

Reply with feedback, questions, or to request a fix.

View guided diff | Turn on auto-fix | Re-trigger cubic

headers: getCorsHeaders(),
},
);
return messageErrorResponse(message, 401);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2: The catch branch returns raw error.message in the 401 body. A DB failure during account provisioning (for example, insertAccount rethrowing Failed to insert account: <Postgres message>) is exposed to the caller. This predates the PR, but the touched line still carries it. Log the full error (already done) and return a generic message for non-auth failures, or add a test asserting internal error text is not returned.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At lib/auth/getAuthenticatedAccountId.ts, line 48:

<comment>The catch branch returns raw `error.message` in the 401 body. A DB failure during account provisioning (for example, `insertAccount` rethrowing `Failed to insert account: <Postgres message>`) is exposed to the caller. This predates the PR, but the touched line still carries it. Log the full error (already done) and return a generic message for non-auth failures, or add a test asserting internal error text is not returned.</comment>

<file context>
@@ -57,15 +45,6 @@ export async function getAuthenticatedAccountId(
-        headers: getCorsHeaders(),
-      },
-    );
+    return messageErrorResponse(message, 401);
   }
 }
</file context>

"responseEvidence": "Selected route exports use real request, response and authentication code under dependency mocks. No live provider, customer or hosted route is exercised.",
"sourceRepository": "recoupable/marketing",
"apiResponseSchemaSha256": "f65397b0466feee5a4d6d2c1b49dcfaa50b419e281e0dcd8ec58bbbb0ee41237",
"responseSchemas": [

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2: Custom agent: Enforce Clear Code Style and Maintainability Practices

This fixture is now 285 lines, well above the rule’s 100-line limit. Split its response-schema data and other cohesive fixture sections into smaller files.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At lib/contracts/fixtures/published-docs.json, line 6:

<comment>This fixture is now 285 lines, well above the rule’s 100-line limit. Split its response-schema data and other cohesive fixture sections into smaller files.</comment>

<file context>
@@ -1,13 +1,152 @@
+  "responseEvidence": "Selected route exports use real request, response and authentication code under dependency mocks. No live provider, customer or hosted route is exercised.",
   "sourceRepository": "recoupable/marketing",
+  "apiResponseSchemaSha256": "f65397b0466feee5a4d6d2c1b49dcfaa50b419e281e0dcd8ec58bbbb0ee41237",
+  "responseSchemas": [
+    {
+      "specification": "releases.json",
</file context>

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

All reported issues were addressed across 18 files

Architecture diagram
sequenceDiagram
    participant Client as API Client
    participant Next as Next.js Route (Artist/Project)
    participant Auth as Auth Helpers (validateAuthContext/getAuth)
    participant Schemas as Zod Response Schemas
    participant Contract as Contract Fixture/OpenAPI Artifact
    participant CI as CI Pipeline

    Note over Client,CI: Response Contract Validation & Publishing Flow

    Client->>Next: GET /api/artists/{id}/profile
    Next->>Auth: validateAuthContext()
    alt Missing/invalid auth
        Auth->>Auth: errorResponse() or messageErrorResponse()
        Auth-->>Next: 401/403 response
        Next-->>Client: JSON {status, error|message}
    else Valid auth
        Auth-->>Next: accountId
        alt Artist not found
            Next->>Next: errorResponse("Artist not found", 404)
            Next-->>Client: JSON {status, error}
        else Artist found
            Next-->>Client: Artist data
        end
    end

    Client->>Next: GET /api/projects/{projectId}
    Next->>Auth: validateAuthContext() / getApiKeyAccountId()
    Auth->>Schemas: NEW: Use typed constructors (errorResponse, messageErrorResponse)
    Schemas-->>Auth: Type-safe response body
    Auth-->>Next: 401 response (either envelope)
    Next-->>Client: JSON {status, error|message}

    Note over Schemas,Contract: Contract Generation & Verification
    Schemas->>Contract: Generate OpenAPI artifact (z.toJSONSchema)
    Contract->>Contract: Freeze schemas, statuses, source hashes
    CI->>Contract: typecheck:response-contracts + tests
    Contract-->>CI: Validate responses match published schemas

    alt Response matches contract
        CI-->>CI: PASS
    else Response violates contract
        CI-->>CI: FAIL (schema mismatch)
    end
Loading

Reply with feedback, questions, or to request a fix.

View guided diff | Turn on auto-fix | Re-trigger cubic

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

0 issues found across 6 files (changes from recent commits).

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Requires human review: Auto-approval blocked by 2 unresolved issues from previous reviews.

View guided diff | Turn on auto-fix | Re-trigger cubic

This branch was successfully deployed

1 active deployment
Preview — 4dd3a1de Deployed Oct 17, 2026 by vercel[bot]
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