@polycode-projects/the-mechanical-code-talker 5.0.15 → 5.0.16
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 +11 -3
- package/bin/tmct.mjs +16 -0
- package/corpus/LICENSES.json +3 -3
- package/corpus/domains/code/LICENSE-NOTICE +43 -0
- package/corpus/{seon → domains/code}/README.md +22 -24
- package/corpus/domains/code/vocab.json +43 -0
- package/corpus/reference/manifest.json +126 -126
- package/corpus/reference/shards/ref-00.jsonl.gz +0 -0
- package/corpus/reference/shards/ref-01.jsonl.gz +0 -0
- package/corpus/reference/shards/ref-02.jsonl.gz +0 -0
- package/corpus/reference/shards/ref-03.jsonl.gz +0 -0
- package/corpus/reference/shards/ref-04.jsonl.gz +0 -0
- package/corpus/reference/shards/ref-05.jsonl.gz +0 -0
- package/corpus/reference/shards/ref-06.jsonl.gz +0 -0
- package/corpus/reference/shards/ref-07.jsonl.gz +0 -0
- package/corpus/reference/shards/ref-08.jsonl.gz +0 -0
- package/corpus/reference/shards/ref-09.jsonl.gz +0 -0
- package/corpus/reference/shards/ref-0a.jsonl.gz +0 -0
- package/corpus/reference/shards/ref-0b.jsonl.gz +0 -0
- package/corpus/reference/shards/ref-0c.jsonl.gz +0 -0
- package/corpus/reference/shards/ref-0d.jsonl.gz +0 -0
- package/corpus/reference/shards/ref-0e.jsonl.gz +0 -0
- package/corpus/reference/shards/ref-0f.jsonl.gz +0 -0
- package/corpus/reference/shards/ref-10.jsonl.gz +0 -0
- package/corpus/reference/shards/ref-11.jsonl.gz +0 -0
- package/corpus/reference/shards/ref-12.jsonl.gz +0 -0
- package/corpus/reference/shards/ref-13.jsonl.gz +0 -0
- package/corpus/reference/shards/ref-14.jsonl.gz +0 -0
- package/corpus/reference/shards/ref-15.jsonl.gz +0 -0
- package/corpus/reference/shards/ref-16.jsonl.gz +0 -0
- package/corpus/reference/shards/ref-17.jsonl.gz +0 -0
- package/corpus/reference/shards/ref-18.jsonl.gz +0 -0
- package/corpus/reference/shards/ref-19.jsonl.gz +0 -0
- package/corpus/reference/shards/ref-1a.jsonl.gz +0 -0
- package/corpus/reference/shards/ref-1b.jsonl.gz +0 -0
- package/corpus/reference/shards/ref-1c.jsonl.gz +0 -0
- package/corpus/reference/shards/ref-1d.jsonl.gz +0 -0
- package/corpus/reference/shards/ref-1e.jsonl.gz +0 -0
- package/corpus/reference/shards/ref-1f.jsonl.gz +0 -0
- package/corpus/reference/shards/ref-20.jsonl.gz +0 -0
- package/corpus/reference/shards/ref-21.jsonl.gz +0 -0
- package/corpus/reference/shards/ref-22.jsonl.gz +0 -0
- package/corpus/reference/shards/ref-23.jsonl.gz +0 -0
- package/corpus/reference/shards/ref-24.jsonl.gz +0 -0
- package/corpus/reference/shards/ref-25.jsonl.gz +0 -0
- package/corpus/reference/shards/ref-26.jsonl.gz +0 -0
- package/corpus/reference/shards/ref-27.jsonl.gz +0 -0
- package/corpus/reference/shards/ref-28.jsonl.gz +0 -0
- package/corpus/reference/shards/ref-29.jsonl.gz +0 -0
- package/corpus/reference/shards/ref-2a.jsonl.gz +0 -0
- package/corpus/reference/shards/ref-2b.jsonl.gz +0 -0
- package/corpus/reference/shards/ref-2c.jsonl.gz +0 -0
- package/corpus/reference/shards/ref-2d.jsonl.gz +0 -0
- package/corpus/reference/shards/ref-2e.jsonl.gz +0 -0
- package/corpus/reference/shards/ref-2f.jsonl.gz +0 -0
- package/corpus/reference/shards/ref-30.jsonl.gz +0 -0
- package/corpus/reference/shards/ref-31.jsonl.gz +0 -0
- package/corpus/reference/shards/ref-32.jsonl.gz +0 -0
- package/corpus/reference/shards/ref-33.jsonl.gz +0 -0
- package/corpus/reference/shards/ref-34.jsonl.gz +0 -0
- package/corpus/reference/shards/ref-35.jsonl.gz +0 -0
- package/corpus/reference/shards/ref-36.jsonl.gz +0 -0
- package/corpus/reference/shards/ref-37.jsonl.gz +0 -0
- package/corpus/reference/shards/ref-38.jsonl.gz +0 -0
- package/corpus/reference/shards/ref-39.jsonl.gz +0 -0
- package/corpus/reference/shards/ref-3a.jsonl.gz +0 -0
- package/corpus/reference/shards/ref-3b.jsonl.gz +0 -0
- package/corpus/reference/shards/ref-3c.jsonl.gz +0 -0
- package/corpus/reference/shards/ref-3d.jsonl.gz +0 -0
- package/corpus/reference/shards/ref-3e.jsonl.gz +0 -0
- package/corpus/reference/shards/ref-3f.jsonl.gz +0 -0
- package/corpus/tier2/vocab-empty.json +6 -0
- package/data/templates/responses.jsonl +3 -0
- package/package.json +17 -1
- package/src/adapters/corpus/conceptnet.mjs +7 -3
- package/src/adapters/corpus/courtesy.mjs +129 -0
- package/src/adapters/corpus/research-source.mjs +120 -0
- package/src/adapters/corpus/wikidata-live.mjs +262 -0
- package/src/adapters/corpus/wikipedia-live.mjs +52 -108
- package/src/domain/codegraph.mjs +10 -2
- package/src/domain/grammar/lexicon-core.json +1 -0
- package/src/domain/reference-pack.mjs +61 -9
- package/src/services/chat-session.mjs +48 -7
- package/src/services/chat.mjs +359 -115
- package/src/services/extensions.mjs +203 -3
- package/src/services/init.mjs +6 -5
- package/src/services/lane-vocab-data.mjs +56 -0
- package/src/surfaces/web/claims-browser-entry.mjs +26 -0
- package/src/surfaces/web/memory-ask-browser.bundle.js +130 -128
- package/src/surfaces/web/planner-instances.mjs +152 -0
- package/src/tools/handlers/tmct-untested.mjs +2 -2
- package/src/tools/server.mjs +9 -4
- package/corpus/seon/LICENSE-NOTICE +0 -37
- /package/corpus/{seon → domains/code}/concepts.jsonl +0 -0
- /package/corpus/{seon → domains/code}/definitions.jsonl +0 -0
- /package/corpus/{seon → domains/code}/relations.jsonl +0 -0
|
@@ -25,10 +25,18 @@
|
|
|
25
25
|
import { normFactTerm } from "../../domain/hash.mjs";
|
|
26
26
|
import { loadLexicon } from "../../domain/grammar/lexicon.mjs";
|
|
27
27
|
import { isReferenceArticleRow, sentencesUpTo, isaOf, SUMMARY_CHAR_CAP } from "../../domain/reference-pack.mjs";
|
|
28
|
+
import { registerResearchSource, researchSourceTag } from "./research-source.mjs";
|
|
29
|
+
import { createCourtesyGate, DEFAULT_TIMEOUT_MS, DEFAULT_MIN_INTERVAL_MS, MAXLAG_SECONDS } from "./courtesy.mjs";
|
|
28
30
|
|
|
29
31
|
export const WIKIPEDIA_LIVE_ORIGIN = "https://en.wikipedia.org";
|
|
30
32
|
export const SIMPLE_WIKIPEDIA_ORIGIN = "https://simple.wikipedia.org";
|
|
31
33
|
|
|
34
|
+
/** The names these two providers go by as research SOURCES
|
|
35
|
+
* (research-source.mjs): the name segment of every provenance tag their facts
|
|
36
|
+
* carry, and the key each registers under. */
|
|
37
|
+
export const WIKIPEDIA_LIVE_SOURCE_NAME = "wikipedia-live";
|
|
38
|
+
export const SIMPLE_WIKIPEDIA_SOURCE_NAME = "simple-wikipedia";
|
|
39
|
+
|
|
32
40
|
/** The identification string Wikimedia's robot policy asks API clients to
|
|
33
41
|
* carry, pointing at this project's public site as the contact. Browsers
|
|
34
42
|
* refuse to override the User-Agent request header, so the API-recognised
|
|
@@ -36,25 +44,20 @@ export const SIMPLE_WIKIPEDIA_ORIGIN = "https://simple.wikipedia.org";
|
|
|
36
44
|
* sent. */
|
|
37
45
|
export const WIKIMEDIA_USER_AGENT = "the-mechanical-code-talker (+https://tmct.polycode.co.uk/)";
|
|
38
46
|
|
|
39
|
-
const DEFAULT_TIMEOUT_MS = 4000;
|
|
40
|
-
const DEFAULT_MIN_INTERVAL_MS = 2000;
|
|
41
|
-
const RETRY_AFTER_FLOOR_MS = 5000;
|
|
42
|
-
// Action-API requests carry maxlag so an overloaded replica set answers with
|
|
43
|
-
// an error we back off from instead of adding to its load (Wikimedia's own
|
|
44
|
-
// recommended default for non-interactive clients).
|
|
45
|
-
const MAXLAG_SECONDS = 5;
|
|
46
|
-
|
|
47
47
|
/**
|
|
48
48
|
* A live-lookup provider: { lookup(normTerm) -> article row | null }, plus
|
|
49
49
|
* the research fan-out surface: pageByTitle(title) fetches an exact title's
|
|
50
50
|
* summary in ONE round trip (no opensearch — a linked title is already
|
|
51
51
|
* exact), and linkedTitles(title) lists the namespace-0 articles the page's
|
|
52
|
-
* LEAD section links to, in document order.
|
|
52
|
+
* LEAD section links to, in document order. It also carries the research
|
|
53
|
+
* source contract (research-source.mjs): `name`, `origin` and
|
|
54
|
+
* `provenanceTag(term)`.
|
|
53
55
|
*
|
|
54
56
|
* `fetchImpl` defaults to the global fetch; `origin` to en.wikipedia.org;
|
|
55
|
-
* `
|
|
56
|
-
* pack's own ({ term, title,
|
|
57
|
-
*
|
|
57
|
+
* `sourceName` to the en.wikipedia source name; `lexicon` (optional) feeds the
|
|
58
|
+
* isa extraction. The row shape is the shipped pack's own ({ term, title,
|
|
59
|
+
* text, summary, url, revid, isa? }), validated by isReferenceArticleRow
|
|
60
|
+
* before it is ever returned.
|
|
58
61
|
*
|
|
59
62
|
* Throttle posture: every public method takes one "slot" gated by the
|
|
60
63
|
* minimum interval, the single-flight guard and any open cool-off. By
|
|
@@ -71,76 +74,9 @@ export function createWikipediaLiveProvider({
|
|
|
71
74
|
lexicon = null,
|
|
72
75
|
userAgent = WIKIMEDIA_USER_AGENT,
|
|
73
76
|
waitForSlot = false,
|
|
77
|
+
sourceName = WIKIPEDIA_LIVE_SOURCE_NAME,
|
|
74
78
|
} = {}) {
|
|
75
|
-
const
|
|
76
|
-
const cache = new Map(); // key -> row | null (hits AND settled misses)
|
|
77
|
-
let lastLookupAt = 0;
|
|
78
|
-
let coolOffUntil = 0;
|
|
79
|
-
let inFlight = false;
|
|
80
|
-
|
|
81
|
-
const identifyingHeaders = () => {
|
|
82
|
-
if (!userAgent) return null;
|
|
83
|
-
const headers = { "Api-User-Agent": userAgent };
|
|
84
|
-
// A browser strips User-Agent as a forbidden header; only set it where
|
|
85
|
-
// no DOM says we are one (Node ships a global `navigator` these days, so
|
|
86
|
-
// `document` is the discriminating global).
|
|
87
|
-
if (typeof document === "undefined") headers["User-Agent"] = userAgent;
|
|
88
|
-
return headers;
|
|
89
|
-
};
|
|
90
|
-
|
|
91
|
-
function openCoolOff(retryAfterSeconds) {
|
|
92
|
-
const retryAfterMs = Number(retryAfterSeconds) * 1000;
|
|
93
|
-
coolOffUntil = Date.now() + Math.max(retryAfterMs || 0, RETRY_AFTER_FLOOR_MS);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
async function fetchJson(url) {
|
|
97
|
-
const controller = typeof AbortController === "function" ? new AbortController() : null;
|
|
98
|
-
const timer = controller ? setTimeout(() => controller.abort(), timeoutMs) : null;
|
|
99
|
-
try {
|
|
100
|
-
const opts = {};
|
|
101
|
-
if (controller) opts.signal = controller.signal;
|
|
102
|
-
const headers = identifyingHeaders();
|
|
103
|
-
if (headers) opts.headers = headers;
|
|
104
|
-
const res = await doFetch(url, opts);
|
|
105
|
-
if (res.status === 429) {
|
|
106
|
-
openCoolOff(res.headers?.get?.("retry-after"));
|
|
107
|
-
return null;
|
|
108
|
-
}
|
|
109
|
-
if (!res.ok) return null;
|
|
110
|
-
const body = await res.json();
|
|
111
|
-
// A maxlag rejection arrives as HTTP 200 with an error body (and a
|
|
112
|
-
// Retry-After header) — back off exactly as a 429 asks.
|
|
113
|
-
if (body?.error?.code === "maxlag") {
|
|
114
|
-
openCoolOff(res.headers?.get?.("retry-after"));
|
|
115
|
-
return null;
|
|
116
|
-
}
|
|
117
|
-
return body;
|
|
118
|
-
} catch {
|
|
119
|
-
return null;
|
|
120
|
-
} finally {
|
|
121
|
-
if (timer !== null) clearTimeout(timer);
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
/** When the next network slot opens: past the cool-off, past the minimum
|
|
126
|
-
* interval since the last taken slot. */
|
|
127
|
-
const slotOpensAt = () => Math.max(coolOffUntil, lastLookupAt + minIntervalMs);
|
|
128
|
-
|
|
129
|
-
/** Take the one network slot, or report it unavailable. Default posture
|
|
130
|
-
* returns false immediately (the clean-miss hook's "null, never block");
|
|
131
|
-
* `waitForSlot` sleeps until the slot opens instead. */
|
|
132
|
-
async function takeSlot() {
|
|
133
|
-
for (;;) {
|
|
134
|
-
const now = Date.now();
|
|
135
|
-
if (!inFlight && now >= slotOpensAt()) {
|
|
136
|
-
lastLookupAt = now;
|
|
137
|
-
inFlight = true;
|
|
138
|
-
return true;
|
|
139
|
-
}
|
|
140
|
-
if (!waitForSlot) return false;
|
|
141
|
-
await new Promise((resolve) => setTimeout(resolve, Math.max(slotOpensAt() - now, 25)));
|
|
142
|
-
}
|
|
143
|
-
}
|
|
79
|
+
const gate = createCourtesyGate({ fetchImpl, timeoutMs, minIntervalMs, userAgent, waitForSlot });
|
|
144
80
|
|
|
145
81
|
/** The opensearch title whose normFactTerm fold equals or extends the key —
|
|
146
82
|
* the topic-drift guard: "quasar" may resolve to "Quasar" or "Quasars",
|
|
@@ -155,7 +91,7 @@ export function createWikipediaLiveProvider({
|
|
|
155
91
|
}
|
|
156
92
|
|
|
157
93
|
async function summaryRow(key, title) {
|
|
158
|
-
const summary = await fetchJson(
|
|
94
|
+
const summary = await gate.fetchJson(
|
|
159
95
|
`${origin}/api/rest_v1/page/summary/${encodeURIComponent(title.replace(/ /g, "_"))}`,
|
|
160
96
|
);
|
|
161
97
|
if (!summary) return null;
|
|
@@ -177,7 +113,7 @@ export function createWikipediaLiveProvider({
|
|
|
177
113
|
}
|
|
178
114
|
|
|
179
115
|
async function roundTrips(key) {
|
|
180
|
-
const search = await fetchJson(
|
|
116
|
+
const search = await gate.fetchJson(
|
|
181
117
|
`${origin}/w/api.php?action=opensearch&format=json&origin=*&maxlag=${MAXLAG_SECONDS}&search=${encodeURIComponent(key)}&limit=3`,
|
|
182
118
|
);
|
|
183
119
|
const title = search ? matchingTitle(key, search) : null;
|
|
@@ -185,29 +121,21 @@ export function createWikipediaLiveProvider({
|
|
|
185
121
|
return summaryRow(key, title);
|
|
186
122
|
}
|
|
187
123
|
|
|
188
|
-
/** One slot-gated, cached operation: cache first (a settled hit or miss is
|
|
189
|
-
* never refetched), then the slot, then `work()`, with every failure
|
|
190
|
-
* cached as null so it never costs a second round trip. */
|
|
191
|
-
async function cachedFetch(cacheKey, work) {
|
|
192
|
-
if (cache.has(cacheKey)) return cache.get(cacheKey);
|
|
193
|
-
if (!(await takeSlot())) return null;
|
|
194
|
-
let value = null;
|
|
195
|
-
try {
|
|
196
|
-
value = await work();
|
|
197
|
-
} catch {
|
|
198
|
-
value = null;
|
|
199
|
-
} finally {
|
|
200
|
-
inFlight = false;
|
|
201
|
-
}
|
|
202
|
-
cache.set(cacheKey, value);
|
|
203
|
-
return value;
|
|
204
|
-
}
|
|
205
|
-
|
|
206
124
|
return {
|
|
125
|
+
name: sourceName,
|
|
126
|
+
origin,
|
|
127
|
+
|
|
128
|
+
/** The tag a fact gained through the research source contract carries. The
|
|
129
|
+
* research lane stamps its own `research:<topic>@<depth>` tag instead when
|
|
130
|
+
* it ingests, because only the lane knows how far its fan-out reached. */
|
|
131
|
+
provenanceTag(term) {
|
|
132
|
+
return researchSourceTag(sourceName, term);
|
|
133
|
+
},
|
|
134
|
+
|
|
207
135
|
async lookup(normTerm) {
|
|
208
136
|
const key = String(normTerm ?? "");
|
|
209
137
|
if (!key) return null;
|
|
210
|
-
return cachedFetch(key, () => roundTrips(key));
|
|
138
|
+
return gate.cachedFetch(key, () => roundTrips(key));
|
|
211
139
|
},
|
|
212
140
|
|
|
213
141
|
/** The summary for an EXACT title — one round trip, no opensearch. The
|
|
@@ -216,7 +144,7 @@ export function createWikipediaLiveProvider({
|
|
|
216
144
|
async pageByTitle(title) {
|
|
217
145
|
const t = String(title ?? "").trim();
|
|
218
146
|
if (!t) return null;
|
|
219
|
-
return cachedFetch(`title\0${normFactTerm(t)}`, () => summaryRow(normFactTerm(t), t));
|
|
147
|
+
return gate.cachedFetch(`title\0${normFactTerm(t)}`, () => summaryRow(normFactTerm(t), t));
|
|
220
148
|
},
|
|
221
149
|
|
|
222
150
|
/** The namespace-0 articles the page's LEAD section links to, in document
|
|
@@ -228,8 +156,8 @@ export function createWikipediaLiveProvider({
|
|
|
228
156
|
async linkedTitles(title, { limit = 25 } = {}) {
|
|
229
157
|
const t = String(title ?? "").trim();
|
|
230
158
|
if (!t) return null;
|
|
231
|
-
const listed = await cachedFetch(`links\0${normFactTerm(t)}`, async () => {
|
|
232
|
-
const parsed = await fetchJson(
|
|
159
|
+
const listed = await gate.cachedFetch(`links\0${normFactTerm(t)}`, async () => {
|
|
160
|
+
const parsed = await gate.fetchJson(
|
|
233
161
|
`${origin}/w/api.php?action=parse&format=json&formatversion=2&origin=*&maxlag=${MAXLAG_SECONDS}&prop=links&redirects=1&page=${encodeURIComponent(t)}§ion=0`,
|
|
234
162
|
);
|
|
235
163
|
const links = parsed?.parse?.links;
|
|
@@ -291,11 +219,27 @@ export function registerResearchProvider(provider) {
|
|
|
291
219
|
export function getResearchProvider({ minIntervalMs } = {}) {
|
|
292
220
|
if (registeredResearchProvider) return registeredResearchProvider;
|
|
293
221
|
if (!defaultResearchProvider) {
|
|
294
|
-
defaultResearchProvider =
|
|
295
|
-
origin: SIMPLE_WIKIPEDIA_ORIGIN,
|
|
296
|
-
waitForSlot: true,
|
|
222
|
+
defaultResearchProvider = createSimpleWikipediaResearchSource({
|
|
297
223
|
minIntervalMs: Math.max(DEFAULT_MIN_INTERVAL_MS, Number(minIntervalMs) || 0),
|
|
298
224
|
});
|
|
299
225
|
}
|
|
300
226
|
return defaultResearchProvider;
|
|
301
227
|
}
|
|
228
|
+
|
|
229
|
+
/** The Simple English Wikipedia research source: the same provider, named and
|
|
230
|
+
* origin-pinned for the research-source registry, with `waitForSlot` on. Every
|
|
231
|
+
* option the caller passes wins, so a test can hand it a stub transport and a
|
|
232
|
+
* zero interval. */
|
|
233
|
+
export function createSimpleWikipediaResearchSource(options = {}) {
|
|
234
|
+
return createWikipediaLiveProvider({
|
|
235
|
+
origin: SIMPLE_WIKIPEDIA_ORIGIN,
|
|
236
|
+
sourceName: SIMPLE_WIKIPEDIA_SOURCE_NAME,
|
|
237
|
+
waitForSlot: true,
|
|
238
|
+
...options,
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
registerResearchSource({
|
|
243
|
+
name: SIMPLE_WIKIPEDIA_SOURCE_NAME,
|
|
244
|
+
create: createSimpleWikipediaResearchSource,
|
|
245
|
+
});
|
package/src/domain/codegraph.mjs
CHANGED
|
@@ -1370,8 +1370,15 @@ export function renderTestsFor(graph, ind) {
|
|
|
1370
1370
|
* statements, so the empty survey has to say which one it means: with no
|
|
1371
1371
|
* source module recorded there is no coverage claim to make at all, and the
|
|
1372
1372
|
* full-coverage sentence would be an assertion about a repo this store has
|
|
1373
|
-
* never seen.
|
|
1374
|
-
|
|
1373
|
+
* never seen.
|
|
1374
|
+
*
|
|
1375
|
+
* `codeDomainActive` (the session-level predicate; omitted derives from the
|
|
1376
|
+
* graph alone, same fallback the chat layer's own gate uses) — with no code
|
|
1377
|
+
* domain active, the empty survey says nothing about a "code index" at all:
|
|
1378
|
+
* this is a slash command, not a miss the chat layer's own text-swap covers,
|
|
1379
|
+
* so the neutral wording has to live here. */
|
|
1380
|
+
export function renderUntested(graph, { codeDomainActive = null } = {}) {
|
|
1381
|
+
const domainActive = codeDomainActive ?? (moduleCountOf(graph) > 0);
|
|
1375
1382
|
const covered = new Set();
|
|
1376
1383
|
const testModules = new Set();
|
|
1377
1384
|
for (const e of edgesOfKind(graph, "tests")) {
|
|
@@ -1384,6 +1391,7 @@ export function renderUntested(graph) {
|
|
|
1384
1391
|
&& !isTestPath(String(i.label).toLowerCase()),
|
|
1385
1392
|
);
|
|
1386
1393
|
if (!sourceModules.length) {
|
|
1394
|
+
if (!domainActive) return "this session holds no graph to check test coverage against.";
|
|
1387
1395
|
return moduleCountOf(graph) === 0
|
|
1388
1396
|
? `no modules to check for test coverage in this index. ${NO_CODE_INDEX_NOTE}`
|
|
1389
1397
|
: "no source modules to check for test coverage in this index — only test modules are recorded.";
|
|
@@ -157,7 +157,26 @@ export function sentencesUpTo(text, cap) {
|
|
|
157
157
|
return out || String(text ?? "").slice(0, cap);
|
|
158
158
|
}
|
|
159
159
|
|
|
160
|
-
|
|
160
|
+
// The preamble before the copula is bounded only by the sentence itself
|
|
161
|
+
// (no "is"/"are" can cross a ".", "!" or "?"), not by a fixed character
|
|
162
|
+
// count — an appositive list ("also called cougars, mountain lions, …")
|
|
163
|
+
// can push the real copula well past what a short cap would reach.
|
|
164
|
+
const ISA_RE = /^[A-Z][^.!?]*? (?:is|are) (?:(a|an|the) )?([a-z][a-z -]{1,60})/;
|
|
165
|
+
|
|
166
|
+
// A "(…)" aside breaks the plain-lowercase word class ISA_RE's capture group
|
|
167
|
+
// matches against, and can itself push the real copula past any fixed cap —
|
|
168
|
+
// so every parenthetical is dropped before matching, not just accounted for
|
|
169
|
+
// in the cap. Applied to the whole string: a later sentence's own aside
|
|
170
|
+
// never reaches back to move the first sentence's boundary.
|
|
171
|
+
function withoutParentheticals(text) {
|
|
172
|
+
let out = text;
|
|
173
|
+
let prior;
|
|
174
|
+
do {
|
|
175
|
+
prior = out;
|
|
176
|
+
out = out.replace(/\s*\([^()]*\)/g, "");
|
|
177
|
+
} while (out !== prior);
|
|
178
|
+
return out;
|
|
179
|
+
}
|
|
161
180
|
|
|
162
181
|
// Words that end the noun phrase: a relative clause or a trailing modifier
|
|
163
182
|
// carries detail, not the category ("a place where ships shelter" → place).
|
|
@@ -178,18 +197,48 @@ const ISA_ARTICLES = new Set(["a", "an", "the"]);
|
|
|
178
197
|
// The classifier heads an of-chain reads THROUGH to the real class ("a kind
|
|
179
198
|
// of dog" → dog). Any other head before "of" keeps the outer phrase: "a body
|
|
180
199
|
// of ice" states composition, and "a game of skill" is a game — neither
|
|
181
|
-
// makes the of-object the class.
|
|
200
|
+
// makes the of-object the class. Partitive, collective and measure heads
|
|
201
|
+
// ("a group of animals", "a piece of matter", "an accumulation of vegetation")
|
|
202
|
+
// read through the same way — the of-object names the class, the head names
|
|
203
|
+
// only how much of it or how it is packaged. "member" and "body" are the
|
|
204
|
+
// deliberate exceptions: "a member of the cat family" and "a body of water"
|
|
205
|
+
// name membership or composition, not what the thing is, so they stay off
|
|
206
|
+
// this set and fall through to ISA_GENERIC_HEADS' null instead.
|
|
182
207
|
const ISA_OF_READ_THROUGH = new Set([
|
|
183
208
|
"type", "kind", "sort", "form", "class", "variety", "species", "breed", "genus",
|
|
209
|
+
"group", "part", "parts", "piece", "bunch", "bit", "name", "set", "way", "unit", "family",
|
|
210
|
+
"ability", "color", "series", "mammal", "practice", "list", "amount", "mass",
|
|
211
|
+
"reworking", "blockage", "goddess", "aura", "development", "consisting",
|
|
212
|
+
"announcement", "messengers", "subset", "carrying", "photograph", "adding",
|
|
213
|
+
"use", "science", "songbird", "symptom", "accumulation", "number", "suborder",
|
|
214
|
+
"falling", "management", "evaluation", "superorder", "activity", "interlacing",
|
|
215
|
+
"introduction",
|
|
184
216
|
]);
|
|
217
|
+
// A quantifier naming a share of a set ("either of two species", "one of
|
|
218
|
+
// several kinds") is never itself the class — it has no noun reading at all
|
|
219
|
+
// — so the of-chain reads through it the same way it reads through a
|
|
220
|
+
// classifier head, on to whatever the rest of the chain names.
|
|
221
|
+
const ISA_OF_QUANTIFIER = new Set(["either", "one", "each", "both", "any"]);
|
|
185
222
|
|
|
186
223
|
/** The isa lemma of a lead's first sentence, or null. The copula must sit in
|
|
187
|
-
* the first sentence;
|
|
188
|
-
*
|
|
189
|
-
*
|
|
190
|
-
*
|
|
224
|
+
* the first sentence; a parenthetical aside is skipped, not read as part of
|
|
225
|
+
* the class; an of-chain resolves to its final noun ("a kind of dog" →
|
|
226
|
+
* dog; "either of two species (…) of rodents" → rodent); a bare-plural
|
|
227
|
+
* predicate counts only when the head is an inflected plural the lexicon
|
|
228
|
+
* folds ("are animals that…" → animal, but "is light" stays null); a
|
|
229
|
+
* generic classifier head is no isa at all. */
|
|
230
|
+
// A classifier/quantifier/generic-head word can appear inflected ("kinds of
|
|
231
|
+
// dog", "sets of threads") — the sets above hold singular forms, so every
|
|
232
|
+
// membership check folds the word through the lexicon to its lemma first.
|
|
233
|
+
// An unresolvable word (the quantifiers are not nouns at all) falls back to
|
|
234
|
+
// itself unchanged, so the raw-string check still applies.
|
|
235
|
+
function classifierKeyFor(word, lexicon) {
|
|
236
|
+
const entry = lookupNoun(lexicon, word);
|
|
237
|
+
return entry ? normFactTerm(entry.lemma) : word;
|
|
238
|
+
}
|
|
239
|
+
|
|
191
240
|
export function isaOf(plain, lexicon) {
|
|
192
|
-
const m = String(plain ?? "").match(ISA_RE);
|
|
241
|
+
const m = withoutParentheticals(String(plain ?? "")).match(ISA_RE);
|
|
193
242
|
if (!m) return null;
|
|
194
243
|
const bare = !m[1];
|
|
195
244
|
const window = [];
|
|
@@ -201,14 +250,17 @@ export function isaOf(plain, lexicon) {
|
|
|
201
250
|
let headWords = window;
|
|
202
251
|
for (let ofIdx = headWords.indexOf("of"); ofIdx > 0; ofIdx = headWords.indexOf("of")) {
|
|
203
252
|
const outer = headWords[ofIdx - 1]?.split("-").pop();
|
|
204
|
-
|
|
253
|
+
const outerKey = outer && classifierKeyFor(outer, lexicon);
|
|
254
|
+
if (outerKey && (ISA_OF_READ_THROUGH.has(outerKey) || ISA_OF_QUANTIFIER.has(outerKey))) { headWords = headWords.slice(ofIdx + 1); continue; }
|
|
205
255
|
headWords = headWords.slice(0, ofIdx);
|
|
206
256
|
break;
|
|
207
257
|
}
|
|
208
258
|
while (headWords.length && ISA_ARTICLES.has(headWords[0])) headWords = headWords.slice(1);
|
|
209
259
|
if (!headWords.length) return null;
|
|
210
260
|
const headToken = headWords[headWords.length - 1].split("-").pop();
|
|
211
|
-
if (!headToken
|
|
261
|
+
if (!headToken) return null;
|
|
262
|
+
const headKey = classifierKeyFor(headToken, lexicon);
|
|
263
|
+
if (ISA_GENERIC_HEADS.has(headToken) || ISA_GENERIC_HEADS.has(headKey)) return null;
|
|
212
264
|
const entry = lookupNoun(lexicon, headToken);
|
|
213
265
|
if (!entry) return null;
|
|
214
266
|
const lemma = normFactTerm(entry.lemma);
|
|
@@ -25,8 +25,8 @@ import { SESSIONS_DIR_REL, appendSessionToGraph } from "./sessions.mjs";
|
|
|
25
25
|
import { uuidv7 } from "../adapters/uuid.mjs";
|
|
26
26
|
import { createTelemetry } from "./telemetry.mjs";
|
|
27
27
|
import * as defaultSource from "../adapters/source.mjs";
|
|
28
|
-
import { resolveExtensions, mergedLexiconExtra } from "./extensions.mjs";
|
|
29
|
-
import { runTurn, hasSeededVocabulary, vocabExampleHint } from "./chat.mjs";
|
|
28
|
+
import { resolveExtensions, mergedLexiconExtra, mergedLaneVocab, defaultCodeLaneVocab, activeDomainPacks, defaultCodeDomainPacks } from "./extensions.mjs";
|
|
29
|
+
import { runTurn, hasSeededVocabulary, vocabExampleHint, codeDomainActive as codeDomainActiveOf } from "./chat.mjs";
|
|
30
30
|
import { resolveGameConfig } from "../domain/game-config.mjs";
|
|
31
31
|
import { emptyRecord, resolveDiscourseConfig } from "../domain/discourse.mjs";
|
|
32
32
|
import { resolveResearchConfig } from "./research.mjs";
|
|
@@ -281,6 +281,36 @@ export async function createSession({
|
|
|
281
281
|
try { ({ entries: extEntries, biasByBundle } = await resolveExtensions(repo)); }
|
|
282
282
|
catch { extEntries = null; biasByBundle = {}; }
|
|
283
283
|
|
|
284
|
+
// The code-domain predicate (chat.mjs): a loaded graph with modules, or a
|
|
285
|
+
// code-domain bundle active in this repo's tmct.toml/persona (the `code`
|
|
286
|
+
// pack, or a direct `seon` activation predating it) — computed ONCE per
|
|
287
|
+
// session and threaded into every turn, so every code-vocabulary surface
|
|
288
|
+
// (counts, misses, banner, greeting, orientation, help) reads the same
|
|
289
|
+
// answer all session long.
|
|
290
|
+
const codePackActive = Boolean(extEntries?.get("seon")?.active || extEntries?.get("code")?.active);
|
|
291
|
+
const domainActive = codeDomainActiveOf({ graph, seonActive: codePackActive });
|
|
292
|
+
|
|
293
|
+
// This session's merged lane vocabulary (count nouns/class labels/help rows/
|
|
294
|
+
// the miss-recovery pointer) — extensions.mjs's mergedLaneVocab over every
|
|
295
|
+
// active pack. A real code graph with no pack formally active (a fixture, a
|
|
296
|
+
// provider-supplied graph, a repo indexed before this pack existed) still
|
|
297
|
+
// needs the code vocabulary to render today's answers, so it falls back to
|
|
298
|
+
// the shipped code pack's own vocabulary — the same "a real graph is
|
|
299
|
+
// enough" rule domainActive itself applies, just above. Empty when the
|
|
300
|
+
// domain is inactive: a bare session imports nothing and carries none of it.
|
|
301
|
+
let laneVocab = extEntries
|
|
302
|
+
? await mergedLaneVocab(extEntries, biasByBundle)
|
|
303
|
+
: { countNouns: {}, classLabels: {}, helpRows: [], missRecoveryPointer: "" };
|
|
304
|
+
if (domainActive && !Object.keys(laneVocab.countNouns).length) {
|
|
305
|
+
laneVocab = await defaultCodeLaneVocab();
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
// Which packs supplied that vocabulary, read from each pack's own
|
|
309
|
+
// declaration — what /capabilities names as this session's loaded domains.
|
|
310
|
+
// Same "a real graph is enough" fallback as the vocabulary itself.
|
|
311
|
+
let domainPacks = extEntries ? await activeDomainPacks(extEntries, biasByBundle) : [];
|
|
312
|
+
if (domainActive && !domainPacks.length) domainPacks = await defaultCodeDomainPacks();
|
|
313
|
+
|
|
284
314
|
// Load this handle's lexicon once, MERGED with any active lexicon/pack
|
|
285
315
|
// extension entries (ascending-bias merge order so a higher-bias bundle's
|
|
286
316
|
// same-lemma entry wins deterministically). Failure-tolerated — a broken
|
|
@@ -403,8 +433,13 @@ export async function createSession({
|
|
|
403
433
|
const bannerLines = [
|
|
404
434
|
noCodeGraph
|
|
405
435
|
// No code graph: honest, orienting messaging — never an error before the prompt.
|
|
406
|
-
|
|
407
|
-
|
|
436
|
+
// domainActive here means the seon bundle is active without an index yet
|
|
437
|
+
// (e.g. --with-persona code, before `tmct index` runs) — same code-graph
|
|
438
|
+
// framing as today. With no code domain at all, naming "no code graph" would
|
|
439
|
+
// itself be a code-domain leak, so a bare install gets a neutral line instead.
|
|
440
|
+
? (domainActive
|
|
441
|
+
? `tmct chat — ${readRepo} — no code graph loaded — ${empty ? "starting empty" : "graph has no code entities"}; ${whereItGoes}`
|
|
442
|
+
: `tmct chat — ${readRepo} — ${whereItGoes}`)
|
|
408
443
|
: `tmct chat — ${readRepo} — ${moduleCount} module(s) — `
|
|
409
444
|
+ (discardsWrites ? `${whereItGoes}` : `log ${logFile}`),
|
|
410
445
|
// the honest seed line appears ONLY on the run that actually seeded — the count
|
|
@@ -415,8 +450,14 @@ export async function createSession({
|
|
|
415
450
|
// example), and at what IS answerable now — `vocabHint` is only ever a term
|
|
416
451
|
// confirmed to resolve in THIS session's actual seed state (see vocabExampleHint),
|
|
417
452
|
// never a hardcoded example that might not have been seeded. tmct can index a
|
|
418
|
-
// repo itself (`tmct index`) or read a graph any other producer wrote.
|
|
419
|
-
|
|
453
|
+
// repo itself (`tmct index`) or read a graph any other producer wrote. With no
|
|
454
|
+
// code domain active, the code-structure pointer has nothing to point at, so
|
|
455
|
+
// this line carries just the teach/vocabulary hint. The pointer text itself
|
|
456
|
+
// is the active code pack's own miss-recovery clause (laneVocab, above) —
|
|
457
|
+
// never a chat-session.mjs literal, so a host pack can carry its own.
|
|
458
|
+
...(noCodeGraph ? [domainActive
|
|
459
|
+
? `${laneVocab.missRecoveryPointer} ${vocabHint}`
|
|
460
|
+
: vocabHint] : []),
|
|
420
461
|
"pass --repo <path> to target a different repo",
|
|
421
462
|
"ask a question, or /help for commands (/stats for an overview) — /exit to leave",
|
|
422
463
|
];
|
|
@@ -462,7 +503,7 @@ export async function createSession({
|
|
|
462
503
|
async turn(line) {
|
|
463
504
|
let result;
|
|
464
505
|
try {
|
|
465
|
-
result = await runTurn(line, { config, source, graph, focus, last, memoryDir, sessionId, env, lexicon, narrate: narrateOn, liveReference: liveReferenceOn, vocabHint, tel, biasByBundle, planState, gameConfig, researchState, researchConfig, discourse: discourseRecord, actingSubject });
|
|
506
|
+
result = await runTurn(line, { config, source, graph, focus, last, memoryDir, sessionId, env, lexicon, narrate: narrateOn, liveReference: liveReferenceOn, vocabHint, tel, biasByBundle, planState, gameConfig, researchState, researchConfig, discourse: discourseRecord, actingSubject, codeDomainActive: domainActive, laneVocab, domainPacks });
|
|
466
507
|
} catch (e) {
|
|
467
508
|
const ts = new Date().toISOString();
|
|
468
509
|
const message = e instanceof Error ? e.message : String(e);
|