Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

fix(reputation): report net staked conviction so a settled position stops counting

nirholas1db788131616
diff --git a/api/_lib/reputation-market.js b/api/_lib/reputation-market.js
index 1dbde8a5d..b3da2176a 100644
--- a/api/_lib/reputation-market.js
+++ b/api/_lib/reputation-market.js
@@ -621,6 +621,127 @@ export async function quotePosition({ position, network, now, env = process.env
return byPosition.get(position.id) || { lamports: 0n, byEpoch: [] };
}
+// ── net conviction (spec §3.3) ──────────────────────────────────────────────
+
+/**
+ * Net staked conviction for one agent: verified `threews.stake.v1` lamports minus
+ * the principal every settlement retired. Spec §3.3, "withdrawn conviction is not
+ * conviction".
+ *
+ * Only the escrow's own unstake memos retire anything. A settlement is
+ * escrow-signed by construction (spec §3.2), so honouring any structurally valid
+ * unstake would let a stranger deflate an agent's conviction with a memo naming
+ * somebody else's stake signature. A deployment with no escrow configured retires
+ * nothing, which reports the gross figure rather than a wrong net one.
+ *
+ * @returns {Promise<{total_lamports: string, count: number, unique_stakers: number,
+ * gross_lamports: string, retired_lamports: string, retired_count: number,
+ * top_stakers: Array<{attester: string, lamports: string, score: number|null}>}>}
+ */
+export async function netStakeForAgent({ asset, network, limit = 5, env = process.env }) {
+ const net = normalizeNetwork(network);
+ let escrow = null;
+ try {
+ escrow = marketConfig(net, env).escrow?.toBase58() || null;
+ } catch {
+ // A misconfigured escrow must not take the whole reputation card down; it
+ // only means nothing is credited as retired.
+ escrow = null;
+ }
+
+ const stakes = await sql`
+ select signature, attester,
+ coalesce(payload->>'lamports', '0') as lamports,
+ (payload->>'score')::int as score
+ from solana_attestations
+ where agent_asset = ${asset} and network = ${net}
+ and kind = 'threews.stake.v1' and verified = true and revoked = false
+ `;
+
+ // No escrow configured means no settlement this deployment can vouch for, so
+ // nothing is retired and the answer is the gross figure.
+ const retirements = escrow
+ ? await sql`
+ select payload->>'stake' as stake_signature, payload->>'principal' as principal
+ from solana_attestations
+ where agent_asset = ${asset} and network = ${net}
+ and kind = ${UNSTAKE_KIND} and verified = true and revoked = false
+ and attester = ${escrow}
+ `
+ : [];
+
+ return netConviction({ stakes, retirements, limit });
+}
+
+/**
+ * Fold indexed stakes and the settlements that retired them into the net
+ * conviction figure. Pure, so the rule in spec §3.3 is testable without a
+ * database and reproduces identically wherever it runs.
+ *
+ * Retirement is per stake signature and clamped to that stake's own principal, so
+ * an over-stated `principal` in one settlement can never eat a different staker's
+ * conviction, and net conviction can never go negative.
+ *
+ * @param {object} input
+ * @param {Array<{signature: string, attester: string, lamports: string, score: number|null}>} input.stakes
+ * @param {Array<{stake_signature: string, principal: string}>} input.retirements
+ * @param {number} [input.limit] how many top stakers to return
+ */
+export function netConviction({ stakes = [], retirements = [], limit = 5 } = {}) {
+ // A stake settles once; if the index somehow holds two settlements naming it,
+ // the largest is the one that retires it, never their sum.
+ const retiredBySignature = new Map();
+ for (const r of retirements) {
+ const sig = r?.stake_signature;
+ if (!sig) continue;
+ const principal = toBigInt(String(r.principal ?? '0'));
+ const prev = retiredBySignature.get(sig) ?? 0n;
+ if (principal > prev) retiredBySignature.set(sig, principal);
+ }
+
+ let gross = 0n;
+ let retired = 0n;
+ let total = 0n;
+ let retiredCount = 0;
+ let openCount = 0;
+ const byAttester = new Map();
+
+ for (const s of stakes) {
+ const principal = toBigInt(String(s?.lamports ?? '0'));
+ const claimed = retiredBySignature.get(s?.signature) ?? 0n;
+ const back = claimed > principal ? principal : claimed;
+ const remaining = principal - back;
+
+ gross += principal;
+ retired += back;
+ total += remaining;
+ if (back > 0n) retiredCount++;
+ if (remaining <= 0n) continue;
+
+ openCount++;
+ const prev = byAttester.get(s.attester) || { lamports: 0n, score: null };
+ prev.lamports += remaining;
+ const score = Number.isInteger(s?.score) ? s.score : null;
+ if (score !== null) prev.score = prev.score === null ? score : Math.max(prev.score, score);
+ byAttester.set(s.attester, prev);
+ }
+
+ const top = [...byAttester.entries()]
+ .sort((a, b) => (a[1].lamports === b[1].lamports ? 0 : b[1].lamports > a[1].lamports ? 1 : -1))
+ .slice(0, limit)
+ .map(([attester, v]) => ({ attester, lamports: v.lamports.toString(), score: v.score }));
+
+ return {
+ total_lamports: total.toString(),
+ count: openCount,
+ unique_stakers: byAttester.size,
+ gross_lamports: gross.toString(),
+ retired_lamports: retired.toString(),
+ retired_count: retiredCount,
+ top_stakers: top,
+ };
+}
+
// ── market listing ──────────────────────────────────────────────────────────
const LISTING_WINDOW_EPOCHS = 7;
diff --git a/api/agents/solana/_handlers.js b/api/agents/solana/_handlers.js
index 73335f9ba..df6672783 100644
--- a/api/agents/solana/_handlers.js
+++ b/api/agents/solana/_handlers.js
@@ -23,6 +23,7 @@ import {
AGENT_COLLECTION,
} from '../../_lib/solana-collection.js';
import { KIND_MAP, crawlAgentAttestations } from '../../_lib/solana-attestations.js';
+import { netStakeForAgent } from '../../_lib/reputation-market.js';
import {
mintAttestation,
loadAttesterKeypair,
@@ -1133,27 +1134,9 @@ export const handleReputation = wrap(async (req, res) => {
const [cursor] = await sql`select last_indexed_at from solana_attestations_cursor where agent_asset = ${asset} limit 1`;
- const [stakeAgg] = await sql`
- select
- coalesce(sum((payload->>'lamports')::numeric), 0)::text as total_lamports,
- count(*)::int as stake_count,
- count(distinct attester)::int as unique_stakers
- from solana_attestations
- where agent_asset = ${asset} and network = ${network}
- and kind = 'threews.stake.v1' and verified = true and revoked = false
- `;
-
- const topStakers = await sql`
- select attester,
- sum((payload->>'lamports')::numeric)::text as lamports,
- max((payload->>'score')::int) as score
- from solana_attestations
- where agent_asset = ${asset} and network = ${network}
- and kind = 'threews.stake.v1' and verified = true and revoked = false
- group by attester
- order by sum((payload->>'lamports')::numeric) desc
- limit 5
- `;
+ // Net, not gross: a settled position's conviction is retired the moment the
+ // escrow's threews.unstake.v1 memo lands. Spec §3.3 (specs/REPUTATION_STAKING_MARKET.md).
+ const stakeAgg = await netStakeForAgent({ asset, network });
let pumpfunRows = [];
try {
@@ -1187,14 +1170,13 @@ export const handleReputation = wrap(async (req, res) => {
tasks: { offered: counts.tasks_offered, accepted: counts.tasks_accepted },
disputes_filed: counts.disputes_filed, revoked_count: counts.revoked_count,
stake: {
- total_lamports: stakeAgg?.total_lamports || '0',
- count: stakeAgg?.stake_count || 0,
- unique_stakers: stakeAgg?.unique_stakers || 0,
- top_stakers: topStakers.map((r) => ({
- attester: r.attester,
- lamports: r.lamports,
- score: r.score,
- })),
+ total_lamports: stakeAgg.total_lamports,
+ count: stakeAgg.count,
+ unique_stakers: stakeAgg.unique_stakers,
+ gross_lamports: stakeAgg.gross_lamports,
+ retired_lamports: stakeAgg.retired_lamports,
+ retired_count: stakeAgg.retired_count,
+ top_stakers: stakeAgg.top_stakers,
},
last_indexed_at: cursor?.last_indexed_at || null,
});