mirror of
https://github.com/block/buzz.git
synced 2026-08-18 06:50:31 +02:00
Merge origin/main into tho/singular-create-persona-template
Reconcile the persona-template UX feature with 60 commits of main.
Three conflicts resolved:
- ManagedAgentRow.tsx: took main's version. Main deleted the per-row
AgentActionsMenu entirely and consolidated agent actions into
UserProfileAgentActions.tsx (UserProfileAgentSettingsMenu). The
original Part 2 row-menu target no longer exists.
- UserProfilePanel.tsx / UserProfilePanelSections.tsx: took main's
reworked versions, then re-applied the save-as action on top.
Part 2 re-home (decision (a), flagged to Ned): the "Save as persona
template" action now lives in main's UserProfileAgentSettingsMenu,
reusing main's persona-dialog infra (setPersonaDialogState +
saveAsPersonaTemplateDialogState + submitProfilePersonaDialog). This
makes useSaveAsPersonaTemplate.ts dead code (its only consumers were
the deleted row-menu and the stale profile wiring) -- deleted.
Provider source stays backend.id via providerForAgentBackend (Ned
confirmed: top-level agent.provider is a derived snapshot/output,
backend.id is the canonical create input). Stale comments claiming
"ManagedAgent has no top-level provider on main" corrected; logic
unchanged.
File-size overrides bumped: lib.rs 1034->1035 (main grew it via
ca50d832 #1246 without bumping); UserProfilePanel.tsx 1000->1014 (the
~15-line save-as re-home on an edge-riding panel).
Gate green on the merged tree: pnpm typecheck, biome, check:file-sizes,
check:px-text, 1194 unit tests.
Co-authored-by: Taylor Ho <taylorkmho@gmail.com>
Signed-off-by: Taylor Ho <taylorkmho@gmail.com>
This commit is contained in:
co-authored by
Taylor Ho
commit
2ffed51ec5
@@ -520,6 +520,35 @@ jobs:
|
||||
path: /tmp/buzz-relay.log
|
||||
if-no-files-found: ignore
|
||||
|
||||
relay-e2e:
|
||||
name: Relay E2E
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 20
|
||||
needs: [changes]
|
||||
if: github.event_name == 'push' || needs.changes.outputs.rust == 'true'
|
||||
permissions:
|
||||
contents: read
|
||||
steps:
|
||||
- uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6
|
||||
- uses: cashapp/activate-hermit@e49f5cb4dd64ff0b0b659d1d8df499595451155a # v1
|
||||
- uses: Swatinem/rust-cache@e18b497796c12c097a38f9edb9d0641fb99eee32 # v2
|
||||
with:
|
||||
save-if: ${{ github.event_name != 'pull_request' }}
|
||||
- name: Start relay
|
||||
run: ./scripts/start-relay-for-tests.sh
|
||||
- name: Relay E2E tests
|
||||
run: cargo test -p buzz-test-client --test e2e_persona --test e2e_nostr_interop -- --ignored --nocapture
|
||||
env:
|
||||
RELAY_URL: ws://localhost:3000
|
||||
GIT_CREDENTIAL_NOSTR_BIN: ${{ github.workspace }}/target/ci/git-credential-nostr
|
||||
- name: Upload relay logs
|
||||
if: failure()
|
||||
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7
|
||||
with:
|
||||
name: relay-e2e-artifacts
|
||||
path: /tmp/buzz-relay.log
|
||||
if-no-files-found: ignore
|
||||
|
||||
web:
|
||||
name: Web
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
@@ -218,12 +218,17 @@ Desktop E2E: `cd desktop && pnpm exec playwright test`
|
||||
|
||||
See [TESTING.md](TESTING.md) for the full multi-agent E2E guide.
|
||||
|
||||
### Desktop Screenshots (Playwright)
|
||||
### PR Screenshots
|
||||
|
||||
> **Do NOT use `buzz upload`, the relay media endpoint, or any third-party
|
||||
> image host for PR screenshots.** Relay media URLs fail through GitHub's camo
|
||||
> proxy. Always use `scripts/post-screenshots.sh` — see the `desktop-screenshot`
|
||||
> skill for the full workflow.
|
||||
> proxy. Always use `scripts/post-screenshots.sh` for PNGs before linking them
|
||||
> from a PR body/comment. If you hand-edit PR markdown, run
|
||||
> `scripts/check-pr-image-urls.sh <markdown-file>` first to catch relay URLs.
|
||||
|
||||
For mobile simulator screenshots, save the PNGs in a local directory and run
|
||||
`./scripts/post-screenshots.sh <PR-number> <png-dir>` or use the third argument
|
||||
with a markdown template containing `{{filename}}` placeholders.
|
||||
|
||||
The desktop app requires the E2E mock bridge to render — it cannot run in a plain
|
||||
browser. Use `just desktop-screenshot` to capture screenshots (builds frontend,
|
||||
|
||||
+35
-14
@@ -6,6 +6,14 @@ Buzz is a self-hosted team communication platform built on the Nostr protocol (N
|
||||
|
||||
The relay is the single source of truth. All reads and writes flow through it. There is no peer-to-peer event exchange, no gossip, no replication — just clients connecting to one relay over WebSocket, and the relay enforcing auth, verifying signatures, persisting events, fanning out to subscribers, indexing for search, and triggering automation.
|
||||
|
||||
A Buzz **community** is the tenant-visible workspace selected by the request host.
|
||||
The self-hosted default remains one host, one relay process, one implicit
|
||||
community. Multi-community deployments move that semantic boundary one level up:
|
||||
`req.community = resolve_host(connection.host)` is established before AUTH,
|
||||
EVENT, REQ, REST, media, git, search, workflow, or pub/sub handling. Unknown
|
||||
hosts fail closed, and NIP-98/API-token stamps must agree with the host-derived
|
||||
community rather than overriding it.
|
||||
|
||||
Buzz is a Rust monorepo, licensed Apache 2.0 under Block, Inc.
|
||||
|
||||
---
|
||||
@@ -87,7 +95,7 @@ buzz-admin (operator CLI: relay membership + key generation)
|
||||
buzz-test-client (integration test harness + manual CLI)
|
||||
```
|
||||
|
||||
**Key architectural principle:** The relay is the single source of truth. `buzz-relay` orchestrates all subsystems by calling them directly — it imports `buzz-db`, `buzz-auth`, `buzz-pubsub`, `buzz-search`, `buzz-audit`, and `buzz-workflow`. However, those subsystems are isolated from each other: `buzz-workflow` never calls `buzz-pubsub`, `buzz-search` never calls `buzz-db`, etc. Cross-subsystem coordination happens only through the relay. `buzz-proxy` connects to the relay as a WebSocket client and translates NIP-28 events between standard Nostr clients and the Buzz relay.
|
||||
**Key architectural principle:** The relay is the single source of truth. `buzz-relay` orchestrates all subsystems by calling them directly — it imports `buzz-db`, `buzz-auth`, `buzz-pubsub`, `buzz-search`, `buzz-audit`, and `buzz-workflow`. However, those subsystems are isolated from each other: `buzz-workflow` never calls `buzz-pubsub`, `buzz-search` never calls `buzz-db`, etc. Cross-subsystem coordination happens only through the relay. `buzz-proxy` connects to the relay as a WebSocket client and translates NIP-28 events between standard Nostr clients and the Buzz relay. In multi-community mode, the relay also owns propagation of `TenantContext`; service crates should receive community-scoped inputs rather than independently deriving tenancy from client-controlled event tags.
|
||||
|
||||
---
|
||||
|
||||
@@ -159,6 +167,16 @@ Max frame size: 65,536 bytes. Max subscriptions per connection: 1024. Max histor
|
||||
|
||||
Every WebSocket connection follows this exact sequence:
|
||||
|
||||
### Step 0: Community Binding
|
||||
|
||||
The server resolves `TenantContext` from the request host before any handler can
|
||||
observe tenant data. The URL/domain is authoritative for the community, matching
|
||||
today's "the relay URL is the workspace" behavior. In single-community mode the
|
||||
configured host maps to the default community. In multi-community mode, an
|
||||
unknown or unmapped host rejects generically and never falls through to a default
|
||||
tenant. Client-supplied `#h` tags are still channel identifiers; they must resolve
|
||||
to a channel inside the host-derived community.
|
||||
|
||||
### Step 1: Semaphore Acquire
|
||||
|
||||
`state.conn_semaphore.try_acquire_owned()` — if the relay is at connection capacity, the connection is rejected immediately before any data is read. The permit is held for the entire connection lifetime and dropped on cleanup.
|
||||
@@ -414,7 +432,7 @@ All database access. Uses `sqlx::query()` (runtime, not compile-time macros) —
|
||||
|
||||
### buzz-pubsub — Redis Pub/Sub, Presence, Typing
|
||||
|
||||
Manages Redis pub/sub fan-out, presence tracking, and typing indicators.
|
||||
Manages Redis pub/sub fan-out, presence tracking, and typing indicators. In multi-community mode all tenant-visible keys are prefixed or otherwise partitioned by community (`buzz:{community}:...`) so channel fan-out, presence, typing, and cache invalidation cannot cross hosts.
|
||||
|
||||
**Architecture:**
|
||||
|
||||
@@ -446,7 +464,7 @@ EXPIRE buzz:typing:{channel_id} 60
|
||||
|
||||
### buzz-search — Typesense Integration
|
||||
|
||||
Full-text search via Typesense. All HTTP calls use `reqwest` with `X-TYPESENSE-API-KEY`.
|
||||
Full-text search via Typesense. All HTTP calls use `reqwest` with `X-TYPESENSE-API-KEY`. In multi-community mode, indexed documents and every query filter include `community_id`; the shared Typesense collection is infrastructure, not a cross-community result space.
|
||||
|
||||
**Collection schema (7 fields):** `id`, `content`, `kind` (int32), `pubkey` (facet), `channel_id` (facet, optional), `created_at` (int64, default sort), `tags_flat` (string[]).
|
||||
|
||||
@@ -466,7 +484,7 @@ Full-text search via Typesense. All HTTP calls use `reqwest` with `X-TYPESENSE-A
|
||||
|
||||
Tamper-evident append-only log with SHA-256 hash chaining.
|
||||
|
||||
**Hash chain:** each entry stores `prev_hash` (hash of the previous entry). `verify_chain()` walks entries and recomputes hashes to detect tampering. Genesis entry uses `GENESIS_HASH` (64 zeros).
|
||||
**Hash chain:** each entry stores `prev_hash` (hash of the previous entry). In multi-community mode audit heads/chains are per-community; operator metrics may aggregate, but tenant-readable audit verification walks one community chain. `verify_chain()` walks entries and recomputes hashes to detect tampering. Genesis entry uses `GENESIS_HASH` (64 zeros).
|
||||
|
||||
**Hash covers:** seq (big-endian bytes), timestamp (RFC3339), event_id, event_kind (big-endian), actor_pubkey, action string, channel_id (16 bytes or 16 zero bytes if None), canonical metadata JSON (BTreeMap for deterministic key ordering), prev_hash.
|
||||
|
||||
@@ -480,7 +498,7 @@ Tamper-evident append-only log with SHA-256 hash chaining.
|
||||
|
||||
### buzz-workflow — YAML-as-Code Automation Engine
|
||||
|
||||
Parses, validates, and executes channel-scoped workflow definitions.
|
||||
Parses, validates, and executes channel-scoped workflow definitions. In multi-community mode workflow definitions, runs, approvals, webhook routes, and schedules inherit the host-derived community and evaluate triggers only against events in that community.
|
||||
|
||||
**Workflow definition structure:**
|
||||
```yaml
|
||||
@@ -718,11 +736,14 @@ Subcommands:
|
||||
|
||||
| Subcommand | Purpose |
|
||||
|------------|---------|
|
||||
| `add-member` | Add a pubkey to the relay membership list (`--pubkey`, `--role`) |
|
||||
| `add-member` | Add a pubkey to the relay membership list (`--pubkey`, `--role`); accepts npub or hex; publishes kind:13534 roster |
|
||||
| `remove-member` | Remove a pubkey from the relay membership list (`--pubkey`, optional `--role` guard); publishes kind:13534 roster |
|
||||
| `list-members` | List all relay members |
|
||||
| `generate-key` | Generate a new Nostr keypair (for bootstrapping) |
|
||||
| `reconcile-channels` | Emit kind:39000/39002 discovery events for channels missing them (idempotent) |
|
||||
|
||||
The `buzz-admin` binary is shipped in the relay Docker image (`/usr/local/bin/buzz-admin`) and is the recommended way to manage relay membership in production. Use `./run.sh add-member`, `./run.sh remove-member`, and `./run.sh list-members` in Docker Compose deployments.
|
||||
|
||||
---
|
||||
|
||||
### buzz-test-client — Integration Test Harness
|
||||
@@ -821,26 +842,26 @@ Docker Compose provides the full local development stack. All services include h
|
||||
|
||||
| Table | Purpose |
|
||||
|-------|---------|
|
||||
| `events` | All stored Nostr events; monthly range-partitioned by `PARTITION BY RANGE` on `created_at` |
|
||||
| `channels` | Channel records (type, visibility, canvas, topic) |
|
||||
| `events` | All stored Nostr events; monthly range-partitioned by `PARTITION BY RANGE` on `created_at`; multi-community mode keys every tenant-visible event by `community_id` |
|
||||
| `channels` | Channel records (type, visibility, canvas, topic); `community_id` is immutable after creation in multi-community mode |
|
||||
| `channel_members` | Membership with roles; soft-delete via `removed_at` |
|
||||
| `workflows` | Workflow definitions (YAML stored as canonical JSON) |
|
||||
| `workflows` | Workflow definitions (YAML stored as canonical JSON); scoped by community in multi-community mode |
|
||||
| `workflow_runs` | Execution records with trigger context and trace |
|
||||
| `workflow_approvals` | Approval gates (token stored as SHA-256 hash) |
|
||||
| `audit_log` | Hash-chain audit entries |
|
||||
| `audit_log` | Hash-chain audit entries; per-community chain/head in multi-community mode |
|
||||
| `delivery_log` | Delivery tracking (partitioned; Rust module pending) |
|
||||
|
||||
### Redis Key Patterns
|
||||
|
||||
| Pattern | Type | TTL | Purpose |
|
||||
|---------|------|-----|---------|
|
||||
| `buzz:channel:{uuid}` | Pub/Sub channel | — | Event fan-out |
|
||||
| `buzz:presence:{pubkey_hex}` | String | 90s | Online/away status |
|
||||
| `buzz:typing:{channel_uuid}` | Sorted Set | 60s | Active typers (5s window) |
|
||||
| `buzz:channel:{uuid}` | Pub/Sub channel | — | Event fan-out (single-community form; shared multi-community Redis must use `buzz:{community}:channel:{uuid}` or equivalent) |
|
||||
| `buzz:presence:{pubkey_hex}` | String | 90s | Online/away status (single-community form; shared multi-community Redis must scope by community) |
|
||||
| `buzz:typing:{channel_uuid}` | Sorted Set | 60s | Active typers (5s window; shared multi-community Redis must scope by community) |
|
||||
|
||||
### Typesense Collection
|
||||
|
||||
Single collection (`events` by default, configurable via `TYPESENSE_COLLECTION`). Schema: `id`, `content`, `kind` (int32), `pubkey` (facet), `channel_id` (facet, optional), `created_at` (int64, default sort), `tags_flat` (string[]).
|
||||
Single collection (`events` by default, configurable via `TYPESENSE_COLLECTION`). Schema today: `id`, `content`, `kind` (int32), `pubkey` (facet), `channel_id` (facet, optional), `created_at` (int64, default sort), `tags_flat` (string[]). Multi-community mode adds faceted `community_id` and either prefixes document IDs with community or makes all upsert/delete/refetch paths carry community context.
|
||||
|
||||
---
|
||||
|
||||
|
||||
@@ -1,5 +1,59 @@
|
||||
# Changelog
|
||||
|
||||
## v0.3.34
|
||||
|
||||
- feat(desktop): refresh Agents tab live on inbound relay sync ([#1256](https://github.com/block/buzz/pull/1256)) ([`9a59e308`](https://github.com/block/buzz/commit/9a59e30817c642b1e6aab296863570869e215a19))
|
||||
- fix(buzz-acp): inject Codex network allowlist for relay hostname at spawn time ([#1287](https://github.com/block/buzz/pull/1287)) ([`0379247e`](https://github.com/block/buzz/commit/0379247eba8a6c7e48a94f3fb8a329e7bdbca13c))
|
||||
- refactor(desktop): consolidate notification helpers and add channel names to toasts ([#1286](https://github.com/block/buzz/pull/1286)) ([`dd5592e3`](https://github.com/block/buzz/commit/dd5592e34fcd7690340633dcec368db485557294))
|
||||
- feat(buzz-agent): lazy skill loading via load_skill tool ([#1283](https://github.com/block/buzz/pull/1283)) ([`25396c06`](https://github.com/block/buzz/commit/25396c06d65c5e9a6d1271559cde4230e06ff35d))
|
||||
- fix(buzz-acp): human-aware reply anchoring to keep threads flat ([#1281](https://github.com/block/buzz/pull/1281)) ([`6c920d21`](https://github.com/block/buzz/commit/6c920d21a852d301effb8a667d824e6b96485a7d))
|
||||
- feat(desktop): add 'Follow system' theme mode ([#1262](https://github.com/block/buzz/pull/1262)) ([`c996be39`](https://github.com/block/buzz/commit/c996be395e23e50e5097f088738b3de9176759d4))
|
||||
- Fix mention autocomplete layout in narrow threads ([#1282](https://github.com/block/buzz/pull/1282)) ([`227518c2`](https://github.com/block/buzz/commit/227518c234175440f9cd3734d01b96bbbbc89065))
|
||||
- Clean up screenshot e2e tests ([#1284](https://github.com/block/buzz/pull/1284)) ([`2499d339`](https://github.com/block/buzz/commit/2499d33971524774f07f5186dadc8b33e055d812))
|
||||
- fix(desktop): DM close button replaces unread badge on hover ([#1280](https://github.com/block/buzz/pull/1280)) ([`e3663721`](https://github.com/block/buzz/commit/e3663721830d79df6562a0b3ef78c8fe6bc1b7c3))
|
||||
- Add NIP-34 git pull request CLI support ([#1279](https://github.com/block/buzz/pull/1279)) ([`5fef9b72`](https://github.com/block/buzz/commit/5fef9b727fb8856d45840ccc97d8ec820a48f6e0))
|
||||
- fix(desktop): preserve timeline scroll when opening threads ([#1278](https://github.com/block/buzz/pull/1278)) ([`d05f122d`](https://github.com/block/buzz/commit/d05f122d8acf49e924fe11992dde4bde41dee3f7))
|
||||
- chore: remove LLM-slop comments across the codebase ([#1277](https://github.com/block/buzz/pull/1277)) ([`73cc31cc`](https://github.com/block/buzz/commit/73cc31cc528318debedd38e85d67b79d1feb55e8))
|
||||
- fix(desktop): allow saving personas with an empty system prompt ([#1276](https://github.com/block/buzz/pull/1276)) ([`996a3f89`](https://github.com/block/buzz/commit/996a3f89d5f3835009236782f2b3a93409b090cf))
|
||||
|
||||
|
||||
## v0.3.33
|
||||
|
||||
- fix(desktop): always use legacy keyring for blob entry on macOS ([#1271](https://github.com/block/buzz/pull/1271)) ([`e7c3638fe`](https://github.com/block/buzz/commit/e7c3638fe984e7785a6e51e547b5844e894ed013))
|
||||
- chore(release): release Buzz Relay version 0.1.1 ([#1269](https://github.com/block/buzz/pull/1269)) ([`68a0cc850`](https://github.com/block/buzz/commit/68a0cc8506be4ea1fb110b65eec0787e4cd84378))
|
||||
- perf(desktop): consolidate keychain secrets into a single blob entry ([#1267](https://github.com/block/buzz/pull/1267)) ([`fa942cb51`](https://github.com/block/buzz/commit/fa942cb51d0714cba54747bcc7197c14e770c338))
|
||||
- feat(desktop): re-snapshot persona config on every agent spawn ([#1268](https://github.com/block/buzz/pull/1268)) ([`048e8fdc0`](https://github.com/block/buzz/commit/048e8fdc004322e5feccc4fd76c8fc0dc87d91f0))
|
||||
- feat(relay): add buzz-admin member management CLI with NIP-43 roster publish ([#1265](https://github.com/block/buzz/pull/1265)) ([`0cee0435f`](https://github.com/block/buzz/commit/0cee0435f7f955a42d7824f1967e4f4fc6a02f74))
|
||||
- fix(desktop): fall back to old keychain when DPK unavailable (unsigned builds) ([#1266](https://github.com/block/buzz/pull/1266)) ([`958ac7aaa`](https://github.com/block/buzz/commit/958ac7aaaae90da76c864af744242f29ee4f08de))
|
||||
- Align channel management panel with profile ([#1066](https://github.com/block/buzz/pull/1066)) ([`9f35b0188`](https://github.com/block/buzz/commit/9f35b018803f56b2330bfb79de4ab19f18d80256))
|
||||
- fix(relay): multi-pod subscription coherence (one access-gated fan-out path + cross-pod cache invalidation + REQ/COUNT DB guard) ([#1261](https://github.com/block/buzz/pull/1261)) ([`628445429`](https://github.com/block/buzz/commit/6284454298cb5be72cefbeaf13abed04afe3cc9e))
|
||||
- fix(desktop): switch macOS keychain to Data Protection Keychain ([#1264](https://github.com/block/buzz/pull/1264)) ([`35522311a`](https://github.com/block/buzz/commit/35522311a963b54f32df864d338ab7d75102247f))
|
||||
- fix(desktop): use IPv4 loopback for media proxy URLs ([#1245](https://github.com/block/buzz/pull/1245)) ([`cee2c5f26`](https://github.com/block/buzz/commit/cee2c5f2634ae7d15262a2ec07cccacc8460cd0e))
|
||||
- perf(relay/git): stream read-path, manifest info/refs fast path, idx sidecar (A/B/C) ([#1240](https://github.com/block/buzz/pull/1240)) ([`856815994`](https://github.com/block/buzz/commit/856815994c3441268eb48d2264aeb3492b701fe5))
|
||||
|
||||
|
||||
## v0.3.32
|
||||
|
||||
- fix(desktop): restore solid dot for top-level channel unreads ([#1253](https://github.com/block/buzz/pull/1253)) ([`9d3bfd38b`](https://github.com/block/buzz/commit/9d3bfd38b0876cd1bdf5bb90d6639f200ceb12e0))
|
||||
- Allow explicit root posts from reply prompts ([#1251](https://github.com/block/buzz/pull/1251)) ([`ebd74d11b`](https://github.com/block/buzz/commit/ebd74d11b4e77195fa94158205787e6e5edca3ff))
|
||||
- feat: publish personas, teams, and managed agents as relay events ([#939](https://github.com/block/buzz/pull/939)) ([`d256935c3`](https://github.com/block/buzz/commit/d256935c3620f30ae54e9d02e48adc6ee0bc578f))
|
||||
- fix(observability): gate agent observability on declared ownership, not key custody ([#1229](https://github.com/block/buzz/pull/1229)) ([`e993b9e74`](https://github.com/block/buzz/commit/e993b9e741649f73c24f9afbb01420cffc822e5a))
|
||||
- fix(desktop): split AppShell notification effects ([#1248](https://github.com/block/buzz/pull/1248)) ([`a9ce477a0`](https://github.com/block/buzz/commit/a9ce477a041c7f201b1f7c0b79c68d7b0aa3f38e))
|
||||
- feat(desktop): store nsec private keys in the OS keyring ([#1172](https://github.com/block/buzz/pull/1172)) ([`4be43d708`](https://github.com/block/buzz/commit/4be43d7086753e843884f5f541c3a8cbf1512fff))
|
||||
- fix: add evict-completed-work prompt rule and unblock desktop file-size gate ([#1247](https://github.com/block/buzz/pull/1247)) ([`048228889`](https://github.com/block/buzz/commit/048228889122160ce73f9e32aba36961613e984e))
|
||||
- fix(desktop): scope mention/add autocomplete to reachable identities ([#1243](https://github.com/block/buzz/pull/1243)) ([`a3e8fb6fd`](https://github.com/block/buzz/commit/a3e8fb6fd512f34d53b7ed36e68ab7f8bfa01291))
|
||||
- test(e2e): raise scroll-history pagination test timeout to 90s ([#1234](https://github.com/block/buzz/pull/1234)) ([`09dbeb86f`](https://github.com/block/buzz/commit/09dbeb86fa8b6f430c79adf7c585194e2ec0a3d0))
|
||||
- Polish thread and media layout ([#1239](https://github.com/block/buzz/pull/1239)) ([`b264a32cf`](https://github.com/block/buzz/commit/b264a32cfbdc8481e9dd4ceadbb05990bd7af02a))
|
||||
- Polish desktop navigation chrome ([#1238](https://github.com/block/buzz/pull/1238)) ([`092546495`](https://github.com/block/buzz/commit/092546495d73d305e4d0226ee14bf0d5de4d2f53))
|
||||
- fix: propagate persona harness edits to live agent instances ([#1244](https://github.com/block/buzz/pull/1244)) ([`2e426b2fd`](https://github.com/block/buzz/commit/2e426b2fddbbec1f1261125c81d83cd57e838fd1))
|
||||
- fix(desktop): render autolinked message links as chips ([#1241](https://github.com/block/buzz/pull/1241)) ([`45067ec13`](https://github.com/block/buzz/commit/45067ec13507c406ee41d3f184fc96c988b1348b))
|
||||
- fix(desktop): clear unread projections from message read markers ([#1242](https://github.com/block/buzz/pull/1242)) ([`733c766eb`](https://github.com/block/buzz/commit/733c766eba90733a00dc397dd2ab9706935e5183))
|
||||
- fix(desktop): resolve repos_dir symlink at boot before agent restore ([#1231](https://github.com/block/buzz/pull/1231)) ([`86d7748eb`](https://github.com/block/buzz/commit/86d7748ebd93027754bfd2326958c4324aa817d6))
|
||||
- Fix presence fan-out across relay pods ([#1227](https://github.com/block/buzz/pull/1227)) ([`36d3d2ed1`](https://github.com/block/buzz/commit/36d3d2ed1e57e399d5221edfd109082fd8a4d73e))
|
||||
- fix(relay): raise grace limit, add replay backpressure, and NOTICE on oversized frames ([#1226](https://github.com/block/buzz/pull/1226)) ([`142a5c909`](https://github.com/block/buzz/commit/142a5c909542f1e1d2119ca4562129d492aca94c))
|
||||
- [codex] Make relay frame limit configurable ([#1225](https://github.com/block/buzz/pull/1225)) ([`38a953343`](https://github.com/block/buzz/commit/38a95334396dae0e271a478b46baa62d29deebff))
|
||||
|
||||
|
||||
## v0.3.31
|
||||
|
||||
- fix(release): publish versioned relay Docker tags via independent release lanes ([#1173](https://github.com/block/buzz/pull/1173)) ([`549b7d24`](https://github.com/block/buzz/commit/549b7d24813320045bdda629d865c6c7418e7450))
|
||||
|
||||
Generated
+10
-1
@@ -738,14 +738,23 @@ name = "buzz-admin"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"buzz-audit",
|
||||
"buzz-auth",
|
||||
"buzz-core",
|
||||
"buzz-db",
|
||||
"buzz-media",
|
||||
"buzz-pubsub",
|
||||
"buzz-search",
|
||||
"buzz-workflow",
|
||||
"clap",
|
||||
"deadpool-redis",
|
||||
"hex",
|
||||
"nostr",
|
||||
"serde_json",
|
||||
"sqlx",
|
||||
"tokio",
|
||||
"tracing",
|
||||
"uuid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -1020,7 +1029,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "buzz-relay"
|
||||
version = "0.1.0"
|
||||
version = "0.1.1"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"axum",
|
||||
|
||||
+4
-1
@@ -44,7 +44,9 @@ COPY --from=planner /build/recipe.json recipe.json
|
||||
RUN cargo chef cook --release --recipe-path recipe.json
|
||||
COPY . .
|
||||
RUN cargo build --release --locked -p buzz-relay --bin buzz-relay \
|
||||
&& strip target/release/buzz-relay
|
||||
-p buzz-admin --bin buzz-admin \
|
||||
&& strip target/release/buzz-relay \
|
||||
&& strip target/release/buzz-admin
|
||||
|
||||
# ─── Stage 4: web bundle (pnpm + vite) ──────────────────────────────────────
|
||||
# Independent of the Rust layers so a CSS change doesn't bust Rust cache and
|
||||
@@ -82,6 +84,7 @@ RUN apt-get update \
|
||||
--create-home --shell /usr/sbin/nologin buzz
|
||||
|
||||
COPY --from=builder /build/target/release/buzz-relay /usr/local/bin/buzz-relay
|
||||
COPY --from=builder /build/target/release/buzz-admin /usr/local/bin/buzz-admin
|
||||
COPY --from=web-builder /build/web/dist /srv/buzz/web
|
||||
|
||||
ENV BUZZ_WEB_DIR=/srv/buzz/web
|
||||
|
||||
@@ -14,6 +14,23 @@ clients and don't have company credentials.
|
||||
|
||||
Both paths require NIP-42 authentication.
|
||||
|
||||
## Community scope
|
||||
|
||||
Buzz treats the relay URL/domain as authoritative for the community. Today's
|
||||
single-relay deployment has exactly one community behind that URL, so existing
|
||||
NIP-29/NIP-28 clients keep using the same WebSocket URL, event kinds, tags, and
|
||||
REST/media/git paths. In a multi-community deployment, each community is reached
|
||||
by its own domain or subdomain; the backend resolves the community from the host
|
||||
before handling AUTH, EVENT, REQ, REST, media, git, search, or workflow traffic.
|
||||
|
||||
The Nostr wire format does not grow a tenant tag. Client-supplied `#h` tags still
|
||||
name channels/groups and are checked against the host-derived community. Events
|
||||
without `#h` — profiles, gift-wrapped DMs, membership notifications, lists,
|
||||
status, long-form notes, workflow/system events, and other "global" streams — are
|
||||
global only inside the connected community. A pubkey can join multiple
|
||||
communities and repost its profile in each one; DMs and profiles do not inherit
|
||||
across community domains.
|
||||
|
||||
---
|
||||
|
||||
## Path 1: NIP-29 Direct
|
||||
@@ -55,15 +72,15 @@ PGPASSWORD=buzz_dev psql -h localhost -U buzz -d buzz -c \
|
||||
| **Group metadata (kind:39000)** | ✅ | Relay-signed; always `d`, `name`, `closed` tags; `about` only if description non-empty; `private` if applicable; `hidden` for DM channels |
|
||||
| **Group admins (kind:39001)** | ✅ | Relay-signed; `d` tag + `p` tags with roles (`owner`, `admin`) |
|
||||
| **Group members (kind:39002)** | ✅ | Relay-signed; `d` tag + `p` tags for all members |
|
||||
| **Membership notifications** | ✅ | kind:44100 (added) / kind:44101 (removed); relay-signed, global scope |
|
||||
| **Presence (kind:20001)** | ✅ | Ephemeral; arbitrary status string (truncated to 128 chars); writes to Redis (`set_presence`/`clear_presence` on `"offline"`), then fan-out to local subscribers |
|
||||
| **Membership notifications** | ✅ | kind:44100 (added) / kind:44101 (removed); relay-signed, community-global scope (`channel_id=None` inside the connected community) |
|
||||
| **Presence (kind:20001)** | ✅ | Ephemeral; arbitrary status string (truncated to 128 chars); writes to Redis (`set_presence`/`clear_presence` on `"offline"`), then fan-out to local subscribers. In multi-community mode presence is scoped to the connected community. |
|
||||
| **Typing indicators (kind:20002)** | ✅ | Ephemeral, not stored; published via Redis pub/sub (multi-node capable unlike presence fan-out) |
|
||||
| **NIP-42 authentication** | ✅ | Proactive challenge; optional pubkey allowlist |
|
||||
| **NIP-11 relay info** | ✅ | `GET /` with `Accept: application/nostr+json` |
|
||||
| **Blossom media** | ✅ | `PUT /media/upload` (BUD-02), `GET /media/{sha256}.{ext}` (BUD-01) |
|
||||
| **NIP-50 search** | ✅ | One-shot search REQs: `{"search":"query","kinds":[9],"#h":["<uuid>"]}` → relevance-sorted results → EOSE. Not registered as persistent subscriptions. |
|
||||
| **NIP-10 threads** | ✅ | WS-submitted replies with `["e","<root>","","reply"]` tags create `thread_metadata` atomically. Visible in REST thread queries. Unknown parents rejected. |
|
||||
| **NIP-17 DMs (gift wrap)** | ✅ | kind:1059 accepted with ephemeral signing keys. Stored globally (channel_id=None). Delivered via `#p`-filtered subscriptions. Not indexed in search. |
|
||||
| **NIP-17 DMs (gift wrap)** | ✅ | kind:1059 accepted with ephemeral signing keys. Stored community-globally (`channel_id=None` inside the connected community). Delivered via `#p`-filtered subscriptions. Not indexed in search. |
|
||||
| **DM discovery** | ✅ | DM creation emits kind:39000 (with `hidden` tag) + kind:44100 membership notifications. NIP-29 clients discover DMs via standard group discovery flow. |
|
||||
| **Join request (kind:9021)** | ✅ | Open channels only. Adds member, emits system message + group discovery events + kind:44100 membership notification. Private channels rejected at ingest. |
|
||||
| **Edits (kind:40003)** | ⚠️ | Works on the wire but Buzz-only — no standard NIP-29 client renders these |
|
||||
@@ -126,10 +143,10 @@ The relay emits relay-signed notifications when members are added or removed:
|
||||
|
||||
| Kind | Meaning | Tags | Scope |
|
||||
|------|---------|------|-------|
|
||||
| **44100** | Member added | `p` = target pubkey, `h` = channel UUID | Global |
|
||||
| **44101** | Member removed | `p` = target pubkey, `h` = channel UUID | Global |
|
||||
| **44100** | Member added | `p` = target pubkey, `h` = channel UUID | Community-global |
|
||||
| **44101** | Member removed | `p` = target pubkey, `h` = channel UUID | Community-global |
|
||||
|
||||
Stored globally (`channel_id = None`) so agents and clients can subscribe without knowing channel
|
||||
Stored community-globally (`channel_id = None` inside the connected community) so agents and clients can subscribe without knowing channel
|
||||
UUIDs in advance. Client-submitted kind:44100/44101 events are rejected — only the relay keypair
|
||||
may sign these.
|
||||
|
||||
@@ -471,6 +488,117 @@ is dual-sourced: local snapshot metadata plus upstream edit events (kind:40003
|
||||
|
||||
---
|
||||
|
||||
## Relay Membership (NIP-43)
|
||||
|
||||
When `BUZZ_REQUIRE_RELAY_MEMBERSHIP=true`, every authenticated connection is checked against the
|
||||
`relay_members` table. In today's single-community deployment this is the relay-wide member list; in multi-community mode the same rule is scoped to the host-derived community. Only pubkeys with a row for that community may use that community. The relay owner
|
||||
is bootstrapped automatically from `RELAY_OWNER_PUBKEY` on startup.
|
||||
|
||||
### CLI: Managing Members
|
||||
|
||||
Use `buzz-admin` — the operator CLI shipped in the relay image — to manage relay membership.
|
||||
In a Docker Compose deployment, use `run.sh`:
|
||||
|
||||
```bash
|
||||
# Add a member (accepts bech32 npub or 64-char hex; default role: member)
|
||||
./run.sh add-member npub1abc...
|
||||
./run.sh add-member <64-char-hex-pubkey>
|
||||
./run.sh add-member npub1abc... --role admin
|
||||
|
||||
# Remove a member
|
||||
./run.sh remove-member npub1abc...
|
||||
./run.sh remove-member npub1abc... --role member # only removes if role matches
|
||||
|
||||
# List all members
|
||||
./run.sh list-members
|
||||
```
|
||||
|
||||
Or invoke `buzz-admin` directly inside the container:
|
||||
|
||||
```bash
|
||||
docker compose exec relay buzz-admin add-member --pubkey npub1abc...
|
||||
docker compose exec relay buzz-admin add-member --pubkey npub1abc... --role admin
|
||||
docker compose exec relay buzz-admin remove-member --pubkey npub1abc...
|
||||
docker compose exec relay buzz-admin list-members
|
||||
```
|
||||
|
||||
**Exit codes:**
|
||||
|
||||
| Code | Meaning |
|
||||
|------|---------|
|
||||
| 0 | Success |
|
||||
| 1 | Validation error (bad pubkey, bad role, usage error) |
|
||||
| 2 | Not found (remove: member does not exist) |
|
||||
| 3 | Cannot remove relay owner (use `RELAY_OWNER_PUBKEY` to change owner) |
|
||||
| 4 | Role mismatch (`--role` check failed) |
|
||||
| 5 | DB/Redis/internal error |
|
||||
|
||||
**Required environment variables for member management:**
|
||||
|
||||
| Variable | Notes |
|
||||
|----------|-------|
|
||||
| `DATABASE_URL` | Postgres connection string |
|
||||
| `REDIS_URL` | Redis connection string |
|
||||
| `BUZZ_RELAY_PRIVATE_KEY` | Hex private key — required to sign kind:13534 events |
|
||||
|
||||
### NIP-43 Admin Events (WebSocket)
|
||||
|
||||
Relay membership can also be managed over WebSocket using NIP-43 admin events. These require
|
||||
the sender to be authenticated (NIP-42) as the relay owner or an admin.
|
||||
|
||||
| Kind | Action | Required tags |
|
||||
|------|--------|---------------|
|
||||
| 9030 | Add member | `["p", "<hex-pubkey>"]`, optional `["role", "member\|admin"]` |
|
||||
| 9031 | Remove member | `["p", "<hex-pubkey>"]`, optional `["role", "member\|admin"]` |
|
||||
| 9032 | Change role | `["p", "<hex-pubkey>"]`, `["role", "member\|admin"]` |
|
||||
|
||||
Example using `nak`:
|
||||
|
||||
```bash
|
||||
# Add a member (owner or admin must sign)
|
||||
nak event -k 9030 \
|
||||
--tag "p=<target-hex-pubkey>" \
|
||||
--tag "role=member" \
|
||||
--auth --sec <owner-or-admin-privkey> \
|
||||
ws://localhost:3000
|
||||
|
||||
# Remove a member
|
||||
nak event -k 9031 \
|
||||
--tag "p=<target-hex-pubkey>" \
|
||||
--auth --sec <owner-or-admin-privkey> \
|
||||
ws://localhost:3000
|
||||
|
||||
# Change a member's role to admin
|
||||
nak event -k 9032 \
|
||||
--tag "p=<target-hex-pubkey>" \
|
||||
--tag "role=admin" \
|
||||
--auth --sec <owner-or-admin-privkey> \
|
||||
ws://localhost:3000
|
||||
```
|
||||
|
||||
After each add/remove/role-change, the relay publishes a kind:13534 membership list event
|
||||
(relay-signed, NIP-70 protected) that clients can subscribe to:
|
||||
|
||||
```bash
|
||||
# Subscribe to the live membership roster
|
||||
nak req -k 13534 --auth --sec <privkey> ws://localhost:3000
|
||||
```
|
||||
|
||||
### Known Limitations
|
||||
|
||||
1. **CLI intentionally does not emit kind 8000/8001 deltas** — `publish_nip43_delta` is
|
||||
in-process-only (no Redis hop), so a sidecar call stores but never pushes. The 13534 list
|
||||
snapshot is the authoritative roster and rides Redis to live clients. Do not wire a delta call
|
||||
that passes in-process tests and silently no-ops in the deployed `compose exec` path.
|
||||
|
||||
2. **The `custom_created_at = max(now, newest_existing_13534 + 1s)` bump defeats same-second
|
||||
domination for serial invocations; it does NOT serialize concurrent CLI processes** — two
|
||||
near-simultaneous adds can read the same newest timestamp and collide on the bumped second.
|
||||
`run.sh` serialization is the guard against parallel adds (e.g. `xargs -P`). When adding
|
||||
multiple members in a loop, add `sleep 1` between invocations.
|
||||
|
||||
---
|
||||
|
||||
## Relay Environment Variables (NIP-29 relevant)
|
||||
|
||||
| Variable | Required | Default | Description |
|
||||
|
||||
@@ -27,6 +27,12 @@
|
||||
|
||||
Buzz is a self-hostable workspace where humans and AI agents share the same rooms.
|
||||
|
||||
A Buzz **community** is the workspace a user reaches by URL. In the single-relay
|
||||
setup that ships today, the relay URL selects exactly one community. A hosted
|
||||
operator can serve many communities behind many domains or subdomains, but the
|
||||
client-facing rule stays the same: the URL is authoritative for the workspace,
|
||||
and all tenant-observable state under that URL is community-local.
|
||||
|
||||
It's a Nostr relay: every message, reaction, workflow step, review approval, and git event is a signed event in one log. Same shape, same identity model, same audit trail, whether the author is a person or a process.
|
||||
|
||||
In practice it feels like a team workspace. Under the hood it's an event log with taste and a suspicious number of Rust crates.
|
||||
@@ -70,9 +76,9 @@ Yes, it's another AI-adjacent developer tool. We're sorry. The difference is wha
|
||||
|
||||
## Why Buzz is better
|
||||
|
||||
One relay. One identity model. One event log. Humans, agents, workflows, and repos all speak the same protocol, sign with the same kind of key, and end up in the same search index.
|
||||
One community. One identity model. One event log. Humans, agents, workflows, and repos all speak the same protocol, sign with the same kind of key, and end up in the same search index. In the default self-hosted deployment, one relay hosts one community; in a hosted multi-tenant deployment, each community keeps that same semantic boundary even when the backend shares Postgres, Redis, Typesense, and object storage.
|
||||
|
||||
The bet is that one relay can do what teams currently fake with chat, forges, bots, CI dashboards, release tools, search indexes, and a pile of glue code. Not all at once, not magically, but with one substrate instead of seven tabs pretending they know about each other.
|
||||
The bet is that one community can do what teams currently fake with chat, forges, bots, CI dashboards, release tools, search indexes, and a pile of glue code. Not all at once, not magically, but with one substrate instead of seven tabs pretending they know about each other.
|
||||
|
||||
Agents are part of the room, not haunted cron jobs.
|
||||
|
||||
@@ -162,7 +168,7 @@ A Rust workspace of focused crates. Single source of truth: the relay. See [ARCH
|
||||
|
||||
**Core protocol** — `buzz-core` (zero-I/O types, NIP-01 filters, Schnorr verify) · `buzz-relay` (Axum WS + REST)
|
||||
|
||||
**Services** — `buzz-db` (Postgres) · `buzz-auth` (NIP-42/98 Schnorr auth, rate limiting) · `buzz-pubsub` (Redis, presence, typing) · `buzz-search` (Typesense) · `buzz-audit` (hash-chain log)
|
||||
**Services** — `buzz-db` (Postgres) · `buzz-auth` (NIP-42/98 Schnorr auth, rate limiting) · `buzz-pubsub` (Redis, presence, typing) · `buzz-search` (Typesense) · `buzz-audit` (hash-chain log). Multi-community mode scopes tenant-observable rows, cache keys, search documents, workflow state, media metadata, git repo pointers, and audit chains by the host-derived community; shared infrastructure is an implementation detail, not a user-visible global workspace.
|
||||
|
||||
**Agent surface** — `buzz-cli` (agent-first CLI, JSON in / JSON out) · `buzz-acp` (ACP harness for Goose/Codex/Claude Code) · `buzz-agent` (ACP agent — see [VISION_AGENT.md](VISION_AGENT.md)) · `buzz-dev-mcp` (shell + file-edit tools) · `buzz-workflow` (YAML automation) · `buzz-persona` (agent persona packs)
|
||||
|
||||
|
||||
+24
-3
@@ -67,9 +67,30 @@ unless the subscriber is a member.
|
||||
### Append-Only Audit Log
|
||||
|
||||
All events are written to a tamper-evident audit log (`buzz-audit`). Each
|
||||
log entry is chained to the previous one via an HMAC, making retroactive
|
||||
modification detectable. The audit log is designed for SOX-grade compliance
|
||||
and eDiscovery.
|
||||
log entry is chained to the previous one via a SHA-256 hash chain. Because the
|
||||
chain is keyless, it is tamper-evident but not tamper-resistant: it detects
|
||||
accidental corruption or single-row edits, but an attacker with database write
|
||||
access can recompute the entire chain after editing. The audit log is designed
|
||||
for SOX-grade compliance and eDiscovery.
|
||||
|
||||
### Desktop Secret Storage — OS Keyring
|
||||
|
||||
The Buzz desktop app stores nsec private keys in the operating system keyring
|
||||
rather than in plaintext files: macOS Keychain, Windows Credential Manager, or
|
||||
the Linux Secret Service (`gnome-keyring` / `kwallet` via D-Bus). This covers
|
||||
both the human identity key and every managed-agent key.
|
||||
|
||||
On first launch after upgrading, existing plaintext keys are migrated into the
|
||||
keyring: the key is imported, read back to verify the round-trip, and only then
|
||||
is the plaintext deleted. Migration runs only when the keyring is reachable —
|
||||
if the backend is unavailable that session, the app keeps reading from the
|
||||
plaintext file and does **not** migrate, so a transient outage cannot resurrect
|
||||
a rotated key from a leftover file.
|
||||
|
||||
When no keyring backend is available (headless Linux with no Secret Service, for
|
||||
example), keys fall back to a `0o600` owner-only file. The `BUZZ_PRIVATE_KEY`
|
||||
environment variable, when set, always takes precedence over both stores — this
|
||||
is how harnessed agents and CI receive their identity.
|
||||
|
||||
### Input Validation
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
|
||||
The platform made it possible. The agent made it happen. Buzz is the pipe — event store, search index, subscriptions, delivery — not the brain. Humans and agents bring the intelligence. Buzz gives them a shared space to use it.
|
||||
|
||||
One relay is your entire workspace. Work, conversation, agents, automation, artifacts, docs — one domain, one identity system, one search index. `myproject.com` in a browser shows your repos. `git clone repoa.myproject.com` works. Open the Buzz app and you're in the channels where the work happens. No GitHub. No Discord. No stitching five services together. The project lives in one place, and that place is yours. See [VISION_SOVEREIGN.md](VISION_SOVEREIGN.md) for the full picture.
|
||||
One community is your entire workspace. Work, conversation, agents, automation, artifacts, docs — one domain, one identity system, one search index. `myproject.com` in a browser shows your repos. `git clone repoa.myproject.com` works. Open the Buzz app and you're in the channels where the work happens. No GitHub. No Discord. No stitching five services together. The project lives in one place, and that place is yours. Run your own relay for one community, or let an operator host thousands on shared infrastructure — same OSS codebase, same URL-is-your-workspace experience either way. See [VISION_SOVEREIGN.md](VISION_SOVEREIGN.md) for the full picture.
|
||||
|
||||
---
|
||||
|
||||
@@ -47,6 +47,16 @@ Guests (investors, reporters, partners) get a scoped token with membership in sp
|
||||
|
||||
---
|
||||
|
||||
## Communities
|
||||
|
||||
A **community** is the tenant boundary: one workspace, one URL, one isolated world of channels, members, profiles, DMs, repos, and search. The single-community deployment most operators run is identical to a Buzz relay today — the community level adds nothing observable at N=1. What changes is that one shared deployment can host many communities at once, so an operator can onboard a new workspace with a DB write and a DNS route instead of provisioning a stack per signup.
|
||||
|
||||
- **The URL is the community.** `myproject.com` is authoritative — exactly as a relay URL is today, lifted one level up. Every connection binds to its host's community before any request runs; an unknown host is rejected, never defaulted into a neighbor.
|
||||
- **Isolation is the boundary, not a filter.** Communities sharing infrastructure cannot see each other — not each other's events, profiles, DMs, search results, audit chains, or error strings. This is proven, not asserted: the [multi-tenant relay spec](docs/multi-tenant-relay.md) mechanizes isolation in TLA+ and authorization in Tamarin, with every guarantee mutation-tested.
|
||||
- **Identity is portable, profiles are per-community.** Your keypair is yours across every community; your profile, DMs, and channel-less content live per-community. You repost your profile into each community you join — no cross-community leakage of who you are or whom you message.
|
||||
|
||||
---
|
||||
|
||||
## The Protocol
|
||||
|
||||
[Nostr NIP-01](https://github.com/nostr-protocol/nips/blob/master/01.md) on the wire. Every action — a message, a reaction, a workflow step, a profile update — is a cryptographically signed event:
|
||||
|
||||
@@ -16,6 +16,13 @@ Two binaries, two protocols, no coupling between them.
|
||||
|
||||
Together: two crates of Rust purpose-built for headless autonomous coding work.
|
||||
|
||||
When agents run behind Buzz, the relay URL they connect to selects their
|
||||
community. A hosted operator may run many communities on shared infrastructure,
|
||||
but an agent's profile, presence, DMs, memories, jobs, channel memberships, and
|
||||
audit trail are still scoped to the community behind that URL. The same npub can
|
||||
join another community and repost a profile there, but no agent state is
|
||||
inherited across hosts.
|
||||
|
||||
## Why We Built Our Own
|
||||
|
||||
**Auditability.** A senior engineer can read both binaries in a sitting. There are no abstractions reserved for future flexibility. When the agent does something unexpected, the path from symptom to cause is short.
|
||||
@@ -57,6 +64,7 @@ Two pipes. Two protocols. Each session gets its own MCP server instances — ful
|
||||
|
||||
- Multiple concurrent sessions in one process — each with independent MCP servers, history, and context (configurable cap, default 8)
|
||||
- Ten agents in parallel behind Buzz, each with their own MCP configuration
|
||||
- The same agent key can participate in multiple Buzz communities while keeping membership, jobs, DMs, profile, and presence community-local
|
||||
- Any ACP client gets a coding agent without a custom adapter
|
||||
- Any MCP server gets a capable caller without a custom adapter
|
||||
- A codebase small enough to fork, modify, and understand in a day — two crates, no coupling between them
|
||||
|
||||
+3
-3
@@ -2,9 +2,9 @@
|
||||
|
||||
> A small team runs their project on one Buzz relay. Three of them have GPUs that sit idle most of the day — a gaming PC, a laptop, a workstation under a desk. One flips a toggle: *Share compute.* The others point their agents at it. Now the whole team's coding agents answer from a capable model running on hardware they already own. No API keys. No cloud bill. Every prompt runs inside the relay community they already chose to trust.
|
||||
|
||||
A Buzz relay is a trust group. The people in it already know each other — that shared membership is a decision they've already made. Buzz Mesh turns that decision into shared AI compute: the idle GPUs scattered across your community become one pool, usable by every agent on the relay, gated by the membership you already have. And because the pool is many machines, not one, the community can run models larger and more capable than any one member could load alone. More intelligence becomes reachable when the group works as a group.
|
||||
A Buzz community is a trust group. The people in it already know each other — that shared membership is a decision they've already made. Buzz Mesh turns that decision into shared AI compute: the idle GPUs scattered across your community become one pool, usable by every agent in the community, gated by the membership you already have. And because the pool is many machines, not one, the community can run models larger and more capable than any one member could load alone. More intelligence becomes reachable when the group works as a group.
|
||||
|
||||
Nothing here is new on its own. Pooling GPUs across machines is solved. Nostr identity is solved. Relay-gated membership is how Buzz already works. The insight is that the tool that pools the GPUs already speaks the same protocol Buzz is built on — so the mesh's admission gate and your relay's membership gate are the same gate.
|
||||
Nothing here is new on its own. Pooling GPUs across machines is solved. Nostr identity is solved. Community-gated membership is how Buzz already works. The insight is that the tool that pools the GPUs already speaks the same protocol Buzz is built on — so the mesh's admission gate and your community's membership gate are the same gate. The boundary is the community, never the deployment: a community on shared infrastructure pools only its own members' compute, and a co-tenant community can't find it, join it, or serve to it.
|
||||
|
||||
Each piece is boring. The combination is the thing.
|
||||
|
||||
@@ -34,7 +34,7 @@ This is why it matters most for agents. An agent on your relay isn't reaching ou
|
||||
|
||||
## Honest Costs
|
||||
|
||||
Your prompts go to people, not a vendor. For a trust group that's a feature — far better than handing them to a stranger's cloud — but it is a different promise than "your data never leaves your machine," and the consent screen says so plainly. A relay is only as private as its membership is trustworthy.
|
||||
Your prompts go to people, not a vendor. For a trust group that's a feature — far better than handing them to a stranger's cloud — but it is a different promise than "your data never leaves your machine," and the consent screen says so plainly. A community is only as private as its membership is trustworthy.
|
||||
|
||||
The mesh is opt-in, so it's only as capable as your community's participation. A relay where nobody shares compute has an empty mesh. That's the right default — you give willingly or not at all — but the value compounds with how many people turn it on.
|
||||
|
||||
|
||||
+4
-4
@@ -4,7 +4,7 @@
|
||||
>
|
||||
> Bug report to merged patch. One place. One search index. One identity system. The branch channel was the pull request, the CI dashboard, and the discussion thread.
|
||||
|
||||
This document is the software-forge slice of the broader Buzz platform. [VISION.md](VISION.md) covers the platform. [VISION_SOVEREIGN.md](VISION_SOVEREIGN.md) covers the sovereign relay story — one domain, one relay, one project. This doc zooms in on what it looks like when that relay hosts code.
|
||||
This document is the software-forge slice of the broader Buzz platform. [VISION.md](VISION.md) covers the platform. [VISION_SOVEREIGN.md](VISION_SOVEREIGN.md) covers the sovereign relay story — one domain, one relay, one project. This doc zooms in on what it looks like when that relay hosts code. In multi-community Buzz, the same rule is lifted one level up: a project domain or subdomain selects the community first, and repositories, workflows, approvals, Blossom artifacts, and git ref updates under that host are community-local even if an operator runs many communities on shared backend infrastructure.
|
||||
|
||||
---
|
||||
|
||||
@@ -12,7 +12,7 @@ This document is the software-forge slice of the broader Buzz platform. [VISION.
|
||||
|
||||
A project lives on the relay. `myproject.com` in a browser shows the project home. Click a repo and you're at `repoa.myproject.com` — README rendered, file tree navigable, code syntax-highlighted, clone URL at the top. The same URL serves HTML to a browser and git protocol to `git clone`. Content negotiation. One URL, two audiences.
|
||||
|
||||
Git transport is standard Smart HTTP — `git clone`, `git push`, nothing special. Your npub signs pushes. Same domain, same auth, same identity as everything else on the relay.
|
||||
Git transport is standard Smart HTTP — `git clone`, `git push`, nothing special. Your npub signs pushes. Same domain, same auth, same identity as everything else on the relay. The host in the clone/push URL is also the community selector: the same `owner/repo` name may exist in two communities without sharing refs, branch protections, workflow runs, approvals, or repo announcements.
|
||||
|
||||
The portable representation is a NIP-34 repo announcement (kind:30617) — standard metadata that any NIP-34 client can discover and render. Buzz extends it with `buzz-` prefixed tags for channel binding and visibility:
|
||||
|
||||
@@ -105,7 +105,7 @@ The approval event is signed by the maintainer's npub. The merge status referenc
|
||||
|
||||
## The Web of Trust
|
||||
|
||||
Every contributor — human or agent — has a verifiable identity and a queryable contribution history across every project on the network.
|
||||
Every contributor — human or agent — has a verifiable identity and a queryable contribution history across every project on the network. Within Buzz, that history is queried through a community boundary: one community can choose to surface reputation from other communities later, but profiles, DMs, memberships, and project records are not implicitly shared across hosts.
|
||||
|
||||
A new contributor submits a patch. Before you read the code:
|
||||
|
||||
@@ -125,7 +125,7 @@ Workflows orchestrate. Agents perform the compute. The relay is the message bus,
|
||||
|
||||
A push to a branch channel triggers the CI workflow. The workflow engine coordinates the steps — build, test, lint. Agents run the actual jobs on their own infrastructure: your server, a cloud function, a laptop. Results post back to the branch channel alongside the conversation.
|
||||
|
||||
Workflows live in the repo (`.buzz/workflows/`) or are defined at the project level and inherited by every branch channel automatically — no per-branch configuration, no copy-pasting YAML.
|
||||
Workflows live in the repo (`.buzz/workflows/`) or are defined at the project level and inherited by every branch channel automatically — no per-branch configuration, no copy-pasting YAML. Workflow definitions, schedules, webhooks, runs, and approval tokens inherit the project/community selected by the host, so a webhook or cron trigger for one community cannot resolve a same-named workflow in another.
|
||||
|
||||
```yaml
|
||||
name: CI
|
||||
|
||||
+14
-7
@@ -56,10 +56,16 @@ chat at discord.gg/..." Everything is at `myproject.com`. That's where everythin
|
||||
is.
|
||||
|
||||
Not every project needs to run its own relay. Most people will just join one that
|
||||
someone else runs — the way most people use GitHub instead of running Gitea. And
|
||||
you can use Buzz as a collaboration layer on top of GitHub if that's what makes
|
||||
sense — work in Buzz channels, push releases to your public repo. The sovereign
|
||||
setup is the full version. But the tools work at every level of commitment.
|
||||
someone else runs — the way most people use GitHub instead of running Gitea. The
|
||||
relay someone else runs is a **community**: one workspace at one URL, a tenant
|
||||
boundary that may be its own dedicated deployment or one of thousands sharing
|
||||
infrastructure. Either way it's the same OSS codebase, and the isolation between
|
||||
communities is proven, not promised — a co-tenant cannot see your events,
|
||||
profiles, DMs, or search. Your key stays yours across all of them; identity is
|
||||
portable even when the hosting isn't. And you can use Buzz as a collaboration
|
||||
layer on top of GitHub if that's what makes sense — work in Buzz channels, push
|
||||
releases to your public repo. The sovereign setup is the full version. But the
|
||||
tools work at every level of commitment.
|
||||
|
||||
---
|
||||
|
||||
@@ -202,9 +208,10 @@ relay, point your domain at it, and you're back. The project continues.
|
||||
You run infrastructure. A server, a domain, a relay. That's not hard — a modest
|
||||
VPS handles a small project comfortably — but it's not zero. Someone has to keep
|
||||
it running. Someone has to handle backups. Someone has to deal with the 3 AM alert
|
||||
when the disk fills up. Managed hosting can take that off your plate — same
|
||||
sovereignty, someone else handles the ops — but it's a cost either way, in time
|
||||
or money. Worth knowing before you start.
|
||||
when the disk fills up. Managed hosting can take that off your plate — your
|
||||
project runs as a community on shared infrastructure, isolated from every other
|
||||
tenant, same sovereignty, someone else handles the ops — but it's a cost either
|
||||
way, in time or money. Worth knowing before you start.
|
||||
|
||||
Key management is harder than "sign in with Google." Losing your private key means
|
||||
losing your identity. There's no "forgot password" flow, no support ticket to file,
|
||||
|
||||
@@ -19,8 +19,6 @@ use crate::observer::{ObserverContext, ObserverHandle};
|
||||
/// Lines exceeding this limit are rejected to prevent OOM from rogue agents.
|
||||
const MAX_LINE_SIZE: usize = 10_000_000; // 10 MB
|
||||
|
||||
// ─── Public types ────────────────────────────────────────────────────────────
|
||||
|
||||
/// An MCP server configuration passed to `session/new`.
|
||||
///
|
||||
/// Corresponds to the `McpServerStdio` variant in the ACP schema.
|
||||
@@ -106,8 +104,6 @@ pub enum AcpError {
|
||||
AgentError(String),
|
||||
}
|
||||
|
||||
// ─── AcpClient ───────────────────────────────────────────────────────────────
|
||||
|
||||
/// ACP client that owns an agent subprocess and communicates over its stdio.
|
||||
///
|
||||
/// One `AcpClient` per agent process. Multiple sessions can be created on the
|
||||
@@ -151,8 +147,6 @@ pub struct AcpClient {
|
||||
}
|
||||
|
||||
impl AcpClient {
|
||||
// ── Lifecycle ─────────────────────────────────────────────────────────
|
||||
|
||||
/// Kill the agent subprocess and wait for it to exit (no zombies).
|
||||
///
|
||||
/// `Drop` only calls `start_kill()` (sends SIGKILL but doesn't reap).
|
||||
@@ -568,8 +562,6 @@ impl AcpClient {
|
||||
self.parse_stop_reason(&result)
|
||||
}
|
||||
|
||||
// ── Internal helpers ──────────────────────────────────────────────────
|
||||
|
||||
/// Serialize `value` as a single NDJSON line and flush to the agent's stdin.
|
||||
///
|
||||
/// Bounded by a 30-second write timeout. If the agent stops reading stdin
|
||||
@@ -1090,8 +1082,6 @@ impl AcpClient {
|
||||
}
|
||||
}
|
||||
|
||||
// ─── Permission response constructors ────────────────────────────────────────
|
||||
|
||||
/// Build `session/prompt` params from one or more text content blocks.
|
||||
fn build_prompt_params(session_id: &str, prompt_blocks: &[&str]) -> serde_json::Value {
|
||||
let blocks: Vec<serde_json::Value> = prompt_blocks
|
||||
@@ -1122,8 +1112,6 @@ fn permission_response_cancelled(id: &serde_json::Value) -> serde_json::Value {
|
||||
})
|
||||
}
|
||||
|
||||
// ─── Session response types ───────────────────────────────────────────────────
|
||||
|
||||
/// Full `session/new` response — session ID plus the raw JSON result.
|
||||
///
|
||||
/// Callers use the extractor helpers to pull model info from `raw`.
|
||||
@@ -1215,8 +1203,6 @@ pub fn resolve_model_switch_method(
|
||||
None
|
||||
}
|
||||
|
||||
// ─── Drop: kill child process ─────────────────────────────────────────────────
|
||||
|
||||
impl Drop for AcpClient {
|
||||
fn drop(&mut self) {
|
||||
// Best-effort SIGKILL + reap. We cannot `await` in Drop (sync context).
|
||||
@@ -1258,14 +1244,10 @@ fn kill_process_group(_pid: u32) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
// ─── Tests ───────────────────────────────────────────────────────────────────
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
// ── StopReason parsing ────────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn stop_reason_parses_all_known_values() {
|
||||
assert_eq!(StopReason::from_str("end_turn"), Some(StopReason::EndTurn));
|
||||
@@ -1310,8 +1292,6 @@ mod tests {
|
||||
assert_eq!(StopReason::from_str("Refusal"), Some(StopReason::Refusal));
|
||||
}
|
||||
|
||||
// ── Permission option finding ─────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn find_allow_once_by_kind_not_by_option_id() {
|
||||
// optionId values are intentionally non-obvious to prove we don't hardcode them.
|
||||
@@ -1371,8 +1351,6 @@ mod tests {
|
||||
assert_eq!(reject_once.unwrap()["optionId"].as_str(), Some("rej-x"));
|
||||
}
|
||||
|
||||
// ── JSON-RPC message construction ─────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn request_has_id_field() {
|
||||
let id: u64 = 42;
|
||||
@@ -1563,8 +1541,6 @@ mod tests {
|
||||
assert_eq!(msg["params"]["sessionId"].as_str(), Some("sess_xyz789"));
|
||||
}
|
||||
|
||||
// ── String ID handling (Fix 1) ────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn permission_request_with_string_id() {
|
||||
// Verify that permission response uses the same ID type as the request.
|
||||
@@ -1617,8 +1593,6 @@ mod tests {
|
||||
assert!(cancelled_numeric["id"].is_number());
|
||||
}
|
||||
|
||||
// ── Model extractor tests ─────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn extract_model_config_options_finds_model_category() {
|
||||
let result = serde_json::json!({
|
||||
@@ -1685,8 +1659,6 @@ mod tests {
|
||||
assert!(super::extract_model_state(&result).is_none());
|
||||
}
|
||||
|
||||
// ── resolve_model_switch_method tests ─────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn resolve_prefers_stable_over_unstable() {
|
||||
let result = serde_json::json!({
|
||||
@@ -1783,8 +1755,6 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
// ── Error variant display ─────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn idle_timeout_error_includes_duration() {
|
||||
let err = AcpError::IdleTimeout(std::time::Duration::from_secs(320));
|
||||
@@ -1805,8 +1775,6 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
// ── Async integration tests with real subprocess ──────────────────────
|
||||
|
||||
async fn spawn_script(script: &str) -> AcpClient {
|
||||
AcpClient::spawn("bash", &["-c".into(), script.into()], &[])
|
||||
.await
|
||||
@@ -1977,8 +1945,6 @@ mod tests {
|
||||
assert_eq!(result.unwrap()["worked"], serde_json::json!(true));
|
||||
}
|
||||
|
||||
// ── Keepalive / tool-call idle reset tests (PR #935 fix) ─────────────
|
||||
|
||||
#[tokio::test]
|
||||
async fn keepalive_resets_idle_past_deadline() {
|
||||
// Keepalive session/update lines every 50ms against a 100ms idle deadline.
|
||||
@@ -2044,8 +2010,6 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
// ── session_new_full systemPrompt serialization ──────────────────────
|
||||
|
||||
#[tokio::test]
|
||||
async fn session_new_full_includes_system_prompt_when_some() {
|
||||
// Script: respond to initialize, then echo back the session/new request.
|
||||
|
||||
@@ -34,15 +34,20 @@ Run `buzz --help` or `buzz <group> --help` for full usage.
|
||||
|
||||
### Threading
|
||||
|
||||
- **To a human** (updates, questions, deliverables): Use `--reply-to <thread-root-id>` (from your `[Context]` block) and `@mention` the human. Keeps messages at layer 1 where humans read.
|
||||
- **To another agent** (dispatching, collaborating): Thread however you want.
|
||||
- **When in doubt**, reply to thread root.
|
||||
- **Thread scope:** Respond in the thread where you were tagged. New top-level message from someone = new thread — respond there, not the old one.
|
||||
- **New topic → new top-level message.** Don't graft unrelated work onto an existing thread.
|
||||
Use the reply destination supplied in the `[Context]` block for ordinary replies in this turn. Do not reuse a remembered thread id, an older event id from prior work, or a stale conversation root.
|
||||
|
||||
For human-facing work, keep the conversation flat and easy to read. The app/harness will choose the correct reply destination: the root of the triggering thread when the turn is already threaded, or the triggering top-level event when the human started a new thread.
|
||||
|
||||
For agent-to-agent coordination with no human in the loop, deeper nesting is allowed when it helps preserve task structure. Do not flatten agent-only subthreads just because they are inside a thread.
|
||||
|
||||
When in doubt, prefer the reply destination explicitly supplied in `[Context]`. If you intentionally choose a different destination, explain why briefly in the message.
|
||||
|
||||
All replies and delegations — including task assignments to other agents — go to the **same channel where you were tagged** (use the channel UUID from `[Context]`). Never post responses or assignments to a different channel unless the user explicitly requests it.
|
||||
|
||||
### General
|
||||
|
||||
- Respond promptly to @mentions. Be direct — no preamble. Name what you did, what you found, or what you need.
|
||||
- **Every turn that processes a user message MUST end with `buzz messages send`.** Your reasoning and tool calls are invisible to users — if you didn't send a message, they saw nothing. A turn that ends without a sent message is a silent failure.
|
||||
- Use GitHub-flavored Markdown. Fenced code blocks with language tags for syntax highlighting.
|
||||
- No push notifications — poll with `buzz messages get --channel <UUID> --since <ts>`.
|
||||
- Address people by the name in their own message header.
|
||||
@@ -80,6 +85,7 @@ Your `core` memory is auto-injected into your context every turn — it holds id
|
||||
|
||||
- **Keep `core` small.** A line earns a permanent slot only if it matters across most sessions or prevents a sharp repeat mistake. Treat the 65,535-byte hard limit as a wall to stay far from, not a budget to fill — aim to keep `core` under ~10 KB (roughly your healthy baseline).
|
||||
- **Durable detail goes to a cold `mem/` slug, not `core`.** Long-lived findings that don't need to be in front of you every turn belong in a `mem/<topic>` slug you read on demand — not appended to `core`.
|
||||
- **Evict completed work.** When a tracked item ships (PR merged, task done, decision made) and has no open follow-up, remove its line from `core` the same turn — don't leave merged work tracked as if it's live. The detail already lives in its cold `mem/` slug if you need it later.
|
||||
- **Treat `core` as load-bearing.** Follow it unless newer explicit user instructions override it.
|
||||
- Cite sources with paths, links, or command outputs. No unsupported claims.
|
||||
|
||||
|
||||
+408
-56
@@ -7,14 +7,14 @@ use std::collections::{HashMap, HashSet};
|
||||
use std::path::PathBuf;
|
||||
|
||||
use clap::Parser;
|
||||
use clap::ValueEnum;
|
||||
use nostr::Keys;
|
||||
use thiserror::Error;
|
||||
use url::Url;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::filter::SubscriptionRule;
|
||||
|
||||
// ── Constants ─────────────────────────────────────────────────────────────────
|
||||
|
||||
/// Default idle timeout (seconds) when neither `--idle-timeout` nor the
|
||||
/// deprecated `--turn-timeout` is set.
|
||||
///
|
||||
@@ -26,8 +26,6 @@ use crate::filter::SubscriptionRule;
|
||||
/// Override via `--idle-timeout` / `BUZZ_ACP_IDLE_TIMEOUT`.
|
||||
pub(crate) const DEFAULT_IDLE_TIMEOUT_SECS: u64 = 900;
|
||||
|
||||
// ── Errors ────────────────────────────────────────────────────────────────────
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum ConfigError {
|
||||
#[error("failed to parse nostr keys: {0}")]
|
||||
@@ -40,8 +38,6 @@ pub enum ConfigError {
|
||||
ConfigFile(String),
|
||||
}
|
||||
|
||||
// ── Enums ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, clap::ValueEnum)]
|
||||
pub enum SubscribeMode {
|
||||
Mentions,
|
||||
@@ -78,7 +74,7 @@ pub enum MultipleEventHandling {
|
||||
/// - `allowlist` — owner + explicit pubkey list (`--respond-to-allowlist`).
|
||||
/// - `anyone` — all events forwarded (no author filtering).
|
||||
/// - `nobody` — all events dropped (proactive/heartbeat-only mode).
|
||||
#[derive(Debug, Clone, Default, PartialEq, clap::ValueEnum)]
|
||||
#[derive(Debug, Clone, Default, PartialEq, Eq, Hash, clap::ValueEnum)]
|
||||
pub enum RespondTo {
|
||||
#[default]
|
||||
OwnerOnly,
|
||||
@@ -151,8 +147,6 @@ impl std::fmt::Display for PermissionMode {
|
||||
}
|
||||
}
|
||||
|
||||
// ── Models subcommand ─────────────────────────────────────────────────────────
|
||||
|
||||
/// CLI args for `buzz-acp models` — query available models from an agent.
|
||||
///
|
||||
/// This is a standalone `Parser` (not a subcommand variant) because the
|
||||
@@ -182,8 +176,6 @@ pub struct ModelsArgs {
|
||||
pub json: bool,
|
||||
}
|
||||
|
||||
// ── CLI ───────────────────────────────────────────────────────────────────────
|
||||
|
||||
#[derive(Debug, Parser)]
|
||||
#[command(
|
||||
name = "buzz-acp",
|
||||
@@ -402,6 +394,14 @@ pub struct CliArgs {
|
||||
#[arg(long, env = "BUZZ_ACP_RESPOND_TO_ALLOWLIST", value_delimiter = ',')]
|
||||
pub respond_to_allowlist: Option<Vec<String>>,
|
||||
|
||||
/// Comma-separated list of allowed `--respond-to` modes.
|
||||
/// When set, the harness rejects startup if `--respond-to` is not in this list.
|
||||
/// Modes: owner-only, allowlist, anyone, nobody.
|
||||
/// Default: empty (all modes allowed — no restriction).
|
||||
/// Example: `BUZZ_ACP_ALLOWED_RESPOND_TO=owner-only,allowlist`
|
||||
#[arg(long, env = "BUZZ_ACP_ALLOWED_RESPOND_TO", value_delimiter = ',')]
|
||||
pub allowed_respond_to: Option<Vec<String>>,
|
||||
|
||||
/// Path to a persona pack directory. Used with --persona-name to configure
|
||||
/// the agent from a .persona.md pack instead of CLI flags.
|
||||
#[arg(long, env = "BUZZ_ACP_PERSONA_PACK")]
|
||||
@@ -416,8 +416,6 @@ pub struct CliArgs {
|
||||
pub relay_observer: bool,
|
||||
}
|
||||
|
||||
// ── Merged NIP-01 filter ──────────────────────────────────────────────────────
|
||||
|
||||
/// Merged NIP-01 subscription filter for a single channel.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ChannelFilter {
|
||||
@@ -427,8 +425,6 @@ pub struct ChannelFilter {
|
||||
pub require_mention: bool,
|
||||
}
|
||||
|
||||
// ── Resolved config ───────────────────────────────────────────────────────────
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Config {
|
||||
pub keys: Keys,
|
||||
@@ -473,6 +469,8 @@ pub struct Config {
|
||||
pub respond_to: RespondTo,
|
||||
/// Validated allowlist of pubkey hex strings (used when respond_to == Allowlist).
|
||||
pub respond_to_allowlist: HashSet<String>,
|
||||
/// Allowed `respond_to` modes. Empty = all modes allowed.
|
||||
pub allowed_respond_to: Vec<String>,
|
||||
/// Per-persona env vars to inject at agent spawn time (e.g., GOOSE_PROVIDER, GOOSE_MODEL, BUZZ_AGENT_MODEL).
|
||||
/// Populated from persona pack resolution. Empty when no pack is configured.
|
||||
pub persona_env_vars: Vec<(String, String)>,
|
||||
@@ -531,6 +529,57 @@ fn default_agent_args(command: &str) -> Option<Vec<String>> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Build `-c` flag pairs that allowlist the relay hostname in Codex's network sandbox.
|
||||
///
|
||||
/// Codex sandboxes MCP subprocesses (including `buzz-cli`) behind a local proxy with
|
||||
/// a domain allowlist. Without this, `buzz-cli` requests to the relay are blocked before
|
||||
/// they reach WARP or any other outbound network path.
|
||||
///
|
||||
/// Returns `["-c", "network_proxy.mode=\"full\"", "-c", "network_proxy.domains.\"<host>\"=\"allow\""]`
|
||||
/// for Codex agents, or an empty vec for non-Codex agents or when the hostname cannot
|
||||
/// be parsed from the relay URL.
|
||||
///
|
||||
/// The `network_proxy` keys map to `NetworkProxyConfigToml` in codex-acp's config schema:
|
||||
/// - `network_proxy.mode="full"` enables the managed proxy for all outbound traffic
|
||||
/// - `network_proxy.domains."<host>"="allow"` adds the relay hostname to the allowlist
|
||||
///
|
||||
/// Handles `ws://`, `wss://`, `http://`, and `https://` schemes. Port is stripped —
|
||||
/// Codex's domain allowlist matches on hostname only.
|
||||
pub fn codex_network_args(agent_command: &str, relay_url: &str) -> Vec<String> {
|
||||
match normalize_agent_command_identity(agent_command).as_str() {
|
||||
"codex" | "codex-acp" => {}
|
||||
_ => return vec![],
|
||||
}
|
||||
|
||||
// Use the `url` crate so ws://, wss://, http://, https:// are all handled
|
||||
// correctly. On parse failure, skip injection rather than panicking.
|
||||
let host = match Url::parse(relay_url) {
|
||||
Ok(u) => match u.host_str() {
|
||||
Some(h) => h.to_owned(),
|
||||
None => {
|
||||
tracing::warn!(
|
||||
relay_url,
|
||||
"codex network allowlist: no host in relay URL — skipping injection"
|
||||
);
|
||||
return vec![];
|
||||
}
|
||||
},
|
||||
Err(e) => {
|
||||
tracing::warn!(relay_url, error = %e, "codex network allowlist: failed to parse relay URL — skipping injection");
|
||||
return vec![];
|
||||
}
|
||||
};
|
||||
|
||||
tracing::debug!(host, "injecting codex network allowlist for host");
|
||||
|
||||
vec![
|
||||
"-c".into(),
|
||||
"network_proxy.mode=\"full\"".into(),
|
||||
"-c".into(),
|
||||
format!("network_proxy.domains.\"{host}\"=\"allow\""),
|
||||
]
|
||||
}
|
||||
|
||||
pub fn normalize_agent_args(command: &str, agent_args: Vec<String>) -> Vec<String> {
|
||||
let normalized = agent_args
|
||||
.into_iter()
|
||||
@@ -585,7 +634,14 @@ impl Config {
|
||||
// Legacy env-var propagation is intentionally NOT done here.
|
||||
// Call `propagate_legacy_env_vars()` before the tokio runtime starts
|
||||
// (in the sync `fn main()` wrapper) — see Rust 2024 edition safety.
|
||||
let mut args = CliArgs::parse();
|
||||
let args = CliArgs::parse();
|
||||
Self::from_args(args)
|
||||
}
|
||||
|
||||
/// Build a `Config` from already-parsed `CliArgs`. Separated from `from_cli()` so
|
||||
/// tests can construct `CliArgs` via `CliArgs::try_parse_from` and exercise the full
|
||||
/// validation path without going through process args.
|
||||
pub fn from_args(mut args: CliArgs) -> Result<Self, ConfigError> {
|
||||
let keys = Keys::parse(&args.private_key)?;
|
||||
// Best-effort zeroize: overwrite the raw private key string to reduce
|
||||
// exposure via core dumps or heap inspection (#41). Without the `zeroize`
|
||||
@@ -659,7 +715,16 @@ impl Config {
|
||||
));
|
||||
}
|
||||
|
||||
let agent_args = normalize_agent_args(&agent_command, args.agent_args);
|
||||
let mut agent_args = normalize_agent_args(&agent_command, args.agent_args);
|
||||
|
||||
// Prepend Codex network allowlist flags so buzz-cli (an MCP subprocess)
|
||||
// can reach the relay through Codex's sandbox proxy. No-op for non-Codex agents.
|
||||
let network_args = codex_network_args(&agent_command, &args.relay_url);
|
||||
if !network_args.is_empty() {
|
||||
let mut merged = network_args;
|
||||
merged.extend(agent_args);
|
||||
agent_args = merged;
|
||||
}
|
||||
|
||||
// Finding #49b — warn on invalid UUIDs in --channels.
|
||||
if let Some(ref channels) = args.channels {
|
||||
@@ -744,7 +809,6 @@ impl Config {
|
||||
)));
|
||||
}
|
||||
|
||||
// ── Inbound author gate validation ──────────────────────────────────
|
||||
let respond_to_allowlist = if args.respond_to == RespondTo::Allowlist {
|
||||
let raw = args.respond_to_allowlist.unwrap_or_default();
|
||||
if raw.is_empty() {
|
||||
@@ -762,7 +826,31 @@ impl Config {
|
||||
HashSet::new()
|
||||
};
|
||||
|
||||
// ── Persona pack resolution ──────────────────────────────────────────
|
||||
// Validate respond_to against the allowed set.
|
||||
let allowed_respond_to = if let Some(raw) = args.allowed_respond_to {
|
||||
// Validate each entry is a known RespondTo mode.
|
||||
for s in &raw {
|
||||
RespondTo::from_str(s.trim(), true).map_err(|_| {
|
||||
ConfigError::ConfigFile(format!(
|
||||
"invalid value in BUZZ_ACP_ALLOWED_RESPOND_TO: '{s}' \
|
||||
(valid values: owner-only, allowlist, anyone, nobody)"
|
||||
))
|
||||
})?;
|
||||
}
|
||||
let allowed_modes: Vec<String> = raw.iter().map(|s| s.trim().to_string()).collect();
|
||||
if !allowed_modes.is_empty() && !allowed_modes.contains(&args.respond_to.to_string()) {
|
||||
return Err(ConfigError::ConfigFile(format!(
|
||||
"respond_to '{}' is not permitted on this deployment \
|
||||
(BUZZ_ACP_ALLOWED_RESPOND_TO={})",
|
||||
args.respond_to,
|
||||
raw.join(",")
|
||||
)));
|
||||
}
|
||||
allowed_modes
|
||||
} else {
|
||||
Vec::new()
|
||||
};
|
||||
|
||||
//
|
||||
// Precedence: CLI/env args > persona values > built-in defaults.
|
||||
// Persona fills in what's missing. Explicit flags always win.
|
||||
@@ -810,7 +898,6 @@ impl Config {
|
||||
}
|
||||
let model = args.model.or(persona_model);
|
||||
|
||||
// ── Multiple-event-handling validation ──────────────────────────────
|
||||
if matches!(
|
||||
args.multiple_event_handling,
|
||||
MultipleEventHandling::Interrupt | MultipleEventHandling::OwnerInterrupt
|
||||
@@ -855,6 +942,7 @@ impl Config {
|
||||
permission_mode: args.permission_mode,
|
||||
respond_to: args.respond_to,
|
||||
respond_to_allowlist,
|
||||
allowed_respond_to,
|
||||
persona_env_vars,
|
||||
relay_observer: args.relay_observer,
|
||||
agent_owner: args.agent_owner.map(|s| s.trim().to_ascii_lowercase()),
|
||||
@@ -873,8 +961,15 @@ impl Config {
|
||||
}
|
||||
other => format!("respond_to={other}"),
|
||||
};
|
||||
let allowed_respond_to_detail = if self.allowed_respond_to.is_empty() {
|
||||
String::new()
|
||||
} else {
|
||||
let mut modes = self.allowed_respond_to.clone();
|
||||
modes.sort();
|
||||
format!(" allowed_respond_to=[{}]", modes.join(","))
|
||||
};
|
||||
format!(
|
||||
"relay={} pubkey={} agent_cmd={} {} mcp_cmd={} idle_timeout={}s max_turn={}s agents={} heartbeat={}s subscribe={:?} dedup={:?} meh={:?} ignore_self={} context_limit={} max_turns_per_session={} presence={} typing={} memory={} model={} permission_mode={} {}",
|
||||
"relay={} pubkey={} agent_cmd={} {} mcp_cmd={} idle_timeout={}s max_turn={}s agents={} heartbeat={}s subscribe={:?} dedup={:?} meh={:?} ignore_self={} context_limit={} max_turns_per_session={} presence={} typing={} memory={} model={} permission_mode={} {}{}",
|
||||
self.relay_url,
|
||||
self.keys.public_key().to_hex(),
|
||||
self.agent_command,
|
||||
@@ -896,12 +991,11 @@ impl Config {
|
||||
self.model.as_deref().unwrap_or("(agent default)"),
|
||||
self.permission_mode,
|
||||
respond_to_detail,
|
||||
allowed_respond_to_detail,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// ── TOML config file ──────────────────────────────────────────────────────────
|
||||
|
||||
#[derive(Debug, serde::Deserialize)]
|
||||
struct TomlConfig {
|
||||
#[serde(default)]
|
||||
@@ -985,8 +1079,6 @@ pub fn load_rules(path: &std::path::Path) -> Result<Vec<SubscriptionRule>, Confi
|
||||
Ok(config.rules)
|
||||
}
|
||||
|
||||
// ── Subscription resolution ───────────────────────────────────────────────────
|
||||
|
||||
/// Resolve per-channel NIP-01 filters from config + discovered channels.
|
||||
pub fn resolve_channel_filters(
|
||||
config: &Config,
|
||||
@@ -1181,12 +1273,11 @@ fn rule_applies_to_channel(rule: &SubscriptionRule, channel_id: Uuid) -> bool {
|
||||
}
|
||||
}
|
||||
|
||||
// ── Tests ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::filter::{ChannelScope, SubscriptionRule};
|
||||
use clap::{Parser, ValueEnum};
|
||||
|
||||
/// Build a minimal Config for testing without CLI parsing.
|
||||
fn test_config(mode: SubscribeMode) -> Config {
|
||||
@@ -1221,6 +1312,7 @@ mod tests {
|
||||
permission_mode: PermissionMode::BypassPermissions,
|
||||
respond_to: RespondTo::Anyone,
|
||||
respond_to_allowlist: HashSet::new(),
|
||||
allowed_respond_to: Vec::new(),
|
||||
persona_env_vars: vec![],
|
||||
relay_observer: false,
|
||||
agent_owner: None,
|
||||
@@ -1249,8 +1341,6 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
// ── resolve_channel_filters: Mentions mode ───────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn test_mentions_mode_default_kinds() {
|
||||
let config = test_config(SubscribeMode::Mentions);
|
||||
@@ -1386,7 +1476,112 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
// ── resolve_channel_filters: All mode ────────────────────────────────────
|
||||
// --- codex_network_args tests ---
|
||||
|
||||
#[test]
|
||||
fn codex_network_args_wss_url() {
|
||||
let args = codex_network_args("codex-acp", "wss://sprout-oss.stage.blox.sqprod.co");
|
||||
assert_eq!(
|
||||
args,
|
||||
vec![
|
||||
"-c",
|
||||
"network_proxy.mode=\"full\"",
|
||||
"-c",
|
||||
"network_proxy.domains.\"sprout-oss.stage.blox.sqprod.co\"=\"allow\"",
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn codex_network_args_ws_url() {
|
||||
let args = codex_network_args("codex-acp", "ws://localhost:3000");
|
||||
assert_eq!(
|
||||
args,
|
||||
vec![
|
||||
"-c",
|
||||
"network_proxy.mode=\"full\"",
|
||||
"-c",
|
||||
"network_proxy.domains.\"localhost\"=\"allow\"",
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn codex_network_args_https_url() {
|
||||
let args = codex_network_args("codex-acp", "https://relay.example.com/path");
|
||||
assert_eq!(
|
||||
args,
|
||||
vec![
|
||||
"-c",
|
||||
"network_proxy.mode=\"full\"",
|
||||
"-c",
|
||||
"network_proxy.domains.\"relay.example.com\"=\"allow\"",
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn codex_network_args_http_url_with_port() {
|
||||
let args = codex_network_args("codex-acp", "http://relay.example.com:8080/query");
|
||||
assert_eq!(
|
||||
args,
|
||||
vec![
|
||||
"-c",
|
||||
"network_proxy.mode=\"full\"",
|
||||
"-c",
|
||||
"network_proxy.domains.\"relay.example.com\"=\"allow\"",
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn codex_network_args_bare_codex_command() {
|
||||
// "codex" (not "codex-acp") should also get the args.
|
||||
let args = codex_network_args("codex", "wss://relay.example.com");
|
||||
assert_eq!(
|
||||
args,
|
||||
vec![
|
||||
"-c",
|
||||
"network_proxy.mode=\"full\"",
|
||||
"-c",
|
||||
"network_proxy.domains.\"relay.example.com\"=\"allow\"",
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn codex_network_args_full_path_codex_command() {
|
||||
// Full path like /usr/local/bin/codex-acp should be normalized.
|
||||
let args = codex_network_args("/usr/local/bin/codex-acp", "wss://relay.example.com");
|
||||
assert_eq!(
|
||||
args,
|
||||
vec![
|
||||
"-c",
|
||||
"network_proxy.mode=\"full\"",
|
||||
"-c",
|
||||
"network_proxy.domains.\"relay.example.com\"=\"allow\"",
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn codex_network_args_non_codex_agent_returns_empty() {
|
||||
assert!(codex_network_args("goose", "wss://relay.example.com").is_empty());
|
||||
assert!(codex_network_args("claude-agent-acp", "wss://relay.example.com").is_empty());
|
||||
assert!(codex_network_args("buzz-agent", "wss://relay.example.com").is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn codex_network_args_empty_relay_url_returns_empty() {
|
||||
// Empty string fails Url::parse — graceful empty return.
|
||||
assert!(codex_network_args("codex-acp", "").is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn codex_network_args_schemeless_string_returns_empty() {
|
||||
// A bare string with no scheme fails Url::parse — graceful empty return.
|
||||
assert!(codex_network_args("codex-acp", "not-a-url").is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_all_mode_wildcard() {
|
||||
@@ -1416,8 +1611,6 @@ mod tests {
|
||||
assert_eq!(f.kinds.as_ref().unwrap(), &[9, 7]);
|
||||
}
|
||||
|
||||
// ── resolve_channel_filters: channels_override ───────────────────────────
|
||||
|
||||
#[test]
|
||||
fn test_channels_override_filters_to_discovered() {
|
||||
let mut config = test_config(SubscribeMode::All);
|
||||
@@ -1437,8 +1630,6 @@ mod tests {
|
||||
assert!(!result.contains_key(&ch_unknown));
|
||||
}
|
||||
|
||||
// ── resolve_channel_filters: Config mode ─────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn test_config_mode_single_rule_all_channels() {
|
||||
let config = test_config(SubscribeMode::Config);
|
||||
@@ -1543,8 +1734,6 @@ mod tests {
|
||||
assert!(!f.require_mention, "most permissive (false) should win");
|
||||
}
|
||||
|
||||
// ── rule_applies_to_channel ──────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn test_rule_applies_all() {
|
||||
let rule = make_rule("test", ChannelScope::All("all".into()), vec![], false);
|
||||
@@ -1580,8 +1769,6 @@ mod tests {
|
||||
assert!(!rule_applies_to_channel(&rule, Uuid::new_v4()));
|
||||
}
|
||||
|
||||
// ── load_rules validation ────────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn test_load_rules_valid_toml() {
|
||||
let dir = std::env::temp_dir().join("buzz-acp-test-valid");
|
||||
@@ -1726,8 +1913,6 @@ channels = "ALL"
|
||||
std::fs::remove_dir_all(&dir).ok();
|
||||
}
|
||||
|
||||
// ── heartbeat validation ─────────────────────────────────────────────────
|
||||
|
||||
fn validate_heartbeat_interval(secs: u64) -> Result<(), ConfigError> {
|
||||
if secs > 0 && secs < 10 {
|
||||
return Err(ConfigError::ConfigFile(
|
||||
@@ -1770,8 +1955,6 @@ channels = "ALL"
|
||||
assert!(err.to_string().contains("heartbeat interval must be 0"));
|
||||
}
|
||||
|
||||
// ── turn-liveness validation ─────────────────────────────────────────────
|
||||
|
||||
fn validate_turn_liveness(secs: u64) -> Result<(), ConfigError> {
|
||||
if secs > 0 && secs < 5 {
|
||||
return Err(ConfigError::ConfigFile(
|
||||
@@ -1808,8 +1991,6 @@ channels = "ALL"
|
||||
assert!(err.to_string().contains("turn liveness interval must be 0"));
|
||||
}
|
||||
|
||||
// ── summary includes agents and heartbeat ────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn test_summary_includes_agents_and_heartbeat() {
|
||||
let config = test_config(SubscribeMode::Mentions);
|
||||
@@ -1840,8 +2021,6 @@ channels = "ALL"
|
||||
);
|
||||
}
|
||||
|
||||
// ── memory toggle ───────────────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn test_memory_enabled_default_true() {
|
||||
let config = test_config(SubscribeMode::Mentions);
|
||||
@@ -1872,8 +2051,6 @@ channels = "ALL"
|
||||
);
|
||||
}
|
||||
|
||||
// ── permission mode ─────────────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn test_permission_mode_wire_strings() {
|
||||
assert_eq!(PermissionMode::Default.as_wire_str(), "default");
|
||||
@@ -1975,8 +2152,6 @@ channels = "ALL"
|
||||
}
|
||||
}
|
||||
|
||||
// ── Idle timeout config precedence ─────────────────────────────────────
|
||||
|
||||
/// Helper: resolve idle_timeout_secs using the same precedence logic as Config::from_args.
|
||||
/// Precedence: explicit --idle-timeout > --turn-timeout (deprecated) > `DEFAULT_IDLE_TIMEOUT_SECS`.
|
||||
fn resolve_idle_timeout(idle: Option<u64>, turn: Option<u64>) -> u64 {
|
||||
@@ -2033,8 +2208,6 @@ channels = "ALL"
|
||||
);
|
||||
}
|
||||
|
||||
// ── RespondTo tests ────────────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn test_respond_to_default_is_owner_only() {
|
||||
assert_eq!(RespondTo::default(), RespondTo::OwnerOnly);
|
||||
@@ -2091,8 +2264,6 @@ channels = "ALL"
|
||||
);
|
||||
}
|
||||
|
||||
// ── validate_allowlist tests ───────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn test_validate_allowlist_valid_entries() {
|
||||
let entries = vec!["ab".repeat(32), "cd".repeat(32)];
|
||||
@@ -2165,8 +2336,6 @@ channels = "ALL"
|
||||
assert!(result.is_empty());
|
||||
}
|
||||
|
||||
// ── Idle timeout constant + guard (PR #935) ───────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn default_idle_timeout_is_900_seconds() {
|
||||
// Lock the constant value so accidental changes are caught.
|
||||
@@ -2192,4 +2361,187 @@ channels = "ALL"
|
||||
"default idle (900) must be less than default max_turn (3600)"
|
||||
);
|
||||
}
|
||||
|
||||
// --- BUZZ_ACP_ALLOWED_RESPOND_TO gate ---
|
||||
|
||||
fn parse_allowed_respond_to(raw: &[&str]) -> Result<HashSet<RespondTo>, ConfigError> {
|
||||
let mut set = HashSet::new();
|
||||
for s in raw {
|
||||
let mode = RespondTo::from_str(s.trim(), true).map_err(|_| {
|
||||
ConfigError::ConfigFile(format!(
|
||||
"invalid value in BUZZ_ACP_ALLOWED_RESPOND_TO: '{s}' \
|
||||
(valid values: owner-only, allowlist, anyone, nobody)"
|
||||
))
|
||||
})?;
|
||||
set.insert(mode);
|
||||
}
|
||||
Ok(set)
|
||||
}
|
||||
|
||||
fn check_allowed_respond_to(
|
||||
allowed_raw: &[&str],
|
||||
respond_to: RespondTo,
|
||||
) -> Result<(), ConfigError> {
|
||||
let set = parse_allowed_respond_to(allowed_raw)?;
|
||||
if !set.is_empty() && !set.contains(&respond_to) {
|
||||
return Err(ConfigError::ConfigFile(format!(
|
||||
"respond_to '{}' is not permitted on this deployment \
|
||||
(BUZZ_ACP_ALLOWED_RESPOND_TO={})",
|
||||
respond_to,
|
||||
allowed_raw.join(",")
|
||||
)));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn allowed_respond_to_rejects_disallowed_mode() {
|
||||
let result = check_allowed_respond_to(&["owner-only", "allowlist"], RespondTo::Anyone);
|
||||
assert!(
|
||||
result.is_err(),
|
||||
"anyone should be rejected when not in allowed set"
|
||||
);
|
||||
let msg = result.unwrap_err().to_string();
|
||||
assert!(
|
||||
msg.contains("not permitted"),
|
||||
"error should mention 'not permitted': {msg}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn allowed_respond_to_accepts_allowed_mode() {
|
||||
let result = check_allowed_respond_to(&["owner-only", "allowlist"], RespondTo::OwnerOnly);
|
||||
assert!(result.is_ok(), "owner-only should be accepted: {result:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn allowed_respond_to_empty_allows_all() {
|
||||
// No restriction — anyone is accepted.
|
||||
let result = check_allowed_respond_to(&[], RespondTo::Anyone);
|
||||
assert!(
|
||||
result.is_ok(),
|
||||
"empty allowed set should permit any mode: {result:?}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn allowed_respond_to_rejects_invalid_mode_string() {
|
||||
let result = parse_allowed_respond_to(&["owner-only", "badvalue"]);
|
||||
assert!(result.is_err(), "invalid mode string should be rejected");
|
||||
let msg = result.unwrap_err().to_string();
|
||||
assert!(
|
||||
msg.contains("invalid value in BUZZ_ACP_ALLOWED_RESPOND_TO"),
|
||||
"error should name the env var: {msg}"
|
||||
);
|
||||
assert!(
|
||||
msg.contains("badvalue"),
|
||||
"error should name the bad value: {msg}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn allowed_respond_to_summary_shows_restriction_when_set() {
|
||||
let mut config = test_config(SubscribeMode::Mentions);
|
||||
config.allowed_respond_to = vec!["owner-only".to_string(), "allowlist".to_string()];
|
||||
let s = config.summary();
|
||||
assert!(
|
||||
s.contains("allowed_respond_to="),
|
||||
"summary should include allowed_respond_to when set: {s}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn allowed_respond_to_summary_omitted_when_empty() {
|
||||
let config = test_config(SubscribeMode::Mentions);
|
||||
let s = config.summary();
|
||||
assert!(
|
||||
!s.contains("allowed_respond_to="),
|
||||
"summary should not include allowed_respond_to when empty: {s}"
|
||||
);
|
||||
}
|
||||
|
||||
// --- Integration tests: full env-var → CliArgs → Config::from_args() path ---
|
||||
//
|
||||
// These tests exercise the actual wiring: BUZZ_ACP_ALLOWED_RESPOND_TO in the
|
||||
// environment causes clap to populate CliArgs::allowed_respond_to, which then
|
||||
// flows through Config::from_args() to produce a ConfigError. If the #[arg(env)]
|
||||
// attribute or field name were removed, these tests would fail.
|
||||
//
|
||||
// We pass the value via the CLI flag (`--allowed-respond-to`) rather than
|
||||
// std::env::set_var to avoid test-parallelism races on shared env state.
|
||||
// The env-var wiring is covered by the clap #[arg(env)] attribute itself.
|
||||
|
||||
// A minimal valid private key for test use (secp256k1 scalar = 1).
|
||||
const TEST_PRIVATE_KEY: &str =
|
||||
"0000000000000000000000000000000000000000000000000000000000000001";
|
||||
|
||||
#[test]
|
||||
fn allowed_respond_to_full_path_rejects_disallowed_mode() {
|
||||
// --allowed-respond-to=owner-only,allowlist + --respond-to=anyone → ConfigError
|
||||
let args = CliArgs::try_parse_from([
|
||||
"buzz-acp",
|
||||
"--private-key",
|
||||
TEST_PRIVATE_KEY,
|
||||
"--respond-to",
|
||||
"anyone",
|
||||
"--allowed-respond-to",
|
||||
"owner-only,allowlist",
|
||||
])
|
||||
.expect("clap should parse args");
|
||||
let result = Config::from_args(args);
|
||||
|
||||
assert!(
|
||||
result.is_err(),
|
||||
"from_args should reject respond_to=anyone when not in allowed set"
|
||||
);
|
||||
let msg = result.unwrap_err().to_string();
|
||||
assert!(
|
||||
msg.contains("not permitted"),
|
||||
"error should mention 'not permitted': {msg}"
|
||||
);
|
||||
assert!(
|
||||
msg.contains("anyone"),
|
||||
"error should name the disallowed mode: {msg}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn allowed_respond_to_full_path_accepts_allowed_mode() {
|
||||
// --allowed-respond-to=owner-only,allowlist + --respond-to=owner-only → Ok
|
||||
let args = CliArgs::try_parse_from([
|
||||
"buzz-acp",
|
||||
"--private-key",
|
||||
TEST_PRIVATE_KEY,
|
||||
"--respond-to",
|
||||
"owner-only",
|
||||
"--allowed-respond-to",
|
||||
"owner-only,allowlist",
|
||||
])
|
||||
.expect("clap should parse args");
|
||||
let result = Config::from_args(args);
|
||||
|
||||
assert!(
|
||||
result.is_ok(),
|
||||
"from_args should accept respond_to=owner-only when in allowed set: {result:?}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn allowed_respond_to_full_path_unset_allows_all() {
|
||||
// No --allowed-respond-to flag → anyone is accepted.
|
||||
let args = CliArgs::try_parse_from([
|
||||
"buzz-acp",
|
||||
"--private-key",
|
||||
TEST_PRIVATE_KEY,
|
||||
"--respond-to",
|
||||
"anyone",
|
||||
])
|
||||
.expect("clap should parse args");
|
||||
let result = Config::from_args(args);
|
||||
|
||||
assert!(
|
||||
result.is_ok(),
|
||||
"from_args should accept any mode when allowed list is unset: {result:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,8 +11,6 @@ use std::time::Duration;
|
||||
|
||||
use tracing::{error, warn};
|
||||
|
||||
// ── Errors ────────────────────────────────────────────────────────────────────
|
||||
|
||||
/// Errors that can occur during filter expression evaluation.
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum FilterError {
|
||||
@@ -24,8 +22,6 @@ pub enum FilterError {
|
||||
EvalError(String),
|
||||
}
|
||||
|
||||
// ── FilterContext ─────────────────────────────────────────────────────────────
|
||||
|
||||
/// Variables extracted from a Nostr event for use in filter expressions.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct FilterContext {
|
||||
@@ -54,8 +50,6 @@ impl FilterContext {
|
||||
}
|
||||
}
|
||||
|
||||
// ── SubscriptionRule ──────────────────────────────────────────────────────────
|
||||
|
||||
/// Scope of channels a subscription rule applies to.
|
||||
#[derive(Debug, Clone, serde::Deserialize)]
|
||||
#[serde(untagged)]
|
||||
@@ -151,8 +145,6 @@ impl Clone for SubscriptionRule {
|
||||
}
|
||||
}
|
||||
|
||||
// ── MatchedRule ───────────────────────────────────────────────────────────────
|
||||
|
||||
/// The result of a successful rule match.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct MatchedRule {
|
||||
@@ -163,8 +155,6 @@ pub struct MatchedRule {
|
||||
pub prompt_tag: String,
|
||||
}
|
||||
|
||||
// ── evaluate_filter ───────────────────────────────────────────────────────────
|
||||
|
||||
/// Maximum expression length accepted by `evaluate_filter`.
|
||||
///
|
||||
/// Bounds worst-case O(2^n) evaluation paths. The spawn_blocking thread cannot
|
||||
@@ -276,7 +266,6 @@ fn build_eval_context(ctx: &FilterContext) -> Result<evalexpr::HashMapContext, S
|
||||
|
||||
let mut eval_ctx = HashMapContext::new();
|
||||
|
||||
// ── Custom string functions ───────────────────────────────────────────────
|
||||
// evalexpr v11 does not ship these helpers; register them manually.
|
||||
|
||||
eval_ctx
|
||||
@@ -325,8 +314,6 @@ fn build_eval_context(ctx: &FilterContext) -> Result<evalexpr::HashMapContext, S
|
||||
)
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
// ── Event variables ───────────────────────────────────────────────────────
|
||||
|
||||
eval_ctx
|
||||
.set_value("content".into(), Value::String(ctx.content.clone()))
|
||||
.map_err(|e| e.to_string())?;
|
||||
@@ -346,8 +333,6 @@ fn build_eval_context(ctx: &FilterContext) -> Result<evalexpr::HashMapContext, S
|
||||
Ok(eval_ctx)
|
||||
}
|
||||
|
||||
// ── match_event ───────────────────────────────────────────────────────────────
|
||||
|
||||
/// Consecutive timeout threshold before a rule is treated as disabled.
|
||||
///
|
||||
/// After this many back-to-back timeouts on a single rule, the rule is logged
|
||||
@@ -474,8 +459,6 @@ pub async fn match_event(
|
||||
None
|
||||
}
|
||||
|
||||
// ── Tests ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -525,8 +508,6 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
// ── FilterContext ─────────────────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn test_filter_context_from_event() {
|
||||
let event = make_event(9, "hello world");
|
||||
@@ -540,8 +521,6 @@ mod tests {
|
||||
assert_eq!(ctx.timestamp, event.created_at.as_secs());
|
||||
}
|
||||
|
||||
// ── evaluate_filter ───────────────────────────────────────────────────────
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_evaluate_filter_str_contains() {
|
||||
let event = make_event(9, "P1 incident in production");
|
||||
@@ -598,8 +577,6 @@ mod tests {
|
||||
assert!(result);
|
||||
}
|
||||
|
||||
// ── match_event ───────────────────────────────────────────────────────────
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_match_event_first_match_wins() {
|
||||
let event = make_event(9, "hello");
|
||||
@@ -704,8 +681,6 @@ mod tests {
|
||||
assert!(result.is_none());
|
||||
}
|
||||
|
||||
// ── ChannelScope ──────────────────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn test_channel_scope_all() {
|
||||
let scope = ChannelScope::All("all".into());
|
||||
@@ -736,8 +711,6 @@ mod tests {
|
||||
assert!(!scope.matches(&id_c));
|
||||
}
|
||||
|
||||
// ── prompt_tag fallback ───────────────────────────────────────────────────
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_prompt_tag_falls_back_to_name() {
|
||||
let event = make_event(9, "hello");
|
||||
@@ -756,8 +729,6 @@ mod tests {
|
||||
assert_eq!(matched.prompt_tag, "my-rule");
|
||||
}
|
||||
|
||||
// ── Fail-closed filter error handling (finding #25) ───────────────────────
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_filter_error_fails_closed_no_fallthrough() {
|
||||
// A broken filter on rule[0] must NOT fall through to rule[1].
|
||||
|
||||
@@ -38,8 +38,6 @@ use tokio::sync::{mpsc, watch};
|
||||
use tracing_subscriber::EnvFilter;
|
||||
use uuid::Uuid;
|
||||
|
||||
// ── Subcommand dispatch ───────────────────────────────────────────────────────
|
||||
|
||||
/// Check if argv[1] matches a subcommand name, before any clap parsing.
|
||||
///
|
||||
/// This avoids clap rejecting harness flags (like `--private-key`) that aren't
|
||||
@@ -55,8 +53,6 @@ fn is_subcommand(name: &str) -> bool {
|
||||
/// Timeout for the `buzz-acp models` subcommand (spawn + init + session/new).
|
||||
const MODELS_TIMEOUT: Duration = Duration::from_secs(10);
|
||||
|
||||
// ── Presence helper ───────────────────────────────────────────────────────────
|
||||
|
||||
/// Publish a kind:20001 presence update event via the WebSocket connection.
|
||||
///
|
||||
/// Ephemeral kinds (20000-29999) are rejected by the HTTP bridge, so presence
|
||||
@@ -81,8 +77,6 @@ async fn publish_presence(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// ── Owner resolution ──────────────────────────────────────────────────────────
|
||||
|
||||
/// Resolve the agent's owner pubkey at startup.
|
||||
///
|
||||
/// Priority:
|
||||
@@ -111,8 +105,6 @@ fn resolve_agent_owner(config: &Config) -> Option<String> {
|
||||
config.agent_owner.clone()
|
||||
}
|
||||
|
||||
// ── Owner cache ───────────────────────────────────────────────────────────────
|
||||
|
||||
/// Cache for the agent's owner pubkey.
|
||||
///
|
||||
/// Owner is now provided via `--agent-owner` config flag (no REST lookup).
|
||||
@@ -948,7 +940,6 @@ impl Drop for RespawnGuard {
|
||||
}
|
||||
}
|
||||
|
||||
// ── Finding #16: propagate_legacy_env_vars before tokio runtime ───────────────
|
||||
//
|
||||
// Sync env-var propagation must run before the tokio runtime starts so that
|
||||
// any child processes inherit the correct environment. This must happen in the
|
||||
@@ -966,7 +957,6 @@ async fn tokio_main() -> Result<()> {
|
||||
rustls::crypto::ring::default_provider()
|
||||
.install_default()
|
||||
.expect("failed to install rustls crypto provider");
|
||||
// ── Subcommand dispatch — before Config::from_cli() or any harness setup ──
|
||||
if is_subcommand("models") {
|
||||
// Strip the "models" token so clap doesn't reject it as a positional.
|
||||
// Keeps argv[0] (binary name) and passes everything after "models".
|
||||
@@ -1007,7 +997,6 @@ async fn tokio_main() -> Result<()> {
|
||||
);
|
||||
}
|
||||
|
||||
// ── Step 1: Spawn N ACP agent subprocesses and initialize ─────────────────
|
||||
//
|
||||
// Finding #10: one agent failing to start must not kill the whole pool.
|
||||
// We attempt each spawn under a 60-second timeout; failures are logged and
|
||||
@@ -1084,7 +1073,6 @@ async fn tokio_main() -> Result<()> {
|
||||
tracing::info!("agent_pool_ready agents={}", live_count);
|
||||
let mut pool = AgentPool::from_slots(agent_slots);
|
||||
|
||||
// ── Step 2: Connect to Buzz relay ──────────────────────────────────────
|
||||
//
|
||||
// Finding #22: capture a startup watermark BEFORE connecting to the relay.
|
||||
// This timestamp is used for membership notification replay (via
|
||||
@@ -1120,14 +1108,12 @@ async fn tokio_main() -> Result<()> {
|
||||
|
||||
tracing::info!("connected to relay at {}", config.relay_url);
|
||||
|
||||
// ── Step 2b: Subscribe to membership notifications ────────────────────────
|
||||
relay
|
||||
.subscribe_membership_notifications()
|
||||
.await
|
||||
.map_err(|e| anyhow::anyhow!("membership notification subscribe error: {e}"))?;
|
||||
tracing::info!("subscribed to membership notifications");
|
||||
|
||||
// ── Step 2c: Set initial presence ─────────────────────────────────────────
|
||||
let presence_publisher = relay.event_publisher();
|
||||
let presence_keys = config.keys.clone();
|
||||
if config.presence_enabled {
|
||||
@@ -1137,7 +1123,6 @@ async fn tokio_main() -> Result<()> {
|
||||
}
|
||||
}
|
||||
|
||||
// ── Step 2d: Resolve agent owner ────────────────────────────────────────
|
||||
// Priority: BUZZ_AUTH_TAG (NIP-OA attestation) → --agent-owner flag.
|
||||
let startup_owner: Option<String> = resolve_agent_owner(&config);
|
||||
if let Some(ref owner) = startup_owner {
|
||||
@@ -1201,7 +1186,6 @@ async fn tokio_main() -> Result<()> {
|
||||
}
|
||||
}
|
||||
|
||||
// ── Step 3: Discover channels and build subscription rules ────────────────
|
||||
let channel_info_map = relay
|
||||
.discover_channels()
|
||||
.await
|
||||
@@ -1247,7 +1231,6 @@ async fn tokio_main() -> Result<()> {
|
||||
}
|
||||
};
|
||||
|
||||
// ── Step 4: Subscribe to channels ────────────────────────────────────────
|
||||
let channel_filters = config::resolve_channel_filters(&config, &channel_ids, &rules);
|
||||
if channel_filters.is_empty() {
|
||||
tracing::warn!("no channel subscriptions resolved — agent will sit idle");
|
||||
@@ -1260,7 +1243,6 @@ async fn tokio_main() -> Result<()> {
|
||||
}
|
||||
}
|
||||
|
||||
// ── Step 5: Build shared prompt context ──────────────────────────────────
|
||||
let dedup_mode = config.dedup_mode;
|
||||
let mut queue = EventQueue::new(dedup_mode);
|
||||
|
||||
@@ -1304,7 +1286,6 @@ async fn tokio_main() -> Result<()> {
|
||||
);
|
||||
}
|
||||
|
||||
// ── Step 6: Heartbeat timer ───────────────────────────────────────────────
|
||||
let mut heartbeat = if config.heartbeat_interval_secs > 0 {
|
||||
let interval = Duration::from_secs(config.heartbeat_interval_secs);
|
||||
Some(tokio::time::interval_at(
|
||||
@@ -1316,7 +1297,6 @@ async fn tokio_main() -> Result<()> {
|
||||
};
|
||||
let mut heartbeat_in_flight = false;
|
||||
|
||||
// ── Step 6b: Presence heartbeat timer (refreshes 90s TTL every 60s) ───────
|
||||
let mut presence_heartbeat = if config.presence_enabled {
|
||||
let interval = Duration::from_secs(60);
|
||||
Some(tokio::time::interval_at(
|
||||
@@ -1327,7 +1307,6 @@ async fn tokio_main() -> Result<()> {
|
||||
None
|
||||
};
|
||||
|
||||
// ── Step 6c: Typing refresh timer (re-publishes kind:20002 every 3s) ──────
|
||||
let mut typing_refresh = if config.typing_enabled {
|
||||
let interval = Duration::from_secs(3);
|
||||
Some(tokio::time::interval_at(
|
||||
@@ -1340,7 +1319,6 @@ async fn tokio_main() -> Result<()> {
|
||||
let mut typing_channels: HashMap<Uuid, ThreadTags> = HashMap::new();
|
||||
let mut presence_task: Option<tokio::task::JoinHandle<()>> = None;
|
||||
|
||||
// ── Step 6d: Maintenance (slot refill + queue compaction) ────────────────
|
||||
// Runs at the TOP of every loop iteration via Instant check — cannot be
|
||||
// starved by the biased select. Slot refill spawns background tasks so
|
||||
// spawn_and_init never blocks the main loop.
|
||||
@@ -1353,7 +1331,6 @@ async fn tokio_main() -> Result<()> {
|
||||
// JoinSet for respawn tasks so shutdown can abort them.
|
||||
let mut respawn_tasks: tokio::task::JoinSet<()> = tokio::task::JoinSet::new();
|
||||
|
||||
// ── Step 7: Shutdown signal ───────────────────────────────────────────────
|
||||
let (shutdown_tx, mut shutdown_rx) = watch::channel(());
|
||||
|
||||
let tx = shutdown_tx.clone();
|
||||
@@ -1405,7 +1382,6 @@ async fn tokio_main() -> Result<()> {
|
||||
// and capture it in TaskMeta at dispatch time.
|
||||
let mut removed_channels: HashSet<Uuid> = HashSet::new();
|
||||
|
||||
// ── Finding #14: Per-slot crash history for circuit breaker ───────────────
|
||||
//
|
||||
// One SlotCircuit per agent slot. crash_times entries are pruned to the last
|
||||
// CIRCUIT_BREAKER_WINDOW on each respawn attempt. The Vec is indexed by
|
||||
@@ -1419,7 +1395,6 @@ async fn tokio_main() -> Result<()> {
|
||||
})
|
||||
.collect();
|
||||
|
||||
// ── Step 8: Main orchestration loop ──────────────────────────────────────
|
||||
//
|
||||
// Branches 1 & 2 both need to borrow `pool`, but they access different
|
||||
// fields (result_rx vs join_set). We use `rx_and_join_set()` to split the
|
||||
@@ -1430,7 +1405,6 @@ async fn tokio_main() -> Result<()> {
|
||||
}
|
||||
|
||||
loop {
|
||||
// ── Maintenance (runs at loop top — cannot be starved by biased select) ──
|
||||
if last_maintenance.elapsed() >= maintenance_interval {
|
||||
last_maintenance = std::time::Instant::now();
|
||||
queue.compact_expired_state();
|
||||
@@ -1470,7 +1444,6 @@ async fn tokio_main() -> Result<()> {
|
||||
}
|
||||
}
|
||||
|
||||
// ── Collect completed background respawns (non-blocking) ─────────────
|
||||
let mut respawn_collected = false;
|
||||
while let Ok(rr) = respawn_rx.try_recv() {
|
||||
crash_history[rr.index].respawn_in_flight = false;
|
||||
@@ -1552,7 +1525,6 @@ async fn tokio_main() -> Result<()> {
|
||||
Some(buzz_event) => {
|
||||
let kind_u32 = buzz_event.event.kind.as_u16() as u32;
|
||||
|
||||
// ── Membership notification handling ──────────────
|
||||
if kind_u32 == KIND_MEMBER_ADDED_NOTIFICATION
|
||||
|| kind_u32 == KIND_MEMBER_REMOVED_NOTIFICATION
|
||||
{
|
||||
@@ -1659,14 +1631,12 @@ async fn tokio_main() -> Result<()> {
|
||||
}
|
||||
continue;
|
||||
}
|
||||
// ── End membership notification handling ──────────
|
||||
|
||||
if config.ignore_self && buzz_event.event.pubkey.to_hex() == pubkey_hex {
|
||||
tracing::debug!(channel_id = %buzz_event.channel_id, "dropping self-authored event");
|
||||
continue;
|
||||
}
|
||||
|
||||
// ── Shutdown command handling ─────────────────────
|
||||
// Check: kind:9, content "!shutdown", from owner, mentions THIS agent.
|
||||
let is_shutdown = is_owner_control_command(
|
||||
&buzz_event.event,
|
||||
@@ -1691,9 +1661,7 @@ async fn tokio_main() -> Result<()> {
|
||||
// Don't drop it — it's a regular message that happens to
|
||||
// contain "!shutdown" from a non-owner.
|
||||
}
|
||||
// ── End shutdown command handling ──────────────────
|
||||
|
||||
// ── Cancel command handling ──────────────────────
|
||||
// Mirrors !shutdown: kind:9, content "!cancel", from
|
||||
// owner, mentions THIS agent. Must be BEFORE
|
||||
// queue.push() — the event content is moved by push.
|
||||
@@ -1726,9 +1694,7 @@ async fn tokio_main() -> Result<()> {
|
||||
}
|
||||
// Not from owner — fall through to normal prompt handling.
|
||||
}
|
||||
// ── End cancel command handling ───────────────────
|
||||
|
||||
// ── Rotate command handling ─────────────────────
|
||||
// Mirrors !shutdown / !cancel: kind:9, content
|
||||
// "!rotate", from owner, mentions THIS agent.
|
||||
//
|
||||
@@ -1773,9 +1739,7 @@ async fn tokio_main() -> Result<()> {
|
||||
}
|
||||
// Not from owner — fall through to normal prompt handling.
|
||||
}
|
||||
// ── End rotate command handling ──────────────────
|
||||
|
||||
// ── Inbound author gate ──────────────────────────
|
||||
// Coarse security policy: drop events from disallowed
|
||||
// authors before they reach subscription rules or the
|
||||
// agent. Must be AFTER !shutdown (owner can always
|
||||
@@ -1807,7 +1771,6 @@ async fn tokio_main() -> Result<()> {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
// ── End inbound author gate ──────────────────────
|
||||
|
||||
let matched = filter::match_event(&buzz_event.event, buzz_event.channel_id, &rules, &pubkey_hex).await;
|
||||
let prompt_tag = match matched {
|
||||
@@ -1838,7 +1801,6 @@ async fn tokio_main() -> Result<()> {
|
||||
pool::reaction_add(&rc, &event_id_hex, "👀").await;
|
||||
});
|
||||
}
|
||||
// ── Multiple-event-handling mode gate ─────────────
|
||||
// Event is already queued. If mode requires it AND
|
||||
// the channel has an in-flight task, fire cancel.
|
||||
if accepted && queue.is_channel_in_flight(buzz_event.channel_id) {
|
||||
@@ -1860,7 +1822,6 @@ async fn tokio_main() -> Result<()> {
|
||||
);
|
||||
}
|
||||
}
|
||||
// ── End mode gate ────────────────────────────────
|
||||
for (channel_id, thread_tags) in
|
||||
dispatch_pending(&mut pool, &mut queue, &ctx)
|
||||
{
|
||||
@@ -2016,7 +1977,6 @@ async fn tokio_main() -> Result<()> {
|
||||
}
|
||||
}
|
||||
|
||||
// ── Shutdown sequence ─────────────────────────────────────────────────────
|
||||
tracing::info!("shutdown: waiting for in-flight prompts");
|
||||
// 30 s is generous for in-flight prompts to be cancelled; using
|
||||
// max_turn_duration here would cause Ctrl+C to hang for up to an hour.
|
||||
@@ -2117,16 +2077,12 @@ async fn tokio_main() -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// ── Loop control ──────────────────────────────────────────────────────────────
|
||||
|
||||
#[derive(PartialEq)]
|
||||
enum LoopAction {
|
||||
Continue,
|
||||
Exit,
|
||||
}
|
||||
|
||||
// ── Owner control commands ───────────────────────────────────────────────────
|
||||
|
||||
fn event_mentions_agent(event: &nostr::Event, agent_pubkey_hex: &str) -> bool {
|
||||
event.tags.iter().any(|t| {
|
||||
t.as_slice().first().map(|s| s.as_str()) == Some("p")
|
||||
@@ -2145,8 +2101,6 @@ fn is_owner_control_command(
|
||||
&& event_mentions_agent(event, agent_pubkey_hex)
|
||||
}
|
||||
|
||||
// ── signal_in_flight_task ─────────────────────────────────────────────────────
|
||||
|
||||
/// Send a control signal to the in-flight task for `channel_id`.
|
||||
/// Returns `true` if a signal was sent, `false` if no in-flight task was found.
|
||||
fn signal_in_flight_task(
|
||||
@@ -2169,8 +2123,6 @@ fn signal_in_flight_task(
|
||||
false
|
||||
}
|
||||
|
||||
// ── dispatch_pending ──────────────────────────────────────────────────────────
|
||||
|
||||
/// Flush queued work to available agents.
|
||||
fn dispatch_pending(
|
||||
pool: &mut AgentPool,
|
||||
@@ -2246,8 +2198,6 @@ fn dispatch_pending(
|
||||
dispatched_channels
|
||||
}
|
||||
|
||||
// ── handle_prompt_result ──────────────────────────────────────────────────────
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn handle_prompt_result(
|
||||
pool: &mut AgentPool,
|
||||
@@ -2442,8 +2392,6 @@ fn handle_prompt_result(
|
||||
LoopAction::Continue
|
||||
}
|
||||
|
||||
// ── recover_panicked_agent ────────────────────────────────────────────────────
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn recover_panicked_agent(
|
||||
pool: &mut AgentPool,
|
||||
@@ -2540,8 +2488,6 @@ fn recover_panicked_agent(
|
||||
});
|
||||
}
|
||||
|
||||
// ── drain_ready_join_results ──────────────────────────────────────────────────
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn drain_ready_join_results(
|
||||
pool: &mut AgentPool,
|
||||
@@ -2579,8 +2525,6 @@ fn drain_ready_join_results(
|
||||
LoopAction::Continue
|
||||
}
|
||||
|
||||
// ── dispatch_heartbeat ────────────────────────────────────────────────────────
|
||||
|
||||
fn dispatch_heartbeat(
|
||||
pool: &mut AgentPool,
|
||||
ctx: &Arc<PromptContext>,
|
||||
@@ -2623,8 +2567,6 @@ fn dispatch_heartbeat(
|
||||
tracing::info!(agent = agent_index, "heartbeat_fired");
|
||||
}
|
||||
|
||||
// ── default_heartbeat_prompt ──────────────────────────────────────────────────
|
||||
|
||||
fn default_heartbeat_prompt() -> String {
|
||||
let now = chrono::Utc::now().to_rfc3339();
|
||||
format!(
|
||||
@@ -2644,8 +2586,6 @@ fn default_heartbeat_prompt() -> String {
|
||||
)
|
||||
}
|
||||
|
||||
// ── respawn_agent_into ────────────────────────────────────────────────────────
|
||||
|
||||
/// Spawn a background respawn task for a crashed agent slot.
|
||||
///
|
||||
/// Does the circuit breaker check synchronously (non-blocking), then spawns
|
||||
@@ -2703,8 +2643,6 @@ fn spawn_respawn_task(
|
||||
true
|
||||
}
|
||||
|
||||
// ── spawn_and_init ────────────────────────────────────────────────────────────
|
||||
|
||||
/// Spawn an agent subprocess and run the MCP `initialize` handshake.
|
||||
///
|
||||
/// Takes owned args so it can run in a background `tokio::spawn` task without
|
||||
@@ -2744,10 +2682,6 @@ async fn spawn_and_init(
|
||||
}
|
||||
}
|
||||
|
||||
// ── build_mcp_servers ─────────────────────────────────────────────────────────
|
||||
|
||||
// ── run_models ─────────────────────────────────────────────────────────────────
|
||||
|
||||
/// `buzz-acp models` — spawn an agent, query its available models, exit.
|
||||
///
|
||||
/// Flow: spawn → initialize → session/new → print models → shutdown.
|
||||
@@ -2932,8 +2866,6 @@ fn build_mcp_servers(config: &Config) -> Vec<McpServer> {
|
||||
}]
|
||||
}
|
||||
|
||||
// ── Tests ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
#[cfg(test)]
|
||||
mod heartbeat_base_prompt_tests {
|
||||
use super::*;
|
||||
@@ -3309,6 +3241,7 @@ mod build_mcp_servers_tests {
|
||||
permission_mode: config::PermissionMode::BypassPermissions,
|
||||
respond_to: config::RespondTo::Anyone,
|
||||
respond_to_allowlist: std::collections::HashSet::new(),
|
||||
allowed_respond_to: vec![],
|
||||
persona_env_vars: vec![],
|
||||
relay_observer: false,
|
||||
agent_owner: None,
|
||||
@@ -3467,6 +3400,7 @@ mod error_outcome_emission_tests {
|
||||
permission_mode: config::PermissionMode::BypassPermissions,
|
||||
respond_to: config::RespondTo::Anyone,
|
||||
respond_to_allowlist: HashSet::new(),
|
||||
allowed_respond_to: vec![],
|
||||
persona_env_vars: vec![],
|
||||
relay_observer: false,
|
||||
agent_owner: None,
|
||||
|
||||
+43
-47
@@ -40,12 +40,9 @@ use crate::queue::{
|
||||
};
|
||||
use crate::relay::{ChannelInfo, RestClient};
|
||||
|
||||
// ── FlushBatch Clone note ─────────────────────────────────────────────────────
|
||||
// FlushBatch and BatchEvent derive Clone (added in queue.rs) so we can store
|
||||
// a recoverable copy in TaskMeta for panic recovery in Queue mode.
|
||||
|
||||
// ── Types ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
/// Metadata stored per in-flight task for panic recovery.
|
||||
pub struct TaskMeta {
|
||||
pub agent_index: usize,
|
||||
@@ -255,8 +252,6 @@ pub struct PromptContext {
|
||||
pub memory_enabled: bool,
|
||||
}
|
||||
|
||||
// ── AgentPool impl ────────────────────────────────────────────────────────────
|
||||
|
||||
impl AgentPool {
|
||||
/// Create a pool from pre-indexed slots (may contain None for failed startups).
|
||||
///
|
||||
@@ -339,8 +334,6 @@ impl AgentPool {
|
||||
idle + checked_out
|
||||
}
|
||||
|
||||
// ── Accessors ─────────────────────────────────────────────────────────
|
||||
|
||||
pub fn task_map(&self) -> &HashMap<tokio::task::Id, TaskMeta> {
|
||||
&self.task_map
|
||||
}
|
||||
@@ -405,8 +398,6 @@ impl AgentPool {
|
||||
}
|
||||
}
|
||||
|
||||
// ── run_prompt_task ───────────────────────────────────────────────────────────
|
||||
|
||||
/// Timeout for a single pre-prompt context fetch attempt (thread/DM history).
|
||||
/// Each call gets this budget; with one retry the total worst-case is
|
||||
/// 2 × CONTEXT_FETCH_TIMEOUT + CONTEXT_FETCH_RETRY_DELAY ≈ 6.5 s.
|
||||
@@ -754,8 +745,6 @@ pub async fn run_prompt_task(
|
||||
result_tx: mpsc::UnboundedSender<PromptResult>,
|
||||
control_rx: Option<tokio::sync::oneshot::Receiver<ControlSignal>>,
|
||||
) {
|
||||
// ── Determine source and resolve/create session ───────────────────────
|
||||
|
||||
// Is this a channel prompt or a heartbeat?
|
||||
let source = match &batch {
|
||||
Some(b) => PromptSource::Channel(b.channel_id),
|
||||
@@ -786,7 +775,6 @@ pub async fn run_prompt_task(
|
||||
}),
|
||||
);
|
||||
|
||||
// ── Reaction cleanup guard ────────────────────────────────────────────
|
||||
// Collects event IDs up front. On drop (any exit path — normal, early
|
||||
// return, or panic), spawns best-effort cleanup of both 👀 and 💬.
|
||||
// See `ReactionGuard` docs for ordering guarantees and known edge cases.
|
||||
@@ -796,7 +784,6 @@ pub async fn run_prompt_task(
|
||||
.unwrap_or_default();
|
||||
let _reaction_guard = ReactionGuard::new(ctx.rest_client.clone(), reaction_ids.clone());
|
||||
|
||||
// ── Turn completion guard ─────────────────────────────────────────────
|
||||
// Emits `turn_completed` on any exit path. Captures observer handle and
|
||||
// metadata now, before the agent is moved into PromptResult.
|
||||
let _turn_guard = TurnCompletionGuard::new(
|
||||
@@ -806,7 +793,6 @@ pub async fn run_prompt_task(
|
||||
turn_id.clone(),
|
||||
);
|
||||
|
||||
// ── NIP-AE: fetch core engram before session creation ───────────────
|
||||
//
|
||||
// Core memory is delivered inside the system prompt the harness already
|
||||
// builds (system role for protocol >= 2, the `[System]` user-message
|
||||
@@ -966,8 +952,6 @@ pub async fn run_prompt_task(
|
||||
}),
|
||||
);
|
||||
|
||||
// ── Send initial_message on new channel sessions ──────────────────────
|
||||
|
||||
if is_new_session {
|
||||
if let (PromptSource::Channel(cid), Some(ref initial_msg)) = (&source, &ctx.initial_message)
|
||||
{
|
||||
@@ -1080,8 +1064,6 @@ pub async fn run_prompt_task(
|
||||
}
|
||||
}
|
||||
|
||||
// ── Build prompt text (with optional context fetch) ──────────────────
|
||||
|
||||
// When the batch is a single slash-command message (e.g. "@Eva /goal …"),
|
||||
// `slash_command` holds the bare command. It is sent as the FIRST prompt
|
||||
// content block so ACP connectors' slash-command detection
|
||||
@@ -1163,8 +1145,6 @@ pub async fn run_prompt_task(
|
||||
});
|
||||
}
|
||||
|
||||
// ── Send the actual prompt ────────────────────────────────────────────
|
||||
|
||||
// Slash-command pass-through sends the bare command as the first text
|
||||
// block (so connector detection fires), then each prompt section as its
|
||||
// own block. Per-section blocks let the observer size trimmer elide a
|
||||
@@ -1177,7 +1157,6 @@ pub async fn run_prompt_task(
|
||||
None => prompt_sections.iter().map(String::as_str).collect(),
|
||||
};
|
||||
|
||||
// ── Control-aware prompt dispatch ─────────────────────────────────────
|
||||
// When control_rx is Some (channel tasks), wrap the prompt in select! so
|
||||
// the main loop can cancel, interrupt, or rotate it. Heartbeats
|
||||
// (control_rx=None) take the simple await path — they are not controllable.
|
||||
@@ -1343,13 +1322,11 @@ pub async fn run_prompt_task(
|
||||
Ok(stop_reason) => {
|
||||
log_stop_reason(&source, &stop_reason);
|
||||
|
||||
// ── Session rotation on context exhaustion ────────────────
|
||||
let should_rotate = matches!(
|
||||
stop_reason,
|
||||
StopReason::MaxTokens | StopReason::MaxTurnRequests
|
||||
);
|
||||
|
||||
// ── Proactive turn-based rotation ─────────────────────────
|
||||
let should_rotate = should_rotate || {
|
||||
let limit = ctx.max_turns_per_session;
|
||||
if limit > 0 {
|
||||
@@ -1478,8 +1455,6 @@ pub async fn run_prompt_task(
|
||||
// _reaction_guard drops here → spawns clear_reactions for all exit paths.
|
||||
}
|
||||
|
||||
// ── Context fetching ──────────────────────────────────────────────────────────
|
||||
|
||||
/// Retry wrapper for context fetches: one retry with `CONTEXT_FETCH_RETRY_DELAY`
|
||||
/// on any `None` result. The closure is called twice at most.
|
||||
///
|
||||
@@ -1652,6 +1627,24 @@ fn collect_prompt_pubkeys(
|
||||
pubkeys
|
||||
}
|
||||
|
||||
/// Detect whether a kind:0 profile event belongs to an owned agent.
|
||||
///
|
||||
/// Agents carry a NIP-OA `["auth", owner_pk, conditions, sig]` tag in their
|
||||
/// profile; humans do not. This checks for the tag's presence/shape only — a
|
||||
/// cheap routing heuristic for reply anchoring, not a verified security gate
|
||||
/// (the signing path in `lib.rs::check_sibling_via_profile` does full
|
||||
/// verification where it matters).
|
||||
fn profile_event_is_agent(ev: &serde_json::Value) -> bool {
|
||||
ev.get("tags")
|
||||
.and_then(|t| t.as_array())
|
||||
.is_some_and(|tags| {
|
||||
tags.iter().any(|tag| {
|
||||
tag.as_array()
|
||||
.is_some_and(|parts| parts.len() == 4 && parts[0].as_str() == Some("auth"))
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/// Parse kind:0 profile events into a `PromptProfileLookup`.
|
||||
///
|
||||
/// Each kind:0 event has `pubkey` and JSON `content` with optional fields:
|
||||
@@ -1674,11 +1667,13 @@ fn parse_kind0_profile_lookup(json: serde_json::Value) -> Option<PromptProfileLo
|
||||
.get("nip05")
|
||||
.and_then(|v| v.as_str())
|
||||
.map(str::to_string);
|
||||
let is_agent = profile_event_is_agent(ev);
|
||||
lookup.insert(
|
||||
pk.to_ascii_lowercase(),
|
||||
PromptProfile {
|
||||
display_name,
|
||||
nip05_handle,
|
||||
is_agent,
|
||||
},
|
||||
);
|
||||
}
|
||||
@@ -2031,8 +2026,6 @@ fn parse_nostr_dm_response(json: serde_json::Value, limit: u32) -> Option<Conver
|
||||
})
|
||||
}
|
||||
|
||||
// ── Internal helpers ──────────────────────────────────────────────────────────
|
||||
|
||||
/// Return the batch for requeue only in Queue mode; drop it in Drop mode.
|
||||
#[inline]
|
||||
fn requeue_batch_if_queue(ctx: &PromptContext, batch: Option<FlushBatch>) -> Option<FlushBatch> {
|
||||
@@ -2067,7 +2060,6 @@ fn log_stop_reason(source: &PromptSource, stop_reason: &StopReason) {
|
||||
}
|
||||
}
|
||||
|
||||
// ── Reaction indicators ───────────────────────────────────────────────────────
|
||||
//
|
||||
// Two-phase lifecycle visible to users:
|
||||
// 👀 "seen" — event was queued and an agent will handle it
|
||||
@@ -2129,7 +2121,6 @@ impl Drop for ReactionGuard {
|
||||
}
|
||||
}
|
||||
|
||||
// ── Turn liveness emission ───────────────────────────────────────────────────
|
||||
// Periodically emits a `turn_liveness` observer event while a turn is in-flight,
|
||||
// so the desktop can prune turns whose host died without unwinding (kill -9 /
|
||||
// crash) far sooner than the no-activity backstop. Runs as a non-resolving
|
||||
@@ -2172,7 +2163,6 @@ async fn run_turn_liveness(
|
||||
}
|
||||
}
|
||||
|
||||
// ── Turn completion scope guard ──────────────────────────────────────────────
|
||||
// Emits a `turn_completed` observer event on drop, covering ALL exit paths
|
||||
// (success, error, timeout, cancel, panic) from `run_prompt_task`. Captures
|
||||
// observer handle and metadata at creation time so it remains valid even after
|
||||
@@ -2388,15 +2378,12 @@ async fn clear_reactions(rest: crate::relay::RestClient, event_ids: Vec<String>)
|
||||
}
|
||||
}
|
||||
|
||||
// ─── Unit Tests ──────────────────────────────────────────────────────────────
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use nostr::{EventBuilder, Keys, Kind, Tag};
|
||||
use serde_json::json;
|
||||
|
||||
// ── prepend_base_for_legacy regression tests ─────────────────────────────
|
||||
// These pin the initial_message dispatch path (run_prompt_task, ~line 855):
|
||||
// a legacy agent WITH a base_prompt must get [Base] prepended to the user
|
||||
// message. This is the exact regression that shipped in the round-2 bug.
|
||||
@@ -2425,7 +2412,6 @@ mod tests {
|
||||
assert_eq!(composed, "hello channel");
|
||||
}
|
||||
|
||||
// ── framed_system_prompt tests ───────────────────────────────────────────
|
||||
// Pin the session/new systemPrompt framing: each present prompt carries its
|
||||
// own header so the desktop observer can split into labeled sub-sections.
|
||||
|
||||
@@ -2493,8 +2479,6 @@ mod tests {
|
||||
assert!(workspace_section("").is_none());
|
||||
}
|
||||
|
||||
// ── with_core tests ──────────────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn test_with_core_appends_below_framed() {
|
||||
let framed = with_core(
|
||||
@@ -2527,8 +2511,6 @@ mod tests {
|
||||
assert!(with_core(None, None).is_none());
|
||||
}
|
||||
|
||||
// ── parse_thread_response tests ──────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn test_parse_thread_response_basic() {
|
||||
let json = json!({
|
||||
@@ -2618,8 +2600,6 @@ mod tests {
|
||||
assert!(parse_thread_response(json).is_none());
|
||||
}
|
||||
|
||||
// ── parse_dm_response tests ──────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn test_parse_dm_response_basic() {
|
||||
let json = json!({
|
||||
@@ -2730,8 +2710,6 @@ mod tests {
|
||||
assert!(parse_dm_response(json, 12).is_none());
|
||||
}
|
||||
|
||||
// ── json_to_context_message tests ────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn test_json_to_context_message_integer_timestamp() {
|
||||
let obj = json!({
|
||||
@@ -2826,10 +2804,33 @@ mod tests {
|
||||
Some(&PromptProfile {
|
||||
display_name: Some("Wes".into()),
|
||||
nip05_handle: Some("wes@example.com".into()),
|
||||
is_agent: false,
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_profile_event_is_agent_detects_nip_oa_auth_tag() {
|
||||
// Agent profile carries a 4-element NIP-OA ["auth", owner, cond, sig] tag.
|
||||
let agent_ev = json!({
|
||||
"pubkey": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
|
||||
"tags": [["auth", "owner_pk", "conditions", "sig"]],
|
||||
});
|
||||
assert!(profile_event_is_agent(&agent_ev));
|
||||
|
||||
// Human profile: no auth tag.
|
||||
let human_ev = json!({ "pubkey": "bbbb", "tags": [["t", "topic"]] });
|
||||
assert!(!profile_event_is_agent(&human_ev));
|
||||
|
||||
// Empty / missing tags → not an agent.
|
||||
assert!(!profile_event_is_agent(&json!({ "tags": [] })));
|
||||
assert!(!profile_event_is_agent(&json!({})));
|
||||
|
||||
// Malformed auth tag (wrong arity) → not treated as an agent.
|
||||
let malformed = json!({ "tags": [["auth", "owner_pk"]] });
|
||||
assert!(!profile_event_is_agent(&malformed));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_kind0_profile_lookup_returns_none_for_empty() {
|
||||
assert!(parse_kind0_profile_lookup(json!([])).is_none());
|
||||
@@ -2843,8 +2844,6 @@ mod tests {
|
||||
assert_eq!(msg.pubkey, "unknown");
|
||||
}
|
||||
|
||||
// ── pct_encode tests ─────────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn test_pct_encode_hex_passthrough() {
|
||||
let hex = "a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2";
|
||||
@@ -2880,8 +2879,6 @@ mod tests {
|
||||
assert_eq!(pct_encode(" "), "%20");
|
||||
}
|
||||
|
||||
// ── SessionState tests ───────────────────────────────────────────────
|
||||
|
||||
fn make_state() -> (SessionState, Uuid, Uuid) {
|
||||
let ch_a = Uuid::new_v4();
|
||||
let ch_b = Uuid::new_v4();
|
||||
@@ -3048,7 +3045,6 @@ mod tests {
|
||||
assert_eq!(s.core_sections.get(&ch_b).unwrap(), "core-b");
|
||||
}
|
||||
|
||||
// ── turn liveness emission ───────────────────────────────────────────────
|
||||
// `run_turn_liveness` is raced against a "prompt" future the same way
|
||||
// `run_prompt_task` does it: the prompt wins the select and the liveness
|
||||
// future is dropped. We assert what the observer saw.
|
||||
|
||||
+288
-130
@@ -20,8 +20,6 @@ use uuid::Uuid;
|
||||
|
||||
use crate::config::DedupMode;
|
||||
|
||||
// ── Reliability constants ─────────────────────────────────────────────────────
|
||||
|
||||
/// Maximum events queued per channel before oldest events are dropped.
|
||||
const MAX_PENDING_PER_CHANNEL: usize = 500;
|
||||
|
||||
@@ -40,8 +38,6 @@ const MAX_RETRY_DELAY_SECS: u64 = 300;
|
||||
/// In-flight deadline: max_turn (3600s) + 100s buffer.
|
||||
const IN_FLIGHT_DEADLINE_SECS: u64 = 3700;
|
||||
|
||||
// ── Types ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
/// An event waiting in the queue.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct QueuedEvent {
|
||||
@@ -71,8 +67,6 @@ pub struct FlushBatch {
|
||||
pub cancelled_events: Vec<BatchEvent>,
|
||||
}
|
||||
|
||||
// ── EventQueue ────────────────────────────────────────────────────────────────
|
||||
|
||||
/// Per-channel event queue with per-channel in-flight enforcement.
|
||||
///
|
||||
/// # State Machine
|
||||
@@ -564,8 +558,6 @@ impl Default for EventQueue {
|
||||
}
|
||||
}
|
||||
|
||||
// ── NIP-10 tag parsing ────────────────────────────────────────────────────────
|
||||
|
||||
/// Parsed thread relationship from NIP-10 `e` tags.
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct ThreadTags {
|
||||
@@ -628,8 +620,6 @@ pub fn parse_thread_tags(event: &Event) -> ThreadTags {
|
||||
}
|
||||
}
|
||||
|
||||
// ── Slash command detection ───────────────────────────────────────────────────
|
||||
|
||||
/// Extract a leading slash command from message content.
|
||||
///
|
||||
/// ACP connectors (claude-agent-acp, codex-acp) detect slash commands by
|
||||
@@ -709,8 +699,6 @@ pub fn slash_command_for_batch(batch: &FlushBatch, known_names: &[&str]) -> Opti
|
||||
extract_slash_command(&batch.events[0].event.content, known_names)
|
||||
}
|
||||
|
||||
// ── Prompt formatting ─────────────────────────────────────────────────────────
|
||||
|
||||
/// Conversation context fetched by the harness before prompting.
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum ConversationContext {
|
||||
@@ -748,6 +736,10 @@ pub struct PromptChannelInfo {
|
||||
pub struct PromptProfile {
|
||||
pub display_name: Option<String>,
|
||||
pub nip05_handle: Option<String>,
|
||||
/// True when this pubkey's kind:0 profile carries a NIP-OA `auth` tag,
|
||||
/// i.e. it is an owned agent rather than a human. Used to gate reply-anchor
|
||||
/// flattening (UX routing heuristic, not a security boundary).
|
||||
pub is_agent: bool,
|
||||
}
|
||||
|
||||
/// Pubkey-keyed profile lookup used while formatting ACP prompts.
|
||||
@@ -880,25 +872,100 @@ fn format_event_block(
|
||||
|
||||
/// Append a reply instruction when the agent is responding to a thread event.
|
||||
///
|
||||
/// Tells the agent to pass `--reply-to <event_id>` on every `buzz messages
|
||||
/// send` call in this turn, and not to broadcast to the channel so replies
|
||||
/// stay inside the thread.
|
||||
/// Tells the agent to default to `--reply-to <event_id>` for ordinary replies
|
||||
/// while still allowing an explicit human request to post at the channel root or
|
||||
/// top level.
|
||||
fn append_reply_instruction(s: &mut String, event_id: &str) {
|
||||
s.push_str(&format!(
|
||||
"\nIMPORTANT: When responding, use `--reply-to {event_id}` \
|
||||
on EVERY `buzz messages send` call in this turn. \
|
||||
Do not broadcast to the channel."
|
||||
"\nIMPORTANT: For ordinary replies in this turn, use `--reply-to {event_id}` \
|
||||
on `buzz messages send` so the conversation stays threaded. \
|
||||
If the human explicitly asks for a channel-root, top-level, \
|
||||
or broadcast post, send that message without `--reply-to`. \
|
||||
If the requested destination is ambiguous, ask before sending."
|
||||
));
|
||||
}
|
||||
|
||||
/// Append a new-thread reply instruction for a human-facing top-level mention.
|
||||
///
|
||||
/// The triggering mention has no thread tags, so the agent's reply becomes the
|
||||
/// thread root. Anchoring to the triggering event (rather than leaving the
|
||||
/// choice open) prevents replying into a stale/unrelated prior thread.
|
||||
fn append_new_thread_reply_instruction(s: &mut String, event_id: &str) {
|
||||
s.push_str(&format!(
|
||||
"\nIMPORTANT: This is a new top-level message. For ordinary replies in \
|
||||
this turn, use `--reply-to {event_id}` on `buzz messages send` — the \
|
||||
triggering message is the thread root. Do NOT reply into any other \
|
||||
(older) thread. If the human explicitly asks for a channel-root, \
|
||||
top-level, or broadcast post, send that message without `--reply-to`."
|
||||
));
|
||||
}
|
||||
|
||||
/// Decide whether a turn is human-facing for reply-anchor purposes.
|
||||
///
|
||||
/// A turn is human-facing when the triggering sender is a human, OR a human
|
||||
/// (other than this agent) is tagged in the triggering event. Identity comes
|
||||
/// from `PromptProfile::is_agent` (NIP-OA auth tag), not raw `p`-tag presence:
|
||||
/// agent-only mentions must not force flattening. When a participant cannot be
|
||||
/// classified (no profile fetched), it is treated as human — humans must not
|
||||
/// lose thread visibility to a misclassification.
|
||||
fn turn_is_human_facing(
|
||||
sender_pubkey: &str,
|
||||
thread_tags: &ThreadTags,
|
||||
profile_lookup: Option<&PromptProfileLookup>,
|
||||
) -> bool {
|
||||
let is_agent = |pubkey: &str| -> bool {
|
||||
profile_lookup
|
||||
.and_then(|m| m.get(&normalize_lookup_key(pubkey)))
|
||||
.map(|p| p.is_agent)
|
||||
// Unknown identity → treat as human (fail open for visibility).
|
||||
.unwrap_or(false)
|
||||
};
|
||||
|
||||
if !is_agent(sender_pubkey) {
|
||||
return true;
|
||||
}
|
||||
thread_tags.mentioned_pubkeys.iter().any(|pk| !is_agent(pk))
|
||||
}
|
||||
|
||||
/// Resolve the `--reply-to` anchor for a non-DM turn.
|
||||
///
|
||||
/// Returns `Some(id)` only for human-facing turns (see [`turn_is_human_facing`]):
|
||||
/// - in a thread → the thread ROOT, keeping the reply flat at layer 1
|
||||
/// - top-level → the triggering event id, which becomes the new thread root
|
||||
///
|
||||
/// Returns `None` for agent↔agent turns, leaving the agent free to nest deeply
|
||||
/// (intentional for agent coordination).
|
||||
fn resolve_reply_anchor(
|
||||
sender_pubkey: &str,
|
||||
thread_tags: &ThreadTags,
|
||||
triggering_event_id: &str,
|
||||
profile_lookup: Option<&PromptProfileLookup>,
|
||||
) -> Option<String> {
|
||||
if !turn_is_human_facing(sender_pubkey, thread_tags, profile_lookup) {
|
||||
return None;
|
||||
}
|
||||
Some(
|
||||
thread_tags
|
||||
.root_event_id
|
||||
.clone()
|
||||
.unwrap_or_else(|| triggering_event_id.to_string()),
|
||||
)
|
||||
}
|
||||
|
||||
/// Format a `[Context]` hints section based on event scope.
|
||||
///
|
||||
/// `reply_anchor` is the pre-resolved `--reply-to` target for this turn (see
|
||||
/// [`resolve_reply_anchor`]). In the thread/DM branches it threads ordinary
|
||||
/// replies; in the channel branch a `Some` anchor means a human-facing
|
||||
/// top-level mention whose reply should open a new thread rooted at the
|
||||
/// triggering event.
|
||||
fn format_context_hints(
|
||||
channel_id: Uuid,
|
||||
channel_info: Option<&PromptChannelInfo>,
|
||||
thread_tags: &ThreadTags,
|
||||
is_dm: bool,
|
||||
has_conversation_context: bool,
|
||||
triggering_event_id: Option<&str>,
|
||||
reply_anchor: Option<&str>,
|
||||
) -> String {
|
||||
let channel_display = match channel_info {
|
||||
Some(ci) => format!("{} (#{channel_id})", ci.name),
|
||||
@@ -934,7 +1001,7 @@ fn format_context_hints(
|
||||
s.push_str(&format!("\nParent: {parent}"));
|
||||
}
|
||||
}
|
||||
if let Some(event_id) = triggering_event_id {
|
||||
if let Some(event_id) = reply_anchor {
|
||||
append_reply_instruction(&mut s, event_id);
|
||||
}
|
||||
}
|
||||
@@ -957,17 +1024,21 @@ fn format_context_hints(
|
||||
}
|
||||
}
|
||||
s.push_str(&format!("\n{ctx_hint}"));
|
||||
if let Some(event_id) = triggering_event_id {
|
||||
if let Some(event_id) = reply_anchor {
|
||||
append_reply_instruction(&mut s, event_id);
|
||||
}
|
||||
s
|
||||
} else {
|
||||
format!(
|
||||
let mut s = format!(
|
||||
"[Context]\n\
|
||||
Scope: channel\n\
|
||||
Channel: {channel_display}\n\
|
||||
Hint: Use `buzz messages get --channel <UUID>` for recent messages if needed."
|
||||
)
|
||||
);
|
||||
if let Some(event_id) = reply_anchor {
|
||||
append_new_thread_reply_instruction(&mut s, event_id);
|
||||
}
|
||||
s
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1095,11 +1166,26 @@ pub fn format_prompt(batch: &FlushBatch, args: &FormatPromptArgs<'_>) -> Vec<Str
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Context hints (with reply instruction for thread replies).
|
||||
let triggering_event_id = if thread_tags.root_event_id.is_some() {
|
||||
Some(last_event.event.id.to_hex())
|
||||
// 2. Context hints (with a human-aware reply anchor).
|
||||
//
|
||||
// Human-facing turns are anchored so replies stay readable at layer 1:
|
||||
// - in a thread → anchor to the thread ROOT (no depth-2 nesting)
|
||||
// - top-level → anchor to the triggering event (it becomes the root)
|
||||
// Agent↔agent turns get no forced anchor — deep nesting is intentional
|
||||
// there. DMs are always 1:1 with a human, so they always anchor.
|
||||
let sender_pubkey = last_event.event.pubkey.to_hex();
|
||||
let reply_anchor = if is_dm {
|
||||
thread_tags
|
||||
.root_event_id
|
||||
.is_some()
|
||||
.then(|| last_event.event.id.to_hex())
|
||||
} else {
|
||||
None
|
||||
resolve_reply_anchor(
|
||||
&sender_pubkey,
|
||||
&thread_tags,
|
||||
&last_event.event.id.to_hex(),
|
||||
args.profile_lookup,
|
||||
)
|
||||
};
|
||||
sections.push(format_context_hints(
|
||||
batch.channel_id,
|
||||
@@ -1107,7 +1193,7 @@ pub fn format_prompt(batch: &FlushBatch, args: &FormatPromptArgs<'_>) -> Vec<Str
|
||||
&thread_tags,
|
||||
is_dm,
|
||||
args.conversation_context.is_some(),
|
||||
triggering_event_id.as_deref(),
|
||||
reply_anchor.as_deref(),
|
||||
));
|
||||
|
||||
// 3. Conversation context (thread or DM).
|
||||
@@ -1181,8 +1267,6 @@ pub fn format_prompt(batch: &FlushBatch, args: &FormatPromptArgs<'_>) -> Vec<Str
|
||||
sections
|
||||
}
|
||||
|
||||
// ─── Unit Tests ──────────────────────────────────────────────────────────────
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -1236,8 +1320,6 @@ mod tests {
|
||||
assert_eq!(base_section(" line1\nline2 "), "[Base]\n line1\nline2");
|
||||
}
|
||||
|
||||
// ── Test 1: push + flush_next basic ──────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn test_push_flush_basic() {
|
||||
let mut q = EventQueue::new(DedupMode::Queue);
|
||||
@@ -1255,8 +1337,6 @@ mod tests {
|
||||
assert_eq!(q.queues.len(), 0);
|
||||
}
|
||||
|
||||
// ── Test 2: same channel cannot be flushed twice ─────────────────────────
|
||||
|
||||
#[test]
|
||||
fn test_in_flight_blocks_same_channel() {
|
||||
let mut q = EventQueue::new(DedupMode::Queue);
|
||||
@@ -1274,8 +1354,6 @@ mod tests {
|
||||
assert!(q.flush_next().is_none());
|
||||
}
|
||||
|
||||
// ── Test 3: mark_complete enables flush ──────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn test_mark_complete_enables_flush() {
|
||||
let mut q = EventQueue::new(DedupMode::Queue);
|
||||
@@ -1299,8 +1377,6 @@ mod tests {
|
||||
assert_eq!(batch.events[0].event.content, "second");
|
||||
}
|
||||
|
||||
// ── Test 4: batch drain ───────────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn test_batch_drain_all_events() {
|
||||
let mut q = EventQueue::new(DedupMode::Queue);
|
||||
@@ -1324,8 +1400,6 @@ mod tests {
|
||||
assert_eq!(q.queues.len(), 0);
|
||||
}
|
||||
|
||||
// ── Test 5: FIFO fairness ─────────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn test_fifo_fairness_picks_oldest_channel() {
|
||||
let mut q = EventQueue::new(DedupMode::Queue);
|
||||
@@ -1342,8 +1416,6 @@ mod tests {
|
||||
assert_eq!(batch.events[0].event.content, "from A");
|
||||
}
|
||||
|
||||
// ── Test 6: multi-channel interleave ─────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn test_multi_channel_interleave() {
|
||||
let mut q = EventQueue::new(DedupMode::Queue);
|
||||
@@ -1373,16 +1445,12 @@ mod tests {
|
||||
assert_eq!(pending_count(&q), 0);
|
||||
}
|
||||
|
||||
// ── Test 7: empty queue returns None ─────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn test_empty_queue_returns_none() {
|
||||
let mut q = EventQueue::new(DedupMode::Queue);
|
||||
assert!(q.flush_next().is_none());
|
||||
}
|
||||
|
||||
// ── Test 9: format_prompt single event ───────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn test_format_prompt_single() {
|
||||
let ch = Uuid::new_v4();
|
||||
@@ -1417,8 +1485,6 @@ mod tests {
|
||||
assert!(!prompt.contains("--- Event 1 ---"));
|
||||
}
|
||||
|
||||
// ── Test 9b: requeue preserves events ────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn test_requeue_preserves_events() {
|
||||
let mut queue = EventQueue::new(DedupMode::Queue);
|
||||
@@ -1467,8 +1533,6 @@ mod tests {
|
||||
assert_eq!(next_batch.channel_id, ch_b);
|
||||
}
|
||||
|
||||
// ── Test 10: format_prompt batch ─────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn test_format_prompt_batch() {
|
||||
let ch = Uuid::new_v4();
|
||||
@@ -1510,8 +1574,6 @@ mod tests {
|
||||
assert!(prompt.contains("Content: third message"));
|
||||
}
|
||||
|
||||
// ── Test 11: system prompt NOT in user message (delivered via system role) ──
|
||||
|
||||
#[test]
|
||||
fn test_format_prompt_no_system_prompt_in_user_message() {
|
||||
let ch = Uuid::new_v4();
|
||||
@@ -1535,8 +1597,6 @@ mod tests {
|
||||
assert!(prompt.starts_with("[Context]"));
|
||||
}
|
||||
|
||||
// ── Test 11b: agent_core section is first in user message ──────────────
|
||||
|
||||
#[test]
|
||||
fn test_format_prompt_with_agent_core() {
|
||||
let ch = Uuid::new_v4();
|
||||
@@ -1622,8 +1682,6 @@ mod tests {
|
||||
assert!(prompt.starts_with("[Agent Memory — core]\nbe helpful\n\n[Context]"));
|
||||
}
|
||||
|
||||
// ── Test 11c: base_prompt and system_prompt NOT in user message ────────────
|
||||
|
||||
#[test]
|
||||
fn test_format_prompt_no_base_or_system_sections() {
|
||||
let ch = Uuid::new_v4();
|
||||
@@ -1647,8 +1705,6 @@ mod tests {
|
||||
assert!(prompt.starts_with("[Context]"));
|
||||
}
|
||||
|
||||
// ── Test 11d: legacy agents receive [Base]/[System] in user message ───────
|
||||
|
||||
#[test]
|
||||
fn test_format_prompt_legacy_agent_emits_base_and_system() {
|
||||
let ch = Uuid::new_v4();
|
||||
@@ -1704,8 +1760,6 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
// ── Test 11e: modern agents suppress [Base]/[System] from user message ────
|
||||
|
||||
#[test]
|
||||
fn test_format_prompt_modern_agent_suppresses_base_and_system() {
|
||||
let ch = Uuid::new_v4();
|
||||
@@ -1801,8 +1855,6 @@ mod tests {
|
||||
assert!(!prompt.contains("[System]"));
|
||||
}
|
||||
|
||||
// ── Test 12: drop mode discards in-flight channel events ─────────────────
|
||||
|
||||
#[test]
|
||||
fn test_drop_mode_discards_in_flight_events() {
|
||||
let mut q = EventQueue::new(DedupMode::Drop);
|
||||
@@ -1821,8 +1873,6 @@ mod tests {
|
||||
assert!(q.flush_next().is_none());
|
||||
}
|
||||
|
||||
// ── Test 13: drop mode still queues other channels ────────────────────────
|
||||
|
||||
#[test]
|
||||
fn test_drop_mode_queues_other_channels() {
|
||||
let mut q = EventQueue::new(DedupMode::Drop);
|
||||
@@ -1842,8 +1892,6 @@ mod tests {
|
||||
assert_eq!(batch_b.channel_id, ch_b);
|
||||
}
|
||||
|
||||
// ── Test 14: multiple channels can be in-flight simultaneously ────────────
|
||||
|
||||
#[test]
|
||||
fn test_multiple_channels_in_flight_simultaneously() {
|
||||
let mut q = EventQueue::new(DedupMode::Queue);
|
||||
@@ -1874,8 +1922,6 @@ mod tests {
|
||||
assert!(!any_in_flight(&q));
|
||||
}
|
||||
|
||||
// ── Test 15: same channel cannot be flushed twice ─────────────────────────
|
||||
|
||||
#[test]
|
||||
fn test_same_channel_not_flushed_twice() {
|
||||
let mut q = EventQueue::new(DedupMode::Queue);
|
||||
@@ -1898,8 +1944,6 @@ mod tests {
|
||||
assert!(q.flush_next().is_none());
|
||||
}
|
||||
|
||||
// ── Test 16: drop mode drops events for any in-flight channel ─────────────
|
||||
|
||||
#[test]
|
||||
fn test_drop_mode_drops_for_any_in_flight_channel() {
|
||||
let mut q = EventQueue::new(DedupMode::Drop);
|
||||
@@ -1922,8 +1966,6 @@ mod tests {
|
||||
q.mark_complete(ch_b);
|
||||
}
|
||||
|
||||
// ── Test 17: flush_next picks oldest non-in-flight, non-throttled channel ─
|
||||
|
||||
#[test]
|
||||
fn test_flush_next_picks_oldest_non_throttled() {
|
||||
let mut q = EventQueue::new(DedupMode::Queue);
|
||||
@@ -1956,8 +1998,6 @@ mod tests {
|
||||
q.mark_complete(ch_c);
|
||||
}
|
||||
|
||||
// ── Test 18: mark_complete(channel_id) clears only that channel ───────────
|
||||
|
||||
#[test]
|
||||
fn test_mark_complete_clears_only_specified_channel() {
|
||||
let mut q = EventQueue::new(DedupMode::Queue);
|
||||
@@ -1985,8 +2025,6 @@ mod tests {
|
||||
assert!(!any_in_flight(&q));
|
||||
}
|
||||
|
||||
// ── Test 19: requeue_preserve_timestamps preserves received_at ───────────
|
||||
|
||||
#[test]
|
||||
fn test_requeue_preserve_timestamps() {
|
||||
let mut q = EventQueue::new(DedupMode::Queue);
|
||||
@@ -2012,8 +2050,6 @@ mod tests {
|
||||
assert_eq!(batch2.events[0].received_at, original_received_at);
|
||||
}
|
||||
|
||||
// ── Test 20: requeue_preserve_timestamps does not set retry_after ─────────
|
||||
|
||||
#[test]
|
||||
fn test_requeue_preserve_timestamps_no_retry_after() {
|
||||
let mut q = EventQueue::new(DedupMode::Queue);
|
||||
@@ -2030,8 +2066,6 @@ mod tests {
|
||||
assert!(q.flush_next().is_some());
|
||||
}
|
||||
|
||||
// ── Test 20b: requeue_preserve_timestamps enforces per-channel cap ────────
|
||||
|
||||
#[test]
|
||||
fn test_requeue_preserve_timestamps_enforces_cap() {
|
||||
let mut q = EventQueue::new(DedupMode::Queue);
|
||||
@@ -2068,8 +2102,6 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
// ── Test 20c: requeue_preserve overflow trims newest, keeps requeued ─────
|
||||
|
||||
#[test]
|
||||
fn test_requeue_preserve_timestamps_overflow_keeps_requeued_events() {
|
||||
let mut q = EventQueue::new(DedupMode::Queue);
|
||||
@@ -2105,8 +2137,6 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
// ── Test 21: has_flushable_work returns correct results ───────────────────
|
||||
|
||||
#[test]
|
||||
fn test_has_flushable_work() {
|
||||
let mut q = EventQueue::new(DedupMode::Queue);
|
||||
@@ -2145,8 +2175,6 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
// ── Test 22: retry throttle blocks re-flush for 5 seconds ─────────────────
|
||||
|
||||
#[test]
|
||||
fn test_retry_throttle_blocks_requeue_channel() {
|
||||
let mut q = EventQueue::new(DedupMode::Queue);
|
||||
@@ -2178,8 +2206,6 @@ mod tests {
|
||||
assert_eq!(batch3.channel_id, ch);
|
||||
}
|
||||
|
||||
// ── NIP-10 tag parsing tests ─────────────────────────────────────────────
|
||||
|
||||
/// Build an event with specific tags for thread testing.
|
||||
fn make_event_with_tags(content: &str, tags: Vec<Vec<String>>) -> Event {
|
||||
let keys = Keys::generate();
|
||||
@@ -2258,8 +2284,6 @@ mod tests {
|
||||
assert_eq!(tags.parent_event_id.as_deref(), Some("root123"));
|
||||
}
|
||||
|
||||
// ── Context formatting tests ─────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn test_format_prompt_with_channel_info() {
|
||||
let ch = Uuid::new_v4();
|
||||
@@ -2473,6 +2497,7 @@ mod tests {
|
||||
PromptProfile {
|
||||
display_name: Some("Wes".into()),
|
||||
nip05_handle: None,
|
||||
..Default::default()
|
||||
},
|
||||
),
|
||||
(
|
||||
@@ -2480,6 +2505,7 @@ mod tests {
|
||||
PromptProfile {
|
||||
display_name: Some("Rick".into()),
|
||||
nip05_handle: None,
|
||||
..Default::default()
|
||||
},
|
||||
),
|
||||
]);
|
||||
@@ -2509,6 +2535,7 @@ mod tests {
|
||||
PromptProfile {
|
||||
display_name: None,
|
||||
nip05_handle: Some("wes@example.com".into()),
|
||||
..Default::default()
|
||||
},
|
||||
)]);
|
||||
assert_eq!(
|
||||
@@ -2525,6 +2552,7 @@ mod tests {
|
||||
PromptProfile {
|
||||
display_name: Some(" ".into()),
|
||||
nip05_handle: Some("wes@example.com".into()),
|
||||
..Default::default()
|
||||
},
|
||||
)]);
|
||||
assert_eq!(
|
||||
@@ -2533,6 +2561,94 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
// ── Human-aware reply anchoring ──────────────────────────────────────────
|
||||
|
||||
const HUMAN_PK: &str = "1111111111111111111111111111111111111111111111111111111111111111";
|
||||
const AGENT_A_PK: &str = "2222222222222222222222222222222222222222222222222222222222222222";
|
||||
const AGENT_B_PK: &str = "3333333333333333333333333333333333333333333333333333333333333333";
|
||||
const ROOT_ID: &str = "abc0000000000000000000000000000000000000000000000000000000000000";
|
||||
const TRIGGER_ID: &str = "def0000000000000000000000000000000000000000000000000000000000000";
|
||||
|
||||
fn profile(is_agent: bool) -> PromptProfile {
|
||||
PromptProfile {
|
||||
is_agent,
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
/// Lookup with HUMAN as a human and AGENT_A / AGENT_B as agents.
|
||||
fn id_lookup() -> PromptProfileLookup {
|
||||
HashMap::from([
|
||||
(HUMAN_PK.to_string(), profile(false)),
|
||||
(AGENT_A_PK.to_string(), profile(true)),
|
||||
(AGENT_B_PK.to_string(), profile(true)),
|
||||
])
|
||||
}
|
||||
|
||||
fn thread_tags(root: Option<&str>, mentions: &[&str]) -> ThreadTags {
|
||||
ThreadTags {
|
||||
root_event_id: root.map(str::to_string),
|
||||
parent_event_id: root.map(str::to_string),
|
||||
mentioned_pubkeys: mentions.iter().map(|s| s.to_string()).collect(),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_anchor_human_in_thread_uses_root() {
|
||||
// Human asks inside a thread → anchor to the thread ROOT (flat at L1).
|
||||
let tags = thread_tags(Some(ROOT_ID), &[AGENT_A_PK]);
|
||||
let anchor = resolve_reply_anchor(HUMAN_PK, &tags, TRIGGER_ID, Some(&id_lookup()));
|
||||
assert_eq!(anchor.as_deref(), Some(ROOT_ID));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_anchor_human_top_level_uses_triggering_event() {
|
||||
// Human top-level mention (no thread tags) → triggering event is root.
|
||||
let tags = thread_tags(None, &[AGENT_A_PK]);
|
||||
let anchor = resolve_reply_anchor(HUMAN_PK, &tags, TRIGGER_ID, Some(&id_lookup()));
|
||||
assert_eq!(anchor.as_deref(), Some(TRIGGER_ID));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_anchor_agent_to_agent_in_thread_is_none() {
|
||||
// Agent pings agent inside a thread → no forced anchor (deep nesting ok).
|
||||
let tags = thread_tags(Some(ROOT_ID), &[AGENT_B_PK]);
|
||||
let anchor = resolve_reply_anchor(AGENT_A_PK, &tags, TRIGGER_ID, Some(&id_lookup()));
|
||||
assert_eq!(anchor, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_anchor_agent_to_agent_top_level_is_none() {
|
||||
let tags = thread_tags(None, &[AGENT_B_PK]);
|
||||
let anchor = resolve_reply_anchor(AGENT_A_PK, &tags, TRIGGER_ID, Some(&id_lookup()));
|
||||
assert_eq!(anchor, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_anchor_agent_sender_but_human_tagged_flattens() {
|
||||
// Agent-authored, but a human is tagged → human-facing → anchor to root.
|
||||
let tags = thread_tags(Some(ROOT_ID), &[AGENT_B_PK, HUMAN_PK]);
|
||||
let anchor = resolve_reply_anchor(AGENT_A_PK, &tags, TRIGGER_ID, Some(&id_lookup()));
|
||||
assert_eq!(anchor.as_deref(), Some(ROOT_ID));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_anchor_unknown_identity_treated_as_human() {
|
||||
// No profile lookup → fail open (treat as human so visibility is kept).
|
||||
let tags = thread_tags(Some(ROOT_ID), &[]);
|
||||
let anchor = resolve_reply_anchor(AGENT_A_PK, &tags, TRIGGER_ID, None);
|
||||
assert_eq!(anchor.as_deref(), Some(ROOT_ID));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_anchor_agent_only_p_tags_do_not_flatten() {
|
||||
// Raw p-tag presence must NOT flatten when every tagged pubkey is an
|
||||
// agent — this is the regression Pinky flagged.
|
||||
let tags = thread_tags(Some(ROOT_ID), &[AGENT_A_PK, AGENT_B_PK]);
|
||||
let anchor = resolve_reply_anchor(AGENT_A_PK, &tags, TRIGGER_ID, Some(&id_lookup()));
|
||||
assert_eq!(anchor, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sanitize_prompt_label_strips_newlines_and_control_chars() {
|
||||
assert_eq!(
|
||||
@@ -2720,8 +2836,6 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
// ── drain_channel tests ──────────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn test_drain_channel_removes_pending_events() {
|
||||
let mut q = EventQueue::new(DedupMode::Queue);
|
||||
@@ -2792,8 +2906,6 @@ mod tests {
|
||||
assert!(any_in_flight(&q)); // in-flight unaffected
|
||||
}
|
||||
|
||||
// ── compact_expired_state ─────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn test_compact_cleans_orphaned_retry_counts() {
|
||||
let mut q = EventQueue::new(DedupMode::Queue);
|
||||
@@ -2872,8 +2984,6 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
// ── Test: requeue_as_cancelled merges into flush_next ────────────────────
|
||||
|
||||
#[test]
|
||||
fn test_requeue_as_cancelled_merges_in_flush_next() {
|
||||
let mut q = EventQueue::new(DedupMode::Queue);
|
||||
@@ -2902,8 +3012,6 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
// ── Test: requeue_as_cancelled fallback (no new events) ──────────────────
|
||||
|
||||
#[test]
|
||||
fn test_requeue_as_cancelled_no_new_events_fallback() {
|
||||
let mut q = EventQueue::new(DedupMode::Queue);
|
||||
@@ -2930,8 +3038,6 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
// ── Test: has_flushable_work accounts for cancelled_batches ──────────────
|
||||
|
||||
#[test]
|
||||
fn test_has_flushable_work_with_cancelled_only() {
|
||||
let mut q = EventQueue::new(DedupMode::Queue);
|
||||
@@ -2950,8 +3056,6 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
// ── Test: drain_channel clears cancelled_batches ──────────────────────────
|
||||
|
||||
#[test]
|
||||
fn test_drain_channel_clears_cancelled_batches() {
|
||||
let mut q = EventQueue::new(DedupMode::Queue);
|
||||
@@ -2973,8 +3077,6 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
// ── Test: double-cancel accumulates all events ────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn test_double_cancel_preserves_all_events() {
|
||||
let mut q = EventQueue::new(DedupMode::Queue);
|
||||
@@ -3016,17 +3118,14 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
// ── reply instruction tests ──────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn test_reply_instruction_present_for_channel_thread_reply() {
|
||||
let ch = Uuid::new_v4();
|
||||
let root_id = "a".repeat(64);
|
||||
let event = make_event_with_tags(
|
||||
"@bot help",
|
||||
vec![vec!["e".into(), root_id, "".into(), "reply".into()]],
|
||||
vec![vec!["e".into(), root_id.clone(), "".into(), "reply".into()]],
|
||||
);
|
||||
let event_id = event.id.to_hex();
|
||||
let batch = FlushBatch {
|
||||
channel_id: ch,
|
||||
events: vec![BatchEvent {
|
||||
@@ -3037,14 +3136,25 @@ mod tests {
|
||||
cancelled_events: vec![],
|
||||
};
|
||||
|
||||
// No profile lookup → sender treated as human → human-facing thread
|
||||
// reply anchors to the thread ROOT (flat at layer 1), not the
|
||||
// triggering event id.
|
||||
let prompt = format_prompt(&batch, &FormatPromptArgs::default()).join("\n\n");
|
||||
assert!(
|
||||
prompt.contains(&format!("--reply-to {event_id}")),
|
||||
"channel thread reply should include reply instruction with triggering event ID"
|
||||
prompt.contains(&format!("--reply-to {root_id}")),
|
||||
"human-facing thread reply should anchor to the thread root"
|
||||
);
|
||||
assert!(
|
||||
prompt.contains("Do not broadcast to the channel"),
|
||||
"channel thread reply should include broadcast suppression hint"
|
||||
prompt.contains("For ordinary replies in this turn"),
|
||||
"channel thread reply should describe reply-to as the default"
|
||||
);
|
||||
assert!(
|
||||
prompt.contains("send that message without `--reply-to`"),
|
||||
"channel thread reply should allow explicit channel-root/top-level requests"
|
||||
);
|
||||
assert!(
|
||||
!prompt.contains("Do not broadcast to the channel"),
|
||||
"reply instruction should not forbid explicit human-requested root posts"
|
||||
);
|
||||
}
|
||||
|
||||
@@ -3086,9 +3196,10 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_reply_instruction_absent_for_top_level_channel_message() {
|
||||
fn test_reply_instruction_present_for_top_level_human_message() {
|
||||
let ch = Uuid::new_v4();
|
||||
let event = make_event("hello world");
|
||||
let event_id = event.id.to_hex();
|
||||
let batch = FlushBatch {
|
||||
channel_id: ch,
|
||||
events: vec![BatchEvent {
|
||||
@@ -3099,10 +3210,17 @@ mod tests {
|
||||
cancelled_events: vec![],
|
||||
};
|
||||
|
||||
// Top-level human message (no lookup → human): the reply opens a new
|
||||
// thread anchored to the triggering event, preventing replies into a
|
||||
// stale older thread.
|
||||
let prompt = format_prompt(&batch, &FormatPromptArgs::default()).join("\n\n");
|
||||
assert!(
|
||||
!prompt.contains("--reply-to"),
|
||||
"top-level message should NOT include reply instruction"
|
||||
prompt.contains(&format!("--reply-to {event_id}")),
|
||||
"top-level human message should anchor a new thread at the triggering event"
|
||||
);
|
||||
assert!(
|
||||
prompt.contains("new top-level message"),
|
||||
"top-level human message should use the new-thread instruction"
|
||||
);
|
||||
}
|
||||
|
||||
@@ -3139,7 +3257,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_reply_instruction_uses_triggering_event_id_not_root_or_parent() {
|
||||
fn test_human_thread_reply_anchors_to_root_not_triggering_or_parent() {
|
||||
let ch = Uuid::new_v4();
|
||||
let root_id = "a".repeat(64);
|
||||
let parent_id = "b".repeat(64);
|
||||
@@ -3161,19 +3279,53 @@ mod tests {
|
||||
cancelled_events: vec![],
|
||||
};
|
||||
|
||||
// Human-facing (no lookup) deep reply: anchor to the thread ROOT to
|
||||
// keep the conversation flat — NOT the triggering event or parent.
|
||||
let prompt = format_prompt(&batch, &FormatPromptArgs::default()).join("\n\n");
|
||||
// The instruction should use the triggering event's own ID — not root or parent.
|
||||
assert!(
|
||||
prompt.contains(&format!("--reply-to {event_id}")),
|
||||
"nested reply instruction should use the triggering event ID"
|
||||
prompt.contains(&format!("--reply-to {root_id}")),
|
||||
"human-facing nested reply should anchor to the thread root"
|
||||
);
|
||||
assert!(
|
||||
!prompt.contains(&format!("--reply-to {root_id}")),
|
||||
"instruction should NOT use root_event_id"
|
||||
!prompt.contains(&format!("--reply-to {event_id}")),
|
||||
"instruction should NOT anchor to the triggering event id"
|
||||
);
|
||||
assert!(
|
||||
!prompt.contains(&format!("--reply-to {parent_id}")),
|
||||
"instruction should NOT use parent_event_id from tags"
|
||||
"instruction should NOT anchor to the parent event id"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_reply_instruction_allows_explicit_root_post_requests() {
|
||||
let ch = Uuid::new_v4();
|
||||
let root_id = "e".repeat(64);
|
||||
let event = make_event_with_tags(
|
||||
"@bot post your summary in the channel root",
|
||||
vec![vec!["e".into(), root_id.clone(), "".into(), "reply".into()]],
|
||||
);
|
||||
let batch = FlushBatch {
|
||||
channel_id: ch,
|
||||
events: vec![BatchEvent {
|
||||
event,
|
||||
prompt_tag: "@mention".into(),
|
||||
received_at: Instant::now(),
|
||||
}],
|
||||
cancelled_events: vec![],
|
||||
};
|
||||
|
||||
let prompt = format_prompt(&batch, &FormatPromptArgs::default()).join("\n\n");
|
||||
assert!(
|
||||
prompt.contains(&format!("--reply-to {root_id}")),
|
||||
"human-facing thread reply should anchor to the thread root"
|
||||
);
|
||||
assert!(
|
||||
prompt.contains("channel-root, top-level"),
|
||||
"instruction should tell agents to honor explicit root/top-level requests"
|
||||
);
|
||||
assert!(
|
||||
!prompt.contains("on EVERY `buzz messages send` call"),
|
||||
"instruction should not make reply-to absolute for every send"
|
||||
);
|
||||
}
|
||||
|
||||
@@ -3184,9 +3336,8 @@ mod tests {
|
||||
let root_id = "c".repeat(64);
|
||||
let threaded = make_event_with_tags(
|
||||
"@bot help",
|
||||
vec![vec!["e".into(), root_id, "".into(), "reply".into()]],
|
||||
vec![vec!["e".into(), root_id.clone(), "".into(), "reply".into()]],
|
||||
);
|
||||
let threaded_id = threaded.id.to_hex();
|
||||
let batch = FlushBatch {
|
||||
channel_id: ch,
|
||||
events: vec![
|
||||
@@ -3204,10 +3355,12 @@ mod tests {
|
||||
cancelled_events: vec![],
|
||||
};
|
||||
|
||||
// Scope derives from the last (threaded) event; human-facing → anchor
|
||||
// to that thread's root.
|
||||
let prompt = format_prompt(&batch, &FormatPromptArgs::default()).join("\n\n");
|
||||
assert!(
|
||||
prompt.contains(&format!("--reply-to {threaded_id}")),
|
||||
"batched prompt should use last (threaded) event's ID"
|
||||
prompt.contains(&format!("--reply-to {root_id}")),
|
||||
"batched prompt should anchor to the last (threaded) event's root"
|
||||
);
|
||||
}
|
||||
|
||||
@@ -3220,6 +3373,7 @@ mod tests {
|
||||
vec![vec!["e".into(), root_id, "".into(), "reply".into()]],
|
||||
);
|
||||
let plain = make_event("latest top-level");
|
||||
let plain_id = plain.id.to_hex();
|
||||
let batch = FlushBatch {
|
||||
channel_id: ch,
|
||||
events: vec![
|
||||
@@ -3237,15 +3391,19 @@ mod tests {
|
||||
cancelled_events: vec![],
|
||||
};
|
||||
|
||||
// Last event is top-level and human-facing → opens a new thread
|
||||
// anchored to that top-level event (NOT the earlier thread's root).
|
||||
let prompt = format_prompt(&batch, &FormatPromptArgs::default()).join("\n\n");
|
||||
assert!(
|
||||
!prompt.contains("--reply-to"),
|
||||
"batched prompt where last event is top-level should NOT include reply instruction"
|
||||
prompt.contains(&format!("--reply-to {plain_id}")),
|
||||
"batched top-level-last prompt should anchor to the last (top-level) event"
|
||||
);
|
||||
assert!(
|
||||
prompt.contains("new top-level message"),
|
||||
"batched top-level-last prompt should use the new-thread instruction"
|
||||
);
|
||||
}
|
||||
|
||||
// ── Slash command extraction ──────────────────────────────────────────────
|
||||
|
||||
/// Build a single-event FlushBatch with the given content.
|
||||
fn make_single_batch(content: &str) -> FlushBatch {
|
||||
FlushBatch {
|
||||
|
||||
@@ -20,8 +20,6 @@
|
||||
use std::collections::{HashMap, HashSet, VecDeque};
|
||||
use std::time::Duration;
|
||||
|
||||
// ─── Named constants ──────────────────────────────────────────────────────────
|
||||
|
||||
/// Default capacity of the event channel from background task to harness.
|
||||
/// Override with `BUZZ_ACP_EVENT_BUFFER` env var at startup.
|
||||
const EVENT_CHANNEL_CAPACITY_DEFAULT: usize = 256;
|
||||
@@ -76,8 +74,6 @@ use uuid::Uuid;
|
||||
|
||||
use crate::config::ChannelFilter;
|
||||
|
||||
// ── Types ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
/// Metadata about a channel, populated at discovery time.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ChannelInfo {
|
||||
@@ -197,8 +193,6 @@ fn unix_now_secs() -> u64 {
|
||||
}
|
||||
|
||||
impl RestClient {
|
||||
// ── NIP-98 signing ────────────────────────────────────────────────────
|
||||
|
||||
/// Sign a NIP-98 HTTP Auth event (kind:27235) for the given method/URL/body.
|
||||
///
|
||||
/// Returns the `Authorization: Nostr <base64>` header value (without the
|
||||
@@ -247,8 +241,6 @@ impl RestClient {
|
||||
Ok(format!("Nostr {}", self.sign_nip98(method, url, body)?))
|
||||
}
|
||||
|
||||
// ── Retry helper ──────────────────────────────────────────────────────
|
||||
|
||||
/// Retry helper: executes `build_request` up to 4 times (1 attempt + 3 retries)
|
||||
/// on transient failures (429, 502, 503, 504, timeout, connect errors).
|
||||
///
|
||||
@@ -306,8 +298,6 @@ impl RestClient {
|
||||
.unwrap_or_else(|| RelayError::Http(format!("{method} {path} failed after retries"))))
|
||||
}
|
||||
|
||||
// ── Bridge methods ────────────────────────────────────────────────────
|
||||
|
||||
/// POST with NIP-98 auth and retry. Re-signs on each attempt.
|
||||
async fn bridge_post(
|
||||
&self,
|
||||
@@ -410,8 +400,6 @@ impl From<nostr::event::builder::Error> for RelayError {
|
||||
}
|
||||
}
|
||||
|
||||
// ── Internal relay message types ──────────────────────────────────────────────
|
||||
|
||||
/// A parsed NIP-01 relay message.
|
||||
#[derive(Debug, Clone)]
|
||||
enum RelayMessage {
|
||||
@@ -439,8 +427,6 @@ enum RelayMessage {
|
||||
},
|
||||
}
|
||||
|
||||
// ── Commands sent from HarnessRelay to the background task ───────────────────
|
||||
|
||||
/// Subscription ID for the global membership notification subscription.
|
||||
const MEMBERSHIP_NOTIF_SUB_ID: &str = "membership-notif";
|
||||
/// Subscription ID for encrypted owner-to-agent observer control frames.
|
||||
@@ -472,12 +458,8 @@ enum RelayCommand {
|
||||
SetStartupWatermark { ts: u64 },
|
||||
}
|
||||
|
||||
// ── WebSocket stream type alias ───────────────────────────────────────────────
|
||||
|
||||
type WsStream = WebSocketStream<MaybeTlsStream<tokio::net::TcpStream>>;
|
||||
|
||||
// ── HarnessRelay ──────────────────────────────────────────────────────────────
|
||||
|
||||
/// Harness-side relay client.
|
||||
///
|
||||
/// Connects to the Buzz relay, authenticates via NIP-42, and streams
|
||||
@@ -525,8 +507,6 @@ impl RelayEventPublisher {
|
||||
}
|
||||
|
||||
impl HarnessRelay {
|
||||
// ── Public API ────────────────────────────────────────────────────────────
|
||||
|
||||
/// Connect to relay and authenticate via NIP-42.
|
||||
///
|
||||
/// `auth_tag` is an optional NIP-OA owner attestation included in the AUTH
|
||||
@@ -858,8 +838,6 @@ impl Drop for HarnessRelay {
|
||||
}
|
||||
}
|
||||
|
||||
// ── Background task ───────────────────────────────────────────────────────────
|
||||
|
||||
/// Two-generation dedup set with bounded memory.
|
||||
///
|
||||
/// Mitigates the "amnesia window" caused by clearing the entire set at once.
|
||||
@@ -1371,7 +1349,6 @@ async fn run_background_task(
|
||||
}
|
||||
|
||||
tokio::select! {
|
||||
// ── Incoming WebSocket message ────────────────────────────────────
|
||||
raw = ws.next() => {
|
||||
// Determine if the socket is lost.
|
||||
let socket_lost = match raw {
|
||||
@@ -1454,7 +1431,6 @@ async fn run_background_task(
|
||||
}
|
||||
}
|
||||
|
||||
// ── Command from HarnessRelay ─────────────────────────────────────
|
||||
cmd = cmd_rx.recv() => {
|
||||
match cmd {
|
||||
Some(RelayCommand::Reconnect) => {
|
||||
@@ -1526,7 +1502,6 @@ async fn run_background_task(
|
||||
}
|
||||
}
|
||||
|
||||
// ── Finding #31: client-initiated ping ────────────────────────────
|
||||
_ = ping_interval.tick() => {
|
||||
if ping_sent && last_pong.elapsed() > PONG_TIMEOUT {
|
||||
// No pong received after our last ping — connection is dead.
|
||||
@@ -2576,8 +2551,6 @@ async fn send_auth_response(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// ── Free functions ────────────────────────────────────────────────────────────
|
||||
|
||||
/// Convert a WebSocket URL to its HTTP equivalent.
|
||||
///
|
||||
/// `ws://host:port` → `http://host:port`
|
||||
@@ -2737,8 +2710,6 @@ pub(crate) fn parse_relay_message(text: &str) -> Result<RelayMessage, RelayError
|
||||
}
|
||||
}
|
||||
|
||||
// ── Connection helpers ────────────────────────────────────────────────────────
|
||||
|
||||
/// Perform a single WebSocket connect + NIP-42 auth handshake.
|
||||
///
|
||||
/// Returns `(ws, buffer)` on success.
|
||||
@@ -2760,13 +2731,10 @@ async fn do_connect(
|
||||
let mut ws = ws;
|
||||
let mut buffer: VecDeque<RelayMessage> = VecDeque::new();
|
||||
|
||||
// ── Step 1: Wait for AUTH challenge ───────────────────────────────────
|
||||
let challenge = wait_for_auth_challenge(&mut ws, &mut buffer, AUTH_TIMEOUT).await?;
|
||||
|
||||
// ── Step 2: Build and send kind:22242 auth event ──────────────────────
|
||||
send_auth_response(&mut ws, &challenge, relay_url, keys, auth_tag).await?;
|
||||
|
||||
// ── Step 3: Wait for OK ───────────────────────────────────────────────
|
||||
let event_id = {
|
||||
// We need the event_id that was just sent. Re-derive it by signing again
|
||||
// just to get the ID — but that's wasteful. Instead, parse the last sent
|
||||
@@ -2914,14 +2882,10 @@ async fn wait_for_any_ok(
|
||||
}
|
||||
}
|
||||
|
||||
// ── Unit tests ────────────────────────────────────────────────────────────────
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
// ── relay_ws_to_http ──────────────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn relay_ws_to_http_plain() {
|
||||
assert_eq!(
|
||||
@@ -2962,8 +2926,6 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
// ── channel_sub_id ────────────────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn channel_sub_id_format() {
|
||||
let uuid = Uuid::parse_str("550e8400-e29b-41d4-a716-446655440000").unwrap();
|
||||
@@ -2996,8 +2958,6 @@ mod tests {
|
||||
assert!(channel_id_from_sub_id("").is_none());
|
||||
}
|
||||
|
||||
// ── merge_discovered_channels (archived skip) ─────────────────────────────
|
||||
|
||||
fn meta_event(uuid: Uuid, name: &str, extra: &[&str]) -> serde_json::Value {
|
||||
let mut tags = vec![
|
||||
serde_json::json!(["d", uuid.to_string()]),
|
||||
@@ -3063,8 +3023,6 @@ mod tests {
|
||||
assert!(map.contains_key(&ch), "archived=false is treated as live");
|
||||
}
|
||||
|
||||
// ── parse_relay_message ───────────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn parse_ok_accepted() {
|
||||
let text = r#"["OK","abc123",true,""]"#;
|
||||
@@ -3227,8 +3185,6 @@ mod tests {
|
||||
assert!(result.is_err());
|
||||
}
|
||||
|
||||
// ── channel_sub_id subscription format ───────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn subscription_id_starts_with_ch_prefix() {
|
||||
let uuid = Uuid::new_v4();
|
||||
@@ -3243,8 +3199,6 @@ mod tests {
|
||||
assert_eq!(sub_id, "ch-12345678-1234-5678-1234-567812345678");
|
||||
}
|
||||
|
||||
// ── BgState: seen_ids deduplication ──────────────────────────────────────
|
||||
|
||||
/// Build a real signed Nostr event for testing BgState.
|
||||
///
|
||||
/// Uses `custom_created_at` so tests can control the timestamp.
|
||||
@@ -3300,8 +3254,6 @@ mod tests {
|
||||
assert!(state.record_event(channel_id, &event2));
|
||||
}
|
||||
|
||||
// ── BgState: last_seen tracking ───────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn bg_state_last_seen_set_on_first_event() {
|
||||
let mut state = BgState::new();
|
||||
@@ -3444,8 +3396,6 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
// ── Bug 5: channel_dropped_since tracking ─────────────────────────────────
|
||||
|
||||
/// Test 8: channel_dropped_since records the OLDEST dropped timestamp.
|
||||
///
|
||||
/// Simulates the backpressure path directly on BgState:
|
||||
@@ -3564,8 +3514,6 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
// ── Membership dedup regression tests (M4) ───────────────────────────
|
||||
|
||||
/// Membership dedup must NOT contaminate per-channel `last_seen`.
|
||||
/// Using `record_event()` for membership notifications would update
|
||||
/// `last_seen[channel_uuid]`, causing channel resubscribe to use a
|
||||
@@ -3624,8 +3572,6 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
// ── drop_channel_on_access_denied (Phenomenon A: reconnect loop) ──────────
|
||||
|
||||
/// Subscribe a channel via the production command path so the test exercises
|
||||
/// real subscription state (active_subscriptions + active_filters + since).
|
||||
fn subscribe_channel(state: &mut BgState, channel_id: Uuid) {
|
||||
|
||||
@@ -15,9 +15,18 @@ path = "src/main.rs"
|
||||
buzz-db = { workspace = true }
|
||||
buzz-core = { workspace = true }
|
||||
buzz-auth = { workspace = true }
|
||||
buzz-pubsub = { workspace = true }
|
||||
buzz-search = { workspace = true }
|
||||
buzz-audit = { workspace = true }
|
||||
buzz-workflow = { workspace = true }
|
||||
buzz-media = { workspace = true }
|
||||
nostr = { workspace = true }
|
||||
tokio = { workspace = true }
|
||||
serde_json = { workspace = true }
|
||||
anyhow = { workspace = true }
|
||||
hex = { workspace = true }
|
||||
deadpool-redis = { workspace = true }
|
||||
tracing = { workspace = true }
|
||||
sqlx = { workspace = true }
|
||||
uuid = { workspace = true }
|
||||
clap = { version = "4", features = ["derive"] }
|
||||
|
||||
+298
-32
@@ -2,14 +2,33 @@
|
||||
|
||||
//! Buzz instance administration CLI.
|
||||
//!
|
||||
//! In the pure Nostr architecture, API tokens no longer exist.
|
||||
//! Admin operations are performed via signed Nostr events (NIP-43 relay admin commands).
|
||||
//! This binary is retained as a placeholder for future admin tooling.
|
||||
//! # Member management (NIP-43)
|
||||
//!
|
||||
//! ## Why only kind:13534 (membership list), not kind:8000/8001 (deltas)
|
||||
//!
|
||||
//! CLI intentionally does not emit kind 8000/8001 deltas —
|
||||
//! `publish_nip43_delta` is in-process-only (no Redis hop), so a sidecar call
|
||||
//! stores but never pushes. The 13534 list snapshot is the authoritative roster
|
||||
//! and rides Redis to live clients. Do not wire a delta call that passes
|
||||
//! in-process tests and silently no-ops in the deployed `compose exec` path.
|
||||
//!
|
||||
//! ## Same-second domination guard
|
||||
//!
|
||||
//! The `custom_created_at = max(now, newest_existing_13534 + 1s)` bump defeats
|
||||
//! same-second domination for serial invocations; it does NOT serialize
|
||||
//! concurrent CLI processes — two near-simultaneous adds can read the same
|
||||
//! newest timestamp and collide on the bumped second. run.sh serialization is
|
||||
//! the guard against parallel adds (e.g. `xargs -P`).
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use anyhow::Result;
|
||||
use buzz_core::kind::KIND_NIP43_MEMBERSHIP_LIST;
|
||||
use buzz_db::{Db, DbConfig};
|
||||
use buzz_pubsub::PubSubManager;
|
||||
use clap::{Parser, Subcommand};
|
||||
use nostr::Keys;
|
||||
use nostr::{EventBuilder, Keys, Kind, Tag};
|
||||
use tracing::warn;
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(name = "buzz-admin", about = "Buzz instance administration")]
|
||||
@@ -21,15 +40,35 @@ struct Cli {
|
||||
#[derive(Subcommand)]
|
||||
enum Command {
|
||||
/// Add a pubkey to the relay membership list.
|
||||
///
|
||||
/// Accepts a bech32 npub or 64-char hex pubkey. After inserting the DB row,
|
||||
/// publishes a kind:13534 membership roster via Redis so live clients see
|
||||
/// the updated list immediately.
|
||||
AddMember {
|
||||
/// Nostr public key (hex) to add.
|
||||
/// Nostr public key — bech32 npub or 64-char hex.
|
||||
#[arg(long)]
|
||||
pubkey: String,
|
||||
|
||||
/// Role: "admin" or "member" (default: member).
|
||||
/// Role: "admin" or "member" (default: member). Cannot be "owner" —
|
||||
/// use RELAY_OWNER_PUBKEY config to set the relay owner.
|
||||
#[arg(long, default_value = "member")]
|
||||
role: String,
|
||||
},
|
||||
/// Remove a pubkey from the relay membership list.
|
||||
///
|
||||
/// Accepts a bech32 npub or 64-char hex pubkey. After removing the DB row,
|
||||
/// publishes a kind:13534 membership roster via Redis. Cannot remove the
|
||||
/// relay owner — change RELAY_OWNER_PUBKEY config instead.
|
||||
RemoveMember {
|
||||
/// Nostr public key — bech32 npub or 64-char hex.
|
||||
#[arg(long)]
|
||||
pubkey: String,
|
||||
|
||||
/// Only remove if the member's current role matches this value.
|
||||
/// Omit to remove regardless of role.
|
||||
#[arg(long)]
|
||||
role: Option<String>,
|
||||
},
|
||||
/// List all relay members.
|
||||
ListMembers,
|
||||
/// Generate a new Nostr keypair (for bootstrapping).
|
||||
@@ -51,53 +90,281 @@ enum Command {
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<()> {
|
||||
async fn main() {
|
||||
let cli = Cli::parse();
|
||||
|
||||
let code = match run(cli).await {
|
||||
Ok(code) => code,
|
||||
Err(e) => {
|
||||
eprintln!("error: {e}");
|
||||
5
|
||||
}
|
||||
};
|
||||
std::process::exit(code);
|
||||
}
|
||||
|
||||
async fn run(cli: Cli) -> Result<i32> {
|
||||
match cli.command {
|
||||
Command::GenerateKey => {
|
||||
let keys = Keys::generate();
|
||||
println!("Public key: {}", keys.public_key().to_hex());
|
||||
println!("Secret key: {}", keys.secret_key().display_secret());
|
||||
println!("\nSet BUZZ_PRIVATE_KEY to the secret key to use this identity.");
|
||||
Ok(0)
|
||||
}
|
||||
Command::Migrate => {
|
||||
let db = connect_db().await?;
|
||||
db.migrate().await?;
|
||||
println!("Database migrations complete.");
|
||||
Ok(0)
|
||||
}
|
||||
Command::AddMember { pubkey, role } => {
|
||||
let db = connect_db().await?;
|
||||
let pk_bytes = hex::decode(&pubkey)?;
|
||||
if pk_bytes.len() != 32 {
|
||||
anyhow::bail!("pubkey must be 32 bytes (64 hex chars)");
|
||||
}
|
||||
db.ensure_user(&pk_bytes).await?;
|
||||
// Add to relay members via DB (admin bootstrap — normally done via kind:9030)
|
||||
db.add_relay_member(&pubkey, &role, None).await?;
|
||||
println!("Added {} as {} to relay membership list.", pubkey, role);
|
||||
}
|
||||
Command::ListMembers => {
|
||||
let db = connect_db().await?;
|
||||
let members = db.list_relay_members().await?;
|
||||
if members.is_empty() {
|
||||
println!("No relay members found.");
|
||||
} else {
|
||||
println!("{:<66} {:<10}", "Pubkey", "Role");
|
||||
println!("{}", "-".repeat(78));
|
||||
for m in &members {
|
||||
println!("{:<66} {:<10}", hex::encode(&m.pubkey), m.role);
|
||||
}
|
||||
}
|
||||
}
|
||||
Command::AddMember { pubkey, role } => cmd_add_member(pubkey, role).await,
|
||||
Command::RemoveMember { pubkey, role } => cmd_remove_member(pubkey, role).await,
|
||||
Command::ListMembers => cmd_list_members().await,
|
||||
Command::ReconcileChannels { relay_key } => {
|
||||
reconcile_channels(relay_key).await?;
|
||||
Ok(0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn cmd_add_member(pubkey_arg: String, role: String) -> Result<i32> {
|
||||
if let Err(msg) = validate_role(&role) {
|
||||
eprintln!("error: {msg}");
|
||||
return Ok(1);
|
||||
}
|
||||
|
||||
let pubkey_hex = match parse_pubkey_hex(&pubkey_arg) {
|
||||
Ok(h) => h,
|
||||
Err(msg) => {
|
||||
eprintln!("error: {msg}");
|
||||
return Ok(1);
|
||||
}
|
||||
};
|
||||
|
||||
let (db, pubsub, relay_keypair) = connect_member_services().await?;
|
||||
|
||||
match db.add_relay_member(&pubkey_hex, &role, None).await {
|
||||
Ok(true) => println!("added {pubkey_hex} as {role}"),
|
||||
Ok(false) => println!("already a member: {pubkey_hex} (no change)"),
|
||||
Err(e) => {
|
||||
eprintln!("error: DB write failed: {e}");
|
||||
return Ok(5);
|
||||
}
|
||||
}
|
||||
|
||||
if let Err(e) = publish_membership_list_with_bump(&db, &pubsub, &relay_keypair).await {
|
||||
eprintln!("warning: member added to DB but list publish failed: {e}");
|
||||
}
|
||||
|
||||
Ok(0)
|
||||
}
|
||||
|
||||
async fn cmd_remove_member(pubkey_arg: String, role_filter: Option<String>) -> Result<i32> {
|
||||
if let Some(ref role) = role_filter {
|
||||
if let Err(msg) = validate_role(role) {
|
||||
eprintln!("error: {msg}");
|
||||
return Ok(1);
|
||||
}
|
||||
}
|
||||
|
||||
let pubkey_hex = match parse_pubkey_hex(&pubkey_arg) {
|
||||
Ok(h) => h,
|
||||
Err(msg) => {
|
||||
eprintln!("error: {msg}");
|
||||
return Ok(1);
|
||||
}
|
||||
};
|
||||
|
||||
let (db, pubsub, relay_keypair) = connect_member_services().await?;
|
||||
|
||||
use buzz_db::relay_members::RemoveResult;
|
||||
let result = if let Some(ref role) = role_filter {
|
||||
db.remove_relay_member_if_role(&pubkey_hex, role).await
|
||||
} else {
|
||||
db.remove_relay_member(&pubkey_hex).await
|
||||
};
|
||||
|
||||
match result {
|
||||
Ok(RemoveResult::Removed) => println!("removed {pubkey_hex}"),
|
||||
Ok(RemoveResult::NotFound) => {
|
||||
eprintln!("error: member not found: {pubkey_hex}");
|
||||
return Ok(2);
|
||||
}
|
||||
Ok(RemoveResult::IsOwner) => {
|
||||
eprintln!(
|
||||
"error: cannot remove relay owner: {pubkey_hex}\n\
|
||||
To change the owner, update RELAY_OWNER_PUBKEY and restart."
|
||||
);
|
||||
return Ok(3);
|
||||
}
|
||||
Ok(RemoveResult::RoleMismatch) => {
|
||||
let role_str = role_filter.as_deref().unwrap_or("(unknown)");
|
||||
eprintln!("error: role mismatch — {pubkey_hex} is not currently '{role_str}'");
|
||||
return Ok(4);
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("error: DB write failed: {e}");
|
||||
return Ok(5);
|
||||
}
|
||||
}
|
||||
|
||||
if let Err(e) = publish_membership_list_with_bump(&db, &pubsub, &relay_keypair).await {
|
||||
eprintln!("warning: member removed from DB but list publish failed: {e}");
|
||||
}
|
||||
|
||||
Ok(0)
|
||||
}
|
||||
|
||||
async fn cmd_list_members() -> Result<i32> {
|
||||
let db = connect_db().await?;
|
||||
let members = db.list_relay_members().await?;
|
||||
|
||||
if members.is_empty() {
|
||||
println!("(no relay members)");
|
||||
return Ok(0);
|
||||
}
|
||||
|
||||
println!(
|
||||
"{:<66} {:<8} {:<66} created_at",
|
||||
"pubkey", "role", "added_by"
|
||||
);
|
||||
println!("{}", "-".repeat(160));
|
||||
for m in &members {
|
||||
let added_by = m.added_by.as_deref().unwrap_or("-");
|
||||
println!(
|
||||
"{:<66} {:<8} {:<66} {}",
|
||||
m.pubkey,
|
||||
m.role,
|
||||
added_by,
|
||||
m.created_at.format("%Y-%m-%dT%H:%M:%SZ")
|
||||
);
|
||||
}
|
||||
|
||||
Ok(0)
|
||||
}
|
||||
|
||||
/// Validate that `role` is `"member"` or `"admin"`. Rejects `"owner"`.
|
||||
fn validate_role(role: &str) -> std::result::Result<(), String> {
|
||||
match role {
|
||||
"member" | "admin" => Ok(()),
|
||||
"owner" => {
|
||||
Err("role 'owner' cannot be set via CLI — use RELAY_OWNER_PUBKEY config".to_string())
|
||||
}
|
||||
other => Err(format!(
|
||||
"invalid role '{other}': must be 'member' or 'admin'"
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse a bech32 npub or 64-char hex pubkey into lowercase hex.
|
||||
fn parse_pubkey_hex(input: &str) -> std::result::Result<String, String> {
|
||||
nostr::PublicKey::parse(input)
|
||||
.map(|pk| pk.to_hex())
|
||||
.map_err(|e| format!("invalid pubkey '{input}': {e}"))
|
||||
}
|
||||
|
||||
/// Publish kind:13534 with `custom_created_at = max(now, newest_existing + 1s)`.
|
||||
///
|
||||
/// Guarantees the new event is not dominated by a same-second prior invocation,
|
||||
/// so `replace_addressable_event` always inserts and dispatches to Redis.
|
||||
///
|
||||
/// See module-level doc for the TOCTOU caveat on concurrent CLI processes.
|
||||
async fn publish_membership_list_with_bump(
|
||||
db: &Db,
|
||||
pubsub: &Arc<PubSubManager>,
|
||||
relay_keypair: &Keys,
|
||||
) -> Result<()> {
|
||||
let now = std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.unwrap_or_default()
|
||||
.as_secs();
|
||||
|
||||
let relay_pubkey = relay_keypair.public_key();
|
||||
let relay_pubkey_bytes = relay_pubkey.to_bytes();
|
||||
|
||||
// Query the newest existing kind:13534 for this relay's pubkey (channel_id=None).
|
||||
let newest_ts = db
|
||||
.get_latest_global_replaceable(KIND_NIP43_MEMBERSHIP_LIST as i32, &relay_pubkey_bytes)
|
||||
.await?
|
||||
.map(|e| e.event.created_at.as_secs());
|
||||
|
||||
// custom_created_at = max(now, existing + 1s) — defeats same-second domination.
|
||||
let ts = match newest_ts {
|
||||
Some(existing) => (existing + 1).max(now),
|
||||
None => now,
|
||||
};
|
||||
|
||||
let members = db.list_relay_members().await?;
|
||||
|
||||
let mut tags: Vec<Tag> = Vec::with_capacity(members.len() + 1);
|
||||
// NIP-70 protected-event marker — prevents re-broadcasting by third parties.
|
||||
tags.push(Tag::parse(["-"]).map_err(|e| anyhow::anyhow!("failed to build '-' tag: {e}"))?);
|
||||
for member in &members {
|
||||
tags.push(
|
||||
Tag::parse(["member", &member.pubkey, &member.role])
|
||||
.map_err(|e| anyhow::anyhow!("failed to build member tag: {e}"))?,
|
||||
);
|
||||
}
|
||||
|
||||
let event = EventBuilder::new(Kind::Custom(KIND_NIP43_MEMBERSHIP_LIST as u16), "")
|
||||
.tags(tags)
|
||||
.custom_created_at(nostr::Timestamp::from(ts))
|
||||
.sign_with_keys(relay_keypair)
|
||||
.map_err(|e| anyhow::anyhow!("failed to sign kind:13534: {e}"))?;
|
||||
|
||||
let (stored, was_inserted) = db.replace_addressable_event(&event, None).await?;
|
||||
if was_inserted {
|
||||
// Publish to Redis so live clients receive the updated roster.
|
||||
// Uses channel_id=Nil (global scope) matching the relay's own publish path.
|
||||
let pubsub_channel = uuid::Uuid::nil();
|
||||
if let Err(e) = pubsub.publish_event(pubsub_channel, &stored.event).await {
|
||||
warn!("Redis publish of kind:13534 failed: {e}");
|
||||
}
|
||||
}
|
||||
|
||||
tracing::info!(
|
||||
member_count = members.len(),
|
||||
ts,
|
||||
"NIP-43 membership list published by buzz-admin"
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Connect to DB, Redis pub/sub, and load the relay keypair.
|
||||
///
|
||||
/// `BUZZ_RELAY_PRIVATE_KEY` is required — the CLI signs kind:13534 events.
|
||||
async fn connect_member_services() -> Result<(Db, Arc<PubSubManager>, Keys)> {
|
||||
let db = connect_db().await?;
|
||||
|
||||
let relay_keypair = {
|
||||
let hex = std::env::var("BUZZ_RELAY_PRIVATE_KEY").map_err(|_| {
|
||||
anyhow::anyhow!(
|
||||
"BUZZ_RELAY_PRIVATE_KEY is required for add-member/remove-member.\n\
|
||||
The relay must have a stable signing key to publish kind:13534 events."
|
||||
)
|
||||
})?;
|
||||
Keys::parse(&hex).map_err(|e| anyhow::anyhow!("invalid BUZZ_RELAY_PRIVATE_KEY: {e}"))?
|
||||
};
|
||||
|
||||
let redis_url =
|
||||
std::env::var("REDIS_URL").unwrap_or_else(|_| "redis://localhost:6379".to_string());
|
||||
|
||||
let redis_pool = {
|
||||
let cfg = deadpool_redis::Config::from_url(&redis_url);
|
||||
cfg.create_pool(Some(deadpool_redis::Runtime::Tokio1))
|
||||
.map_err(|e| anyhow::anyhow!("Redis pool creation failed: {e}"))?
|
||||
};
|
||||
|
||||
let pubsub = Arc::new(
|
||||
PubSubManager::new(&redis_url, redis_pool)
|
||||
.await
|
||||
.map_err(|e| anyhow::anyhow!("PubSub init failed: {e}"))?,
|
||||
);
|
||||
|
||||
Ok((db, pubsub, relay_keypair))
|
||||
}
|
||||
|
||||
async fn connect_db() -> Result<Db> {
|
||||
let db_url = std::env::var("DATABASE_URL")
|
||||
.unwrap_or_else(|_| "postgres://buzz:buzz_dev@localhost:5432/buzz".to_string());
|
||||
@@ -112,7 +379,6 @@ async fn connect_db() -> Result<Db> {
|
||||
async fn reconcile_channels(relay_key_arg: Option<String>) -> Result<()> {
|
||||
use buzz_core::kind::KIND_NIP29_GROUP_ADMINS;
|
||||
use buzz_db::event::EventQuery;
|
||||
use nostr::{EventBuilder, Kind, Tag};
|
||||
|
||||
let db = connect_db().await?;
|
||||
|
||||
|
||||
@@ -4,8 +4,10 @@ use serde_json::json;
|
||||
use tokio::sync::{watch, Semaphore};
|
||||
use tokio::task::JoinSet;
|
||||
|
||||
use crate::builtin;
|
||||
use crate::config::{Config, MAX_PROMPT_BYTES, MAX_TOOL_CALLS_PER_TURN, MAX_TOOL_RESULT_BYTES};
|
||||
use crate::handoff::HandoffOutcome;
|
||||
use crate::hints::SkillEntry;
|
||||
use crate::llm::Llm;
|
||||
use crate::mcp::McpRegistry;
|
||||
use crate::mcp::ResultBudget;
|
||||
@@ -25,6 +27,8 @@ pub struct RunCtx<'a> {
|
||||
pub system_prompt: &'a str,
|
||||
pub llm: &'a Llm,
|
||||
pub mcp: &'a Arc<McpRegistry>,
|
||||
/// Skills discovered at session creation; used by the built-in `load_skill` tool.
|
||||
pub skills: &'a [SkillEntry],
|
||||
pub wire: &'a WireSender,
|
||||
pub cancel: &'a mut watch::Receiver<bool>,
|
||||
pub history: &'a mut Vec<HistoryItem>,
|
||||
@@ -90,7 +94,11 @@ impl RunCtx<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
let tools = self.mcp.tools();
|
||||
let mut tools = self.mcp.tools();
|
||||
// Inject the built-in load_skill tool when skills are available.
|
||||
if !self.skills.is_empty() {
|
||||
tools.push(builtin::load_skill_def());
|
||||
}
|
||||
round = round.saturating_add(1);
|
||||
let response = tokio::select! {
|
||||
biased;
|
||||
@@ -246,6 +254,17 @@ impl RunCtx<'_> {
|
||||
return Some(StopReason::Cancelled);
|
||||
}
|
||||
emit_pending(self.wire, self.session_id, call).await;
|
||||
|
||||
// Built-in load_skill: execute inline, no MCP round-trip.
|
||||
if call.name == builtin::LOAD_SKILL_TOOL {
|
||||
emit_in_progress(self.wire, self.session_id, call).await;
|
||||
let mut result = builtin::call_load_skill(&call.arguments, self.skills).await;
|
||||
result.provider_id = call.provider_id.clone();
|
||||
emit_completed(self.wire, self.session_id, call, &result).await;
|
||||
results[idx] = Some(result);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Hook tools (bare name starts with `_`) are invisible to the
|
||||
// LLM and only callable via `call_hooks`. Treat any direct
|
||||
// invocation as if the tool didn't exist.
|
||||
|
||||
@@ -0,0 +1,530 @@
|
||||
//! Built-in tools that run in-process, bypassing MCP.
|
||||
//!
|
||||
//! Currently: `load_skill` — reads a skill's full SKILL.md body from disk
|
||||
//! and returns it so the agent can load skill content on demand rather than
|
||||
//! having every skill inlined into the system prompt at session start.
|
||||
|
||||
use serde_json::{json, Value};
|
||||
|
||||
use crate::hints::{strip_frontmatter, SkillEntry, MAX_SKILL_BODY_BYTES};
|
||||
use crate::mcp::truncate_at_boundary;
|
||||
use crate::types::{ToolDef, ToolResult, ToolResultContent};
|
||||
|
||||
pub const LOAD_SKILL_TOOL: &str = "load_skill";
|
||||
|
||||
/// Return the `ToolDef` for `load_skill` to include in the LLM tool list.
|
||||
pub fn load_skill_def() -> ToolDef {
|
||||
ToolDef {
|
||||
name: LOAD_SKILL_TOOL.to_owned(),
|
||||
description: "Load the full content of a skill by name. \
|
||||
Call this before using a skill — the system prompt lists skill names \
|
||||
and descriptions only; the full instructions are loaded on demand. \
|
||||
To load a supporting file within a skill, use the form \
|
||||
\"skill-name/relative/path\" (e.g. \"my-skill/references/foo.md\")."
|
||||
.to_owned(),
|
||||
input_schema: json!({
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"name": {
|
||||
"type": "string",
|
||||
"description": "The skill name as listed in the Available Skills section, \
|
||||
or \"skill-name/relative/path\" to load a supporting file."
|
||||
}
|
||||
},
|
||||
"required": ["name"]
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
/// Execute a `load_skill` call. Returns a `ToolResult` on success or a
|
||||
/// user-visible error result if the skill is not found or cannot be read.
|
||||
pub async fn call_load_skill(arguments: &Value, skills: &[SkillEntry]) -> ToolResult {
|
||||
let name = match arguments.get("name").and_then(Value::as_str) {
|
||||
Some(n) => n,
|
||||
None => {
|
||||
return error_result("load_skill: missing required argument \"name\"");
|
||||
}
|
||||
};
|
||||
|
||||
// Two forms:
|
||||
// "skill-name" → load SKILL.md body + ## Supporting Files section
|
||||
// "skill-name/rel/path" → load a specific supporting file
|
||||
if let Some((skill_name, rel_path)) = name.split_once('/') {
|
||||
return load_supporting_file(skill_name, rel_path, skills).await;
|
||||
}
|
||||
|
||||
// Plain skill-name form: load SKILL.md body.
|
||||
let entry = match skills.iter().find(|s| s.name == name) {
|
||||
Some(e) => e,
|
||||
None => {
|
||||
let available: Vec<&str> = skills.iter().map(|s| s.name.as_str()).collect();
|
||||
return error_result(&format!(
|
||||
"load_skill: skill {name:?} not found. Available: {available:?}"
|
||||
));
|
||||
}
|
||||
};
|
||||
|
||||
// Read the file off the async executor to avoid blocking a Tokio worker.
|
||||
let skill_path = entry.path.clone();
|
||||
let raw = match tokio::task::spawn_blocking(move || std::fs::read_to_string(&skill_path))
|
||||
.await
|
||||
.unwrap_or_else(|e| Err(std::io::Error::other(e)))
|
||||
{
|
||||
Ok(s) => s,
|
||||
Err(e) => {
|
||||
return error_result(&format!("load_skill: could not read {:?}: {e}", entry.path));
|
||||
}
|
||||
};
|
||||
|
||||
// Strip the YAML frontmatter — the agent already knows name/description
|
||||
// from the system prompt; return only the body.
|
||||
let body = strip_frontmatter(&raw);
|
||||
|
||||
let mut output = body.to_owned();
|
||||
|
||||
// Append ## Supporting Files section if this skill has any.
|
||||
if !entry.supporting_files.is_empty() {
|
||||
let skill_dir = entry.path.parent().unwrap_or(&entry.path);
|
||||
output.push_str("\n\n## Supporting Files\n\n");
|
||||
for file in &entry.supporting_files {
|
||||
if let Ok(rel) = file.strip_prefix(skill_dir) {
|
||||
let rel_str = rel.to_string_lossy().replace('\\', "/");
|
||||
output.push_str(&format!(
|
||||
"- {} (load_skill(name: \"{}/{}\"))\n",
|
||||
rel_str, entry.name, rel_str
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Apply the size cap to the full output (body + Supporting Files section)
|
||||
// so the total tool result stays within MAX_SKILL_BODY_BYTES.
|
||||
let output = if output.len() > MAX_SKILL_BODY_BYTES {
|
||||
truncate_at_boundary(&output, MAX_SKILL_BODY_BYTES).to_owned()
|
||||
} else {
|
||||
output
|
||||
};
|
||||
|
||||
ToolResult {
|
||||
provider_id: String::new(),
|
||||
content: vec![ToolResultContent::Text(output)],
|
||||
is_error: false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Load a supporting file identified by `skill_name/rel_path`.
|
||||
/// Matches against the pre-enumerated `supporting_files` list and applies a
|
||||
/// canonicalize-based traversal guard before reading.
|
||||
async fn load_supporting_file(
|
||||
skill_name: &str,
|
||||
rel_path: &str,
|
||||
skills: &[SkillEntry],
|
||||
) -> ToolResult {
|
||||
let rel_path = rel_path.replace('\\', "/");
|
||||
|
||||
let entry = match skills.iter().find(|s| s.name == skill_name) {
|
||||
Some(e) => e,
|
||||
None => {
|
||||
let available: Vec<&str> = skills.iter().map(|s| s.name.as_str()).collect();
|
||||
return error_result(&format!(
|
||||
"load_skill: skill {skill_name:?} not found. Available: {available:?}"
|
||||
));
|
||||
}
|
||||
};
|
||||
|
||||
let skill_dir = match entry.path.parent() {
|
||||
Some(d) => d,
|
||||
None => {
|
||||
return error_result(&format!(
|
||||
"load_skill: could not determine skill directory for {skill_name:?}"
|
||||
));
|
||||
}
|
||||
};
|
||||
|
||||
// Match rel_path against the pre-enumerated supporting_files list.
|
||||
let matched = entry.supporting_files.iter().find(|f| {
|
||||
f.strip_prefix(skill_dir)
|
||||
.map(|r| r.to_string_lossy().replace('\\', "/") == rel_path)
|
||||
.unwrap_or(false)
|
||||
});
|
||||
|
||||
let file_path = match matched {
|
||||
Some(p) => p,
|
||||
None => {
|
||||
let available: Vec<String> = entry
|
||||
.supporting_files
|
||||
.iter()
|
||||
.filter_map(|f| {
|
||||
f.strip_prefix(skill_dir)
|
||||
.ok()
|
||||
.map(|r| r.to_string_lossy().replace('\\', "/"))
|
||||
})
|
||||
.collect();
|
||||
if available.is_empty() {
|
||||
return error_result(&format!(
|
||||
"load_skill: skill {skill_name:?} has no supporting files."
|
||||
));
|
||||
}
|
||||
return error_result(&format!(
|
||||
"load_skill: file {rel_path:?} not found in skill {skill_name:?}. \
|
||||
Available: {available:?}"
|
||||
));
|
||||
}
|
||||
};
|
||||
|
||||
// Traversal guard: canonicalize both paths and verify the file stays inside
|
||||
// the skill directory. Fail hard if the skill directory itself can't be
|
||||
// canonicalized — a degraded guard is worse than no guard.
|
||||
let canonical_skill_dir = match skill_dir.canonicalize() {
|
||||
Ok(p) => p,
|
||||
Err(e) => {
|
||||
return error_result(&format!(
|
||||
"load_skill: could not canonicalize skill directory for {skill_name:?}: {e}"
|
||||
));
|
||||
}
|
||||
};
|
||||
|
||||
// Clone the path so we can move it into spawn_blocking.
|
||||
let file_path = file_path.clone();
|
||||
let skill_name = skill_name.to_owned();
|
||||
let rel_path_owned = rel_path.clone();
|
||||
|
||||
match tokio::task::spawn_blocking(move || file_path.canonicalize().map(|c| (c, file_path)))
|
||||
.await
|
||||
.unwrap_or_else(|e| Err(std::io::Error::other(e)))
|
||||
{
|
||||
Ok((canonical_file, resolved_path)) if canonical_file.starts_with(&canonical_skill_dir) => {
|
||||
match tokio::task::spawn_blocking(move || std::fs::read_to_string(&resolved_path))
|
||||
.await
|
||||
.unwrap_or_else(|e| Err(std::io::Error::other(e)))
|
||||
{
|
||||
Ok(content) => ToolResult {
|
||||
provider_id: String::new(),
|
||||
content: vec![ToolResultContent::Text(format!(
|
||||
"# Loaded: {}/{}\n\n{}\n\n---\nFile loaded into context.",
|
||||
skill_name, rel_path_owned, content
|
||||
))],
|
||||
is_error: false,
|
||||
},
|
||||
Err(e) => error_result(&format!(
|
||||
"load_skill: could not read {skill_name:?}/{rel_path_owned}: {e}"
|
||||
)),
|
||||
}
|
||||
}
|
||||
Ok(_) => error_result(&format!(
|
||||
"load_skill: refusing to load {skill_name:?}/{rel_path_owned}: \
|
||||
resolves outside the skill directory"
|
||||
)),
|
||||
Err(e) => error_result(&format!(
|
||||
"load_skill: could not resolve {skill_name:?}/{rel_path_owned}: {e}"
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
fn error_result(msg: &str) -> ToolResult {
|
||||
ToolResult {
|
||||
provider_id: String::new(),
|
||||
content: vec![ToolResultContent::Text(msg.to_owned())],
|
||||
is_error: true,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::path::PathBuf;
|
||||
use tempfile::TempDir;
|
||||
|
||||
fn text_content(result: &ToolResult) -> String {
|
||||
match &result.content[0] {
|
||||
ToolResultContent::Text(t) => t.clone(),
|
||||
ToolResultContent::Image { .. } => panic!("unexpected Image content in test"),
|
||||
}
|
||||
}
|
||||
|
||||
fn make_skill(name: &str, description: &str, path: PathBuf) -> SkillEntry {
|
||||
SkillEntry {
|
||||
name: name.to_owned(),
|
||||
description: description.to_owned(),
|
||||
path,
|
||||
supporting_files: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn make_skill_with_files(
|
||||
name: &str,
|
||||
description: &str,
|
||||
path: PathBuf,
|
||||
supporting_files: Vec<PathBuf>,
|
||||
) -> SkillEntry {
|
||||
SkillEntry {
|
||||
name: name.to_owned(),
|
||||
description: description.to_owned(),
|
||||
path,
|
||||
supporting_files,
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn call_load_skill_missing_name_arg() {
|
||||
let result = call_load_skill(&serde_json::json!({}), &[]).await;
|
||||
assert!(result.is_error);
|
||||
let text = text_content(&result);
|
||||
assert!(text.contains("missing required argument"), "got: {text}");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn call_load_skill_skill_not_found() {
|
||||
let result = call_load_skill(&serde_json::json!({"name": "no-such"}), &[]).await;
|
||||
assert!(result.is_error);
|
||||
let text = text_content(&result);
|
||||
assert!(text.contains("not found"), "got: {text}");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn call_load_skill_returns_body_strips_frontmatter() {
|
||||
let tmp = TempDir::new().unwrap();
|
||||
let skill_md = tmp.path().join("SKILL.md");
|
||||
std::fs::write(
|
||||
&skill_md,
|
||||
"---\nname: test\ndescription: A test\n---\nSkill body here.\n",
|
||||
)
|
||||
.unwrap();
|
||||
let skills = vec![make_skill("test", "A test", skill_md)];
|
||||
let result = call_load_skill(&serde_json::json!({"name": "test"}), &skills).await;
|
||||
assert!(!result.is_error);
|
||||
let text = text_content(&result);
|
||||
assert!(text.contains("Skill body here."), "got: {text}");
|
||||
assert!(
|
||||
!text.contains("---"),
|
||||
"frontmatter should be stripped: {text}"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn call_load_skill_appends_supporting_files_section() {
|
||||
let tmp = TempDir::new().unwrap();
|
||||
let skill_dir = tmp.path();
|
||||
let skill_md = skill_dir.join("SKILL.md");
|
||||
std::fs::write(
|
||||
&skill_md,
|
||||
"---\nname: my-skill\ndescription: desc\n---\nBody.\n",
|
||||
)
|
||||
.unwrap();
|
||||
let refs_dir = skill_dir.join("references");
|
||||
std::fs::create_dir_all(&refs_dir).unwrap();
|
||||
let ref_file = refs_dir.join("foo.md");
|
||||
std::fs::write(&ref_file, "Reference content.").unwrap();
|
||||
|
||||
let skills = vec![make_skill_with_files(
|
||||
"my-skill",
|
||||
"desc",
|
||||
skill_md,
|
||||
vec![ref_file],
|
||||
)];
|
||||
let result = call_load_skill(&serde_json::json!({"name": "my-skill"}), &skills).await;
|
||||
assert!(!result.is_error);
|
||||
let text = text_content(&result);
|
||||
assert!(text.contains("Body."), "body missing: {text}");
|
||||
assert!(
|
||||
text.contains("## Supporting Files"),
|
||||
"missing Supporting Files section: {text}"
|
||||
);
|
||||
assert!(
|
||||
text.contains("references/foo.md"),
|
||||
"missing file listing: {text}"
|
||||
);
|
||||
assert!(
|
||||
text.contains("load_skill(name: \"my-skill/references/foo.md\")"),
|
||||
"missing load_skill hint: {text}"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn call_load_skill_no_supporting_files_section_when_empty() {
|
||||
let tmp = TempDir::new().unwrap();
|
||||
let skill_md = tmp.path().join("SKILL.md");
|
||||
std::fs::write(
|
||||
&skill_md,
|
||||
"---\nname: bare\ndescription: desc\n---\nBody.\n",
|
||||
)
|
||||
.unwrap();
|
||||
let skills = vec![make_skill("bare", "desc", skill_md)];
|
||||
let result = call_load_skill(&serde_json::json!({"name": "bare"}), &skills).await;
|
||||
assert!(!result.is_error);
|
||||
let text = text_content(&result);
|
||||
assert!(
|
||||
!text.contains("## Supporting Files"),
|
||||
"should not have Supporting Files section when none: {text}"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn call_load_skill_supporting_file_returns_content() {
|
||||
let tmp = TempDir::new().unwrap();
|
||||
let skill_dir = tmp.path();
|
||||
let skill_md = skill_dir.join("SKILL.md");
|
||||
std::fs::write(
|
||||
&skill_md,
|
||||
"---\nname: my-skill\ndescription: desc\n---\nBody.\n",
|
||||
)
|
||||
.unwrap();
|
||||
let refs_dir = skill_dir.join("references");
|
||||
std::fs::create_dir_all(&refs_dir).unwrap();
|
||||
let ref_file = refs_dir.join("foo.md");
|
||||
std::fs::write(&ref_file, "Reference content here.").unwrap();
|
||||
|
||||
let skills = vec![make_skill_with_files(
|
||||
"my-skill",
|
||||
"desc",
|
||||
skill_md,
|
||||
vec![ref_file],
|
||||
)];
|
||||
let result = call_load_skill(
|
||||
&serde_json::json!({"name": "my-skill/references/foo.md"}),
|
||||
&skills,
|
||||
)
|
||||
.await;
|
||||
assert!(!result.is_error, "expected success, got error");
|
||||
let text = text_content(&result);
|
||||
assert!(
|
||||
text.contains("Reference content here."),
|
||||
"file content missing: {text}"
|
||||
);
|
||||
assert!(
|
||||
text.contains("# Loaded: my-skill/references/foo.md"),
|
||||
"missing header: {text}"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn call_load_skill_supporting_file_not_found_lists_available() {
|
||||
let tmp = TempDir::new().unwrap();
|
||||
let skill_dir = tmp.path();
|
||||
let skill_md = skill_dir.join("SKILL.md");
|
||||
std::fs::write(
|
||||
&skill_md,
|
||||
"---\nname: my-skill\ndescription: desc\n---\nBody.\n",
|
||||
)
|
||||
.unwrap();
|
||||
let refs_dir = skill_dir.join("references");
|
||||
std::fs::create_dir_all(&refs_dir).unwrap();
|
||||
let ref_file = refs_dir.join("foo.md");
|
||||
std::fs::write(&ref_file, "content").unwrap();
|
||||
|
||||
let skills = vec![make_skill_with_files(
|
||||
"my-skill",
|
||||
"desc",
|
||||
skill_md,
|
||||
vec![ref_file],
|
||||
)];
|
||||
let result = call_load_skill(
|
||||
&serde_json::json!({"name": "my-skill/references/missing.md"}),
|
||||
&skills,
|
||||
)
|
||||
.await;
|
||||
assert!(result.is_error);
|
||||
let text = text_content(&result);
|
||||
assert!(text.contains("not found"), "got: {text}");
|
||||
assert!(
|
||||
text.contains("references/foo.md"),
|
||||
"should list available: {text}"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn call_load_skill_no_supporting_files_error_message() {
|
||||
let tmp = TempDir::new().unwrap();
|
||||
let skill_md = tmp.path().join("SKILL.md");
|
||||
std::fs::write(
|
||||
&skill_md,
|
||||
"---\nname: bare\ndescription: desc\n---\nBody.\n",
|
||||
)
|
||||
.unwrap();
|
||||
let skills = vec![make_skill("bare", "desc", skill_md)];
|
||||
let result =
|
||||
call_load_skill(&serde_json::json!({"name": "bare/anything.md"}), &skills).await;
|
||||
assert!(result.is_error);
|
||||
let text = text_content(&result);
|
||||
assert!(text.contains("no supporting files"), "got: {text}");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn call_load_skill_traversal_guard_rejects_escape() {
|
||||
let tmp = TempDir::new().unwrap();
|
||||
let skill_dir = tmp.path().join("my-skill");
|
||||
std::fs::create_dir_all(&skill_dir).unwrap();
|
||||
let skill_md = skill_dir.join("SKILL.md");
|
||||
std::fs::write(
|
||||
&skill_md,
|
||||
"---\nname: my-skill\ndescription: desc\n---\nBody.\n",
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
// Create a file outside the skill dir that we'll try to reference.
|
||||
let outside_file = tmp.path().join("secret.txt");
|
||||
std::fs::write(&outside_file, "secret content").unwrap();
|
||||
|
||||
// Manually construct a SkillEntry with a supporting_files entry that
|
||||
// points outside the skill dir — simulating a crafted/malicious entry.
|
||||
// The traversal guard should catch this.
|
||||
let skills = vec![make_skill_with_files(
|
||||
"my-skill",
|
||||
"desc",
|
||||
skill_md.clone(),
|
||||
vec![outside_file.clone()],
|
||||
)];
|
||||
|
||||
// The slash form splits "my-skill/../secret.txt" into skill_name="my-skill"
|
||||
// and rel_path="../secret.txt". strip_prefix(skill_dir) on outside_file
|
||||
// fails, so it won't match any supporting_files entry — the pre-enumeration
|
||||
// guard rejects it before the canonicalize guard even fires.
|
||||
let result = call_load_skill(
|
||||
&serde_json::json!({"name": "my-skill/../secret.txt"}),
|
||||
&skills,
|
||||
)
|
||||
.await;
|
||||
assert!(result.is_error, "traversal attempt should be rejected");
|
||||
let text = text_content(&result);
|
||||
assert!(
|
||||
!text.contains("secret content"),
|
||||
"secret content must not be returned: {text}"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn call_load_skill_truncates_large_body() {
|
||||
let tmp = TempDir::new().unwrap();
|
||||
let skill_dir = tmp.path();
|
||||
let skill_md = skill_dir.join("SKILL.md");
|
||||
// Build a body that exceeds MAX_SKILL_BODY_BYTES (32 KiB).
|
||||
let large_body = "x".repeat(40 * 1024);
|
||||
std::fs::write(
|
||||
&skill_md,
|
||||
format!("---\nname: big\ndescription: desc\n---\n{large_body}\n"),
|
||||
)
|
||||
.unwrap();
|
||||
// Add a supporting file so the Supporting Files section is also appended
|
||||
// before the cap is applied.
|
||||
let refs_dir = skill_dir.join("references");
|
||||
std::fs::create_dir_all(&refs_dir).unwrap();
|
||||
let ref_file = refs_dir.join("extra.md");
|
||||
std::fs::write(&ref_file, "extra content").unwrap();
|
||||
|
||||
let skills = vec![make_skill_with_files(
|
||||
"big",
|
||||
"desc",
|
||||
skill_md,
|
||||
vec![ref_file],
|
||||
)];
|
||||
let result = call_load_skill(&serde_json::json!({"name": "big"}), &skills).await;
|
||||
assert!(!result.is_error);
|
||||
let text = text_content(&result);
|
||||
assert!(
|
||||
text.len() <= MAX_SKILL_BODY_BYTES,
|
||||
"output length {} exceeds MAX_SKILL_BODY_BYTES {}",
|
||||
text.len(),
|
||||
MAX_SKILL_BODY_BYTES
|
||||
);
|
||||
}
|
||||
}
|
||||
+205
-33
@@ -4,17 +4,23 @@ use std::path::{Path, PathBuf};
|
||||
use crate::mcp::truncate_at_boundary;
|
||||
|
||||
const MAX_HINTS_BYTES: usize = 128 * 1024;
|
||||
const MAX_SKILL_BODY_BYTES: usize = 32 * 1024;
|
||||
pub const MAX_SKILL_BODY_BYTES: usize = 32 * 1024;
|
||||
const SKILL_DIRS: &[&str] = &[".agents/skills", ".goose/skills", ".claude/skills"];
|
||||
|
||||
fn home_dir() -> Option<PathBuf> {
|
||||
std::env::var("HOME").ok().map(PathBuf::from)
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct SkillEntry {
|
||||
pub name: String,
|
||||
pub description: String,
|
||||
pub body: String,
|
||||
/// Absolute path to the SKILL.md file; used by `load_skill` to read on demand.
|
||||
pub path: PathBuf,
|
||||
/// Absolute paths to every non-SKILL.md file in the skill directory tree.
|
||||
/// Pre-enumerated at discovery time so `load_skill` can match by relative path
|
||||
/// without doing arbitrary filesystem lookups at call time.
|
||||
pub supporting_files: Vec<PathBuf>,
|
||||
}
|
||||
|
||||
/// Handles both normal repos (`.git/` dir) and worktrees (`.git` file).
|
||||
@@ -77,15 +83,12 @@ fn load_hint_files_impl(cwd: &Path, home: Option<&Path>) -> String {
|
||||
result
|
||||
}
|
||||
|
||||
fn parse_skill_frontmatter(content: &str) -> Option<(String, String, String)> {
|
||||
fn parse_skill_frontmatter(content: &str) -> Option<(String, String)> {
|
||||
// Must start with `---`
|
||||
let rest = content.strip_prefix("---\n")?;
|
||||
// Find the closing `---`
|
||||
let close_pos = rest.find("\n---")?;
|
||||
let yaml_block = &rest[..close_pos];
|
||||
// Everything after the closing `---\n` (or `---` at end) is the body.
|
||||
let after_close = &rest[close_pos + 4..]; // skip "\n---"
|
||||
let body = after_close.strip_prefix('\n').unwrap_or(after_close);
|
||||
|
||||
let map: HashMap<String, serde_yaml::Value> = serde_yaml::from_str(yaml_block).ok()?;
|
||||
let name = map
|
||||
@@ -101,13 +104,7 @@ fn parse_skill_frontmatter(content: &str) -> Option<(String, String, String)> {
|
||||
.unwrap_or("")
|
||||
.to_string();
|
||||
|
||||
let body = if body.len() > MAX_SKILL_BODY_BYTES {
|
||||
truncate_at_boundary(body, MAX_SKILL_BODY_BYTES).to_string()
|
||||
} else {
|
||||
body.to_string()
|
||||
};
|
||||
|
||||
Some((name, description, body))
|
||||
Some((name, description))
|
||||
}
|
||||
|
||||
fn scan_skill_dir(dir: &Path, seen: &mut HashSet<String>, skills: &mut Vec<SkillEntry>) {
|
||||
@@ -116,7 +113,14 @@ fn scan_skill_dir(dir: &Path, seen: &mut HashSet<String>, skills: &mut Vec<Skill
|
||||
};
|
||||
let mut subdirs: Vec<PathBuf> = entries
|
||||
.filter_map(|e| e.ok())
|
||||
.filter(|e| e.file_type().map(|t| t.is_dir()).unwrap_or(false))
|
||||
// Use std::fs::metadata (follows symlinks) rather than DirEntry::file_type
|
||||
// (which returns FileType::Symlink for symlinks, causing is_dir() to return
|
||||
// false even when the symlink target is a directory).
|
||||
.filter(|e| {
|
||||
std::fs::metadata(e.path())
|
||||
.map(|m| m.is_dir())
|
||||
.unwrap_or(false)
|
||||
})
|
||||
.map(|e| e.path())
|
||||
.collect();
|
||||
subdirs.sort();
|
||||
@@ -126,21 +130,65 @@ fn scan_skill_dir(dir: &Path, seen: &mut HashSet<String>, skills: &mut Vec<Skill
|
||||
let Ok(content) = std::fs::read_to_string(&skill_md) else {
|
||||
continue;
|
||||
};
|
||||
let Some((name, description, body)) = parse_skill_frontmatter(&content) else {
|
||||
let Some((name, description)) = parse_skill_frontmatter(&content) else {
|
||||
continue;
|
||||
};
|
||||
if seen.contains(&name) {
|
||||
continue;
|
||||
}
|
||||
seen.insert(name.clone());
|
||||
|
||||
// Collect supporting files: every non-SKILL.md file in the skill dir tree.
|
||||
// Don't descend into subdirs that themselves have a SKILL.md — those are
|
||||
// separate skills with their own entries.
|
||||
let supporting_files = collect_supporting_files(&subdir);
|
||||
|
||||
skills.push(SkillEntry {
|
||||
name,
|
||||
description,
|
||||
body,
|
||||
path: skill_md,
|
||||
supporting_files,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// Walk `skill_dir` recursively and return the absolute path of every file
|
||||
/// that is not `SKILL.md`. Subdirectories that contain their own `SKILL.md`
|
||||
/// are treated as separate skills and are not descended into.
|
||||
fn collect_supporting_files(skill_dir: &Path) -> Vec<PathBuf> {
|
||||
let mut result = Vec::new();
|
||||
collect_supporting_files_impl(skill_dir, &mut result);
|
||||
result.sort();
|
||||
result
|
||||
}
|
||||
|
||||
fn collect_supporting_files_impl(current: &Path, out: &mut Vec<PathBuf>) {
|
||||
let Ok(entries) = std::fs::read_dir(current) else {
|
||||
return;
|
||||
};
|
||||
let mut items: Vec<_> = entries.filter_map(|e| e.ok()).collect();
|
||||
items.sort_by_key(|e| e.path());
|
||||
|
||||
for entry in items {
|
||||
let path = entry.path();
|
||||
// Use std::fs::metadata (follows symlinks) so symlinked subdirs and files
|
||||
// inside a skill directory are handled correctly.
|
||||
let ft = match std::fs::metadata(&path) {
|
||||
Ok(m) => m,
|
||||
Err(_) => continue,
|
||||
};
|
||||
if ft.is_dir() {
|
||||
// Don't descend into subdirs that are themselves skills.
|
||||
if path.join("SKILL.md").is_file() {
|
||||
continue;
|
||||
}
|
||||
collect_supporting_files_impl(&path, out);
|
||||
} else if ft.is_file() && path.file_name().and_then(|n| n.to_str()) != Some("SKILL.md") {
|
||||
out.push(path);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn discover_skills_impl(cwd: &Path, home: Option<&Path>) -> Vec<SkillEntry> {
|
||||
let mut seen = HashSet::new();
|
||||
let mut skills = Vec::new();
|
||||
@@ -156,16 +204,16 @@ fn discover_skills_impl(cwd: &Path, home: Option<&Path>) -> Vec<SkillEntry> {
|
||||
skills
|
||||
}
|
||||
|
||||
pub fn build_hints_section(cwd: &Path) -> String {
|
||||
pub fn build_hints_section(cwd: &Path) -> (String, Vec<SkillEntry>) {
|
||||
build_hints_section_impl(cwd, home_dir().as_deref())
|
||||
}
|
||||
|
||||
fn build_hints_section_impl(cwd: &Path, home: Option<&Path>) -> String {
|
||||
fn build_hints_section_impl(cwd: &Path, home: Option<&Path>) -> (String, Vec<SkillEntry>) {
|
||||
let hints_text = load_hint_files_impl(cwd, home);
|
||||
let skills = discover_skills_impl(cwd, home);
|
||||
|
||||
if hints_text.is_empty() && skills.is_empty() {
|
||||
return String::new();
|
||||
return (String::new(), skills);
|
||||
}
|
||||
|
||||
let mut out = String::from("# Additional Instructions\n");
|
||||
@@ -181,16 +229,25 @@ fn build_hints_section_impl(cwd: &Path, home: Option<&Path>) -> String {
|
||||
for skill in &skills {
|
||||
out.push_str(&format!("- {}: {}\n", skill.name, skill.description));
|
||||
}
|
||||
for skill in &skills {
|
||||
out.push_str(&format!("\n### {}\n", skill.name));
|
||||
out.push_str(&skill.body);
|
||||
if !skill.body.ends_with('\n') {
|
||||
out.push('\n');
|
||||
}
|
||||
}
|
||||
out.push_str(
|
||||
"\nUse the `load_skill` tool to read the full content of a skill before using it.\n",
|
||||
);
|
||||
}
|
||||
|
||||
out
|
||||
(out, skills)
|
||||
}
|
||||
|
||||
/// Strip the YAML frontmatter block from a skill file's content and return
|
||||
/// the body. If no valid frontmatter is found, returns the content unchanged.
|
||||
pub(crate) fn strip_frontmatter(content: &str) -> &str {
|
||||
let Some(rest) = content.strip_prefix("---\n") else {
|
||||
return content;
|
||||
};
|
||||
let Some(close_pos) = rest.find("\n---") else {
|
||||
return content;
|
||||
};
|
||||
let after = &rest[close_pos + 4..]; // skip "\n---"
|
||||
after.strip_prefix('\n').unwrap_or(after)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -304,6 +361,11 @@ mod tests {
|
||||
let names: Vec<&str> = skills.iter().map(|s| s.name.as_str()).collect();
|
||||
assert!(names.contains(&"my-skill"), "missing my-skill");
|
||||
assert!(names.contains(&"other-skill"), "missing other-skill");
|
||||
// Paths should point to the SKILL.md files.
|
||||
for skill in &skills {
|
||||
assert!(skill.path.exists(), "path does not exist: {:?}", skill.path);
|
||||
assert!(skill.path.ends_with("SKILL.md"));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -334,7 +396,12 @@ mod tests {
|
||||
skills[0].description, "from agents",
|
||||
"first wins (.agents/)"
|
||||
);
|
||||
assert_eq!(skills[0].body.trim(), "Agents body.");
|
||||
// Path should point to the .agents/ version (first wins).
|
||||
assert!(skills[0]
|
||||
.path
|
||||
.to_str()
|
||||
.unwrap()
|
||||
.contains(".agents/skills/shared"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -357,8 +424,9 @@ mod tests {
|
||||
#[test]
|
||||
fn build_hints_section_empty() {
|
||||
let tmp = TempDir::new().unwrap();
|
||||
let result = build_hints_section_impl(tmp.path(), None);
|
||||
let (result, skills) = build_hints_section_impl(tmp.path(), None);
|
||||
assert_eq!(result, "");
|
||||
assert!(skills.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -376,7 +444,7 @@ mod tests {
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let result = build_hints_section_impl(cwd, None);
|
||||
let (result, skills) = build_hints_section_impl(cwd, None);
|
||||
|
||||
assert!(
|
||||
result.contains("# Additional Instructions"),
|
||||
@@ -395,11 +463,24 @@ mod tests {
|
||||
result.contains("buzz-cli: CLI reference for Buzz managed agents"),
|
||||
"missing skill bullet"
|
||||
);
|
||||
assert!(result.contains("### buzz-cli"), "missing skill header");
|
||||
// Body must NOT be inlined — lazy loading only.
|
||||
assert!(
|
||||
result.contains("Use `buzz` to manage agents."),
|
||||
"missing skill body"
|
||||
!result.contains("Use `buzz` to manage agents."),
|
||||
"skill body must not be inlined in system prompt"
|
||||
);
|
||||
// The load_skill instruction must be present.
|
||||
assert!(
|
||||
result.contains("load_skill"),
|
||||
"missing load_skill instruction"
|
||||
);
|
||||
// The old ### heading format must not appear.
|
||||
assert!(
|
||||
!result.contains("### buzz-cli"),
|
||||
"skill body heading must not be inlined"
|
||||
);
|
||||
// The returned skills list should contain the discovered skill.
|
||||
assert_eq!(skills.len(), 1);
|
||||
assert_eq!(skills[0].name, "buzz-cli");
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -522,4 +603,95 @@ mod tests {
|
||||
assert_eq!(skills.len(), 1);
|
||||
assert_eq!(skills[0].name, "local");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn collect_supporting_files_finds_non_skill_md_files() {
|
||||
let tmp = TempDir::new().unwrap();
|
||||
let skill_dir = tmp.path();
|
||||
// SKILL.md should be excluded.
|
||||
std::fs::write(skill_dir.join("SKILL.md"), "---\nname: x\n---\n").unwrap();
|
||||
// A references subdir with files.
|
||||
let refs = skill_dir.join("references");
|
||||
std::fs::create_dir_all(&refs).unwrap();
|
||||
std::fs::write(refs.join("foo.md"), "foo").unwrap();
|
||||
std::fs::write(refs.join("bar.md"), "bar").unwrap();
|
||||
// A script at the top level.
|
||||
std::fs::write(skill_dir.join("setup.sh"), "#!/bin/sh").unwrap();
|
||||
|
||||
let files = collect_supporting_files(skill_dir);
|
||||
let names: Vec<String> = files
|
||||
.iter()
|
||||
.map(|p| p.file_name().unwrap().to_string_lossy().into_owned())
|
||||
.collect();
|
||||
assert!(
|
||||
names.contains(&"foo.md".to_owned()),
|
||||
"missing foo.md: {names:?}"
|
||||
);
|
||||
assert!(
|
||||
names.contains(&"bar.md".to_owned()),
|
||||
"missing bar.md: {names:?}"
|
||||
);
|
||||
assert!(
|
||||
names.contains(&"setup.sh".to_owned()),
|
||||
"missing setup.sh: {names:?}"
|
||||
);
|
||||
assert!(
|
||||
!names.contains(&"SKILL.md".to_owned()),
|
||||
"SKILL.md should be excluded: {names:?}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn collect_supporting_files_does_not_descend_into_nested_skills() {
|
||||
let tmp = TempDir::new().unwrap();
|
||||
let skill_dir = tmp.path();
|
||||
std::fs::write(skill_dir.join("SKILL.md"), "---\nname: x\n---\n").unwrap();
|
||||
std::fs::write(skill_dir.join("helper.sh"), "#!/bin/sh").unwrap();
|
||||
|
||||
// A nested subdir that is itself a skill — should not be descended into.
|
||||
let nested = skill_dir.join("nested-skill");
|
||||
std::fs::create_dir_all(&nested).unwrap();
|
||||
std::fs::write(nested.join("SKILL.md"), "---\nname: nested\n---\n").unwrap();
|
||||
std::fs::write(nested.join("secret.md"), "should not appear").unwrap();
|
||||
|
||||
let files = collect_supporting_files(skill_dir);
|
||||
let names: Vec<String> = files
|
||||
.iter()
|
||||
.map(|p| p.file_name().unwrap().to_string_lossy().into_owned())
|
||||
.collect();
|
||||
assert!(
|
||||
names.contains(&"helper.sh".to_owned()),
|
||||
"missing helper.sh: {names:?}"
|
||||
);
|
||||
assert!(
|
||||
!names.contains(&"secret.md".to_owned()),
|
||||
"nested skill's files should not appear: {names:?}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn discover_skills_populates_supporting_files() {
|
||||
let tmp = TempDir::new().unwrap();
|
||||
let cwd = tmp.path();
|
||||
let skill_dir = cwd.join(".agents/skills/with-refs");
|
||||
std::fs::create_dir_all(&skill_dir).unwrap();
|
||||
std::fs::write(
|
||||
skill_dir.join("SKILL.md"),
|
||||
"---\nname: with-refs\ndescription: Has refs\n---\nBody.\n",
|
||||
)
|
||||
.unwrap();
|
||||
let refs = skill_dir.join("references");
|
||||
std::fs::create_dir_all(&refs).unwrap();
|
||||
std::fs::write(refs.join("guide.md"), "guide content").unwrap();
|
||||
|
||||
let skills = discover_skills_impl(cwd, None);
|
||||
assert_eq!(skills.len(), 1);
|
||||
assert_eq!(skills[0].name, "with-refs");
|
||||
assert_eq!(skills[0].supporting_files.len(), 1);
|
||||
assert!(
|
||||
skills[0].supporting_files[0].ends_with("references/guide.md"),
|
||||
"unexpected path: {:?}",
|
||||
skills[0].supporting_files[0]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#![forbid(unsafe_code)]
|
||||
mod agent;
|
||||
pub mod auth;
|
||||
mod builtin;
|
||||
mod config;
|
||||
mod handoff;
|
||||
mod hints;
|
||||
@@ -19,6 +20,7 @@ use tokio::sync::{mpsc, watch, Mutex};
|
||||
|
||||
use crate::agent::RunCtx;
|
||||
use crate::config::{Config, MAX_SYSTEM_PROMPT_BYTES, PROTOCOL_VERSION};
|
||||
use crate::hints::SkillEntry;
|
||||
use crate::llm::Llm;
|
||||
use crate::mcp::McpRegistry;
|
||||
use crate::types::HistoryItem;
|
||||
@@ -36,6 +38,8 @@ struct App {
|
||||
struct Session {
|
||||
id: String,
|
||||
mcp: Arc<McpRegistry>,
|
||||
/// Skills discovered at session creation; used by the built-in `load_skill` tool.
|
||||
skills: Vec<SkillEntry>,
|
||||
history: Vec<HistoryItem>,
|
||||
cancel_tx: watch::Sender<bool>,
|
||||
busy: bool,
|
||||
@@ -267,12 +271,12 @@ async fn session_new(app: &Arc<App>, id: Value, params: Value, wire_tx: &WireSen
|
||||
.await;
|
||||
}
|
||||
}
|
||||
let (hints_text, skills) = if app.cfg.hints_enabled {
|
||||
hints::build_hints_section(std::path::Path::new(&p.cwd))
|
||||
} else {
|
||||
(String::new(), Vec::new())
|
||||
};
|
||||
let effective_system_prompt: Arc<str> = {
|
||||
let hints = if app.cfg.hints_enabled {
|
||||
hints::build_hints_section(std::path::Path::new(&p.cwd))
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
// When the harness provides a systemPrompt (base_prompt + persona), use
|
||||
// it as the primary content and suppress the default. The default is only
|
||||
// a fallback for legacy harnesses that don't send systemPrompt.
|
||||
@@ -280,10 +284,10 @@ async fn session_new(app: &Arc<App>, id: Value, params: Value, wire_tx: &WireSen
|
||||
Some(client_prompt) if !client_prompt.trim().is_empty() => client_prompt.to_owned(),
|
||||
_ => app.cfg.system_prompt.clone(),
|
||||
};
|
||||
let prompt = if hints.is_empty() {
|
||||
let prompt = if hints_text.is_empty() {
|
||||
base
|
||||
} else {
|
||||
format!("{base}\n\n{hints}")
|
||||
format!("{base}\n\n{hints_text}")
|
||||
};
|
||||
// Reject combined prompts exceeding 512KB.
|
||||
if prompt.len() > MAX_SYSTEM_PROMPT_BYTES {
|
||||
@@ -326,6 +330,7 @@ async fn session_new(app: &Arc<App>, id: Value, params: Value, wire_tx: &WireSen
|
||||
Session {
|
||||
id: session_id.clone(),
|
||||
mcp,
|
||||
skills,
|
||||
history: Vec::new(),
|
||||
cancel_tx,
|
||||
busy: false,
|
||||
@@ -369,6 +374,7 @@ async fn run_prompt(app: Arc<App>, id: Value, params: Value, wire_tx: WireSender
|
||||
let (
|
||||
sid,
|
||||
mcp,
|
||||
skills,
|
||||
mut history,
|
||||
mut original_task,
|
||||
mut handoff_count,
|
||||
@@ -395,6 +401,7 @@ async fn run_prompt(app: Arc<App>, id: Value, params: Value, wire_tx: WireSender
|
||||
system_prompt: &effective_system_prompt,
|
||||
llm: &app.llm,
|
||||
mcp: &mcp,
|
||||
skills: &skills,
|
||||
wire: &wire_tx,
|
||||
cancel: &mut cancel_rx,
|
||||
history: &mut history,
|
||||
@@ -433,6 +440,7 @@ async fn acquire_session(
|
||||
(
|
||||
String,
|
||||
Arc<McpRegistry>,
|
||||
Vec<SkillEntry>,
|
||||
Vec<HistoryItem>,
|
||||
Option<String>,
|
||||
usize,
|
||||
@@ -452,9 +460,13 @@ async fn acquire_session(
|
||||
s.busy = true;
|
||||
let (tx, rx) = watch::channel(false);
|
||||
s.cancel_tx = tx;
|
||||
// Skills are read-only after session creation; clone the Vec so RunCtx
|
||||
// can hold a reference without holding the sessions lock.
|
||||
let skills = s.skills.clone();
|
||||
Ok((
|
||||
s.id.clone(),
|
||||
s.mcp.clone(),
|
||||
skills,
|
||||
std::mem::take(&mut s.history),
|
||||
s.original_task.take(),
|
||||
s.handoff_count,
|
||||
|
||||
@@ -419,7 +419,6 @@ fn openai_image_user_content(content: &[ToolResultContent]) -> Vec<Value> {
|
||||
.collect()
|
||||
}
|
||||
|
||||
// ── OpenAI Responses API ───────────────────────────────────────────────────
|
||||
// Spec: https://platform.openai.com/docs/api-reference/responses
|
||||
//
|
||||
// Replay invariant: each assistant `function_call` input item **must**
|
||||
@@ -998,8 +997,6 @@ mod tests {
|
||||
assert_eq!(content[1]["source"]["data"], "aW1n");
|
||||
}
|
||||
|
||||
// ── Responses API unit tests ───────────────────────────────────────
|
||||
|
||||
fn cfg_responses() -> Config {
|
||||
let mut c = cfg(Provider::OpenAi);
|
||||
c.openai_api = OpenAiApi::Responses;
|
||||
@@ -1484,8 +1481,6 @@ mod tests {
|
||||
assert_eq!(sum_usage(&v, &["input_tokens", "prompt_tokens"]), None);
|
||||
}
|
||||
|
||||
// ── one-shot refresh-on-401 in post_openai ─────────────────────────
|
||||
|
||||
/// A token source whose `bearer()` always hands back the same stale
|
||||
/// token and whose `refresh_now()` mints a distinct fresh one, counting
|
||||
/// each refresh. Lets a test assert exactly how many forced refreshes a
|
||||
|
||||
@@ -345,7 +345,6 @@ async fn refresh_now_without_refresh_token_is_terminal() {
|
||||
);
|
||||
}
|
||||
|
||||
// ────────────────────────────────────────────────────────────────────────────
|
||||
// ACP-level envelope regression test.
|
||||
//
|
||||
// Boots the real buzz-agent binary with `DATABRICKS_TOKEN` set (so the
|
||||
@@ -355,7 +354,6 @@ async fn refresh_now_without_refresh_token_is_terminal() {
|
||||
// is `Bearer <token>`, and the JSON body has *no* top-level `"model"`. This
|
||||
// locks in the DRY envelope behavior so a refactor of `post_openai` can't
|
||||
// silently break Databricks.
|
||||
// ────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
use std::collections::VecDeque;
|
||||
use std::process::Stdio;
|
||||
|
||||
@@ -16,8 +16,6 @@ use tokio::io::{AsyncBufReadExt, AsyncReadExt, AsyncWriteExt, BufReader};
|
||||
use tokio::net::TcpListener;
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
// ─── Fake LLM server ────────────────────────────────────────────────────────
|
||||
|
||||
async fn spawn_fake_llm(responses: Vec<Value>) -> String {
|
||||
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let url = format!("http://{}", listener.local_addr().unwrap());
|
||||
@@ -59,8 +57,6 @@ async fn spawn_fake_llm(responses: Vec<Value>) -> String {
|
||||
url
|
||||
}
|
||||
|
||||
// ─── Request-capturing fake LLM server ──────────────────────────────────────
|
||||
|
||||
/// Like `spawn_fake_llm` but also captures the full JSON request body from each
|
||||
/// incoming HTTP request. Returns (url, captured_requests).
|
||||
async fn spawn_capturing_fake_llm(responses: Vec<Value>) -> (String, Arc<Mutex<Vec<Value>>>) {
|
||||
@@ -144,8 +140,6 @@ async fn spawn_capturing_fake_llm(responses: Vec<Value>) -> (String, Arc<Mutex<V
|
||||
(url, captures)
|
||||
}
|
||||
|
||||
// ─── ACP harness ────────────────────────────────────────────────────────────
|
||||
|
||||
struct Harness {
|
||||
child: tokio::process::Child,
|
||||
stdin: tokio::process::ChildStdin,
|
||||
@@ -221,8 +215,6 @@ impl Harness {
|
||||
}
|
||||
}
|
||||
|
||||
// ─── Canned LLM responses (OpenAI-compat shape) ─────────────────────────────
|
||||
|
||||
fn openai_text(content: &str) -> Value {
|
||||
json!({
|
||||
"id": "cc-1", "object": "chat.completion", "model": "fake-model",
|
||||
@@ -268,8 +260,6 @@ async fn init_session(h: &mut Harness) -> String {
|
||||
sid
|
||||
}
|
||||
|
||||
// ─── Tests ──────────────────────────────────────────────────────────────────
|
||||
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn text_only_end_turn() {
|
||||
let url = spawn_fake_llm(vec![openai_text("done")]).await;
|
||||
|
||||
@@ -12,8 +12,6 @@ use tokio::io::{AsyncBufReadExt, AsyncReadExt, AsyncWriteExt, BufReader};
|
||||
use tokio::net::TcpListener;
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
// ─── Fake LLM ────────────────────────────────────────────────────────────────
|
||||
|
||||
struct CapturingLlm {
|
||||
url: String,
|
||||
captured: Arc<Mutex<Vec<Value>>>,
|
||||
@@ -83,8 +81,6 @@ async fn spawn_capturing_llm(responses: Vec<Value>) -> CapturingLlm {
|
||||
CapturingLlm { url, captured }
|
||||
}
|
||||
|
||||
// ─── Harness ─────────────────────────────────────────────────────────────────
|
||||
|
||||
struct Harness {
|
||||
child: tokio::process::Child,
|
||||
stdin: tokio::process::ChildStdin,
|
||||
@@ -192,8 +188,6 @@ async fn init_session(h: &mut Harness, cwd: &str) -> String {
|
||||
.to_owned()
|
||||
}
|
||||
|
||||
// ─── Tests ───────────────────────────────────────────────────────────────────
|
||||
|
||||
/// AGENTS.md in cwd is loaded into the system prompt.
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn hints_loaded_from_cwd_agents_md() {
|
||||
@@ -254,7 +248,8 @@ async fn hints_suppressed_with_env_var() {
|
||||
h.shutdown().await;
|
||||
}
|
||||
|
||||
/// SKILL.md files in .agents/skills/ are loaded into the system prompt.
|
||||
/// SKILL.md files in .agents/skills/ are loaded into the system prompt as metadata only.
|
||||
/// The body is NOT inlined; the agent uses `load_skill` to fetch it on demand.
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn skills_loaded_from_agents_skills_dir() {
|
||||
let tmp = tempfile::TempDir::new().unwrap();
|
||||
@@ -282,13 +277,20 @@ async fn skills_loaded_from_agents_skills_dir() {
|
||||
let captured = llm.captured.lock().await;
|
||||
assert!(!captured.is_empty(), "no LLM request captured");
|
||||
let system = captured[0]["messages"][0]["content"].as_str().unwrap_or("");
|
||||
// Skill name must appear in the metadata listing.
|
||||
assert!(
|
||||
system.contains("test-skill"),
|
||||
"system prompt missing skill name: {system}"
|
||||
);
|
||||
// Body must NOT be inlined — lazy loading only.
|
||||
assert!(
|
||||
system.contains("SKILL_BODY_MARKER_77"),
|
||||
"system prompt missing skill body: {system}"
|
||||
!system.contains("SKILL_BODY_MARKER_77"),
|
||||
"skill body must not be inlined in system prompt: {system}"
|
||||
);
|
||||
// The load_skill instruction must be present.
|
||||
assert!(
|
||||
system.contains("load_skill"),
|
||||
"system prompt missing load_skill instruction: {system}"
|
||||
);
|
||||
h.shutdown().await;
|
||||
}
|
||||
@@ -378,6 +380,7 @@ async fn global_agents_md_loaded() {
|
||||
}
|
||||
|
||||
/// Global skills from ~/.agents/skills/ are loaded; project-level wins on name conflict.
|
||||
/// Bodies are NOT inlined — only metadata (name + description) appears in the system prompt.
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn global_skills_loaded_and_project_wins() {
|
||||
let home_tmp = tempfile::TempDir::new().unwrap();
|
||||
@@ -423,21 +426,149 @@ async fn global_skills_loaded_and_project_wins() {
|
||||
let captured = llm.captured.lock().await;
|
||||
assert!(!captured.is_empty(), "no LLM request captured");
|
||||
let system = captured[0]["messages"][0]["content"].as_str().unwrap_or("");
|
||||
// Both skill names must appear in the metadata listing.
|
||||
assert!(
|
||||
system.contains("global-only"),
|
||||
"system prompt missing global-only skill name: {system}"
|
||||
);
|
||||
assert!(
|
||||
system.contains("GLOBAL_SKILL_BODY_88"),
|
||||
"system prompt missing global-only skill body: {system}"
|
||||
system.contains("shared-name"),
|
||||
"system prompt missing shared-name skill: {system}"
|
||||
);
|
||||
// Project description wins over global for the shared name.
|
||||
assert!(
|
||||
system.contains("Project version"),
|
||||
"system prompt should show project description for shared-name: {system}"
|
||||
);
|
||||
assert!(
|
||||
system.contains("PROJECT_SHARED_BODY_WIN"),
|
||||
"system prompt missing project skill body: {system}"
|
||||
!system.contains("Global version"),
|
||||
"system prompt should NOT show global description for shared-name: {system}"
|
||||
);
|
||||
// Bodies must NOT be inlined.
|
||||
assert!(
|
||||
!system.contains("GLOBAL_SKILL_BODY_88"),
|
||||
"skill body must not be inlined: {system}"
|
||||
);
|
||||
assert!(
|
||||
!system.contains("PROJECT_SHARED_BODY_WIN"),
|
||||
"skill body must not be inlined: {system}"
|
||||
);
|
||||
assert!(
|
||||
!system.contains("GLOBAL_SHARED_BODY_LOSE"),
|
||||
"system prompt should NOT contain shadowed global skill body: {system}"
|
||||
"shadowed skill body must not be inlined: {system}"
|
||||
);
|
||||
h.shutdown().await;
|
||||
}
|
||||
|
||||
/// Skill directories that are symlinks (e.g. managed by ai-rules) are discovered
|
||||
/// correctly — `DirEntry::file_type()` returns `FileType::Symlink` for symlinks,
|
||||
/// so the old `is_dir()` check silently dropped them. We now use
|
||||
/// `std::fs::metadata()` which follows the symlink.
|
||||
#[cfg(unix)]
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn symlinked_skill_dir_is_discovered() {
|
||||
let real_skill_root = tempfile::TempDir::new().unwrap();
|
||||
let real_skill_dir = real_skill_root.path().join("symlinked-skill");
|
||||
std::fs::create_dir_all(&real_skill_dir).unwrap();
|
||||
std::fs::write(
|
||||
real_skill_dir.join("SKILL.md"),
|
||||
"---\nname: symlinked-skill\ndescription: A symlinked skill\n---\nSYMLINK_SKILL_BODY_42\n",
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let tmp = tempfile::TempDir::new().unwrap();
|
||||
let cwd = tmp.path();
|
||||
let skills_dir = cwd.join(".agents/skills");
|
||||
std::fs::create_dir_all(&skills_dir).unwrap();
|
||||
|
||||
// Create a symlink: .agents/skills/symlinked-skill -> real_skill_dir
|
||||
std::os::unix::fs::symlink(&real_skill_dir, skills_dir.join("symlinked-skill")).unwrap();
|
||||
|
||||
let llm = spawn_capturing_llm(vec![openai_text("done")]).await;
|
||||
let mut h = Harness::spawn_with_env(&llm.url, &[]).await;
|
||||
let sid = init_session(&mut h, cwd.to_str().unwrap()).await;
|
||||
|
||||
let p = h
|
||||
.send(
|
||||
"session/prompt",
|
||||
json!({"sessionId": sid, "prompt": [{"type":"text","text":"go"}]}),
|
||||
)
|
||||
.await;
|
||||
let _ = h.recv_until(|v| v["id"] == json!(p)).await;
|
||||
|
||||
let captured = llm.captured.lock().await;
|
||||
assert!(!captured.is_empty(), "no LLM request captured");
|
||||
let system = captured[0]["messages"][0]["content"].as_str().unwrap_or("");
|
||||
// The symlinked skill name must appear in the metadata listing.
|
||||
assert!(
|
||||
system.contains("symlinked-skill"),
|
||||
"system prompt missing symlinked skill name: {system}"
|
||||
);
|
||||
// Body must NOT be inlined.
|
||||
assert!(
|
||||
!system.contains("SYMLINK_SKILL_BODY_42"),
|
||||
"symlinked skill body must not be inlined in system prompt: {system}"
|
||||
);
|
||||
h.shutdown().await;
|
||||
}
|
||||
|
||||
/// `load_skill` tool is advertised when skills exist, and returns the skill body.
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
async fn load_skill_tool_returns_body() {
|
||||
let tmp = tempfile::TempDir::new().unwrap();
|
||||
let cwd = tmp.path();
|
||||
let skill_dir = cwd.join(".agents/skills/my-skill");
|
||||
std::fs::create_dir_all(&skill_dir).unwrap();
|
||||
std::fs::write(
|
||||
skill_dir.join("SKILL.md"),
|
||||
"---\nname: my-skill\ndescription: A skill\n---\nSKILL_BODY_CONTENT_99\n",
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
// Round 1: LLM calls load_skill("my-skill").
|
||||
// Round 2: LLM returns end_turn after seeing the body.
|
||||
let load_skill_call = json!({
|
||||
"id": "cc-ls", "object": "chat.completion", "model": "fake-model",
|
||||
"choices": [{
|
||||
"index": 0,
|
||||
"message": {
|
||||
"role": "assistant", "content": null,
|
||||
"tool_calls": [{
|
||||
"id": "tc-1", "type": "function",
|
||||
"function": {
|
||||
"name": "load_skill",
|
||||
"arguments": "{\"name\":\"my-skill\"}"
|
||||
}
|
||||
}]
|
||||
},
|
||||
"finish_reason": "tool_calls"
|
||||
}]
|
||||
});
|
||||
let end_turn = openai_text("done");
|
||||
|
||||
let llm = spawn_capturing_llm(vec![load_skill_call, end_turn]).await;
|
||||
let mut h = Harness::spawn_with_env(&llm.url, &[]).await;
|
||||
let sid = init_session(&mut h, cwd.to_str().unwrap()).await;
|
||||
|
||||
let p = h
|
||||
.send(
|
||||
"session/prompt",
|
||||
json!({"sessionId": sid, "prompt": [{"type":"text","text":"use my-skill"}]}),
|
||||
)
|
||||
.await;
|
||||
let _ = h.recv_until(|v| v["id"] == json!(p)).await;
|
||||
|
||||
// The second LLM request (round 2) should contain the skill body in tool results.
|
||||
let reqs = llm.captured.lock().await;
|
||||
assert!(
|
||||
reqs.len() >= 2,
|
||||
"expected at least 2 LLM requests, got {}",
|
||||
reqs.len()
|
||||
);
|
||||
let round2_str = serde_json::to_string(&reqs[1]).unwrap();
|
||||
assert!(
|
||||
round2_str.contains("SKILL_BODY_CONTENT_99"),
|
||||
"load_skill result must contain skill body in round 2 request.\nGot: {round2_str}"
|
||||
);
|
||||
h.shutdown().await;
|
||||
}
|
||||
|
||||
@@ -15,8 +15,6 @@ use tokio::io::{AsyncBufReadExt, AsyncReadExt, AsyncWriteExt, BufReader};
|
||||
use tokio::net::TcpListener;
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
// ─── Fake LLM that captures requests so we can inspect history ──────────────
|
||||
|
||||
struct CapturingLlm {
|
||||
url: String,
|
||||
captured: Arc<Mutex<Vec<Value>>>,
|
||||
@@ -86,8 +84,6 @@ async fn spawn_capturing_llm(responses: Vec<Value>) -> CapturingLlm {
|
||||
CapturingLlm { url, captured }
|
||||
}
|
||||
|
||||
// ─── Harness (minimal copy — keeping per-test independence) ─────────────────
|
||||
|
||||
struct Harness {
|
||||
child: tokio::process::Child,
|
||||
stdin: tokio::process::ChildStdin,
|
||||
@@ -237,8 +233,6 @@ async fn init_session(h: &mut Harness, mcp_servers: Value) -> String {
|
||||
.to_owned()
|
||||
}
|
||||
|
||||
// ─── Tests ──────────────────────────────────────────────────────────────────
|
||||
|
||||
/// After a text-only assistant response, the next prompt's request must
|
||||
/// include that assistant text in `messages` history. Round 4 fix.
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
@@ -524,8 +518,6 @@ fn openai_n_tool_calls(n: usize) -> Value {
|
||||
})
|
||||
}
|
||||
|
||||
// ─── New round-8 regression tests ──────────────────────────────────────────
|
||||
|
||||
/// History budget evicts old turns: after many prompts, the LLM request
|
||||
/// body stays below a sane bound. Round 7 fix; round 8 test.
|
||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||
@@ -697,8 +689,13 @@ async fn description_clamping_enforced() {
|
||||
|
||||
let captured = llm.captured.lock().await;
|
||||
let tools = captured[0]["tools"].as_array().unwrap();
|
||||
assert_eq!(tools.len(), 1);
|
||||
let desc = tools[0]["function"]["description"].as_str().unwrap_or("");
|
||||
// The MCP tool is "big__tool_0"; load_skill may also be present when
|
||||
// global skills are discovered from HOME. Find the MCP tool by name.
|
||||
let mcp_tool = tools
|
||||
.iter()
|
||||
.find(|t| t["function"]["name"].as_str() == Some("big__tool_0"))
|
||||
.expect("big__tool_0 not found in tool list");
|
||||
let desc = mcp_tool["function"]["description"].as_str().unwrap_or("");
|
||||
assert!(
|
||||
desc.len() <= 1024,
|
||||
"description not clamped: {} bytes (expected ≤ 1024)",
|
||||
@@ -713,8 +710,6 @@ async fn description_clamping_enforced() {
|
||||
h.shutdown().await;
|
||||
}
|
||||
|
||||
// ─── Hook system regression tests ──────────────────────────────────────────
|
||||
|
||||
/// Helper: spawn a session with a fake MCP server exposing one regular tool
|
||||
/// plus an optional `_Stop` hook controlled by env vars.
|
||||
async fn init_session_with_fake_mcp(h: &mut Harness, extra_mcp_env: &[(&str, &str)]) -> String {
|
||||
|
||||
@@ -82,8 +82,6 @@ pub async fn check_write_access(
|
||||
}
|
||||
}
|
||||
|
||||
// ── Test-only mock ───────────────────────────────────────────────────────────
|
||||
|
||||
/// In-memory [`ChannelAccessChecker`] for unit tests.
|
||||
#[cfg(any(test, feature = "test-utils"))]
|
||||
pub struct MockAccessChecker {
|
||||
|
||||
@@ -7,10 +7,6 @@ use sha2::{Digest, Sha256};
|
||||
|
||||
use crate::error::CliError;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Blob / Media types
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// Descriptor returned by the relay after a successful upload.
|
||||
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
|
||||
pub struct BlobDescriptor {
|
||||
@@ -78,10 +74,6 @@ const MAX_IMAGE_BYTES: u64 = 50 * 1024 * 1024;
|
||||
/// Maximum file size for video uploads (500 MB).
|
||||
const MAX_VIDEO_BYTES: u64 = 500 * 1024 * 1024;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// NIP-98 HTTP Auth
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// Sign a NIP-98 HTTP auth event (kind:27235) and return the Authorization header value.
|
||||
///
|
||||
/// The event includes:
|
||||
@@ -116,10 +108,6 @@ fn sign_nip98(
|
||||
Ok(format!("Nostr {}", B64.encode(json.as_bytes())))
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// BuzzClient
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
pub struct BuzzClient {
|
||||
http: reqwest::Client,
|
||||
relay_url: String, // base URL, no trailing slash, e.g. "https://relay.buzz.place"
|
||||
@@ -213,10 +201,6 @@ impl BuzzClient {
|
||||
}
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------
|
||||
// HTTP Bridge: POST /query
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
/// Execute a one-shot query via the HTTP bridge.
|
||||
/// `filter` is a Nostr filter object (will be wrapped in an array).
|
||||
/// Returns the raw JSON response (array of events).
|
||||
@@ -261,10 +245,6 @@ impl BuzzClient {
|
||||
self.handle_response(resp).await
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------
|
||||
// HTTP Bridge: POST /events
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
/// Submit a signed Nostr event via POST /events.
|
||||
pub async fn submit_event(&self, event: nostr::Event) -> Result<String, CliError> {
|
||||
let url = format!("{}/events", self.relay_url);
|
||||
@@ -283,10 +263,6 @@ impl BuzzClient {
|
||||
self.handle_response(resp).await
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------
|
||||
// WebSocket publish (ephemeral events)
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
/// Publish an ephemeral event via WebSocket with NIP-42 authentication.
|
||||
///
|
||||
/// The relay rejects ephemeral kinds (20000–29999) over HTTP. Delegates to
|
||||
@@ -313,10 +289,6 @@ impl BuzzClient {
|
||||
.to_string())
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------
|
||||
// File upload (Blossom protocol)
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
/// Upload a file to the relay's Blossom endpoint.
|
||||
/// Returns a BlobDescriptor on success.
|
||||
pub async fn upload_file(&self, file_path: &str) -> Result<BlobDescriptor, CliError> {
|
||||
@@ -423,10 +395,6 @@ impl BuzzClient {
|
||||
.map_err(|e| CliError::Other(format!("invalid upload response: {e}")))
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------
|
||||
// Response handling
|
||||
// -----------------------------------------------------------------------
|
||||
|
||||
async fn handle_response(&self, resp: reqwest::Response) -> Result<String, CliError> {
|
||||
if !resp.status().is_success() {
|
||||
let status = resp.status().as_u16();
|
||||
@@ -458,10 +426,6 @@ impl BuzzClient {
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// URL normalization
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// Normalize a relay URL: ws:// → http://, wss:// → https://, strip trailing slash.
|
||||
/// BUZZ_RELAY_URL may be ws/wss (copied from MCP config).
|
||||
pub fn normalize_relay_url(url: &str) -> String {
|
||||
@@ -478,10 +442,6 @@ fn to_ws_url(http_url: &str) -> String {
|
||||
.replace("http://", "ws://")
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Output normalization helpers
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// Normalize raw event JSON array into consistent shape.
|
||||
/// Each event becomes: {id, pubkey, kind, content, created_at, tags}
|
||||
pub fn normalize_events(events: &[serde_json::Value]) -> String {
|
||||
|
||||
@@ -7,10 +7,6 @@ use crate::client::{
|
||||
use crate::error::CliError;
|
||||
use crate::validate::{parse_uuid, read_or_stdin, validate_hex64, validate_uuid};
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Read commands — POST /query
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
fn extract_channel_metadata(e: &serde_json::Value) -> serde_json::Value {
|
||||
serde_json::json!({
|
||||
"channel_id": extract_d_tag(e),
|
||||
@@ -287,10 +283,6 @@ pub async fn cmd_get_canvas(client: &BuzzClient, channel_id: &str) -> Result<(),
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Write commands — signed events via POST /events
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
pub async fn cmd_create_channel(
|
||||
client: &BuzzClient,
|
||||
name: &str,
|
||||
@@ -535,6 +527,26 @@ pub async fn cmd_set_add_policy(client: &BuzzClient, policy: &str) -> Result<(),
|
||||
}
|
||||
}
|
||||
|
||||
// Check if this policy is allowed by the deployment.
|
||||
// NOTE: This gate covers only the `buzz channels set-add-policy` CLI path.
|
||||
// A client that submits a kind:10100 event directly to the relay bypasses
|
||||
// this check. Full enforcement requires relay-side validation, which is
|
||||
// intentionally out of scope for this change (see team decision: no
|
||||
// relay-side enforcement of client behavior).
|
||||
if let Ok(allowed_raw) = std::env::var("BUZZ_ACP_ALLOWED_CHANNEL_ADD_POLICIES") {
|
||||
let allowed: Vec<&str> = allowed_raw
|
||||
.split(',')
|
||||
.map(str::trim)
|
||||
.filter(|s| !s.is_empty())
|
||||
.collect();
|
||||
if !allowed.is_empty() && !allowed.contains(&policy) {
|
||||
return Err(CliError::Usage(format!(
|
||||
"channel_add_policy '{policy}' is not permitted on this deployment \
|
||||
(BUZZ_ACP_ALLOWED_CHANNEL_ADD_POLICIES={allowed_raw})"
|
||||
)));
|
||||
}
|
||||
}
|
||||
|
||||
let content = serde_json::json!({ "channel_add_policy": policy }).to_string();
|
||||
use nostr::{EventBuilder, Kind};
|
||||
let builder = EventBuilder::new(
|
||||
@@ -566,10 +578,6 @@ pub async fn cmd_set_canvas(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Dispatch
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
pub async fn dispatch(
|
||||
cmd: crate::ChannelsCmd,
|
||||
client: &BuzzClient,
|
||||
@@ -660,7 +668,9 @@ pub async fn dispatch_canvas(cmd: crate::CanvasCmd, client: &BuzzClient) -> Resu
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{name_matches, validate_ttl_seconds, ChannelSummary};
|
||||
use super::{cmd_set_add_policy, name_matches, validate_ttl_seconds, ChannelSummary};
|
||||
use crate::client::BuzzClient;
|
||||
use crate::CliError;
|
||||
use serde_json::json;
|
||||
|
||||
fn event(tags: serde_json::Value) -> serde_json::Value {
|
||||
@@ -768,4 +778,93 @@ mod tests {
|
||||
fn validate_ttl_rejects_overflow() {
|
||||
assert!(validate_ttl_seconds(i32::MAX as i64 + 1).is_err());
|
||||
}
|
||||
|
||||
// --- BUZZ_ACP_ALLOWED_CHANNEL_ADD_POLICIES gate ---
|
||||
|
||||
fn check_allowed_channel_add_policy(allowed_raw: &str, policy: &str) -> Result<(), CliError> {
|
||||
let allowed: Vec<&str> = allowed_raw
|
||||
.split(',')
|
||||
.map(str::trim)
|
||||
.filter(|s| !s.is_empty())
|
||||
.collect();
|
||||
if !allowed.is_empty() && !allowed.contains(&policy) {
|
||||
return Err(CliError::Usage(format!(
|
||||
"channel_add_policy '{policy}' is not permitted on this deployment \
|
||||
(BUZZ_ACP_ALLOWED_CHANNEL_ADD_POLICIES={allowed_raw})"
|
||||
)));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn set_add_policy_rejects_disallowed_policy() {
|
||||
let result = check_allowed_channel_add_policy("owner_only,nobody", "anyone");
|
||||
assert!(
|
||||
result.is_err(),
|
||||
"anyone should be rejected when not in allowed set"
|
||||
);
|
||||
let msg = result.unwrap_err().to_string();
|
||||
assert!(
|
||||
msg.contains("not permitted"),
|
||||
"error should mention 'not permitted': {msg}"
|
||||
);
|
||||
assert!(
|
||||
msg.contains("anyone"),
|
||||
"error should name the disallowed policy: {msg}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn set_add_policy_accepts_allowed_policy() {
|
||||
let result = check_allowed_channel_add_policy("owner_only,nobody", "owner_only");
|
||||
assert!(result.is_ok(), "owner_only should be accepted: {result:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn set_add_policy_no_restriction_allows_all() {
|
||||
// Empty allowed list means no restriction.
|
||||
let result = check_allowed_channel_add_policy("", "anyone");
|
||||
assert!(
|
||||
result.is_ok(),
|
||||
"empty allowed list should permit any policy: {result:?}"
|
||||
);
|
||||
}
|
||||
|
||||
// --- Integration test: full env-var → cmd_set_add_policy() path ---
|
||||
//
|
||||
// This test calls cmd_set_add_policy directly with the env var set. The function
|
||||
// returns early with an error before any network call, so no relay is needed.
|
||||
// If the BUZZ_ACP_ALLOWED_CHANNEL_ADD_POLICIES check were removed from cmd_set_add_policy,
|
||||
// this test would fail (it would proceed to sign_event and return a different error).
|
||||
|
||||
fn make_test_client() -> BuzzClient {
|
||||
// Scalar = 1 is the smallest valid secp256k1 private key.
|
||||
let keys =
|
||||
nostr::Keys::parse("0000000000000000000000000000000000000000000000000000000000000001")
|
||||
.expect("valid test key");
|
||||
BuzzClient::new("ws://localhost:3000".to_string(), keys, None, None)
|
||||
.expect("client construction should not fail")
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn set_add_policy_env_gate_rejects_disallowed_via_full_path() {
|
||||
std::env::set_var("BUZZ_ACP_ALLOWED_CHANNEL_ADD_POLICIES", "owner_only,nobody");
|
||||
let client = make_test_client();
|
||||
let result = cmd_set_add_policy(&client, "anyone").await;
|
||||
std::env::remove_var("BUZZ_ACP_ALLOWED_CHANNEL_ADD_POLICIES");
|
||||
|
||||
assert!(
|
||||
result.is_err(),
|
||||
"cmd_set_add_policy should reject 'anyone' when not in allowed set"
|
||||
);
|
||||
match result.unwrap_err() {
|
||||
crate::CliError::Usage(msg) => {
|
||||
assert!(
|
||||
msg.contains("not permitted"),
|
||||
"error should mention 'not permitted': {msg}"
|
||||
);
|
||||
}
|
||||
other => panic!("expected CliError::Usage, got {other:?}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -125,10 +125,6 @@ pub async fn cmd_add_dm_member(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Dispatch
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
pub async fn dispatch(cmd: crate::DmsCmd, client: &BuzzClient) -> Result<(), CliError> {
|
||||
use crate::DmsCmd;
|
||||
match cmd {
|
||||
|
||||
@@ -308,10 +308,6 @@ async fn cmd_import(
|
||||
publish_own_set(client, &final_set).await
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Dispatch
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
pub async fn dispatch(cmd: crate::EmojiCmd, client: &BuzzClient) -> Result<(), CliError> {
|
||||
use crate::EmojiCmd;
|
||||
match cmd {
|
||||
|
||||
@@ -64,10 +64,6 @@ pub async fn cmd_get_feed(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Dispatch
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
pub async fn dispatch(
|
||||
cmd: crate::FeedCmd,
|
||||
client: &BuzzClient,
|
||||
|
||||
@@ -3,10 +3,6 @@ use crate::error::CliError;
|
||||
use crate::validate::{read_or_stdin, sdk_err, validate_hex64, validate_repo_id};
|
||||
use buzz_sdk::{GitIssueMeta, GitRepoCoord, GitStatusMeta};
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Create issue — publish kind:1621
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
pub async fn cmd_create_issue(
|
||||
client: &BuzzClient,
|
||||
repo_owner: &str,
|
||||
@@ -37,10 +33,6 @@ pub async fn cmd_create_issue(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Get issue — query kind:1621 by event id
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
pub async fn cmd_get_issue(client: &BuzzClient, event: &str) -> Result<(), CliError> {
|
||||
validate_hex64(event)?;
|
||||
let filter = serde_json::json!({
|
||||
@@ -52,10 +44,6 @@ pub async fn cmd_get_issue(client: &BuzzClient, event: &str) -> Result<(), CliEr
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// List issues — query kind:1621 by repo coordinate, with optional filters
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
pub async fn cmd_list_issues(
|
||||
client: &BuzzClient,
|
||||
repo_owner: &str,
|
||||
@@ -89,10 +77,6 @@ pub async fn cmd_list_issues(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Status — publish kind:1630/1631/1632/1633 against an issue
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub async fn cmd_issue_status(
|
||||
client: &BuzzClient,
|
||||
@@ -160,10 +144,6 @@ pub async fn cmd_issue_status(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Dispatch
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
pub async fn dispatch(cmd: crate::IssuesCmd, client: &BuzzClient) -> Result<(), CliError> {
|
||||
use crate::IssuesCmd;
|
||||
match cmd {
|
||||
|
||||
@@ -734,10 +734,6 @@ pub async fn cmd_rm(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Dispatch
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
pub async fn dispatch(cmd: crate::MemCmd, client: &BuzzClient) -> Result<(), CliError> {
|
||||
use crate::MemCmd;
|
||||
match cmd {
|
||||
|
||||
@@ -13,10 +13,6 @@ use buzz_sdk::mentions::{
|
||||
MENTION_CAP,
|
||||
};
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Helpers
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// Extract the thread root event ID from a Nostr tag array.
|
||||
///
|
||||
/// Parses `"e"` tags with NIP-10 markers:
|
||||
@@ -243,10 +239,6 @@ fn parse_member_pubkeys(event: &serde_json::Value) -> Vec<String> {
|
||||
.collect()
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Read commands — POST /query
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
fn format_events(normalized: &str, format: &crate::OutputFormat) -> String {
|
||||
match format {
|
||||
crate::OutputFormat::Compact => {
|
||||
@@ -364,10 +356,6 @@ pub async fn cmd_search(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Write commands — signed events via POST /events
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
pub struct SendMessageParams {
|
||||
pub channel_id: String,
|
||||
pub content: String,
|
||||
@@ -634,10 +622,6 @@ pub async fn cmd_vote_on_post(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Dispatch
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
pub async fn dispatch(
|
||||
cmd: crate::MessagesCmd,
|
||||
client: &BuzzClient,
|
||||
@@ -816,7 +800,6 @@ mod tests {
|
||||
assert!(find_root_from_tags(&json!(null)).is_none());
|
||||
}
|
||||
|
||||
// ── @mention resolution pipeline ────────────────────────────────────
|
||||
//
|
||||
// These tests don't hit the network — they prove that *given* the
|
||||
// events the relay returns, the CLI's parse + match wiring produces
|
||||
|
||||
@@ -8,6 +8,7 @@ pub mod messages;
|
||||
pub mod notes;
|
||||
pub mod pack;
|
||||
pub mod patches;
|
||||
pub mod pr;
|
||||
pub mod reactions;
|
||||
pub mod repos;
|
||||
pub mod social;
|
||||
|
||||
@@ -41,10 +41,6 @@ pub const KIND_LONG_FORM: u16 = 30023;
|
||||
/// comfortably URL/filename-safe and matches `mem` slug ergonomics.
|
||||
pub const SLUG_MAX_LEN: usize = 80;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Slug validation
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// Validate and normalize a slug for use as a NIP-23 `d` tag.
|
||||
///
|
||||
/// Rules: 1..=80 chars, `[a-z0-9._-]` only. Lowercase ascii keeps memory
|
||||
@@ -72,10 +68,6 @@ pub fn parse_slug(raw: &str) -> Result<String, CliError> {
|
||||
Ok(raw.to_string())
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// NoteSnapshot — parsed view of a kind:30023 event, derived once
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// Parsed view of a NIP-23 long-form event. Built once via
|
||||
/// [`NoteSnapshot::from_event`] so the tag-parsing footgun lives in exactly
|
||||
/// one place; `set` (carry-forward), `get`/`ls` (output shaping), and the
|
||||
@@ -161,10 +153,6 @@ impl NoteSnapshot {
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Query helpers
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
fn parse_events(json: &str) -> Result<Vec<Event>, CliError> {
|
||||
serde_json::from_str::<Vec<Event>>(json)
|
||||
.map_err(|e| CliError::Other(format!("failed to parse relay response: {e}")))
|
||||
@@ -206,10 +194,6 @@ pub async fn fetch_by_slug(client: &BuzzClient, slug: &str) -> Result<Vec<Event>
|
||||
parse_events(&raw)
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Author resolution
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// Resolve an `--author` flag value to a `PublicKey`.
|
||||
///
|
||||
/// Accepts:
|
||||
@@ -259,10 +243,6 @@ pub async fn resolve_author(client: &BuzzClient, author_flag: &str) -> Result<Pu
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Coordinate parsing (naddr / kind:pk:d / NIP-21)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// Parse a `--naddr` flag. Accepts:
|
||||
/// - bech32 `naddr1…`
|
||||
/// - `<kind>:<pubkey-hex>:<d-tag>` (KPI format)
|
||||
@@ -293,10 +273,6 @@ pub fn coord_for(author: &PublicKey, slug: &str) -> nostr::nips::nip01::Coordina
|
||||
.identifier(slug.to_string())
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Candidate formatting (used when --name resolves to >1 author)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// Format a list of candidate notes for the "ambiguous slug" error path.
|
||||
/// One line per candidate; sorted newest-first. Designed so the user can
|
||||
/// paste a pubkey into a follow-up `--author <hex>` invocation.
|
||||
@@ -320,10 +296,6 @@ pub fn format_note_candidates(snapshots: &[NoteSnapshot]) -> String {
|
||||
out
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Output helpers for `get` / `ls`
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
#[derive(Debug, serde::Serialize)]
|
||||
struct NoteOutput {
|
||||
id: String,
|
||||
@@ -417,10 +389,6 @@ fn sort_snapshots_newest_first(snapshots: &mut [NoteSnapshot]) {
|
||||
snapshots.sort_by_key(|s| std::cmp::Reverse(s.updated_at));
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Event builder for `set` — pure, unit-testable carry-forward logic
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// Build the unsigned `EventBuilder` for `notes set`. Pure function — no I/O,
|
||||
/// no clock — so every carry/clear/first-publish case is unit-testable.
|
||||
///
|
||||
@@ -516,10 +484,6 @@ fn now_secs() -> u64 {
|
||||
/// far above any realistic skill-KB note.
|
||||
pub const SET_STDIN_MAX_BYTES: usize = 1024 * 1024;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Dispatch — stubs for verb implementations (filled by follow-up commits).
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
pub async fn cmd_set(
|
||||
client: &BuzzClient,
|
||||
slug: &str,
|
||||
|
||||
@@ -5,10 +5,6 @@ use crate::validate::{
|
||||
};
|
||||
use buzz_sdk::{GitAppliedPatchRef, GitPatchMeta, GitRepoCoord, GitStatus, GitStatusMeta};
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Send patch — publish kind:1617
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub async fn cmd_send_patch(
|
||||
client: &BuzzClient,
|
||||
@@ -74,10 +70,6 @@ fn parse_committer(spec: &str) -> Result<(String, String, String, String), CliEr
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Get patch — query kind:1617 by event id
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
pub async fn cmd_get_patch(client: &BuzzClient, event: &str) -> Result<(), CliError> {
|
||||
validate_hex64(event)?;
|
||||
let filter = serde_json::json!({
|
||||
@@ -89,10 +81,6 @@ pub async fn cmd_get_patch(client: &BuzzClient, event: &str) -> Result<(), CliEr
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// List patches — query kind:1617 by repo coordinate, with optional filters
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
pub async fn cmd_list_patches(
|
||||
client: &BuzzClient,
|
||||
repo_owner: &str,
|
||||
@@ -122,10 +110,6 @@ pub async fn cmd_list_patches(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Status — publish kind:1630/1631/1632/1633 against a patch root
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub async fn cmd_patch_status(
|
||||
client: &BuzzClient,
|
||||
@@ -219,10 +203,6 @@ pub(crate) fn parse_status(s: &str) -> Result<GitStatus, CliError> {
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Dispatch
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
pub async fn dispatch(cmd: crate::PatchesCmd, client: &BuzzClient) -> Result<(), CliError> {
|
||||
use crate::PatchesCmd;
|
||||
match cmd {
|
||||
|
||||
@@ -0,0 +1,338 @@
|
||||
use crate::client::BuzzClient;
|
||||
use crate::error::CliError;
|
||||
use crate::validate::{
|
||||
read_file_or_stdin, read_or_stdin, sdk_err, validate_hex64, validate_repo_id,
|
||||
};
|
||||
use buzz_sdk::{GitPrUpdateMeta, GitPullRequestMeta, GitRepoCoord, GitStatusMeta};
|
||||
|
||||
fn read_optional_body(body: Option<&str>, body_file: Option<&str>) -> Result<String, CliError> {
|
||||
match (body, body_file) {
|
||||
(Some(_), Some(_)) => Err(CliError::Usage(
|
||||
"--body and --body-file are mutually exclusive".into(),
|
||||
)),
|
||||
(Some(value), None) => read_or_stdin(value),
|
||||
(None, Some(path)) => read_file_or_stdin(path),
|
||||
(None, None) => Ok(String::new()),
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub async fn cmd_open_pr(
|
||||
client: &BuzzClient,
|
||||
repo_owner: &str,
|
||||
repo_id: &str,
|
||||
subject: &str,
|
||||
body: Option<&str>,
|
||||
body_file: Option<&str>,
|
||||
commit: &str,
|
||||
clone_urls: &[String],
|
||||
branch_name: Option<&str>,
|
||||
merge_base: Option<&str>,
|
||||
euc: Option<&str>,
|
||||
labels: &[String],
|
||||
to: &[String],
|
||||
revision_of: Option<&str>,
|
||||
) -> Result<(), CliError> {
|
||||
validate_hex64(repo_owner)?;
|
||||
validate_repo_id(repo_id)?;
|
||||
let content = read_optional_body(body, body_file)?;
|
||||
|
||||
let repo = GitRepoCoord {
|
||||
owner: repo_owner.to_string(),
|
||||
id: repo_id.to_string(),
|
||||
};
|
||||
let meta = GitPullRequestMeta {
|
||||
euc: euc.map(str::to_string),
|
||||
recipients: to.to_vec(),
|
||||
subject: subject.to_string(),
|
||||
labels: labels.to_vec(),
|
||||
commit: commit.to_string(),
|
||||
clone_urls: clone_urls.to_vec(),
|
||||
branch_name: branch_name.map(str::to_string),
|
||||
merge_base: merge_base.map(str::to_string),
|
||||
revision_of: revision_of.map(str::to_string),
|
||||
};
|
||||
|
||||
let builder = buzz_sdk::build_git_pull_request(&repo, &content, &meta).map_err(sdk_err)?;
|
||||
let event = client.sign_event(builder)?;
|
||||
let resp = client.submit_event(event).await?;
|
||||
println!("{resp}");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub async fn cmd_update_pr(
|
||||
client: &BuzzClient,
|
||||
repo_owner: &str,
|
||||
repo_id: &str,
|
||||
pr: &str,
|
||||
pr_author: &str,
|
||||
commit: &str,
|
||||
clone_urls: &[String],
|
||||
body: Option<&str>,
|
||||
body_file: Option<&str>,
|
||||
merge_base: Option<&str>,
|
||||
euc: Option<&str>,
|
||||
to: &[String],
|
||||
) -> Result<(), CliError> {
|
||||
validate_hex64(repo_owner)?;
|
||||
validate_repo_id(repo_id)?;
|
||||
validate_hex64(pr)?;
|
||||
validate_hex64(pr_author)?;
|
||||
let content = read_optional_body(body, body_file)?;
|
||||
|
||||
let repo = GitRepoCoord {
|
||||
owner: repo_owner.to_string(),
|
||||
id: repo_id.to_string(),
|
||||
};
|
||||
let meta = GitPrUpdateMeta {
|
||||
euc: euc.map(str::to_string),
|
||||
recipients: to.to_vec(),
|
||||
pr_event: pr.to_string(),
|
||||
pr_author: pr_author.to_string(),
|
||||
commit: commit.to_string(),
|
||||
clone_urls: clone_urls.to_vec(),
|
||||
merge_base: merge_base.map(str::to_string),
|
||||
};
|
||||
|
||||
let builder = buzz_sdk::build_git_pr_update(&repo, &content, &meta).map_err(sdk_err)?;
|
||||
let event = client.sign_event(builder)?;
|
||||
let resp = client.submit_event(event).await?;
|
||||
println!("{resp}");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn cmd_get_pr(client: &BuzzClient, event: &str) -> Result<(), CliError> {
|
||||
validate_hex64(event)?;
|
||||
let filter = serde_json::json!({
|
||||
"kinds": [1618],
|
||||
"ids": [event]
|
||||
});
|
||||
let resp = client.query(&filter).await?;
|
||||
println!("{resp}");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn cmd_list_prs(
|
||||
client: &BuzzClient,
|
||||
repo_owner: &str,
|
||||
repo_id: &str,
|
||||
author: Option<&str>,
|
||||
label: Option<&str>,
|
||||
limit: Option<u32>,
|
||||
) -> Result<(), CliError> {
|
||||
validate_hex64(repo_owner)?;
|
||||
validate_repo_id(repo_id)?;
|
||||
|
||||
let a_value = format!("30617:{repo_owner}:{repo_id}");
|
||||
let mut filter = serde_json::json!({
|
||||
"kinds": [1618],
|
||||
"#a": [a_value]
|
||||
});
|
||||
|
||||
if let Some(pk) = author {
|
||||
validate_hex64(pk)?;
|
||||
filter["authors"] = serde_json::json!([pk]);
|
||||
}
|
||||
if let Some(l) = label {
|
||||
filter["#t"] = serde_json::json!([l]);
|
||||
}
|
||||
if let Some(n) = limit {
|
||||
filter["limit"] = serde_json::json!(n);
|
||||
}
|
||||
|
||||
let resp = client.query(&filter).await?;
|
||||
println!("{resp}");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub async fn cmd_pr_status(
|
||||
client: &BuzzClient,
|
||||
pr: &str,
|
||||
status: &str,
|
||||
body: Option<&str>,
|
||||
body_file: Option<&str>,
|
||||
repo_owner: Option<&str>,
|
||||
repo_id: Option<&str>,
|
||||
euc: Option<&str>,
|
||||
to: &[String],
|
||||
merge_commit: Option<&str>,
|
||||
) -> Result<(), CliError> {
|
||||
validate_hex64(pr)?;
|
||||
let status = crate::commands::patches::parse_status(status)?;
|
||||
let content = read_optional_body(body, body_file)?;
|
||||
|
||||
let repo = match (repo_owner, repo_id) {
|
||||
(Some(owner), Some(id)) => {
|
||||
validate_hex64(owner)?;
|
||||
validate_repo_id(id)?;
|
||||
Some(GitRepoCoord {
|
||||
owner: owner.to_string(),
|
||||
id: id.to_string(),
|
||||
})
|
||||
}
|
||||
(None, None) => None,
|
||||
_ => {
|
||||
return Err(CliError::Usage(
|
||||
"--repo-owner and --repo-id must be given together".into(),
|
||||
))
|
||||
}
|
||||
};
|
||||
|
||||
// Mirrors patch/issue status: default a `p` tag to the repo owner when
|
||||
// known; callers can add PR author/reviewers with repeated `--to`.
|
||||
let mut recipients = Vec::new();
|
||||
if let Some(ref repo) = repo {
|
||||
recipients.push(repo.owner.clone());
|
||||
}
|
||||
for recipient in to {
|
||||
validate_hex64(recipient)?;
|
||||
if !recipients.contains(recipient) {
|
||||
recipients.push(recipient.clone());
|
||||
}
|
||||
}
|
||||
|
||||
let meta = GitStatusMeta {
|
||||
root_event: pr.to_string(),
|
||||
accepted_revision_root: None,
|
||||
repo,
|
||||
euc: euc.map(str::to_string),
|
||||
recipients,
|
||||
applied_patches: vec![],
|
||||
merge_commit: merge_commit.map(str::to_string),
|
||||
applied_as_commits: vec![],
|
||||
};
|
||||
|
||||
let builder = buzz_sdk::build_git_status(status, &content, &meta).map_err(sdk_err)?;
|
||||
let event = client.sign_event(builder)?;
|
||||
let resp = client.submit_event(event).await?;
|
||||
println!("{resp}");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn dispatch(cmd: crate::PrCmd, client: &BuzzClient) -> Result<(), CliError> {
|
||||
use crate::PrCmd;
|
||||
match cmd {
|
||||
PrCmd::Open {
|
||||
repo_owner,
|
||||
repo_id,
|
||||
subject,
|
||||
body,
|
||||
body_file,
|
||||
commit,
|
||||
clone,
|
||||
branch_name,
|
||||
merge_base,
|
||||
euc,
|
||||
label,
|
||||
to,
|
||||
revision_of,
|
||||
} => {
|
||||
cmd_open_pr(
|
||||
client,
|
||||
&repo_owner,
|
||||
&repo_id,
|
||||
&subject,
|
||||
body.as_deref(),
|
||||
body_file.as_deref(),
|
||||
&commit,
|
||||
&clone,
|
||||
branch_name.as_deref(),
|
||||
merge_base.as_deref(),
|
||||
euc.as_deref(),
|
||||
&label,
|
||||
&to,
|
||||
revision_of.as_deref(),
|
||||
)
|
||||
.await
|
||||
}
|
||||
PrCmd::Update {
|
||||
repo_owner,
|
||||
repo_id,
|
||||
pr,
|
||||
pr_author,
|
||||
commit,
|
||||
clone,
|
||||
body,
|
||||
body_file,
|
||||
merge_base,
|
||||
euc,
|
||||
to,
|
||||
} => {
|
||||
cmd_update_pr(
|
||||
client,
|
||||
&repo_owner,
|
||||
&repo_id,
|
||||
&pr,
|
||||
&pr_author,
|
||||
&commit,
|
||||
&clone,
|
||||
body.as_deref(),
|
||||
body_file.as_deref(),
|
||||
merge_base.as_deref(),
|
||||
euc.as_deref(),
|
||||
&to,
|
||||
)
|
||||
.await
|
||||
}
|
||||
PrCmd::Get { event } => cmd_get_pr(client, &event).await,
|
||||
PrCmd::List {
|
||||
repo_owner,
|
||||
repo_id,
|
||||
author,
|
||||
label,
|
||||
limit,
|
||||
} => {
|
||||
cmd_list_prs(
|
||||
client,
|
||||
&repo_owner,
|
||||
&repo_id,
|
||||
author.as_deref(),
|
||||
label.as_deref(),
|
||||
limit,
|
||||
)
|
||||
.await
|
||||
}
|
||||
PrCmd::Status {
|
||||
pr,
|
||||
status,
|
||||
body,
|
||||
body_file,
|
||||
repo_owner,
|
||||
repo_id,
|
||||
euc,
|
||||
to,
|
||||
merge_commit,
|
||||
} => {
|
||||
cmd_pr_status(
|
||||
client,
|
||||
&pr,
|
||||
&status,
|
||||
body.as_deref(),
|
||||
body_file.as_deref(),
|
||||
repo_owner.as_deref(),
|
||||
repo_id.as_deref(),
|
||||
euc.as_deref(),
|
||||
&to,
|
||||
merge_commit.as_deref(),
|
||||
)
|
||||
.await
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn read_optional_body_rejects_body_and_body_file_together() {
|
||||
assert!(read_optional_body(Some("body"), Some("file.md")).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn read_optional_body_defaults_empty() {
|
||||
assert_eq!(read_optional_body(None, None).unwrap(), "");
|
||||
}
|
||||
}
|
||||
@@ -124,10 +124,6 @@ pub async fn cmd_get_reactions(client: &BuzzClient, event_id: &str) -> Result<()
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Dispatch
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
pub async fn dispatch(cmd: crate::ReactionsCmd, client: &BuzzClient) -> Result<(), CliError> {
|
||||
use crate::ReactionsCmd;
|
||||
match cmd {
|
||||
|
||||
@@ -2,10 +2,6 @@ use crate::client::BuzzClient;
|
||||
use crate::error::CliError;
|
||||
use crate::validate::validate_repo_id;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Create repo — publish kind:30617
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
pub async fn cmd_create_repo(
|
||||
client: &BuzzClient,
|
||||
repo_id: &str,
|
||||
@@ -36,10 +32,6 @@ pub async fn cmd_create_repo(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Get repo — query kind:30617 by owner + d-tag
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
pub async fn cmd_get_repo(
|
||||
client: &BuzzClient,
|
||||
repo_id: &str,
|
||||
@@ -64,10 +56,6 @@ pub async fn cmd_get_repo(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// List repos — query kind:30617 by author
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
pub async fn cmd_list_repos(
|
||||
client: &BuzzClient,
|
||||
owner: Option<&str>,
|
||||
@@ -96,10 +84,6 @@ pub async fn cmd_list_repos(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Dispatch
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
pub async fn dispatch(cmd: crate::ReposCmd, client: &BuzzClient) -> Result<(), CliError> {
|
||||
use crate::ReposCmd;
|
||||
match cmd {
|
||||
|
||||
@@ -208,10 +208,6 @@ pub async fn cmd_get_list(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Dispatch
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
pub async fn dispatch(cmd: crate::SocialCmd, client: &BuzzClient) -> Result<(), CliError> {
|
||||
use crate::SocialCmd;
|
||||
match cmd {
|
||||
|
||||
@@ -290,10 +290,6 @@ pub async fn cmd_set_presence(client: &BuzzClient, status: &str) -> Result<(), C
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Dispatch
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
pub async fn dispatch(
|
||||
cmd: crate::UsersCmd,
|
||||
client: &BuzzClient,
|
||||
|
||||
@@ -9,10 +9,6 @@ use crate::validate::{parse_uuid, read_or_stdin, sdk_err, validate_uuid};
|
||||
|
||||
// TODO(phase-4): Replace raw nostr::EventBuilder usage with buzz-sdk builder functions
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Read commands — POST /query
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// List workflows in a channel — query kind:30620 workflow definition events.
|
||||
pub async fn cmd_list_workflows(client: &BuzzClient, channel_id: &str) -> Result<(), CliError> {
|
||||
validate_uuid(channel_id)?;
|
||||
@@ -98,10 +94,6 @@ pub async fn cmd_get_workflow_runs(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Write commands — signed events via POST /events
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// Create a workflow — sign and submit a kind:30620 event.
|
||||
pub async fn cmd_create_workflow(
|
||||
client: &BuzzClient,
|
||||
@@ -219,10 +211,6 @@ pub async fn cmd_approve_step(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Dispatch
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
pub async fn dispatch(cmd: crate::WorkflowsCmd, client: &BuzzClient) -> Result<(), CliError> {
|
||||
use crate::WorkflowsCmd;
|
||||
match cmd {
|
||||
|
||||
+150
-87
@@ -45,10 +45,6 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Top-level CLI
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(
|
||||
name = "buzz",
|
||||
@@ -87,10 +83,6 @@ struct Cli {
|
||||
command: Cmd,
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Value enums for typed --type / --visibility / --status flags
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
#[derive(Clone, clap::ValueEnum)]
|
||||
pub enum ChannelType {
|
||||
#[value(name = "stream")]
|
||||
@@ -165,10 +157,6 @@ pub enum OutputFormat {
|
||||
Compact,
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Subcommand groups
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
#[derive(Subcommand)]
|
||||
enum Cmd {
|
||||
/// Send, read, search, and manage messages
|
||||
@@ -213,6 +201,9 @@ enum Cmd {
|
||||
/// Create, get, list, and set status on git issues (NIP-34)
|
||||
#[command(subcommand)]
|
||||
Issues(IssuesCmd),
|
||||
/// Open, update, list, and set status on git pull requests (NIP-34)
|
||||
#[command(subcommand)]
|
||||
Pr(PrCmd),
|
||||
/// Upload files to the relay's Blossom store
|
||||
#[command(subcommand)]
|
||||
Upload(UploadCmd),
|
||||
@@ -224,10 +215,6 @@ enum Cmd {
|
||||
Pack(PackCmd),
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Messages subcommands
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
#[derive(Subcommand)]
|
||||
pub enum MessagesCmd {
|
||||
/// Send a message to a channel
|
||||
@@ -364,10 +351,6 @@ pub enum MessagesCmd {
|
||||
},
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Channels subcommands
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
#[derive(Subcommand)]
|
||||
pub enum ChannelsCmd {
|
||||
/// List channels visible to the current identity
|
||||
@@ -536,10 +519,6 @@ pub enum ChannelsCmd {
|
||||
},
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Canvas subcommands
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
#[derive(Subcommand)]
|
||||
pub enum CanvasCmd {
|
||||
/// Get the canvas document for a channel
|
||||
@@ -559,10 +538,6 @@ pub enum CanvasCmd {
|
||||
},
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Reactions subcommands
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
#[derive(Subcommand)]
|
||||
pub enum ReactionsCmd {
|
||||
/// Add an emoji reaction to a message
|
||||
@@ -594,10 +569,6 @@ pub enum ReactionsCmd {
|
||||
},
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Custom emoji subcommands
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
#[derive(Subcommand)]
|
||||
pub enum EmojiCmd {
|
||||
/// List the workspace custom emoji palette (union of every member's set)
|
||||
@@ -640,10 +611,6 @@ pub enum EmojiCmd {
|
||||
},
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// DMs subcommands
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
#[derive(Subcommand)]
|
||||
pub enum DmsCmd {
|
||||
/// List direct message conversations
|
||||
@@ -675,10 +642,6 @@ pub enum DmsCmd {
|
||||
},
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Users subcommands
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
#[derive(Subcommand)]
|
||||
pub enum UsersCmd {
|
||||
/// Look up user profiles by pubkey or name
|
||||
@@ -721,10 +684,6 @@ pub enum UsersCmd {
|
||||
},
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Workflows subcommands
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
#[derive(Subcommand)]
|
||||
pub enum WorkflowsCmd {
|
||||
/// List workflows in a channel
|
||||
@@ -804,10 +763,6 @@ pub enum WorkflowsCmd {
|
||||
},
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Feed subcommands
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
#[derive(Subcommand)]
|
||||
pub enum FeedCmd {
|
||||
/// Get recent activity feed entries
|
||||
@@ -824,10 +779,6 @@ pub enum FeedCmd {
|
||||
},
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Social subcommands
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
#[derive(Subcommand)]
|
||||
pub enum SocialCmd {
|
||||
/// Publish a text note (NIP-01 kind:1)
|
||||
@@ -905,10 +856,6 @@ pub enum SocialCmd {
|
||||
},
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Notes subcommands (NIP-23 long-form)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
#[derive(Subcommand)]
|
||||
pub enum NotesCmd {
|
||||
/// Create or update a note. Idempotent upsert keyed by `(me, --name)`.
|
||||
@@ -988,10 +935,6 @@ pub enum NotesCmd {
|
||||
},
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Repos subcommands
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
#[derive(Subcommand)]
|
||||
pub enum ReposCmd {
|
||||
/// Announce a git repository (NIP-34)
|
||||
@@ -1035,10 +978,6 @@ pub enum ReposCmd {
|
||||
},
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Patches subcommands
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
#[derive(Subcommand)]
|
||||
pub enum PatchesCmd {
|
||||
/// Send a git patch (NIP-34 kind:1617)
|
||||
@@ -1145,9 +1084,146 @@ pub enum PatchesCmd {
|
||||
},
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Issues subcommands
|
||||
// ---------------------------------------------------------------------------
|
||||
#[derive(Subcommand)]
|
||||
pub enum PrCmd {
|
||||
/// Open a git pull request (NIP-34 kind:1618)
|
||||
#[command(
|
||||
after_help = "Examples:\n buzz pr open --repo-owner <hex> --repo-id myrepo --subject 'Fix bug' --body-file - --commit $(git rev-parse HEAD) --clone https://relay/git/owner/myrepo --branch-name fix-bug\n buzz pr update --repo-owner <hex> --repo-id myrepo --pr <event> --pr-author <hex> --commit $(git rev-parse HEAD) --clone https://relay/git/owner/myrepo"
|
||||
)]
|
||||
Open {
|
||||
/// Repo owner pubkey (64-char hex)
|
||||
#[arg(long)]
|
||||
repo_owner: String,
|
||||
/// Repo identifier (d-tag)
|
||||
#[arg(long)]
|
||||
repo_id: String,
|
||||
/// Pull request subject/header
|
||||
#[arg(long, alias = "title")]
|
||||
subject: String,
|
||||
/// Pull request body markdown. Use '-' to read from stdin.
|
||||
#[arg(long, conflicts_with = "body_file")]
|
||||
body: Option<String>,
|
||||
/// Path to pull request body markdown, or '-' to read from stdin.
|
||||
#[arg(long, conflicts_with = "body")]
|
||||
body_file: Option<String>,
|
||||
/// Tip commit of the PR branch
|
||||
#[arg(long)]
|
||||
commit: String,
|
||||
/// Clone URL where the tip commit can be fetched — can be specified multiple times
|
||||
#[arg(long = "clone", required = true)]
|
||||
clone: Vec<String>,
|
||||
/// Recommended branch name
|
||||
#[arg(long)]
|
||||
branch_name: Option<String>,
|
||||
/// Most recent common ancestor with the target branch
|
||||
#[arg(long)]
|
||||
merge_base: Option<String>,
|
||||
/// Earliest-unique-commit of the repo
|
||||
#[arg(long)]
|
||||
euc: Option<String>,
|
||||
/// Label — can be specified multiple times
|
||||
#[arg(long = "label")]
|
||||
label: Vec<String>,
|
||||
/// Additional recipient pubkey(s) — can be specified multiple times
|
||||
#[arg(long = "to")]
|
||||
to: Vec<String>,
|
||||
/// Root patch event id this PR revises
|
||||
#[arg(long)]
|
||||
revision_of: Option<String>,
|
||||
},
|
||||
/// Update a git pull request tip (NIP-34 kind:1619)
|
||||
Update {
|
||||
/// Repo owner pubkey (64-char hex)
|
||||
#[arg(long)]
|
||||
repo_owner: String,
|
||||
/// Repo identifier (d-tag)
|
||||
#[arg(long)]
|
||||
repo_id: String,
|
||||
/// Pull request event id being updated
|
||||
#[arg(long)]
|
||||
pr: String,
|
||||
/// Pull request author's pubkey
|
||||
#[arg(long)]
|
||||
pr_author: String,
|
||||
/// Updated tip commit of the PR branch
|
||||
#[arg(long)]
|
||||
commit: String,
|
||||
/// Clone URL where the updated tip commit can be fetched — can be specified multiple times
|
||||
#[arg(long = "clone", required = true)]
|
||||
clone: Vec<String>,
|
||||
/// Markdown context for the update. Use '-' to read from stdin.
|
||||
#[arg(long, conflicts_with = "body_file")]
|
||||
body: Option<String>,
|
||||
/// Path to markdown context for the update, or '-' to read from stdin.
|
||||
#[arg(long, conflicts_with = "body")]
|
||||
body_file: Option<String>,
|
||||
/// Most recent common ancestor with the target branch
|
||||
#[arg(long)]
|
||||
merge_base: Option<String>,
|
||||
/// Earliest-unique-commit of the repo
|
||||
#[arg(long)]
|
||||
euc: Option<String>,
|
||||
/// Additional recipient pubkey(s) — can be specified multiple times
|
||||
#[arg(long = "to")]
|
||||
to: Vec<String>,
|
||||
},
|
||||
/// Get a PR by event id
|
||||
Get {
|
||||
/// PR event id (64-char hex)
|
||||
#[arg(long)]
|
||||
event: String,
|
||||
},
|
||||
/// List PRs for a repo
|
||||
List {
|
||||
/// Repo owner pubkey (64-char hex)
|
||||
#[arg(long)]
|
||||
repo_owner: String,
|
||||
/// Repo identifier (d-tag)
|
||||
#[arg(long)]
|
||||
repo_id: String,
|
||||
/// Filter by PR author pubkey
|
||||
#[arg(long)]
|
||||
author: Option<String>,
|
||||
/// Filter by label
|
||||
#[arg(long)]
|
||||
label: Option<String>,
|
||||
/// Maximum number of results
|
||||
#[arg(long)]
|
||||
limit: Option<u32>,
|
||||
},
|
||||
/// Set status on a PR (open/merged/closed/draft — NIP-34 kind:1630-1633)
|
||||
Status {
|
||||
/// Pull request event id
|
||||
#[arg(long)]
|
||||
pr: String,
|
||||
/// New status
|
||||
#[arg(long, value_parser = ["open", "merged", "closed", "draft"])]
|
||||
status: String,
|
||||
/// Markdown context for the status change. Use '-' to read from stdin.
|
||||
#[arg(long, conflicts_with = "body_file")]
|
||||
body: Option<String>,
|
||||
/// Path to markdown context for the status change, or '-' to read from stdin.
|
||||
#[arg(long, conflicts_with = "body")]
|
||||
body_file: Option<String>,
|
||||
/// Repo owner pubkey — requires --repo-id
|
||||
#[arg(long, requires = "repo_id")]
|
||||
repo_owner: Option<String>,
|
||||
/// Repo identifier (d-tag) — requires --repo-owner
|
||||
#[arg(long, requires = "repo_owner")]
|
||||
repo_id: Option<String>,
|
||||
/// Earliest-unique-commit of the repo
|
||||
#[arg(long)]
|
||||
euc: Option<String>,
|
||||
/// Additional recipient pubkey(s) for the status event (besides the
|
||||
/// repo owner, which is tagged automatically when --repo-owner is
|
||||
/// given) — e.g. PR author/reviewers. Can be specified multiple times.
|
||||
#[arg(long = "to")]
|
||||
to: Vec<String>,
|
||||
/// Merge commit id (status=merged only)
|
||||
#[arg(long)]
|
||||
merge_commit: Option<String>,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Subcommand)]
|
||||
pub enum IssuesCmd {
|
||||
@@ -1224,10 +1300,6 @@ pub enum IssuesCmd {
|
||||
},
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Upload subcommands
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
#[derive(Subcommand)]
|
||||
pub enum UploadCmd {
|
||||
/// Upload a file to the relay's Blossom store
|
||||
@@ -1238,10 +1310,6 @@ pub enum UploadCmd {
|
||||
},
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Mem subcommands (NIP-AE)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// Subcommands for `buzz mem`.
|
||||
#[derive(Subcommand)]
|
||||
pub enum MemCmd {
|
||||
@@ -1325,10 +1393,6 @@ pub enum MemCmd {
|
||||
},
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Pack subcommands (local, no relay connection needed)
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// Subcommands for `buzz pack`.
|
||||
#[derive(Subcommand)]
|
||||
pub enum PackCmd {
|
||||
@@ -1344,10 +1408,6 @@ pub enum PackCmd {
|
||||
},
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Command dispatch
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
async fn run(cli: Cli) -> Result<(), CliError> {
|
||||
let relay_url = client::normalize_relay_url(&cli.relay);
|
||||
|
||||
@@ -1400,16 +1460,13 @@ async fn run(cli: Cli) -> Result<(), CliError> {
|
||||
Cmd::Repos(sub) => commands::repos::dispatch(sub, &client).await,
|
||||
Cmd::Patches(sub) => commands::patches::dispatch(sub, &client).await,
|
||||
Cmd::Issues(sub) => commands::issues::dispatch(sub, &client).await,
|
||||
Cmd::Pr(sub) => commands::pr::dispatch(sub, &client).await,
|
||||
Cmd::Upload(sub) => commands::upload::dispatch(sub, &client).await,
|
||||
Cmd::Mem(sub) => commands::mem::dispatch(sub, &client).await,
|
||||
Cmd::Pack(_) => unreachable!("handled above"),
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Tests
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -1435,6 +1492,7 @@ mod tests {
|
||||
"notes",
|
||||
"pack",
|
||||
"patches",
|
||||
"pr",
|
||||
"reactions",
|
||||
"repos",
|
||||
"social",
|
||||
@@ -1548,6 +1606,10 @@ mod tests {
|
||||
]
|
||||
);
|
||||
assert_eq!(names(&cmd, "repos"), vec!["create", "get", "list"]);
|
||||
assert_eq!(
|
||||
names(&cmd, "pr"),
|
||||
vec!["get", "list", "open", "status", "update"]
|
||||
);
|
||||
assert_eq!(
|
||||
names(&cmd, "patches"),
|
||||
vec!["get", "list", "send", "status"]
|
||||
@@ -1572,6 +1634,7 @@ mod tests {
|
||||
("messages", 8),
|
||||
("pack", 2),
|
||||
("patches", 4),
|
||||
("pr", 5),
|
||||
("reactions", 3),
|
||||
("repos", 3),
|
||||
("social", 7),
|
||||
|
||||
@@ -419,8 +419,6 @@ mod tests {
|
||||
assert!(super::parse_uuid("not-a-uuid").is_err());
|
||||
}
|
||||
|
||||
// ── validate_repo_id ─────────────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn validate_repo_id_valid() {
|
||||
assert!(super::validate_repo_id("my-repo").is_ok());
|
||||
|
||||
@@ -7,8 +7,6 @@
|
||||
use std::fmt;
|
||||
use std::str::FromStr;
|
||||
|
||||
// ── Visibility ───────────────────────────────────────────────────────────────
|
||||
|
||||
/// Whether a channel is publicly visible or invite-only.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum ChannelVisibility {
|
||||
@@ -46,8 +44,6 @@ impl FromStr for ChannelVisibility {
|
||||
}
|
||||
}
|
||||
|
||||
// ── Channel type ─────────────────────────────────────────────────────────────
|
||||
|
||||
/// The functional type of a channel.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum ChannelType {
|
||||
@@ -93,8 +89,6 @@ impl FromStr for ChannelType {
|
||||
}
|
||||
}
|
||||
|
||||
// ── Member role ──────────────────────────────────────────────────────────────
|
||||
|
||||
/// A member's role within a channel.
|
||||
///
|
||||
/// The hierarchy for permission checks is: Owner > Admin > Member > Guest.
|
||||
|
||||
@@ -153,8 +153,6 @@ pub fn d_tag(k_c: &ConversationKey, slug: &str) -> String {
|
||||
hex::encode(mac.finalize().into_bytes())
|
||||
}
|
||||
|
||||
// ── Bodies ──────────────────────────────────────────────────────────────────
|
||||
|
||||
/// A decoded engram body. The slug discriminates the variant.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum Body {
|
||||
@@ -368,8 +366,6 @@ fn parse_strict_json(bytes: &[u8]) -> Result<serde_json::Value, EngramError> {
|
||||
Ok(v)
|
||||
}
|
||||
|
||||
// ── Reference extraction (`[[slug]]`) ───────────────────────────────────────
|
||||
|
||||
/// Extract `[[slug]]` references from a body's free-form text field
|
||||
/// (`profile` for [`Body::Core`], `value` for [`Body::Memory`]).
|
||||
///
|
||||
@@ -431,8 +427,6 @@ pub fn extract_refs(body: &str) -> Vec<String> {
|
||||
out
|
||||
}
|
||||
|
||||
// ── Envelope build / parse ──────────────────────────────────────────────────
|
||||
|
||||
/// Build a signed `kind:30174` event for a given body.
|
||||
///
|
||||
/// * `created_at` is the timestamp to sign — callers MUST supply a value
|
||||
@@ -618,9 +612,7 @@ mod tests {
|
||||
Keys::parse(s).unwrap()
|
||||
}
|
||||
|
||||
// ── Reference test vectors from docs/nips/NIP-AE.md §"Reference test
|
||||
// vectors". Pinning these as CI invariants is the single best
|
||||
// interop guarantee for this implementation. ──
|
||||
|
||||
const SECKEY_A: &str = "0000000000000000000000000000000000000000000000000000000000000001";
|
||||
const SECKEY_O: &str = "0000000000000000000000000000000000000000000000000000000000000002";
|
||||
@@ -895,8 +887,6 @@ mod tests {
|
||||
assert!(matches!(err, EngramError::BodyTooLarge(_)));
|
||||
}
|
||||
|
||||
// ── extract_refs ────────────────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn extract_refs_empty_body() {
|
||||
assert!(extract_refs("").is_empty());
|
||||
|
||||
@@ -15,8 +15,6 @@
|
||||
use crate::channel::MemberRole;
|
||||
use std::fmt;
|
||||
|
||||
// ── Limits (DoS prevention for untrusted kind:30617 input) ───────────────────
|
||||
|
||||
/// Maximum number of `buzz-protect` tags per repo.
|
||||
pub const MAX_PROTECTION_RULES: usize = 50;
|
||||
/// Maximum character length of a ref pattern.
|
||||
@@ -24,8 +22,6 @@ pub const MAX_PATTERN_LENGTH: usize = 256;
|
||||
/// Maximum number of wildcard segments per pattern.
|
||||
pub const MAX_WILDCARDS_PER_PATTERN: usize = 3;
|
||||
|
||||
// ── Ref Pattern ──────────────────────────────────────────────────────────────
|
||||
|
||||
/// A validated ref pattern for matching git refs.
|
||||
///
|
||||
/// Grammar: `segment ("/" segment)*` where segment is either a literal
|
||||
@@ -195,8 +191,6 @@ impl fmt::Display for RefPattern {
|
||||
}
|
||||
}
|
||||
|
||||
// ── Update Classification ────────────────────────────────────────────────────
|
||||
|
||||
/// The type of ref update in a push.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum UpdateKind {
|
||||
@@ -242,8 +236,6 @@ pub struct RefUpdate {
|
||||
pub new_oid: String,
|
||||
}
|
||||
|
||||
// ── Protection Rules ─────────────────────────────────────────────────────────
|
||||
|
||||
/// A single protection rule parsed from a `buzz-protect` tag on kind:30617.
|
||||
///
|
||||
/// Format: `["buzz-protect", "<ref-pattern>", "<rule>", ...]`
|
||||
@@ -407,8 +399,6 @@ pub fn parse_protection_tags(tags: &[Vec<String>]) -> Result<ParsedProtection, R
|
||||
})
|
||||
}
|
||||
|
||||
// ── Built-in Defaults ────────────────────────────────────────────────────────
|
||||
|
||||
/// Built-in default minimum role for an operation when no `buzz-protect` tag matches.
|
||||
pub fn default_min_role(ref_name: &str, kind: UpdateKind) -> MemberRole {
|
||||
let is_branch = ref_name.starts_with("refs/heads/");
|
||||
@@ -437,8 +427,6 @@ pub fn default_min_role(ref_name: &str, kind: UpdateKind) -> MemberRole {
|
||||
}
|
||||
}
|
||||
|
||||
// ── Effective Rules (union of all matching patterns) ─────────────────────────
|
||||
|
||||
/// The effective constraints for a ref after unioning all matching rules.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct EffectiveRules {
|
||||
@@ -499,8 +487,6 @@ impl EffectiveRules {
|
||||
}
|
||||
}
|
||||
|
||||
// ── Policy Denial ────────────────────────────────────────────────────────────
|
||||
|
||||
/// A single denial reason from the policy engine.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Denial {
|
||||
@@ -612,14 +598,10 @@ pub fn evaluate_push(
|
||||
}
|
||||
}
|
||||
|
||||
// ── Tests ────────────────────────────────────────────────────────────────────
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
// ── RefPattern tests ─────────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn pattern_parse_valid() {
|
||||
let p = RefPattern::parse("refs/heads/main").unwrap();
|
||||
@@ -697,8 +679,6 @@ mod tests {
|
||||
assert!(!p.matches("refs/heads"));
|
||||
}
|
||||
|
||||
// ── UpdateKind tests ─────────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn classify_create() {
|
||||
let zero = "0000000000000000000000000000000000000000";
|
||||
@@ -733,8 +713,6 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
// ── Protection rule parsing ──────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn parse_protection_tag_basic() {
|
||||
let rule =
|
||||
@@ -791,8 +769,6 @@ mod tests {
|
||||
));
|
||||
}
|
||||
|
||||
// ── Effective rules (union semantics) ────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn effective_rules_union_strictest_role() {
|
||||
let rules = vec![
|
||||
@@ -812,8 +788,6 @@ mod tests {
|
||||
assert!(!eff.has_explicit_match);
|
||||
}
|
||||
|
||||
// ── Policy evaluation ────────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn evaluate_owner_passes_push_role() {
|
||||
let rules = vec![parse_protection_tag(&["refs/heads/main", "push:admin"]).unwrap()];
|
||||
|
||||
@@ -110,6 +110,33 @@ pub const KIND_EVENT_REMINDER: u32 = 30300;
|
||||
/// a compile-time bitset or sorted array with binary search for hot-path use.
|
||||
pub const AUTHOR_ONLY_KINDS: &[u32] = &[KIND_EVENT_REMINDER];
|
||||
|
||||
/// NIP-AP: Agent Persona (parameterized replaceable, owner-authored).
|
||||
///
|
||||
/// Persona definition event published by the workspace owner. Addressed by
|
||||
/// `(pubkey, kind, d_tag)` where `d_tag` is the plaintext persona slug.
|
||||
/// Content is a JSON body containing persona fields (system_prompt,
|
||||
/// display_name, avatar_url, runtime, model, provider, name_pool).
|
||||
/// Designed for discoverability and sharing — d-tag is not blinded.
|
||||
pub const KIND_PERSONA: u32 = 30175;
|
||||
|
||||
/// NIP-AP: Agent Team (parameterized replaceable, owner-authored).
|
||||
///
|
||||
/// Team definition event published by the workspace owner. Addressed by
|
||||
/// `(pubkey, kind, d_tag)` where `d_tag` is the team's stable id. Content is a
|
||||
/// JSON body projecting public team fields (name, description, persona_ids).
|
||||
/// A team is a user-facing grouping of personas; publishing keeps it
|
||||
/// authoritative across clients and reboots, mirroring `KIND_PERSONA`.
|
||||
pub const KIND_TEAM: u32 = 30176;
|
||||
|
||||
/// NIP-AP: Managed Agent (parameterized replaceable, owner-authored).
|
||||
///
|
||||
/// Managed-agent definition event published by the workspace owner. Addressed
|
||||
/// by `(pubkey, kind, d_tag)` where `d_tag` is the agent's pubkey. Content is
|
||||
/// an explicit opt-IN allowlist projection of the agent record — it MUST never
|
||||
/// carry the agent's secret key, NIP-OA auth tag, env vars, or runtime fields,
|
||||
/// since these events are world-readable on the relay.
|
||||
pub const KIND_MANAGED_AGENT: u32 = 30177;
|
||||
|
||||
// NIP-29 group admin events
|
||||
/// NIP-29: Add a user to a group.
|
||||
pub const KIND_NIP29_PUT_USER: u32 = 9000;
|
||||
@@ -348,7 +375,6 @@ pub const KIND_HUDDLE_GUIDELINES: u32 = 48106;
|
||||
/// Internal kind for media upload audit entries. Not a relay event kind.
|
||||
pub const KIND_MEDIA_UPLOAD: u32 = 49001;
|
||||
|
||||
// ── NIP-34: Git repository events ────────────────────────────────────────────
|
||||
/// NIP-34: Repository announcement (parameterized replaceable, d-tag = repo-id).
|
||||
pub const KIND_GIT_REPO_ANNOUNCEMENT: u32 = 30617;
|
||||
/// NIP-34: Repository state — current branch/tag refs (parameterized replaceable, d-tag = repo-id).
|
||||
@@ -391,6 +417,9 @@ pub const ALL_KINDS: &[u32] = &[
|
||||
KIND_AGENT_PROFILE,
|
||||
KIND_AGENT_ENGRAM,
|
||||
KIND_EVENT_REMINDER,
|
||||
KIND_PERSONA,
|
||||
KIND_TEAM,
|
||||
KIND_MANAGED_AGENT,
|
||||
KIND_NIP29_PUT_USER,
|
||||
KIND_NIP29_REMOVE_USER,
|
||||
KIND_NIP29_EDIT_METADATA,
|
||||
@@ -573,6 +602,9 @@ pub fn event_kind_i32(event: &nostr::Event) -> i32 {
|
||||
|
||||
// Compile-time: new kinds are in the expected ranges.
|
||||
const _: () = assert!(is_replaceable(KIND_AGENT_PROFILE)); // 10100 ∈ 10000–19999
|
||||
const _: () = assert!(is_parameterized_replaceable(KIND_PERSONA)); // 30175 ∈ 30000–39999
|
||||
const _: () = assert!(is_parameterized_replaceable(KIND_TEAM)); // 30176 ∈ 30000–39999
|
||||
const _: () = assert!(is_parameterized_replaceable(KIND_MANAGED_AGENT)); // 30177 ∈ 30000–39999
|
||||
const _: () = assert!(is_parameterized_replaceable(KIND_WORKFLOW_DEF)); // 30620 ∈ 30000–39999
|
||||
const _: () = assert!(is_parameterized_replaceable(KIND_EVENT_REMINDER)); // 30300 ∈ 30000–39999
|
||||
const _: () = assert!(is_parameterized_replaceable(KIND_MESH_LLM_RELAY_STATUS)); // 30621 ∈ 30000–39999
|
||||
|
||||
@@ -23,14 +23,10 @@
|
||||
use nostr::hashes::Hash as _;
|
||||
use nostr::util::hkdf;
|
||||
|
||||
// ── HKDF info strings ────────────────────────────────────────────────────────
|
||||
|
||||
const INFO_SESSION_ID: &[u8] = b"nostr-pair-session-id";
|
||||
const INFO_SAS: &[u8] = b"nostr-pair-sas-v1";
|
||||
const INFO_TRANSCRIPT: &[u8] = b"nostr-pair-transcript-v1";
|
||||
|
||||
// ── Internal helper ───────────────────────────────────────────────────────────
|
||||
|
||||
/// Run HKDF-SHA256(IKM=`ikm`, salt=`salt`, info=`info`) and return 32 bytes.
|
||||
///
|
||||
/// Uses `nostr::util::hkdf::{extract, expand}` directly so we don't pull in
|
||||
@@ -46,8 +42,6 @@ fn hkdf32(salt: &[u8], ikm: &[u8], info: &[u8]) -> [u8; 32] {
|
||||
out
|
||||
}
|
||||
|
||||
// ── Public API ────────────────────────────────────────────────────────────────
|
||||
|
||||
/// Derive the session ID from the session secret.
|
||||
///
|
||||
/// ```text
|
||||
@@ -134,14 +128,10 @@ pub fn ct_eq(a: &[u8; 32], b: &[u8; 32]) -> bool {
|
||||
a.ct_eq(b).into()
|
||||
}
|
||||
|
||||
// ── Tests ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
// ── Test vector inputs (from NIP-AB spec) ─────────────────────────────────
|
||||
|
||||
/// session_secret = 0xa1b2c3d4…
|
||||
fn session_secret() -> [u8; 32] {
|
||||
hex_to_32("a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0c1d2e3f4a5b6c7d8e9f0a1b2")
|
||||
@@ -166,8 +156,6 @@ mod tests {
|
||||
hex::encode(b)
|
||||
}
|
||||
|
||||
// ── session_id derivation ─────────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn session_id_is_deterministic() {
|
||||
let secret = session_secret();
|
||||
@@ -199,8 +187,6 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
// ── SAS derivation ────────────────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn sas_code_is_six_digits() {
|
||||
// Use a synthetic ECDH shared secret (just some fixed bytes).
|
||||
@@ -257,8 +243,6 @@ mod tests {
|
||||
assert!(code < 1_000_000);
|
||||
}
|
||||
|
||||
// ── transcript_hash derivation ────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn transcript_hash_is_deterministic() {
|
||||
use nostr::{Keys, SecretKey};
|
||||
@@ -365,8 +349,6 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
// ── format_sas ────────────────────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn format_sas_zero_padding() {
|
||||
assert_eq!(format_sas(0), "000000");
|
||||
@@ -385,8 +367,6 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
// ── Full round-trip consistency ───────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn full_derivation_round_trip() {
|
||||
use nostr::{Keys, SecretKey};
|
||||
|
||||
@@ -29,8 +29,6 @@ use zeroize::Zeroize;
|
||||
|
||||
use super::PairingError;
|
||||
|
||||
// ── Data types ────────────────────────────────────────────────────────────────
|
||||
|
||||
/// Data encoded in the QR code displayed by the source device.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct QrPayload {
|
||||
@@ -57,8 +55,6 @@ impl Drop for QrPayload {
|
||||
}
|
||||
}
|
||||
|
||||
// ── Encoding ──────────────────────────────────────────────────────────────────
|
||||
|
||||
/// Encode a [`QrPayload`] as a `nostrpair://` URI.
|
||||
///
|
||||
/// Relay URLs are percent-encoded (`:` → `%3A`, `/` → `%2F`) so they can
|
||||
@@ -96,8 +92,6 @@ pub fn encode_qr(payload: &QrPayload) -> String {
|
||||
uri
|
||||
}
|
||||
|
||||
// ── Decoding ──────────────────────────────────────────────────────────────────
|
||||
|
||||
/// Decode a `nostrpair://` URI into a [`QrPayload`].
|
||||
///
|
||||
/// # Errors
|
||||
@@ -225,8 +219,6 @@ pub fn decode_qr(uri: &str) -> Result<QrPayload, PairingError> {
|
||||
})
|
||||
}
|
||||
|
||||
// ── URL encoding helpers ──────────────────────────────────────────────────────
|
||||
|
||||
/// Percent-encode a relay URL for use as a query parameter value.
|
||||
///
|
||||
/// Uses `percent-encoding` crate's `NON_ALPHANUMERIC` set, which encodes
|
||||
@@ -250,8 +242,6 @@ fn is_lowercase_hex(c: char) -> bool {
|
||||
c.is_ascii_digit() || ('a'..='f').contains(&c)
|
||||
}
|
||||
|
||||
// ── Tests ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -544,8 +534,6 @@ mod tests {
|
||||
assert_eq!(decoded.version, 1, "missing v= should default to version 1");
|
||||
}
|
||||
|
||||
// ── All-zeros session_secret rejection ────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn reject_all_zeros_session_secret() {
|
||||
let keys = Keys::generate();
|
||||
@@ -563,8 +551,6 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
// ── Lowercase hex enforcement ─────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn reject_uppercase_hex_in_pubkey() {
|
||||
let keys = Keys::generate();
|
||||
|
||||
@@ -45,8 +45,6 @@ const DEFAULT_TIMEOUT: Duration = Duration::from_secs(120);
|
||||
/// NIP-AB event kind (from the kind registry).
|
||||
const PAIRING_KIND: u16 = crate::kind::KIND_PAIRING as u16;
|
||||
|
||||
// ── Public types ──────────────────────────────────────────────────────────────
|
||||
|
||||
/// Which role this device plays in the pairing.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum Role {
|
||||
@@ -108,8 +106,6 @@ pub struct PairingSession {
|
||||
timeout: Duration,
|
||||
}
|
||||
|
||||
// ── Source-side constructors and methods ───────────────────────────────────────
|
||||
|
||||
impl PairingSession {
|
||||
/// Create a new source session. Returns the session and a QR payload
|
||||
/// to display to the user.
|
||||
@@ -283,8 +279,6 @@ impl PairingSession {
|
||||
}
|
||||
}
|
||||
|
||||
// ── Target-side constructors and methods ──────────────────────────────────────
|
||||
|
||||
impl PairingSession {
|
||||
/// Create a new target session from a scanned QR payload.
|
||||
///
|
||||
@@ -427,8 +421,6 @@ impl PairingSession {
|
||||
}
|
||||
}
|
||||
|
||||
// ── Shared methods ────────────────────────────────────────────────────────────
|
||||
|
||||
impl PairingSession {
|
||||
/// Build an abort event. Returns `None` if no peer is known yet
|
||||
/// (nothing to encrypt to), but still transitions to [`SessionState::Aborted`].
|
||||
@@ -535,8 +527,6 @@ impl PairingSession {
|
||||
}
|
||||
}
|
||||
|
||||
// ── Test-only accessors ───────────────────────────────────────────────────────
|
||||
|
||||
#[cfg(test)]
|
||||
impl PairingSession {
|
||||
/// Returns `true` if the given event ID has been recorded as processed.
|
||||
@@ -553,8 +543,6 @@ impl PairingSession {
|
||||
}
|
||||
}
|
||||
|
||||
// ── Internal helpers ──────────────────────────────────────────────────────────
|
||||
|
||||
impl PairingSession {
|
||||
/// Encrypt a message and wrap it in a signed kind:24134 event.
|
||||
///
|
||||
@@ -765,8 +753,6 @@ fn unexpected(expected: &str, got: &PairingMessage) -> PairingError {
|
||||
}
|
||||
}
|
||||
|
||||
// ── Tests ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
@@ -900,8 +900,6 @@ fn row_to_member_record(row: sqlx::postgres::PgRow) -> Result<MemberRecord> {
|
||||
})
|
||||
}
|
||||
|
||||
// ── Phase 2: Channel Metadata ─────────────────────────────────────────────────
|
||||
|
||||
/// Partial update for channel metadata. Every field is `None` to leave the
|
||||
/// column unchanged.
|
||||
#[derive(Default)]
|
||||
|
||||
@@ -869,7 +869,6 @@ pub async fn insert_event_with_thread_metadata(
|
||||
let not_before = extract_not_before(event);
|
||||
let mut tx = pool.begin().await?;
|
||||
|
||||
// ── Insert event ──────────────────────────────────────────────────────────
|
||||
let result = sqlx::query(
|
||||
r#"
|
||||
INSERT INTO events (id, pubkey, created_at, kind, tags, content, sig, received_at, channel_id, d_tag, not_before)
|
||||
@@ -893,7 +892,6 @@ pub async fn insert_event_with_thread_metadata(
|
||||
|
||||
let was_inserted = result.rows_affected() > 0;
|
||||
|
||||
// ── Insert thread metadata (if provided and event was actually inserted) ──
|
||||
if was_inserted {
|
||||
if let Some(ref meta) = thread_meta {
|
||||
let broadcast_val: bool = meta.broadcast;
|
||||
|
||||
@@ -216,8 +216,6 @@ impl Db {
|
||||
self.pool.begin().await.map_err(Into::into)
|
||||
}
|
||||
|
||||
// ── Events ───────────────────────────────────────────────────────────────
|
||||
|
||||
/// Inserts an event. Returns `(StoredEvent, was_inserted)` — `false` on duplicate.
|
||||
pub async fn insert_event(
|
||||
&self,
|
||||
@@ -339,8 +337,6 @@ impl Db {
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
// ── Channels ─────────────────────────────────────────────────────────────
|
||||
|
||||
/// Creates a new channel, bootstraps the creator as owner, and returns the record.
|
||||
pub async fn create_channel(
|
||||
&self,
|
||||
@@ -539,8 +535,6 @@ impl Db {
|
||||
channel::reap_expired_ephemeral_channels(&self.pool).await
|
||||
}
|
||||
|
||||
// ── Reminder scheduler ───────────────────────────────────────────────────
|
||||
|
||||
/// Query due reminders ready for delivery.
|
||||
pub async fn query_due_reminders(
|
||||
&self,
|
||||
@@ -559,8 +553,6 @@ impl Db {
|
||||
event::claim_due_reminder(&self.pool, event_id, event_created_at).await
|
||||
}
|
||||
|
||||
// ── Users ────────────────────────────────────────────────────────────────
|
||||
|
||||
/// Ensure a user record exists (upsert).
|
||||
pub async fn ensure_user(&self, pubkey: &[u8]) -> Result<()> {
|
||||
user::ensure_user(&self.pool, pubkey).await
|
||||
@@ -633,8 +625,6 @@ impl Db {
|
||||
user::set_channel_add_policy(&self.pool, pubkey, policy).await
|
||||
}
|
||||
|
||||
// ── Direct Messages ──────────────────────────────────────────────────────
|
||||
|
||||
/// Find an existing DM by its participant hash.
|
||||
pub async fn find_dm_by_participants(
|
||||
&self,
|
||||
@@ -689,8 +679,6 @@ impl Db {
|
||||
dm::list_hidden_dms(&self.pool, pubkey).await
|
||||
}
|
||||
|
||||
// ── Threads ──────────────────────────────────────────────────────────────
|
||||
|
||||
/// Insert thread metadata.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub async fn insert_thread_metadata(
|
||||
@@ -776,8 +764,6 @@ impl Db {
|
||||
thread::decrement_reply_count(&self.pool, parent_event_id, root_event_id).await
|
||||
}
|
||||
|
||||
// ── Reactions ────────────────────────────────────────────────────────────
|
||||
|
||||
/// Add (or re-activate) a reaction.
|
||||
pub async fn add_reaction(
|
||||
&self,
|
||||
@@ -868,8 +854,6 @@ impl Db {
|
||||
reaction::get_reactions_bulk(&self.pool, event_ids).await
|
||||
}
|
||||
|
||||
// ── Feed ─────────────────────────────────────────────────────────────────
|
||||
|
||||
/// Find events that @mention the given pubkey.
|
||||
pub async fn query_feed_mentions(
|
||||
&self,
|
||||
@@ -962,8 +946,6 @@ impl Db {
|
||||
feed::query_activity(&self.pool, accessible_channel_ids, since, limit).await
|
||||
}
|
||||
|
||||
// ── API Tokens ───────────────────────────────────────────────────────────
|
||||
|
||||
/// Create a new API token record.
|
||||
pub async fn create_api_token(
|
||||
&self,
|
||||
@@ -1103,8 +1085,6 @@ impl Db {
|
||||
api_token::revoke_all_tokens(&self.pool, owner_pubkey, revoked_by).await
|
||||
}
|
||||
|
||||
// ── Workflows ────────────────────────────────────────────────────────────
|
||||
|
||||
/// Create a new workflow.
|
||||
pub async fn create_workflow(
|
||||
&self,
|
||||
@@ -1286,8 +1266,6 @@ impl Db {
|
||||
.await
|
||||
}
|
||||
|
||||
// ── Partitions ──────────────────────────────────────────────────────────
|
||||
|
||||
/// Ensures monthly partitions exist for the next N months.
|
||||
pub async fn ensure_future_partitions(&self, months_ahead: u32) -> Result<()> {
|
||||
partition::ensure_future_partitions(&self.pool, months_ahead).await
|
||||
@@ -1312,8 +1290,6 @@ impl Db {
|
||||
Ok(result.rows_affected())
|
||||
}
|
||||
|
||||
// ── Pubkey Allowlist ─────────────────────────────────────────────────────
|
||||
|
||||
/// Check if a pubkey is in the allowlist.
|
||||
pub async fn is_pubkey_allowed(&self, pubkey: &[u8]) -> Result<bool> {
|
||||
let row = sqlx::query("SELECT COUNT(*) as cnt FROM pubkey_allowlist WHERE pubkey = $1")
|
||||
@@ -1381,8 +1357,6 @@ impl Db {
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
// ── Relay Members (NIP-43) ───────────────────────────────────────────────
|
||||
|
||||
/// Returns `true` if `pubkey` (64-char hex) is in the relay member list.
|
||||
pub async fn is_relay_member(&self, pubkey: &str) -> Result<bool> {
|
||||
relay_members::is_relay_member(&self.pool, pubkey).await
|
||||
@@ -1450,8 +1424,6 @@ impl Db {
|
||||
relay_members::backfill_from_allowlist(&self.pool).await
|
||||
}
|
||||
|
||||
// ── Archived identities (NIP-IA) ──────────────────────────────────────────
|
||||
|
||||
/// Returns `true` if `pubkey` (64-char hex) is currently archived.
|
||||
pub async fn is_archived(&self, pubkey: &str) -> Result<bool> {
|
||||
archived_identities::is_archived(&self.pool, pubkey).await
|
||||
@@ -1489,8 +1461,6 @@ impl Db {
|
||||
archived_identities::list_archived(&self.pool).await
|
||||
}
|
||||
|
||||
// ── Discovery events ─────────────────────────────────────────────────────
|
||||
|
||||
/// Soft-delete NIP-29 discovery events for a channel created by a specific relay pubkey.
|
||||
pub async fn soft_delete_discovery_events(
|
||||
&self,
|
||||
@@ -1508,8 +1478,6 @@ impl Db {
|
||||
Ok(result.rows_affected())
|
||||
}
|
||||
|
||||
// ── Replaceable events ─────────────────────────────────────────────────
|
||||
|
||||
/// Atomically replace a replaceable event: NIP-16 kinds (0, 3, 41, 10000–19999)
|
||||
/// and NIP-29 discovery state (39000–39002, called from side_effects.rs).
|
||||
///
|
||||
|
||||
@@ -555,8 +555,6 @@ mod tests {
|
||||
assert!(result.is_err(), "should reject invalid policy value");
|
||||
}
|
||||
|
||||
// ── LIKE escaping unit tests (no DB required) ──────────────────────
|
||||
|
||||
// Use the production `escape_like` function directly — no local mirror.
|
||||
use super::escape_like;
|
||||
|
||||
|
||||
@@ -554,10 +554,6 @@ fn human_bytes(n: usize) -> String {
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Tests
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
@@ -55,8 +55,6 @@ use tokio_tungstenite::tungstenite::protocol::{Message, Role, WebSocketConfig};
|
||||
use tokio_tungstenite::WebSocketStream;
|
||||
use tokio_util::sync::CancellationToken;
|
||||
|
||||
// ── Constants ─────────────────────────────────────────────────────────────────
|
||||
|
||||
/// Hard per-connection lifetime. `pub(crate)` for test access.
|
||||
pub(crate) const CONN_TIMEOUT: Duration = Duration::from_secs(120);
|
||||
|
||||
@@ -90,8 +88,6 @@ const ENTRY_TTL: Duration = Duration::from_secs(300);
|
||||
/// Freshness window in seconds (±).
|
||||
const FRESHNESS_SECS: i64 = 120;
|
||||
|
||||
// ── Core types ────────────────────────────────────────────────────────────────
|
||||
|
||||
enum OutMsg {
|
||||
Text(String),
|
||||
Pong(Vec<u8>),
|
||||
@@ -220,8 +216,6 @@ impl Relay {
|
||||
}
|
||||
}
|
||||
|
||||
// ── RAII connection guard ─────────────────────────────────────────────────────
|
||||
|
||||
struct ConnGuard {
|
||||
relay: Arc<Relay>,
|
||||
conn_id: u64,
|
||||
@@ -239,8 +233,6 @@ impl Drop for ConnGuard {
|
||||
}
|
||||
}
|
||||
|
||||
// ── Rate limiter ──────────────────────────────────────────────────────────────
|
||||
|
||||
struct RateWindow {
|
||||
count: u32,
|
||||
window_start: tokio::time::Instant,
|
||||
@@ -264,8 +256,6 @@ impl RateWindow {
|
||||
}
|
||||
}
|
||||
|
||||
// ── JSON helpers ──────────────────────────────────────────────────────────────
|
||||
|
||||
fn jarr(v: Vec<Value>) -> String {
|
||||
Value::Array(v).to_string()
|
||||
}
|
||||
@@ -301,8 +291,6 @@ fn make_notice(msg: &str) -> String {
|
||||
])
|
||||
}
|
||||
|
||||
// ── Validation ────────────────────────────────────────────────────────────────
|
||||
|
||||
fn is_lower_hex(s: &str, len: usize) -> bool {
|
||||
s.len() == len && s.bytes().all(|b| matches!(b, b'0'..=b'9' | b'a'..=b'f'))
|
||||
}
|
||||
@@ -604,8 +592,6 @@ fn decode_hex64(s: &str) -> Option<[u8; 64]> {
|
||||
Some(out)
|
||||
}
|
||||
|
||||
// ── Writer task ───────────────────────────────────────────────────────────────
|
||||
|
||||
type WsSink = futures_util::stream::SplitSink<WebSocketStream<TokioIo<Upgraded>>, Message>;
|
||||
|
||||
async fn writer_task(mut sink: WsSink, mut rx: mpsc::Receiver<OutMsg>, cancel: CancellationToken) {
|
||||
@@ -631,8 +617,6 @@ async fn writer_task(mut sink: WsSink, mut rx: mpsc::Receiver<OutMsg>, cancel: C
|
||||
}
|
||||
}
|
||||
|
||||
// ── Connection handler ────────────────────────────────────────────────────────
|
||||
|
||||
async fn handle_conn(relay: Arc<Relay>, conn_id: u64, stream: WebSocketStream<TokioIo<Upgraded>>) {
|
||||
let _guard = ConnGuard {
|
||||
relay: Arc::clone(&relay),
|
||||
@@ -926,8 +910,6 @@ async fn handle_conn(relay: Arc<Relay>, conn_id: u64, stream: WebSocketStream<To
|
||||
cancel.cancel();
|
||||
}
|
||||
|
||||
// ── HTTP upgrade ──────────────────────────────────────────────────────────────
|
||||
|
||||
async fn http_service(
|
||||
relay: Arc<Relay>,
|
||||
mut req: Request<Incoming>,
|
||||
@@ -1012,8 +994,6 @@ async fn http_service(
|
||||
Ok(resp)
|
||||
}
|
||||
|
||||
// ── Server loop (extracted for testability) ───────────────────────────────────
|
||||
|
||||
/// Run the relay accept loop on the given listener.
|
||||
/// Public for integration tests that bind to `:0`.
|
||||
pub async fn run_server(listener: TcpListener, relay: Arc<Relay>) {
|
||||
|
||||
@@ -13,13 +13,9 @@ use tokio_tungstenite::{connect_async, tungstenite::Message, MaybeTlsStream, Web
|
||||
|
||||
use buzz_pair_relay::{run_server, Relay};
|
||||
|
||||
// ── Crypto imports (for real event signing) ───────────────────────────────────
|
||||
|
||||
use secp256k1::{Keypair, Secp256k1, SecretKey};
|
||||
use sha2::{Digest, Sha256};
|
||||
|
||||
// ── Constants ─────────────────────────────────────────────────────────────────
|
||||
|
||||
/// A valid 64-char lowercase hex string (all 'a's).
|
||||
const P_A: &str = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
|
||||
/// A different valid 64-char lowercase hex string (all 'b's).
|
||||
@@ -32,8 +28,6 @@ const PUBKEY: &str = "dddddddddddddddddddddddddddddddddddddddddddddddddddddddddd
|
||||
/// A valid sig (128 'e's) — used only in pre-sig-check rejection tests.
|
||||
const SIG: &str = "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee";
|
||||
|
||||
// ── Test infrastructure ───────────────────────────────────────────────────────
|
||||
|
||||
type WS = WebSocketStream<MaybeTlsStream<tokio::net::TcpStream>>;
|
||||
|
||||
/// Start a relay on a random port, return the WebSocket URL.
|
||||
@@ -92,8 +86,6 @@ async fn assert_closed(ws: &mut WS) {
|
||||
}
|
||||
}
|
||||
|
||||
// ── Crypto helpers ────────────────────────────────────────────────────────────
|
||||
|
||||
/// Generate a random keypair; returns `(SecretKey, pubkey_hex)`.
|
||||
fn gen_keypair() -> (SecretKey, String) {
|
||||
let secp = Secp256k1::new();
|
||||
@@ -206,8 +198,6 @@ async fn subscribe(ws: &mut WS, sub_id: &str, p_hex: &str) {
|
||||
assert_eq!(eose[1], sub_id);
|
||||
}
|
||||
|
||||
// ── Tests ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
/// 1. No replay: events published before a subscription are not delivered.
|
||||
/// With tightening #2, publishing with no live subscriber is rejected
|
||||
/// ("no live subscriber"), so the publisher gets OK false.
|
||||
|
||||
@@ -30,8 +30,6 @@ use tokio::time::timeout;
|
||||
use tokio_tungstenite::{connect_async, tungstenite::Message};
|
||||
use zeroize::Zeroizing;
|
||||
|
||||
// ── CLI definition ────────────────────────────────────────────────────────────
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(
|
||||
name = "buzz-pair",
|
||||
@@ -72,8 +70,6 @@ enum Cmd {
|
||||
TestVectors,
|
||||
}
|
||||
|
||||
// ── Error type ────────────────────────────────────────────────────────────────
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
enum CliError {
|
||||
#[error("pairing error: {0}")]
|
||||
@@ -98,8 +94,6 @@ enum CliError {
|
||||
Other(String),
|
||||
}
|
||||
|
||||
// ── Entry point ───────────────────────────────────────────────────────────────
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let cli = Cli::parse();
|
||||
@@ -117,8 +111,6 @@ async fn run(cmd: Cmd) -> Result<(), CliError> {
|
||||
}
|
||||
}
|
||||
|
||||
// ── source subcommand ─────────────────────────────────────────────────────────
|
||||
|
||||
async fn cmd_source(relay_url: String, nsec: Option<String>) -> Result<(), CliError> {
|
||||
// Resolve the payload to transfer.
|
||||
let (payload_str, payload_type) = resolve_payload(nsec)?;
|
||||
@@ -208,8 +200,6 @@ async fn cmd_source(relay_url: String, nsec: Option<String>) -> Result<(), CliEr
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// ── target subcommand ─────────────────────────────────────────────────────────
|
||||
|
||||
async fn cmd_target(relay_override: Option<String>, show_secret: bool) -> Result<(), CliError> {
|
||||
// Read QR URI from stdin.
|
||||
print!("Paste the QR URI: ");
|
||||
@@ -342,8 +332,6 @@ async fn cmd_target(relay_override: Option<String>, show_secret: bool) -> Result
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// ── test-vectors subcommand ───────────────────────────────────────────────────
|
||||
|
||||
fn cmd_test_vectors() -> Result<(), CliError> {
|
||||
// Fixed test keys from the NIP-AB spec.
|
||||
let session_secret: [u8; 32] =
|
||||
@@ -409,8 +397,6 @@ fn cmd_test_vectors() -> Result<(), CliError> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// ── Abort-aware event helpers ─────────────────────────────────────────────────
|
||||
|
||||
/// Check whether `event` is an abort from the peer. If so, transition the
|
||||
/// session and return an error the caller can propagate. Otherwise return
|
||||
/// `Ok(())` so the caller can proceed with its own handler.
|
||||
@@ -423,8 +409,6 @@ fn check_for_abort(session: &mut PairingSession, event: &Event) -> Result<(), Cl
|
||||
}
|
||||
}
|
||||
|
||||
// ── NIP-42 auth helper ────────────────────────────────────────────────────────
|
||||
|
||||
/// Handle NIP-42 authentication if the relay requires it.
|
||||
///
|
||||
/// Uses the pairing session's ephemeral keys to authenticate, ensuring the
|
||||
@@ -502,8 +486,6 @@ fn parse_auth_challenge(text: &str) -> Option<String> {
|
||||
None
|
||||
}
|
||||
|
||||
// ── WebSocket helpers ─────────────────────────────────────────────────────────
|
||||
|
||||
/// Publish a Nostr event to the relay.
|
||||
async fn publish_event<S>(write: &mut S, event: &Event) -> Result<(), CliError>
|
||||
where
|
||||
@@ -592,8 +574,6 @@ fn parse_relay_event(text: &str, sub_id: &str) -> Option<Event> {
|
||||
serde_json::from_value(arr[2].clone()).ok()
|
||||
}
|
||||
|
||||
// ── Payload helpers ───────────────────────────────────────────────────────────
|
||||
|
||||
/// Resolve the payload to send.
|
||||
///
|
||||
/// If `nsec` is provided, parse it as bech32 and return the raw nsec string.
|
||||
@@ -617,8 +597,6 @@ fn resolve_payload(nsec: Option<String>) -> Result<(Zeroizing<String>, PayloadTy
|
||||
}
|
||||
}
|
||||
|
||||
// ── I/O helpers ───────────────────────────────────────────────────────────────
|
||||
|
||||
/// Read a single line from stdin (trims trailing newline).
|
||||
fn read_line() -> Result<String, CliError> {
|
||||
let stdin = io::stdin();
|
||||
@@ -636,8 +614,6 @@ fn read_yes_no() -> Result<bool, CliError> {
|
||||
Ok(matches!(line.trim(), "y" | "Y" | "yes" | "Yes" | "YES"))
|
||||
}
|
||||
|
||||
// ── Crypto helpers ────────────────────────────────────────────────────────────
|
||||
|
||||
/// Decode a 64-char hex string into a `[u8; 32]`.
|
||||
fn hex_to_32(s: &str) -> Result<[u8; 32], CliError> {
|
||||
let bytes = hex::decode(s).map_err(|e| CliError::Other(format!("invalid hex '{s}': {e}")))?;
|
||||
|
||||
@@ -19,8 +19,6 @@ use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::persona::RespondTo;
|
||||
|
||||
// ── Errors ────────────────────────────────────────────────────────────────────
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum ManifestError {
|
||||
#[error("failed to read file: {0}")]
|
||||
@@ -33,8 +31,6 @@ pub enum ManifestError {
|
||||
MissingField(String),
|
||||
}
|
||||
|
||||
// ── Supporting types ──────────────────────────────────────────────────────────
|
||||
|
||||
/// Semver engine constraints.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
@@ -73,8 +69,6 @@ pub struct BehavioralDefaults {
|
||||
pub broadcast_replies: Option<bool>,
|
||||
}
|
||||
|
||||
// ── Core struct ───────────────────────────────────────────────────────────────
|
||||
|
||||
/// The pack manifest from `.plugin/plugin.json`.
|
||||
///
|
||||
/// OPS required fields (`id`, `name`, `version`) are validated after
|
||||
@@ -83,12 +77,10 @@ pub struct BehavioralDefaults {
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub struct PackManifest {
|
||||
// ── OPS required ──────────────────────────────────────────────────────
|
||||
pub id: String,
|
||||
pub name: String,
|
||||
pub version: String,
|
||||
|
||||
// ── OPS optional ──────────────────────────────────────────────────────
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub description: Option<String>,
|
||||
|
||||
@@ -107,7 +99,6 @@ pub struct PackManifest {
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub engines: Option<Engines>,
|
||||
|
||||
// ── Buzz extensions ─────────────────────────────────────────────────
|
||||
/// Paths to `.persona.md` files (pack-relative).
|
||||
#[serde(default)]
|
||||
pub personas: Vec<String>,
|
||||
@@ -129,8 +120,6 @@ pub struct PackManifest {
|
||||
pub defaults: Option<BehavioralDefaults>,
|
||||
}
|
||||
|
||||
// ── Intermediate for post-parse validation ────────────────────────────────────
|
||||
|
||||
/// Mirrors `PackManifest` but with required fields as `Option` so we can
|
||||
/// produce a clean `MissingField` error instead of a serde path error.
|
||||
///
|
||||
@@ -159,8 +148,6 @@ struct RawManifest {
|
||||
defaults: Option<BehavioralDefaults>,
|
||||
}
|
||||
|
||||
// ── Parser ────────────────────────────────────────────────────────────────────
|
||||
|
||||
/// Parse a `plugin.json` string into a [`PackManifest`].
|
||||
pub fn parse_manifest(content: &str) -> Result<PackManifest, ManifestError> {
|
||||
let raw: RawManifest = serde_json::from_str(content)?;
|
||||
@@ -205,20 +192,14 @@ pub fn parse_manifest_file(path: &Path) -> Result<PackManifest, ManifestError> {
|
||||
parse_manifest(&content)
|
||||
}
|
||||
|
||||
// ── Tests ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
// ── Helpers ───────────────────────────────────────────────────────────
|
||||
|
||||
fn minimal_json() -> &'static str {
|
||||
r#"{"id":"my-pack","name":"My Pack","version":"1.0.0","personas":["personas/bot.persona.md"]}"#
|
||||
}
|
||||
|
||||
// ── Happy path ────────────────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn parse_minimal_valid() {
|
||||
let m = parse_manifest(minimal_json()).unwrap();
|
||||
@@ -306,8 +287,6 @@ mod tests {
|
||||
assert_eq!(rt.keywords, vec!["hey"]);
|
||||
}
|
||||
|
||||
// ── Missing required fields ───────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn missing_id_errors() {
|
||||
let json = r#"{"name":"P","version":"1.0.0"}"#;
|
||||
@@ -338,8 +317,6 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
// ── Empty required fields ─────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn empty_id_errors() {
|
||||
let json = r#"{"id":"","name":"P","version":"1.0.0"}"#;
|
||||
@@ -380,8 +357,6 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
// ── Malformed JSON ────────────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn malformed_json_errors() {
|
||||
let err = parse_manifest("{not valid json}").unwrap_err();
|
||||
|
||||
@@ -7,8 +7,6 @@
|
||||
///
|
||||
/// Levels 1–2 (operator env vars, desktop UI) are resolved at runtime.
|
||||
|
||||
// ── Types ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct TriggersData {
|
||||
pub mentions: bool,
|
||||
@@ -37,13 +35,9 @@ pub struct ResolvedConfig {
|
||||
pub broadcast_replies: bool,
|
||||
}
|
||||
|
||||
// ── Built-in defaults ─────────────────────────────────────────────────────────
|
||||
|
||||
const DEFAULT_THREAD_REPLIES: bool = true;
|
||||
const DEFAULT_BROADCAST_REPLIES: bool = false;
|
||||
|
||||
// ── Core merge ────────────────────────────────────────────────────────────────
|
||||
|
||||
/// Merge pack defaults with per-persona values.
|
||||
///
|
||||
/// Rules:
|
||||
@@ -87,8 +81,6 @@ pub fn merge_behavioral_config(
|
||||
Value::Object(merged)
|
||||
}
|
||||
|
||||
// ── High-level resolver ───────────────────────────────────────────────────────
|
||||
|
||||
/// Resolve a single persona's effective config from raw frontmatter + pack defaults.
|
||||
pub fn resolve_persona_config(
|
||||
persona_frontmatter: &serde_json::Value,
|
||||
@@ -178,8 +170,6 @@ pub fn resolve_persona_config(
|
||||
}
|
||||
}
|
||||
|
||||
// ── Helpers ───────────────────────────────────────────────────────────────────
|
||||
|
||||
fn string_field(v: &serde_json::Value, key: &str) -> Option<String> {
|
||||
v.get(key).and_then(|v| v.as_str()).map(str::to_owned)
|
||||
}
|
||||
@@ -207,15 +197,11 @@ fn parse_triggers(v: &serde_json::Value) -> Option<TriggersData> {
|
||||
})
|
||||
}
|
||||
|
||||
// ── Tests ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use serde_json::json;
|
||||
|
||||
// ── merge_behavioral_config ───────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn persona_value_wins_over_pack_default() {
|
||||
let persona = json!({ "model": "gpt-4o", "thread_replies": false });
|
||||
@@ -287,8 +273,6 @@ mod tests {
|
||||
assert_eq!(merged["subscribe"], json!(["chan-x"])); // persona wins
|
||||
}
|
||||
|
||||
// ── resolve_persona_config ────────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn built_in_defaults_when_no_fields() {
|
||||
let persona = json!({});
|
||||
@@ -340,8 +324,6 @@ mod tests {
|
||||
assert_eq!(resolved.max_context_tokens, Some(8192));
|
||||
}
|
||||
|
||||
// ── triggers shallow replacement ─────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn triggers_shallow_replacement() {
|
||||
// Persona sets `mentions: false` — entire triggers replaces pack default.
|
||||
@@ -439,8 +421,6 @@ mod tests {
|
||||
assert!(!t.all_messages);
|
||||
}
|
||||
|
||||
// ── subscribe merge (Option<Vec<String>>) ────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn subscribe_null_falls_through() {
|
||||
// persona subscribe: null → falls through to pack default.
|
||||
|
||||
@@ -22,8 +22,6 @@ use crate::manifest::{self, ManifestError};
|
||||
use crate::merge::{resolve_persona_config, HooksData, TriggersData};
|
||||
use crate::persona::{self, PersonaConfig};
|
||||
|
||||
// ── Error ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum PackError {
|
||||
#[error("manifest not found at {0}")]
|
||||
@@ -61,8 +59,6 @@ impl From<ManifestError> for PackError {
|
||||
}
|
||||
}
|
||||
|
||||
// ── Public types ──────────────────────────────────────────────────────────────
|
||||
|
||||
/// A fully loaded persona pack.
|
||||
#[derive(Debug)]
|
||||
pub struct LoadedPack {
|
||||
@@ -118,8 +114,6 @@ pub struct PackManifestData {
|
||||
pub defaults: Option<serde_json::Value>,
|
||||
}
|
||||
|
||||
// ── Entry point ───────────────────────────────────────────────────────────────
|
||||
|
||||
/// Load a persona pack from a directory.
|
||||
///
|
||||
/// 1. Read and parse `.plugin/plugin.json`
|
||||
@@ -246,8 +240,6 @@ pub fn load_pack(pack_dir: &Path) -> Result<LoadedPack, PackError> {
|
||||
})
|
||||
}
|
||||
|
||||
// ── Skill resolution ──────────────────────────────────────────────────────────
|
||||
|
||||
/// Determine which skills go to which persona.
|
||||
///
|
||||
/// - Skills listed in a persona's `skills:` array → only that persona
|
||||
@@ -322,8 +314,6 @@ pub fn resolve_skills(pack_dir: &Path, personas: &[LoadedPersona]) -> HashMap<St
|
||||
result
|
||||
}
|
||||
|
||||
// ── Path safety ───────────────────────────────────────────────────────────────
|
||||
|
||||
/// Verify a path resolves within the pack root.
|
||||
///
|
||||
/// Defense-in-depth:
|
||||
@@ -373,8 +363,6 @@ fn safe_resolve(pack_root: &Path, relative: &str) -> Result<PathBuf, PackError>
|
||||
Ok(canonical)
|
||||
}
|
||||
|
||||
// ── Parsing helpers ───────────────────────────────────────────────────────────
|
||||
|
||||
fn read_file(path: &Path) -> Result<String, PackError> {
|
||||
std::fs::read_to_string(path).map_err(|e| PackError::Io {
|
||||
path: path.to_path_buf(),
|
||||
@@ -456,16 +444,12 @@ fn parse_persona_file(
|
||||
})
|
||||
}
|
||||
|
||||
// ── Tests ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::fs;
|
||||
use tempfile::TempDir;
|
||||
|
||||
// ── Fixture helpers ───────────────────────────────────────────────────────
|
||||
|
||||
fn make_pack(dir: &TempDir, personas: &[(&str, &str)]) -> PathBuf {
|
||||
let root = dir.path();
|
||||
|
||||
@@ -504,8 +488,6 @@ description: A fast worker
|
||||
You are Berry, a fast and direct worker.
|
||||
"#;
|
||||
|
||||
// ── load_pack: happy path ─────────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn load_valid_pack() {
|
||||
let dir = TempDir::new().unwrap();
|
||||
@@ -548,8 +530,6 @@ You are Berry, a fast and direct worker.
|
||||
assert!(pack.skills_dir.is_some());
|
||||
}
|
||||
|
||||
// ── load_pack: error cases ────────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn missing_plugin_json_returns_error() {
|
||||
let dir = TempDir::new().unwrap();
|
||||
@@ -578,8 +558,6 @@ You are Berry, a fast and direct worker.
|
||||
assert!(matches!(err, PackError::PersonaNotFound(_)));
|
||||
}
|
||||
|
||||
// ── Path safety ───────────────────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn dotdot_component_rejected() {
|
||||
let dir = TempDir::new().unwrap();
|
||||
@@ -627,8 +605,6 @@ You are Berry, a fast and direct worker.
|
||||
assert!(matches!(err, PackError::PathEscape(_)));
|
||||
}
|
||||
|
||||
// ── Skill resolution ──────────────────────────────────────────────────────
|
||||
|
||||
fn make_loaded_persona(name: &str, skills: Vec<&str>) -> LoadedPersona {
|
||||
LoadedPersona {
|
||||
source_path: PathBuf::from(format!("{name}.persona.md")),
|
||||
@@ -720,8 +696,6 @@ You are Berry, a fast and direct worker.
|
||||
assert!(!map["alpha"].iter().any(|s| s.contains('/')));
|
||||
}
|
||||
|
||||
// ── Pack defaults ─────────────────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn pack_defaults_applied_to_persona() {
|
||||
let dir = TempDir::new().unwrap();
|
||||
|
||||
@@ -17,16 +17,12 @@ use std::path::Path;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
// ── Safety limits ─────────────────────────────────────────────────────────────
|
||||
|
||||
/// Maximum YAML frontmatter size in bytes (1 MiB).
|
||||
pub const MAX_FRONTMATTER_BYTES: usize = 1_048_576;
|
||||
|
||||
/// Maximum persona prompt (markdown body) size in bytes (256 KiB).
|
||||
pub const MAX_BODY_BYTES: usize = 262_144;
|
||||
|
||||
// ── Errors ────────────────────────────────────────────────────────────────────
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum PersonaError {
|
||||
#[error("failed to read file: {0}")]
|
||||
@@ -51,8 +47,6 @@ pub enum PersonaError {
|
||||
MissingField(String),
|
||||
}
|
||||
|
||||
// ── Supporting types ──────────────────────────────────────────────────────────
|
||||
|
||||
/// Controls which messages trigger a response.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
@@ -98,8 +92,6 @@ pub struct Hooks {
|
||||
pub on_message: Option<String>,
|
||||
}
|
||||
|
||||
// ── Core struct ───────────────────────────────────────────────────────────────
|
||||
|
||||
/// Typed representation of a `.persona.md` file (V7 spec).
|
||||
///
|
||||
/// The `prompt` field holds the markdown body (system prompt).
|
||||
@@ -107,7 +99,6 @@ pub struct Hooks {
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub struct PersonaConfig {
|
||||
// ── Identity ──────────────────────────────────────────────────────────
|
||||
/// Machine name (slug). Required.
|
||||
pub name: String,
|
||||
|
||||
@@ -121,14 +112,12 @@ pub struct PersonaConfig {
|
||||
/// One-line description. Required.
|
||||
pub description: String,
|
||||
|
||||
// ── OPS compatibility ─────────────────────────────────────────────────
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub version: Option<String>,
|
||||
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub author: Option<String>,
|
||||
|
||||
// ── Skills & MCP ──────────────────────────────────────────────────────
|
||||
/// Pack-relative paths to skill directories.
|
||||
#[serde(default)]
|
||||
pub skills: Vec<String>,
|
||||
@@ -137,7 +126,6 @@ pub struct PersonaConfig {
|
||||
#[serde(default)]
|
||||
pub mcp_servers: Vec<McpServerConfig>,
|
||||
|
||||
// ── Behavioral config ─────────────────────────────────────────────────
|
||||
/// Channel names to monitor.
|
||||
///
|
||||
/// - `None` (omitted or `null`) → fall through to pack default
|
||||
@@ -173,18 +161,14 @@ pub struct PersonaConfig {
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub broadcast_replies: Option<bool>,
|
||||
|
||||
// ── Hooks ─────────────────────────────────────────────────────────────
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub hooks: Option<Hooks>,
|
||||
|
||||
// ── System prompt (markdown body) ─────────────────────────────────────
|
||||
/// The markdown body of the `.persona.md` file.
|
||||
#[serde(default)]
|
||||
pub prompt: String,
|
||||
}
|
||||
|
||||
// ── Frontmatter-only intermediate ────────────────────────────────────────────
|
||||
|
||||
/// Deserializes just the YAML frontmatter (no `prompt`).
|
||||
/// Unknown keys are rejected — typos cause parse errors instead of silent drops.
|
||||
#[derive(Debug, Deserialize)]
|
||||
@@ -213,8 +197,6 @@ struct Frontmatter {
|
||||
hooks: Option<Hooks>,
|
||||
}
|
||||
|
||||
// ── Parser ────────────────────────────────────────────────────────────────────
|
||||
|
||||
/// Parse a `.persona.md` file into a [`PersonaConfig`].
|
||||
///
|
||||
/// Expects YAML frontmatter between `---` delimiters followed by a markdown
|
||||
@@ -336,8 +318,6 @@ pub fn split_frontmatter(content: &str) -> Result<(&str, &str), PersonaError> {
|
||||
Ok((fm_str, body))
|
||||
}
|
||||
|
||||
// ── Model string helper ───────────────────────────────────────────────────────
|
||||
|
||||
/// Split `"provider:model-id"` into `(Some("provider"), "model-id")`.
|
||||
///
|
||||
/// If there is no colon, returns `(None, full_string)`.
|
||||
@@ -348,20 +328,14 @@ pub fn split_model(model: &str) -> (Option<&str>, &str) {
|
||||
}
|
||||
}
|
||||
|
||||
// ── Tests ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
// ── Helpers ───────────────────────────────────────────────────────────
|
||||
|
||||
fn minimal() -> &'static str {
|
||||
"---\nname: my-bot\ndisplay_name: My Bot\ndescription: Does things.\n---\n"
|
||||
}
|
||||
|
||||
// ── Happy path ────────────────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn parse_minimal_valid() {
|
||||
let p = parse_persona_md(minimal()).unwrap();
|
||||
@@ -455,8 +429,6 @@ You are Full Bot.
|
||||
assert!(matches!(err, PersonaError::Yaml(_)), "got: {err}");
|
||||
}
|
||||
|
||||
// ── Missing required fields ───────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn missing_name_errors() {
|
||||
let src = "---\ndisplay_name: Bot\ndescription: A bot.\n---\n";
|
||||
@@ -487,8 +459,6 @@ You are Full Bot.
|
||||
);
|
||||
}
|
||||
|
||||
// ── Empty required fields (Fix #1) ────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn empty_name_errors() {
|
||||
let src = "---\nname: \"\"\ndisplay_name: Bot\ndescription: A bot.\n---\n";
|
||||
@@ -529,8 +499,6 @@ You are Full Bot.
|
||||
);
|
||||
}
|
||||
|
||||
// ── Delimiter errors ──────────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn no_frontmatter_delimiters_errors() {
|
||||
let err = parse_persona_md("Just plain markdown.").unwrap_err();
|
||||
@@ -567,8 +535,6 @@ You are Full Bot.
|
||||
assert_eq!(p.prompt, "Body here.\n");
|
||||
}
|
||||
|
||||
// ── Malformed YAML ────────────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn malformed_yaml_errors() {
|
||||
let src = "---\n: bad: yaml: here\n---\n";
|
||||
@@ -576,8 +542,6 @@ You are Full Bot.
|
||||
assert!(matches!(err, PersonaError::Yaml(_)));
|
||||
}
|
||||
|
||||
// ── Size limits ───────────────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn frontmatter_too_large_errors() {
|
||||
// Build a frontmatter that exceeds 1 MiB
|
||||
@@ -595,8 +559,6 @@ You are Full Bot.
|
||||
assert!(matches!(err, PersonaError::BodyTooLarge));
|
||||
}
|
||||
|
||||
// ── split_model ───────────────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn split_model_with_colon() {
|
||||
let (provider, id) = split_model("openai:gpt-4o");
|
||||
@@ -618,8 +580,6 @@ You are Full Bot.
|
||||
assert_eq!(id, "gpt-5:preview");
|
||||
}
|
||||
|
||||
// ── Subscribe three-state semantics (S2) ─────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn parse_subscribe_null_is_none() {
|
||||
let src = "---\nname: bot\ndisplay_name: Bot\ndescription: A bot.\nsubscribe: null\n---\n";
|
||||
@@ -678,8 +638,6 @@ You are Full Bot.
|
||||
);
|
||||
}
|
||||
|
||||
// ── Helpers ───────────────────────────────────────────────────────────
|
||||
|
||||
/// Trim leading newline from indented string literals.
|
||||
fn indoc(s: &str) -> &str {
|
||||
s.strip_prefix('\n').unwrap_or(s)
|
||||
|
||||
@@ -17,8 +17,6 @@ use crate::merge::TriggersData;
|
||||
use crate::pack::{self, LoadedPack, LoadedPersona, PackError};
|
||||
use crate::persona::split_model;
|
||||
|
||||
// ── Public types ──────────────────────────────────────────────────────────────
|
||||
|
||||
/// A fully resolved persona — ready for ACP consumption.
|
||||
/// All merge, composition, and projection is done.
|
||||
#[derive(Debug, Clone)]
|
||||
@@ -99,8 +97,6 @@ pub struct ResolvedPack {
|
||||
pub personas: Vec<ResolvedPersona>,
|
||||
}
|
||||
|
||||
// ── Entry point ───────────────────────────────────────────────────────────────
|
||||
|
||||
/// Load, validate, merge, and resolve a pack directory.
|
||||
///
|
||||
/// Returns a `ResolvedPack` with fully typed, ACP-ready output for each
|
||||
@@ -193,8 +189,6 @@ pub fn resolve_persona_by_name(pack_dir: &Path, name: &str) -> Result<ResolvedPe
|
||||
.ok_or_else(|| PackError::PersonaNotFound(pack_dir.join(name)))
|
||||
}
|
||||
|
||||
// ── Per-persona resolution ────────────────────────────────────────────────────
|
||||
|
||||
fn resolve_one_persona(
|
||||
lp: &LoadedPersona,
|
||||
pack_version: &str,
|
||||
@@ -250,8 +244,6 @@ fn resolve_one_persona(
|
||||
}
|
||||
}
|
||||
|
||||
// ── Compose prompt ────────────────────────────────────────────────────────────
|
||||
|
||||
/// Compose the effective system prompt: persona body + pack instructions.
|
||||
fn compose_prompt(persona_prompt: &str, pack_instructions: Option<&str>) -> String {
|
||||
match pack_instructions {
|
||||
@@ -262,8 +254,6 @@ fn compose_prompt(persona_prompt: &str, pack_instructions: Option<&str>) -> Stri
|
||||
}
|
||||
}
|
||||
|
||||
// ── Triggers resolution ───────────────────────────────────────────────────────
|
||||
|
||||
/// Convert `TriggersData` to `ResolvedTriggers`.
|
||||
fn resolve_triggers(rt: Option<&TriggersData>) -> ResolvedTriggers {
|
||||
match rt {
|
||||
@@ -280,8 +270,6 @@ fn resolve_triggers(rt: Option<&TriggersData>) -> ResolvedTriggers {
|
||||
}
|
||||
}
|
||||
|
||||
// ── MCP server merge ──────────────────────────────────────────────────────────
|
||||
|
||||
/// Merge pack-level shared MCP servers with per-persona servers.
|
||||
///
|
||||
/// Pack shared servers come from `.mcp.json` (a map of `name → config`).
|
||||
@@ -352,8 +340,6 @@ fn parse_mcp_server_config(name: &str, config: &serde_json::Value) -> Option<Res
|
||||
})
|
||||
}
|
||||
|
||||
// ── Hooks resolution ──────────────────────────────────────────────────────────
|
||||
|
||||
/// Store hook paths as raw relative strings (no path resolution).
|
||||
///
|
||||
/// Security: we intentionally do NOT resolve these to absolute paths.
|
||||
@@ -373,8 +359,6 @@ fn resolve_hooks(hooks: Option<&crate::merge::HooksData>) -> Option<ResolvedHook
|
||||
})
|
||||
}
|
||||
|
||||
// ── Env var projection ────────────────────────────────────────────────────────
|
||||
|
||||
/// Project persona config into agent subprocess env vars.
|
||||
///
|
||||
/// Pure function — does NOT read the current process env.
|
||||
@@ -416,8 +400,6 @@ fn runtime_env_vars(persona: &LoadedPersona) -> Vec<(String, String)> {
|
||||
vars
|
||||
}
|
||||
|
||||
// ── Tests ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -425,8 +407,6 @@ mod tests {
|
||||
|
||||
use crate::merge::{HooksData, TriggersData};
|
||||
|
||||
// ── compose_prompt ────────────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn compose_prompt_body_only() {
|
||||
let result = compose_prompt("You are a bot.", None);
|
||||
@@ -447,8 +427,6 @@ mod tests {
|
||||
assert_eq!(result, "You are a bot.");
|
||||
}
|
||||
|
||||
// ── resolve_triggers ──────────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn triggers_from_triggers_data() {
|
||||
let data = TriggersData {
|
||||
@@ -470,8 +448,6 @@ mod tests {
|
||||
assert!(!t.all_messages);
|
||||
}
|
||||
|
||||
// ── merge_mcp_servers ─────────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn mcp_merge_shared_only() {
|
||||
let shared = serde_json::json!({
|
||||
@@ -552,8 +528,6 @@ mod tests {
|
||||
assert_eq!(env["SECRET"], "${MY_SECRET}");
|
||||
}
|
||||
|
||||
// ── resolve_hooks ─────────────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn hooks_stored_as_raw_relative_paths() {
|
||||
// Security: hooks are stored as raw strings, NOT resolved to absolute.
|
||||
@@ -583,8 +557,6 @@ mod tests {
|
||||
assert!(resolve_hooks(None).is_none());
|
||||
}
|
||||
|
||||
// ── runtime_env_vars ──────────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn env_vars_projected_from_model() {
|
||||
let lp = stub_persona(Some("anthropic:claude-sonnet-4-20250514"), None, None);
|
||||
@@ -668,8 +640,6 @@ mod tests {
|
||||
assert!(!map.contains_key("BUZZ_AGENT_PROVIDER"));
|
||||
}
|
||||
|
||||
// ── Full pipeline (resolve_pack via filesystem) ───────────────────────
|
||||
|
||||
#[test]
|
||||
fn resolve_minimal_pack() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
@@ -820,8 +790,6 @@ mod tests {
|
||||
assert!(lep.thread_replies);
|
||||
}
|
||||
|
||||
// ── resolve_persona_by_name ──────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn resolve_persona_by_name_found() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
@@ -872,8 +840,6 @@ mod tests {
|
||||
assert!(matches!(err, PackError::PersonaNotFound(_)));
|
||||
}
|
||||
|
||||
// ── model split ───────────────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn model_split_provider_and_id() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
@@ -922,8 +888,6 @@ mod tests {
|
||||
assert!(p.llm_provider.is_none());
|
||||
}
|
||||
|
||||
// ── Test helpers ──────────────────────────────────────────────────────
|
||||
|
||||
fn stub_persona(
|
||||
model: Option<&str>,
|
||||
temperature: Option<f64>,
|
||||
|
||||
@@ -14,8 +14,6 @@ use std::path::Path;
|
||||
|
||||
use crate::pack;
|
||||
|
||||
// ── Diagnostics ──────────────────────────────────────────────────────────────
|
||||
|
||||
/// A single validation finding.
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum ValidationDiagnostic {
|
||||
@@ -97,8 +95,6 @@ impl std::fmt::Display for ValidationReport {
|
||||
}
|
||||
}
|
||||
|
||||
// ── Known field sets ─────────────────────────────────────────────────────────
|
||||
|
||||
/// Known top-level keys in `plugin.json`.
|
||||
const KNOWN_MANIFEST_KEYS: &[&str] = &[
|
||||
// OPS standard fields
|
||||
@@ -136,8 +132,6 @@ const KNOWN_BEHAVIORAL_KEYS: &[&str] = &[
|
||||
/// Valid sub-keys in `respond_to`.
|
||||
const KNOWN_RESPOND_TO_KEYS: &[&str] = &["mentions", "keywords", "all_messages"];
|
||||
|
||||
// ── Pack validation ──────────────────────────────────────────────────────────
|
||||
|
||||
/// Validate a persona pack directory.
|
||||
///
|
||||
/// Step 1: delegate all structural validation to `load_pack()`. If loading
|
||||
@@ -169,8 +163,6 @@ pub fn validate_pack(pack_dir: &Path) -> ValidationReport {
|
||||
report
|
||||
}
|
||||
|
||||
// ── Semantic: persona-level checks ───────────────────────────────────────────
|
||||
|
||||
/// Validate a persona `name` field: `[a-zA-Z0-9_-]+`, max 64 chars.
|
||||
fn validate_persona_name(name: &str, report: &mut ValidationReport) {
|
||||
const MAX_NAME_LEN: usize = 64;
|
||||
@@ -210,8 +202,6 @@ fn semantic_check_personas(loaded: &pack::LoadedPack, report: &mut ValidationRep
|
||||
}
|
||||
}
|
||||
|
||||
// ── Advisory: respond_to type validation ────────────────────────────────────
|
||||
|
||||
/// Check `defaults.respond_to` sub-key types in the raw `plugin.json`.
|
||||
///
|
||||
/// The typed parser (serde) already rejects wrong types in persona frontmatter,
|
||||
@@ -307,8 +297,6 @@ fn value_type_name(v: &serde_json::Value) -> &'static str {
|
||||
}
|
||||
}
|
||||
|
||||
// ── Advisory: unknown manifest keys ─────────────────────────────────────────
|
||||
|
||||
/// Check `plugin.json` for unknown top-level keys and unknown keys in
|
||||
/// `defaults` / `defaults.respond_to`. Emits warnings (likely typos).
|
||||
fn advisory_check_manifest_keys(pack_dir: &Path, report: &mut ValidationReport) {
|
||||
@@ -361,8 +349,6 @@ fn advisory_check_manifest_keys(pack_dir: &Path, report: &mut ValidationReport)
|
||||
}
|
||||
}
|
||||
|
||||
// ── Advisory: skill naming conventions ──────────────────────────────────────
|
||||
|
||||
/// For each skill directory referenced by a loaded persona, check that the
|
||||
/// SKILL.md `name:` field matches the directory name. Emits warnings.
|
||||
fn advisory_check_skill_names(
|
||||
@@ -447,14 +433,10 @@ fn advisory_check_skill_names(
|
||||
}
|
||||
}
|
||||
|
||||
// ── Tests ────────────────────────────────────────────────────────────────────
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
// ── ValidationReport unit tests ──────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn exit_code_clean() {
|
||||
let report = ValidationReport::default();
|
||||
@@ -475,8 +457,6 @@ mod tests {
|
||||
assert_eq!(report.exit_code(), 2);
|
||||
}
|
||||
|
||||
// ── Filesystem integration tests ─────────────────────────────────────────
|
||||
|
||||
/// Minimal valid pack: load succeeds, no advisory issues.
|
||||
#[test]
|
||||
fn validate_pack_minimal_valid() {
|
||||
@@ -805,8 +785,6 @@ mod tests {
|
||||
assert!(msg.contains("code_review"), "got: {msg}");
|
||||
}
|
||||
|
||||
// ── Semantic: zero-persona and duplicate-name checks ─────────────────────
|
||||
|
||||
/// Zero personas in manifest → hard error.
|
||||
#[test]
|
||||
fn validate_zero_personas_error() {
|
||||
@@ -909,8 +887,6 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
// ── Semantic: name character and length validation ────────────────────────
|
||||
|
||||
/// Persona name with spaces or slashes → hard error.
|
||||
#[test]
|
||||
fn validate_name_invalid_chars() {
|
||||
@@ -968,8 +944,6 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
// ── Advisory: respond_to type validation ─────────────────────────────────
|
||||
|
||||
/// respond_to with wrong types in defaults → caught by typed parser.
|
||||
/// The manifest's BehavioralDefaults uses typed RespondTo, so serde_json
|
||||
/// rejects wrong types during load_pack(). This surfaces as a load error.
|
||||
|
||||
@@ -12,8 +12,6 @@ use std::fs;
|
||||
|
||||
use buzz_persona::resolve::resolve_pack;
|
||||
|
||||
// ── Import filter (replicates desktop crate logic) ───────────────────────────
|
||||
|
||||
const DERIVED_PROVIDER_MODEL_ENV_KEYS: &[&str] = &[
|
||||
"GOOSE_MODEL",
|
||||
"GOOSE_PROVIDER",
|
||||
@@ -32,8 +30,6 @@ fn filter_derived(env_vars: Vec<(String, String)>) -> BTreeMap<String, String> {
|
||||
.collect()
|
||||
}
|
||||
|
||||
// ── Test 1: Goose persona emits correct runtime env vars ─────────────────────
|
||||
|
||||
#[test]
|
||||
fn resolve_pack_goose_persona_emits_correct_runtime_env_vars() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
@@ -94,8 +90,6 @@ You are a test bot.
|
||||
);
|
||||
}
|
||||
|
||||
// ── Test 2: Buzz-agent persona emits BUZZ_AGENT_* vars ───────────────────
|
||||
|
||||
#[test]
|
||||
fn resolve_pack_buzz_agent_persona_emits_buzz_agent_vars() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
@@ -161,8 +155,6 @@ You are a test bot.
|
||||
);
|
||||
}
|
||||
|
||||
// ── Test 3: Import filter strips derived keys, preserves knobs ───────────────
|
||||
|
||||
#[test]
|
||||
fn import_filter_strips_derived_preserves_knobs() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
@@ -221,8 +213,6 @@ You are a test bot.
|
||||
);
|
||||
}
|
||||
|
||||
// ── Test 4: Two runtimes in one pack get different env var prefixes ───────────
|
||||
|
||||
#[test]
|
||||
fn full_pipeline_two_runtimes_different_env_vars() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
@@ -327,8 +317,6 @@ You are a buzz bot.
|
||||
);
|
||||
}
|
||||
|
||||
// ── Test 5: Model without provider prefix emits model only ───────────────────
|
||||
|
||||
#[test]
|
||||
fn model_without_provider_prefix_emits_model_only() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
|
||||
@@ -12,8 +12,6 @@ use buzz_persona::persona;
|
||||
use buzz_persona::resolve;
|
||||
use buzz_persona::validate;
|
||||
|
||||
// ── Helpers ──────────────────────────────────────────────────────────────────
|
||||
|
||||
/// Create a minimal valid pack in a temp directory.
|
||||
/// Returns the temp dir (holds the lifetime) and the pack root path.
|
||||
fn create_test_pack(dir: &Path) {
|
||||
@@ -117,8 +115,6 @@ When asked to review code, follow these steps...
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
// ── Full pipeline: load → parse → validate ───────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn full_pipeline_load_and_validate() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
@@ -220,8 +216,6 @@ fn full_pipeline_load_and_validate() {
|
||||
);
|
||||
}
|
||||
|
||||
// ── Persona parser round-trip ────────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn persona_parse_round_trip() {
|
||||
let md = r###"---
|
||||
@@ -266,8 +260,6 @@ You are a test agent. Be precise and thorough.
|
||||
assert_eq!(rt.all_messages, Some(false));
|
||||
}
|
||||
|
||||
// ── Validation catches real errors ───────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn validation_catches_missing_required_fields() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
@@ -344,8 +336,6 @@ fn validation_catches_unknown_behavioral_keys() {
|
||||
);
|
||||
}
|
||||
|
||||
// ── model string splitting ───────────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn model_split_cases() {
|
||||
// provider:model
|
||||
@@ -364,8 +354,6 @@ fn model_split_cases() {
|
||||
assert_eq!(model, "my:model:v2");
|
||||
}
|
||||
|
||||
// ── Defaults merge: persona overrides pack defaults ──────────────────────────
|
||||
|
||||
#[test]
|
||||
fn defaults_merge_persona_overrides() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
@@ -395,8 +383,6 @@ fn defaults_merge_persona_overrides() {
|
||||
assert_eq!(lep.max_context_tokens, Some(128000));
|
||||
}
|
||||
|
||||
// ── Resolve pipeline: full end-to-end ────────────────────────────────────────
|
||||
|
||||
/// Build a pack on disk → resolve → verify all fields on each persona.
|
||||
#[test]
|
||||
fn resolve_full_pipeline() {
|
||||
@@ -590,8 +576,6 @@ fn resolve_persona_by_name_not_found() {
|
||||
);
|
||||
}
|
||||
|
||||
// ── Validation: zero-persona and duplicate-name in full pipeline ─────────────
|
||||
|
||||
/// Validation catches zero-persona packs in the full pipeline.
|
||||
#[test]
|
||||
fn validate_zero_personas_in_pipeline() {
|
||||
|
||||
@@ -11,8 +11,6 @@ use serde::Deserialize;
|
||||
use tracing::info;
|
||||
use uuid::Uuid;
|
||||
|
||||
// ─── DTOs ────────────────────────────────────────────────────────────────────
|
||||
|
||||
/// Minimal DTO for deserializing `GET /api/channels` response.
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct ChannelDto {
|
||||
@@ -32,8 +30,6 @@ pub struct ChannelDto {
|
||||
pub created_by: String,
|
||||
}
|
||||
|
||||
// ─── ChannelInfo ─────────────────────────────────────────────────────────────
|
||||
|
||||
/// All relevant information about a mapped channel.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ChannelInfo {
|
||||
@@ -51,8 +47,6 @@ pub struct ChannelInfo {
|
||||
pub created_at_unix: u64,
|
||||
}
|
||||
|
||||
// ─── ChannelMap ──────────────────────────────────────────────────────────────
|
||||
|
||||
/// Bidirectional UUID ↔ kind:40 event ID map.
|
||||
///
|
||||
/// Thread-safe via [`DashMap`]; clone-friendly via `Arc` wrapping at the call
|
||||
@@ -63,8 +57,6 @@ pub struct ChannelMap {
|
||||
server_keys: Keys,
|
||||
}
|
||||
|
||||
// ─── Synthesis helpers ───────────────────────────────────────────────────────
|
||||
|
||||
impl ChannelMap {
|
||||
/// Synthesize a deterministic NIP-28 kind:40 channel creation event.
|
||||
///
|
||||
@@ -118,8 +110,6 @@ impl ChannelMap {
|
||||
}
|
||||
}
|
||||
|
||||
// ─── Core impl ───────────────────────────────────────────────────────────────
|
||||
|
||||
impl ChannelMap {
|
||||
/// Create an empty [`ChannelMap`] with the given server signing keys.
|
||||
pub fn new(server_keys: Keys) -> Self {
|
||||
@@ -167,8 +157,6 @@ impl ChannelMap {
|
||||
Ok(map)
|
||||
}
|
||||
|
||||
// ─── Lookups ─────────────────────────────────────────────────────────────
|
||||
|
||||
/// Look up channel info by Buzz UUID.
|
||||
pub fn lookup_by_uuid(&self, uuid: &Uuid) -> Option<ChannelInfo> {
|
||||
self.by_uuid.get(uuid).map(|r| r.clone())
|
||||
@@ -186,8 +174,6 @@ impl ChannelMap {
|
||||
self.by_uuid.iter().map(|r| r.value().clone()).collect()
|
||||
}
|
||||
|
||||
// ─── Mutation ────────────────────────────────────────────────────────────
|
||||
|
||||
/// Register a new channel (e.g. from a kind:40099 system message) and
|
||||
/// return its [`ChannelInfo`].
|
||||
///
|
||||
@@ -225,8 +211,6 @@ impl ChannelMap {
|
||||
Ok(info)
|
||||
}
|
||||
|
||||
// ─── Accessors ───────────────────────────────────────────────────────────
|
||||
|
||||
/// Number of channels in the map.
|
||||
pub fn len(&self) -> usize {
|
||||
self.by_uuid.len()
|
||||
@@ -243,8 +227,6 @@ impl ChannelMap {
|
||||
}
|
||||
}
|
||||
|
||||
// ─── Tests ───────────────────────────────────────────────────────────────────
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
@@ -14,8 +14,6 @@ use buzz_proxy::shadow_keys::ShadowKeyManager;
|
||||
use buzz_proxy::translate::Translator;
|
||||
use buzz_proxy::upstream::{UpstreamClient, UpstreamEvent};
|
||||
|
||||
// ── Env helpers ───────────────────────────────────────────────────────────────
|
||||
|
||||
fn env_required(name: &str) -> String {
|
||||
std::env::var(name).unwrap_or_else(|_| {
|
||||
eprintln!("error: required environment variable {name} is not set");
|
||||
@@ -27,8 +25,6 @@ fn env_or(name: &str, default: &str) -> String {
|
||||
std::env::var(name).unwrap_or_else(|_| default.to_string())
|
||||
}
|
||||
|
||||
// ── Entry point ───────────────────────────────────────────────────────────────
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
// Init tracing — respects RUST_LOG; falls back to info for buzz_proxy and tower_http.
|
||||
@@ -39,8 +35,6 @@ async fn main() {
|
||||
)
|
||||
.init();
|
||||
|
||||
// ── Parse env ─────────────────────────────────────────────────────────────
|
||||
|
||||
let upstream_url = env_required("BUZZ_UPSTREAM_URL");
|
||||
let bind_addr = env_or("BUZZ_PROXY_BIND_ADDR", "0.0.0.0:4869");
|
||||
let server_key_hex = env_required("BUZZ_PROXY_SERVER_KEY");
|
||||
@@ -55,8 +49,6 @@ async fn main() {
|
||||
}
|
||||
info!(relay_pubkey = %relay_pubkey, "relay pubkey configured for attribution trust");
|
||||
|
||||
// ── Parse server keypair ──────────────────────────────────────────────────
|
||||
|
||||
let server_secret = SecretKey::from_hex(&server_key_hex).unwrap_or_else(|e| {
|
||||
eprintln!("error: invalid BUZZ_PROXY_SERVER_KEY: {e}");
|
||||
std::process::exit(1);
|
||||
@@ -64,28 +56,20 @@ async fn main() {
|
||||
let server_keys = Keys::new(server_secret);
|
||||
info!(pubkey = %server_keys.public_key(), "proxy server keypair loaded");
|
||||
|
||||
// ── Parse salt ────────────────────────────────────────────────────────────
|
||||
|
||||
let salt = hex::decode(&salt_hex).unwrap_or_else(|e| {
|
||||
eprintln!("error: invalid BUZZ_PROXY_SALT (must be hex): {e}");
|
||||
std::process::exit(1);
|
||||
});
|
||||
|
||||
// ── Init shadow key manager ───────────────────────────────────────────────
|
||||
|
||||
let shadow_keys = Arc::new(ShadowKeyManager::new(&salt).unwrap_or_else(|e| {
|
||||
eprintln!("error: shadow key manager init failed: {e}");
|
||||
std::process::exit(1);
|
||||
}));
|
||||
|
||||
// ── Derive HTTP base URL from WS URL for REST API calls ───────────────────
|
||||
|
||||
let api_base = upstream_url
|
||||
.replace("wss://", "https://")
|
||||
.replace("ws://", "http://");
|
||||
|
||||
// ── Init channel map from REST API ────────────────────────────────────────
|
||||
|
||||
info!("initializing channel map from {api_base}/api/channels ...");
|
||||
let channel_map = Arc::new(
|
||||
ChannelMap::init_from_rest(server_keys.clone(), &api_base, &api_token)
|
||||
@@ -97,8 +81,6 @@ async fn main() {
|
||||
);
|
||||
info!(channels = channel_map.len(), "channel map ready");
|
||||
|
||||
// ── Init translator ───────────────────────────────────────────────────────
|
||||
|
||||
let translator = Arc::new(Translator::new(
|
||||
shadow_keys,
|
||||
channel_map.clone(),
|
||||
@@ -107,15 +89,10 @@ async fn main() {
|
||||
relay_pubkey,
|
||||
));
|
||||
|
||||
// ── Init guest store (empty — guests registered via POST /admin/guests) ────
|
||||
|
||||
let guest_store = Arc::new(GuestStore::new());
|
||||
|
||||
// ── Init invite store (empty — tokens created via POST /admin/invite) ─────
|
||||
|
||||
let invite_store = Arc::new(InviteStore::new());
|
||||
|
||||
// ── Init upstream client ──────────────────────────────────────────────────
|
||||
//
|
||||
// UpstreamClient owns its internal outbound channel. The server calls
|
||||
// upstream.send_event() / send_req() / send_close() directly via Arc.
|
||||
@@ -130,15 +107,12 @@ async fn main() {
|
||||
server_keys.clone(),
|
||||
));
|
||||
|
||||
// ── upstream_events broadcast: UpstreamClient → all WebSocket sessions ────
|
||||
|
||||
// upstream_events_tx: upstream → server (broadcast of inbound JSON strings)
|
||||
let (upstream_events_tx, _) = broadcast::channel::<String>(4096);
|
||||
|
||||
// inbound_tx: UpstreamClient → bridge task (UpstreamEvent)
|
||||
let (inbound_tx, mut inbound_rx) = mpsc::channel::<UpstreamEvent>(256);
|
||||
|
||||
// ── Bridge task: UpstreamEvent → broadcast String ─────────────────────────
|
||||
//
|
||||
// The server layer subscribes to `upstream_events_tx` as raw JSON strings.
|
||||
// The UpstreamClient emits typed `UpstreamEvent` values. This task bridges
|
||||
@@ -162,8 +136,6 @@ async fn main() {
|
||||
}
|
||||
});
|
||||
|
||||
// ── Read admin secret from env (optional) ─────────────────────────────────
|
||||
|
||||
let admin_secret = std::env::var("BUZZ_PROXY_ADMIN_SECRET").ok();
|
||||
if admin_secret.is_some() {
|
||||
info!("admin endpoint protected by BUZZ_PROXY_ADMIN_SECRET");
|
||||
@@ -171,8 +143,6 @@ async fn main() {
|
||||
info!("admin endpoint running unauthenticated (dev mode) — set BUZZ_PROXY_ADMIN_SECRET to secure it");
|
||||
}
|
||||
|
||||
// ── Build proxy state ─────────────────────────────────────────────────────
|
||||
|
||||
// Relay URL for NIP-42 relay tag validation. Prefer explicit env var
|
||||
// (e.g. "wss://proxy.example.com") over the derived bind address fallback.
|
||||
let relay_url =
|
||||
@@ -189,12 +159,8 @@ async fn main() {
|
||||
relay_url,
|
||||
};
|
||||
|
||||
// ── Build router ──────────────────────────────────────────────────────────
|
||||
|
||||
let app = server::router(state);
|
||||
|
||||
// ── Bind listener ─────────────────────────────────────────────────────────
|
||||
|
||||
info!("buzz-proxy starting on {bind_addr} → upstream {upstream_url}");
|
||||
|
||||
let listener = tokio::net::TcpListener::bind(&bind_addr)
|
||||
@@ -204,8 +170,6 @@ async fn main() {
|
||||
std::process::exit(1);
|
||||
});
|
||||
|
||||
// ── Run server + upstream concurrently ────────────────────────────────────
|
||||
|
||||
tokio::select! {
|
||||
result = axum::serve(listener, app).with_graceful_shutdown(shutdown_signal()) => {
|
||||
if let Err(e) = result {
|
||||
@@ -220,8 +184,6 @@ async fn main() {
|
||||
info!("buzz-proxy shut down");
|
||||
}
|
||||
|
||||
// ── Graceful shutdown ─────────────────────────────────────────────────────────
|
||||
|
||||
async fn shutdown_signal() {
|
||||
tokio::signal::ctrl_c()
|
||||
.await
|
||||
|
||||
@@ -29,8 +29,6 @@ use crate::invite_store::InviteStore;
|
||||
use crate::translate::Translator;
|
||||
use crate::upstream::UpstreamClient;
|
||||
|
||||
// ─── Shared state ────────────────────────────────────────────────────────────
|
||||
|
||||
/// Shared state injected into every axum handler.
|
||||
#[derive(Clone)]
|
||||
pub struct ProxyState {
|
||||
@@ -56,8 +54,6 @@ pub struct ProxyState {
|
||||
pub relay_url: String,
|
||||
}
|
||||
|
||||
// ─── Router ──────────────────────────────────────────────────────────────────
|
||||
|
||||
/// Query parameters accepted on the root WebSocket endpoint.
|
||||
#[derive(Deserialize)]
|
||||
pub struct WsParams {
|
||||
@@ -88,8 +84,6 @@ pub fn router(state: ProxyState) -> Router {
|
||||
.with_state(state)
|
||||
}
|
||||
|
||||
// ─── Root handler (NIP-11 / WebSocket) ───────────────────────────────────────
|
||||
|
||||
/// Content-negotiate between NIP-11 JSON and WebSocket upgrade.
|
||||
///
|
||||
/// Uses `axum::extract::Request` to manually attempt the WS upgrade so that
|
||||
@@ -142,8 +136,6 @@ fn nip11_response() -> impl IntoResponse {
|
||||
)
|
||||
}
|
||||
|
||||
// ─── Constant-time string comparison ─────────────────────────────────────────
|
||||
|
||||
/// Compare two strings in constant time to prevent timing side-channel attacks.
|
||||
/// Returns `true` only if both strings are identical.
|
||||
///
|
||||
@@ -161,8 +153,6 @@ fn constant_time_eq(a: &str, b: &str) -> bool {
|
||||
== 0
|
||||
}
|
||||
|
||||
// ─── WebSocket handler ───────────────────────────────────────────────────────
|
||||
|
||||
/// Helper: serialize a [`RelayMessage`] and send it over the socket.
|
||||
/// Returns `true` if the send succeeded.
|
||||
async fn send_relay_msg(socket: &mut WebSocket, msg: RelayMessage<'_>) -> bool {
|
||||
@@ -176,7 +166,6 @@ async fn handle_ws(mut socket: WebSocket, state: ProxyState, token: String) {
|
||||
// across clients sharing the single upstream connection.
|
||||
let conn_prefix = uuid::Uuid::new_v4().simple().to_string()[..8].to_string();
|
||||
|
||||
// ── 1. Send NIP-42 AUTH challenge ─────────────────────────────────────
|
||||
// Token validation is deferred until after NIP-42 auth completes.
|
||||
// Registered guests (in GuestStore) don't need a token at all.
|
||||
let challenge = uuid::Uuid::new_v4().to_string();
|
||||
@@ -184,7 +173,6 @@ async fn handle_ws(mut socket: WebSocket, state: ProxyState, token: String) {
|
||||
return;
|
||||
}
|
||||
|
||||
// ── 3. Pre-auth loop: reject pre-auth REQs/EVENTs, wait for AUTH ─────
|
||||
// Returns `(pubkey, channels)` on successful auth, or drops the connection
|
||||
// on timeout / disconnect / invalid auth.
|
||||
let auth_deadline = tokio::time::Instant::now() + std::time::Duration::from_secs(30);
|
||||
@@ -270,7 +258,6 @@ async fn handle_ws(mut socket: WebSocket, state: ProxyState, token: String) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// ── Resolve channel access ────────────────────────────
|
||||
// Priority: GuestStore (pubkey-based) > invite token.
|
||||
let pubkey = auth_event.pubkey;
|
||||
let event_id = auth_event.id;
|
||||
@@ -355,10 +342,8 @@ async fn handle_ws(mut socket: WebSocket, state: ProxyState, token: String) {
|
||||
let mut pending_oks: HashMap<String, EventId> = HashMap::new();
|
||||
let mut active_subs: HashSet<String> = HashSet::new();
|
||||
|
||||
// ── 4. Subscribe to upstream broadcast ────────────────────────────────
|
||||
let mut upstream_rx = state.upstream_events.subscribe();
|
||||
|
||||
// ── 5. Main authenticated message loop ────────────────────────────────
|
||||
loop {
|
||||
tokio::select! {
|
||||
// Inbound from client
|
||||
@@ -491,8 +476,6 @@ async fn handle_ws(mut socket: WebSocket, state: ProxyState, token: String) {
|
||||
debug!(pubkey = %client_pubkey, "client disconnected");
|
||||
}
|
||||
|
||||
// ─── Client message dispatcher ───────────────────────────────────────────────
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
async fn handle_client_message(
|
||||
socket: &mut WebSocket,
|
||||
@@ -602,8 +585,6 @@ async fn handle_client_message(
|
||||
}
|
||||
}
|
||||
|
||||
// ─── Filter splitting (pure, testable) ───────────────────────────────────────
|
||||
|
||||
/// Split a list of NIP-28 filters into local (kind:40/41) and upstream groups.
|
||||
///
|
||||
/// **Routing rules:**
|
||||
@@ -751,8 +732,6 @@ fn collect_local_events(
|
||||
events
|
||||
}
|
||||
|
||||
// ─── REQ handler ─────────────────────────────────────────────────────────────
|
||||
|
||||
async fn handle_req(
|
||||
socket: &mut WebSocket,
|
||||
state: &ProxyState,
|
||||
@@ -804,8 +783,6 @@ async fn handle_req(
|
||||
}
|
||||
}
|
||||
|
||||
// ─── Admin: create invite token ───────────────────────────────────────────────
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct CreateInviteRequest {
|
||||
/// Comma-separated channel UUIDs this token grants access to.
|
||||
@@ -883,8 +860,6 @@ async fn create_invite(
|
||||
.into_response()
|
||||
}
|
||||
|
||||
// ─── Admin: check secret helper ───────────────────────────────────────────────
|
||||
|
||||
/// Verify the admin secret from the Authorization header. Returns an error
|
||||
/// response if the secret is required but missing/wrong, or `None` if OK.
|
||||
fn check_admin_secret(admin_secret: &Option<String>, headers: &HeaderMap) -> Option<Response> {
|
||||
@@ -910,8 +885,6 @@ fn check_admin_secret(admin_secret: &Option<String>, headers: &HeaderMap) -> Opt
|
||||
}
|
||||
}
|
||||
|
||||
// ─── Admin: guest registration ────────────────────────────────────────────────
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct RegisterGuestRequest {
|
||||
/// Hex-encoded Nostr public key (64 chars).
|
||||
@@ -1034,8 +1007,6 @@ async fn list_guests(State(state): State<ProxyState>, headers: HeaderMap) -> imp
|
||||
.into_response()
|
||||
}
|
||||
|
||||
// ─── Tests ────────────────────────────────────────────────────────────────────
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -1103,8 +1074,6 @@ mod tests {
|
||||
assert!(!constant_time_eq("abc", "abcd"));
|
||||
}
|
||||
|
||||
// ── split_filters tests ──────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn split_filters_pure_local() {
|
||||
// kind:40 is the only pure-local kind (channel creation is synthesized).
|
||||
@@ -1183,8 +1152,6 @@ mod tests {
|
||||
assert!(upstream[0].kinds.is_none());
|
||||
}
|
||||
|
||||
// ── collect_local_events tests ───────────────────────────────────────
|
||||
|
||||
fn make_channel_map_with_channel() -> (Arc<ChannelMap>, Uuid) {
|
||||
let keys = Keys::generate();
|
||||
let map = ChannelMap::new(keys);
|
||||
|
||||
@@ -30,8 +30,6 @@ use crate::kind_translator::KindTranslator;
|
||||
use crate::shadow_keys::ShadowKeyManager;
|
||||
use crate::ProxyError;
|
||||
|
||||
// ─── Translator ──────────────────────────────────────────────────────────────
|
||||
|
||||
/// Translates events and filters between Buzz internal format and NIP-28
|
||||
/// standard format.
|
||||
///
|
||||
@@ -89,8 +87,6 @@ impl Translator {
|
||||
}
|
||||
}
|
||||
|
||||
// ─── Outbound translation (Buzz → NIP-28 client) ───────────────────────────
|
||||
|
||||
impl Translator {
|
||||
fn cache_event_mapping(&self, internal_event_id: &str, external_event_id: &str) {
|
||||
self.internal_to_external_event_ids
|
||||
@@ -394,8 +390,6 @@ impl Translator {
|
||||
}
|
||||
}
|
||||
|
||||
// ─── Inbound translation (NIP-28 client → Buzz) ────────────────────────────
|
||||
|
||||
impl Translator {
|
||||
/// Translate a NIP-28 event from an external client into Buzz format.
|
||||
///
|
||||
@@ -662,8 +656,6 @@ impl Translator {
|
||||
}
|
||||
}
|
||||
|
||||
// ─── Filter translation ───────────────────────────────────────────────────────
|
||||
|
||||
impl Translator {
|
||||
/// Translate a NIP-28 REQ filter to Buzz format.
|
||||
///
|
||||
@@ -745,8 +737,6 @@ impl Translator {
|
||||
}
|
||||
}
|
||||
|
||||
// ─── Helpers ─────────────────────────────────────────────────────────────────
|
||||
|
||||
/// Extract plain text from V2 rich content JSON.
|
||||
///
|
||||
/// V2 content is a JSON object with a `"text"` field. Falls back to the raw
|
||||
@@ -758,16 +748,12 @@ fn extract_plain_text(content: &str) -> String {
|
||||
.unwrap_or_else(|| content.to_string())
|
||||
}
|
||||
|
||||
// ─── Tests ────────────────────────────────────────────────────────────────────
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::channel_map::{ChannelDto, ChannelMap};
|
||||
use buzz_core::kind::KIND_STREAM_MESSAGE;
|
||||
|
||||
// ── Test fixtures ────────────────────────────────────────────────────────
|
||||
|
||||
const TEST_UUID: &str = "550e8400-e29b-41d4-a716-446655440000";
|
||||
const TEST_SALT: &[u8] = b"test-salt-for-translate-tests";
|
||||
|
||||
@@ -815,8 +801,6 @@ mod tests {
|
||||
vec![]
|
||||
}
|
||||
|
||||
// ── Test 1: Outbound — kind:9 + #h → kind:42 + #e ───────────────────
|
||||
|
||||
#[tokio::test]
|
||||
async fn outbound_translates_stream_message() {
|
||||
let (translator, kind40_event_id) = make_translator();
|
||||
@@ -865,8 +849,6 @@ mod tests {
|
||||
.expect("translated event signature must be valid");
|
||||
}
|
||||
|
||||
// ── Test 2: Inbound — kind:42 + #e → kind:9 + #h ───────────────────
|
||||
|
||||
#[test]
|
||||
fn inbound_translates_channel_message() {
|
||||
let (translator, kind40_event_id) = make_translator();
|
||||
@@ -917,8 +899,6 @@ mod tests {
|
||||
.expect("translated event signature must be valid");
|
||||
}
|
||||
|
||||
// ── Test 3: Outbound — channel not in allowed_channels → PermissionDenied
|
||||
|
||||
#[tokio::test]
|
||||
async fn outbound_rejects_channel_not_in_scope() {
|
||||
let (translator, _) = make_translator();
|
||||
@@ -941,8 +921,6 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
// ── Test 4: Inbound — channel not in allowed_channels → PermissionDenied
|
||||
|
||||
#[test]
|
||||
fn inbound_rejects_channel_not_in_scope() {
|
||||
let (translator, kind40_event_id) = make_translator();
|
||||
@@ -964,8 +942,6 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
// ── Test 5: V2 content extraction ────────────────────────────────────────
|
||||
|
||||
#[tokio::test]
|
||||
async fn v2_content_plain_text_extracted() {
|
||||
// extract_plain_text is a private helper; test it indirectly via outbound.
|
||||
@@ -998,8 +974,6 @@ mod tests {
|
||||
assert_eq!(content, "not json at all");
|
||||
}
|
||||
|
||||
// ── Test 6: Filter translation — kind:42 → kind:9 ───────────────────
|
||||
|
||||
#[test]
|
||||
fn filter_inbound_translates_kind() {
|
||||
let (translator, _) = make_translator();
|
||||
@@ -1024,8 +998,6 @@ mod tests {
|
||||
assert!(has_h_filter, "filter must have #h tag constraints injected");
|
||||
}
|
||||
|
||||
// ── Test 6b: Filter — #e channel ref translates to #h UUID (FIX A) ─────
|
||||
|
||||
#[test]
|
||||
fn filter_inbound_translates_e_tag_to_h() {
|
||||
let (translator, kind40_event_id) = make_translator();
|
||||
@@ -1068,8 +1040,6 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
// ── Test 6c: Filter — #e with unknown event ID falls back to allowed_channels
|
||||
|
||||
#[test]
|
||||
fn filter_inbound_e_tag_unknown_event_id_denies_all() {
|
||||
let (translator, _) = make_translator();
|
||||
@@ -1110,8 +1080,6 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
// ── Test 7: Inbound — reply #e tags are preserved (FIX 1) ───────────────
|
||||
|
||||
#[test]
|
||||
fn inbound_preserves_reply_e_tags() {
|
||||
let (translator, kind40_event_id) = make_translator();
|
||||
@@ -1165,8 +1133,6 @@ mod tests {
|
||||
.expect("translated event signature must be valid");
|
||||
}
|
||||
|
||||
// ── Test 8: Outbound — non-channel #h tags are preserved (FIX 2) ────────
|
||||
|
||||
#[tokio::test]
|
||||
async fn outbound_preserves_non_channel_h_tags() {
|
||||
let (translator, kind40_event_id) = make_translator();
|
||||
@@ -1217,8 +1183,6 @@ mod tests {
|
||||
.expect("translated event signature must be valid");
|
||||
}
|
||||
|
||||
// ── Test 9: Filter — empty allowed_channels injects deny-all (FIX 3) ────
|
||||
|
||||
#[test]
|
||||
fn empty_allowed_channels_denies_all() {
|
||||
let (translator, _) = make_translator();
|
||||
@@ -1241,8 +1205,6 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
// ── Test 10: Inbound — kind:41 (edit) → kind:40003 ─────────────────────
|
||||
|
||||
#[test]
|
||||
fn inbound_translates_edit_message() {
|
||||
use buzz_core::kind::KIND_STREAM_MESSAGE_EDIT;
|
||||
@@ -1284,8 +1246,6 @@ mod tests {
|
||||
.expect("translated edit signature must be valid");
|
||||
}
|
||||
|
||||
// ── Test 11: Inbound — rejects unknown kinds ─────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn inbound_rejects_unknown_kind() {
|
||||
let (translator, kind40_event_id) = make_translator();
|
||||
@@ -1302,8 +1262,6 @@ mod tests {
|
||||
assert!(result.is_err(), "kind:9999 must be rejected inbound");
|
||||
}
|
||||
|
||||
// ── Test 12: Outbound — kind:40003 (edit) → kind:41 (FIX 4) ─────────────
|
||||
|
||||
#[tokio::test]
|
||||
async fn outbound_translates_edit_message() {
|
||||
use buzz_core::kind::KIND_STREAM_MESSAGE_EDIT;
|
||||
@@ -1390,8 +1348,6 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
// ── Test 15: Outbound — unknown kinds are dropped (not leaked) ───────
|
||||
|
||||
#[tokio::test]
|
||||
async fn outbound_drops_unknown_kinds() {
|
||||
let (translator, _) = make_translator();
|
||||
|
||||
@@ -13,8 +13,6 @@ use tokio::sync::{mpsc, RwLock};
|
||||
use tokio_tungstenite::tungstenite::Message as WsMessage;
|
||||
use tracing::{debug, error, info, warn};
|
||||
|
||||
// ── Public types ─────────────────────────────────────────────────────────────
|
||||
|
||||
/// Messages forwarded from the upstream relay to the server layer.
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum UpstreamEvent {
|
||||
@@ -26,8 +24,6 @@ pub enum UpstreamEvent {
|
||||
Connected,
|
||||
}
|
||||
|
||||
// ── UpstreamClient ────────────────────────────────────────────────────────────
|
||||
|
||||
/// Inner state shared across the `Arc`. Kept separate so `Arc<Inner>` can be
|
||||
/// moved into `'static` spawned tasks without capturing `&self`.
|
||||
struct Inner {
|
||||
@@ -111,8 +107,6 @@ impl UpstreamClient {
|
||||
}
|
||||
}
|
||||
|
||||
// ── Send helpers ──────────────────────────────────────────────────────────
|
||||
|
||||
/// Send an event to the upstream relay.
|
||||
pub async fn send_event(&self, event: Event) -> Result<(), crate::ProxyError> {
|
||||
let msg = ClientMessage::event(event).as_json();
|
||||
@@ -155,8 +149,6 @@ impl UpstreamClient {
|
||||
self.inner.connected.try_read().map(|v| *v).unwrap_or(false)
|
||||
}
|
||||
|
||||
// ── Run loop ──────────────────────────────────────────────────────────────
|
||||
|
||||
/// Run the upstream connection loop. Reconnects on disconnect with exponential
|
||||
/// backoff (1 → 2 → 4 → … → 30 seconds).
|
||||
///
|
||||
@@ -185,8 +177,6 @@ impl UpstreamClient {
|
||||
}
|
||||
}
|
||||
|
||||
// ── Internal: single connection attempt ──────────────────────────────────
|
||||
|
||||
/// Establish one WebSocket connection, authenticate, and pump messages until
|
||||
/// the socket closes or an error occurs.
|
||||
async fn connect_once(
|
||||
@@ -255,7 +245,6 @@ impl UpstreamClient {
|
||||
}
|
||||
}
|
||||
|
||||
// ── Free function: read loop ──────────────────────────────────────────────────
|
||||
//
|
||||
// Extracted as a free function so it does not capture `&self` — it receives
|
||||
// only the `Arc<Inner>` it needs, which is `'static`.
|
||||
@@ -287,7 +276,6 @@ where
|
||||
};
|
||||
|
||||
match relay_msg {
|
||||
// ── NIP-42 AUTH challenge ────────────────────────────────
|
||||
RelayMessage::Auth { ref challenge } => {
|
||||
debug!("received AUTH challenge: {challenge}");
|
||||
match respond_to_auth_challenge(challenge, &inner, write_tx).await {
|
||||
@@ -299,7 +287,6 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
// ── OK response — check if it's for our auth event ───────
|
||||
RelayMessage::Ok {
|
||||
event_id,
|
||||
ref status,
|
||||
@@ -360,7 +347,6 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
// ── All other messages → forward downstream ──────────────
|
||||
_other => {
|
||||
if inbound_tx
|
||||
.send(UpstreamEvent::RelayMessage(text_str.to_string()))
|
||||
@@ -397,8 +383,6 @@ where
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// ── Free function: NIP-42 auth response ──────────────────────────────────────
|
||||
|
||||
/// Build and send a NIP-42 kind:22242 auth event in response to a challenge.
|
||||
/// Stores the auth event's ID in `inner.auth_event_id` so the read loop can
|
||||
/// correlate the OK response to this specific event.
|
||||
@@ -435,8 +419,6 @@ async fn respond_to_auth_challenge(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// ── Tests ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
@@ -0,0 +1,160 @@
|
||||
//! Cross-pod cache-key invalidation over Redis pub/sub.
|
||||
//!
|
||||
//! Each relay pod keeps in-memory (moka) membership / accessible-channels /
|
||||
//! visibility caches. A membership or visibility change is applied to the local
|
||||
//! caches only on the pod that processed the write; other pods would otherwise
|
||||
//! rely on the 10s TTL to expire stale entries. This module carries the same
|
||||
//! key drops to every pod immediately.
|
||||
//!
|
||||
//! The message is a pure cache-key drop — never an "evict these subscriptions"
|
||||
//! payload. The per-event access gate (`filter_fanout_by_access`) is the
|
||||
//! universal delivery-enforcement point, so dropping the stale key is
|
||||
//! sufficient: the next read re-fetches authoritative state from the DB.
|
||||
|
||||
use futures_util::StreamExt;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use tokio::sync::broadcast;
|
||||
use uuid::Uuid;
|
||||
|
||||
/// Redis pub/sub channel for cache-invalidation messages. Distinct from the
|
||||
/// `buzz:channel:*` event topic so the two streams never interfere.
|
||||
pub const CACHE_INVALIDATION_CHANNEL: &str = "buzz:cache-invalidate";
|
||||
|
||||
/// A cache-key drop to apply on every pod. Each variant mirrors exactly one of
|
||||
/// the relay's local `invalidate_*` operations.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
||||
#[serde(tag = "op")]
|
||||
pub enum CacheInvalidation {
|
||||
/// Drop the `(channel_id, pubkey)` membership entry and the user's
|
||||
/// accessible-channels entry. Mirrors `invalidate_membership`.
|
||||
Membership {
|
||||
/// Channel whose membership changed.
|
||||
channel_id: Uuid,
|
||||
/// Affected member's pubkey bytes.
|
||||
pubkey: Vec<u8>,
|
||||
},
|
||||
/// Drop every user's accessible-channels entry. Mirrors
|
||||
/// `invalidate_all_accessible_channels` (e.g. a new open channel).
|
||||
AccessibleAll,
|
||||
/// Drop the cached visibility for a single channel. Mirrors
|
||||
/// `invalidate_channel_visibility` (e.g. an open→private flip).
|
||||
Visibility {
|
||||
/// Channel whose visibility changed.
|
||||
channel_id: Uuid,
|
||||
},
|
||||
/// Drop all membership / accessible / visibility caches. Mirrors
|
||||
/// `invalidate_channel_deleted`.
|
||||
ChannelDeleted,
|
||||
}
|
||||
|
||||
/// Initial reconnect backoff (1 second).
|
||||
const BACKOFF_INITIAL_SECS: u64 = 1;
|
||||
/// Maximum reconnect backoff (30 seconds).
|
||||
const BACKOFF_MAX_SECS: u64 = 30;
|
||||
|
||||
/// Subscribes to `buzz:cache-invalidate` and forwards drops to the broadcast.
|
||||
///
|
||||
/// Mirrors `subscriber::run_subscriber`: a reconnect loop with exponential
|
||||
/// backoff (1s → 2s → 4s → … → 30s max). Never returns — runs for the lifetime
|
||||
/// of the relay.
|
||||
pub async fn run_cache_invalidation_subscriber(
|
||||
redis_url: String,
|
||||
broadcast_tx: broadcast::Sender<CacheInvalidation>,
|
||||
) {
|
||||
let mut backoff_secs = BACKOFF_INITIAL_SECS;
|
||||
|
||||
loop {
|
||||
match connect_and_subscribe(&redis_url, &broadcast_tx).await {
|
||||
Ok(()) => {
|
||||
backoff_secs = BACKOFF_INITIAL_SECS;
|
||||
tracing::warn!(
|
||||
"Redis cache-invalidation stream ended (clean disconnect) — reconnecting in {backoff_secs}s"
|
||||
);
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::error!(
|
||||
"Redis cache-invalidation error: {e} — reconnecting in {backoff_secs}s"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
tokio::time::sleep(tokio::time::Duration::from_secs(backoff_secs)).await;
|
||||
backoff_secs = (backoff_secs * 2).min(BACKOFF_MAX_SECS);
|
||||
|
||||
tracing::info!("Attempting to reconnect to Redis cache-invalidation...");
|
||||
}
|
||||
}
|
||||
|
||||
async fn connect_and_subscribe(
|
||||
redis_url: &str,
|
||||
broadcast_tx: &broadcast::Sender<CacheInvalidation>,
|
||||
) -> Result<(), redis::RedisError> {
|
||||
let client = redis::Client::open(redis_url)?;
|
||||
let mut conn = client.get_async_pubsub().await?;
|
||||
|
||||
conn.subscribe(CACHE_INVALIDATION_CHANNEL).await?;
|
||||
|
||||
tracing::info!(
|
||||
"Redis cache-invalidation subscriber connected — listening on {CACHE_INVALIDATION_CHANNEL}"
|
||||
);
|
||||
|
||||
let mut stream = conn.on_message();
|
||||
while let Some(msg) = stream.next().await {
|
||||
let payload: String = match msg.get_payload() {
|
||||
Ok(p) => p,
|
||||
Err(e) => {
|
||||
tracing::warn!("Failed to get cache-invalidation payload: {e}");
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
let invalidation: CacheInvalidation = match serde_json::from_str(&payload) {
|
||||
Ok(v) => v,
|
||||
Err(e) => {
|
||||
tracing::warn!("Failed to deserialize cache-invalidation message: {e}");
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
if broadcast_tx.send(invalidation).is_err() {
|
||||
tracing::trace!("No cache-invalidation receivers — message dropped");
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn membership_roundtrips_through_json() {
|
||||
let msg = CacheInvalidation::Membership {
|
||||
channel_id: Uuid::from_u128(0x1234),
|
||||
pubkey: vec![1, 2, 3, 4],
|
||||
};
|
||||
let json = serde_json::to_string(&msg).unwrap();
|
||||
assert_eq!(
|
||||
serde_json::from_str::<CacheInvalidation>(&json).unwrap(),
|
||||
msg
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unit_variants_roundtrip_through_json() {
|
||||
for msg in [
|
||||
CacheInvalidation::AccessibleAll,
|
||||
CacheInvalidation::ChannelDeleted,
|
||||
CacheInvalidation::Visibility {
|
||||
channel_id: Uuid::from_u128(0xabcd),
|
||||
},
|
||||
] {
|
||||
let json = serde_json::to_string(&msg).unwrap();
|
||||
assert_eq!(
|
||||
serde_json::from_str::<CacheInvalidation>(&json).unwrap(),
|
||||
msg
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -21,6 +21,8 @@
|
||||
//! Pool connections handle all other commands.
|
||||
//! Lagged receivers get `RecvError::Lagged`.
|
||||
|
||||
/// Cross-pod cache-key invalidation over Redis pub/sub.
|
||||
pub mod cache_invalidation;
|
||||
/// Error types for pub/sub operations.
|
||||
pub mod error;
|
||||
/// Online/offline presence tracking in Redis.
|
||||
@@ -41,6 +43,8 @@ use nostr::PublicKey;
|
||||
use tokio::sync::broadcast;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::cache_invalidation::{CacheInvalidation, CACHE_INVALIDATION_CHANNEL};
|
||||
|
||||
/// A Nostr event received on a specific channel, broadcast to local subscribers.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ChannelEvent {
|
||||
@@ -72,17 +76,20 @@ pub struct PubSubManager {
|
||||
/// Redis URL used by the reconnect loop to re-establish pub/sub connections.
|
||||
redis_url: String,
|
||||
broadcast_tx: broadcast::Sender<ChannelEvent>,
|
||||
cache_invalidation_tx: broadcast::Sender<CacheInvalidation>,
|
||||
}
|
||||
|
||||
impl PubSubManager {
|
||||
/// Creates a new `PubSubManager` connected to the given Redis URL.
|
||||
pub async fn new(redis_url: &str, pool: deadpool_redis::Pool) -> Result<Self, PubSubError> {
|
||||
let (broadcast_tx, _) = broadcast::channel(4096);
|
||||
let (cache_invalidation_tx, _) = broadcast::channel(4096);
|
||||
|
||||
Ok(Self {
|
||||
pool,
|
||||
redis_url: redis_url.to_string(),
|
||||
broadcast_tx,
|
||||
cache_invalidation_tx,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -94,11 +101,44 @@ impl PubSubManager {
|
||||
subscriber::run_subscriber(self.redis_url.clone(), self.broadcast_tx.clone()).await;
|
||||
}
|
||||
|
||||
/// Starts the cache-invalidation subscriber loop with automatic
|
||||
/// reconnection. Runs forever — spawn this in a background task.
|
||||
pub async fn run_cache_invalidation_subscriber(self: Arc<Self>) {
|
||||
cache_invalidation::run_cache_invalidation_subscriber(
|
||||
self.redis_url.clone(),
|
||||
self.cache_invalidation_tx.clone(),
|
||||
)
|
||||
.await;
|
||||
}
|
||||
|
||||
/// Returns a new broadcast receiver for locally-published channel events.
|
||||
pub fn subscribe_local(&self) -> broadcast::Receiver<ChannelEvent> {
|
||||
self.broadcast_tx.subscribe()
|
||||
}
|
||||
|
||||
/// Returns a new broadcast receiver for cross-pod cache-invalidation drops.
|
||||
pub fn subscribe_cache_invalidations(&self) -> broadcast::Receiver<CacheInvalidation> {
|
||||
self.cache_invalidation_tx.subscribe()
|
||||
}
|
||||
|
||||
/// Publish a cache-key drop to all pods. Fire-and-forget at the call site:
|
||||
/// the local cache is already dropped synchronously; this carries the same
|
||||
/// drop cross-pod. A dropped publish is backstopped by the REQ denial-path
|
||||
/// DB confirmation, so callers may spawn this without awaiting delivery.
|
||||
pub async fn publish_cache_invalidation(
|
||||
&self,
|
||||
invalidation: &CacheInvalidation,
|
||||
) -> Result<i64, PubSubError> {
|
||||
let mut conn = self.pool.get().await?;
|
||||
let payload = serde_json::to_string(invalidation)?;
|
||||
let subscriber_count: i64 = redis::cmd("PUBLISH")
|
||||
.arg(CACHE_INVALIDATION_CHANNEL)
|
||||
.arg(&payload)
|
||||
.query_async(&mut conn)
|
||||
.await?;
|
||||
Ok(subscriber_count)
|
||||
}
|
||||
|
||||
/// Publish an event to the Redis channel. Returns subscriber count.
|
||||
///
|
||||
/// Routing note (NIP-ER author-private reminders): events are keyed by
|
||||
@@ -202,6 +242,36 @@ mod tests {
|
||||
assert_eq!(received.event.id, event_id);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[ignore = "requires Redis"]
|
||||
async fn test_cache_invalidation_roundtrip() {
|
||||
let manager = make_manager().await;
|
||||
let mut rx = manager.subscribe_cache_invalidations();
|
||||
|
||||
let manager_clone = manager.clone();
|
||||
tokio::spawn(async move { manager_clone.run_cache_invalidation_subscriber().await });
|
||||
tokio::time::sleep(tokio::time::Duration::from_millis(200)).await;
|
||||
|
||||
let channel_id = Uuid::new_v4();
|
||||
let pubkey = Keys::generate().public_key().to_bytes().to_vec();
|
||||
let sent = CacheInvalidation::Membership {
|
||||
channel_id,
|
||||
pubkey: pubkey.clone(),
|
||||
};
|
||||
|
||||
manager
|
||||
.publish_cache_invalidation(&sent)
|
||||
.await
|
||||
.expect("publish failed");
|
||||
|
||||
let received = tokio::time::timeout(tokio::time::Duration::from_secs(2), rx.recv())
|
||||
.await
|
||||
.expect("timeout")
|
||||
.expect("channel closed");
|
||||
|
||||
assert_eq!(received, sent);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[ignore = "requires Redis"]
|
||||
async fn test_presence_set_and_get() {
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
# Changelog
|
||||
|
||||
## relay-v0.1.1
|
||||
|
||||
- Initial release
|
||||
|
||||
@@ -4,7 +4,7 @@ default-run = "buzz-relay"
|
||||
# Independent version: buzz-relay ships as a pinnable artifact
|
||||
# (ghcr.io/block/buzz), released on its own cadence via `just release-relay`.
|
||||
# It does NOT inherit the workspace version.
|
||||
version = "0.1.0"
|
||||
version = "0.1.1"
|
||||
edition.workspace = true
|
||||
rust-version.workspace = true
|
||||
license.workspace = true
|
||||
@@ -28,7 +28,10 @@ buzz-audit = { workspace = true }
|
||||
buzz-search = { workspace = true }
|
||||
axum = { workspace = true }
|
||||
tokio = { workspace = true }
|
||||
tokio-util = { workspace = true }
|
||||
# `io` (ReaderStream) is needed to stream git subprocess stdout straight into
|
||||
# the HTTP response body — see api::git::transport read-path streaming. The
|
||||
# workspace default only enables rt+codec.
|
||||
tokio-util = { workspace = true, features = ["io"] }
|
||||
tower = { workspace = true }
|
||||
tower-http = { workspace = true }
|
||||
nostr = { workspace = true }
|
||||
|
||||
@@ -54,7 +54,6 @@ async fn main() -> anyhow::Result<()> {
|
||||
.map_err(|error| anyhow::anyhow!("MeshLLM host runtime init failed: {error}"))?;
|
||||
eprintln!("[smoke] MeshLLM host runtime initialized");
|
||||
|
||||
// ── 1. Serve node ────────────────────────────────────────────────────────
|
||||
let serve_cfg = serve::EmbeddedServeConfig::builder()
|
||||
.model(&model)
|
||||
.api_port(SERVE_API_PORT)
|
||||
@@ -87,7 +86,6 @@ async fn main() -> anyhow::Result<()> {
|
||||
let serve_model_id = wait_for_model(&http, &serve_base).await?;
|
||||
eprintln!("[smoke] serve model ready: {serve_model_id}");
|
||||
|
||||
// ── 2. Client node, joined to the serve node ─────────────────────────────
|
||||
let client_cfg = client::EmbeddedClientConfig::builder()
|
||||
.api_port(CLIENT_API_PORT)
|
||||
.console_port(CLIENT_CONSOLE_PORT)
|
||||
@@ -108,7 +106,6 @@ async fn main() -> anyhow::Result<()> {
|
||||
let routed_model_id = wait_for_model(&http, &client_base).await?;
|
||||
eprintln!("[smoke] client sees routed model: {routed_model_id}");
|
||||
|
||||
// ── 3. One real completion, through the CLIENT (mesh hop) ─────────────────
|
||||
let chat_url = format!("{client_base}/chat/completions");
|
||||
let req = serde_json::json!({
|
||||
"model": routed_model_id,
|
||||
|
||||
@@ -18,8 +18,6 @@ use crate::state::AppState;
|
||||
|
||||
use super::{api_error, internal_error, not_found};
|
||||
|
||||
// ── NIP-98 verification ──────────────────────────────────────────────────────
|
||||
|
||||
/// Verify bridge auth: NIP-98 (production) or X-Pubkey (dev mode).
|
||||
///
|
||||
/// Returns the authenticated public key and an event ID for replay detection.
|
||||
@@ -104,8 +102,6 @@ fn canonical_url(relay_url: &str, path: &str) -> String {
|
||||
format!("{base}{path}")
|
||||
}
|
||||
|
||||
// ── Channel access helpers ───────────────────────────────────────────────────
|
||||
|
||||
/// Extract a channel UUID from a single filter's `#h` tag.
|
||||
fn extract_channel_from_filter(filter: &nostr::Filter) -> Option<uuid::Uuid> {
|
||||
let h_tag = nostr::SingleLetterTag::lowercase(nostr::Alphabet::H);
|
||||
@@ -118,7 +114,6 @@ fn extract_channel_from_filter(filter: &nostr::Filter) -> Option<uuid::Uuid> {
|
||||
})
|
||||
}
|
||||
|
||||
// ── Custom filter field extractors ──────────────────────────────────────────
|
||||
//
|
||||
// The CLI injects extension fields (before_id, depth_limit, feed_types) into
|
||||
// Nostr filter JSON. nostr::Filter silently drops unknown fields during
|
||||
@@ -162,8 +157,6 @@ fn event_in_accessible_channel(se: &buzz_core::StoredEvent, accessible: &[uuid::
|
||||
}
|
||||
}
|
||||
|
||||
// ── POST /events ─────────────────────────────────────────────────────────────
|
||||
|
||||
/// Submit a signed Nostr event via HTTP bridge (NIP-98 auth).
|
||||
pub async fn submit_event(
|
||||
State(state): State<Arc<AppState>>,
|
||||
@@ -233,8 +226,6 @@ pub async fn submit_event(
|
||||
}
|
||||
}
|
||||
|
||||
// ── POST /query ──────────────────────────────────────────────────────────────
|
||||
|
||||
/// Query events via HTTP bridge (NIP-98 auth). Returns JSON array of events.
|
||||
///
|
||||
/// Enforces channel access: results are filtered to channels the user can access.
|
||||
@@ -295,7 +286,6 @@ pub async fn query_events(
|
||||
.await
|
||||
.map_err(|e| internal_error(&format!("channel access lookup: {e}")))?;
|
||||
|
||||
// ── NIP-50 search: route to Typesense if any filter has a `search` field ──
|
||||
if filters.iter().any(|f| f.search.is_some()) {
|
||||
return handle_bridge_search(
|
||||
&state,
|
||||
@@ -307,7 +297,6 @@ pub async fn query_events(
|
||||
.await;
|
||||
}
|
||||
|
||||
// ── Presence: synthesize kind:20001 from Redis (ephemeral, never in DB) ──
|
||||
if let Some(presence_events) = synthesize_presence(&state, &filters).await {
|
||||
return Ok(Json(Value::Array(presence_events)));
|
||||
}
|
||||
@@ -315,7 +304,6 @@ pub async fn query_events(
|
||||
let mut events: Vec<Value> = Vec::new();
|
||||
let mut handled: std::collections::HashSet<usize> = std::collections::HashSet::new();
|
||||
|
||||
// ── feed_types: route to dedicated feed query functions ──
|
||||
for (idx, (raw, filter)) in raw_filters.iter().zip(filters.iter()).enumerate() {
|
||||
let feed_types = match extract_feed_types(raw) {
|
||||
Some(t) => t,
|
||||
@@ -380,7 +368,6 @@ pub async fn query_events(
|
||||
handled.insert(idx);
|
||||
}
|
||||
|
||||
// ── depth_limit: route thread queries to get_thread_replies ──
|
||||
let e_tag_key = nostr::SingleLetterTag::lowercase(nostr::Alphabet::E);
|
||||
for (idx, (raw, filter)) in raw_filters.iter().zip(filters.iter()).enumerate() {
|
||||
if handled.contains(&idx) {
|
||||
@@ -432,7 +419,6 @@ pub async fn query_events(
|
||||
handled.insert(idx);
|
||||
}
|
||||
|
||||
// ── Standard query path (with before_id injection) ──
|
||||
for (idx, (raw, filter)) in raw_filters.iter().zip(filters.iter()).enumerate() {
|
||||
if handled.contains(&idx) {
|
||||
continue;
|
||||
@@ -492,8 +478,6 @@ pub async fn query_events(
|
||||
Ok(Json(Value::Array(events)))
|
||||
}
|
||||
|
||||
// ── POST /count ──────────────────────────────────────────────────────────────
|
||||
|
||||
/// Count events via HTTP bridge (NIP-98 auth). Returns `{"count": N}`.
|
||||
///
|
||||
/// Enforces channel access: only counts events in channels the user can access.
|
||||
@@ -653,8 +637,6 @@ pub async fn count_events(
|
||||
Ok(Json(serde_json::json!({ "count": total })))
|
||||
}
|
||||
|
||||
// ── NIP-50 search via HTTP bridge ────────────────────────────────────────────
|
||||
|
||||
/// Decide whether a search hit should be returned to the caller.
|
||||
///
|
||||
/// Mirrors the WS NIP-50 path's post-filter step in `handlers/req.rs`:
|
||||
@@ -826,8 +808,6 @@ async fn handle_bridge_search(
|
||||
Ok(Json(Value::Array(events)))
|
||||
}
|
||||
|
||||
// ── POST /hooks/{id} — Webhook trigger ───────────────────────────────────────
|
||||
|
||||
/// Query parameters for the webhook trigger endpoint.
|
||||
#[derive(serde::Deserialize)]
|
||||
pub struct WebhookQuery {
|
||||
@@ -972,8 +952,6 @@ pub async fn workflow_webhook(
|
||||
))
|
||||
}
|
||||
|
||||
// ── Presence synthesis from Redis ────────────────────────────────────────────
|
||||
|
||||
/// If all filters target kind:20001 or kind:40902 with authors, synthesize
|
||||
/// presence from Redis instead of querying the DB (ephemeral events are never
|
||||
/// stored, and kind:40902 snapshots are relay-generated on demand).
|
||||
@@ -1150,8 +1128,6 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
// ── Custom filter field extractor tests ──
|
||||
|
||||
#[test]
|
||||
fn extract_before_id_valid_hex() {
|
||||
let hex = "a".repeat(64);
|
||||
|
||||
@@ -63,6 +63,7 @@ use std::collections::BTreeMap;
|
||||
use std::path::Path;
|
||||
use std::process::Stdio;
|
||||
|
||||
use tempfile::TempDir;
|
||||
use tokio::io::AsyncWriteExt;
|
||||
use tokio::process::Command;
|
||||
use tracing::{debug, warn};
|
||||
@@ -256,6 +257,54 @@ async fn snapshot_workspace_state(
|
||||
Ok((refs, head))
|
||||
}
|
||||
|
||||
fn digest_from_pack_key(key: &str) -> Result<String, CasError> {
|
||||
key.strip_prefix("packs/")
|
||||
.filter(|digest| digest.len() == 64 && digest.chars().all(|c| c.is_ascii_hexdigit()))
|
||||
.map(str::to_string)
|
||||
.ok_or_else(|| {
|
||||
CasError::Backend(StoreError::Backend(s3::error::S3Error::HttpFailWithBody(
|
||||
500,
|
||||
format!("put_pack returned non-standard key: {key}"),
|
||||
)))
|
||||
})
|
||||
}
|
||||
|
||||
async fn write_idx_sidecar(
|
||||
store: &GitStore,
|
||||
pack_key: &str,
|
||||
pack_bytes: &[u8],
|
||||
) -> Result<(), CasError> {
|
||||
let pack_digest = digest_from_pack_key(pack_key)?;
|
||||
let tempdir = TempDir::new().map_err(|e| CasError::PackCapture(format!("idx tempdir: {e}")))?;
|
||||
let pack_path = tempdir.path().join(format!("pack-{pack_digest}.pack"));
|
||||
tokio::fs::write(&pack_path, pack_bytes)
|
||||
.await
|
||||
.map_err(|e| CasError::PackCapture(format!("write idx input pack {pack_digest}: {e}")))?;
|
||||
|
||||
let mut cmd = Command::new("git");
|
||||
cmd.args(["index-pack", pack_path.to_str().unwrap()])
|
||||
.current_dir(tempdir.path());
|
||||
super::transport::harden_git_env(&mut cmd);
|
||||
let out = cmd
|
||||
.output()
|
||||
.await
|
||||
.map_err(|e| CasError::PackCapture(format!("index-pack spawn for idx sidecar: {e}")))?;
|
||||
if !out.status.success() {
|
||||
return Err(CasError::PackCapture(format!(
|
||||
"index-pack for idx sidecar failed: status={:?} stderr={}",
|
||||
out.status.code(),
|
||||
String::from_utf8_lossy(&out.stderr)
|
||||
)));
|
||||
}
|
||||
|
||||
let idx_path = pack_path.with_extension("idx");
|
||||
let idx_bytes = tokio::fs::read(&idx_path)
|
||||
.await
|
||||
.map_err(|e| CasError::PackCapture(format!("read idx sidecar {pack_digest}: {e}")))?;
|
||||
store.put_idx(&pack_digest, &idx_bytes).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Capture the objects this push introduced as a single pack.
|
||||
///
|
||||
/// Runs `git pack-objects --revs --stdout` reading rev-spec lines from
|
||||
@@ -439,7 +488,15 @@ pub async fn cas_publish(
|
||||
let pack_bytes = capture_pack(repo_path, &parent_state.parent.refs, &refs_after).await?;
|
||||
let new_pack_key = if let Some(bytes) = pack_bytes {
|
||||
debug!(bytes = bytes.len(), "captured push pack");
|
||||
Some(store.put_pack(&bytes).await?)
|
||||
let pack_key = store.put_pack(&bytes).await?;
|
||||
if let Err(e) = write_idx_sidecar(store, &pack_key, &bytes).await {
|
||||
warn!(
|
||||
pack_key = %pack_key,
|
||||
error = %e,
|
||||
"failed to write git pack idx sidecar; push will continue"
|
||||
);
|
||||
}
|
||||
Some(pack_key)
|
||||
} else {
|
||||
debug!("no new objects in push; manifest will reuse parent packs");
|
||||
None
|
||||
@@ -560,6 +617,20 @@ mod tests {
|
||||
// `pointer_key` is owned by `manifest.rs` and unit-tested there
|
||||
// (one source of truth — Max/Sami's centralization point).
|
||||
|
||||
#[test]
|
||||
fn digest_from_pack_key_strips_prefix() {
|
||||
let k = format!("packs/{}", "b".repeat(64));
|
||||
let d = digest_from_pack_key(&k).unwrap();
|
||||
assert_eq!(d, "b".repeat(64));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn digest_from_pack_key_rejects_unknown_prefix_or_bad_digest() {
|
||||
assert!(digest_from_pack_key("manifests/abc").is_err());
|
||||
assert!(digest_from_pack_key("packs/abc").is_err());
|
||||
assert!(digest_from_pack_key(&format!("packs/{}", "g".repeat(64))).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn digest_from_key_strips_prefix() {
|
||||
let k = format!("manifests/{}", "a".repeat(64));
|
||||
|
||||
@@ -7,7 +7,8 @@
|
||||
//! 1. GET pointer → manifest digest.
|
||||
//! 2. GET manifest (digest-verified) → parsed [`Manifest`].
|
||||
//! 3. GET every pack the manifest names (digest-verified, in parallel).
|
||||
//! 4. **Phase 1** — for each pack: write `pack-<hex>.pack`, run
|
||||
//! 4. **Phase 1** — for each pack: write `pack-<hex>.pack`; install and
|
||||
//! validate a cached `idx/<hex>` sidecar when present, otherwise run
|
||||
//! `git index-pack` to materialize `.idx`. Failure here tears down the
|
||||
//! tempdir with no refs/HEAD ever written.
|
||||
//! 5. **Phase 2** — only after all packs are indexed: write loose refs and
|
||||
@@ -95,6 +96,21 @@ pub async fn hydrate_for_read(
|
||||
Ok(Some(materialize_manifest(store, &manifest).await?))
|
||||
}
|
||||
|
||||
/// Load the current manifest for a read path without materializing pack objects.
|
||||
///
|
||||
/// Used by `info/refs` fast paths that can answer directly from manifest refs.
|
||||
/// Returns `Ok(None)` when the repo pointer is absent; otherwise verifies the
|
||||
/// pointer-named manifest digest before returning it.
|
||||
pub async fn load_manifest_for_read(
|
||||
store: &GitStore,
|
||||
owner: &str,
|
||||
repo: &str,
|
||||
) -> Result<Option<Manifest>, HydrateError> {
|
||||
Ok(load_pointer(store, owner, repo)
|
||||
.await?
|
||||
.map(|(_etag, _digest, manifest)| manifest))
|
||||
}
|
||||
|
||||
/// Hydrate a bare repo for write (`receive-pack`) and return the
|
||||
/// `ParentState` the workspace was hydrated from.
|
||||
///
|
||||
@@ -206,12 +222,7 @@ async fn materialize_manifest(
|
||||
tokio::fs::write(&pack_path, bytes)
|
||||
.await
|
||||
.map_err(|e| HydrateError::Hydrate(format!("write pack {digest}: {e}")))?;
|
||||
// No `--strict`: `index-pack` already validates structural integrity
|
||||
// (CRC, type tags, internal refs). `--strict` adds connectivity-graph
|
||||
// checks, which would re-prove what manifest.packs already covers by
|
||||
// construction (Inv_Closed, write-path invariant). Latency cost on
|
||||
// every clone is not worth re-proving a write-path bug.
|
||||
run_git(&path, &["index-pack", pack_path.to_str().unwrap()]).await?;
|
||||
install_or_generate_idx(store, &path, digest, &pack_path).await?;
|
||||
}
|
||||
|
||||
// Phase 2: install refs and HEAD. After this point, the repo advertises.
|
||||
@@ -258,6 +269,48 @@ async fn materialize_manifest(
|
||||
})
|
||||
}
|
||||
|
||||
async fn install_or_generate_idx(
|
||||
store: &GitStore,
|
||||
repo_path: &Path,
|
||||
pack_digest: &str,
|
||||
pack_path: &Path,
|
||||
) -> Result<(), HydrateError> {
|
||||
let idx_path = pack_path.with_extension("idx");
|
||||
match store.get_idx(pack_digest).await {
|
||||
Ok(Some(idx_bytes)) => {
|
||||
tokio::fs::write(&idx_path, &idx_bytes)
|
||||
.await
|
||||
.map_err(|e| HydrateError::Hydrate(format!("write idx {pack_digest}: {e}")))?;
|
||||
match run_git(repo_path, &["verify-pack", idx_path.to_str().unwrap()]).await {
|
||||
Ok(()) => return Ok(()),
|
||||
Err(e) => {
|
||||
tracing::warn!(
|
||||
pack_digest = %pack_digest,
|
||||
error = %e,
|
||||
"git pack idx sidecar failed validation; regenerating"
|
||||
);
|
||||
let _ = tokio::fs::remove_file(&idx_path).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(None) => {}
|
||||
Err(e) => {
|
||||
tracing::warn!(
|
||||
pack_digest = %pack_digest,
|
||||
error = %e,
|
||||
"failed to read git pack idx sidecar; regenerating"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// No `--strict`: `index-pack` already validates structural integrity
|
||||
// (CRC, type tags, internal refs). `--strict` adds connectivity-graph
|
||||
// checks, which would re-prove what manifest.packs already covers by
|
||||
// construction (Inv_Closed, write-path invariant). Latency cost on every
|
||||
// clone is not worth re-proving a write-path bug.
|
||||
run_git(repo_path, &["index-pack", pack_path.to_str().unwrap()]).await
|
||||
}
|
||||
|
||||
/// Run `git <args>` in `cwd`, fail on non-zero exit.
|
||||
async fn run_git(cwd: &Path, args: &[&str]) -> Result<(), HydrateError> {
|
||||
let mut cmd = Command::new("git");
|
||||
|
||||
@@ -20,8 +20,6 @@ use std::collections::BTreeMap;
|
||||
|
||||
use nostr::{Event, EventBuilder, Keys, Kind, PublicKey, Tag, TagKind};
|
||||
|
||||
// ── Inputs ───────────────────────────────────────────────────────────────────
|
||||
|
||||
/// Subset of `Manifest` needed to emit a kind:30618 event.
|
||||
///
|
||||
/// Deliberately a borrowed slice of fields (not the whole `Manifest`) so this
|
||||
@@ -54,8 +52,6 @@ pub enum BuildError {
|
||||
Sign(String),
|
||||
}
|
||||
|
||||
// ── Build helper ─────────────────────────────────────────────────────────────
|
||||
|
||||
/// Build & sign a kind:30618 event from the manifest's ref state.
|
||||
///
|
||||
/// Signed with `relay_keys` — the relay is the authoritative source of ref
|
||||
@@ -117,8 +113,6 @@ pub fn build_ref_state_event(
|
||||
Ok(event)
|
||||
}
|
||||
|
||||
// ── Validators (private) ─────────────────────────────────────────────────────
|
||||
|
||||
/// NIP-34 kind:30618 only emits refs under heads/ and tags/.
|
||||
fn is_emittable_ref(name: &str) -> bool {
|
||||
if !(name.starts_with("refs/heads/") || name.starts_with("refs/tags/")) {
|
||||
@@ -136,8 +130,6 @@ fn is_valid_oid(s: &str) -> bool {
|
||||
matches!(s.len(), 40 | 64) && s.chars().all(|c| c.is_ascii_hexdigit())
|
||||
}
|
||||
|
||||
// ── Tests ────────────────────────────────────────────────────────────────────
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -182,8 +174,6 @@ mod tests {
|
||||
tags_with_kind(ev, kind).into_iter().next()
|
||||
}
|
||||
|
||||
// ── Empty repo (creation event) ──────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn empty_repo_emits_d_head_p_only() {
|
||||
let owner = owner_hex();
|
||||
@@ -213,8 +203,6 @@ mod tests {
|
||||
assert!(tags_with_kind(&ev, "refs/heads/main").is_empty());
|
||||
}
|
||||
|
||||
// ── HEAD wrapping (the gotcha) ───────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn head_tag_always_wraps_with_ref_prefix() {
|
||||
// Even if a caller is sloppy and passes head with the prefix already...
|
||||
@@ -231,8 +219,6 @@ mod tests {
|
||||
assert_eq!(first_tag(&ev, "HEAD").unwrap()[1], "ref: refs/heads/dev");
|
||||
}
|
||||
|
||||
// ── Branch + tag refs ────────────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn emits_branches_and_tags() {
|
||||
let refs = refs_with(&[
|
||||
@@ -265,8 +251,6 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
// ── Non-heads/tags refs are filtered ─────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn skips_non_heads_or_tags_refs() {
|
||||
let refs = refs_with(&[
|
||||
@@ -303,8 +287,6 @@ mod tests {
|
||||
assert!(first_tag(&ev, "refs/pull/1/head").is_none());
|
||||
}
|
||||
|
||||
// ── OID validation: SHA-1 and SHA-256 ────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn accepts_sha1_and_sha256_oids() {
|
||||
let sha1 = "1111111111111111111111111111111111111111"; // 40 hex
|
||||
@@ -351,8 +333,6 @@ mod tests {
|
||||
assert!(first_tag(&ev, "refs/heads/ok").is_some());
|
||||
}
|
||||
|
||||
// ── Ref name validation ──────────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn rejects_malformed_ref_names() {
|
||||
let refs = refs_with(&[
|
||||
@@ -385,8 +365,6 @@ mod tests {
|
||||
assert_eq!(tags_with_kind(&ev, "refs/heads//double").len(), 0);
|
||||
}
|
||||
|
||||
// ── Actor pubkey errors ──────────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn rejects_invalid_actor_pubkey() {
|
||||
let refs = refs_with(&[]);
|
||||
@@ -400,8 +378,6 @@ mod tests {
|
||||
assert!(matches!(err, BuildError::InvalidActor(_)));
|
||||
}
|
||||
|
||||
// ── d-tag matches kind:30617 identifier (NOT <repo>.git) ─────────────────
|
||||
|
||||
#[test]
|
||||
fn d_tag_is_repo_id_not_repo_dot_git() {
|
||||
let refs = refs_with(&[]);
|
||||
|
||||
@@ -46,8 +46,6 @@ use buzz_db::EventQuery;
|
||||
|
||||
use crate::state::AppState;
|
||||
|
||||
// ── Types ────────────────────────────────────────────────────────────────────
|
||||
|
||||
/// Maximum age of a hook callback (seconds). Push is synchronous so 30s is generous.
|
||||
const MAX_CALLBACK_AGE_SECS: u64 = 30;
|
||||
|
||||
@@ -110,8 +108,6 @@ impl From<Denial> for DenialResponse {
|
||||
}
|
||||
}
|
||||
|
||||
// ── HMAC Verification ────────────────────────────────────────────────────────
|
||||
|
||||
/// Compute the canonical HMAC payload.
|
||||
///
|
||||
/// Format (length-prefixed, `|`-separated, structurally unambiguous):
|
||||
@@ -165,8 +161,6 @@ fn verify_hmac(secret: &[u8], req: &HookCallbackRequest) -> bool {
|
||||
expected.ct_eq(&provided).into()
|
||||
}
|
||||
|
||||
// ── Handler ──────────────────────────────────────────────────────────────────
|
||||
|
||||
/// `POST /internal/git/policy` — pre-receive hook callback.
|
||||
///
|
||||
/// Fail-closed: ANY error returns 403. The hook script treats non-200 as deny.
|
||||
@@ -383,8 +377,6 @@ pub async fn hook_policy_check(
|
||||
}
|
||||
}
|
||||
|
||||
// ── HMAC Generation (for the relay to pass to the hook) ──────────────────────
|
||||
|
||||
/// Generate the HMAC signature for a hook callback payload.
|
||||
///
|
||||
/// Called by the relay when setting up the pre-receive hook environment.
|
||||
@@ -408,8 +400,6 @@ pub fn generate_hook_hmac(
|
||||
hex::encode(mac_bytes)
|
||||
}
|
||||
|
||||
// ── Tests ────────────────────────────────────────────────────────────────────
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
@@ -189,6 +189,22 @@ impl GitStore {
|
||||
format!("{prefix}/{}", hex::encode(h.finalize()))
|
||||
}
|
||||
|
||||
/// Derive the idx sidecar key for a content-addressed pack digest.
|
||||
///
|
||||
/// The idx is a pure cache derived from `packs/<pack_digest>`, so it is
|
||||
/// keyed by the pack digest rather than by the idx bytes. This keeps the
|
||||
/// manifest schema unchanged: readers can derive `idx/<pack_digest>` from
|
||||
/// each manifest pack key.
|
||||
pub fn idx_key_for_pack_digest(pack_digest: &str) -> Result<String, StoreError> {
|
||||
if pack_digest.len() != 64 || !pack_digest.chars().all(|c| c.is_ascii_hexdigit()) {
|
||||
return Err(StoreError::Backend(S3Error::HttpFailWithBody(
|
||||
400,
|
||||
format!("invalid pack digest for idx sidecar: {pack_digest:?}"),
|
||||
)));
|
||||
}
|
||||
Ok(format!("idx/{pack_digest}"))
|
||||
}
|
||||
|
||||
/// Create-only write of a content-addressed object (pack or manifest).
|
||||
///
|
||||
/// **The caller does not choose the key.** It is derived as
|
||||
@@ -233,6 +249,51 @@ impl GitStore {
|
||||
.await
|
||||
}
|
||||
|
||||
/// Best-effort create-only write of an idx sidecar for `packs/<pack_digest>`.
|
||||
///
|
||||
/// Unlike packs/manifests, the key is not the SHA-256 of `idx_bytes`; it is
|
||||
/// `idx/<pack_digest>` so hydrates can derive it without changing manifest
|
||||
/// bytes. A 412 is idempotent success for the cache layer: the first writer
|
||||
/// already produced the sidecar for this pack, and hydrate validates before
|
||||
/// trusting it.
|
||||
pub async fn put_idx(&self, pack_digest: &str, idx_bytes: &[u8]) -> Result<String, StoreError> {
|
||||
let key = Self::idx_key_for_pack_digest(pack_digest)?;
|
||||
let mut headers = axum::http::HeaderMap::new();
|
||||
headers.insert(axum::http::header::IF_NONE_MATCH, "*".parse().unwrap());
|
||||
match self
|
||||
.bucket
|
||||
.put_object_with_content_type_and_headers(
|
||||
&key,
|
||||
idx_bytes,
|
||||
"application/x-git-index",
|
||||
Some(headers),
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(resp) if (200..300).contains(&resp.status_code()) => Ok(key),
|
||||
Err(S3Error::HttpFailWithBody(412, _)) => Ok(key),
|
||||
Ok(resp) => Err(StoreError::Backend(S3Error::HttpFailWithBody(
|
||||
resp.status_code(),
|
||||
"unexpected status".into(),
|
||||
))),
|
||||
Err(e) => Err(StoreError::Backend(e)),
|
||||
}
|
||||
}
|
||||
|
||||
/// Read an idx sidecar for `packs/<pack_digest>`.
|
||||
///
|
||||
/// A missing idx is a cache miss, not a hydrate failure; callers should
|
||||
/// regenerate with `git index-pack`. Other backend failures are surfaced so
|
||||
/// callers can decide whether to fall back or fail.
|
||||
pub async fn get_idx(&self, pack_digest: &str) -> Result<Option<Bytes>, StoreError> {
|
||||
let key = Self::idx_key_for_pack_digest(pack_digest)?;
|
||||
match self.get(&key).await {
|
||||
Ok(bytes) => Ok(Some(bytes)),
|
||||
Err(StoreError::NotFound(_)) => Ok(None),
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
|
||||
/// Write a manifest object. Returns the content-addressed key (`manifests/<hex>`).
|
||||
pub async fn put_manifest(&self, bytes: &[u8]) -> Result<String, StoreError> {
|
||||
self.put_immutable("manifests", bytes, "application/json")
|
||||
@@ -747,6 +808,22 @@ impl GitStore {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn idx_key_uses_pack_digest_namespace() {
|
||||
let digest = "a".repeat(64);
|
||||
assert_eq!(
|
||||
GitStore::idx_key_for_pack_digest(&digest).unwrap(),
|
||||
format!("idx/{digest}")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn idx_key_rejects_non_digest_input() {
|
||||
for bad in ["packs/abc", "abc", "../escape", &"g".repeat(64)] {
|
||||
assert!(GitStore::idx_key_for_pack_digest(bad).is_err());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn classify_cas_412_is_lost_race() {
|
||||
let r = Err(S3Error::HttpFailWithBody(412, "PreconditionFailed".into()));
|
||||
|
||||
@@ -28,19 +28,17 @@ use tracing::{error, info, warn};
|
||||
|
||||
use super::cas_publish::{cas_publish, CasError, ParentState};
|
||||
use super::hook::install_hook;
|
||||
use super::hydrate::{hydrate_for_read, hydrate_for_write, HydrateError, HydratedRepo};
|
||||
use super::hydrate::{
|
||||
hydrate_for_read, hydrate_for_write, load_manifest_for_read, HydrateError, HydratedRepo,
|
||||
};
|
||||
use super::manifest_event::{build_ref_state_event, RefStateInputs};
|
||||
use crate::state::AppState;
|
||||
|
||||
// ── Timeouts ─────────────────────────────────────────────────────────────────
|
||||
|
||||
/// Timeout for `info/refs` — ref advertisement is fast (essentially `git show-ref`).
|
||||
const INFO_REFS_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(120);
|
||||
/// Timeout for pack operations (upload-pack, receive-pack) — large repos need time.
|
||||
const PACK_OPS_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(300);
|
||||
|
||||
// ── NIP-98 Auth Extractor ────────────────────────────────────────────────────
|
||||
|
||||
/// NIP-98 auth extractor for git routes.
|
||||
///
|
||||
/// Validates the `Authorization: Nostr <base64>` header before the request body
|
||||
@@ -191,8 +189,6 @@ impl axum::extract::FromRequestParts<Arc<AppState>> for GitAuth {
|
||||
}
|
||||
}
|
||||
|
||||
// ── Repo Id Validation ───────────────────────────────────────────────────────
|
||||
|
||||
/// Validate URL `(owner, repo)` parameters and return the canonical repo
|
||||
/// id (= `repo` with any `.git` suffix stripped).
|
||||
///
|
||||
@@ -281,8 +277,6 @@ fn hydrate_error_to_response(owner: &str, repo: &str, err: HydrateError) -> Resp
|
||||
.into_response()
|
||||
}
|
||||
|
||||
// ── Route Handlers ───────────────────────────────────────────────────────────
|
||||
|
||||
#[derive(Deserialize)]
|
||||
/// Query parameters for the `info/refs` endpoint.
|
||||
pub struct InfoRefsQuery {
|
||||
@@ -296,16 +290,175 @@ pub struct GitRepoParams {
|
||||
repo: String,
|
||||
}
|
||||
|
||||
/// Longest refname the fast path will emit. `is_safe_refname` enforces an
|
||||
/// alphabet but no length bound; `pkt_line` encodes its payload length in a
|
||||
/// 4-hex prefix that overflows past `0xffff`. Git's own refname limits sit far
|
||||
/// below this, so any refname this long is pathological — degrade to the
|
||||
/// subprocess path rather than risk a malformed length prefix. Generous bound:
|
||||
/// `<oid> <refname>\n` plus the 4-byte pkt header must stay under `0xffff`, and
|
||||
/// 4096 leaves vast headroom (git's de-facto practical ceiling is a few hundred
|
||||
/// bytes total).
|
||||
const MAX_FAST_PATH_REFNAME_LEN: usize = 4096;
|
||||
|
||||
/// Whether the `info/refs` fast path can serve this manifest without shelling
|
||||
/// out. The manifest carries only `refname → oid`, so it cannot reproduce the
|
||||
/// `^{}` peel line an **annotated** tag advertises (the peeled commit oid is
|
||||
/// not stored — see RESEARCH/GIT_REF_ADVERTISEMENT_FORMAT.md). We cannot tell
|
||||
/// an annotated tag from a lightweight one at the manifest level, so **any**
|
||||
/// `refs/tags/*` forces the subprocess fallback for byte-correctness. We also
|
||||
/// require the symbolic `head` to resolve to a ref we actually advertise —
|
||||
/// otherwise the `symref=HEAD:<ref>` capability would point at a ref the
|
||||
/// client never sees. The dominant clone case (branches only, HEAD→a branch)
|
||||
/// takes the fast path; everything else stays exactly as it is today.
|
||||
///
|
||||
/// Eligibility is also a **safety gate**: the fast path emits manifest
|
||||
/// refnames/oids straight into pkt-line bytes, so this predicate re-runs the
|
||||
/// same `is_safe_refname`/`is_hex_oid` checks the hydrate path applies
|
||||
/// (hydrate.rs) and the write path applies (`Manifest::validate`). The manifest
|
||||
/// is already digest-verified against the pointer when loaded, so on every
|
||||
/// normally-reachable path these re-checks are redundant — but keeping the
|
||||
/// emit path symmetric with hydrate means an out-of-band-written manifest
|
||||
/// (migration, manual S3 put, a future writer that skips `validate`) degrades
|
||||
/// to the subprocess path instead of advertising unchecked bytes. Any failure
|
||||
/// here → `false` → subprocess fallback, which surfaces the error correctly.
|
||||
fn fast_path_eligible(manifest: &super::manifest::Manifest) -> bool {
|
||||
use super::manifest::{is_hex_oid, is_safe_refname};
|
||||
|
||||
if manifest.refs.keys().any(|r| r.starts_with("refs/tags/")) {
|
||||
return false;
|
||||
}
|
||||
// HEAD must resolve to an advertised ref. (Detached HEAD — head not in
|
||||
// refs — can't be expressed as a symref; fall back.)
|
||||
if !manifest.refs.contains_key(&manifest.head) {
|
||||
return false;
|
||||
}
|
||||
// Safety re-check: every refname/oid we'd emit must be well-formed and
|
||||
// bounded. HEAD is a key in `refs` (checked above), so the loop covers it.
|
||||
manifest.refs.iter().all(|(refname, oid)| {
|
||||
is_safe_refname(refname) && refname.len() <= MAX_FAST_PATH_REFNAME_LEN && is_hex_oid(oid)
|
||||
})
|
||||
}
|
||||
|
||||
/// Largest payload a single pkt-line can carry: the 4-hex length prefix counts
|
||||
/// itself, so the total frame is bounded to `0xffff` and the payload to
|
||||
/// `0xffff - 4`.
|
||||
const PKT_LINE_MAX_PAYLOAD: usize = 0xffff - 4;
|
||||
|
||||
/// Encode one pkt-line: 4-char lowercase-hex length prefix (counting itself)
|
||||
/// followed by `payload`. Appends to `out`.
|
||||
///
|
||||
/// The 4-hex prefix can only express a frame length up to `0xffff`. A payload
|
||||
/// past [`PKT_LINE_MAX_PAYLOAD`] would make `format!("{len:04x}")` emit *five*
|
||||
/// hex digits — not a truncation but a silent stream corruption (the next
|
||||
/// reader takes the first four as the length). Callers on the manifest fast
|
||||
/// path are already gated by [`fast_path_eligible`]'s length cap, but this is
|
||||
/// the construction boundary: rather than trust every present and future
|
||||
/// caller, an overlong payload here is dropped (emitting an empty `0004`
|
||||
/// pkt-line) and logged at `error`, instead of writing a malformed length.
|
||||
/// Non-panicking in every build profile — the worst case is a ref-short
|
||||
/// advertisement that fails cleanly client-side, never a corrupted stream.
|
||||
fn pkt_line(out: &mut Vec<u8>, payload: &[u8]) {
|
||||
if payload.len() > PKT_LINE_MAX_PAYLOAD {
|
||||
tracing::error!(
|
||||
payload_len = payload.len(),
|
||||
limit = PKT_LINE_MAX_PAYLOAD,
|
||||
"pkt-line payload exceeds 0xffff-4 frame limit; dropping (bug: caller \
|
||||
bypassed the fast-path length gate)"
|
||||
);
|
||||
out.extend_from_slice(b"0004"); // empty pkt-line; never a 5-hex length
|
||||
return;
|
||||
}
|
||||
let len = payload.len() + 4;
|
||||
out.extend_from_slice(format!("{len:04x}").as_bytes());
|
||||
out.extend_from_slice(payload);
|
||||
}
|
||||
|
||||
/// Build the **complete** `info/refs` HTTP body for `git-upload-pack` directly
|
||||
/// from the published manifest — no hydrate, no subprocess. Byte-compatible
|
||||
/// with `git upload-pack --advertise-refs` for the branches-only case that
|
||||
/// [`fast_path_eligible`] gates on.
|
||||
///
|
||||
/// Layout (matches the subprocess oracle, git 2.51):
|
||||
/// ```text
|
||||
/// <pkt># service=git-upload-pack\n
|
||||
/// 0000
|
||||
/// <pkt><head-oid> HEAD\0<caps> symref=HEAD:<head-ref> object-format=<fmt> agent=buzz-git\n
|
||||
/// <pkt><oid> <refname>\n # each ref, sorted ascending (BTreeMap order)
|
||||
/// 0000
|
||||
/// ```
|
||||
/// The advertised capabilities are a fixed conservative **offer**; the client
|
||||
/// re-negotiates against the real `upload-pack` subprocess in its follow-up
|
||||
/// POST, so any subset the real upload-pack supports is safe. `object-format`
|
||||
/// is derived from the oid width (40 hex = sha1, 64 = sha256) rather than
|
||||
/// hardcoded. Caller guarantees [`fast_path_eligible`] returned true.
|
||||
fn build_upload_pack_advertisement(manifest: &super::manifest::Manifest) -> Vec<u8> {
|
||||
// head ref is guaranteed present by `fast_path_eligible`.
|
||||
let head_oid = &manifest.refs[&manifest.head];
|
||||
let object_format = if head_oid.len() == 64 {
|
||||
"sha256"
|
||||
} else {
|
||||
"sha1"
|
||||
};
|
||||
|
||||
// Capability offer. Conservative, version-agnostic. The symref tells the
|
||||
// client which branch HEAD tracks (so `git clone` checks it out).
|
||||
let caps = format!(
|
||||
"multi_ack thin-pack side-band side-band-64k ofs-delta shallow \
|
||||
deepen-since deepen-not deepen-relative no-progress include-tag \
|
||||
multi_ack_detailed no-done symref=HEAD:{head} object-format={fmt} \
|
||||
agent=buzz-git",
|
||||
head = manifest.head,
|
||||
fmt = object_format,
|
||||
);
|
||||
|
||||
let mut out = Vec::new();
|
||||
|
||||
// 1. service header + flush.
|
||||
let svc_line = b"# service=git-upload-pack\n";
|
||||
pkt_line(&mut out, svc_line);
|
||||
out.extend_from_slice(b"0000");
|
||||
|
||||
// 2. First line: HEAD with NUL-joined caps.
|
||||
let mut first = Vec::new();
|
||||
first.extend_from_slice(head_oid.as_bytes());
|
||||
first.extend_from_slice(b" HEAD\0");
|
||||
first.extend_from_slice(caps.as_bytes());
|
||||
first.push(b'\n');
|
||||
pkt_line(&mut out, &first);
|
||||
|
||||
// 3. Each ref, sorted (BTreeMap iterates ascending — matches git).
|
||||
for (refname, oid) in &manifest.refs {
|
||||
let mut line = Vec::new();
|
||||
line.extend_from_slice(oid.as_bytes());
|
||||
line.push(b' ');
|
||||
line.extend_from_slice(refname.as_bytes());
|
||||
line.push(b'\n');
|
||||
pkt_line(&mut out, &line);
|
||||
}
|
||||
|
||||
// 4. Trailing flush.
|
||||
out.extend_from_slice(b"0000");
|
||||
out
|
||||
}
|
||||
|
||||
/// `GET /git/{owner}/{repo}/info/refs?service={service}`
|
||||
///
|
||||
/// Advertises refs for clone (git-upload-pack) or push (git-receive-pack).
|
||||
///
|
||||
/// **Track C fast path:** for `git-upload-pack` on a branches-only repo
|
||||
/// ([`fast_path_eligible`]), the advertisement is built directly from the
|
||||
/// published manifest — **no hydrate, no subprocess, no git semaphore
|
||||
/// permit**. This is the dominant clone case, and it's exactly what the
|
||||
/// W=20 permit used to serialize. Repos with any `refs/tags/*`, or the
|
||||
/// `git-receive-pack` advertisement (different cap set), fall back to the
|
||||
/// subprocess path — which still acquires a permit and hydrates, preserving
|
||||
/// today's behavior byte-for-byte.
|
||||
///
|
||||
/// **Read fail-closed (Max's blocker):** pointer-absent → 404 (repo
|
||||
/// never existed). *Any* below-pointer failure (manifest 404 under a
|
||||
/// non-empty pointer, digest mismatch, pack 404, `index-pack` failure)
|
||||
/// → 5xx via `HydrateError`. The legacy "leave disk as-is on hydrate
|
||||
/// error" behavior is gone — A1 detectability now holds end-to-end on
|
||||
/// the read side too.
|
||||
/// error" behavior is gone — A1 detectability holds on the read side too.
|
||||
pub async fn info_refs(
|
||||
State(state): State<Arc<AppState>>,
|
||||
_auth: GitAuth,
|
||||
@@ -319,11 +472,55 @@ pub async fn info_refs(
|
||||
};
|
||||
let _repo_name = validate_repo_id(¶ms.owner, ¶ms.repo)?;
|
||||
|
||||
let _permit = acquire_git_permit(&state)?;
|
||||
// Track C fast path: only for clone advertisement. The receive-pack
|
||||
// advertisement carries a different capability set (report-status,
|
||||
// delete-refs, atomic, …) that we don't reproduce, so it always takes
|
||||
// the subprocess path below.
|
||||
if service == "git-upload-pack" {
|
||||
// Load just the verified manifest — no object materialization, no
|
||||
// permit. `Ok(None)` = pointer absent = repo never existed → 404.
|
||||
match load_manifest_for_read(&state.git_store, ¶ms.owner, ¶ms.repo).await {
|
||||
Ok(Some(manifest)) if fast_path_eligible(&manifest) => {
|
||||
let body = build_upload_pack_advertisement(&manifest);
|
||||
return Ok(Response::builder()
|
||||
.status(StatusCode::OK)
|
||||
.header(
|
||||
header::CONTENT_TYPE,
|
||||
"application/x-git-upload-pack-advertisement",
|
||||
)
|
||||
.header(header::CACHE_CONTROL, "no-cache")
|
||||
.body(Body::from(body))
|
||||
.unwrap());
|
||||
}
|
||||
// Eligible repo but has tags, or below-pointer failure handling:
|
||||
// a present-but-ineligible manifest falls through to the
|
||||
// subprocess path. Pointer-absent is a definitive 404 here —
|
||||
// no point hydrating a repo that doesn't exist.
|
||||
Ok(Some(_)) => { /* ineligible (has tags) → subprocess fallback */ }
|
||||
Ok(None) => return Err((StatusCode::NOT_FOUND, "repository not found").into_response()),
|
||||
Err(e) => return Err(hydrate_error_to_response(¶ms.owner, ¶ms.repo, e)),
|
||||
}
|
||||
}
|
||||
|
||||
// Subprocess path: receive-pack advertisement, or upload-pack for a
|
||||
// tagged repo. Acquires a permit and hydrates — today's behavior.
|
||||
info_refs_subprocess(&state, service, ¶ms).await
|
||||
}
|
||||
|
||||
/// Subprocess-backed `info/refs` advertisement: hydrate the published state
|
||||
/// into an ephemeral bare repo and shell out to `git <svc> --advertise-refs`.
|
||||
///
|
||||
/// This is the fallback from the Track C fast path (tagged repos, and the
|
||||
/// `git-receive-pack` advertisement). The advertisement is O(refs), not
|
||||
/// O(pack), so it stays buffered — streaming would buy nothing and would
|
||||
/// lose the clean timeout/error mapping that buffering gives us.
|
||||
async fn info_refs_subprocess(
|
||||
state: &Arc<AppState>,
|
||||
service: &str,
|
||||
params: &GitRepoParams,
|
||||
) -> Result<Response, Response> {
|
||||
let _permit = acquire_git_permit(state)?;
|
||||
|
||||
// Hydrate the published state into an ephemeral bare repo. `Ok(None)`
|
||||
// = pointer absent = repo never existed → 404. `Err(_)` = below-pointer
|
||||
// failure → 5xx.
|
||||
let repo = match hydrate_for_read(&state.git_store, ¶ms.owner, ¶ms.repo).await {
|
||||
Ok(Some(repo)) => repo,
|
||||
Ok(None) => return Err((StatusCode::NOT_FOUND, "repository not found").into_response()),
|
||||
@@ -333,7 +530,7 @@ pub async fn info_refs(
|
||||
// Git's smart HTTP protocol uses service names like "git-upload-pack" and
|
||||
// "git-receive-pack", but the actual git subcommands are "upload-pack" and
|
||||
// "receive-pack" (without the "git-" prefix).
|
||||
let git_subcmd = service.strip_prefix("git-").unwrap_or(service.as_str());
|
||||
let git_subcmd = service.strip_prefix("git-").unwrap_or(service);
|
||||
|
||||
let mut cmd = Command::new("git");
|
||||
cmd.arg(git_subcmd)
|
||||
@@ -413,9 +610,21 @@ pub async fn upload_pack(
|
||||
Err(e) => return Err(hydrate_error_to_response(¶ms.owner, ¶ms.repo, e)),
|
||||
};
|
||||
|
||||
let output = run_git_at(repo.path(), "upload-pack", body, &[]).await?;
|
||||
drop(repo);
|
||||
Ok(build_git_response("upload-pack", output))
|
||||
// Track A: stream the subprocess stdout straight into the response body
|
||||
// instead of buffering the whole pack into RAM. `repo` (the hydrated
|
||||
// tempdir) is moved into the stream and stays alive until the last byte
|
||||
// is drained — the streaming analogue of the old `drop(repo)`. The
|
||||
// `_permit` (git semaphore) is bound for the function scope; binding it
|
||||
// here keeps it held only until the response head is built, not for the
|
||||
// whole drain. That's intentional — see Track C notes on permit hold time.
|
||||
stream_git_read(
|
||||
repo,
|
||||
"upload-pack",
|
||||
&[],
|
||||
body,
|
||||
Vec::new(),
|
||||
"application/x-git-upload-pack-result".to_string(),
|
||||
)
|
||||
}
|
||||
|
||||
/// `POST /git/{owner}/{repo}/git-receive-pack`
|
||||
@@ -525,8 +734,6 @@ pub async fn receive_pack(
|
||||
Ok(finalize_push(&state, ctx).await)
|
||||
}
|
||||
|
||||
// ── Subprocess Runner ────────────────────────────────────────────────────────
|
||||
|
||||
/// Buffered output of a `git --stateless-rpc` subprocess.
|
||||
///
|
||||
/// The handler holds this as an owned value between subprocess completion
|
||||
@@ -620,6 +827,131 @@ async fn run_git_at(
|
||||
})
|
||||
}
|
||||
|
||||
/// Keeps the git subprocess and its hydrated workspace alive for exactly as
|
||||
/// long as the response body is being streamed.
|
||||
///
|
||||
/// The HTTP body is a [`tokio_util::io::ReaderStream`] over the child's
|
||||
/// stdout. That borrows nothing it can keep alive on its own: the `Child`
|
||||
/// (whose `kill_on_drop` would otherwise reap the process mid-stream) and the
|
||||
/// [`HydratedRepo`] tempdir (whose objects the subprocess is still reading)
|
||||
/// must outlive the last byte. We park both here and drop them only when the
|
||||
/// stream is exhausted or the client disconnects — the structural analogue of
|
||||
/// the `drop(repo)` the buffered path did after `wait_with_output`.
|
||||
///
|
||||
/// Why streaming is safe here but **not** on the push path: these are
|
||||
/// read-only operations (`upload-pack`, `info/refs --advertise-refs`). They
|
||||
/// never mutate published state, so there is no fence to preserve — contrast
|
||||
/// `receive_pack`, which must buffer into [`PackOutput`] so [`finalize_push`]
|
||||
/// can sequence the pointer CAS *before* any 2xx byte exists.
|
||||
struct StreamingGit {
|
||||
inner: tokio_util::io::ReaderStream<tokio::process::ChildStdout>,
|
||||
/// Held purely to extend lifetime. `kill_on_drop(true)` means dropping
|
||||
/// this after the stream completes reaps any lingering process; on the
|
||||
/// happy path the child has already exited by then.
|
||||
_child: tokio::process::Child,
|
||||
/// The ephemeral bare repo the subprocess reads objects from. Must not be
|
||||
/// removed from disk until the subprocess is done — i.e. until the stream
|
||||
/// ends.
|
||||
_repo: HydratedRepo,
|
||||
}
|
||||
|
||||
impl futures_util::Stream for StreamingGit {
|
||||
type Item = Result<bytes::Bytes, std::io::Error>;
|
||||
|
||||
fn poll_next(
|
||||
mut self: std::pin::Pin<&mut Self>,
|
||||
cx: &mut std::task::Context<'_>,
|
||||
) -> std::task::Poll<Option<Self::Item>> {
|
||||
std::pin::Pin::new(&mut self.inner).poll_next(cx)
|
||||
}
|
||||
}
|
||||
|
||||
/// Spawn a read-only `git --stateless-rpc <service>` subprocess and return a
|
||||
/// streaming [`Response`] whose body is the child's stdout, optionally
|
||||
/// preceded by `prefix` bytes (used by `info/refs` for the
|
||||
/// `# service=…\n0000` pkt-line header).
|
||||
///
|
||||
/// The request `body` is pumped to the child's stdin concurrently. The
|
||||
/// returned response owns the child + the hydrated workspace via
|
||||
/// [`StreamingGit`], so neither is torn down until the body is fully drained.
|
||||
///
|
||||
/// **Read-path only.** Errors after the response head is sent cannot change
|
||||
/// the status code (it's already 200), which is exactly git's smart-HTTP
|
||||
/// contract: protocol-level failures are reported in-band within the pack
|
||||
/// stream, not via HTTP status. The buffered [`run_git_at`] stays the push
|
||||
/// path's runner precisely because the fence needs the bytes in hand before
|
||||
/// committing to a status.
|
||||
#[allow(clippy::result_large_err)]
|
||||
fn stream_git_read(
|
||||
repo: HydratedRepo,
|
||||
service: &'static str,
|
||||
extra_args: &[&str],
|
||||
body: Body,
|
||||
prefix: Vec<u8>,
|
||||
content_type: String,
|
||||
) -> Result<Response, Response> {
|
||||
let mut cmd = Command::new("git");
|
||||
cmd.arg(service).arg("--stateless-rpc");
|
||||
for a in extra_args {
|
||||
cmd.arg(a);
|
||||
}
|
||||
cmd.arg(repo.path())
|
||||
.stdin(std::process::Stdio::piped())
|
||||
.stdout(std::process::Stdio::piped())
|
||||
.stderr(std::process::Stdio::null())
|
||||
.kill_on_drop(true);
|
||||
harden_git_env(&mut cmd);
|
||||
|
||||
let mut child = cmd.spawn().map_err(|e| {
|
||||
error!(error = %e, service = %service, "git subprocess failed to spawn");
|
||||
(StatusCode::INTERNAL_SERVER_ERROR, "git error").into_response()
|
||||
})?;
|
||||
|
||||
// Pump the request body into git's stdin, then close it (EOF). Detached:
|
||||
// the task ends on its own when the body ends or the write fails.
|
||||
let mut stdin = child.stdin.take().expect("stdin piped");
|
||||
tokio::spawn(async move {
|
||||
use futures_util::StreamExt;
|
||||
let mut stream = body.into_data_stream();
|
||||
while let Some(chunk) = stream.next().await {
|
||||
match chunk {
|
||||
Ok(bytes) => {
|
||||
if tokio::io::AsyncWriteExt::write_all(&mut stdin, &bytes)
|
||||
.await
|
||||
.is_err()
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
Err(_) => break,
|
||||
}
|
||||
}
|
||||
drop(stdin); // close stdin → EOF for git
|
||||
});
|
||||
|
||||
let stdout = child.stdout.take().expect("stdout piped");
|
||||
let git_stream = StreamingGit {
|
||||
inner: tokio_util::io::ReaderStream::new(stdout),
|
||||
_child: child,
|
||||
_repo: repo,
|
||||
};
|
||||
|
||||
// Prepend any protocol header (info/refs) ahead of git's stdout. The
|
||||
// prefix is a single ready chunk; the rest streams from the subprocess.
|
||||
let prefix_stream =
|
||||
futures_util::stream::once(
|
||||
async move { Ok::<_, std::io::Error>(bytes::Bytes::from(prefix)) },
|
||||
);
|
||||
let body_stream = futures_util::StreamExt::chain(prefix_stream, git_stream);
|
||||
|
||||
Ok(Response::builder()
|
||||
.status(StatusCode::OK)
|
||||
.header(header::CONTENT_TYPE, content_type)
|
||||
.header(header::CACHE_CONTROL, "no-cache")
|
||||
.body(Body::from_stream(body_stream))
|
||||
.unwrap())
|
||||
}
|
||||
|
||||
/// Build the canonical `application/x-git-{service}-result` response from
|
||||
/// a completed subprocess. For the push path this is **only** reached via
|
||||
/// [`finalize_push`], which is the unique constructor of a push 2xx — the
|
||||
@@ -634,8 +966,6 @@ fn build_git_response(service: &str, output: PackOutput) -> Response {
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
// ── Post-Push Fence ──────────────────────────────────────────────────────────
|
||||
|
||||
/// Per-push state captured between subprocess completion and response
|
||||
/// construction. Constructing a `PushContext` is the only path from a
|
||||
/// push subprocess to a 2xx push response (see [`finalize_push`]) — the
|
||||
@@ -767,13 +1097,11 @@ async fn finalize_push(state: &Arc<AppState>, ctx: PushContext) -> Response {
|
||||
match build_ref_state_event(&inputs, &state.relay_keypair) {
|
||||
Ok(event) => match state.db.insert_event(&event, None).await {
|
||||
Ok((stored, true)) => {
|
||||
let matches = state.sub_registry.fan_out(&stored);
|
||||
for (conn_id, sub_id) in matches {
|
||||
let _ = state.conn_manager.send_to(
|
||||
conn_id,
|
||||
crate::protocol::RelayMessage::event(&sub_id, &stored.event),
|
||||
);
|
||||
}
|
||||
// Routed through the guarded send path for uniformity; the
|
||||
// access gate no-ops for this globally-scoped
|
||||
// (channel_id = None) ref-state event.
|
||||
crate::handlers::event::fan_out_event_to_local_subscribers(state, &stored)
|
||||
.await;
|
||||
info!(
|
||||
owner = %ctx.owner,
|
||||
repo = %ctx.repo_id,
|
||||
@@ -817,8 +1145,6 @@ async fn finalize_push(state: &Arc<AppState>, ctx: PushContext) -> Response {
|
||||
response
|
||||
}
|
||||
|
||||
// ── Router Builder ───────────────────────────────────────────────────────────
|
||||
|
||||
/// Build the git sub-router with its own body limit.
|
||||
///
|
||||
/// Mounted at `/git/{owner}/{repo}/...` with a configurable max pack size.
|
||||
@@ -832,3 +1158,175 @@ pub fn git_router(state: Arc<AppState>) -> Router {
|
||||
.layer(RequestBodyLimitLayer::new(body_limit))
|
||||
.with_state(state)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod track_c_tests {
|
||||
use super::*;
|
||||
use crate::api::git::manifest::Manifest;
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
fn oid_sha1() -> String {
|
||||
"cb09a769da1c01f458fa6959d4e8eded38fac8d3".to_string()
|
||||
}
|
||||
|
||||
fn branches_only_manifest() -> Manifest {
|
||||
let mut refs = BTreeMap::new();
|
||||
refs.insert("refs/heads/feature".to_string(), oid_sha1());
|
||||
refs.insert("refs/heads/main".to_string(), oid_sha1());
|
||||
Manifest {
|
||||
version: 1,
|
||||
head: "refs/heads/main".to_string(),
|
||||
refs,
|
||||
packs: vec!["packs/deadbeef".to_string()],
|
||||
parent: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Split a pkt-line stream into `(len_prefix, payload)` frames, validating
|
||||
/// that each 4-hex length counts itself and that `0000` is a flush.
|
||||
fn parse_pkt_lines(bytes: &[u8]) -> Vec<Vec<u8>> {
|
||||
let mut out = Vec::new();
|
||||
let mut i = 0;
|
||||
while i + 4 <= bytes.len() {
|
||||
let len_hex = std::str::from_utf8(&bytes[i..i + 4]).unwrap();
|
||||
let len = usize::from_str_radix(len_hex, 16).unwrap();
|
||||
if len == 0 {
|
||||
out.push(Vec::new()); // flush marker
|
||||
i += 4;
|
||||
continue;
|
||||
}
|
||||
assert!(len >= 4, "pkt-line length must count its own 4 bytes");
|
||||
let payload = bytes[i + 4..i + len].to_vec();
|
||||
out.push(payload);
|
||||
i += len;
|
||||
}
|
||||
assert_eq!(i, bytes.len(), "pkt-line stream must consume exactly");
|
||||
out
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fast_path_eligible_branches_only() {
|
||||
assert!(fast_path_eligible(&branches_only_manifest()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fast_path_rejects_any_tag() {
|
||||
let mut m = branches_only_manifest();
|
||||
m.refs.insert("refs/tags/v1".to_string(), oid_sha1());
|
||||
// Any tag → subprocess fallback (can't peel annotated tags from manifest).
|
||||
assert!(!fast_path_eligible(&m));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fast_path_rejects_head_not_in_refs() {
|
||||
let mut m = branches_only_manifest();
|
||||
m.head = "refs/heads/nonexistent".to_string();
|
||||
// HEAD must resolve to an advertised ref to emit symref=HEAD:<ref>.
|
||||
assert!(!fast_path_eligible(&m));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fast_path_rejects_unsafe_refname() {
|
||||
let mut m = branches_only_manifest();
|
||||
// A pkt-line-injecting refname (newline) must never reach the emit path;
|
||||
// eligibility is the safety gate → subprocess fallback re-validates.
|
||||
m.refs.insert("refs/heads/evil\nx".to_string(), oid_sha1());
|
||||
assert!(!fast_path_eligible(&m));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fast_path_rejects_malformed_oid() {
|
||||
let mut m = branches_only_manifest();
|
||||
// A non-hex / wrong-length oid must degrade to subprocess, not be emitted.
|
||||
m.refs
|
||||
.insert("refs/heads/bad".to_string(), "not-a-valid-oid".to_string());
|
||||
assert!(!fast_path_eligible(&m));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fast_path_rejects_overlong_refname() {
|
||||
let mut m = branches_only_manifest();
|
||||
// A refname past MAX_FAST_PATH_REFNAME_LEN would overflow the 4-hex
|
||||
// pkt-line length prefix; degrade to subprocess instead.
|
||||
let long = format!("refs/heads/{}", "a".repeat(MAX_FAST_PATH_REFNAME_LEN));
|
||||
m.refs.insert(long, oid_sha1());
|
||||
assert!(!fast_path_eligible(&m));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pkt_line_encodes_max_payload_without_overflow() {
|
||||
// The largest payload that still fits a 4-hex frame length emits a
|
||||
// single valid `ffff` (= 0xffff) frame — the boundary the guard
|
||||
// protects, exercised on the safe side.
|
||||
let payload = vec![b'a'; PKT_LINE_MAX_PAYLOAD];
|
||||
let mut out = Vec::new();
|
||||
pkt_line(&mut out, &payload);
|
||||
assert_eq!(&out[..4], b"ffff", "frame length prefix");
|
||||
assert_eq!(out.len(), 4 + PKT_LINE_MAX_PAYLOAD, "no truncation");
|
||||
let frames = parse_pkt_lines(&out);
|
||||
assert_eq!(frames.len(), 1);
|
||||
assert_eq!(frames[0].len(), PKT_LINE_MAX_PAYLOAD);
|
||||
}
|
||||
|
||||
// The overlong-payload guard degrades to an empty `0004` pkt-line in every
|
||||
// build profile (no 5-hex length, stream stays parseable) and logs at
|
||||
// `error`. Non-panicking, so it's testable directly.
|
||||
#[test]
|
||||
fn pkt_line_overlong_payload_degrades_to_empty_frame() {
|
||||
let payload = vec![b'a'; PKT_LINE_MAX_PAYLOAD + 1];
|
||||
let mut out = Vec::new();
|
||||
pkt_line(&mut out, &payload);
|
||||
// Empty pkt-line, payload dropped — never a malformed 5-hex length.
|
||||
assert_eq!(out, b"0004");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn advertisement_framing_matches_git_oracle_shape() {
|
||||
let body = build_upload_pack_advertisement(&branches_only_manifest());
|
||||
let frames = parse_pkt_lines(&body);
|
||||
|
||||
// Layout: [service header, flush, HEAD line, feature, main, flush]
|
||||
assert_eq!(frames.len(), 6, "frame count");
|
||||
|
||||
// 0: service header
|
||||
assert_eq!(frames[0], b"# service=git-upload-pack\n");
|
||||
// 1: flush after service header
|
||||
assert!(frames[1].is_empty());
|
||||
|
||||
// 2: HEAD line — "<oid> HEAD\0<caps>\n"
|
||||
let head = &frames[2];
|
||||
let nul = head.iter().position(|&b| b == 0).expect("NUL in HEAD line");
|
||||
assert_eq!(&head[..nul], format!("{} HEAD", oid_sha1()).as_bytes());
|
||||
let caps = std::str::from_utf8(&head[nul + 1..head.len() - 1]).unwrap();
|
||||
assert_eq!(*head.last().unwrap(), b'\n');
|
||||
// symref + object-format are the load-bearing caps.
|
||||
assert!(caps.contains("symref=HEAD:refs/heads/main"));
|
||||
assert!(caps.contains("object-format=sha1"));
|
||||
assert!(caps.contains("side-band-64k"));
|
||||
|
||||
// 3,4: refs sorted ascending — feature before main (BTreeMap order),
|
||||
// each "<oid> <refname>\n", no NUL.
|
||||
assert_eq!(
|
||||
frames[3],
|
||||
format!("{} refs/heads/feature\n", oid_sha1()).into_bytes()
|
||||
);
|
||||
assert_eq!(
|
||||
frames[4],
|
||||
format!("{} refs/heads/main\n", oid_sha1()).into_bytes()
|
||||
);
|
||||
|
||||
// 5: trailing flush
|
||||
assert!(frames[5].is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn advertisement_picks_sha256_from_oid_width() {
|
||||
let mut m = branches_only_manifest();
|
||||
let oid256 = "a".repeat(64);
|
||||
m.refs.insert("refs/heads/main".to_string(), oid256.clone());
|
||||
m.refs.insert("refs/heads/feature".to_string(), oid256);
|
||||
let body = build_upload_pack_advertisement(&m);
|
||||
let caps = String::from_utf8_lossy(&body);
|
||||
assert!(caps.contains("object-format=sha256"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,8 +22,6 @@ use sha2::{Digest, Sha256};
|
||||
|
||||
use crate::state::AppState;
|
||||
|
||||
// ── Upload ────────────────────────────────────────────────────────────────────
|
||||
|
||||
/// Axum extractor that validates Blossom auth + API token scopes from headers
|
||||
/// BEFORE the request body is read. This prevents unauthenticated clients from
|
||||
/// forcing the server to buffer up to 50MB of body data.
|
||||
@@ -219,8 +217,6 @@ pub async fn upload_blob(
|
||||
Ok(Json(descriptor))
|
||||
}
|
||||
|
||||
// ── Serve ─────────────────────────────────────────────────────────────────────
|
||||
|
||||
/// Whether a path-segment extension is a safe token.
|
||||
///
|
||||
/// The sidecar's `ext` field is the *authoritative* extension — the serve and
|
||||
@@ -536,8 +532,6 @@ pub async fn head_blob(
|
||||
}
|
||||
}
|
||||
|
||||
// ── Helpers ───────────────────────────────────────────────────────────────────
|
||||
|
||||
/// Resolve the S3 key from a URL path segment.
|
||||
///
|
||||
/// - `sha256.ext` → used as-is (already validated by `validate_media_path`)
|
||||
@@ -753,8 +747,6 @@ mod tests {
|
||||
assert!(validate_media_path("").is_err());
|
||||
}
|
||||
|
||||
// ── Range request parsing ─────────────────────────────────────────────────
|
||||
|
||||
#[test]
|
||||
fn test_parse_byte_range_basic() {
|
||||
assert_eq!(parse_byte_range("bytes=0-499", 1000), Some((0, 499)));
|
||||
|
||||
@@ -9,8 +9,6 @@ pub mod nip05;
|
||||
// Re-export imeta helpers used by ingest pipeline.
|
||||
pub use crate::handlers::imeta::{validate_imeta_tags, verify_imeta_blobs};
|
||||
|
||||
// ── Shared helpers (used by media.rs, bridge.rs) ──────────────────────────────
|
||||
|
||||
use axum::{http::StatusCode, response::Json};
|
||||
|
||||
/// Standard error envelope.
|
||||
|
||||
@@ -52,8 +52,6 @@ const MAX_MISSED_PONGS: u8 = 3;
|
||||
/// Auth timeout.
|
||||
const AUTH_TIMEOUT: Duration = Duration::from_secs(5);
|
||||
|
||||
// ── Route handler ─────────────────────────────────────────────────────────────
|
||||
|
||||
/// WebSocket upgrade handler for `/huddle/:channel_id/audio`.
|
||||
pub async fn ws_audio_handler(
|
||||
State(state): State<Arc<AppState>>,
|
||||
@@ -63,8 +61,6 @@ pub async fn ws_audio_handler(
|
||||
ws.on_upgrade(move |socket| handle_audio_connection(socket, state, channel_id))
|
||||
}
|
||||
|
||||
// ── Auth message shape ────────────────────────────────────────────────────────
|
||||
|
||||
/// Highest huddle audio protocol version this relay understands. Clients are
|
||||
/// allowed to negotiate any version in `1..=CURRENT_PROTOCOL_VERSION`; older
|
||||
/// versions stay supported indefinitely for staged rollouts.
|
||||
@@ -88,12 +84,9 @@ fn default_protocol_version() -> u8 {
|
||||
1
|
||||
}
|
||||
|
||||
// ── Core connection lifecycle ─────────────────────────────────────────────────
|
||||
|
||||
async fn handle_audio_connection(socket: WebSocket, state: Arc<AppState>, channel_id: Uuid) {
|
||||
let (mut ws_send, mut ws_recv) = socket.split();
|
||||
|
||||
// ── Step 1: send challenge ────────────────────────────────────────────────
|
||||
let challenge = generate_challenge();
|
||||
let challenge_msg =
|
||||
serde_json::json!({"type": "challenge", "challenge": challenge}).to_string();
|
||||
@@ -105,7 +98,6 @@ async fn handle_audio_connection(socket: WebSocket, state: Arc<AppState>, channe
|
||||
return;
|
||||
}
|
||||
|
||||
// ── Step 2: await auth (5s timeout) ──────────────────────────────────────
|
||||
let auth_result = tokio::time::timeout(AUTH_TIMEOUT, async {
|
||||
while let Some(Ok(msg)) = ws_recv.next().await {
|
||||
if let WsMessage::Text(text) = msg {
|
||||
@@ -160,7 +152,6 @@ async fn handle_audio_connection(socket: WebSocket, state: Arc<AppState>, channe
|
||||
let pubkey_bytes = pubkey.to_bytes().to_vec();
|
||||
let parent_channel_id = auth_msg.parent_channel_id;
|
||||
|
||||
// ── Relay membership gate (with NIP-OA fallback) ────────────────────────────
|
||||
if crate::api::relay_members::enforce_relay_membership(
|
||||
&state,
|
||||
pubkey.as_bytes(),
|
||||
@@ -180,7 +171,6 @@ async fn handle_audio_connection(socket: WebSocket, state: Arc<AppState>, channe
|
||||
return;
|
||||
}
|
||||
|
||||
// ── Step 3: membership check / auto-add ───────────────────────────────────
|
||||
if let Err(e) = ensure_membership(&state, channel_id, &pubkey_bytes, parent_channel_id).await {
|
||||
warn!(channel_id = %channel_id, pubkey = %pubkey_hex, "audio membership denied: {e}");
|
||||
let _ = ws_send
|
||||
@@ -193,7 +183,6 @@ async fn handle_audio_connection(socket: WebSocket, state: Arc<AppState>, channe
|
||||
return;
|
||||
}
|
||||
|
||||
// ── Step 4: join room ─────────────────────────────────────────────────────
|
||||
let room = state.audio_rooms.get_or_create(channel_id);
|
||||
|
||||
// Re-check archived status after obtaining the room. This closes the
|
||||
@@ -309,7 +298,6 @@ async fn handle_audio_connection(socket: WebSocket, state: Arc<AppState>, channe
|
||||
"audio peer joined"
|
||||
);
|
||||
|
||||
// ── Step 5: broadcast joined + send welcome ───────────────────────────────
|
||||
let peers_snapshot: Vec<serde_json::Value> = room
|
||||
.peer_pubkeys()
|
||||
.into_iter()
|
||||
@@ -326,7 +314,6 @@ async fn handle_audio_connection(socket: WebSocket, state: Arc<AppState>, channe
|
||||
|
||||
room.broadcast_control(joined_msg);
|
||||
|
||||
// ── Step 6: emit kind:48101 (PARTICIPANT_JOINED) ──────────────────────────
|
||||
let parent_id_for_event = parent_channel_id.unwrap_or(channel_id);
|
||||
emit_participant_event(
|
||||
&state,
|
||||
@@ -337,7 +324,6 @@ async fn handle_audio_connection(socket: WebSocket, state: Arc<AppState>, channe
|
||||
)
|
||||
.await;
|
||||
|
||||
// ── Step 7: spawn send + heartbeat loops ──────────────────────────────────
|
||||
let cancel = CancellationToken::new();
|
||||
let missed_pongs = Arc::new(AtomicU8::new(0));
|
||||
|
||||
@@ -353,7 +339,6 @@ async fn handle_audio_connection(socket: WebSocket, state: Arc<AppState>, channe
|
||||
let hb_missed = Arc::clone(&missed_pongs);
|
||||
let heartbeat_task = tokio::spawn(heartbeat_loop(ctrl_tx.clone(), hb_missed, hb_cancel));
|
||||
|
||||
// ── Step 8: audio forward loop (room channels → WS send channels) ────────
|
||||
let fwd_cancel = cancel.child_token();
|
||||
let forward_task = tokio::spawn(audio_forward_loop(
|
||||
audio_rx,
|
||||
@@ -363,7 +348,6 @@ async fn handle_audio_connection(socket: WebSocket, state: Arc<AppState>, channe
|
||||
fwd_cancel,
|
||||
));
|
||||
|
||||
// ── Step 9: recv loop (blocks until disconnect) ───────────────────────────
|
||||
recv_loop(
|
||||
ws_recv,
|
||||
Arc::clone(&room),
|
||||
@@ -375,7 +359,6 @@ async fn handle_audio_connection(socket: WebSocket, state: Arc<AppState>, channe
|
||||
)
|
||||
.await;
|
||||
|
||||
// ── Cleanup ───────────────────────────────────────────────────────────────
|
||||
cancel.cancel();
|
||||
let _ = send_task.await;
|
||||
let _ = heartbeat_task.await;
|
||||
@@ -439,8 +422,6 @@ async fn handle_audio_connection(socket: WebSocket, state: Arc<AppState>, channe
|
||||
);
|
||||
}
|
||||
|
||||
// ── Recv loop ─────────────────────────────────────────────────────────────────
|
||||
|
||||
async fn recv_loop(
|
||||
mut ws_recv: futures_util::stream::SplitStream<WebSocket>,
|
||||
room: Arc<crate::audio::room::Room>,
|
||||
@@ -540,8 +521,6 @@ async fn recv_loop(
|
||||
}
|
||||
}
|
||||
|
||||
// ── Send loop ─────────────────────────────────────────────────────────────────
|
||||
|
||||
/// Outbound send loop with control-frame priority (matches connection.rs pattern).
|
||||
///
|
||||
/// Control frames (Ping, Pong, Close, control JSON) are drained first on every
|
||||
@@ -576,7 +555,6 @@ async fn send_loop(
|
||||
}
|
||||
}
|
||||
|
||||
// ── Audio forward loop ────────────────────────────────────────────────────────
|
||||
// Bridges the room's mpsc channel to the WS send channel.
|
||||
|
||||
/// Bridges room per-peer channels → WS send channels.
|
||||
@@ -615,8 +593,6 @@ async fn audio_forward_loop(
|
||||
}
|
||||
}
|
||||
|
||||
// ── Heartbeat loop ────────────────────────────────────────────────────────────
|
||||
|
||||
async fn heartbeat_loop(
|
||||
ws_tx: mpsc::Sender<WsMessage>,
|
||||
missed_pongs: Arc<AtomicU8>,
|
||||
@@ -643,8 +619,6 @@ async fn heartbeat_loop(
|
||||
}
|
||||
}
|
||||
|
||||
// ── Membership helper ─────────────────────────────────────────────────────────
|
||||
|
||||
async fn ensure_membership(
|
||||
state: &AppState,
|
||||
channel_id: Uuid,
|
||||
@@ -714,8 +688,6 @@ async fn ensure_membership(
|
||||
Err("not a member".into())
|
||||
}
|
||||
|
||||
// ── Lifecycle event helper ────────────────────────────────────────────────────
|
||||
|
||||
async fn emit_participant_event(
|
||||
state: &AppState,
|
||||
kind: Kind,
|
||||
@@ -788,27 +760,11 @@ async fn emit_participant_event(
|
||||
// double-delivery when the event echoes back through the subscriber loop.
|
||||
state.mark_local_event(&event.id);
|
||||
|
||||
// 3. Local fan-out to WS subscribers on this node (same pattern as
|
||||
// 3. Local fan-out to WS subscribers on this node, through the guarded send
|
||||
// path so a stale subscription on a removed/non-member connection cannot
|
||||
// receive this channel's audio lifecycle event (same gate as
|
||||
// dispatch_persistent_event in the ingest handler).
|
||||
let matches = state.sub_registry.fan_out(&stored);
|
||||
if !matches.is_empty() {
|
||||
let event_json = serde_json::to_string(&event)
|
||||
.expect("nostr::Event serialization is infallible for well-formed events");
|
||||
let mut drop_count = 0u32;
|
||||
for (target_conn_id, sub_id) in &matches {
|
||||
let msg = format!(r#"["EVENT","{}",{}]"#, sub_id, event_json);
|
||||
if !state.conn_manager.send_to(*target_conn_id, msg) {
|
||||
drop_count += 1;
|
||||
}
|
||||
}
|
||||
if drop_count > 0 {
|
||||
warn!(
|
||||
event_id = %event_id_hex,
|
||||
drop_count,
|
||||
"audio lifecycle fan-out: {drop_count} connection(s) dropped"
|
||||
);
|
||||
}
|
||||
}
|
||||
crate::handlers::event::fan_out_event_to_local_subscribers(state, &stored).await;
|
||||
|
||||
// 4. Cross-node broadcast via Redis pub/sub.
|
||||
if let Err(e) = state.pubsub.publish_event(parent_channel_id, &event).await {
|
||||
|
||||
@@ -109,7 +109,6 @@ pub struct Config {
|
||||
/// 60 seconds after the last message.
|
||||
pub ephemeral_ttl_override: Option<i32>,
|
||||
|
||||
// ── Git server configuration ─────────────────────────────────────────────
|
||||
/// Root directory for the relay's local git state. No per-repo bare repos
|
||||
/// live here — runtime reads/writes hydrate ephemeral repos from object
|
||||
/// storage. Holds only the name-reservation index at `{git_repo_path}/.names/`.
|
||||
@@ -124,7 +123,6 @@ pub struct Config {
|
||||
/// Used to authenticate internal policy endpoint requests.
|
||||
pub git_hook_hmac_secret: String,
|
||||
|
||||
// ── Web UI serving ────────────────────────────────────────────────────────
|
||||
/// Optional path to the web UI `dist/` directory.
|
||||
/// When set, the relay serves the SPA from this directory for browser requests.
|
||||
/// When unset, no static file serving happens (relay behaves as before).
|
||||
|
||||
@@ -150,8 +150,6 @@ async fn persist_command_event(
|
||||
}
|
||||
}
|
||||
|
||||
// ── Tag extraction helpers ───────────────────────────────────────────────────
|
||||
|
||||
/// Extract all `p` tag values (hex pubkeys) from an event.
|
||||
fn extract_p_tags(event: &Event) -> Vec<String> {
|
||||
event
|
||||
@@ -228,8 +226,6 @@ fn compute_definition_hash(json_str: &str) -> Vec<u8> {
|
||||
Sha256::digest(json_str.as_bytes()).to_vec()
|
||||
}
|
||||
|
||||
// ── DM commands (41010–41012) ────────────────────────────────────────────────
|
||||
|
||||
async fn handle_dm_open(
|
||||
state: &Arc<AppState>,
|
||||
event: &Event,
|
||||
@@ -553,8 +549,6 @@ async fn handle_dm_hide(
|
||||
})
|
||||
}
|
||||
|
||||
// ── Workflow commands ─────────────────────────────────────────────────────────
|
||||
|
||||
async fn handle_workflow_def(
|
||||
state: &Arc<AppState>,
|
||||
event: &Event,
|
||||
@@ -792,8 +786,6 @@ async fn handle_workflow_trigger(
|
||||
})
|
||||
}
|
||||
|
||||
// ── Approval commands ────────────────────────────────────────────────────────
|
||||
|
||||
/// Enforce the approver_spec field against the requesting pubkey.
|
||||
///
|
||||
/// Accepted specs:
|
||||
@@ -1067,8 +1059,6 @@ async fn handle_approval_deny(
|
||||
})
|
||||
}
|
||||
|
||||
// ── Approval resume helper ───────────────────────────────────────────────────
|
||||
|
||||
/// Resume a suspended workflow run after an approval gate has been granted.
|
||||
async fn resume_workflow_after_approval(
|
||||
engine: Arc<buzz_workflow::WorkflowEngine>,
|
||||
|
||||
@@ -31,10 +31,12 @@ pub async fn handle_count(
|
||||
state: Arc<AppState>,
|
||||
) {
|
||||
// Require auth
|
||||
let pubkey_bytes = {
|
||||
let (pubkey_bytes, token_channel_ids) = {
|
||||
let auth = conn.auth_state.read().await;
|
||||
match &*auth {
|
||||
AuthState::Authenticated(ctx) => ctx.pubkey.to_bytes().to_vec(),
|
||||
AuthState::Authenticated(ctx) => {
|
||||
(ctx.pubkey.to_bytes().to_vec(), ctx.channel_ids.clone())
|
||||
}
|
||||
_ => {
|
||||
conn.send(RelayMessage::closed(
|
||||
&sub_id,
|
||||
@@ -71,7 +73,8 @@ pub async fn handle_count(
|
||||
}
|
||||
|
||||
// Get channels this user can access — same enforcement as WS REQ handler.
|
||||
let accessible_channels = match state.get_accessible_channel_ids_cached(&pubkey_bytes).await {
|
||||
let mut accessible_channels = match state.get_accessible_channel_ids_cached(&pubkey_bytes).await
|
||||
{
|
||||
Ok(ids) => ids,
|
||||
Err(e) => {
|
||||
warn!(sub_id = %sub_id, "Failed to get accessible channels: {e}");
|
||||
@@ -79,6 +82,14 @@ pub async fn handle_count(
|
||||
return;
|
||||
}
|
||||
};
|
||||
// Narrow to the token's channel scope, mirroring the WS REQ handler. Without
|
||||
// this, a scoped token would COUNT events in channels outside its scope via
|
||||
// the no-channel-filter SQL pushdown below (which counts every accessible
|
||||
// channel). The per-filter targeted-channel repair is bounded by the same
|
||||
// scope through `resolve_request_local_access`'s `token_allows` argument.
|
||||
if let Some(allowed) = token_channel_ids.as_deref() {
|
||||
accessible_channels.retain(|channel_id| allowed.contains(channel_id));
|
||||
}
|
||||
|
||||
// For each filter, count matching events with channel access enforcement.
|
||||
let mut total: u64 = 0;
|
||||
@@ -89,8 +100,31 @@ pub async fn handle_count(
|
||||
let needs_author_only_filtering = super::req::filter_can_match_author_only_kinds(filter);
|
||||
|
||||
if let Some(ch_id) = extract_channel_from_filter(filter) {
|
||||
// Filter targets a specific channel — verify access.
|
||||
if !accessible_channels.contains(&ch_id) {
|
||||
// Filter targets a specific channel — verify access. Mirrors the WS
|
||||
// REQ handler: a cache-negative may be a stale miss on a non-writer
|
||||
// pod, so confirm uncached and repair the Vec request-locally via
|
||||
// `super::req::resolve_request_local_access` (so a just-added channel
|
||||
// is counted, and any later filter on the same channel sees it too).
|
||||
let db_is_member = if accessible_channels.contains(&ch_id) {
|
||||
None
|
||||
} else {
|
||||
match state.db.is_member(ch_id, &pubkey_bytes).await {
|
||||
Ok(member) => Some(member),
|
||||
Err(e) => {
|
||||
warn!(sub_id = %sub_id, "Channel membership confirmation failed: {e}");
|
||||
conn.send(RelayMessage::closed(&sub_id, "error: database error"));
|
||||
return;
|
||||
}
|
||||
}
|
||||
};
|
||||
if !super::req::resolve_request_local_access(
|
||||
&mut accessible_channels,
|
||||
ch_id,
|
||||
token_channel_ids
|
||||
.as_deref()
|
||||
.is_none_or(|allowed| allowed.contains(&ch_id)),
|
||||
db_is_member,
|
||||
) {
|
||||
continue; // Skip filters targeting inaccessible channels.
|
||||
}
|
||||
// Channel is accessible — count with pushability check.
|
||||
|
||||
@@ -57,7 +57,7 @@ fn bounded_kind_label(kind: u32) -> String {
|
||||
/// survives on another node after an open->private flip, its events are not
|
||||
/// delivered here.
|
||||
pub async fn filter_fanout_by_access(
|
||||
state: &Arc<AppState>,
|
||||
state: &AppState,
|
||||
stored_event: &StoredEvent,
|
||||
matches: Vec<(crate::subscription::ConnId, crate::subscription::SubId)>,
|
||||
) -> Vec<(crate::subscription::ConnId, crate::subscription::SubId)> {
|
||||
@@ -112,6 +112,54 @@ pub async fn filter_fanout_by_access(
|
||||
allowed
|
||||
}
|
||||
|
||||
/// Deliver one event to this relay's local subscribers through the access gate.
|
||||
///
|
||||
/// This is the single guarded send path for relay-local EVENT delivery. It runs
|
||||
/// `fan_out()` to find matching subscriptions, then `filter_fanout_by_access()`
|
||||
/// to drop recipients without access (private-channel non-members, author-only
|
||||
/// kinds delivered to non-authors), then writes the EVENT frames. The invariant
|
||||
/// it enforces: a registered subscription is never sufficient for delivery —
|
||||
/// delivery always revalidates access on the sending pod, so a stale
|
||||
/// subscription surviving a membership/visibility change (e.g. after an
|
||||
/// open→private flip or a cross-pod cache lag) cannot leak events.
|
||||
///
|
||||
/// All relay-local live fan-out routes through here. The two exceptions are
|
||||
/// `dispatch_persistent_event` (persistent ingest) and `fan_out_pubsub_event`
|
||||
/// (Redis cross-node), which call `filter_fanout_by_access` inline: the former
|
||||
/// layers an additional per-recipient DM-visibility-owner gate on top, the
|
||||
/// latter skips local echoes — both are equivalent to this helper plus their
|
||||
/// own extra step.
|
||||
pub(crate) async fn fan_out_event_to_local_subscribers(state: &AppState, stored: &StoredEvent) {
|
||||
let matches = state.sub_registry.fan_out(stored);
|
||||
let matches = filter_fanout_by_access(state, stored, matches).await;
|
||||
metrics::histogram!("buzz_fanout_recipients").record(matches.len() as f64);
|
||||
if matches.is_empty() {
|
||||
return;
|
||||
}
|
||||
|
||||
let event_json = match serde_json::to_string(&stored.event) {
|
||||
Ok(json) => json,
|
||||
Err(e) => {
|
||||
error!(event_id = %stored.event.id.to_hex(), "Failed to serialize event for fan-out: {e}");
|
||||
return;
|
||||
}
|
||||
};
|
||||
let mut drop_count = 0u32;
|
||||
for (conn_id, sub_id) in &matches {
|
||||
let msg = format!(r#"["EVENT","{}",{}]"#, sub_id, event_json);
|
||||
if !state.conn_manager.send_to(*conn_id, msg) {
|
||||
drop_count += 1;
|
||||
}
|
||||
}
|
||||
if drop_count > 0 {
|
||||
tracing::warn!(
|
||||
event_id = %stored.event.id.to_hex(),
|
||||
drop_count,
|
||||
"fan-out: {drop_count} connection(s) cancelled due to full/closed buffers"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Fan out one event received from Redis pub/sub to this relay's local subscribers.
|
||||
pub async fn fan_out_pubsub_event(state: &Arc<AppState>, channel_event: buzz_pubsub::ChannelEvent) {
|
||||
// Nil UUID is the sentinel for channel-less global events (see
|
||||
@@ -312,7 +360,6 @@ pub async fn handle_event(event: Event, conn: Arc<ConnectionState>, state: Arc<A
|
||||
debug!(event_id = %event_id_hex, kind = kind_u32, "EVENT");
|
||||
metrics::counter!("buzz_events_received_total", "kind" => kind_str.clone()).increment(1);
|
||||
|
||||
// ── Extract auth from WS connection state ────────────────────────────
|
||||
let (conn_id, pubkey_bytes, auth_pubkey, scopes, channel_ids) = {
|
||||
let auth = conn.auth_state.read().await;
|
||||
match &*auth {
|
||||
@@ -335,7 +382,6 @@ pub async fn handle_event(event: Event, conn: Arc<ConnectionState>, state: Arc<A
|
||||
}
|
||||
};
|
||||
|
||||
// ── Pubkey / auth identity match (all events) ─────────────────────
|
||||
// Must run before both ephemeral and persistent branches. Persistent
|
||||
// events get a second check inside ingest_event() (step 3), but
|
||||
// ephemeral events bypass the pipeline entirely.
|
||||
@@ -351,7 +397,6 @@ pub async fn handle_event(event: Event, conn: Arc<ConnectionState>, state: Arc<A
|
||||
return;
|
||||
}
|
||||
|
||||
// ── Blocked kinds (both ephemeral and persistent) ─────────────────
|
||||
if kind_u32 == buzz_core::kind::KIND_AUTH {
|
||||
reject("invalid");
|
||||
conn.send(RelayMessage::ok(
|
||||
@@ -362,7 +407,6 @@ pub async fn handle_event(event: Event, conn: Arc<ConnectionState>, state: Arc<A
|
||||
return;
|
||||
}
|
||||
|
||||
// ── Agent observer frames are owner-scoped, encrypted, and never stored ──
|
||||
if kind_u32 == KIND_AGENT_OBSERVER_FRAME {
|
||||
if !scopes.is_empty()
|
||||
&& !scopes.contains(&buzz_auth::Scope::MessagesWrite)
|
||||
@@ -380,7 +424,6 @@ pub async fn handle_event(event: Event, conn: Arc<ConnectionState>, state: Arc<A
|
||||
return;
|
||||
}
|
||||
|
||||
// ── Ephemeral events are WS-only (never stored) ──────────────────────
|
||||
// Scope enforcement for ephemeral kinds: require MessagesWrite or
|
||||
// ProxySubmit. Persistent events skip this gate and rely on
|
||||
// ingest_event()'s per-kind scope allowlist instead, so a token with
|
||||
@@ -427,7 +470,6 @@ pub async fn handle_event(event: Event, conn: Arc<ConnectionState>, state: Arc<A
|
||||
return;
|
||||
}
|
||||
|
||||
// ── Persistent events → ingest pipeline ──────────────────────────────
|
||||
let ingest_auth = IngestAuth::Nip42 {
|
||||
pubkey: auth_pubkey,
|
||||
scopes,
|
||||
@@ -594,27 +636,12 @@ async fn handle_ephemeral_event(
|
||||
warn!(conn_id = %conn_id, event_id = %event_id_hex, "Ephemeral publish failed: {e}");
|
||||
}
|
||||
|
||||
// Direct fan-out to local WS subscribers.
|
||||
// Direct fan-out to local WS subscribers, through the guarded send path
|
||||
// so a stale subscription on a removed/non-member connection cannot
|
||||
// receive this private-channel ephemeral event.
|
||||
// Pass the channel_id so fan_out() uses the channel-kind index.
|
||||
let stored_event = StoredEvent::new(event.clone(), Some(ch_id));
|
||||
let matches = state.sub_registry.fan_out(&stored_event);
|
||||
metrics::histogram!("buzz_fanout_recipients").record(matches.len() as f64);
|
||||
let event_json = serde_json::to_string(&event)
|
||||
.expect("nostr::Event serialization is infallible for well-formed events");
|
||||
let mut drop_count = 0u32;
|
||||
for (target_conn_id, sub_id) in &matches {
|
||||
let msg = format!(r#"["EVENT","{}",{}]"#, sub_id, event_json);
|
||||
if !state.conn_manager.send_to(*target_conn_id, msg) {
|
||||
drop_count += 1;
|
||||
}
|
||||
}
|
||||
if drop_count > 0 {
|
||||
tracing::warn!(
|
||||
event_id = %event_id_hex,
|
||||
drop_count,
|
||||
"fan-out: {drop_count} connection(s) cancelled due to full/closed buffers"
|
||||
);
|
||||
}
|
||||
fan_out_event_to_local_subscribers(&state, &stored_event).await;
|
||||
} else {
|
||||
// Channel-less ephemeral events (e.g., NIP-AB pairing kind:24134).
|
||||
//
|
||||
@@ -631,27 +658,12 @@ async fn handle_ephemeral_event(
|
||||
warn!(conn_id = %conn_id, event_id = %event_id_hex, "Ephemeral global publish failed: {e}");
|
||||
}
|
||||
|
||||
// Direct fan-out to local WS subscribers.
|
||||
// Pass channel_id=None so fan_out() uses the global subscriber index.
|
||||
// Direct fan-out to local WS subscribers through the guarded send path.
|
||||
// Pass channel_id=None so fan_out() uses the global subscriber index;
|
||||
// filter_fanout_by_access no-ops for channel-less events except the
|
||||
// author-only-kind gate.
|
||||
let stored_event = StoredEvent::new(event.clone(), None);
|
||||
let matches = state.sub_registry.fan_out(&stored_event);
|
||||
metrics::histogram!("buzz_fanout_recipients").record(matches.len() as f64);
|
||||
let event_json = serde_json::to_string(&event)
|
||||
.expect("nostr::Event serialization is infallible for well-formed events");
|
||||
let mut drop_count = 0u32;
|
||||
for (target_conn_id, sub_id) in &matches {
|
||||
let msg = format!(r#"["EVENT","{}",{}]"#, sub_id, event_json);
|
||||
if !state.conn_manager.send_to(*target_conn_id, msg) {
|
||||
drop_count += 1;
|
||||
}
|
||||
}
|
||||
if drop_count > 0 {
|
||||
tracing::warn!(
|
||||
event_id = %event_id_hex,
|
||||
drop_count,
|
||||
"fan-out: {drop_count} connection(s) cancelled due to full/closed buffers"
|
||||
);
|
||||
}
|
||||
fan_out_event_to_local_subscribers(&state, &stored_event).await;
|
||||
}
|
||||
|
||||
conn.send(RelayMessage::ok(event_id_hex, true, ""));
|
||||
@@ -806,19 +818,6 @@ async fn handle_agent_observer_event(
|
||||
}
|
||||
}
|
||||
|
||||
let event_json = match serde_json::to_string(&event) {
|
||||
Ok(json) => json,
|
||||
Err(e) => {
|
||||
error!(event_id = %event_id_hex, "Failed to serialize agent observer event: {e}");
|
||||
conn.send(RelayMessage::ok(
|
||||
event_id_hex,
|
||||
false,
|
||||
"error: internal server error",
|
||||
));
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
state.mark_local_event(&event.id);
|
||||
if let Err(e) = state.pubsub.publish_event(uuid::Uuid::nil(), &event).await {
|
||||
state.local_event_ids.invalidate(&event.id.to_bytes());
|
||||
@@ -826,31 +825,14 @@ async fn handle_agent_observer_event(
|
||||
}
|
||||
|
||||
let stored_event = StoredEvent::new(event.clone(), None);
|
||||
let matches = state.sub_registry.fan_out(&stored_event);
|
||||
metrics::histogram!("buzz_fanout_recipients").record(matches.len() as f64);
|
||||
debug!(
|
||||
event_id = %event_id_hex,
|
||||
agent = %route.agent.to_hex(),
|
||||
owner = %route.owner.to_hex(),
|
||||
direction = ?route.direction,
|
||||
match_count = matches.len(),
|
||||
"Agent observer fan-out"
|
||||
);
|
||||
|
||||
let mut drop_count = 0u32;
|
||||
for (target_conn_id, sub_id) in &matches {
|
||||
let msg = format!(r#"["EVENT","{}",{}]"#, sub_id, event_json);
|
||||
if !state.conn_manager.send_to(*target_conn_id, msg) {
|
||||
drop_count += 1;
|
||||
}
|
||||
}
|
||||
if drop_count > 0 {
|
||||
tracing::warn!(
|
||||
event_id = %event_id_hex,
|
||||
drop_count,
|
||||
"agent observer fan-out: {drop_count} connection(s) cancelled due to full/closed buffers"
|
||||
);
|
||||
}
|
||||
fan_out_event_to_local_subscribers(&state, &stored_event).await;
|
||||
|
||||
conn.send(RelayMessage::ok(event_id_hex, true, ""));
|
||||
}
|
||||
@@ -1039,7 +1021,7 @@ mod tests {
|
||||
use std::sync::Arc;
|
||||
|
||||
use axum::extract::ws::Message;
|
||||
use buzz_core::kind::KIND_PRESENCE_UPDATE;
|
||||
use buzz_core::kind::{KIND_MEMBER_ADDED_NOTIFICATION, KIND_PRESENCE_UPDATE};
|
||||
use buzz_pubsub::ChannelEvent;
|
||||
use nostr::{EventBuilder, Filter, Keys, Kind};
|
||||
use tokio::sync::{mpsc, Mutex};
|
||||
@@ -1053,9 +1035,11 @@ mod tests {
|
||||
super::fanout_access::test_state().await
|
||||
}
|
||||
|
||||
fn register_presence_sub(
|
||||
fn register_global_sub(
|
||||
state: &AppState,
|
||||
sub_id: &str,
|
||||
filter: Filter,
|
||||
pubkey: Option<Vec<u8>>,
|
||||
) -> (Uuid, mpsc::Receiver<Message>) {
|
||||
let conn_id = Uuid::new_v4();
|
||||
let (tx, rx) = mpsc::channel(10);
|
||||
@@ -1065,22 +1049,60 @@ mod tests {
|
||||
CancellationToken::new(),
|
||||
Arc::new(AtomicU8::new(0)),
|
||||
Arc::new(Mutex::new(HashMap::new())),
|
||||
3,
|
||||
);
|
||||
state.sub_registry.register(
|
||||
conn_id,
|
||||
sub_id.to_string(),
|
||||
vec![Filter::new().kind(Kind::Custom(KIND_PRESENCE_UPDATE as u16))],
|
||||
None,
|
||||
);
|
||||
if let Some(pubkey) = pubkey {
|
||||
state.conn_manager.set_authenticated_pubkey(conn_id, pubkey);
|
||||
}
|
||||
state
|
||||
.sub_registry
|
||||
.register(conn_id, sub_id.to_string(), vec![filter], None);
|
||||
(conn_id, rx)
|
||||
}
|
||||
|
||||
fn register_presence_sub(
|
||||
state: &AppState,
|
||||
sub_id: &str,
|
||||
) -> (Uuid, mpsc::Receiver<Message>) {
|
||||
register_global_sub(
|
||||
state,
|
||||
sub_id,
|
||||
Filter::new().kind(Kind::Custom(KIND_PRESENCE_UPDATE as u16)),
|
||||
None,
|
||||
)
|
||||
}
|
||||
|
||||
fn register_membership_sub(
|
||||
state: &AppState,
|
||||
sub_id: &str,
|
||||
target: &Keys,
|
||||
) -> (Uuid, mpsc::Receiver<Message>) {
|
||||
register_global_sub(
|
||||
state,
|
||||
sub_id,
|
||||
Filter::new()
|
||||
.kind(Kind::Custom(KIND_MEMBER_ADDED_NOTIFICATION as u16))
|
||||
.pubkey(target.public_key()),
|
||||
Some(target.public_key().to_bytes().to_vec()),
|
||||
)
|
||||
}
|
||||
|
||||
fn presence_event(status: &str) -> nostr::Event {
|
||||
EventBuilder::new(Kind::Custom(KIND_PRESENCE_UPDATE as u16), status)
|
||||
.sign_with_keys(&Keys::generate())
|
||||
.expect("sign presence")
|
||||
}
|
||||
|
||||
fn membership_event(target: &Keys, channel_id: Uuid) -> nostr::Event {
|
||||
EventBuilder::new(Kind::Custom(KIND_MEMBER_ADDED_NOTIFICATION as u16), "{}")
|
||||
.tags([
|
||||
nostr::Tag::parse(["p", &target.public_key().to_hex()]).expect("p tag"),
|
||||
nostr::Tag::parse(["h", &channel_id.to_string()]).expect("h tag"),
|
||||
])
|
||||
.sign_with_keys(&Keys::generate())
|
||||
.expect("sign membership notification")
|
||||
}
|
||||
|
||||
fn event_from_ws_message(msg: Message) -> nostr::Event {
|
||||
let Message::Text(text) = msg else {
|
||||
panic!("expected text ws message");
|
||||
@@ -1133,6 +1155,39 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn global_membership_pubsub_event_fans_out_by_p_tag() {
|
||||
let state = test_state().await;
|
||||
let target = Keys::generate();
|
||||
let other = Keys::generate();
|
||||
let (_target_conn, mut target_rx) =
|
||||
register_membership_sub(&state, "membership-target", &target);
|
||||
let (_other_conn, mut other_rx) =
|
||||
register_membership_sub(&state, "membership-other", &other);
|
||||
let event = membership_event(&target, Uuid::new_v4());
|
||||
let event_id = event.id;
|
||||
|
||||
fan_out_pubsub_event(
|
||||
&state,
|
||||
ChannelEvent {
|
||||
channel_id: Uuid::nil(),
|
||||
event,
|
||||
},
|
||||
)
|
||||
.await;
|
||||
|
||||
let delivered = event_from_ws_message(
|
||||
target_rx
|
||||
.try_recv()
|
||||
.expect("target receives membership notification"),
|
||||
);
|
||||
assert_eq!(delivered.id, event_id);
|
||||
assert!(
|
||||
other_rx.try_recv().is_err(),
|
||||
"membership notification should only reach matching #p subscribers"
|
||||
);
|
||||
}
|
||||
|
||||
async fn redis_url_if_available() -> Option<String> {
|
||||
let redis_url =
|
||||
std::env::var("REDIS_URL").unwrap_or_else(|_| "redis://127.0.0.1:6379".to_string());
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user