{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "72a873924b76",
   "metadata": {},
   "source": [
    "# Chapter 15: What an Agent Should Remember: Retrieval, Compression, and Experience\n",
    "\n",
    "A controller remembers an earlier approval. The text is relevant, concise, and easy to retrieve. It can still be the wrong memory to use if the approval belongs to an older document version. Memory quality therefore includes authority and freshness, not only similarity or token efficiency.\n",
    "\n",
    "This notebook solves a small declared retrieval allocation. Records consume tokens and contribute additive decision value, but stale records and invalid version-bound authority are excluded first. A separate compression diagnostic checks whether the preferred action survives. The example intentionally gives an obsolete approval a very attractive value, making it impossible to confuse a high relevance score with current permission.\n",
    "\n",
    "**Outcome:** Choose fresh valid records and inspect action preservation under compression.\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": "b383d4e2ae2e",
   "metadata": {},
   "source": [
    "## The question and its mathematics\n",
    "\n",
    "For record i, let t_i be token cost, v_i declared decision value, and x_i in{0,1} retrieval status. The finite allocation maximizes sum_i v_i x_i subject to sum_i t_i x_i<=B. Freshness requires now-timestamp_i<=ttl_i. An authority-bearing record also requires its version to equal the current version.\n",
    "\n",
    "The code uses exact zero-one knapsack dynamic programming over integer budgets. Descending budget updates prevent the same record from being selected repeatedly. Its frontier gives the greatest eligible additive value at every budget from zero to B. These values are supplied judgments, not embedding similarity or empirically learned information gain.\n",
    "\n",
    "For compression, the implementation compares argmax_a Q_original(a) with argmax_a Q_compressed(a). Equal choices mean this particular decision is preserved. They do not imply the compressed memory retains every fact or supports every future task. Ties follow list order, so a genuine decision-preservation claim should also inspect whether a meaningful action gap remains."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fc0ecf647d2a",
   "metadata": {},
   "source": [
    "## A calculation you can run\n",
    "\n",
    "Supply records with token counts, value, timestamps, time-to-live, authority flags, and version identifiers. The function checks numeric domains and rejects a timestamp later than now. It constructs freshness and authority validity before allocating budget. Invalid records remain visible in the returned table rather than disappearing without explanation.\n",
    "\n",
    "The plot shows the eligible retrieval frontier against token budget. The metrics identify selected and excluded records, used tokens, and compression choices. In the changed case reduce the budget and alter the compressed action values. Predict which record remains affordable and whether the action still matches. The two interventions address separate questions, so report both rather than attributing the entire change to memory quality.\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": "86212d08868b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Chapter 15: memory-budget\n",
      "Which memories deserve a limited retrieval budget, and does compression preserve the decision?\n",
      "Evidence: constructed teaching example\n",
      "\n",
      "Calculated quantities:\n",
      "{\n",
      "  \"retrieved_ids\": [\n",
      "    \"review-v2\",\n",
      "    \"summary\"\n",
      "  ],\n",
      "  \"retrieval_value\": 11.0,\n",
      "  \"tokens_used\": 6,\n",
      "  \"excluded_ids\": [\n",
      "    \"old-review\"\n",
      "  ],\n",
      "  \"compression_preserves_action\": true,\n",
      "  \"original_action\": 1,\n",
      "  \"compressed_action\": 1\n",
      "}\n",
      "\n",
      "Interpretation:\n",
      "Retrieval optimizes declared decision value among records that satisfy freshness and version-bound authority checks. Compression is checked by the action it preserves.\n",
      "\n",
      "Assumptions:\n",
      "- Record values are additive and supplied, not inferred from text similarity.\n",
      "- Exact finite subset search; no semantic embedding model.\n",
      "\n",
      "Limitations:\n",
      "- Preserving one decision vector does not prove sufficiency for every future task.\n",
      "- Retrieval relevance is not authority.\n",
      "\n",
      "Execution: completed locally; constructed inputs are not deployment measurements.\n"
     ]
    }
   ],
   "source": [
    "chapter = 15\n",
    "inputs = {'budget': 6,\n",
    " 'now': 10,\n",
    " 'current_version': 'v2',\n",
    " 'records': [{'id': 'review-v2',\n",
    "              'tokens': 4,\n",
    "              'decision_value': 8,\n",
    "              'timestamp': 9,\n",
    "              'ttl': 3,\n",
    "              'authority': True,\n",
    "              'version': 'v2'},\n",
    "             {'id': 'summary',\n",
    "              'tokens': 2,\n",
    "              'decision_value': 3,\n",
    "              'timestamp': 8,\n",
    "              'ttl': 5,\n",
    "              'authority': False,\n",
    "              'version': 'v1'},\n",
    "             {'id': 'old-review',\n",
    "              'tokens': 1,\n",
    "              'decision_value': 100,\n",
    "              'timestamp': 9,\n",
    "              'ttl': 3,\n",
    "              'authority': True,\n",
    "              'version': 'v1'}],\n",
    " 'original_action_values': [4, 6],\n",
    " 'compressed_action_values': [3, 5]}\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": "fd5520949108",
   "metadata": {},
   "source": [
    "At budget 6, review-v2 and summary fit exactly and contribute value 11. Old-review is excluded because its authority belongs to v1, despite its declared value 100 and low token cost. Its timestamp is fresh; freshness alone is therefore insufficient.\n",
    "\n",
    "Original action values [4,6] and compressed values [3,5] both prefer action 1. With changed budget 2, only summary is retrieved, giving value 3. Changed compressed values [6,5] prefer action 0, so compression no longer preserves the decision. That flag concerns this supplied value comparison; it does not infer which memory sentence caused the reversal.\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=\"md1935e0aed\" d=\"M 0 0  L 0 3.5  \" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </defs>\n",
       "      <g>\n",
       "       <use xlink:href=\"#md1935e0aed\" 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</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_2\">\n",
       "     <g id=\"line2d_2\">\n",
       "      <g>\n",
       "       <use xlink:href=\"#md1935e0aed\" x=\"147.529761\" 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=\"147.529761\" y=\"210.075312\" transform=\"rotate(-0 147.529761 210.075312)\">1</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_3\">\n",
       "     <g id=\"line2d_3\">\n",
       "      <g>\n",
       "       <use xlink:href=\"#md1935e0aed\" x=\"226.335466\" 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=\"226.335466\" y=\"210.075312\" transform=\"rotate(-0 226.335466 210.075312)\">2</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_4\">\n",
       "     <g id=\"line2d_4\">\n",
       "      <g>\n",
       "       <use xlink:href=\"#md1935e0aed\" x=\"305.141172\" 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=\"305.141172\" y=\"210.075312\" transform=\"rotate(-0 305.141172 210.075312)\">3</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_5\">\n",
       "     <g id=\"line2d_5\">\n",
       "      <g>\n",
       "       <use xlink:href=\"#md1935e0aed\" x=\"383.946877\" 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=\"383.946877\" y=\"210.075312\" transform=\"rotate(-0 383.946877 210.075312)\">4</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_6\">\n",
       "     <g id=\"line2d_6\">\n",
       "      <g>\n",
       "       <use xlink:href=\"#md1935e0aed\" x=\"462.752583\" 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=\"462.752583\" y=\"210.075312\" transform=\"rotate(-0 462.752583 210.075312)\">5</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_7\">\n",
       "     <g id=\"line2d_7\">\n",
       "      <g>\n",
       "       <use xlink:href=\"#md1935e0aed\" x=\"541.558288\" 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=\"541.558288\" y=\"210.075312\" transform=\"rotate(-0 541.558288 210.075312)\">6</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)\">token budget</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 189.323228  L 565.2 189.323228  \" clip-path=\"url(#p2d83a9adbd)\" style=\"fill: none; stroke: #d4d8da; stroke-width: 0.6; stroke-linecap: square\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_9\">\n",
       "      <defs>\n",
       "       <path id=\"m691ae1d5f6\" d=\"M 0 0  L -3.5 0  \" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </defs>\n",
       "      <g>\n",
       "       <use xlink:href=\"#m691ae1d5f6\" x=\"45.082344\" y=\"189.323228\" 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=\"193.122056\" transform=\"rotate(-0 38.082344 193.122056)\">0.0</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_2\">\n",
       "     <g id=\"line2d_10\">\n",
       "      <path d=\"M 45.082344 161.348554  L 565.2 161.348554  \" clip-path=\"url(#p2d83a9adbd)\" 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=\"#m691ae1d5f6\" x=\"45.082344\" y=\"161.348554\" 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=\"165.147382\" transform=\"rotate(-0 38.082344 165.147382)\">2.5</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_3\">\n",
       "     <g id=\"line2d_12\">\n",
       "      <path d=\"M 45.082344 133.37388  L 565.2 133.37388  \" clip-path=\"url(#p2d83a9adbd)\" 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=\"#m691ae1d5f6\" x=\"45.082344\" y=\"133.37388\" 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=\"137.172708\" transform=\"rotate(-0 38.082344 137.172708)\">5.0</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_4\">\n",
       "     <g id=\"line2d_14\">\n",
       "      <path d=\"M 45.082344 105.399206  L 565.2 105.399206  \" clip-path=\"url(#p2d83a9adbd)\" 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=\"#m691ae1d5f6\" x=\"45.082344\" y=\"105.399206\" 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=\"109.198034\" transform=\"rotate(-0 38.082344 109.198034)\">7.5</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_5\">\n",
       "     <g id=\"line2d_16\">\n",
       "      <path d=\"M 45.082344 77.424532  L 565.2 77.424532  \" clip-path=\"url(#p2d83a9adbd)\" style=\"fill: none; stroke: #d4d8da; stroke-width: 0.6; stroke-linecap: square\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_17\">\n",
       "      <g>\n",
       "       <use xlink:href=\"#m691ae1d5f6\" x=\"45.082344\" y=\"77.424532\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_13\">\n",
       "      <text style=\"font-size: 10px; font-family: 'DejaVu Sans'; text-anchor: end\" x=\"38.082344\" y=\"81.22336\" transform=\"rotate(-0 38.082344 81.22336)\">10.0</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"text_14\">\n",
       "     <text style=\"font-size: 10px; font-family: 'DejaVu Sans'; text-anchor: middle\" x=\"9.414375\" y=\"127.778945\" transform=\"rotate(-90 9.414375 127.778945)\">declared decision value</text>\n",
       "    </g>\n",
       "   </g>\n",
       "   <g id=\"line2d_18\">\n",
       "    <path d=\"M 68.724055 189.323228  L 147.529761 189.323228  L 226.335466 155.753619  L 305.141172 155.753619  L 383.946877 99.804271  L 462.752583 99.804271  L 541.558288 66.234663  \" clip-path=\"url(#p2d83a9adbd)\" style=\"fill: none; stroke: #31586b; stroke-width: 1.8; stroke-linecap: square\"/>\n",
       "    <defs>\n",
       "     <path id=\"m0c0f3b2cdf\" d=\"M 0 2  C 0.530406 2 1.03916 1.789267 1.414214 1.414214  C 1.789267 1.03916 2 0.530406 2 0  C 2 -0.530406 1.789267 -1.03916 1.414214 -1.414214  C 1.03916 -1.789267 0.530406 -2 0 -2  C -0.530406 -2 -1.03916 -1.789267 -1.414214 -1.414214  C -1.789267 -1.03916 -2 -0.530406 -2 0  C -2 0.530406 -1.789267 1.03916 -1.414214 1.414214  C -1.03916 1.789267 -0.530406 2 0 2  z \" style=\"stroke: #31586b\"/>\n",
       "    </defs>\n",
       "    <g clip-path=\"url(#p2d83a9adbd)\">\n",
       "     <use xlink:href=\"#m0c0f3b2cdf\" x=\"68.724055\" y=\"189.323228\" style=\"fill: #31586b; stroke: #31586b\"/>\n",
       "     <use xlink:href=\"#m0c0f3b2cdf\" x=\"147.529761\" y=\"189.323228\" style=\"fill: #31586b; stroke: #31586b\"/>\n",
       "     <use xlink:href=\"#m0c0f3b2cdf\" x=\"226.335466\" y=\"155.753619\" style=\"fill: #31586b; stroke: #31586b\"/>\n",
       "     <use xlink:href=\"#m0c0f3b2cdf\" x=\"305.141172\" y=\"155.753619\" style=\"fill: #31586b; stroke: #31586b\"/>\n",
       "     <use xlink:href=\"#m0c0f3b2cdf\" x=\"383.946877\" y=\"99.804271\" style=\"fill: #31586b; stroke: #31586b\"/>\n",
       "     <use xlink:href=\"#m0c0f3b2cdf\" x=\"462.752583\" y=\"99.804271\" style=\"fill: #31586b; stroke: #31586b\"/>\n",
       "     <use xlink:href=\"#m0c0f3b2cdf\" x=\"541.558288\" y=\"66.234663\" style=\"fill: #31586b; stroke: #31586b\"/>\n",
       "    </g>\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_15\">\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)\">retrieval budget frontier</text>\n",
       "   </g>\n",
       "  </g>\n",
       "  <g id=\"text_16\">\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 15: memory budget</text>\n",
       "  </g>\n",
       " </g>\n",
       " <defs>\n",
       "  <clipPath id=\"p2d83a9adbd\">\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": "e3699acde71c",
   "metadata": {},
   "source": [
    "**Figure 15.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": "7dc80316f32d",
   "metadata": {},
   "source": [
    "## Change the assumption\n",
    "\n",
    "Additive record values can overcount redundant memories. Two records that repeat the same fact may contribute less together than the sum of their isolated values. This implementation declares additivity rather than inventing a submodular or semantic model. If redundancy matters, provide a different valuation experiment.\n",
    "\n",
    "The authority boundary is equally important. A remembered instruction does not become authorized because it is recent, highly ranked, or compressed into a confident summary. Version checks preserve a limited current-state contract; broader permissions still need a live authority check.\n",
    "\n",
    "Compression can also preserve today's action while destroying tomorrow's distinction. The argmax diagnostic is task-relative. Test novel decisions and changed state before claiming sufficient memory. Keep excluded-record reasons in the report so token scarcity is not mistaken for intentional retention of unsafe evidence."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "1bd9328523b6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Chapter 15: memory-budget\n",
      "Which memories deserve a limited retrieval budget, and does compression preserve the decision?\n",
      "Evidence: constructed changed-assumption example\n",
      "\n",
      "Calculated quantities:\n",
      "{\n",
      "  \"retrieved_ids\": [\n",
      "    \"summary\"\n",
      "  ],\n",
      "  \"retrieval_value\": 3.0,\n",
      "  \"tokens_used\": 2,\n",
      "  \"excluded_ids\": [\n",
      "    \"old-review\"\n",
      "  ],\n",
      "  \"compression_preserves_action\": false,\n",
      "  \"original_action\": 1,\n",
      "  \"compressed_action\": 0\n",
      "}\n",
      "\n",
      "Interpretation:\n",
      "Retrieval optimizes declared decision value among records that satisfy freshness and version-bound authority checks. Compression is checked by the action it preserves.\n",
      "\n",
      "Assumptions:\n",
      "- Record values are additive and supplied, not inferred from text similarity.\n",
      "- Exact finite subset search; no semantic embedding model.\n",
      "\n",
      "Limitations:\n",
      "- Preserving one decision vector does not prove sufficiency for every future task.\n",
      "- Retrieval relevance is not 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 47.962344 195.477656  L 565.2 195.477656  L 565.2 60.080234  L 47.962344 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=\"mc25d1deb22\" d=\"M 0 0  L 0 3.5  \" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </defs>\n",
       "      <g>\n",
       "       <use xlink:href=\"#mc25d1deb22\" x=\"71.473146\" 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=\"71.473146\" y=\"210.075312\" transform=\"rotate(-0 71.473146 210.075312)\">0</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_2\">\n",
       "     <g id=\"line2d_2\">\n",
       "      <g>\n",
       "       <use xlink:href=\"#mc25d1deb22\" x=\"306.581172\" 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=\"306.581172\" y=\"210.075312\" transform=\"rotate(-0 306.581172 210.075312)\">1</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"xtick_3\">\n",
       "     <g id=\"line2d_3\">\n",
       "      <g>\n",
       "       <use xlink:href=\"#mc25d1deb22\" x=\"541.689197\" 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=\"541.689197\" y=\"210.075312\" transform=\"rotate(-0 541.689197 210.075312)\">2</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"text_4\">\n",
       "     <text style=\"font-size: 10px; font-family: 'DejaVu Sans'; text-anchor: middle\" x=\"306.581172\" y=\"224.076094\" transform=\"rotate(-0 306.581172 224.076094)\">token budget</text>\n",
       "    </g>\n",
       "   </g>\n",
       "   <g id=\"matplotlib.axis_2\">\n",
       "    <g id=\"ytick_1\">\n",
       "     <g id=\"line2d_4\">\n",
       "      <path d=\"M 47.962344 189.323228  L 565.2 189.323228  \" clip-path=\"url(#p268d1f10f5)\" style=\"fill: none; stroke: #d4d8da; stroke-width: 0.6; stroke-linecap: square\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_5\">\n",
       "      <defs>\n",
       "       <path id=\"m6be5f8486b\" d=\"M 0 0  L -3.5 0  \" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </defs>\n",
       "      <g>\n",
       "       <use xlink:href=\"#m6be5f8486b\" x=\"47.962344\" y=\"189.323228\" 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: end\" x=\"40.962344\" y=\"193.122056\" transform=\"rotate(-0 40.962344 193.122056)\">0</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_2\">\n",
       "     <g id=\"line2d_6\">\n",
       "      <path d=\"M 47.962344 148.293706  L 565.2 148.293706  \" clip-path=\"url(#p268d1f10f5)\" style=\"fill: none; stroke: #d4d8da; stroke-width: 0.6; stroke-linecap: square\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_7\">\n",
       "      <g>\n",
       "       <use xlink:href=\"#m6be5f8486b\" x=\"47.962344\" y=\"148.293706\" 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: end\" x=\"40.962344\" y=\"152.092534\" transform=\"rotate(-0 40.962344 152.092534)\">1</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_3\">\n",
       "     <g id=\"line2d_8\">\n",
       "      <path d=\"M 47.962344 107.264184  L 565.2 107.264184  \" clip-path=\"url(#p268d1f10f5)\" style=\"fill: none; stroke: #d4d8da; stroke-width: 0.6; stroke-linecap: square\"/>\n",
       "     </g>\n",
       "     <g id=\"line2d_9\">\n",
       "      <g>\n",
       "       <use xlink:href=\"#m6be5f8486b\" x=\"47.962344\" y=\"107.264184\" 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: end\" x=\"40.962344\" y=\"111.063013\" transform=\"rotate(-0 40.962344 111.063013)\">2</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"ytick_4\">\n",
       "     <g id=\"line2d_10\">\n",
       "      <path d=\"M 47.962344 66.234663  L 565.2 66.234663  \" clip-path=\"url(#p268d1f10f5)\" 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=\"#m6be5f8486b\" x=\"47.962344\" y=\"66.234663\" style=\"stroke: #000000; stroke-width: 0.8\"/>\n",
       "      </g>\n",
       "     </g>\n",
       "     <g id=\"text_8\">\n",
       "      <text style=\"font-size: 10px; font-family: 'DejaVu Sans'; text-anchor: end\" x=\"40.962344\" y=\"70.033491\" transform=\"rotate(-0 40.962344 70.033491)\">3</text>\n",
       "     </g>\n",
       "    </g>\n",
       "    <g id=\"text_9\">\n",
       "     <text style=\"font-size: 10px; font-family: 'DejaVu Sans'; text-anchor: middle\" x=\"28.1975\" y=\"127.778945\" transform=\"rotate(-90 28.1975 127.778945)\">declared decision value</text>\n",
       "    </g>\n",
       "   </g>\n",
       "   <g id=\"line2d_12\">\n",
       "    <path d=\"M 71.473146 189.323228  L 306.581172 189.323228  L 541.689197 66.234663  \" clip-path=\"url(#p268d1f10f5)\" style=\"fill: none; stroke: #31586b; stroke-width: 1.8; stroke-linecap: square\"/>\n",
       "    <defs>\n",
       "     <path id=\"m8443e4920f\" d=\"M 0 2  C 0.530406 2 1.03916 1.789267 1.414214 1.414214  C 1.789267 1.03916 2 0.530406 2 0  C 2 -0.530406 1.789267 -1.03916 1.414214 -1.414214  C 1.03916 -1.789267 0.530406 -2 0 -2  C -0.530406 -2 -1.03916 -1.789267 -1.414214 -1.414214  C -1.789267 -1.03916 -2 -0.530406 -2 0  C -2 0.530406 -1.789267 1.03916 -1.414214 1.414214  C -1.03916 1.789267 -0.530406 2 0 2  z \" style=\"stroke: #31586b\"/>\n",
       "    </defs>\n",
       "    <g clip-path=\"url(#p268d1f10f5)\">\n",
       "     <use xlink:href=\"#m8443e4920f\" x=\"71.473146\" y=\"189.323228\" style=\"fill: #31586b; stroke: #31586b\"/>\n",
       "     <use xlink:href=\"#m8443e4920f\" x=\"306.581172\" y=\"189.323228\" style=\"fill: #31586b; stroke: #31586b\"/>\n",
       "     <use xlink:href=\"#m8443e4920f\" x=\"541.689197\" y=\"66.234663\" style=\"fill: #31586b; stroke: #31586b\"/>\n",
       "    </g>\n",
       "   </g>\n",
       "   <g id=\"patch_3\">\n",
       "    <path d=\"M 47.962344 195.477656  L 47.962344 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 47.962344 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_10\">\n",
       "    <text style=\"font-size: 11px; font-family: 'DejaVu Sans'; text-anchor: start\" x=\"47.962344\" y=\"54.080234\" transform=\"rotate(-0 47.962344 54.080234)\">retrieval budget frontier</text>\n",
       "   </g>\n",
       "  </g>\n",
       "  <g id=\"text_11\">\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 15: memory budget</text>\n",
       "  </g>\n",
       " </g>\n",
       " <defs>\n",
       "  <clipPath id=\"p268d1f10f5\">\n",
       "   <rect x=\"47.962344\" y=\"60.080234\" width=\"517.237656\" height=\"135.397422\"/>\n",
       "  </clipPath>\n",
       " </defs>\n",
       "</svg>"
      ],
      "text/plain": [
       "<IPython.core.display.SVG object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "changed_inputs = {'budget': 2,\n",
    " 'now': 10,\n",
    " 'current_version': 'v2',\n",
    " 'records': [{'id': 'review-v2',\n",
    "              'tokens': 4,\n",
    "              'decision_value': 8,\n",
    "              'timestamp': 9,\n",
    "              'ttl': 3,\n",
    "              'authority': True,\n",
    "              'version': 'v2'},\n",
    "             {'id': 'summary',\n",
    "              'tokens': 2,\n",
    "              'decision_value': 3,\n",
    "              'timestamp': 8,\n",
    "              'ttl': 5,\n",
    "              'authority': False,\n",
    "              'version': 'v1'},\n",
    "             {'id': 'old-review',\n",
    "              'tokens': 1,\n",
    "              'decision_value': 100,\n",
    "              'timestamp': 9,\n",
    "              'ttl': 3,\n",
    "              'authority': True,\n",
    "              'version': 'v1'}],\n",
    " 'original_action_values': [4, 6],\n",
    " 'compressed_action_values': [6, 5]}\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": "ee8fda1f4718",
   "metadata": {},
   "source": [
    "**Figure 15.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": "97a5d9c17f6d",
   "metadata": {},
   "source": [
    "## Try a new case\n",
    "\n",
    "The transfer case excludes expired even though it is cheap and valuable. Current is fresh, version-valid, and consumes the full budget 3, so it is selected with value 4. Both supplied decision vectors prefer action 0, preserving this action under compression.\n",
    "\n",
    "For local use, attach timestamps and document versions to records when those affect authority. Give decision values an explicit source, such as a constructed study or measured task benefit. A retrieval recommendation should name records to retain, records to exclude, and the evidence needed to justify their values. Use separate trace analysis when memory is trying to promote untrusted data into control."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "feb4c2bdf9b0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Chapter 15: memory-budget\n",
      "Which memories deserve a limited retrieval budget, and does compression preserve the decision?\n",
      "Evidence: constructed transfer example\n",
      "\n",
      "Calculated quantities:\n",
      "{\n",
      "  \"retrieved_ids\": [\n",
      "    \"current\"\n",
      "  ],\n",
      "  \"retrieval_value\": 4.0,\n",
      "  \"tokens_used\": 3,\n",
      "  \"excluded_ids\": [\n",
      "    \"expired\"\n",
      "  ],\n",
      "  \"compression_preserves_action\": true,\n",
      "  \"original_action\": 0,\n",
      "  \"compressed_action\": 0\n",
      "}\n",
      "\n",
      "Interpretation:\n",
      "Retrieval optimizes declared decision value among records that satisfy freshness and version-bound authority checks. Compression is checked by the action it preserves.\n",
      "\n",
      "Assumptions:\n",
      "- Record values are additive and supplied, not inferred from text similarity.\n",
      "- Exact finite subset search; no semantic embedding model.\n",
      "\n",
      "Limitations:\n",
      "- Preserving one decision vector does not prove sufficiency for every future task.\n",
      "- Retrieval relevance is not authority.\n",
      "\n",
      "Execution: completed locally; constructed inputs are not deployment measurements.\n"
     ]
    }
   ],
   "source": [
    "transfer_inputs = {'budget': 3,\n",
    " 'now': 20,\n",
    " 'current_version': 'doc-C',\n",
    " 'records': [{'id': 'expired',\n",
    "              'tokens': 1,\n",
    "              'decision_value': 10,\n",
    "              'timestamp': 10,\n",
    "              'ttl': 2,\n",
    "              'authority': False,\n",
    "              'version': 'doc-C'},\n",
    "             {'id': 'current',\n",
    "              'tokens': 3,\n",
    "              'decision_value': 4,\n",
    "              'timestamp': 19,\n",
    "              'ttl': 3,\n",
    "              'authority': True,\n",
    "              'version': 'doc-C'}],\n",
    " 'original_action_values': [1, 0],\n",
    " 'compressed_action_values': [2, 1]}\n",
    "transfer = analyze(chapter, transfer_inputs)\n",
    "transfer['evidence_kind'] = 'constructed transfer example'\n",
    "print(report_text(transfer))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b6f027d12192",
   "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",
    "- **budget:** Nonnegative integer token budget up to 10000.\n",
    "- **now:** Nonnegative current time.\n",
    "- **current_version:** Current document or state version identifier.\n",
    "- **records:** At most 20 records. Each has id,tokens:positive integer,decision_value:nonnegative number,timestamp,ttl:nonnegative numbers,authority:boolean,version:string.\n",
    "- **original_action_values:** Finite values for each original decision alternative.\n",
    "- **compressed_action_values:** Aligned values after compression."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "1b68936bf048",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Chapter 15: memory-budget\n",
      "Which memories deserve a limited retrieval budget, and does compression preserve the decision?\n",
      "Evidence: supplied local inputs; provenance not independently verified\n",
      "\n",
      "Calculated quantities:\n",
      "{\n",
      "  \"retrieved_ids\": [\n",
      "    \"current\"\n",
      "  ],\n",
      "  \"retrieval_value\": 4.0,\n",
      "  \"tokens_used\": 3,\n",
      "  \"excluded_ids\": [\n",
      "    \"expired\"\n",
      "  ],\n",
      "  \"compression_preserves_action\": true,\n",
      "  \"original_action\": 0,\n",
      "  \"compressed_action\": 0\n",
      "}\n",
      "\n",
      "Interpretation:\n",
      "Retrieval optimizes declared decision value among records that satisfy freshness and version-bound authority checks. Compression is checked by the action it preserves.\n",
      "\n",
      "Assumptions:\n",
      "- Record values are additive and supplied, not inferred from text similarity.\n",
      "- Exact finite subset search; no semantic embedding model.\n",
      "\n",
      "Limitations:\n",
      "- Preserving one decision vector does not prove sufficiency for every future task.\n",
      "- Retrieval relevance is not authority.\n",
      "\n",
      "Execution: completed locally; constructed inputs are not deployment measurements.\n"
     ]
    }
   ],
   "source": [
    "reader_file = LAB_ROOT / 'data/examples/ch15.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": "793db50da629",
   "metadata": {},
   "source": [
    "## Questions\n",
    "\n",
    "1. Why exclude old-review?\n",
    "\n",
    "2. What is default retrieval value?\n",
    "\n",
    "3. Does action preservation prove all future information survives?\n",
    "\n",
    "Answers: [separate solutions](../solutions/ch15.md). Try the calculation before opening them."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4a939bdb7eb7",
   "metadata": {},
   "source": [
    "## Summary\n",
    "\n",
    "Memory selection is a decision under cost, freshness, and authority. Exact knapsack allocation chooses among eligible records; action comparison checks a limited compression property. The obsolete approval is excluded despite its high value, the changed budget reduces retrieval, and changed compression reverses the action. These calculations do not perform semantic retrieval or establish universal sufficiency. Preserve the valuation and version contract whenever memory influences an action.\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-15-memory-budget`](../skills/maa-15-memory-budget/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": "c5c902c8502c",
   "metadata": {},
   "source": [
    "### Equation 15.1\n",
    "\n",
    "![Equation 15.1](../assets/math/f7d464531063a86995f9.svg)\n",
    "\n",
    "Equation (15.1) selects records by expected decision value under a stated context budget.\n",
    "\n",
    "Include records only when expected decision improvement exceeds the decision-value-priced token cost within budget.\n",
    "\n",
    "LaTeX source, preserved for inspection:\n",
    "\n",
    "```latex\n",
    "R^\\star\\in\\arg\\max_{R:\\,\\sum_{m\\in R}c(m)\\le B}\n",
    "\\left\\{\\operatorname E[\\text{decision value}\\mid R]-\\lambda\\sum_{m\\in R}c(m)\\right\\}.\n",
    "\\tag{15.1}\n",
    "```"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Mathematics of AI Agents",
   "language": "python",
   "name": "maa-lab"
  },
  "lab_chapter": 15,
  "lab_execution": {
   "code_cells": 6,
   "created_utc": "2026-10-02T05:15:09.957858+00:00",
   "elapsed_seconds": 8.917202083976008,
   "method": "fresh process; new ipykernel InProcessKernelManager; cells submitted as Jupyter execute requests",
   "network_transport_tested": false,
   "python": "3.11.15",
   "source_sha256": "1fef17b53eeb38382eab888047ae3be60894bd9e930c97dfd839f2b19316fdd0"
  },
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
