@polycode-projects/the-mechanical-code-talker 5.0.21 → 5.0.23

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 CHANGED
@@ -1293,6 +1293,10 @@ source = "wikipedia" # wikipedia | wikidata — where "research <topic>"
1293
1293
  [discourse]
1294
1294
  max_referents = 12 # how many referents stay resolvable across turns
1295
1295
 
1296
+ # Goal recognition (src/domain/router/recognize.mjs).
1297
+ [recognition]
1298
+ max_trace = 32 # how many observed steps a recognition reads back
1299
+
1296
1300
  [telemetry]
1297
1301
  enabled = false # local-only counters; never phones home
1298
1302
 
package/bin/tmct.mjs CHANGED
@@ -603,13 +603,14 @@ async function writeStandaloneViewPage(archetype, rest) {
603
603
  const engineBundleJs = await buildEngineBundleJs("build-sprites-bundle.mjs");
604
604
  const { readSpriteTemplateFiles } = await import("../src/adapters/corpus/sprite-template-files.mjs");
605
605
  const { readSpriteLargeTemplateFiles } = await import("../src/adapters/corpus/sprite-large-template-files.mjs");
606
- const { loadSpriteOntologyFactRows, renderSpriteCatalogHtml } = await import("../src/services/sprite-catalog-viz.mjs");
606
+ const { loadSpriteOntologyFactRows, loadAdventureSceneRoomClasses, renderSpriteCatalogHtml } = await import("../src/services/sprite-catalog-viz.mjs");
607
607
  html = renderSpriteCatalogHtml({
608
608
  iconTemplates: readSpriteTemplateFiles(),
609
609
  largeTemplates: readSpriteLargeTemplateFiles(),
610
610
  factRows: await loadSpriteOntologyFactRows(),
611
611
  spritesBundleAvailable: true,
612
612
  engineBundleJs,
613
+ adventureRoomClasses: await loadAdventureSceneRoomClasses(),
613
614
  });
614
615
  } else if (archetype === "spider-fly") {
615
616
  const engineBundleJs = await buildEngineBundleJs("build-spider-fly-bundle.mjs");
@@ -1595,7 +1596,7 @@ async function main() {
1595
1596
  "graph-query tool calls for a compound or maintenance-goal request.\n\n" +
1596
1597
  "Usage:\n" +
1597
1598
  " tmct plan \"<request>\" [--repo <abs>] [--graph <path>] [--config <path>]\n" +
1598
- " [--tools <name,name,...>] [--json]\n\n" +
1599
+ " [--tools <name,name,...>] [--json] [--goals]\n\n" +
1599
1600
  " --repo <abs> target a repo's graph (<abs>/.tmct/graph.json); default: git root/cwd\n" +
1600
1601
  " --graph <path> explicit graph file (repeatable — multiple graphs merge)\n" +
1601
1602
  " --config <path> an alternate tmct.toml location (a file or a directory)\n" +
@@ -1604,7 +1605,9 @@ async function main() {
1604
1605
  " registered capability\n" +
1605
1606
  " --json print the full machine-readable loop result instead of\n" +
1606
1607
  " the human-readable report (a non-zero exit still means\n" +
1607
- " an honest refusal, not a crash)\n\n" +
1608
+ " an honest refusal, not a crash)\n" +
1609
+ " --goals print the declared goals a trace is recognized against,\n" +
1610
+ " with where each came from, and exit\n\n" +
1608
1611
  "Examples:\n" +
1609
1612
  " tmct plan \"of the modules impacted by src/lib/http.mjs, which are untested\"\n" +
1610
1613
  " tmct plan \"what most needs a test in this codebase\"\n" +
@@ -1615,6 +1618,7 @@ async function main() {
1615
1618
  const { strFlag, resolveRuntimeConfig } = await import("../src/services/cli-args.mjs");
1616
1619
  const toolsFlag = strFlag(rest, ["--tools"]);
1617
1620
  const jsonFlag = rest.includes("--json");
1621
+ const goalsFlag = rest.includes("--goals");
1618
1622
  // The request is every argv token that isn't a recognized flag or its value,
1619
1623
  // rejoined with single spaces — so both `tmct plan "quoted request"` and a
1620
1624
  // bare `tmct plan unquoted request words` work.
@@ -1623,11 +1627,11 @@ async function main() {
1623
1627
  for (let i = 0; i < rest.length; i += 1) {
1624
1628
  const a = rest[i];
1625
1629
  if (FLAG_WITH_VALUE.has(a)) { i += 1; continue; }
1626
- if (a === "--json") continue;
1630
+ if (a === "--json" || a === "--goals") continue;
1627
1631
  requestParts.push(a);
1628
1632
  }
1629
1633
  const request = requestParts.join(" ").trim();
1630
- if (!request) {
1634
+ if (!request && !goalsFlag) {
1631
1635
  process.stderr.write("tmct plan: needs a request, e.g. `tmct plan \"of the modules impacted by X, which are untested\"`\n");
1632
1636
  process.exit(2);
1633
1637
  }
@@ -1661,6 +1665,17 @@ async function main() {
1661
1665
  process.exit(2);
1662
1666
  }
1663
1667
  }
1668
+ if (goalsFlag) {
1669
+ const { declaredGoals } = await import("../src/domain/router/recognize.mjs");
1670
+ const { renderDeclaredGoals } = await import("../src/services/chat.mjs");
1671
+ const { loadMemory, readFactRows, readRuleRows } = await import("../src/adapters/memory/core.mjs");
1672
+ const memory = await loadMemory(memoryDir);
1673
+ const goals = declaredGoals({ worldRows: readFactRows(memory), ruleRows: readRuleRows(memory) }, { tools });
1674
+ process.stdout.write(renderDeclaredGoals(goals) + "\n");
1675
+ await closeMemoryStore();
1676
+ for (const dispose of ctx.disposers || []) dispose();
1677
+ return;
1678
+ }
1664
1679
  try {
1665
1680
  const result = await runCapabilityPlan(request, tools, ctx);
1666
1681
  if (jsonFlag) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@polycode-projects/the-mechanical-code-talker",
3
- "version": "5.0.21",
3
+ "version": "5.0.23",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "description": "The Mechanical Code Talker (tmct) — a tolerant, offline, $0 chat surface that guides you toward precision queries about a software repository. ELIZA/PARRY-style but domain-obsessed with code. No model calls; indexes a repo on request (tmct index) or reads any producer's graph.",
@@ -147,6 +147,11 @@ export async function normalizeConfig(raw, { configDir } = {}) {
147
147
  // through; default-filling is resolveDiscourseConfig's job.
148
148
  if (src.discourse !== undefined) cfg.discourse = src.discourse;
149
149
 
150
+ // Recognition knob (src/domain/router/recognize.mjs): sparse PASS-THROUGH,
151
+ // the raw `[recognition]` table (max_trace) rides through; default-filling
152
+ // is resolveRecognitionConfig's job.
153
+ if (src.recognition !== undefined) cfg.recognition = src.recognition;
154
+
150
155
  const idx = src.index || {};
151
156
  const index = {};
152
157
  if (idx.languages !== undefined) index.languages = idx.languages;
@@ -172,6 +172,7 @@ export const CLI_VERBS = [
172
172
  { flag: "[--config <path>]", prose: ["untested\", \"what most needs a test\") — a real STRIPS/"] },
173
173
  { flag: "[--tools <a,b,...>]", prose: ["PDDL planner (src/domain/router/*), never a guessed call."] },
174
174
  { flag: "[--json]", prose: ["Prints the grounded step sequence + composed answer,", "or an honest \"no plan found\". --tools restricts the", "declared toolset; --json prints the full loop result."] },
175
+ { flag: "[--goals]", prose: ["print the declared goals a trace is recognized", "against, with where each came from, and exit"] },
175
176
  ],
176
177
  },
177
178
  {
@@ -197,6 +197,14 @@ const domainIndividuals = (domain) => {
197
197
  return out;
198
198
  };
199
199
 
200
+ /** `{ base, step }` for an @stepN-stamped subject, or null for a bare one —
201
+ * the one parser for the board's own snapshot grammar, so a caller outside
202
+ * this file never keeps a second copy of the regex. */
203
+ export function parseStepSubject(subject) {
204
+ const m = SNAPSHOT_RE.exec(String(subject || ""));
205
+ return m ? { base: m[1], step: Number(m[2]) } : null;
206
+ }
207
+
200
208
  /** The current state as canonical sorted rows over the domain's dynamic
201
209
  * predicates. Prefers the newest @stepN snapshot when one exists, so a
202
210
  * re-plan after per-step execution never reads the stale step-0 board. */
@@ -173,6 +173,10 @@ const isListingRemainder = (rest) => {
173
173
  return LISTING_TAIL_KINDS.has((words[words.length - 1] || "").toLowerCase());
174
174
  };
175
175
 
176
+ // The punctuation set every delimiter-anchored preamble/filler frame in this
177
+ // file treats as a clause boundary.
178
+ const DELIM_SRC = "[,.;:!]|—|–|-";
179
+
176
180
  /** Greeting lead-in with a delimiter (+ optional "quick question" bridge):
177
181
  * "hey there, quick question - <Q>" -> "<Q>". Delimiter and non-empty
178
182
  * remainder are both required, so a bare greeting stays small-talk. */
@@ -186,7 +190,14 @@ const THANKS_PREAMBLE_RE = /^(?:thanks|thank\s+you|many\s+thanks|thx|ty|cheers)(
186
190
  * different wording — a throwaway counting aside before the real content,
187
191
  * never part of the content itself ("ok, one more, teach me: no server is a
188
192
  * client" must peel exactly as "ok cool, <Q>" already does). */
189
- const ACK_PREAMBLE_RE = /^(?:(?:ok(?:ay)?|aight|cool|alright|sure|right|fine|great|nice|got it|gotcha|sounds good|no worries|no problem|(?:just\s+)?(?:one|another)\s+more|another\s+one)[\s,]+)+(.+)$/i;
193
+ const ACK_WORDS_SRC = "ok(?:ay)?|aight|cool|alright|sure|right|fine|great|nice|got it|gotcha|sounds good|no worries|no problem|another\\s+one";
194
+ // The counting arm ("one more", "just one more") requires a trailing
195
+ // delimiter, unlike the ack words above — without it, "one more disk rests
196
+ // on peg-a" loses its own quantifier to this frame.
197
+ const ACK_COUNTING_SRC = "(?:just\\s+)?(?:one|another)\\s+more";
198
+ const ACK_PREAMBLE_RE = new RegExp(
199
+ `^(?:(?:${ACK_WORDS_SRC})[\\s,]+|(?:${ACK_COUNTING_SRC})\\s*(?:${DELIM_SRC})\\s*)+(.+)$`, "i",
200
+ );
190
201
  /** Self-orientation lead-in with a delimiter — "just poking around, <Q>",
191
202
  * "first time using this, <Q>". */
192
203
  const BROWSING_PREAMBLE_RE = /^(?:just\s+(?:poking\s+around|looking\s+around|browsing|exploring|checking\s+(?:this|it)\s+out)|first\s+time\s+(?:trying\s+this\s+out|using\s+this|here)|i'?m\s+new\s+(?:here|around\s+here|to\s+(?:this|all\s+this)(?:\s+(?:repo|codebase|project|app|tool|thing))?))\s*[,.—–-]\s*(.+)$/i;
@@ -342,6 +353,80 @@ export function applyPreambleFrames(text) {
342
353
  return q;
343
354
  }
344
355
 
356
+ // ---- closed FILLER-CLAUSE prefixes: sentence-initial small talk ahead of a
357
+ // real question, distinct from the PREAMBLE frames above because family B
358
+ // (hesitation + pivot) needs no delimiter — an unconditional peel here would
359
+ // change what every existing normalizeQuery caller sees, so this is exposed
360
+ // as its own function and consumed only where a caller opts in explicitly.
361
+
362
+ const alt = (list) => [...list].sort((a, b) => b.length - a.length).map(escapeRegex).join("|");
363
+
364
+ // Family A — interjection + comment. A delimiter is required: a bare
365
+ // interjection followed by a noun ("oh water") is ordinary content.
366
+ const FILLER_INTERJECTIONS = Object.freeze([
367
+ "oh", "ah", "aha", "ooh", "ohh", "wow", "huh", "hm", "hmm", "mm", "mmm",
368
+ "heh", "ha", "yay", "ugh", "oof", "phew", "whoa", "oops", "yikes", "oi",
369
+ ]);
370
+ const FILLER_COMMENT_WORDS = Object.freeze([
371
+ "nice", "cool", "great", "neat", "lovely", "interesting", "good", "ok", "okay",
372
+ "fair enough", "makes sense", "i see", "got it", "fine",
373
+ ]);
374
+
375
+ // Family B — hesitation + pivot. Repeatable, delimiter optional. Deliberately
376
+ // excludes "like"/"so"/"now"/"then"/"right"/"ok": those either carry real
377
+ // content ("like what?") or already peel through LEADING_CONNECTIVE_RE /
378
+ // ACK_PREAMBLE_RE, and duplicating them here would give the same word two
379
+ // competing owners.
380
+ const FILLER_HESITATIONS = Object.freeze([
381
+ "um", "umm", "uhm", "uh", "uhh", "er", "err", "erm", "ehm", "eh",
382
+ "well", "anyway", "anyhow", "anyways", "regardless", "moving on",
383
+ ]);
384
+
385
+ // Family C — meta-announcement: a self-describing announcement of the
386
+ // question about to come. Delimiter required.
387
+ const FILLER_ANNOUNCEMENTS = Object.freeze([
388
+ "quick question", "quick q", "one quick question", "just a quick question",
389
+ "random question", "random thing", "one more random thing", "another random thing",
390
+ "one more thing", "one last thing", "last thing", "one more question",
391
+ "side note", "side question", "sidebar", "off topic", "unrelated",
392
+ "on a different note", "different topic", "changing the subject", "new topic",
393
+ "just curious", "out of curiosity", "curious", "genuinely curious",
394
+ "while i'm here", "while i have you", "before i forget",
395
+ ]);
396
+
397
+ const FILLER_A_SRC = `(?:${alt(FILLER_INTERJECTIONS)})(?:\\s+(?:${alt(FILLER_COMMENT_WORDS)}))?\\s*(?:${DELIM_SRC})\\s*`;
398
+ const FILLER_B_SRC = `(?:${alt(FILLER_HESITATIONS)})\\s*(?:(?:${DELIM_SRC})\\s*|\\s+)`;
399
+ const FILLER_C_SRC = `(?:${alt(FILLER_ANNOUNCEMENTS)})\\s*(?:${DELIM_SRC})\\s*`;
400
+
401
+ // An optional leading "sorry"/"sorry," ahead of any family, once.
402
+ const FILLER_CLAUSE_PREFIX_RE = new RegExp(
403
+ `^(?:sorry\\s*(?:${DELIM_SRC})?\\s*)?(?:${FILLER_A_SRC}|${FILLER_B_SRC}|${FILLER_C_SRC})+`, "i",
404
+ );
405
+
406
+ /** Peel a sentence-initial closed filler clause off `text`. Returns
407
+ * { prefix, remainder } or null when nothing in the closed inventory leads
408
+ * the line. Longest-alternative-first inside each family, so "one more
409
+ * random thing" wins over "one more thing". Pure; no graph, no store. */
410
+ export function fillerClausePrefix(text) {
411
+ const line = String(text || "").trim();
412
+ const m = line.match(FILLER_CLAUSE_PREFIX_RE);
413
+ if (!m || !m[0]) return null;
414
+ const remainder = line.slice(m[0].length).trim();
415
+ if (!remainder) return null; // a bare filler is small talk, not a peel
416
+ return { prefix: m[0].trim(), remainder };
417
+ }
418
+
419
+ /** QUESTION_LEAD_RE is anchored to the FIRST word, so a closed filler clause
420
+ * in front of a question moves the interrogative off word one and every
421
+ * write gate reads the line as a declarative. Peel once, then re-check the
422
+ * same question underneath. */
423
+ export function leadsInterrogative(text) {
424
+ const line = String(text || "").trim();
425
+ if (QUESTION_LEAD_RE.test(line)) return true;
426
+ const peel = fillerClausePrefix(line);
427
+ return Boolean(peel && QUESTION_LEAD_RE.test(peel.remainder));
428
+ }
429
+
345
430
  /** Strippable leading framing clause: "since/[even] though/although/while/
346
431
  * because/[even] if/whereas/given that/now that <clause>, <Q>" -> "<Q>".
347
432
  * Comma-anchored and non-empty-remainder-required, same discipline as
@@ -40,10 +40,12 @@ import {
40
40
  compileDomain, stateFromFacts, stateKeyFor, movesFromRules, compileGoal, PlanBudgetError,
41
41
  } from "../domain.mjs";
42
42
  import { findActionPath } from "../planning.mjs";
43
+ import { traceOfCalls, declaredGoals, recognizeGoal } from "./recognize.mjs";
43
44
 
44
45
  export const ROUTER_DRIVER = "resolver-0.8.0";
45
46
  export const GOAL_DRIVER = "goal-0.8.1";
46
47
  export const TAUGHT_DRIVER = "taught-0.1.0";
48
+ export const RECOGNIZE_DRIVER = "recognize-0.1.0";
47
49
 
48
50
  /** Every registered capability's name — the default declared toolset for a
49
51
  * caller that doesn't want to hand-pick a subset. */
@@ -259,13 +261,52 @@ export async function runTaughtPlan(request, tools, ctx) {
259
261
  };
260
262
  }
261
263
 
264
+ // ---- the recognition lane -----------------------------------------------------
265
+
266
+ /** Recognize what an observed trace was doing, as a loopResult the same graders
267
+ * and surfaces read. `ctx.trace` is the observed step source: an array of
268
+ * { name, input } calls (a runCapabilityPlan result, or a transcript's
269
+ * tool_use blocks). Makes no calls and writes nothing — recognition never
270
+ * changes what it is recognizing. */
271
+ export async function runRecognition(tools, ctx) {
272
+ const trace = traceOfCalls(ctx.trace || []);
273
+ const goals = declaredGoals(ctx, { tools });
274
+ const r = recognizeGoal(trace, goals);
275
+ return {
276
+ calls: [], terminated: true, driver: RECOGNIZE_DRIVER,
277
+ refused: r.goal === null,
278
+ proof: r.proof, why: r.why,
279
+ ...(r.goal ? { inferredGoal: r.goal.id } : {}),
280
+ ...(r.reject ? { reject: true } : {}),
281
+ ...(r.ambiguous ? { ambiguousGoals: r.ambiguous.map((g) => g.id) } : {}),
282
+ };
283
+ }
284
+
285
+ /** A ctx carrying an observed trace also gets a `recognition` field on the
286
+ * result: what the trace fits, or the reject class. Additive — it never
287
+ * changes calls, refused, or why, so a caller that ignores it sees exactly
288
+ * the loop result it saw before. */
289
+ async function withRecognition(result, tools, ctx) {
290
+ if (!Array.isArray(ctx.trace) || !ctx.trace.length) return result;
291
+ const r = await runRecognition(tools, ctx);
292
+ return {
293
+ ...result,
294
+ recognition: {
295
+ inferredGoal: r.inferredGoal ?? null,
296
+ reject: r.reject === true,
297
+ ambiguousGoals: r.ambiguousGoals ?? null,
298
+ why: r.why,
299
+ },
300
+ };
301
+ }
302
+
262
303
  /** The full drive: resolver/planner first; a refusal there falls through to
263
304
  * the taught world-goal lane (runTaughtPlan, above), and only a request that
264
305
  * is not a world goal escalates to the closed-world goal-reasoner. Mirrors
265
306
  * test-benchmarks/agentbench's driver-resolver.mjs + driver-goal.mjs composition, with no
266
307
  * test-benchmarks/agentbench/ dependency (test-benchmarks/agentbench/ is dev-only, never shipped). Returns a
267
308
  * loopResult:
268
- * `{ calls, refused, terminated, proof, why, driver, composed?, observed?, candidateResults? }`.
309
+ * `{ calls, refused, terminated, proof, why, driver, composed?, observed?, candidateResults?, recognition? }`.
269
310
  *
270
311
  * A C1 refusal that already carries `candidateResults` (the tied-candidate
271
312
  * composer's enumerate-or-refuse answer) is TERMINAL — it stands as-is rather
@@ -275,15 +316,15 @@ export async function runTaughtPlan(request, tools, ctx) {
275
316
  * plainer refuse with no candidates at all). */
276
317
  export async function runCapabilityPlan(request, tools, ctx) {
277
318
  const c1 = await runResolverPlan(request, tools, ctx);
278
- if (!c1.refused || c1.candidateResults) return c1;
319
+ if (!c1.refused || c1.candidateResults) return withRecognition(c1, tools, ctx);
279
320
  const taught = await runTaughtPlan(request, tools, ctx);
280
- if (taught) return taught.refused ? { ...taught, c1Why: c1.why } : taught;
321
+ if (taught) return withRecognition(taught.refused ? { ...taught, c1Why: c1.why } : taught, tools, ctx);
281
322
  const c2 = await goalReason(request, tools, ctx, { driver: GOAL_DRIVER });
282
323
  // Both stages refused: carry the resolver/planner's own reason alongside the
283
324
  // goal-reasoner's so a caller can show why the direct route AND the
284
325
  // maintenance-goal route both declined,
285
326
  // rather than only the last (often less specific) of the two.
286
- return c2.refused ? { ...c2, c1Why: c1.why } : c2;
327
+ return withRecognition(c2.refused ? { ...c2, c1Why: c1.why } : c2, tools, ctx);
287
328
  }
288
329
 
289
330
  /** Build a real `{ dispatch, resolve, graph, config }` context against a repo's