@polycode-projects/the-mechanical-code-talker 5.0.25 → 5.0.26
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 +74 -11
- package/bin/tmct.mjs +81 -0
- package/package.json +5 -1
- package/src/adapters/corpus/courtesy.mjs +91 -28
- package/src/adapters/corpus/dbpedia-lookup-live.mjs +116 -0
- package/src/adapters/corpus/news-sources.mjs +436 -0
- package/src/adapters/corpus/research-source.mjs +16 -3
- package/src/adapters/corpus/wiktionary-live.mjs +158 -0
- package/src/adapters/news-store.mjs +87 -0
- package/src/adapters/toml-config.mjs +18 -0
- package/src/domain/choice-question.mjs +275 -0
- package/src/domain/cli-verbs.mjs +20 -0
- package/src/domain/dialogue-acts.mjs +1 -0
- package/src/domain/el-classify.mjs +569 -0
- package/src/domain/fact-phrase.mjs +70 -0
- package/src/domain/feed-normalize.mjs +224 -0
- package/src/domain/grammar/ace.mjs +223 -7
- package/src/domain/grammar/lexicon-core.json +2 -0
- package/src/domain/memory/trust.mjs +15 -0
- package/src/domain/news-feed.mjs +219 -0
- package/src/domain/reasoning-config.mjs +56 -0
- package/src/domain/sprite-animation.mjs +20 -2
- package/src/domain/tableau.mjs +1225 -0
- package/src/domain/term-ledger.mjs +118 -0
- package/src/services/chat.mjs +420 -20
- package/src/services/extract-facts.mjs +91 -1
- package/src/services/index.mjs +12 -0
- package/src/services/init.mjs +33 -0
- package/src/services/news.mjs +918 -0
- package/src/services/sprite-catalog-viz.mjs +148 -78
- package/src/surfaces/web/memory-ask-browser.bundle.js +132 -132
- package/src/surfaces/web/sprites-browser-entry.mjs +4 -4
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
// news-feed.mjs — from fact rows plus `now`, choose hubs, cut two-hop
|
|
2
|
+
// sub-graphs, assemble news items, and render the fixed paraphrase paragraph
|
|
3
|
+
// (PLAN_NEWS_FEED.md sections 8.2-8.3). Pure throughout: no clock, no I/O, no
|
|
4
|
+
// reliance on the caller's row order — feeding one fact set in two different
|
|
5
|
+
// orders yields byte-identical items, the same discipline p2p-room.mjs's
|
|
6
|
+
// sortFactIndividualsById holds for a CRDT-merged fact set.
|
|
7
|
+
|
|
8
|
+
import { sha256Bytes, normFactTerm } from "./hash.mjs";
|
|
9
|
+
import { FACT_PREDICATE_PHRASES, predicatePhrase, factSentence } from "./fact-phrase.mjs";
|
|
10
|
+
import { STOP_SET } from "./hub-terms.mjs";
|
|
11
|
+
|
|
12
|
+
export const NEWS_HUB_HOPS = 2; // fixed by design, not a knob
|
|
13
|
+
|
|
14
|
+
function sha256HexPrefix(str, nBytes) {
|
|
15
|
+
const bytes = sha256Bytes(str);
|
|
16
|
+
let hex = "";
|
|
17
|
+
for (let i = 0; i < nBytes; i += 1) hex += bytes[i].toString(16).padStart(2, "0");
|
|
18
|
+
return hex;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const byId = (a, b) => (a.id < b.id ? -1 : a.id > b.id ? 1 : 0);
|
|
22
|
+
|
|
23
|
+
function toMs(value) {
|
|
24
|
+
if (typeof value === "number") return value;
|
|
25
|
+
const t = Date.parse(value);
|
|
26
|
+
return Number.isFinite(t) ? t : NaN;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const NEWS_WINDOW_PROVENANCE_RE = /^(news:|news-fixture:|research:)/;
|
|
30
|
+
|
|
31
|
+
/** Rows whose provenance carries a `news:`, `news-fixture:` or `research:`
|
|
32
|
+
* tag and whose observedAt (else createdAt) falls inside [now - windowMs,
|
|
33
|
+
* now]. Pure filter — `now` is the caller's clock reading, nothing here
|
|
34
|
+
* reads a live one. */
|
|
35
|
+
export function newsWindowRows(rows, { now, windowMs }) {
|
|
36
|
+
const nowMs = toMs(now);
|
|
37
|
+
const startMs = nowMs - windowMs;
|
|
38
|
+
return rows.filter((row) => {
|
|
39
|
+
if (!NEWS_WINDOW_PROVENANCE_RE.test(String(row.provenance || ""))) return false;
|
|
40
|
+
const t = toMs(row.observedAt || row.createdAt || "");
|
|
41
|
+
return Number.isFinite(t) && t >= startMs && t <= nowMs;
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Counts window facts per term (subject and object, normalized, STOP_SET
|
|
46
|
+
* removed) -> [{ term, changed }] sorted changed desc then term asc, capped
|
|
47
|
+
* at `limit`. */
|
|
48
|
+
export function scoreHubs(rows, windowRows, { limit = 6 } = {}) {
|
|
49
|
+
const counts = new Map();
|
|
50
|
+
for (const row of windowRows) {
|
|
51
|
+
for (const raw of [row.subject, row.object]) {
|
|
52
|
+
const term = normFactTerm(raw);
|
|
53
|
+
if (!term || STOP_SET.has(term)) continue;
|
|
54
|
+
counts.set(term, (counts.get(term) || 0) + 1);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return [...counts.entries()]
|
|
58
|
+
.map(([term, changed]) => ({ term, changed }))
|
|
59
|
+
.sort((a, b) => b.changed - a.changed || (a.term < b.term ? -1 : a.term > b.term ? 1 : 0))
|
|
60
|
+
.slice(0, limit);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Breadth-first over subject/object adjacency from `hub`, exactly `hops`
|
|
64
|
+
* levels deep, then capped by content-addressed id — so the cap never
|
|
65
|
+
* depends on `rows`' own order, only on which rows the hop-bounded walk
|
|
66
|
+
* actually reaches. */
|
|
67
|
+
export function subgraphAround(rows, hub, { hops = NEWS_HUB_HOPS, cap = 60 } = {}) {
|
|
68
|
+
const hubTerm = normFactTerm(hub);
|
|
69
|
+
const visited = new Set([hubTerm]);
|
|
70
|
+
let frontier = [hubTerm];
|
|
71
|
+
const collected = new Map();
|
|
72
|
+
for (let hop = 0; hop < hops; hop += 1) {
|
|
73
|
+
const nextFrontier = new Set();
|
|
74
|
+
for (const term of [...frontier].sort()) {
|
|
75
|
+
const matches = rows.filter((r) => normFactTerm(r.subject) === term || normFactTerm(r.object) === term);
|
|
76
|
+
for (const row of matches) {
|
|
77
|
+
collected.set(row.id, row);
|
|
78
|
+
const s = normFactTerm(row.subject);
|
|
79
|
+
const o = normFactTerm(row.object);
|
|
80
|
+
if (!visited.has(s)) nextFrontier.add(s);
|
|
81
|
+
if (!visited.has(o)) nextFrontier.add(o);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
for (const term of nextFrontier) visited.add(term);
|
|
85
|
+
frontier = [...nextFrontier].sort();
|
|
86
|
+
}
|
|
87
|
+
return [...collected.values()].sort(byId).slice(0, cap);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** The strongest prior kind among `rows`, for the item's trust chip — read
|
|
91
|
+
* off each row's own `trust` (a number) and `sourceTypes` (the kind array
|
|
92
|
+
* readFactRows already computes), never a re-derivation of SOURCE_PRIOR. */
|
|
93
|
+
function tierOf(rows) {
|
|
94
|
+
if (!rows.length) return "";
|
|
95
|
+
let strongest = rows[0];
|
|
96
|
+
for (const row of rows) {
|
|
97
|
+
if (Number(row.trust ?? 0) > Number(strongest.trust ?? 0)) strongest = row;
|
|
98
|
+
}
|
|
99
|
+
const kinds = Array.isArray(strongest.sourceTypes) ? strongest.sourceTypes : [];
|
|
100
|
+
return kinds[0] || "";
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function collectSources(subgraphRows, sourcesByFactId) {
|
|
104
|
+
const get = (id) => (sourcesByFactId instanceof Map ? sourcesByFactId.get(id) : sourcesByFactId?.[id]);
|
|
105
|
+
const seen = new Set();
|
|
106
|
+
const sources = [];
|
|
107
|
+
for (const row of subgraphRows) {
|
|
108
|
+
const src = get(row.id);
|
|
109
|
+
if (!src) continue;
|
|
110
|
+
const key = src.url || src.title || "";
|
|
111
|
+
if (!key || seen.has(key)) continue;
|
|
112
|
+
seen.add(key);
|
|
113
|
+
sources.push({ title: src.title || "", url: src.url || "", name: src.name || "" });
|
|
114
|
+
}
|
|
115
|
+
return sources;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function joinWithAnd(items) {
|
|
119
|
+
if (items.length === 0) return "";
|
|
120
|
+
if (items.length === 1) return items[0];
|
|
121
|
+
return `${items.slice(0, -1).join(", ")} and ${items[items.length - 1]}`;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const IDENTITY_PREDICATES = new Set(["rdf:type", "rdfs:subClassOf"]);
|
|
125
|
+
const SENTENCE_CAP = 5;
|
|
126
|
+
|
|
127
|
+
/** The fixed five-sentence paraphrase template (PLAN_NEWS_FEED.md section
|
|
128
|
+
* 8.3): identity first, then the hub's own relations grouped by predicate in
|
|
129
|
+
* FACT_PREDICATE_PHRASES table order, then one closing sentence naming up to
|
|
130
|
+
* three second-hop facts. Every sentence shown is a grounded fact, never a
|
|
131
|
+
* paraphrase of prose the grammar could not read. */
|
|
132
|
+
export function renderNewsParagraph(hub, subgraphRows) {
|
|
133
|
+
const hubTerm = normFactTerm(hub);
|
|
134
|
+
const hubRows = subgraphRows.filter((r) => normFactTerm(r.subject) === hubTerm);
|
|
135
|
+
const secondHopRows = subgraphRows.filter(
|
|
136
|
+
(r) => normFactTerm(r.subject) !== hubTerm && normFactTerm(r.object) !== hubTerm,
|
|
137
|
+
);
|
|
138
|
+
|
|
139
|
+
const sentences = [];
|
|
140
|
+
|
|
141
|
+
const identityObjects = hubRows
|
|
142
|
+
.filter((r) => IDENTITY_PREDICATES.has(r.predicate))
|
|
143
|
+
.map((r) => r.object)
|
|
144
|
+
.sort();
|
|
145
|
+
if (identityObjects.length) sentences.push(`${hub} is a ${joinWithAnd(identityObjects)}`);
|
|
146
|
+
|
|
147
|
+
for (const predicate of Object.keys(FACT_PREDICATE_PHRASES)) {
|
|
148
|
+
if (IDENTITY_PREDICATES.has(predicate) || sentences.length >= SENTENCE_CAP) continue;
|
|
149
|
+
const objects = hubRows
|
|
150
|
+
.filter((r) => r.predicate === predicate)
|
|
151
|
+
.map((r) => r.object)
|
|
152
|
+
.sort();
|
|
153
|
+
if (!objects.length) continue;
|
|
154
|
+
sentences.push(`${hub} ${predicatePhrase(predicate)} ${joinWithAnd(objects)}`);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
if (sentences.length < SENTENCE_CAP && secondHopRows.length) {
|
|
158
|
+
const around = secondHopRows
|
|
159
|
+
.slice()
|
|
160
|
+
.sort(byId)
|
|
161
|
+
.slice(0, 3)
|
|
162
|
+
.map((r) => factSentence(r))
|
|
163
|
+
.join("; ");
|
|
164
|
+
sentences.push(`Around it: ${around}`);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const capped = sentences.slice(0, SENTENCE_CAP);
|
|
168
|
+
return capped.length ? `${capped.join(". ")}.` : "";
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** scoreHubs -> one item per hub (PLAN_NEWS_FEED.md section 6.6), paragraph
|
|
172
|
+
* included, sorted builtAt desc then id asc. `sourcesByFactId` maps fact ids
|
|
173
|
+
* to snapshot source links ({ title, url, name }). */
|
|
174
|
+
export function buildNewsItems(rows, { now, windowMs, limit = 6, sourcesByFactId = new Map() } = {}) {
|
|
175
|
+
const windowRows = newsWindowRows(rows, { now, windowMs });
|
|
176
|
+
const hubs = scoreHubs(rows, windowRows, { limit });
|
|
177
|
+
const items = hubs.map(({ term, changed }) => {
|
|
178
|
+
const subgraphRows = subgraphAround(rows, term);
|
|
179
|
+
const factIds = subgraphRows.map((r) => r.id).sort();
|
|
180
|
+
return {
|
|
181
|
+
id: `news-feed:${sha256HexPrefix(`${term}\0${factIds.join(",")}`, 8)}`,
|
|
182
|
+
hub: term,
|
|
183
|
+
factIds,
|
|
184
|
+
changedCount: changed,
|
|
185
|
+
builtAt: now,
|
|
186
|
+
paragraph: renderNewsParagraph(term, subgraphRows),
|
|
187
|
+
tier: tierOf(subgraphRows),
|
|
188
|
+
sources: collectSources(subgraphRows, sourcesByFactId),
|
|
189
|
+
};
|
|
190
|
+
});
|
|
191
|
+
return items.sort((a, b) => (toMs(b.builtAt) - toMs(a.builtAt)) || byId(a, b));
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// A `news:` tag, matched wherever it sits in a fact's provenance — bare
|
|
195
|
+
// ("news:<sourceId>@<itemId>", the shape a caller writing directly under
|
|
196
|
+
// this tag produces) or nested inside the strict/optimistic ingest wrapper
|
|
197
|
+
// ("extracted:news:<sourceId>@<itemId>", "optimistic-extract:news:<sourceId>
|
|
198
|
+
// @<itemId>" — ingestText's own audit-tag wrapping, and a fact the strict
|
|
199
|
+
// recognizer also grounds carries a THIRD, unioned ace:/teach: tag ahead of
|
|
200
|
+
// it). Never matches "news-fixture:" (no colon follows "news" there), which
|
|
201
|
+
// is the deliberate exclusion the fixture-replay rows need.
|
|
202
|
+
const NEWS_PROVENANCE_RE = /(?:^|[:|]\s*)news:/;
|
|
203
|
+
|
|
204
|
+
/** News-tagged fact ids to retract, oldest observedAt first, ties by id —
|
|
205
|
+
* the eviction the service applies at ingest time so the graph cannot grow
|
|
206
|
+
* past `cap` unattended. Never selects a seed/taught/research/fixture-
|
|
207
|
+
* replay row. */
|
|
208
|
+
export function evictNewsFacts(rows, { cap }) {
|
|
209
|
+
const newsRows = rows.filter((r) => NEWS_PROVENANCE_RE.test(String(r.provenance || "")));
|
|
210
|
+
if (newsRows.length <= cap) return [];
|
|
211
|
+
const sorted = newsRows.slice().sort((a, b) => {
|
|
212
|
+
const at = toMs(a.observedAt || "");
|
|
213
|
+
const bt = toMs(b.observedAt || "");
|
|
214
|
+
const an = Number.isFinite(at) ? at : 0;
|
|
215
|
+
const bn = Number.isFinite(bt) ? bt : 0;
|
|
216
|
+
return an - bn || byId(a, b);
|
|
217
|
+
});
|
|
218
|
+
return sorted.slice(0, newsRows.length - cap).map((r) => r.id);
|
|
219
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
// reasoning-config.mjs — the shipped defaults for the reasoning-lane tuning
|
|
2
|
+
// knobs (syllogise's own budget/depth, the EL classifier's budget/rounds, the
|
|
3
|
+
// shared environment-set cap, and the DL tableau's step/branch/node budgets
|
|
4
|
+
// phase 3 reads) and the pure function that folds a normalized tmct.toml's
|
|
5
|
+
// `[reasoning]` table over them — the same precedent resolveGameConfig sets
|
|
6
|
+
// for `[games.*]`/`[planning]`.
|
|
7
|
+
//
|
|
8
|
+
// Pure: no filesystem access, no module-level mutable state.
|
|
9
|
+
|
|
10
|
+
export const DEFAULT_REASONING_CONFIG = Object.freeze({
|
|
11
|
+
syllogiseBudget: 50,
|
|
12
|
+
syllogiseDepth: 32,
|
|
13
|
+
classifyBudget: 2000,
|
|
14
|
+
classifyRounds: 64,
|
|
15
|
+
maxEnvironments: 4,
|
|
16
|
+
proveSteps: 5000,
|
|
17
|
+
proveBranches: 256,
|
|
18
|
+
proveNodes: 512,
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
// snake_case tmct.toml key -> camelCase internal key — mirrors game-config.mjs's own KEY_MAP tables.
|
|
22
|
+
const REASONING_KEY_MAP = Object.freeze({
|
|
23
|
+
syllogise_budget: "syllogiseBudget",
|
|
24
|
+
syllogise_depth: "syllogiseDepth",
|
|
25
|
+
classify_budget: "classifyBudget",
|
|
26
|
+
classify_rounds: "classifyRounds",
|
|
27
|
+
max_environments: "maxEnvironments",
|
|
28
|
+
prove_steps: "proveSteps",
|
|
29
|
+
prove_branches: "proveBranches",
|
|
30
|
+
prove_nodes: "proveNodes",
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
/** A positive-integer knob, clamped to its shipped default when the raw value
|
|
34
|
+
* is missing, non-numeric, non-integer or not positive — a corrupt tmct.toml
|
|
35
|
+
* value degrades to the default, never a crash. */
|
|
36
|
+
function clampPositiveInt(raw, fallback) {
|
|
37
|
+
const n = Number(raw);
|
|
38
|
+
return Number.isInteger(n) && n > 0 ? n : fallback;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Fold a normalized tmct.toml's `reasoning` table (the raw sparse pass-through
|
|
43
|
+
* src/adapters/toml-config.mjs's normalizeConfig produces — snake_case keys,
|
|
44
|
+
* present only when actually set in the file) over DEFAULT_REASONING_CONFIG.
|
|
45
|
+
* `toml` may be null/undefined (no tmct.toml, or one that failed to load) —
|
|
46
|
+
* every key then falls back to its default. Returns a fully populated object
|
|
47
|
+
* of the same shape as DEFAULT_REASONING_CONFIG.
|
|
48
|
+
*/
|
|
49
|
+
export function resolveReasoningConfig(toml) {
|
|
50
|
+
const raw = toml?.reasoning || {};
|
|
51
|
+
const out = { ...DEFAULT_REASONING_CONFIG };
|
|
52
|
+
for (const [tomlKey, camelKey] of Object.entries(REASONING_KEY_MAP)) {
|
|
53
|
+
if (raw[tomlKey] !== undefined) out[camelKey] = clampPositiveInt(raw[tomlKey], DEFAULT_REASONING_CONFIG[camelKey]);
|
|
54
|
+
}
|
|
55
|
+
return out;
|
|
56
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// sprite-animation.mjs — the pure state machine behind every animated sprite
|
|
2
|
-
// tile: the hover flip-book (static/moving),
|
|
2
|
+
// tile: the hover flip-book (static/moving), each card's own turning
|
|
3
3
|
// rotation and emotion cycle, and the oscillate-and-turn walk a "moving"
|
|
4
4
|
// scene entity performs. Every function here is a pure function of its
|
|
5
5
|
// arguments — no wall clock, no local counter; the caller passes the tick.
|
|
@@ -29,7 +29,7 @@ export function movingCounterpartLabel(restLabel) {
|
|
|
29
29
|
return restLabel + " + moving";
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
-
/** The two modes
|
|
32
|
+
/** The two modes an animating sprite steps between, and the click toggle. */
|
|
33
33
|
export const FOCUS_MODE_ORDER = Object.freeze(["turning", "emotions"]);
|
|
34
34
|
|
|
35
35
|
/** Pure; `.toString()`-splice safe. */
|
|
@@ -37,6 +37,24 @@ export function nextFocusMode(mode) {
|
|
|
37
37
|
return mode === "turning" ? "emotions" : "turning";
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
+
/** The animation state a sprite card starts in: at rest, with the turning
|
|
41
|
+
* rotation queued for its first click. Pure; self-contained,
|
|
42
|
+
* `.toString()`-splice safe. */
|
|
43
|
+
export function initialCardAnimation() {
|
|
44
|
+
return { animating: false, mode: "turning" };
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** One click on a sprite card. A card at rest starts animating, turning
|
|
48
|
+
* rotation first; a card already animating keeps running and toggles
|
|
49
|
+
* between turning and emotion-cycling. Returns a fresh state and never
|
|
50
|
+
* stops the card — each card owns its own state, so starting or toggling
|
|
51
|
+
* one leaves every other card's animation alone. Pure; self-contained,
|
|
52
|
+
* `.toString()`-splice safe. */
|
|
53
|
+
export function cardAnimationClick(state) {
|
|
54
|
+
if (!state || !state.animating) return { animating: true, mode: "turning" };
|
|
55
|
+
return { animating: true, mode: state.mode === "turning" ? "emotions" : "turning" };
|
|
56
|
+
}
|
|
57
|
+
|
|
40
58
|
/** The frame a cycling cell shows at `tick`, looping. Pure;
|
|
41
59
|
* `.toString()`-splice safe. */
|
|
42
60
|
export function frameAtTick(frames, tick) {
|