Keeping the fork publishable
The discipline that lets a private Azure fork ship AGPL source without leaking deployment IP: the three-repo, three-tier model, the upstream PRs, and the leakage gates.
Tier A — upstream PR plan
The upstream Mike repository is the AGPL source we cloned from. We want to submit changes back so that we can keep merging future upstream releases without re-resolving the same provider-boundary conflicts every time.
This document covers only the Tier A changes — the provider-boundary refactors that contain zero Azure dependency. Tier B (Azure adapters) and Tier C (deployment IP) are covered elsewhere.
Strategy
- Granularity: one PR per boundary. Reviewers can accept storage independently of auth; bundling them creates a "block" problem where one nit holds up both.
- Commit shape: a single commit per PR. Each PR is a refactor-without-behaviour-change. A single commit is easier to review; the upstream maintainer can squash-merge cleanly.
- No Azure imports anywhere in any A-tier diff. If a file imports
@azure/*, it is Tier B and does not appear in the A-tier extraction. Thestorage.tsandmiddleware/auth.tsextractions must therefore be partial-file patches that include only the provider-neutral portions. - Behaviour parity for the existing default. With
AUTH_PROVIDERunset (or set tosupabase), the post-PR middleware must behave identically to the upstream baseline. Ditto for the storage path with R2 env vars set.
Pre-existing design docs
Two of these PRs have already been thought through in detail:
- docs/issues/azure-migration/006.1-storage-provider-interface.md — the storage PR.
- docs/issues/azure-migration/006.2-auth-provider-interface.md — the auth PR.
The agent should treat those documents as the spec for the PR descriptions; they list the exact interface, the exact acceptance criteria, and the explicit "what stays in the fork" exclusions.
PR 1 — Storage provider interface
Branch name: feat/storage-provider-interface
Single commit message: refactor(storage): extract StorageProvider interface and R2Provider
Files in the PR:
- backend/src/lib/storage.ts — partial; only the interface,
R2Providerclass, factory function, and the public-API delegation.
What MUST NOT appear in the PR:
- The
import { BlobServiceClient, ContainerClient } from "@azure/storage-blob";andimport { DefaultAzureCredential } from "@azure/identity";lines. - The
AzureBlobProviderclass. - The Azure branch in
createProvider(). - The "Azure Blob Storage requires AZURE_STORAGE_…" error message in the factory's "no provider" exception text.
Extraction approach (for the agent):
- Start from the upstream baseline
b780a4b:backend/src/lib/storage.ts. - Apply only:
- The
StorageProviderinterface. - The
R2Providerclass (lifted-and-shifted from the existing module-level functions, no behaviour change). - The
createProvider()factory containing only the R2 branch and the "no provider configured" final throw. - The singleton initialization with
_provider/_initError/requireProvider. - The four public functions delegating to the singleton.
- The
storageEnabledexport derived from_provider !== null.
- The
- Keep the existing
storageKey,pdfStorageKey,generatedDocKey,versionStorageKey,storageExtension,normalizeDownloadFilename,sanitizeDispositionFilename,encodeRFC5987,buildContentDispositionhelpers (unchanged from upstream).
Acceptance: with R2_ENDPOINT_URL + R2_ACCESS_KEY_ID +
R2_SECRET_ACCESS_KEY set, every call site behaves identically to
upstream. With those env vars unset, the server starts (no provider)
and read operations return null while writes throw a clear error.
PR description: lift the rationale + acceptance bullets from docs/issues/azure-migration/006.1-storage-provider-interface.md.
PR 2 — Auth provider interface
Branch name: feat/auth-provider-interface
Single commit message: refactor(auth): extract AuthProvider interface and Supabase provider
Files in the PR:
- backend/src/lib/auth/types.ts — new file, exports
AuthPrincipalandAuthValidationResult. Includes thedisplayNamefield — keep this generic enough that other IdPs (Auth0, Clerk, custom JWT) can populate it. - backend/src/lib/auth/providers/supabase.ts — new file,
validateSupabaseTokenlifted from the upstream middleware. - backend/src/middleware/auth.ts — replaces inline Supabase
validation with provider dispatch on
process.env.AUTH_PROVIDER, defaulting to"supabase". Unknown values throw 500 — the agent must not include Entra/local branches in the upstream PR. res.locals.principalbecomes the canonical surface; existingres.locals.userIdandres.locals.userEmailcontinue to be set for callers that haven't migrated.
What MUST NOT appear in the PR:
- backend/src/lib/auth/providers/entra.ts
- backend/src/lib/auth/providers/local.ts
- backend/src/lib/auth/roles.ts
- The
entraandlocaldispatch branches inmiddleware/auth.ts. tenantAccessmiddleware composition.upsertUserProfileinvocation from the middleware. This is important. The upstream middleware does not callupsertUserProfilein itsrequireAuthpath; bringing it in would change upstream behaviour for every existing deployment. Keep the upstream middleware shape: validate token → set locals → callnext().
Extraction approach (for the agent):
- Start from upstream baseline
b780a4b:backend/src/middleware/auth.ts. - Diff that against our
claude/agpl-compliance-analysis-VSy2J:HEADto see the full delta. - Apply only:
- The new
auth/types.tsfile. - The new
auth/providers/supabase.tsfile (a lift-and-shift of the existing inline validation, plus the principal-shaped return). - In
middleware/auth.ts: replace the inline Supabase validation with aswitchonprocess.env.AUTH_PROVIDER ?? "supabase", callingvalidateSupabaseTokenfor the supabase branch and returning 500 for any other value. - Set
res.locals.principalfrom the validation result, in addition to the existinguserId/userEmail/token.
- The new
Acceptance: with AUTH_PROVIDER unset (or supabase), every
upstream route behaves identically. With AUTH_PROVIDER=anything-else,
the middleware returns 500 with a clear message.
PR description: lift from 006.2-auth-provider-interface.md.
PR 3 — (Optional, deferred) JSONB shared-with fix
backend/src/lib/access.ts and backend/src/routes/projects.ts
contain a real bug fix: passing a JS array to PostgREST's .contains()
on a JSONB column emits Postgres array-literal syntax, which
Postgres rejects. The fix passes a JSON-stringified array instead.
This is a clean upstream bug-fix PR, no Azure involvement. We are deferring it — it is included in the Tier B publication and we will revisit upstreaming after PR 1 and PR 2 land. Listed here so we don't forget it.
Branch name (when we get to it): fix(projects): JSONB containment for shared_with
PR 4 — (Optional, deferred) OpenAI provider adapter
backend/src/lib/llm/openai.ts adds plain OpenAI alongside the
existing Claude/Gemini adapters. The shape mirrors the upstream
adapter contract exactly. Could be a clean upstream addition.
Why deferred: ships in Tier B as part of the AOAI work. No
Azure dependency in this file specifically, but the model-routing
in models.ts interleaves OpenAI + AOAI tiers and would need
splitting out for a clean PR. Not worth the work right now.
Submission order
- PR 1 (storage) and PR 2 (auth) can be opened simultaneously — they touch disjoint files.
- Wait for both to merge before publishing the Tier B fork. The public fork's first commit will then be a clean rebase against the post-merge upstream HEAD, and our published delta becomes smaller and easier for outside reviewers to read.
- If upstream rejects either PR (or sits on it for >30 days), proceed with the public fork publication anyway. The provider abstractions ship in Tier B regardless; upstream merging them is a "future merges are easier" benefit, not a blocker for the AGPL §13 obligation.
Per-PR self-test the agent must run before opening
For each PR, on a fresh checkout of upstream + the PR branch:
npm install --prefix backendnpm run build --prefix backend- With
R2_ENDPOINT_URL/R2_ACCESS_KEY_ID/R2_SECRET_ACCESS_KEYexported (any non-empty values are fine for this test — the provider construction is lazy enough that no real R2 call is made at startup),node dist/index.jsmust boot without error. - With those env vars cleared and
SUPABASE_URL/SUPABASE_SECRET_KEYset,node dist/index.jsmust boot. (The server must not require storage to start.)
For PR 2 specifically, after step 4, curl -i -H 'Authorization: Bearer totally-invalid' http://localhost:3001/health must return 200 (no
auth required for /health), and any authenticated route must return
401. With AUTH_PROVIDER=anything-bogus, the same authenticated
route must return 500 with a clear message.