@devfellowship/access 0.0.0-stage → 0.2.0
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/README.md +152 -2
- package/capabilities.json +37309 -0
- package/db-surface.json +920 -0
- package/dist/bindings.d.ts +61 -0
- package/dist/bindings.js +88 -0
- package/dist/can.d.ts +25 -0
- package/dist/can.js +113 -0
- package/dist/capability.d.ts +20 -0
- package/dist/capability.js +51 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +8 -0
- package/dist/line.d.ts +22 -0
- package/dist/line.js +46 -0
- package/dist/principal.d.ts +49 -0
- package/dist/principal.js +73 -0
- package/dist/rule.d.ts +103 -0
- package/dist/rule.js +191 -0
- package/dist/rules.generated.d.ts +14 -0
- package/dist/rules.generated.js +101 -0
- package/dist/runtime.d.ts +20 -0
- package/dist/runtime.js +19 -0
- package/dist/types.d.ts +86 -0
- package/dist/types.js +8 -0
- package/package.json +63 -5
- package/parity.json +663 -0
- package/runtime.pin.json +26 -0
package/README.md
CHANGED
|
@@ -1,3 +1,153 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @devfellowship/access
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The one fleet access decision. The gateway (S3) and `dfl-os-commands` (S2) import the same version.
|
|
4
|
+
Plan: `20261116-fleet-access-s2-access-service` (S2-T1); parent `20261115-fleet-access-iam-gateway-flows-rules` (ADR-5, ADR-16).
|
|
5
|
+
|
|
6
|
+
Pure functions. No I/O, no clock read, no DB function. The caller reads the `iam.permissions` rows with the user's own token and passes them in.
|
|
7
|
+
|
|
8
|
+
```ts
|
|
9
|
+
import { can } from "@devfellowship/access";
|
|
10
|
+
|
|
11
|
+
const decision = can({
|
|
12
|
+
principal: {
|
|
13
|
+
actor_id: agentId,
|
|
14
|
+
kind: "agent",
|
|
15
|
+
role_ids: ["agent"],
|
|
16
|
+
owner: { actor_id: ownerId, kind: "person", role_ids: ["member"] },
|
|
17
|
+
},
|
|
18
|
+
capability: "work.task.create",
|
|
19
|
+
target: { business_unit_id: smokeLabId },
|
|
20
|
+
lines, // iam.permissions rows of the agent, its roles, its owner and the owner's roles
|
|
21
|
+
now: new Date(),
|
|
22
|
+
});
|
|
23
|
+
// { outcome: "allow" | "deny" | "ask", decided_by: "permission:<id>" | "owner_cap" | null, reason, line?, owner? }
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Rules
|
|
27
|
+
|
|
28
|
+
1. **Deny wins.** Among the matching lines in force, deny beats ask and ask beats allow. No matching line: deny (`reason: "no_match"`, `decided_by: null`).
|
|
29
|
+
2. **Match.** A line matches when its capability covers the request and its scope covers the target.
|
|
30
|
+
- `work.task.create` matches only itself. `work.*` matches `work.task.create` but not `work`. `work.*.create` matches one middle segment. `*` matches all. A malformed pattern matches nothing.
|
|
31
|
+
- Scope `all` covers every target. `business_unit` covers `target.business_unit_id`. `item` covers `target.item_id`.
|
|
32
|
+
3. **In force.** A revoked line, or a line with `expires_at` at or before `now`, is ignored. An unparseable `expires_at` keeps a deny or ask line and drops an allow line.
|
|
33
|
+
4. **Agent ∩ owner.** effective(agent) = (agent role bundles ∪ agent lines) ∩ effective(owner). An owner deny gives `decided_by: "owner_cap"`. An ask on either side gives ask. An agent without an owner is denied. The owner chain is at most `MAX_OWNER_DEPTH` (4) long.
|
|
34
|
+
|
|
35
|
+
`decided_by` uses the values of `public.mcp_tool_calls.decided_by` (parent ADR-7).
|
|
36
|
+
|
|
37
|
+
## Rules: level 2 of a write (S2-T3)
|
|
38
|
+
|
|
39
|
+
A rule is a decision table of `dfl-flows-definitions` (`policies/**.dmn.json`) whose output `outcome` is `allow`, `deny` or `ask`. `decideWrite(grant, rule)` runs `can()` first; a deny stops there. Then it evaluates the rule, and the stricter outcome wins (deny over ask over allow).
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
import { decideWrite } from "@devfellowship/access";
|
|
43
|
+
|
|
44
|
+
const decision = decideWrite(canInput, {
|
|
45
|
+
rule: {
|
|
46
|
+
path: "policies/work/task_create_points.dmn.json",
|
|
47
|
+
version: "abc1234", // the short git blob id of the file (engine loader dmnFileVersion)
|
|
48
|
+
definition, // the parsed .dmn.json
|
|
49
|
+
decision_id: "task_create_points",
|
|
50
|
+
inputs: {
|
|
51
|
+
role: { from: "verified", path: "role", type: "string" },
|
|
52
|
+
points: { from: "body", path: "points", type: "number" },
|
|
53
|
+
},
|
|
54
|
+
},
|
|
55
|
+
verified: { role: callerGlobalRole }, // from the verified token and rows read with the user's token
|
|
56
|
+
body: requestBody, // untrusted
|
|
57
|
+
});
|
|
58
|
+
// { outcome, decided_by: "permission:<id>" | "rule:<path>@<version>" | ..., grant, rule? }
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
- **Inputs come only from the binding.** Each table input names its source: `verified` or `body`. A field that the client sends in the body (for example `role: "admin"`) is never read for an input bound to `verified` (parent F22). Paths read own properties only.
|
|
62
|
+
- **Fail closed.** A table input with no source, an absent or mistyped input, an evaluator error, no matching row, a COLLECT table, or an outcome that is not `allow`/`deny`/`ask` gives `deny` with a `reason`.
|
|
63
|
+
- `test/fixtures/task_create_points.dmn.json` is the pilot rule as a test fixture. The rule file itself lands in `dfl-flows-definitions` with the pilot (S2-T6).
|
|
64
|
+
|
|
65
|
+
## Rule bindings and the vendored rules (S2-T6)
|
|
66
|
+
|
|
67
|
+
`src/bindings.ts` says which capability runs which rule, with which inputs, where. The pilot (parent ADR-13, Q5 = A):
|
|
68
|
+
|
|
69
|
+
| capability | rule (manifest key) | inputs | scope |
|
|
70
|
+
|---|---|---|---|
|
|
71
|
+
| `work.task.create` | `task_create_points` | `role` from the verified caller (`verifiedRoleOf(iam.is_global_admin())`: `admin` or `member`), `points` from the body | the Smoke Lab business unit `SMOKE_LAB_WORK_BUSINESS_UNIT_ID` only |
|
|
72
|
+
|
|
73
|
+
```ts
|
|
74
|
+
import { bindingApplies, evaluateRule, ruleBindingOf, ruleRefOf, verifiedRoleOf } from "@devfellowship/access";
|
|
75
|
+
|
|
76
|
+
const binding = ruleBindingOf("work.task.create");
|
|
77
|
+
if (binding && bindingApplies(binding, { business_unit_id })) {
|
|
78
|
+
const d = evaluateRule({ rule: ruleRefOf("work.task.create")!, verified: { role: verifiedRoleOf(isAdmin) }, body });
|
|
79
|
+
// d.outcome: allow | ask (d.output.approver_role) | deny; d.decided_by: rule:<path>@<version>
|
|
80
|
+
}
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
The rule file is not fetched at run time. `scripts/vendor-rules.mjs <dfl-flows-definitions checkout>` copies each bound rule, at the commit of `runtime.pin.json` (`definitions.commit`), into `src/rules.generated.ts` with its version (the engine `dmnFileVersion`, the 7-char git blob id). CI (`access-package / corpus`) runs it with `--check` on the pinned checkout. A new binding: add the key to `VENDORED_KEYS`, re-pin the definitions commit, re-generate. The catalog entry of the capability names the same key (`rule`), from `COMMANDS` in `scripts/catalog-lib.mjs`; `test/bindings.test.mjs` checks that both agree.
|
|
84
|
+
|
|
85
|
+
IAM "Try it" (S4-T6B) runs the same binding: `POST /v1/access/explain` with a rule input evaluates `ruleRefOf(capability)` with the verified role of the subject and the given `points`.
|
|
86
|
+
|
|
87
|
+
## The capability catalog (S2-T2)
|
|
88
|
+
|
|
89
|
+
`capabilities.json` lists one capability name per MCP tool: `<domain>.<entity>.<verb>` (tool `work.create_task` -> `work.task.create`). The domain is the MCP host, the verb is the first word of the tool name, the entity is the rest. It ships in the package. `dfl-os-commands` reads it (`ACCESS_CATALOG_PATH`, or `<package>/capabilities.json`) for the IAM read endpoints (`catalog.available`).
|
|
90
|
+
|
|
91
|
+
Each capability also carries `domain_group`: the big domain of its host (`work`, `marketing`, `money`, `people`, `studio`, `engineering`), read from the fleet domain map `domains.json` of `dfl-mcp-server` (`--domains <file|url>`, default `https://mcp.devfellowship.com/domains.json`). The name keeps the host prefix (plan `20261117-fleet-domain-consolidation`, ADR-4). A host with no group fails the build. `GET /v1/access/capabilities?domain_group=money` filters by it.
|
|
92
|
+
|
|
93
|
+
```bash
|
|
94
|
+
npm run catalog # reads https://mcp.devfellowship.com/tools-manifest.json and domains.json, writes capabilities.json
|
|
95
|
+
npm run catalog:check # writes nothing; exit 1 when the file is out of date
|
|
96
|
+
node scripts/build-catalog.mjs --manifest <file> --gateway-modes <oauth-tools.generated.ts>
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
The build prints `tools: N` and `capabilities: N`. It exits 1 when a tool is **unmapped** (its verb is not in `VERBS`, or its name has no entity and `OVERRIDES` has no entry) or when two tools get one name. A new MCP tool with a new verb therefore fails the `catalog` CI job until someone adds the verb to `VERBS` with its mode (`read` or `write`) in `scripts/catalog-lib.mjs`. With `--gateway-modes`, the build also exits 1 when the catalog offers a write tool of the gateway registry as a read. The **mode guard** (CATALOG-READ-MODE-FIX): a tool whose name starts with `list`, `get`, `wait`, `read`, `search` or `describe` and is `write` fails the build, on the catalog and on the gateway registry, unless `READ_NAME_WRITE_ALLOWLIST` in `scripts/catalog-lib.mjs` names it with the reason it writes (an upload, a paid call, a job that registers media). An allow-list line with no reason, or for a tool that is no longer `write`, also fails. The verb `check` is `write` by default; a check that only reads gets an `OVERRIDES` entry with `mode: "read"`, and the gateway cross-check fails the build if the gateway says `write` for it. A deprecated alias adds no entry. Commands (kind `command`) enter the catalog when the first command exists (S2-T6). The kinds `table`, `rpc` and `function` come from the database surface (next section).
|
|
100
|
+
|
|
101
|
+
## The database surface in the catalog (S7-T4)
|
|
102
|
+
|
|
103
|
+
Plan `20261117-fleet-access-s7-rest-front-dfl-client`. The REST front of the gateway decides a Supabase REST call by a capability name, like an MCP call. `capabilities.json` therefore holds a second source next to the MCP tools, each entry with a `kind`:
|
|
104
|
+
|
|
105
|
+
| kind | name | mode | example |
|
|
106
|
+
|---|---|---|---|
|
|
107
|
+
| `table` | `<schema>.<table>.<select\|insert\|update\|delete>` | `select` read, the rest write | `work.tasks.insert` |
|
|
108
|
+
| `table` (a view) | `<schema>.<view>.select`, `relation: "view"` | read | `work.v_epic_metrics.select` |
|
|
109
|
+
| `rpc` | `<schema>.rpc.<function>` | read when the function is STABLE or IMMUTABLE (PostgREST runs it in a read-only transaction), else write | `iam.rpc.can_access_course` |
|
|
110
|
+
| `function` | `functions.<edge-function>.invoke` | write (fail closed) | `functions.upload-file.invoke` |
|
|
111
|
+
|
|
112
|
+
Entries carry `doors: ["rest"]` and the source object in `table`, `rpc` or `function`. `domain` is the schema (or `functions`). `domain_group` comes from the `schemas` lists of `domains.json`; a schema that no domain lists, and every edge function, get the fallback group `unassigned` (never a crash). The MCP tool entries do not change.
|
|
113
|
+
|
|
114
|
+
**Collision.** A tool and a table can make one name (`strategy.update_keyword_metrics` and the table `strategy.keyword_metrics` both give `strategy.keyword_metrics.update`). One name is one grant, so the table entry takes the entity `<table>_table` (`strategy.keyword_metrics_table.update`) and carries `collides_with: "<tool id>"`. The tool keeps its name. A client must find the capability of a table call through the catalog `table` field, not by building the name from the verb.
|
|
115
|
+
|
|
116
|
+
**Where the surface comes from.** CI cannot read prod, and parsing the 745 migrations is not exact (measured 2026-11-18: about 90 percent; a loop with `format()` makes 30 tables, and words in comments read as tables). So `db-surface.json` is a committed snapshot of the prod catalog (`pg_class`, `pg_proc` of the schemas that `supabase/config.toml` exposes), made by `scripts/db-surface.sql` (read-only, `pg_catalog` only) and written by `scripts/refresh-db-surface.mjs`. CI proves the snapshot is not stale against `devfellowship/dfl-schema` `main`, with no credential beyond the `GH_PAT` checkout: `--schema-repo` fails the build when an edge function was added or removed, when `config.toml` exposes a schema that the snapshot lacks, or when a migration newer than `migration_head` creates, drops or renames a table, view or function in a way that the snapshot does not hold. The daily schedule turns it red when a dfl-schema merge moves the surface.
|
|
117
|
+
|
|
118
|
+
```bash
|
|
119
|
+
# refresh (an agent with the read-only supabase-helpers skill; psql prod stays read-only)
|
|
120
|
+
bash /root/.claude/skills/supabase-helpers/run.sh select "$(cat scripts/db-surface.sql)" > rows.txt
|
|
121
|
+
npm run db-surface -- --schema-repo <dfl-schema main checkout> --rows rows.txt
|
|
122
|
+
npm run catalog # builds capabilities.json from the tools manifest and the snapshot
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
`npm run catalog` prints `table capabilities`, `rpc capabilities` and `function capabilities`, and `capability kind <kind>: N` for each kind. `--db-surface none` builds a catalog of tools only.
|
|
126
|
+
|
|
127
|
+
## The principal: resolvePrincipal()
|
|
128
|
+
|
|
129
|
+
`resolvePrincipal(reads, actorId)` builds the `Principal` of `can()` and returns the lines to pass in: the actor, its role bundles, and for an agent its owner (the delegator of its one active delegation), up to `MAX_OWNER_DEPTH` layers. It does no I/O: the caller passes four reads (`readActor`, `readActorRoles`, `readActorOwners`, `readLines`), each with the caller's own token. `dfl-os-commands` passes SQL in a caller transaction; the gateway passes PostgREST reads. One copy of the owner-chain rules for both (plan `20261116-fleet-access-s3-gateway-decision`, ADR-1).
|
|
130
|
+
|
|
131
|
+
## The parity fixture
|
|
132
|
+
|
|
133
|
+
`parity.json` (shipped, `@devfellowship/access/parity.json`) holds one world (actors, role bundles, owners, permission lines) and the decision of each case. `expected` is the answer of `dfl-os-commands` `runExplain`. Three tests assert it: `test/parity.test.mjs` here, `services/dfl-os-commands/src/commands/access/parity.test.ts` (the service), and the gateway test in `dfl-mcp-server`. A change of a rule that moves an answer changes `parity.json` in the same PR.
|
|
134
|
+
|
|
135
|
+
## Release
|
|
136
|
+
|
|
137
|
+
The source stays here. A merge to `main` that bumps `version` makes the `access-package` workflow tag the commit `access-v<version>`. `dfl-flows-engine` `.github/workflows/access-package-release.yml` builds and tests that tag with no secret, then publishes the tarball with the existing `NPM_TOKEN` org secret (it reaches `dfl-flows-engine`, not this repository). The job runs every 30 minutes and on `workflow_dispatch`. Consumers outside this repository (the gateway in `dfl-mcp-server`) pin the exact version. ADR-1 of plan `20261116-fleet-access-s3-gateway-decision`.
|
|
138
|
+
|
|
139
|
+
## The pinned runtime
|
|
140
|
+
|
|
141
|
+
The evaluator is `@devfellowship/dmn` (dfl-flows-engine `packages/dmn`), the same package that the engine and the `dfl-flows-definitions` CI run. `package.json` pins the exact version. `runtime.pin.json` records the tarball integrity and the SHA-256 of each installed `dist` file; `test/pin.test.mjs` fails when they differ.
|
|
142
|
+
|
|
143
|
+
The rule corpus (`npm run test:corpus -- <dfl-flows-definitions checkout>`) runs every case of `policies/**/*.tests.yml` at the commit in `runtime.pin.json` (`definitions.commit`) on this runtime. It fails on a failed case, on a checkout at another commit, or on fewer than `definitions.min_cases` cases. CI job: `access-package / corpus`.
|
|
144
|
+
|
|
145
|
+
### Re-pin the runtime
|
|
146
|
+
|
|
147
|
+
```bash
|
|
148
|
+
npm install --save-exact @devfellowship/dmn@<version>
|
|
149
|
+
npm ci && npm run pin:runtime # rewrites runtime.pin.json; set runtime.source_commit from `npm view @devfellowship/dmn@<version> gitHead`
|
|
150
|
+
npm test
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
Move the corpus: `npm run pin:runtime -- --definitions-commit <sha>`, then run the corpus on a checkout at that commit.
|