GLOBALGRID2050 / EXPLORE CODE

Part of: Explore Code / Solar and storage design / globalgrid2050 / solar-design-studio/202609271853-site-world/world

Cmd Model and 2 more files

A JavaScript module in the world folder of GlobalGrid2050 that works with cables and 33kV. It applies set (`applySet`), creates session (`createSession`) and runs layout (`runLayout`).

630 lines of JavaScript from globalgrid2050, in 3 files. Topic: Solar and storage design. Section 179 of 2044.

Cut 2026-12-06T03:07:51+00:00. A copy for reading; the repository at the stated commit is the source.

In the State lines below, domain stage, users and canonical status are rule-based first approximations from code and path evidence (rules explore-agent-rules-v1), not observed facts: the rules.

In this section

  1. Cmd Model transform · globalgrid2050 / solar-design-studio/202609271853-site-world/world/cmd-model.mjs lines 1–350
  2. Cmd Mount render · globalgrid2050 / solar-design-studio/202609271853-site-world/world/cmd-mount.mjs lines 1–63
  3. Cmd Run render · globalgrid2050 / solar-design-studio/202609271853-site-world/world/cmd-run.mjs lines 1–217

Cmd Model

A JavaScript module in the world folder of GlobalGrid2050 that works with cables and 33kV. It applies set (`applySet`), creates session (`createSession`) and runs layout (`runLayout`).

transformglobalgrid2050 / solar-design-studio/202609271853-site-world/world/cmd-model.mjs · lines 1–350 of 350 · version 2026-09-27 18:53 · pattern createSession (shared by 3 files)

What it does
It calculates cables and 33kV. Defines 27 functions or classes, including STRUCT, PACK, moduleOf, tplOpts, f.
Why it exists in the grid workflow
Domain stage sub33kv_design (path word 'design'): inside-the-fence electrical and civil design below 33kV, which Ventus Ltd states is within its commercial execution boundary. Domain terms in the code: cables, 33kV, 1500V.
Inputs & outputs
Voltage levels named: 33kV, 1500V.
Downstream impact
Used directly by 2 files: Cmd Run (globalgrid2050); Cmd UI (globalgrid2050). Reached from 2 other entry points, including Solar Design Studio — Site World Page (globalgrid2050, https://globalgrid2050.com/solar-design-studio/202609271853-site-world/); Substrate (globalgrid2050).
What it achieves
The design as typed. A session holds the typed values, runs each command through an engine (the page's plant, block and layers, or a plain CPU layout in tests), echoes what changed in words, keeps an undo stack. Its code works with 33 kV.
Why it matters
Imported by Cmd Run and Cmd UI.
Application
globalgrid2050 · solar-design-studio/202609271853-site-world/world · JavaScript module
Uses
Plant Feeders (import), Cmd Grammar (import), Plant Layout (import), Plant Template (import), String Design (import), Measure Format (import), Cmd Derive (import), Cmd Trench (import), and 2 more
Used by
imported by Cmd Run (import), imported by Cmd UI (import)
Produces
DEFAULTS, moduleOf, derive, postOf, TYPED_SECTION, spoilOf, echo, layoutInput, createSession, explain, summarize
Defines here
STRUCT, PACK, moduleOf, tplOpts, f, int, derive, postOf, spoilOf, echo, warnings, inWords, layoutInput, applySet, strOf, same, fmtPts, createSession, view, runLayout, exec, view_, undo, replay, canonical, explain, summarize
Cannot conclude
Whether this code runs, is used by a live app today, or is correct. This is a static reading.
Evidence
globalgrid2050@0aca5121f0795e469b6cf4fc3d3b628edba8d883 solar-design-studio/202609271853-site-world/world/cmd-model.mjs:1-350 · blob 0a057d5aea18a1f93740361db9287bba767e120b · GitHub at these lines
State
achieves: comment · context: readme_derived · stage: transform · effects: none found · domain stage: sub33kv_design · users: EPC_contractor, ICP_grid_engineer · canonical: production_core
// cmd-model.mjs: the design as typed. A session holds the typed values, runs each command through an engine (the page's
// plant, block and layers, or a plain CPU layout in tests), echoes what changed in words, keeps an undo stack, and keeps
// the script: the canonical lines that rebuild the design when replayed. Pure: the engine does all the world work.
//
// engine: { layout(input) -> Promise<summary | null>, openLand(input) -> Promise<[[e, n]...]>, drawnBoundary() -> pts | null,
//   setBoundary(pts), clearPlant(), block({ stations, pocKv, at }) -> Promise<{ text, at }>, clearBlock(), piles() -> Promise<text>,
//   show(arg) -> text, go(arg) -> text, constraints(summary, state) -> [text], avoid(state) -> [{ id, test }] }
// summary: { tables, mwp, mw, fits, rowsHa, trenchM, skipped, notes }

import { SECTION_FOR } from './plant-feeders.mjs';
import { parse, help, KEYS, guard } from './cmd-grammar.mjs';
import { tableGeometry, LAYOUT_DEFAULTS } from './plant-layout.mjs';
import { plantTemplate, moduleColdVoc, TEMPLATE_DEFAULTS as TD } from './plant-template.mjs';
import { STRING_ADVICE } from './string-design.mjs';
import { formatNumber, NONE } from './measure-format.mjs';
import { noonSun, rowShade, cableFor, bendOf, trenchOf, stringLoops } from './cmd-derive.mjs';
import { rateText, designText, TRENCH_KEYS, TRENCH_DEFAULTS, trenchEnv } from './cmd-trench.mjs';
import { structureById, catalogueText, formatRefusal, formatName, mpptCap, STRUCTURE_NOTE } from './structures.mjs';
import { presetById, presetText, PRESET_NOTE } from './structure-presets.mjs';

export const DEFAULTS = Object.freeze({
  mw: null, layout: 'south', tilt: LAYOUT_DEFAULTS.tiltSouthDeg, tiltEw: LAYOUT_DEFAULTS.tiltEwDeg, pitch: null, gcr: LAYOUT_DEFAULTS.gcr,
  edge: LAYOUT_DEFAULTS.lowEdgeM, mps: null, tempMin: -10, wp: null, voc: null, fence: LAYOUT_DEFAULTS.fenceSetbackM, water: LAYOUT_DEFAULTS.waterSetbackM,
  slope: LAYOUT_DEFAULTS.slopeLimitPct, avoidOhl: true, depth: 1.11, width: 0.45, formation: 'trefoil', size: 300, metal: 'al',
  wiring: 'standard', stations: 10, pocKv: null, soil: 1.2, load: 'continuous', ...TRENCH_DEFAULTS, boundary: null,
  // structures (structures.mjs): format (null tiers: the layout's own), post lines, tracker strings, row gap, MPPT cap, post height
  tiers: null, orient: 'portrait', tableCols: null, posts: 2, trackerStrings: 2, rowGap: 'slope', mppts: 12, stringsPerMppt: 2, postMax: 3,
  gcrTracker: LAYOUT_DEFAULTS.gcrTracker,
  // inverter class (plant-template.mjs), packing (plant-packing.mjs; band and run null: the packing's own), module size
  inverterClass: 'string', inverterKVA: 300, invertersPerStation: 20, packing: LAYOUT_DEFAULTS.packing, bandM: null, runM: null,
  hedge: LAYOUT_DEFAULTS.hedgeSetbackM, moduleLong: LAYOUT_DEFAULTS.moduleLongM, moduleShort: LAYOUT_DEFAULTS.moduleShortM
}); // depth/width/size/formation: the plant's typed 33 kV trench, 33kv-1-agri (data/trench-sections.json); the block's trenches are sized by rating
const LAYOUT_KEYS = ['mw', 'layout', 'tilt', 'tiltEw', 'pitch', 'gcr', 'edge', 'mps', 'tempMin', 'wp', 'voc', 'fence', 'water', 'slope', 'avoidOhl',
  'tiers', 'orient', 'tableCols', 'posts', 'trackerStrings', 'rowGap', 'mppts', 'stringsPerMppt', 'postMax', 'gcrTracker',
  'inverterClass', 'inverterKVA', 'invertersPerStation', 'packing', 'bandM', 'runM', 'hedge', 'moduleLong', 'moduleShort'];
const STRUCT = st => ({ tiers: st.tiers, orient: st.orient, tableCols: st.tableCols, trackerStrings: st.trackerStrings, gcrTracker: st.gcrTracker,
  moduleLongM: st.moduleLong ?? LAYOUT_DEFAULTS.moduleLongM, moduleShortM: st.moduleShort ?? LAYOUT_DEFAULTS.moduleShortM });
const PACK = st => ({ packing: st.packing || LAYOUT_DEFAULTS.packing, hedgeSetbackM: st.hedge ?? LAYOUT_DEFAULTS.hedgeSetbackM,
  ...(st.bandM ? { bandTargetM: st.bandM } : {}), ...(st.runM ? { runTargetM: st.runM } : {}) });
// The module (one answer with the block, string-design.mjs): typed power and Voc win; else the selected module class
// (env.moduleClass() -> { wp, voc, coeffPctPerK }); else the template's default class A.
export function moduleOf(st, env = {}) {
  const c = env.moduleClass?.() || null;
  return { wp: st.wp ?? c?.wp ?? TD.moduleWp, voc: st.voc ?? c?.voc ?? TD.moduleVoc,
    coeff: st.voc == null && c?.coeffPctPerK != null ? c.coeffPctPerK / 100 : TD.vocCoeffPerK, typed: st.voc != null };
}
const tplOpts = (st, env) => ({ moduleWp: moduleOf(st, env).wp, moduleVoc: moduleOf(st, env).voc, vocCoeffPerK: moduleOf(st, env).coeff,
  minCellC: st.tempMin ?? -10, mppts: st.mppts, stringsPerMppt: st.stringsPerMppt,
  inverterClass: st.inverterClass || 'string', inverterKVA: st.inverterKVA ?? 300, invertersPerStation: st.invertersPerStation ?? 20,
  ...(st.mps ? { modulesPerString: st.mps } : {}) });
// Typed settings keep their places (a pitch of 7.00 m); the one formatter underneath (measure-format.mjs), one dash for none.
const f = (v, d = 2) => (v == null || !Number.isFinite(v) ? NONE : formatNumber(v, d));
const int = v => f(v, 0);

/** The table, rows, cables and loops the typed values give. env: { latDeg, catalogue }; lay: the last layout summary. */
export function derive(st, env = {}, lay = null) {
  const tpl = plantTemplate({ targetMW: st.mw || 50, ...tplOpts(st, env) });
  const south = st.layout === 'south', base = { ...LAYOUT_DEFAULTS, tiltSouthDeg: st.tilt, tiltEwDeg: st.tiltEw, lowEdgeM: st.edge, ...STRUCT(st) };
  const T0 = tableGeometry(st.layout, base, tpl);
  const gcr = south ? (st.pitch ? T0.slope / st.pitch : st.gcr) : null, ewGapM = !south && st.pitch ? st.pitch - T0.depth : base.ewGapM;
  const T = tableGeometry(st.layout, { ...base, ...(south ? { gcr } : { ewGapM }) }, tpl);
  const sun = noonSun(env.latDeg ?? 51.8), shade = rowShade({ rise: T.rise, depth: T.depth, pitch: T.pitch, elevDeg: sun.winter });
  const cable = cableFor(env.catalogue, st.size, st.metal), bend = bendOf(env.catalogue, cable);
  const trench = trenchOf({ depth: st.depth, width: st.width, formation: st.formation, od: cable ? cable.od : 0.05 });
  const mps = tpl.counts.modulesPerString, cv = moduleColdVoc(tpl.params), loops = stringLoops({ mps });
  const voc = { module: cv.v, string: cv.v * mps, most: Math.floor(tpl.params.systemV / cv.v + 1e-9), rule: cv.rule, moduleVoc: tpl.params.moduleVoc };
  return { st, T, gcr, ewGapM, sun, shade, cable, bend, trench, mps, voc, loops, lay, spoilM3: spoilOf(lay, st, env.sections), tpl, post: postOf(st, T) };
}
// The tallest post by design: south two lines at 0.2 / 0.8 of the depth (one line: mid depth); reveal = low edge + rise x f - rails 0.3 m.
export function postOf(st, T) {
  if (T.tracker) return null;
  const f = st.layout === 'south' ? (st.posts === 1 ? 0.5 : 0.8) : 0.5, reveal = st.edge + T.rise * f - 0.3;
  return { reveal, over: reveal > st.postMax + 1e-9 };
}
// Bank spoil of the plant's MV trenches: each trench type's length x its section; the typed trench replaces the single-circuit
// section the layout routes (SECTION_FOR.one: 33kv-1-agri); two-circuit and ducted crossings keep their catalogue sections.
export const TYPED_SECTION = SECTION_FOR.one;
export function spoilOf(lay, st, sections = []) {
  if (!lay?.trenchByType) return null;
  let v = 0;
  for (const [id, L] of Object.entries(lay.trenchByType)) {
    const sec = (sections || []).find(x => x.id === id);
    v += L * (id === TYPED_SECTION || !sec ? st.width * st.depth : sec.trench_width_m * sec.trench_depth_m);
  }
  return v;
}

// What an echo compares, in order: [label, value(d) as text].
const WATCH = [
  ['layout', d => d.st.layout], ['plant export', d => (d.st.mw ? `${int(d.st.mw)} MW` : NONE)],
  ['tilt', d => `${f(d.st.layout === 'south' ? d.st.tilt : d.st.tiltEw, 0)}°`], ['modules per string', d => int(d.mps)],
  ['coldest cell', d => `${f(d.st.tempMin ?? -10, 0)} °C`], ['string cold Voc', d => `${int(d.voc.string)} V`], ['row pitch', d => `${f(d.T.pitch)} m`], ['GCR', d => (d.gcr ? f(d.gcr) : NONE)],
  ['clear gap between rows', d => `${f(d.shade.gapM)} m`], ['midwinter noon shadow', d => `${f(d.shade.shadowM)} m`],
  ['lowest module edge', d => `${f(d.st.edge)} m`], ['top edge', d => `${f(d.st.edge + d.T.rise)} m`],
  ['slope limit', d => `${int(d.st.slope)} %`], ['watercourse setback', d => `${int(d.st.water)} m`], ['fence setback', d => `${f(d.st.fence, 1)} m`],
  ['overhead line zones', d => (d.st.avoidOhl ? 'kept clear' : 'not avoided')],
  ['trench depth', d => `${f(d.st.depth)} m`], ['trench width', d => `${f(d.st.width)} m`], ['formation', d => d.st.formation],
  ['cover to top cable', d => `${f(d.trench.cover)} m`], ['cable', d => (d.cable ? `${d.st.size} mm² ${d.st.metal === 'cu' ? 'Cu' : 'Al'}, OD ${f(d.cable.odMm, 1)} mm` : NONE)],
  ['bend radius (installation)', d => (d.bend?.installM ? `${f(d.bend.installM)} m` : NONE)],
  ['table', d => formatName(d.T.tiers, d.T.orient, d.T.cols)],
  ['post lines', d => (d.T.tracker ? 'one along each tube' : d.st.layout === 'east-west' ? 'one under each face' : d.st.posts === 1 ? 'single' : 'twin')],
  ['strings per tracker row', d => (d.T.tracker ? int(d.st.trackerStrings) : NONE)], ['tracker GCR', d => (d.T.tracker ? f(d.st.gcrTracker) : NONE)],
  ['row gap', d => (d.st.layout === 'south' ? (d.st.rowGap === 'slope' ? 'slope-aware' : 'flat') : NONE)],
  ['inverters', d => `${d.st.inverterClass || 'string'}, ${int(d.st.inverterKVA ?? 300)} kVA, ${int(d.st.invertersPerStation ?? 20)} a station`],
  ['strings per inverter', d => (d.st.inverterClass === 'central' ? `${int(d.tpl.counts.stringsPerInverter)} in ${int(d.tpl.counts.combiners / d.tpl.counts.inverters)} combiner boxes`
    : `${int(d.tpl.counts.stringsPerInverter)} (cap ${mpptCap(d.st)}: ${d.st.mppts} MPPT × ${d.st.stringsPerMppt})`)],
  ['packing', d => `${d.st.packing || 'fields'}${d.st.bandM ? `, tracks every ${int(d.st.bandM)} m` : ''}${d.st.runM ? `, runs ${int(d.st.runM)} m` : ''}`],
  ['hedge setback', d => `${f(d.st.hedge ?? 5, 1)} m`], ['module size', d => `${f(d.st.moduleLong ?? 2.384, 3)} × ${f(d.st.moduleShort ?? 1.134, 3)} m`],
  ['tallest post by design', d => (d.post ? `${f(d.post.reveal)} m (limit ${f(d.st.postMax, 1)} m)` : NONE)],
  ['string wiring', d => d.st.wiring], ['loop area per string', d => `${f(d.loops[d.st.wiring].area, 1)} m²`],
  ['spoil (bank)', d => (d.spoilM3 ? `${int(d.spoilM3)} m³` : NONE)], ['soil', d => `${f(d.st.soil, 1)} K.m/W`], ['load profile', d => d.st.load],
  ['trenches side by side', d => ['Dc', 'Branch', 'Collector', 'Mv'].map(k => `${k.toLowerCase()} ${d.st['split' + k] || 'by rating'}`).join(', ')],
  ['trench cover', d => `dc ${f(d.st.coverDc)} m, lv ${f(d.st.coverLv)} m, 33 kV ${f(d.st.coverMv)} m`], ['cable drum', d => `${int(d.st.drumM)} m`],
  ['33 kV ring case', d => d.st.ring]
];
const COUNTS = [['tables', s => int(s.tables)], ['MWp', s => f(s.mwp, 1)], ['land under rows', s => `${f(s.rowsHa, 1)} ha`],
  ['built', s => `${int(s.mw)} MW${s.fits ? '' : ' (not all fits)'}`]];

/** "row pitch 11.97 m → 7.00 m; GCR 0.40 → 0.68; 1,402 tables → 1,402" */
export function echo(a, b) {
  const parts = WATCH.map(([k, v]) => [k, v(a), v(b)]).filter(([, x, y]) => x !== y).map(([k, x, y]) => `${k} ${x} → ${y}`);
  if (a.lay && b.lay && a.lay !== b.lay) parts.push(...COUNTS.map(([k, v]) => [k, v(a.lay), v(b.lay)]).filter(([, x, y]) => x !== y)
    .map(([k, x, y]) => (k === 'tables' ? `${x} tables → ${y}` : `${k} ${x} → ${y}`)));
  else if (!a.lay && b.lay) parts.push(`${int(b.lay.tables)} tables, ${f(b.lay.mwp, 1)} MWp, ${f(b.lay.rowsHa, 1)} ha under rows${b.lay.fits ? '' : ` (${int(b.lay.mw)} MW fits)`}`);
  return parts.join('; ');
}

// Warnings the new values raise (never refusals: the owner decides).
function warnings(d) {
  const w = [];
  if (d.st.layout === 'south' && d.shade.shaded) w.push(`rows shade each other at midwinter noon (${f(d.shade.shadowM)} m shadow, ${f(d.shade.gapM)} m gap)`);
  if (d.trench.cover < 0.91 - 1e-9) w.push(`cover ${f(d.trench.cover)} m is under the 0.91 m asked on good farmland (a network operator's cable standard)`);
  // Owner, 27 Sept: modules in series are his to change; an over-voltage is said, never refused, with the advisory.
  if (d.voc.string > 1500) w.push(`plant layout: ${d.mps} modules in a string reach ${int(d.voc.string)} V cold (module Voc ${f(d.voc.moduleVoc, 1)} V; `
    + `the stricter of ${f(d.st.tempMin ?? -10, 0)} °C and Voc × 1.15, here ${d.voc.rule}), over the 1500 V system limit; at most ${d.voc.most} `
    + `(type string ${d.voc.most}). ${STRING_ADVICE}`);
  if (d.post?.over) w.push(`posts stand ${f(d.post.reveal)} m by design, over the ${f(d.st.postMax, 1)} m limit: lower the tilt, use fewer tiers, `
    + `a lower edge or a single post line (flagged on the HUD). ${STRUCTURE_NOTE}`);
  const c = d.tpl.counts, dcac = c.stringsPerInverter * c.modulesPerString * d.tpl.params.moduleWp / 1000 / d.tpl.params.inverterKVA;
  if (d.st.inverterClass !== 'central' && c.stringsPerInverter >= mpptCap(d.st) && dcac < d.tpl.params.dcAcRatio - 0.05) w.push(`DC/AC ${f(dcac)}: strings per inverter are capped at `
    + `${mpptCap(d.st)} by the MPPTs (${d.st.mppts} × ${d.st.stringsPerMppt}); more strings per input or longer strings raise it (mppt ${d.st.mppts} inputs `
    + `${Math.ceil(Math.round(d.tpl.params.inverterKVA * d.tpl.params.dcAcRatio / (c.modulesPerString * d.tpl.params.moduleWp / 1000)) / d.st.mppts)})`);
  if (!d.trench.fits) w.push(`the ${d.st.formation} needs a ${f(d.trench.needWidth)} m trench; it is ${f(d.st.width)} m`);
  return w;
}

/** The typed values as plant.run parameters. */
// A refusal in words: a fault inside the page (a JavaScript error) is never shown by its programming name.
const inWords = e => (e instanceof TypeError || e instanceof ReferenceError || e instanceof RangeError ? 'That did not run here (a fault in the page)' : e.message);

export function layoutInput(st, env = {}) {
  const d = derive(st, env), south = st.layout === 'south';
  return { mw: st.mw, layout: st.layout, slopeLimitPct: st.slope, fenceSetbackM: st.fence,
    options: { tiltSouthDeg: st.tilt, tiltEwDeg: st.tiltEw, lowEdgeM: st.edge, waterSetbackM: st.water, ...STRUCT(st), ...PACK(st), rowGap: st.rowGap,
      ...(env.latDeg ? { latDeg: env.latDeg } : {}), ...(south ? { gcr: d.gcr } : st.layout === 'tracker' ? {} : { ewGapM: d.ewGapM }) },
    template: tplOpts(st, env), piles: { posts: st.posts, rules: { revealFailM: st.postMax } } };
}

// Applies typed values; returns { st } or { why } (refused in words).
function applySet(st0, c, env) {
  const set = { ...c.set }, st = { ...st0 };
  if (c.arg.maybeEw && 'tilt' in set && st.layout === 'east-west') {
    const g = guard('tiltEw', set.tilt); if (g) return { why: g }; set.tiltEw = set.tilt; delete set.tilt;
  }
  if ('gcr' in set && (set.layout || st.layout) === 'tracker') { set.gcrTracker = set.gcr; delete set.gcr; st.pitch = null; }
  if ('gcr' in set) { if ((set.layout || st.layout) !== 'south') return { why: 'east-west rows are set by the gap between them: type pitch' }; st.pitch = null; }
  Object.assign(st, set);
  // A format kept from another layout that this one does not build goes back to the layout's own; one typed now is refused.
  const bad = formatRefusal(st.layout, st.tiers || (st.layout === 'tracker' || st.layout === 'east-west' ? 1 : 2), st.orient);
  if (bad && ('tiers' in set || 'orient' in set)) return { why: bad };
  if (bad) Object.assign(st, { tiers: null, orient: 'portrait', tableCols: null });
  if (st.posts === 1 && st.layout === 'east-west' && ('posts' in set || 'layout' in set)) {
    if ('posts' in set) return { why: 'a single post line is built for south-facing tables here; east-west keeps one line under each face' };
    st.posts = 2;
  }
  let d;
  try { d = derive(st, env); } catch (e) { return { why: e.message.replace(/^plant template: /, '') }; } // an inverter class and rating that do not agree
  if (st.layout === 'south' && st.pitch && !(d.gcr <= KEYS.gcr.max)) return { why: `row pitch ${f(st.pitch)} m leaves no room: the table is ${f(d.T.depth)} m deep in plan `
    + `and ${f(d.T.slope)} m up the slope, so the ground cover ratio would be ${f(d.T.slope / st.pitch)} (most ${KEYS.gcr.max}); the least pitch is ${f(d.T.slope / KEYS.gcr.max)} m` };
  if (st.layout === 'east-west' && st.pitch && d.ewGapM < 0.5) {
    return { why: `row pitch ${f(st.pitch)} m leaves ${f(d.ewGapM)} m between east-west tables; at least 0.5 m (least pitch ${f(d.T.pitch - d.ewGapM + 0.5)} m)` };
  }
  if (!d.cable) return { why: `no 33 kV ${st.metal === 'cu' ? 'copper' : 'aluminium'} single core of ${st.size} mm² in the cable catalogue` };
  return { st };
}
// The typed string length and coldest cell go with every rebuild of the block, so a re-size never drops them.
const strOf = st => ({ ...(st.mps ? { modulesPerString: st.mps } : {}), ...(st.tempMin !== DEFAULTS.tempMin ? { tempMinC: st.tempMin } : {}) });
const same = (a, b, keys) => keys.every(k => JSON.stringify(a[k]) === JSON.stringify(b[k]));
const fmtPts = pts => pts.map(([e, n]) => `${Math.round(e)} ${Math.round(n)}`).join(', ');
const ANIM = [['tilt', ['tilt', 'tiltEw']], ['rows', ['pitch', 'gcr', 'layout', 'edge']], ['trench', ['depth', 'width', 'formation', 'size', 'metal']],
  ['loop', ['wiring', 'mps']]];

export function createSession({ engine, env = {} }) {
  let st = { ...DEFAULTS }, lay = null, script = [], block = null, pending = false;
  const hist = [];
  const view = () => derive(st, env, lay);
  async function runLayout() {
    if (!st.boundary) {
      const drawn = engine.drawnBoundary?.();
      // Whole metres, as the script writes it, so a replay packs the very same boundary (the fields packing starts at its edges).
      const got = drawn || await engine.openLand(layoutInput(st, env));
      st = { ...st, boundary: got && got.map(([e, n]) => [Math.round(e), Math.round(n)]) };
      if (!st.boundary) return { why: 'no boundary: draw one (Plant > Draw boundary) or stand on open land' };
      script.splice(Math.max(0, script.length - 1), 0, `boundary ${fmtPts(st.boundary)}`); // before the line that asked for it
    }
    await engine.setBoundary?.(st.boundary);
    const s = await engine.layout({ ...layoutInput(st, env), avoid: st.avoidOhl ? engine.avoid?.(st) || [] : [] });
    if (!s) return { why: 'the layout did not run (see the Plant readout)' };
    lay = s; pending = false;
    return { s };
  }
  async function exec(line, { replay = false } = {}) {
    const c = parse(line);
    if (!c.ok) return { ok: false, text: c.why, line };
    if (c.act === 'help') return { ok: true, text: help(c.arg.topic), line, quiet: true };
    if (c.act === 'script') {
      const typed = new Set(script.flatMap(l => Object.keys(parse(l).set || {})));
      const plantTrench = new Set(['depth', 'width', 'formation', 'size']);   // the plant's 33 kV feeder trench, not the block's
      const assumed = ['tilt', 'gcr', 'edge', 'tempMin', 'wp', 'voc', 'fence', 'water', 'slope', 'depth', 'width', 'formation', 'size', 'wiring', ...TRENCH_KEYS]
        .filter(k => !typed.has(k) && !(k === 'gcr' && typed.has('pitch')))
        .map(k => `${plantTrench.has(k) ? 'plant 33 kV feeder ' : ''}${KEYS[k].label} ${st[k] ?? moduleOf(st, env)[k]}${KEYS[k].unit ? ' ' + KEYS[k].unit : ''}`);
      if (block) assumed.push('block trenches: LV 400 mm² Al and DC 6 mm² Cu, each trench sized by rating (type trench design)');
      return { ok: true, line, quiet: true, text: `${script.length ? script.join('\n') : 'Nothing typed yet.'}`
        + `\nTYPED: ${[...typed].map(k => KEYS[k]?.label || k).join(', ') || 'nothing'}\nASSUMED (defaults): ${assumed.join(', ')}` };
    }
    if (c.act === 'undo') return undo();
    if (c.act === 'structure' && !c.arg.id) return { ok: true, text: c.arg.preset ? presetText() : catalogueText(), line, quiet: true };
    if (c.act === 'structure') { // a preset of typed values, echoed and undone like any other line
      const S = structureById(c.arg.id) || presetById(c.arg.id), bad = Object.entries(S.set).map(([k, v]) => (v === null ? null : guard(k, v))).filter(Boolean);
      if (bad.length) return { ok: false, text: bad.join('; ') + '. Nothing was changed.', line };
      c.set = { ...S.set }; c.act = null; c.structure = S;
    }
    const view_ = async fn => { try { return { ok: true, text: await fn(), line, quiet: true }; } catch (e) { return { ok: false, text: inWords(e) + '.', line }; } };
    if (c.act === 'show') return view_(() => engine.show(c.arg));
    if (c.act === 'rate') return view_(() => rateText(c.arg, st));
    if (c.act === 'xray') return view_(() => (engine.xray ? engine.xray(c.arg.on) : 'X-ray is not in this build.'));
    if (c.act === 'trench-design' && !Object.keys(c.set).length) return view_(async () => designText(st, await engine.trenchRows?.()));
    if (c.act === 'explain') { const d = view(); return { ok: true, text: explain(c.arg.what, d), line, quiet: true, anim: c.arg.what === 'bend' ? 'trench' : 'loop', d, before: d }; }
    // Earthing (earth-mount.mjs): its settings live in the cartridge; the typed line joins the script so a replay rebuilds it.
    if (c.act === 'earthing') {
      const r = await view_(async () => { if (!engine.earthing) throw Error('earthing is not in this build'); return engine.earthing(c.arg); });
      if (r.ok) script.push(canonical(c));
      return r;
    }
    if (c.act === 'binoculars') return view_(async () => { if (!engine.binoculars) throw Error('the binoculars are not in this build'); return engine.binoculars(c.arg); });
    if (c.act === 'hud') return view_(async () => { if (!engine.hud) throw Error('the check HUD is not in this build'); return engine.hud(c.arg); });
    if (c.act === 'find') return view_(async () => { if (!engine.find) throw Error('Find is not in this build'); return engine.find(c.arg); });
    if (c.act === 'go') return view_(async () => { if (pending) await runLayout(); return engine.go(c.arg, lay); });
    // Follow the power and the single-line diagram read the world; they change no typed value and stay out of the script.
    if (c.act === 'follow') return view_(async () => { if (pending) await runLayout(); if (!engine.follow) throw Error('Follow is not in this build'); return engine.follow(c.arg, lay); });
    if (c.act === 'sld') return view_(async () => { if (pending) await runLayout(); if (!engine.sld) throw Error('the single-line diagram is not in this build'); return engine.sld(c.arg, lay); });
    const before = { st, lay, script: script.slice(), block, d: view() };
    const a = applySet(st, c, env);
    if (a.why) return { ok: false, text: a.why + '. Nothing was changed.', line };
    if (!c.act && same(st, a.st, Object.keys(DEFAULTS))) return { ok: true, text: `No change: ${c.line} is already set.`, line, quiet: true }; // not an undo step
    st = a.st;
    let extra = [];
    try {
      if (c.act === 'boundary') {
        st = { ...st, boundary: c.arg.open ? null : c.arg.points };
        if (st.boundary) await engine.setBoundary?.(st.boundary);
        if (lay && st.boundary) { if (replay) pending = true; else { const r = await runLayout(); if (r.why) throw Error(r.why); } }
      } else if (c.act === 'fill' && !st.boundary && !engine.drawnBoundary?.()) {
        throw Error('fill needs a boundary: draw one (Plant > Draw boundary), or type plant 50mw to use the open land around you');
      }
      if (c.act === 'fill' && !st.mw) throw Error('fill needs a capacity first: plant 50mw, or fill 50mw');
      if (c.act === 'layout' && !st.mw) throw Error('plant needs a capacity, such as plant 50mw south');
      const redo = c.act === 'layout' || c.act === 'fill' || (lay && !same(before.st, st, LAYOUT_KEYS));
      script.push(canonical(c));
      if (redo) {
        if (replay) pending = true;
        else { const r = await runLayout(); if (r.why) throw Error(r.why); extra = engine.constraints?.(lay, st) || []; }
      }
      if (c.act === 'block') {
        if (pending) await runLayout();
        const at = c.arg.at || block?.at || null, b = await engine.block({ stations: st.stations / 10, pocKv: st.pocKv, at, trench: trenchEnv(st), frame: !replay, ...strOf(st) });
        block = { at: b.at, stations: st.stations }; extra.push(b.text);
        script[script.length - 1] = `block ${st.stations}mva${st.pocKv ? ` poc ${st.pocKv}` : ''}${b.at ? ` at ${Math.round(b.at.e)} ${Math.round(b.at.n)}` : ''}`;
      }
      if (('mps' in c.set || 'tempMin' in c.set) && engine.strings) { const t = await engine.strings({ mps: st.mps, tempMinC: st.tempMin }); if (t) extra.push(t); }
      if (c.act === 'piles') { if (pending) await runLayout(); extra.push(await engine.piles(lay)); }
      if (block && c.act !== 'block' && !same(before.st, st, TRENCH_KEYS)) {     // soil, load or split typed: the block's trenches re-sized
        await engine.block({ stations: st.stations / 10, pocKv: st.pocKv, at: block.at, trench: trenchEnv(st), ...strOf(st) }); extra.push('Block trenches re-sized.');
      }
      if (c.act === 'trench-design') extra.push(designText(st, await engine.trenchRows?.()));
    } catch (e) {
      st = before.st; lay = before.lay; script = before.script; block = before.block;
      return { ok: false, text: inWords(e) + '. Nothing was changed.', line };
    }
    const after = view(), said = echo(before.d, after);
    hist.push({ line: c.line, before });
    const warn = warnings(after).filter(w => !before.d.lay !== !after.lay || !warnings(before.d).includes(w)); // all of them on a new layout
    const keys = Object.keys(c.set), anim = (ANIM.find(([, ks]) => ks.some(k => keys.includes(k))) || [null])[0];
    if (c.structure) extra.unshift(`${c.structure.evidence ? 'Preset' : 'Structure'} ${c.structure.id}: ${c.structure.label}. Class ranges: ${c.structure.ranges}.`
      + (c.structure.evidence ? ` Evidence: ${c.structure.evidence}. ${PRESET_NOTE}` : ` ${STRUCTURE_NOTE}`));
    const text = [said || (c.act ? '' : `${c.line}: set (the same as before, to the precision shown)`), ...extra, ...warn.map(w => 'Note: ' + w)].filter(Boolean).join('\n');
    return { ok: true, text, line, anim, d: after, before: before.d };
  }
  async function undo() {
    const h = hist.pop();
    if (!h) return { ok: false, text: 'Nothing to undo.', line: 'undo' };
    const now = view(), b = h.before, layoutChanged = b.lay !== lay || !same(b.st, st, [...LAYOUT_KEYS, 'boundary']);
    st = b.st; script = b.script;
    if (block && !b.block) engine.clearBlock?.();
    else if (b.block && (block !== b.block || !same(now.st, b.st, TRENCH_KEYS))) {
      await engine.block({ stations: b.block.stations / 10, pocKv: b.st.pocKv, at: b.block.at, trench: trenchEnv(b.st), ...strOf(b.st) });
    }
    block = b.block;
    if (engine.strings && !same(b.st, now.st, ['mps', 'tempMin'])) await engine.strings({ mps: st.mps, tempMinC: st.tempMin });
    if (layoutChanged) {
      if (b.lay) { const r = await runLayout(); if (r.s) lay = r.s; } else { lay = null; engine.clearPlant?.(); if (st.boundary) await engine.setBoundary?.(st.boundary); }
    }
    const said = echo(now, view());
    return { ok: true, text: `Undid "${h.line}"${said ? ': ' + said : '.'}`, line: 'undo', anim: null, d: view() };
  }
  // Replays a script from the start: values are set line by line, the layout runs once where it is needed and at the end.
  async function replay(lines) {
    st = { ...DEFAULTS }; lay = null; script = []; block = null; pending = false; hist.length = 0;
    const out = [];
    for (const l of lines) { if (!String(l).trim() || /^\s*#/.test(l)) continue; out.push(await exec(l, { replay: true })); }
    if (pending) { const r = await runLayout(); out.push(r.why ? { ok: false, text: r.why } : { ok: true, text: echo(derive(st, env), view()) }); }
    hist.length = 0;
    return out;
  }
  return { exec, undo, replay, script: () => script.slice(), state: () => ({ ...st }), layout: () => lay, derived: view, history: () => hist.length };
}

// The line kept in the script: the typed words, in the language's own spelling.
function canonical(c) {
  if (c.act === 'boundary') return c.arg.open ? 'boundary open' : `boundary ${fmtPts(c.arg.points)}`;
  return c.line.replace(/\s+/g, ' ').trim();
}

/** Words for "bend radius" and "loop area". */
export function explain(what, d) {
  if (what === 'bend') {
    if (!d.bend?.installM) return 'No bend rule in the catalogue for this cable.';
    return `${d.st.size} mm² ${d.st.metal === 'cu' ? 'Cu' : 'Al'} (OD ${f(d.cable.odMm, 1)} mm): bend radius ${f(d.bend.installM)} m while pulling`
      + ` (${d.bend.installX} × OD), ${f(d.bend.finalM)} m set in place (${d.bend.finalX} × OD). ${d.bend.source}`;
  }
  const L = d.loops;
  return `Loop between + and − of one ${d.mps}-module string: standard ${f(L.standard.area, 1)} m², leapfrog ${f(L.leapfrog.area, 1)} m²`
    + ` (${f(100 * (1 - L.leapfrog.area / L.standard.area), 0)} % less). Routes on the table assumed; wiring ${d.st.wiring}.`;
}

/** The counts an echo compares, from a plant layout (plant-layout.mjs result). */
export const summarize = r => (r ? { tables: r.built.tables, mwp: r.built.mwp, mw: r.built.mw, fits: r.fits,
  rowsHa: r.built.tables * r.table.pitch * (r.table.lenU + r.params.tableGapM) / 1e4, trenchM: r.electrical?.trenchM ?? 0,
  trenchByType: { ...(r.electrical?.trenchByType || {}) }, crossReview: r.crossReview || 0, rowGap: r.rowGap ? { ...r.rowGap } : null,
  skipped: { ...r.skipped }, notes: r.notes.slice() } : null);

Cmd Mount

A JavaScript module in the world folder of GlobalGrid2050 that mounts command line (`mountCommandLine`) (units: MW), covering overhead lines (OHL) and solar.

renderglobalgrid2050 / solar-design-studio/202609271853-site-world/world/cmd-mount.mjs · lines 1–63 of 63 · version 2026-09-27 18:53 · pattern opened (shared by 2 files)

What it does
It draws overhead lines (OHL) and solar. Defines 6 functions or classes, including formLines, typing, mountCommandLine, changed, load. Side effects: changes the web page (DOM).
Why it exists in the grid workflow
Domain stage sub33kv_design (path word 'design'): inside-the-fence electrical and civil design below 33kV, which Ventus Ltd states is within its commercial execution boundary. Domain terms in the code: overhead lines (OHL), solar.
Downstream impact
Used directly by 1 file: Substrate (globalgrid2050). Reached from 1 other entry point, including Solar Design Studio — Site World Page (globalgrid2050, https://globalgrid2050.com/solar-design-studio/202609271853-site-world/).
What it achieves
It is built from the form lines helper (`formLines`), the typing step (`typing`) and the mount command line helper (`mountCommandLine`). The command line's only part that loads with the world (just after the first frame): a "Type a command" button in the Design panel and the "/" key. It responds to click and keydown events.
Why it matters
Imported by Substrate.
Application
globalgrid2050 · solar-design-studio/202609271853-site-world/world · JavaScript module
Uses
Cmd UI (dynamic import), Xray UI (dynamic import), Build Mount (dynamic import)
Used by
imported by Substrate (dynamic import)
Produces
formLines, mountCommandLine
Defines here
formLines, typing, mountCommandLine, changed, load, opened
Cannot conclude
Whether this code runs, is used by a live app today, or is correct. This is a static reading.
Evidence
globalgrid2050@0aca5121f0795e469b6cf4fc3d3b628edba8d883 solar-design-studio/202609271853-site-world/world/cmd-mount.mjs:1-63 · blob 86b6b1f9c209f92944e5e1b998601370388e4414 · GitHub at these lines
State
achieves: comment · context: readme_derived · stage: output · effects: dom · domain stage: sub33kv_design · users: EPC_contractor, ICP_grid_engineer · canonical: production_core
// cmd-mount.mjs: the command line's only part that loads with the world (just after the first frame): a "Type a command"
// button in the Design panel and the "/" key. The bar and its engines (cmd-ui.mjs and what it imports) load the first time either is used,
// or when a design with a typed script is opened: the script is the design, so opening it replays it.
// Saved designs carry the script as one item: { id: 'script-1', kind: 'script', params: { lines: [...] }, points_bng: [] }.

// The typed lines that rebuild what the forms made: the plant (its boundary, limits and capacity) and the solar block.
const LAYOUT_WORD = { south: 'south', 'east-west': 'eastwest', tracker: 'tracker' };
export function formLines(world) {
  const out = [], r = world.plant?.result?.(), b = world.plant?.boundary?.(), s = world.block?.state?.();
  if (r && b?.length >= 3) {
    out.push('boundary ' + b.map(([e, n]) => `${Math.round(e)} ${Math.round(n)}`).join(', '));
    const P = r.params || {};
    if (Number.isFinite(P.slopeLimitPct)) out.push(`avoid slope ${P.slopeLimitPct}%`);
    if (Number.isFinite(P.fenceSetbackM)) out.push(`setback fence ${P.fenceSetbackM}`);
    // The forms lay south rows with the flat row gap; the typed bar starts slope-aware, so the saved lines say which.
    if ((r.layout || 'south') === 'south' && !r.rowGap) out.push('rowgap flat');
    out.push(`fill ${r.asked.mw}mw ${LAYOUT_WORD[r.layout] || 'south'}`);
  }
  if (s?.placed && s.anchor && s.params) out.push(`block ${s.params.stations * 10}mva poc ${s.params.pocKv} at ${Math.round(s.anchor[0])} ${Math.round(s.anchor[1])}`);
  return out;
}
const typing = t => t instanceof HTMLInputElement || t instanceof HTMLTextAreaElement || t instanceof HTMLSelectElement || t?.isContentEditable;

// deps: { api (substrate extension api, with hooks.items and hooks.open), world (window.world), project (project-ui), ohl }
export function mountCommandLine({ api, world, project = null, ohl = null, doc = document }) {
  let ui = null, loading = null, kept = null;
  const button = doc.createElement('button');
  button.type = 'button'; button.id = 'cmd-toggle'; button.textContent = 'Type a command';
  button.title = 'Command line: type a design command (press / on a keyboard)';
  button.setAttribute('aria-expanded', 'false'); button.setAttribute('aria-controls', 'cmd');
  // In the Design panel, not the dash (the dash keeps its five views; "/" opens the bar on a keyboard).
  const design = doc.getElementById('design');
  if (design) { const row = doc.createElement('div'); row.className = 'row'; row.append(button); design.prepend(row); }
  const changed = () => api.hooks.design.forEach(f => f()); // the design file (and the copy kept in this browser) takes the new script
  const load = () => (loading ||= import('./cmd-ui.mjs')
    .then(m => m.createCommandLine({ api, world, ohl, doc, onChange: changed, toggle: button }))
    .then(u => (ui = u))
    .catch(e => { loading = null; console.warn('command line: ' + e.message); throw e; }));
  button.addEventListener('click', () => load().then(u => u.toggle()).catch(() => {}));
  doc.addEventListener('keydown', e => {
    if (e.key !== '/' || e.ctrlKey || e.metaKey || e.altKey || typing(e.target)) return;
    e.preventDefault();
    load().then(u => u.open()).catch(() => {});
  });
  api.hooks.items?.add(() => {
    // Nothing typed: the plant and block made with the forms are saved as the lines that rebuild them (piles follow).
    const typed = ui ? ui.script() : kept, lines = typed && typed.length ? typed : formLines(world);
    return lines && lines.length ? [{ id: 'script-1', kind: 'script', params: { lines: lines.slice() }, points_bng: [] }] : [];
  });
  const opened = d => {
    const it = (d?.items || []).find(i => i.kind === 'script' && Array.isArray(i.params?.lines));
    if (!it) return;
    kept = it.params.lines.filter(s => typeof s === 'string').slice(0, 500);
    load().then(u => u.replay(kept)).catch(() => {});
  };
  api.hooks.open?.add(opened);
  // The As-built X-ray switch (xray-ui.mjs) sits in the Layers panel; the command line types it too (xray on).
  import('./xray-ui.mjs').then(m => { api.xray = m.mountXray({ api, world, doc }); }).catch(e => console.warn('x-ray: ' + e.message));
  if (project?.opened?.()) opened(project.opened());
  import('./build-mount.mjs').then(m => { window.worldBuild = m.mountBuild({ api, world, ohl, doc }); }) // build mode: Place row, build lines
    .catch(e => console.warn('build mode: ' + e.message));
  return { load, ui: () => ui, xray: () => api.xray?.debug() ?? null, batches: () => api.batches().map(b => `${b.key}@${b.version}`) }; // batches: what the last frame drew (tests)
}

Cmd Run

A JavaScript module in the world folder of GlobalGrid2050 that fetches overhead lines (OHL), solar and substations, using SHA-256.

renderglobalgrid2050 / solar-design-studio/202609271853-site-world/world/cmd-run.mjs · lines 1–217 of 217 · version 2026-09-27 18:53

What it does
It fetches overhead lines (OHL), solar and substations. Defines 10 functions or classes, including n0, createEngine, same, status, here. Side effects: changes the web page (DOM); makes network requests.
Why it exists in the grid workflow
Domain stage sub33kv_design (path word 'design'): inside-the-fence electrical and civil design below 33kV, which Ventus Ltd states is within its commercial execution boundary. Domain terms in the code: overhead lines (OHL), solar, substations.
Downstream impact
Used directly by 2 files: Build UI (globalgrid2050); Cmd UI (globalgrid2050). Reached from 2 other entry points, including Solar Design Studio — Site World Page (globalgrid2050, https://globalgrid2050.com/solar-design-studio/202609271853-site-world/); Substrate (globalgrid2050).
What it achieves
It creates engine (`createEngine`) and is built from the same step (`same`) and the status step (`status`). The page's engine for the typed session (`cmd-model.mjs`).
Why it matters
Imported by Build UI and Cmd UI.
Application
globalgrid2050 · solar-design-studio/202609271853-site-world/world · JavaScript module
Uses
Cmd Model (import), Cmd Derive (import), Clash (import), Measure Format (import), Ground (import), Layers (import), Earth Mount (dynamic import)
Used by
imported by Build UI (dynamic import), imported by Cmd UI (import)
Produces
createEngine, pilesText
Defines here
n0, createEngine, same, status, here, ohlTest, soilModel, soilAt, soilLine, pilesText
Cannot conclude
Whether this code runs, is used by a live app today, or is correct. This is a static reading.
Evidence
globalgrid2050@0aca5121f0795e469b6cf4fc3d3b628edba8d883 solar-design-studio/202609271853-site-world/world/cmd-run.mjs:1-217 · blob 2e63330037193361055b433c8825e0da1ad84080 · GitHub at these lines
State
achieves: comment · context: readme_derived · stage: output · effects: dom, network · domain stage: sub33kv_design · users: EPC_contractor, ICP_grid_engineer · canonical: production_core
// cmd-run.mjs: the page's engine for the typed session (cmd-model.mjs). Every command reaches the world through the same
// calls the forms use: world.plant.run(params), world.plant.openLand(params), world.block.place(params), world.setLayer,
// world.flyTo, and world.piles.auto() where this build has it. It also says what each layout kept clear: overhead line
// zones (the zones layer, HSE GS6), watercourses (the water layer and its setback), gradients (the slope limit) and the
// ground (the site's ground file, BGS Geology 625k). Illustrative throughout: nothing here is a design for any site.

import { summarize } from './cmd-model.mjs';
import { soilEffect } from './cmd-derive.mjs';
import { distToGeometry } from './clash.mjs';
import { formatNumber, fmtPct } from './measure-format.mjs';
import { parseGroundFile, at as groundAt } from './layers/ground.mjs';
import { makeApi } from './layers.mjs';

const ENDS = { 'gis-bay': 'a bay at the substation', 'dno-sub': 'the network operator substation', tower: 'the tee into the line', none: 'an assumed landing' };
const LAYER = { pylons: 'grid', water: 'water', slope: 'slope', soil: 'ground' };
const n0 = v => formatNumber(v, 0); // the one formatter (measure-format.mjs)

// deps: { world (window.world), api (the substrate's extension api), ohl ({ layer } from ohl-ui.mjs), doc }
export function createEngine({ world, api, ohl, doc = document }) {
  let ours = null, soil = null, ohlSeen = null, earth = null;
  const same = (a, b) => JSON.stringify(a) === JSON.stringify(b);
  const status = id => doc.getElementById(id)?.textContent || '';
  const here = () => { const s = world.state(), o = world.origin(); return { e: o.e + s.pos[0], n: o.n + s.pos[1] }; };

  // Overhead line zones as a test in national-grid metres; null when the grid layer has drawn none.
  function ohlTest() {
    const L = ohl?.layer;
    if (!L) return null;
    L.refresh?.(world.groundVersion?.());
    const spans = L.spans?.() || [], o = world.origin();
    ohlSeen = spans.length;
    if (!spans.length) return null;
    const boxes = spans.map(s => {
      const xs = s.quad.pts.map(p => p[0]), ys = s.quad.pts.map(p => p[1]), r = s.zoneReach;
      return [Math.min(...xs) - r, Math.min(...ys) - r, Math.max(...xs) + r, Math.max(...ys) + r];
    });
    return (e, n) => {
      const x = e - o.e, y = n - o.n;
      for (let i = 0; i < spans.length; i++) {
        const b = boxes[i];
        if (x < b[0] || x > b[2] || y < b[1] || y > b[3]) continue;
        if (distToGeometry(spans[i].quad, x, y) <= spans[i].zoneReach) return true;
      }
      return false;
    };
  }
  async function soilModel() {
    if (soil) return soil;
    try {
      const base = new URL('./world/', location.href).href, m = await (await fetch(base + 'manifest.json', { cache: 'no-cache' })).json();
      const cfg = (m.layers || []).find(l => l.id === 'ground')?.config;
      soil = cfg?.path ? parseGroundFile(await makeApi(base).fetchJSON(cfg.path, cfg.sha256)) : { none: true };
    } catch { soil = { none: true }; }
    return soil;
  }
  const soilAt = async (e, n) => { const m = await soilModel(); return soilEffect(m.none ? null : groundAt(m, e, n)); };
  soilModel();

  return {
    async layout(input) {
      const s = await world.plant.run(input);
      if (!s) throw Error(status('plant-status') || 'the layout did not run');
      return summarize(world.plant.result());
    },
    async openLand(input) {
      const b = await world.plant.openLand({ ...input, at: here() });
      ours = b ? b.map(p => p.slice()) : null;
      return ours;
    },
    drawnBoundary() { const b = world.plant.boundary?.(); return b && !same(b, ours) ? b.map(p => p.slice()) : null; },
    async setBoundary(pts) { ours = pts.map(p => p.slice()); await world.plant.setBoundary(pts.map(([e, n]) => ({ e, n }))); },
    clearPlant() { ours = null; world.plant.clear?.(); },
    avoid(st) { const t = st.avoidOhl ? ohlTest() : null; return t ? [{ id: 'ohl', test: t }] : []; },
    // What the layout kept clear, in words.
    constraints(lay, st) {
      const k = lay.skipped, bits = [`slope over ${st.slope} %: ${n0(k.slope)}`, `watercourses (${st.water} m setback): ${n0(k.water)}`];
      bits.push(!st.avoidOhl ? 'overhead line zones: not avoided (avoid pylons on)' : ohlSeen ? `overhead line zones: ${n0(k.ohl || 0)}`
        : 'overhead line zones: none drawn here (show pylons to load them)');
      if (k.hollow) bits.push(`hollows: ${n0(k.hollow)}`);
      const out = [`Kept clear (${world.plant.result()?.table?.tracker ? 'tracker rows' : 'table positions'}): ${bits.join(' · ')}.`];
      const wn = lay.notes.find(x => /No mapped water/.test(x));
      if (wn) out.push(wn);
      out.push(...lay.notes.filter(x => /tracker rows left out|row gaps stretched/.test(x)));
      if (lay.crossReview) out.push(`${n0(lay.crossReview)} tracker rows kept at 7 to 10 % across the row: for the tracker maker's review (flagged on the HUD).`);
      const r = world.plant.result(), c = r ? r.frame.en(r.compound.u, r.compound.v) : null;
      if (c) out.push(soilLine(c));
      return out;
    },
    async block({ stations, pocKv, at, trench, frame = false, modulesPerString, tempMinC }) {
      const pos = at || (() => { const h = here(); return { e: h.e - 16, n: h.n + 12 }; })();
      const c = await world.block.place({ stations, pocKv, at: pos, trench, modulesPerString, tempMinC });
      if (!c) throw Error(status('block-status') || 'the block was not placed');
      const f = frame ? world.block.bounds?.() : null;
      if (f) world.flyTo(f.e, f.n, { height: f.height });      // frame the new block so the X-ray has something in view
      return { at: pos, text: `Block: ${c.stations} station${c.stations > 1 ? 's' : ''} on ${c.rings} ring${c.rings > 1 ? 's' : ''}, ${n0(c.tables)} tables, `
        + `${n0(c.inverters)} inverters, ${n0(c.blockKwp)} kWp per station block, ${c.pocKv} kV connection.` };
    },
    clearBlock() { world.block.clear?.(); },
    // string N / temp min T: the solar block is rebuilt with the new string length (or its check redone for the new cold);
    // the reply says the cold string voltage and its headroom in the block's own module class. Null when no block is placed.
    async strings({ mps, tempMinC }) {
      if (!world.block?.strings) return null;   // before a block is placed, block-ui keeps the values in its form for the next one
      const d = await world.block.strings({ modulesPerString: mps ?? undefined, tempMinC });
      if (!d) return null;
      return `Solar block: ${d.mps} modules in series, ${n0(d.stringV)} V cold (the stricter of ${d.tempMinC} °C and Voc × 1.15: ${d.rule}; highest bin) `
        + `against ${d.systemV} V: `
        + (d.ok ? `${Math.floor(d.headroomV)} V headroom${d.borderline ? ' (borderline)' : ''}` : `OVER by ${Math.ceil(-d.headroomV)} V; at most ${d.most} in series`)
        + '. Illustrative — early design, no warranty; confirm with a qualified engineer.';
    },
    trenchRows: () => world.block.trenchRows?.() ?? null,
    // binoculars on|off, zoom 7|10, range: the lens and mount of senses-mount.mjs (binoculars-lens.mjs, binoculars-mount.mjs).
    async binoculars(arg) { if (!api.senses) throw Error('the binoculars are still loading; try again in a moment'); return api.senses.binoculars(arg); },
    async xray(on) { if (!api.xray) throw Error('the X-ray is still loading; try again in a moment'); return api.xray.set(on); },
    async piles(lay) {
      if (typeof world.piles?.auto !== 'function') return 'Piles: automatic piling is not in this build; nothing placed.';
      const r = await world.piles.auto();
      return pilesText(r, lay);
    },
    async show({ layer, on, limit }) {
      const id = LAYER[layer];
      world.setLayer(id, on);
      if (layer === 'slope' && Number.isFinite(limit)) api.live().find(l => l.id === 'slope')?.layer?.setLimit?.(limit);
      if (!on) return `${layer} hidden.`;
      if (layer === 'soil') { const h = here(); return `Ground layer shown. ${await soilAt(h.e, h.n)}`; }
      if (layer === 'slope') return `Slope shown${Number.isFinite(limit) ? ` where it is over ${limit} %` : ''} (hachures point downhill).`;
      if (layer === 'pylons') return 'Grid shown, with the overhead line zones (HSE GS6; tower heights and sag assumed).';
      return 'Watercourses shown (OS Open Rivers).';
    },
    // follow [next|back|play|pause|close]: world.sld.follow() from a module of the solar block (placed first if need be) to the grid landing.
    async follow({ step }) {
      if (!world.sld) throw Error('Follow is not in this build');
      const f = step ? await world.sld[step]() : await world.sld.follow();
      if (step === 'close') return 'Follow closed.';
      if (!f) return 'Follow: nothing to follow yet: type follow to start from the first module.';
      return `Follow the power from module ${f.panel}: stop ${f.stop + 1} of ${f.stops}, ${f.title}; cable drop so far ${fmtPct(f.cumDropPct)}`
        + ` (DC ${fmtPct(f.dcDropPct)} + AC ${fmtPct(f.acDropPct)}); ${fmtPct(f.totalDropPct)} by the end of the path;`
        + ` it ends at ${ENDS[f.ends] || 'the grid landing'}.${step ? '' : ' Type follow next, follow play or follow close.'}`;
    },
    async sld({ on }) {
      if (!world.sld) throw Error('the single-line diagram is not in this build');
      if (!on) { await world.sld.hide(); return 'Single-line diagram hidden.'; }
      await world.sld.show();
      const s = await world.sld.summary();
      return s ? `Single-line diagram: ${s.stations} stations, ${s.inverters} inverters, ${s.feeders} feeders, ${s.gridTransformers} grid transformer${s.gridTransformers === 1 ? '' : 's'};`
        + ` landing: ${s.ruleText || s.rule || 'by rule'}${s.ok ? '' : ' (check failed: see the diagram)'}.` : 'Single-line diagram shown.';
    },
    // go <place>: the words go to Find, which lists the matches; choosing one flies there.
    async find({ place }) {
      const t = doc.getElementById('find-toggle');
      if (t && t.getAttribute('aria-expanded') !== 'true') t.click();
      const q = doc.querySelector('#find input[type="search"]');
      if (!q) throw Error('Find is not open here');
      q.value = place; q.dispatchEvent(new Event('input', { bubbles: true }));
      return `Find: looking up "${place}". Choose a match in the Find panel to fly there.`;
    },
    // earthing / earth / soil rho / bonding: the earthing cartridge (earth-mount.mjs), loaded the first time one is typed.
    async earthing(arg) {
      earth ||= import('./earth-mount.mjs').then(m => m.mountEarth({ api, world, doc })).catch(e => { earth = null; throw e; });
      return (await earth).command(arg);
    },
    // hud on|off|rules: the check HUD (hud-ui.mjs); rules opens the file picker for a private rules file on this machine.
    async hud({ on, rules }) {
      const h = globalThis.worldHud;
      if (!h) throw Error('the check HUD is not loaded yet');
      if (rules) { h.pickRules(); return 'Choose a private rules file on this machine; it is read here and never sent anywhere.'; }
      h.set(on);
      const n = h.lines().length;
      return on ? `Check HUD on: ${n} failed check${n === 1 ? '' : 's'} shown${n ? '' : ' (place the solar block to check it)'}.` : 'Check HUD off.';
    },
    async go(arg, lay) {
      if (Number.isFinite(arg.e)) { world.flyTo(arg.e, arg.n, { height: 120 }); return `Flying to E ${n0(arg.e)} N ${n0(arg.n)}.`; }
      const r = world.plant.result(), f = world.block?.bounds?.();
      // go block: the placed solar block (also go block N when no plant is laid out), framed whole.
      if (!arg.compound && (arg.block == null || !r) && f) {
        world.flyTo(f.e, f.n, { height: f.height });
        return `Flying to the solar block: ${n0(f.w)} m × ${n0(f.h)} m, seen from ${n0(f.height)} m up. Type xray on to see the trenches.`;
      }
      if (arg.block === null) throw Error('no solar block placed yet: type block 10mva first');
      if (!r) throw Error('no plant laid out yet: type plant 50mw first, or block 10mva for a solar block');
      const T = r.table, en = (u, v) => r.frame.en(u, v);
      if (arg.compound) { const [e, n] = en(r.compound.u, r.compound.v); world.flyTo(e, n, { height: 120 }); return `Flying to the compound, E ${n0(e)} N ${n0(n)}.`; }
      const k = arg.block - 1, st = r.stations[k];
      if (!st) throw Error(`there are ${r.stations.length} blocks; you typed block ${arg.block}`);
      if (!arg.table) { const [e, n] = en(st.u, st.v); world.flyTo(e, n, { height: 90 }); return `Flying to block ${arg.block} (${st.id}, ${st.mva} MVA, ${st.tables} tables).`; }
      if (!(arg.table >= 1 && arg.table <= st.tables)) throw Error(`block ${arg.block} has tables 1 to ${st.tables}; you typed ${arg.table}`);
      const q = (r.stations.slice(0, k).reduce((s, x) => s + x.tables, 0) + arg.table - 1) * 6;
      const [e, n] = en(r.tables[q] + T.lenU / 2, r.tables[q + 1] + T.depth / 2);
      world.flyTo(e, n, { height: 35 });
      return `Flying to block ${arg.block} table ${arg.table} of ${st.tables}, E ${n0(e)} N ${n0(n)}${lay ? '' : ''}.`;
    }
  };
  function soilLine([e, n]) {
    if (!soil) return 'Soil type from BGS 1:625k: still loading; type show soil to read it.';
    return soil.none ? soilEffect(null) : soilEffect(groundAt(soil, e, n));
  }
}

// The reply to "piles auto": world.piles.auto() gives { plant, block } pile summaries (plant-piles.mjs, block-build.mjs).
export function pilesText(r, lay) {
  const p = r?.plant, b = r?.block;
  if (!p && !b) return lay ? 'Piles: no tables to pile yet.' : 'Piles: no plant laid out yet: type plant 50mw first.';
  const out = [];
  if (p) {
    const flags = (p.revealHigh || 0) + (p.revealLow || 0), warn = (p.revealWarn || 0) + (p.revealFail || 0);
    const how = p.system === 'tracker' ? ' along the tracker tubes' : p.posts === 1 ? ', one post per table' : ', front and rear posts';
    out.push(`Piles placed automatically: ${n0(p.piles)} (${n0(p.perMWp)} per MWp)${how}`
      + `, ${n0(p.totalPileLengthM)} m of pile; ${n0(p.proofTests)} proof tests.`);
    const bits = [];
    if (flags) bits.push(`${n0(flags)} outside the reveal band`);
    if (warn) bits.push(`${n0(warn)} standing high above their design height`);
    if (p.gradingTables) bits.push(`${n0(p.gradingTables)} tables need grading`);
    if (p.cannotFollow) bits.push(`${n0(p.cannotFollow)} tracker rows cannot follow the ground`);
    out.push(bits.length ? `Flagged: ${bits.join(' · ')} (see the Plant readout).` : 'No piles flagged.');
  }
  if (b && Number.isFinite(b.count ?? b.piles)) out.push(`Solar block: ${n0(b.count ?? b.piles)} piles.`);
  return out.join(' ');
}