SLOPSHOPPER

khub

khub in the session: the khub and setup skills, a band above the prompt with what the session changed, and compact rows for khub calls

bandrowsguardstatusprocess
★ 4v0.1.4Apache-2.0updated 2026-10-21endgame-build/khub/plugin
A shopper browsing a rack in a slop shop
README

khub

CI License: Apache 2.0 Go 1.26+

Schema-bound, agent-facing context management.

khub gives an AI agent typed, validated, queryable context (structured memory it can navigate and write back to) instead of unstructured documents stuffed into a context window.

khub: an agent captures into a schema-bound graph that lives on disk as heterogeneous git storage

One generic engine: entities live in git as Markdown with YAML frontmatter (the default), as .json/.yaml documents, or as rows of a single-file collection, per-type schema config. The khub schema is the contract: types, attributes, and legal relations, authored in YAML and resolved in memory. A Go core provides schema-validated CRUD and graph queries; a generic khub CLI and an agent skill are thin, schema-driven surfaces over it. It ships as one static binary — no runtime to install. The graph is a projection rebuilt from workspace files on demand; no database is ever the source of truth.

Built on top of the Open Knowledge Format (OKF). khub's Markdown entities are OKF concepts; on top, khub adds a typed schema, a graph, and the serialization formats and collections OKF lacks. Any workspace projects to a conformant OKF bundle.

The schema is the operational setup. It configures what a given hub is for. Canonical ontology presets, one per domain (building and maintaining a system, consulting, research, operations), are the reusable IP. Each engagement gets its own repo seeded from a preset, where agents and humans co-author entities and extend the schema as the work demands.

What it does today

  • Author — add / get ID... / edit / link / unlink / remove: schema-validated writes, referential integrity hard-fails, capture never blocked, minimal-diff round-trips. One get returns one record; multiple IDs return an all-or-nothing array in argument order. A templated type's add seeds the body from its template (--no-template opts out).
  • Find — query (frontmatter + edge filters), search (BM25 full-text over titles, bodies, and fields: FTS5, built in-memory per call, never stale), neighbors / impact / history (graph walks).
  • Gate — validate (per-entity well-formedness, including body structure against the type's template: required section headings as an ordered subsequence; per-section rules report as gaps, which never gate) and check (graph-wide completeness, dangling edges, strays, cycles, missing required singletons, body shape; orphans informational unless --strict, thin bodies informational as thin); stale reads git at entity altitude, row-accurate even inside collections.
  • Project — reindex (OKF index.md), viz (Cytoscape HTML), backfill (git-derived dates and scaffolding).
  • Store — per-type layout (file / folder / collection / singleton) × format (md / json / yaml; collections take json / jsonl / yaml). A singleton is one fixed file whose slug is the type name (khub get prd). Non-md entities carry prose in a reserved body field. Every existing-workspace mutation holds one workspace-wide lock from scan through publication; atomic writes preserve existing permissions and refuse workspace storage symlinks.
  • Scaffold — presets are directories (<name>/{ontology,policy,storage}.yaml + templates/*.yaml); khub init copies them into .khub/ and creates every missing md singleton from its template (creations only — an existing file is never touched); khub upgrade --dry-run preflights the complete candidate and tails without workspace artifacts. A real upgrade publishes schema, templates and scaffolds transactionally, preserves edited files as <name>.bak, writes the version last, then refreshes wiring and index.md as non-fatal tails.
  • Wire — wire: link the schema into a project's agent files. Bare wire updates whichever of CLAUDE.md / AGENTS.md exist; --target claude|agents|both creates one. CLAUDE.md gets @.khub/ontology.yaml (+ policy/storage) imports, AGENTS.md a schema pointer, both with the command surface — so an agent reasons in the ontology with or without the CLI.

Full command surface and JSON contracts: docs/cli.md. Feature history: CHANGELOG.md. All documentation: docs/.

Quickstart

khub is a single static binary for macOS, Linux and Windows (amd64, arm64; Windows is experimental). The primary install is the npm package @endgame-build/khub — one package carrying a prebuilt binary per platform behind a launcher that picks the matching one — pinned per repo, so everyone (and every agent) touching that repo runs the same khub:

npm install -D @endgame-build/khub                  # exact per-repo pin (required: later commands run it)
npx @endgame-build/khub init firm-ops ./my-hub      # scaffold .khub/ (+ templates, singletons), wire agent files
cd my-hub
npx skills add endgame-build/khub                   # the agent skills (Claude Code: the plugin, below)

With no version after the name, npx @endgame-build/khub runs the version package.json pins.

Upgrading a repo is a one-line package.json bump in a PR: any on-disk byte changes a release makes land in that reviewed commit, not in everyone's unrelated diffs. More in docs/getting-started.md.

Author entities. Referential integrity hard-fails on write (a relation to a missing target is rejected), but a missing field never blocks capture:

khub add client  --name "Acme Corp" --industry manufacturing
khub add person  --name "Dana Lee"  --role partner
khub add project --title "Acme Diagnostic" --client acme-corp --owner dana-lee

Then walk the graph and gate it:

khub query --type project        # → project/acme-diagnostic, with orphan/stale flags
khub neighbors acme-diagnostic   # → its client and owner edges
khub check                       # graph-wide: relations resolve, nothing dangling → passed

Every read command takes --format json for an agent and prints a Rich table for a human.

Headless by design. Every input is a flag; a missing one is a usage error, never a prompt. khub carried an interactive wizard through 0.8.0 behind a gate that switched it off for agents, pipes, and CI — dead weight on exactly the invocation khub is built for. Removed in 0.9.0, along with the --agent flag that disabled it.

Why not a folder of Markdown, a database, or a RAG store?

Instead of…What you give up
A folder of Markdown / ObsidianNo schema, no typed relations, no integrity gate: nothing rejects a broken or dangling reference, and an agent can't walk the graph.
A databaseTruth stops being git — no diff, no PR review, no plain-text portability — and the schema lives in migrations instead of one readable file.
A vector / RAG storeRetrieval is fuzzy and lossy: no exact relations to traverse, no completeness gate, and writes don't round-trip.

khub keeps git as the source of truth, then adds a typed schema and a derived graph on top: exact reads, validated writes, and an integrity gate an agent can rely on.

Presets

A preset is a canonical ontology for one domain — a directory holding its three layer files (ontology.yaml for entity types, attributes and legal relations; policy.yaml for workspace gates; storage.yaml for layouts and paths) and optional body templates/. khub init copies them into an engagement's .khub/, which agents and humans then extend as the work demands. The base block every entity carries (type, created/updated, tags, the OKF fields, the any → any edges) is embedded in the binary and supplied at resolve time — never copied into a workspace.

Two presets ship today:

  • firm-ops — the operating graph of a consulting and delivery firm (client, project, person, opportunity, meeting, and more); see docs/firm-ops-preset.md.
  • build-hub — the knowledge hub of a build project: eleven types (prd, arc42, capability, actor, use-case, requirement, adr, system, component, api, repo) in one flat knowledge/; see docs/build-hub-preset.md. build-lite is an alias — khub init build-lite resolves to it and records build-hub. New to khub? Start with docs/getting-started.md.

Agent skills

khub ships two skills: khub (the read and write verbs) and setup (install the CLI, set up a project). They live in this repo under plugin/skills/, apart from the binary.

Claude Code — the plugin. The repo is a plugin marketplace. The khub plugin carries both skills and a small session mod. The mod draws a one-line band above the prompt with the entity count, what the session added, removed and edited, the draft count and the check verdict. In a verbose session it also draws each khub call Claude makes as a compact row. See docs/plugin.md.

/plugin marketplace add endgame-build/khub
/plugin install khub@khub

To update the plugin later, run these from a shell, then start a new session or run /reload-plugins:

claude plugin marketplace update khub
claude plugin update khub@khub

Every other agent — npx skills. opencode, Cursor, Codex, Gemini CLI and any other agent that reads a SKILL.md get the two skills, with no mod. Needs Node and access to the repo:

npx skills add endgame-build/khub             # both skills, into the agents it finds
npx skills add endgame-build/khub -s setup    # only setup, on a machine with no khub yet

On a machine with no khub yet, install setup and ask the agent to set khub up.

Status: v1 engine shipped and in daily use on a live firm-operations corpus that cut over from hand-rolled scripts. Design rationale in docs/design-memo.md; the collections row model in docs/collections-design.md.

License

Apache License 2.0 — see LICENSE.

Source 10 files
hooks/register.tsx 358 lines
1// This file is the shell. Every call on `$` lives here, because the engine follows `$`
2// into the hooks module only. The files beside it are pure and hold the logic.
3
4import { atom, memberOf, read, update } from 'claude-code'
5import type { EngineInterface, Register } from 'claude-code'
6
7import type { KhubCall, KhubType } from '../types'
8import { summaryLine } from './band'
9import { clean, documentsOf, firstLine, parseJson, refusal } from './cli'
10import { callsOf, classify, mutates } from './parse'
11import { callRow, emptyBlock } from './rows'
12import { bandParts, diffOf, editedBy, EMPTY_SESSION, summaryOf, withEdited, withIds } from './session'
13import { finished, waiting } from './summarize'
14import { binCandidates, entityAt, flavourOf, isElsewhere, isOlder, MIN_KHUB, relative } from './workspace'
15import type { Ws } from './workspace'
16
17// The engine reads which state a module touches from atoms that are consts of this file.
18const workspaceAtom = atom({ plugin: 'khub', key: 'workspace' } as const, null)
19const summaryAtom = atom({ plugin: 'khub', key: 'summary' } as const, null)
20const sessionAtom = atom({ plugin: 'khub', key: 'session' } as const, EMPTY_SESSION)
21
22// These two are families with one member per tool_use_id.
23const callRows = atom({ plugin: 'khub', key: 'calls' } as const, null)
24const rawRows = atom({ plugin: 'khub', key: 'rawRows' } as const, false)
25
26type Ran = { exit: number; json: unknown; out: string; note: string; isCut: boolean }
27type SchemaView = { provenance?: { preset?: string; version?: string }; types?: KhubType[] }
28
29// The session's workspace is kept beside its state copy, so hooks read it without a hop.
30let ws: Ws | null = null
31
32// Refreshes run one after another, so an older read never lands on a newer one.
33let refreshing: Promise<void> = Promise.resolve()
34
35// A refresh asked for while one waits to start joins the waiting one.
36let isQueued = false
37
38// Runs khub in the workspace. Exit 0 is success, 1 a failed gate, 2 a refusal, and -1
39// means khub did not run, with the reason in `note`.
40async function khub($: EngineInterface, here: Ws, args: readonly string[]): Promise<Ran> {
41  try {
42    const ran = await $.process.run([...here.bin, '-C', here.root, ...args], { timeoutMs: 20_000 })
43
44    return {
45      exit: ran.exitCode,
46      json: parseJson(ran.stdout),
47      out: ran.stdout,
48      note: ran.stderr.trim(),
49      isCut: ran.isStdoutTruncated,
50    }
51  } catch (error) {
52    return { exit: -1, json: undefined, out: '', note: String(error), isCut: false }
53  }
54}
55
56// Lets work run on after its hook returned. A failure goes to the debug log.
57function background($: EngineInterface, work: Promise<unknown>) {
58  void work.catch(error => $.ui.log(String(error), { to: 'debug' }))
59}
60
61// Reads the resolved schema. A schema that does not resolve is the status line's trouble
62// until it does.
63async function loadSchema($: EngineInterface) {
64  const here = ws
65
66  if (!here) return
67
68  const ran = await khub($, here, ['schema', '--format', 'json'])
69  const view = ran.json as SchemaView | undefined
70
71  if (ran.exit !== 0 || !view?.types) {
72    $.ui.status(clean(`schema: ${firstLine(refusal(ran.json)?.message ?? (ran.out || ran.note))}`))
73
74    return
75  }
76
77  const types = view.types.map(({ name, layout, format, path }) => ({ name, layout, format, path }))
78
79  // The preset's name and version come from workspace files, so they are cleaned like entity text.
80  const workspace = {
81    root: here.root,
82    bin: here.bin,
83    version: here.version,
84    preset: clean(view.provenance?.preset ?? ''),
85    presetVersion: clean(view.provenance?.version ?? ''),
86  }
87
88  ws = { ...workspace, types, flavour: here.flavour }
89  $.ui.status(undefined)
90  await update($, workspaceAtom, () => workspace)
91}
92
93// Finds the workspace above `cwd`, resolves khub and loads the schema. The session's
94// platform is told by the spelling of its directory.
95async function detect($: EngineInterface, cwd: string, binary: string): Promise<'ok' | 'none' | 'no-binary' | 'old'> {
96  ws = null
97
98  const flavour = flavourOf(cwd)
99  let root: string | undefined
100
101  for (const dir of flavour.ancestors(cwd)) {
102    if (await $.fs.exists(flavour.childOf(dir, '.khub'))) {
103      root = dir
104      break
105    }
106  }
107
108  if (root === undefined) return 'none'
109
110  let found: Ws | null = null
111
112  for (const bin of binCandidates(root, binary, flavour)) {
113    const file = flavour.installedAt(bin)
114
115    if (file !== null && !(await $.fs.exists(file))) continue
116
117    try {
118      const ran = await $.process.run([...bin, '--version'], { timeoutMs: 5_000 })
119
120      if (ran.exitCode === 0) {
121        found = { root, bin, version: ran.stdout.trim(), preset: '', presetVersion: '', types: [], flavour }
122        break
123      }
124    } catch {
125      // This candidate did not start, so the next one is tried, as after a failed exit.
126    }
127  }
128
129  if (found === null) return 'no-binary'
130
131  // An older khub prints documents the mod misreads, so every hook passes through.
132  if (isOlder(found.version, MIN_KHUB)) return 'old'
133
134  ws = found
135  await loadSchema($)
136
137  return 'ok'
138}
139
140// Reads the gate and the entities. A cut document, or a call that did not answer, keeps
141// what the band shows.
142async function runRefresh($: EngineInterface) {
143  const here = ws
144
145  if (!here) return
146
147  const [check, query] = await Promise.all([
148    khub($, here, ['check', '--format', 'json']),
149    khub($, here, ['query', '--format', 'json']),
150  ])
151  const found = check.isCut || query.isCut || query.exit !== 0 ? null : summaryOf(check.json, query.json)
152
153  if (found === null) return
154
155  await update($, summaryAtom, () => found.summary)
156  await update($, sessionAtom, session => withIds(session, found.ids))
157}
158
159// Queues a refresh behind the running one. The first refresh of a session fixes the baseline.
160function refresh($: EngineInterface): Promise<void> {
161  if (isQueued) return refreshing
162
163  isQueued = true
164
165  const run = () => {
166    isQueued = false
167
168    return runRefresh($)
169  }
170
171  refreshing = refreshing.then(run, run)
172
173  return refreshing
174}
175
176// Looks for the workspace and khub. The status line says why khub cannot be asked, and
177// the engine draws that line as a warning under the plugin's name.
178async function start($: EngineInterface, cwd: string, binary: string) {
179  const found = await detect($, cwd, binary)
180
181  if (found === 'no-binary') $.ui.status('not installed')
182  if (found === 'old') $.ui.status(`needs khub ${MIN_KHUB} or newer`)
183  if (found === 'ok') await refresh($)
184}
185
186// Follows an Edit or a Write of a file khub reads. An entity file counts its entity as
187// edited, and a schema file is read again before the band.
188async function afterFile($: EngineInterface, path: string) {
189  const here = ws
190  const inside = here ? relative(here, path) : null
191
192  if (!here || inside === null) return
193
194  if (inside.startsWith('.khub/')) {
195    background($, loadSchema($).then(() => refresh($)))
196
197    return
198  }
199
200  const entity = entityAt(here, path)
201
202  if (entity === null) return
203
204  // A collection file holds many entities, so it names none.
205  if (entity.slug !== '*') {
206    const id = `${entity.type}/${entity.slug}`
207
208    await update($, sessionAtom, session => withEdited(session, [id]))
209  }
210
211  background($, refresh($))
212}
213
214export const register: Register = (on, options) => {
215  const binary = String(options.binary ?? '')
216
217  // The session's directory is kept for a later look when khub could not be asked.
218  let cwd = ''
219
220  // The baseline is taken before the first prompt, so no write lands inside it.
221  on('session.start', async ($, e, next) => {
222    cwd = e.cwd
223    await start($, cwd, binary)
224
225    return next(e)
226  })
227
228  // A turn may follow edits made outside Claude, so the schema and the band are read again.
229  // Without a workspace the mod looks again, so trouble clears once its cause is gone.
230  on('turn.start', ($, e, next) => {
231    if (ws) background($, loadSchema($).then(() => refresh($)))
232    else if (cwd !== '') background($, start($, cwd, binary))
233
234    return next(e)
235  })
236
237  // The call runs unchanged and the model reads what khub printed. A simple call and a
238  // chain of calls get a compact row, and a call that changes the workspace is followed
239  // by a refresh.
240  on('tool.call', { tool: 'Bash' }, async ($, e, next) => {
241    if (!ws) return next(e)
242
243    const here = ws
244    const parsed = classify(e.command, here.flavour)
245    const calls = callsOf(parsed).filter(call => !isElsewhere(here, call.workspace))
246
247    if (calls.length === 0) return next(e)
248
249    const simple = parsed.kind === 'simple' ? parsed : null
250
251    // A chain keeps its row only while every call in it is aimed at this workspace.
252    const chain = parsed.kind === 'chain' && calls.length === parsed.calls.length ? parsed.calls : null
253    const shown = simple ? [simple] : (chain ?? [])
254    const row = memberOf(callRows, { requestId: e.tool_use_id ?? '' })
255
256    if (shown.length > 0) await update($, row, (): KhubCall[] => waiting(shown))
257
258    const startedAt = await $.clock.now()
259    const ran = await next(e)
260    const ms = (await $.clock.now()) - startedAt
261
262    // A call the user or a hook refused never ran, so its row is the engine's.
263    if (ran.deny !== undefined) {
264      if (shown.length > 0) await update($, row, () => null)
265
266      return ran
267    }
268
269    if (shown.length > 0) {
270      // A failed gate or a refusal arrives as an error whose text is what khub printed.
271      const result = (ran.isError === true ? undefined : ran.result) as
272        | { stdout?: string; stderr?: string; persistedOutputPath?: string }
273        | undefined
274      const stdout = ran.isError === true ? (ran.text ?? '') : (result?.stdout ?? '')
275      const stderr = result?.stderr ?? ''
276
277      // A simple call may print prose, or notes around its document. A chain must print
278      // one document per call, and one that does not is the engine's row.
279      const docs = simple ? [parseJson(stdout)] : documentsOf(stdout, shown.length)
280      const isProse = simple !== null && docs?.[0] === undefined
281
282      // Output the engine moved to disk may be cut here. A cut document is the engine's row.
283      const isCut = isProse && result?.persistedOutputPath !== undefined
284
285      // A prose command prints no document, so its first line is the result. stderr does
286      // not say which call of a chain wrote it, so a chain's rows carry no note.
287      const note = simple ? (isProse ? stdout || stderr : stderr) : ''
288      const edited = docs === null ? [] : shown.flatMap((call, i) => editedBy(call, docs[i]))
289
290      await update($, row, (): KhubCall[] | null => (docs === null || isCut ? null : finished(shown, docs, ms, note)))
291      if (edited.length > 0) await update($, sessionAtom, session => withEdited(session, edited))
292    }
293
294    if (calls.some(mutates)) background($, refresh($))
295
296    return ran
297  }).catch(async ($, e, next) => {
298    // A failure in the mod hands the row back to the engine and passes the call on.
299    await update($, memberOf(callRows, { requestId: e.tool_use_id ?? '' }), () => null)
300
301    return next(e)
302  })
303
304  on('tool.call', { tool: 'Edit' }, async ($, e, next) => {
305    const ran = await next(e)
306
307    if (ran.deny === undefined && ran.isError !== true) await afterFile($, e.file_path)
308
309    return ran
310  }).catch(($, e, next) => next(e))
311
312  on('tool.call', { tool: 'Write' }, async ($, e, next) => {
313    const ran = await next(e)
314
315    if (ran.deny === undefined && ran.isError !== true) await afterFile($, e.file_path)
316
317    return ran
318  }).catch(($, e, next) => next(e))
319
320  // The compact row holds a call's head, its result and its list, so it stands for the
321  // whole row. The `json` button hands the row back to the engine.
322  on('ui.render', { component: 'ToolUse' }, async ($, e, next) => {
323    if (!ws || e.props.tool !== 'Bash') return next(e)
324
325    const calls = await read($, memberOf(callRows, e))
326
327    if (calls === null || (await read($, memberOf(rawRows, e)))) return next(e)
328
329    return callRow($.ui.resolve(e), calls, () => background($, update($, memberOf(rawRows, e), () => true)))
330  })
331
332  // The detailed transcript draws a call's result as a block of its own. The compact row
333  // already shows the result, so the block of a row the mod drew is drawn as nothing.
334  on('ui.render', { component: 'ToolResult' }, async ($, e, next) => {
335    if (!ws || e.props.tool !== 'Bash') return next(e)
336
337    const calls = await read($, memberOf(callRows, e))
338
339    if (calls === null || (await read($, memberOf(rawRows, e)))) return next(e)
340
341    return emptyBlock($.ui.resolve(e))
342  })
343
344  on('ui.render', { component: 'AbovePrompt' }, async ($, e, next) => {
345    if (!ws || e.props.hasSurvey) return next(e)
346
347    const summary = await read($, summaryAtom)
348    const workspace = await read($, workspaceAtom)
349
350    // Before the first refresh there is nothing to say yet.
351    if (summary === null || workspace === null) return next(e)
352
353    const parts = bandParts(workspace.preset, workspace.presetVersion, summary, diffOf(await read($, sessionAtom)))
354
355    return summaryLine($.ui.resolve(e), parts, !summary.passed, e.props.bodyColumns)
356  })
357}
358
hooks/band.tsx 27 lines
1import type { RenderElement } from 'claude-code'
2
3import { toneColor } from './el'
4import type { El } from './el'
5import type { Part } from './session'
6
7// Draws the one line above the prompt, cut to `columns`. A failing check draws the line red.
8// Otherwise a plain part is dim and a toned part takes its color.
9export function summaryLine({ Box, Text }: El, parts: Part[], isFailing: boolean, columns: number): RenderElement {
10  const style = (part: Part) => (isFailing ? { color: 'red' } : part.tone === 'plain' ? { dimColor: true } : toneColor(part.tone))
11
12  return (
13    <Box width={columns}>
14      <Box flexShrink={0} marginRight={2}>
15        <Text {...(isFailing ? { color: 'red' } : { dimColor: true })}>khub</Text>
16      </Box>
17      <Box flexShrink={1}>
18        <Text wrap="truncate-end">
19          {parts.map(part => (
20            <Text {...style(part)}>{part.text}</Text>
21          ))}
22        </Text>
23      </Box>
24    </Box>
25  )
26}
27
hooks/cli.ts 67 lines
1// This file reads khub's output.
2
3export type Row = Record<string, unknown>
4export type Refusal = { code: string; message: string }
5
6// Returns the lines that may hold a JSON document. khub prints each document on one line.
7const documentLines = (text: string) => text.split('\n').filter(line => /^[[{]/.test(line))
8
9// Returns the first JSON document in khub's output. A `2>&1` call may carry `note:` lines
10// around it.
11export function parseJson(text: string): unknown {
12  const trimmed = text.trim()
13
14  for (const candidate of [trimmed, ...documentLines(trimmed)]) {
15    try {
16      return JSON.parse(candidate)
17    } catch {
18      continue
19    }
20  }
21
22  return undefined
23}
24
25// Returns one JSON document per call of a chain, or null when the output does not hold
26// exactly `count` of them.
27export function documentsOf(text: string, count: number): unknown[] | null {
28  const lines = documentLines(text)
29
30  if (lines.length !== count) return null
31
32  try {
33    return lines.map(line => JSON.parse(line) as unknown)
34  } catch {
35    return null
36  }
37}
38
39// khub's error envelope is `{"error": {"code", "message"}}`, printed on stdout with exit 2.
40export function refusal(json: unknown): Refusal | undefined {
41  const error = (json as { error?: Partial<Refusal> } | undefined)?.error
42
43  return typeof error?.code === 'string' ? { code: error.code, message: String(error.message ?? '') } : undefined
44}
45
46// These `khub check` buckets never fail the gate. Orphans fail it under `--strict`.
47export const INFORMATIONAL = new Set(['draft_singletons', 'orphans', 'thin'])
48
49export const isRow = (value: unknown): value is Row =>
50  typeof value === 'object' && value !== null && !Array.isArray(value)
51
52export const rowsOf = (json: unknown): Row[] => (Array.isArray(json) ? json.filter(isRow) : [])
53
54export const text = (value: unknown) => (value === undefined || value === null ? '' : String(value))
55
56export const count = (n: number, one: string, many = `${one}s`) => `${n} ${n === 1 ? one : many}`
57
58// Returns the first line that says something.
59export const firstLine = (lines: string) =>
60  lines
61    .split('\n')
62    .map(line => line.trim())
63    .find(line => line !== '') ?? ''
64
65// Entity text is untrusted, so control characters never reach the terminal.
66export const clean = (value: string) => value.replace(/[\u0000-\u001f\u007f-\u009f]/g, ' ')
67
hooks/parse.ts 390 lines
1// This file recognizes khub invocations in a Bash command string.
2
3import type { Flavour } from './workspace'
4
5export type Simple = {
6  kind: 'simple'
7  verb: string
8
9  // Holds the second word of a two-word verb, such as `show` in `schema show adr`.
10  sub: string | null
11
12  // Holds the value of `-C` or `--workspace`, when the call names one.
13  workspace: string | null
14  positional: string[]
15  flags: Record<string, string | true>
16}
17
18// `simple` is one khub call whose stdout is khub's alone. `chain` is several such calls
19// joined by `&&` or `;`. `compound` runs khub among other things, and lists the khub
20// calls it could read.
21export type Parsed =
22  | { kind: 'none' }
23  | { kind: 'compound'; calls: Simple[] }
24  | { kind: 'chain'; calls: Simple[] }
25  | Simple
26
27type Word = { text: string }
28type Operator = { op: '&&' | '||' | ';' | '|' | '&' | '>out' | '>err' | '<' }
29type Token = Word | Operator
30type Segment = { words: string[]; ops: string[] }
31
32const NONE: Parsed = { kind: 'none' }
33const BOOLEAN_FLAGS = new Set([
34  'active', 'draft', 'dry-run', 'edges', 'force', 'help', 'in', 'no-schema',
35  'no-template', 'no-wire', 'open', 'orphan', 'out', 'plain', 'reverse', 'stale', 'strict',
36])
37const WRITES = new Set(['add', 'edit', 'link', 'unlink', 'remove'])
38const OPERATORS = new Set(['backfill', 'init', 'reindex', 'upgrade', 'wire'])
39const SCHEMA_VIEWS = new Set(['base', 'diff', 'edges', 'show', 'snapshot', 'types'])
40
41// These flags of `khub edit` name no field.
42const EDIT_OPTIONS = new Set(['format', 'strict'])
43
44const isWord = (token: Token): token is Word => 'text' in token
45const isOn = (flag: string | true | undefined) => flag !== undefined && flag !== 'false'
46const call = (verb: string, flags: Simple['flags'] = {}): Simple => ({
47  kind: 'simple',
48  verb,
49  sub: null,
50  workspace: null,
51  positional: [],
52  flags,
53})
54
55// Returns the index of the `)` that closes the `$(` opening at `start`, or -1.
56function closeOf(command: string, start: number): number {
57  let depth = 0
58
59  for (let i = start + 1; i < command.length; i += 1) {
60    const c = command[i]
61
62    if (c === '\\') {
63      i += 1
64    } else if (c === "'") {
65      const end = command.indexOf("'", i + 1)
66
67      if (end < 0) return -1
68      i = end
69    } else if (c === '(') {
70      depth += 1
71    } else if (c === ')') {
72      depth -= 1
73      if (depth === 0) return i
74    }
75  }
76
77  return -1
78}
79
80// Returns the command with the body of the heredoc that opens at `at` (just past `<<`) cut
81// out, and the index where reading goes on. It is null when the heredoc never closes.
82function withoutHeredoc(command: string, at: number): { command: string; next: number } | null {
83  const opener = /^-?\s*(['"]?)([^\s'"<>|;&]+)\1/.exec(command.slice(at))
84  const lineEnd = command.indexOf('\n', at)
85
86  if (!opener || lineEnd < 0) return null
87
88  const stripsTabs = command[at] === '-'
89  const lines = command.slice(lineEnd + 1).split('\n')
90  const closing = lines.findIndex(line => (stripsTabs ? line.replace(/^\t+/, '') : line) === opener[2])
91
92  if (closing < 0) return null
93
94  return {
95    command: [command.slice(0, lineEnd), ...lines.slice(closing + 1)].join('\n'),
96    next: at + opener[0].length,
97  }
98}
99
100// Splits a command into words and operators. A substitution stays inside its word,
101// unread. Returns null for a construct it does not follow, such as a subshell or an
102// unterminated quote.
103export function tokenize(input: string): Token[] | null {
104  const tokens: Token[] = []
105  let command = input
106  let word: string | null = null
107  let isTarget = false
108  let i = 0
109
110  // A redirection's target is read as a word and dropped.
111  const flush = () => {
112    if (word !== null && !isTarget) tokens.push({ text: word })
113    if (word !== null) isTarget = false
114    word = null
115  }
116
117  // Takes a `$(…)` or a backtick span whole into the current word.
118  const substitution = (): boolean => {
119    const end = command[i] === '`' ? command.indexOf('`', i + 1) : closeOf(command, i)
120
121    if (end < 0) return false
122    word = (word ?? '') + command.slice(i, end + 1)
123    i = end + 1
124
125    return true
126  }
127
128  while (i < command.length) {
129    const c = command[i] as string
130    const pair = command.slice(i, i + 2)
131
132    if (c === "'") {
133      const end = command.indexOf("'", i + 1)
134
135      if (end < 0) return null
136      word = (word ?? '') + command.slice(i + 1, end)
137      i = end + 1
138    } else if (c === '"') {
139      word ??= ''
140      i += 1
141
142      while (command[i] !== '"') {
143        if (i >= command.length) return null
144
145        if (command[i] === '`' || command.slice(i, i + 2) === '$(') {
146          if (!substitution()) return null
147          continue
148        }
149
150        if (command[i] === '\\' && '"\\$`'.includes(command[i + 1] ?? '')) i += 1
151        word += command[i]
152        i += 1
153      }
154
155      i += 1
156    } else if (c === '\\') {
157      word = (word ?? '') + (command[i + 1] ?? '')
158      i += 2
159    } else if (c === '`' || pair === '$(') {
160      if (!substitution()) return null
161    } else if (c === '(' || c === ')') {
162      return null
163    } else if (c === ' ' || c === '\t') {
164      flush()
165      i += 1
166    } else if (c === '\n' || c === ';') {
167      flush()
168      tokens.push({ op: ';' })
169      i += 1
170    } else if (pair === '&&' || pair === '||') {
171      flush()
172      tokens.push({ op: pair })
173      i += 2
174    } else if (pair === '<<' && command[i + 2] !== '<') {
175      const cut = withoutHeredoc(command, i + 2)
176
177      if (cut === null) return null
178      flush()
179      tokens.push({ op: '<' })
180      command = cut.command
181      i = cut.next
182    } else if (c === '>' || c === '<' || pair === '&>') {
183      // A digit word straight before `>` is the descriptor, such as `2` in `2>`.
184      const fd = pair !== '&>' && word !== null && /^\d+$/.test(word) ? word : ''
185
186      if (fd === '') flush()
187      word = null
188      i += pair === '&>' ? 2 : 1
189      while (command[i] === '>' || command[i] === '<') i += 1
190      tokens.push({ op: c === '<' ? '<' : fd === '2' ? '>err' : '>out' })
191
192      if (command[i] === '&') {
193        i += 1
194        while (/\d/.test(command[i] ?? '')) i += 1
195      } else {
196        isTarget = true
197      }
198    } else if (c === '|' || c === '&') {
199      flush()
200      tokens.push({ op: c })
201      i += 1
202    } else {
203      word = (word ?? '') + c
204      i += 1
205    }
206  }
207
208  flush()
209
210  return tokens
211}
212
213// Matches a word that names the khub binary: the name alone or at the end of a path.
214const binaryOf = ({ name, separator }: Flavour) => new RegExp(`(?:^|${separator})${name}$`)
215
216// Matches a khub call in raw command text, capturing its verb and the rest of its segment.
217const scanOf = ({ name, separator }: Flavour) =>
218  new RegExp(`(?:^|[\\s;&|(\`])(?:[^\\s;&|]*${separator})?${name}\\s+(?:(?:-C|--workspace)\\s+\\S+\\s+)?([a-z][a-z-]*)([^;&|\\n]*)`, 'g')
219
220// Reads one segment's words as a khub call, or null when it runs something else.
221function khubCall(words: string[], binary: RegExp): Simple | null {
222  let i = 0
223
224  while (/^[A-Za-z_][A-Za-z0-9_]*=/.test(words[i] ?? '')) i += 1
225
226  const program = words[i] ?? ''
227
228  if (program === 'npx') {
229    i += 1
230    while ((words[i] ?? '').startsWith('-')) i += 1
231    if (!/^@endgame-build\/khub(@|$)/.test(words[i] ?? '')) return null
232  } else if (!binary.test(program)) {
233    return null
234  }
235
236  i += 1
237
238  let workspace: string | null = null
239
240  // Global options come before the verb.
241  for (;;) {
242    const word = words[i] ?? ''
243
244    if (word === '-C' || word === '--workspace') {
245      workspace = words[i + 1] ?? null
246      i += 2
247    } else if (word.startsWith('--workspace=')) {
248      workspace = word.slice('--workspace='.length)
249      i += 1
250    } else {
251      break
252    }
253  }
254
255  const verb = words[i] ?? ''
256
257  if (verb === '' || verb.startsWith('-')) return { ...call(verb.replace(/^--/, '')), workspace }
258
259  i += 1
260
261  const found: Simple = { ...call(verb), workspace }
262
263  if (verb === 'schema' && SCHEMA_VIEWS.has(words[i] ?? '')) {
264    found.sub = words[i] as string
265    i += 1
266  }
267
268  while (i < words.length) {
269    const word = words[i] as string
270
271    if (!word.startsWith('--')) {
272      found.positional.push(word)
273      i += 1
274      continue
275    }
276
277    const equals = word.indexOf('=')
278    const name = word.slice(2, equals < 0 ? undefined : equals)
279    const next = words[i + 1]
280
281    if (equals >= 0) {
282      found.flags[name] = word.slice(equals + 1)
283      i += 1
284    } else if (BOOLEAN_FLAGS.has(name) || next === undefined || next.startsWith('--')) {
285      found.flags[name] = true
286      i += 1
287    } else {
288      found.flags[name] = next
289      i += 2
290    }
291  }
292
293  return found
294}
295
296// Reads the khub calls off the text of a command the tokenizer could not follow. It keeps
297// each verb, and whether `--force` or `--dry-run` follows it.
298function scan(command: string, flavour: Flavour): Simple[] {
299  const calls = command.matchAll(scanOf(flavour))
300
301  return [...calls].map(match => {
302    const rest = match[2] ?? ''
303
304    return call(match[1] as string, {
305      ...(rest.includes('--force') ? { force: true as const } : {}),
306      ...(rest.includes('--dry-run') ? { 'dry-run': true as const } : {}),
307    })
308  })
309}
310
311// Reads a Bash command. On Windows, where Git Bash runs it, the binary may be `khub.exe` and
312// its path may hold backslashes.
313export function classify(command: string, flavour: Flavour): Parsed {
314  if (!new RegExp(flavour.name).test(command)) return NONE
315
316  const tokens = tokenize(command)
317
318  if (tokens === null) {
319    const calls = scan(command, flavour)
320
321    return calls.length === 0 ? NONE : { kind: 'compound', calls }
322  }
323
324  // Collects segments between control operators, with the operators and redirections each saw.
325  const segments: Segment[] = [{ words: [], ops: [] }]
326  const separators: string[] = []
327
328  for (const token of tokens) {
329    const segment = segments[segments.length - 1] as Segment
330
331    if (isWord(token)) {
332      segment.words.push(token.text)
333    } else if (token.op === '>out' || token.op === '>err' || token.op === '<') {
334      segment.ops.push(token.op)
335    } else {
336      separators.push(token.op)
337      segments.push({ words: [], ops: [] })
338    }
339  }
340
341  const filled = segments.filter(segment => segment.words.length > 0)
342  const binary = binaryOf(flavour)
343  const read = filled.map(segment => khubCall(segment.words, binary))
344  const calls = read.filter((found): found is Simple => found !== null)
345
346  if (calls.length === 0) return NONE
347
348  const firstKhub = read.findIndex(found => found !== null)
349
350  // The output is khub's alone when nothing but `cd <dir>` comes before the first khub call,
351  // every part from there on is a khub call with stdout left alone, and `&&` or `;` joins them.
352  const isKhubOnly =
353    separators.every(op => op === '&&' || op === ';') &&
354    filled.every((segment, i) =>
355      i < firstKhub
356        ? segment.words[0] === 'cd' && segment.words.length === 2
357        : read[i] !== null && !segment.ops.includes('>out'),
358    )
359
360  if (!isKhubOnly) return { kind: 'compound', calls }
361
362  return calls.length === 1 ? (calls[0] as Simple) : { kind: 'chain', calls }
363}
364
365// Returns the khub calls of a command, however it was read.
366export const callsOf = (parsed: Parsed): Simple[] =>
367  parsed.kind === 'simple' ? [parsed] : parsed.kind === 'none' ? [] : parsed.calls
368
369export const isWrite = (found: Simple) => WRITES.has(found.verb)
370
371// Tells whether a call changes entities, the schema, the index or the agent files.
372export const mutates = (found: Simple) =>
373  isWrite(found) ||
374  (OPERATORS.has(found.verb) && !isOn(found.flags['dry-run'])) ||
375  (found.verb === 'schema' && found.sub === 'snapshot')
376
377// Returns what a `khub edit` call changed, as `<field> <value>` pairs. A body is named,
378// never quoted.
379export function edited(found: Simple): string {
380  const [, field, value] = found.positional
381  const pairs = field === undefined ? [] : [field === 'body' ? 'body' : `${field} ${value ?? ''}`.trim()]
382
383  for (const [name, flag] of Object.entries(found.flags)) {
384    if (EDIT_OPTIONS.has(name)) continue
385    pairs.push(name === 'body' || name === 'body-file' ? 'body' : flag === true ? name : `${name} ${flag}`)
386  }
387
388  return pairs.join(', ')
389}
390
hooks/rows.tsx 128 lines
1// This file draws the transcript row of a command's khub calls. Each call gets a head line,
2// a result line, and the first rows of a list.
3
4import type { RenderElement } from 'claude-code'
5
6import type { KhubCall, KhubRow, KhubTone } from '../types'
7import { toneColor as tint } from './el'
8import type { El } from './el'
9
10// Caps the padding of a list's first column.
11const TEXT_MAX = 48
12
13const duration = (ms: number) => (ms < 1000 ? `${Math.round(ms)} ms` : `${(ms / 1000).toFixed(1)} s`)
14
15// Splits a result line into `[mark, text, check]`. An ok line is colored on its leading sign and
16// its trailing check alone; a warning or an error is colored whole.
17function partsOf(line: string, tone: KhubTone): [string, string, string] {
18  if (tone !== 'ok') return ['', line, '']
19
20  const sign = /^[+~→⇢−] /.exec(line)?.[0] ?? ''
21  const check = line.endsWith(' ✓') ? ' ✓' : ''
22
23  return [sign, line.slice(sign.length, line.length - check.length), check]
24}
25
26// Draws the head line. `onRaw` flips the row to the engine's own drawing, and only the
27// first call of a command carries its button.
28function headLine({ Box, Text, Button }: El, call: KhubCall, onRaw: (() => void) | null): RenderElement {
29  return (
30    <Box>
31      <Box flexShrink={0}>
32        <Text {...tint(call.tone)} dimColor={call.isRunning}>
33          {'● '}
34        </Text>
35      </Box>
36      <Text bold wrap="truncate-end">
37        {call.head}
38      </Text>
39      <Box flexShrink={0}>
40        {call.ms > 0 && <Text dimColor>{` ${duration(call.ms)}`}</Text>}
41        {onRaw !== null && <Text> </Text>}
42        {onRaw !== null && <Button key="json" label="json" plain dimColor onPress={onRaw} />}
43      </Box>
44    </Box>
45  )
46}
47
48// Draws `  ⎿ ` and what the call answered. The text is the one part that truncates.
49function resultLine({ Box, Text }: El, call: KhubCall): RenderElement {
50  if (call.line === '') {
51    return (
52      <Box>
53        <Text dimColor>{call.isRunning ? '  ⎿ …' : '  ⎿ (no output)'}</Text>
54      </Box>
55    )
56  }
57
58  const [sign, text, check] = partsOf(call.line, call.tone)
59  const whole = call.tone === 'ok' ? {} : tint(call.tone)
60
61  return (
62    <Box>
63      <Box flexShrink={0}>
64        <Text dimColor>{'  ⎿ '}</Text>
65        {sign !== '' && <Text {...tint(call.tone)}>{sign}</Text>}
66      </Box>
67      <Text {...whole} wrap="truncate-end">
68        {text}
69      </Text>
70      {check !== '' && (
71        <Box flexShrink={0}>
72          <Text {...tint(call.tone)}>{check}</Text>
73        </Box>
74      )}
75    </Box>
76  )
77}
78
79// Draws one list row under the result line: the padded first column, the note, the dim flags.
80function listRow({ Box, Text }: El, row: KhubRow, width: number): RenderElement {
81  return (
82    <Box>
83      <Box flexShrink={0}>
84        <Text>{`    ${row.text.padEnd(width)}`}</Text>
85      </Box>
86      <Text wrap="truncate-end">{row.note === '' ? '' : `   ${row.note}`}</Text>
87      {row.flags.length > 0 && (
88        <Box flexShrink={0}>
89          <Text dimColor>{`   ${row.flags.join(' ')}`}</Text>
90        </Box>
91      )}
92    </Box>
93  )
94}
95
96// Draws the lines of one khub call. The list's first column is padded to its widest id.
97function callLines(el: El, call: KhubCall, onRaw: (() => void) | null): RenderElement[] {
98  const { Box, Text } = el
99  const width = Math.min(TEXT_MAX, Math.max(0, ...call.rows.map(row => row.text.length)))
100
101  return [
102    headLine(el, call, onRaw),
103    resultLine(el, call),
104    ...call.rows.map(row => listRow(el, row, width)),
105    ...(call.more > 0
106      ? [
107          <Box>
108            <Text dimColor>{`    … ${call.more} more`}</Text>
109          </Box>,
110        ]
111      : []),
112  ]
113}
114
115// Draws the whole row of a command's khub calls, one after another. The engine may draw a
116// call's result inside its ToolUse row, so this one tree holds the heads, the results and
117// the lists.
118export function callRow(el: El, calls: KhubCall[], onRaw: () => void): RenderElement {
119  const { Box } = el
120
121  return <Box flexDirection="column">{calls.flatMap((call, i) => callLines(el, call, i === 0 ? onRaw : null))}</Box>
122}
123
124// Draws a result block with nothing in it, for a call whose row already shows the result.
125export function emptyBlock({ Box }: El): RenderElement {
126  return <Box />
127}
128
hooks/session.ts 85 lines
1// This file tracks what the session changed and builds the band's line. The functions are pure.
2
3import type { KhubSession, KhubSummary, KhubTone } from '../types'
4import { count, isRow, refusal, rowsOf, text } from './cli'
5import type { Simple } from './parse'
6import { failingOf } from './summarize'
7
8export const EMPTY_SESSION: KhubSession = { baseline: null, current: [], edited: [] }
9
10// These verbs change an entity in place. An add or a remove moves the id set instead.
11const EDITS = new Set(['edit', 'link', 'unlink'])
12
13// Returns what a refresh read from `khub check` and `khub query`, or null when either
14// document is not the one khub prints.
15export function summaryOf(check: unknown, query: unknown): { summary: KhubSummary; ids: string[] } | null {
16  if (!isRow(check) || typeof check.passed !== 'boolean' || !Array.isArray(query)) return null
17
18  const rows = rowsOf(query)
19  const errors = failingOf(check).reduce((sum, found) => sum + found.items.length, 0)
20
21  return {
22    summary: { total: rows.length, draft: rows.filter(row => row.draft === true).length, passed: check.passed, errors },
23    ids: rows.map(row => text(row.id)).filter(id => id !== ''),
24  }
25}
26
27// Returns the entity a khub call changed in place, by qualified id. A refusal and an edge
28// call that changed nothing name none.
29export function editedBy(call: Simple, json: unknown): string[] {
30  if (!EDITS.has(call.verb) || !isRow(json) || refusal(json) || json.changed === false) return []
31
32  const id = text(json.id)
33
34  return id === '' ? [] : [id]
35}
36
37// Returns the session after a refresh read `ids`. The first read fixes the baseline.
38export const withIds = (session: KhubSession, ids: string[]): KhubSession => ({
39  ...session,
40  baseline: session.baseline ?? ids,
41  current: ids,
42})
43
44// Returns the session after its tools changed the entities `ids` name.
45export function withEdited(session: KhubSession, ids: string[]): KhubSession {
46  const fresh = ids.filter(id => !session.edited.includes(id))
47
48  return fresh.length === 0 ? session : { ...session, edited: [...session.edited, ...fresh] }
49}
50
51export type Diff = { added: number; removed: number; edited: number }
52
53// Counts against the baseline. An entity added and then edited counts as added alone.
54export function diffOf(session: KhubSession): Diff {
55  const baseline = new Set(session.baseline ?? session.current)
56  const current = new Set(session.current)
57
58  return {
59    added: session.current.filter(id => !baseline.has(id)).length,
60    removed: [...baseline].filter(id => !current.has(id)).length,
61    edited: session.edited.filter(id => baseline.has(id) && current.has(id)).length,
62  }
63}
64
65export type Part = { text: string; tone: KhubTone }
66
67// Returns the band's line after its `khub` label, such as
68// `build-hub 0.6.0 · 87 entities [+2 −1 ~3] · 4 draft · check ✓`. A plain part draws dim.
69export function bandParts(preset: string, version: string, summary: KhubSummary, diff: Diff): Part[] {
70  const plain = (text: string): Part => ({ text, tone: 'plain' })
71  const changes: Part[] = [
72    ...(diff.added > 0 ? [{ text: `+${diff.added}`, tone: 'ok' as const }] : []),
73    ...(diff.removed > 0 ? [{ text: `−${diff.removed}`, tone: 'bad' as const }] : []),
74    ...(diff.edited > 0 ? [plain(`~${diff.edited}`)] : []),
75  ]
76  const name = [preset, version].filter(word => word !== '').join(' ')
77  const check = summary.passed ? 'check ✓' : `check ✗ ${count(summary.errors, 'error')}`
78
79  return [
80    plain(`${name === '' ? '' : `${name} · `}${count(summary.total, 'entity', 'entities')}`),
81    ...(changes.length > 0 ? [plain(' ['), ...changes.flatMap((part, i) => (i > 0 ? [plain(' '), part] : [part])), plain(']')] : []),
82    plain(`${summary.draft > 0 ? ` · ${summary.draft} draft` : ''} · ${check}`),
83  ]
84}
85
hooks/summarize.ts 282 lines
1import type { KhubCall, KhubRow, KhubTone } from '../types'
2import { clean, count, firstLine, INFORMATIONAL, isRow, refusal, rowsOf, text } from './cli'
3import type { Row } from './cli'
4import { edited } from './parse'
5import type { Simple } from './parse'
6
7export type Summary = { head: string; line: string; tone: KhubTone }
8
9type Result = { line: string; tone: KhubTone }
10
11// The first positional of these verbs names what the call is about, a type for add and an
12// id for the rest.
13const SUBJECT_VERBS = new Set([
14  'add', 'edit', 'get', 'history', 'impact', 'link', 'neighbors', 'remove', 'unlink', 'validate',
15])
16
17const NOTHING: Result = { line: '', tone: 'plain' }
18
19const sizeOf = (value: unknown) => (Array.isArray(value) ? value.length : 0)
20
21// Returns the title a record carries, at its top or in its frontmatter. An entity is
22// titled by `title` or by `name`, so both are read.
23function titleOf(row: Row): string {
24  const meta = isRow(row.frontmatter) ? row.frontmatter : row
25
26  return text(meta.title ?? meta.name)
27}
28
29// Returns the buckets of a `khub check` payload that fail the gate, each with its findings.
30export function failingOf(check: Row): Array<{ bucket: string; items: unknown[] }> {
31  return Object.entries(check)
32    .filter((entry): entry is [string, unknown[]] => Array.isArray(entry[1]) && entry[1].length > 0)
33    .filter(([bucket]) => !INFORMATIONAL.has(bucket) || (bucket === 'orphans' && check.strict === true))
34    .map(([bucket, items]) => ({ bucket, items }))
35}
36
37function subjectOf(call: Simple): string {
38  if (call.verb === 'query') return typeof call.flags.type === 'string' ? call.flags.type : ''
39  if (call.verb === 'search') return call.positional[0] === undefined ? '' : `"${call.positional[0]}"`
40  if (call.verb === 'schema') return call.sub === 'show' ? (call.positional[0] ?? '') : ''
41
42  return SUBJECT_VERBS.has(call.verb) ? (call.positional[0] ?? '') : ''
43}
44
45// The subject is text the agent typed, so it is cleaned like entity text.
46const headOf = (call: Simple) =>
47  clean(['khub', call.verb, call.sub ?? '', subjectOf(call)].filter(word => word !== '').join(' '))
48
49function edgeResult(call: Simple, row: Row): Result {
50  if (row.changed === false) {
51    return { line: call.verb === 'link' ? 'edge already present' : 'no such edge', tone: 'plain' }
52  }
53
54  // An edge added draws `→`, an edge taken away `⇢`.
55  const sign = call.verb === 'link' ? '→' : '⇢'
56
57  return { line: `${sign} ${text(row.slug)} ${text(row.predicate)} ${text(row.target)}`, tone: 'ok' }
58}
59
60function searchResult(rows: Row[], note: string): Result {
61  const hits = rows.length === 0 ? 'no hits' : `${count(rows.length, 'hit')} · top ${text(rows[0]?.id)}`
62  const noted = firstLine(note).replace(/^note: /, '').slice(0, 80)
63
64  if (noted === '') return { line: hits, tone: 'plain' }
65
66  // khub's own note for an empty result already says there were no hits.
67  return { line: rows.length === 0 && /^no hits/i.test(noted) ? noted : `${hits} · ${noted}`, tone: 'warn' }
68}
69
70function validateResult(row: Row): Result {
71  const errors = sizeOf(row.errors)
72  const gaps = sizeOf(row.gaps)
73  const line = `${text(row.count)} checked · ${count(errors, 'error')} · ${count(gaps, 'gap')}`
74
75  if (errors > 0) return { line, tone: 'bad' }
76
77  return gaps > 0 ? { line, tone: 'warn' } : { line: `${line} ✓`, tone: 'ok' }
78}
79
80function checkResult(row: Row): Result {
81  if (row.passed === true) return { line: 'passed ✓', tone: 'ok' }
82
83  const failing = failingOf(row)
84
85  return { line: ['failed', ...failing.map(found => `${found.bucket} ${found.items.length}`)].join(' · '), tone: 'bad' }
86}
87
88function schemaResult(call: Simple, json: unknown): Result {
89  const row = isRow(json) ? json : {}
90
91  if (call.sub === null || call.sub === 'types') {
92    return { line: count(sizeOf(Array.isArray(json) ? json : row.types), 'type'), tone: 'plain' }
93  }
94
95  if (call.sub === 'show') {
96    const fields = count(sizeOf(row.fields), 'field')
97
98    return { line: `${text(row.name)} · ${fields} · ${count(sizeOf(row.relations), 'relation')}`, tone: 'plain' }
99  }
100
101  if (call.sub === 'edges') return { line: count(sizeOf(json), 'predicate'), tone: 'plain' }
102
103  if (call.sub === 'diff') {
104    const line = row.pending === true ? `pending · ${count(sizeOf(row.changes), 'change')}` : 'no pending changes'
105
106    return { line, tone: 'plain' }
107  }
108
109  return call.sub === 'snapshot' ? { line: `snapshot · ${count(Number(row.types), 'type')}`, tone: 'ok' } : NOTHING
110}
111
112// Reads what a call's JSON document says, by verb and by the document's shape.
113function resultOf(call: Simple, json: unknown, note: string): Result {
114  const rows = rowsOf(json)
115  const row = isRow(json) ? json : {}
116  const entities = { line: rows.length === 0 ? 'no entities' : count(rows.length, 'entity', 'entities'), tone: 'plain' } as const
117
118  switch (call.verb) {
119    case 'add':
120      return { line: `+ ${text(row.id)}${row.draft === true ? ' · draft' : ''}`, tone: 'ok' }
121
122    case 'edit': {
123      const fields = edited(call)
124
125      return { line: `~ ${text(row.id ?? call.positional[0])}${fields === '' ? '' : ` · ${fields}`}`, tone: 'ok' }
126    }
127
128    case 'link':
129    case 'unlink':
130      return edgeResult(call, row)
131
132    case 'remove':
133      return { line: `− ${text(row.id)}`, tone: 'ok' }
134
135    case 'get': {
136      if (Array.isArray(json)) return entities
137
138      const title = titleOf(row)
139      const edges = Array.isArray(row.edges) ? count(row.edges.length, 'edge') : ''
140
141      return { line: [text(row.id), title, edges].filter(part => part !== '').join(' · '), tone: 'plain' }
142    }
143
144    case 'query':
145    case 'stale':
146      return entities
147
148    case 'search':
149      return searchResult(rows, note)
150
151    case 'neighbors': {
152      const inbound = rows.filter(found => found.direction === 'in').length
153
154      return { line: `${count(rows.length, 'neighbor')} · in ${inbound} · out ${rows.length - inbound}`, tone: 'plain' }
155    }
156
157    case 'impact': {
158      // The depth-0 row is the entity itself.
159      const depths = rows.map(found => Number(found.depth)).filter(depth => depth > 0)
160
161      return { line: `${depths.length} affected · depth ${Math.max(0, ...depths)}`, tone: 'plain' }
162    }
163
164    case 'history':
165      return { line: `${rows.length} in chain`, tone: 'plain' }
166
167    case 'status': {
168      const total = count(Number(row.total), 'entity', 'entities')
169
170      return { line: `${total} · ${text(row.draft)} draft · ${text(row.orphan)} orphan · ${text(row.stale)} stale`, tone: 'plain' }
171    }
172
173    case 'validate':
174      return validateResult(row)
175
176    case 'check':
177      return checkResult(row)
178
179    case 'schema':
180      return schemaResult(call, json)
181
182    default:
183      return { line: firstLine(note), tone: 'plain' }
184  }
185}
186
187// Returns one line for a khub call. `head` names the call and `line` says what came back.
188// `json` is khub's stdout document (undefined when it printed none), `note` its stderr.
189export function summarize(call: Simple, json: unknown, note: string): Summary {
190  const head = headOf(call)
191  const refused = refusal(json)
192
193  if (refused) return { head, line: clean(`✗ ${refused.code}: ${refused.message}`), tone: 'bad' }
194
195  // With no document the call is still running, or it printed prose.
196  if (json === undefined) return { head, line: clean(firstLine(note)), tone: 'plain' }
197
198  const result = resultOf(call, json, note)
199
200  return { head, line: clean(result.line), tone: result.tone }
201}
202
203// Sets how many list rows a call's row shows before `… n more`.
204const PREVIEW = 5
205
206const FLAGS = ['draft', 'orphan', 'stale']
207
208const entityRow = (row: Row): KhubRow => ({
209  text: text(row.id),
210  note: titleOf(row),
211  flags: FLAGS.filter(flag => row[flag] === true),
212})
213
214// Returns one row per finding of a bucket that fails the gate, named by its id, path or alias.
215function findingRows(check: Row): KhubRow[] {
216  return failingOf(check).flatMap(found =>
217    found.items.map(item => ({
218      text: found.bucket,
219      note: isRow(item) ? text(item.id ?? item.path ?? item.alias) : text(item),
220      flags: [],
221    })),
222  )
223}
224
225// Returns every row a call's document lists, by verb.
226function listOf(call: Simple, json: unknown): KhubRow[] {
227  const rows = rowsOf(json)
228  const row = isRow(json) ? json : {}
229
230  switch (call.verb) {
231    case 'get':
232    case 'query':
233    case 'search':
234    case 'stale':
235      return rows.map(entityRow)
236
237    case 'neighbors':
238      return rows.map(found => ({
239        text: text(found.id),
240        note: `${text(found.direction)} ${text(found.predicate)}`.trim(),
241        flags: [],
242      }))
243
244    case 'check':
245      return row.passed === false ? findingRows(row) : []
246
247    case 'validate':
248      return rowsOf(row.errors).map(found => ({
249        text: text(found.id),
250        note: `${text(found.field)}: ${text(found.reason)}`,
251        flags: [],
252      }))
253
254    default:
255      return []
256  }
257}
258
259// Returns the first rows of a list result, and how many more it holds.
260export function previewOf(call: Simple, json: unknown): { rows: KhubRow[]; more: number } {
261  const all = refusal(json) ? [] : listOf(call, json)
262
263  return {
264    rows: all.slice(0, PREVIEW).map(row => ({ ...row, text: clean(row.text), note: clean(row.note) })),
265    more: Math.max(0, all.length - PREVIEW),
266  }
267}
268
269// Returns the rows of a command's calls while the command runs.
270export const waiting = (calls: Simple[]): KhubCall[] =>
271  calls.map(call => ({ ...summarize(call, undefined, ''), isRunning: true, ms: 0, rows: [], more: 0 }))
272
273// Returns the rows of a command's calls once it has answered, each read from its own
274// document. The first row carries the command's duration.
275export const finished = (calls: Simple[], docs: unknown[], ms: number, note: string): KhubCall[] =>
276  calls.map((call, i) => ({
277    ...summarize(call, docs[i], note),
278    isRunning: false,
279    ms: i === 0 ? ms : 0,
280    ...previewOf(call, docs[i]),
281  }))
282
hooks/workspace.ts 216 lines
1// This file handles paths and layouts of a khub workspace. The functions are pure, and the
2// shell holds the workspace value.
3
4import type { KhubType, KhubWorkspace } from '../types'
5
6export type Entity = { type: string; slug: string }
7
8// What the session's platform changes. The shell picks one from the shape of the session's
9// directory, once, and hands it to the functions here and in parse.ts.
10export type Flavour = {
11  // Lists every directory from `cwd` up to the root, nearest first.
12  ancestors: (cwd: string) => string[]
13
14  // Returns `name` inside `dir`, with the directory's own separator.
15  childOf: (dir: string, name: string) => string
16
17  // Returns the path under `root`, or null outside it.
18  under: (root: string, path: string) => string | null
19
20  isAbsolute: (path: string) => boolean
21
22  // Lists the commands to try for khub when the setting names none.
23  installs: (root: string) => string[][]
24
25  // Returns the file a candidate needs on disk before it is worth running, or null when it
26  // is looked up on the path.
27  installedAt: (bin: readonly string[]) => string | null
28
29  // The binary's file name, and a path separator, as regular expression sources.
30  name: string
31  separator: string
32}
33
34export type Ws = KhubWorkspace & { types: KhubType[]; flavour: Flavour }
35
36export const dirname = (path: string) => path.replace(/\/[^/]*$/, '') || '/'
37
38// Tells whether the session's directory is a Windows path: a drive, such as `C:\`, or a
39// network share. The engine names no platform, and a unix directory always starts with `/`.
40export const isWindowsPath = (cwd: string) => /^[A-Za-z]:[\\/]/.test(cwd) || /^\\\\/.test(cwd)
41
42// The separator a Windows path is spelled with: a slash only when it holds no backslash.
43const separatorOf = (path: string) => (path.includes('/') && !path.includes('\\') ? '/' : '\\')
44
45const windowsChildOf = (dir: string, name: string) => `${dir.replace(/[\\/]+$/, '')}${separatorOf(dir)}${name}`
46
47// Lists the directories from a Windows path up to its drive or share, nearest first, with
48// the path's own separator. `C:\a\b` gives `C:\a\b`, `C:\a` and `C:\`.
49function windowsAncestors(cwd: string): string[] {
50  const sep = separatorOf(cwd)
51  const top = /^(?:[A-Za-z]:|[\\/]{2}[^\\/]+[\\/][^\\/]+)/.exec(cwd)?.[0] ?? ''
52  const parts = cwd
53    .slice(top.length)
54    .split(/[\\/]/)
55    .filter(part => part !== '')
56  const dirs: string[] = []
57
58  for (let n = parts.length; n >= 0; n -= 1) dirs.push(`${top}${sep}${parts.slice(0, n).join(sep)}`)
59
60  return dirs
61}
62
63function unixAncestors(cwd: string): string[] {
64  const dirs = [cwd]
65
66  while (dirs[dirs.length - 1] !== '/') dirs.push(dirname(dirs[dirs.length - 1] as string))
67
68  return dirs
69}
70
71// cmd may take these characters in a command line as its own.
72const CMD_SPECIAL = /[%!^&|<>"()]/
73
74// Windows runs neither the shell script nor the `.cmd` npm lays in `node_modules/.bin`
75// without a shell, so there the repo's install is the npm package's launcher run by node,
76// which is what `khub.cmd` runs. The last candidate reaches npm's global `khub.cmd` through
77// cmd. The shell passes khub fixed words and the workspace root, so that candidate is left
78// out for a root cmd would read.
79function windowsInstalls(root: string): string[][] {
80  const launcher = ['node_modules', '@endgame-build', 'khub', 'bin', 'khub.js'].reduce(windowsChildOf, root)
81
82  return [['node', launcher], ['khub'], ...(CMD_SPECIAL.test(root) ? [] : [['cmd', '/c', 'khub']])]
83}
84
85// Spells a Windows path for comparison: forward slashes, and a Git Bash drive such as
86// `/c/Users` as `c:/Users`.
87const windowsForm = (path: string) => path.replace(/\\/g, '/').replace(/^\/(?:cygdrive\/)?([A-Za-z])(?=\/|$)/, '$1:')
88
89// Returns the path under a Windows root, with forward slashes. The root matches whatever
90// the separators and the case, since the file system ignores case.
91function windowsUnder(root: string, path: string): string | null {
92  const base = `${windowsForm(root).replace(/\/+$/, '')}/`
93  const rel = windowsForm(path)
94
95  return rel.length > base.length && rel.slice(0, base.length).toLowerCase() === base.toLowerCase()
96    ? rel.slice(base.length)
97    : null
98}
99
100// macOS spells /tmp and /var two ways.
101function unixUnder(root: string, path: string): string | null {
102  const roots = [root, root.replace(/^\/private/, ''), `/private${root}`]
103  const found = roots.find(candidate => path.startsWith(`${candidate}/`))
104
105  return found === undefined ? null : path.slice(found.length + 1)
106}
107
108// A unix session takes paths as spelled, and the binary is `khub` alone.
109export const UNIX: Flavour = {
110  ancestors: unixAncestors,
111  childOf: (dir, name) => `${dir}/${name}`,
112  under: unixUnder,
113  isAbsolute: path => path.startsWith('/'),
114  installs: root => [[`${root}/node_modules/.bin/khub`], ['khub']],
115  installedAt: bin => ((bin[0] ?? '').startsWith('/') ? (bin[0] as string) : null),
116  name: 'khub',
117  separator: '/',
118}
119
120// Git Bash runs a Windows session's commands, so a path may take either separator and a
121// Git Bash drive, and the binary may carry its extension and any case. A verb is always
122// lowercase, so the name spells its case out rather than ignoring it.
123export const WINDOWS: Flavour = {
124  ancestors: windowsAncestors,
125  childOf: windowsChildOf,
126  under: windowsUnder,
127  isAbsolute: path => /^(?:[A-Za-z]:)?[\\/]/.test(path),
128  installs: windowsInstalls,
129  installedAt: bin => (bin.length === 2 && bin[0] === 'node' && isWindowsPath(bin[1] ?? '') ? (bin[1] as string) : null),
130  name: '[Kk][Hh][Uu][Bb](?:\\.[Ee][Xx][Ee]|\\.[Cc][Mm][Dd])?',
131  separator: '[\\\\/]',
132}
133
134export const flavourOf = (cwd: string): Flavour => (isWindowsPath(cwd) ? WINDOWS : UNIX)
135
136// Splits the `binary` setting into words at whitespace. A double or single quote that opens
137// a word and has its closing quote keeps a path with spaces in one word. Any other quote is
138// a plain character, as is a backslash, so `C:\Users\O'Brien\khub.exe` stays one word. An
139// empty word (`''`) is dropped, so a setting of only empty words is an empty setting.
140export function settingWords(setting: string): string[] {
141  const words = [...setting.matchAll(/(["'])([\s\S]*?)\1(\S*)|\S+/g)].map(found =>
142    found[1] === undefined ? found[0] : `${found[2]}${found[3]}`,
143  )
144
145  return words.filter(word => word !== '')
146}
147
148// Lists the commands to try for khub, in order: the setting, the repo's pinned install, the path.
149export function binCandidates(root: string, setting: string, flavour: Flavour): string[][] {
150  const words = settingWords(setting)
151
152  return words.length > 0 ? [words] : flavour.installs(root)
153}
154
155// Returns the path under the workspace root, or null outside it.
156export const relative = (ws: Ws, path: string): string | null => ws.flavour.under(ws.root, path)
157
158// Returns the entity a file holds, by its type's layout. A collection file holds many, so
159// its slug is `*`.
160export function entityAt(ws: Ws, path: string): Entity | null {
161  const rel = relative(ws, path)
162
163  if (rel === null) return null
164
165  for (const type of ws.types) {
166    if (type.layout === 'singleton' || type.layout === 'collection') {
167      if (rel === type.path) return { type: type.name, slug: type.layout === 'singleton' ? type.name : '*' }
168      continue
169    }
170
171    const ext = `.${type.format}`
172
173    if (!rel.startsWith(`${type.path}/`) || !rel.endsWith(ext)) continue
174
175    const parts = rel.slice(type.path.length + 1, -ext.length).split('/')
176
177    if (type.layout === 'file' && parts.length === 1) return { type: type.name, slug: parts[0] as string }
178    if (type.layout === 'folder' && parts.length === 2 && parts[1] === '_index') {
179      return { type: type.name, slug: parts[0] as string }
180    }
181  }
182
183  return null
184}
185
186// The oldest khub whose output the mod reads. `search --plain` and `schema diff`, which the
187// row summaries read, arrived with it.
188export const MIN_KHUB = '0.27.0'
189
190// Tells whether what `khub --version` printed names a release before `min`. Text with no
191// version in it is a development build, which is taken as new enough. A pre-release of
192// `min` is older.
193export function isOlder(printed: string, min: string): boolean {
194  const found = /(\d+)\.(\d+)\.(\d+)(-)?/.exec(printed)
195  const need = min.split('.').map(Number)
196
197  if (!found) return false
198
199  const have = found.slice(1, 4).map(Number)
200
201  for (let i = 0; i < 3; i += 1) {
202    if (have[i] !== need[i]) return (have[i] as number) < (need[i] as number)
203  }
204
205  return found[4] !== undefined
206}
207
208// Tells whether a `-C` value names a workspace other than this one. khub resolves the
209// nearest `.khub` at or above the path, so a directory inside the root is this workspace,
210// and a relative value is taken as this one.
211export function isElsewhere(ws: Ws, workspace: string | null): boolean {
212  if (workspace === null || !ws.flavour.isAbsolute(workspace)) return false
213
214  return relative(ws, `${workspace.replace(new RegExp(`${ws.flavour.separator}$`), '')}/.`) === null
215}
216
hooks/el.ts 15 lines
1import type { Elements } from 'claude-code'
2
3import type { KhubTone } from '../types'
4
5// Holds the elements every surface draws. A view takes this table from the shell's
6// `$.ui.resolve(e)`.
7export type El = Pick<Elements['terminal'], 'Box' | 'Text' | 'Button'>
8
9// Returns a tone's text color, as props to spread. A plain tone keeps the surface's own color.
10export function toneColor(tone: KhubTone): { color?: string } {
11  if (tone === 'plain') return {}
12
13  return { color: tone === 'ok' ? 'green' : tone === 'warn' ? 'yellow' : 'red' }
14}
15
types/index.d.ts 66 lines
1// The khub mod's session state. The file is self-contained, because the engine refuses a contract that imports.
2
3// How a type is stored, as `khub schema --format json` lists it.
4export type KhubType = {
5  name: string
6  layout: string
7  format: string
8  path: string
9}
10
11export type KhubWorkspace = {
12  root: string
13  bin: string[]
14  version: string
15  preset: string
16  presetVersion: string
17}
18
19// What the last refresh read from `khub query` and `khub check`.
20export type KhubSummary = {
21  total: number
22  draft: number
23  passed: boolean
24  errors: number
25}
26
27// Qualified ids, `type/slug`. `baseline` is the first read of the session, null until then.
28export type KhubSession = {
29  baseline: string[] | null
30  current: string[]
31  edited: string[]
32}
33
34export type KhubTone = 'ok' | 'warn' | 'bad' | 'plain'
35
36// One list row under a call's result line.
37export type KhubRow = {
38  text: string
39  note: string
40  flags: string[]
41}
42
43// One khub call the agent ran through Bash. A command's calls are keyed by tool_use_id,
44// and the first one carries the command's duration.
45export type KhubCall = {
46  head: string
47  line: string
48  tone: KhubTone
49  isRunning: boolean
50  ms: number
51  rows: KhubRow[]
52  more: number
53}
54
55declare module 'claude-code' {
56  interface PluginState {
57    khub: {
58      workspace: KhubWorkspace | null
59      summary: KhubSummary | null
60      session: KhubSession
61      calls: StateFamily<KhubCall[] | null>
62      rawRows: StateFamily<boolean>
63    }
64  }
65}
66