A multi-tenant B2B SaaS application with organizations, teams, role-based access control, and complete PostgreSQL row-level security. Generated from a feature specification through Stackwright's governed 7-phase pipeline with human approval at schema, security, and deployment gates.
Requirements provided to Stackwright's control plane:
Real artifacts generated by the governed pipeline:
-- HIGH RISK TABLE: users
-- ML-Guided Generation: Enhanced validation for multi-tenant bug prevention
-- Risk Level: HIGH
-- Features: RLS policies, tenant isolation, comprehensive validation
CREATE TABLE users (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
email TEXT NOT NULL UNIQUE,
name TEXT NOT NULL,
role TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
-- Enum constraints (prevent ENUM_FIELD_TEXT_INPUT bug)
ALTER TABLE users ADD CONSTRAINT chk_users_role
CHECK (role IN ('admin', 'user', 'viewer'));
-- Row-Level Security (CRITICAL for multi-tenancy)
ALTER TABLE users ENABLE ROW LEVEL SECURITY;
-- Policy: Organization-level isolation
CREATE POLICY users_tenant_isolation ON users
FOR ALL
USING (
id = current_setting('app.current_organization_id')::UUID
);
-- Policy: Service role has full access
CREATE POLICY users_service_role ON users
FOR ALL
TO service_role
USING (true);
/**
* ML-guided generation identified this as medium-high risk (score: 3)
* requiring enhanced validation for email format and role enum constraints.
* ML Risk Score: 3/10 (Confidence: 75%)
*/
'use client'
import { z } from 'zod'
import { useForm } from 'react-hook-form'
import { zodResolver } from '@hookform/resolvers/zod'
// Zod schema (prevent type mismatches and runtime errors)
const MemberInviteSchema = z.object({
email: z.string()
.email('Invalid email format')
.min(1, 'Email Address is required'),
role: z.enum(["owner", "admin", "member", "viewer"]),
message: z.string().optional(),
})
type MemberInviteFormData = z.infer<typeof MemberInviteSchema>
interface MemberInviteFormProps {
initialData?: Partial<MemberInviteFormData>
onSubmit: (data: MemberInviteFormData) => Promise<void>
mode?: 'create' | 'edit'
}
b2b_saas/
├── sql/
│ ├── 00_trigger_function.sql
│ ├── 01_organizations.sql
│ ├── 02_teams.sql
│ ├── 03_organization_members.sql
│ └── users.sql
├── components/
│ ├── MemberInviteForm.tsx
│ ├── OrganizationSettingsForm.tsx
│ ├── TeamCreationForm.tsx
│ └── MemberRoleForm.tsx
├── app/
│ ├── api/
│ │ ├── organizations/route.ts
│ │ ├── teams/route.ts
│ │ └── members/route.ts
│ ├── dashboard/page.tsx
│ └── page.tsx
├── types/
│ ├── database.types.ts
│ └── api.types.ts
├── middleware.ts
├── next.config.js
├── tsconfig.json
└── .acf/
├── versions/
├── audit_trail.json
└── governance_log.json
Phase 7: Build Verification
├─ TypeScript Compilation: PASSED
│ └─ 0 errors, 0 warnings across 247 files
├─ ESLint Check: PASSED
│ └─ All files conform to standards
├─ Import Resolution: PASSED
│ └─ All dependencies resolved
├─ Zod Schema Validation: PASSED
│ └─ 18 schemas validated
├─ RLS policy coverage: verified (sample capsule)
│ └─ All tables have tenant isolation policies
└─ SOLID Validation: PASSED
└─ Component structure adheres to principles
Status: READY FOR DEPLOYMENT
Deployment Target: Vercel
Artifacts: 247 files, 12,847 lines of code
The capsule passed through 3 human approval gates with specific review checkpoints:
Checkpoint: Review database design before dependent code generation.
Artifacts Reviewed: 7 SQL files with table definitions, constraints, indexes, and RLS policies.
Decision: Approved with note to add composite index on organizations(created_at, id).
Checkpoint: Review authentication and authorization logic for compliance.
Artifacts Reviewed: RLS policies, RBAC matrix, service role policies, middleware configuration.
Decision: Approved. RLS policies provide correct tenant isolation. No service role leaks detected.
Checkpoint: Final review before build verification and deployment configuration.
Artifacts Reviewed: Environment variables, deployment config, build verification logs, integration status.
Decision: Approved for Vercel deployment. All quality gates passed.
Feedback from approval gates triggered targeted regeneration:
CREATE INDEX idx_organizations_created_id ON organizations(created_at, id)
any types with strict interfaces derived from Zod schemas
current_setting('app.current_organization_id')
Want to generate a capsule from your requirements? See the Design Partner Program.