# OpenKrill Golden Skill and Tool Blueprint

One tool, four surfaces, one engine.
Copy this blueprint for a new tool and fill each section before you call it done.
The App Store Opportunity Tracker is the worked example in brackets.

## 1. Package and CLI

The engine lives in one package with zero runtime dependencies.
Every network call goes through an injected `fetch` and clock, so the same code runs in Node, a Worker and tests.

- [ ] `src/` holds pure parsers per source and one function per user question. [`getCharts`, `lookupApp`, `findOpportunities`, `exploreKeyword`]
- [ ] Errors are one class with `code`, `message`, `help[]` and `retryable`. [`TrackerError`]
- [ ] Codes: `invalid_input`, `not_found`, `rate_limited`, `upstream_error`, `parse_error`.
- [ ] Retries with backoff on network errors, 429 and 5xx only.
- [ ] Tests replay trimmed real answers recorded by a script, with personal data removed. [`npm run record-fixtures`]
- [ ] A live `health` script calls every source once and exits non-zero on a broken shape.

CLI rules for agents:

- [ ] Compact default: a header line, a `name[count]{fields}:` table with 3 or 4 fields per row, then `help[n]:` next steps.
- [ ] `--json` prints the full result; `--full` prints every row.
- [ ] Empty results say so in words: `apps[0]: no app in this chart passed the minimum score`.
- [ ] Errors: `error[code]: message` plus `help[]`, or `{"error":{...}}` with `--json`.
- [ ] Exit codes: 0 ok, 1 upstream, 2 invalid input, 3 not found.
- [ ] No prompts, ever.

## 2. Skill folder

Follows the Agent Skills specification (agentskills.io/specification) and the multi-phase layout of Cloudflare's open-source security-audit skill: a short SKILL.md that routes, reference files for each phase, a JSON Schema for the output, and a zero-dependency validator with its tests in the same folder.

```text
skills/<name>/
  SKILL.md                     modes, setup, core principles, the phase list, API reference, anti-patterns
  <PHASE-TOPIC>.md             one reference file per phase or pair of phases, one level deep
  <thing>-schema.json          JSON Schema for the structured output, a top-level array of record shapes
  <thing>.example.json         one real record of each shape, valid against the schema
  validate-<thing>.cjs         zero-dependency validator: the schema keywords it uses, plus cross-field checks
  validate-<thing>.test.cjs    node:test suite for the validator; runs with node --test
  TEMPLATE.md                  this blueprint
```

[`RECONNAISSANCE-AND-CHARTS.md`, `OPPORTUNITY-SCORING.md`, `REVIEW-AND-IAP-TEARDOWN.md`, `OUTPUT-AND-VERIFICATION.md`, `opportunity-schema.json`, `validate-opportunities.cjs`]

SKILL.md:

- [ ] Folder name equals `name`: lowercase, digits and single hyphens, 64 characters or fewer.
- [ ] `description` says what it does and when to load it, with the words a user would type, 1024 characters or fewer.
- [ ] Body under 500 lines; long material moves to the reference files.
- [ ] Two operating modes: a default answer mode for one question, and a full mode that runs every phase and writes files only when the user asks for that outcome.
- [ ] Setup: the surface table (MCP, HTTP, CLI), the output directory, and a call budget.
- [ ] Key terms defined once, in plain words.
- [ ] Core principles as short numbered rules.
- [ ] A numbered phase list; each phase names its output and links its reference file. [1 reconnaissance, 2 hunting, 3 disproof and validation, 4 structured output, 5 independent verification]
- [ ] One phase exists to reject candidates, and one fresh pass re-checks every confirmed record before the report.
- [ ] Store or user text that reaches the agent is named as untrusted data, never instructions.
- [ ] Two terminal states only: the validator passes and the report is written, or the report opens with what is missing.
- [ ] Anti-patterns as a numbered list.

Structured output:

- [ ] Three record shapes: confirmed, needs validation (with the exact missing fact and the next call), rejected (with a reason from a closed list).
- [ ] A stable fingerprint per record, so later runs match earlier ones.
- [ ] The validator refuses schema keywords it does not implement, bounds input size and nesting, exits 0 valid, 1 invalid, 2 unreadable.
- [ ] Cross-field checks for what a schema cannot say: ranges, score math, sorted unique fingerprints, no personal data in quotes.

## 3. MCP server and HTTP API

- [ ] One MCP tool per user question, named `<tool>_<verb>`, each read-only, idempotent and open-world when it only reads public data.
- [ ] Every string input has `maxLength`; enums for closed sets; integer bounds on limits.
- [ ] Each tool has an output schema and a one-line text summary next to the structured content.
- [ ] REST mirrors the tools under `/v1/<tool>/<verb>` with GET and query parameters.
- [ ] Anonymous calls are rate limited per IP; errors carry the same `code` and `help[]` as the package.
- [ ] Results that cost upstream calls are cached with a short TTL.
- [ ] Snapshots that power deltas live in a database table, seeded from a real fetch so day-one deltas work.
- [ ] A synthetic probe calls one tool with fixed input and checks a stable fact.

## 4. Web UI and agent experience

- [ ] The page shell, scope controls and agent copy blocks are static HTML that paints before scripts run; each data view fills from the API behind a skeleton of the same size, so nothing shifts when the data lands.
- [ ] Every view has five states: empty, loading skeleton, partial (with what is missing), error with retry, populated.
- [ ] Keyboard: `/` focuses search, number keys switch tabs, Escape closes drawers.
- [ ] Light and dark themes from design tokens; contrast 4.5 to 1 or more for text.
- [ ] Works at 390 px wide with no sideways page scroll.
- [ ] An agent tab with one-click copy for SKILL.md, the CLI command, the MCP config and a curl example.
- [ ] A Markdown twin linked with `<link rel="alternate" type="text/markdown">`.

## 5. Open-source readiness check

Run before the first public release.

- [ ] A release scan checks every file of the package and the skill for internal hostnames, private paths, personal handles and secret token shapes, and allows only public store hosts.
- [ ] The skill validator's own tests run before release, and its example output validates against the schema.
- [ ] LICENSE and NOTICE (credit any request shapes learned from other projects) are present.
- [ ] README explains install, commands, the method behind every estimate and its limits.
- [ ] The published manifest (`npm run stage` writes it) points at compiled JavaScript with type declarations and ships only built output and docs.
- [ ] The workspace manifest keeps `"private": true` until the first release, so nothing is published by accident.
