Skip to content

24: first decision: trusted principal, memory facts, can, and decide - #40

Merged
AmaraNecib merged 5 commits into
developfrom
feature/24-first-decision-path
Jul 19, 2026
Merged

24: first decision: trusted principal, memory facts, can, and decide#40
AmaraNecib merged 5 commits into
developfrom
feature/24-first-decision-path

Conversation

@AmaraNecib

@AmaraNecib AmaraNecib commented Jul 18, 2026

Copy link
Copy Markdown
Owner

Refs #24

Summary

Implements the first authorization decision path: a trusted principal can be evaluated against normalized facts supplied by the memory adapter.

Changes

packages/core

  • Added SourceResolver interface for adapter-backed source registration
  • Added Mizan.registerSource(name, resolver) for registering named sources
  • Added Mizan.forPrincipal(principalId) — returns a PrincipalEvaluator bound to the principal
  • Added PrincipalEvaluator class with can() and decide()
  • Implemented collectFacts() with contract validation
  • Implemented evaluate() — exact match, denial-overrides-grant, deny-by-default

packages/memory

  • useMemoryAdapter() now registers the adapter's source on Mizan

Tests

  • 40 tests total (24 core + 16 memory) covering all acceptance criteria

Acceptance criteria

  • Principal-bound evaluator
  • Memory adapter supplies facts without exposing decisions
  • Grant allows when no matching denial
  • No grant = deny by default
  • Denial overrides grant
  • can() returns boolean, decide() returns structured result
  • Expected outcomes don't throw; errors throw
  • End-to-end tests

Summary by CodeRabbit

  • New Features
    • Added principal-scoped authorization evaluation via can()/decide() with structured allow/deny results and reasons.
    • Introduced pluggable authorization sources with registration and principal-bound evaluators.
  • Bug Fixes
    • Implemented the memory adapter so registered facts now correctly drive authorization, including denial-over-grant precedence.
  • Tests
    • Added core and memory decision test suites and improved the memory adapter smoke coverage.
  • Chores
    • Updated ignore rules to exclude .scratch/.
  • Documentation
    • Added internal guidance for code review, security evaluation rules, and testing practices.

Refs #24

- Add SourceResolver interface for adapter-backed source registration
- Add PrincipalEvaluator class with can() and decide() methods
- Add Mizan.registerSource() and Mizan.forPrincipal() for source registration and principal binding
- Implement collectFacts() with contract validation and error handling
- Implement evaluate() with exact-match, denial-overrides-grant, deny-by-default logic
- Implement useMemoryAdapter() to register the memory source on Mizan
- Add end-to-end tests demonstrating the complete principal-to-memory-source-to-decision path
@AmaraNecib

Copy link
Copy Markdown
Owner Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jul 18, 2026

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai

coderabbitai Bot commented Jul 18, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: a1eaa00e-6bea-49e3-a833-e70ecbc3ff81

📥 Commits

Reviewing files that changed from the base of the PR and between 4889c83 and e621dc1.

📒 Files selected for processing (2)
  • .agents/wisdom/process.md
  • .agents/wisdom/testing.md
🚧 Files skipped from review as they are similar to previous changes (2)
  • .agents/wisdom/process.md
  • .agents/wisdom/testing.md

📝 Walkthrough

Walkthrough

The core package now supports registered source resolvers, principal-bound authorization decisions, outcome validation, and deny-overrides-grant evaluation. The memory package registers its adapter and adds integration coverage for grants, denials, roles, and default denial.

Changes

Authorization decision flow

Layer / File(s) Summary
Core source collection and decision evaluation
packages/core/src/index.ts, packages/core/__tests__/decision.test.ts
Adds source registration and validation, principal-bound can/decide evaluation, deny-by-default behavior, and decision tests.
Memory adapter source integration
packages/memory/src/index.ts, packages/memory/__tests__/*
Registers the memory resolver with Mizan and tests grants, denials, precedence, role assignments, and fact-only adapter results.
Authorization and testing guidance
.agents/wisdom/security.md, .agents/wisdom/testing.md
Documents source safety rules and testing expectations for malformed outcomes, async assertions, integration flow, and cross-source denial precedence.

Repository maintenance

Layer / File(s) Summary
Repository hygiene and process guidance
.gitignore, .agents/wisdom/process.md
Ignores .scratch/ and documents review gates, merge handling, and commit conventions.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Caller
  participant PrincipalEvaluator
  participant Mizan
  participant MemoryAdapter
  Caller->>PrincipalEvaluator: decide(permission)
  PrincipalEvaluator->>Mizan: collect facts for principalId
  Mizan->>MemoryAdapter: resolve(context)
  MemoryAdapter-->>Mizan: facts outcome
  Mizan-->>PrincipalEvaluator: aggregated facts
  PrincipalEvaluator-->>Caller: allow or deny result
Loading

Possibly related issues

  • AmaraNecib/mizan issue 24 — Implements principal-bound memory-facts authorization with can, decide, precedence, adapter integration, and tests.
  • AmaraNecib/mizan issue 27 — Implements named source registration, normalized outcomes, contract validation, and fact-only adapter integration.

Possibly related PRs

  • AmaraNecib/mizan#37 — Establishes the core and memory package foundation that this PR replaces and extends with source resolution, decision evaluation, and adapter registration.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title is specific and accurately reflects the main change: the first decision flow with trusted principal, memory facts, and can/decide APIs.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feature/24-first-decision-path

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@AmaraNecib

Copy link
Copy Markdown
Owner Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jul 18, 2026

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 4

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/core/__tests__/decision.test.ts`:
- Around line 163-164: Update the async assertions for auth.can and auth.decide
to await the promise-aware matchers, replacing synchronous not.toThrow checks
with awaited rejects.not.toThrow assertions so both promise results and
rejection behavior are exercised.

In `@packages/core/src/index.ts`:
- Around line 253-266: Update the resolver outcome validation before status
evaluation to reject null or non-object payloads, and validate every entry in
outcome.facts as an object with a string permission and supported effect. Use
the existing source name and contract-violation TypeError pattern for these
failures, while preserving valid status and fact processing in the surrounding
resolver flow.
- Around line 223-225: Update the register method to detect when the sources map
already contains the given name and reject the registration instead of
overwriting the existing resolver. Preserve the current insertion behavior for
unique names, and use an appropriate error or rejection consistent with the
surrounding API.
- Around line 265-267: Update the authorization outcome aggregation around the
outcome.status check so an "unavailable" source cannot be discarded: immediately
reject the decision or return the existing structured denial when any source is
unavailable, while preserving the current facts collection for available "facts"
outcomes.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 02f603da-3a45-43b4-a645-522739ff19a8

📥 Commits

Reviewing files that changed from the base of the PR and between 0627873 and c3863f6.

📒 Files selected for processing (6)
  • .gitignore
  • packages/core/__tests__/decision.test.ts
  • packages/core/src/index.ts
  • packages/memory/__tests__/decision.test.ts
  • packages/memory/__tests__/smoke.test.ts
  • packages/memory/src/index.ts

Comment thread packages/core/__tests__/decision.test.ts Outdated
Comment thread packages/core/src/index.ts
Comment thread packages/core/src/index.ts
Comment thread packages/core/src/index.ts
…me validation, unavailable = fail closed, async test assertions
@AmaraNecib

Copy link
Copy Markdown
Owner Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jul 18, 2026

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@AmaraNecib

Copy link
Copy Markdown
Owner Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jul 18, 2026

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/core/__tests__/decision.test.ts`:
- Line 211: Await both async rejection assertions in
packages/core/__tests__/decision.test.ts at lines 211-211 and 223-223 by adding
await before expect(auth.can("x")).rejects.toThrow(...), matching the existing
fix at lines 163-164.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: f5a07a9f-4ef0-45ca-8ddf-a39634f2cdac

📥 Commits

Reviewing files that changed from the base of the PR and between c3863f6 and 369ab66.

📒 Files selected for processing (2)
  • packages/core/__tests__/decision.test.ts
  • packages/core/src/index.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/core/src/index.ts

Comment thread packages/core/__tests__/decision.test.ts Outdated
@AmaraNecib

Copy link
Copy Markdown
Owner Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jul 18, 2026

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@AmaraNecib

Copy link
Copy Markdown
Owner Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jul 18, 2026

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@AmaraNecib

Copy link
Copy Markdown
Owner Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jul 18, 2026

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai

coderabbitai Bot commented Jul 18, 2026

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In @.agents/wisdom/process.md:
- Around line 28-30: Add a language identifier to the fenced code block in the
issue template example, using text (or another appropriate language) while
preserving the example content.

In @.agents/wisdom/testing.md:
- Around line 19-25: Update the malformed-output test guidance in the relevant
testing specification so an empty facts array is treated as valid. Split
“missing/empty facts array” into separate cases: test missing or non-array facts
as malformed, and add a separate test confirming facts: [] is accepted by
SourceOutcome.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: cf423387-a722-472f-a71d-55cd349b49c3

📥 Commits

Reviewing files that changed from the base of the PR and between 068932a and 4889c83.

📒 Files selected for processing (3)
  • .agents/wisdom/process.md
  • .agents/wisdom/security.md
  • .agents/wisdom/testing.md

Comment thread .agents/wisdom/process.md Outdated
Comment thread .agents/wisdom/testing.md
@AmaraNecib

Copy link
Copy Markdown
Owner Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jul 18, 2026

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@AmaraNecib

Copy link
Copy Markdown
Owner Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jul 18, 2026

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@AmaraNecib

Copy link
Copy Markdown
Owner Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jul 18, 2026

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@AmaraNecib

Copy link
Copy Markdown
Owner Author

@coderabbitai resume

@coderabbitai

coderabbitai Bot commented Jul 18, 2026

Copy link
Copy Markdown
✅ Action performed

Reviews resumed.

@AmaraNecib

Copy link
Copy Markdown
Owner Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jul 18, 2026

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant