@polycode-projects/the-mechanical-code-talker 5.0.34 → 5.0.36

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.
@@ -179,6 +179,131 @@ export function parseAgentEditorText(text) {
179
179
  return { triples, unrecognized };
180
180
  }
181
181
 
182
+ // ---- asking ----------------------------------------------------------------
183
+ //
184
+ // The read side of the same table. `AGENT_LINE_PATTERNS` states each trait as
185
+ // a sentence; the entries below ask for one back, in the two shapes a
186
+ // relation has: FORWARD names the subject and asks for its objects ("what
187
+ // does the fox eat"), INVERSE names the object and asks which subjects state
188
+ // it ("who eats the goat"). Only the four relational predicates take an
189
+ // inverse — "who weighs 8" is not a question the actor card has a sentence
190
+ // for in either direction.
191
+ //
192
+ // `mgx:display-name` and `mgx:is-predator` carry no ask entry: their plain
193
+ // English phrasings ("what is the fox's name", "is the fox a predator") are
194
+ // the relation lane's and the type-check lane's own, and both already answer
195
+ // them from the store.
196
+
197
+ const AGENT_TRAIT_ASK_PATTERNS = [
198
+ { predicate: "mgx:consumes", direction: "forward",
199
+ re: /^(?:what|who)\s+does\s+(?:the\s+|a\s+|an\s+)?(.+?)\s+(?:want\s+to\s+)?eat[?.!\s]*$/i,
200
+ gap: (t) => `what ${t} eats` },
201
+ { predicate: "mgx:consumes", direction: "inverse",
202
+ re: /^(?:what|who)\s+eats\s+(?:the\s+|a\s+|an\s+)?(.+?)[?.!\s]*$/i,
203
+ gap: (t) => `who eats ${t}` },
204
+ { predicate: "mgx:evades", direction: "forward",
205
+ re: /^(?:what|who)\s+does\s+(?:the\s+|a\s+|an\s+)?(.+?)\s+evade[?.!\s]*$/i,
206
+ gap: (t) => `what ${t} evades` },
207
+ { predicate: "mgx:evades", direction: "inverse",
208
+ re: /^(?:what|who)\s+evades\s+(?:the\s+|a\s+|an\s+)?(.+?)[?.!\s]*$/i,
209
+ gap: (t) => `who evades ${t}` },
210
+ { predicate: "mgx:guards", direction: "forward",
211
+ re: /^(?:what|who)\s+does\s+(?:the\s+|a\s+|an\s+)?(.+?)\s+guard[?.!\s]*$/i,
212
+ gap: (t) => `what ${t} guards` },
213
+ { predicate: "mgx:guards", direction: "inverse",
214
+ re: /^(?:what|who)\s+guards\s+(?:the\s+|a\s+|an\s+)?(.+?)[?.!\s]*$/i,
215
+ gap: (t) => `who guards ${t}` },
216
+ { predicate: "mgx:pursues", direction: "forward",
217
+ re: /^(?:what|who)\s+does\s+(?:the\s+|a\s+|an\s+)?(.+?)\s+(?:pursue|chase)[?.!\s]*$/i,
218
+ gap: (t) => `what ${t} pursues` },
219
+ { predicate: "mgx:pursues", direction: "inverse",
220
+ re: /^(?:what|who)\s+(?:pursues|chases)\s+(?:the\s+|a\s+|an\s+)?(.+?)[?.!\s]*$/i,
221
+ gap: (t) => `who pursues ${t}` },
222
+ { predicate: "mgx:hasMass", direction: "forward",
223
+ re: /^(?:how\s+much|what)\s+does\s+(?:the\s+|a\s+|an\s+)?(.+?)\s+weigh[?.!\s]*$/i,
224
+ gap: (t) => `what ${t} weighs` },
225
+ { predicate: "mgx:mass-drain-per-turn", direction: "forward",
226
+ re: /^(?:how\s+much|what)\s+does\s+(?:the\s+|a\s+|an\s+)?(.+?)\s+lose\s+each\s+turn[?.!\s]*$/i,
227
+ gap: (t) => `what ${t} loses each turn` },
228
+ { predicate: "mgx:vision-radius", direction: "forward",
229
+ re: /^how\s+(?:far|many\s+cells)\s+can\s+(?:the\s+|a\s+|an\s+)?(.+?)\s+see[?.!\s]*$/i,
230
+ gap: (t) => `how far ${t} sees` },
231
+ { predicate: "mgx:model", direction: "forward",
232
+ re: /^(?:what|who)\s+does\s+(?:the\s+|a\s+|an\s+)?(.+?)\s+look\s+like[?.!\s]*$/i,
233
+ gap: (t) => `what ${t} looks like` },
234
+ ];
235
+
236
+ /** One question -> `{ predicate, direction, term, gapPhrase }`, or null when
237
+ * nothing in the ask table recognizes it. `gapPhrase` is the entry's own
238
+ * wording for what went unanswered, so a miss says which question missed
239
+ * rather than restating the raw predicate. Pure. */
240
+ export function parseAgentTraitAsk(line) {
241
+ const trimmed = String(line || "").trim();
242
+ if (!trimmed) return null;
243
+ for (const pattern of AGENT_TRAIT_ASK_PATTERNS) {
244
+ const m = trimmed.match(pattern.re);
245
+ if (!m) continue;
246
+ const term = LOW(m[1]);
247
+ if (!term) continue;
248
+ return { predicate: pattern.predicate, direction: pattern.direction, term, gapPhrase: pattern.gap(term) };
249
+ }
250
+ return null;
251
+ }
252
+
253
+ /** Whether `rows` state anything trait-shaped about `term` at all — as the
254
+ * subject of a trait row anywhere on its own class chain, or as the object
255
+ * of one. This is the gate on the whole ask family: a store that holds no
256
+ * agent traits for the term is not this table's to answer, so the question
257
+ * falls back to whatever lane owned it before. Pure. */
258
+ export function namesAgentTrait(rows, term) {
259
+ const chain = new Set(classChainOf(rows, term));
260
+ return (rows || []).some((r) => AGENT_TRAIT_PREDICATES.includes(r.predicate)
261
+ && (chain.has(r.subject) || r.object === term));
262
+ }
263
+
264
+ /** The rows answering a forward ask: `subject`'s own if it declares any,
265
+ * otherwise the nearest class link that does — the same nearest-first walk
266
+ * `traitValuesOf` resolves a value through, kept as ROWS so the answer can
267
+ * cite each one. Empty when no chain link declares the predicate. Pure. */
268
+ function traitRowsOnChain(rows, subject, predicate) {
269
+ for (const node of classChainOf(rows, subject)) {
270
+ const hits = (rows || []).filter((r) => r.subject === node && r.predicate === predicate);
271
+ if (hits.length) return hits.slice().sort(compareTraitHits);
272
+ }
273
+ return [];
274
+ }
275
+
276
+ const compareTraitHits = (a, b) => {
277
+ if (a.subject !== b.subject) return a.subject < b.subject ? -1 : 1;
278
+ return a.object < b.object ? -1 : a.object > b.object ? 1 : 0;
279
+ };
280
+
281
+ const defaultCite = (row) => (row.provenance ? ` (source: ${row.provenance})` : "");
282
+
283
+ /** Answer a parsed ask against `rows`: `{ text, miss, matches }`, or null
284
+ * when the store states no trait about the term at all (the caller keeps
285
+ * the line). A hit names the answer first and then every row it stands on,
286
+ * each cited; a subject with nothing stated under that predicate gets the
287
+ * gap said out loud, never a default filled in for it. An answer resolved
288
+ * off a class rather than the named instance says which class carried it.
289
+ * `cite` renders one row's citation, so a caller can normalize provenance
290
+ * its own way. Pure. */
291
+ export function answerAgentTraitAsk(rows, ask, { cite = defaultCite } = {}) {
292
+ if (!ask || !namesAgentTrait(rows, ask.term)) return null;
293
+ const hits = ask.direction === "forward"
294
+ ? traitRowsOnChain(rows, ask.term, ask.predicate)
295
+ : (rows || []).filter((r) => r.predicate === ask.predicate && r.object === ask.term).sort(compareTraitHits);
296
+ if (!hits.length) return { text: `nothing on record says ${ask.gapPhrase}.`, miss: true, matches: [] };
297
+
298
+ const named = [...new Set(hits.map((r) => (ask.direction === "forward" ? r.object : r.subject)))].sort();
299
+ const answeredBy = hits[0].subject;
300
+ const lead = ask.direction === "forward" && answeredBy !== ask.term
301
+ ? `from ${ask.term}'s ${answeredBy} class: `
302
+ : "";
303
+ const lines = hits.map((r) => `${agentTraitSentence(r.subject, r.predicate, r.object).replace(/\.$/, "")}${cite(r)}`);
304
+ return { text: `${named.join(", ")} — ${lead}${lines.join("; ")}.`, miss: false, matches: hits };
305
+ }
306
+
182
307
  // ---- sync planning ---------------------------------------------------------
183
308
 
184
309
  const tripleKey = (t) => `${t.subject} ${t.predicate} ${t.object}`;