# SpacePress Coding Agent Rules

Targets SpacePress v0 ("Istanbul") / `typedpress` CLI `0.3.0` (WordPress 7.1+, PHP 8.2+).

## Core compilation model

- **Server TypeScript compiles to native PHP — not Node.js.** Never import Node built-ins (`fs`, `path`, `crypto`, `process`, `buffer`, `child_process`) or arbitrary npm packages inside `typedpress.config.ts`, `tp/app/`, actions, hooks, jobs, or routes.
- **Headful WordPress is the default.** Files under `tp/app/**/page.tsx` and `layout.tsx` compile directly into server-rendered PHP inside WordPress. Interactive widgets belong in `"use client"` components compiled to the WordPress Interactivity API. Only wire `tp/_generated/client/` or `@typedpress/astro` when the user explicitly wants a separate headless frontend.
- **Never edit `tp/_generated/` or `tp/dist/`.** `typedpress build` regenerates them. Commit `tp/_generated/` (migrations, snapshots, types, plugin lockfile); git-ignore `tp/dist/` and `.tp/`.
- **Pre-release identifier:** The framework brand is SpacePress (`spacepress.dev`), while the npm package, CLI binary, config file (`typedpress.config.ts`), `tp/` directory, and imports (`"typedpress"`, `"typedpress:db"`, `"typedpress/app"`, `"typedpress/client"`) use `typedpress` during v0.

## Exact import boundaries

- `"typedpress"`: schema & server builders (`collection`, `table`, `global`, `wp`, `taxonomy`, `store`, `product`, `productCategory`, `productTag`, `order`, `action`, `job`, `jobs`, `db`, `mail`, `HttpError`, `defineConfig`, and field builders `text`, `integer`, `boolean`, `richText`, `slug`, `select`, `timestamp`, `ref`, `email`).
- `"typedpress:db"`: visitor-scoped kind queries (e.g. `import { posts, tasks } from "typedpress:db"`). **Always prefer `"typedpress:db"` in `tp/app/` pages** because it enforces `.access()` rules automatically. Reserve trusted `db` from `"typedpress"` for `action()`/`job()` handlers after verifying authorization.
- `"typedpress/app"`: server page/layout helpers (`Link`, `Content`, `useFormState`, `type PageProps`, `type LayoutProps`).
- `"typedpress/client"`: client-only hooks (`useState`, `useLive`) — valid **only** inside files starting with `"use client";`.
- `"php/wordpress"`: typed bridges to WordPress core functions and `abilities.call(...)`.

## Data modeling & migrations

- Use `collection()` (or `wp.posts.extend()`) for editorial content that needs Gutenberg, drafts, revisions, comments, URL routing, or `sync: "content/**/*.md"`.
- Use `table()` for relational application rows (tasks, votes, orders, subscriptions) stored in dedicated SQL tables with `.index([...])` and incremental migrations.
- Export kinds, actions, and jobs from `typedpress.config.ts` or any file under `tp/` — they register automatically by export name; `defineConfig()` does not list them.
- **Timestamp values:** Pass ISO-8601 strings (`new Date().toISOString()`) when writing `timestamp()` fields — not raw `Date` objects.
- **Safe renames:** When renaming a field or column, chain `.was("oldName")` so `typedpress build` emits a `RENAME` migration instead of dropping data.
- **WooCommerce checkout:** Whenever a project exports `store()` alongside `tp/app/`, include a public `tp/app/checkout/page.md` containing WooCommerce's native Checkout Block (`<!-- wp:woocommerce/checkout -->`).
- **Markdown sync:** Avoid raw HTML comments (`<!-- ... -->`) in synced `.md` files and never reuse a deleted document's `uid` for a new file.

## Security & authorization

- Declare explicit `access` rules on every `collection()`, `table()`, `global()`, and `action()`, and export `guard` on every custom `route.ts`.
- For user-owned rows, return a row filter (`({ user }) => (user ? { ownerId: user.id } : false)`) rather than only checking truthiness; never trust a client-supplied `userId` in an action.
- Attach field-level `.access({ read: "admin" })` to sensitive columns so REST, pages, and generated clients strip them automatically.
- Annotate `action()` declarations with `readonly: true` or `destructive: true` to guide WordPress Abilities safety checks (`agents: false` opts internal actions out).

## Verification workflow

- Run `npx typedpress check` after every edit to `typedpress.config.ts` or `tp/` (`npx typedpress check --fix` applies auto-fixes such as `capital_P_dangit` for proper `WordPress` spelling).
- Run `npx typedpress init` after config changes (`npx typedpress init --check` in CI).
- Before deploying to production, always run `npx typedpress push --dry-run`, obtain explicit user approval, and verify afterward with `npx typedpress doctor --site <url> --links`.
- On WP Cloud (Pressable / WordPress.com), avoid `/files/*` for custom public downloads and copy top-level `public/*.{js,wasm,txt}` files into `/srv/htdocs/` before purging edge cache.
