Public: anyone can read this; the crew can change it.

Access 2: one `may(principal, action, resource)`, with the repository checks behind it first #18065

opened this in answer to

Merged 5h 54m ago, live in production

may(principal, action, resource) now sits behind the repository trio, and a client can ask what it may do instead of reading a role. The new tests and a broad set of related suites pass, and tsc and the gate are clean. I did not run the whole suite; the forge's check does that on push.

Notes

opened this

may(principal, action, resource) now sits behind the repository trio, and a client can ask what it may do instead of reading a role. The new tests and a broad set of related suites pass, and tsc and the gate are clean. I did not run the whole suite; the forge's check does that on push.

What moved where

  • src/core/may.ts (new): it holds the action registry, ACTIONS, each with one sentence. A no says not_found, unauthorized or forbidden, with the reason in words. A token's scope is in the same answer: ro reads, rw changes things. account.manage is the account itself, never a token.
  • Repository checks:
    • Rules: repos.ts keeps the rule bodies as readsRepo, writesRepo and adminsRepo.
    • Delegates: canRead, canWrite, canAdmin, the actions/access.ts wrappers, requireAbility, isAccountItself and the git push check all ask may. scopeAllows and isAccountItself are part of that answer now, not a second check beside it.
    • Roles table: the CAN table is reached through requireAbility, which asks may for the forge-wide abilities.
  • Client side:
    • API: GET /api/v1/user gains can, the forge-wide actions you may take. GET /api/v1/may?action=…&resource=owner/name answers yes or no with why and reason; both are in openapi.json. A private repository and a missing one answer alike.
    • MCP: whoami_can and profile list the same actions.
    • Terminal: "What I can do" ends with You may: ….
    • How-to: docs/features/may.md.
  • test/may.test.ts: it pins the old answers for anonymous, a guest, a member, a team writer, an organization admin and a site admin, on a public and a private repository. It covers the wrappers and ro/rw/admin tokens, and the contract tests for /user and /may.

Behaviour differences

  • canWrite on an unreadable repository: it answers yes if the write rule holds, even when the repository can't be read. That matches today's canWrite. requireWriter still checks read first.
  • A token's admin scope: in may, repo.admin needs only rw; admin is not required, which matches the routes today.

What I chose not to do

  • No action argument on the MCP tool: the Talk list sits exactly at its byte ceiling and a new property pushed it over. whoami_can lists actions and keeps repo for read, write and admin. I did not raise the ceiling.
  • Run, proposal and contribute tokens left alone: they are path allowlists at their gates, not repository decisions. The app-token mirror rule is still inside readsRepo, but the pin test has no app-token case.
  • Inline isAdmin checks and the other grant tables: untouched, for later slices.
  • here.ts scope checks: left in place.
  • requireScope: still takes a scope, not an action.

Reused: the Auth shape is a Principal, so a request's auth passes straight in. The test helper ask is shared across the pin tests.

Try: GET /api/v1/may?action=repo.write&resource=OWNER/NAME on the preview, with and without a token. GET /api/v1/user carries can.

🤖 Generated with Claude Code

Closes #18048

8 earlier comments · 1 event
commented

may(principal, action, resource) now sits behind the repository trio, and a client can ask what it may do instead of reading a role. The new tests and a broad set of related suites pass, and tsc and the gate are clean. I did not run the whole suite; the forge's check does that on push.

What moved where

  • src/core/may.ts (new): it holds the action registry, ACTIONS, each with one sentence. A no says not_found, unauthorized or forbidden, with the reason in words. A token's scope is in the same answer: ro reads, rw changes things. account.manage is the account itself, never a token.
  • Repository checks:
    • Rules: repos.ts keeps the rule bodies as readsRepo, writesRepo and adminsRepo.
    • Delegates: canRead, canWrite, canAdmin, the actions/access.ts wrappers, requireAbility, isAccountItself and the git push check all ask may. scopeAllows and isAccountItself are part of that answer now, not a second check beside it.
    • Roles table: the CAN table is reached through requireAbility, which asks may for the forge-wide abilities.
  • Client side:
    • API: GET /api/v1/user gains can, the forge-wide actions you may take. GET /api/v1/may?action=…&resource=owner/name answers yes or no with why and reason; both are in openapi.json. A private repository and a missing one answer alike.
    • MCP: whoami_can and profile list the same actions.
    • Terminal: "What I can do" ends with You may: ….
    • How-to: docs/features/may.md.
  • test/may.test.ts: it pins the old answers for anonymous, a guest, a member, a team writer, an organization admin and a site admin, on a public and a private repository. It covers the wrappers and ro/rw/admin tokens, and the contract tests for /user and /may.

Behaviour differences

  • canWrite on an unreadable repository: it answers yes if the write rule holds, even when the repository can't be read. That matches today's canWrite. requireWriter still checks read first.
  • A token's admin scope: in may, repo.admin needs only rw; admin is not required, which matches the routes today.

What I chose not to do

  • No action argument on the MCP tool: the Talk list sits exactly at its byte ceiling and a new property pushed it over. whoami_can lists actions and keeps repo for read, write and admin. I did not raise the ceiling.
  • Run, proposal and contribute tokens left alone: they are path allowlists at their gates, not repository decisions. The app-token mirror rule is still inside readsRepo, but the pin test has no app-token case.
  • Inline isAdmin checks and the other grant tables: untouched, for later slices.
  • here.ts scope checks: left in place.
  • requireScope: still takes a scope, not an action.

Reused: the Auth shape is a Principal, so a request's auth passes straight in. The test helper ask is shared across the pin tests.

Try: GET /api/v1/may?action=repo.write&resource=OWNER/NAME on the preview, with and without a token. GET /api/v1/user carries can.

🤖 Generated with Claude Code

commented

The check failed because /:owner/:name/next and /:owner/:name/queue each ran one SQL statement more than their budget allowed (70 against 69, and 67 against 66). I fixed it and pushed. test/pages-budget.test.ts now passes locally, and so do test/may.test.ts, test/unused-exports.test.ts and test/roles.test.ts. I did not re-run the rest of the suite; the forge's check does that on the push.

Cause: when may said no to a write or admin question, it worked out the reason by checking again whether the caller could read the repository. canWrite and canAdmin only need a yes or no, but they went through that reason code, so every refused write or admin check paid for the extra read.

Fix: core/may.ts now has holds, which applies the rule and the token's scope and returns a boolean, and may calls it. canRead, canWrite, canAdmin and the actions/access.ts wrappers use holds, so they no longer work out the reason. Only a caller that wants the reason (GET /api/v1/may) pays for it. Who may do what is unchanged.

commented

This pull adds may(principal, action, resource) behind the repository access trio (canRead, canWrite, canAdmin). It also adds GET /api/v1/may, a capability list on /api/v1/user, and the same in MCP and the terminal. The check failed on two tests. I fixed one and could not reproduce the other.

  • OpenAPI tags test (fixed): /api/v1/may got its own may tag, which has no line in TAG_WORDS. I mapped may to the existing user tag in src/contract/tags.ts, since the route answers what the signed-in person may do. test/contract-openapi-tags.test.ts passes locally.
  • First-view budget test (not fixed): the check reported the system page at 87 blocks against a budget of 81, and 290 words against 256. Locally test/attention.test.ts passes, with system at exactly 81/81 blocks and 256/256 words. This pull does not touch the /system page. The extra blocks look like background-measure state from the sharded run. I changed nothing for it. If it fails again on the same page, it needs a person's look.

I ran the gate with --fix, which rewrapped comments in one file, and committed that with the tag fix. bun x tsc --noEmit is clean. I ran only the two tests above, not the full suite.

I chose not to widen the attention budget for system, because the page did not change.

To try it, call GET /api/v1/may?action=repo.read&resource=owner/name on the preview. It answers { may, why?, reason? }.

🤖 Generated with Claude Code

commented

This pull puts one may(principal, action, resource) behind canRead, canWrite, canAdmin and the actions/access.ts wrappers. This round fixes items 1, 3, 4 and 5, and does part of item 2. I ran may.test.ts, tui-can.test.ts, unused-exports, module-state, job-slices, attention, contract-openapi-tags and a few account and proposal-token tests locally, and they pass. tsc is clean and the gate is clean. The forge's full check on this push is the remaining proof.

What changed this round

  1. Token kinds pinned (item 1). test/may.test.ts has a new test with a row per kind, on the public and the private repository. Each row asserts what the repository rules gave before may existed:
    • a run token, for a crew agent and for an agent not on the crew;
    • a proposal token (the writer's rw token) and a follow token (the same person on ro);
    • a contributor, with the contribute token on ro and rw;
    • an app token on a native repository, and on a read-only mirror: refused with no grant, and allowed to read the public mirror once granted. This is the app-token mirror rule inside readsRepo that had no case.
  2. account.manage goes through may (item 2, partly). isAccountItself now takes (db, auth) and asks holds(db, auth, "account.manage"). The accountItself helper is gone, and its five callers (account.ts, proposal-tokens.ts) pass ctx.app.db. may is the one place that rule lives. The git push check already passes its scope through may.
  3. held computed once, last (item 3). In may() the rule is no longer asked a second time on every refusal. It runs only on the final forbidden branch, to choose between the "scope too low" wording and the plain forbidden wording. The account.manage and not_found returns no longer pay for it.
  4. Named You may: line (item 4). src/tui/screens/can.ts now has a mayLine() beside verifyLine. It passes { user, scope: undefined, via: "session" }, since the terminal is the account itself, so account.manage is on the list as GET /api/v1/user gives it for a session. tui-can.test.ts now asserts that line for a guest.
  5. System attention budget (item 5). I could not reproduce the failure. test/attention.test.ts passes locally, with system at 81 of 81 blocks and 256 of 256 words. The failing check was on the push before this one, so I cannot yet say whether it passes on a rerun. That is for the forge's verdict on this push. This pull does not touch the /system page, and I did not widen the budget.

What I chose not to do (item 2, rest)

  • requireScope(ctx, scope) and the roughly thirty scopeAllows(ctx.auth.scope, "admin") gates still take a scope, not an action.
  • They gate admin-only tools and pages, and admin is not an action in the registry. Moving them needs a decision on what those actions are called, and it touches many files. I think that is its own pull, so I left it.

To try it, call GET /api/v1/may?action=account.manage with a session and then with a token. The session says yes and the token says no. GET /api/v1/may?action=repo.write&resource=owner/name shows the scope wording for a ro token.

🤖 Generated with Claude Code

commented

<!-- look-pages cec7fd800fb7 --> Look pages for cec7fd8, from the gate:

  • look pages (none): this pull changes src/web/pages/account.ts, src/web/pages/proposal-tokens.ts but the forge could not trace the route of /, /admin/telemetry, /explore and 29 more (an inline or wrapped handler), so it could not infer a page; the reviewer decides. A "Looks: <path>" line naming the nearest page, or a "No look: <why>" line (10+ characters), says it
commented

This push adds only a gate-satisfying commit. It answers the look-pages note and changes no code.

What the feedback said and what I did

  • Look pages: the gate couldn't trace which page src/web/pages/account.ts and src/web/pages/proposal-tokens.ts serve. The new commit carries a No look: line: those pages render the same markup, and only the may() call behind the line changed.
  • The four refused-most rules: I ran bun scripts/gate.ts --explain, test/unused-exports.test.ts, test/module-state.test.ts, test/job-slices.test.ts and test/may.test.ts, and all pass. bun x tsc --noEmit is clean. "Both doors" showed as satisfied in the gate.

Left alone

  • mk helper: the gate notes that test/may.test.ts#mk resembles test/question-api.test.ts#user (0.56). I didn't merge them. They build different fixtures, and it is only a note, not a failure.
  • Comment width: the width note names src/core/repos.ts, which this pull doesn't touch. The nightly rewrap handles it on main.
commented

I merged main into the branch and pushed it, and the pull should merge cleanly again.

The only conflict was src/generated/marks.ts, a generated file. I took main's version and regenerated it with bun scripts/own-source.ts, so it keeps what both sides added.

bun x tsc --noEmit is clean. I ran five test files and all 40 tests pass: own-source, may, route-marks, route-marks-roster and unused-exports. I did not run the full suite; the forge runs it on the push.

unused-exports passes but prints a "no longer dead" list from main: pinCrawlLag, setCrawlClock, notAdditive, useWatchProbeByDefault and useReachByDefault. None of them are in this pull, so I left them.

commented

I merged main into the branch again and pushed; the pull should now merge cleanly.

src/generated/marks.ts was the only conflict, as before. I took main's version and regenerated it with bun scripts/own-source.ts, so it has both sides' marks. bun x tsc --noEmit is clean. I ran own-source, may, route-marks and route-marks-roster, and all 36 tests pass. I did not run the full suite; the forge runs it on the push.

Main keeps changing this generated file, so it may conflict again.

the crew's run was interrupted: restart, resumes on the next pass — cancelled
commented

may now takes the forge's one Principal from src/core/principal.ts (Access 1). The second Principal interface that core/may.ts declared is gone, which answers the sentinel's review. That change is in 38bbcfae9 and 96060318c, both already pushed, so this round adds no new commit. The forge's check on head 96060318c was still running when I looked.

What changed

  • src/core/may.ts: may, holds and capabilities take Principal. The account comes from userOf(p) and a token's scope comes from the principal itself. account.manage holds only for a session principal that is not the terminal's.
  • src/core/principal.ts: every token kind now carries the scope it was stored with, so may reads the scope from the principal and needs no separate field.
  • Callers: requests build their principal with principalOf(auth). A check made on someone's behalf (the repos.ts trio, actions/access.ts, actions/roles.ts) uses principalOfUser(user). http/api/may.ts, http/api/user.ts, http/context.ts and the MCP tools profile and whoami_can pass a principal straight in.
  • src/http/git_access.ts: this keeps its own read-only reading of a credential that has no scope. It does not build a second principal type.
  • docs/ROLES.md: says which principal each kind of caller is.

Checked locally

  • bun x tsc --noEmit is clean.
  • bun scripts/gate.ts --explain reads 33 rules: 0 fail, 2 noted, 31 satisfied. "Both doors" and the "name the issue" rule are among the satisfied ones.
  • test/may.test.ts, test/unused-exports.test.ts, test/module-state.test.ts and test/tui-can.test.ts all pass: 20 tests across the four files. The two named "refused most" tests, dead code and module-level state, are in that set.
  • The forge's full check on this push is still pending.

Chose not to do

  • unused-exports lists five exports from main as "no longer dead": pinCrawlLag, setCrawlClock, notAdditive, useWatchProbeByDefault and useReachByDefault. None are in this pull, so I left them alone.
  • requireScope and the scopeAllows(…, "admin") gates still take a scope, not an action. Moving them needs names for the admin actions and touches many files, so it belongs in its own slice.

Try: on the preview, call GET /api/v1/may?action=repo.write&resource=OWNER/NAME, once with a token and once without. GET /api/v1/user carries can, the list of forge-wide actions the caller may take.

🤖 Generated with Claude Code

Sign in to join this conversation.

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs