@adonis-agora/agent
Version:
Governed, durable-backed AI agent (chat + tool-calling + governance) for AdonisJS — part of the Agora ecosystem.
41 lines • 2.52 kB
JavaScript
/**
* Decide whether an already-resolved actor holds cross-actor governance privilege — the router-free
* core of the provider's governance gate, extracted so it can be unit tested without booting an app.
*
* - No `authorize` configured → `{ ok: true }` (see the caveat below — the provider never gets here).
* - `authorize` returns truthy → `{ ok: true }`.
* - `authorize` returns falsy → `{ ok: false, status: 403 }`.
* - `authorize` throws → `{ ok: false, status: 403 }` (fail-closed). `debug` decides whether the
* thrown `.message` reaches the client (`true`, e.g. local dev — the message is meant for whoever
* is wiring `governanceAuthorize`, useful to see while testing it) or a generic `'forbidden'`
* (`false`, the default — this predicate runs on every request from a caller who is, by
* definition, not yet proven trustworthy; matches `@adonis-agora/durable`'s dashboard convention of
* a uniform message on every credential failure). The provider passes `!app.inProduction`.
*
* The `undefined` branch is a pure-function convenience, NOT a claim that governance is readable
* without a gate. Neither provider caller can reach it: `#resolveGovernanceActor` backs the
* `/agent/governance/*` routes, which are NOT mounted at all when no gate is configured (they answer
* `404`), so it always passes one; and `#assertOwner` — which reuses this predicate as the "may act
* across actors" seam for the per-actor run/thread routes — short-circuits to "not privileged" when
* no gate is configured, leaving ownership strict, rather than calling in. To mount the governance
* routes, set `governanceAuthorize` (typically an ADMIN check); to deliberately restore the old
* behaviour of letting ANY authenticated actor read them, set `governanceAuthorize: () => true`.
* Either way `GET /agent/approvals/mine` is unaffected — it stays mounted and scoped to the calling
* actor's own pending approvals.
*/
export async function evaluateGovernanceGate(actor, ctx, authorize, debug = false) {
if (authorize === undefined)
return { ok: true, status: 200 };
try {
const allowed = await authorize(actor, ctx);
return allowed ? { ok: true, status: 200 } : { ok: false, status: 403, error: 'forbidden' };
}
catch (error) {
return {
ok: false,
status: 403,
error: debug && error instanceof Error ? error.message : 'forbidden',
};
}
}
//# sourceMappingURL=governance-gate.js.map