@polycode-projects/the-mechanical-code-talker 5.0.28 → 5.0.29

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.
@@ -18,16 +18,21 @@ import {
18
18
  agentKindOf, liveIdsOfKind, layoutNamed, isFoodId,
19
19
  } from "../domain/town-square-world.mjs";
20
20
  import {
21
- MUDIII_ROLES, foldTownSquareState, startTownSquareGame, runTownSquareTick,
21
+ MUDIII_ROLES, foldTownSquareState, startTownSquareGame, runTownSquareTick, planTownSquareTurn,
22
22
  placeFood, roleOfId, beliefSnapshotFor,
23
23
  } from "./predator-prey.mjs";
24
24
  import { snapshotSubject, parseSnapshotSubject } from "./adventure.mjs";
25
25
  import { correctMisspellings, QUESTION_LEAD_RE } from "../domain/interpret/normalize.mjs";
26
26
  import { worldProvenanceTag } from "../domain/worlds-pack.mjs";
27
27
  import { getWorldsPackProvider } from "../adapters/corpus/worlds-pack.mjs";
28
- import { appendFacts, appendRule, loadMemory, readFactRows } from "../adapters/memory/core.mjs";
28
+ import { appendFacts, appendRule, loadMemory, readFactRows, readRuleRows } from "../adapters/memory/core.mjs";
29
29
  import { DEFAULT_GAME_CONFIG } from "../domain/game-config.mjs";
30
- import { agentTraitsOf } from "../domain/agent-traits.mjs";
30
+ import { agentTraitsOf, constraintsFromDrives } from "../domain/agent-traits.mjs";
31
+ import { beliefOriginOf } from "../domain/agent-belief.mjs";
32
+ import {
33
+ compileDomain, stateFromFacts, stateKeyFor, movesFromRules, compileGoal, PlanBudgetError,
34
+ } from "../domain/domain.mjs";
35
+ import { findActionPath } from "../domain/planning.mjs";
31
36
 
32
37
  const PREDATOR_KIND = MUDIII_ROLES.predator.kind; // "fox"
33
38
  const PREY_KIND = MUDIII_ROLES.prey.kind; // "goblin"
@@ -522,6 +527,112 @@ async function mudiiiBeliefAnswer(match, { memoryDir, gameConfig = DEFAULT_GAME_
522
527
  };
523
528
  }
524
529
 
530
+ // ---- beliefs and plans, recomputed live, no turn spent ----------------------
531
+
532
+ // The one puzzle this plan ships (PLAN_RIVER_CROSSING.md's R3): a world that
533
+ // carries no "ferry onto" action-signature has no puzzle plan to compute, so
534
+ // riverPuzzleOutlook stays null on the town square and any other board with
535
+ // no ferry action rule.
536
+ const RIVER_CROSSING_ACTION = "ferry onto";
537
+ const RIVER_CROSSING_GOAL = [{ universal: true, term: "passenger", predicate: "currently-in", object: "bank-west" }];
538
+
539
+ /** The river-crossing puzzle's own plan, recomputed from `rows`/`ruleRows` as
540
+ * they stand right now: the world's own action rules plus whatever
541
+ * action-constraint rows the drive facts themselves imply
542
+ * (constraintsFromDrives), searched the same way the chat lane solves
543
+ * data/games/river.txt. Null on a world whose rule rows carry no "ferry
544
+ * onto" action-signature, or whose action domain does not carry a
545
+ * "passenger" class the goal can quantify over. Returns `{ moves, plan,
546
+ * miss }`: `moves` is every legal move from the CURRENT position (the empty
547
+ * list when nothing may move), `plan` is the found action sequence or null,
548
+ * `miss` is null when a plan was found and the stated reason otherwise — a
549
+ * budget decline as well as an exhausted search, never a shortened plan
550
+ * either way. Pure. */
551
+ function riverPuzzleOutlook(rows, ruleRows) {
552
+ const hasFerryAction = (ruleRows || []).some((r) => r.kind === "action-signature" && r.name === RIVER_CROSSING_ACTION);
553
+ if (!hasFerryAction) return null;
554
+ const derived = constraintsFromDrives(rows).map((slots) => ({ name: RIVER_CROSSING_ACTION, kind: "action-constraint", slots }));
555
+ const domain = compileDomain(rows, [...(ruleRows || []), ...derived]);
556
+ let isGoal;
557
+ try {
558
+ isGoal = compileGoal(RIVER_CROSSING_GOAL, domain);
559
+ } catch {
560
+ return null; // this board's action domain is not the river puzzle's own shape
561
+ }
562
+ const start = stateFromFacts(rows, domain);
563
+ const moves = movesFromRules(start, domain).map((m) => m.action.label);
564
+ const maxDepth = DEFAULT_GAME_CONFIG.planning.maxDepth;
565
+ try {
566
+ const found = findActionPath(start, isGoal, (s) => movesFromRules(s, domain), { maxDepth, stateKey: stateKeyFor });
567
+ return { moves, plan: found ? found.actions : null, miss: found ? null : `no plan found within ${maxDepth} moves` };
568
+ } catch (err) {
569
+ if (!(err instanceof PlanBudgetError)) throw err;
570
+ return { moves, plan: null, miss: `action grounding count ${err.groundings} exceeds the budget of ${err.budget}` };
571
+ }
572
+ }
573
+
574
+ /** Every live agent's belief map and plan, computed from the store as it
575
+ * stands right now, without writing a fact or advancing a turn. Runs the
576
+ * SAME derivation runTownSquareTick's own decision phase performs
577
+ * (predator-prey.mjs's planTownSquareTurn) and reads only the answers — the
578
+ * rung, the goal line, the belief snapshot and the plan each live agent
579
+ * would carry into its NEXT tick — never a tick's effects (no eating, no
580
+ * spawn, no mass debit, no write).
581
+ *
582
+ * `layout` is optional here (unlike runTownSquareTick, which requires one):
583
+ * a board with no town-square roster to decide over — the puzzle world
584
+ * before R6 wires it to a layout, or a caller asking only about `puzzle` —
585
+ * still gets its `puzzle` field computed, with `agents` empty rather than a
586
+ * thrown "no such layout".
587
+ *
588
+ * Returns `{ turn, agents: { id: { rung, goal, belief, beliefOrigin, plan,
589
+ * traits } }, puzzle: { moves, plan, miss } | null }`. `beliefOrigin` mirrors
590
+ * `belief`'s own key set, each value `"seen"`, `{ told: turn }` or null —
591
+ * `beliefOriginOf` walking the exact cell/candidates/opts the tick's own
592
+ * belief snapshot was taken with (planTownSquareTurn's `beliefContext`), so
593
+ * a panel's cell and its "how it knows" label can never disagree.
594
+ *
595
+ * Deterministic: a pure function of the store's current rows (and
596
+ * `toldFacts`), whatever order either arrived in. Two calls with no edit
597
+ * between them, or the same rows fed in twice in a different order, return
598
+ * deep-equal results. */
599
+ export async function agentOutlook(memoryDir, {
600
+ layout = null, toldFacts = [], config = DEFAULT_GAME_CONFIG.mudiii, roles = MUDIII_ROLES,
601
+ } = {}) {
602
+ const memory = await loadMemory(memoryDir);
603
+ const rows = readFactRows(memory);
604
+ const ruleRows = readRuleRows(memory);
605
+ const state = foldTownSquareState(rows);
606
+
607
+ const outlookAgents = {};
608
+ if (layout) {
609
+ const { agents, rungs, beliefContext, traitRows } =
610
+ planTownSquareTurn(rows, state, { layout, config, roles, toldFacts, manualMoves: {} });
611
+ for (const id of Object.keys(agents).sort()) {
612
+ const ctx = beliefContext[id];
613
+ const beliefOrigin = {};
614
+ for (const candidateId of ctx.candidates) {
615
+ if (candidateId === id) continue;
616
+ beliefOrigin[candidateId] = beliefOriginOf(candidateId, id, ctx.cell, state, ctx.opts);
617
+ }
618
+ for (const candidateId of ctx.foodCandidates) {
619
+ if (candidateId === id) continue;
620
+ beliefOrigin[candidateId] = beliefOriginOf(candidateId, id, ctx.cell, state, ctx.foodOpts);
621
+ }
622
+ outlookAgents[id] = {
623
+ rung: rungs[id],
624
+ goal: agents[id].goal,
625
+ belief: agents[id].belief,
626
+ beliefOrigin,
627
+ plan: agents[id].plan,
628
+ traits: agentTraitsOf(traitRows, id),
629
+ };
630
+ }
631
+ }
632
+
633
+ return { turn: state.tickCount, agents: outlookAgents, puzzle: riverPuzzleOutlook(rows, ruleRows) };
634
+ }
635
+
525
636
  function noLivePlacedSubjectAnswer(kind) {
526
637
  return {
527
638
  text: `there's no live ${kind} on the board for that to be about.`,
@@ -727,6 +727,15 @@ ${RING_POINTS.map((point) => ` <span class="dir-slot dir-${point}"><button
727
727
  <div class="chatpills" id="actorEditorPills" aria-label="related words for the term before the cursor"></div>
728
728
  <p class="edit-status" id="actorEditorStatus" role="status"></p>
729
729
  </section>
730
+ <section class="outlook-panel" id="mudiiiOutlookPanel" aria-label="the followed actor's own beliefs and plan">
731
+ <h2>what it believes, and its plan</h2>
732
+ <table class="outlook-belief-table" id="outlookBeliefTable" aria-label="what the followed actor currently believes">
733
+ <caption>beliefs</caption>
734
+ <tbody id="outlookBeliefBody"></tbody>
735
+ </table>
736
+ <p class="outlook-plan" id="outlookPlanText" role="status"></p>
737
+ <p class="outlook-plan outlook-puzzle-plan" id="outlookPuzzlePlanText" role="status" hidden></p>
738
+ </section>
730
739
  <section class="mudiii-chat" aria-label="talk to the square">
731
740
  <div class="chatlog" id="chatLog" aria-live="polite"></div>
732
741
  <div class="log-popup" id="chatLogPopup" role="dialog" aria-label="the whole reply" hidden>
@@ -991,6 +1000,18 @@ const MUDIII_STYLE = `
991
1000
  }
992
1001
  #actorEditorText:disabled { opacity: .55; }
993
1002
 
1003
+ .outlook-panel {
1004
+ display: none; flex-direction: column; gap: .45rem; margin-bottom: 1rem;
1005
+ background: var(--parchment); border: 1px solid var(--square-stone-dark); border-radius: 4px;
1006
+ box-shadow: 0 2px 0 rgba(0,0,0,.18); padding: .7rem .8rem; min-width: 0;
1007
+ }
1008
+ body.editing .outlook-panel { display: flex; }
1009
+ .outlook-panel h2 { margin: 0; font-size: .8rem; }
1010
+ .outlook-belief-table { width: 100%; border-collapse: collapse; font-family: ${MONO_STACK}; font-size: .68rem; }
1011
+ .outlook-belief-table caption { caption-side: top; text-align: left; font-size: .62rem; color: var(--square-stone-dark); padding-bottom: .2rem; }
1012
+ .outlook-belief-table td { padding: .12rem .4rem .12rem 0; vertical-align: top; }
1013
+ .outlook-plan { margin: 0; font-family: ${MONO_STACK}; font-size: .68rem; white-space: pre-wrap; color: var(--square-ink); }
1014
+
994
1015
  .mudiii-chat { background: var(--parchment); border: 1px solid var(--square-stone-dark); border-radius: 4px; box-shadow: 0 2px 0 rgba(0,0,0,.18); padding: .7rem .8rem; display: flex; flex-direction: column; gap: .5rem; }
995
1016
  .chatlog { min-height: 8rem; max-height: 16rem; overflow-y: auto; }
996
1017
  .chatlog > * { margin: 0 0 .32rem; }
@@ -1602,7 +1623,7 @@ function pageScript() {
1602
1623
  camera = { mode: mode, selectedId: id, status: null };
1603
1624
  renderCameraButtons();
1604
1625
  renderDriveRing();
1605
- if (editing) renderActorEditor();
1626
+ if (editing) { renderActorEditor(); renderOutlookPanel(); }
1606
1627
  sendCameraToScene(camera);
1607
1628
  });
1608
1629
 
@@ -1764,6 +1785,7 @@ function pageScript() {
1764
1785
  renderSuggestionPills();
1765
1786
  renderEditPlacements();
1766
1787
  renderActorEditor();
1788
+ await refreshOutlook();
1767
1789
  }
1768
1790
 
1769
1791
  function exitEditMode() {
@@ -1864,6 +1886,7 @@ function pageScript() {
1864
1886
  : "synced \\u2014 no change.";
1865
1887
  }
1866
1888
  renderEditPlacements();
1889
+ await refreshOutlook();
1867
1890
  }
1868
1891
 
1869
1892
  // ---- actor card (per-instance edit) -------------------------------------
@@ -2009,6 +2032,66 @@ function pageScript() {
2009
2032
  ? "synced \\u2014 " + result.added + " fact(s) written, " + result.removed + " retracted."
2010
2033
  : "synced \\u2014 no change.";
2011
2034
  }
2035
+ await refreshOutlook();
2036
+ }
2037
+
2038
+ // ---- belief and plan panels: recomputed live, no turn spent ------------
2039
+ // agentOutlook (mudiii-turn.mjs, reached through session.outlook()) is a
2040
+ // pure read over the store as it stands — same rows, same outlook, byte
2041
+ // for byte, whatever order they arrived in. Refreshed on boot and after
2042
+ // every edit (world or actor); the follow select alone just re-renders the
2043
+ // already-fetched outlook for whichever agent it now names, no new fetch.
2044
+ let lastOutlook = null;
2045
+
2046
+ async function refreshOutlook() {
2047
+ if (!session || !session.outlook) return;
2048
+ lastOutlook = await session.outlook();
2049
+ renderOutlookPanel();
2050
+ }
2051
+
2052
+ function beliefOriginText(cell, origin) {
2053
+ if (!cell) return "nothing seen, nothing told";
2054
+ if (origin === "seen") return "seen";
2055
+ if (origin && typeof origin === "object" && "told" in origin) return "told, turn " + origin.told;
2056
+ return "seen";
2057
+ }
2058
+
2059
+ function renderOutlookPanel() {
2060
+ const id = el("agentSelect").value;
2061
+ const body = el("outlookBeliefBody");
2062
+ const planText = el("outlookPlanText");
2063
+ const puzzleText = el("outlookPuzzlePlanText");
2064
+ const entry = (lastOutlook && id) ? lastOutlook.agents[id] : null;
2065
+ if (!entry) {
2066
+ body.innerHTML = '<tr><td class="outlook-empty" colspan="3">Follow an agent above to see what it believes.</td></tr>';
2067
+ planText.textContent = "";
2068
+ puzzleText.hidden = true;
2069
+ return;
2070
+ }
2071
+ const subjects = Object.keys(entry.belief).sort();
2072
+ body.innerHTML = subjects.length
2073
+ ? subjects.map(function (subject) {
2074
+ const cell = entry.belief[subject];
2075
+ const howText = beliefOriginText(cell, entry.beliefOrigin[subject]);
2076
+ return '<tr><td class="mono">' + esc(subject) + '</td><td class="mono">' + esc(cell || "\\u2014") + "</td><td>" + esc(howText) + "</td></tr>";
2077
+ }).join("")
2078
+ : '<tr><td class="outlook-empty" colspan="3">' + esc(id) + " is alone on the board \\u2014 nothing else to believe anything about.</td></tr>";
2079
+ const planLine = entry.plan.length ? entry.plan.join(" \\u2192 ") : "holding";
2080
+ planText.textContent = id + "'s plan (" + entry.rung + "): " + planLine + " \\u2014 " + entry.goal;
2081
+ const puzzle = lastOutlook.puzzle;
2082
+ if (!puzzle) {
2083
+ puzzleText.hidden = true;
2084
+ puzzleText.textContent = "";
2085
+ } else {
2086
+ puzzleText.hidden = false;
2087
+ if (puzzle.plan) {
2088
+ const n = puzzle.plan.length;
2089
+ const moveLines = puzzle.plan.map(function (a, i) { return " " + (i + 1) + ". " + a.label; }).join("\\n");
2090
+ puzzleText.textContent = "puzzle plan \\u2014 found " + n + " move" + (n === 1 ? "" : "s") + " (shortest):\\n" + moveLines;
2091
+ } else {
2092
+ puzzleText.textContent = "puzzle plan \\u2014 " + puzzle.miss;
2093
+ }
2094
+ }
2012
2095
  }
2013
2096
 
2014
2097
  // ---- booting ---------------------------------------------------------
@@ -2086,6 +2169,7 @@ function pageScript() {
2086
2169
  camera.selectedId = openingFollowId(opening.agents);
2087
2170
  applyTickResult(opening);
2088
2171
  renderAll();
2172
+ await refreshOutlook();
2089
2173
  if (!prefersReducedMotion()) {
2090
2174
  autoOn = true;
2091
2175
  ensureTicker().play();
@@ -1157,42 +1157,34 @@ function itemsPayload(liveItemIds, { state, itemCellOf, roles, taken = null }) {
1157
1157
  // ---- one tick --------------------------------------------------------------------
1158
1158
 
1159
1159
  /**
1160
- * One full tick over `layout`: fold, believe, decide, move, run the ecology
1161
- * pass, and append everything as this turn's stamped facts in a single write.
1162
- *
1163
- * Predators move before prey, and that ordering is load-bearing for replay: a
1164
- * prey's belief is computed against the PRE-move predator positions (it reacts
1165
- * to where the predator was when it looked), while eating resolves on the
1166
- * post-move ones (it is caught where the predator actually ends up).
1167
- *
1168
- * `manualMoves` is the one place a visitor's own hand reaches the world:
1169
- * `{ agentId: cellId }`, or `{ agentId: { cell, facing } }`, checked before
1170
- * that agent's belief chain runs. A legal request moves the agent under the
1171
- * `driven` rung; an illegal one is refused and that agent decides for itself
1172
- * this turn, so a rejected press never freezes it. A driven turn spends a
1173
- * turn like any other: the ecology pass runs, and every other agent decides
1174
- * and moves in this same call.
1160
+ * The movement DECISION half of one tick, over `rows`/`state` as they stand
1161
+ * right now: fold nothing further, believe, and decide — for every live
1162
+ * predator and prey, exactly the rung, plan, goal, mood and belief a real
1163
+ * tick would assign it, with none of a tick's EFFECTS (no eating, no spawn,
1164
+ * no mass debit, no write). `runTownSquareTick` runs this and then applies
1165
+ * the effects; `agentOutlook` (mudiii-turn.mjs) runs the SAME derivation and
1166
+ * reads only the answers, so the two can never disagree about what an agent
1167
+ * currently believes or plans.
1175
1168
  *
1176
- * Returns `{ turn, epoch, agents, items, ecology, rungs, activeWebs, writes }`.
1177
- * `agents` and `items` and `ecology` are the frozen render payload — see
1178
- * townSquareTickPayload, which projects exactly those three plus the turn.
1179
- * `rungs` is the decision each live agent reached this turn ("driven" for a
1180
- * hand-driven move, then "chase", "evade", "forage", "wander", and —
1181
- * on a cast that carries or spins webs —
1182
- * "carry", "deliver", "carried", "trapped", "hold-web", "build-web"); an agent
1183
- * that decided and then died still has a rung and no longer has an `agents`
1184
- * entry, which is the difference between a decision and a survivor.
1185
- * `activeWebs` is every unexpired predator-built web, empty on a board with
1186
- * none.
1169
+ * Returns everything either caller needs to keep going: `{ lay, k, epoch,
1170
+ * stamp, agents, rungs, movementWrites, postMovePlacements, tickWebs,
1171
+ * traitRows, traitsOf, predators, prey, liveItemIds, foodIds, itemCellOf,
1172
+ * itemMassOf, beliefContext }`. `agents[id]` carries exactly the shape a tick
1173
+ * payload's own entry does before the ecology pass and mass debit finalise
1174
+ * it — `{ role, cell, facing, goal, mood, plan, mass: 0, belief }` — so
1175
+ * appending fields here would drift the tick's own frozen payload shape.
1176
+ * `beliefContext[id]` is `{ cell, candidates, opts, foodCandidates, foodOpts
1177
+ * }`, the exact inputs this agent's own belief snapshot was taken with —
1178
+ * `agentOutlook`'s own "how it knows" column reads it through
1179
+ * `beliefOriginOf`, off the same walk, so a panel's cell and its origin can
1180
+ * never drift apart either.
1187
1181
  */
1188
- export async function runTownSquareTick(memoryDir, {
1189
- layout, toldFacts = [], config = DEFAULT_GAME_CONFIG.mudiii, roles = MUDIII_ROLES,
1190
- manualMoves = {},
1182
+ export function planTownSquareTurn(rows, state, {
1183
+ layout, config = DEFAULT_GAME_CONFIG.mudiii, roles = MUDIII_ROLES,
1184
+ toldFacts = [], manualMoves = {},
1191
1185
  } = {}) {
1192
1186
  const lay = typeof layout === "string" ? TOWN_SQUARE_LAYOUTS[layout] : layout;
1193
- if (!lay) throw new Error(`runTownSquareTick: no such layout "${layout}"`);
1194
- const rows = readFactRows(await loadMemory(memoryDir));
1195
- const state = foldTownSquareState(rows);
1187
+ if (!lay) throw new Error(`planTownSquareTurn: no such layout "${layout}"`);
1196
1188
  const k = state.tickCount + 1;
1197
1189
  const epoch = state.epoch;
1198
1190
  const stamp = (base) => snapshotSubject(base, k, epoch);
@@ -1204,6 +1196,7 @@ export async function runTownSquareTick(memoryDir, {
1204
1196
  const postMovePlacements = new Map();
1205
1197
  const agents = {};
1206
1198
  const rungs = {};
1199
+ const beliefContext = {};
1207
1200
 
1208
1201
  const carriesPrey = config.carryPreyToWeb === true;
1209
1202
  const buildsWebs = config.buildWebs === true;
@@ -1442,6 +1435,7 @@ export async function runTownSquareTick(memoryDir, {
1442
1435
  }
1443
1436
 
1444
1437
  const belief = beliefSnapshotFor(agentId, beliefCell, beliefCandidates, state, beliefOpts);
1438
+ const foodCandidates = foodVisionGated ? [] : beliefCandidates.filter((id) => foodIds.has(id));
1445
1439
  // beliefSnapshotFor's one call covers every candidate, food included, so
1446
1440
  // an ungated food call needs the food entries re-taken at the widened
1447
1441
  // radius too — otherwise the panel would show a crumb null while the
@@ -1449,9 +1443,12 @@ export async function runTownSquareTick(memoryDir, {
1449
1443
  // only: every key already exists from the call above, so this can never
1450
1444
  // reorder the panel.
1451
1445
  if (!foodVisionGated) {
1452
- const foodCandidates = beliefCandidates.filter((id) => foodIds.has(id));
1453
1446
  Object.assign(belief, beliefSnapshotFor(agentId, beliefCell, foodCandidates, state, foodBeliefOpts));
1454
1447
  }
1448
+ // What a belief panel's own "how it knows" column re-derives per entry —
1449
+ // the exact cell and opts this agent's belief was just taken with, so
1450
+ // `beliefOriginOf` walks the identical rung `belief` did.
1451
+ beliefContext[agentId] = { cell: beliefCell, candidates: beliefCandidates, opts: beliefOpts, foodCandidates, foodOpts: foodBeliefOpts };
1455
1452
  // A hand-picked facing beats the step it came with, which is what lets the
1456
1453
  // page walk an agent backwards without spinning it round.
1457
1454
  const facing = drivenFacing ?? plan[0] ?? restingFacing;
@@ -1464,6 +1461,53 @@ export async function runTownSquareTick(memoryDir, {
1464
1461
  for (const id of predators) decide(id, "predator");
1465
1462
  for (const id of prey) decide(id, "prey");
1466
1463
 
1464
+ return {
1465
+ lay, k, epoch, stamp, agents, rungs, movementWrites, postMovePlacements, tickWebs, traitRows, traitsOf,
1466
+ predators, prey, liveItemIds, foodIds, itemCellOf, itemMassOf, beliefContext,
1467
+ };
1468
+ }
1469
+
1470
+ /**
1471
+ * One full tick over `layout`: fold, believe, decide, move, run the ecology
1472
+ * pass, and append everything as this turn's stamped facts in a single write.
1473
+ *
1474
+ * Predators move before prey, and that ordering is load-bearing for replay: a
1475
+ * prey's belief is computed against the PRE-move predator positions (it reacts
1476
+ * to where the predator was when it looked), while eating resolves on the
1477
+ * post-move ones (it is caught where the predator actually ends up).
1478
+ *
1479
+ * `manualMoves` is the one place a visitor's own hand reaches the world:
1480
+ * `{ agentId: cellId }`, or `{ agentId: { cell, facing } }`, checked before
1481
+ * that agent's belief chain runs. A legal request moves the agent under the
1482
+ * `driven` rung; an illegal one is refused and that agent decides for itself
1483
+ * this turn, so a rejected press never freezes it. A driven turn spends a
1484
+ * turn like any other: the ecology pass runs, and every other agent decides
1485
+ * and moves in this same call.
1486
+ *
1487
+ * Returns `{ turn, epoch, agents, items, ecology, rungs, activeWebs, writes }`.
1488
+ * `agents` and `items` and `ecology` are the frozen render payload — see
1489
+ * townSquareTickPayload, which projects exactly those three plus the turn.
1490
+ * `rungs` is the decision each live agent reached this turn ("driven" for a
1491
+ * hand-driven move, then "chase", "evade", "forage", "wander", and —
1492
+ * on a cast that carries or spins webs —
1493
+ * "carry", "deliver", "carried", "trapped", "hold-web", "build-web"); an agent
1494
+ * that decided and then died still has a rung and no longer has an `agents`
1495
+ * entry, which is the difference between a decision and a survivor.
1496
+ * `activeWebs` is every unexpired predator-built web, empty on a board with
1497
+ * none.
1498
+ */
1499
+ export async function runTownSquareTick(memoryDir, {
1500
+ layout, toldFacts = [], config = DEFAULT_GAME_CONFIG.mudiii, roles = MUDIII_ROLES,
1501
+ manualMoves = {},
1502
+ } = {}) {
1503
+ const rows = readFactRows(await loadMemory(memoryDir));
1504
+ const state = foldTownSquareState(rows);
1505
+ const {
1506
+ lay, k, epoch, stamp, agents, rungs, movementWrites, postMovePlacements, tickWebs, traitRows, traitsOf,
1507
+ predators, prey, liveItemIds, foodIds, itemCellOf, itemMassOf,
1508
+ } = planTownSquareTurn(rows, state, { layout, config, roles, toldFacts, manualMoves });
1509
+ const carriesPrey = config.carryPreyToWeb === true;
1510
+
1467
1511
  const postMoveMass = new Map();
1468
1512
  for (const id of [...predators, ...prey]) {
1469
1513
  const role = agents[id].role;