@odatano/core 2.0.0-rc.5 → 2.0.0-rc.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -14,10 +14,13 @@
14
14
  * lastUsedAt. Ownership needs no special code: a token's jobs are created
15
15
  * with createdBy = `agent:<grantId>`, so the existing `createdBy = $user`
16
16
  * gates and the `$user.grantId` row restrictions do the narrowing.
17
- * - `registerAgentGrantHandlers`: CreateAgentGrant / RevokeAgentGrant /
18
- * GetGrantStatus for CardanoAgentService, on top of the programmatic
19
- * `issueAgentGrant` / `revokeAgentGrantById` that @odatano/x402 uses to sell
20
- * grants.
17
+ * - `registerAgentGrantHandlers`: the grant lifecycle for CardanoAgentService
18
+ * (CreateAgentGrant / UpdateAgentGrant / RotateAgentGrantToken /
19
+ * RevokeAgentGrant, GetGrantStatus / GetGrantUsage), on top of the
20
+ * programmatic `issueAgentGrant` / `updateAgentGrant` /
21
+ * `rotateAgentGrantToken` / `revokeAgentGrantById` that @odatano/x402 uses
22
+ * to sell grants. Names are this repo's PascalCase; semantics follow
23
+ * NIGHTGATE's so a gateway (ODATANO ACCESS) drives both with one code path.
21
24
  *
22
25
  * The token is a capability, not an identity: it never carries the operator's
23
26
  * roles, so every `@requires: 'Admin'` surface refuses it by construction.
@@ -31,6 +34,8 @@ export declare const AGENT_ROLE = "agent-grant";
31
34
  /** `agent:<grantId>`: the principal id a token request runs under. */
32
35
  export declare const AGENT_PRINCIPAL_PREFIX = "agent:";
33
36
  export declare const GRANTS_ENTITY = "odatano.cardano.CardanoAgentGrants";
37
+ /** Daily per-action counters of admitted calls (feeds GetGrantUsage); never exposed as an entity. */
38
+ export declare const GRANT_USAGE_ENTITY = "odatano.cardano.CardanoAgentGrantUsage";
34
39
  /** The services the hook attaches to and the transport lane opens. */
35
40
  export declare const AGENT_SERVICE_NAMES: readonly string[];
36
41
  /**
@@ -51,7 +56,7 @@ export declare const AGENT_WALLET_ACTIONS: ReadonlySet<string>;
51
56
  export declare const AGENT_ALWAYS_ALLOWED_EVENTS: ReadonlySet<string>;
52
57
  /** Mirrors WalletJobKind in db/types.cds; kept literal so this module stays free of cds-models. */
53
58
  export declare const AGENT_JOB_KINDS: readonly string[];
54
- /** Forget every rate-limit window (tests). */
59
+ /** Forget every rate-limit window and re-read the limit from the config (tests). */
55
60
  export declare function __resetGrantRateLimiterForTests(): void;
56
61
  export interface AgentGrantRow {
57
62
  ID: string;
@@ -122,7 +127,7 @@ export interface IssuedAgentGrant {
122
127
  }
123
128
  /** A refused grant input; carries the 400 the action answers with. */
124
129
  export declare class AgentGrantInputError extends BackendError {
125
- constructor(message: string, target?: string);
130
+ constructor(message: string, target?: string, context?: string);
126
131
  }
127
132
  interface NormalizedGrantInput {
128
133
  allowedActions: string[];
@@ -132,7 +137,7 @@ interface NormalizedGrantInput {
132
137
  validUntil: string | null;
133
138
  agentLabel: string | null;
134
139
  }
135
- export declare function validateGrantInput(input: IssueAgentGrantInput, now?: Date): NormalizedGrantInput;
140
+ export declare function validateGrantInput(input: IssueAgentGrantInput, now?: Date, context?: string): NormalizedGrantInput;
136
141
  /**
137
142
  * Mint a grant for `operatorId`. Validation errors surface as
138
143
  * AgentGrantInputError (400). The INSERT runs on `runner`: the action passes its
@@ -141,6 +146,93 @@ export declare function validateGrantInput(input: IssueAgentGrantInput, now?: Da
141
146
  export declare function issueAgentGrant(input: IssueAgentGrantInput, operatorId: string, runner?: Runner): Promise<IssuedAgentGrant>;
142
147
  /** Deactivate a grant; false when it does not exist or is already revoked. */
143
148
  export declare function revokeAgentGrantById(grantId: string, runner?: Runner): Promise<boolean>;
149
+ export interface RotatedAgentGrant {
150
+ grantId: string;
151
+ /** Shown once. Never logged, never stored. */
152
+ token: string;
153
+ }
154
+ /**
155
+ * Replace the grant's token; the old one is an unknown token from the next
156
+ * request (the lane reads tokenHash per request, so nothing needs invalidating;
157
+ * a request in flight with the old token completes). Budget, wallet, allow
158
+ * list and expiry survive. Null when the grant does not exist or is revoked.
159
+ */
160
+ export declare function rotateAgentGrantToken(grantId: string, runner?: Runner): Promise<RotatedAgentGrant | null>;
161
+ /** The editable grant fields, as a client sends them: absent = untouched, explicit null = cleared. */
162
+ export interface UpdateAgentGrantInput {
163
+ agentLabel?: string | null;
164
+ allowedActions?: string[] | null;
165
+ allowedJobKinds?: string[] | null;
166
+ maxJobsPerDay?: number | null;
167
+ validUntil?: string | null;
168
+ }
169
+ /** Row fields a grant edit never touches; a different wallet binding is a different grant (NIGHTGATE: sessionId). */
170
+ export declare const GRANT_IMMUTABLE_FIELDS: readonly string[];
171
+ export type UpdateAgentGrantResult = {
172
+ ok: true;
173
+ grantId: string;
174
+ updated: string[];
175
+ } | {
176
+ ok: false;
177
+ status: 404 | 409;
178
+ code?: 'GRANT_REVOKED';
179
+ message: string;
180
+ };
181
+ /**
182
+ * Change the given fields of an ACTIVE grant. A field absent from `input`
183
+ * keeps its value; an explicit null clears agentLabel, allowedJobKinds,
184
+ * maxJobsPerDay or validUntil (NIGHTGATE's updateAgentGrant semantics).
185
+ * allowedActions cannot be emptied. The merged row goes through
186
+ * validateGrantInput, so the cross-field rules hold: wallet actions need the
187
+ * grant's (immutable) wallet, job kinds need SubmitWalletJob, a given
188
+ * validUntil lies in the future. Lowering maxJobsPerDay below jobsUsedToday is
189
+ * allowed; the grant is simply over budget until the UTC day rolls.
190
+ *
191
+ * Validation errors surface as AgentGrantInputError (400); an unknown grant is
192
+ * 404 and a revoked one 409 GRANT_REVOKED (revoked grants are never edited;
193
+ * re-mint instead).
194
+ */
195
+ export declare function updateAgentGrant(grantId: string, input: UpdateAgentGrantInput, runner?: Runner): Promise<UpdateAgentGrantResult>;
196
+ export declare const USAGE_WINDOW_MAX_MS: number;
197
+ export declare const USAGE_WINDOW_DEFAULT_MS: number;
198
+ export type UsageWindow = {
199
+ ok: true;
200
+ since: string;
201
+ until: string;
202
+ sinceDay: string;
203
+ untilDay: string;
204
+ } | {
205
+ ok: false;
206
+ message: string;
207
+ target: string;
208
+ };
209
+ /**
210
+ * since (default until - 30 days) .. until (default now), at most 366 days
211
+ * (NIGHTGATE's rules and wording). Counters are per UTC day, so the days both
212
+ * ends fall on are included whole.
213
+ */
214
+ export declare function resolveUsageWindow(since: unknown, until: unknown, now?: Date): UsageWindow;
215
+ export interface GrantUsageCall {
216
+ service: string;
217
+ action: string;
218
+ /** Admitted and kept (budget charged and not refunded). */
219
+ count: number;
220
+ /** Admitted, then refunded because the handler refused the input. */
221
+ refunded: number;
222
+ }
223
+ export interface AgentGrantUsage {
224
+ grantId: string;
225
+ since: string;
226
+ until: string;
227
+ calls: GrantUsageCall[];
228
+ total: number;
229
+ jobsUsedToday: number;
230
+ maxJobsPerDay: number | null;
231
+ }
232
+ /** The grant's admitted calls in `window`, grouped by service and action (one SUM per group). */
233
+ export declare function getGrantUsage(grant: AgentGrantRow, window: Extract<UsageWindow, {
234
+ ok: true;
235
+ }>, runner?: Runner): Promise<AgentGrantUsage>;
144
236
  export interface AgentGrantStatus {
145
237
  grantId: string;
146
238
  agentLabel: string | null;
@@ -188,7 +280,7 @@ export declare function budgetRunnerFor(req: Request, injected?: Runner, detache
188
280
  * principals pass through untouched: this hook is a no-op without a grant.
189
281
  * `runner` / `detached` are test seams; production picks them per request.
190
282
  */
191
- export declare function enforceAgentGrant(req: Request, runner?: Runner, detached?: boolean): Promise<unknown>;
283
+ export declare function enforceAgentGrant(req: Request, runner?: Runner, detached?: boolean, serviceName?: string): Promise<unknown>;
192
284
  export interface BudgetCharge {
193
285
  consumed: boolean;
194
286
  /** The UTC day the unit was charged to; the refund must name the same window. */
@@ -207,5 +299,16 @@ export declare function consumeDailyBudget(br: BudgetRunner | Runner, grant: Age
207
299
  * the new day's counter: the unit was spent yesterday, yesterday keeps it.
208
300
  */
209
301
  export declare function refundDailyBudget(br: BudgetRunner | Runner, grant: AgentGrantRow, window: string): Promise<void>;
302
+ /**
303
+ * One admitted call: `calls + 1` on (grant, UTC day, service, action). A
304
+ * conditional UPDATE first (the common path), an INSERT when the row does not
305
+ * exist yet, one more UPDATE when a racing request inserted it first. Runs on
306
+ * the hook's BudgetRunner and is AWAITED like every other write of the hook
307
+ * (see touchLastUsed), but never fails the request: usage is accounting, not
308
+ * admission. Returns the day the call was counted on (the refund names it).
309
+ */
310
+ export declare function recordGrantUsage(br: BudgetRunner | Runner, grant: AgentGrantRow, service: string, action: string, now?: Date): Promise<string>;
311
+ /** `refunded + 1` on the row the call was counted on: a refused request admitted nothing. */
312
+ export declare function refundGrantUsage(br: BudgetRunner | Runner, grant: AgentGrantRow, service: string, action: string, day: string): Promise<void>;
210
313
  export {};
211
314
  //# sourceMappingURL=agent-grants.d.ts.map