@north-light/api 0.3.750 → 0.3.751
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.
- package/dist/core/canvas/schema-version.d.ts +1 -1
- package/dist/core/canvas/schema-version.js +1 -1
- package/dist/core/grants/local.js +1 -1
- package/dist/core/grants/mirror.d.ts +11 -3
- package/dist/core/grants/mirror.js +35 -11
- package/dist/core/grants/shadow.js +4 -2
- package/dist/core/grants/sync.js +5 -4
- package/dist/core/ledger/payer.d.ts +4 -4
- package/dist/core/ledger/payer.js +8 -3
- package/dist/core/llm/app-rungs.js +2 -2
- package/dist/core/llm/run.d.ts +2 -2
- package/dist/core/llm/run.js +3 -2
- package/dist/core/runtime/start-check.js +2 -2
- package/dist/daemon/api/handlers/git-credential.js +12 -2
- package/dist/daemon/api/principal.d.ts +11 -5
- package/dist/daemon/api/principal.js +26 -7
- package/package.json +1 -1
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/** The canvas schema version this build supports: the number of the highest delta in `schema-pg/` (the baseline
|
|
2
2
|
* `schema-pg.sql` is version 1). `scripts/install-runtime.mjs` reads this module from a candidate generation to refuse
|
|
3
3
|
* one below the live database's `schema_min_reader`. */
|
|
4
|
-
export declare const CANVAS_SCHEMA_VERSION =
|
|
4
|
+
export declare const CANVAS_SCHEMA_VERSION = 37;
|
|
5
5
|
/** The Postgres schema holding the canvas tables: `CRTR_DATABASE_SCHEMA` (`crtr` in the cloud deploy). Unset or empty
|
|
6
6
|
* means the role's own search_path (`public` on a Mac and on a VM). A name that is not a plain identifier is an error:
|
|
7
7
|
* it is put in a `search_path` connection option. */
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
/** The canvas schema version this build supports: the number of the highest delta in `schema-pg/` (the baseline
|
|
7
7
|
* `schema-pg.sql` is version 1). `scripts/install-runtime.mjs` reads this module from a candidate generation to refuse
|
|
8
8
|
* one below the live database's `schema_min_reader`. */
|
|
9
|
-
export const CANVAS_SCHEMA_VERSION =
|
|
9
|
+
export const CANVAS_SCHEMA_VERSION = 37;
|
|
10
10
|
/** The Postgres schema holding the canvas tables: `CRTR_DATABASE_SCHEMA` (`crtr` in the cloud deploy). Unset or empty
|
|
11
11
|
* means the role's own search_path (`public` on a Mac and on a VM). A name that is not a plain identifier is an error:
|
|
12
12
|
* it is put in a `search_path` connection option. */
|
|
@@ -22,5 +22,5 @@ export async function installLocalTerminalGrant(installId, person) {
|
|
|
22
22
|
await replaceGrants([{ id, runtime: installId, sub: person,
|
|
23
23
|
grantee: 'app:terminal', rev: 1, scopes: TERMINAL_SCOPES,
|
|
24
24
|
resolved_scopes: TERMINAL_SCOPES, spend_cap: null, expires_at: null,
|
|
25
|
-
status: 'active', revoked_at: null, approved_at: now, payer:
|
|
25
|
+
status: 'active', revoked_at: null, approved_at: now, payer: person, dependencies: {}, synced_at: now }], now, 'local');
|
|
26
26
|
}
|
|
@@ -17,6 +17,8 @@ export interface GrantRecord {
|
|
|
17
17
|
runtime: string;
|
|
18
18
|
sub: string;
|
|
19
19
|
grantee: string;
|
|
20
|
+
/** A wildcard grant (`grantee = '*'`): the grantee-principal prefix it covers; null on an exact grant. */
|
|
21
|
+
grantee_prefix?: string | null;
|
|
20
22
|
rev: number;
|
|
21
23
|
scopes: GrantScope[];
|
|
22
24
|
resolved_scopes: GrantScope[];
|
|
@@ -25,15 +27,20 @@ export interface GrantRecord {
|
|
|
25
27
|
status: 'active' | 'revoked' | 'removed';
|
|
26
28
|
revoked_at: string | null;
|
|
27
29
|
approved_at: string | null;
|
|
28
|
-
|
|
30
|
+
/** The principal that pays: the grant's `sub` for an owner-paid grant, its `grantee` for an app-paid one. Null reads as `sub`. */
|
|
31
|
+
payer: string | null;
|
|
29
32
|
/** The directory's setup entry (`{ state, message?, next, step? }`) per setup id the grantee requires. Absent (an older directory) reads as `{}`. */
|
|
30
33
|
dependencies?: GrantDependencies;
|
|
31
34
|
/** The grantee's registered run-notice endpoint (https), or null: runs of this grant send no notice. */
|
|
32
35
|
run_notice_url?: string | null;
|
|
33
|
-
/** The endpoint of an app-paid grant (
|
|
36
|
+
/** The endpoint of an app-paid grant (its payer is an app namespace root); null on a grant that app does not pay or one the directory sent no valid endpoint for. */
|
|
34
37
|
inference_endpoint?: InferenceEndpoint | null;
|
|
35
38
|
synced_at: string;
|
|
36
39
|
}
|
|
40
|
+
/** The payer a mirrored grant carries, as a principal. The directory's older `'user'` (the grant's `sub`) and `'app'` (its `grantee`) read as those principals; anything else must parse as a principal. */
|
|
41
|
+
export declare function principalPayer(payer: unknown, sub: string, grantee: string): string;
|
|
42
|
+
/** Whether the app's inference endpoint pays for this grant's model calls: its payer is an `app:` namespace root. */
|
|
43
|
+
export declare function endpointPays(grant: Pick<GrantRecord, 'payer'>): boolean;
|
|
37
44
|
export interface ActiveGrant extends GrantRecord {
|
|
38
45
|
resolved: Scope[];
|
|
39
46
|
}
|
|
@@ -147,9 +154,10 @@ export interface GrantStanding {
|
|
|
147
154
|
export declare function grantStanding(grantId: string): Promise<GrantStanding>;
|
|
148
155
|
/** Ids of the grants in `status` (this runtime's), for a catch-up pass. */
|
|
149
156
|
export declare function grantIdsWhere(where: string): Promise<string[]>;
|
|
150
|
-
/** Who a grant's model calls are charged to: `app` only
|
|
157
|
+
/** Who a grant's model calls are charged to: `app` only when the app's endpoint pays (`endpointPays`), else the person. */
|
|
151
158
|
export declare function payerOfGrant(grantId: string | null | undefined): Promise<'user' | 'app'>;
|
|
152
159
|
export declare function activeGrant(grantId: string): Promise<ActiveGrant | null>;
|
|
160
|
+
/** `sub`'s active grant to `grantee`: the exact row, else the wildcard row whose `grantee_prefix` is the longest prefix of `grantee`. The `'*'` sentinel never matches as an exact grantee. */
|
|
153
161
|
export declare function grantFor(grantee: string, sub: string): Promise<ActiveGrant | null>;
|
|
154
162
|
/** The app's own active grant on this machine. Null for a mirror-only grant: an
|
|
155
163
|
* unsandboxed machine runs no app, so a fanned-out hosted grant is never "the
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { createHash } from 'node:crypto';
|
|
2
|
-
import { parseScope } from '@north-light/identity';
|
|
2
|
+
import { parsePrincipal, parseScope } from '@north-light/identity';
|
|
3
3
|
import { openDb, withCanvasWrite } from '../canvas/db.js';
|
|
4
4
|
import { isUnsandboxedMachine } from '../runtime/unsandboxed.js';
|
|
5
5
|
import { replaceWrittenBy, userSubject } from '../permissions/facts.js';
|
|
@@ -7,6 +7,21 @@ import { GROUP_TYPE, OBJECT_TYPE } from '../permissions/builtin.js';
|
|
|
7
7
|
import { shadowCheck } from './shadow.js';
|
|
8
8
|
const SETUP_STATES = ['not_started', 'pending', 'ready', 'failed', 'unavailable'];
|
|
9
9
|
const STRENGTHS = ['ultra', 'strong', 'medium', 'light'];
|
|
10
|
+
/** The payer a mirrored grant carries, as a principal. The directory's older `'user'` (the grant's `sub`) and `'app'` (its `grantee`) read as those principals; anything else must parse as a principal. */
|
|
11
|
+
export function principalPayer(payer, sub, grantee) {
|
|
12
|
+
if (payer === undefined || payer === null || payer === 'user')
|
|
13
|
+
return sub;
|
|
14
|
+
if (payer === 'app')
|
|
15
|
+
return grantee;
|
|
16
|
+
if (typeof payer !== 'string' || parsePrincipal(payer) === null)
|
|
17
|
+
throw new TypeError('invalid grant payer');
|
|
18
|
+
return payer;
|
|
19
|
+
}
|
|
20
|
+
/** Whether the app's inference endpoint pays for this grant's model calls: its payer is an `app:` namespace root. */
|
|
21
|
+
export function endpointPays(grant) {
|
|
22
|
+
const payer = grant.payer === null ? null : parsePrincipal(grant.payer);
|
|
23
|
+
return payer !== null && payer.kind === 'app' && payer.path.length === 0;
|
|
24
|
+
}
|
|
10
25
|
/** The fleet's runtime principal (`CRTR_RUNTIME`): a cloud role reads only its own runtime's grants, so a person's VM or laptop grants are never used. A Mac sets none. */
|
|
11
26
|
export function runtimeScope() {
|
|
12
27
|
const runtime = process.env['CRTR_RUNTIME'];
|
|
@@ -93,7 +108,7 @@ function record(row) {
|
|
|
93
108
|
// A row whose computed columns lag its revision (a consent written but not yet computed) reads as no grant, never as the previous scopes.
|
|
94
109
|
if (!computedFor(row.rev, row.computed_rev, row.scopes_json !== null, row.resolved_scopes_json !== null))
|
|
95
110
|
throw new TypeError('grant columns are not computed for this revision');
|
|
96
|
-
return { id: row.id, runtime: row.runtime, sub: row.sub, grantee: row.grantee,
|
|
111
|
+
return { id: row.id, runtime: row.runtime, sub: row.sub, grantee: row.grantee, grantee_prefix: row.grantee_prefix ?? null,
|
|
97
112
|
rev: row.rev, scopes: entries(JSON.parse(row.scopes_json)),
|
|
98
113
|
resolved_scopes: entries(JSON.parse(row.resolved_scopes_json)),
|
|
99
114
|
spend_cap: row.spend_cap, expires_at: row.expires_at, status: row.status,
|
|
@@ -342,15 +357,22 @@ export async function grantStanding(grantId) {
|
|
|
342
357
|
export async function grantIdsWhere(where) {
|
|
343
358
|
return (await grantRows('id', where)).map((row) => row.id);
|
|
344
359
|
}
|
|
345
|
-
/** Who a grant's model calls are charged to: `app` only
|
|
360
|
+
/** Who a grant's model calls are charged to: `app` only when the app's endpoint pays (`endpointPays`), else the person. */
|
|
346
361
|
export async function payerOfGrant(grantId) {
|
|
347
|
-
|
|
362
|
+
const grant = grantId ? await grantRecord(grantId) : null;
|
|
363
|
+
return grant !== null && endpointPays(grant) ? 'app' : 'user';
|
|
348
364
|
}
|
|
349
365
|
export async function activeGrant(grantId) {
|
|
350
366
|
return active(await grantRow('*', 'id = ?', [grantId]));
|
|
351
367
|
}
|
|
368
|
+
/** `sub`'s active grant to `grantee`: the exact row, else the wildcard row whose `grantee_prefix` is the longest prefix of `grantee`. The `'*'` sentinel never matches as an exact grantee. */
|
|
352
369
|
export async function grantFor(grantee, sub) {
|
|
353
|
-
|
|
370
|
+
const exact = await grantRow('*', 'grantee = ? AND grantee_prefix IS NULL AND sub = ?', [grantee, sub]);
|
|
371
|
+
if (exact)
|
|
372
|
+
return active(exact);
|
|
373
|
+
const rows = await grantRows('*', 'sub = ? AND grantee_prefix IS NOT NULL AND starts_with(?, grantee_prefix)', [sub, grantee]);
|
|
374
|
+
rows.sort((a, b) => b.grantee_prefix.length - a.grantee_prefix.length);
|
|
375
|
+
return active(rows[0]);
|
|
354
376
|
}
|
|
355
377
|
/** The app's own active grant on this machine. Null for a mirror-only grant: an
|
|
356
378
|
* unsandboxed machine runs no app, so a fanned-out hosted grant is never "the
|
|
@@ -430,6 +452,8 @@ export async function replaceGrants(grants, syncedAt, source, teams = []) {
|
|
|
430
452
|
!Number.isInteger(grant.rev) || !['active', 'revoked', 'removed'].includes(grant.status)) {
|
|
431
453
|
throw new TypeError('invalid grant sync response');
|
|
432
454
|
}
|
|
455
|
+
if ((grant.grantee === '*') !== (grant.grantee_prefix != null))
|
|
456
|
+
throw new TypeError('invalid grant sync response');
|
|
433
457
|
ids.add(grant.id);
|
|
434
458
|
entries(grant.scopes);
|
|
435
459
|
entries(grant.resolved_scopes);
|
|
@@ -443,22 +467,22 @@ export async function replaceGrants(grants, syncedAt, source, teams = []) {
|
|
|
443
467
|
? db.prepareUncached(`DELETE FROM grants WHERE id NOT IN (${kept.map(() => '?').join(',')})`).run(...kept)
|
|
444
468
|
: db.prepareUncached('DELETE FROM grants').run())).changes);
|
|
445
469
|
const upsert = db.prepare(`INSERT INTO grants
|
|
446
|
-
(id, runtime, sub, grantee, rev, scopes_json, resolved_scopes_json, spend_cap, expires_at,
|
|
470
|
+
(id, runtime, sub, grantee, grantee_prefix, rev, scopes_json, resolved_scopes_json, spend_cap, expires_at,
|
|
447
471
|
status, revoked_at, approved_at, payer, dependencies_json, run_notice_url, inference_endpoint_json, synced_at, computed_rev)
|
|
448
|
-
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
449
|
-
ON CONFLICT (id) DO UPDATE SET runtime = excluded.runtime, sub = excluded.sub, grantee = excluded.grantee, rev = excluded.rev,
|
|
472
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
473
|
+
ON CONFLICT (id) DO UPDATE SET runtime = excluded.runtime, sub = excluded.sub, grantee = excluded.grantee, grantee_prefix = excluded.grantee_prefix, rev = excluded.rev,
|
|
450
474
|
scopes_json = excluded.scopes_json, resolved_scopes_json = excluded.resolved_scopes_json, spend_cap = excluded.spend_cap,
|
|
451
475
|
expires_at = excluded.expires_at, status = excluded.status, revoked_at = excluded.revoked_at, approved_at = excluded.approved_at,
|
|
452
476
|
payer = excluded.payer, dependencies_json = excluded.dependencies_json, run_notice_url = excluded.run_notice_url,
|
|
453
477
|
inference_endpoint_json = excluded.inference_endpoint_json, synced_at = excluded.synced_at, computed_rev = excluded.computed_rev
|
|
454
|
-
WHERE (grants.runtime, grants.sub, grants.grantee, grants.rev, grants.scopes_json, grants.resolved_scopes_json, grants.spend_cap,
|
|
478
|
+
WHERE (grants.runtime, grants.sub, grants.grantee, grants.grantee_prefix, grants.rev, grants.scopes_json, grants.resolved_scopes_json, grants.spend_cap,
|
|
455
479
|
grants.expires_at, grants.status, grants.revoked_at, grants.approved_at, grants.payer, grants.dependencies_json,
|
|
456
480
|
grants.run_notice_url, grants.inference_endpoint_json, grants.computed_rev)
|
|
457
|
-
IS DISTINCT FROM (excluded.runtime, excluded.sub, excluded.grantee, excluded.rev, excluded.scopes_json, excluded.resolved_scopes_json,
|
|
481
|
+
IS DISTINCT FROM (excluded.runtime, excluded.sub, excluded.grantee, excluded.grantee_prefix, excluded.rev, excluded.scopes_json, excluded.resolved_scopes_json,
|
|
458
482
|
excluded.spend_cap, excluded.expires_at, excluded.status, excluded.revoked_at, excluded.approved_at, excluded.payer,
|
|
459
483
|
excluded.dependencies_json, excluded.run_notice_url, excluded.inference_endpoint_json, excluded.computed_rev)`);
|
|
460
484
|
for (const grant of grants) {
|
|
461
|
-
changed += Number((await upsert.run(grant.id, grant.runtime, grant.sub, grant.grantee, grant.rev, JSON.stringify(grant.scopes), JSON.stringify(grant.resolved_scopes), grant.spend_cap, grant.expires_at, grant.status, grant.revoked_at, grant.approved_at, grant.payer, JSON.stringify(dependenciesOf(grant.dependencies)), grant.run_notice_url ?? null, grant.inference_endpoint ? JSON.stringify(grant.inference_endpoint) : null, grant.synced_at, grant.rev)).changes);
|
|
485
|
+
changed += Number((await upsert.run(grant.id, grant.runtime, grant.sub, grant.grantee, grant.grantee_prefix ?? null, grant.rev, JSON.stringify(grant.scopes), JSON.stringify(grant.resolved_scopes), grant.spend_cap, grant.expires_at, grant.status, grant.revoked_at, grant.approved_at, principalPayer(grant.payer, grant.sub, grant.grantee), JSON.stringify(dependenciesOf(grant.dependencies)), grant.run_notice_url ?? null, grant.inference_endpoint ? JSON.stringify(grant.inference_endpoint) : null, grant.synced_at, grant.rev)).changes);
|
|
462
486
|
}
|
|
463
487
|
await replaceWrittenBy(db, 'directory', GROUP_TYPE, 'member', teams.flatMap(({ team, members }) => members.map((member) => ({ resourceId: team, subject: userSubject(member) }))));
|
|
464
488
|
await reconcileAppAdminFacts(db);
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
import { directoryIssuer } from '../directory/issuer.js';
|
|
4
4
|
import { outboundFetch } from '../events/outbound.js';
|
|
5
5
|
import { warn } from '../output.js';
|
|
6
|
-
import { canonicalJson } from './mirror.js';
|
|
6
|
+
import { canonicalJson, principalPayer } from './mirror.js';
|
|
7
7
|
let ttlMs = 5_000;
|
|
8
8
|
/** Test hook: how long a cached answer is used before its epochs are re-checked. */
|
|
9
9
|
export function setShadowTtl(ms) { ttlMs = ms; }
|
|
@@ -67,7 +67,9 @@ async function compare(stored) {
|
|
|
67
67
|
shadowCounts.lagging++;
|
|
68
68
|
return;
|
|
69
69
|
}
|
|
70
|
-
|
|
70
|
+
// The directory may still send the older `'user'`/`'app'` payer; the mirror stores it as a principal.
|
|
71
|
+
const liveSide = { ...live, payer: principalPayer(live['payer'], stored.sub, stored.grantee) };
|
|
72
|
+
const fields = COMPARED.filter((key) => canonicalJson(liveSide[key] ?? null) !== canonicalJson(stored[key] ?? null));
|
|
71
73
|
if (fields.length === 0) {
|
|
72
74
|
shadowCounts.agree++;
|
|
73
75
|
return;
|
package/dist/core/grants/sync.js
CHANGED
|
@@ -7,7 +7,7 @@ import { reconcilePackages } from '../graph/package-reconcile.js';
|
|
|
7
7
|
import { isUnsandboxedMachine } from '../runtime/unsandboxed.js';
|
|
8
8
|
import { emitEvent } from '../events/emit.js';
|
|
9
9
|
import { describeOutboundFailure, outboundFetch, outboundJson, outboundRefusal } from '../events/outbound.js';
|
|
10
|
-
import { dependenciesOf, inferenceEndpointOf, replaceGrants, teamsOf } from './mirror.js';
|
|
10
|
+
import { dependenciesOf, endpointPays, inferenceEndpointOf, principalPayer, replaceGrants, teamsOf } from './mirror.js';
|
|
11
11
|
import { onGrantSync } from './on-sync.js';
|
|
12
12
|
import { hostedEndpointOf, paymentSetupUrlOf, storeHostedEndpoint } from './hosted-endpoint.js';
|
|
13
13
|
import { adoptInstallPerson } from '../scoped-state/migrate.js';
|
|
@@ -108,11 +108,12 @@ function responseGrant(value, client) {
|
|
|
108
108
|
const approvedAt = revisions?.find((revision) => revision.rev === grant.rev)?.approved_at;
|
|
109
109
|
if (typeof approvedAt !== 'string')
|
|
110
110
|
throw new TypeError('grant revision has no approval time');
|
|
111
|
-
//
|
|
112
|
-
const payer = grant.payer
|
|
111
|
+
// The payer is a principal; an older directory's `'user'`/`'app'` (or none) maps to the grant's `sub`/`grantee`. The endpoint rides only a grant the app's endpoint pays for.
|
|
112
|
+
const payer = principalPayer(grant.payer, grant.sub, grant.grantee);
|
|
113
113
|
return { ...grant, spend_cap: grant.spend_cap ?? null, expires_at: grant.expires_at ?? null,
|
|
114
|
+
grantee_prefix: typeof grant.grantee_prefix === 'string' ? grant.grantee_prefix : null,
|
|
114
115
|
revoked_at: grant.revoked_at ?? null, approved_at: approvedAt, payer, dependencies: dependenciesOf(grant.dependencies),
|
|
115
|
-
inference_endpoint: payer
|
|
116
|
+
inference_endpoint: endpointPays({ payer }) ? inferenceEndpointOf(grant.inference_endpoint) : null,
|
|
116
117
|
run_notice_url: typeof grant.run_notice_url === 'string' && grant.run_notice_url !== '' ? grant.run_notice_url : null };
|
|
117
118
|
}
|
|
118
119
|
/** The person this pull serves: the bound one, else (a hosted first boot has none until its first snapshot commits) the one
|
|
@@ -18,16 +18,16 @@ export type PayerCall = (
|
|
|
18
18
|
route: 'subscription' | 'key';
|
|
19
19
|
sub: string;
|
|
20
20
|
}
|
|
21
|
-
/** A tool call over HTTP: the outbound token's `payer` claim
|
|
21
|
+
/** A tool call over HTTP: the outbound token's `payer` claim, any principal. It is always `bill`; `sub` is used only when the claim is absent. */
|
|
22
22
|
| {
|
|
23
23
|
call: 'tool';
|
|
24
|
-
tokenPayer: string;
|
|
24
|
+
tokenPayer: string | null | undefined;
|
|
25
25
|
sub: string;
|
|
26
26
|
}
|
|
27
|
-
/** The payer of a node's model calls, for the start check:
|
|
27
|
+
/** The payer of a node's model calls, for the start check: the grant's principal payer. With none, the grantee's namespace owner when `sub` is in that namespace, else `sub`. */
|
|
28
28
|
| {
|
|
29
29
|
call: 'start';
|
|
30
|
-
grantPayer:
|
|
30
|
+
grantPayer: string | null | undefined;
|
|
31
31
|
sub: string;
|
|
32
32
|
grantee: string;
|
|
33
33
|
}) & {
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
// Payer resolution (design-ledger, "Payer resolution"): one function. It reads the grant's payer and the credential route,
|
|
2
2
|
// never a request header, so a caller cannot name its payer.
|
|
3
|
+
import { endpointPays } from '../grants/mirror.js';
|
|
4
|
+
import { isNamespaceOwner } from '../grants/owner.js';
|
|
3
5
|
/** Who pays, and with what funding. A device call is not priced and never asks. */
|
|
4
6
|
export function payerOf(call) {
|
|
5
7
|
const paid = fundedBy(call);
|
|
@@ -10,9 +12,12 @@ function fundedBy(call) {
|
|
|
10
12
|
case 'model':
|
|
11
13
|
return call.route === 'endpoint' ? { payer: call.grantee, funding: 'endpoint' } : { payer: call.sub, funding: call.route };
|
|
12
14
|
case 'tool':
|
|
13
|
-
return
|
|
14
|
-
case 'start':
|
|
15
|
+
return { payer: call.tokenPayer || call.sub, funding: 'bill' };
|
|
16
|
+
case 'start': {
|
|
15
17
|
// Funding is chosen per candidate later; a start check ignores it.
|
|
16
|
-
|
|
18
|
+
if (call.grantPayer)
|
|
19
|
+
return { payer: call.grantPayer, funding: endpointPays({ payer: call.grantPayer }) ? 'endpoint' : 'bill' };
|
|
20
|
+
return { payer: isNamespaceOwner(call.grantee, call.sub) ? call.grantee : call.sub, funding: 'bill' };
|
|
21
|
+
}
|
|
17
22
|
}
|
|
18
23
|
}
|
|
@@ -5,7 +5,7 @@ import { defaultModelLaddersConfig } from '../../types.js';
|
|
|
5
5
|
import { MODEL_STRENGTHS, stripThinkingSuffix } from '../runtime/model-selection.js';
|
|
6
6
|
import { modelTokenStrength } from '../runtime/launch.js';
|
|
7
7
|
import { RunError } from '../runs/operations.js';
|
|
8
|
-
import { grantRecord } from '../grants/mirror.js';
|
|
8
|
+
import { endpointPays, grantRecord } from '../grants/mirror.js';
|
|
9
9
|
const STRENGTHS = MODEL_STRENGTHS;
|
|
10
10
|
/** The endpoint's declared rungs, nearest to `strength` first (the stronger on a tie). */
|
|
11
11
|
function rungsByDistance(endpoint, strength) {
|
|
@@ -55,7 +55,7 @@ export function appCandidates(endpoint, requested, exact = false) {
|
|
|
55
55
|
* each rung's own level. */
|
|
56
56
|
export async function appRungForGrant(grantId, requested, exact = false) {
|
|
57
57
|
const grant = grantId ? await grantRecord(grantId) : null;
|
|
58
|
-
if (grant
|
|
58
|
+
if (!grant || !endpointPays(grant))
|
|
59
59
|
return null;
|
|
60
60
|
if (!grant.inference_endpoint)
|
|
61
61
|
throw new RunError(404, 'model_unavailable', `${grant.grantee} is app-paid but its grant carries no inference endpoint`);
|
package/dist/core/llm/run.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { type ThinkingLevel } from '@earendil-works/pi-ai/compat';
|
|
2
2
|
import { openModelRegistry } from '../runtime/model-registry.js';
|
|
3
3
|
import { type ManagedProviderId, type SubscriptionCredential } from '../managed-providers.js';
|
|
4
|
-
import type
|
|
4
|
+
import { type GrantRecord, type InferenceEndpoint } from '../grants/mirror.js';
|
|
5
5
|
type WireContent = string | null | Array<{
|
|
6
6
|
type: 'text';
|
|
7
7
|
text: string;
|
|
@@ -70,7 +70,7 @@ export declare function usdEquivalent(model: {
|
|
|
70
70
|
}, total: number): number | null;
|
|
71
71
|
/** Who the call is made for: the caller's grant decides the payer (RT-RTE-12). A caller with no grant (the person) is user-paid. */
|
|
72
72
|
export interface LlmCaller {
|
|
73
|
-
payer:
|
|
73
|
+
payer: string | null;
|
|
74
74
|
sub: string;
|
|
75
75
|
grantee: string;
|
|
76
76
|
inference_endpoint: InferenceEndpoint | null;
|
package/dist/core/llm/run.js
CHANGED
|
@@ -15,6 +15,7 @@ import { eligibleHostedEndpoint, readHostedEndpoint } from '../grants/hosted-end
|
|
|
15
15
|
import { parseProviderPayment } from '../provider-failure.js';
|
|
16
16
|
import { personIdAsync } from '../canvas/person.js';
|
|
17
17
|
import { getNode } from '../canvas/canvas.js';
|
|
18
|
+
import { endpointPays } from '../grants/mirror.js';
|
|
18
19
|
import { outboundToken } from '../providers/mint.js';
|
|
19
20
|
import { discardFile } from '../providers/token-cache.js';
|
|
20
21
|
import { ProviderError } from '../providers/errors.js';
|
|
@@ -154,7 +155,7 @@ function allowedFallback(model, responseModel) {
|
|
|
154
155
|
return fallbacks?.find((fallback) => fallback.provider === model.provider && fallback.model === responseModel);
|
|
155
156
|
}
|
|
156
157
|
export function llmCallerOf(grant) {
|
|
157
|
-
return grant ? { payer: grant.payer
|
|
158
|
+
return grant ? { payer: grant.payer, sub: grant.sub, grantee: grant.grantee, inference_endpoint: grant.inference_endpoint ?? null } : undefined;
|
|
158
159
|
}
|
|
159
160
|
/** A directory-token failure as the `llm` refusal it is (RT-RTE-26): the directory unreachable or failing is
|
|
160
161
|
* transient, a refused mint is a configuration fault. */
|
|
@@ -184,7 +185,7 @@ function vendorRequestId(headers) {
|
|
|
184
185
|
* and a completed call writes one `charges` row. Omitted, the call is neither checked nor recorded.
|
|
185
186
|
* `causedBy`: the request's `Crtr-Caused-By` value (`message:<id>`, `request:<id>`, `cron_run:<id>`), when it carried one; stored on the row. */
|
|
186
187
|
export async function runLlm(input, signal, onChunk, target, spend, caller, causedBy) {
|
|
187
|
-
const app = caller
|
|
188
|
+
const app = caller && endpointPays(caller) ? caller : undefined;
|
|
188
189
|
if (input.payer === 'app' && !app)
|
|
189
190
|
throw new RunError(403, 'payer_not_app', 'this grant is not app-paid');
|
|
190
191
|
if (input.payer === 'user' && app)
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// starts or a dormant node revives. The candidate's payer is whoever pays the node's model calls.
|
|
3
3
|
import { check, environmentPayerOfProfile, findRefusal, payerOf } from '../ledger/index.js';
|
|
4
4
|
import { RunError } from '../runs/operations.js';
|
|
5
|
-
import {
|
|
5
|
+
import { grantRecord, grantStanding } from '../grants/mirror.js';
|
|
6
6
|
import { callerTerminalGrant } from '../grants/terminal.js';
|
|
7
7
|
/** The start candidate for an identity, or null when no person is known yet (a terminal node before its first grant snapshot). */
|
|
8
8
|
export async function startCandidateOf(who) {
|
|
@@ -10,7 +10,7 @@ export async function startCandidateOf(who) {
|
|
|
10
10
|
const sub = who.sub ?? (grantee === 'app:terminal' ? (await callerTerminalGrant())?.sub : undefined) ?? null;
|
|
11
11
|
if (sub === null)
|
|
12
12
|
return null;
|
|
13
|
-
const paid = await
|
|
13
|
+
const paid = who.grant_id ? (await grantRecord(who.grant_id))?.payer : null;
|
|
14
14
|
const payer = payerOf({ call: 'start', grantPayer: paid, sub, grantee, environmentPayer: await environmentPayerOfProfile(who.profile_id) });
|
|
15
15
|
return { scope: 'start', payer: payer.payer, sub, grantee };
|
|
16
16
|
}
|
|
@@ -48,8 +48,18 @@ export async function mintInstallationToken(sub, repositories, allowPullRequestW
|
|
|
48
48
|
if (!response.ok) {
|
|
49
49
|
const code = typeof parsed.error === 'object' ? parsed.error?.code : parsed.code;
|
|
50
50
|
const message = (typeof parsed.error === 'object' ? parsed.error?.message : parsed.message) ?? `the GitHub service answered ${response.status}`;
|
|
51
|
-
|
|
52
|
-
|
|
51
|
+
// The GitHub app names the page where the person links GitHub and installs the App; the CLI shows it as the next step.
|
|
52
|
+
const next = typeof parsed.error === 'object' ? parsed.error?.next : parsed.next;
|
|
53
|
+
if (response.status === 409 && (code === 'repositories_span_installations' || code === 'github_not_connected')) {
|
|
54
|
+
const connect = typeof next === 'string' && next !== '' ? next : undefined;
|
|
55
|
+
throw new ApiError(409, code, message, { repositories, ...(connect === undefined ? {} : {
|
|
56
|
+
error: code, message,
|
|
57
|
+
next: code === 'github_not_connected'
|
|
58
|
+
? `Ask the person to open ${connect}, link GitHub and install the Northlight GitHub App on the account that owns these repositories (choose "All repositories"), then retry.`
|
|
59
|
+
: `List repositories from one GitHub account only; manage installations at ${connect}.`,
|
|
60
|
+
url: connect,
|
|
61
|
+
}) });
|
|
62
|
+
}
|
|
53
63
|
throw new ApiError(502, 'github_service_failed', message, { status: response.status, ...(code === undefined ? {} : { code }) });
|
|
54
64
|
}
|
|
55
65
|
if (typeof parsed.token !== 'string' || typeof parsed.expires_at !== 'string')
|
|
@@ -5,9 +5,12 @@ import { type NoticeTicket } from './sync-notice.js';
|
|
|
5
5
|
export interface AppPrincipal {
|
|
6
6
|
sub: string;
|
|
7
7
|
grantee: string;
|
|
8
|
+
/** The caller: the token's presenting link (an app, an app owning `sub`'s namespace, or a person acting through an exchanged token). */
|
|
9
|
+
actor: string;
|
|
8
10
|
client_id: string;
|
|
9
|
-
grant
|
|
10
|
-
|
|
11
|
+
/** Null for an owner principal, which holds its namespace without a grant row. */
|
|
12
|
+
grant: string | null;
|
|
13
|
+
rev: number | null;
|
|
11
14
|
resolved: Scope[];
|
|
12
15
|
resolved_scopes: GrantScope[];
|
|
13
16
|
spend_cap: number | null;
|
|
@@ -58,9 +61,10 @@ export declare class AppPrincipalResolver {
|
|
|
58
61
|
* 503 before the mirror loads; every other failure is 401. */
|
|
59
62
|
verifyCall(authorization: string | undefined): Promise<CallToken>;
|
|
60
63
|
resolve(authorization: string | undefined, scope?: string): Promise<AppPrincipal>;
|
|
61
|
-
/** The caller an already-verified
|
|
62
|
-
* (
|
|
63
|
-
*
|
|
64
|
+
/** The caller an already-verified token names, from `chain[0]` (which must be the whole chain): (a) an app link holding a
|
|
65
|
+
* grant, (b) an owner link (a grantless app link whose app owns `sub`'s namespace: no grant row, the app's declared
|
|
66
|
+
* scopes), or (c) a person's grant-bearing link on an exchanged token. A grant is covered by the mirror and read now
|
|
67
|
+
* (`coverGrant`). Any other presenter is refused 401. */
|
|
64
68
|
resolveVerified(token: CallToken, scope?: string): Promise<AppPrincipal>;
|
|
65
69
|
}
|
|
66
70
|
/** The mirrored grant a caller's token names, read now, when it is still the
|
|
@@ -68,3 +72,5 @@ export declare class AppPrincipalResolver {
|
|
|
68
72
|
* Otherwise 401 `grant_revoked`. `resolve()` and every later re-read during a
|
|
69
73
|
* request (a `grant.get` long-poll waking) apply this one check. */
|
|
70
74
|
export declare function callerGrant(caller: Pick<AppPrincipal, 'grant' | 'sub' | 'grantee'>): Promise<ActiveGrant>;
|
|
75
|
+
/** The grant id of a caller that must hold a grant row; an owner principal (CR-9 (b)) holds none, so the operation is 403. */
|
|
76
|
+
export declare function grantIdOf(caller: Pick<AppPrincipal, 'grant'>): string;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { covers, JwksCache, MissingRevClaimError, verifyCallToken } from '@north-light/identity';
|
|
1
|
+
import { covers, JwksCache, parsePrincipal, MissingRevClaimError, verifyCallToken } from '@north-light/identity';
|
|
2
2
|
import { createLocalJWKSet, decodeProtectedHeader, jwtVerify } from 'jose';
|
|
3
3
|
import { callerGrantOf, coverGrant } from '../../core/grants/cover.js';
|
|
4
|
+
import { ownerScopes } from '../../core/grants/owner.js';
|
|
4
5
|
import { RunError } from '../../core/runs/operations.js';
|
|
5
6
|
import { NoticeLedger } from './sync-notice.js';
|
|
6
7
|
/** The 401 for a handoff that does not verify; one message per purpose, shared by the resolver and the vendor and key pages. */
|
|
@@ -119,21 +120,33 @@ export class AppPrincipalResolver {
|
|
|
119
120
|
async resolve(authorization, scope) {
|
|
120
121
|
return this.resolveVerified(await this.verifyCall(authorization), scope);
|
|
121
122
|
}
|
|
122
|
-
/** The caller an already-verified
|
|
123
|
-
* (
|
|
124
|
-
*
|
|
123
|
+
/** The caller an already-verified token names, from `chain[0]` (which must be the whole chain): (a) an app link holding a
|
|
124
|
+
* grant, (b) an owner link (a grantless app link whose app owns `sub`'s namespace: no grant row, the app's declared
|
|
125
|
+
* scopes), or (c) a person's grant-bearing link on an exchanged token. A grant is covered by the mirror and read now
|
|
126
|
+
* (`coverGrant`). Any other presenter is refused 401. */
|
|
125
127
|
async resolveVerified(token, scope) {
|
|
126
128
|
const link = token.chain[0];
|
|
127
129
|
const claims = token.claims;
|
|
128
|
-
|
|
130
|
+
const grantLink = link !== undefined && link.grant !== undefined && link.rev !== undefined;
|
|
131
|
+
const person = link !== undefined && parsePrincipal(link.sub)?.kind === 'user';
|
|
132
|
+
if (token.chain.length !== 1 || !link || !/^[A-Za-z0-9_-]{1,64}$/.test(claims.client_id) ||
|
|
133
|
+
!(link.owner === true || (grantLink && (link.sub.startsWith('app:') || person))) ||
|
|
129
134
|
(claims.grantee !== undefined && claims.grantee !== link.sub) || (claims['rev'] !== undefined && claims['rev'] !== link.rev)) {
|
|
130
135
|
throw new RunError(401, 'unauthorized', 'invalid directory access token');
|
|
131
136
|
}
|
|
137
|
+
if (link.owner === true) {
|
|
138
|
+
const resolved = ownerScopes(link.sub);
|
|
139
|
+
if (scope && !resolved.some((held) => covers(held, scope))) {
|
|
140
|
+
throw new RunError(403, 'scope_missing', `missing scope ${scope}`, undefined, undefined, scope);
|
|
141
|
+
}
|
|
142
|
+
return { sub: token.sub, grantee: link.sub, actor: link.sub, client_id: claims.client_id, grant: null, rev: null, resolved,
|
|
143
|
+
resolved_scopes: resolved.map((held) => ({ scope: held, mode: 'always' })), spend_cap: null, dependencies: {}, exp: claims.exp };
|
|
144
|
+
}
|
|
132
145
|
const grant = await coverGrant(link.grant, link.rev, token.sub, link.sub, this.syncGrants);
|
|
133
146
|
if (scope && !grant.resolved.some((held) => covers(held, scope))) {
|
|
134
147
|
throw new RunError(403, 'scope_missing', `missing scope ${scope}`, undefined, undefined, scope);
|
|
135
148
|
}
|
|
136
|
-
return { sub: token.sub, grantee: link.sub, client_id: claims.client_id, grant: link.grant,
|
|
149
|
+
return { sub: token.sub, grantee: link.sub, actor: link.sub, client_id: claims.client_id, grant: link.grant,
|
|
137
150
|
rev: grant.rev, resolved: grant.resolved, resolved_scopes: grant.resolved_scopes,
|
|
138
151
|
spend_cap: grant.spend_cap, dependencies: grant.dependencies ?? {}, exp: claims.exp };
|
|
139
152
|
}
|
|
@@ -143,5 +156,11 @@ export class AppPrincipalResolver {
|
|
|
143
156
|
* Otherwise 401 `grant_revoked`. `resolve()` and every later re-read during a
|
|
144
157
|
* request (a `grant.get` long-poll waking) apply this one check. */
|
|
145
158
|
export async function callerGrant(caller) {
|
|
146
|
-
return await callerGrantOf(caller
|
|
159
|
+
return await callerGrantOf(grantIdOf(caller), caller.sub, caller.grantee);
|
|
160
|
+
}
|
|
161
|
+
/** The grant id of a caller that must hold a grant row; an owner principal (CR-9 (b)) holds none, so the operation is 403. */
|
|
162
|
+
export function grantIdOf(caller) {
|
|
163
|
+
if (caller.grant === null)
|
|
164
|
+
throw new RunError(403, 'permission_error', 'this operation needs a grant; an owner token holds none');
|
|
165
|
+
return caller.grant;
|
|
147
166
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@north-light/api",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.751",
|
|
4
4
|
"description": "Typed crtrd /v1 API contract — DTOs, route builders, the error contract, the CrtrClient, and the command-plugin manifest format. Zero runtime dependencies.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/api/index.js",
|