Skip to content

Scripted commands and a skill so AI agents can label datasets - #1

Merged
shellbear merged 3 commits into
mainfrom
feat/agent-cli
Nov 28, 2026
Merged

shellbear merged 3 commits into
mainfrom
feat/agent-cli

Conversation

@shellbear

Copy link
Copy Markdown
Owner

What this does

An AI agent (or a shell script) can now label a dataset with labelkit end to end, without the editor and without hand-editing annotations.json. Setup for the person using it is one command: labelkit skill --install.

It also makes large datasets load, list and save several times faster, because profiling the new commands showed their time going to shared format code.

New commands

Command Purpose
labelkit annotate <dataset> … Add or replace boxes, from --box flags, JSON, NDJSON or stdin
labelkit stats <dataset> JSON summary: labelled, negative, unlabelled and missing images, boxes per label
labelkit validate <dataset> Errors and warnings with stable codes; exits 1 on errors
labelkit render <dataset> --out DIR Draw the saved boxes on pictures to check them; --grid N adds a pixel ruler
labelkit skill [--install] Print or install the guide that teaches an agent the loop
labelkit <dataset> --select <image> Open the editor on one image; reloads if it is already open

All of them write the result to stdout as JSON, notes to stderr, and exit 0 (ok), 1 (problem found) or 64 (usage error). None touches AppKit.

Design decisions worth reviewing

  • annotate accepts what models actually emit. Gemini box_2d, corner lists, 0–1 and 0–1000 units, centre anchors, parallel boxes/labels lists, COCO files and labelkit detect output are read as they are. For anything else you name the convention (--units, --origin, --anchor, --order, --image-size) instead of converting numbers. Nothing is inferred from the values.
  • Adding is idempotent down to the bytes. A box that already exists (same label, overlapping) is skipped, so a retried run changes nothing. There is a unit test and a CI check for this.
  • Writes are all-or-nothing. Any unusable input (unknown image, box outside its image) means nothing is written and exit 1; --lenient applies the valid part.
  • The format contract is unchanged. Scripted edits go through CreateMLWriter, so entry order, unknown keys, negatives and the file's own indentation are preserved.
  • The agent guide is compiled into the binary (AgentGuide.skill) so labelkit skill always matches the installed version. skills/labelkit/SKILL.md is the same text for repository-based installers; a test fails if they differ.
  • No MCP server. Every client surveyed can run shell commands, so a CLI with JSON output reaches all of them with nothing to run or configure.
  • AGENTS.md is now the canonical project instructions; CLAUDE.md imports it. This is a change to how the repo instructs agents, so please check you are happy with it.

Two real-world inputs shaped this: the iris card-detector labelling work (agents writing whole-image box lists, checking them against a labelled pixel grid, mixed coordinate conventions, byte-identical duplicate images with conflicting labels) and the current documentation of nine agent clients and the common box formats.

Speed

Measured on a 10,000-image dataset with 26,000 boxes (5 MB annotations.json), Apple silicon, release build:

Command First version on this branch Now Peak memory now
stats 0.94 s 0.2 s 44 MB
annotate (one image) 0.58 s 0.1 s 35 MB
validate 2.11 s 0.5 s 70 MB

The gains come from the first commit and also apply to the editor's open and save: the loader reads Foundation's containers directly instead of bridging every dictionary, filenames and labels become native Swift strings once, the writer formats coordinates as integer hundredths, indent detection reads 64 KB instead of the whole file, and the directory sort stays inside Foundation.

Saved bytes are unchanged. A test compares the new coordinate formatting with the previous implementation over 420,000 values, and the existing golden and round-trip tests pass untouched.

How it was checked

  • swift test: 166 tests, 0 failures (63 of them new).
  • scripts/smoke-scripted.sh (new, runs in CI): drives every scripted command on a throwaway dataset and checks exit codes, refusal of bad input, and byte-identical repeats.
  • Every recipe in docs/agents.md was run against the release binary, with the saved coordinates checked by hand.
  • claude plugin validate . passes for the plugin manifest.

Not checked:

  • --select in the running editor. It builds and is wired through all three launch paths, but I did not see it work in a window.
  • Installing through the plugin marketplace or npx skills add. Both need this branch on main; only the manifest was validated.
  • macOS 14 and 15. Built and run on macOS 27 only; CI covers 15 and 26.

Commits

  1. perf: the loader, scanner and writer speed-ups, with their tests
  2. feat: the commands, the library code behind them, tests, the skill file, the smoke test
  3. docs: docs/agents.md, README, CHANGELOG, AGENTS.md, the plugin manifest

Left for later

  • YOLO import and YOLO/COCO export
  • A draft/reviewed marker per image, so agent labels can be told apart from human-confirmed ones
  • The editor noticing when annotations.json changes underneath it (today you re-run labelkit <dataset> to reload)

🤖 Generated with Claude Code

Profiling the headless commands on a 10,000-image / 26,000-box dataset showed
the time going to shared format code, most of it Foundation bridging:

- CreateMLFormat.load reads JSONSerialization's NSArray/NSDictionary directly
  instead of casting each entry to [String: Any], which copied the dictionary
  and bridged every key
- filenames and labels are converted to native Swift strings once, on the way
  in, so later Set/Dictionary work no longer hashes through NSString
- CreateMLWriter caches its indentation strings, formats coordinates as
  integer hundredths instead of String(format:), and skips escaping for
  strings that need none
- JSONIndent sniffs the first 64 KB rather than splitting the whole file
- ImageDirectoryScanner lists with contentsOfDirectory and sorts by selector,
  keeping comparisons inside Foundation; names that compare equal are settled
  by plain comparison so the order is deterministic

Saved bytes are unchanged: a test compares the new coordinate formatting with
the previous implementation over 420,000 values, and the existing golden and
round-trip tests still pass. Adds concurrentMap and uniqued helpers used by
the commands that follow.
Until now nothing could write boxes into annotations.json from the terminal,
so an agent had to hand-edit the file and lose the format guarantees. This
adds the headless verbs for a full label-check-fix loop:

- annotate: add or replace boxes from --box flags, JSON, NDJSON or stdin.
  Reads what models and tools emit (Gemini box_2d, corner lists, 0-1 and
  0-1000 units, centre anchors, COCO files, detect output); --units, --origin,
  --anchor, --order and --image-size name a convention instead of converting.
  Adding is idempotent down to the bytes; unusable input writes nothing.
- stats: JSON summary (labelled, negative, unlabelled, missing, per-label
  counts, the image with the most boxes)
- validate: errors and warnings with stable codes, exit 1 on errors; also
  finds the same photo saved under two names
- render: draw saved boxes on downscaled JPEGs, numbered to match validate;
  --grid adds a ruler in original-image pixels, --unlabeled renders the to-do
- skill: print or install the Agent Skill that teaches a client the loop
  (~/.agents/skills and ~/.claude/skills, or --project / --dir / --snippet).
  The text is compiled in and mirrored at skills/labelkit/SKILL.md; a test
  keeps the two identical.
- open --select <image>: show a given image; with the editor already open it
  reloads the dataset from disk and jumps there

The logic is in Sources/LabelKit/Headless with tests; the command files only
parse and print. detect and train now share the same output helpers. All
commands write the result to stdout, notes to stderr, and exit 0 / 1 / 64.
scripts/smoke-scripted.sh exercises them end to end and runs in CI.
…ions

- docs/agents.md: one-minute setup, per-client skill locations (Claude Code,
  Codex, opencode, Cursor, Gemini CLI, Antigravity, Copilot, Amp, Goose,
  Aider), the label-check-fix loop, how to review, input formats per model
  and tool, the JSON and exit-code contract, measured timings
- README: a section on using labelkit from an agent and the scripted commands
- CHANGELOG: the new commands and the faster load/save under Unreleased
- AGENTS.md is now the canonical instructions file, read natively by most
  agent clients; CLAUDE.md imports it so Claude Code reads the same text. It
  gains a section on the scripted surface as a public interface.
- .claude-plugin/marketplace.json lets Claude Code install the skill as a
  plugin (passes 'claude plugin validate')
@shellbear
shellbear merged commit e7a14d6 into main Nov 28, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant