renor-engine 1.1.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -2
- package/dist/.tsbuildinfo +1 -1
- package/dist/cli.js +26 -0
- package/dist/eval/indexSnapshots.js +1 -0
- package/dist/evidence/ollamaChat.js +1 -1
- package/dist/extraction/jevDecisionModel.js +307 -0
- package/dist/extraction/secretGuard.js +7 -3
- package/dist/learning/cli.js +113 -0
- package/dist/learning/schema.js +144 -0
- package/dist/learning/settings.js +45 -0
- package/dist/learning/store.js +267 -0
- package/dist/learning/tap.js +401 -0
- package/dist/memory/dreamer/adjudicate.js +215 -0
- package/dist/memory/dreamer/apply.js +120 -0
- package/dist/memory/dreamer/budget.js +64 -0
- package/dist/memory/dreamer/candidates.js +149 -0
- package/dist/memory/dreamer/collection.js +104 -0
- package/dist/memory/dreamer/dreamer.js +352 -0
- package/dist/memory/dreamer/review.js +111 -0
- package/dist/memory/factConflicts.js +2 -0
- package/dist/memory/lifecycle.js +28 -5
- package/dist/memory/writer.js +3 -2
- package/dist/mvp/agentMemory.js +3 -0
- package/dist/mvp/cli.js +11 -0
- package/dist/mvp/config.js +4 -0
- package/dist/mvp/exportAll.js +19 -0
- package/dist/mvp/server.js +118 -10
- package/dist/retrieval/orchestrator.js +69 -16
- package/dist/retrieval/rankingTrace.js +184 -0
- package/dist/sharing/cli.js +181 -0
- package/dist/sharing/examples.js +136 -0
- package/dist/sharing/fields.js +225 -0
- package/dist/sharing/loop.js +80 -0
- package/dist/sharing/project.js +327 -0
- package/dist/sharing/send.js +466 -0
- package/dist/sharing/state.js +222 -0
- package/dist/sharing/wording.js +48 -0
- package/package.json +1 -1
- package/dist/BUILD.json +0 -6
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DREAMER_SOURCE = void 0;
|
|
4
|
+
exports.scopeMayReplace = scopeMayReplace;
|
|
5
|
+
exports.provenanceStatement = provenanceStatement;
|
|
6
|
+
exports.planWrites = planWrites;
|
|
7
|
+
exports.applyWrites = applyWrites;
|
|
8
|
+
const rowStore_1 = require("../../context/rowStore");
|
|
9
|
+
const lifecycle_1 = require("../lifecycle");
|
|
10
|
+
const collection_1 = require("./collection");
|
|
11
|
+
const adjudicate_1 = require("./adjudicate");
|
|
12
|
+
/**
|
|
13
|
+
* What the dreamer writes, and the rules it holds the model's labels to. Only "updates" ever writes, and then as an
|
|
14
|
+
* ordinary lifecycle supersede (`remember`): the older fact is closed at the write time and stays in history, a new
|
|
15
|
+
* event carries the replacing value, and nothing is deleted. "same", "coexist" and "uncertain" write nothing.
|
|
16
|
+
*
|
|
17
|
+
* - The value goes into the older fact's own subject, predicate and scope, so a read of that name, in that scope,
|
|
18
|
+
* now says what the newer fact says. It is never written into the newer fact's name.
|
|
19
|
+
* - A value may replace one in its own scope, or one in a project scope when it was saved in `global` (an agent that
|
|
20
|
+
* forgot the project). It is never copied from one project scope into another, and never from a project scope into
|
|
21
|
+
* `global`: scope is the owner's isolation boundary.
|
|
22
|
+
* - The event is written by source `renor-dreamer`. Its statement says why, and names the ledger events it relied on.
|
|
23
|
+
* - A group about a collection predicate is never superseded here (collection.ts): it is skipped and flagged `both-may-hold`.
|
|
24
|
+
* - It is written only if the older fact is still the current one (`expectCurrentEvent`): an agent that corrected it
|
|
25
|
+
* while the model was thinking wins.
|
|
26
|
+
*/
|
|
27
|
+
exports.DREAMER_SOURCE = 'renor-dreamer';
|
|
28
|
+
/** Whether a value saved in scope `from` may replace the value of a fact in scope `into`. */
|
|
29
|
+
function scopeMayReplace(from, into) {
|
|
30
|
+
return from === into || from === 'global';
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* The ledger statement of a dreamer write: who and how (the dreamer, the prompt version, the model, a second look), the
|
|
34
|
+
* model's reason, which event it replaces and which events it relied on. It names the facts by event and by subject and
|
|
35
|
+
* predicate and does not repeat either value: a reply about the new value then does not carry the old one as a memory.
|
|
36
|
+
*/
|
|
37
|
+
function provenanceStatement(older, newer, via, reason, provenance) {
|
|
38
|
+
const relied = [older, ...via, newer].map(fact => fact.eventId).join(', ');
|
|
39
|
+
const how = [exports.DREAMER_SOURCE, provenance.promptVersion, (0, adjudicate_1.oneLine)(provenance.model, 60), ...(provenance.secondLook ? [`second look: ${provenance.secondLook}`] : [])].join(' · ');
|
|
40
|
+
return [
|
|
41
|
+
`[${how}] ${(0, adjudicate_1.oneLine)(reason, 160) || 'the newer fact replaces the older one'}.`,
|
|
42
|
+
`Replaces event ${older.eventId} (${(0, adjudicate_1.oneLine)(older.subject, 60)} · ${(0, adjudicate_1.oneLine)(older.predicate, 40)} · ${older.scope})`,
|
|
43
|
+
`with the value of event ${newer.eventId} (${(0, adjudicate_1.oneLine)(newer.subject, 60)} · ${newer.scope}).`,
|
|
44
|
+
`Relied on events: ${relied}.`,
|
|
45
|
+
].join(' ');
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* The writes a group's labels allow. `facts` are oldest first (candidates.ts). Each older fact is replaced by the newest
|
|
49
|
+
* fact labelled "updates" over it, and a replacement that was itself replaced hands on its own replacement, so the value
|
|
50
|
+
* written is the last one in the chain, never an intermediate one.
|
|
51
|
+
*/
|
|
52
|
+
function planWrites(kind, facts, pairs, provenance = { promptVersion: 'unknown', model: 'unknown' }) {
|
|
53
|
+
const position = new Map(facts.map((fact, i) => [fact.id, i]));
|
|
54
|
+
const replacedBy = new Map();
|
|
55
|
+
for (const pair of pairs) {
|
|
56
|
+
if (pair.verdict !== 'updates')
|
|
57
|
+
continue;
|
|
58
|
+
const o = position.get(pair.older);
|
|
59
|
+
const n = position.get(pair.newer);
|
|
60
|
+
if (o === undefined || n === undefined || n <= o)
|
|
61
|
+
continue; // the model does not decide which is older
|
|
62
|
+
const held = replacedBy.get(pair.older);
|
|
63
|
+
if (!held || position.get(held.fact.id) < n)
|
|
64
|
+
replacedBy.set(pair.older, { fact: facts[n], reason: pair.reason });
|
|
65
|
+
}
|
|
66
|
+
const writes = [];
|
|
67
|
+
const skipped = [];
|
|
68
|
+
const collection = (0, collection_1.collectionVerdict)(kind, facts, pairs);
|
|
69
|
+
for (const older of facts) {
|
|
70
|
+
const first = replacedBy.get(older.id);
|
|
71
|
+
if (!first)
|
|
72
|
+
continue;
|
|
73
|
+
const via = [];
|
|
74
|
+
let last = first.fact;
|
|
75
|
+
for (let next = replacedBy.get(last.id); next; next = replacedBy.get(last.id)) {
|
|
76
|
+
via.push(last);
|
|
77
|
+
last = next.fact;
|
|
78
|
+
}
|
|
79
|
+
if (kind === 'predicate') {
|
|
80
|
+
skipped.push({ older, newer: last, reason: 'predicate drift is named, not written: one predicate\'s value is not another\'s replacement' });
|
|
81
|
+
}
|
|
82
|
+
else if (collection.collection) {
|
|
83
|
+
skipped.push({ older, newer: last, reason: `a collection predicate (${collection.why}): both may hold`, flag: 'both-may-hold' });
|
|
84
|
+
}
|
|
85
|
+
else if (!scopeMayReplace(last.scope, older.scope)) {
|
|
86
|
+
skipped.push({ older, newer: last, reason: `a value saved in ${last.scope} may not replace one in ${older.scope}` });
|
|
87
|
+
}
|
|
88
|
+
else if ((0, rowStore_1.normDedupeValue)(last.object) === (0, rowStore_1.normDedupeValue)(older.object)) {
|
|
89
|
+
skipped.push({ older, newer: last, reason: 'the older fact already says the same' });
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
writes.push({ older, newer: last, via, reason: first.reason, statement: provenanceStatement(older, last, via, first.reason, provenance) });
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return { writes, skipped };
|
|
96
|
+
}
|
|
97
|
+
/** Write the planned supersedes, one at a time, through the lifecycle. Each is refused rather than forced when its older fact has changed. */
|
|
98
|
+
async function applyWrites(target, writes) {
|
|
99
|
+
const out = [];
|
|
100
|
+
for (const write of writes) {
|
|
101
|
+
try {
|
|
102
|
+
const receipt = await (0, lifecycle_1.remember)(target.db, target.vaultPath, target.ledgerRelPath, {
|
|
103
|
+
subject: write.older.subject,
|
|
104
|
+
predicate: write.older.predicate,
|
|
105
|
+
object: write.newer.object,
|
|
106
|
+
source: exports.DREAMER_SOURCE,
|
|
107
|
+
scope: write.older.scope,
|
|
108
|
+
statement: write.statement,
|
|
109
|
+
expectCurrentEvent: write.older.eventId,
|
|
110
|
+
});
|
|
111
|
+
out.push(receipt.action === 'supersede'
|
|
112
|
+
? { write, receipt }
|
|
113
|
+
: { write, receipt, refused: `unexpected action ${receipt.action}` });
|
|
114
|
+
}
|
|
115
|
+
catch (err) {
|
|
116
|
+
out.push({ write, refused: err instanceof lifecycle_1.StaleExpectationError ? 'the fact changed since it was judged' : (0, adjudicate_1.oneLine)(err instanceof Error ? err.message : String(err), 200) });
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return out;
|
|
120
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DreamerBudget = exports.DreamerCapError = exports.DEFAULT_BUDGET = void 0;
|
|
4
|
+
exports.meteredChat = meteredChat;
|
|
5
|
+
/** DeepSeek V4.1 Flash on OpenCode Go, rounded up from the list cost the provider reports per call (0.143 and 0.592 USD per million). */
|
|
6
|
+
exports.DEFAULT_BUDGET = { maxUsdPerDay: 0.05, priceInPerM: 0.15, priceOutPerM: 0.6 };
|
|
7
|
+
class DreamerCapError extends Error {
|
|
8
|
+
constructor(message = 'the dreamer\'s cost cap for today is reached') {
|
|
9
|
+
super(message);
|
|
10
|
+
this.name = 'DreamerCapError';
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
exports.DreamerCapError = DreamerCapError;
|
|
14
|
+
const utcDay = (ms) => new Date(ms).toISOString().slice(0, 10);
|
|
15
|
+
const CHARS_PER_TOKEN = 3.2;
|
|
16
|
+
class DreamerBudget {
|
|
17
|
+
store;
|
|
18
|
+
config;
|
|
19
|
+
now;
|
|
20
|
+
constructor(store, config, now = Date.now) {
|
|
21
|
+
this.store = store;
|
|
22
|
+
this.config = config;
|
|
23
|
+
this.now = now;
|
|
24
|
+
}
|
|
25
|
+
key() {
|
|
26
|
+
return `spend:${utcDay(this.now())}`;
|
|
27
|
+
}
|
|
28
|
+
/** USD spent today. */
|
|
29
|
+
spentToday() {
|
|
30
|
+
const raw = Number(this.store.get(this.key()));
|
|
31
|
+
return Number.isFinite(raw) && raw > 0 ? raw : 0;
|
|
32
|
+
}
|
|
33
|
+
/** The most a call can cost: its prompt and its output ceiling. */
|
|
34
|
+
worstCase(promptChars, maxTokens) {
|
|
35
|
+
const promptTokens = Math.ceil(promptChars / CHARS_PER_TOKEN) + 16;
|
|
36
|
+
return (promptTokens * this.config.priceInPerM + maxTokens * this.config.priceOutPerM) / 1e6;
|
|
37
|
+
}
|
|
38
|
+
canAfford(usd) {
|
|
39
|
+
return this.spentToday() + usd <= this.config.maxUsdPerDay;
|
|
40
|
+
}
|
|
41
|
+
/** Add what a call cost, from its usage; `fallbackUsd` stands in when the provider reported none. */
|
|
42
|
+
record(usage, fallbackUsd) {
|
|
43
|
+
const cost = usage.promptTokens === undefined && usage.completionTokens === undefined
|
|
44
|
+
? fallbackUsd
|
|
45
|
+
: ((usage.promptTokens ?? 0) * this.config.priceInPerM + (usage.completionTokens ?? 0) * this.config.priceOutPerM) / 1e6;
|
|
46
|
+
this.store.set(this.key(), String(this.spentToday() + cost));
|
|
47
|
+
return cost;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
exports.DreamerBudget = DreamerBudget;
|
|
51
|
+
/** A chat that checks the cap before each call and charges its usage after. `onCap` is told when a call was refused. */
|
|
52
|
+
function meteredChat(chat, budget, onCap) {
|
|
53
|
+
return async (request) => {
|
|
54
|
+
const chars = request.messages.reduce((n, m) => n + m.content.length, 0);
|
|
55
|
+
const worst = budget.worstCase(chars, request.max_tokens ?? 8192);
|
|
56
|
+
if (!budget.canAfford(worst)) {
|
|
57
|
+
onCap();
|
|
58
|
+
throw new DreamerCapError();
|
|
59
|
+
}
|
|
60
|
+
const response = await chat(request);
|
|
61
|
+
budget.record(response, worst);
|
|
62
|
+
return response;
|
|
63
|
+
};
|
|
64
|
+
}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MAX_CLUSTER_FACTS = void 0;
|
|
4
|
+
exports.currentDreamerFacts = currentDreamerFacts;
|
|
5
|
+
exports.findCandidateClusters = findCandidateClusters;
|
|
6
|
+
const rowStore_1 = require("../../context/rowStore");
|
|
7
|
+
const queryHelpers_1 = require("../../retrieval/queryHelpers");
|
|
8
|
+
const factTerms_1 = require("../factTerms");
|
|
9
|
+
const factConflicts_1 = require("../factConflicts");
|
|
10
|
+
/** The most facts one adjudication judges; a group with more is reported as too large and left alone. */
|
|
11
|
+
exports.MAX_CLUSTER_FACTS = 4;
|
|
12
|
+
const ORDER = (a, b) => a.validFrom < b.validFrom ? -1 : a.validFrom > b.validFrom ? 1 : a.id - b.id;
|
|
13
|
+
/** Every current lifecycle fact that has a ledger event, as the reads show it. */
|
|
14
|
+
function currentDreamerFacts(db) {
|
|
15
|
+
return db.prepare(`SELECT f.id AS id, f.event_id AS eventId, e.canonical_name AS subject, f.predicate AS predicate,
|
|
16
|
+
COALESCE(f.object_text, '') AS object, COALESCE(f.scope, 'global') AS scope, f.valid_from AS validFrom
|
|
17
|
+
FROM facts f JOIN entities e ON e.id = f.subject_entity_id
|
|
18
|
+
WHERE f.valid_to IS NULL AND f.source_path = '_mcp_' AND f.event_id IS NOT NULL
|
|
19
|
+
ORDER BY f.valid_from, f.id`).all();
|
|
20
|
+
}
|
|
21
|
+
/** Union-find over `n` items. */
|
|
22
|
+
function disjointSets(n) {
|
|
23
|
+
const parent = Array.from({ length: n }, (_, i) => i);
|
|
24
|
+
const root = (i) => {
|
|
25
|
+
while (parent[i] !== i)
|
|
26
|
+
i = parent[i] = parent[parent[i]];
|
|
27
|
+
return i;
|
|
28
|
+
};
|
|
29
|
+
return { root, join: (a, b) => { parent[root(a)] = root(b); } };
|
|
30
|
+
}
|
|
31
|
+
/** The sets the pairs `connected` joins; only sets of two or more, each as the indices of `facts` it holds. */
|
|
32
|
+
function components(facts, connected, buckets) {
|
|
33
|
+
const sets = disjointSets(facts.length);
|
|
34
|
+
for (const members of buckets.values()) {
|
|
35
|
+
for (let i = 0; i < members.length; i++) {
|
|
36
|
+
for (let j = i + 1; j < members.length; j++) {
|
|
37
|
+
if (sets.root(members[i]) !== sets.root(members[j]) && connected(members[i], members[j]))
|
|
38
|
+
sets.join(members[i], members[j]);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
const byRoot = new Map();
|
|
43
|
+
facts.forEach((_, i) => {
|
|
44
|
+
const r = sets.root(i);
|
|
45
|
+
const members = byRoot.get(r);
|
|
46
|
+
if (members)
|
|
47
|
+
members.push(i);
|
|
48
|
+
else
|
|
49
|
+
byRoot.set(r, [i]);
|
|
50
|
+
});
|
|
51
|
+
return [...byRoot.values()].filter(members => members.length > 1);
|
|
52
|
+
}
|
|
53
|
+
function add(buckets, key, index) {
|
|
54
|
+
const members = buckets.get(key);
|
|
55
|
+
if (members)
|
|
56
|
+
members.push(index);
|
|
57
|
+
else
|
|
58
|
+
buckets.set(key, [index]);
|
|
59
|
+
}
|
|
60
|
+
/** Whether a group's values are not all one value (after the fold the reads use to compare values). */
|
|
61
|
+
function disagrees(facts) {
|
|
62
|
+
return new Set(facts.map(fact => (0, rowStore_1.normDedupeValue)(fact.object))).size > 1;
|
|
63
|
+
}
|
|
64
|
+
/** Same predicate, related subjects (factConflicts.ts), values that disagree. */
|
|
65
|
+
function nameOrScope(facts, names) {
|
|
66
|
+
const byPredicate = new Map();
|
|
67
|
+
for (const fact of facts) {
|
|
68
|
+
const group = byPredicate.get(fact.predicate);
|
|
69
|
+
if (group)
|
|
70
|
+
group.push(fact);
|
|
71
|
+
else
|
|
72
|
+
byPredicate.set(fact.predicate, [fact]);
|
|
73
|
+
}
|
|
74
|
+
const out = [];
|
|
75
|
+
for (const group of byPredicate.values()) {
|
|
76
|
+
if (group.length < 2)
|
|
77
|
+
continue;
|
|
78
|
+
// A related name has the other's first word first, or its last word last, or is one identity: so only facts that
|
|
79
|
+
// share one of those are compared.
|
|
80
|
+
const buckets = new Map();
|
|
81
|
+
group.forEach((fact, i) => {
|
|
82
|
+
const name = (0, factConflicts_1.nameOf)(fact.subject, names);
|
|
83
|
+
add(buckets, `k:${name.key ??= (0, queryHelpers_1.identityKey)(fact.subject)}`, i);
|
|
84
|
+
if (name.words) {
|
|
85
|
+
add(buckets, `f:${name.words[0]}`, i);
|
|
86
|
+
add(buckets, `l:${name.words[name.words.length - 1]}`, i);
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
for (const members of components(group, (a, b) => (0, factConflicts_1.related)((0, factConflicts_1.nameOf)(group[a].subject, names), (0, factConflicts_1.nameOf)(group[b].subject, names)), buckets)) {
|
|
90
|
+
const cluster = members.map(i => group[i]).sort(ORDER);
|
|
91
|
+
if (disagrees(cluster))
|
|
92
|
+
out.push({ kind: 'name-or-scope', facts: cluster });
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return out;
|
|
96
|
+
}
|
|
97
|
+
/** The shortest predicate word that can echo a value: two-letter words ("on", "id") appear in too many values. */
|
|
98
|
+
const MIN_ECHO_WORD = 3;
|
|
99
|
+
/**
|
|
100
|
+
* One subject in one scope under two predicates, where a word of one predicate is a word of the other fact's value.
|
|
101
|
+
* An agent that writes `database = Postgres 16 (AWS RDS)` and another that writes `postgres_version = 16` have used a
|
|
102
|
+
* second predicate for the same thing, and the second one's name repeats the first one's value ("postgres").
|
|
103
|
+
*
|
|
104
|
+
* Not "two predicates that share a word". Tried on a week of an agent's saves, that rule misses this pair (`database` and
|
|
105
|
+
* `postgres_version` share none) and returns two that are not drift, because a generic word is shared:
|
|
106
|
+
* `commit_message_style` / `answer_style`, and `release_cadence` / `current_release`.
|
|
107
|
+
*/
|
|
108
|
+
function predicateDrift(facts) {
|
|
109
|
+
const bySubject = new Map();
|
|
110
|
+
for (const fact of facts) {
|
|
111
|
+
const key = `${(0, queryHelpers_1.identityKey)(fact.subject)}\u0000${fact.scope}`;
|
|
112
|
+
const group = bySubject.get(key);
|
|
113
|
+
if (group)
|
|
114
|
+
group.push(fact);
|
|
115
|
+
else
|
|
116
|
+
bySubject.set(key, [fact]);
|
|
117
|
+
}
|
|
118
|
+
const out = [];
|
|
119
|
+
for (const group of bySubject.values()) {
|
|
120
|
+
if (group.length < 2)
|
|
121
|
+
continue;
|
|
122
|
+
const predicateWords = group.map(fact => (0, factTerms_1.atoms)(fact.predicate).filter(word => word.length >= MIN_ECHO_WORD));
|
|
123
|
+
const valueWords = group.map(fact => new Set((0, factTerms_1.atoms)(fact.object)));
|
|
124
|
+
const everyone = new Map([['*', group.map((_, i) => i)]]);
|
|
125
|
+
const echoes = (a, b) => predicateWords[a].some(word => valueWords[b].has(word));
|
|
126
|
+
const drift = (a, b) => group[a].predicate !== group[b].predicate && (echoes(a, b) || echoes(b, a));
|
|
127
|
+
for (const members of components(group, drift, everyone)) {
|
|
128
|
+
const cluster = members.map(i => group[i]).sort(ORDER);
|
|
129
|
+
if (disagrees(cluster))
|
|
130
|
+
out.push({ kind: 'predicate', facts: cluster });
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return out;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* The groups of current facts that may be one thing, newest first (by the newest fact each holds). Which of them were
|
|
137
|
+
* already judged is the dreamer's to know (a group is identified by its facts' ledger events), not this module's.
|
|
138
|
+
*/
|
|
139
|
+
function findCandidateClusters(db) {
|
|
140
|
+
const facts = currentDreamerFacts(db);
|
|
141
|
+
const names = new Map();
|
|
142
|
+
const clusters = [...nameOrScope(facts, names), ...predicateDrift(facts)];
|
|
143
|
+
return clusters.sort((a, b) => {
|
|
144
|
+
const newest = (c) => c.facts[c.facts.length - 1];
|
|
145
|
+
const x = newest(a);
|
|
146
|
+
const y = newest(b);
|
|
147
|
+
return x.validFrom < y.validFrom ? 1 : x.validFrom > y.validFrom ? -1 : y.id - x.id;
|
|
148
|
+
});
|
|
149
|
+
}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SEVERAL_CURRENT_VALUES = void 0;
|
|
4
|
+
exports.collectionByName = collectionByName;
|
|
5
|
+
exports.collectionVerdict = collectionVerdict;
|
|
6
|
+
/**
|
|
7
|
+
* The collection guard. Some predicates hold a collection: what a project supports, who contributes
|
|
8
|
+
* to it, which languages someone speaks. A newer value for such a predicate is another member, not a replacement, and a
|
|
9
|
+
* model reading two facts cannot always tell (it replaced one member in 6 of 8 clusters with the round-2 "newest value wins"
|
|
10
|
+
* prompt, 2 of 8 once the prompt named collections). So the dreamer does not rely on the prompt alone: a group about a
|
|
11
|
+
* collection predicate is never superseded automatically. It is reported as `both-may-hold` and left as it is.
|
|
12
|
+
*
|
|
13
|
+
* Two deterministic signs, no model:
|
|
14
|
+
* 1. the predicate's name: a word that names a collection or a verb that makes one ("supports", "integrates_with",
|
|
15
|
+
* "exports", "contributors", "languages", "skills"...), or a plural noun;
|
|
16
|
+
* 2. the workspace: three or more different current values for one predicate under the same thing, when the reader
|
|
17
|
+
* did not call each value replaced by the next one. A single-valued attribute does not keep three values for long (the lifecycle
|
|
18
|
+
* replaces a value saved under the same name and scope), but a plain chain of replacements under three spellings of
|
|
19
|
+
* a name (Fly.io -> ECS -> Render) has three too, and is exactly what the dreamer is for: so a group whose consecutive pairs
|
|
20
|
+
* are all "updates" is a chain and is not held back.
|
|
21
|
+
*
|
|
22
|
+
* The guard errs toward holding back: a false "collection" costs a stale value left in place (what the dreamer did not do
|
|
23
|
+
* yesterday either), a missed one costs a member hidden from current reads (history keeps it). It does not know a
|
|
24
|
+
* collection by an unusual name; the prompt and the second look are the backstop there, and the report says so.
|
|
25
|
+
*/
|
|
26
|
+
/** Words that make a predicate a collection or a membership, split on `_`, `-` and spaces. Exact words only. */
|
|
27
|
+
const COLLECTION_WORDS = new Set([
|
|
28
|
+
'supports', 'supported', 'supporting', 'integrates', 'integration', 'integrations', 'exports', 'export', 'exported',
|
|
29
|
+
'imports', 'offers', 'includes', 'contains', 'speaks', 'knows', 'holds', 'plays', 'reads',
|
|
30
|
+
'member', 'members', 'contributor', 'contributors', 'maintainers', 'collaborators', 'participants', 'attendees', 'reviewers',
|
|
31
|
+
'skill', 'skills', 'certification', 'certifications', 'hobby', 'hobbies', 'interest', 'interests', 'language', 'languages',
|
|
32
|
+
'plugin', 'plugins', 'extension', 'extensions', 'tag', 'tags', 'topic', 'topics', 'label', 'labels', 'dependency', 'dependencies',
|
|
33
|
+
'endpoint', 'endpoints', 'feature', 'features', 'capability', 'capabilities', 'compatible', 'compatibility',
|
|
34
|
+
'alias', 'aliases', 'allowlist', 'blocklist', 'whitelist', 'blacklist', 'also',
|
|
35
|
+
]);
|
|
36
|
+
/** Phrases (consecutive words) that make a collection and are not single words above. */
|
|
37
|
+
const COLLECTION_PHRASES = [
|
|
38
|
+
['works', 'with'], ['member', 'of'], ['contributes', 'to'], ['belongs', 'to'], ['part', 'of'], ['related', 'to'],
|
|
39
|
+
];
|
|
40
|
+
/** Plural-looking words that are not plurals ("status", "address", "dns"...), so the plural rule leaves them alone. */
|
|
41
|
+
const NOT_PLURAL = new Set([
|
|
42
|
+
'status', 'address', 'class', 'process', 'access', 'analysis', 'alias', 'bias', 'canvas', 'business', 'progress', 'focus', 'bonus',
|
|
43
|
+
'plus', 'series', 'species', 'news', 'mass', 'glass', 'loss', 'gas', 'lens', 'sms', 'aws', 'https', 'dns', 'tls', 'ios', 'cors',
|
|
44
|
+
'js', 'css', 'pass', 'less', 'success', 'chaos', 'basis', 'axis', 'radius', 'virus', 'campus', 'corpus', 'consensus', 'previous',
|
|
45
|
+
'various', 'serious', 'famous', 'always', 'perhaps', 'across', 'versus', 'its', 'this', 'has', 'was', 'does', 'goes', 'is', 'as',
|
|
46
|
+
'uses', 'raises', 'increases',
|
|
47
|
+
// plural in form, one value in practice: a span of time, a limit, a setting ("working_hours" is one range, "rate_limits" one number)
|
|
48
|
+
'hours', 'days', 'minutes', 'seconds', 'weeks', 'months', 'years', 'times', 'dates', 'limits', 'settings', 'options',
|
|
49
|
+
]);
|
|
50
|
+
const wordsOf = (predicate) => predicate.toLowerCase().split(/[^a-z0-9]+/).filter(Boolean);
|
|
51
|
+
function pluralWord(word) {
|
|
52
|
+
if (word.length < 5 || NOT_PLURAL.has(word))
|
|
53
|
+
return false;
|
|
54
|
+
if (!word.endsWith('s'))
|
|
55
|
+
return false;
|
|
56
|
+
if (/(ss|us|is|os)$/.test(word))
|
|
57
|
+
return false;
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
60
|
+
/** Whether the NAME of a predicate says it holds a collection, and which rule said so. */
|
|
61
|
+
function collectionByName(predicate) {
|
|
62
|
+
const words = wordsOf(predicate);
|
|
63
|
+
const listed = words.find(w => COLLECTION_WORDS.has(w));
|
|
64
|
+
if (listed)
|
|
65
|
+
return { collection: true, why: `collection word "${listed}"` };
|
|
66
|
+
for (const phrase of COLLECTION_PHRASES) {
|
|
67
|
+
for (let i = 0; i + phrase.length <= words.length; i++) {
|
|
68
|
+
if (phrase.every((w, j) => words[i + j] === w))
|
|
69
|
+
return { collection: true, why: `collection phrase "${phrase.join('_')}"` };
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
const plural = words.find(pluralWord);
|
|
73
|
+
if (plural)
|
|
74
|
+
return { collection: true, why: 'plural word' };
|
|
75
|
+
return { collection: false, why: '' };
|
|
76
|
+
}
|
|
77
|
+
/** Whether each fact (oldest first) was called replaced by the next one: a chain of replacements, whatever the other pairs say. */
|
|
78
|
+
function isChain(facts, pairs) {
|
|
79
|
+
for (let i = 0; i + 1 < facts.length; i++) {
|
|
80
|
+
const pair = pairs.find(p => p.older === facts[i].id && p.newer === facts[i + 1].id);
|
|
81
|
+
if (pair?.verdict !== 'updates')
|
|
82
|
+
return false;
|
|
83
|
+
}
|
|
84
|
+
return true;
|
|
85
|
+
}
|
|
86
|
+
/** The number of current values for one predicate under the same thing at which the workspace itself says it is a collection. */
|
|
87
|
+
exports.SEVERAL_CURRENT_VALUES = 3;
|
|
88
|
+
/**
|
|
89
|
+
* Whether a candidate group is about a collection: by the name of its predicate, or because it holds three or more different
|
|
90
|
+
* current values for it and (when the labels are known) the reader did not call every pair an update. Without labels (a look at a workspace before any
|
|
91
|
+
* model is asked) the second sign is not applied: it needs the reader. A `predicate` group (two predicates, one naming
|
|
92
|
+
* the other's value) is never written anyway.
|
|
93
|
+
*/
|
|
94
|
+
function collectionVerdict(kind, facts, pairs) {
|
|
95
|
+
if (kind === 'predicate')
|
|
96
|
+
return { collection: false, why: '' };
|
|
97
|
+
const byName = collectionByName(facts[0].predicate);
|
|
98
|
+
if (byName.collection)
|
|
99
|
+
return byName;
|
|
100
|
+
const values = new Set(facts.map(f => f.object.trim().toLowerCase()));
|
|
101
|
+
if (pairs && facts.length >= exports.SEVERAL_CURRENT_VALUES && values.size >= exports.SEVERAL_CURRENT_VALUES && !isChain(facts, pairs))
|
|
102
|
+
return { collection: true, why: `${values.size} current values` };
|
|
103
|
+
return { collection: false, why: '' };
|
|
104
|
+
}
|