import type { Database } from "./database.js"; /** Why a candidate cannot be provisioned from deterministic evidence alone. */ export type OrganizationMembershipBackfillUnresolvedReason = /** No Better Auth login identity exists for this subject: the `user:` * subject is a persisted string with nothing behind it. Guessing an * identity here would be exactly the inference Phase D forbids. */ "missing_login_identity" /** The organization's own external identity is not this human. 0219's seam * provisions only a managed human's self-owned organization, and 0263's * invitation acceptance is bound to the invited human's own authenticated * session - so no operator-runnable idempotent lifecycle operation exists * for this row. It needs a human act, not a backfill. */ | "organization_identity_mismatch" /** The subject has workspace access in this organization but holds no * owner-role workspace membership, which the lifecycle seam requires as * proof of ownership. Promoting them would manufacture authority. */ | "missing_owner_workspace_membership" /** An anchor exists but is suspended or revoked. Reactivation is an explicit * owner-authorized 0263 lifecycle action; a backfill must never perform it. */ | "membership_terminal_status"; export type OrganizationMembershipBackfillCandidate = { subjectId: string; /** Stable UUID of the exact authority/source row that made this subject a * backfill obligation. Existing anchors use their membership id; an * anchorless subject uses the first persisted workspace-membership id. * This is the content-free resource id written to migration 0300's ledger. */ evidenceResourceId: string; resolution: "provisionable" | "already_anchored" | "unresolved"; reasonCode: OrganizationMembershipBackfillUnresolvedReason | null; organizationMembershipId: string | null; personalWorkspaceId: string | null; }; export type OrganizationMembershipBackfillOutcome = { subjectId: string; outcome: "provisioned"; organizationMembershipId: string; personalWorkspaceId: string; } | { subjectId: string; outcome: "already_anchored"; organizationMembershipId: string; personalWorkspaceId: string | null; } | { subjectId: string; outcome: "unresolved"; reasonCode: OrganizationMembershipBackfillUnresolvedReason; } /** Another concurrent run holds this candidate's claim row. Deliberately not * a failure: the next pass picks it up, which is what resumability means. */ | { subjectId: string; outcome: "contended"; } | { subjectId: string; outcome: "failed"; reasonCode: string; }; export type OrganizationMembershipBackfillCounts = { inspected: number; provisioned: number; alreadyAnchored: number; unresolved: number; contended: number; failed: number; }; export type OrganizationMembershipBackfillReport = { organizationId: string; dryRun: boolean; /** The exclusive keyset cursor this pass started after (`null` = from the * beginning of the ordered subject stream). */ startCursor: string | null; /** Feed this back as `afterSubjectId` to continue. `null` means this pass * reached the end of the ordered stream: the organization is drained. */ nextCursor: string | null; candidates: OrganizationMembershipBackfillCandidate[]; results: OrganizationMembershipBackfillOutcome[]; counts: OrganizationMembershipBackfillCounts; }; /** One full walk of an organization: repeated bounded passes, cursor-chained. */ export type OrganizationMembershipBackfillDrainReport = { organizationId: string; dryRun: boolean; limit: number; passes: number; /** True when the walk reached the end of the ordered stream. False only when * `maxPasses` stopped it first, and then `lastCursor` resumes it. */ drained: boolean; lastCursor: string | null; counts: OrganizationMembershipBackfillCounts; unresolved: Array<{ subjectId: string; resourceId: string; reasonCode: OrganizationMembershipBackfillUnresolvedReason; }>; /** Claimed by a peer run during this walk. Not a failure and not skipped * work: re-running the command from the start converges them. */ contended: string[]; failed: Array<{ subjectId: string; reasonCode: string; }>; /** Null when no run key was supplied or the walk was a dry run. */ receiptId: string | null; receiptStatus: "completed" | "failed" | null; }; export type OrganizationMembershipBackfillPage = { candidates: OrganizationMembershipBackfillCandidate[]; nextCursor: string | null; }; /** * Enumerate one bounded, deterministically ordered, keyset-paged backfill * candidate window for one organization and classify each candidate. Strictly * read-only: safe to call repeatedly, and the exact query `--dry-run` reports. * * The window is the first `limit` subjects strictly greater than * `afterSubjectId` in the merge of both populations. `nextCursor` is the last * subject of that window when more may remain, and `null` once the stream is * exhausted - which is the only thing that makes repeated passes terminate on * a drained organization instead of re-reading one window forever. */ export declare function listOrganizationMembershipBackfillCandidates(db: Database, input: { organizationId: string; limit: number; afterSubjectId?: string | null; }): Promise; /** * Provision exactly one candidate inside one transaction. * * The transaction first claims the candidate's owner workspace membership with * `FOR UPDATE SKIP LOCKED`. That row is the natural claim: it is the exact * evidence the lifecycle seam validates, so holding it for the duration also * stops a concurrent workspace-membership removal from racing the anchor we are * writing. A skipped claim means a peer run owns this candidate right now - * reported as `contended`, picked up by the next pass. */ export declare function provisionOrganizationMembershipBackfillCandidate(db: Database, input: { organizationId: string; subjectId: string; }): Promise; /** * Run one bounded backfill pass over an organization. Safe to run repeatedly * and concurrently; `dryRun` inspects and classifies without writing anything. * * One pass is one keyset window. Chain `report.nextCursor` back in as * `afterSubjectId` to walk the whole organization, or call * `drainOrganizationMembershipBackfill` which does exactly that. */ export declare function runOrganizationMembershipBackfill(db: Database, input: { organizationId: string; limit: number; dryRun: boolean; afterSubjectId?: string | null; }): Promise; /** * Walk an entire organization by chaining bounded passes on the keyset cursor * until the ordered stream is exhausted. * * `maxPasses` is a safety stop, not the termination condition: a walk that hits * it reports `drained: false` and a `lastCursor` that resumes it exactly where * it stopped. Because the cursor is strictly increasing, this terminates on * every organization - including one whose first `limit` subjects are all * permanently unresolvable, which is precisely the shape a fixed window could * never get past. */ export declare function drainOrganizationMembershipBackfill(db: Database, input: { organizationId: string; limit: number; dryRun: boolean; maxPasses?: number; afterSubjectId?: string | null; /** Optional migration-0300 idempotency key. A non-dry walk settles one * durable organization_memberships receipt after the bounded walk. */ runKey?: string | null; }): Promise;