{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a8b3d3eb155b",
   "metadata": {},
   "source": [
    "# Chapter 27: The Mathematics of Delegation\n",
    "\n",
    "A controller reaches the edge of its authority and asks for review. That is often the correct action, but delegation does not make the remaining work disappear. A reviewer has finite service capacity, and a growing queue can consume the deadline before approval arrives.\n",
    "\n",
    "This notebook combines an authority handoff predicate with a simple review queue. It identifies tasks that may proceed autonomously, tasks requiring a review packet, and tasks whose declared mean review time exceeds their deadline. The method keeps review receipt separate from human authority. Its output is a local scheduling and authorization diagnostic, not a sent message or a prediction that a particular reviewer will answer on time.\n",
    "\n",
    "**Outcome:** Compute a review queue diagnostic and identify required handoff packets and release blockers.\n",
    "\n",
    "- Inspect the declared input contract\n",
    "- Predict the hand-checkable case\n",
    "- Run the shared computation\n",
    "- Change the critical assumption\n",
    "- Apply the method to the transfer data\n",
    "\n",
    "**Guided route:** Run the worked calculation, inspect its figure, change the stated assumption, and try the transfer case. Read the explanations beside each result before opening the answers.\n",
    "\n",
    "**Deeper route:** First read the mathematics and canonical equation reference. Audit the input contract, predict the changed result, then inspect the shared chapter implementation and solve the questions independently. Both routes use the same calculations and preserve the equations."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a7781f3ee7dc",
   "metadata": {},
   "source": [
    "## Technical Requirements\n",
    "\n",
    "Python 3.11 or later, the complete laboratory folder, and the notebook dependencies listed in `requirements-notebooks.txt` (the launcher's **Install notebook tools** choice installs them; see START-HERE). Standard-library chapter commands also support Python 3.10. No API key, model account or network call is used by this experiment.\n",
    "\n",
    "Prior knowledge:\n",
    "\n",
    "- Python lists and dictionaries\n",
    "- The mapped chapter and its notation"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "db94aee5e1ff",
   "metadata": {},
   "source": [
    "## The question and its mathematics\n",
    "\n",
    "Let total arrival rate be lambda and delegated fraction f. Effective review arrival is lambda_review=lambda f. Under an M/M/1 construction with service rate mu, the queue is stable when lambda_review<mu. Mean system time, including service, is W=1/(mu-lambda_review). At or above capacity no finite stationary mean exists, so the output remains unavailable.\n",
    "\n",
    "A task requires delegation when the agent lacks authority or its declared risk exceeds the autonomous threshold. In this simplified contract, autonomous release requires current agent authority and acceptable risk. Delegated release requires human authority, a received review, and a mean-time diagnostic that does not exceed the task deadline.\n",
    "\n",
    "The last predicate is deliberately a planning convention, not a pathwise guarantee. Queue mean W is not a measured duration for an individual task. A receipt may in reality arrive earlier or later. The returned flag identifies incompatibility between the declared capacity plan and deadline, and should not be mistaken for evidence that a specific observed review was late."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5ef8f07ce6ef",
   "metadata": {},
   "source": [
    "## A calculation you can run\n",
    "\n",
    "Supply arrival and service rates in the same time unit, delegated fraction, risk threshold, and task rows. Exact booleans distinguish current agent authority, human authority, and review receipt. Validation requires positive service rate, valid probabilities, and nonnegative deadlines.\n",
    "\n",
    "The function calculates effective load, stability, and mean sojourn when available. It then emits delegation-required, queue-mean-exceeds-deadline, release, and review-packet-required flags for each task. The sensitivity figure varies review arrivals below service capacity and shows how mean time grows near saturation. In the changed case increase the delegated fraction while preserving total arrivals and service rate. Predict which releases the capacity contract will block.\n",
    "\n",
    "The next cell finds the bundle and imports the same computation used by the chapter skill. It does not change your system Python."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "42ebefc0e5c1",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pathlib import Path\n",
    "import sys, json\n",
    "LAB_ROOT = next((p for p in [Path.cwd(), *Path.cwd().parents] if (p / \"lab-manifest.json\").is_file()), None)\n",
    "if LAB_ROOT is None:\n",
    "    raise RuntimeError(\"Open this notebook from the complete extracted laboratory folder.\")\n",
    "sys.path.insert(0, str(LAB_ROOT / \"src\"))\n",
    "from math_ai_agents.core import analyze, report_text\n",
    "from math_ai_agents.plotting import figure_svg\n",
    "from IPython.display import SVG, display\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7fffde8c0edb",
   "metadata": {},
   "source": [
    "Set the declared inputs below. These are constructed teaching values, not measurements from a production agent. Change a value only after predicting what it should change."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "e79ab0f7dfee",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Chapter 27: delegation-queue\n",
      "Can human review supply missing authority within the workflow's capacity and timing contract?\n",
      "Evidence: constructed teaching example\n",
      "\n",
      "Calculated quantities:\n",
      "{\n",
      "  \"effective_review_arrival_rate\": 2.0,\n",
      "  \"queue_stable\": true,\n",
      "  \"mean_review_sojourn\": 1.0,\n",
      "  \"tasks\": [\n",
      "    {\n",
      "      \"task\": \"routine\",\n",
      "      \"delegation_required\": false,\n",
      "      \"queue_mean_exceeds_deadline\": false,\n",
      "      \"released\": true,\n",
      "      \"review_packet_required\": false\n",
      "    },\n",
      "    {\n",
      "      \"task\": \"release\",\n",
      "      \"delegation_required\": true,\n",
      "      \"queue_mean_exceeds_deadline\": false,\n",
      "      \"released\": true,\n",
      "      \"review_packet_required\": false\n",
      "    },\n",
      "    {\n",
      "      \"task\": \"sensitive\",\n",
      "      \"delegation_required\": true,\n",
      "      \"queue_mean_exceeds_deadline\": false,\n",
      "      \"released\": false,\n",
      "      \"review_packet_required\": true\n",
      "    }\n",
      "  ],\n",
      "  \"released_count\": 2\n",
      "}\n",
      "\n",
      "Interpretation:\n",
      "Delegation moves work into a limited review service. Missing authority remains a release blocker even when expected review capacity is adequate.\n",
      "\n",
      "Assumptions:\n",
      "- M/M/1 review queue with Poisson arrivals, exponential service, one reviewer.\n",
      "- Arrival and service rates use the same time unit.\n",
      "- Deadline test uses mean queue sojourn as a diagnostic, not a per-task prediction.\n",
      "\n",
      "Limitations:\n",
      "- A queue mean does not prove any individual deadline will be met.\n",
      "- A human review receipt grants only the supplied current authority.\n",
      "\n",
      "Execution: completed locally; constructed inputs are not deployment measurements.\n"
     ]
    }
   ],
   "source": [
    "chapter = 27\n",
    "inputs = {'arrival_rate': 4,\n",
    " 'service_rate': 3,\n",
    " 'delegation_fraction': 0.5,\n",
    " 'agent_risk_limit': 0.1,\n",
    " 'tasks': [{'name': 'routine',\n",
    "            'agent_authorized': True,\n",
    "            'risk': 0.02,\n",
    "            'deadline': 2,\n",
    "            'human_authorized': False,\n",
    "            'review_received': False},\n",
    "           {'name': 'release',\n",
    "            'agent_authorized': False,\n",
    "            'risk': 0.05,\n",
    "            'deadline': 2,\n",
    "            'human_authorized': True,\n",
    "            'review_received': True},\n",
    "           {'name': 'sensitive',\n",
    "            'agent_authorized': False,\n",
    "            'risk': 0.2,\n",
    "            'deadline': 2,\n",
    "            'human_authorized': True,\n",
    "            'review_received': False}]}\n",
    "report = analyze(chapter, inputs)\n",
    "# This input was explicitly taken from the teaching fixture.\n",
    "report['evidence_kind'] = 'constructed teaching example'\n",
    "print(report_text(report))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8bf96b02e40a",
   "metadata": {},
   "source": [
    "Default review load is 4(0.5)=2, below service rate 3. Mean sojourn is 1/(3-2)=1 time unit. Routine is authorized and below the agent risk limit, so it can proceed autonomously. Release requires delegation but has human authority and a received review, with deadline 2 exceeding the mean diagnostic, so it passes the declared release contract.\n",
    "\n",
    "Sensitive lacks a received review and requires a packet. With delegated fraction 0.9, review load becomes 3.6, exceeding capacity. The stationary mean is unavailable and delegated release is blocked by the capacity plan. Routine remains autonomous. Review capacity did not change the underlying authority predicate.\n",
    "\n",
    "The mean-versus-deadline flag is a planning diagnostic, not a probability of meeting the deadline. If the M/M/1 first-come-first-served assumption held exactly, sojourn time would be exponential with rate 3-2=1, so with deadline 2 the chance of meeting it would be 1-exp(-2), about 0.865, even though the mean 1 is below 2. This probability is a hand calculation, not a field the lab reports.\n",
    "\n",
    "The plot below uses the calculated quantities. Read each panel's units before comparing its values."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "590f57e74048",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Matplotlib is building the font cache; this may take a moment.\n"
     ]
    },
    {
     "data": {
      "image/svg+xml": [
       "<svg xmlns:xlink=\"http://www.w3.org/1999/xlink\" xmlns=\"http://www.w3.org/2000/svg\" width=\"576pt\" height=\"237.6pt\" viewBox=\"0 0 576 237.6\" version=\"1.1\"><title>Calculated chapter experiment</title><desc>Labeled plot of the explicitly supplied chapter inputs. See the adjacent explanation for assumptions.</desc>\n",
       " <metadata>\n",
       "  <rdf:RDF xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:cc=\"http://creativecommons.org/ns#\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n",
       "   <cc:Work>\n",
       "    <dc:type rdf:resource=\"http://purl.org/dc/dcmitype/StillImage\"/>\n",
       "    <dc:format>image/svg+xml</dc:format>\n",
       "    <dc:creator>\n",
       "     <cc:Agent>\n",
       "      <dc:title>Mathematics of AI Agents Laboratory</dc:title>\n",
       "     </cc:Agent>\n",
       "    </dc:creator>\n",
       "   </cc:Work>\n",
       "  </rdf:RDF>\n",
       " </metadata>\n",
       " <defs>\n",
       "  <style type=\"text/css\">*{stroke-linejoin: round; stroke-linecap: butt}</style>\n",
       " </defs>\n",
       " <g id=\"figure_1\">\n",
       "  <g id=\"patch_1\">\n",
       "   <path d=\"M 0 237.6  L 576 237.6  L 576 0  L 0 0  z \" style=\"fill: #ffffff\"/>\n",
       "  </g>\n",
       "  <g id=\"axes_1\">\n",
       "   <g id=\"patch_2\">\n",
       "    <path d=\"M 45.082344 195.477656  L 565.2 195.477656  L 565.2 60.080234  L 45.082344 60.080234  z \" style=\"fill: #ffffff\"/>\n",
       "   </g>\n",
       "   <g id=\"matplotlib.axis_1\">\n",
       "    <g id=\"xtick_1\">\n",
       "     <g id=\"line2d_1\">\n",
       "      <defs>\n",
       "       <path id=\"m2a09c72a3a\" d=\"M 0 0  L 0 3.5  \" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </defs>\n",
       "      <g>\n",
       "       <use xlink:href=\"#m2a09c72a3a\" x=\"68.724055\" y=\"195.477656\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_1\">\n",
       "      <text style=\"font-size: 10px; font-family: 'DejaVu Sans'; text-anchor: middle\" x=\"68.724055\" y=\"210.075312\" transform=\"rotate(-0 68.724055 210.075312)\">0.0</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_2\">\n",
       "     <g id=\"line2d_2\">\n",
       "      <g>\n",
       "       <use xlink:href=\"#m2a09c72a3a\" x=\"148.325778\" y=\"195.477656\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_2\">\n",
       "      <text style=\"font-size: 10px; font-family: 'DejaVu Sans'; text-anchor: middle\" x=\"148.325778\" y=\"210.075312\" transform=\"rotate(-0 148.325778 210.075312)\">0.5</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_3\">\n",
       "     <g id=\"line2d_3\">\n",
       "      <g>\n",
       "       <use xlink:href=\"#m2a09c72a3a\" x=\"227.927501\" y=\"195.477656\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_3\">\n",
       "      <text style=\"font-size: 10px; font-family: 'DejaVu Sans'; text-anchor: middle\" x=\"227.927501\" y=\"210.075312\" transform=\"rotate(-0 227.927501 210.075312)\">1.0</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_4\">\n",
       "     <g id=\"line2d_4\">\n",
       "      <g>\n",
       "       <use xlink:href=\"#m2a09c72a3a\" x=\"307.529224\" y=\"195.477656\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_4\">\n",
       "      <text style=\"font-size: 10px; font-family: 'DejaVu Sans'; text-anchor: middle\" x=\"307.529224\" y=\"210.075312\" transform=\"rotate(-0 307.529224 210.075312)\">1.5</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_5\">\n",
       "     <g id=\"line2d_5\">\n",
       "      <g>\n",
       "       <use xlink:href=\"#m2a09c72a3a\" x=\"387.130946\" y=\"195.477656\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_5\">\n",
       "      <text style=\"font-size: 10px; font-family: 'DejaVu Sans'; text-anchor: middle\" x=\"387.130946\" y=\"210.075312\" transform=\"rotate(-0 387.130946 210.075312)\">2.0</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_6\">\n",
       "     <g id=\"line2d_6\">\n",
       "      <g>\n",
       "       <use xlink:href=\"#m2a09c72a3a\" x=\"466.732669\" y=\"195.477656\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_6\">\n",
       "      <text style=\"font-size: 10px; font-family: 'DejaVu Sans'; text-anchor: middle\" x=\"466.732669\" y=\"210.075312\" transform=\"rotate(-0 466.732669 210.075312)\">2.5</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_7\">\n",
       "     <g id=\"line2d_7\">\n",
       "      <g>\n",
       "       <use xlink:href=\"#m2a09c72a3a\" x=\"546.334392\" y=\"195.477656\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_7\">\n",
       "      <text style=\"font-size: 10px; font-family: 'DejaVu Sans'; text-anchor: middle\" x=\"546.334392\" y=\"210.075312\" transform=\"rotate(-0 546.334392 210.075312)\">3.0</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"text_8\">\n",
       "     <text style=\"font-size: 10px; font-family: 'DejaVu Sans'; text-anchor: middle\" x=\"305.141172\" y=\"224.076094\" transform=\"rotate(-0 305.141172 224.076094)\">review arrivals per time unit</text>\n",
       "    </g>\n",
       "   </g>\n",
       "   <g id=\"matplotlib.axis_2\">\n",
       "    <g id=\"ytick_1\">\n",
       "     <g id=\"line2d_8\">\n",
       "      <path d=\"M 45.082344 190.566547  L 565.2 190.566547  \" clip-path=\"url(#p230bcac566)\" style=\"fill: none; stroke: #d4d8da; stroke-width: 0.6; stroke-linecap: square\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_9\">\n",
       "      <defs>\n",
       "       <path id=\"m58a1dc89b3\" d=\"M 0 0  L -3.5 0  \" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </defs>\n",
       "      <g>\n",
       "       <use xlink:href=\"#m58a1dc89b3\" x=\"45.082344\" y=\"190.566547\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_9\">\n",
       "      <text style=\"font-size: 10px; font-family: 'DejaVu Sans'; text-anchor: end\" x=\"38.082344\" y=\"194.365375\" transform=\"rotate(-0 38.082344 194.365375)\">0</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_2\">\n",
       "     <g id=\"line2d_10\">\n",
       "      <path d=\"M 45.082344 153.266982  L 565.2 153.266982  \" clip-path=\"url(#p230bcac566)\" style=\"fill: none; stroke: #d4d8da; stroke-width: 0.6; stroke-linecap: square\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_11\">\n",
       "      <g>\n",
       "       <use xlink:href=\"#m58a1dc89b3\" x=\"45.082344\" y=\"153.266982\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_10\">\n",
       "      <text style=\"font-size: 10px; font-family: 'DejaVu Sans'; text-anchor: end\" x=\"38.082344\" y=\"157.06581\" transform=\"rotate(-0 38.082344 157.06581)\">10</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_3\">\n",
       "     <g id=\"line2d_12\">\n",
       "      <path d=\"M 45.082344 115.967416  L 565.2 115.967416  \" clip-path=\"url(#p230bcac566)\" style=\"fill: none; stroke: #d4d8da; stroke-width: 0.6; stroke-linecap: square\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_13\">\n",
       "      <g>\n",
       "       <use xlink:href=\"#m58a1dc89b3\" x=\"45.082344\" y=\"115.967416\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_11\">\n",
       "      <text style=\"font-size: 10px; font-family: 'DejaVu Sans'; text-anchor: end\" x=\"38.082344\" y=\"119.766244\" transform=\"rotate(-0 38.082344 119.766244)\">20</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_4\">\n",
       "     <g id=\"line2d_14\">\n",
       "      <path d=\"M 45.082344 78.667851  L 565.2 78.667851  \" clip-path=\"url(#p230bcac566)\" style=\"fill: none; stroke: #d4d8da; stroke-width: 0.6; stroke-linecap: square\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_15\">\n",
       "      <g>\n",
       "       <use xlink:href=\"#m58a1dc89b3\" x=\"45.082344\" y=\"78.667851\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_12\">\n",
       "      <text style=\"font-size: 10px; font-family: 'DejaVu Sans'; text-anchor: end\" x=\"38.082344\" y=\"82.466679\" transform=\"rotate(-0 38.082344 82.466679)\">30</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"text_13\">\n",
       "     <text style=\"font-size: 10px; font-family: 'DejaVu Sans'; text-anchor: middle\" x=\"18.955\" y=\"127.778945\" transform=\"rotate(-90 18.955 127.778945)\">mean review time</text>\n",
       "    </g>\n",
       "   </g>\n",
       "   <g id=\"line2d_16\">\n",
       "    <path d=\"M 68.724055 189.323228  L 73.500159 189.310669  L 78.276262 189.297854  L 83.052365 189.284775  L 87.828469 189.271423  L 92.604572 189.25779  L 97.380676 189.243867  L 102.156779 189.229645  L 106.932882 189.215113  L 111.708986 189.200262  L 116.485089 189.185081  L 121.261192 189.169559  L 126.037296 189.153685  L 130.813399 189.137445  L 135.589502 189.120827  L 140.365606 189.103819  L 145.141709 189.086405  L 149.917813 189.068572  L 154.693916 189.050304  L 159.470019 189.031585  L 164.246123 189.012398  L 169.022226 188.992726  L 173.798329 188.972548  L 178.574433 188.951847  L 183.350536 188.930601  L 188.126639 188.908788  L 192.902743 188.886386  L 197.678846 188.86337  L 202.45495 188.839715  L 207.231053 188.815394  L 212.007156 188.790377  L 216.78326 188.764635  L 221.559363 188.738137  L 226.335466 188.710847  L 231.11157 188.68273  L 235.887673 188.653749  L 240.663776 188.623861  L 245.43988 188.593025  L 250.215983 188.561194  L 254.992087 188.528319  L 259.76819 188.494349  L 264.544293 188.459227  L 269.320397 188.422894  L 274.0965 188.385286  L 278.872603 188.346335  L 283.648707 188.305967  L 288.42481 188.264105  L 293.200913 188.220662  L 297.977017 188.175549  L 302.75312 188.128667  L 307.529224 188.079909  L 312.305327 188.029161  L 317.08143 187.976299  L 321.857534 187.921188  L 326.633637 187.86368  L 331.40974 187.803616  L 336.185844 187.740822  L 340.961947 187.675108  L 345.73805 187.606264  L 350.514154 187.534062  L 355.290257 187.45825  L 360.066361 187.37855  L 364.842464 187.294655  L 369.618567 187.206226  L 374.394671 187.112883  L 379.170774 187.014207  L 383.946877 186.909727  L 388.722981 186.798914  L 393.499084 186.681175  L 398.275187 186.555841  L 403.051291 186.422151  L 407.827394 186.27924  L 412.603498 186.126122  L 417.379601 185.961662  L 422.155704 185.784551  L 426.931808 185.593271  L 431.707911 185.386052  L 436.484014 185.160813  L 441.260118 184.915098  L 446.036221 184.645981  L 450.812324 184.349953  L 455.588428 184.022763  L 460.364531 183.65922  L 465.140635 183.252907  L 469.916738 182.795804  L 474.692841 182.277755  L 479.468945 181.685698  L 484.245048 181.002556  L 489.021151 180.205556  L 493.797255 179.263648  L 498.573358 178.133358  L 503.349461 176.751893  L 508.125565 175.025061  L 512.901668 172.804849  L 517.677772 169.844566  L 522.453875 165.70017  L 527.229978 159.483576  L 532.006082 149.122585  L 536.782185 128.400605  L 541.558288 66.234663  \" clip-path=\"url(#p230bcac566)\" style=\"fill: none; stroke: #31586b; stroke-width: 1.8; stroke-linecap: square\"/>\n",
       "   </g>\n",
       "   <g id=\"patch_3\">\n",
       "    <path d=\"M 45.082344 195.477656  L 45.082344 60.080234  \" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n",
       "   </g>\n",
       "   <g id=\"patch_4\">\n",
       "    <path d=\"M 45.082344 195.477656  L 565.2 195.477656  \" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n",
       "   </g>\n",
       "   <g id=\"text_14\">\n",
       "    <text style=\"font-size: 11px; font-family: 'DejaVu Sans'; text-anchor: start\" x=\"45.082344\" y=\"54.080234\" transform=\"rotate(-0 45.082344 54.080234)\">review queue sensitivity</text>\n",
       "   </g>\n",
       "  </g>\n",
       "  <g id=\"text_15\">\n",
       "   <text style=\"font-size: 12px; font-family: 'DejaVu Sans'; text-anchor: start\" x=\"46.08\" y=\"13.870125\" transform=\"rotate(-0 46.08 13.870125)\">Chapter 27: delegation queue</text>\n",
       "  </g>\n",
       " </g>\n",
       " <defs>\n",
       "  <clipPath id=\"p230bcac566\">\n",
       "   <rect x=\"45.082344\" y=\"60.080234\" width=\"520.117656\" height=\"135.397422\"/>\n",
       "  </clipPath>\n",
       " </defs>\n",
       "</svg>"
      ],
      "text/plain": [
       "<IPython.core.display.SVG object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "display(SVG(figure_svg(report)))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a98d917f184b",
   "metadata": {},
   "source": [
    "**Figure 27.L1:** Calculated chapter experiment. Each panel labels its input and output units; interpret it under the assumptions printed in the report."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f99a34e2aa52",
   "metadata": {},
   "source": [
    "## Change the assumption\n",
    "\n",
    "Delegation can fail when it becomes an automatic escape from uncertainty without a capacity model. Sending every doubtful task to a fixed reviewer raises arrival load and can make the queue unstable. The sensitivity curve shows that even stable load near capacity creates large mean delays.\n",
    "\n",
    "A queue calculation can also be overread. M/M/1 assumes Poisson arrivals, exponential service, one reviewer, and stationary rates. Real review teams may batch work, prioritize cases, or have correlated arrivals. A single mean cannot prove deadline compliance or determine an actual task's elapsed review time.\n",
    "\n",
    "Authority remains independent. A review receipt from someone without the required permission cannot authorize release. Conversely, the method's mean-time flag is not evidence that an already received review was late. Preserve actual timestamps for empirical deadline checks and use this output as a capacity-design diagnostic."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "57e716511af8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Chapter 27: delegation-queue\n",
      "Can human review supply missing authority within the workflow's capacity and timing contract?\n",
      "Evidence: constructed changed-assumption example\n",
      "\n",
      "Calculated quantities:\n",
      "{\n",
      "  \"effective_review_arrival_rate\": 3.6,\n",
      "  \"queue_stable\": false,\n",
      "  \"mean_review_sojourn\": null,\n",
      "  \"tasks\": [\n",
      "    {\n",
      "      \"task\": \"routine\",\n",
      "      \"delegation_required\": false,\n",
      "      \"queue_mean_exceeds_deadline\": false,\n",
      "      \"released\": true,\n",
      "      \"review_packet_required\": false\n",
      "    },\n",
      "    {\n",
      "      \"task\": \"release\",\n",
      "      \"delegation_required\": true,\n",
      "      \"queue_mean_exceeds_deadline\": true,\n",
      "      \"released\": false,\n",
      "      \"review_packet_required\": false\n",
      "    },\n",
      "    {\n",
      "      \"task\": \"sensitive\",\n",
      "      \"delegation_required\": true,\n",
      "      \"queue_mean_exceeds_deadline\": true,\n",
      "      \"released\": false,\n",
      "      \"review_packet_required\": true\n",
      "    }\n",
      "  ],\n",
      "  \"released_count\": 1\n",
      "}\n",
      "\n",
      "Interpretation:\n",
      "Delegation moves work into a limited review service. Missing authority remains a release blocker even when expected review capacity is adequate.\n",
      "\n",
      "Assumptions:\n",
      "- M/M/1 review queue with Poisson arrivals, exponential service, one reviewer.\n",
      "- Arrival and service rates use the same time unit.\n",
      "- Deadline test uses mean queue sojourn as a diagnostic, not a per-task prediction.\n",
      "\n",
      "Limitations:\n",
      "- A queue mean does not prove any individual deadline will be met.\n",
      "- A human review receipt grants only the supplied current authority.\n",
      "\n",
      "Execution: completed locally; constructed inputs are not deployment measurements.\n"
     ]
    },
    {
     "data": {
      "image/svg+xml": [
       "<svg xmlns:xlink=\"http://www.w3.org/1999/xlink\" xmlns=\"http://www.w3.org/2000/svg\" width=\"576pt\" height=\"237.6pt\" viewBox=\"0 0 576 237.6\" version=\"1.1\"><title>Calculated chapter experiment</title><desc>Labeled plot of the explicitly supplied chapter inputs. See the adjacent explanation for assumptions.</desc>\n",
       " <metadata>\n",
       "  <rdf:RDF xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:cc=\"http://creativecommons.org/ns#\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n",
       "   <cc:Work>\n",
       "    <dc:type rdf:resource=\"http://purl.org/dc/dcmitype/StillImage\"/>\n",
       "    <dc:format>image/svg+xml</dc:format>\n",
       "    <dc:creator>\n",
       "     <cc:Agent>\n",
       "      <dc:title>Mathematics of AI Agents Laboratory</dc:title>\n",
       "     </cc:Agent>\n",
       "    </dc:creator>\n",
       "   </cc:Work>\n",
       "  </rdf:RDF>\n",
       " </metadata>\n",
       " <defs>\n",
       "  <style type=\"text/css\">*{stroke-linejoin: round; stroke-linecap: butt}</style>\n",
       " </defs>\n",
       " <g id=\"figure_1\">\n",
       "  <g id=\"patch_1\">\n",
       "   <path d=\"M 0 237.6  L 576 237.6  L 576 0  L 0 0  z \" style=\"fill: #ffffff\"/>\n",
       "  </g>\n",
       "  <g id=\"axes_1\">\n",
       "   <g id=\"patch_2\">\n",
       "    <path d=\"M 45.082344 195.477656  L 565.2 195.477656  L 565.2 60.080234  L 45.082344 60.080234  z \" style=\"fill: #ffffff\"/>\n",
       "   </g>\n",
       "   <g id=\"matplotlib.axis_1\">\n",
       "    <g id=\"xtick_1\">\n",
       "     <g id=\"line2d_1\">\n",
       "      <defs>\n",
       "       <path id=\"me613909461\" d=\"M 0 0  L 0 3.5  \" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </defs>\n",
       "      <g>\n",
       "       <use xlink:href=\"#me613909461\" x=\"68.724055\" y=\"195.477656\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_1\">\n",
       "      <text style=\"font-size: 10px; font-family: 'DejaVu Sans'; text-anchor: middle\" x=\"68.724055\" y=\"210.075312\" transform=\"rotate(-0 68.724055 210.075312)\">0.0</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_2\">\n",
       "     <g id=\"line2d_2\">\n",
       "      <g>\n",
       "       <use xlink:href=\"#me613909461\" x=\"148.325778\" y=\"195.477656\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_2\">\n",
       "      <text style=\"font-size: 10px; font-family: 'DejaVu Sans'; text-anchor: middle\" x=\"148.325778\" y=\"210.075312\" transform=\"rotate(-0 148.325778 210.075312)\">0.5</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_3\">\n",
       "     <g id=\"line2d_3\">\n",
       "      <g>\n",
       "       <use xlink:href=\"#me613909461\" x=\"227.927501\" y=\"195.477656\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_3\">\n",
       "      <text style=\"font-size: 10px; font-family: 'DejaVu Sans'; text-anchor: middle\" x=\"227.927501\" y=\"210.075312\" transform=\"rotate(-0 227.927501 210.075312)\">1.0</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_4\">\n",
       "     <g id=\"line2d_4\">\n",
       "      <g>\n",
       "       <use xlink:href=\"#me613909461\" x=\"307.529224\" y=\"195.477656\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_4\">\n",
       "      <text style=\"font-size: 10px; font-family: 'DejaVu Sans'; text-anchor: middle\" x=\"307.529224\" y=\"210.075312\" transform=\"rotate(-0 307.529224 210.075312)\">1.5</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_5\">\n",
       "     <g id=\"line2d_5\">\n",
       "      <g>\n",
       "       <use xlink:href=\"#me613909461\" x=\"387.130946\" y=\"195.477656\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_5\">\n",
       "      <text style=\"font-size: 10px; font-family: 'DejaVu Sans'; text-anchor: middle\" x=\"387.130946\" y=\"210.075312\" transform=\"rotate(-0 387.130946 210.075312)\">2.0</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_6\">\n",
       "     <g id=\"line2d_6\">\n",
       "      <g>\n",
       "       <use xlink:href=\"#me613909461\" x=\"466.732669\" y=\"195.477656\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_6\">\n",
       "      <text style=\"font-size: 10px; font-family: 'DejaVu Sans'; text-anchor: middle\" x=\"466.732669\" y=\"210.075312\" transform=\"rotate(-0 466.732669 210.075312)\">2.5</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_7\">\n",
       "     <g id=\"line2d_7\">\n",
       "      <g>\n",
       "       <use xlink:href=\"#me613909461\" x=\"546.334392\" y=\"195.477656\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_7\">\n",
       "      <text style=\"font-size: 10px; font-family: 'DejaVu Sans'; text-anchor: middle\" x=\"546.334392\" y=\"210.075312\" transform=\"rotate(-0 546.334392 210.075312)\">3.0</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"text_8\">\n",
       "     <text style=\"font-size: 10px; font-family: 'DejaVu Sans'; text-anchor: middle\" x=\"305.141172\" y=\"224.076094\" transform=\"rotate(-0 305.141172 224.076094)\">review arrivals per time unit</text>\n",
       "    </g>\n",
       "   </g>\n",
       "   <g id=\"matplotlib.axis_2\">\n",
       "    <g id=\"ytick_1\">\n",
       "     <g id=\"line2d_8\">\n",
       "      <path d=\"M 45.082344 190.566547  L 565.2 190.566547  \" clip-path=\"url(#p08843ec460)\" style=\"fill: none; stroke: #d4d8da; stroke-width: 0.6; stroke-linecap: square\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_9\">\n",
       "      <defs>\n",
       "       <path id=\"m54d35708b7\" d=\"M 0 0  L -3.5 0  \" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </defs>\n",
       "      <g>\n",
       "       <use xlink:href=\"#m54d35708b7\" x=\"45.082344\" y=\"190.566547\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_9\">\n",
       "      <text style=\"font-size: 10px; font-family: 'DejaVu Sans'; text-anchor: end\" x=\"38.082344\" y=\"194.365375\" transform=\"rotate(-0 38.082344 194.365375)\">0</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_2\">\n",
       "     <g id=\"line2d_10\">\n",
       "      <path d=\"M 45.082344 153.266982  L 565.2 153.266982  \" clip-path=\"url(#p08843ec460)\" style=\"fill: none; stroke: #d4d8da; stroke-width: 0.6; stroke-linecap: square\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_11\">\n",
       "      <g>\n",
       "       <use xlink:href=\"#m54d35708b7\" x=\"45.082344\" y=\"153.266982\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_10\">\n",
       "      <text style=\"font-size: 10px; font-family: 'DejaVu Sans'; text-anchor: end\" x=\"38.082344\" y=\"157.06581\" transform=\"rotate(-0 38.082344 157.06581)\">10</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_3\">\n",
       "     <g id=\"line2d_12\">\n",
       "      <path d=\"M 45.082344 115.967416  L 565.2 115.967416  \" clip-path=\"url(#p08843ec460)\" style=\"fill: none; stroke: #d4d8da; stroke-width: 0.6; stroke-linecap: square\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_13\">\n",
       "      <g>\n",
       "       <use xlink:href=\"#m54d35708b7\" x=\"45.082344\" y=\"115.967416\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_11\">\n",
       "      <text style=\"font-size: 10px; font-family: 'DejaVu Sans'; text-anchor: end\" x=\"38.082344\" y=\"119.766244\" transform=\"rotate(-0 38.082344 119.766244)\">20</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_4\">\n",
       "     <g id=\"line2d_14\">\n",
       "      <path d=\"M 45.082344 78.667851  L 565.2 78.667851  \" clip-path=\"url(#p08843ec460)\" style=\"fill: none; stroke: #d4d8da; stroke-width: 0.6; stroke-linecap: square\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_15\">\n",
       "      <g>\n",
       "       <use xlink:href=\"#m54d35708b7\" x=\"45.082344\" y=\"78.667851\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_12\">\n",
       "      <text style=\"font-size: 10px; font-family: 'DejaVu Sans'; text-anchor: end\" x=\"38.082344\" y=\"82.466679\" transform=\"rotate(-0 38.082344 82.466679)\">30</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"text_13\">\n",
       "     <text style=\"font-size: 10px; font-family: 'DejaVu Sans'; text-anchor: middle\" x=\"18.955\" y=\"127.778945\" transform=\"rotate(-90 18.955 127.778945)\">mean review time</text>\n",
       "    </g>\n",
       "   </g>\n",
       "   <g id=\"line2d_16\">\n",
       "    <path d=\"M 68.724055 189.323228  L 73.500159 189.310669  L 78.276262 189.297854  L 83.052365 189.284775  L 87.828469 189.271423  L 92.604572 189.25779  L 97.380676 189.243867  L 102.156779 189.229645  L 106.932882 189.215113  L 111.708986 189.200262  L 116.485089 189.185081  L 121.261192 189.169559  L 126.037296 189.153685  L 130.813399 189.137445  L 135.589502 189.120827  L 140.365606 189.103819  L 145.141709 189.086405  L 149.917813 189.068572  L 154.693916 189.050304  L 159.470019 189.031585  L 164.246123 189.012398  L 169.022226 188.992726  L 173.798329 188.972548  L 178.574433 188.951847  L 183.350536 188.930601  L 188.126639 188.908788  L 192.902743 188.886386  L 197.678846 188.86337  L 202.45495 188.839715  L 207.231053 188.815394  L 212.007156 188.790377  L 216.78326 188.764635  L 221.559363 188.738137  L 226.335466 188.710847  L 231.11157 188.68273  L 235.887673 188.653749  L 240.663776 188.623861  L 245.43988 188.593025  L 250.215983 188.561194  L 254.992087 188.528319  L 259.76819 188.494349  L 264.544293 188.459227  L 269.320397 188.422894  L 274.0965 188.385286  L 278.872603 188.346335  L 283.648707 188.305967  L 288.42481 188.264105  L 293.200913 188.220662  L 297.977017 188.175549  L 302.75312 188.128667  L 307.529224 188.079909  L 312.305327 188.029161  L 317.08143 187.976299  L 321.857534 187.921188  L 326.633637 187.86368  L 331.40974 187.803616  L 336.185844 187.740822  L 340.961947 187.675108  L 345.73805 187.606264  L 350.514154 187.534062  L 355.290257 187.45825  L 360.066361 187.37855  L 364.842464 187.294655  L 369.618567 187.206226  L 374.394671 187.112883  L 379.170774 187.014207  L 383.946877 186.909727  L 388.722981 186.798914  L 393.499084 186.681175  L 398.275187 186.555841  L 403.051291 186.422151  L 407.827394 186.27924  L 412.603498 186.126122  L 417.379601 185.961662  L 422.155704 185.784551  L 426.931808 185.593271  L 431.707911 185.386052  L 436.484014 185.160813  L 441.260118 184.915098  L 446.036221 184.645981  L 450.812324 184.349953  L 455.588428 184.022763  L 460.364531 183.65922  L 465.140635 183.252907  L 469.916738 182.795804  L 474.692841 182.277755  L 479.468945 181.685698  L 484.245048 181.002556  L 489.021151 180.205556  L 493.797255 179.263648  L 498.573358 178.133358  L 503.349461 176.751893  L 508.125565 175.025061  L 512.901668 172.804849  L 517.677772 169.844566  L 522.453875 165.70017  L 527.229978 159.483576  L 532.006082 149.122585  L 536.782185 128.400605  L 541.558288 66.234663  \" clip-path=\"url(#p08843ec460)\" style=\"fill: none; stroke: #31586b; stroke-width: 1.8; stroke-linecap: square\"/>\n",
       "   </g>\n",
       "   <g id=\"patch_3\">\n",
       "    <path d=\"M 45.082344 195.477656  L 45.082344 60.080234  \" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n",
       "   </g>\n",
       "   <g id=\"patch_4\">\n",
       "    <path d=\"M 45.082344 195.477656  L 565.2 195.477656  \" style=\"fill: none; stroke: #000000; stroke-width: 0.8; stroke-linejoin: miter; stroke-linecap: square\"/>\n",
       "   </g>\n",
       "   <g id=\"text_14\">\n",
       "    <text style=\"font-size: 11px; font-family: 'DejaVu Sans'; text-anchor: start\" x=\"45.082344\" y=\"54.080234\" transform=\"rotate(-0 45.082344 54.080234)\">review queue sensitivity</text>\n",
       "   </g>\n",
       "  </g>\n",
       "  <g id=\"text_15\">\n",
       "   <text style=\"font-size: 12px; font-family: 'DejaVu Sans'; text-anchor: start\" x=\"46.08\" y=\"13.870125\" transform=\"rotate(-0 46.08 13.870125)\">Chapter 27: delegation queue</text>\n",
       "  </g>\n",
       " </g>\n",
       " <defs>\n",
       "  <clipPath id=\"p08843ec460\">\n",
       "   <rect x=\"45.082344\" y=\"60.080234\" width=\"520.117656\" height=\"135.397422\"/>\n",
       "  </clipPath>\n",
       " </defs>\n",
       "</svg>"
      ],
      "text/plain": [
       "<IPython.core.display.SVG object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "changed_inputs = {'arrival_rate': 4,\n",
    " 'service_rate': 3,\n",
    " 'delegation_fraction': 0.9,\n",
    " 'agent_risk_limit': 0.1,\n",
    " 'tasks': [{'name': 'routine',\n",
    "            'agent_authorized': True,\n",
    "            'risk': 0.02,\n",
    "            'deadline': 2,\n",
    "            'human_authorized': False,\n",
    "            'review_received': False},\n",
    "           {'name': 'release',\n",
    "            'agent_authorized': False,\n",
    "            'risk': 0.05,\n",
    "            'deadline': 2,\n",
    "            'human_authorized': True,\n",
    "            'review_received': True},\n",
    "           {'name': 'sensitive',\n",
    "            'agent_authorized': False,\n",
    "            'risk': 0.2,\n",
    "            'deadline': 2,\n",
    "            'human_authorized': True,\n",
    "            'review_received': False}]}\n",
    "changed = analyze(chapter, changed_inputs)\n",
    "changed['evidence_kind'] = 'constructed changed-assumption example'\n",
    "print(report_text(changed))\n",
    "display(SVG(figure_svg(changed)))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "58f1d19ca50c",
   "metadata": {},
   "source": [
    "**Figure 27.L2:** The changed-assumption result. Compare the printed quantities and the stated assumptions with the first run. A different input need not imply a causal effect in a deployed agent."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "77ecb25c8a57",
   "metadata": {},
   "source": [
    "## Try a new case\n",
    "\n",
    "The transfer review load is 1 with service rate 2, so mean sojourn is 1. The task deadline is 0.5, below that planning mean. Despite human authority and a declared receipt, the simplified capacity contract does not approve release. This flags the planning mismatch rather than asserting the actual receipt arrived after 0.5.\n",
    "\n",
    "For local use, draft a handoff packet containing current state, missing authority, risk, observations, proposed action, and deadline. Do not send it automatically from this computation. Measure real arrival and service patterns before adopting the queue law, and preserve review timestamps when evaluating individual deadline outcomes."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "14c4457b588e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Chapter 27: delegation-queue\n",
      "Can human review supply missing authority within the workflow's capacity and timing contract?\n",
      "Evidence: constructed transfer example\n",
      "\n",
      "Calculated quantities:\n",
      "{\n",
      "  \"effective_review_arrival_rate\": 1.0,\n",
      "  \"queue_stable\": true,\n",
      "  \"mean_review_sojourn\": 1.0,\n",
      "  \"tasks\": [\n",
      "    {\n",
      "      \"task\": \"approval\",\n",
      "      \"delegation_required\": true,\n",
      "      \"queue_mean_exceeds_deadline\": true,\n",
      "      \"released\": false,\n",
      "      \"review_packet_required\": false\n",
      "    }\n",
      "  ],\n",
      "  \"released_count\": 0\n",
      "}\n",
      "\n",
      "Interpretation:\n",
      "Delegation moves work into a limited review service. Missing authority remains a release blocker even when expected review capacity is adequate.\n",
      "\n",
      "Assumptions:\n",
      "- M/M/1 review queue with Poisson arrivals, exponential service, one reviewer.\n",
      "- Arrival and service rates use the same time unit.\n",
      "- Deadline test uses mean queue sojourn as a diagnostic, not a per-task prediction.\n",
      "\n",
      "Limitations:\n",
      "- A queue mean does not prove any individual deadline will be met.\n",
      "- A human review receipt grants only the supplied current authority.\n",
      "\n",
      "Execution: completed locally; constructed inputs are not deployment measurements.\n"
     ]
    }
   ],
   "source": [
    "transfer_inputs = {'arrival_rate': 1,\n",
    " 'service_rate': 2,\n",
    " 'delegation_fraction': 1,\n",
    " 'agent_risk_limit': 0.05,\n",
    " 'tasks': [{'name': 'approval',\n",
    "            'agent_authorized': False,\n",
    "            'risk': 0.01,\n",
    "            'deadline': 0.5,\n",
    "            'human_authorized': True,\n",
    "            'review_received': True}]}\n",
    "transfer = analyze(chapter, transfer_inputs)\n",
    "transfer['evidence_kind'] = 'constructed transfer example'\n",
    "print(report_text(transfer))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "450c4d5a1255",
   "metadata": {},
   "source": [
    "## Apply the method to your inputs\n",
    "\n",
    "The example file below has the exact input shape the method accepts. Copy it to a new file, replace its values, then point `reader_file` at your copy. Run the cell again. Supplied inputs retain their stated provenance; the program cannot establish that they are representative observations.\n",
    "\n",
    "- **arrival_rate:** Nonnegative incoming task rate per declared time unit.\n",
    "- **service_rate:** Positive reviewer service rate in the same unit.\n",
    "- **delegation_fraction:** Fraction in [0,1] entering review.\n",
    "- **agent_risk_limit:** Probability threshold in [0,1] for autonomous task handling.\n",
    "- **tasks:** Rows with name,agent_authorized:boolean,risk:probability,deadline:nonnegative time,human_authorized:boolean,review_received:boolean."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "62cfb762a27a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Chapter 27: delegation-queue\n",
      "Can human review supply missing authority within the workflow's capacity and timing contract?\n",
      "Evidence: supplied local inputs; provenance not independently verified\n",
      "\n",
      "Calculated quantities:\n",
      "{\n",
      "  \"effective_review_arrival_rate\": 1.0,\n",
      "  \"queue_stable\": true,\n",
      "  \"mean_review_sojourn\": 1.0,\n",
      "  \"tasks\": [\n",
      "    {\n",
      "      \"task\": \"approval\",\n",
      "      \"delegation_required\": true,\n",
      "      \"queue_mean_exceeds_deadline\": true,\n",
      "      \"released\": false,\n",
      "      \"review_packet_required\": false\n",
      "    }\n",
      "  ],\n",
      "  \"released_count\": 0\n",
      "}\n",
      "\n",
      "Interpretation:\n",
      "Delegation moves work into a limited review service. Missing authority remains a release blocker even when expected review capacity is adequate.\n",
      "\n",
      "Assumptions:\n",
      "- M/M/1 review queue with Poisson arrivals, exponential service, one reviewer.\n",
      "- Arrival and service rates use the same time unit.\n",
      "- Deadline test uses mean queue sojourn as a diagnostic, not a per-task prediction.\n",
      "\n",
      "Limitations:\n",
      "- A queue mean does not prove any individual deadline will be met.\n",
      "- A human review receipt grants only the supplied current authority.\n",
      "\n",
      "Execution: completed locally; constructed inputs are not deployment measurements.\n"
     ]
    }
   ],
   "source": [
    "reader_file = LAB_ROOT / 'data/examples/ch27.json'\n",
    "reader_inputs = json.loads(reader_file.read_text())\n",
    "reader_report = analyze(chapter, reader_inputs)\n",
    "print(report_text(reader_report))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4a97a3724959",
   "metadata": {},
   "source": [
    "## Questions\n",
    "\n",
    "1. Compute default review mean.\n",
    "\n",
    "2. Why is changed stationary mean unavailable?\n",
    "\n",
    "3. Does mean 1 prove the transfer receipt arrived after 0.5?\n",
    "\n",
    "Answers: [separate solutions](../solutions/ch27.md). Try the calculation before opening them."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "189425695847",
   "metadata": {},
   "source": [
    "## Summary\n",
    "\n",
    "Delegation transfers work to a bounded human service. The notebook calculates conditional queue stability and mean time, then keeps authority and review receipts explicit in a task contract. More delegation can overload review even when it is individually sensible. The changed case exposes saturation; the transfer exposes a deadline-planning mismatch. Queue means support capacity design, while actual deadline compliance requires timestamps and permission requires a valid authority source.\n",
    "\n",
    "Limits of this experiment:\n",
    "\n",
    "- This is a local calculation under declared inputs, not an empirical claim about a deployed agent.\n",
    "- Read the returned assumptions and limitations before applying the numerical result.\n",
    "\n",
    "The assistant skill is [`maa-27-delegation-queue`](../skills/maa-27-delegation-queue/SKILL.md). It uses this notebook's tested computation and input contract."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9cc9f25ba898",
   "metadata": {},
   "source": [
    "## Equations from the chapter\n",
    "\n",
    "These are the unchanged display equations and their explanations from the canonical chapter. They are a reference for the experiment, not a claim that every equation is numerically implemented by this one method."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fa3e290d66e0",
   "metadata": {},
   "source": [
    "### Equation 27.1\n",
    "\n",
    "![Equation 27.1](../assets/math/de77306687ec0648e0c4.svg)\n",
    "\n",
    "Defines current authority as actions permitted by declared grants and satisfied state-specific preconditions.\n",
    "\n",
    "A high-scoring action absent from this set remains a recommendation, not an executable act.\n",
    "\n",
    "LaTeX source, preserved for inspection:\n",
    "\n",
    "```latex\n",
    "\\mathcal A_{\\mathrm{auth}}(x)\n",
    "=\\{a\\in\\mathcal A:\\operatorname{Authorized}_{\\mathcal G}(a,x)=1,\n",
    "\\;\\operatorname{Pre}(a,x)=1\\}.\n",
    "\\tag{27.1}\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0b27267a4434",
   "metadata": {},
   "source": [
    "### Equation 27.2\n",
    "\n",
    "![Equation 27.2](../assets/math/7975917fee2d7cc96458.svg)\n",
    "\n",
    "Routes a zero-one-loss prediction to the classifier or named expert by comparing their conditional chances of correctness.\n",
    "\n",
    "Deferral begins where expert correctness matches or exceeds the classifier's best class probability, not where confidence merely feels low.\n",
    "\n",
    "LaTeX source, preserved for inspection:\n",
    "\n",
    "```latex\n",
    "\\operatorname{route}^{\\star}(x)=\n",
    "\\begin{cases}\n",
    "\\operatorname{Del}(E,\\Delta), & p_E(x)\\geq \\max_y p(y\\mid x),\\\\\n",
    "\\arg\\max_y p(y\\mid x), & p_E(x)<\\max_y p(y\\mid x).\n",
    "\\end{cases}\n",
    "\\tag{27.2}\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4de5a990195e",
   "metadata": {},
   "source": [
    "### Equation 27.3\n",
    "\n",
    "![Equation 27.3](../assets/math/b6ceb29c5e478c1354a4.svg)\n",
    "\n",
    "Defines candidate routes and selects an authorized act, named delegation, wait, narrow, or return route by declared route value.\n",
    "\n",
    "Every maximand is one route `\\varrho`, so its value and selected output share one coherent domain.\n",
    "\n",
    "LaTeX source, preserved for inspection:\n",
    "\n",
    "```latex\n",
    "\\mathcal R_{\\mathrm{auth}}(x)=\n",
    "\\{(\\mathrm{act},a),(\\mathrm{delegate},d,\\Delta),(\\mathrm{wait},o,\\Delta),(\\mathrm{narrow},a'),(\\mathrm{return})\n",
    "\\ \\text{that are authorized at }x\\},\n",
    "\\qquad\n",
    "\\operatorname{choose}^{\\star}(x)\\in\n",
    "\\arg\\max_{\\varrho\\in\\mathcal R_{\\mathrm{auth}}(x)}V(\\varrho,x).\n",
    "\\tag{27.3}\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a868d015d102",
   "metadata": {},
   "source": [
    "### Equation 27.4\n",
    "\n",
    "![Equation 27.4](../assets/math/f09c52733cdad330374e.svg)\n",
    "\n",
    "States a stopping discipline: wait only for a named useful observation, bounded delay, and fallback authorized in every reachable deadline state.\n",
    "\n",
    "Authority must survive every reachable post-wait state; otherwise delay can leave no permitted next action.\n",
    "\n",
    "LaTeX source, preserved for inspection:\n",
    "\n",
    "```latex\n",
    "\\operatorname{wait}^{\\star}(o,\\Delta,x)\n",
    "\\quad\\text{only if}\\quad\n",
    "\\operatorname{VOI}(o)>\n",
    "\\operatorname{DelayCost}(\\Delta,x)\n",
    "\\quad\\text{and}\\quad\n",
    "\\operatorname{fallback}(\\Delta,x_\\Delta)\\in\\mathcal A_{\\mathrm{auth}}(x_\\Delta)\n",
    "\\ \\text{for every reachable }x_\\Delta.\n",
    "\\tag{27.4}\n",
    "```"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Mathematics of AI Agents",
   "language": "python",
   "name": "maa-lab"
  },
  "lab_chapter": 27,
  "lab_execution": {
   "code_cells": 6,
   "created_utc": "2026-10-02T05:17:00.012371+00:00",
   "elapsed_seconds": 8.834935207967646,
   "method": "fresh process; new ipykernel InProcessKernelManager; cells submitted as Jupyter execute requests",
   "network_transport_tested": false,
   "python": "3.11.15",
   "source_sha256": "e1490581af993610c50c89ef246dc0465ece979eccdc83dcdd722527722f89a9"
  },
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
