cred402/lib/services/reputation_decay.ts
caelum0x 2d43618725 feat: real integrations (Casper Testnet, x402/EIP-712, MCP, RealFi, cross-chain), wallet sign-in, branding
Replace the design-note docs (p1–p7) with the implemented "make it real" phase
docs (p8–p10) and wire production-grade, env-gated real integrations:

- p8 Casper Testnet: byte-exact deploys + WASM install via casper-js-sdk; live
  node reads (node.testnet.casper.network); CRED402_CHAIN=sim|testnet transport.
- p9 x402 + data + MCP: real EIP-712 typed-data digests (@casper-ecosystem/
  casper-eip-712); casper-x402 facilitator client (x402 V2); live Open-Meteo GHI;
  official @modelcontextprotocol/sdk server; casper-sidecar SSE event feed.
- p10 RealFi + cross-chain: real Stripe test-mode webhooks (HMAC-verified) and
  Plaid sandbox → privacy-preserving on-chain envelopes; Base Sepolia EVM deploy
  (Foundry); reachable via /api/realfi/* and /api/x402/facilitator routes.
- Sign-in with Casper Wallet: real extension provider + challenge/ed25519/session
  flow (/v1/auth/wallet/*), console connect + sign-in button.
- Branding: Cred402 logo mark, favicon, OG image; Vercel config for the console.

All env-gated with sim/free-API fallbacks; full suite green, all toolchains
(TS/Rust/Go/Solidity) build.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-17 00:37:36 +03:00

52 lines
2.0 KiB
TypeScript

import type { Ledger } from "../ledger/ledger.js";
/**
* Reputation time-decay (p2 §6.6).
*
* Reputation is not permanent: an inactive agent drifts back toward a neutral
* floor, so credit reflects RECENT proven work. Decay is driven by days since the
* agent's last finalized receipt, past a grace period, at a configurable rate,
* never below the floor. Run periodically; returns what changed.
*/
export interface DecayResult {
applied: Array<{ agent_id: string; from: number; to: number; inactive_days: number }>;
floor: number;
}
export interface DecayOptions {
floor?: number; // reputation never decays below this
graceDays?: number; // no decay within this many days of last activity
pointsPerDay?: number; // decay rate beyond the grace period
/** Override the elapsed-inactivity calculation (for projection/testing). */
assumeInactiveDays?: number;
}
const DAY = 24 * 60 * 60;
export function applyReputationDecay(ledger: Ledger, opts: DecayOptions = {}): DecayResult {
const floor = opts.floor ?? 50;
const graceDays = opts.graceDays ?? 7;
const pointsPerDay = opts.pointsPerDay ?? 2;
const now = ledger.clock.now();
const applied: DecayResult["applied"] = [];
for (const agent of ledger.agents.list()) {
if (agent.reputation_score <= floor) continue;
const receipts = ledger.receipts.forSeller(agent.agent_id);
const lastActivity = receipts.length ? Math.max(...receipts.map((r) => r.timestamp)) : agent.registered_at;
const inactiveDays = opts.assumeInactiveDays ?? Math.max(0, (now - lastActivity) / DAY);
if (inactiveDays <= graceDays) continue;
const rawDecay = Math.floor((inactiveDays - graceDays) * pointsPerDay);
const decay = Math.min(rawDecay, agent.reputation_score - floor);
if (decay <= 0) continue;
const from = agent.reputation_score;
ledger.agents.update_reputation(agent.agent_id, -decay, "0xdecay", "REPUTATION_DECAY");
applied.push({ agent_id: agent.agent_id, from, to: from - decay, inactive_days: Math.round(inactiveDays * 10) / 10 });
}
return { applied, floor };
}