scaffold+broker: dedicated <ns>.balance method for managed budget apps#73
Open
Alexgodoroja wants to merge 1 commit into
Open
scaffold+broker: dedicated <ns>.balance method for managed budget apps#73Alexgodoroja wants to merge 1 commit into
Alexgodoroja wants to merge 1 commit into
Conversation
Managed (credit-metered) apps could meter a per-user $-budget and surface it on the X-Pilot-Credits-Remaining header, but a keyless adapter only returns the response body — so an agent could never actually read its balance, and there was no first-class way to check funds before a spend op. This wires a dedicated balance method, end to end: - Broker answers a canonical /_pilot/balance for any credit app from its own per-caller ledger — no upstream call, no master key, no debit, never a 402, scoped to the caller (the pooled account balance is never disclosed). Reuses the existing serveCreditBalance (which also backs the optional creditBalancePath partner-endpoint shadow); the canonical path is always available alongside it. - Scaffolder auto-injects `<ns>.balance` into every `auth: managed` app (no submission field), a GET to that path — flowing through registration, the manifest `exposes` list, and `<ns>.help` like any authored method. - scaffold.BalanceMetaPath == broker.pilotBalancePath keeps the two ends in sync. - Tests: broker (canonical path seeds, reflects spend, free, never forwards) and scaffold (injected for managed only, generated + compiles + in the manifest). - Docs: MANAGED-KEY.md and the publishing playbook document the pattern for all broker apps that meter a budget.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Managed (credit-metered) apps meter a per-user $-budget and surface it on the
X-Pilot-Credits-Remainingheader — but a keyless adapter only returns the response body, so an agent can't actually read that header. There was no first-class way to answer "how much budget do I have left?" before a spend op.The broker already had half of this:
serveCreditBalance+ an optionalcredit.balance_pathto shadow a partner's account-balance endpoint (so it returns per-user budget instead of leaking the pooled account). But it was opt-in with no default and no adapter-facing method — dormant.This PR — wires it end to end
/_pilot/balancefor any credit app, from its own per-caller ledger — no upstream call, no master key, no debit, never a 402, scoped to the caller (pooled balance never disclosed). Reuses the existingserveCreditBalance; the canonical path is always available alongside the optionalcreditBalancePathshadow.<ns>.balanceinto everyauth: managedapp (no submission field) — aGETto/_pilot/balancethat flows through registration, the manifestexposeslist, and<ns>.helplike any authored method.scaffold.BalanceMetaPath==broker.pilotBalancePathkeeps both ends in sync.Response body:
{ "balance": "$1.80", "credits_remaining": 1800000, "credits_seed": 5000000, "unit": "micro_usd", "scope": "per-pilot-user" }Tests
zz_balance_meta_test.go): canonical path seeds a fresh caller, reflects a spend, is free, and never forwards upstream.zz_balance_method_test.go): injected for managed apps only (notbyo), generated + wired to the reserved path + in the manifest, and the project compiles.broker+scaffold+publishsuites green.Docs
MANAGED-KEY.mdandPUBLISHING-PLAYBOOK.mddocument the pattern for all broker apps that meter a budget: set thecreditblock → the adapter exposes<ns>.balancefor free; tell agents to check it (or the header) before a spend.