Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 351551af0f | |||
| 2dc478f5a3 |
Generated
+6
-6
@@ -1,15 +1,15 @@
|
||||
{
|
||||
"name": "@arbiter/evidence-dsl",
|
||||
"version": "1.1.0",
|
||||
"version": "1.4.0",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "@arbiter/evidence-dsl",
|
||||
"version": "1.1.0",
|
||||
"version": "1.4.0",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"@arbiter/core": "^1.0.2"
|
||||
"@arbiter/core": "^1.0.3"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@rigor/core": "^3.1.0",
|
||||
@@ -17,9 +17,9 @@
|
||||
}
|
||||
},
|
||||
"node_modules/@arbiter/core": {
|
||||
"version": "1.0.2",
|
||||
"resolved": "https://hub.kl1.tenere.ai/api/packages/Arbiter/npm/%40arbiter%2Fcore/-/1.0.2/core-1.0.2.tgz",
|
||||
"integrity": "sha512-N1duiHy1Rlsxqpvu8uPf4tMaLOQ2tNXvGs53jLkRcIAYqafIAMvcf0BPS2iE2xVKNsqY92+F05bZZEAO5jnbyQ==",
|
||||
"version": "1.0.3",
|
||||
"resolved": "https://hub.kl1.tenere.ai/api/packages/Arbiter/npm/%40arbiter%2Fcore/-/1.0.3/core-1.0.3.tgz",
|
||||
"integrity": "sha512-MCXxyeWBoYjEJMrdO8N8q9uEdX7JgDvwRH39D+8x65zFz+JCNWIQ8H4DgsP2rgF+yzv/cdVH4BX9PfFt6i0ftQ==",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"@tenere/pltc-core": "^0.6.3",
|
||||
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@arbiter/evidence-dsl",
|
||||
"version": "1.2.0",
|
||||
"version": "1.4.0",
|
||||
"description": "Evidence DSL v2 compiler: translates the natural Evidence DSL (ADR-000) into @arbiter/core relation configurations.",
|
||||
"license": "ISC",
|
||||
"type": "module",
|
||||
@@ -24,7 +24,7 @@
|
||||
"generate:parser": "node scripts/generate-parser.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"@arbiter/core": "^1.0.2"
|
||||
"@arbiter/core": "^1.0.3"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@rigor/core": "^3.1.0",
|
||||
|
||||
@@ -155,6 +155,7 @@ export class RuleGenerator {
|
||||
for (const step of rule.steps) {
|
||||
if (typeof step === 'string') targetSet.add(step);
|
||||
else if (step && typeof step.relation === 'string') targetSet.add(step.relation);
|
||||
else if (step && step.rule) collect(step.rule, targetSet);
|
||||
}
|
||||
}
|
||||
if (rule.type === 'relational_comparator') {
|
||||
@@ -1054,6 +1055,70 @@ export class RuleGenerator {
|
||||
if (out.left?.rule) out.left = { ...out.left, rule: this._resolveRule(out.left.rule, stack) };
|
||||
if (out.right?.rule) out.right = { ...out.right, rule: this._resolveRule(out.right.rule, stack) };
|
||||
}
|
||||
// Chain steps may reference a derived evidence; expand those steps
|
||||
// (direct evidence → underlying relation, chain evidence → spliced steps).
|
||||
if (rule.type === 'chain' && Array.isArray(out.steps)) {
|
||||
out.steps = this._expandChainSteps(out.steps, stack);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Expand chain steps that reference a derived evidence:
|
||||
* - direct evidence → rename the step to the underlying relation
|
||||
* (member_of(user,*g){ group_read(g,doc) } where group_read = can_view
|
||||
* becomes step 'can_view');
|
||||
* - chain evidence → splice its steps into this chain (flattening)
|
||||
* (a step that is itself a sub-path becomes its steps, preserving the
|
||||
* linear source→…→object traversal);
|
||||
* - logical / defeasible / comparator evidence → only expressible as a
|
||||
* FINAL condition-gated step (the object is known, so the engine can
|
||||
* verify the condition at (intermediate, object) instead of traversing
|
||||
* an edge). Emitted as a `{ rule: <config> }` step the ChainRule
|
||||
* evaluates as a condition hop. Non-final such steps are a compile
|
||||
* error: a condition cannot discover intermediate nodes.
|
||||
*/
|
||||
_expandChainSteps(steps, stack) {
|
||||
const out = [];
|
||||
for (let idx = 0; idx < steps.length; idx++) {
|
||||
const step = steps[idx];
|
||||
const isLast = idx === steps.length - 1;
|
||||
const stepName = typeof step === 'string' ? step : step.relation;
|
||||
if (stepName && this.evidenceNames.has(stepName)) {
|
||||
if (stack.has(stepName)) {
|
||||
this.errors.push(`Cyclic evidence reference involving '${stepName}'. Evidence composition must be acyclic.`);
|
||||
out.push(step);
|
||||
continue;
|
||||
}
|
||||
const referencedConfig = this.generatedRules.get(stepName);
|
||||
if (referencedConfig) {
|
||||
const refStack = new Set(stack);
|
||||
refStack.add(stepName);
|
||||
const resolved = this._resolveRule(referencedConfig, refStack);
|
||||
if (resolved.type === 'direct' && resolved.relation && resolved.relation !== stepName) {
|
||||
out.push(typeof step === 'string'
|
||||
? resolved.relation
|
||||
: { ...step, relation: resolved.relation });
|
||||
continue;
|
||||
}
|
||||
if (resolved.type === 'chain' && Array.isArray(resolved.steps)) {
|
||||
out.push(...this._expandChainSteps(resolved.steps, refStack));
|
||||
continue;
|
||||
}
|
||||
if (isLast) {
|
||||
// Condition-gated final hop: inline the evidence's config as a
|
||||
// rule step the engine evaluates at (intermediate, object).
|
||||
out.push({ rule: this._deepCloneRule(resolved), conditionStep: true });
|
||||
continue;
|
||||
}
|
||||
this.errors.push(`Chain step '${stepName}' references an evidence with type '${resolved.type || 'logical'}'. ` +
|
||||
'Only the final chain step may reference a defeasible/logical evidence (a condition-gated hop); intermediate steps must be edge traversals.');
|
||||
out.push(step);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
out.push(step);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
|
||||
@@ -103,7 +103,11 @@ export class DSLRuntime {
|
||||
if (rule.computedRelation) deps.add(rule.computedRelation);
|
||||
}
|
||||
if (rule.type === 'chain' && Array.isArray(rule.steps)) {
|
||||
for (const s of rule.steps) deps.add(typeof s === 'string' ? s : s.relation);
|
||||
for (const s of rule.steps) {
|
||||
if (typeof s === 'string') deps.add(s);
|
||||
else if (s && s.relation) deps.add(s.relation);
|
||||
else if (s && s.rule) collect(s.rule);
|
||||
}
|
||||
}
|
||||
if (rule.type === 'parent' && rule.parentRelation) deps.add(rule.parentRelation);
|
||||
if (rule.type === 'multi_hop' && rule.relation) deps.add(rule.relation);
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
/**
|
||||
* tests/ChainConditionStep.test.js — a chain whose FINAL (object-side) hop
|
||||
* references a defeasible/logical evidence. The compiler lowers it to a
|
||||
* condition step: `{ rule: <config>, conditionStep: true }`, which the engine
|
||||
* verifies at (intermediate, object) rather than traversing an edge.
|
||||
*
|
||||
* Only the final step may be a condition (the object is known); an
|
||||
* intermediate condition cannot discover nodes and is a compile error.
|
||||
*/
|
||||
import { describe, it } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { Arbiter } from '@arbiter/core';
|
||||
import { DSLCompiler } from '../src/DSLCompiler.js';
|
||||
|
||||
const DEFS = `
|
||||
definition Employee { id: string }
|
||||
definition Group { id: string }
|
||||
definition Doc { id: string }
|
||||
fact member_of(user: Employee, group: Group)
|
||||
fact can_view(group: Group, doc: Doc)
|
||||
fact banned(group: Group)
|
||||
fact can_edit(group: Group, doc: Doc)
|
||||
`;
|
||||
|
||||
function compile(dsl, name = 'chain-cond') {
|
||||
const arb = new Arbiter();
|
||||
const compiler = new DSLCompiler(arb);
|
||||
const result = compiler.compile(dsl, name);
|
||||
return { arb, result };
|
||||
}
|
||||
|
||||
describe('Chain condition step (logical evidence as final hop)', () => {
|
||||
it('lowers a defeasible final step to a condition step and grants', () => {
|
||||
const { arb, result } = compile(`
|
||||
${DEFS}
|
||||
evidence gated(group: Group, doc: Doc) { WHEN can_view(group, doc) UNLESS banned(group) }
|
||||
evidence can_via(user: Employee, doc: Doc) { member_of(user, *g) { gated(g, doc) } }
|
||||
`);
|
||||
assert.ok(result.success, JSON.stringify(result.errors));
|
||||
const steps = arb.relationConfigs.get('can_via').steps;
|
||||
assert.equal(steps[0], 'member_of');
|
||||
assert.equal(steps[1].conditionStep, true);
|
||||
assert.equal(steps[1].rule.type, 'logical');
|
||||
// transitive dependency collection through the condition step
|
||||
assert.deepEqual(arb.relationConfigs.get('can_via').dependsOn, ['member_of', 'can_view', 'banned']);
|
||||
|
||||
arb.addNode('u:1', 'Employee'); arb.addNode('g:1', 'Group'); arb.addNode('doc:9', 'Doc');
|
||||
arb.addRelation('u:1', 'member_of', 'g:1', { possibility: 1.0 });
|
||||
arb.addRelation('g:1', 'can_view', 'doc:9', { possibility: 0.7 });
|
||||
assert.equal(arb.check('u:1', 'can_via', 'doc:9').possibility, 0.7);
|
||||
|
||||
// banning the intermediate defeats the condition hop
|
||||
arb.addRelation('g:1', 'banned', 'g:1', { possibility: 1.0 });
|
||||
assert.equal(arb.check('u:1', 'can_via', 'doc:9').possibility, 0);
|
||||
});
|
||||
|
||||
it('supports ALWAYS/NEVER evidence as a condition step', () => {
|
||||
const { arb, result } = compile(`
|
||||
${DEFS}
|
||||
evidence gated(group: Group, doc: Doc) { ALWAYS can_edit(group, doc) }
|
||||
evidence can_via(user: Employee, doc: Doc) { member_of(user, *g) { gated(g, doc) } }
|
||||
`);
|
||||
assert.ok(result.success, JSON.stringify(result.errors));
|
||||
const steps = arb.relationConfigs.get('can_via').steps;
|
||||
assert.equal(steps[1].conditionStep, true);
|
||||
arb.addNode('u:1', 'Employee'); arb.addNode('g:1', 'Group'); arb.addNode('doc:9', 'Doc');
|
||||
arb.addRelation('u:1', 'member_of', 'g:1', { possibility: 1.0 });
|
||||
arb.addRelation('g:1', 'can_edit', 'doc:9', { possibility: 0.6 });
|
||||
assert.equal(arb.check('u:1', 'can_via', 'doc:9').possibility, 0.6);
|
||||
arb.removeRelation('g:1', 'can_edit', 'doc:9');
|
||||
assert.equal(arb.check('u:1', 'can_via', 'doc:9').possibility, 0);
|
||||
});
|
||||
|
||||
it('keeps the condition evidence checkable in its own right', () => {
|
||||
const { arb, result } = compile(`
|
||||
${DEFS}
|
||||
evidence gated(group: Group, doc: Doc) { WHEN can_view(group, doc) UNLESS banned(group) }
|
||||
evidence can_via(user: Employee, doc: Doc) { member_of(user, *g) { gated(g, doc) } }
|
||||
`);
|
||||
assert.ok(result.success, JSON.stringify(result.errors));
|
||||
arb.addNode('g:1', 'Group'); arb.addNode('doc:9', 'Doc');
|
||||
arb.addRelation('g:1', 'can_view', 'doc:9', { possibility: 0.8 });
|
||||
assert.equal(arb.check('g:1', 'gated', 'doc:9').possibility, 0.8);
|
||||
});
|
||||
|
||||
it('parallel intermediates aggregate through the condition step', () => {
|
||||
const { arb, result } = compile(`
|
||||
${DEFS}
|
||||
evidence gated(group: Group, doc: Doc) { can_view(group, doc) }
|
||||
evidence can_via(user: Employee, doc: Doc) { member_of(user, *g) { gated(g, doc) } }
|
||||
`);
|
||||
assert.ok(result.success, JSON.stringify(result.errors));
|
||||
arb.addNode('u:1', 'Employee'); arb.addNode('g:1', 'Group'); arb.addNode('g2:2', 'Group'); arb.addNode('doc:9', 'Doc');
|
||||
arb.addRelation('u:1', 'member_of', 'g:1', { possibility: 0.5 });
|
||||
arb.addRelation('g:1', 'can_view', 'doc:9', { possibility: 0.7 });
|
||||
arb.addRelation('u:1', 'member_of', 'g2:2', { possibility: 1.0 });
|
||||
arb.addRelation('g2:2', 'can_view', 'doc:9', { possibility: 0.8 });
|
||||
// max over paths: min(0.5,0.7)=0.5, min(1.0,0.8)=0.8 -> 0.8
|
||||
assert.equal(arb.check('u:1', 'can_via', 'doc:9').possibility, 0.8);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,127 @@
|
||||
/**
|
||||
* tests/ChainStepComposition.test.js — evidence composition inside CHAIN
|
||||
* steps. A chain step that references a derived evidence is expanded at
|
||||
* compile time:
|
||||
* - a DIRECT evidence step → renamed to its underlying relation
|
||||
* (member_of(user,*g){ group_read(g,doc) } where group_read = can_view
|
||||
* becomes step 'can_view');
|
||||
* - a CHAIN evidence step → its steps are spliced into the parent chain
|
||||
* (a sub-path flattens into the linear source→…→object traversal);
|
||||
* - a DEFEASIBLE / LOGICAL / COMPARATOR evidence step is not an edge
|
||||
* traversal and is rejected at compile time;
|
||||
* - cycles and self-references through chain steps are compile errors.
|
||||
*/
|
||||
import { describe, it } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { Arbiter } from '@arbiter/core';
|
||||
import { DSLCompiler } from '../src/DSLCompiler.js';
|
||||
|
||||
const DEFS = `
|
||||
definition Employee { id: string }
|
||||
definition Group { id: string }
|
||||
definition Doc { id: string }
|
||||
fact member_of(user: Employee, group: Group)
|
||||
fact group_has(group: Group, sub: Group)
|
||||
fact can_view(group: Group, doc: Doc)
|
||||
fact can_access(group: Group, doc: Doc)
|
||||
fact banned(group: Group)
|
||||
`;
|
||||
|
||||
function compile(dsl, name = 'chain-compose') {
|
||||
const arb = new Arbiter();
|
||||
const compiler = new DSLCompiler(arb);
|
||||
const result = compiler.compile(dsl, name);
|
||||
return { arb, result };
|
||||
}
|
||||
|
||||
describe('Chain step composition', () => {
|
||||
it('renames a direct-evidence chain step to its underlying relation', () => {
|
||||
const { arb, result } = compile(`
|
||||
${DEFS}
|
||||
evidence group_read(group: Group, doc: Doc) { can_view(group, doc) }
|
||||
evidence can_via(user: Employee, doc: Doc) { member_of(user, *g) { group_read(g, doc) } }
|
||||
`);
|
||||
assert.ok(result.success, JSON.stringify(result.errors));
|
||||
// step 'group_read' → 'can_view'
|
||||
assert.deepEqual(arb.relationConfigs.get('can_via').steps, ['member_of', 'can_view']);
|
||||
arb.addNode('u:1', 'Employee'); arb.addNode('g:1', 'Group'); arb.addNode('doc:9', 'Doc');
|
||||
arb.addRelation('u:1', 'member_of', 'g:1', { possibility: 1.0 });
|
||||
arb.addRelation('g:1', 'can_view', 'doc:9', { possibility: 0.7 });
|
||||
assert.equal(arb.check('u:1', 'can_via', 'doc:9').possibility, 0.7);
|
||||
});
|
||||
|
||||
it('splices a chain-evidence step into the parent chain', () => {
|
||||
const { arb, result } = compile(`
|
||||
${DEFS}
|
||||
evidence group_enter(group: Group, doc: Doc) { group_has(group, *s) { can_access(s, doc) } }
|
||||
evidence can_deep(user: Employee, doc: Doc) { member_of(user, *g) { group_enter(g, doc) } }
|
||||
`);
|
||||
assert.ok(result.success, JSON.stringify(result.errors));
|
||||
// step 'group_enter' → its steps [group_has, can_access]
|
||||
assert.deepEqual(arb.relationConfigs.get('can_deep').steps, ['member_of', 'group_has', 'can_access']);
|
||||
arb.addNode('u:1', 'Employee'); arb.addNode('g:1', 'Group'); arb.addNode('g2:2', 'Group'); arb.addNode('doc:9', 'Doc');
|
||||
arb.addRelation('u:1', 'member_of', 'g:1', { possibility: 1.0 });
|
||||
arb.addRelation('g:1', 'group_has', 'g2:2', { possibility: 0.9 });
|
||||
arb.addRelation('g2:2', 'can_access', 'doc:9', { possibility: 0.8 });
|
||||
assert.equal(arb.check('u:1', 'can_deep', 'doc:9').possibility, 0.8);
|
||||
});
|
||||
|
||||
it('expands a chain step whose direct evidence is itself composed', () => {
|
||||
const { arb, result } = compile(`
|
||||
${DEFS}
|
||||
evidence group_view(group: Group, doc: Doc) { can_view(group, doc) }
|
||||
evidence group_read(group: Group, doc: Doc) { group_view(group, doc) }
|
||||
evidence can_via(user: Employee, doc: Doc) { member_of(user, *g) { group_read(g, doc) } }
|
||||
`);
|
||||
assert.ok(result.success, JSON.stringify(result.errors));
|
||||
assert.deepEqual(arb.relationConfigs.get('can_via').steps, ['member_of', 'can_view']);
|
||||
arb.addNode('u:1', 'Employee'); arb.addNode('g:1', 'Group'); arb.addNode('doc:9', 'Doc');
|
||||
arb.addRelation('u:1', 'member_of', 'g:1', { possibility: 1.0 });
|
||||
arb.addRelation('g:1', 'can_view', 'doc:9', { possibility: 0.6 });
|
||||
assert.equal(arb.check('u:1', 'can_via', 'doc:9').possibility, 0.6);
|
||||
});
|
||||
|
||||
it('lowers a logical evidence FINAL step to a condition step', () => {
|
||||
const { arb, result } = compile(`
|
||||
${DEFS}
|
||||
evidence gated(group: Group, doc: Doc) { WHEN can_view(group, doc) UNLESS banned(group) }
|
||||
evidence can_via(user: Employee, doc: Doc) { member_of(user, *g) { gated(g, doc) } }
|
||||
`);
|
||||
assert.ok(result.success, JSON.stringify(result.errors));
|
||||
// final-step logical evidence → condition step (verified at the object)
|
||||
const steps = arb.relationConfigs.get('can_via').steps;
|
||||
assert.equal(steps[0], 'member_of');
|
||||
assert.equal(steps[1].conditionStep, true);
|
||||
assert.equal(steps[1].rule.type, 'logical');
|
||||
});
|
||||
|
||||
it('rejects a mutual cycle through chain steps', () => {
|
||||
const { result } = compile(`
|
||||
${DEFS}
|
||||
evidence cyc_a(group: Group, doc: Doc) { group_has(group, *g) { cyc_b(g, doc) } }
|
||||
evidence cyc_b(group: Group, doc: Doc) { cyc_a(group, doc) }
|
||||
`);
|
||||
assert.equal(result.success, false);
|
||||
assert.ok(result.errors.some(e => /[Cc]yclic/.test(e)), JSON.stringify(result.errors));
|
||||
});
|
||||
|
||||
it('rejects a self-reference through its own chain step', () => {
|
||||
const { result } = compile(`
|
||||
${DEFS}
|
||||
evidence cyc_c(group: Group, doc: Doc) { group_has(group, *g) { cyc_c(g, doc) } }
|
||||
`);
|
||||
assert.equal(result.success, false);
|
||||
assert.ok(result.errors.some(e => /[Cc]yclic/.test(e)), JSON.stringify(result.errors));
|
||||
});
|
||||
|
||||
it('re-derives transitive dependencies through expanded chain steps', () => {
|
||||
const { arb, result } = compile(`
|
||||
${DEFS}
|
||||
evidence group_read(group: Group, doc: Doc) { can_view(group, doc) }
|
||||
evidence can_via(user: Employee, doc: Doc) { member_of(user, *g) { group_read(g, doc) } }
|
||||
`);
|
||||
assert.ok(result.success, JSON.stringify(result.errors));
|
||||
// dependsOn reflects the expanded step, not the evidence reference
|
||||
assert.deepEqual(arb.relationConfigs.get('can_via').dependsOn, ['member_of', 'can_view']);
|
||||
});
|
||||
});
|
||||
@@ -188,4 +188,21 @@ describe('DSLRuntime', () => {
|
||||
assert.equal(denied.possibility, 0);
|
||||
assert.equal(denied.reason, 'defeated_by_unless');
|
||||
});
|
||||
|
||||
it('derives transitive required facts through a condition-step chain', () => {
|
||||
const dsl = `
|
||||
definition Employee { id: string }
|
||||
definition Group { id: string }
|
||||
definition Doc { id: string }
|
||||
fact *member_of(user: Employee, group: Group)
|
||||
fact *can_view(group: Group, doc: Doc)
|
||||
fact *banned(group: Group)
|
||||
evidence gated(group: Group, doc: Doc) { WHEN can_view(group, doc) UNLESS banned(group) }
|
||||
evidence can_via(user: Employee, doc: Doc) { member_of(user, *g) { gated(g, doc) } }
|
||||
`;
|
||||
const rt = new DSLRuntime(new Arbiter()).compile(dsl, 'rt-cond');
|
||||
// The condition step's facts (can_view, banned) reach through to the
|
||||
// evidence's requirements, alongside the edge-traversal fact.
|
||||
assert.deepEqual(rt.requiredFacts('can_via'), ['member_of', 'can_view', 'banned']);
|
||||
});
|
||||
});
|
||||
|
||||
+14
-13
@@ -45,6 +45,7 @@ const DSL_SUPPORT = `
|
||||
fact isMember(user: any, group: any)
|
||||
fact isFriend(user: any, friend: any)
|
||||
fact similar(a: any, b: any)
|
||||
fact reachable(user: any, doc: any)
|
||||
fact parentOf(user: any, parent: any)
|
||||
fact isEditable(doc: any)
|
||||
fact isPublic(doc: any)
|
||||
@@ -159,7 +160,7 @@ describe('Evidence Rules', () => {
|
||||
{
|
||||
input: `evidence canRead(user: Employee, doc: Document) {
|
||||
isMember(user, *group) {
|
||||
canRead(group, doc)
|
||||
reachable(group, doc)
|
||||
}
|
||||
}`,
|
||||
description: 'Basic pattern matching with wildcard'
|
||||
@@ -167,7 +168,7 @@ describe('Evidence Rules', () => {
|
||||
{
|
||||
input: `evidence canRead(user: Employee, doc: Document) {
|
||||
isMember(user, *group) {
|
||||
canRead(group, doc)
|
||||
reachable(group, doc)
|
||||
} limit 5
|
||||
}`,
|
||||
description: 'Pattern matching with limit'
|
||||
@@ -175,7 +176,7 @@ describe('Evidence Rules', () => {
|
||||
{
|
||||
input: `evidence canRead(user: Employee, doc: Document) {
|
||||
similar(doc, *similar) |similarity| {
|
||||
canRead(user, similar)
|
||||
reachable(user, similar)
|
||||
} with similarity > 0.7
|
||||
}`,
|
||||
description: 'Pattern matching with binding and condition'
|
||||
@@ -183,7 +184,7 @@ describe('Evidence Rules', () => {
|
||||
{
|
||||
input: `evidence canRead(user: Employee, doc: Document) {
|
||||
similar(doc, *similar) |similarity| {
|
||||
canRead(user, similar)
|
||||
reachable(user, similar)
|
||||
} limit 5 with similarity > 0.7
|
||||
}`,
|
||||
description: 'Pattern matching with binding, condition, and limit'
|
||||
@@ -192,7 +193,7 @@ describe('Evidence Rules', () => {
|
||||
input: `evidence canRead(user: Employee, doc: Document) {
|
||||
isMember(user, *group) {
|
||||
isMember(group, *parentGroup) {
|
||||
canRead(parentGroup, doc)
|
||||
reachable(parentGroup, doc)
|
||||
} limit 2
|
||||
} limit 3
|
||||
}`,
|
||||
@@ -202,7 +203,7 @@ describe('Evidence Rules', () => {
|
||||
input: `evidence canRead(user: Employee, doc: Document) {
|
||||
isFriend(user, *friend) {
|
||||
isMember(friend, *group) {
|
||||
canRead(group, doc)
|
||||
reachable(group, doc)
|
||||
} limit 1
|
||||
} limit 5
|
||||
}`,
|
||||
@@ -291,15 +292,15 @@ describe('Evidence Rules', () => {
|
||||
owns(user, doc)
|
||||
|
||||
isMember(user, *group) {
|
||||
canRead(group, doc)
|
||||
reachable(group, doc)
|
||||
} limit 5
|
||||
|
||||
parentOf(user, *parent) {
|
||||
canRead(parent, doc)
|
||||
reachable(parent, doc)
|
||||
} limit 3
|
||||
|
||||
similar(doc, *similar) |similarity| {
|
||||
canRead(user, similar)
|
||||
reachable(user, similar)
|
||||
} limit 5 with similarity > 0.7
|
||||
|
||||
WHEN hasRole(user, 'admin') UNLESS isSuspended(user)
|
||||
@@ -337,11 +338,11 @@ describe('Evidence Rules', () => {
|
||||
owns(user, doc)
|
||||
|
||||
isMember(user, *group) {
|
||||
canModify(group, doc)
|
||||
reachable(group, doc)
|
||||
} limit 3
|
||||
|
||||
similar(doc, *similar) |similarity| {
|
||||
canModify(user, similar)
|
||||
reachable(user, similar)
|
||||
isEditable(similar)
|
||||
} limit 2 with similarity > 0.8
|
||||
|
||||
@@ -385,7 +386,7 @@ describe('Evidence Rules', () => {
|
||||
{
|
||||
input: `evidence canRead(user: Employee, doc: Document) {
|
||||
isMember(user, *group) {
|
||||
canRead(group, doc)
|
||||
reachable(group, doc)
|
||||
} with
|
||||
}`,
|
||||
description: 'Incomplete with clause should fail'
|
||||
@@ -393,7 +394,7 @@ describe('Evidence Rules', () => {
|
||||
{
|
||||
input: `evidence canRead(user: Employee, doc: Document) {
|
||||
isMember(user, *group) {
|
||||
canRead(group, doc)
|
||||
reachable(group, doc)
|
||||
} limit
|
||||
}`,
|
||||
description: 'Incomplete limit should fail'
|
||||
|
||||
+16
-12
@@ -105,6 +105,7 @@ describe('Integration Tests', () => {
|
||||
fact hasAccess(user: Employee, resource: Resource, level: string) CACHE lazy
|
||||
fact isColleague(user: any, colleague: any) symmetrical CACHE lazy limit 50
|
||||
fact isParentOf(parent: Employee, child: Employee) transitive CACHE eager limit 3
|
||||
fact reachable(user: any, doc: any) CACHE lazy
|
||||
fact hasClearance(user: Employee, level: string) CACHE eager
|
||||
fact parentOf(user: any, parent: any) CACHE eager
|
||||
fact similar(a: any, b: any) CACHE lazy
|
||||
@@ -119,15 +120,15 @@ describe('Integration Tests', () => {
|
||||
owns(user, doc)
|
||||
|
||||
isMember(user, *group) {
|
||||
canRead(group, doc)
|
||||
reachable(group, doc)
|
||||
} limit 5
|
||||
|
||||
parentOf(user, *parent) {
|
||||
canRead(parent, doc)
|
||||
reachable(parent, doc)
|
||||
} limit 3
|
||||
|
||||
similar(doc, *similar) |similarity| {
|
||||
canRead(user, similar)
|
||||
reachable(user, similar)
|
||||
} limit 5 with similarity > 0.7
|
||||
|
||||
WHEN hasRole(user, 'admin') UNLESS isSuspended(user)
|
||||
@@ -137,7 +138,7 @@ describe('Integration Tests', () => {
|
||||
owns(user, doc)
|
||||
|
||||
isMember(user, *group) {
|
||||
canWrite(group, doc)
|
||||
reachable(group, doc)
|
||||
} limit 3
|
||||
|
||||
WHEN hasRole(user, 'admin') UNLESS isSuspended(user)
|
||||
@@ -330,6 +331,7 @@ describe('Integration Tests', () => {
|
||||
|
||||
fact isMember(user: any, org: any) transitive CACHE lazy limit 5
|
||||
fact isParentOf(parent: Organization, child: Organization) transitive CACHE eager limit 3
|
||||
fact reachable(user: any, doc: any) CACHE lazy
|
||||
fact hasRole(user: Employee, role: string) CACHE eager
|
||||
fact hasClearance(user: Employee, level: string) CACHE eager
|
||||
fact isSuspended(user: any) CACHE lazy
|
||||
@@ -339,7 +341,7 @@ describe('Integration Tests', () => {
|
||||
isMember(user, org)
|
||||
|
||||
isParentOf(org, *parentOrg) {
|
||||
canAccessOrg(user, parentOrg)
|
||||
reachable(user, parentOrg)
|
||||
} limit 3
|
||||
|
||||
WHEN hasRole(user, 'admin') UNLESS isSuspended(user)
|
||||
@@ -347,11 +349,11 @@ describe('Integration Tests', () => {
|
||||
|
||||
evidence canAccessResource(user: Employee, resource: Resource) {
|
||||
isMember(user, *org) {
|
||||
canAccessResource(org, resource)
|
||||
reachable(org, resource)
|
||||
} limit 5
|
||||
|
||||
parentOf(user, *parent) {
|
||||
canAccessResource(parent, resource)
|
||||
reachable(parent, resource)
|
||||
} limit 2
|
||||
}
|
||||
`;
|
||||
@@ -379,6 +381,7 @@ describe('Integration Tests', () => {
|
||||
fact hasInterest(user: any, interest: string) CACHE lazy
|
||||
fact hasTag(doc: any, tag: string) CACHE lazy
|
||||
fact owns(user: any, doc: any) CACHE eager
|
||||
fact reachable(user: any, doc: any) CACHE lazy
|
||||
fact similar(a: any, b: any) CACHE lazy
|
||||
fact isPublic(doc: any) CACHE eager
|
||||
fact hasInterests(user: any) CACHE lazy
|
||||
@@ -390,12 +393,12 @@ describe('Integration Tests', () => {
|
||||
owns(user, doc)
|
||||
|
||||
similar(doc, *similar) |similarity| {
|
||||
canRead(user, similar)
|
||||
reachable(user, similar)
|
||||
isPublic(similar)
|
||||
} limit 10 with similarity > 0.7
|
||||
|
||||
isFriend(user, *friend) {
|
||||
canRead(friend, doc)
|
||||
reachable(friend, doc)
|
||||
} limit 5
|
||||
|
||||
fusion majority {
|
||||
@@ -406,7 +409,7 @@ describe('Integration Tests', () => {
|
||||
|
||||
evidence canRecommend(user: Employee, doc: Document) {
|
||||
similar(user, *similarUser) |similarity| {
|
||||
canRead(similarUser, doc)
|
||||
reachable(similarUser, doc)
|
||||
} limit 20 with similarity > 0.8
|
||||
|
||||
fusion average {
|
||||
@@ -556,13 +559,14 @@ describe('Integration Tests', () => {
|
||||
fact isFriend(user: any, friend: any) symmetrical CACHE eager limit 50
|
||||
fact hasPermission(user: Employee, resource: Resource, action: string) CACHE eager
|
||||
fact owns(user: Employee, resource: Resource) CACHE eager
|
||||
fact reachable(user: any, doc: any) CACHE lazy
|
||||
|
||||
// Optimized evidence rules
|
||||
evidence canAccess(user: Employee, resource: Resource) {
|
||||
owns(user, resource)
|
||||
|
||||
isMember(user, *group) {
|
||||
canAccess(group, resource)
|
||||
reachable(group, resource)
|
||||
} limit 3
|
||||
|
||||
WHEN hasPermission(user, resource, 'read')
|
||||
@@ -572,7 +576,7 @@ describe('Integration Tests', () => {
|
||||
owns(user, resource)
|
||||
|
||||
isMember(user, *group) {
|
||||
canModify(group, resource)
|
||||
reachable(group, resource)
|
||||
} limit 2
|
||||
|
||||
WHEN hasPermission(user, resource, 'write')
|
||||
|
||||
@@ -32,7 +32,9 @@ const FACTS = `
|
||||
fact can_access(group: Group, doc: Doc)
|
||||
fact owner(group: Group, doc: Doc)
|
||||
fact granted(user: Employee, doc: Doc)
|
||||
fact group_perm(group: Group, doc: Doc)
|
||||
fact banned(user: Employee)
|
||||
fact group_banned(group: Group)
|
||||
fact mfa(user: Employee)
|
||||
`;
|
||||
|
||||
@@ -116,6 +118,30 @@ function buildProgram(kind, ps) {
|
||||
oracle = pOwn;
|
||||
break;
|
||||
}
|
||||
case 'chain_step_composition': {
|
||||
// group_read (a direct evidence) used as a CHAIN STEP inside can_via:
|
||||
// the step is expanded at compile time to the underlying can_view edge.
|
||||
const [pm, pv] = ps;
|
||||
evidence = `evidence group_read(group: Group, doc: Doc) { group_perm(group, doc) }
|
||||
evidence can_via(user: Employee, doc: Doc) { member_of(user, *g) { group_read(g, doc) } }`;
|
||||
edges.push({ src: 'u:1', relation: 'member_of', dst: 'g:1', possibility: pm });
|
||||
edges.push({ src: 'g:1', relation: 'group_perm', dst: 'doc:9', possibility: pv });
|
||||
oracle = Math.min(pm, pv);
|
||||
break;
|
||||
}
|
||||
case 'chain_condition_step': {
|
||||
// gated (a defeasible evidence) as the FINAL chain step → a condition
|
||||
// step: the engine verifies gated at (intermediate, object). The oracle
|
||||
// is the chain's min combined with the condition's base*(1-defeat).
|
||||
const [pm, pv, pb] = ps;
|
||||
evidence = `evidence gated(group: Group, doc: Doc) { WHEN group_perm(group, doc) UNLESS group_banned(group) }
|
||||
evidence can_via(user: Employee, doc: Doc) { member_of(user, *g) { gated(g, doc) } }`;
|
||||
edges.push({ src: 'u:1', relation: 'member_of', dst: 'g:1', possibility: pm });
|
||||
edges.push({ src: 'g:1', relation: 'group_perm', dst: 'doc:9', possibility: pv });
|
||||
edges.push({ src: 'g:1', relation: 'group_banned', dst: 'g:1', possibility: pb });
|
||||
oracle = Math.min(pm, pv * (1 - pb));
|
||||
break;
|
||||
}
|
||||
default:
|
||||
throw new Error(`unknown construct: ${kind}`);
|
||||
}
|
||||
@@ -151,7 +177,8 @@ function runCheck({ kind, ps }) {
|
||||
}
|
||||
|
||||
const CONSTRUCTS = ['direct', 'chain', 'tuple_to_userset', 'fusion_min', 'fusion_max',
|
||||
'when_unless', 'never_always', 'requires_when', 'composition'];
|
||||
'when_unless', 'never_always', 'requires_when', 'composition', 'chain_step_composition',
|
||||
'chain_condition_step'];
|
||||
|
||||
describe('DSL generative oracle parity (rigor)', () => {
|
||||
it('generated legal DSL compiles and every check matches the oracle', async () => {
|
||||
@@ -162,7 +189,7 @@ describe('DSL generative oracle parity (rigor)', () => {
|
||||
kind: rigor.gen.oneOf(CONSTRUCTS),
|
||||
// exactly two edge possibilities (direct uses only the first);
|
||||
// a shorter array would leave pB undefined and produce a NaN oracle
|
||||
ps: rigor.gen.tuple(rigor.gen.oneOf(P), rigor.gen.oneOf(P))
|
||||
ps: rigor.gen.tuple(rigor.gen.oneOf(P), rigor.gen.oneOf(P), rigor.gen.oneOf(P))
|
||||
})
|
||||
))
|
||||
],
|
||||
@@ -180,13 +207,14 @@ describe('DSL generative oracle parity (rigor)', () => {
|
||||
});
|
||||
|
||||
it('exhaustive deterministic sweep: every construct x every possibility value', () => {
|
||||
// Anti-vacuity complement to the campaign: sweep the full P × P grid per
|
||||
// construct without any RNG, so a construct the campaign skipped would
|
||||
// Anti-vacuity complement to the campaign: sweep the full P × P × P grid
|
||||
// per construct without any RNG, so a construct the campaign skipped would
|
||||
// still be caught here.
|
||||
for (const kind of CONSTRUCTS) {
|
||||
for (const a of P) {
|
||||
for (const b of P) {
|
||||
const ps = kind === 'direct' ? [a] : [a, b];
|
||||
for (const c of P) {
|
||||
const ps = kind === 'direct' ? [a] : [a, b, c];
|
||||
const { dsl, edges, oracle, relation } = buildProgram(kind, ps);
|
||||
const arbiter = new Arbiter();
|
||||
arbiter.addNode('u:1', 'Employee');
|
||||
@@ -203,5 +231,6 @@ describe('DSL generative oracle parity (rigor)', () => {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user