Skip to content
context Artifact [49ccbeedec]
Login

Artifact [49ccbeedec]

Artifact 49ccbeedece1148ca2c993d676b3b7baacb13c72f14dd509249cc886342ac0cc:


<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>ctx decisions</title>
<style>
:root {
  /* Layout: one column to 1080px; each section is a list of one-line rows with a three-way control; a summary bar stays at the foot */
  --bg:#f7f8f7; --surface:#eef1ef; --fg:#1a1e1c; --muted:#5d6863; --line:#d3dad6; --accent:#1f5f8a;
  --yes:#23724a; --no:#a8321f; --mid:#8a6410; --yes-bg:#dff1e6; --no-bg:#f8e1dc; --mid-bg:#f5ead0;
  --bad:#b42318; --spark:#6b7671; --rule:#9aa5a0;
  --font-body:"Instrument Sans",system-ui,-apple-system,"Segoe UI",sans-serif;
  --font-mono:"IBM Plex Mono",ui-monospace,Menlo,Consolas,monospace;
  color-scheme:light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg:#121514; --surface:#1a1e1c; --fg:#e7eae8; --muted:#98a39e; --line:#303734; --accent:#7fb6de;
    --yes:#73c797; --no:#f0907f; --mid:#dfb45a; --yes-bg:#173625; --no-bg:#40201a; --mid-bg:#3a2f14;
    --bad:#f97066; --spark:#a3ada8; --rule:#68736e;
    color-scheme:dark;
  }
}
:root[data-theme="dark"] {
  --bg:#121514; --surface:#1a1e1c; --fg:#e7eae8; --muted:#98a39e; --line:#303734; --accent:#7fb6de;
  --yes:#73c797; --no:#f0907f; --mid:#dfb45a; --yes-bg:#173625; --no-bg:#40201a; --mid-bg:#3a2f14;
  --bad:#f97066; --spark:#a3ada8; --rule:#68736e;
  color-scheme:dark;
}
* { box-sizing:border-box; }
[hidden] { display:none !important; }
html { scroll-padding-bottom:120px; }
body { margin:0; background:var(--bg); color:var(--fg); font-family:var(--font-body); line-height:1.45; font-size:15px; }
main { max-width:1080px; margin:0 auto; padding:28px 16px 150px; display:flex; flex-direction:column; gap:30px; }
.kicker { font-size:.78rem; letter-spacing:.06em; text-transform:uppercase; color:var(--muted); }
h1 { font-size:clamp(1.5rem,3.4vw,2.1rem); line-height:1.15; margin:6px 0 10px; text-wrap:balance; max-width:22em; }
h2 { font-size:1.2rem; margin:0; text-wrap:balance; }
h3 { font-size:.98rem; margin:0 0 4px; }
p { margin:0; } .lede { color:var(--muted); max-width:68ch; }
code, .mono { font-family:var(--font-mono); font-size:.86em; }
button { font:inherit; color:inherit; cursor:pointer; }
button:focus-visible, input:focus-visible, .row:focus-visible, a:focus-visible { outline:2px solid var(--accent); outline-offset:2px; }
a { color:var(--accent); }

.batches { display:flex; flex-wrap:wrap; gap:10px; align-items:center; margin-top:14px; }
.bt { border:1px solid var(--accent); background:var(--bg); color:var(--accent); border-radius:8px; padding:7px 14px; font-weight:600; }
.bt[aria-pressed="true"] { background:var(--accent); color:var(--bg); }
.bt small { font-weight:400; margin-left:6px; }
details.sub { margin-top:10px; }
details.sub > summary { cursor:pointer; color:var(--accent); font-size:.9rem; width:max-content; max-width:100%; }
details.sub > .list { margin-top:8px; }
details.fold { border-top:1px solid var(--line); padding-top:14px; }
details.fold > summary { cursor:pointer; list-style:none; display:flex; flex-wrap:wrap; gap:4px 12px; align-items:baseline; }
details.fold > summary::-webkit-details-marker { display:none; }
details.fold > summary::before { content:"\25B6"; color:var(--muted); font-size:.8rem; display:inline-block; width:1em; transition:transform .12s; align-self:center; }
details.fold[open] > summary::before { transform:rotate(90deg); }
details.fold > summary .count { color:var(--muted); font-size:.9rem; }
details.fold > .body { display:flex; flex-direction:column; gap:26px; margin-top:14px; }
summary:focus-visible { outline:2px solid var(--accent); outline-offset:2px; }
.sub-h { display:flex; flex-wrap:wrap; gap:4px 12px; align-items:baseline; margin-bottom:8px; }
.sub-h .count { color:var(--muted); font-size:.88rem; }

section.q { display:flex; flex-direction:column; gap:10px; }
.qhead { display:flex; flex-wrap:wrap; gap:8px 14px; align-items:baseline; }
.qhead .count { color:var(--muted); font-size:.9rem; }
.tools { display:flex; flex-wrap:wrap; gap:8px; align-items:center; }
.tools input { font:inherit; padding:5px 9px; border:1px solid var(--line); border-radius:6px; background:var(--bg); color:var(--fg); min-width:0; width:170px; }
.chip { border:1px solid var(--line); background:transparent; border-radius:999px; padding:3px 11px; font-size:.84rem; }
.chip[aria-pressed="true"] { background:var(--fg); color:var(--bg); border-color:var(--fg); }
.chip.act { border-style:dashed; }
.note { color:var(--muted); font-size:.88rem; max-width:80ch; }

.list { border:1px solid var(--line); border-radius:10px; overflow:hidden; }
.row { display:grid; grid-template-columns:minmax(0,1.5fr) minmax(0,1.1fr) auto; gap:6px 16px; align-items:center; padding:9px 12px; border-top:1px solid var(--line); }
.row:first-child { border-top:0; }
.row.cur { background:var(--surface); }
.row.done-no .main, .row.done-no .ev { opacity:.55; }
.main { min-width:0; display:flex; flex-direction:column; gap:1px; }
.title { font-weight:600; overflow-wrap:anywhere; }
.meta { color:var(--muted); font-size:.82rem; overflow-wrap:anywhere; }
.ev { min-width:0; font-size:.86rem; color:var(--muted); display:flex; flex-direction:column; gap:2px; }
.ev q { quotes:"\201C" "\201D"; color:var(--fg); }
.why { font-size:.8rem; }
.why b { font-weight:600; }
.dot { display:inline-block; width:7px; height:7px; border-radius:50%; background:var(--accent); margin-right:5px; vertical-align:middle; }
.seg { display:inline-flex; border:1px solid var(--line); border-radius:8px; overflow:hidden; background:var(--bg); }
.seg button { border:0; background:transparent; padding:5px 10px; font-size:.84rem; border-left:1px solid var(--line); white-space:nowrap; }
.seg button:first-child { border-left:0; }
.seg button[aria-pressed="true"].k0 { background:var(--yes-bg); color:var(--yes); font-weight:600; }
.seg button[aria-pressed="true"].k1 { background:var(--mid-bg); color:var(--mid); font-weight:600; }
.seg button[aria-pressed="true"].k2 { background:var(--no-bg); color:var(--no); font-weight:600; }
.seg button:disabled { opacity:.4; cursor:not-allowed; }
.seg kbd, .pick kbd { font-family:var(--font-mono); font-size:.72em; color:var(--muted); margin-right:4px; }
.row.line { grid-template-columns:minmax(0,1fr) auto; cursor:pointer; }
.row.line .meta { white-space:nowrap; overflow:hidden; text-overflow:ellipsis; }
.row.line.chosen .title { color:var(--muted); }
.row.line .meta.cov { white-space:normal; display:-webkit-box; -webkit-line-clamp:2; -webkit-box-orient:vertical; }
.row.line .meta.cov q { quotes:"\201C" "\201D"; color:var(--fg); }
.pick { justify-self:start; border:1px solid var(--line); background:var(--bg); border-radius:8px; padding:5px 12px; font-size:.84rem; white-space:nowrap; }
.pick[aria-pressed="true"] { background:var(--mid-bg); color:var(--mid); border-color:var(--mid); font-weight:600; }
.detail { grid-column:1 / -1; border-top:1px dashed var(--line); margin-top:4px; padding-top:8px; display:flex; flex-direction:column; gap:6px; font-size:.88rem; }
.detail blockquote { margin:0; padding:5px 9px; border-left:3px solid var(--line); font-family:var(--font-mono); font-size:.82rem; overflow-wrap:anywhere; background:var(--bg); }
.detail .where { color:var(--muted); font-size:.82rem; overflow-wrap:anywhere; }
.t-yes { color:var(--yes); font-weight:600; } .t-mid { color:var(--mid); font-weight:600; } .t-no { color:var(--no); font-weight:600; }
.nums { display:flex; flex-wrap:wrap; gap:2px 12px; font-variant-numeric:tabular-nums; }
.bars { display:inline-flex; align-items:flex-end; gap:2px; height:18px; vertical-align:middle; margin-right:6px; }
.bars i { display:block; width:5px; background:var(--muted); border-radius:1px; min-height:1px; }
.group { padding:7px 12px; background:var(--surface); border-top:1px solid var(--line); display:flex; flex-wrap:wrap; gap:4px 12px; align-items:baseline; font-size:.86rem; }
.group:first-child { border-top:0; } .group b { font-size:.95rem; } .group span { color:var(--muted); }
.row.g2 { box-shadow:inset 4px 0 0 var(--no); } .row.g1 { box-shadow:inset 4px 0 0 var(--mid); } .row.g0 { box-shadow:inset 4px 0 0 var(--line); }
.tl { display:block; width:100%; max-width:230px; height:26px; }
.tl .ax { stroke:var(--line); stroke-width:1; } .tl .sp { stroke:var(--muted); stroke-width:2; }
.tl .cur { fill:var(--yes); } .tl .oth { fill:var(--mid); } .tl text { fill:var(--muted); font-size:8px; font-family:var(--font-body); }
.docs { display:grid; grid-template-columns:repeat(auto-fit,minmax(300px,1fr)); gap:12px; }
.doc { border:1px solid var(--line); border-radius:8px; padding:10px 12px; background:var(--bg); display:flex; flex-direction:column; gap:7px; min-width:0; }
.dh { display:flex; justify-content:space-between; gap:10px; align-items:baseline; } .dn { font-weight:600; overflow-wrap:anywhere; } .dd { color:var(--muted); font-size:.8rem; white-space:nowrap; }
.dp { color:var(--muted); font-size:.74rem; font-family:var(--font-mono); overflow-wrap:anywhere; margin-top:-5px; }
.say { display:flex; flex-direction:column; gap:1px; padding-left:10px; border-left:3px solid var(--mid); }
.say.cur { border-left-color:var(--yes); } .sq { overflow-wrap:anywhere; } .sh { color:var(--muted); font-size:.76rem; }
.detail input { font:inherit; width:100%; padding:6px 9px; border:1px solid var(--line); border-radius:6px; background:var(--bg); color:var(--fg); }
.detail label.member { display:flex; gap:8px; align-items:baseline; color:var(--fg); font-size:.86rem; overflow-wrap:anywhere; cursor:pointer; }
.detail label.member input { width:auto; padding:0; margin:0; flex:none; }
.detail label.member span { color:var(--muted); font-size:.8rem; }
.big { display:flex; flex-wrap:wrap; gap:6px 26px; align-items:baseline; }
.big b { font-size:clamp(1.6rem,4vw,2.4rem); line-height:1; font-variant-numeric:tabular-nums; } .big span { color:var(--muted); font-size:.9rem; }
.heatwrap { overflow-x:auto; border:1px solid var(--line); border-radius:10px; padding:12px; background:var(--surface); }
.heat { display:grid; grid-template-columns:minmax(96px,150px) repeat(var(--weeks),minmax(13px,1fr)); gap:2px; min-width:520px; align-items:center; }
.heat .lab { font-size:.74rem; white-space:nowrap; overflow:hidden; text-overflow:ellipsis; padding-right:8px; border:0; background:transparent; text-align:left; color:var(--fg); }
.heat .lab.gone { text-decoration:line-through; color:var(--no); } .heat .lab.stop { color:var(--mid); }
.heat .mo { font-size:.68rem; color:var(--muted); grid-row:1; } .heat .c { height:11px; border-radius:2px; background:var(--line); position:relative; }
.heat .c i { position:absolute; inset:0; border-radius:2px; background:var(--accent); } .heat .c.gone i { background:var(--no); }
.heat .c u { position:absolute; right:1px; top:1px; width:5px; height:5px; border-radius:50%; background:var(--bg); border:1.5px solid var(--yes); }
.legend { display:flex; flex-wrap:wrap; gap:6px 16px; align-items:center; font-size:.8rem; color:var(--muted); margin-top:8px; }
.lg { display:inline-block; vertical-align:middle; margin-right:5px; } .lg.cur, .lg.oth { width:9px; height:9px; border-radius:50%; background:var(--yes); } .lg.oth { background:var(--mid); width:7px; height:7px; } .lg.e2, .lg.e1 { width:4px; height:14px; background:var(--no); } .lg.e1 { background:var(--mid); }
.legend .sw { display:inline-flex; gap:2px; vertical-align:middle; margin-right:4px; } .legend .sw i { width:11px; height:11px; border-radius:2px; background:var(--accent); display:block; }
.more { border:0; background:transparent; color:var(--accent); padding:8px 12px; width:100%; text-align:left; border-top:1px solid var(--line); }
.empty { padding:14px 12px; color:var(--muted); }
.row.line .meta + button.pick { justify-self:end; }
.undo { font-family:var(--font-mono); font-size:.8rem; white-space:nowrap; color:var(--fg); }

/* Findings: plain sentences, a red mark only where a finding is open, neutral everywhere else. */
main > header { border-top:1px solid var(--line); padding-top:18px; }
.findings { display:flex; flex-direction:column; gap:14px; }
.findings h1 { margin-bottom:2px; }
.finding { border-top:1px solid var(--line); padding-top:14px; display:grid; grid-template-columns:minmax(0,1.4fr) minmax(0,1fr); gap:8px 24px; align-items:start; }
.finding h3 { font-size:1.05rem; grid-column:1 / -1; text-wrap:balance; }
.finding .ev, .finding .next { color:var(--fg); font-size:.92rem; display:block; }
.finding .next { margin-top:6px; color:var(--muted); }
.finding figure { margin:0; }
.finding figcaption { color:var(--muted); font-size:.8rem; margin-top:4px; }
.fspark { display:block; width:100%; max-width:300px; height:auto; }
.fspark .ln { fill:none; stroke:var(--bad); stroke-width:2; } .fspark .pt { fill:var(--bad); }
.fspark .tg, .tspark .tg { stroke:var(--rule); stroke-width:1; stroke-dasharray:4 3; }
.fspark .ax, .tspark .ax { stroke:var(--line); stroke-width:1; }
.fspark text { fill:var(--muted); font-size:9px; font-family:var(--font-body); }
.twrap { overflow-x:auto; border:1px solid var(--line); border-radius:10px; }
table.calls { border-collapse:collapse; width:100%; min-width:640px; font-size:.88rem; font-variant-numeric:tabular-nums; }
table.calls th, table.calls td { text-align:left; padding:7px 12px; border-top:1px solid var(--line); vertical-align:middle; }
table.calls thead th { border-top:0; color:var(--muted); font-weight:600; font-size:.8rem; }
table.calls td.n { text-align:right; } table.calls th.n { text-align:right; }
.tspark { display:block; width:168px; height:28px; }
.tspark .ln { fill:none; stroke:var(--spark); stroke-width:1.5; } .tspark .pt, .tspark .over { fill:var(--spark); }
.fsrc { font-size:.8rem; }
@media (max-width:760px) { .finding { grid-template-columns:minmax(0,1fr); } }

.foot { position:fixed; left:0; right:0; bottom:0; background:var(--surface); border-top:1px solid var(--line); }
.foot .in { max-width:1080px; margin:0 auto; padding:9px 16px; display:flex; flex-wrap:wrap; gap:8px 18px; align-items:center; }
.foot .sum { flex:1; min-width:220px; font-size:.9rem; }
.foot button { border:1px solid var(--line); background:var(--bg); border-radius:7px; padding:6px 12px; }
.foot button.primary { background:var(--accent); color:var(--bg); border-color:var(--accent); font-weight:600; }
.cmds { display:flex; flex-direction:column; gap:8px; }
.cmds pre { margin:0; padding:12px; border:1px solid var(--line); border-radius:8px; background:var(--surface); font-family:var(--font-mono); font-size:.8rem; overflow-x:auto; white-space:pre; max-height:340px; }
@media (max-width:760px) {
  .row, .row.line { grid-template-columns:minmax(0,1fr); }
  .row.line .meta { white-space:normal; }
  .seg { justify-self:start; }
  .tools input { width:130px; }
}
@media (prefers-reduced-motion: no-preference) { .row { transition:background-color .12s; } }
@media (prefers-reduced-motion: reduce) { details.fold > summary::before { transition:none; } }
</style>
</head>
<body>
<main>
<section class="findings" id="findings" aria-labelledby="fhead">
  <div class="kicker" id="kicker"></div>
  <h1 id="fhead"></h1>
  <p class="lede" id="flede"></p>
  <div id="f-open"></div>
  <section class="q" id="everything">
    <h2>Everything else</h2>
    <div class="twrap"><table class="calls" id="f-table"></table></div>
    <p class="note" id="f-watch" hidden></p>
    <p class="note fsrc" id="f-foot"></p>
  </section>
</section>

<header>
  <h2 id="head"></h2>
  <p class="lede" id="batch"></p>
  <section class="q" id="tickets" hidden>
    <div class="qhead"><h2>Tickets</h2><span class="count" id="tk-sum"></span></div>
    <div class="list" id="tk-list"></div>
  </section>
  <p class="note" id="scope" hidden>Everything: ctx page --all</p>
  <div class="batches" id="batches"></div>
  <details class="sub" id="f-batch" hidden><summary id="f-batch-s"></summary><div class="list" id="s-batch"></div></details>
</header>

<section class="q" id="s-need">
  <div class="qhead"><h2>Needs you</h2><span class="count" id="need-n"></span></div>
  <div class="list" id="need-list"></div>
</section>

<section class="q" id="s-same" hidden>
  <div class="qhead"><h2>Same rule in several projects</h2><span class="count" id="same-n"></span></div>
  <p class="note">Rules saved separately in two or more projects that seem to say the same thing. Make global: one rule for every project, in the words you pick, replaces them all. Not the same: these are not offered together again while they stay as they are. Later: nothing changes. ctx never widens a rule without your choice.</p>
  <div class="list" id="same-list"></div>
</section>

<details class="fold" id="f-auto">
  <summary><h2>Decided for you this week</h2><span class="count" id="auto-n"></span></summary>
  <div class="body" id="s-auto"></div>
</details>

<details class="fold" id="f-status">
  <summary><h2>Status</h2><span class="count" id="status-n"></span></summary>
  <div class="body">
    <section class="q" id="s-held"></section>
    <section class="q" id="s-hooks"></section>
  </div>
</details>

<section class="cmds" id="cmds">
  <h2>Your choices</h2>
  <p class="note">One line per choice that changes something. Copy them into a file and run <code>ctx page apply FILE</code> to see what would be done; add <code>--yes</code> to do it. Lines beginning with <code>#</code> are left for you: ctx does not edit your Claude Code settings, so removing a hook is yours to do.</p>
  <pre id="out" tabindex="0"></pre>
</section>

</main>

<div class="foot"><div class="in">
  <div class="sum" id="sum" aria-live="polite"></div>
  <span class="note">J K move · 1 2 3 choose · Enter details</span>
  <button id="jump">Show choices</button>
  <button class="primary" id="copy">Copy choices</button>
</div></div>

<script>
(function () {
  "use strict";
  var D = /*DATA*/;
  var SEC = [
    { key: "rules", title: "Proposed rules", noun: "rule", labels: ["Accept", "Later", "Not a rule"] },
    { key: "conf", title: "Document conflicts", noun: "conflict", labels: ["Fix", "Not a conflict", "Later"] },
    { key: "held", el: "s-held", title: "What ctx holds", noun: "project", labels: ["Keep", "Stop archiving", "Forget"], lead: "Every project ctx has saved sessions for. A turn is one message, reply or tool result; a session day is one session on one day. Keep: nothing changes. Stop archiving: what is saved stays searchable, and nothing new is saved. Forget: the project’s sessions are removed from search for good; the saved text stays in ctx’s own history, but nothing will find it. The grid is every project by every week: darker means more was saved, a ring means an agent searched that project's archive that week.", page: 12, filter: true },
    { key: "hooks", el: "s-hooks", title: "Hooks ctx runs", noun: "hook", labels: ["Keep", "Later", "Remove"], lead: "What ctx does by itself and ran in the last seven days, with how often, how long it took, and what it did. Keep: nothing changes. Later: decide another time. Remove: ctx cannot do this for you, because hooks live in your Claude Code settings file and ctx does not edit it; the list at the foot names the entry to remove.", page: 20, filter: false }
  ];
  var BY = {}; SEC.forEach(function (s) { BY[s.key] = s; });
  D.tickets = /*TICKETS*/;
  var state = { auto: {}, same: {} }, view = {}, cursor = null;
  SEC.forEach(function (s) {
    state[s.key] = {};
    D[s.key].forEach(function (it) { state[s.key][it.id] = { c: null, open: false, text: null }; /* nothing is chosen for you: a batch is applied only when you press it */ });
    view[s.key] = { show: "all", text: "", limit: s.page };
  });
  D.auto.forEach(function (a) { state.auto[a.id] = { undo: false, open: false }; });
  D.same.forEach(function (g) { state.same[g.id] = { c: null, pick: g.pick }; });
  function esc(t) { return String(t).replace(/[&<>"]/g, function (c) { return { "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;" }[c]; }); }
  function num(n) { return Number(n).toLocaleString("en-US"); }
  function el(tag, cls, htmlText) { var e = document.createElement(tag); if (cls) e.className = cls; if (htmlText !== undefined) e.innerHTML = htmlText; return e; }
  function plural(n, one, many) { return num(n) + " " + (n === 1 ? one : (many || one + "s")); }
  function has(it) { return it.s !== null && it.s !== undefined; }
  /* a project or hook is the person's only when ctx suggests changing it; keeping it is no change */
  function change(it) { return has(it) && it.s > 0; }

  function evidence(s, it) {
    if (s.key === "rules") return '<span>You said: <q>' + esc(it.said || "(the turn is no longer in the index)") + '</q></span>';
    if (s.key === "conf") return timeline(it) + '<span class="nums"><span>' + it.n + ' passages in ' + docCount(it) + ' documents</span></span>';
    if (s.key === "held") {
      var m = Math.max.apply(null, it.w.concat([1]));
      var bars = it.w.map(function (v) { return '<i style="height:' + Math.max(1, Math.round(18 * v / m)) + 'px"></i>'; }).join("");
      return '<span class="nums"><span><span class="bars" role="img" aria-label="turns in each of the last four weeks: ' + it.w.join(", ") + '">' + bars + '</span>last 4 weeks</span><span>' + num(it.turns) + ' turns in all</span><span>' + it.mb + ' MB</span><span>last active ' + esc(it.last) + '</span><span>' + (it.reads ? 'searched by an agent ' + it.reads + '× in 30 days' : 'not searched by an agent in 30 days') + '</span></span>';
    }
    return '<span class="nums"><span>ran ' + num(it.runs) + ' times in 7 days</span><span>usually ' + it.med + ' ms</span><span>slowest ' + num(it.mx) + ' ms</span><span>' + (it.runs * it.med < 1000 ? 'under a second' : Math.round(it.runs * it.med / 1000) + ' seconds') + ' a week in all</span></span><span>' + esc(it.eff) + '</span>';
  }
  function docCount(it) { return it.ps.map(function (p) { return p[0]; }).filter(function (v, i, a) { return a.indexOf(v) === i; }).length; }
  var MONTHS = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
  function nice(d) { return d ? +d.slice(8, 10) + " " + MONTHS[+d.slice(5, 7) - 1] + " " + d.slice(0, 4) : "date unknown"; }
  function day(d) { return Date.UTC(+d.slice(0, 4), +d.slice(5, 7) - 1, +d.slice(8, 10)) / 864e5; }
  function timeline(it) {
    var a = day(D.span[0]), b = day(D.span[1]), W = 230, x = function (d) { return 6 + (W - 12) * (day(d) - a) / Math.max(1, b - a); };
    var dots = it.ps.filter(function (p) { return p[4]; });
    if (!dots.length) return "";
    var xs = dots.map(function (p) { return x(p[4]); }), lo = Math.min.apply(null, xs), hi = Math.max.apply(null, xs);
    /* about four month labels across whatever span the data covers */
    var y0 = +D.span[0].slice(0, 4), m0 = +D.span[0].slice(5, 7) - 1, count = (+D.span[1].slice(0, 4) * 12 + +D.span[1].slice(5, 7) - 1) - (y0 * 12 + m0) + 1, step = Math.max(1, Math.ceil(count / 4)), months = "";
    for (var k = 0; k < count; k += step) {
      var mm = (m0 + k) % 12, d = (y0 + Math.floor((m0 + k) / 12)) + "-" + ("0" + (mm + 1)).slice(-2) + "-01";
      months += '<text x="' + x(d).toFixed(1) + '" y="25" text-anchor="middle">' + MONTHS[mm] + '</text>';
    }
    return '<svg class="tl" viewBox="0 0 ' + W + ' 26" role="img" aria-label="When each document was last changed: from ' + esc(it.old) + ' to ' + esc(it.new) + '"><line class="ax" x1="6" y1="9" x2="' + (W - 6) + '" y2="9"/>' +
      '<line class="sp" x1="' + lo.toFixed(1) + '" y1="9" x2="' + hi.toFixed(1) + '" y2="9"/>' +
      dots.map(function (p, i) { return '<circle class="' + (p[3] ? 'cur' : 'oth') + '" cx="' + xs[i].toFixed(1) + '" cy="9" r="' + (p[3] ? 4.5 : 3.5) + '"><title>' + esc(p[0].split("/").pop()) + ' last changed ' + esc(p[4]) + (p[3] ? ' (likely current)' : '') + '</title></circle>'; }).join("") + months + '</svg>';
  }
  function ago(d) {
    if (!d) return "date unknown";
    var n = Math.round(day(D.tot.today) - day(d));
    return n <= 0 ? "today" : n === 1 ? "yesterday" : n < 60 ? n + " days ago" : Math.round(n / 30.4) + " months ago";
  }
  function gapText(it) {
    return it.gap >= 90 ? '<span class="t-no">last changed ' + Math.round(it.gap / 30.4) + ' months apart</span>' : it.gap >= 30 ? '<span class="t-mid">last changed ' + Math.round(it.gap / 7) + ' weeks apart</span>' : it.gap >= 2 ? 'last changed ' + it.gap + ' days apart' : 'last changed the same day';
  }
  /* the full line, for the held and hooks lists */
  function meta(s, it) {
    if (s.key === "held") return it.days + " session days · since " + esc(it.first);
    return 'runs ' + esc(it.ev);
  }
  /* the short muted line under a title in a one-line row */
  function brief(s, it) {
    if (s.key === "rules") return esc(it.p) + " · #" + it.id + " · " + esc(ago(it.d)) + (it.again ? ' · <span class="t-yes">said ' + it.again + ' more time' + (it.again === 1 ? '' : 's') + '</span>' : '') + (has(it) ? ' · ctx suggests ' + esc(s.labels[it.s].toLowerCase()) : '');
    if (s.key === "conf") return esc(it.p) + " · " + it.n + " passages in " + docCount(it) + " documents · " + gapText(it) + (has(it) ? ' · ctx suggests ' + esc(s.labels[it.s].toLowerCase()) : '');
    return meta(s, it) + (has(it) ? ' · ctx suggests ' + esc(s.labels[it.s].toLowerCase()) : '');
  }
  function detail(s, it) {
    if (s.key === "conf") {
      /* one block a document, newest first: its name and date once, then what it says in plain words */
      var clean = function (q) { return q.replace(/[`*]/g, "").replace(/^\s*\|\s*|\s*\|\s*$/g, "").replace(/\s*\|\s*/g, " · ").replace(/\s+/g, " ").trim(); };
      var docs = [], by = {};
      it.ps.forEach(function (p) {
        var d = by[p[0]]; if (!d) { d = by[p[0]] = { path: p[0], date: p[4], says: [], cur: false }; docs.push(d); }
        var q = clean(p[2]); if (!d.says.some(function (x) { return x.q === q; })) d.says.push({ q: q, head: p[1], cur: p[3] });
        if (p[3]) d.cur = true;
      });
      docs.sort(function (x, y) { return x.date < y.date ? 1 : -1; });
      return '<div class="docs">' + docs.map(function (d) {
        var name = d.path.split("/").pop(), dir = d.path.slice(0, d.path.length - name.length);
        return '<div class="doc"><div class="dh"><span class="dn">' + esc(name) + '</span><span class="dd">' + (d.date ? "changed " + nice(d.date) : "date unknown") + '</span></div><div class="dp">' + esc(dir) + '</div>' +
          d.says.map(function (x) { return '<div class="say ' + (x.cur ? 'cur' : 'oth') + '"><span class="sq">' + esc(x.q) + '</span><span class="sh">' + (x.cur ? 'read as current · ' : '') + 'under “' + esc(x.head) + '”</span></div>'; }).join("") + '</div>';
      }).join("") + '</div>';
    }
    if (s.key === "rules") return '<label class="where" for="w' + it.id + '">Wording agents would be given. Edit it before accepting if the paraphrase is off:</label><input id="w' + it.id + '" data-word="' + it.id + '" value="' + esc(state.rules[it.id].text === null ? it.t : state.rules[it.id].text) + '">' + '<div class="where">Accept: agents in <b>' + esc(it.p) + '</b> are given it at every session start. Later: it stays here. Not a rule: ctx does not propose this sentence again.</div>';
    if (s.key === "held") return '<div class="where">' + it.days + ' session days from ' + esc(it.first) + ' to ' + esc(it.last) + ' · ' + num(it.turns) + ' turns · ' + it.mb + ' MB of text · turns in the last four weeks: ' + it.w.map(num).join(", ") + '</div><div class="where">Read back counts ctx search and ctx turn calls in this project\'s own sessions in 30 days: ' + it.reads + '.</div>';
    return '<div class="where">Fires on: ' + esc(it.ev) + ' · 95th percentile ' + it.p95 + ' ms · seven days of the call log</div>';
  }
  function why(s, it) { return '<span class="why">' + (has(it) ? '<b>Suggests ' + esc(s.labels[it.s].toLowerCase()) + ':</b> ' : '<b>No suggestion:</b> ') + esc(it.why) + '</span>'; }

  /* A row of one line: title, a short muted line, the choices. The evidence opens on click or Enter. */
  function lineRow(s, it, after) {
    var st = state[s.key][it.id];
    var row = el("div", "row line" + (st.c !== null ? " chosen" : "") + (st.c === 2 || (s.key === "conf" && st.c === 1) ? " done-no" : ""));
    row.tabIndex = 0; row.dataset.sec = s.key; row.dataset.id = it.id; row.setAttribute("aria-expanded", String(st.open));
    if (cursor && cursor.sec === s.key && String(cursor.id) === String(it.id)) row.classList.add("cur");
    row.appendChild(el("div", "main", '<span class="title">' + esc(it.t) + '</span><span class="meta">' + brief(s, it) + '</span>'));
    var seg = el("div", "seg"); seg.setAttribute("role", "group"); seg.setAttribute("aria-label", "Choice for " + it.t);
    s.labels.forEach(function (lab, c) {
      var b = el("button", "k" + (s.key === "conf" ? [0, 2, 1][c] : c), '<kbd>' + (c + 1) + '</kbd>' + esc(lab)); b.type = "button"; b.setAttribute("aria-pressed", String(st.c === c));
      if (s.key === "hooks" && it.lock && c === 2) { b.disabled = true; b.title = "Cannot be removed here: everything else depends on it"; }
      b.addEventListener("click", function (e) { e.stopPropagation(); cursor = { sec: s.key, id: it.id }; choose(s, it, c); });
      seg.appendChild(b);
    });
    row.appendChild(seg);
    if (st.open) {
      var det = el("div", "detail", '<div class="ev">' + evidence(s, it) + why(s, it) + '</div>' + detail(s, it)); row.appendChild(det);
      var w = det.querySelector("input[data-word]");
      if (w) { w.addEventListener("click", function (e) { e.stopPropagation(); }); w.addEventListener("input", function () { st.text = w.value === it.t ? null : w.value; totals(); }); }
    }
    row.addEventListener("click", function (e) { if (e.target.closest("button") || e.target.closest("input") || e.target.closest(".detail")) return; cursor = { sec: s.key, id: it.id }; st.open = !st.open; after(); focusCursor(); });
    return row;
  }

  /* What needs the person: conflicts with no suggestion, proposals the automatic judge left, and
   * projects or hooks only where ctx suggests a change. Suggested conflicts are in the batches. */
  function needs() {
    return [
      [BY.conf, D.conf.filter(function (it) { return !has(it); })],
      [BY.rules, D.rules],
      [BY.held, D.held.filter(change)],
      [BY.hooks, D.hooks.filter(change)]
    ].filter(function (g) { return g[1].length; });
  }
  function undecided(s, items) { return items.filter(function (it) { return state[s.key][it.id].c === null; }).length; }
  function renderNeed() {
    var host = document.getElementById("need-list"), groups = needs(), left = 0;
    host.innerHTML = "";
    groups.forEach(function (g) {
      var s = g[0], items = g[1], open = undecided(s, items); left += open;
      host.appendChild(el("div", "group", '<b>' + esc(s.title) + '</b><span>' + plural(items.length, s.noun) + (open < items.length ? ' · ' + (items.length - open) + ' chosen' : '') + '</span>'));
      items.forEach(function (it) { host.appendChild(lineRow(s, it, renderNeed)); });
    });
    if (!groups.length) host.appendChild(el("div", "empty", "Nothing needs you."));
    document.getElementById("need-n").textContent = "· " + num(left);
  }

  var BATCH = [
    { c: 0, s: 0, say: function (n) { return "File " + plural(n, "docs ticket"); }, hint: "one ticket a conflict, for an agent to correct the documents" },
    { c: 1, s: 1, say: function (n) { return "Dismiss " + num(n); }, hint: "not a conflict: not raised again unless a passage changes" }
  ];
  function batchItems(b) { return D.conf.filter(function (it) { return it.s === b.s; }); }
  function renderBatch() {
    var host = document.getElementById("batches"), all = D.conf.filter(has);
    host.innerHTML = "";
    BATCH.forEach(function (b) {
      var items = batchItems(b); if (!items.length) return;
      var on = items.every(function (it) { return state.conf[it.id].c === b.c; });
      var btn = el("button", "bt", esc(b.say(items.length)) + (on ? '<small>chosen · click to clear</small>' : ''));
      btn.type = "button"; btn.setAttribute("aria-pressed", String(on)); btn.title = b.hint;
      btn.addEventListener("click", function () {
        items.forEach(function (it) { state.conf[it.id].c = on ? null : b.c; });
        refresh("conf");
      });
      host.appendChild(btn);
    });
    var fold = document.getElementById("f-batch");
    fold.hidden = !all.length;
    document.getElementById("f-batch-s").textContent = "See the " + plural(all.length, "conflict") + " ctx has a suggestion for";
    var list = document.getElementById("s-batch"); list.innerHTML = "";
    all.forEach(function (it) { list.appendChild(lineRow(BY.conf, it, renderBatch)); });
  }

  var REASON = {
    covered: ["Already covered by a rule", "A rule in force or your global instructions already says it; the rule's words are quoted."],
    setup: ["Session setup", "About how one session or period is run, such as which model is running; not a standing instruction."],
    not: ["Not a rule", "Not an instruction, or distorted as a rule."]
  };
  function autoRow(a) {
    var st = state.auto[a.id];
    var row = el("div", "row line" + (st.undo ? " chosen" : "")); row.tabIndex = 0; row.dataset.sec = "auto"; row.dataset.id = a.id; row.setAttribute("aria-expanded", String(st.open));
    if (cursor && cursor.sec === "auto" && String(cursor.id) === String(a.id)) row.classList.add("cur");
    row.appendChild(el("div", "main", '<span class="title">' + esc(a.t) + '</span><span class="meta">' + esc(a.p) + ' · ' + esc(ago(a.d)) + ' · ' + esc(a.why) + '</span>' + (a.q ? '<span class="meta cov">Covered by <q>' + esc(a.q) + '</q></span>' : '')));
    var b = el("button", "pick", '<kbd>1</kbd>' + (st.undo ? "Will undo" : "Undo")); b.type = "button"; b.setAttribute("aria-pressed", String(st.undo));
    b.title = a.v === "serve" ? "Stop giving it to agents; it waits for you instead" : "Reopen it; it waits for you instead";
    b.addEventListener("click", function (e) { e.stopPropagation(); cursor = { sec: "auto", id: a.id }; undo(a); });
    row.appendChild(b);
    if (st.open) {
      var again = a.rep && a.rep.length ? "You said it again on " + a.rep.map(function (d) { return d ? nice(d) : "a date not recorded"; }).join(", ") + "." : "You have not said it again since.";
      row.appendChild(el("div", "detail", '<div class="ev"><span>You typed: <q>' + esc(a.said || "(the turn is no longer in the index)") + '</q></span><span>' + esc(again) + '</span><span class="why"><b>Decided ' + esc(nice(a.d)) + ':</b> ' + esc(a.why) + '</span></div>'));
    }
    row.addEventListener("click", function (e) { if (e.target.closest("button") || e.target.closest(".detail")) return; cursor = { sec: "auto", id: a.id }; st.open = !st.open; renderAuto(); focusCursor(); });
    return row;
  }
  function undo(a) { state.auto[a.id].undo = !state.auto[a.id].undo; renderAuto(); totals(); focusCursor(); }

  /* A group of rules from several projects: the members, the words to keep, and the three choices. */
  var SAME = ["Make global", "Not the same", "Later"];
  function sameRow(g) {
    var st = state.same[g.id];
    var row = el("div", "row line" + (st.c !== null ? " chosen" : "") + (st.c === 1 ? " done-no" : "")); row.tabIndex = 0; row.dataset.sec = "same"; row.dataset.id = g.id;
    if (cursor && cursor.sec === "same" && cursor.id === g.id) row.classList.add("cur");
    var projects = g.m.map(function (m) { return m.p; }).filter(function (v, i, a) { return a.indexOf(v) === i; });
    row.appendChild(el("div", "main", '<span class="title">' + esc(g.m[st.pick].t) + '</span><span class="meta">' + plural(g.m.length, "rule") + ' in ' + plural(projects.length, "project") + ': ' + esc(projects.join(", ")) + '</span>'));
    var seg = el("div", "seg"); seg.setAttribute("role", "group"); seg.setAttribute("aria-label", "Choice for " + g.m[st.pick].t);
    SAME.forEach(function (lab, c) {
      var b = el("button", "k" + [0, 2, 1][c], '<kbd>' + (c + 1) + '</kbd>' + esc(lab)); b.type = "button"; b.setAttribute("aria-pressed", String(st.c === c));
      b.addEventListener("click", function (e) { e.stopPropagation(); cursor = { sec: "same", id: g.id }; sameChoose(g, c); });
      seg.appendChild(b);
    });
    row.appendChild(seg);
    var det = el("div", "detail", '<div class="where">The words the one rule for every project would keep:</div>' + g.m.map(function (m, i) {
      return '<label class="member"><input type="radio" name="pick-' + esc(g.id) + '" data-i="' + i + '"' + (i === st.pick ? ' checked' : '') + '>' + esc(m.t) + '<span>' + esc(m.p) + ' · ' + esc(m.s) + '</span></label>';
    }).join(""));
    Array.prototype.forEach.call(det.querySelectorAll("input"), function (r) {
      r.addEventListener("change", function () { st.pick = Number(r.dataset.i); cursor = { sec: "same", id: g.id }; renderSame(); totals(); focusCursor(); });
    });
    row.appendChild(det);
    return row;
  }
  function sameChoose(g, c) { var st = state.same[g.id]; st.c = st.c === c ? null : c; renderSame(); totals(); focusCursor(); }
  function renderSame() {
    var sec = document.getElementById("s-same"), host = document.getElementById("same-list");
    sec.hidden = !D.same.length; host.innerHTML = "";
    document.getElementById("same-n").textContent = "· " + plural(D.same.length, "group");
    D.same.forEach(function (g) { host.appendChild(sameRow(g)); });
  }

  /* Tickets: one row a project, open ones first, each opening to its ticket list. A claimed
   * ticket names the session holding it, with a Copy button for its prod command; one closed
   * this week shows the result or the reason it was dropped. */
  function mins(sec) { return sec < 90 ? "just now" : sec < 5400 ? Math.round(sec / 60) + " min ago" : sec < 172800 ? Math.round(sec / 3600) + " h ago" : Math.round(sec / 86400) + " days ago"; }
  /* times come as UTC ISO strings, so the age is from when the page is read */
  function agoText(when, none) {
    var d = when ? new Date(when).getTime() : NaN;
    return isNaN(d) ? none : mins(Math.max(0, (Date.now() - d) / 1000));
  }
  function copyText(t, b) {
    try { navigator.clipboard.writeText(t).then(function () { b.textContent = "Copied"; setTimeout(function () { b.textContent = "Copy prod"; }, 1600); }, function () { b.textContent = "Copy: " + t; }); } catch (e) { b.textContent = "Copy: " + t; }
  }
  var TKOPEN = {};
  function renderTickets() {
    var TK = D.tickets, host = document.getElementById("tk-list"), sec = document.getElementById("tickets");
    if (!TK || !TK.rows.length) { if (sec) sec.hidden = true; return; }
    if (sec) sec.hidden = false;
    document.getElementById("tk-sum").textContent = "· " + num(TK.open) + " open tickets across " + num(TK.projects) + " projects · agents closed " + num(TK.closed) + " this week";
    host.innerHTML = "";
    TK.rows.forEach(function (r) {
      var ff = el("details", "fold");
      var name = D.scope ? esc(r["p"]) : '<a href="file:' + esc(r["link"]) + '">' + esc(r["p"]) + '</a>';
      ff.appendChild(el("summary", "", "<b>Tickets</b><span>" + name + "</span><span class=\"count\">" + plural(r["open"], "open ticket") + (r["docs"] ? " (" + r["docs"] + " docs)" : "") + " · " + (r["inprog"] ? r["inprog"] + " in progress · " : "") + "closed this week " + r["done"] + " done · " + r["dropped"] + " dropped" + (r["last"] ? " · active " + agoText(r["last"], "") : "") + "</span>"));
      ff.dataset.fold = "tk:" + r["p"]; /* keeps its open state across a reload, by project, wherever the row moves */
      var body = el("div", "body"), list = el("div", "list");
      if (!r["tickets"].length) list.appendChild(el("div", "empty", "No tickets."));
      r["tickets"].forEach(function (t) {
        var row = el("div", "row line");
        var state = t["s"] === "Claimed" ? "claimed" : t["s"] === "Done" ? "done" : t["s"] === "Dropped" ? "dropped" : "open";
        var meta = t["id"] + " · " + esc(t["ty"]) + " · " + plural(t["age"], "day") + " old · " + state + (t["why"] ? ": " + esc(t["why"]) : "");
        var main = el("div", "main", '<span class="title">' + esc(t["t"]) + '</span><span class="meta">' + meta + (t["session"] ? ' · held by a session active ' + esc(agoText(t["seen"], "a moment ago")) : "") + '</span>');
        row.appendChild(main);
        if (t["s"] === "Claimed" && t["session"]) {
          var cmd = 'ctx ticket prod ' + t["id"] + ' "…"';
          var b = el("button", "pick", "Copy prod"); b.type = "button"; b.title = cmd;
          b.addEventListener("click", function () { copyText(cmd, b); });
          row.appendChild(b);
        }
        list.appendChild(row);
      });
      body.appendChild(list);
      ff.appendChild(body);
      host.appendChild(ff);
    });
  }
  /* The page refreshes itself only when nothing is chosen and no detail is open, and not while it
   * is hidden. Folds keep their state across reloads through sessionStorage. */
  function anyChoice() {
    var s = SEC.some(function (x) { return D[x.key].some(function (it) { return state[x.key][it.id] && state[x.key][it.id].c !== null; }); });
    var u = D.auto.some(function (a) { return state.auto[a.id] && state.auto[a.id].undo; });
    var g = D.same.some(function (x) { return state.same[x.id].c !== null; });
    return s || u || g;
  }
  /* Every named fold keeps its open state across a reload, and does so quietly if storage is denied. */
  function rememberFolds() {
    Array.prototype.forEach.call(document.querySelectorAll("details.fold"), function (ff) {
      var name = ff.dataset.fold || ff.id;
      if (!name) return;
      var key = "ctx-fold:" + name;
      try { if (sessionStorage.getItem(key) !== null) ff.open = sessionStorage.getItem(key) === "1"; } catch (e) {}
      ff.addEventListener("toggle", function () { try { sessionStorage.setItem(key, ff.open ? "1" : "0"); } catch (e) {} });
    });
  }
  /* In use: hidden, something chosen, a row's detail open, a rule's wording or a filter typed
   * in. A reload then would lose it. */
  function inUse() {
    if (document.hidden || anyChoice()) return true;
    for (var k in state) for (var id in state[k]) { var st = state[k][id]; if (st.open || (st.text !== undefined && st.text !== null)) return true; }
    for (k in view) if (view[k].text !== "") return true;
    return false;
  }
  function armReload() {
    setInterval(function () { if (!inUse()) location.reload(); }, 30000);
  }

  function renderAuto() {
    var host = document.getElementById("s-auto"), A = D.auto, added = A.filter(function (a) { return a.v === "serve"; }), gone = A.length - added.length;
    document.getElementById("auto-n").textContent = "· " + num(added.length) + " added · " + num(gone) + " dismissed";
    host.innerHTML = "";
    host.appendChild(el("p", "note", "Proposed rules ctx's automatic judge decided without asking you in the last seven days. An added rule is given to agents as a tentative rule until you stop saying it; a dismissed one is not raised again. Undo puts one back in your hands."));
    if (!A.length) {
      host.appendChild(el("div", "list", '<div class="empty">ctx decided nothing by itself in the last seven days.' + (D.judge ? '' : ' The automatic judge is off; <code>ctx rules auto CMD</code> turns it on.') + '</div>'));
      return;
    }
    var part = function (title, count, items, groups) {
      var sec = el("section", "q");
      sec.appendChild(el("div", "sub-h", '<h3>' + esc(title) + '</h3><span class="count">' + num(count) + '</span>'));
      var list = el("div", "list");
      if (groups) groups.forEach(function (g) {
        var mine = items.filter(function (a) { return a.v === g; }); if (!mine.length) return;
        list.appendChild(el("div", "group", '<b>' + esc(REASON[g][0]) + ' · ' + mine.length + '</b><span>' + esc(REASON[g][1]) + '</span>'));
        mine.forEach(function (a) { list.appendChild(autoRow(a)); });
      });
      else items.forEach(function (a) { list.appendChild(autoRow(a)); });
      if (!items.length) list.appendChild(el("div", "empty", "None this week."));
      sec.appendChild(list); host.appendChild(sec);
    };
    part("Added", added.length, added, null);
    part("Dismissed", gone, A.filter(function (a) { return a.v !== "serve"; }), ["covered", "setup", "not"]);
  }

  function visible(s) {
    var v = view[s.key], t = v.text.toLowerCase();
    return D[s.key].filter(function (it) {
      var st = state[s.key][it.id];
      if (v.show === "open" && st.c !== null) return false;
      if (v.show === "chosen" && st.c === null) return false;
      if (t && (it.t + " " + (it.p || "")).toLowerCase().indexOf(t) < 0) return false;
      return true;
    });
  }
  function heat() {
    var wrap = el("div"), weeks = D.weeks, mx = 1, turns = 0, mb = 0;
    D.held.forEach(function (it) { turns += it.turns; mb += it.mb; it.hist.forEach(function (h) { if (h[0] > mx) mx = h[0]; }); });
    wrap.appendChild(el("div", "big", '<div><b>' + num(turns) + '</b> <span>turns saved (messages, replies and tool results)</span></div><div><b>' + D.held.length + '</b> <span>projects</span></div><div><b>' + weeks.length + '</b> <span>weeks</span></div><div><b>' + Math.round(mb) + ' MB</b> <span>of text</span></div><div><b>' + D.held.filter(function (x) { return x.reads; }).length + '</b> <span>projects an agent searched last month</span></div>'));
    if (!weeks.length) return wrap;
    var hw = el("div", "heatwrap"), g = el("div", "heat"); g.style.setProperty("--weeks", weeks.length); g.setAttribute("role", "img"); g.setAttribute("aria-label", "Turns archived for each project in each week since " + weeks[0]);
    g.appendChild(el("span", "mo", ""));
    var last = "";
    weeks.forEach(function (w) { var m = MONTHS[+w.slice(5, 7) - 1], lab = m !== last ? m : ""; last = m; g.appendChild(el("span", "mo", lab)); });
    D.held.forEach(function (it) {
      var c = state.held[it.id].c, lab = el("button", "lab" + (c === 2 ? " gone" : c === 1 ? " stop" : ""), esc(it.t)); lab.type = "button"; lab.title = it.t + ": " + num(it.turns) + " turns, " + it.mb + " MB. Click to find its row.";
      lab.addEventListener("click", function () { view.held.text = it.t; view.held.limit = 12; cursor = { sec: "held", id: it.id }; render(BY.held); focusCursor(); });
      g.appendChild(lab);
      it.hist.forEach(function (h, i) {
        var cell = el("span", "c" + (c === 2 ? " gone" : ""));
        if (h[0]) { var f = el("i"); f.style.opacity = (0.16 + 0.84 * Math.log(1 + h[0]) / Math.log(1 + mx)).toFixed(2); cell.appendChild(f); }
        if (h[1]) cell.appendChild(el("u"));
        cell.title = it.t + " · week of " + weeks[i] + " · " + num(h[0]) + " turns" + (h[1] ? " · read back " + h[1] + "×" : "");
        g.appendChild(cell);
      });
    });
    hw.appendChild(el("div", "legend", '<span><span class="sw"><i style="opacity:.16"></i><i style="opacity:.45"></i><i style="opacity:.72"></i><i></i></span>each square is one week: darker means more was saved</span><span>a small ring: an agent searched that project\'s archive that week</span><span>struck through: you chose Forget</span>'));
    hw.lastChild.style.margin = "0 0 10px";
    hw.appendChild(g);
    wrap.appendChild(hw); wrap.style.display = "flex"; wrap.style.flexDirection = "column"; wrap.style.gap = "12px";
    return wrap;
  }
  function choose(s, it, c) {
    var st = state[s.key][it.id];
    st.c = st.c === c ? null : c;
    refresh(s.key);
  }
  function refresh(key) {
    renderNeed(); renderBatch();
    if (key === "held" || key === "hooks") render(BY[key]);
    totals(); focusCursor();
  }
  /* the held and hooks lists, as they were before the page was folded */
  function render(s) {
    var host = document.getElementById(s.el), v = view[s.key], items = visible(s), all = D[s.key];
    var decided = all.filter(function (it) { return state[s.key][it.id].c !== null; }).length;
    host.innerHTML = "";
    host.appendChild(el("div", "qhead", '<h3>' + esc(s.title) + '</h3><span class="count">' + all.length + ' ' + s.noun + (all.length === 1 ? '' : 's') + ' · ' + decided + ' chosen</span>'));
    host.appendChild(el("p", "note", esc(s.lead)));
    if (s.key === "held") host.appendChild(heat());
    var tools = el("div", "tools");
    [["all", "All"], ["open", "Undecided"], ["chosen", "Chosen"]].forEach(function (f) {
      var b = el("button", "chip", f[1]); b.type = "button"; b.setAttribute("aria-pressed", String(v.show === f[0]));
      b.addEventListener("click", function () { v.show = f[0]; v.limit = s.page; render(s); }); tools.appendChild(b);
    });
    if (s.filter) {
      var inp = el("input"); inp.type = "search"; inp.placeholder = "Filter"; inp.value = v.text; inp.setAttribute("aria-label", "Filter " + s.title);
      inp.addEventListener("input", function () { v.text = inp.value; v.limit = s.page; var pos = inp.selectionStart; render(s); var n = document.getElementById(s.el).querySelector("input"); n.focus(); n.setSelectionRange(pos, pos); });
      tools.appendChild(inp);
    }
    s.labels.forEach(function (lab, c) {
      if (s.key === "held" && c === 2) return; /* no bulk forget: it is the one that cannot be undone */
      var b = el("button", "chip act", lab + " all " + items.length + " matching"); b.type = "button";
      b.addEventListener("click", function () { items.forEach(function (it) { state[s.key][it.id].c = c; }); refresh(s.key); });
      tools.appendChild(b);
    });
    var sug = el("button", "chip act", "Use suggestions"); sug.type = "button";
    sug.addEventListener("click", function () { items.forEach(function (it) { state[s.key][it.id].c = has(it) ? it.s : null; }); refresh(s.key); });
    tools.appendChild(sug);
    host.appendChild(tools);
    var list = el("div", "list");
    if (!items.length) list.appendChild(el("div", "empty", v.show === "open" ? "Nothing left undecided here." : "Nothing matches."));
    items.slice(0, v.limit).forEach(function (it) {
      var st = state[s.key][it.id], changed = st.c !== null && has(it) && st.c !== it.s;
      var row = el("div", "row" + (st.c === 2 ? " done-no" : "")); row.tabIndex = 0; row.dataset.sec = s.key; row.dataset.id = it.id;
      if (cursor && cursor.sec === s.key && String(cursor.id) === String(it.id)) row.classList.add("cur");
      var main = el("div", "main", '<span class="title">' + (changed ? '<span class="dot" title="Your choice differs from the suggestion"></span>' : '') + esc(it.t) + '</span><span class="meta">' + meta(s, it) + '</span>');
      var ev = el("div", "ev", evidence(s, it) + why(s, it));
      var seg = el("div", "seg"); seg.setAttribute("role", "group"); seg.setAttribute("aria-label", "Choice for " + it.t);
      s.labels.forEach(function (lab, c) {
        var b = el("button", "k" + c, '<kbd>' + (c + 1) + '</kbd>' + esc(lab)); b.type = "button"; b.setAttribute("aria-pressed", String(st.c === c));
        if (s.key === "hooks" && it.lock && c === 2) { b.disabled = true; b.title = "Cannot be removed here: everything else depends on it"; }
        b.addEventListener("click", function (e) { e.stopPropagation(); cursor = { sec: s.key, id: it.id }; choose(s, it, c); });
        seg.appendChild(b);
      });
      row.appendChild(main); row.appendChild(ev); row.appendChild(seg);
      if (st.open) row.appendChild(el("div", "detail", detail(s, it)));
      row.addEventListener("click", function (e) { if (e.target.closest("button") || e.target.closest("input")) return; cursor = { sec: s.key, id: it.id }; st.open = !st.open; render(s); focusCursor(); });
      list.appendChild(row);
    });
    if (items.length > v.limit) {
      var more = el("button", "more", "Show " + Math.min(items.length - v.limit, s.page * 3) + " more (" + (items.length - v.limit) + " not shown)"); more.type = "button";
      more.addEventListener("click", function () { v.limit += s.page * 3; render(s); }); list.appendChild(more);
    }
    host.appendChild(list);
  }
  function commands() {
    var out = [], one = function (t) { return String(t).replace(/\s+/g, " ").trim(); }, sq = function (t) { return "'" + one(t).replace(/'/g, "'\\''") + "'"; };
    D.rules.forEach(function (it) { var c = state.rules[it.id].c; var tx = state.rules[it.id].text; if (c === 0) out.push("accept rule " + it.id + (tx ? " as " + sq(tx) : "") + "    # " + one(it.p + ": " + (tx || it.t))); if (c === 2) out.push("reject rule " + it.id + "    # " + one(it.p + ": " + it.t)); });
    D.auto.forEach(function (a) { if (state.auto[a.id].undo) out.push("undo decision " + a.id + "    # " + one(a.p + ": " + a.t)); });
    D.same.forEach(function (g) {
      var st = state.same[g.id], first = g.m[st.pick], rest = g.m.filter(function (m, i) { return i !== st.pick; });
      var why = "    # " + one(first.t) + " (" + g.m.map(function (m) { return m.p; }).filter(function (v, i, a) { return a.indexOf(v) === i; }).join(", ") + ")";
      if (st.c === 0) out.push("make global " + [first].concat(rest).map(function (m) { return sq(m.k); }).join(" ") + why);
      if (st.c === 1) out.push("not the same " + g.m.map(function (m) { return sq(m.k); }).join(" ") + why);
    });
    D.conf.forEach(function (it) {
      var c = state.conf[it.id].c;
      if (c === 0) out.push("ticket " + sq("Docs: " + it.t) + " in " + sq(it.p) + " saying " + sq(it.ps.map(function (p) { return p[0] + " § " + p[1] + ": \"" + p[2].slice(0, 120) + "\"" + (p[3] ? " (likely current)" : ""); }).join(" | ")) + " for conflict " + it.id);
      if (c === 1) out.push("dismiss conflict " + it.id + "    # " + one(it.t) + " (" + it.files.join(", ") + ")");
    });
    D.held.forEach(function (it) { var c = state.held[it.id].c; if (c === 1) out.push("stop archiving " + sq(it.t) + "    # keeps " + it.mb + " MB"); if (c === 2) out.push("forget " + sq(it.t) + "    # removes " + it.turns + " turns, " + it.mb + " MB"); });
    D.hooks.forEach(function (it) { if (state.hooks[it.id].c === 2) out.push("# ctx cannot do this: in your Claude Code settings, remove the `ctx " + it.id.replace("-", " ") + "` entries (" + one(it.ev) + ")"); });
    if (out.length) out.unshift("# choices from the ctx page. Apply them with: ctx page apply FILE");
    return out;
  }
  function totals() {
    var left = undecided(BY.conf, D.conf) + undecided(BY.rules, D.rules) + undecided(BY.held, D.held.filter(change)) + undecided(BY.hooks, D.hooks.filter(change));
    var cmds = commands(), n = cmds.filter(function (l) { return l.charAt(0) !== "#"; }).length;
    document.getElementById("sum").textContent = (n ? plural(n, "choice") + " ready" : "No choices yet") + " · " + (left ? num(left) + " still waiting on you" : "nothing waiting on you");
    document.getElementById("out").textContent = cmds.length ? cmds.join("\n") : "# nothing chosen yet";
  }
  function rowsOnPage() { return Array.prototype.slice.call(document.querySelectorAll(".row")).filter(function (r) { return r.offsetParent !== null; }); }
  function focusCursor() {
    if (!cursor) return;
    var sel = '.row[data-sec="' + cursor.sec + '"][data-id="' + String(cursor.id).replace(/["\\]/g, "\\$&") + '"]';
    var r = Array.prototype.slice.call(document.querySelectorAll(sel)).filter(function (x) { return x.offsetParent !== null; })[0];
    if (r) { r.classList.add("cur"); r.focus({ preventScroll: true }); r.scrollIntoView({ block: "nearest" }); }
  }
  document.addEventListener("keydown", function (e) {
    if (e.target.tagName === "INPUT" || e.metaKey || e.ctrlKey || e.altKey) return;
    var rows = rowsOnPage(); if (!rows.length) return;
    var i = cursor ? rows.findIndex(function (r) { return r.dataset.sec === cursor.sec && r.dataset.id === String(cursor.id); }) : -1;
    var k = e.key.toLowerCase();
    if (k === "j" || e.key === "ArrowDown" || k === "k" || e.key === "ArrowUp") {
      e.preventDefault();
      var n = (k === "j" || e.key === "ArrowDown") ? Math.min(rows.length - 1, i + 1) : Math.max(0, i < 0 ? 0 : i - 1);
      rows.forEach(function (r) { r.classList.remove("cur"); });
      cursor = { sec: rows[n].dataset.sec, id: rows[n].dataset.id }; focusCursor(); return;
    }
    if (i < 0 || (e.target.tagName === "BUTTON" && e.key === "Enter") || e.target.tagName === "SUMMARY") return;
    if (cursor.sec === "same") {
      var g = D.same.filter(function (x) { return x.id === cursor.id; })[0];
      if (k === "1" || k === "2" || k === "3") { e.preventDefault(); sameChoose(g, Number(k) - 1); }
      return;
    }
    if (cursor.sec === "auto") {
      var a = D.auto.filter(function (x) { return String(x.id) === String(cursor.id); })[0];
      if (k === "1") { e.preventDefault(); undo(a); }
      else if (e.key === "Enter") { e.preventDefault(); state.auto[a.id].open = !state.auto[a.id].open; renderAuto(); focusCursor(); }
      return;
    }
    var s = BY[cursor.sec];
    var it = D[s.key].filter(function (x) { return String(x.id) === String(cursor.id); })[0];
    if (k === "1" || k === "2" || k === "3") {
      var c = Number(k) - 1; if (s.key === "hooks" && it.lock && c === 2) return;
      e.preventDefault(); choose(s, it, c);
    } else if (e.key === "Enter") {
      e.preventDefault(); state[s.key][it.id].open = !state[s.key][it.id].open;
      renderNeed(); renderBatch(); if (s.el) render(s); focusCursor();
    }
  });
  document.getElementById("jump").addEventListener("click", function () { document.getElementById("cmds").scrollIntoView({ behavior: "smooth", block: "start" }); });
  document.getElementById("copy").addEventListener("click", function () {
    var b = this, text = commands().join("\n") || "# nothing chosen yet";
    var fail = function () { var pre = document.getElementById("out"), range = document.createRange(); range.selectNodeContents(pre); var sel = window.getSelection(); sel.removeAllRanges(); sel.addRange(range); document.getElementById("cmds").scrollIntoView({ block: "start" }); b.textContent = "Selected: press copy"; };
    try { navigator.clipboard.writeText(text).then(function () { b.textContent = "Copied"; setTimeout(function () { b.textContent = "Copy choices"; }, 1600); }, fail); } catch (err) { fail(); }
  });
  /* The findings: ctx's own problems at the top, then every hook and search on one shared scale. */
  function ms(v) { return v === null || v === undefined || v < 0 ? "no calls" : v < 1000 ? num(v) + " ms" : (v % 1000 === 0 ? v / 1000 : (v / 1000).toFixed(1)) + " s"; }
  function cmdText(t) { return esc(t).replace(/`([^`]+)`/g, "<code>$1</code>"); }
  var COUNT = ["No", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten"];
  function countWord(n) { return n < COUNT.length ? COUNT[n] : num(n); }
  function howLong(age) { var d = Math.floor(age / 86400); return d < 1 ? "under a day" : plural(d, "day"); }
  function span(from, to) { var out = [], a = day(from), b = day(to); for (var d = a; d <= b && out.length < 31; d++) out.push(new Date(d * 864e5).toISOString().slice(0, 10)); return out; }
  var TOP_MS = D.find.scale[1], LOG_TOP = Math.log(TOP_MS) / Math.LN10;
  function logY(v, top, h) { var c = Math.min(Math.max(v, 1), Math.pow(10, top)); return h - (Math.log(c) / Math.LN10) / top * h; }
  /* An open finding's daily series: time on a log scale from 1 ms, a count from zero. */
  function findSpark(f) {
    var pts = f.series.filter(function (p) { return p[1] !== null; });
    if (!pts.length) return "";
    var W = 300, H = 70, L = 34, R = 6, T0 = 6, B = 16, w = W - L - R, h = H - T0 - B, timed = f.target > 0;
    var peak = pts.reduce(function (m, p) { return p[1] > m[1] ? p : m; }, pts[0]);
    var top = timed ? Math.log(Math.max(3000, peak[1], f.target) * 1.3) / Math.LN10 : Math.max(1, peak[1]);
    var x = function (i) { return L + (f.series.length < 2 ? w / 2 : w * i / (f.series.length - 1)); };
    var y = function (v) { return T0 + (timed ? logY(v, top, h) : h - h * v / top); };
    var path = "", dots = "", run = false;
    f.series.forEach(function (p, i) { if (p[1] === null) { run = false; return; } path += (run ? "L" : "M") + x(i).toFixed(1) + " " + y(p[1]).toFixed(1); run = true; dots += '<circle class="pt" cx="' + x(i).toFixed(1) + '" cy="' + y(p[1]).toFixed(1) + '" r="2.5"><title>' + esc(p[0]) + ": " + esc(timed ? ms(p[1]) : num(p[1])) + '</title></circle>'; });
    var lab = timed ? [[1, "1 ms"], [1000, "1 s"]] : [[0, "0"], [top, num(top)]];
    var svg = '<svg class="fspark" viewBox="0 0 ' + W + ' ' + H + '" role="img" aria-label="' + esc(f.title) + '">' +
      '<line class="ax" x1="' + L + '" x2="' + (W - R) + '" y1="' + (T0 + h) + '" y2="' + (T0 + h) + '"/>' +
      lab.map(function (l) { return '<text x="' + (L - 4) + '" y="' + (y(l[0]) + 3).toFixed(1) + '" text-anchor="end">' + l[1] + '</text>'; }).join("") +
      (timed ? '<line class="tg" x1="' + L + '" x2="' + (W - R) + '" y1="' + y(f.target).toFixed(1) + '" y2="' + y(f.target).toFixed(1) + '"/>' : "") +
      '<path class="ln" d="' + path + '"/>' + dots +
      '<text x="' + L + '" y="' + (H - 3) + '">' + esc(f.series[0][0]) + '</text><text x="' + (W - R) + '" y="' + (H - 3) + '" text-anchor="end">' + esc(f.series[f.series.length - 1][0]) + '</text></svg>';
    var first = pts[0], last = pts[pts.length - 1], v = function (p) { return (timed ? ms(p[1]) : num(p[1])) + " (" + p[0] + ")"; };
    var cap = (timed ? "Daily p95, log scale from 1 ms" : "Calls cut off each day, from zero") + ": first " + v(first) + ", peak " + v(peak) + ", last " + v(last) + "." +
      (timed ? " Dashed line: the " + ms(f.target) + " " + (f.kind === "search-slow" ? "target" : "budget") + "." : "");
    return '<figure>' + svg + '<figcaption>' + esc(cap) + '</figcaption></figure>';
  }
  function tableSpark(r, days) {
    var W = 168, H = 28, p = 2, w = W - 2 * p, h = H - 2 * p, at = {};
    r.days.forEach(function (d) { at[d[0]] = d[1]; });
    var x = function (i) { return p + (days.length < 2 ? w / 2 : w * i / (days.length - 1)); }, y = function (v) { return p + logY(v, LOG_TOP, h); };
    var path = "", dots = "", run = false;
    days.forEach(function (d, i) {
      var v = at[d];
      if (v === undefined || v === null) { run = false; return; }
      path += (run ? "L" : "M") + x(i).toFixed(1) + " " + y(v).toFixed(1); run = true;
      dots += v > TOP_MS ? '<path class="over" d="M' + (x(i) - 4).toFixed(1) + ' ' + (p + 5) + 'L' + (x(i) + 4).toFixed(1) + ' ' + (p + 5) + 'L' + x(i).toFixed(1) + ' 0Z"><title>' + esc(d) + ": " + esc(ms(v)) + ', above the top of the scale</title></path>'
        : '<circle class="pt" cx="' + x(i).toFixed(1) + '" cy="' + y(v).toFixed(1) + '" r="1.8"><title>' + esc(d) + ": " + esc(ms(v)) + '</title></circle>';
    });
    var limit = r.name === "search" ? D.find.target : r.budget;
    var tg = limit && limit <= TOP_MS ? '<line class="tg" x1="' + p + '" x2="' + (W - p) + '" y1="' + y(limit).toFixed(1) + '" y2="' + y(limit).toFixed(1) + '"/>' : "";
    return '<svg class="tspark" viewBox="0 0 ' + W + ' ' + H + '" role="img" aria-label="Daily p95 of ' + esc(r.name) + ' on a log scale from 1 ms to ' + ms(TOP_MS) + '">' + tg + '<path class="ln" d="' + path + '"/>' + dots + '</svg>';
  }
  function renderFindings() {
    var F = D.find, open = F.open, n = open.length;
    document.getElementById("fhead").textContent = n ? countWord(n) + " problem" + (n === 1 ? "" : "s") + "." : "No problems.";
    var scan = F.scan < 0 ? "no scan has run yet" : "the hourly scan last ran " + mins(F.scan);
    document.getElementById("flede").textContent = n ? "ctx reports only what it is fairly sure of. A problem closes by itself after three days without it; " + scan + "." :
      "No hook was over its budget and search was not over its target on two of the last three days; " + scan + ".";
    var host = document.getElementById("f-open"); host.innerHTML = "";
    open.forEach(function (f) {
      var since = (f.kind === "scan-late" ? " Late since " : " Open since ") + f.changed.slice(0, 10) + ", " + howLong(f.age) + "." +
        (f.reopened ? " It had closed and came back; reopened " + plural(f.reopened, "time") + ", first seen " + f.first.slice(0, 10) + "." : "");
      var a = el("article", "finding");
      a.appendChild(el("h3", "", esc(f.title)));
      a.appendChild(el("div", "", '<span class="ev">' + esc(f.evidence + since) + '</span><span class="next">Suggested next step: ' + cmdText(f.next) + '</span>'));
      a.appendChild(el("div", "", findSpark(f)));
      host.appendChild(a);
    });
    var days = F.from ? span(F.from, F.to) : [], t = document.getElementById("f-table");
    if (!F.rows.length) t.innerHTML = '<tbody><tr><td>No daily figures yet: the first are written once a UTC day with calls is over.</td></tr></tbody>';
    else {
      var head = '<thead><tr><th>Hook or search</th><th class="n">Calls, ' + plural(days.length, "day") + '</th><th>Daily p95, log scale 1 ms to ' + ms(TOP_MS) + '</th><th>Range of daily p95</th><th>Last day\u2019s p95</th><th class="n">Share of budget</th></tr></thead>';
      t.innerHTML = head + '<tbody>' + F.rows.map(function (r) {
        var vals = r.days.filter(function (d) { return d[1] !== null; }), calls = r.days.reduce(function (s, d) { return s + d[2]; }, 0);
        var lo = vals.length ? Math.min.apply(null, vals.map(function (d) { return d[1]; })) : null, hi = vals.length ? Math.max.apply(null, vals.map(function (d) { return d[1]; })) : null;
        var last = vals.length ? vals[vals.length - 1] : null, limit = r.name === "search" ? D.find.target : r.budget;
        var share = !last ? "" : limit ? Math.round(100 * last[1] / limit) + "% of " + (r.name === "search" ? "the " + ms(limit) + " target" : ms(limit)) : "no budget";
        return '<tr><td>' + esc(r.name) + '</td><td class="n">' + num(calls) + '</td><td>' + tableSpark(r, days) + '</td><td>' + (vals.length ? esc(ms(lo) + " to " + ms(hi) + (hi > TOP_MS ? ", over the top of the scale" : "")) : "no calls ended") +
          '</td><td>' + (last ? esc(ms(last[1]) + " on " + last[0]) : "") + '</td><td class="n">' + esc(share) + '</td></tr>';
      }).join("") + '</tbody>';
    }
    var w = document.getElementById("f-watch");
    w.hidden = !F.watch.length;
    w.textContent = F.watch.length ? "Also being watched, below confidence: " + F.watch.map(function (f) { return f.title + " (since " + f.changed.slice(0, 10) + ")"; }).join("; ") + "." : "";
    document.getElementById("f-foot").innerHTML = "Source: the call log beside the archive and its daily summaries" + (F.from ? ", UTC days " + esc(F.from) + " to " + esc(F.to) + " (" + plural(days.length, "day") + "), " + plural(F.calls, "call") : ", no full day yet") +
      "; ctx " + esc(F.version) + ". p95 is the time within which 19 calls in 20 finished that day. Budgets are the timeouts Claude Code gives each hook; search has a " + ms(F.target) +
      " target, not a limit. The sparklines share one log scale from 1 ms to " + ms(TOP_MS) + ", so rows compare; the dashed line is the budget or target, and a value over the top is a filled triangle at the top edge. Run <code>ctx doctor</code> for the same findings in the terminal.";
  }
  renderFindings();

  var T = D.tot, sugg = D.held.filter(change).length + D.hooks.filter(change).length;
  document.getElementById("kicker").textContent = "ctx · " + (D.scope || "decisions") + " · your archive as of " + nice(T.today);
  document.getElementById("scope").hidden = !D.scope;
  document.getElementById("head").textContent = D.head;
  document.getElementById("batch").textContent = D.batch;
  document.getElementById("batch").hidden = !D.batch;
  document.getElementById("status-n").textContent = "· " + plural(D.held.length, "project") + (D.scope ? "" : " · " + plural(D.hooks.length, "hook")) + " · " + plural(D.weeks.length, "week") + "; " + (sugg ? plural(sugg, "change") + " suggested" : "no changes suggested");
  renderNeed(); renderBatch(); renderSame(); renderAuto(); render(BY.held); renderTickets(); rememberFolds(); totals(); armReload();
  if (D.scope) document.getElementById("s-hooks").innerHTML = '<p class="note">Hooks run on the whole machine. <code>ctx page --all</code> shows them.</p>';
  else render(BY.hooks);
})();
</script>
</body>
</html>