@geml/geml 1.5.0 → 1.6.0

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.
package/dist/history.d.ts CHANGED
@@ -27,14 +27,14 @@ interface History {
27
27
  }
28
28
  /** Reconstruct the content of revision `targetId`. */
29
29
  export declare function reconstruct(h: History, targetId: string): string;
30
- export interface CommitOpts {
30
+ export interface SaveOpts {
31
31
  gemlPath: string;
32
32
  historyPath: string;
33
33
  summary: string;
34
34
  author?: string;
35
35
  at?: Date;
36
36
  }
37
- export declare function commit(o: CommitOpts): {
37
+ export declare function save(o: SaveOpts): {
38
38
  id: string;
39
39
  hash: string;
40
40
  };
@@ -63,7 +63,9 @@ export interface RevisionInfo {
63
63
  current: boolean;
64
64
  }
65
65
  /** Is the working file byte-identical to the sidecar's tip revision? False
66
- * means uncommitted drift (e.g. an earlier commit attempt was refused). */
66
+ * means unsaved drift (e.g. an earlier save was refused by the round-trip
67
+ * gate). Both write paths gate on this so a no-change `save` appends nothing:
68
+ * `geml mcp` before each write (mcp.ts) and `geml history save` (geml.ts). */
67
69
  export declare function isCurrent(historyPath: string, gemlPath: string): boolean;
68
70
  /** Revisions newest-first, each tagged with the `-N` offset that selects it. */
69
71
  export declare function listRevisions(historyPath: string): RevisionInfo[];
@@ -74,11 +76,12 @@ export declare function listRevisions(historyPath: string): RevisionInfo[];
74
76
  *
75
77
  * There is exactly one selector grammar — `0` (the tip), `-N` (N revisions
76
78
  * back), or an unambiguous revision id (prefix, suffix, or exact) — and it is
77
- * the grammar `history log` prints in its first column, so its output is
78
- * copy-pasteable into `revert --rev`, `history show`, and `history restore`
79
- * alike. Keeping this in one function is what makes that true: it used to be
80
- * written twice, and the copy in `restore` never grew the `0`/`-N` arm, so the
81
- * selectors `history log` advertised were rejected by `history show`. */
79
+ * the grammar `history get` prints in its first column, so its output is
80
+ * copy-pasteable into `revert --rev`, `history get <rev>`, and
81
+ * `history restore` alike. Keeping this in one function is what makes that
82
+ * true: it used to be written twice, and the copy in `restore` never grew the
83
+ * `0`/`-N` arm, so the selectors the revision list advertised were rejected by
84
+ * the command that printed a revision. */
82
85
  export declare function resolveRevision(h: History, selector: string): string;
83
86
  export declare function resolveContent(historyPath: string, selector: string): {
84
87
  id: string;
package/dist/history.js CHANGED
@@ -1,9 +1,9 @@
1
- // GEML History extension — commit / restore / verify.
1
+ // GEML History extension — save / restore / verify.
2
2
  //
3
3
  // Implements the `.gemlhistory` companion spec: a self-contained, reverse-delta
4
4
  // version history beside the live `.geml` file. The history file is itself a
5
5
  // GEML document (meta + keyframe + revision + blob blocks). Reverse patches and
6
- // hashes are tool-generated here; every commit re-applies its reverse patch and
6
+ // hashes are tool-generated here; every save re-applies its reverse patch and
7
7
  // asserts a byte-exact round-trip before writing (the spec's verify gate).
8
8
  //
9
9
  // Revision id = `<YYYYMMDDTHHMMSSZ>-<first 8 hex of the version content hash>`.
@@ -83,7 +83,7 @@ function fenceFor(contentLf) {
83
83
  // Unit-key = `#id` (explicit), or `@<8hex content hash>` (derived), with `~n`
84
84
  // disambiguating equal keys by document-order occurrence (§4). `~n` on an #id
85
85
  // key only arises for OUT-OF-SPEC documents that repeat an id — without it the
86
- // key is ambiguous, reverse-patch ops hit the wrong occurrence, and commit()'s
86
+ // key is ambiguous, reverse-patch ops hit the wrong occurrence, and save()'s
87
87
  // round-trip gate (correctly) aborts. Well-formed documents never emit it.
88
88
  const KEY = String.raw `(#[A-Za-z][A-Za-z0-9_-]*(?:~\d+)?|@[0-9a-f]+(?:~\d+)?)`;
89
89
  function sha8(s) {
@@ -267,7 +267,7 @@ function applyReverse(textLf, ops, blobs) {
267
267
  *
268
268
  * If keys were ever non-unique (no public entry point produces that — the only
269
269
  * caller, diffReverse, feeds keyedUnits output), posInB keeps b's LAST index
270
- * per key and the LIS still yields *a* valid monotonic matching; commit()'s
270
+ * per key and the LIS still yields *a* valid monotonic matching; save()'s
271
271
  * byte-exact round-trip gate rejects any diff that fails to reproduce the
272
272
  * parent regardless. */
273
273
  function lcsMatch(a, b) {
@@ -478,7 +478,7 @@ function renderHistory(h, baseName) {
478
478
  }
479
479
  return parts.join("\n");
480
480
  }
481
- export function commit(o) {
481
+ export function save(o) {
482
482
  const { lf: working, nl } = loadBytes(o.gemlPath);
483
483
  const hash = fullHash(working, nl);
484
484
  const stamp = stampUTC(o.at ?? new Date());
@@ -494,10 +494,10 @@ export function commit(o) {
494
494
  const blobMap = new Map(patch.blobs.map((b) => [b.id, b.payload]));
495
495
  const back = applyReverse(working, patch.ops, blobMap);
496
496
  if (bytesOf(back, nl).compare(bytesOf(prevContent, nl)) !== 0) {
497
- throw new Error("history: reverse patch does NOT round-trip to the previous revision; aborting commit");
497
+ throw new Error("history: reverse patch does NOT round-trip to the previous revision; aborting save");
498
498
  }
499
- // Blob ids are minted per-diff (b1, b2, …). Renumber this commit's blobs to
500
- // start past the highest id already stored, so a later commit never reuses
499
+ // Blob ids are minted per-diff (b1, b2, …). Renumber this save's blobs to
500
+ // start past the highest id already stored, so a later save never reuses
501
501
  // an earlier revision's blob id — an overwrite in the shared store silently
502
502
  // corrupts reconstruction of older revisions, whose `replace … <- blob:bN`
503
503
  // would then resolve to the wrong (newer) content.
@@ -597,7 +597,9 @@ export function restore(o) {
597
597
  if (existsSync(o.gemlPath)) {
598
598
  const { lf, nl } = loadBytes(o.gemlPath);
599
599
  if (fullHash(lf, nl) !== h.revisions.get(h.current).hash && !o.force) {
600
- throw new Error("history: uncommitted changes in doc.geml; rerun with force to discard them, or commit first");
600
+ // "save first" names the live verb: this string used to say `commit`,
601
+ // which the four-verb collapse removed (design §2).
602
+ throw new Error("history: uncommitted changes in doc.geml; rerun with force to discard them, or save first");
601
603
  }
602
604
  }
603
605
  // destructive linear truncation to `target`
@@ -623,7 +625,9 @@ export function restore(o) {
623
625
  return content;
624
626
  }
625
627
  /** Is the working file byte-identical to the sidecar's tip revision? False
626
- * means uncommitted drift (e.g. an earlier commit attempt was refused). */
628
+ * means unsaved drift (e.g. an earlier save was refused by the round-trip
629
+ * gate). Both write paths gate on this so a no-change `save` appends nothing:
630
+ * `geml mcp` before each write (mcp.ts) and `geml history save` (geml.ts). */
627
631
  export function isCurrent(historyPath, gemlPath) {
628
632
  const h = parseHistory(historyPath);
629
633
  const tip = h.revisions.get(h.current);
@@ -647,11 +651,12 @@ export function listRevisions(historyPath) {
647
651
  *
648
652
  * There is exactly one selector grammar — `0` (the tip), `-N` (N revisions
649
653
  * back), or an unambiguous revision id (prefix, suffix, or exact) — and it is
650
- * the grammar `history log` prints in its first column, so its output is
651
- * copy-pasteable into `revert --rev`, `history show`, and `history restore`
652
- * alike. Keeping this in one function is what makes that true: it used to be
653
- * written twice, and the copy in `restore` never grew the `0`/`-N` arm, so the
654
- * selectors `history log` advertised were rejected by `history show`. */
654
+ * the grammar `history get` prints in its first column, so its output is
655
+ * copy-pasteable into `revert --rev`, `history get <rev>`, and
656
+ * `history restore` alike. Keeping this in one function is what makes that
657
+ * true: it used to be written twice, and the copy in `restore` never grew the
658
+ * `0`/`-N` arm, so the selectors the revision list advertised were rejected by
659
+ * the command that printed a revision. */
655
660
  export function resolveRevision(h, selector) {
656
661
  const off = /^(0|-\d+)$/.exec(selector);
657
662
  if (off) {
package/dist/inline.js CHANGED
@@ -315,8 +315,13 @@ function scanAtoms(s, line, sink, depth = 0) {
315
315
  // guesswork. Delimiters pair only *within* one text run: they never reach across
316
316
  // a code span, inline math, a link or image (atoms from phase A), or a block
317
317
  // boundary. Any delimiter left unpaired is literal text.
318
- const ASCII_PUNCT = /[!-\/:-@\[-`{-~]/;
319
- const isPunct = (c) => c !== undefined && ASCII_PUNCT.test(c);
318
+ // Unicode punctuation, not just ASCII (§5.3). With an ASCII-only test, `“` and
319
+ // `,` count as ordinary letters, and a run hugged by CJK punctuation on the
320
+ // outside and ASCII punctuation on the inside stops flanking: `“*(foo)*”` loses
321
+ // its emphasis. CommonMark's rule is Unicode-wide, and the algorithm here is
322
+ // meant to be that rule restricted to `*` and `~~` — not a narrower one.
323
+ const PUNCT = /[\p{P}\p{S}]/u;
324
+ const isPunct = (c) => c !== undefined && PUNCT.test(c);
320
325
  const isWS = (c) => c === undefined || /\s/.test(c);
321
326
  // Left/right-flanking for a delimiter run, given the chars on either side.
322
327
  function flank(before, after) {
package/dist/mcp.d.ts CHANGED
@@ -22,5 +22,5 @@ export declare function allTools(): Tool[];
22
22
  */
23
23
  export declare function loadGraphTools(): Promise<Tool[]>;
24
24
  export declare function handleLine(line: string, write?: (s: string) => void): void;
25
- export declare const MCP_USAGE = "usage: geml mcp --root <dir> [--graph <dir>] [--no-history]\n\n Serve GEML document CRUD over the MCP stdio transport (JSON-RPC 2.0), plus the\n read-only code-graph tools when the root holds a code graph.\n\n --root <dir> REQUIRED. Root directory holding the .geml documents.\n Relative paths resolve against the server process's CWD,\n which the CLIENT chooses \u2014 pass an absolute path.\n Every path a client names is confined to this directory;\n a client cannot widen or override it.\n --graph <dir> Code-graph directory, inside --root. Defaults to\n <root>/.geml-code-graph when that holds an index.geml.\n With no graph, the code-graph tools are not served\n at all (a client sees only the document tools).\n --no-history Do not auto-commit a .gemlhistory revision before each\n write. Default is to commit, so geml_revert always\n has a revision to undo to.\n\n Register with a client:\n claude mcp add geml -- geml mcp --root /abs/path/to/repo";
25
+ export declare const MCP_USAGE = "usage: geml mcp --root <dir> [--graph <dir>] [--no-history]\n\n Serve GEML document CRUD over the MCP stdio transport (JSON-RPC 2.0), plus the\n read-only code-graph tools when the root holds a code graph.\n\n --root <dir> REQUIRED. Root directory holding the .geml documents.\n Relative paths resolve against the server process's CWD,\n which the CLIENT chooses \u2014 pass an absolute path.\n Every path a client names is confined to this directory;\n a client cannot widen or override it.\n --graph <dir> Code-graph directory, inside --root. Defaults to\n <root>/.geml-code-graph when that holds an index.geml.\n With no graph, the code-graph tools are not served\n at all (a client sees only the document tools).\n --no-history Do not save a .gemlhistory revision before each\n write. Default is to save one, so geml_revert always\n has a revision to undo to.\n\n Register with a client:\n claude mcp add geml -- geml mcp --root /abs/path/to/repo";
26
26
  export declare function parseArgs(args: string[]): McpOptions;
package/dist/mcp.js CHANGED
@@ -35,14 +35,14 @@
35
35
  // The mutations run through the CLI rather than re-implementing block editing:
36
36
  // the tool table is *defined* as CLI equivalences, and `-o -` already yields
37
37
  // the mutated document without touching the file — exactly the "produce, then
38
- // validate, then commit" order invariant 1 needs.
38
+ // validate, then save" order invariant 1 needs.
39
39
  import { readFileSync, writeFileSync, existsSync, realpathSync, statSync } from "node:fs";
40
40
  import { resolve, dirname, sep } from "node:path";
41
41
  import { fileURLToPath, pathToFileURL } from "node:url";
42
42
  import { spawnSync } from "node:child_process";
43
43
  import { createInterface } from "node:readline";
44
44
  import { parse, PARSER_VERSION } from "./geml.js";
45
- import { commit, listRevisions, isCurrent } from "./history.js";
45
+ import { save, listRevisions, isCurrent, resolveContent } from "./history.js";
46
46
  // One version for the whole package: `geml --version` and the MCP handshake
47
47
  // must not disagree. This used to be its own literal and had drifted to 0.1.0
48
48
  // against a 1.4.x package — invisible to everyone except the user reading their
@@ -200,12 +200,12 @@ function applyWrite(spec) {
200
200
  if (after === before) {
201
201
  return { ok: true, file: spec.file, diagnostics: diags, hint: "No change: the document already had this content." };
202
202
  }
203
- // 3. Commit the PRE-write state so this edit is revertible, then write.
203
+ // 3. Save the PRE-write state so this edit is revertible, then write.
204
204
  const revision = spec.summary && OPTS.history ? snapshot(real, spec.summary) : undefined;
205
205
  writeFileSync(real, after, "utf8");
206
206
  return { ok: true, file: spec.file, diagnostics: diags, revision };
207
207
  }
208
- // Commit the file's CURRENT bytes as a revision, so the about-to-happen write
208
+ // Save the file's CURRENT bytes as a revision, so the about-to-happen write
209
209
  // has something to revert to. A file already identical to its tip needs no
210
210
  // second revision.
211
211
  function snapshot(realPath, summary) {
@@ -213,7 +213,7 @@ function snapshot(realPath, summary) {
213
213
  try {
214
214
  if (existsSync(historyPath) && isCurrent(historyPath, realPath))
215
215
  return undefined;
216
- return commit({ gemlPath: realPath, historyPath, summary }).id;
216
+ return save({ gemlPath: realPath, historyPath, summary }).id;
217
217
  }
218
218
  catch {
219
219
  // A sidecar that cannot be written must not cost the caller their edit;
@@ -243,12 +243,27 @@ function docResolver(root, fromFile) {
243
243
  };
244
244
  }
245
245
  const hashId = (id) => (id.startsWith("#") ? id : `#${id}`);
246
+ // `geml get`/`geml set` take a full block SELECTOR, not only an id: a content
247
+ // address reaches a block the author never named, which is the whole point of
248
+ // `geml_list` now reporting one for those. So a value that is ALREADY a
249
+ // selector must pass through untouched — hashId would turn `@a3f9c1d2` into
250
+ // `#@a3f9c1d2` and address nothing. A bare word is still an id, so the
251
+ // long-standing "id with or without #" contract is unchanged.
252
+ //
253
+ // The parameter is still NAMED `id`: renaming it to `selector` would break
254
+ // every registered client for a cosmetic gain, and both design docs park that
255
+ // rename as a follow-up. The other verbs keep hashId — their CLI counterparts
256
+ // (add/delete/rename/revert) take ids only, so accepting a selector here would
257
+ // promise something the CLI would then refuse.
258
+ // A selector starts with `#` (id or heading line), `@` (content address), or a
259
+ // `=` fence run (type filter). Anything else is a bare id.
260
+ const selectorArg = (s) => (/^([#@]|={3,})/.test(s.trim()) ? s.trim() : `#${s}`);
246
261
  const FILE_ARG = { type: "string", description: "Document path relative to the server's --root directory, e.g. notes/spec.geml" };
247
262
  export const TOOLS = [
248
263
  // ----- read -----
249
264
  {
250
265
  name: "geml_list",
251
- description: "List every addressable block in a GEML document: its `#id`, kind, and heading text. Call this FIRST — the ids it returns are what every other tool in this server addresses. Cheaper and more reliable than reading the file to find out what is in it.",
266
+ description: "List every addressable block in a GEML document: its address, kind, and heading text. Call this FIRST — the `id` values it returns are what every other tool in this server addresses. Cheaper and more reliable than reading the file to find out what is in it. Rows marked `anon` have no `#id` (their `address` is a type or content address the CLI understands); this server's other tools take an `id`, so give such a block an id before addressing it here.",
252
267
  inputSchema: { type: "object", properties: { file: FILE_ARG }, required: ["file"] },
253
268
  run: (args) => {
254
269
  const real = resolveInRoot(args.file);
@@ -260,20 +275,24 @@ export const TOOLS = [
260
275
  },
261
276
  {
262
277
  name: "geml_get",
263
- description: "Read ONE block from a GEML document by its `#id`. Use this instead of reading the whole file: it returns only that block, typically a few percent of the document. Get available ids from `geml_list` first. Reading the whole file to change one block wastes context and risks modifying unrelated content.",
278
+ description: "Read ONE block from a GEML document. Use this instead of reading the whole file: it returns only that block, typically a few percent of the document. Call `geml_list` first and pass back the `address` it gives that also reaches blocks with no `#id`, which an id alone cannot.",
264
279
  inputSchema: {
265
280
  type: "object",
266
281
  properties: {
267
282
  file: FILE_ARG,
268
- id: { type: "string", description: "Block id, with or without the leading `#`" },
283
+ id: {
284
+ type: "string",
285
+ description: "What to read: a block id (with or without `#`), a `## Heading` line (its whole section), `=== type` for every block of a type, or a `@<hex>` content address for a block with no id — the forms `geml_list` prints",
286
+ },
269
287
  },
270
288
  required: ["file", "id"],
271
289
  },
272
290
  run: (args) => {
273
291
  const real = resolveInRoot(args.file);
274
- const run = runCli(["get", real, hashId(args.id)]);
292
+ const sel = selectorArg(args.id);
293
+ const run = runCli(["get", real, sel]);
275
294
  if (!run.ok)
276
- throw new Error(run.stderr || `no block with id ${hashId(args.id)}`);
295
+ throw new Error(run.stderr || `nothing matches ${sel}`);
277
296
  return run.stdout;
278
297
  },
279
298
  },
@@ -304,14 +323,41 @@ export const TOOLS = [
304
323
  },
305
324
  {
306
325
  name: "geml_history",
307
- description: "List the recorded revisions of a document, newest first. Each entry's `offset` is the selector `geml_revert` takes as `rev` (-1 is the revision before the current one). Use this to find WHICH revision to revert a block to; an empty list means the document has no sidecar yet and nothing can be reverted.",
308
- inputSchema: { type: "object", properties: { file: FILE_ARG }, required: ["file"] },
326
+ // The name mirrors the CLI COMMAND PATH (`geml history`), not a verb: this
327
+ // group's only read verb is `get`, and it is the only one that belongs on a
328
+ // server an agent drives (`save` would insert hand-made revisions between
329
+ // the automatic pre-write ones, and `restore` rewrites a whole file where
330
+ // the agent already has block-level geml_revert). So there will be no second
331
+ // history tool to disambiguate from, and `_get` would be a suffix that
332
+ // distinguishes nothing — design §5.
333
+ description: "Read a document's recorded history. WITHOUT `rev`: list every revision, newest first — each entry's `offset` is the selector `geml_revert` takes as `rev` (-1 is the revision before the current one), and an empty list means the document has no sidecar yet and nothing can be reverted. WITH `rev`: the full text of that one revision, for reading what the document looked like then without restoring it.",
334
+ inputSchema: {
335
+ type: "object",
336
+ properties: {
337
+ file: FILE_ARG,
338
+ rev: { type: "string", description: "Revision selector — `0` for the current tip, `-N` for N revisions back, or a revision id from the list. Omit it to get the list instead of one revision's text." },
339
+ },
340
+ required: ["file"],
341
+ },
309
342
  run: (args) => {
310
343
  const real = resolveInRoot(args.file);
311
344
  const historyPath = real.replace(/\.geml$/, "") + ".gemlhistory";
312
- if (!existsSync(historyPath))
345
+ const rev = args.rev === undefined ? undefined : String(args.rev);
346
+ if (!existsSync(historyPath)) {
347
+ // Naming a revision of a document that has no history at all is an
348
+ // error, not an empty result: the caller asked for specific content.
349
+ // The LIST tier stays a plain empty answer — "nothing yet" is a real,
350
+ // useful state there.
351
+ if (rev !== undefined)
352
+ throw new Error(`no .gemlhistory sidecar for ${args.file} yet, so revision ${rev} does not exist — the first write through this server creates one`);
313
353
  return { file: args.file, revisions: [], note: "no .gemlhistory sidecar yet — the first write through this server creates one" };
314
- return { file: args.file, revisions: listRevisions(historyPath) };
354
+ }
355
+ if (rev === undefined)
356
+ return { file: args.file, revisions: listRevisions(historyPath) };
357
+ // resolveContent() is the CLI's own path for `geml history get <file>
358
+ // <rev>`, so one selector grammar answers on both surfaces.
359
+ const { id, text } = resolveContent(historyPath, rev);
360
+ return { file: args.file, id, text };
315
361
  },
316
362
  },
317
363
  {
@@ -362,12 +408,15 @@ export const TOOLS = [
362
408
  // ----- write -----
363
409
  {
364
410
  name: "geml_set",
365
- description: "Replace ONE block, addressed by `#id`, leaving every other byte of the document untouched. Prefer this over rewriting a file. The replacement is VALIDATED BEFORE it is written: if it would break the document, nothing is written and you get the diagnostics back — re-read them and fix the body rather than retrying the same content. `part` selects whole block (default), just the head/fence line, or just the body.",
411
+ description: "Replace ONE block, leaving every other byte of the document untouched. Prefer this over rewriting a file. The replacement is VALIDATED BEFORE it is written: if it would break the document, nothing is written and you get the diagnostics back — re-read them and fix the body rather than retrying the same content. `part` selects whole block (default), just the head/fence line, or just the body. An address matching SEVERAL blocks is refused — this writes one block, so narrow it first.",
366
412
  inputSchema: {
367
413
  type: "object",
368
414
  properties: {
369
415
  file: FILE_ARG,
370
- id: { type: "string", description: "Block id to replace, with or without `#`" },
416
+ id: {
417
+ type: "string",
418
+ description: "Which block to replace: an id (with or without `#`), or a `@<hex>` content address from `geml_list` for a block with no id. Must match exactly one block",
419
+ },
371
420
  body: { type: "string", description: "The replacement text" },
372
421
  part: { type: "string", enum: ["whole", "head", "body"], description: "What to replace (default: whole)" },
373
422
  },
@@ -381,9 +430,9 @@ export const TOOLS = [
381
430
  const flag = part === "head" ? ["--head"] : part === "body" ? ["--body"] : [];
382
431
  return applyWrite({
383
432
  file: args.file,
384
- cliArgs: ["set", real, hashId(args.id), ...flag, "--in", "-", "-o", "-"],
433
+ cliArgs: ["set", real, selectorArg(args.id), ...flag, "--in", "-", "-o", "-"],
385
434
  input: args.body,
386
- summary: `mcp: before write to ${hashId(args.id)}`,
435
+ summary: `mcp: before write to ${selectorArg(args.id)}`,
387
436
  });
388
437
  },
389
438
  },
@@ -633,8 +682,8 @@ export const MCP_USAGE = `usage: geml mcp --root <dir> [--graph <dir>] [--no-his
633
682
  <root>/.geml-code-graph when that holds an index.geml.
634
683
  With no graph, the code-graph tools are not served
635
684
  at all (a client sees only the document tools).
636
- --no-history Do not auto-commit a .gemlhistory revision before each
637
- write. Default is to commit, so geml_revert always
685
+ --no-history Do not save a .gemlhistory revision before each
686
+ write. Default is to save one, so geml_revert always
638
687
  has a revision to undo to.
639
688
 
640
689
  Register with a client:
package/dist/render.js CHANGED
@@ -611,35 +611,14 @@ export class RenderCtx {
611
611
  const maxRows = this.isCodemapDoc && id === "modules" ? Infinity : (this.opts.tableRows ?? 500);
612
612
  const allRows = t.rows;
613
613
  const rows = allRows.length > maxRows ? allRows.slice(0, maxRows) : allRows;
614
- // Coverage grid for declared spans, so cells a span covers are not emitted.
615
- const covered = rows.map((r) => r.map(() => false));
616
- rows.forEach((row, r) => row.forEach((cell, c) => {
617
- if (!cell.span)
618
- return;
619
- // Bound the sweep to the rendered grid regardless of the declared span, so
620
- // an oversized span can never drive an O(hugerows×hugecols) loop (DoS).
621
- const spanRows = Math.min(cell.span.rows, rows.length - r);
622
- const spanCols = Math.min(cell.span.cols, row.length - c);
623
- for (let dr = 0; dr < spanRows; dr++)
624
- for (let dc = 0; dc < spanCols; dc++) {
625
- if (dr === 0 && dc === 0)
626
- continue;
627
- const rr = r + dr, cc = c + dc;
628
- if (covered[rr]?.[cc] !== undefined)
629
- covered[rr][cc] = true;
630
- }
631
- }));
632
614
  const thead = t.header
633
615
  ? `<thead><tr>${t.columns.map((col, c) => `<th${alignStyle(t.align[c])}>${esc(col)}</th>`).join("")}</tr></thead>`
634
616
  : "";
635
617
  const bodyRows = rows.map((row, r) => {
636
618
  const cells = row.map((cell, c) => {
637
- if (covered[r]?.[c])
638
- return "";
639
- const span = cell.span ? `${cell.span.rows > 1 ? ` rowspan="${cell.span.rows}"` : ""}${cell.span.cols > 1 ? ` colspan="${cell.span.cols}"` : ""}` : "";
640
619
  const cls = cell.computed ? ' class="computed"' : "";
641
620
  const sortVal = typeof cell.value === "number" ? ` data-sort="${cell.value}"` : "";
642
- return `<td${alignStyle(cell.align ?? t.align[c])}${span}${cls}${sortVal}>${this.inlines(cell.inlines)}</td>`;
621
+ return `<td${alignStyle(cell.align ?? t.align[c])}${cls}${sortVal}>${this.inlines(cell.inlines)}</td>`;
643
622
  }).join("");
644
623
  return `<tr>${cells}</tr>`;
645
624
  }).join("\n");
@@ -0,0 +1,55 @@
1
+ export interface Span {
2
+ start: number;
3
+ end: number;
4
+ }
5
+ export interface Unit {
6
+ span: Span;
7
+ kind: "block" | "heading" | "footnote";
8
+ type?: string;
9
+ id?: string;
10
+ level?: number;
11
+ text?: string;
12
+ }
13
+ export type Selector = {
14
+ form: "list";
15
+ } | {
16
+ form: "id";
17
+ raw: string;
18
+ } | {
19
+ form: "type";
20
+ type: string;
21
+ } | {
22
+ form: "content";
23
+ type?: string;
24
+ hex: string;
25
+ nth: number;
26
+ } | {
27
+ form: "attr";
28
+ type: string;
29
+ key: string;
30
+ };
31
+ export declare function sha8(text: string): string;
32
+ export declare function parseSelector(raw: string | undefined, attrsIdOf: (braces: string) => string | undefined): Selector;
33
+ export interface Addressed {
34
+ unit: Unit;
35
+ hex: string;
36
+ nth: number;
37
+ }
38
+ export declare function addressUnits(units: Unit[], textOf: (u: Unit) => string): Addressed[];
39
+ export declare function shortestAddress(a: Addressed, all: Addressed[]): string;
40
+ export type ContentHit = {
41
+ ok: true;
42
+ unit: Unit;
43
+ } | {
44
+ ok: false;
45
+ why: "no-match";
46
+ } | {
47
+ ok: false;
48
+ why: "wrong-type";
49
+ found: string;
50
+ };
51
+ export declare function matchContent(sel: Extract<Selector, {
52
+ form: "content";
53
+ }>, all: Addressed[]): ContentHit;
54
+ export declare function discoveryHint(where: string): string;
55
+ export declare function matchType(type: string, all: Addressed[]): Unit[];
@@ -0,0 +1,112 @@
1
+ // Block selectors — the one syntax `get`, `set` and `history get` all address
2
+ // blocks with (design: docs/design/specs/2026-08-04-geml-get-set-selector-design-change.md).
3
+ //
4
+ // §2's rule: a selector is a FILTER over blocks, `{…}` holds keys, and the same
5
+ // abbreviation rule applies twice — `#id` is `{#id}` short, `@<hex>` is
6
+ // `{@<hex>}` short. Both are keys; they differ only in selectivity.
7
+ //
8
+ // This module is PURE: it parses selector text and matches it against a unit
9
+ // index the caller supplies. It deliberately imports nothing from geml.ts —
10
+ // that module runs the CLI dispatch on import, so depending on it here would
11
+ // turn `import { parseSelector }` into "run the CLI". The scan that produces
12
+ // `Unit[]` therefore stays in geml.ts (one walk, several sinks) and the
13
+ // selector logic stays here, where it can be unit-tested on plain data.
14
+ import { createHash } from "node:crypto";
15
+ // The content address's hash. Same spelling as the `.gemlhistory` unit key
16
+ // (history.ts:112) because both answer the same question — how to address a
17
+ // unit that carries no id. The VALUES are deliberately not promised to match:
18
+ // history hashes a tile (trailing blank lines included), this hashes a block's
19
+ // span (§3.3). Do not port an address from one layer to the other.
20
+ export function sha8(text) {
21
+ return createHash("sha256").update(Buffer.from(text, "utf8")).digest("hex").slice(0, 8);
22
+ }
23
+ const FENCE_SEL = /^={3,}[ \t]*([A-Za-z][A-Za-z0-9_-]*)[ \t]*(@[0-9a-fA-F]{1,}(?:~\d+)?)?[ \t]*(\{.*\})?[ \t]*$/;
24
+ const BARE_AT = /^@([0-9a-fA-F]+)(?:~(\d+))?$/;
25
+ // Parse selector TEXT. Never touches a document: every form is decided by
26
+ // lexis alone, which is also what keeps the two selector namespaces on
27
+ // `history get <file> <rev> <selector>` from overlapping (history design §10.2).
28
+ // `attrsIdOf` lets the caller reuse its own `{…}` parser (parseAttrs) rather
29
+ // than this module growing a second one.
30
+ export function parseSelector(raw, attrsIdOf) {
31
+ if (raw === undefined || raw.trim() === "")
32
+ return { form: "list" };
33
+ const s = raw.trim();
34
+ const bare = BARE_AT.exec(s);
35
+ if (bare)
36
+ return { form: "content", hex: bare[1].toLowerCase(), nth: bare[2] ? Number(bare[2]) : 0 };
37
+ const fence = FENCE_SEL.exec(s);
38
+ if (fence) {
39
+ const type = fence[1];
40
+ const at = fence[2];
41
+ const braces = fence[3];
42
+ if (braces !== undefined) {
43
+ // `=== type {#id}` is the id key written out in full — redundant but
44
+ // legal (§2). Any OTHER key is the declared-not-implemented form.
45
+ const id = attrsIdOf(braces);
46
+ if (id !== undefined)
47
+ return { form: "id", raw: `#${id}` };
48
+ return { form: "attr", type, key: firstKey(braces) };
49
+ }
50
+ if (at !== undefined) {
51
+ const m = BARE_AT.exec(at);
52
+ return { form: "content", type, hex: m[1].toLowerCase(), nth: m[2] ? Number(m[2]) : 0 };
53
+ }
54
+ return { form: "type", type };
55
+ }
56
+ // Anything else is an id or a pasted heading line; the caller resolves it.
57
+ return { form: "id", raw: s };
58
+ }
59
+ // The first key inside `{…}`, for the §7 error message. Best-effort: it only
60
+ // has to name what the caller typed, and a class (`.warn`) is reported as
61
+ // written so the message does not claim a key that is not there.
62
+ function firstKey(braces) {
63
+ const inner = braces.replace(/^\{/, "").replace(/\}$/, "").trim();
64
+ const m = /^([.#]?[A-Za-z_][A-Za-z0-9_-]*)/.exec(inner);
65
+ return m ? m[1] : inner.split(/[\s=]/)[0] ?? "";
66
+ }
67
+ export function addressUnits(units, textOf) {
68
+ const seen = new Map();
69
+ return units.map((unit) => {
70
+ // LF-normalized so a CRLF checkout and an LF one address the same block.
71
+ const hex = sha8(textOf(unit).replace(/\r\n?/g, "\n"));
72
+ const nth = seen.get(hex) ?? 0;
73
+ seen.set(hex, nth + 1);
74
+ return { unit, hex, nth };
75
+ });
76
+ }
77
+ // §6.1 — the SHORTEST address that identifies this unit uniquely, which is what
78
+ // the listing prints. `#id` when it has one; else the bare type when the
79
+ // document holds exactly one block of it; else the content address. The three
80
+ // cases are one rule ("shortest unique"), not three rules.
81
+ export function shortestAddress(a, all) {
82
+ const u = a.unit;
83
+ if (u.id !== undefined)
84
+ return `#${u.id}`;
85
+ if (u.type === undefined)
86
+ return `@${a.hex}${a.nth ? `~${a.nth}` : ""}`;
87
+ const sameType = all.filter((x) => x.unit.type === u.type).length;
88
+ if (sameType === 1)
89
+ return `=== ${u.type}`;
90
+ return `=== ${u.type}@${a.hex}${a.nth ? `~${a.nth}` : ""}`;
91
+ }
92
+ export function matchContent(sel, all) {
93
+ const hit = all.find((a) => a.hex === sel.hex && a.nth === sel.nth);
94
+ if (!hit)
95
+ return { ok: false, why: "no-match" };
96
+ if (sel.type !== undefined && hit.unit.type !== sel.type) {
97
+ return { ok: false, why: "wrong-type", found: hit.unit.type ?? hit.unit.kind };
98
+ }
99
+ return { ok: true, unit: hit.unit };
100
+ }
101
+ // Where to send a caller whose selector found nothing: the listing IS the
102
+ // discovery command, and every address it prints pastes straight back (§6.2).
103
+ // One place, so `get`, `set` and `history get` all point at the same next step.
104
+ export function discoveryHint(where) {
105
+ return ` — run \`geml get ${where}\` to list every addressable block`;
106
+ }
107
+ // Match a type filter: every block of that type in document order. Blocks that
108
+ // carry an id are INCLUDED — the selector says nothing about ids, so filtering
109
+ // by whether one is present would be a rule nobody wrote down (§2).
110
+ export function matchType(type, all) {
111
+ return all.filter((a) => a.unit.type === type).map((a) => a.unit);
112
+ }
package/dist/table.d.ts CHANGED
@@ -8,10 +8,6 @@ export interface TableCell {
8
8
  align?: Align;
9
9
  value?: number;
10
10
  computed?: boolean;
11
- span?: {
12
- rows: number;
13
- cols: number;
14
- };
15
11
  }
16
12
  export interface TableModel {
17
13
  caption?: string;