These instructions apply to the whole Pen monorepo and are written for AI coding agents (Cursor agents and Agent mode in the Cursor IDE). Human contributors follow the same conventions where noted.
Pen is a headless, extension-first, block-native rich text editor SDK built on a Yjs CRDT document, with first-class AI co-authoring. It is licensed under the MIT License (LICENSE.md).
The monorepo is layered; dependencies point strictly downward:
packages/types(@input/pen-types) — shared contracts: types, constants, guards. The schema builders and registry moved topackages/core/src/schema/and re-export from core; what remains isgenerateId, which cannot follow them because@input/pen-yjssits below core (seespec/rules/api.mdAPI3, enforced byscripts/types-purity.mjs).packages/crdt/yjs(@input/pen-yjs) — the Yjs adapter: document shape (blockOrder,blocks,apps,metadata), transactions, update handling, undo integration.packages/core(@input/pen-core) — the editor runtime:editor.apply(ops, options)pipeline, validation, normalization, selection, extension manager, events. Runtime authority for everything.packages/schema— the default block/inline schema set.packages/rendering/dom(@input/pen-dom) — the framework-free DOM engine: field editors (EditContext + contenteditable backends), selection bridge, key handling, clipboard/transfer, reconciliation, overlays. The hardest code in the repo lives here.packages/rendering/react/packages/rendering/vue— thin framework bindings over pen-dom. Behavior belongs in pen-dom or core, never here.packages/extensions/*— undo, snapshots, search, autoformat, shortcuts, multiplayer, tools,@input/pen-ai(subpaths: suggestions, autocomplete, skills, tools, stream), and@input/pen-interop(subpaths: html, markdown, json, xml).packages/pen—@input/pen, the batteries-included starter: default schema + recommended extensions, withcreateEditor/createHeadlessEditorwrapping core's to default an omittedpresettodefaultPreset().packages/shared/*(ingest, markdown),packages/transport(./direct,./ssesubpaths),packages/tooling/*(test, bench, assets),packages/docs,playground/(reference app;pnpm test:e2eruns against it).
spec/README.md is the index. spec/ describes shipped behavior only — no roadmap. Load the matching spec/rules/ document and package spec before editing a surface. Normative rules carry stable IDs (A1, S4, SEC1, API6, …); cite them in PR descriptions and test names. When implementation proves a rule wrong or untestable, amend the spec in the same PR. Silent divergence is a defect.
DocumentOp[]is the mutation currency;editor.apply(ops, { origin })is the only durable write path. Never writeY.Text/Y.Mapdirectly or calladapter.transactoutside core. Streaming goes througheditor.openTextStream/TextStreamWriter(@input/pen-ai/stream).- Set operation origins intentionally (
user,ai,collaborator,input-rule, structured origins withgroupId/requestId); undo, suggestions, and diagnostics depend on them. - Keep Pen headless: core and extensions must work without a DOM (
createHeadlessEditor). Only@input/pen-dommay touch browser globals. - Prefer non-fatal behavior in runtime paths: drop invalid input with a
diagnosticevent rather than throwing from hooks, observers, or extension code. - Normalization is incremental and idempotent; repeated passes must not produce new changes.
- The
\u200Bempty-block sentinel is removed from storage (spec/rules/empty-blocks.mdEM1–EM8). Do not add new code that tests for it. The two-seam confinement inspec/rules/selection.md§2 was the interim v2 position and is retired (I11 → I14). - Selection has one reader and one writer in
@input/pen-dom(spec/rules/selection.mdS1); do not addrequestAnimationFrame/setTimeoutretries, suppression flags, or intent counters to selection paths (S4). If a selection bug cannot be fixed without one, stop and surface it. - Follow
.cursor/rules/*.mdcfor import style (extensionless), extension resilience, and headless React primitive conventions.
From the repository root (pnpm + turbo):
pnpm build/pnpm typecheck/pnpm test— all workspaces via turbo.pnpm --filter @input/pen-core test(or any package name) — scoped runs; prefer these while iterating.pnpm test:e2e— Playwright suite.pnpm lint— Prettier format check plus turbo lint.pnpm changeset— required for any change to a published package (see Releases).
For substantive changes run pnpm build, pnpm typecheck, and pnpm test before finishing (.cursor/rules/pen-security-quality-gates.mdc). Scale scope to the change: a one-package fix needs that package's checks plus its dependents' tests, not the world.
- Vitest,
.test.ts, colocated per package (src/__tests__/or alongside sources — match the package you are in). - Test headlessly by default: core logic, ops, schema, extension behavior, and anything expressible without a DOM. jsdom cannot represent real selection, IME, or geometry — do not write jsdom tests that pretend to cover those; that is what the conformance package (
spec/rules/reliability.md) is for. - Perf-sensitive paths have benchmark expectations in
packages/tooling/bench; do not regress them. - Deterministic fixtures live in
packages/tooling/test; reuse them instead of hand-rolling documents.
- Local agents: never create or switch branches; stay on the user's checkout.
- Commit messages: imperative sentence naming the component and objective, matching repo history (
Refactor editor extension handling and improve test structure,Enhance inline atom handling and DOM reconciliation). No conventional-commit prefixes. - Only commit when explicitly asked.
- Changesets drive versioning (
pnpm changeset,pnpm version-packages). GitHub opens the Version Packages PR with its built-in token. A maintainer approves the bot PR workflows and merges after required checks pass; release CI then builds and publishes withpnpm releaseusingNPM_TOKEN, atomically pushes package tags plusvX.Y.Z, and creates thevX.Y.ZGitHub Release. The version PR's required checks are the release gate; the release job does not repeat them. Recover failures by re-running the original version-commit workflow. Setup and token rotation are documented in.changeset/README.md; no GitHub App is required. Any PR that changes a published package's behavior or API includes a changeset. The train is0.x; breaking isminor, additive ispatch, andmajoris rejected until 1.0. The first published train was0.1.0. - Published packages ship dual ESM/CJS with
exportsmaps,files, andsideEffects: false; keep manifests consistent (sync-package-metadata.mjsexists for shared fields).
.agents/skills/pen-deslop— audit for AI-generated slop patterns specific to this codebase..agents/skills/ligne-blanche— boundary/structural integrity review for changes..cursor/skills/investigate— root-cause investigation workflow; use before fixing non-trivial bugs..cursor/agents/spec-reviewer.md— reviews a diff againstspec/,spec/rules/rule IDs, and.cursor/rules; use proactively after implementing features or refactors.
Turborepo configuration, task behavior, and CLI commands can vary between installed versions and may differ from your training data. Resolve the turbo package from this file's directory or relevant workspace; in monorepos, it may not be visible from the repository root. For example, run node -p "require.resolve('turbo/package.json')" from a workspace that depends on turbo.
Read docs/README.md inside that installed package first, then read the relevant pages from its docs/ directory before changing Turborepo configuration or commands. Heed deprecation notices. These bundled docs match the installed package version and are available without network access.
This block is written and re-added by turbo before repository-scoped commands when an AI agent is detected. In the Turborepo source repository, its template is defined in crates/turborepo-cli/src/cli/agent_guidance.rs. Removing the managed block while updates are enabled means a later qualifying invocation will add it again. Set "agentGuidance": false in the root turbo.json or turbo.jsonc to opt out; this does not remove an existing block. Keep the block committed with your work to avoid an uncommitted change on the next agent invocation.