From 51de0907f1d7151611080af67807e22b02474242 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Mon, 16 Nov 2026 12:32:19 +0000 Subject: [PATCH] docs(skills): add ignition-quickstart entry skill and index Add the Agent Skill that walks a beginner through the CartPole docs Quickstart, bounds the DQN loop with stop(), and indexes all nine skills from issue #102. Include a measured example plus tests. Co-authored-by: Salim --- examples/skill-quickstart.test.ts | 37 ++++++ examples/skill-quickstart.ts | 87 +++++++++++++ skills/README.md | 41 +++++++ skills/ignition-quickstart/SKILL.md | 114 ++++++++++++++++++ .../ignition-quickstart.test.ts | 57 +++++++++ .../references/signatures.md | 43 +++++++ 6 files changed, 379 insertions(+) create mode 100644 examples/skill-quickstart.test.ts create mode 100644 examples/skill-quickstart.ts create mode 100644 skills/README.md create mode 100644 skills/ignition-quickstart/SKILL.md create mode 100644 skills/ignition-quickstart/ignition-quickstart.test.ts create mode 100644 skills/ignition-quickstart/references/signatures.md diff --git a/examples/skill-quickstart.test.ts b/examples/skill-quickstart.test.ts new file mode 100644 index 0000000..ff8ffaa --- /dev/null +++ b/examples/skill-quickstart.test.ts @@ -0,0 +1,37 @@ +import { describe, expect, it } from 'vitest'; +import { + QUICKSTART_BUDGET, + QUICKSTART_SEEDS, + proveStopHaltsAutomaticLoop, + runQuickstartBudget, +} from './skill-quickstart'; + +describe('skill-quickstart CartPole loop', () => { + it('declares budget and at least three seeds before any run', () => { + expect(QUICKSTART_SEEDS).toEqual([11, 29, 47]); + expect(QUICKSTART_BUDGET.transitionsPerSeed).toBe(8); + expect(QUICKSTART_BUDGET.backend).toBe('cpu'); + }); + + it.each([...QUICKSTART_SEEDS])( + 'runs the declared budget on seed %i without claiming convergence', + async (seed) => { + const snapshot = await runQuickstartBudget(seed); + console.log(`QUICKSTART_RAW ${JSON.stringify(snapshot)}`); + expect(snapshot.seed).toBe(seed); + expect(snapshot.stepCount).toBe(QUICKSTART_BUDGET.transitionsPerSeed); + expect(snapshot.rewards).toHaveLength(QUICKSTART_BUDGET.transitionsPerSeed); + expect(snapshot.rewards.every((reward) => Number.isFinite(reward))).toBe(true); + expect(snapshot.observationSize).toBe(4); + expect(snapshot.lastError).toBeNull(); + }, + ); + + it('stop() cancels further automatic ticks', async () => { + const proof = await proveStopHaltsAutomaticLoop(QUICKSTART_SEEDS[0]); + console.log(`QUICKSTART_STOP_RAW ${JSON.stringify(proof)}`); + expect(proof.afterStop).toBeGreaterThanOrEqual(1); + expect(proof.later - proof.afterStop).toBeLessThanOrEqual(1); + expect(proof.lastError).toBeNull(); + }); +}); diff --git a/examples/skill-quickstart.ts b/examples/skill-quickstart.ts new file mode 100644 index 0000000..68dc176 --- /dev/null +++ b/examples/skill-quickstart.ts @@ -0,0 +1,87 @@ +import { IgnitionEnvTFJS } from '@ignitionai/backend-tfjs'; +import { CartPoleEnv } from '@ignitionai/environments'; + +/** Declared before any training call in this module. */ +export const QUICKSTART_SEEDS = [11, 29, 47] as const; + +/** Declared budget: 8 DQN transitions per seed on a small CPU network. */ +export const QUICKSTART_BUDGET = { + transitionsPerSeed: 8, + hiddenLayers: [8] as const, + batchSize: 4, + memorySize: 64, + backend: 'cpu' as const, +}; + +export interface QuickstartSnapshot { + seed: number; + stepCount: number; + rewards: number[]; + observationSize: number; + lastError: Error | null; +} + +function createRunner(seed: number): IgnitionEnvTFJS { + const runner = new IgnitionEnvTFJS(new CartPoleEnv()); + runner.train('dqn', { + backend: QUICKSTART_BUDGET.backend, + seed, + hiddenLayers: [...QUICKSTART_BUDGET.hiddenLayers], + batchSize: QUICKSTART_BUDGET.batchSize, + memorySize: QUICKSTART_BUDGET.memorySize, + }); + return runner; +} + +function dispose(runner: IgnitionEnvTFJS): void { + runner.stop(); + runner.agent?.dispose?.(); +} + +/** + * Docs Quickstart plus a declared budget: `train('dqn')` then `stop()`, + * then that many manual `step()` calls. Does not claim CartPole solved. + */ +export async function runQuickstartBudget(seed: number): Promise { + const runner = createRunner(seed); + runner.stop(); + const rewards: number[] = []; + let observationSize = 0; + for (let i = 0; i < QUICKSTART_BUDGET.transitionsPerSeed; i++) { + const result = await runner.step(); + rewards.push(result.reward); + observationSize = result.observation.length; + } + const snapshot: QuickstartSnapshot = { + seed, + stepCount: runner.stepCount, + rewards, + observationSize, + lastError: runner.lastError, + }; + dispose(runner); + return snapshot; +} + +/** + * Start the automatic loop, wait for at least one tick, then `stop()`. + * One in-flight transition may still finish (IgnitionEnv.stop()). + */ +export async function proveStopHaltsAutomaticLoop(seed: number): Promise<{ + afterStop: number; + later: number; + lastError: Error | null; +}> { + const runner = createRunner(seed); + const deadline = Date.now() + 5000; + while (runner.stepCount < 1 && Date.now() < deadline) { + await new Promise((resolve) => setTimeout(resolve, 10)); + } + runner.stop(); + const afterStop = runner.stepCount; + await new Promise((resolve) => setTimeout(resolve, 200)); + const later = runner.stepCount; + const lastError = runner.lastError; + dispose(runner); + return { afterStop, later, lastError }; +} diff --git a/skills/README.md b/skills/README.md new file mode 100644 index 0000000..88c4479 --- /dev/null +++ b/skills/README.md @@ -0,0 +1,41 @@ +# IgnitionRL agent skills + +Nine Agent Skills (`SKILL.md`) so a code agent can use `@ignitionai/*` **0.1.0** without extra context: create an env, train, split train/infer, evaluate, export. + +Success is measured only with numbers you actually ran: CartPole training, evaluation on several seeds, ONNX export. Do not claim convergence without those measurements. + +## Skills + +| Skill | One sentence | Path | +|---|---|---| +| ignition-quickstart | Install the three packages, run the CartPole docs Quickstart, stop the loop, and pick the next skill. | `skills/ignition-quickstart/` | +| ignition-create-env | Implement a `TrainingEnv` the runner can wrap. | `skills/ignition-create-env/` | +| ignition-train | Start and bound a DQN / PPO / Q-table loop with public `train()` / `step()` / `stop()`. | `skills/ignition-train/` | +| ignition-train-infer-split | Separate learning ticks from greedy `infer()` / `inferStep()` without mixing modes. | `skills/ignition-train-infer-split/` | +| ignition-reward-shaping | Change `reward()` (and related signals) without hiding the true task. | `skills/ignition-reward-shaping/` | +| ignition-debug-training | Inspect `lastError`, step counts, and loop lifecycle when learning looks stuck. | `skills/ignition-debug-training/` | +| ignition-evaluate | Report returns across at least three declared seeds; no invented thresholds. | `skills/ignition-evaluate/` | +| ignition-export-onnx | Export a trained TF.js policy to ONNX using the published backend API. | `skills/ignition-export-onnx/` | +| ignition-save-load | Persist and restore agent weights and runner-facing state. | `skills/ignition-save-load/` | + +Start at **ignition-quickstart**. The other eight folders are specified in the same batch and may land in parallel pull requests. + +## Conventions + +- Location: `skills/ignition-/SKILL.md`. +- Frontmatter: `name` equals the folder name; `description` says what the skill does and when to use it (max 1024 characters). Body in English, max 500 lines; extra detail in `references/`. +- API: only `dev` at SHA `343a76952f9dcd6594674c3526ee03ee45b4d93e`. Cite each signature as `file:line`. +- Runnable example: `examples/*.ts` plus `examples/*.test.ts`, executed by the root Vitest config with `@ignitionai/*` aliases. Tests call the real framework (no mocks) and stay under 60 s CPU. +- Declare budget and seeds (at least three) before running. Paste raw results, including failures. Never weaken an existing test. + +## How to install a skill + +There is no Ignition CLI that registers skills. + +Copy a skill directory into the project's Agent Skills folder, keeping the folder name: + +```text +cp -R skills/ignition-quickstart .claude/skills/ignition-quickstart +``` + +The file the agent reads is `.claude/skills/ignition-quickstart/SKILL.md`. Repeat for any other `skills/ignition-*` directory you need. This matches how this repository already stores project skills under `.claude/skills/`. diff --git a/skills/ignition-quickstart/SKILL.md b/skills/ignition-quickstart/SKILL.md new file mode 100644 index 0000000..0711319 --- /dev/null +++ b/skills/ignition-quickstart/SKILL.md @@ -0,0 +1,114 @@ +--- +name: ignition-quickstart +description: Entry point for IgnitionRL. Install @ignitionai/core, @ignitionai/backend-tfjs, and @ignitionai/environments; run the CartPole docs Quickstart; call stop() to halt the loop; then choose the next skill. Use when starting from zero, indexing Ignition skills, or mapping a goal to a skill. +--- + +# Ignition Quickstart + +Use this skill to go from an empty project to a running CartPole DQN loop, then pick the next skill. Packages are `@ignitionai/*` **0.1.0** as published from `dev` SHA `343a76952f9dcd6594674c3526ee03ee45b4d93e`. + +Do not claim the pole stays up, that training converged, or that a seed is reproducible for the environment unless you measured it. `CartPoleEnv` draws the initial state with `Math.random()` and has no seed argument. + +## Install + +From the docs Quickstart (`packages/web/content/en/quickstart.mdx`): + +```bash +npm install @ignitionai/core @ignitionai/backend-tfjs @ignitionai/environments +``` + +or: + +```bash +pnpm add @ignitionai/core @ignitionai/backend-tfjs @ignitionai/environments +``` + +Need Node.js 20+ and an ES-module bundler (Vite, Next.js, Webpack 5, …) for browser work. Tests in this repo run under Node with `backend: 'cpu'`. + +## Docs Quickstart (CartPole) + +Create `src/main.ts`: + +```ts +import { IgnitionEnvTFJS } from '@ignitionai/backend-tfjs' +import { CartPoleEnv } from '@ignitionai/environments' + +const env = new IgnitionEnvTFJS(new CartPoleEnv()) + +env.train('dqn') // starts the automatic DQN loop +// env.infer() // greedy loop after an agent exists +// env.setSpeed(50) // faster automatic ticks +``` + +What those calls do on this SHA: + +- `new CartPoleEnv()` — built-in `TrainingEnv`: actions `push_left` / `push_right`, 4-D observe, Euler physics, `done()` when the pole tips past 12°, the cart leaves `±2.4`, or 500 env steps elapse. +- `new IgnitionEnvTFJS(env)` — TF.js factories for `dqn`, `double-dqn`, `ppo`, `qtable`. Input size is `observe().length`; action size is `actions.length`. +- `train('dqn')` — creates a DQN agent from `ALGORITHM_DEFAULTS.dqn` and starts the automatic loop (`stepIntervalMs` 50, `stepsPerTick` 1). +- `stop()` — cancels future ticks. A transition already started may finish. `lastError` holds automatic-loop failures. +- `step()` / `inferStep()` — one serialized transition (train vs greedy). + +Runtime DQN defaults live in `packages/backend-tfjs/src/defaults.ts` (`hiddenLayers: [64, 64]`, not the `[24, 24]` still written in some comments and in the web Quickstart narrative). Cite exact `file:line` from [references/signatures.md](references/signatures.md). + +## Bound the loop (required) + +`train()` does not stop by itself. For scripts and tests: declare **budget** and **seeds** (at least three) **before** any `train()` / `step()`, then halt the automatic loop. + +```ts +import { IgnitionEnvTFJS } from '@ignitionai/backend-tfjs' +import { CartPoleEnv } from '@ignitionai/environments' + +const SEEDS = [11, 29, 47] +const TRANSITIONS = 8 + +export async function runSeed(seed: number) { + const env = new IgnitionEnvTFJS(new CartPoleEnv()) + env.train('dqn', { + backend: 'cpu', + seed, + hiddenLayers: [8], + batchSize: 4, + memorySize: 64, + }) + env.stop() + const rewards: number[] = [] + for (let i = 0; i < TRANSITIONS; i++) { + rewards.push((await env.step()).reward) + } + env.stop() + env.agent?.dispose?.() + return { seed, stepCount: env.stepCount, rewards, lastError: env.lastError } +} +``` + +`DQNConfig.seed` (integer 1–1_000_000) seeds the agent. It does **not** seed `CartPoleEnv`. Paste raw rewards; do not invent a success threshold. + +Runnable copy: `examples/skill-quickstart.ts` (tested by `examples/skill-quickstart.test.ts`). + +## I want… → skill + +| I want… | Skill | Path | +|---|---|---| +| A first CartPole agent and this index | **ignition-quickstart** | `skills/ignition-quickstart/` | +| To implement my own `TrainingEnv` | **ignition-create-env** | `skills/ignition-create-env/` | +| To choose algorithm, overrides, and a training loop | **ignition-train** | `skills/ignition-train/` | +| To separate training from greedy inference | **ignition-train-infer-split** | `skills/ignition-train-infer-split/` | +| To change rewards without silently breaking learning | **ignition-reward-shaping** | `skills/ignition-reward-shaping/` | +| To diagnose a loop that does not improve | **ignition-debug-training** | `skills/ignition-debug-training/` | +| To evaluate across seeds and report numbers | **ignition-evaluate** | `skills/ignition-evaluate/` | +| To export a trained policy to ONNX | **ignition-export-onnx** | `skills/ignition-export-onnx/` | +| To save and load weights / runner state | **ignition-save-load** | `skills/ignition-save-load/` | + +The other eight skill folders land in parallel PRs. Follow this index; do not invent extra skill names. + +Save/load shown in the web Quickstart (`IndexedDBProvider`, `env.save`, `env.load`) belongs to **ignition-save-load**, not this skill. + +## Install this skill on an agent + +Verified method (no CLI): copy this directory to the project's Agent Skills folder: + +```text +.claude/skills/ignition-quickstart/SKILL.md +``` + +Keep the folder name equal to the frontmatter `name`. See `skills/README.md` for the full set. diff --git a/skills/ignition-quickstart/ignition-quickstart.test.ts b/skills/ignition-quickstart/ignition-quickstart.test.ts new file mode 100644 index 0000000..4d6e222 --- /dev/null +++ b/skills/ignition-quickstart/ignition-quickstart.test.ts @@ -0,0 +1,57 @@ +import { readFileSync } from 'node:fs'; +import { dirname, join } from 'node:path'; +import { fileURLToPath } from 'node:url'; +import { describe, expect, it } from 'vitest'; + +const skillDir = dirname(fileURLToPath(import.meta.url)); +const repoRoot = join(skillDir, '../..'); + +function parseFrontmatter(markdown: string): { name: string; description: string; body: string } { + const match = markdown.match(/^---\n([\s\S]*?)\n---\n([\s\S]*)$/); + if (!match) { + throw new Error('SKILL.md is missing YAML frontmatter'); + } + const frontmatter = match[1]; + const body = match[2]; + const name = frontmatter.match(/^name:\s*(.+)$/m)?.[1]?.trim(); + const descriptionBlock = frontmatter.match(/^description:\s*(?:\|>-?|>-?|)?\s*([\s\S]*?)(?=^[a-zA-Z0-9_-]+:|\s*$)/m); + const description = (descriptionBlock?.[1] ?? '') + .split('\n') + .map((line) => line.replace(/^\s{2,}/, '').trim()) + .filter((line, index, lines) => !(index === lines.length - 1 && line.length === 0)) + .join(' ') + .trim(); + if (!name) { + throw new Error('frontmatter is missing name'); + } + return { name, description, body }; +} + +describe('ignition-quickstart skill artifacts', () => { + it('has valid Agent Skill frontmatter and size limits', () => { + const markdown = readFileSync(join(skillDir, 'SKILL.md'), 'utf8'); + const { name, description, body } = parseFrontmatter(markdown); + expect(name).toBe('ignition-quickstart'); + expect(description.length).toBeGreaterThan(0); + expect(description.length).toBeLessThanOrEqual(1024); + expect(body.split(/\r?\n/).length).toBeLessThanOrEqual(500); + }); + + it('indexes exactly the nine skills from issue 102', () => { + const readme = readFileSync(join(repoRoot, 'skills/README.md'), 'utf8'); + const expected = [ + 'skills/ignition-quickstart/', + 'skills/ignition-create-env/', + 'skills/ignition-train/', + 'skills/ignition-train-infer-split/', + 'skills/ignition-reward-shaping/', + 'skills/ignition-debug-training/', + 'skills/ignition-evaluate/', + 'skills/ignition-export-onnx/', + 'skills/ignition-save-load/', + ]; + const listed = [...new Set(readme.match(/skills\/ignition-[a-z-]+\//g) ?? [])].sort(); + expect(listed).toEqual([...expected].sort()); + expect(listed).toHaveLength(9); + }); +}); diff --git a/skills/ignition-quickstart/references/signatures.md b/skills/ignition-quickstart/references/signatures.md new file mode 100644 index 0000000..f5e8b81 --- /dev/null +++ b/skills/ignition-quickstart/references/signatures.md @@ -0,0 +1,43 @@ +# Public API used by ignition-quickstart + +Pinned to `dev` SHA `343a76952f9dcd6594674c3526ee03ee45b4d93e`. Line numbers are 1-based in that tree. + +## Packages + +- `@ignitionai/core` `0.1.0` — `packages/core/package.json` +- `@ignitionai/backend-tfjs` `0.1.0` — `packages/backend-tfjs/package.json` +- `@ignitionai/environments` `0.1.0` — `packages/environments/package.json` + +## Signatures + +| Use | Signature | Cite | +|---|---|---| +| TF.js runner | `export class IgnitionEnvTFJS extends IgnitionEnv` | `packages/backend-tfjs/src/ignition-env-tfjs.ts:26` | +| Re-export | `export { IgnitionEnvTFJS, IgnitionEnvTFJS as IgnitionEnv }` | `packages/backend-tfjs/src/index.ts:11` | +| CartPole class | `export class CartPoleEnv implements TrainingEnv` | `packages/environments/src/cartpole.ts:23` | +| CartPole export | `export { CartPoleEnv } from './cartpole'` | `packages/environments/src/index.ts:2` | +| CartPole actions | `actions = ['push_left', 'push_right']` | `packages/environments/src/cartpole.ts:24` | +| CartPole observe | `observe(): number[]` | `packages/environments/src/cartpole.ts:41` | +| CartPole step | `step(action: number \| number[]): void` | `packages/environments/src/cartpole.ts:45` | +| CartPole reward | `reward(): number` | `packages/environments/src/cartpole.ts:67` | +| CartPole done | `done(): boolean` | `packages/environments/src/cartpole.ts:71` | +| CartPole reset | `reset(): void` | `packages/environments/src/cartpole.ts:79` | +| Env contract | `export interface TrainingEnv` | `packages/core/src/types.ts:95` | +| Train | `public train(algorithm?: AlgorithmType, overrides?: Record): void` | `packages/core/src/ignition-env.ts:41` | +| Manual step | `public step(): Promise` | `packages/core/src/ignition-env.ts:73` | +| Greedy step | `public inferStep(): Promise` | `packages/core/src/ignition-env.ts:77` | +| Infer loop | `public infer(): void` | `packages/core/src/ignition-env.ts:111` | +| Stop | `public stop(): void` | `packages/core/src/ignition-env.ts:147` | +| Speed | `public setSpeed(multiplier: number): void` | `packages/core/src/ignition-env.ts:164` | +| lastError | `public lastError: Error \| null = null` | `packages/core/src/ignition-env.ts:14` | +| stepCount | `public stepCount = 0` | `packages/core/src/ignition-env.ts:15` | +| Algorithm union | `export type AlgorithmType = 'dqn' \| 'double-dqn' \| 'ppo' \| 'qtable'` | `packages/core/src/types.ts:126` | +| DQN seed | `seed?: number` on `DQNConfig` | `packages/backend-tfjs/src/types.ts:11` | +| Seed schema | `seed: z.number().int().min(1).max(1000000).optional()` | `packages/backend-tfjs/src/schemas.ts:10` | +| DQN defaults | `hiddenLayers: [64, 64], ...` | `packages/backend-tfjs/src/defaults.ts:1-11` | +| Merge overrides | `export function mergeDefaults(...)` | `packages/core/src/defaults.ts:5` | +| Agent dispose | `dispose?(): void` | `packages/core/src/types.ts:62` | + +## Docs source for the 7-line example + +`packages/web/content/en/quickstart.mdx` (install + `IgnitionEnvTFJS` + `CartPoleEnv` + `train('dqn')`).