Skip to content

A plugin marketplace for GitHub Copilot and other AI agents that provides Power Platform development plugins, including reusable skills, agents, and commands for building and deploying solutions.

32.4 KB
Raw Blame
'use strict';
// App Spec -> `model-app-plan.md`: the readable design document a human reviews before the build and
// keeps alongside the app afterwards.
//
// WHY this is a script and not a prose instruction: `model-app-plan.md` used to be written freehand
// ("write a short human-readable summary… include: tables, relationships, artifact COUNTS"). Testers
// reported the result was an abbreviated plan with no durable, reviewable detail — and a freehand doc
// drifts from the spec the moment either changes. Rendering it from `app-spec.json` makes the document
// (a) complete, (b) always in sync, and (c) regenerable at any time:
//     node scripts/write-app-spec-doc.js --spec @<dir>/app-spec.json
//
// This is NOT app-preview.js. That renders an ASCII console preview for the in-conversation approval
// gate (form wireframes included); this renders durable Markdown for review and archival. They share
// the spec but not the audience, so they intentionally differ in shape.
//
// PURE: no I/O. The CLI wrapper (scripts/write-app-spec-doc.js) owns writing the file.

const lc = (s) => String(s || '').toLowerCase();
const { labelText, lookupLabelFor } = require('./app-spec.js');
const { appAccessCondition, appAccessModel, rolesOfPersonas, roleGrantRefusals, uncoveredTables, uncoveredTablesCallout } = require('./app-access.js');

// Every collection read below goes through these. The renderer runs against WORK-IN-PROGRESS specs
// during authoring — a half-typed `jobs: "x"`, a null entry left by an edit, a `dataSources` written
// as a bare string — and a crash here kills the authoring flow. Shape errors are validateAppSpec's
// job to report; this module's job is to render whatever it is given without throwing.
const arr = (v) => (Array.isArray(v) ? v : []);
const objs = (v) => arr(v).filter((x) => x && typeof x === 'object' && !Array.isArray(x));

// Table cells: a spec string (app description, job name, page purpose) may contain a `|` or a line
// break, either of which silently breaks a Markdown table row. Flatten and escape for cells only —
// prose lines elsewhere keep their original text.
function cell(value, fallback = '—') {
  const s = String(value == null ? '' : value).replace(/[\r\n]+/g, ' ').replace(/\|/g, '\\|').replace(/\s+/g, ' ').trim();
  return s || fallback;
}

const text = (value, fallback = '') => {
  const s = String(value == null ? '' : value).replace(/[\r\n]+/g, ' ').replace(/\s+/g, ' ').trim();
  return s || fallback;
};

// Where a sub-grid says it goes (`tab` / `column` / `after` / `before`), worded for the plan; empty for a grid
// that says nothing, which is how every grid read before the keys existed. A value of the wrong type is skipped:
// reporting it is validateAppSpec's job, not the plan's.
function gridPlacement(sg) {
  const parts = [];
  if (typeof sg.tab === 'string' && sg.tab.trim()) parts.push(`tab ${sg.tab.trim()}`);
  if (Number.isInteger(sg.column)) parts.push(`column ${sg.column}`);
  if (typeof sg.after === 'string' && sg.after.trim()) parts.push(`after ${sg.after.trim()}`);
  if (typeof sg.before === 'string' && sg.before.trim()) parts.push(`before ${sg.before.trim()}`);
  return parts.join(', ');
}

function overview(spec) {
  const app = spec.app || {};
  const out = [`# ${text(app.name, 'Model-driven app')} — design`, ''];
  if (app.description) out.push(text(app.description), '');
  // #583: the routing description is reviewed beside the tile text, since the two now say different
  // things — the tile stays short, and the routing text says who the app is for and what it leaves out.
  if (app.aiDescription) out.push(`**Routing description:** ${text(app.aiDescription)}`, '');
  out.push(
    'Generated from `app-spec.json` by `scripts/write-app-spec-doc.js`. **Regenerate rather than',
    'hand-edit** — `app-spec.json` is the source of truth, so a manual edit here is lost on the next',
    'run and silently disagrees with what actually builds.',
    ''
  );
  return out;
}

function environment(spec, opts) {
  const out = ['## Environment', ''];
  const sol = spec.solution && typeof spec.solution === 'object' ? spec.solution : {};
  const rows = [
    ['Environment', opts.envUrl],
    ['Solution', sol.uniqueName || sol.displayName],
    ['Publisher prefix', sol.publisherPrefix],
    ['App unique name', spec.app && spec.app.uniqueName],
  ].filter(([, v]) => v);
  if (!rows.length) return [];
  out.push('| Setting | Value |', '|---|---|');
  for (const [k, v] of rows) out.push(`| ${cell(k)} | ${cell(v)} |`);
  out.push('');
  return out;
}

// Personas -> jobs-to-be-done. This section leads the document on purpose: the jobs are what the app
// EXISTS to do, so a reviewer should read them before the tables. Testers found that enumerating jobs
// explicitly is what makes the design complete — without it, surfaces get missed.
function jobsSection(spec) {
  const personas = objs(spec.personas);
  const out = ['## Jobs to be done', ''];
  if (!personas.length) {
    // All the spec can say here is what it does NOT author: who can open the app is decided by the roles associated with it, and a spec with no
    // personas[] leaves those as they are (the Security section says what that means, and what the environment holds when it was read).
    out.push(
      '> ⚠ No `personas[]` were captured, so this app has no recorded jobs-to-be-done and this spec authors no security role.',
      '> That does not say who can open the app — see **Security**. Capture who uses the app and what each of them needs',
      '> to get done, then regenerate this document.',
      ''
    );
    return out;
  }
  out.push('| Persona | Job to be done | Surfaces that satisfy it |', '|---|---|---|');
  for (const p of personas) {
    for (const j of objs(p.jobs)) {
      const covering = arr(j.surfaces).map((s) => cell(s)).join(', ');
      out.push(`| ${cell(p.persona)} | ${cell(j.name)}${j.description ? ` — ${cell(j.description, '')}` : ''} | ${covering || '_not mapped_'} |`);
    }
  }
  out.push('');
  // Deliberate exclusions, rendered beside the traceability table so scope is REVIEWED rather than
  // assumed (#583). What an app leaves out is what distinguishes two apps built over the same tables,
  // and a reviewer cannot approve an omission they were never shown.
  const excluded = personas
    .map((p) => ({ persona: p.persona, items: arr(p.excludes).filter((x) => typeof x === 'string' && x.trim()) }))
    .filter((e) => e.items.length);
  if (excluded.length) {
    out.push('**Deliberately out of scope**', '');
    for (const e of excluded) {
      for (const x of e.items) out.push(`- ${cell(e.persona)} — ${cell(x)}`);
    }
    out.push('');
  }
  const unmapped = personas.flatMap((p) => objs(p.jobs).filter((j) => !arr(j.surfaces).length).map((j) => `${p.persona} → ${j.name}`));
  if (unmapped.length) {
    out.push(
      `> ⚠ ${unmapped.length} job(s) are not mapped to a surface, so nothing in this app demonstrably`,
      '> lets that persona do the job. Either add the surface, or record which existing one covers it.',
      ''
    );
  }
  return out;
}

function dataModel(spec) {
  const out = ['## Data model', ''];
  const entities = objs(spec.entities);
  if (!entities.length) { out.push('_No tables._', ''); return out; }
  for (const e of entities) {
    const reused = e.existing ? ' _(existing table — reused, not created)_' : '';
    out.push(`### ${text(labelText(e.displayName, spec && spec.languageCode), e.schemaName)} \`${lc(e.schemaName)}\`${reused}`, '');
    if (e.description) out.push(text(e.description), '');
    // The nav icon is something the user APPROVES, and at plan time the SVG may not exist yet — so
    // show what the glyph will DEPICT, not the web-resource name (which tells a reviewer nothing).
    if (e.iconDescription || e.vectorIcon || e.icon) {
      const depicts = e.iconDescription ? text(e.iconDescription) : '_no description — the user cannot approve an icon they cannot picture_';
      out.push(`**Nav icon:** ${depicts}`, '');
    }
    out.push('| Column | Type | Notes |', '|---|---|---|');
    const pa = e.primaryAttribute && typeof e.primaryAttribute === 'object' ? e.primaryAttribute : null;
    if (pa) out.push(`| ${cell(labelText(pa.displayName, spec && spec.languageCode) || pa.schemaName)} | ${cell(pa.type, 'Text')} | primary name${pa.autoNumberFormat ? `, auto-number \`${cell(pa.autoNumberFormat)}\`` : ''} |`);
    for (const c of objs(e.columns)) {
      const notes = [];
      if (c.required === true) notes.push('required');
      else if (c.required === 'recommended') notes.push('recommended');
      // `o.label` is the legacy object-option shape. A LOCALIZED option is also an object but has no
      // `label` key, so the legacy branch rendered every one as an empty cell — resolve through
      // labelText first and only then fall back.
      if (c.options) notes.push(`choices: ${arr(c.options).map((o) => cell(labelText(o, spec && spec.languageCode) || (o && typeof o === 'object' ? o.label : o))).join(', ')}`);
      if (c.globalChoice) notes.push(`global choice \`${cell(c.globalChoice)}\``);
      out.push(`| ${cell(labelText(c.displayName, spec && spec.languageCode) || c.schemaName)} | ${cell(c.type, 'Text')} | ${notes.join('; ') || '—'} |`);
    }
    out.push('');
    if (e.hasNotes) out.push('Notes/timeline enabled.', '');
  }
  const rels = objs(spec.relationships);
  if (rels.length) {
    // `Label` is what the lookup column (and its form field) is called: the author's lookup.displayName, else
    // the parent table's name — see lookupLabelFor — so the plan reads "Project", not a logical name.
    out.push('### Relationships', '', '| Kind | From | To | Lookup | Label |', '|---|---|---|---|---|');
    for (const r of rels) {
      if (r.type === 'ManyToMany') out.push(`| N:N | ${cell(lc(r.entity1))} | ${cell(lc(r.entity2))} | — | — |`);
      else out.push(`| 1:N | ${cell(lc(r.referenced))} | ${cell(lc(r.referencing))} | ${cell(r.lookup && r.lookup.schemaName)} | ${cell(labelText(lookupLabelFor(spec, r).label, spec && spec.languageCode))} |`);
    }
    out.push('');
  }
  return out;
}

// Every user-facing surface, grouped by kind, so a reviewer can see what the app actually presents —
// and, critically, whether generative pages were considered at all (the genpage-first policy makes
// them the default for any non-record surface, and testers found they were being skipped silently).
function surfaces(spec) {
  const out = ['## Surfaces', ''];
  const pages = objs(spec.pages);
  const forms = objs(spec.forms);
  const views = objs(spec.views);
  const charts = objs(spec.charts);
  const dashboards = objs(spec.dashboards);

  out.push('### Generative pages', '');
  if (!pages.length) {
    out.push(
      '> ⚠ No generative pages. Per the genpage-first policy, any non-record surface — an overview or',
      '> landing page, a dashboard, an analytics view, a guided/wizard flow — should be a generative',
      "> page rather than a classic dashboard. If this app genuinely has only record CRUD, that's",
      '> fine; otherwise a surface is missing.',
      ''
    );
  } else {
    out.push('| Page | Key | Purpose | Reads | Navigates to | State |', '|---|---|---|---|---|---|');
    for (const p of pages) {
      const state = p.source && p.source.kind === 'tsx' ? `built (\`${cell(p.source.codeFile)}\`)` : 'intent (code not yet generated)';
      const nav = objs(p.navigatesTo).map((n) => cell(n.targetKey)).join(', ');
      out.push(`| ${cell(p.name)} | \`${cell(p.key)}\` | ${cell(p.purpose)} | ${cell(arr(p.dataSources).map(lc).join(', '))} | ${nav || '—'} | ${state} |`);
    }
    out.push('');
  }

  out.push('### Forms', '');
  if (!forms.length) out.push('_No forms._', '');
  else {
    out.push('| Form | Table | Type | Layout | Sub-grids |', '|---|---|---|---|---|');
    for (const f of forms) {
      const sg = objs(f.subgrids).map((s) => { const where = gridPlacement(s); return `${cell(lc(s.childEntity))}${where ? ` (${cell(where)})` : ''}`; }).join(', ');
      // `formType` is the canonical field (Main / QuickCreate / QuickView), defaulting to Main —
      // NOT `type`. Reading the wrong one rendered every QuickCreate/QuickView form as "main",
      // hiding a real difference in what the form does.
      const kind = f.formType === undefined ? 'Main' : f.formType;
      // An authored `tabs[]` IS the layout — reporting "auto" for it misstates the design, and the
      // build treats an explicit layout differently (it prunes fields the author dropped).
      const tabs = objs(f.tabs);
      const layout = tabs.length ? `explicit (${tabs.length} tab${tabs.length === 1 ? '' : 's'})` : (f.layout || 'auto');
      out.push(`| ${cell(f.name)} | ${cell(lc(f.entity))} | ${cell(kind)} | ${cell(layout)} | ${sg || '—'} |`);
    }
    out.push('');
    const withQv = forms.filter((f) => objs(f.quickViews).length);
    if (withQv.length) {
      out.push('Quick-view cards embedded on a form:', '');
      for (const f of withQv) {
        for (const qv of objs(f.quickViews)) out.push(`- **${cell(f.name)}** shows \`${cell(qv.form)}\` via lookup \`${cell(qv.lookup)}\``);
      }
      out.push('');
    }
    const withEvents = forms.filter((f) => objs(f.events).length);
    if (withEvents.length) {
      out.push('Form scripts:', '');
      for (const f of withEvents) {
        for (const ev of objs(f.events)) out.push(`- **${cell(f.name)}** — \`${cell(ev.event)}\`${ev.attribute ? ` on \`${cell(ev.attribute)}\`` : ''} → \`${cell(ev.function)}\` (\`${cell(ev.library)}\`)`);
      }
      out.push('');
    }
  }

  out.push('### Views', '');
  if (!views.length) out.push('_No views._', '');
  else {
    // Filters and sort decide what a user actually sees in a view ("my open tickets, newest first"),
    // so a doc that lists only columns lets two materially different views read identically.
    // Canonical shapes (references/app-spec-schema.md → views[]): sort is `{attr, dir}` and a filter
    // is `{attr, op, value?|values?}` — NOT `{column, descending}` / `{column, operator}`.
    out.push('| View | Table | Columns | Filters | Sort |', '|---|---|---|---|---|');
    for (const v of views) {
      const filters = objs(v.filters).map((f) => {
        const val = f.values !== undefined ? arr(f.values).join('/') : f.value;
        return `${cell(f.attr)} ${cell(f.op, '=')}${val === undefined || val === '' ? '' : ` ${cell(val)}`}`;
      }).join('; ');
      const sort = objs(v.sort).map((s) => `${cell(s.attr)} ${cell(s.dir, 'asc')}`).join(', ');
      const scope = v.activeOnly === false ? 'all records' : '';
      out.push(`| ${cell(v.name)} | ${cell(lc(v.entity))} | ${cell(arr(v.columns).join(', '))} | ${[filters, scope].filter(Boolean).join('; ') || '—'} | ${sort || '—'} |`);
    }
    out.push('');
  }

  if (charts.length) {
    out.push('### Charts', '', '| Chart | Table | Type | Measure | Grouped by |', '|---|---|---|---|---|');
    for (const c of charts) out.push(`| ${cell(c.name)} | ${cell(lc(c.entity))} | ${cell(c.chartType)} | ${cell(c.measure, 'count')} | ${cell(c.groupBy)} |`);
    out.push('');
  }
  if (dashboards.length) {
    out.push('### Classic dashboards', '', '| Dashboard | Tiles |', '|---|---|');
    for (const d of dashboards) out.push(`| ${cell(d.name)} | ${arr(d.tiles).length} |`);
    out.push('');
  }

  // Commands are actions a user can take — omitting them let two specs differing by an "Escalate"
  // button render identically, which is exactly the kind of behaviour a reviewer signs off on.
  const commands = objs(spec.commands);
  if (commands.length) {
    out.push('### Command-bar buttons', '', '| Button | Table | Kind | Runs |', '|---|---|---|---|');
    for (const c of commands) {
      const kind = c.type || 'Button';
      const runs = c.function ? `\`${cell(c.function)}\`${c.library ? ` (\`${cell(c.library)}\`)` : ''}` : (objs(c.children).length ? `${objs(c.children).length} child button(s)` : '—');
      out.push(`| ${cell(c.label || c.name)} | ${cell(lc(c.entity))} | ${cell(kind)} | ${runs} |`);
      for (const ch of objs(c.children)) {
        out.push(`| ↳ ${cell(ch.label || ch.name)} | ${cell(lc(c.entity))} | child | ${ch.function ? `\`${cell(ch.function)}\`` : '—'} |`);
      }
    }
    out.push('');
  }
  // Existing buttons whose hidden/disabled flags the build sets (commandStates[]). Only a spec that declares some gets this section. A flag the
  // entry leaves out is one the build does not touch, so it reads "unchanged", not a value.
  const commandStates = objs(spec.commandStates);
  if (commandStates.length) {
    const flag = (v) => (v === true ? 'yes' : v === false ? 'no' : 'unchanged');
    out.push('### Existing command buttons shown or hidden', '', '| Row | Table | Bar | Scope | Hidden | Disabled |', '|---|---|---|---|---|---|');
    for (const s of commandStates) {
      out.push(`| \`${cell(s.rowId)}\` | ${cell(lc(s.entity))} | ${cell(s.location)} | ${cell(s.scope)} | ${flag(s.hidden)} | ${flag(s.disabled)} |`);
    }
    out.push('', '> Each change is applied only with approval of that exact row, and publishing its table changes the button for every app that shows the table.', '');
  }
  return out;
}

function navigation(spec) {
  const areas = objs(spec.appShell && spec.appShell.areas);
  if (!areas.length) return [];
  // A sitemap icon is chrome the user approves, so show its depiction inline. An `entity` subarea's
  // nav glyph comes from the TABLE's own icon, not the subarea — so point at the table rather than
  // repeating a description that would not be the one actually rendered.
  const depiction = (o) => (o && o.iconDescription ? ` — icon: ${text(o.iconDescription)}` : (o && (o.icon || o.vectorIcon) ? ' — icon: _not described_' : ''));
  const out = ['## Navigation', ''];
  for (const a of areas) {
    out.push(`- **${text(a.label, '(area)')}**${depiction(a)}`);
    for (const g of objs(a.groups)) {
      out.push(`  - ${text(g.label, '(group)')}${depiction(g)}`);
      for (const sa of objs(g.subAreas)) {
        const target = sa.entity ? `table \`${lc(sa.entity)}\`` : sa.page ? `page \`${sa.page}\`` : sa.dashboard ? `dashboard \`${sa.dashboard}\`` : sa.url ? `URL ${sa.url}` : '(no target)';
        const icon = sa.entity ? ' — icon: the table\'s own' : depiction(sa);
        out.push(`    - ${text(sa.title, target)} → ${target}${icon}`);
      }
    }
  }
  out.push('');
  return out;
}

// The access model, rendered as what each persona's role can actually DO. A reviewer cannot approve
// an access model they cannot see, and this is the durable copy of it.
//
// Scope is unioned PER (entity, ACCESS), never per entity. That mirrors how the SDK actually applies
// a role (see evals/model-apps/app-builder/lib/facts.js `unionPrivileges`): collapsing to one scope
// per table would render `read@organization` + `write@user` as "read, write @ organization" and
// OVERSTATE write access. A document that misreports the access model is worse than no document.
const SCOPE_RANK = { user: 0, businessunit: 1, parentchild: 2, organization: 3 };
const scopeRank = (s) => {
  const r = SCOPE_RANK[String(s || 'user').toLowerCase()];
  return r === undefined ? 0 : r;
};

// The privileges a list declares, per table and per ACCESS: table -> access -> most permissive declared scope.
function unionPrivileges(privileges) {
  const byEntity = new Map();
  for (const pr of privileges) {
    if (!pr || typeof pr !== 'object') continue;
    const entity = lc(pr.entity);
    if (!entity) continue;
    let byAccess = byEntity.get(entity);
    if (!byAccess) { byAccess = new Map(); byEntity.set(entity, byAccess); }
    const scope = String(pr.scope || 'user');
    for (const a of Array.isArray(pr.access) ? pr.access : []) {
      const prev = byAccess.get(a);
      if (prev === undefined || scopeRank(scope) > scopeRank(prev)) byAccess.set(a, scope);
    }
  }
  return byEntity;
}

// The rows of a privilege table, one per (table, scope). The accesses that share a scope are grouped so the common case stays one row, while a genuine
// split (read@organization + write@user) renders as two — visibly different access levels.
function privilegeRows(byEntity) {
  const rows = [];
  for (const [entity, byAccess] of byEntity) {
    const byScope = new Map();
    for (const [access, scope] of byAccess) {
      if (!byScope.has(scope)) byScope.set(scope, []);
      byScope.get(scope).push(access);
    }
    const scopes = [...byScope.keys()].sort((a, b) => scopeRank(b) - scopeRank(a));
    for (const scope of scopes) rows.push({ entity, access: byScope.get(scope).join(', '), scope });
  }
  return rows;
}

// What in the spec may be an associated role, for the table of the roles the app has. A role is one of (name, business unit) and the read carries no business unit,
// so a grant that names only a name, and a persona (whose role is made in its own business unit), match a role by name alone: "may be", never "is". Only a grant that
// names the role's id says what the role is — and what a build does for a persona is said of the persona's role ("if it is"), not of the role a name happens to match.
function roleSources(role) {
  if (!role.sources.length) return 'not touched by this spec';
  return role.sources.map((s) => {
    if (s.kind === 'roleGrant') return s.certain ? 'extended by `roleGrants[]`' : 'may be the role `roleGrants[]` extends (matched by name only)';
    return s.getsApp
      ? `may be the role of persona \`${s.label}\` (matched by name); if it is, a build keeps this association`
      : `may be the role of persona \`${s.label}\` (matched by name, \`appAccess: false\`); if it is, a build removes this association`;
  }).join('; ');
}

// WHO CAN OPEN THE APP, kept apart from who can use a table. The roles associated with the app decide which users see it. A build associates the roles of the
// personas that get the app (`appAccess` is not false), removes the association of those that opt out, and leaves every other role's as it is; the SPEC alone says
// nothing more, and — when the environment was read (--env) — the ENVIRONMENT says what the app has. Every sentence here follows the model (lib/app-access.js
// appAccessModel): which personas get the app, which role is which, whether the app is known to be administrator-only. That claim is made only on the evidence of a
// read — an app the environment has with no role associated with it, or one neither layer has yet — and only while no persona gets the app.
function appAccessSection(spec, model, opts) {
  const { gets, optedOut } = model.personas;
  const out = ['### Access to the app', ''];
  const where = opts.envUrl ? text(opts.envUrl) : 'the environment';
  const app = model.appUnique ? `The app \`${model.appUnique}\`` : 'The app';
  // Why a build associates no role of its own with the app, when no persona gets it.
  const noneBecause = optedOut.length ? 'its personas are data-only: `appAccess: false`' : 'it authors no role';
  if (model.state === 'read' && model.roles.length) {
    out.push(`${app} already exists in ${where} and these roles are associated with it, so their users can open it:`, '', '| Role | In this spec |', '|---|---|');
    for (const r of model.roles) out.push(`| ${cell(r.name || r.roleId)} | ${cell(roleSources(r))} |`);
    const changes = [];
    if (gets.length) changes.push(`associates the app with ${rolesOfPersonas(gets)}`);
    if (optedOut.length) changes.push(`removes it from ${rolesOfPersonas(optedOut)} (\`appAccess: false\`) where it is associated`);
    const build = changes.length ? `A build with this spec ${changes.join(', ')}, and leaves every other association as it is.` : 'A build with this spec leaves these associations as they are.';
    out.push('', `${build} Opening the app is one thing and using a table another: the privileges on the tables this spec introduces are declared separately, below.`, '');
  } else if (model.state === 'read') {
    out.push(`${app} exists in ${where}, but no role is associated with it, so only administrators and other makers (System Customizer, Environment Maker) can open it. ${gets.length ? `A build with this spec associates the app with ${rolesOfPersonas(gets)}.` : `This spec associates none with it (${noneBecause}), so that does not change.`}`, '');
  } else if (model.state === 'absent') {
    out.push(gets.length
      ? `${app} does not exist in ${where} yet. A build creates it and associates the System Administrator role with it, and ${rolesOfPersonas(gets)}.`
      : `${app} does not exist in ${where} yet. A build creates it and associates the System Administrator role with it; this spec associates no other role (${noneBecause}), so users without a maker-level role cannot open it until a role is associated with it.`, '');
  } else if (model.state === 'unreadable') {
    out.push(`> ⚠ The roles associated with the app could not be read from ${where} (${text(model.reason)}), so who can open it was not checked.`, '', appAccessCondition(spec), '');
  } else {
    out.push(appAccessCondition(spec), '', 'The roles associated with the app were not read: pass `--env <envUrl>` to read them.', '');
  }
  return out;
}

// A grant that names a role only by name, beside the roles of that name the app has: which of them the grant extends, if any, is for the business unit to say.
function namedGrantNote(grant) {
  const n = grant.candidates.length;
  const have = n === 1 ? '1 associated role has that name' : `${n} associated roles have that name`;
  const ids = `${n === 1 ? 'id' : 'ids'} ${grant.candidates.map((c) => text(c.roleId)).join(', ')}`;
  return `Role "${text(grant.label)}" is named without a role id: ${have} (${ids}), and roles are per business unit, so ${n === 1 ? 'it may not be' : 'at most one of them is'} the role this grant extends. Pin the role with \`roleId\` to be sure.`;
}

// roleGrants[]: privileges ADDED to roles that already exist. Shown with the role they extend — the persona sections above show the roles this spec owns — and,
// when the environment was read, with how each grant stands to the roles the app has: a grant gives a role a table, not the app.
//
// An entry that does not name exactly one role is refused by the gate a build runs, and is shown as that refusal, in the gate's own words
// (roleGrantRefusals), never as rows: listed with the rest, it read as a grant the build would make. Shape errors are otherwise validateAppSpec's to
// report and not this document's — but this one is the document's own claim about what the build does.
function roleGrantsSection(spec, model) {
  const grants = objs(spec.roleGrants);
  if (!grants.length) return [];
  const refusals = roleGrantRefusals(spec);
  const refused = new Set(refusals.map((r) => r.index));
  const rows = [];
  arr(spec.roleGrants).forEach((g, index) => {
    if (!g || typeof g !== 'object' || Array.isArray(g) || refused.has(index)) return;
    const role = typeof g.role === 'string' && g.role.trim() ? g.role.trim() : (typeof g.roleId === 'string' ? g.roleId.trim() : '');
    for (const r of privilegeRows(unionPrivileges(arr(g.privileges)))) rows.push({ role, ...r });
  });
  const out = ['### Roles extended by `roleGrants[]`', '', 'These roles already exist. The build ADDS the privileges below and removes none, and a grant does not associate the role with the app. After a build, `verify-model-app.js` checks that each role holds them.', ''];
  if (rows.length) {
    out.push('| Role | Table | Access | Scope |', '|---|---|---|---|');
    for (const r of rows) out.push(`| ${cell(r.role)} | ${cell(r.entity)} | ${cell(r.access)} | ${cell(r.scope)} |`);
    out.push('');
  } else if (!refusals.length) {
    out.push('_No privileges declared._', '');
  }
  for (const { messages } of refusals) {
    for (const message of messages) out.push(`> ✗ ${text(message)}`, '');
  }
  if (refusals.length) out.push('The build refuses this spec until each of these entries names one role, so their privileges are not listed.', '');
  if (model.state === 'read') {
    for (const g of model.grants) {
      if (g.status === 'not-associated') out.push(`> ⚠ Role "${text(g.label)}" is not among the roles associated with the app, so its users get these privileges but may not be able to open the app.`, '');
      else if (g.status === 'maybe') out.push(namedGrantNote(g), '');
    }
  }
  return out;
}

function security(spec, opts) {
  const personas = Array.isArray(spec.personas) ? spec.personas.filter((p) => p && typeof p === 'object') : [];
  const model = appAccessModel(spec, opts.appAccess);
  const out = ['## Security', ''];
  if (!personas.length) out.push('This spec authors no security role (`personas[]` is empty).', '');
  for (const p of personas) {
    out.push(`### Role: ${text(p.persona)}`, '');
    out.push(p.appAccess === false ? '_Data-only — the app is **not** granted to this role._' : 'The app is granted to this role, so it opens for this persona.', '');

    const privileges = [];
    for (const j of Array.isArray(p.jobs) ? p.jobs : []) {
      if (j && typeof j === 'object') privileges.push(...(Array.isArray(j.privileges) ? j.privileges : []));
    }
    privileges.push(...(Array.isArray(p.additionalPrivileges) ? p.additionalPrivileges : []));
    const rows = privilegeRows(unionPrivileges(privileges));
    if (rows.length) {
      out.push('| Table | Access | Scope |', '|---|---|---|');
      for (const r of rows) out.push(`| ${cell(r.entity)} | ${cell(r.access)} | ${cell(r.scope)} |`);
      out.push('');
    } else {
      out.push('_No privileges declared._', '');
    }
  }
  // The persona sections already say, per role, whether the app is granted to it; the section on the app is for what they cannot: a spec that gives the app to
  // no role, and what the environment says.
  if ((!model.personas.gets.length && !model.personas.optedOut.length) || model.state !== 'unread') out.push(...appAccessSection(spec, model, opts));
  out.push(...roleGrantsSection(spec, model));
  const withoutAccess = uncoveredTables(spec);
  if (withoutAccess.length) out.push('### Tables with no access declared', '', `> ⚠ ${uncoveredTablesCallout(withoutAccess)}`, '');
  return out;
}

function sampleData(spec) {
  const sd = spec.sampleData;
  if (!sd || typeof sd !== 'object') return [];
  const rows = Object.entries(sd).filter(([, v]) => Array.isArray(v));
  if (!rows.length) return [];
  const out = ['## Sample data', '', '| Table | Records |', '|---|---|'];
  for (const [entity, records] of rows) out.push(`| ${cell(entity)} | ${records.length} |`);
  out.push('');
  return out;
}

function designContract(spec) {
  const d = spec.design;
  if (!d || typeof d !== 'object' || Array.isArray(d)) return [];
  const entries = Object.entries(d).filter(([, v]) => v != null && String(v).trim());
  if (!entries.length) return [];
  const out = ['## Design contract', '', '| Token | Value |', '|---|---|'];
  for (const [k, v] of entries) out.push(`| ${cell(k)} | ${cell(v)} |`);
  out.push('', 'These tokens are threaded to every generative page so the app looks consistent.', '');
  return out;
}

function aiFeatures(spec) {
  const ai = spec.ai;
  if (!ai || typeof ai !== 'object' || Array.isArray(ai)) return [];
  const on = Object.entries(ai).filter(([, v]) => v && v !== 'off');
  if (!on.length) return [];
  const out = ['## AI features', '', '| Feature | Setting |', '|---|---|'];
  for (const [k, v] of on) out.push(`| ${cell(k)} | ${cell(typeof v === 'object' ? JSON.stringify(v) : v)} |`);
  out.push('', '_All AI features are admin-gated; the build preflights and skips anything not enabled._', '');
  return out;
}

/**
 * Render an App Spec as a readable Markdown design document.
 * @param {object} spec migrated App Spec
 * @param {object} [opts] { envUrl, appAccess } `appAccess` is the answer of lib/app-roles.js `appRolesOf` for the app when the environment was read, else absent
 * @returns {string} markdown
 */
function renderAppSpecDoc(spec, opts = {}) {
  const s = spec || {};
  const lines = [
    ...overview(s),
    ...environment(s, opts),
    ...jobsSection(s),
    ...dataModel(s),
    ...surfaces(s),
    ...navigation(s),
    ...security(s, opts),
    ...sampleData(s),
    ...designContract(s),
    ...aiFeatures(s),
  ];
  return lines.join('\n').replace(/\n{3,}/g, '\n\n').trimEnd() + '\n';
}

module.exports = { renderAppSpecDoc };