@polycode-projects/the-mechanical-code-talker 6.0.1 → 6.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -540,6 +540,34 @@ function sparkCaptionHtml(stats) {
540
540
  : "cumulative facts, teach order";
541
541
  }
542
542
 
543
+ /**
544
+ * The page's backend mode from its own query string: "aws" when
545
+ * `backend=aws` is present, "local" for anything else — absent, empty, or
546
+ * any other value, silently. Mode is a boot choice (the slider rewrites the
547
+ * URL and reloads rather than switching a live session), so this is the one
548
+ * place either mode is ever decided.
549
+ *
550
+ * Self-contained (no outer refs), `.toString()`-splice safe.
551
+ */
552
+ export function resolveBackendMode(search) {
553
+ return new URLSearchParams(String(search || "")).get("backend") === "aws" ? "aws" : "local";
554
+ }
555
+
556
+ /**
557
+ * The current page address with its `backend` query parameter rewritten to
558
+ * match `mode` — "aws" written explicitly, anything else removed rather than
559
+ * written out as "local", so a plain reload never carries a redundant param.
560
+ * Every other query parameter and the hash survive untouched.
561
+ *
562
+ * Self-contained (no outer refs), `.toString()`-splice safe.
563
+ */
564
+ export function backendModeUrl(pageUrl, mode) {
565
+ const url = new URL(String(pageUrl));
566
+ if (mode === "aws") url.searchParams.set("backend", "aws");
567
+ else url.searchParams.delete("backend");
568
+ return url.toString();
569
+ }
570
+
543
571
  /** One complete, self-contained document: the ledger, segment rail,
544
572
  * worth-a-look panel, breadcrumb/search, two-hop minimap, and (when the
545
573
  * memory-ask bundle is present) the ask-the-graph chat dock, all over the
@@ -585,7 +613,15 @@ export function renderLedgerHtml({ rows, terms, edges, focus, contradictions, wo
585
613
  // session store, so they appear only where that bundle is offered — the
586
614
  // demo site, never the CLI's self-contained tmct viz page.
587
615
  const ingestHtml = ledgerBundleAvailable
588
- ? `<div class="docktools">
616
+ ? `<div class="backendrow" title="local: everything taught here lives in this browser tab only, and nothing survives a reload. AWS: taught facts save to an anonymous server-side session that expires after seven days, and reload where you left off. Switching reloads the page — a session is never moved between modes.">
617
+ <fieldset class="backendMode" id="backendMode">
618
+ <legend class="mono">backend</legend>
619
+ <label><input type="radio" name="backendMode" id="backendLocal" value="local" checked> local</label>
620
+ <label><input type="radio" name="backendMode" id="backendAws" value="aws"> AWS</label>
621
+ </fieldset>
622
+ <p class="backendnote mono" id="backendNote"></p>
623
+ </div>
624
+ <div class="docktools">
589
625
  <button type="button" id="ingestToggle" class="dockbtn">ingest text&hellip;</button>
590
626
  <button type="button" id="exportFacts" class="dockbtn">export facts</button>
591
627
  </div>
@@ -764,6 +800,15 @@ ${DASH_DARK_CHROME_CSS}
764
800
  .chatask input { flex: 1; font-family: ${MONO_STACK}; font-size: .78rem; background: var(--bg); color: var(--ink); border: 1px solid var(--line); border-radius: 6px; padding: .32rem .6rem; min-width: 0; }
765
801
  .chatask input:disabled { opacity: .6; }
766
802
  .chatnote { font-family: ${MONO_STACK}; font-size: .72rem; color: var(--muted); margin: 0; }
803
+ /* local (this tab only) or AWS (an anonymous server-side session) — a boot
804
+ choice, so picking one reloads the page rather than switching a live
805
+ session. backendNote states whichever mode's promise is actually live. */
806
+ .backendrow { display: flex; align-items: center; gap: .6rem; flex-wrap: wrap; margin-bottom: .55rem; }
807
+ fieldset.backendMode { border: none; margin: 0; padding: 0; display: inline-flex; align-items: center; gap: .5rem; font-size: .68rem; color: var(--muted); }
808
+ fieldset.backendMode legend { padding: 0; margin-right: .1rem; }
809
+ fieldset.backendMode label { display: inline-flex; align-items: center; gap: .22rem; cursor: pointer; white-space: nowrap; font-size: .72rem; }
810
+ fieldset.backendMode input[type="radio"] { margin: 0; accent-color: var(--corpus); }
811
+ .backendnote { font-size: .68rem; color: var(--muted); margin: 0; }
767
812
  .docktools { display: flex; gap: .5rem; margin-top: .55rem; }
768
813
  .dockbtn { font-family: ${MONO_STACK}; font-size: .68rem; color: var(--muted); border: 1px solid var(--line); border-radius: 6px; padding: .22rem .6rem; background: var(--bg); cursor: pointer; }
769
814
  .dockbtn:hover { color: var(--ink); border-color: var(--ink); }
@@ -847,6 +892,8 @@ ${ledgerBundleAvailable ? `<script src="./ledger-browser.bundle.js"></script>` :
847
892
  if ("serviceWorker" in navigator) navigator.serviceWorker.register("./tmct-sw.js").catch(() => {});
848
893
  const DAY = 86400000;
849
894
  const facetCounts = ${facetCounts.toString()};
895
+ const resolveBackendMode = ${resolveBackendMode.toString()};
896
+ const backendModeUrl = ${backendModeUrl.toString()};
850
897
  const el = (id) => document.getElementById(id);
851
898
  const esc = ${escapeHtml.toString()};
852
899
  // Aliased so the dashboard/sparkline builders below (toString-embedded
@@ -1167,6 +1214,44 @@ ${ledgerBundleAvailable ? `<script src="./ledger-browser.bundle.js"></script>` :
1167
1214
  return d;
1168
1215
  };
1169
1216
 
1217
+ // The backend mode is a boot choice, decided once from the URL this page
1218
+ // was loaded with — never switched under a live session. The radios only
1219
+ // ever reflect that choice and, on a change, rewrite the URL and reload.
1220
+ const backendModeFieldset = el("backendMode");
1221
+ const backendModeRadios = Array.prototype.slice.call(backendModeFieldset.querySelectorAll('input[type="radio"]'));
1222
+ const backendMode = resolveBackendMode(location.search);
1223
+ for (const radio of backendModeRadios) radio.checked = radio.value === backendMode;
1224
+ backendModeFieldset.addEventListener("change", function () {
1225
+ const chosen = backendModeRadios.find((r) => r.checked);
1226
+ location.href = backendModeUrl(location.href, chosen ? chosen.value : "local");
1227
+ });
1228
+ const backendNoteEl = el("backendNote");
1229
+ backendNoteEl.textContent = backendMode === "aws"
1230
+ ? "taught facts save to an anonymous server-side session that expires after seven days."
1231
+ : "taught facts stay in this browser tab only, and are lost on reload.";
1232
+
1233
+ // AWS mode's one local item: a session pointer, never facts. Resolved
1234
+ // once, eagerly, so the FIRST dock turn (not just a later one) already
1235
+ // knows whether to bind the session's memory to the row service.
1236
+ const AWS_SESSION_KEY = "tmct.ledger.sessionKey";
1237
+ function mintAwsSessionKey() {
1238
+ const key = crypto.randomUUID();
1239
+ try { localStorage.setItem(AWS_SESSION_KEY, key); } catch { /* private mode — this visit still works, just not restorable */ }
1240
+ return key;
1241
+ }
1242
+ let awsSessionKey = null;
1243
+ if (backendMode === "aws") {
1244
+ let stored = null;
1245
+ try { stored = localStorage.getItem(AWS_SESSION_KEY); } catch { stored = null; }
1246
+ awsSessionKey = stored || mintAwsSessionKey();
1247
+ }
1248
+
1249
+ // A row-backed session's memory has no .payload of its own (the
1250
+ // in-memory store's shape) — its current state lives in .cachedPayload,
1251
+ // kept fresh in place after every turn. .impl is present on a row
1252
+ // handle alone, so it doubles as the branch test.
1253
+ const payloadOf = (memoryDir) => (memoryDir.impl ? memoryDir.cachedPayload : memoryDir.payload);
1254
+
1170
1255
  let session = null;
1171
1256
  // Serializes every engine-touching call through this one dock — the same
1172
1257
  // posture plan-viz.mjs's own chat-assert dock takes with its withLock,
@@ -1204,11 +1289,11 @@ ${ledgerBundleAvailable ? `<script src="./ledger-browser.bundle.js"></script>` :
1204
1289
  async function ensureSession() {
1205
1290
  if (session) return session;
1206
1291
  await tryLoadWink();
1207
- session = await tmct.open({ seedPayload: PAYLOAD });
1292
+ session = await tmct.open({ seedPayload: PAYLOAD, awsSessionKey: awsSessionKey || undefined });
1208
1293
  // From here the live store is the source of truth for the digest, so a
1209
1294
  // digest of a term taught this session reads its fresh facts — the store
1210
1295
  // grows in place, so this one closure stays current.
1211
- getLivePayload = () => session.memoryDir.payload;
1296
+ getLivePayload = () => payloadOf(session.memoryDir);
1212
1297
  return session;
1213
1298
  }
1214
1299
 
@@ -1230,7 +1315,7 @@ ${ledgerBundleAvailable ? `<script src="./ledger-browser.bundle.js"></script>` :
1230
1315
  pending.className = "a" + (taught ? " taught" : (record && record.miss ? " miss" : ""));
1231
1316
  pending.innerHTML = taught ? '<span class="tag">taught</span>' + body : body;
1232
1317
  if (taught) {
1233
- const fresh = tmct.page.computeLedgerDataFromPayload(live.memoryDir.payload, {});
1318
+ const fresh = tmct.page.computeLedgerDataFromPayload(payloadOf(live.memoryDir), {});
1234
1319
  applyLedgerData(fresh);
1235
1320
  } else if (!(record && record.miss)) {
1236
1321
  // Only a genuine answer (never a miss) tries to resolve a
@@ -1298,7 +1383,7 @@ ${ledgerBundleAvailable ? `<script src="./ledger-browser.bundle.js"></script>` :
1298
1383
  const r = await tmct.turn(sentence);
1299
1384
  if (r.record && r.record.miss === false && r.record.via === "assert") grounded += 1;
1300
1385
  }
1301
- const fresh = tmct.page.computeLedgerDataFromPayload(s.memoryDir.payload, {});
1386
+ const fresh = tmct.page.computeLedgerDataFromPayload(payloadOf(s.memoryDir), {});
1302
1387
  applyLedgerData(fresh);
1303
1388
  const skipped = sentences.length - grounded;
1304
1389
  statusEl.textContent = countLabel(sentences.length, "sentence")
@@ -1373,7 +1458,7 @@ ${ledgerBundleAvailable ? `<script src="./ledger-browser.bundle.js"></script>` :
1373
1458
  researchQueue = result.research;
1374
1459
  renderResearchControls();
1375
1460
  if (!missed) {
1376
- const fresh = tmct.page.computeLedgerDataFromPayload(live.memoryDir.payload, {});
1461
+ const fresh = tmct.page.computeLedgerDataFromPayload(payloadOf(live.memoryDir), {});
1377
1462
  applyLedgerData(fresh);
1378
1463
  }
1379
1464
  }
@@ -164,8 +164,9 @@ export async function loadSeedPayload(fetcher, url, seedQuery, onProgress) {
164
164
  * opts.onForget when set, renders a "forget everything" button wired
165
165
  * to this callback; omitted, no button renders (a page
166
166
  * with no persistence to forget).
167
- * opts.persistNote when set (and onForget is set), a short note under the
168
- * forget button naming where the state lives.
167
+ * opts.persistNote when set, a short note naming where taught state
168
+ * lives (or that it isn't being saved), with or
169
+ * without the forget button above it.
169
170
  */
170
171
  export function renderStatsPanelInto(panelEl, stats, opts) {
171
172
  const { bandLabel, taughtHint, onForget = null, persistNote = null } = opts;
@@ -222,12 +223,15 @@ export function renderStatsPanelInto(panelEl, stats, opts) {
222
223
  forget.title = "clear what this device has saved and restart from the fresh seed";
223
224
  forget.addEventListener("click", onForget);
224
225
  panelEl.appendChild(forget);
225
- if (persistNote) {
226
- const note = document.createElement("p");
227
- note.className = "persist-note";
228
- note.textContent = persistNote;
229
- panelEl.appendChild(note);
230
- }
226
+ }
227
+ // The note stands on its own: a visit with nothing to forget (an
228
+ // unreachable service, an unpersisted mode) is exactly when the reader
229
+ // most needs to be told where taught facts are (not) going.
230
+ if (persistNote) {
231
+ const note = document.createElement("p");
232
+ note.className = "persist-note";
233
+ note.textContent = persistNote;
234
+ panelEl.appendChild(note);
231
235
  }
232
236
  }
233
237