Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions scripts/e2e/owner-budget-testnet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ async function run(execute: boolean): Promise<void> {
const HINT: Record<string, string> = {
spending_off: 'AGENT_SPEND_ENABLED is not true on the engine (agent-spend.ts:111-117).',
owner_budget_not_checked: 'the engine could not read the ledger: is migration 20261114200000_owner_spend_ledger.sql applied? (agent-spend.ts:160-164)',
no_owner_budget: "the agent's owner is not a builder account, so no stake funds it (agent-spend.ts:174-179).",
no_owner_budget: "no account's stake funds this owner (owner-budget.ts ownerBuilderOfAgent). A proof-of-human (human_sbt) owner has one only where the engine has HUMAN_SBT_OWNER_BUDGET_ENABLED=true.",
not_bound: 'the agent is not bound (agent-spend.ts:133-138).',
no_gas: 'the agent wallet has too little ETH: raise GAS_TOPUP_ETH (max 0.002).',
owner_balance: "the owner's USDC balance is below the amount.",
Expand Down Expand Up @@ -389,10 +389,19 @@ async function run(execute: boolean): Promise<void> {
headers: { ...(await walletHeaders('POST', '/api/v1/human/bind')), 'x-agent-key': a.key },
});
if (b.status !== 201 || b.body?.ok !== true) stop('3-bind', `expected 201 ok, got ${b.status} ${b.body?.reason ?? b.body?.error ?? ''}`, b.body);
if (b.body.binding?.owner_kind !== 'builder' || String(b.body.binding?.builder_id) !== builderId) {
stop('3-bind', `bound as ${b.body.binding?.owner_kind} ${b.body.binding?.builder_id ?? b.body.binding?.human_token_id}, not builder ${builderId}. A wallet with a proof-of-human SBT binds as human_sbt and has no builder stake budget: use another wallet.`, b.body);
const kind = b.body.binding?.owner_kind;
if (kind === 'human_sbt' && b.body.binding?.human_token_id) {
// A proof-of-human wallet binds as human_sbt. Its budget is the stake of the account that holds the
// same wallet (step 1), but only where the server has HUMAN_SBT_OWNER_BUDGET_ENABLED=true
// (owner-budget.ts ownerBuilderOfAgent). Not readable from here. Where it is off, the first owner-budget
// read answers no_owner_budget before any USDC moves: step 3b in stake mode (the spend-limit route), and
// step R in wallet mode, where 3b does not run.
log('3-bind', `${a.name} bound to the owner as a verified human (${b.body.binding.human_token_id}); its budget is account ${builderId}'s stake`, b.body);
} else if (kind !== 'builder' || String(b.body.binding?.builder_id) !== builderId) {
stop('3-bind', `bound as ${kind} ${b.body.binding?.builder_id ?? b.body.binding?.human_token_id}, not builder ${builderId}.`, b.body);
} else {
log('3-bind', `${a.name} bound to the owner as builder ${builderId}`, b.body);
}
log('3-bind', `${a.name} bound to the owner as builder ${builderId}`, b.body);
}

// ── 3b. spend limits ── stake mode: the agent's key alone may not move the owner's stake (Strix on
Expand Down
1 change: 1 addition & 0 deletions src/config/known-env-vars.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ HITL_RECONCILE_POLL_MS
HITL_TIMEOUT_VERDICT
HUGGINGFACE_API_TOKEN
HUMAN_AGENT_BIND_ENABLED
HUMAN_SBT_OWNER_BUDGET_ENABLED
HYPERDAG_ATTESTOR_ADDRESS
HYPERDAG_ATTESTOR_PRIVATE_KEY
IDENTITY_TOKENS_ENABLED
Expand Down
1 change: 1 addition & 0 deletions src/ops/flag-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ HITL_EXPIRATION_ENABLED
HITL_EXPIRY_SWEEPER_ENABLED
HITL_RECONCILE_MODE
HUMAN_AGENT_BIND_ENABLED
HUMAN_SBT_OWNER_BUDGET_ENABLED
IMMUNE_ANCHOR_ENABLED
IMMUNE_PERSIST_ENABLED
IMMUNE_SETTLE_ENABLED
Expand Down
6 changes: 5 additions & 1 deletion src/routes/agent-spend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,11 @@ export interface AgentSpendDeps {
/** The wallet bound as this agent's owner, or null if nobody has bound it. */
loadOwnerWallet?: (agentId: string) => Promise<string | null>;
enabled?: () => boolean;
/** The owner's account (builder) whose stake is the shared budget, or null if not a builder. */
/**
* The owner's account (builder) whose stake is the shared budget, or null if there is none
* (owner-budget.ts ownerBuilderOfAgent): a builder owner's own account, or, only with
* HUMAN_SBT_OWNER_BUDGET_ENABLED=true, the account holding a verified human owner's wallet.
*/
loadOwnerBuilder?: (agentId: string) => Promise<string | null>;
ownerBudget?: OwnerBudgetStore;
budgetMode?: () => OwnerBudgetMode;
Expand Down
35 changes: 32 additions & 3 deletions src/services/owner-budget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,38 @@ export const supabaseAgentLimits: AgentLimitStore = {
},
};

/** The owner's account (builder) for an agent, or null when the owner is not a builder account. */
export async function ownerBuilderOfAgent(agentId: string): Promise<string | null> {
/**
* The account (builder) whose stake funds this agent's owner budget, or null when there is none.
*
* A builder owner: its own account. A verified human (human_sbt): until 2026-11-16 always null, so a
* proof-of-human owner could not hold a budget at all and the first live run stopped at bind
* (reports/2026-11-16/owner-budget-e2e-execute.attempt1-sbt-wallet.json). With
* HUMAN_SBT_OWNER_BUDGET_ENABLED=true it is the account that holds the same wallet. That wallet is
* the binding's, which bind took from human_sbt_registry, never from the caller. The binding itself is
* unchanged, so the owner's assurance label stays proof-of-human.
*
* An account for a wallet does NOT prove that wallet signed. A signed connect (POST /account/connect)
* makes one, but so does POST /builder/register, behind an API key and with no signature from the
* wallet. This comment said "only created for a wallet that signed" until 2026-11-16 (review of #1557).
* What funds the budget is the account's stake, not the account; two accounts for one wallet are
* refused below as ambiguous rather than picked between.
*/
export async function ownerBuilderOfAgent(
agentId: string,
humanSbtBudget: boolean = process.env.HUMAN_SBT_OWNER_BUDGET_ENABLED === 'true',
): Promise<string | null> {
const { ownerOfAgent } = require('./human-agent-binding') as typeof import('./human-agent-binding');
const owner = await ownerOfAgent(agentId);
return owner && owner.owner_kind === 'builder' && owner.builder_id ? String(owner.builder_id) : null;
if (!owner) return null;
if (owner.owner_kind === 'builder') return owner.builder_id ? String(owner.builder_id) : null;
if (owner.owner_kind !== 'human_sbt' || !humanSbtBudget) return null;
const wallet = typeof owner.human_wallet === 'string' ? owner.human_wallet : '';
// Hex only: the value goes into an ILIKE, where % and _ would be wildcards.
if (!/^0x[0-9a-fA-F]{40}$/.test(wallet)) return null;
const { data, error } = await db.from('builders').select('id').ilike('address', wallet).limit(2);
if (error) throw new Error(`owner account lookup failed: ${error.message}`);
const rows = (data ?? []) as Array<{ id: string }>;
// builders.address is unique only case-sensitively; two spellings of one wallet is refused, never a pick.
if (rows.length > 1) throw new Error(`owner account lookup ambiguous: ${rows.length} accounts match this wallet`);
return rows[0] ? String(rows[0].id) : null;
}
109 changes: 109 additions & 0 deletions tests/owner-budget-human-sbt.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/**
* R2 (2026-11-16): a verified human gets the same owner budget a builder does.
*
* MEASURED: the first live owner-budget run stopped at bind. The owner's wallet holds a proof-of-human
* SBT, so it bound as human_sbt, and ownerBuilderOfAgent returned null for every human_sbt owner, so
* no budget existed (E:\dev\reports\2026-11-16\owner-budget-e2e-execute.attempt1-sbt-wallet.json).
* The stake is keyed by builder account, and that run had just created one for the same wallet.
*
* Behind HUMAN_SBT_OWNER_BUDGET_ENABLED (default off). The binding is not changed, so the owner's
* assurance label stays proof-of-human.
*/
const WALLET = '0x' + 'ab'.repeat(20);
let owner: any = null;
let builderRows: any[] = [];
let builderErr: any = null;
const ilikeCalls: unknown[][] = [];

jest.mock('../src/services/human-agent-binding', () => ({ ownerOfAgent: async () => owner }));
jest.mock('../src/db', () => ({
db: {
from(table: string) {
if (table !== 'builders') throw new Error(`unexpected table ${table}`);
const b: any = {
select: () => b,
ilike: (...args: unknown[]) => { ilikeCalls.push(args); return b; },
limit: async () => ({ data: builderErr ? null : builderRows, error: builderErr }),
};
return b;
},
},
}));

import { ownerBuilderOfAgent } from '../src/services/owner-budget';

const SBT_OWNER = { owner_kind: 'human_sbt', human_token_id: 'SBT-AB-1', builder_id: null, human_wallet: WALLET };
const ON = true;

beforeEach(() => {
owner = null;
builderRows = [];
builderErr = null;
ilikeCalls.length = 0;
});

test('a builder owner resolves to its builder account, flag or not (unchanged)', async () => {
owner = { owner_kind: 'builder', builder_id: 'b-1', human_wallet: WALLET };
await expect(ownerBuilderOfAgent('a-1', false)).resolves.toBe('b-1');
await expect(ownerBuilderOfAgent('a-1', ON)).resolves.toBe('b-1');
});

test('flag off: a human_sbt owner still has no budget (today, unchanged)', async () => {
owner = SBT_OWNER;
builderRows = [{ id: 'b-same-wallet' }];
await expect(ownerBuilderOfAgent('a-1', false)).resolves.toBeNull();
expect(ilikeCalls).toHaveLength(0);
});

test('flag on: a human_sbt owner gets the budget of the account that holds the same wallet', async () => {
owner = SBT_OWNER;
builderRows = [{ id: 'b-same-wallet' }];
await expect(ownerBuilderOfAgent('a-1', ON)).resolves.toBe('b-same-wallet');
// The wallet comes from the binding row (taken from human_sbt_registry at bind), never the caller.
expect(ilikeCalls[0]).toEqual(['address', WALLET]);
});

test('flag on: no account for that wallet means no budget, not someone else\'s', async () => {
owner = SBT_OWNER;
builderRows = [];
await expect(ownerBuilderOfAgent('a-1', ON)).resolves.toBeNull();
});

test('flag on: two accounts matching the wallet is refused, never a pick', async () => {
owner = SBT_OWNER;
builderRows = [{ id: 'b-1' }, { id: 'b-2' }];
await expect(ownerBuilderOfAgent('a-1', ON)).rejects.toThrow(/ambiguous/);
});

test('flag on: a failed read is an error, not "no budget"', async () => {
owner = SBT_OWNER;
builderErr = { message: 'connection reset' };
await expect(ownerBuilderOfAgent('a-1', ON)).rejects.toThrow(/connection reset/);
});

test('flag on: a binding with no wallet, or a malformed one, has no budget and runs no query', async () => {
for (const w of [null, '', 'not-a-wallet', WALLET + '%', '0x' + 'g'.repeat(40)]) {
owner = { ...SBT_OWNER, human_wallet: w };
builderRows = [{ id: 'b-any' }];
await expect(ownerBuilderOfAgent('a-1', ON)).resolves.toBeNull();
}
expect(ilikeCalls).toHaveLength(0);
});

test('the default reads HUMAN_SBT_OWNER_BUDGET_ENABLED, and only the exact string "true" turns it on', async () => {
owner = SBT_OWNER;
builderRows = [{ id: 'b-same-wallet' }];
const saved = process.env.HUMAN_SBT_OWNER_BUDGET_ENABLED;
try {
for (const v of [undefined, '1', 'TRUE', 'yes', ' true']) {
if (v === undefined) delete process.env.HUMAN_SBT_OWNER_BUDGET_ENABLED;
else process.env.HUMAN_SBT_OWNER_BUDGET_ENABLED = v;
await expect(ownerBuilderOfAgent('a-1')).resolves.toBeNull();
}
process.env.HUMAN_SBT_OWNER_BUDGET_ENABLED = 'true';
await expect(ownerBuilderOfAgent('a-1')).resolves.toBe('b-same-wallet');
} finally {
if (saved === undefined) delete process.env.HUMAN_SBT_OWNER_BUDGET_ENABLED;
else process.env.HUMAN_SBT_OWNER_BUDGET_ENABLED = saved;
}
});
Loading