sokf 0.5.1

sokf validates knowledge stores in the Superdev Open Knowledge Format (SOKF): directories of Markdown documents with YAML frontmatter, which sokf checks against their schemas.
# sokf

[![Publish](https://github.com/six5536/sokf/actions/workflows/publish.yml/badge.svg)](https://github.com/six5536/sokf/actions/workflows/publish.yml)
[![crates.io](https://img.shields.io/crates/v/sokf.svg)](https://crates.io/crates/sokf)
[![npm](https://img.shields.io/npm/v/@six5536/sokf.svg)](https://www.npmjs.com/package/@six5536/sokf)
[![docs.rs](https://img.shields.io/docsrs/sokf-core)](https://docs.rs/sokf-core)
[![License: Elastic-2.0](https://img.shields.io/badge/license-Elastic--2.0-blue.svg)](LICENSE)

sokf validates knowledge stores in the Superdev Open Knowledge Format (SOKF):
directories of Markdown documents with YAML frontmatter, kept in a Git
repository and maintained largely by AI agents. It checks every document
against its type's schema, resolves every link, keeps generated content
current, allocates document ids, prints the store as a graph from any point,
and locates a document by id.

- [Install]#install
- [Usage]#usage
- [Configuration]#configuration
- [Agent harness]#agent-harness
- [Schema packs]#schema-packs
- [Output and exit codes]#output-and-exit-codes
- [Specifications]#specifications
- [Project layout]#project-layout
- [Development]#development
- [Licence]#licence

## Install

Install the launcher, which brings the prebuilt binary for your machine, or
build sokf from crates.io:

```sh
npm install -g @six5536/sokf   # prebuilt binary, Linux/macOS/Windows
cargo install sokf             # from source, needs a Rust toolchain
```

Prebuilt binaries cover Linux and macOS on `x64` and `arm64`, and Windows on
`x64`. The Linux builds are statically linked against musl, so they need no
particular glibc version and run on Alpine too.

## Usage

A store is a directory with a manifest, and `sokf init` creates one.

```sh
sokf init                           # a store at /knowledge, registered as a root
sokf init docs/kb --name project-kb # a store elsewhere, added to /.sokf/config.toml
sokf init docs                      # a store of a directory that holds documents: the manifest only, and --fix writes the indexes
sokf harness install claude         # the sokf skill, instructions, and permission for Claude Code
sokf harness install codex cursor   # the parts that OpenAI Codex and Cursor support; `all` names every harness
sokf harness status all             # each part of each harness: current, shared, stale, edited, absent
sokf harness diff claude            # a patch for each part that you edited
sokf harness install claude --force skills  # overwrite an edited part, or every part with `all`
sokf pack add sokf                  # declare the built-in sokf pack and copy it into /knowledge
sokf pack add https://example.com/method.git    # declare and copy a schema pack from a git repository
sokf pack sync                      # merge each later change of every declared pack with your edits
sokf pack status                    # each pack, and each of its files that you edited or that conflicts
sokf pack status --check            # the same, and exit with code 1 when a file differs from its pack
sokf pack accept /.gitignore --note "the project's binary"   # keep an edit of a pack's file on purpose
sokf validate                       # check every knowledge store and additional file
sokf validate --fix                 # regenerate blocks and indexes, move lifecycle documents, and rewrite headings
sokf validate knowledge/core --json # report on a directory, as one JSON object
sokf validate --json --query "errors[].path"   # a JMESPath projection of the report
sokf validate --warnings --no-cache # list the warnings, and skip the cache
sokf validate --changed knowledge/core/planner.md   # read the metadata of only the paths named and the paths under them; the rest come from the cache

sokf graph                          # the layout of every knowledge root: its directories and documents
sokf graph planner                  # one document and its edges; --depth 2 for two hops out
sokf graph src/planner.rs           # the documents that describe or include a file
sokf graph '#cli-1_ac-2'            # where a key is defined, and who cites it
sokf graph --all --archived         # every entry past the limit of 20, and the archived documents
sokf graph -l "type=='Issue'" --format '{id}  {state}  {title}'   # the rows a predicate selects
sokf graph --json --query "nodes[?kind=='document'].id"           # the JSON form, projected
sokf graph planner --mermaid        # a Mermaid flowchart
sokf locate planner                 # the absolute path of a document: cat "$(sokf locate planner)"
sokf locate --asset flowers         # the file a sidecar describes; --json gives both paths

sokf info grammar                   # the schema language, verbatim: the tramlines core grammar, the tramlines-md grammar, then SOKF's schema extension
sokf info formats --schema readme   # the line formats a schema sees
sokf info formats --layer pack      # only the formats of the packs in /.sokf/packs.lock
sokf new Issue --slug retry --title "Add a retry" --write
                                    # a document from its schema's template, at its derived path
sokf new Issue --slug retry --title "Add a retry" --seq 42 --write
                                    # keep the sequence number 42 that the work already has
sokf new Issue --slug retry --title "Add a retry" --write --json
                                    # write the document, and print its id, path, and text as JSON

sokf completions zsh                # a completion script for your shell
sokf --help
sokf --version
```

`--repo-root <DIR>` sets the repository root. Without it, sokf uses the Git
repository that contains the current directory.

The cache, under `/.sokf/cache/`, holds the results of the last run. With the
cache, a run rechecks only the changed files and the files that depend on
them. With `--no-cache`, sokf reads every file from disk and leaves the cache
unchanged. Both `--repo-root` and `--no-cache` are global options.

## Configuration

Three files under `/.sokf/` configure the tool, and `sokf pack` keeps two
more:

- `/.sokf/config.toml` lists the knowledge roots (default `/knowledge`), the
  paths that sokf skips in its walk of the repository, and the declined
  harness parts. sokf skips a path only outside the knowledge roots, so
  `sokf validate` warns at each glob of `repository.exclude` that lies under a
  root.
- `/.sokf/formats.yaml` adds line formats for the whole repository.
- `/.sokf/harness.toml` records the harness parts that the tool wrote.
- `/.sokf/packs.lock` records each schema pack that `sokf pack sync` copied,
  and `/.sokf/packs/` holds each pack's formats and the base copies of its
  files.

Every path that the tool prints is relative to the repository root and starts
with `/`, as paths in documents do. Only `locate` prints filesystem paths.
sokf accepts a path argument in either form.

## Agent harness

`sokf harness install claude` makes a repository ready for Claude Code. It
writes the `sokf` skill, an instructions block, and the permission to run
`sokf`. A hook is off by default: with `with = ["hooks"]` under
`[harness.claude]` in `/.sokf/config.toml`, the command also writes a hook that
runs `sokf validate` when the user submits a prompt, and tells the agent how
many errors remain, without blocking it. At the
project and the local scopes, the instructions block also carries the
`instructions.md` of each pack in `/.sokf/packs.lock`, in the order of the
packs. `sokf pack sync` and `sokf pack remove` rewrite each installed block
that you haven't edited, so that it holds the instructions of each pack, and
no command rewrites a block while the copy of a pack's instructions holds a
conflict. To change a pack's rule for your project, edit the copy
`/.sokf/packs/<NAME>/instructions.md`, not the block, and run
`sokf harness install`: a sync merges each upstream change into the copy,
`sokf pack diff` returns your edit to the pack, and `sokf pack remove` keeps
an edited copy. The command
touches only the parts that it writes. It keeps a part that you edited unless
you pass `--force`. To refresh the stale parts after an upgrade, run the
command again.

sokf installs the same integration into every agent harness of
[agent-harness-adapter](https://github.com/six5536/agent-harness-adapter). Name one or
more, or `all`. A harness that has no place for a part, shown as none in the
following table, reports the part as unsupported:

| Harness | Name | Instructions | Hook | Permission | Skill |
| --- | --- | --- | --- | --- | --- |
| Claude Code | `claude` | `CLAUDE.md` or `AGENTS.md` | `.claude/settings.json` | `.claude/settings.json` | `.claude/skills` |
| OpenAI Codex | `codex` | `AGENTS.md` | `.codex/hooks.json` | none | `.agents/skills` |
| Factory Droid | `factory` | `AGENTS.md` | `.factory/hooks.json` | none | `.agents/skills` |
| Gemini CLI | `gemini` | `GEMINI.md` | `.gemini/settings.json` | `.gemini/settings.json` | `.agents/skills` |
| GitHub Copilot | `copilot` | `AGENTS.md` | `.github/hooks/sokf.json` | none | `.agents/skills` |
| Cursor | `cursor` | `AGENTS.md` | `.cursor/hooks.json` | `.cursor/cli.json` | `.agents/skills` |
| Pi | `pi` | `AGENTS.md` | `.pi/extensions/sokf.ts` | none | `.agents/skills` |
| OpenCode | `opencode` | `AGENTS.md` | `.opencode/plugins/sokf.ts` | `opencode.json` | `.agents/skills` |
| Kilo Code | `kilo` | `AGENTS.md` | `.kilo/plugins/sokf.ts` | `kilo.json` | `.agents/skills` |
| Qwen Code | `qwen` | `AGENTS.md` | `.qwen/settings.json` | `.qwen/settings.json` | `.agents/skills` |
| Devin | `devin` | `AGENTS.md` | `.devin/hooks.v1.json` | `.devin/config.json` | `.agents/skills` |
| Any `AGENTS.md` agent | `agents` | `AGENTS.md` | none | none | `.agents/skills` |

A file that several harnesses read, such as `AGENTS.md`, is written once, and
the report shows it as `shared` for the others. `--scope user` installs into
your home directory, for every repository, and `--scope local` into the
repository's files that stay out of git (Claude Code and GitHub Copilot). Keep
`/.sokf/harness.local.toml` and `/.sokf/config.local.toml` out of git too.

## Schema packs

A schema pack is a directory, with the manifest `pack.sokf.yaml`, that holds a
method for a store: schemas, documents such as documentation standards, and
line formats. `sokf pack add <SOURCE>` fetches the pack with your own `git`,
declares it in a `[[pack]]` entry of `/.sokf/config.toml`, copies it into the
store, and records it in `/.sokf/packs.lock`. The store
owns the copy, so you edit it as any other document, and a later
`sokf pack sync` merges each upstream change into your edit with
`git merge-file`. A merge that conflicts leaves git's markers in the file, and
the command exits with code 1. Run `sokf validate --fix` after a sync.

A pack can also hold `files/`, the files that projects share outside a store,
such as `rustfmt.toml`, a lint configuration, or a workflow. Sync places each
at the same path from the repository root, `files/rustfmt.toml` at
`/rustfmt.toml`, and keeps it current as it keeps a document, with its
executable bit. A pack that holds only `files/` needs no store. Sync refuses a
file that would land in `/.git/` or `/.sokf/`, inside a store, or on a file
that a harness writes.

`builtin:<NAME>`, or `NAME` alone, names a pack that the binary embeds, and
`sokf pack list` shows them. `sokf pack diff <NAME>` prints your edits as a
patch that `git apply` applies in the pack's repository, without the edits
that you accepted unless you pass `--accepted`. `--new <PATH>...` adds a
file that the pack doesn't hold yet, such as a new schema or script, to the
patch at its path in the pack, and the next `sokf pack sync` records your
copy as the pack's file, because it holds the pack's version. `sokf pack
remove <NAME>` deletes the pack's files that you haven't edited, and `sokf
pack export <NAME> <DIR>` writes an embedded pack to a new directory.
To move a pack to another source or ref, edit its `[[pack]]` entry and run
`sokf pack sync`, and delete an entry to remove its pack at the next sync.

An edit of a pack's file either goes back to the pack or stays on purpose.
`sokf pack accept <PATH> --note <TEXT>` records an edit or a deletion that the
project keeps, such as its own line in a shared `.gitignore`, and
`sokf pack status --check` then passes on the file while it holds what you
accepted. A clean merge of a later change of the pack keeps the acceptance, and
a conflict drops it. `--withdraw` undoes an acceptance.

A pack can build on another: its manifest names the packs that it depends on,
each with its oldest version, as in `depends: { house-base: 1.4.0 }`. A pack
comes after the packs that it depends on, the order of the `[[pack]]` entries
orders the rest, and the highest pack that supplies a file, a schema type, or
a format owns it. Sync writes only the owner's version, so a group pack can
replace a file of the base pack, such as a stricter Issue schema. When the
owner of a file changes, sync merges the new owner's version with your edit, and
`sokf pack status` lists each shadowed file with the pack that owns it.

## Output and exit codes

`sokf validate` prints one line for each finding,
`PATH:LINE: error: MESSAGE`, followed by the counts. `PATH` is the file,
`LINE` is the line in it, and `MESSAGE` describes the finding. Each message
ends with the authority for the finding: the key of the broken criterion, or
the schema and its broken rule:

```
moved   /knowledge/issues/open/issue-003-retry.md -> /knowledge/issues/closed/issue-003-retry.md
rewrote /knowledge/architecture.md
/knowledge/architecture.md:41: error: `[sokf:planer]` resolves to no document (SOKF_FORMAT_61)
/README.md: error: section `## Usage` is missing (schema schema-readme, section "Usage")

2 errors, 0 of 1 warnings listed
```

The last line gives the number of warnings listed out of the total.
Without `--warnings`, only the warnings at a stale or an absent part of an
installed harness are listed; `--warnings` lists them all.

Without a start argument, `sokf graph` prints the layout of each knowledge
root in the way that `tree` does, with each store's description and each
document's id:

```
/knowledge - The example knowledge of the graph design.
├── config
├── core/
│   ├── executor
│   └── planner
└── schemas/
    └── subsystem

4 / 4 docs
```

For a document, `sokf graph` prints its edges: `links`, `resource`,
`sources`, and includes, and it adds the inverse of every edge. `(*)` marks a
node whose edges are printed elsewhere, so the output stays finite at any
depth.

For a group of more than 20 entries, the command prints the first 20 and a
line that counts the rest. It folds the includes of one file into one line.
It leaves out the documents that a schema archives, such as closed issues.
`--all` and `--archived` show these entries.

The last line gives the number of documents printed out of those in the whole
view. When the view reaches an archived document, the line also gives the
number of archived documents printed out of their total. The following view
reaches none:

```
planner
├── includes → /crates/app/sokf/src/main.rs#cli
├── depends-on → config
├── depended-on-by → executor
├── references → planer
└── describes → /crates/lib/example-core/src/planner.rs

3 / 3 docs
```

sokf exits with `0` when it finds no error, with `1` when it finds errors,
and with `2` on a usage or internal error. It reports a usage or internal
error on stderr as `error: MESSAGE`, where `MESSAGE` describes the error.
When a downstream pipe closes (`sokf info grammar | head`), sokf stops
without an error.

## Specifications

Three core specifications define the format:
[`sokf-spec/sokf-format-spec.md`](sokf-spec/sokf-format-spec.md),
[`sokf-spec/sokf-schema-spec.md`](sokf-spec/sokf-schema-spec.md), and
[`sokf-spec/sokf-validation-spec.md`](sokf-spec/sokf-validation-spec.md).
They are documents of `sokf-spec`, the knowledge store of SOKF itself. The
sokf pack, in [`packs/sokf/`](packs/sokf/), shows SOKF in use. It is an optional
method for a project's knowledge. It holds schemas for the architecture, the
stories, the contracts, the designs, the issues, and other documents, the
formats that they share, documentation standards, and recipes for rewriting a
document and for planning a building block. It also holds instructions for the
agent, which the harness's instructions block carries. sokf's own development
records its knowledge with the pack. The binary embeds it, so a repository
adds it with `sokf pack add sokf`.

The schema language comes from [tramlines](https://github.com/six5536/tramlines),
a Markdown schema engine with its own
[specifications and grammars](https://github.com/six5536/tramlines/tree/main/knowledge/spec).
SOKF's schema extension,
[`sokf-spec/sokf.tramlines.yaml`](sokf-spec/sokf.tramlines.yaml), extends the
tramlines-md grammar. The guide to the extension,
[`sokf-spec/sokf.tramlines.yaml.md`](sokf-spec/sokf.tramlines.yaml.md), sits
beside the extension.

The agent reference, which describes the format on one page for an AI agent,
is the [`sokf` skill](harness/skills/sokf/SKILL.md). `sokf harness install`
writes the skill into a repository.

## Project layout


```
sokf-spec/               the knowledge store of SOKF itself: its specifications, its grammar, and its formats, which sokf validates
harness/                 the embedded harness integration: the sokf skill and the instructions block
packs/sokf/              the sokf pack, the method of a project's knowledge that the binary embeds
crates/lib/sokf-core     the library: it walks the repository, builds the model, hosts SOKF's schema extension in the tramlines engine, checks and fixes the knowledge, reports the findings, builds the graph, runs queries, and installs the harness
crates/app/sokf          the binary: it parses the command line, calls the library, and renders the results as text, JSON, or Mermaid
packages/sokf            the npm launcher (a JS shim that spawns the binary)
packages/sokf-OS-CPU     the five prebuilt-binary packages it selects from
scripts/                 the version and smoke-test scripts that the release runs
.github/workflows/       publish.yml, which builds and publishes each release
```


In `packages/sokf-OS-CPU`, `OS` is `linux`, `darwin`, or `win32`, and `CPU` is
`x64` or `arm64`.

## Development

`.mise.toml` and `rust-toolchain.toml` pin the toolchains, which
[mise](https://mise.jdx.dev/) installs.


```sh
npm run build           # cargo build --workspace
npm run lint            # cargo clippy --workspace
npm run fmt             # cargo fmt --all
npm run verify-version  # check that every version in the tree agrees
cargo run -p sokf -- validate   # run the validator from source
```


For the setup, and for how to report a bug or ask for a feature, see the
[contributing guide](CONTRIBUTING.md).

## Licence

sokf is released under the Elastic License 2.0. The [licence file](LICENSE) states its terms.