@cloudhal/agent-runtime 0.6.3 → 0.6.5
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/src/frontmatter-lines.d.ts +10 -0
- package/dist/src/frontmatter-lines.js +42 -0
- package/dist/src/index.d.ts +1 -0
- package/dist/src/index.js +1 -0
- package/dist/src/policy.d.ts +7 -0
- package/dist/src/policy.js +21 -2
- package/dist/src/skills.js +5 -4
- package/dist/src/step-up.d.ts +17 -0
- package/dist/src/step-up.js +64 -0
- package/package.json +4 -4
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The two line shapes of SKILL.md frontmatter, matched in one linear pass each (#174). They give
|
|
3
|
+
* the groups `/^\s+([^:]+):\s*(.*)$/` and `/^([A-Za-z][\w-]*):\s*(.*)$/` gave, including where
|
|
4
|
+
* those regexes backtracked: the first one's `\s+` and `[^:]+` both take whitespace, which made a
|
|
5
|
+
* line of spaces quadratic, and each `\s*` overlapped `(.*)` before a lone `\r`.
|
|
6
|
+
*/
|
|
7
|
+
/** An indented `key: value` line (a nested map entry): `[key, value]`, or undefined. */
|
|
8
|
+
export declare function nestedLine(raw: string): [string, string] | undefined;
|
|
9
|
+
/** A top-level `key: value` line, the key a letter then letters, digits, `_` or `-`. */
|
|
10
|
+
export declare function topLine(raw: string): [string, string] | undefined;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The two line shapes of SKILL.md frontmatter, matched in one linear pass each (#174). They give
|
|
3
|
+
* the groups `/^\s+([^:]+):\s*(.*)$/` and `/^([A-Za-z][\w-]*):\s*(.*)$/` gave, including where
|
|
4
|
+
* those regexes backtracked: the first one's `\s+` and `[^:]+` both take whitespace, which made a
|
|
5
|
+
* line of spaces quadratic, and each `\s*` overlapped `(.*)` before a lone `\r`.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
const isSpace = (ch ) => ch !== undefined && /\s/.test(ch);
|
|
9
|
+
/** What `.` does not match without the `s` flag. */
|
|
10
|
+
const LINE_END = /[\n\r\u2028\u2029]/;
|
|
11
|
+
|
|
12
|
+
/** After the `:` at `colon`: whitespace skipped; undefined when the rest holds a line end. */
|
|
13
|
+
function valueAfter(raw , colon ) {
|
|
14
|
+
let i = colon + 1;
|
|
15
|
+
while (isSpace(raw[i])) i++;
|
|
16
|
+
const rest = raw.slice(i);
|
|
17
|
+
return LINE_END.test(rest) ? undefined : rest;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** An indented `key: value` line (a nested map entry): `[key, value]`, or undefined. */
|
|
21
|
+
export function nestedLine(raw ) {
|
|
22
|
+
let lead = 0;
|
|
23
|
+
while (isSpace(raw[lead])) lead++;
|
|
24
|
+
if (lead === 0) return undefined;
|
|
25
|
+
const colon = raw.indexOf(":");
|
|
26
|
+
if (colon < 0) return undefined;
|
|
27
|
+
// The key needs a character: when the indent runs up to the `:`, its last space is the key.
|
|
28
|
+
const start = lead < colon ? lead : lead >= 2 ? lead - 1 : -1;
|
|
29
|
+
if (start < 0) return undefined;
|
|
30
|
+
const value = valueAfter(raw, colon);
|
|
31
|
+
return value === undefined ? undefined : [raw.slice(start, colon), value];
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** A top-level `key: value` line, the key a letter then letters, digits, `_` or `-`. */
|
|
35
|
+
export function topLine(raw ) {
|
|
36
|
+
if (!/^[A-Za-z]/.test(raw)) return undefined;
|
|
37
|
+
let j = 1;
|
|
38
|
+
while (j < raw.length && /[\w-]/.test(raw[j] )) j++;
|
|
39
|
+
if (raw[j] !== ":") return undefined;
|
|
40
|
+
const value = valueAfter(raw, j);
|
|
41
|
+
return value === undefined ? undefined : [raw.slice(0, j), value];
|
|
42
|
+
}
|
package/dist/src/index.d.ts
CHANGED
package/dist/src/index.js
CHANGED
package/dist/src/policy.d.ts
CHANGED
|
@@ -51,6 +51,8 @@ export type OwnLimit = {
|
|
|
51
51
|
level: "own";
|
|
52
52
|
target: "release";
|
|
53
53
|
};
|
|
54
|
+
/** The own variants of `scope` that satisfy `tool` with a limit, strongest first (none without `own`). */
|
|
55
|
+
export declare function ownVariantsOf(tool: ToolPolicy, scope: Scope): Scope[];
|
|
54
56
|
/**
|
|
55
57
|
* What a call does, decided from its arguments (RFC-0007 §10.2), for tools whose policy depends
|
|
56
58
|
* on them (`moderate_comments` may hide in `edits` mode but never approve):
|
|
@@ -171,6 +173,11 @@ export type Decision = {
|
|
|
171
173
|
kind: "deny";
|
|
172
174
|
reason: string;
|
|
173
175
|
code?: "missing_scope" | "mode" | "skill";
|
|
176
|
+
/**
|
|
177
|
+
* `missing_scope` only: the required scopes (`proposeScopes` for a propose call) that
|
|
178
|
+
* neither the caller's scopes nor an own variant of them satisfy (RFC-0044 §1 step-up).
|
|
179
|
+
*/
|
|
180
|
+
missing?: Scope[];
|
|
174
181
|
} | {
|
|
175
182
|
kind: "approval-required";
|
|
176
183
|
reason: string;
|
package/dist/src/policy.js
CHANGED
|
@@ -188,6 +188,11 @@ const OWN_VARIANTS
|
|
|
188
188
|
"releases:publish": [{ scope: "releases:publish:own", level: "own" }],
|
|
189
189
|
};
|
|
190
190
|
|
|
191
|
+
/** The own variants of `scope` that satisfy `tool` with a limit, strongest first (none without `own`). */
|
|
192
|
+
export function ownVariantsOf(tool , scope ) {
|
|
193
|
+
return tool.own ? (OWN_VARIANTS[scope] ?? []).map((v) => v.scope) : [];
|
|
194
|
+
}
|
|
195
|
+
|
|
191
196
|
/**
|
|
192
197
|
* What a call does, decided from its arguments (RFC-0007 §10.2), for tools whose policy depends
|
|
193
198
|
* on them (`moderate_comments` may hide in `edits` mode but never approve):
|
|
@@ -307,7 +312,16 @@ export function effectiveMode(principal ) {
|
|
|
307
312
|
|
|
308
313
|
|
|
309
314
|
|
|
310
|
-
|
|
315
|
+
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
|
|
311
325
|
|
|
312
326
|
|
|
313
327
|
|
|
@@ -379,7 +393,12 @@ export class PolicyEngine {
|
|
|
379
393
|
missing.push(s);
|
|
380
394
|
}
|
|
381
395
|
if (missing.length > 0)
|
|
382
|
-
return {
|
|
396
|
+
return {
|
|
397
|
+
kind: "deny",
|
|
398
|
+
reason: `missing scope ${missing.join(", ")}`,
|
|
399
|
+
code: "missing_scope",
|
|
400
|
+
missing,
|
|
401
|
+
};
|
|
383
402
|
if (principal.skill) {
|
|
384
403
|
// A skill may only call tools inside the scopes it declared (own variants count).
|
|
385
404
|
const declared = principal.skill.scopes;
|
package/dist/src/skills.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { readdir, readFile, stat } from "node:fs/promises";
|
|
2
2
|
import path from "node:path";
|
|
3
3
|
import { assertValid, } from "@cloudhal/core-contracts";
|
|
4
|
+
import { nestedLine, topLine } from "./frontmatter-lines.js";
|
|
4
5
|
|
|
5
6
|
|
|
6
7
|
|
|
@@ -36,14 +37,14 @@ export function parseFrontmatter(markdown )
|
|
|
36
37
|
let currentMap ;
|
|
37
38
|
for (const raw of (m[1] ).split(/\r?\n/)) {
|
|
38
39
|
if (!raw.trim() || raw.trim().startsWith("#")) continue;
|
|
39
|
-
const nested = raw
|
|
40
|
+
const nested = nestedLine(raw);
|
|
40
41
|
if (nested && currentMap) {
|
|
41
|
-
currentMap[nested[
|
|
42
|
+
currentMap[nested[0]] = unquote(nested[1].trim());
|
|
42
43
|
continue;
|
|
43
44
|
}
|
|
44
|
-
const top = raw
|
|
45
|
+
const top = topLine(raw);
|
|
45
46
|
if (!top) throw new SkillError(`invalid frontmatter line: ${raw}`);
|
|
46
|
-
const [
|
|
47
|
+
const [key, value] = top;
|
|
47
48
|
if (value.trim() === "") {
|
|
48
49
|
currentMap = {};
|
|
49
50
|
fm[key] = currentMap;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* RFC-0044 §1 step-up: which scopes an OAuth connection must add for a call it was denied. The
|
|
3
|
+
* answer comes from the same decision the MCP handler makes (`PolicyEngine.evaluate`), so it
|
|
4
|
+
* follows the own-variant lattice, a propose call's `proposeScopes`, and the member's role on the
|
|
5
|
+
* call's site (RFC-0034 A2), not their installation role.
|
|
6
|
+
*/
|
|
7
|
+
import { type PolicyEngine, type Principal, type Scope } from "./policy.js";
|
|
8
|
+
/**
|
|
9
|
+
* The scopes to ask for so that `principal` may make the call `toolName(args)`, each the
|
|
10
|
+
* strongest one a token can hold on the call's site: given by the member's role there and by
|
|
11
|
+
* their installation role, which caps what consent issues (`site:publish:own` for an Author, not
|
|
12
|
+
* `site:publish`). Undefined when the call is not denied for a missing scope, when the principal
|
|
13
|
+
* has no person behind it, when the roles cannot give every missing scope, or when the call
|
|
14
|
+
* would still be denied with them (plan mode, a skill that does not cover it): a challenge would
|
|
15
|
+
* then make the client re-authorize for nothing, or loop.
|
|
16
|
+
*/
|
|
17
|
+
export declare function stepUpScopes(policy: PolicyEngine, principal: Principal, toolName: string, args?: Record<string, unknown>): Scope[] | undefined;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* RFC-0044 §1 step-up: which scopes an OAuth connection must add for a call it was denied. The
|
|
3
|
+
* answer comes from the same decision the MCP handler makes (`PolicyEngine.evaluate`), so it
|
|
4
|
+
* follows the own-variant lattice, a propose call's `proposeScopes`, and the member's role on the
|
|
5
|
+
* call's site (RFC-0034 A2), not their installation role.
|
|
6
|
+
*/
|
|
7
|
+
import {
|
|
8
|
+
holds,
|
|
9
|
+
isKeyScope,
|
|
10
|
+
ownVariantsOf,
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
} from "./policy.js";
|
|
15
|
+
import { narrow, roleScopes } from "./roles.js";
|
|
16
|
+
import { forInstallation, forSite, INSTALLATION_SCOPES, isSiteLimited } from "./site-access.js";
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The scopes to ask for so that `principal` may make the call `toolName(args)`, each the
|
|
20
|
+
* strongest one a token can hold on the call's site: given by the member's role there and by
|
|
21
|
+
* their installation role, which caps what consent issues (`site:publish:own` for an Author, not
|
|
22
|
+
* `site:publish`). Undefined when the call is not denied for a missing scope, when the principal
|
|
23
|
+
* has no person behind it, when the roles cannot give every missing scope, or when the call
|
|
24
|
+
* would still be denied with them (plan mode, a skill that does not cover it): a challenge would
|
|
25
|
+
* then make the client re-authorize for nothing, or loop.
|
|
26
|
+
*/
|
|
27
|
+
export function stepUpScopes(
|
|
28
|
+
policy ,
|
|
29
|
+
principal ,
|
|
30
|
+
toolName ,
|
|
31
|
+
args = {},
|
|
32
|
+
) {
|
|
33
|
+
const tool = policy.tool(toolName);
|
|
34
|
+
if (!tool || !principal.person) return undefined;
|
|
35
|
+
// The handler judges the call as the principal acts on its site (`handleToolCall`).
|
|
36
|
+
const siteId = typeof args.siteId === "string" ? args.siteId : undefined;
|
|
37
|
+
const onSite = (p ) => (siteId ? forSite(p, siteId) : forInstallation(p));
|
|
38
|
+
const sited = onSite(principal);
|
|
39
|
+
const person = sited?.person;
|
|
40
|
+
if (!sited || !person) return undefined;
|
|
41
|
+
const decision = policy.evaluate(sited, toolName, { args });
|
|
42
|
+
if (decision.kind !== "deny" || decision.code !== "missing_scope" || !decision.missing?.length)
|
|
43
|
+
return undefined;
|
|
44
|
+
// Consent caps a token by the installation role, as key creation does; `forSite` then caps it
|
|
45
|
+
// by the site role. Only scopes both give survive, so only those are asked for.
|
|
46
|
+
const installation = roleScopes(principal.person.role);
|
|
47
|
+
const both = narrow(roleScopes(person.role), installation);
|
|
48
|
+
const grantable =
|
|
49
|
+
!siteId && isSiteLimited(person) ? both.filter((s) => !INSTALLATION_SCOPES.includes(s)) : both;
|
|
50
|
+
const asked = [];
|
|
51
|
+
for (const scope of decision.missing) {
|
|
52
|
+
const pick = [scope, ...ownVariantsOf(tool, scope)].find(
|
|
53
|
+
(s) => isKeyScope(s) && holds(grantable, s),
|
|
54
|
+
);
|
|
55
|
+
if (!pick) return undefined;
|
|
56
|
+
if (!asked.includes(pick)) asked.push(pick);
|
|
57
|
+
}
|
|
58
|
+
// The call as it would run after consent must not be denied for any reason (scope, mode,
|
|
59
|
+
// skill): otherwise the client would re-authorize for nothing, or loop.
|
|
60
|
+
const granted = narrow([...(principal.keyScopes ?? principal.scopes), ...asked], installation);
|
|
61
|
+
const after = onSite({ ...principal, scopes: granted, keyScopes: granted });
|
|
62
|
+
if (!after) return undefined;
|
|
63
|
+
return policy.evaluate(after, toolName, { args }).kind === "deny" ? undefined : asked;
|
|
64
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cloudhal/agent-runtime",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.5",
|
|
4
4
|
"description": "Cloudhal agent runtime: policy engine (actors, scopes, approvals), skill loader, audit log",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"homepage": "https://cloudhal.dev",
|
|
@@ -24,14 +24,14 @@
|
|
|
24
24
|
"README.md"
|
|
25
25
|
],
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@cloudhal/core-contracts": "0.6.
|
|
28
|
-
"@cloudhal/storage": "0.6.
|
|
27
|
+
"@cloudhal/core-contracts": "0.6.5",
|
|
28
|
+
"@cloudhal/storage": "0.6.5"
|
|
29
29
|
},
|
|
30
30
|
"devDependencies": {
|
|
31
31
|
"@types/node": "^26.6.5",
|
|
32
32
|
"better-sqlite3": "13.0.3",
|
|
33
33
|
"typescript": "7.0.2",
|
|
34
|
-
"vitest": "5.0
|
|
34
|
+
"vitest": "5.1.0"
|
|
35
35
|
},
|
|
36
36
|
"engines": {
|
|
37
37
|
"node": ">=24"
|