
12 So.3d 843 (2009)
Runner O. SANTANA, Appellant,
v.
Mark HENRY, Warden, Florida Department of Corrections, Appellee.
No. 1D08-3852.
District Court of Appeal of Florida, First District.
May 29, 2009.
*844 Runner O. Santana, pro se, Appellant.
Bill McCollum, Attorney General, Kathleen Von Hoene, General Counsel, and Beverly Brewster, Assistant General Counsel, Tallahassee, for Appellee.
BENTON, J.
Runner O. Santana appeals the dismissal of his petition for writ of habeas corpus alleging "that he is entitled to immediate release when properly credited with time served" and requesting "issuance of an Order com[m]anding the Florida Department of Corrections ... to immediately release" him. Without prior notice to the parties or input from them, the trial court summarily dismissed the petition. It reasoned, in part, that Mr. Santana failed to exhaust administrative remedies, although the Department of Corrections (DOC) never raised this below.[1] Mindful that the "writ of habeas *845 corpus is the fundamental instrument for safeguarding individual freedom against arbitrary and lawless state action," Harris v. Nelson, 394 U.S. 286, 290-91, 89 S.Ct. 1082, 22 L.Ed.2d 281 (1969), we reverse and remand with directions that the trial court issue an order to show cause to the Department of Corrections before proceeding further.
The petition below alleges that, after his probation (in three separate cases) was revoked, Mr. Santana was sentenced anew on October 4, 2007, receiving three concurrent prison sentences. In case No. 95-CF-4926, the petition alleges, he was sentenced to six years in prison with credit for 2,023 days, to be followed by two years' probation; in case No. 96-CF-9601 to 60.75 months with credit for 831 days; and in case No. 96-CF-10668 to six years with credit for 1682 days.[2] In addition, against each sentence, the petition alleges, he was awarded "credit for time served at the State Hospital,"[3] and a separate credit for 142 days for time spent in jail before the revocation hearing. See generally Barnishin v. State, 927 So.2d 68, 71 (Fla. 1st DCA 2006) (stating that, following arrest for probation violations, too, "when a defendant is entitled to presentence jail-time credit against concurrent sentences, jail time must be credited against each concurrent sentence" (citing Daniels v. State, 491 So.2d 543, 545 (Fla.1986))).
Attached to the petition are the sentencing documents, as well as a transcript of the sentencing hearing. See Williams v. State, 957 So.2d 600, 603 (Fla.2007) (citing Ashley v. State, 850 So.2d 1265, 1268 (Fla. 2003)) (holding that the sentencing court's oral pronouncement controls in the event of a discrepancy between oral pronouncement and written sentence); Shands Teaching Hosp. & Clinics, Inc. v. Beech St. Corp., 899 So.2d 1222, 1224 (Fla. 1st DCA 2005) (quoting City of Gainesville v. State, Dep't of Transp., 778 So.2d 519, 522 (Fla. 1st DCA 2001)); Abele v. Sawyer, 750 So.2d 70, 74 (Fla. 4th DCA 1999) ("While the court must confine its review to the four corners of the complaint ..., the exhibits are encompassed within the four corners of the complaint and must be considered therewith. See Fla. R. Civ. P. 1.130(b)." (internal citation omitted)).
At issue is whether the habeas court properly dismissed the petition on its own motion without hearing from the authorities alleged to hold the petitioner unlawfully. We are not concerned here with mere conditions of confinement, cf. Harvard v. Singletary, 733 So.2d 1020, 1021 (Fla.1999) (alleged misassignment to close management status); Sykes v. State, 974 So.2d 1133, 1134 (Fla. 1st DCA 2008) (alleged misassignment to particular correctional institution); Moore v. Dugger, 613 So.2d 571, 572 (Fla. 1st DCA 1993) (alleged misclassification); Van Poyck v. Dugger, 579 So.2d 346, 347 (Fla. 1st DCA 1991) (alleged poor ventilation), or gain-time calculations not affecting DOC's current right *846 to hold the petitioner, see Williams v. State, 519 So.2d 723, 724 (Fla. 5th DCA 1988); Sutton v. Strickland, 485 So.2d 25, 25 (Fla. 1st DCA 1986), or anything less than a state prisoner's alleged right to immediate release from custody.
"The writ of habeas corpus is a high prerogative writ of ancient origin designed to obtain immediate relief from unlawful imprisonment without sufficient legal reasons. Essentially, it is a writ of inquiry and is issued to test the reasons or grounds of restraint and detention. The writ is venerated by all free and liberty loving people and recognized as a fundamental guaranty and protection of their right of liberty." Allison v. Baker, 152 Fla. 274, 275, 11 So.2d 578, 579 (1943). "The great writ has its origins in antiquity and its parameters have been shaped by suffering and deprivation. It is more than a privilege with which free men are endowed by constitutional mandate; it is a writ of ancient right." Jamason v. State, 447 So.2d 892, 894 (Fla. 4th DCA 1983), decision approved, 455 So.2d 380 (Fla. 1984). "[H]istorically, habeas corpus is a high prerogative writ. It is as old as the common law itself and is an integral part of our own democratic process." Anglin v. Mayo, 88 So.2d 918, 919 (Fla.1956).
By comparison, judicial abstention in favor of exhaustion of administrative remedies is a relatively recent invention. The doctrine of exhaustion of remedies counsels against judicial intervention in the decision-making function of the executive branch in certain circumstances. See Art. V, 5(b) & 20(c)(3), Fla. Const.; Key Haven Associated Enters., Inc. v. Bd. of Trs. of the Internal Improvement Trust Fund, 427 So.2d 153, 157 (Fla.1982); State ex rel. Dep't of Gen. Servs. v. Willis, 344 So.2d 580, 589 (Fla. 1st DCA 1977). Whether to require exhaustion of administrative remedies is a question of judicial "policy rather than power." Gulf Pines Mem'l Park, Inc. v. Oaklawn Mem'l Park, Inc., 361 So.2d 695, 699 (Fla.1978). See also State, Dep't of Revenue v. Brock, 576 So.2d 848, 850 (Fla. 1st DCA 1991) ("[T]he doctrine requiring the exhaustion of administrative remedies is not jurisdictional. The exhaustion requirement is a court-created prudential doctrine; it is a matter of policy, not of power." (citations omitted)).
Notions of administrative autonomy have been thought to require that agencies be given the opportunity to discover and correct their own errors, even after a case has reached the courts for judicial review of agency action. See Rice v. Dep't of Health & Rehabilitative Servs., 386 So.2d 844, 847 (Fla. 1st DCA 1980). In some contexts, judicial restraint may be necessary "to support the integrity of the administrative process and to allow the executive branch to carry out its responsibilities as a co-equal branch of government." Key Haven, 427 So.2d at 157. When an agency has discretion to exercise, it should of course be allowed to make discretionary decisions. If a party succeeds in vindicating its rights in the administrative process, thus obviating the need for judicial intervention, judicial resources are conserved; and immediate judicial access can weaken the effectiveness of an agency by encouraging people to ignore its procedures. See, e.g., Weinberger v. Salfi, 422 U.S. 749, 765, 95 S.Ct. 2457, 45 L.Ed.2d 522 (1975).
But the rationales for requiring exhaustion of administrative remedies diminish and disappear where an executive branch agency has little or no discretion to exercise and little or no expertise to bring to bear. The Department of Corrections does have discretion in deciding, for example, *847 the conditions of confinement, and does have its own procedures on this subject deserving of judicial deference. On the other hand, the DOC has no discretion about which prisoners to release upon expiration of their sentences. Sentencing is a power, obligation, and prerogative of the courts, not the DOC. See Pearson v. Moore, 767 So.2d 1235, 1239 (Fla. 1st DCA 2000), approved sub nom. Moore v. Pearson, 789 So.2d 316 (Fla.2001).
In State, Department of Environmental Regulation v. Falls Chase Special Taxing District, 424 So.2d 787, 796 (Fla. 1st DCA 1982), we recognized an important exception to the exhaustion doctrine:
When an agency acts without colorable statutory authority that is clearly in excess of its delegated powers, a party is not required to exhaust administrative remedies before seeking judicial relief.
(Footnote omitted.) See generally St. Joe Paper Co. v. Fla. Dep't of Natural Res., 536 So.2d 1119, 1124 (Fla. 1st DCA 1988); Dep't of Prof'l Regulation, Fla. State Bd. of Med. v. Marrero, 536 So.2d 1094, 1095-96 (Fla. 1st DCA 1988).
Mr. Santana's claim that the DOC has no lawful authority to hold him is analogous to a "challenge to agency jurisdiction," which, when made "on persuasive grounds," constitutes "a widely recognized exception to the exhaustion doctrine." Falls Chase, 424 So.2d at 794 (footnote omitted). An exception available when the Department of Environmental Regulation's jurisdiction to regulate dredge and fill activities is challenged, see id. at 793, ought arguably apply to a challenge to the jurisdiction of the DOC to hold a citizen prisoner. An exception to the exhaustion doctrine for petitioners alleging entitlement in plausible detail to immediate release from custody would not do violence to the contours of the exhaustion doctrine as understood in other settings.
We need not decide whether such an exception exists here, however, and assume for purposes of decision that it does not. See Bush v. State, 945 So.2d 1207, 1210 (Fla.2006). While the general rule is that exhaustion of administrative remedies is an affirmative defense, see Sylvia H. Walbolt, Matthew J. Conigliaro & J. Andrew Meyer, Florida Civil Practice Before Trial, § 25.34 at 25-30 (7th ed. 2004) ("Affirmative defenses to extraordinary writs include impossibility or lack of power to perform, laches, unclean hands, absence of parties whose substantial rights would be affected, illegality of purpose, detriment to the public interest, mootness, and failure to exhaust administrative remedies. See FLORIDA APPELLATE PRACTICE § 20.38 (Fla. Bar CLE 5th ed.2003)"), moreover, in the prisoner habeas context, it has been held that certain petitions must allege exhaustion of administrative remedies in order to be facially sufficient. But none of the cases to which our attention has been drawn has laid down such a pleading requirement for petitions for writ of habeas corpus alleging entitlement to immediate release. The petitioners in Harvard v. Singletary, Moore v. Dugger, and Sutton v. Strickland sought relief from conditions of confinement or restoration of forfeited gain time, not immediate release from the DOC's custody. See Harvard, 733 So.2d at 1021; Moore, 613 So.2d at 572; Sutton, 485 So.2d at 25.
In any event, we hold the trial court erred by dismissing Mr. Santana's petition for writ of habeas corpus on the basis of a technicality  an assumed pleading defect  that was not raised by the parties. It is not clear DOC would have defended in this fashion, left to its own devices. "A trial judge may not sua sponte dismiss an action based on affirmative defenses not raised by proper pleadings." Liton Lighting v. Platinum Television *848 Group, Inc., 2 So.3d 366, 367 (Fla. 4th DCA 2008) (citing Kerrigan, Estess, Rankin & McLeod v. State, 711 So.2d 1246, 1249 (Fla. 4th DCA 1998)). See Lenoir v. Jones, 979 So.2d 1129, 1130 (Fla. 1st DCA 2008) (trial court's sua sponte dismissal reversed because plaintiff was not given notice or an opportunity to be heard); Mumma v. Mumma, 734 So.2d 571, 571 (Fla. 4th DCA 1999) (trial court did not have inherent power to dismiss causes as not being prosecuted with reasonable diligence); Lambrix v. Dugger, 547 So.2d 1265, 1266 (Fla. 1st DCA 1989) (trial court erred in dismissing prisoner's complaint against prison officials who did not move to dismiss).
The general rule that pleadings ought not be dismissed on grounds no party urges has special force when the pleading is a petition for writ of habeas corpus. "The scope and flexibility of the writ  its capacity to reach all manner of illegal detention  its ability to cut through barriers of form and procedural mazes  have always been emphasized and jealously guarded by courts and lawmakers. The very nature of the writ demands that it be administered with the initiative and flexibility essential to insure that miscarriages of justice within its reach are surfaced and corrected." Harris v. Nelson, 394 U.S. 286, 291, 89 S.Ct. 1082, 22 L.Ed.2d 281 (1969). When a petition for writ of habeas corpus alleging that the petitioner is entitled to immediate release sets out plausible reasons and a specific factual basis in some detail, the custodian should be required to respond to the petition. To the extent Pope v. State, 898 So.2d 253 (Fla. 3d DCA 2005), holds to the contrary, we certify conflict.
If in this case the petition had not been summarily denied and the trial court had instead ordered the DOC to show cause why Mr. Santana's petition should not be granted, the DOC might have resisted by moving to dismiss on exhaustion of administrative remedies grounds or for failure to allege exhaustion but it might also have decided that the petition was meritorious and released the petitioner. "The procedure for the granting of this particular writ [i.e., habeas corpus] is not to be circumscribed by hard and fast rules or technicalities which often accompany our consideration of other processes. If it appears to a court of competent jurisdiction that a man is being illegally restrained of his liberty, it is the responsibility of the court to brush aside formal technicalities and issue such appropriate orders as will do justice. In habeas corpus the niceties of the procedure are not anywhere near as important as the determination of the ultimate question as to the legality of the restraint." Anglin, 88 So.2d at 919-20.
Reversed and remanded.
WEBSTER and ROBERTS, JJ., concur.
NOTES
[1]  The order on review dismissing the habeas petition provides in part:

To the extent [Mr. Santana] seeks an order directing the Department to award proper credit for time served, he should have sought relief through the inmate grievance procedure. In the instant case, Petitioner failed to demonstrate the exhaustion of his administrative remedies with the Department.
(Internal citations omitted.)
[2]  Thus credit was not, according to the petition, simply "to be calculated by the Department of Corrections" as was the situation in Hardenbrook v. State, 953 So.2d 717, 719 n. 2 (Fla. 1st DCA 2007).
[3]  At the sentencing hearing, testimony that he had spent six months at a state hospital in connection with an evaluation of his competency to stand trial was uncontroverted. It is by no means clear that DOC's records document the length of appellant's stay at the state hospital, which is presumably operated not by the Department of Corrections but by the Department of Children and Family Services.
