| 1 | # Community Assistant Agent |
| 2 | |
| 3 | The community assistant is a small set of Cloudflare Cron Triggers: the weekly digest drafter (`deepseek-flash`), the facts-drift refresher and the content watchers. Triage, PR-review, stale, duplicate and Today's Dispatch tasks were retired (§132.4); scheduled website upkeep now runs as pull requests from `.github/workflows/web-maintainer.yml` (see `maintainer/README.md`). **The Worker never posts to GitHub directly.** Every drafted output is staged in Workers KV for maintainer review. |
| 4 | |
| 5 | ## Architecture |
| 6 | |
| 7 | ``` |
| 8 | Cloudflare Cron Triggers |
| 9 | └─ worker.ts scheduled() handler |
| 10 | ├─ daily → linkcheck + semantic-drift watchers |
| 11 | ├─ weekly → digest (Mon 09:00 UTC) |
| 12 | └─ 6h → facts-drift |
| 13 | |
| 14 | Drafts stored in Workers KV (keys always derived via `draftStorageKey` in `lib/community-agent.ts`): |
| 15 | draft:digest:<year>-W<week> |
| 16 | draft:linkcheck:<slug>-<sha256-16> (identity: full broken URL) |
| 17 | draft:semantic-drift:<slug>-<sha256-16> (identity: page+claim+evidence+replacement) |
| 18 | |
| 19 | Watcher draft IDs are deterministic — readable slug plus a 64-bit SHA-256 |
| 20 | suffix over the finding's full identity, max 80 chars — so unchanged findings |
| 21 | dedup and changed findings land as new drafts. Semantic-drift model output is |
| 22 | validated and capped (10 drafts/run) before any KV writes. |
| 23 | |
| 24 | Before sending a GitHub post, the action records its target/text identity in the |
| 25 | existing durable claim object and KV. Read or write failures post nothing. |
| 26 | The durable receipt survives lease expiry; retries with changed text or target |
| 27 | are refused while unresolved. Reconciliation searches up to ten pages under |
| 28 | one 30-second deadline and a per-page byte cap; an incomplete search posts |
| 29 | nothing. The KV fallback has eventual-consistency limits; deployment of the |
| 30 | existing durable binding still needs its own receipt. |
| 31 | |
| 32 | A post whose GitHub outcome was unknown (network error, 5xx, 408, 429) leaves |
| 33 | draft-post-unknown:<type>:<id> (until resolved; the next post of that |
| 34 | draft first looks on GitHub for |
| 35 | the earlier attempt's post) |
| 36 | |
| 37 | Usage logged to (one record per model call; sum the day's prefix): |
| 38 | usage:<YYYY-MM-DD>:<timestamp>:<uuid> |
| 39 | ``` |
| 40 | |
| 41 | ## Cron schedule |
| 42 | |
| 43 | | Expression | Frequency | Tasks | |
| 44 | |---|---|---| |
| 45 | | `0 */6 * * *` | Every 6 hours | Facts drift | |
| 46 | | `0 0 * * *` | Daily 00:00 UTC | Link check + semantic drift | |
| 47 | | `0 9 * * 1` | Monday 09:00 UTC | Weekly digest | |
| 48 | |
| 49 | ## Voice constraints |
| 50 | |
| 51 | All drafts follow these rules: |
| 52 | |
| 53 | - Calm, factual, never breathless. |
| 54 | - Never uses first person plural ("we"/"我们") — the maintainer is one person. |
| 55 | - Never commits to timing, prioritisation, or merge intent. |
| 56 | - Never apologises on the maintainer's behalf. |
| 57 | - Cites specific files / line numbers / linked issues when discussing code. |
| 58 | - Ends with: "— drafted by community assistant, pending maintainer review" |
| 59 | - Chinese drafts end with: "— 由社区助理草拟,待维护者审阅" |
| 60 | - Chinese output is rewritten in zh-CN, not machine-translated. |
| 61 | |
| 62 | ## Cost guardrails |
| 63 | |
| 64 | - Each cron invocation caps at ~30k input tokens and ~2k output tokens. |
| 65 | - Issue/PR bodies are truncated to 1000–4000 chars before sending to the model. |
| 66 | - Deduplication: the existing `DRAFT_CLAIM_LOCK` authority serializes each cron task before source reads or model calls (45-minute crash lease, then a two-minute propagation hold). Missing KV or lock bindings skip generation before spending. Inside the claim, `hasFreshDraft` skips drafts newer than the item's `updated_at`. |
| 67 | - Token usage is logged as one `usage:<YYYY-MM-DD>:…` KV record per model call |
| 68 | (retained 90 days); list the day's prefix and sum `calls`/`inputTokens`/ |
| 69 | `outputTokens`. Records are append-only because KV has no atomic increment. |
| 70 | - If `DEEPSEEK_API_KEY` is missing or the API errors, the cron returns 200 with `{ skipped: true, reason }` — never crashes, never retry-loops. |
| 71 | |
| 72 | ## Maintainer review surface |
| 73 | |
| 74 | Open `/en/admin` (or `/zh/admin`) and enter `MAINTAINER_TOKEN` in the login |
| 75 | form. The form posts to `/api/admin/login`; the token never goes in the URL. |
| 76 | |
| 77 | - Lists all pending drafts with source link, draft body, and three actions: |
| 78 | - **Post as comment** — calls GitHub REST API using `MAINTAINER_GITHUB_PAT` |
| 79 | - **Edit & post** — opens a textarea for editing before posting |
| 80 | - **Discard** — removes the draft from KV |
| 81 | - The auth token is set via `MAINTAINER_TOKEN` env var. A successful login sets an httpOnly `mt_sid` session cookie that lasts 24 hours. |
| 82 | - **Nothing posts to GitHub without an explicit maintainer click.** |
| 83 | |
| 84 | ## Environment variables |
| 85 | |
| 86 | | Variable | Required | Purpose | |
| 87 | |---|---|---| |
| 88 | | `DEEPSEEK_API_KEY` | Yes | DeepSeek API key for the community agent | |
| 89 | | `GITHUB_TOKEN` | Optional | Fine-grained PAT for GitHub API (raises rate limit) | |
| 90 | | `CRON_SECRET` | Optional | Shared secret for manual cron invocation (32 to 512 characters, at least 8 distinct, and a shorter value repeated is refused; otherwise the trigger stays disabled); attempts are rate limited (5 per minute per location) | |
| 91 | | `MAINTAINER_TOKEN` | Optional | Auth token for /admin panel; 32 to 512 characters with at least 8 distinct, and a shorter value repeated is refused (otherwise sign-in stays disabled); changing it signs out open sessions | |
| 92 | | `MAINTAINER_GITHUB_PAT` | Optional | GitHub PAT with `issues:write` scope for posting comments | |
| 93 | |
| 94 | ## Initial deployment |
| 95 | |
| 96 | One-time setup before the first `npm run deploy`: |
| 97 | |
| 98 | 1. **Create the KV namespaces:** |
| 99 | ```bash |
| 100 | npx wrangler kv namespace create CURATED_KV |
| 101 | npx wrangler kv namespace create NEXT_INC_CACHE_KV |
| 102 | ``` |
| 103 | Copy the returned `id` values into the matching `CURATED_KV` and |
| 104 | `NEXT_INC_CACHE_KV` bindings in `cloudflare.config.ts`. |
| 105 | |
| 106 | 2. **Set secrets:** generate `MAINTAINER_TOKEN` and `CRON_SECRET` with |
| 107 | `openssl rand -base64 33` (any random value of 32 to 512 characters with at |
| 108 | least 8 distinct and not a shorter value repeated works; `openssl rand -hex 32` is fine). |
| 109 | ```bash |
| 110 | npx wrangler secret put DEEPSEEK_API_KEY |
| 111 | npx wrangler secret put MAINTAINER_TOKEN |
| 112 | npx wrangler secret put MAINTAINER_GITHUB_PAT |
| 113 | npx wrangler secret put CRON_SECRET |
| 114 | ``` |
| 115 | |
| 116 | 3. **(Optional) Raise GitHub rate limit:** |
| 117 | ```bash |
| 118 | npx wrangler secret put GITHUB_TOKEN |
| 119 | ``` |
| 120 | |
| 121 | 4. **Verify:** |
| 122 | ```bash |
| 123 | npm run predeploy # checks KV ID is set |
| 124 | npm run deploy # builds + deploys |
| 125 | ``` |
| 126 | |
| 127 | ## Kill switch |
| 128 | |
| 129 | To disable the community agent entirely: |
| 130 | |
| 131 | 1. Remove all cron triggers from `cloudflare.config.ts` except the `0 */6 * * *` facts-drift cron. |
| 132 | 2. Redeploy: `npm run deploy`. |
| 133 | |
| 134 | Facts drift continues working independently. Individual tasks remain callable manually for testing through `/api/cron?task=digest`, `/api/cron?task=linkcheck`, etc. |
| 135 | |
| 136 | To disable a specific cron task, remove its cron expression from `cloudflare.config.ts` and redeploy. |
| 137 | |
| 138 | ## Bilingual output |
| 139 | |
| 140 | Every draft contains both `bodyEn` (English) and `bodyZh` (Chinese zh-CN). The admin panel shows the version matching the current locale. The zh version is rewritten natively by the model, not translated from English. |
| 141 |