feat: provider-result caching with time expiry + DSL fact-level TTL
Automatically-retrieved facts (balances, sessions, etc.) are now cached with
a time expiry so repeated checks don't re-invoke the underlying data store.
- check(): registered providers cache their normalized edges per
(relation, subject, object) with a TTL; a fresh entry is reused without
re-invoking the provider. Per-check factProviders are one-off observations
and stay cache-transparent (no read, no write).
- TTL resolution: DSL-declared 'BEHAVES { ttl <duration> }' on a fact >
setFactTTL(relation, ms) > policy.providerCacheTTL (default 30s; 0 disables).
- Grammar: facts may now declare a freshness window via
'fact balance(user, amount) BEHAVES { ttl 1h }' (previously BEHAVES only
accepted 'AS edge|transitive|hierarchical|symmetrical_graph'). The runtime
indexes it as the fact's ttlMs.
- registerFact/unregisterFact, graph mutations (add/update/remove node or
relation), and invalidateProviderCache(relation?) keep the cache consistent.
- Injectable clock (default wall clock) drives cache freshness, mirroring the
core's unpinned-clock contract.
Tests: DSLRuntimeCache (reuse within TTL, expiry re-invoke, per-check
override transparency, registerFact invalidation, mutation invalidation,
per-relation/all invalidation, policy default, DSL-declared 1h TTL).
This commit is contained in:
+124
-62
@@ -490,39 +490,46 @@ function peg$parse(input, options) {
|
||||
function peg$f29(behavior) {
|
||||
return { type: "BehaviorAnnotation", behavior };
|
||||
}
|
||||
function peg$f30() { return "transitive"; }
|
||||
function peg$f31() { return "symmetrical"; }
|
||||
function peg$f32(value) { return value; }
|
||||
function peg$f33(b) { return b; }
|
||||
function peg$f34(direction, period) {
|
||||
function peg$f30(behavior) {
|
||||
// Fact-level freshness: `fact balance(user, amount) BEHAVES { ttl 1h }`
|
||||
// declares the relation's value-freshness window, which the runtime uses
|
||||
// as the provider-result cache TTL. The behavior is wrapped like the
|
||||
// `BEHAVES AS` form so consumers read `behavior.behaviorType`.
|
||||
return { type: "BehaviorAnnotation", behavior };
|
||||
}
|
||||
function peg$f31() { return "transitive"; }
|
||||
function peg$f32() { return "symmetrical"; }
|
||||
function peg$f33(value) { return value; }
|
||||
function peg$f34(b) { return b; }
|
||||
function peg$f35(direction, period) {
|
||||
return { type: "Behavior", behaviorType: "decay", direction, period };
|
||||
}
|
||||
function peg$f35(mode, confidence) {
|
||||
function peg$f36(mode, confidence) {
|
||||
return { type: "Behavior", behaviorType: "blur", mode, confidence: confidence ? confidence[1] : null };
|
||||
}
|
||||
function peg$f36(duration) {
|
||||
function peg$f37(duration) {
|
||||
return { type: "Behavior", behaviorType: "ttl", duration };
|
||||
}
|
||||
function peg$f37(directive) { return directive; }
|
||||
function peg$f38(head, tail) { return buildLeftAssoc(head, tail); }
|
||||
function peg$f38(directive) { return directive; }
|
||||
function peg$f39(head, tail) { return buildLeftAssoc(head, tail); }
|
||||
function peg$f40(head, typeName) {
|
||||
function peg$f40(head, tail) { return buildLeftAssoc(head, tail); }
|
||||
function peg$f41(head, typeName) {
|
||||
return { type: "BinaryExpression", operator: "is", left: head, right: typeName };
|
||||
}
|
||||
function peg$f41(head, tail) { return buildLeftAssoc(head, tail); }
|
||||
function peg$f42(head, right) {
|
||||
function peg$f42(head, tail) { return buildLeftAssoc(head, tail); }
|
||||
function peg$f43(head, right) {
|
||||
return { type: "BinaryExpression", operator: "within", left: head, right };
|
||||
}
|
||||
function peg$f43(head, tail) { return buildLeftAssoc(head, tail); }
|
||||
function peg$f44(head, tail) { return buildLeftAssoc(head, tail); }
|
||||
function peg$f45(operator, operand) { return { type: "UnaryExpression", operator: "NOT", operand }; }
|
||||
function peg$f46(primary, binding) {
|
||||
function peg$f45(head, tail) { return buildLeftAssoc(head, tail); }
|
||||
function peg$f46(operator, operand) { return { type: "UnaryExpression", operator: "NOT", operand }; }
|
||||
function peg$f47(primary, binding) {
|
||||
if (binding) {
|
||||
return { type: "BindingAccess", expression: primary, binding };
|
||||
}
|
||||
return primary;
|
||||
}
|
||||
function peg$f47(head, tail) {
|
||||
function peg$f48(head, tail) {
|
||||
return tail.reduce((obj, part) => {
|
||||
return {
|
||||
type: "AttributeAccess",
|
||||
@@ -532,31 +539,31 @@ function peg$parse(input, options) {
|
||||
};
|
||||
}, head);
|
||||
}
|
||||
function peg$f48(expr) { return expr; }
|
||||
function peg$f49(name, args) {
|
||||
function peg$f49(expr) { return expr; }
|
||||
function peg$f50(name, args) {
|
||||
return { type: "PredicateCall", name, args: args || [], challenge: true };
|
||||
}
|
||||
function peg$f50(name, args) {
|
||||
function peg$f51(name, args) {
|
||||
return { type: "PredicateCall", name, args: args || [] };
|
||||
}
|
||||
function peg$f51(name) { return { type: "Variable", name }; }
|
||||
function peg$f52(head, tail) {
|
||||
return [head, ...tail.map(t => t[3])];
|
||||
}
|
||||
function peg$f52(name) { return { type: "Variable", name }; }
|
||||
function peg$f53(head, tail) {
|
||||
return [head, ...tail.map(t => t[3])];
|
||||
}
|
||||
function peg$f54(chars) {
|
||||
return { type: "Literal", value: JSON.parse(text()) };
|
||||
function peg$f54(head, tail) {
|
||||
return [head, ...tail.map(t => t[3])];
|
||||
}
|
||||
function peg$f55(chars) {
|
||||
return { type: "Literal", value: JSON.parse(text()) };
|
||||
}
|
||||
function peg$f56(chars) {
|
||||
return { type: "Literal", value: JSON.parse("\"" + chars.map(c => c[0] === '\\' ? c[1] : c[1]).join('') + "\"") };
|
||||
}
|
||||
function peg$f56(value) { return { type: "Literal", value: parseFloat(text()) }; }
|
||||
function peg$f57(value) { return { type: "Literal", value: parseInt(text(), 10) }; }
|
||||
function peg$f58(value) { return { type: "Literal", value: value === "true" }; }
|
||||
function peg$f59(value) { return { type: "Literal", value: text(), unit: text().slice(-1) }; }
|
||||
function peg$f60(name) { return name; }
|
||||
function peg$f57(value) { return { type: "Literal", value: parseFloat(text()) }; }
|
||||
function peg$f58(value) { return { type: "Literal", value: parseInt(text(), 10) }; }
|
||||
function peg$f59(value) { return { type: "Literal", value: value === "true" }; }
|
||||
function peg$f60(value) { return { type: "Literal", value: text(), unit: text().slice(-1) }; }
|
||||
function peg$f61(name) { return name; }
|
||||
let peg$currPos = options.peg$currPos | 0;
|
||||
let peg$savedPos = peg$currPos;
|
||||
const peg$posDetailsCache = [{ line: 1, column: 1 }];
|
||||
@@ -2456,7 +2463,7 @@ function peg$parse(input, options) {
|
||||
}
|
||||
|
||||
function peg$parseBehaviorAnnotation() {
|
||||
let s0, s1, s2, s3, s4, s5;
|
||||
let s0, s1, s2, s3, s4, s5, s6, s7;
|
||||
|
||||
s0 = peg$currPos;
|
||||
if (input.substr(peg$currPos, 7) === peg$c28) {
|
||||
@@ -2536,6 +2543,61 @@ function peg$parse(input, options) {
|
||||
peg$currPos = s0;
|
||||
s0 = peg$FAILED;
|
||||
}
|
||||
if (s0 === peg$FAILED) {
|
||||
s0 = peg$currPos;
|
||||
if (input.substr(peg$currPos, 7) === peg$c28) {
|
||||
s1 = peg$c28;
|
||||
peg$currPos += 7;
|
||||
} else {
|
||||
s1 = peg$FAILED;
|
||||
if (peg$silentFails === 0) { peg$fail(peg$e34); }
|
||||
}
|
||||
if (s1 !== peg$FAILED) {
|
||||
s2 = peg$parse__();
|
||||
if (s2 !== peg$FAILED) {
|
||||
if (input.charCodeAt(peg$currPos) === 123) {
|
||||
s3 = peg$c2;
|
||||
peg$currPos++;
|
||||
} else {
|
||||
s3 = peg$FAILED;
|
||||
if (peg$silentFails === 0) { peg$fail(peg$e3); }
|
||||
}
|
||||
if (s3 !== peg$FAILED) {
|
||||
s4 = peg$parse_();
|
||||
s5 = peg$parseTTLBehavior();
|
||||
if (s5 !== peg$FAILED) {
|
||||
s6 = peg$parse_();
|
||||
if (input.charCodeAt(peg$currPos) === 125) {
|
||||
s7 = peg$c3;
|
||||
peg$currPos++;
|
||||
} else {
|
||||
s7 = peg$FAILED;
|
||||
if (peg$silentFails === 0) { peg$fail(peg$e4); }
|
||||
}
|
||||
if (s7 !== peg$FAILED) {
|
||||
peg$savedPos = s0;
|
||||
s0 = peg$f30(s5);
|
||||
} else {
|
||||
peg$currPos = s0;
|
||||
s0 = peg$FAILED;
|
||||
}
|
||||
} else {
|
||||
peg$currPos = s0;
|
||||
s0 = peg$FAILED;
|
||||
}
|
||||
} else {
|
||||
peg$currPos = s0;
|
||||
s0 = peg$FAILED;
|
||||
}
|
||||
} else {
|
||||
peg$currPos = s0;
|
||||
s0 = peg$FAILED;
|
||||
}
|
||||
} else {
|
||||
peg$currPos = s0;
|
||||
s0 = peg$FAILED;
|
||||
}
|
||||
}
|
||||
|
||||
return s0;
|
||||
}
|
||||
@@ -2553,7 +2615,7 @@ function peg$parse(input, options) {
|
||||
}
|
||||
if (s1 !== peg$FAILED) {
|
||||
peg$savedPos = s0;
|
||||
s1 = peg$f30();
|
||||
s1 = peg$f31();
|
||||
}
|
||||
s0 = s1;
|
||||
if (s0 === peg$FAILED) {
|
||||
@@ -2567,7 +2629,7 @@ function peg$parse(input, options) {
|
||||
}
|
||||
if (s1 !== peg$FAILED) {
|
||||
peg$savedPos = s0;
|
||||
s1 = peg$f31();
|
||||
s1 = peg$f32();
|
||||
}
|
||||
s0 = s1;
|
||||
}
|
||||
@@ -2592,7 +2654,7 @@ function peg$parse(input, options) {
|
||||
s3 = peg$parseInteger();
|
||||
if (s3 !== peg$FAILED) {
|
||||
peg$savedPos = s0;
|
||||
s0 = peg$f32(s3);
|
||||
s0 = peg$f33(s3);
|
||||
} else {
|
||||
peg$currPos = s0;
|
||||
s0 = peg$FAILED;
|
||||
@@ -2650,7 +2712,7 @@ function peg$parse(input, options) {
|
||||
}
|
||||
if (s7 !== peg$FAILED) {
|
||||
peg$savedPos = s0;
|
||||
s0 = peg$f33(s5);
|
||||
s0 = peg$f34(s5);
|
||||
} else {
|
||||
peg$currPos = s0;
|
||||
s0 = peg$FAILED;
|
||||
@@ -2762,7 +2824,7 @@ function peg$parse(input, options) {
|
||||
}
|
||||
if (s5 !== peg$FAILED) {
|
||||
peg$savedPos = s0;
|
||||
s0 = peg$f34(s3, s5);
|
||||
s0 = peg$f35(s3, s5);
|
||||
} else {
|
||||
peg$currPos = s0;
|
||||
s0 = peg$FAILED;
|
||||
@@ -2870,7 +2932,7 @@ function peg$parse(input, options) {
|
||||
s4 = null;
|
||||
}
|
||||
peg$savedPos = s0;
|
||||
s0 = peg$f35(s3, s4);
|
||||
s0 = peg$f36(s3, s4);
|
||||
} else {
|
||||
peg$currPos = s0;
|
||||
s0 = peg$FAILED;
|
||||
@@ -2904,7 +2966,7 @@ function peg$parse(input, options) {
|
||||
s3 = peg$parseDuration();
|
||||
if (s3 !== peg$FAILED) {
|
||||
peg$savedPos = s0;
|
||||
s0 = peg$f36(s3);
|
||||
s0 = peg$f37(s3);
|
||||
} else {
|
||||
peg$currPos = s0;
|
||||
s0 = peg$FAILED;
|
||||
@@ -2953,7 +3015,7 @@ function peg$parse(input, options) {
|
||||
}
|
||||
if (s3 !== peg$FAILED) {
|
||||
peg$savedPos = s0;
|
||||
s0 = peg$f37(s3);
|
||||
s0 = peg$f38(s3);
|
||||
} else {
|
||||
peg$currPos = s0;
|
||||
s0 = peg$FAILED;
|
||||
@@ -3027,7 +3089,7 @@ function peg$parse(input, options) {
|
||||
}
|
||||
}
|
||||
peg$savedPos = s0;
|
||||
s0 = peg$f38(s1, s2);
|
||||
s0 = peg$f39(s1, s2);
|
||||
} else {
|
||||
peg$currPos = s0;
|
||||
s0 = peg$FAILED;
|
||||
@@ -3093,7 +3155,7 @@ function peg$parse(input, options) {
|
||||
}
|
||||
}
|
||||
peg$savedPos = s0;
|
||||
s0 = peg$f39(s1, s2);
|
||||
s0 = peg$f40(s1, s2);
|
||||
} else {
|
||||
peg$currPos = s0;
|
||||
s0 = peg$FAILED;
|
||||
@@ -3122,7 +3184,7 @@ function peg$parse(input, options) {
|
||||
s5 = peg$parseTypeName();
|
||||
if (s5 !== peg$FAILED) {
|
||||
peg$savedPos = s0;
|
||||
s0 = peg$f40(s1, s5);
|
||||
s0 = peg$f41(s1, s5);
|
||||
} else {
|
||||
peg$currPos = s0;
|
||||
s0 = peg$FAILED;
|
||||
@@ -3266,7 +3328,7 @@ function peg$parse(input, options) {
|
||||
}
|
||||
}
|
||||
peg$savedPos = s0;
|
||||
s0 = peg$f41(s1, s2);
|
||||
s0 = peg$f42(s1, s2);
|
||||
} else {
|
||||
peg$currPos = s0;
|
||||
s0 = peg$FAILED;
|
||||
@@ -3296,7 +3358,7 @@ function peg$parse(input, options) {
|
||||
s5 = peg$parseDuration();
|
||||
if (s5 !== peg$FAILED) {
|
||||
peg$savedPos = s0;
|
||||
s0 = peg$f42(s1, s5);
|
||||
s0 = peg$f43(s1, s5);
|
||||
} else {
|
||||
peg$currPos = s0;
|
||||
s0 = peg$FAILED;
|
||||
@@ -3377,7 +3439,7 @@ function peg$parse(input, options) {
|
||||
}
|
||||
}
|
||||
peg$savedPos = s0;
|
||||
s0 = peg$f43(s1, s2);
|
||||
s0 = peg$f44(s1, s2);
|
||||
} else {
|
||||
peg$currPos = s0;
|
||||
s0 = peg$FAILED;
|
||||
@@ -3443,7 +3505,7 @@ function peg$parse(input, options) {
|
||||
}
|
||||
}
|
||||
peg$savedPos = s0;
|
||||
s0 = peg$f44(s1, s2);
|
||||
s0 = peg$f45(s1, s2);
|
||||
} else {
|
||||
peg$currPos = s0;
|
||||
s0 = peg$FAILED;
|
||||
@@ -3478,7 +3540,7 @@ function peg$parse(input, options) {
|
||||
s3 = peg$parseUnary();
|
||||
if (s3 !== peg$FAILED) {
|
||||
peg$savedPos = s0;
|
||||
s0 = peg$f45(s1, s3);
|
||||
s0 = peg$f46(s1, s3);
|
||||
} else {
|
||||
peg$currPos = s0;
|
||||
s0 = peg$FAILED;
|
||||
@@ -3512,7 +3574,7 @@ function peg$parse(input, options) {
|
||||
s2 = null;
|
||||
}
|
||||
peg$savedPos = s0;
|
||||
s0 = peg$f46(s1, s2);
|
||||
s0 = peg$f47(s1, s2);
|
||||
} else {
|
||||
peg$currPos = s0;
|
||||
s0 = peg$FAILED;
|
||||
@@ -3583,7 +3645,7 @@ function peg$parse(input, options) {
|
||||
}
|
||||
if (s2 !== peg$FAILED) {
|
||||
peg$savedPos = s0;
|
||||
s0 = peg$f47(s1, s2);
|
||||
s0 = peg$f48(s1, s2);
|
||||
} else {
|
||||
peg$currPos = s0;
|
||||
s0 = peg$FAILED;
|
||||
@@ -3630,7 +3692,7 @@ function peg$parse(input, options) {
|
||||
}
|
||||
if (s5 !== peg$FAILED) {
|
||||
peg$savedPos = s0;
|
||||
s0 = peg$f48(s3);
|
||||
s0 = peg$f49(s3);
|
||||
} else {
|
||||
peg$currPos = s0;
|
||||
s0 = peg$FAILED;
|
||||
@@ -3694,7 +3756,7 @@ function peg$parse(input, options) {
|
||||
}
|
||||
if (s8 !== peg$FAILED) {
|
||||
peg$savedPos = s0;
|
||||
s0 = peg$f49(s2, s6);
|
||||
s0 = peg$f50(s2, s6);
|
||||
} else {
|
||||
peg$currPos = s0;
|
||||
s0 = peg$FAILED;
|
||||
@@ -3744,7 +3806,7 @@ function peg$parse(input, options) {
|
||||
}
|
||||
if (s6 !== peg$FAILED) {
|
||||
peg$savedPos = s0;
|
||||
s0 = peg$f50(s1, s4);
|
||||
s0 = peg$f51(s1, s4);
|
||||
} else {
|
||||
peg$currPos = s0;
|
||||
s0 = peg$FAILED;
|
||||
@@ -3768,7 +3830,7 @@ function peg$parse(input, options) {
|
||||
s1 = peg$parseIdentifier();
|
||||
if (s1 !== peg$FAILED) {
|
||||
peg$savedPos = s0;
|
||||
s1 = peg$f51(s1);
|
||||
s1 = peg$f52(s1);
|
||||
}
|
||||
s0 = s1;
|
||||
|
||||
@@ -3832,7 +3894,7 @@ function peg$parse(input, options) {
|
||||
}
|
||||
}
|
||||
peg$savedPos = s0;
|
||||
s0 = peg$f52(s1, s2);
|
||||
s0 = peg$f53(s1, s2);
|
||||
} else {
|
||||
peg$currPos = s0;
|
||||
s0 = peg$FAILED;
|
||||
@@ -3898,7 +3960,7 @@ function peg$parse(input, options) {
|
||||
}
|
||||
}
|
||||
peg$savedPos = s0;
|
||||
s0 = peg$f53(s1, s2);
|
||||
s0 = peg$f54(s1, s2);
|
||||
} else {
|
||||
peg$currPos = s0;
|
||||
s0 = peg$FAILED;
|
||||
@@ -4083,7 +4145,7 @@ function peg$parse(input, options) {
|
||||
}
|
||||
if (s3 !== peg$FAILED) {
|
||||
peg$savedPos = s0;
|
||||
s0 = peg$f54(s2);
|
||||
s0 = peg$f55(s2);
|
||||
} else {
|
||||
peg$currPos = s0;
|
||||
s0 = peg$FAILED;
|
||||
@@ -4245,7 +4307,7 @@ function peg$parse(input, options) {
|
||||
}
|
||||
if (s3 !== peg$FAILED) {
|
||||
peg$savedPos = s0;
|
||||
s0 = peg$f55(s2);
|
||||
s0 = peg$f56(s2);
|
||||
} else {
|
||||
peg$currPos = s0;
|
||||
s0 = peg$FAILED;
|
||||
@@ -4340,7 +4402,7 @@ function peg$parse(input, options) {
|
||||
}
|
||||
if (s1 !== peg$FAILED) {
|
||||
peg$savedPos = s0;
|
||||
s1 = peg$f56(s1);
|
||||
s1 = peg$f57(s1);
|
||||
}
|
||||
s0 = s1;
|
||||
peg$silentFails--;
|
||||
@@ -4381,7 +4443,7 @@ function peg$parse(input, options) {
|
||||
}
|
||||
if (s1 !== peg$FAILED) {
|
||||
peg$savedPos = s0;
|
||||
s1 = peg$f57(s1);
|
||||
s1 = peg$f58(s1);
|
||||
}
|
||||
s0 = s1;
|
||||
peg$silentFails--;
|
||||
@@ -4416,7 +4478,7 @@ function peg$parse(input, options) {
|
||||
}
|
||||
if (s1 !== peg$FAILED) {
|
||||
peg$savedPos = s0;
|
||||
s1 = peg$f58(s1);
|
||||
s1 = peg$f59(s1);
|
||||
}
|
||||
s0 = s1;
|
||||
peg$silentFails--;
|
||||
@@ -4477,7 +4539,7 @@ function peg$parse(input, options) {
|
||||
}
|
||||
if (s1 !== peg$FAILED) {
|
||||
peg$savedPos = s0;
|
||||
s1 = peg$f59(s1);
|
||||
s1 = peg$f60(s1);
|
||||
}
|
||||
s0 = s1;
|
||||
peg$silentFails--;
|
||||
@@ -4545,7 +4607,7 @@ function peg$parse(input, options) {
|
||||
}
|
||||
if (s2 !== peg$FAILED) {
|
||||
peg$savedPos = s0;
|
||||
s0 = peg$f60(s2);
|
||||
s0 = peg$f61(s2);
|
||||
} else {
|
||||
peg$currPos = s0;
|
||||
s0 = peg$FAILED;
|
||||
|
||||
Reference in New Issue
Block a user