miller/pkg
John Kerl 6a357d5df9
Add JSON index and mlr which capability router (#2098) (#2107)
* Add JSON index and mlr which capability router (#2098)

PR 2 of the AI-friendly roadmap (plans/plan-2098-llm.md):

- `mlr help --as-json --index` emits a lightweight [{kind,name,summary}]
  index across all 651 catalog items (verbs, functions, flags, keywords),
  sorted by kind then name. Agents use this as a cheap first call to pick
  a verb before fetching its full entry.

- `mlr which "<query>"` is a new terminal that tokenizes a natural-language
  query, scores every catalog item (name match +20/token, body match +5/token),
  and returns ranked JSON [{kind,name,score,summary}]. Exit code 0 means a
  confident match (at least one token hit the item name); exit code 2 means
  low confidence. Agents branch on the exit code rather than parsing prose.

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>

* Move mlr which into pkg/terminals/help, deduplicate firstLine

pkg/terminals/which/ was a misplaced package: it imported the same four
catalog registries as pkg/terminals/help/ and duplicated the firstLine
helper. Moving the logic into help/entry_which.go fixes both issues:

- WhichMain and all which helpers now live alongside the other --as-json
  catalog machinery in pkg/terminals/help/
- indexFirstLine (entry_json.go) and firstLine (which/entry.go) collapse
  into a single firstLine shared by both files
- pkg/terminals/terminals.go calls help.WhichMain directly; the
  pkg/terminals/which/ package is deleted

No behavior change.

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>

* Fix five Go-purity issues found in code review

1. Exit-code false positive: whichScore now returns (int, bool) where the
   bool records whether any token hit the item name. WhichMain uses
   results[0].nameHit for exit-code 0, so 4 body-only token hits (4×5=20)
   no longer incorrectly signal a confident match.

2. Flag Summary inconsistency: whichSearch was setting Summary: fl.Help
   directly for flags while using firstLine(...) for functions and keywords.
   Changed to firstLine(fl.Help) so all four kinds behave consistently.

3+5. kindOrder/whichKindRank duplication: the verb<function<flag<keyword
   ordering was encoded twice — as a local map[string]int in buildIndex and
   as a switch in whichKindRank. Replaced both with a single package-level
   kindRank() function. The map lookup also silently returned 0 (= verb rank)
   for unknown kinds; the switch correctly returns 4 (sorts last).

4. extractIndexFlag/extractAsJSONFlag duplication: both had identical loop
   bodies differing only in the sentinel string. Introduced a generic
   extractFlag(args, flag) helper; both are now one-liners.

Also promoted whichStopwords to a package-level var so whichTokenize does
not allocate a new map on every call.

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
2026-06-28 17:40:50 -04:00
..
auxents Next batch of lint fixes (#2108) 2026-06-28 17:36:36 -04:00
bifs Next batch of lint fixes (#2108) 2026-06-28 17:36:36 -04:00
cli Next batch of lint fixes (#2108) 2026-06-28 17:36:36 -04:00
climain Next batch of lint fixes (#2108) 2026-06-28 17:36:36 -04:00
colorizer Multiple style updates (#1974) 2026-02-16 15:49:21 -05:00
dkvpx Next batch of lint fixes (#2108) 2026-06-28 17:36:36 -04:00
dsl Next batch of lint fixes (#2108) 2026-06-28 17:36:36 -04:00
entrypoint Improve error propagation (#1975) 2026-02-16 16:48:41 -05:00
go-csv Some fixes for staticcheck (#2006) 2026-03-03 09:27:03 -05:00
input Next batch of lint fixes (#2108) 2026-06-28 17:36:36 -04:00
lib Add new tail -n +N, head -n -N options (#2071) 2026-06-21 10:34:16 -04:00
mlrval Next batch of lint fixes (#2108) 2026-06-28 17:36:36 -04:00
output Batch-allocate per-record objects; reuse CSV writer field buffer (#2083) 2026-06-19 17:03:04 -04:00
parsing Replace GOCC parser-generator with PGPG (#2015) 2026-03-15 22:28:57 -04:00
pbnjay-strptime Performance and style fixes (#1981) 2026-02-18 09:19:31 -05:00
platform Performance and style fixes (#1981) 2026-02-18 09:19:31 -05:00
runtime Pool DSL stack-frame sets across UDF/subroutine calls (~31% perf on function-heavy mlr put) (#2088) 2026-06-19 17:04:19 -04:00
scan Performance and style fixes (#1981) 2026-02-18 09:19:31 -05:00
stream Multiple style updates (#1974) 2026-02-16 15:49:21 -05:00
terminals Add JSON index and mlr which capability router (#2098) (#2107) 2026-06-28 17:40:50 -04:00
transformers Next batch of lint fixes (#2108) 2026-06-28 17:36:36 -04:00
types Bind scalar locals/params by reference, not by copy (~4-9% on DSL) (#2090) 2026-06-19 17:05:51 -04:00
version Post-6.19.0 release: back to 6.19.0-dev 2026-06-19 19:34:20 -04:00
README.md Export library code in pkg/ (#1391) 2023-09-10 17:15:13 -04:00

Please see ../../README-dev.md for an overview; please see each subdirectory for details about it.