In any case to test the legality of the detention of a person pursuant to this article, whether by habeas corpus or otherwise, the attorney for the Commonwealth of the county or city in which the hearing is held shall, on request of the director of the facility or other institution having or claiming custody of the person, represent the Commonwealth in opposition to any such petition, appeal, or procedure for the discharge of the person from custody.
Va. Code § 37.2-847