From 1c8a434fda2fb7aecd8fbc33dc35c12a9879d28b Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 29 Oct 2026 02:46:39 +0000 Subject: [PATCH] AV3: ready to instantiate The README says what the site is, how to run, test and preview it (the model off with `pnpm dev`, the account's with `pnpm dev:remote`), how its owner adds pages with the admin token, how it deploys, and how the KV namespace's id reaches the Worker. The as-built status records every departure from the upstream template and what is proven, and how. Co-Authored-By: Claude Claude-Session: https://claude.ai/code/session_019PcpNVpFBcbeiX3EHdXfmn --- README.md | 162 +++++++++++++++--- specs/epics/README.md | 2 +- .../agent-visibility/IMPLEMENTATION-STATUS.md | 108 +++++++++++- specs/epics/agent-visibility/README.md | 2 +- 4 files changed, 244 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index f050488..ea81d2f 100644 --- a/README.md +++ b/README.md @@ -1,26 +1,142 @@ # agent-visibility Cloudflare's [Agent Visibility template](https://github.com/cloudflare/templates/tree/main/agent-visibility-template) -— one content store, served to AI agents through every discovery surface: -`llms.txt` and `llms-full.txt`, a typed JSON index, per-page Markdown, -`robots.txt` with explicit AI-bot directives, `Content-Signal` headers and -schema.org JSON-LD, each page enriched by **Workers AI** and cached in -**Workers KV** — running on **Cloudflare Workers + Static Assets**, with the -KV namespace provisioned by Terraform through -[Orun](https://github.com/sourceplane/orun), its writes closed until its -owner stores an admin token — and, at its last milestone (AV4), packaged as -an **Orun baseline**: one command taking a new product from nothing to live -on stage and prod, in its own repository, under its own name, in ONE -landing. - -This repository is being built in five steps, one pull request each: the -spec (AV0), the site on Workers (AV1), the KV namespace from Terraform -(AV2), ready to instantiate (AV3), and the baseline (AV4). The design, the -milestones and the as-built status are in -[`specs/epics/agent-visibility`](specs/epics/agent-visibility/README.md). - -## Credit - -The app is Cloudflare's [`agent-visibility-template`](https://github.com/cloudflare/templates/tree/main/agent-visibility-template), -MIT-licensed (© 2018 Cloudflare, Inc.); the licence arrives with it, in -`apps/web/LICENSE`, at AV1. +— one content store, served to AI agents through every surface they read: +`/llms.txt` and `/llms-full.txt`, a typed `/index.json`, per-page Markdown +and JSON-LD, a `/robots.txt` that names the AI crawlers it welcomes, and a +`Content-Signal` header on each, every page enriched once by **Workers AI** +(a title, a summary, key points, topics, a clean Markdown body) and cached +in **Workers KV**, with a small dashboard previewing the surfaces — running +on **Cloudflare Workers + Static Assets**, with the KV namespace provisioned +by Terraform through [Orun](https://github.com/sourceplane/orun) — and, at +its last milestone (AV4), packaged as an **Orun baseline**: one command +taking a new product from nothing to live on stage and prod, in its own +repository, under its own name, in ONE landing. + +The Worker, its surfaces, its enrichment and its dashboard are upstream's, +unchanged; the dashboard is built by Vite into the Worker's Static Assets. +Every surface is **public by design** — it exists to be read by agents. +Writes — adding a page, clearing the cache — are **refused until its owner +stores an admin token**, as upstream ships it. See +[`specs/epics/agent-visibility`](specs/epics/agent-visibility/README.md) +for the design, the milestones and the as-built status. + +## What is here + +| Surface | Where | +|---|---| +| The Worker: every agent surface, the dashboard's API, the optional Web Bot Auth surface | `apps/web/src/worker/index.ts` | +| The health route (the deploy smoke's store check) | `apps/web/src/worker/health.ts` | +| The enrichment (Workers AI, and its deterministic fallback) and the surfaces' renderers | `apps/web/src/enrichment` | +| The KV store, the sample pages, the types | `apps/web/src/lib` (`store.ts`, `content.ts`, `types.ts`) | +| The dashboard | `apps/web/src/react-app`, `apps/web/index.html`, built by `apps/web/vite.config.ts` | +| Workers bindings and deploy contract | `apps/web/wrangler.template.jsonc` + `wiring.fixture.json` (→ the committed `wrangler.jsonc`), `component.yaml` | +| The admin token's path to the Worker (a Worker secret) | `tooling/wire/worker-secrets.mjs` | +| The KV namespace (stage, prod) | `infra/terraform/cloudflare-kv` | +| The wrangler renderer | `tooling/wire` | +| Tests | `tests/web` (vitest: upstream's suite over an in-memory KV store and a stand-in model, the health route, the enrichment and its fallback, writes with no token, the config's routes and bindings) | + +## Getting started + +```bash +pnpm install +pnpm run wire:fixture # render apps/web/wrangler.jsonc from the offline fixture +pnpm typecheck +pnpm lint +pnpm test +pnpm build # the dashboard → apps/web/dist/client +``` + +```bash +pnpm --filter @app/web dev # the site on http://localhost:8787: a local KV store, the model off +pnpm --filter @app/web dev:remote # the same with your account's Workers AI (wrangler login first) +``` + +`pnpm dev` runs `wrangler dev --local`, remote bindings off, as upstream's +dev server does: Workers AI has no local mode, so every page is enriched by +upstream's fallback from its own text (its `model` reads `… (fallback)`). +`dev:remote` calls the real model and may incur Workers AI usage. + +## Adding your pages + +The site starts with upstream's five sample pages for "Acme" (in +`apps/web/src/lib/content.ts`). Replace them in the source, or post your +own: store `ADMIN_TOKEN` for an environment — an Orun secret, +`orun secrets set ADMIN_TOKEN --env ` — and redeploy it; the deploy +pushes it to the Worker as a Worker secret. Then: + +```bash +curl -X POST "https:///api/resources" \ + -H "Authorization: Bearer $ADMIN_TOKEN" -H "content-type: application/json" \ + -d '{"slug":"changelog","title":"Changelog","url":"https://example.com/changelog","body":"# Changelog\n\nv2 adds templates."}' +``` + +```bash +curl -X POST "https:///api/refresh" -H "Authorization: Bearer $ADMIN_TOKEN" +``` + +The first adds (or replaces) one page — `slug` is 1–63 characters of +`[a-z0-9-]`, `body` HTML or Markdown up to 100 kB, at most 100 pages — and +enriches it; the second clears the enriched cache, so the next read +re-enriches every page. With no token stored both answer `401`, and so +does a wrong one. Locally the token goes in `apps/web/.dev.vars` +(`.dev.vars.example`). The site's name on its surfaces is `SITE_NAME` in +`apps/web/wrangler.template.jsonc` (`pnpm run wire:fixture` after editing). + +## Deploying + +A merge to `main` applies the KV namespace on stage and prod and then +deploys `agent-visibility-web-{stage,prod}` bound to it and to Workers AI, +once `intent.yaml` names the product's Orun workspace (it names the +placeholder `ws_CHANGE_ME` until it is linked, and CI plans nothing). By +hand: Run workflow on `main` (`ci.yml`) with the environment, and +optionally the components, to deploy. The deploy's smoke reads +`/api/health`, which never calls the model: the first visitor's read of a +surface enriches the pages. + +## How it maps to Cloudflare's template + +| The template | Here | +|---|---| +| `src/worker`, `src/enrichment`, `src/lib`, `src/react-app`, `index.html` | The same files in `apps/web`, plus `src/worker/health.ts` and its one route | +| `vite` + `@cloudflare/vite-plugin` (dev server and build) | Vite with the React plugin builds the dashboard into `dist/client`; wrangler bundles the Worker. `pnpm dev` is `wrangler dev --local`, remote bindings off as upstream's (`remoteBindings: false`) | +| A KV namespace provisioned by "Deploy to Cloudflare" | `infra/terraform/cloudflare-kv`, applied by Orun before the Worker deploys, one namespace per environment, its id rendered into the binding | +| `"ai": { "binding": "AI" }` | Unchanged: keyless, the account's Workers AI | +| `SITE_NAME` "Acme Docs", `SITE_DESCRIPTION` | The product's name and a line about it; the sample pages stay upstream's | +| `npx wrangler secret put ADMIN_TOKEN` | `orun secrets set ADMIN_TOKEN --env ` and redeploy: pushed as a Worker secret | +| `npx wrangler deploy` | Orun applies the namespace and deploys `-web-{stage,prod}` on merge to `main` (`apps/web/component.yaml`) | +| "Deploy to Cloudflare" (clone the template) | `orun new --blueprint repo-blueprint.yaml` — a new repository, branded, deployed | +| `test/index.test.ts` under `@cloudflare/vitest-pool-workers` | `tests/web`: the same assertions, the Worker's own `fetch` over an in-memory store | +| npm, `hono` 4.11.1, `react` 19.2.1 | pnpm, the same versions pinned exactly | + +## How the namespace reaches the Worker + +``` +infra/terraform/cloudflare-kv ──apply──► orun secret WIRING_CLOUDFLARE_KV (stage, prod) + │ (each deploy resolves it as it starts; + │ a missing one fails the lane) + ▼ +apps/web dependsOn cloudflare-kv (enforced on main) · secretEnv WIRING_CLOUDFLARE_KV_ + optionalSecretEnv ADMIN_TOKEN + build: tooling/wire/live.mjs → wrangler.jsonc with the environment's namespace id + (dev, verify-only, renders the fixture) → vite build (the token unset) + → tooling/wire/worker-secrets.mjs: the token, when stored, to the Worker + deploy: CLOUDFLARE_API_TOKEN (workers-deploy); Workers AI through the AI binding, no key +``` + +`wrangler.jsonc` is committed as the fixture render of +`wrangler.template.jsonc`, because the deploy lane's `verify-app-structure` +step checks the config exists before it builds; `tests/web` fails if the +committed render and the fixture disagree. + +## CI + +| Workflow | What it runs | +|---|---| +| `ci.yml` | the Orun pipeline: `orun plan --changed`, then one job per lane. A pull request runs the verify lanes (the KV root planned for stage and prod, the dashboard built and the Worker dry-deployed per environment, the tests run); a merge applies the KV namespace, then deploys stage → prod. It plans nothing while `intent.yaml` names the placeholder workspace `ws_CHANGE_ME`, as this repository does. A manual run (Run workflow on `main`) plans the environment and components it names under the push-to-main trigger, so they deploy as a merge deploys them | + +## Credits + +The template this port starts from is Cloudflare's +[`agent-visibility-template`](https://github.com/cloudflare/templates/tree/main/agent-visibility-template), +MIT (© 2018 Cloudflare, Inc.; kept as `apps/web/LICENSE`). diff --git a/specs/epics/README.md b/specs/epics/README.md index 9379ce6..b2ae6e9 100644 --- a/specs/epics/README.md +++ b/specs/epics/README.md @@ -7,6 +7,6 @@ folder is the adoption; the merge creates the epic in the workspace. | Epic | Cluster | Status | What it is | |------|---------|--------|------------| -| [`agent-visibility/`](./agent-visibility/) | **AV** | In progress (AV0) | Cloudflare's Agent Visibility template — one content store served to AI agents as llms.txt, a JSON index, per-page Markdown, robots.txt, Content-Signal headers and JSON-LD, enriched by Workers AI — on Cloudflare Workers with the KV namespace from Terraform, and an Orun baseline that instantiates it in one landing (AV4) | +| [`agent-visibility/`](./agent-visibility/) | **AV** | In progress (AV0–AV3 shipped) | Cloudflare's Agent Visibility template — one content store served to AI agents as llms.txt, a JSON index, per-page Markdown, robots.txt, Content-Signal headers and JSON-LD, enriched by Workers AI — on Cloudflare Workers with the KV namespace from Terraform, and an Orun baseline that instantiates it in one landing (AV4) | As-built ≠ intent: what shipped lives in each epic's `IMPLEMENTATION-STATUS.md`. diff --git a/specs/epics/agent-visibility/IMPLEMENTATION-STATUS.md b/specs/epics/agent-visibility/IMPLEMENTATION-STATUS.md index e7427f1..9c5d6f0 100644 --- a/specs/epics/agent-visibility/IMPLEMENTATION-STATUS.md +++ b/specs/epics/agent-visibility/IMPLEMENTATION-STATUS.md @@ -7,12 +7,110 @@ at `7ae5750`, MIT). | Milestone | State | PR | |---|---|---| -| AV0 — the spec | In progress | [#1](https://github.com/sourceplane/agent-visibility/pull/1) (AV-1) | -| AV1 — the site on Workers | Planned | — | -| AV2 — the KV namespace from Terraform | Planned | — | -| AV3 — ready to instantiate | Planned | — | +| AV0 — the spec | ✅ Shipped | [#1](https://github.com/sourceplane/agent-visibility/pull/1) (AV-1) | +| AV1 — the site on Workers | ✅ Shipped | [#2](https://github.com/sourceplane/agent-visibility/pull/2) (AV-2) | +| AV2 — the KV namespace from Terraform | ✅ Shipped (validated; apply gated on a linked workspace) | [#3](https://github.com/sourceplane/agent-visibility/pull/3) (AV-3) | +| AV3 — ready to instantiate | ✅ Shipped | [#4](https://github.com/sourceplane/agent-visibility/pull/4) (AV-4) | | AV4 — a baseline | Planned | — | ## Departures from the upstream template -None yet: each milestone records here what it changed from the template, as it lands. +### Where it lives + +Upstream's template is `apps/web` (`@app/web`) in the monorepo, with its +`LICENSE` beside it (`apps/web/LICENSE`, the cloudflare/templates +licence). Its `src/` and `index.html` are carried file for file; its +`wrangler.jsonc` becomes `wrangler.template.jsonc`, rendered per +environment into `wrangler.jsonc`. Upstream's `package-lock.json`, +`worker-configuration.d.ts`, `tsconfig.node.json` and `vitest.config.ts` +are not carried: pnpm locks the workspace, `src/lib/types.ts` (upstream's) +with `@cloudflare/workers-types` declares the bindings, and two +`tsconfig`s extend the monorepo's — `tsconfig.json` for the Worker, +`tsconfig.app.json` for the dashboard — with upstream's compiler settings +for this code (the `ES2023` lib without `WebWorker`, whose stricter +WebCrypto signatures reject upstream's Web Bot Auth module, and no +unchecked-index rule). + +### The build — Vite alone + +Upstream builds the Worker and the client together with +`@cloudflare/vite-plugin`. Here Vite with `@vitejs/plugin-react` builds the +dashboard into `dist/client` (`vite.config.ts`) and wrangler bundles +`src/worker/index.ts` — the composition's `workerEntrypoint` — with the +same assets directory, `not_found_handling` and `run_worker_first` list. +`pnpm dev` is `vite build && wrangler dev --local` (remote bindings off, as +upstream's Vite config sets `remoteBindings: false`); `pnpm dev:remote` +drops `--local` and reaches the account's Workers AI. + +### The KV namespace + +Upstream's `kv_namespaces` entry carries an id from its own account. Here +the id is `@@wiring(cloudflare-kv/__ENV__:app_kv_id)@@`, rendered from the +`cloudflare-kv` root's wiring document on stage and prod, and from +`wiring.fixture.json` for dev and local work (the committed +`wrangler.jsonc` is the stage fixture render). The binding name, +`VISIBILITY_CACHE`, and every key the store writes are upstream's. + +### The admin token's path to the Worker + +Upstream sets `ADMIN_TOKEN` with `wrangler secret put`. Here it is an +optional Orun secret (`optionalSecretEnv`), so a product deploys without +it and refuses writes until it is stored. The deploy's `buildCommand` +renders the config, runs the build with it unset (`env -u ADMIN_TOKEN`), +then `tooling/wire/worker-secrets.mjs` pushes it, when present, to +`-web-` with `wrangler secret bulk` over stdin, naming nothing +when it is not stored. + +### `/api/health` + +New (`src/worker/health.ts`, one route in `src/worker/index.ts`). It reads +`resources:enriched` from KV — never the model — and answers +`{ ok, kv, ai, cached, pages }`, or `503` with `ok: false` when KV fails. +It is the component's smoke (`"ok":true`, then `/` serving the dashboard, +`id="root"`) and the deployment record's probe. + +### `SITE_NAME` and `SITE_DESCRIPTION` + +Upstream's are the sample's ("Acme Docs", "Product documentation for the +Acme platform."). Here they are the product's name ("AI Agent Visibility", +upstream's own label for the template) and a line about it, so a product's +surfaces carry its own name; the sample pages themselves stay upstream's, +for their owner to replace. + +### Tests + +Upstream's `test/index.test.ts` ran under `@cloudflare/vitest-pool-workers` +against miniflare. `tests/web/src/worker.test.ts` carries it case for case, +every assertion kept, calling the Worker's own `fetch` over an in-memory KV +namespace and a model that fails every call (as upstream's did, with +remote bindings off), `ADMIN_TOKEN` "test-token". Added: the health route, +the enrichment of a cold store through the stand-in model (one call per +page, cached for the TTL) and its fallback (cached for 60 s), a posted +page's enrichment, writes refused with no token stored or a wrong one, the +site's identity, and the wrangler config's routes, bindings, vars and +wiring. + +### Pins + +`hono` 4.11.1, `react` and `react-dom` 19.2.1, `@vitejs/plugin-react` +5.1.1, `@types/react` 19.2.7, `@types/react-dom` 19.2.3 and `typescript` +5.9.3 are upstream's exact versions. `wrangler` is the workspace's +`^4.151.0` (upstream: 4.136.1); `vitest` is the monorepo's `^3.2.4` +(upstream: 4.1.10, for its Workers pool). + +### Left out + +Nothing of the Worker. Web Bot Auth ships off (`ENABLE_WEB_BOT_AUTH: +"false"`), as upstream's (risk AV-D). Upstream's `package.json` +`cloudflare` block (the Deploy to Cloudflare card's metadata) is not +carried; the Orun card is `blueprint.yaml` (AV4). + +## What is proven, and how + +| Claim | Proven by | +|---|---| +| The Worker serves every surface, the API and the dashboard's routes | `tests/web` (upstream's suite); `wrangler dev --local` by hand (AV1) | +| Writes are refused without the token and admitted with it | `tests/web`; `wrangler dev --local` with `.dev.vars` by hand (AV1) | +| The config binds `VISIBILITY_CACHE`, `AI` and `ASSETS` | `wrangler deploy --dry-run` (AV1); `tests/web/src/config.test.ts` | +| The KV root is valid and publishes `app_kv_id` | `terraform validate` (AV2) | +| A deploy on a linked workspace | Not yet: gated on a linked workspace | diff --git a/specs/epics/agent-visibility/README.md b/specs/epics/agent-visibility/README.md index 6173f9d..a0026b1 100644 --- a/specs/epics/agent-visibility/README.md +++ b/specs/epics/agent-visibility/README.md @@ -8,7 +8,7 @@ The port keeps the Worker, its surfaces, its enrichment (and its deterministic f | Field | Value | |-------|-------| -| Status | In progress — AV0 (the spec) | +| Status | In progress — AV0–AV3 shipped; AV4 (the baseline) next | | Cluster | **AV** (AV0–AV4) | | Owner(s) | `apps/web` (`src/worker`, `src/enrichment`, `src/lib`, `src/react-app`, `/api/health`, the wrangler template) · `tests/web` · `infra/terraform/cloudflare-kv` | | Builds on | `edge-middleware-recipes` (a Hono Worker on Orun with its optional secrets as runtime secrets); `kv-redis-starter` and `ai-artifacts` (the Cirrus KV root `cloudflare-kv`, its wiring render, the keyless `AI` binding and its one-landing bootstrap); `r2-explorer` (optional Orun secrets handed to a Static Assets Worker as Worker secrets) |