feat: sources wired as recency-gated injectables; transitive closure, NOT scoping, recompile-scope uninstall; targeted parse errors
CI / test (push) Successful in 20s
CI / publish (push) Has been skipped

BEHAVES AS transitive now emits bounded multi_hop configs (direct checks and
evidence references), fixing a silent no-op. NOT builds keep _subjectAsObject
scoping so unary predicates negate the right node, and value-typed evidence
objects gate by exact edge value. Recompiling a scope uninstalls its stale
relation configs (compileMultiple coexistence preserved). Sources become
injectable relations honored by requiredFacts with a within-X recency gate.
Duplicate definition fields and three common declaration mistakes (within on a
fact, two BEHAVES clauses, limit on a non-pattern body) now produce targeted
errors. Provider edges referencing unknown nodes are warned and dropped.
This commit is contained in:
John Dvorak
2026-08-03 20:27:13 -07:00
parent 4d498b07e8
commit 2a7f4c315b
10 changed files with 556 additions and 28 deletions
+30
View File
@@ -196,6 +196,36 @@ describe('DSL Compiler', () => {
assert.ok(invalidResult.errors.length > 0, 'Should have validation errors');
});
test('Rejects duplicate fields within a definition', () => {
const dupFieldDSL = `
definition Employee { id: string id: string }
`;
const result = compiler.validate(dupFieldDSL);
assert.ok(!result.success, 'Duplicate field should fail validation');
assert.match(result.errors[0], /Duplicate field 'Employee.id'/);
});
test('Hints at the real constraint for common declaration mistakes', () => {
const withinOnFact = compiler.compile(`
definition Employee { id: string? }
fact owns(user: Employee, doc: Employee) within 1h
`, 'err-within');
assert.match(withinOnFact.errors[0], /within.*only valid on `source`/);
const doubleBehaves = compiler.compile(`
definition Employee { id: string? }
fact rel(user: Employee, doc: Employee) BEHAVES AS transitive BEHAVES { ttl 1h }
`, 'err-behaves');
assert.match(doubleBehaves.errors[0], /only one `BEHAVES` clause/);
const limitAfterBody = compiler.compile(`
definition Employee { id: string? }
fact owns(user: Employee, doc: Employee)
evidence can_read(user: Employee, doc: Employee) { owns(user, doc) } limit 5
`, 'err-limit');
assert.match(limitAfterBody.errors[0], /`limit` is only valid on pattern/);
});
test('Rule generation', () => {
const dsl = `
definition Employee {
+119
View File
@@ -205,4 +205,123 @@ describe('DSLRuntime', () => {
// evidence's requirements, alongside the edge-traversal fact.
assert.deepEqual(rt.requiredFacts('can_via'), ['member_of', 'can_view', 'banned']);
});
it('negates a NOT predicate (1 - possibility)', async () => {
const dsl = `
definition Employee { id: string? }
fact banned(user: Employee)
evidence can_enter(user: Employee) { NOT banned(user) }
`;
// Absent predicate negates to allow.
const absent = new DSLRuntime(new Arbiter()).compile(dsl, 'rt-not-absent');
absent.addNode('u:1', 'Employee', {});
assert.equal((await absent.check('u:1', 'can_enter', 'u:1')).possibility, 1);
// Present predicate negates to its complement.
const present = new DSLRuntime(new Arbiter()).compile(dsl, 'rt-not-present');
present.addNode('u:1', 'Employee', {});
present.addRelation('u:1', 'banned', 'u:1', { possibility: 0.9 });
const denied = await present.check('u:1', 'can_enter', 'u:1');
assert.ok(Math.abs(denied.possibility - 0.1) < 1e-9, `expected 0.1, got ${denied.possibility}`);
// Nested inside an AND: the negated child still scopes to the subject.
const nested = new DSLRuntime(new Arbiter()).compile(`
definition Employee { id: string? }
definition Doc { id: string? }
fact owns(user: Employee, doc: Doc)
fact banned(user: Employee)
evidence can_open(user: Employee, doc: Doc) { owns(user, doc) NOT banned(user) }
`, 'rt-not-nested');
nested.addNode('u:1', 'Employee', {});
nested.addNode('doc:9', 'Doc', {});
nested.addRelation('u:1', 'owns', 'doc:9', { possibility: 0.8 });
nested.addRelation('u:1', 'banned', 'u:1', { possibility: 0.6 });
assert.equal((await nested.check('u:1', 'can_open', 'doc:9')).possibility, 0.4);
});
it('resolves BEHAVES AS transitive facts as bounded transitive closure', async () => {
const dsl = `
definition Employee { id: string? }
fact reports_to(user: Employee, boss: Employee) BEHAVES AS transitive
evidence can_see(user: Employee, doc: Employee) { reports_to(user, doc) }
`;
const rt = new DSLRuntime(new Arbiter()).compile(dsl, 'rt-transitive');
['e:1', 'e:2', 'e:3', 'e:4'].forEach(k => rt.addNode(k, 'Employee', {}));
rt.addRelation('e:1', 'reports_to', 'e:2', { possibility: 1.0 });
rt.addRelation('e:2', 'reports_to', 'e:3', { possibility: 0.9 });
rt.addRelation('e:3', 'reports_to', 'e:4', { possibility: 0.8 });
// Direct checks on the transitive fact follow multi-hop paths.
assert.equal((await rt.check('e:1', 'reports_to', 'e:3')).possibility, 0.9);
assert.equal((await rt.check('e:1', 'reports_to', 'e:4')).possibility, 0.8);
// Reverse direction does not grant.
assert.equal((await rt.check('e:2', 'reports_to', 'e:1')).possibility, 0);
// Evidence references inherit the closure.
assert.equal((await rt.check('e:1', 'can_see', 'e:4')).possibility, 0.8);
// Non-transitive facts stay direct-only.
const direct = new DSLRuntime(new Arbiter()).compile(`
definition Employee { id: string? }
fact knows(user: Employee, peer: Employee)
evidence can_ping(user: Employee, doc: Employee) { knows(user, doc) }
`, 'rt-nontransitive');
['a:1', 'a:2', 'a:3'].forEach(k => direct.addNode(k, 'Employee', {}));
direct.addRelation('a:1', 'knows', 'a:2', { possibility: 1.0 });
direct.addRelation('a:2', 'knows', 'a:3', { possibility: 1.0 });
assert.equal((await direct.check('a:1', 'can_ping', 'a:3')).possibility, 0);
});
it('bounds transitive closure depth by the fact limit', async () => {
const rt = new DSLRuntime(new Arbiter()).compile(`
definition Employee { id: string? }
fact reports_to(user: Employee, boss: Employee) BEHAVES AS transitive limit 2
evidence can_see(user: Employee, doc: Employee) { reports_to(user, doc) }
`, 'rt-transitive-limit');
['e:1', 'e:2', 'e:3', 'e:4'].forEach(k => rt.addNode(k, 'Employee', {}));
rt.addRelation('e:1', 'reports_to', 'e:2', { possibility: 1.0 });
rt.addRelation('e:2', 'reports_to', 'e:3', { possibility: 1.0 });
rt.addRelation('e:3', 'reports_to', 'e:4', { possibility: 1.0 });
assert.equal((await rt.check('e:1', 'can_see', 'e:3')).possibility, 1.0);
assert.equal((await rt.check('e:1', 'can_see', 'e:4')).possibility, 0);
});
it('retrieves sources through providers and gates them by within recency', async () => {
const rt = new DSLRuntime(new Arbiter()).compile(`
definition Employee { id: string? }
definition Doc { id: string? }
source *session(user: Employee) within 1h
fact *owns(user: Employee, doc: Doc)
evidence can_read(user: Employee, doc: Doc) { session(user) owns(user, doc) }
`, 'rt-source');
rt.addNode('u:1', 'Employee', {});
rt.addNode('doc:9', 'Doc', {});
assert.ok(rt.relations.has('session'));
assert.equal(rt.relations.get('session').kind, 'source');
assert.equal(rt.relations.get('session').withinMs, 3_600_000);
// A fresh session proof grants.
const fresh = await rt.check('u:1', 'can_read', 'doc:9', {
factProviders: { session: async () => ({ possibility: 1.0, value: Date.now() }), owns: async () => 0.9 }
});
assert.equal(fresh.possibility, 0.9);
assert.deepEqual(fresh.providedFacts, ['session', 'owns']);
// A stale proof (2h old, beyond the 1h window) denies as stale.
const stale = await rt.check('u:1', 'can_read', 'doc:9', {
factProviders: { session: async () => ({ possibility: 1.0, value: Date.now() - 2 * 3600_000 }), owns: async () => 0.9 }
});
assert.equal(stale.possibility, 0);
assert.deepEqual(stale.missingFacts, [{ relation: 'session', reason: 'stale' }]);
});
it('warns about and drops provider edges referencing unknown nodes', async () => {
const rt = new DSLRuntime(new Arbiter()).compile(`
definition Employee { id: string? }
definition Doc { id: string? }
fact *owns(user: Employee, doc: Doc)
evidence can_read(user: Employee, doc: Doc) { owns(user, doc) }
`, 'rt-ghost');
rt.addNode('u:1', 'Employee', {});
rt.addNode('doc:9', 'Doc', {});
const res = await rt.check('u:1', 'can_read', 'doc:9', {
factProviders: { owns: async () => [{ src: 'ghost:1', dst: 'doc:9', possibility: 0.9 }] }
});
assert.equal(res.possibility, 0);
assert.equal(res.warnings.length, 1);
assert.match(res.warnings[0], /unknown source node 'ghost:1'/);
});
});
+29
View File
@@ -165,4 +165,33 @@ describe('DSLRuntime extended', () => {
assert.equal(missed.possibility, 0);
assert.deepEqual(missed.missingFacts, [{ relation: 'owns', reason: 'no_provider' }]);
});
it('recompiling a scope uninstalls its stale relation configs', async () => {
const rt = new DSLRuntime(new Arbiter());
rt.compile(`
definition Employee { id: string? }
definition Doc { id: string? }
fact owns(user: Employee, doc: Doc)
evidence can_read(user: Employee, doc: Doc) { owns(user, doc) }
`, 'rt-stale');
rt.addNode('u:1', 'Employee', {});
rt.addNode('doc:9', 'Doc', {});
rt.addRelation('u:1', 'owns', 'doc:9', { possibility: 1.0 });
assert.equal(rt.arbiter.check('u:1', 'can_read', 'doc:9').possibility, 1.0);
// Recompile the SAME scope with a different program: can_read/owns are no
// longer declared and must not keep granting. A second scope's relations
// would be unaffected (compileMultiple coexistence).
rt.compile(`
definition Employee { id: string? }
definition Doc { id: string? }
fact shares(user: Employee, doc: Doc)
evidence can_share(user: Employee, doc: Doc) { shares(user, doc) }
`, 'rt-stale');
const stale = rt.arbiter.check('u:1', 'can_read', 'doc:9');
assert.equal(stale.possibility, 0, 'revoked can_read must not keep granting');
assert.ok(!rt.arbiter.relationConfigs.has('can_read'));
assert.ok(!rt.arbiter.relationConfigs.has('owns'));
assert.ok(rt.arbiter.relationConfigs.has('can_share'));
});
});
+40
View File
@@ -93,4 +93,44 @@ describe('DSLRuntime typing', () => {
rt.registerFact('owns', async () => ({ possibility: 2.0 }));
await assert.rejects(() => rt.check('u:1', 'can_read', 'doc:9'), /invalid possibility/);
});
it('enforces a literal value in evidence as an exact edge-value gate', async () => {
const rt = new DSLRuntime(new Arbiter()).compile(`
definition Employee { id: string? }
fact balance(user: Employee, amount: number)
evidence can_afford(user: Employee) { balance(user, 5) }
`, 'rt-expected-value');
rt.addNode('u:1', 'Employee', {});
// An edge carrying amount 3 must NOT satisfy balance(user, 5) — without the
// gate every balance edge matched regardless of amount (silent over-grant).
rt.addRelation('u:1', 'balance', 'u:1', { possibility: 1.0, value: 3 });
const denied = await rt.check('u:1', 'can_afford', 'u:1');
assert.equal(denied.possibility, 0, 'value-3 edge must not satisfy balance(user, 5)');
rt.addRelation('u:1', 'balance', 'u:1', { possibility: 0.8, value: 5 });
const granted = await rt.check('u:1', 'can_afford', 'u:1');
assert.equal(granted.possibility, 0.8, 'value-5 edge must satisfy balance(user, 5)');
});
it('treats a value-typed evidence OBJECT param as the expected edge value', async () => {
const rt = new DSLRuntime(new Arbiter()).compile(`
definition Employee { id: string? }
fact balance(user: Employee, amount: number)
evidence can_withdraw(user: Employee, amount: number) { balance(user, amount) }
`, 'rt-value-object');
rt.addNode('u:1', 'Employee', {});
rt.addRelation('u:1', 'balance', 'u:1', { possibility: 0.9, value: 5 });
// The check object is the VALUE, not a node key: grant only on exact match.
const granted = await rt.check('u:1', 'can_withdraw', 5);
assert.equal(granted.possibility, 0.9, 'value-5 check must match the value-5 edge');
const denied = await rt.check('u:1', 'can_withdraw', 3);
assert.equal(denied.possibility, 0, 'value-3 check must not match the value-5 edge');
// Re-checking the granted value must not hit a value-3 cache entry.
const again = await rt.check('u:1', 'can_withdraw', 5);
assert.equal(again.possibility, 0.9, 'value-5 re-check must not be served the value-3 result');
// Direct fact check with a value object works the same way.
const direct = await rt.check('u:1', 'balance', 5);
assert.equal(direct.possibility, 0.9, 'direct balance(user, 5) must match the value-5 edge');
// A non-scalar object for a value-typed param is rejected loudly.
await assert.rejects(() => rt.check('u:1', 'can_withdraw', 'u:1'), /must be number/);
});
});