diff --git a/docs/CHIT_INTEGRATION.md b/docs/CHIT_INTEGRATION.md new file mode 100644 index 0000000..172d411 --- /dev/null +++ b/docs/CHIT_INTEGRATION.md @@ -0,0 +1,225 @@ +# CHIT Integration + +> Contract for publishing CHIT geometry/attribution events from PMOVES-ToKenism-Multi onto the PMOVES.AI Geometry Bus (NATS). This document is referenced by the parent `POWERFULMOVES/PMOVES.AI` repo's NATS subject catalog (`pmoves/.claude/context/geometry-nats-subjects.md`). + +## Overview + +ToKenism runs weekly economic simulations whose attribution outcomes are exported as **CHIT geometry documents (CGP)** and broadcast on the shared PMOVES.AI NATS bus. The integration is publish-only from this repo's simulation side; parent services (publisher-discord, Hi-RAG v2, Shape Store, consciousness service) consume the events. + +**Wiring status:** the publisher is fully implemented and test-covered, but has **no production call site yet** — `CHITNATSPublisher` is currently exercised only by tests and re-exports. Simulator wiring is planned; until then treat these subjects as the forward contract for consumers. + +Implementation lives in: + +- `integrations/contracts/chit/chit-nats-publisher.ts` — validated publisher (`CHITNATSPublisher`) +- `integrations/contracts/chit/cgp-generator.ts` — CGP document generation +- `integrations/nats/nats-client.ts` — connection/reconnect handling +- `contracts/schemas/tokenism/*.v1.schema.json` — JSON Schemas (AJV-validated) + +## NATS Subjects + +All subjects are published (not subscribed) by this repo and match the parent Geometry Bus subject catalog exactly: + +| Subject | Trigger | Required payload keys | Key optional keys | +|---|---|---|---| +| `tokenism.swarm.population.v1` | Per-generation swarm progress during CGP optimization | `namespace, modality, pack_id, status, timestamp` | `population_id, generation, best_fitness, metrics` | +| `tokenism.attribution.recorded.v1` | Per-member CHIT attribution recorded in a simulation week | `chit_id, address, action, amount, week, timestamp` | `category, merkle_root` | +| `tokenism.cgp.weekly.v1` | Weekly CGP export (end-of-week attribution summary) | `week, cgp, super_node_count, total_attributions, cgp_spec` | `gini, poverty_rate` | +| `tokenism.cgp.ready.v1` | CGP document ready for downstream consumption (also used by parent research services: DeepResearch / SupaSerch CGP publish) | `cgp, super_node_count, source, cgp_spec, timestamp` | `week` | + +The CGP document embedded in `cgp` follows the spec versions accepted by the bus — per the `spec` enum in `contracts/schemas/geometry/cgp.v1.schema.json`, that is `chit.cgp.v0.2` (current weekly export) or `chit.cgp.v1.0`. Anything else is rejected before publication. + +## Payload Reference + +### `tokenism.cgp.weekly.v1` + +```json +{ + "week": 12, + "cgp": { + "spec": "chit.cgp.v0.2", + "summary": "ToKenism Week 12 Economic Simulation", + "created_at": "2026-01-05T12:00:00Z", + "updated_at": "2026-01-05T12:00:00Z", + "super_nodes": [ + { + "id": "grotoken-week-12", + "label": "GroToken Distribution", + "summary": "GroToken rewards issued in week 12", + "x": 0.0, + "y": 0.0, + "r": 0.3, + "constellations": [ + { + "id": "attr-week-12", + "summary": "Week 12 attribution cluster", + "points": [ + { + "id": "attr-grotoken-week-12-0001", + "modality": "economic-sim", + "ref_id": "member-001", + "summary": "GroToken reward for cooperative purchase", + "x": 0.2, + "y": 0.2, + "conf": 0.98 + } + ] + } + ] + } + ], + "meta": { + "attribution": { + "total_attributions": 1240 + } + } + }, + "super_node_count": 1, + "total_attributions": 1240, + "cgp_spec": "chit.cgp.v0.2", + "gini": 0.34, + "poverty_rate": 0.11 +} +``` + +### `tokenism.attribution.recorded.v1` + +```json +{ + "chit_id": "chit-2026-w12-000123", + "address": "0xabc...def", + "action": "grotoken_reward", + "amount": 12.5, + "week": 12, + "category": "grotoken_reward", + "merkle_root": "0xa3f19c8ea3f19c8ea3f19c8ea3f19c8ea3f19c8ea3f19c8ea3f19c8ea3f19c8e", + "timestamp": "2026-01-05T12:00:00Z" +} +``` + +### `tokenism.swarm.population.v1` + +```json +{ + "namespace": "tokenism", + "modality": "economic-sim", + "pack_id": "grotoken-week-12", + "status": "active", + "population_id": "pop-001", + "generation": 42, + "best_fitness": 0.87, + "metrics": { + "gini": 0.34, + "poverty_rate": 0.11 + }, + "timestamp": "2026-01-05T12:00:00Z" +} +``` + +### `tokenism.cgp.ready.v1` + +```json +{ + "cgp": { + "spec": "chit.cgp.v0.2", + "summary": "ToKenism Week 12 Economic Simulation", + "created_at": "2026-01-05T12:00:00Z", + "updated_at": "2026-01-05T12:00:00Z", + "super_nodes": [ + { + "id": "grotoken-week-12", + "label": "GroToken Distribution", + "summary": "GroToken rewards issued in week 12", + "x": 0.0, + "y": 0.0, + "r": 0.3, + "constellations": [ + { + "id": "attr-week-12", + "summary": "Week 12 attribution cluster", + "points": [ + { + "id": "attr-grotoken-week-12-0001", + "modality": "economic-sim", + "ref_id": "member-001", + "summary": "GroToken reward for cooperative purchase", + "x": 0.2, + "y": 0.2, + "conf": 0.98 + } + ] + } + ] + } + ], + "meta": { + "attribution": { + "total_attributions": 1240 + } + } + }, + "super_node_count": 1, + "week": 12, + "source": "tokenism-simulator", + "cgp_spec": "chit.cgp.v0.2", + "timestamp": "2026-01-05T12:00:00Z" +} +``` + +## Validation & Publishing Guarantees + +- Every payload is **AJV-validated** against its JSON Schema **before** publish. The geometry CGP schema is registered alongside the tokenism schemas so nested `cgp` objects validate against `contracts/schemas/geometry/cgp.v1.schema.json`. +- **Two publish modes** — constructor option `strictPublish` (default `false`): + - **Default (lenient):** a schema-invalid payload or publish error is logged (`[CHIT] Failed to publish : `) and the method returns `false` — nothing is published, but the caller keeps running. + - **Strict (`strictPublish: true`):** the same conditions **throw** — use for lanes where silent drops are unacceptable. +- In **both** modes an invalid payload is never published. +- Successful publishes log `[CHIT] Published : `. +- A subject with no registered validator throws regardless of mode (programming error, not data error). + +## Connection + +- Client: `integrations/nats/nats-client.ts` +- URL resolution order: explicit config → `NATS_URL` env → default `nats://nats:pmoves@nats:4222` +- Reconnect handling and drain-on-shutdown are built in; subscription drain logs `[NATS] Drained subscription: `. + +## Consumers (Parent Side) + +| Service | Subjects | Behavior | +|---|---|---| +| Publisher Discord | `tokenism.*` | Formats events as Discord embeds | +| Hi-RAG Gateway v2 | `tokenism.cgp.ready.v1`, `geometry.*` | `POST /geometry/event` ingestion | +| Shape Store | `geometry.event.v1` | Persistence layer | +| Consciousness Service | `tokenism.cgp.ready.v1` | Always-on consumer | + +Parent-side enablement flags (from the parent catalog): `DEEPRESEARCH_CGP_PUBLISH`, `SUPASERCH_CGP_PUBLISH` (research-produced CGPs → `tokenism.cgp.ready.v1`), `CHIT_VOICE_ATTRIBUTION` (`tokenism.geometry.event.v1`, voice events). + +## Corpus & Integration Links + +This integration is governed by the AGNOTE4482 corpus and PMOVESCHIT doctrine in the parent monorepo: + +| Document | Link | Relevance | +|---|---|---| +| AGNOTE4482 (main corpus) | [pmoves/docs/AGENTS/AGNOTE4482.md](https://github.com/POWERFULMOVES/PMOVES.AI/blob/main/pmoves/docs/AGENTS/AGNOTE4482.md) | Canonical convergence record; agents claim work here before edits | +| AGNOTE4482 Signoff Checklist | [AGNOTE4482_SIGNOFF_CHECKLIST.md](https://github.com/POWERFULMOVES/PMOVES.AI/blob/main/pmoves/docs/AGENTS/AGNOTE4482_SIGNOFF_CHECKLIST.md) | Shared multi-agent merge-signoff gate | +| AGNOTE4482.FlOO$ (PR review flow) | [AGNOTE4482.FlOO$.md](https://github.com/POWERFULMOVES/PMOVES.AI/blob/main/pmoves/docs/AGENTS/AGNOTE4482.FlOO$.md) | Sanctioned merge chain: validate → resolve → run-dry → chit-packet → strict | +| pr-monitor skill | [pr-monitor-skill/SKILL.md](https://github.com/POWERFULMOVES/PMOVES.AI/blob/main/pmoves/skills/pr-monitor-skill/SKILL.md) | Retry discipline, learning taxonomy, strict gate behavior | +| Geometry Bus NATS Subject Catalog | [geometry-nats-subjects.md](https://github.com/POWERFULMOVES/PMOVES.AI/blob/main/pmoves/.claude/context/geometry-nats-subjects.md) | The subject catalog this document fulfills | +| Three-Body Doctrine | [THREE_BODY_DOCTRINE.md](https://github.com/POWERFULMOVES/PMOVES.AI/blob/main/pmoves/docs/PMOVESCHIT/THREE_BODY_DOCTRINE.md) | Foundational Human/AI/System stabilization layer behind CHIT geometry | +| CHIT Flow Index | [CHIT_FLOW_INDEX.md](https://github.com/POWERFULMOVES/PMOVES.AI/blob/main/pmoves/docs/PMOVESCHIT/CHIT_FLOW_INDEX.md) | CHIT pipeline flow index (run-dry → chit-packet chain) | + +Integrating repositories and services: + +| Repo / Service | Role | +|---|---| +| [POWERFULMOVES/PMOVES.AI](https://github.com/POWERFULMOVES/PMOVES.AI) | Parent monorepo; consumes this repo as a core submodule tracking `PMOVES.AI-Edition-Hardened`; builds GHCR images from it | +| [POWERFULMOVES/PMOVES-Wealth](https://github.com/POWERFULMOVES/PMOVES-Wealth) | Submodule sibling — wealth/finance lane; Firefly III settlement bridge feeds it | +| [POWERFULMOVES/PMOVES-DoX](https://github.com/POWERFULMOVES/PMOVES-DoX) | Submodule sibling — Agent Zero MCP tool for standalone mode | +| Pmoves-hyperdimensions (parent) | Three.js CHIT geometry visualizer — consumes CGP documents | +| publisher-discord (parent service) | Consumes `tokenism.*` → Discord embeds | +| Hi-RAG Gateway v2 (parent service) | Consumes `tokenism.cgp.ready.v1`, `geometry.*` → `POST /geometry/event` | +| Shape Store (parent service) | Persistence for `geometry.event.v1` | +| Consciousness Service (parent service) | Always-on consumer of `tokenism.cgp.ready.v1` | + +## Monitoring + +- Grep targets: `[CHIT] Published`, `[CHIT] Failed to publish`, `[NATS] Connecting`, `[NATS] Drained subscription` +- Schema validation errors include subject name and AJV error list in the failure log line. \ No newline at end of file