@polycode-projects/the-mechanical-code-talker 0.7.1 → 0.8.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.
@@ -0,0 +1,293 @@
1
+ // src/router/resolver.mjs — Stage 1 of the capability router (PLAN_CAPABILITY_ROUTER.md):
2
+ // THE RESOLVER. Turn a request into a SELECTED registry capability with bound
3
+ // arguments, by unification + backward chaining over capabilities-as-facts
4
+ // (a mini-Datalog/SLD step, exactly the "open-condition satisfaction" the plan
5
+ // names). Deterministic, no-LLM, glass-box: every choice is provable.
6
+ //
7
+ // THE CORE is the ask-kind -> capability MAPPING. A request becomes an epistemic
8
+ // GOAL `(knows <topic> ?of)`; each capability's add-effect declares which topic
9
+ // it achieves (registry.mjs `knows(topic, of)`); backward chaining finds the
10
+ // capability whose add-list unifies with the goal and binds ?of to the request's
11
+ // object term. Three fact sources feed the SAME backward-chaining step:
12
+ //
13
+ // 1. THE COMMAND REGISTER (server-http.mjs `selectTool`, the terse verbs
14
+ // "describe X" / "callers X" / "untested") — exact + unambiguous, tried
15
+ // FIRST because a terse command mis-parses through the NL grammar (e.g.
16
+ // "callees Widget.render" keyword-spots to shape:reverse/kind:calls, which
17
+ // would wrongly route to callers). A literal command verb is ground truth.
18
+ // 2. THE NL PARSE (ask.mjs `parseQuery` -> {shape, kind, entityType, object}) —
19
+ // the relational grammar ("which functions call X", "what does X export").
20
+ // NL_INTENTS maps a {shape,kind} to the epistemic TOPIC; backwardChain maps
21
+ // the topic to the capability. This is the Stage-1 deliverable proper.
22
+ // 3. IMPERATIVE INTENT FRAMES (Stage 2, this module's FRAMES table) — curated
23
+ // phrasings the relational grammar does not carry ("blast radius of X",
24
+ // "who calls X", "search for X"): a regex -> {topic, arg}. Same backward
25
+ // chaining (topic -> capability), same resolveObject binding.
26
+ //
27
+ // ENTITY BINDING is DELEGATED to `resolveObject` (ask.mjs — the tiered lemma/
28
+ // fuzzy binding oracle with honest ambiguity). This module NEVER re-implements
29
+ // resolution: the registry's `resolves(param, as)` precondition maps exactly to
30
+ // a resolveObject call, and an ambiguous / no-match term is an HONEST REFUSE,
31
+ // never a guess. On any no-fit -> refuse.
32
+ //
33
+ // Pure-ish: mapParse/mapFrame/backwardChain/commandCapability are pure; resolveOne
34
+ // is async only because it consults ctx.resolve (the graph binding oracle) and
35
+ // ctx.dispatch (executes the grounded call). No network, no Date.now.
36
+
37
+ import { parseQuery } from "../ask.mjs";
38
+ import { selectTool } from "../server-http.mjs";
39
+ import {
40
+ capabilities, capabilityByName, preconditionsOf, effectsOf, PRECOND,
41
+ } from "./registry.mjs";
42
+ import { hallucinationsIn } from "../../agentbench/grade.mjs";
43
+
44
+ // ---- the ask-kind -> epistemic-topic MAPPING (the Stage-1 core) --------------
45
+ // Keyed `${shape}:${kind}` off parseQuery's simple-clause output. The VALUE is
46
+ // the epistemic TOPIC a capability's add-effect must achieve; backwardChain then
47
+ // unifies that topic with the registry (capabilities-as-facts). Every entry's
48
+ // topic MUST be achievable by exactly one registered capability (the bidirectional
49
+ // conformance test proves it). `arg` is the parameter grain the object binds to.
50
+ export const NL_INTENTS = Object.freeze({
51
+ "reverse:calls": { topic: "callers", arg: "symbol" }, // "which fns call X" -> callers of X
52
+ "forward:calls": { topic: "callees", arg: "symbol" }, // "what does X call" -> callees of X
53
+ "reverse:tests": { topic: "tests", arg: "symbol" }, // "which tests cover X"
54
+ "forward:tests": { topic: "tests", arg: "symbol" }, // "what tests X"
55
+ "reverse:inherits": { topic: "subclasses", arg: "class" }, // "which classes extend X"
56
+ "forward:reexports": { topic: "exports", arg: "module" }, // "what does X export"
57
+ "forward:contains": { topic: "members", arg: "class" }, // "what does X contain" -> members of X
58
+ "when:touches": { topic: "history", arg: "symbol" }, // "when did X change" -> commit history
59
+ "forward:cochange": { topic: "cochanges", arg: "symbol" }, // "what changes with X"
60
+ "reverse:cochange": { topic: "cochanges", arg: "symbol" },
61
+ });
62
+
63
+ // ---- ask-vocab RELATION kinds with NO capability — the HONEST ceiling --------
64
+ // Every ask-vocab.mjs RELATIONS key must be either mapped (above) or listed here
65
+ // with a reason (the bidirectional conformance test enforces the partition — no
66
+ // silent gap). These are relations tmct's grammar SPEAKS but the read-only
67
+ // graph-query registry has no operator for: routing them anywhere would be a
68
+ // mis-route, so the resolver REFUSES (never a guess).
69
+ export const UNMAPPED_KINDS = Object.freeze({
70
+ imports: "no importer/imports query tool in the registry (there is no tmct_imports); refusing beats mis-routing to calls",
71
+ uses: "a query-side UNION (imports+calls+callsSymbol) with no single capability; a router that must emit ONE call cannot honour it — refuse",
72
+ defines: "Module->symbol `defines` has no dedicated capability (tmct_members is Class-scoped `contains`); refuse rather than answer a different grain",
73
+ });
74
+
75
+ // ---- capabilities the NL surface cannot reach today (named, not accidental) ---
76
+ // A declared capability with no NL/command/frame path is a ROUTING GAP. The
77
+ // conformance test FAILS on an untagged gap; a genuinely-unreachable cap must be
78
+ // tagged HERE with the Stage it needs, so the ceiling is honest rather than a
79
+ // silent low-completion refuse. (Coordinator reinforcement 2.)
80
+ export const NOT_NL_REACHABLE = Object.freeze({
81
+ tmct_calls: "the raw call-edge dump collides with tmct_callees on every NL phrasing (\"what does X call\" -> callees) and has no command verb; distinguishing it needs a Stage-2 intent frame we have not authored",
82
+ });
83
+
84
+ // ---- imperative intent FRAMES (Stage 2 — fills what the relational grammar and
85
+ // the command register both miss). regex -> { topic, arg | noArg }. `arg` names
86
+ // the parameter grain; the entity is pulled by extractEntity (or, for search, the
87
+ // residual query text). Ordered: first match wins. Every frame's topic is
88
+ // backward-chained to a capability just like an NL intent, so the frame table
89
+ // adds PHRASINGS, never a new routing path. ----
90
+ export const FRAMES = Object.freeze([
91
+ { re: /\buntested\b|\bwithout\s+(?:a\s+)?tests?\b|\bhas\s+no\s+tests?\b|\bneeds?\s+(?:a\s+)?tests?\b/i, topic: "untested", noArg: true },
92
+ { re: /\bblast\s*radius\b|\bimpacts?\b|\bimpacted\b|what\s+(?:a\s+)?change.*(?:reach|affect|touch)|what\s+(?:depends?\s+on|dependents?)\b/i, topic: "impact", arg: "module" },
93
+ { re: /\bcallees?\b|what\s+does\s+\S+\s+call\b/i, topic: "callees", arg: "symbol" },
94
+ { re: /\bcallers?\b|who\s+calls\b|what\s+calls\b/i, topic: "callers", arg: "symbol" },
95
+ { re: /\btests?\b.*\b(?:for|cover|covering|of)\b|which\s+tests?\b|covers?\b|covered\b|test\s+coverage\b/i, topic: "tests", arg: "symbol" },
96
+ { re: /\bcochang|change[- ]coupl/i, topic: "cochanges", arg: "symbol" },
97
+ { re: /\bexports?\b|\bpublic\s+api\b/i, topic: "exports", arg: "module" },
98
+ { re: /\bsubclasses?\b|children\s+of\b|\bextends?\b/i, topic: "subclasses", arg: "class" },
99
+ { re: /\bmembers?\b|\bmethods?\s+of\b|\battributes?\s+of\b/i, topic: "members", arg: "class" },
100
+ { re: /\bhistory\b|who\s+changed\b|commits?\s+(?:that\s+)?touch/i, topic: "history", arg: "symbol" },
101
+ { re: /\bsignature\b/i, topic: "signature", arg: "symbol" },
102
+ { re: /\bdescribe\b|what\s+is\b|tell\s+me\s+about\b|definition\s+of\b/i, topic: "description", arg: "symbol" },
103
+ { re: /\bsearch\b|\bfind\b|look\s+for\b/i, topic: "matches", arg: "query" },
104
+ ]);
105
+
106
+ // ---- backward chaining (the SLD/Datalog step) --------------------------------
107
+
108
+ /** Backward-chain a goal `(knows <topic> ?of)` to the registered capability whose
109
+ * add-effect ACHIEVES it. Pure over the registry; returns the capability or null.
110
+ * This is the whole selection primitive — a capability is chosen ONLY because its
111
+ * declared effect unifies with the request's epistemic goal, never by name. */
112
+ export function backwardChain(topic) {
113
+ for (const cap of capabilities()) {
114
+ if (effectsOf(cap.name).add.some((e) => e.topic === topic)) return cap;
115
+ }
116
+ return null;
117
+ }
118
+
119
+ // The stopword set for the imperative-frame entity extractor (Stage-2 slot
120
+ // filling). Deliberately generous: a wrong pick is caught by the resolveObject
121
+ // miss -> honest refuse, never emitted.
122
+ const STOP = new Set([
123
+ "what", "whats", "which", "who", "whom", "does", "do", "did", "is", "are", "the", "a", "an",
124
+ "of", "for", "to", "in", "on", "by", "me", "us", "tell", "about", "show", "list", "give", "get",
125
+ "assess", "check", "then", "and", "or", "instead", "if", "end", "up", "its", "it", "this", "that",
126
+ "call", "calls", "called", "caller", "callers", "callee", "callees", "test", "tests", "tested",
127
+ "cover", "covers", "covering", "describe", "description", "define", "definition", "impact",
128
+ "member", "members", "method", "methods", "attribute", "attributes", "subclass", "subclasses",
129
+ "export", "exports", "history", "commit", "commits", "signature", "search", "find", "look",
130
+ "module", "modules", "class", "classes", "function", "functions", "symbol", "symbols",
131
+ "untested", "blast", "radius", "change", "changes", "changing", "reach", "reaches", "affect", "affects",
132
+ ]);
133
+
134
+ /** Pull one entity token from a request (imperative-frame slot-filling). Prefer a
135
+ * path/dotted/CamelCase token, else the last non-stopword identifier. "" if none. */
136
+ export function extractEntity(request) {
137
+ const tokens = String(request).match(/[A-Za-z_][A-Za-z0-9_./-]*/g) || [];
138
+ const pool = tokens.filter((t) => !STOP.has(t.toLowerCase()));
139
+ const strong = pool.filter((t) => /[./]/.test(t) || /^[A-Z][a-z]/.test(t) || /\.[a-z]+$/.test(t));
140
+ const pick = strong.length ? strong : pool;
141
+ return pick.length ? pick[pick.length - 1] : "";
142
+ }
143
+
144
+ /** The residual search query: strip the leading search/find verb + a filler "for". */
145
+ function searchQuery(request) {
146
+ return String(request).replace(/^\s*(?:search|find|look\s+for|look)\s+(?:for\s+)?/i, "").trim();
147
+ }
148
+
149
+ // ---- request -> capability + raw term (PURE, no binding yet) -----------------
150
+
151
+ /** Map a parseQuery simple-clause parse to a capability selection, or a refusal,
152
+ * or null (not an NL shape this resolver routes). Returns one of:
153
+ * { name, arg, term, topic, source:"nl", why:[...] } — a selection
154
+ * { refuse:true, reason } — an honest no-fit
155
+ * null — parse absent / not simple
156
+ * Backward-chains topic -> capability so selection is provable, never by name. */
157
+ export function mapParse(parse) {
158
+ if (!parse || parse.node || !parse.shape) return null; // compositional/absent -> not here
159
+ const { shape, kind } = parse;
160
+ // yes/no + locational/vocabulary shapes have no read-only query operator
161
+ if (shape === "ask") return { refuse: true, reason: "a yes/no `does X <verb> Y` question has no capability that emits it (the registry answers WHAT, not WHETHER)" };
162
+ if (shape === "where" || shape === "meta" || shape === "mentions") {
163
+ return { refuse: true, reason: `the "${shape}" shape (location/vocabulary/prose) has no read-only graph-query capability` };
164
+ }
165
+ const intent = NL_INTENTS[`${shape}:${kind}`];
166
+ if (!intent) {
167
+ const reason = UNMAPPED_KINDS[kind] || `no capability maps the ${shape}/${kind} intent`;
168
+ return { refuse: true, reason };
169
+ }
170
+ const cap = backwardChain(intent.topic);
171
+ if (!cap) return { refuse: true, reason: `backward chaining found no capability achieving (knows ${intent.topic})` };
172
+ return {
173
+ name: cap.name, arg: intent.arg, term: String(parse.object || "").trim(), topic: intent.topic, source: "nl",
174
+ why: [`parse ${shape}/${kind} => goal (knows ${intent.topic} ?${intent.arg})`, `backward-chain => ${cap.name} (its add-effect achieves ${intent.topic})`],
175
+ };
176
+ }
177
+
178
+ /** Match an imperative FRAME. Returns { name, arg|noArg, term, topic, source:"frame", why }
179
+ * or null. Backward-chains the frame's topic to a capability. */
180
+ export function mapFrame(request) {
181
+ for (const f of FRAMES) {
182
+ if (!f.re.test(request)) continue;
183
+ const cap = backwardChain(f.topic);
184
+ if (!cap) continue;
185
+ if (f.noArg) return { name: cap.name, noArg: true, topic: f.topic, source: "frame", why: [`imperative frame => goal (knows ${f.topic})`, `backward-chain => ${cap.name}`] };
186
+ const term = f.arg === "query" ? searchQuery(request) : extractEntity(request);
187
+ return { name: cap.name, arg: f.arg, term, topic: f.topic, source: "frame", why: [`imperative frame => goal (knows ${f.topic} ?${f.arg})`, `backward-chain => ${cap.name}`] };
188
+ }
189
+ return null;
190
+ }
191
+
192
+ /** The command register (server-http.mjs selectTool) filtered to the registry.
193
+ * Returns { name, input, source:"command", why } or null. selectTool binds the
194
+ * arg from the terse command; we re-clean a search query (its raw join keeps a
195
+ * stray "for"). Never reaches outside the declared set. */
196
+ export function commandCapability(request, declaredNames) {
197
+ const sel = selectTool(request, new Set(declaredNames));
198
+ if (!sel || !capabilityByName(sel.name)) return null;
199
+ const input = { ...sel.input };
200
+ if (sel.name === "tmct_search" && input.query != null) input.query = searchQuery(request) || String(input.query);
201
+ return { name: sel.name, input, source: "command", why: [`command register: "${String(request).trim().split(/\s+/)[0]}" => ${sel.name}`] };
202
+ }
203
+
204
+ // ---- the full single-call resolver (async — binds + grounds) -----------------
205
+
206
+ /** Build the glass-box proof chain for a grounded single call: its preconditions
207
+ * (graphLoaded + resolves(param) with the BOUND value + any-present) then the
208
+ * epistemic add-effect. Dispatch has SUCCEEDED, so `resolves` steps are ok. */
209
+ export function proofFor(name, input) {
210
+ const steps = [];
211
+ for (const pre of preconditionsOf(name)) {
212
+ if (pre.pred === PRECOND.graphLoaded) steps.push({ step: "precondition", pred: pre.pred, ok: true });
213
+ else if (pre.pred === PRECOND.resolves) steps.push({ step: "precondition", pred: pre.pred, param: pre.param, value: input[pre.param] ?? null, ok: true });
214
+ else if (pre.pred === PRECOND.anyPresent) steps.push({ step: "precondition", pred: pre.pred, params: pre.params, ok: pre.params.some((k) => input[k]) });
215
+ }
216
+ for (const eff of effectsOf(name).add) steps.push({ step: "effect", pred: eff.pred, topic: eff.topic, of: eff.of });
217
+ return steps;
218
+ }
219
+
220
+ const REFUSE = (why) => ({ selected: null, refused: true, reason: why });
221
+
222
+ /** Select a capability for a request and BIND its arguments — the full resolver.
223
+ * Order: command register -> NL parse -> imperative frame. On a bound selection
224
+ * it delegates entity binding to ctx.resolve (resolveObject) and, unless
225
+ * `execute:false`, grounds it via ctx.dispatch. Returns
226
+ * { selected:{name,input}, proof, why, resolved, observed? } — a grounded call
227
+ * { selected:null, refused:true, reason } — an honest refusal
228
+ * NEVER emits an ungrounded / ambiguous / undeclared call. */
229
+ export async function resolveOne(request, declaredNames, ctx, { execute = true } = {}) {
230
+ const declared = new Set(declaredNames);
231
+
232
+ // 1. command register (exact) 2. NL parse 3. imperative frame. An NL-parse
233
+ // REFUSAL is NOT terminal: a shape with no relational operator ("what is the
234
+ // impact of X", "list what covers X") can still be an imperative FRAME hit,
235
+ // so we fall through and only surface the NL reason if the frame misses too.
236
+ let pick = commandCapability(request, declared);
237
+ let nlRefuse = null;
238
+ if (!pick) {
239
+ const mapped = mapParse(parseQuery(request));
240
+ if (mapped && !mapped.refuse) pick = mapped;
241
+ else if (mapped && mapped.refuse) nlRefuse = mapped.reason;
242
+ }
243
+ if (!pick) pick = mapFrame(request);
244
+ if (!pick) return REFUSE(nlRefuse || "no command, NL parse, or imperative frame selects a capability");
245
+ let why = pick.why ?? [];
246
+ if (!declared.has(pick.name)) return REFUSE(`selected ${pick.name} but it is not in the declared toolset`);
247
+
248
+ // build the bound input. A command pick already carries a bound input; an NL /
249
+ // frame pick carries a raw term that we BIND via resolveObject (the oracle).
250
+ let input = pick.input ? { ...pick.input } : {};
251
+ let resolved = null;
252
+ if (!pick.input && !pick.noArg) {
253
+ const term = String(pick.term || "").trim();
254
+ if (!term) return REFUSE(`the ${pick.topic} intent named no entity to bind`);
255
+ // DELEGATE binding to resolveObject — the resolves(param,as) precondition.
256
+ const r = ctx.resolve ? ctx.resolve(term) : { match: { label: term }, ambiguous: false };
257
+ if (!r || !r.match) return REFUSE(`"${term}" does not resolve to any graph entity (honest miss)`);
258
+ if (r.ambiguous) return REFUSE(`"${term}" is ambiguous (${[r.match, ...(r.candidates || [])].slice(0, 4).map((m) => m.label).join(", ")}) — narrow it`);
259
+ resolved = r.match;
260
+ input = { [pick.arg]: r.match.label };
261
+ why = [...why, `resolveObject: "${term}" => ${r.match.label} (${r.match.class || "?"}, tier ${r.tier})`];
262
+ }
263
+
264
+ const call = { name: pick.name, input };
265
+ // the same zero-hallucination gate the grader enforces — self-check before emit.
266
+ const problems = hallucinationsIn(call, [...declared]);
267
+ if (problems.length) return REFUSE(`bound call did not validate: ${problems.map((p) => p.reason).join(",")}`);
268
+
269
+ // ground it: a ToolError (unresolvable entity) is an honest miss -> refuse.
270
+ if (execute && ctx.dispatch) {
271
+ const res = await ctx.dispatch(pick.name, input);
272
+ if (!res.ok) return REFUSE(`unresolvable at dispatch: ${res.error}`);
273
+ return { selected: call, proof: proofFor(pick.name, input), why, resolved: res.resolved ?? resolved, observed: String(res.text ?? "").slice(0, 240) };
274
+ }
275
+ return { selected: call, proof: proofFor(pick.name, input), why, resolved };
276
+ }
277
+
278
+ // ---- reachability (used by the bidirectional conformance test + docs) ---------
279
+
280
+ /** The epistemic topics some NL intent or imperative frame can reach. */
281
+ export function nlReachableTopics() {
282
+ const topics = new Set();
283
+ for (const v of Object.values(NL_INTENTS)) topics.add(v.topic);
284
+ for (const f of FRAMES) topics.add(f.topic);
285
+ return topics;
286
+ }
287
+
288
+ /** Every capability an NL intent or frame can reach (by name), via backwardChain. */
289
+ export function reachableCapabilityNames() {
290
+ const names = new Set();
291
+ for (const topic of nlReachableTopics()) { const c = backwardChain(topic); if (c) names.add(c.name); }
292
+ return names;
293
+ }
@@ -0,0 +1,296 @@
1
+ // server-http.mjs — `tmct serve`: an Anthropic Messages API-compatible HTTP
2
+ // endpoint (POST /v1/messages) over tmct's existing zero-model engine.
3
+ //
4
+ // This is Phase A of the capability-router plan (PLAN_CAPABILITY_ROUTER.md): the
5
+ // COMMON INTERFACE a tool-loop client (Claude Code) already speaks. It is a
6
+ // deterministic serialization/HTTP shim — NO model, ever. A request carries
7
+ // { model, messages[], tools[], max_tokens, system? }; a response is a message
8
+ // with `content` blocks (text and/or tool_use) and a `stop_reason`:
9
+ //
10
+ // - TEXT ANSWER (stop_reason "end_turn"): the latest user text is run through
11
+ // runTurn (src/chat.mjs) over the configured graph — the same cited,
12
+ // read-only answer the chat surface gives. Emitted when no tools are
13
+ // declared, or when nothing maps to a declared graph-query tool.
14
+ // - TOOL_USE (stop_reason "tool_use"): when tools[] are declared and the
15
+ // request maps to a declared graph-query tool, a { type:"tool_use", id,
16
+ // name, input } block is emitted — `name`+`input` are backed by dispatchTool
17
+ // (src/server.mjs). The caller executes it and returns a tool_result block;
18
+ // the next request closes the loop with an end_turn text answer.
19
+ //
20
+ // bedrock-meter-pluggable: every response's `usage` is { input_tokens: 0,
21
+ // output_tokens: 0 } — tmct is the $0 floor, priced as free by the meter.
22
+ //
23
+ // NOTE: src/server.mjs is the TOOL-DISPATCH layer (dispatchTool), NOT an HTTP
24
+ // server; this module is the HTTP surface and imports that layer's exports.
25
+
26
+ import { createServer } from "node:http";
27
+ import { runTurn, COMMANDS, asBareCommand, isConversational } from "./chat.mjs";
28
+ import { TOOLS } from "./server.mjs";
29
+ import { parseEntities } from "./codegraph.mjs";
30
+ import { uuidv7 } from "./uuid.mjs";
31
+ import * as defaultSource from "./source.mjs";
32
+
33
+ /** The zero usage every response carries — the meter prices tmct as the $0 floor. */
34
+ const ZERO_USAGE = { input_tokens: 0, output_tokens: 0 };
35
+
36
+ /** The tmct tools dispatchTool can back (the set the shim will emit a tool_use for).
37
+ * A declared tool outside this set is ignored for emission (the request falls
38
+ * through to a text answer) — the shim never emits a call it cannot ground. The
39
+ * COMMANDS map (chat.mjs) names the richer graph tools; TOOLS names the hot
40
+ * catalog. Their union is what dispatchTool serves. */
41
+ const BACKED_TOOLS = new Set([
42
+ ...TOOLS.map((t) => t.name),
43
+ ...Object.values(COMMANDS).map((s) => s.tool),
44
+ ]);
45
+
46
+ /** Flatten a message's `content` (a string OR a content-block array) into plain
47
+ * text — concatenating the `text` blocks. Non-text blocks are ignored here. */
48
+ function textOfContent(content) {
49
+ if (typeof content === "string") return content;
50
+ if (!Array.isArray(content)) return "";
51
+ return content
52
+ .filter((b) => b && b.type === "text" && typeof b.text === "string")
53
+ .map((b) => b.text)
54
+ .join("\n")
55
+ .trim();
56
+ }
57
+
58
+ /** The last message with the given role, or null. */
59
+ function lastMessageOfRole(messages, role) {
60
+ if (!Array.isArray(messages)) return null;
61
+ for (let i = messages.length - 1; i >= 0; i--) {
62
+ if (messages[i] && messages[i].role === role) return messages[i];
63
+ }
64
+ return null;
65
+ }
66
+
67
+ /** The first tool_result block in a message's content array, or null. The caller
68
+ * returns one of these after executing a tool_use — its presence means the loop
69
+ * is closing and we answer with end_turn. */
70
+ function firstToolResult(message) {
71
+ const content = message && message.content;
72
+ if (!Array.isArray(content)) return null;
73
+ return content.find((b) => b && b.type === "tool_result") || null;
74
+ }
75
+
76
+ /** Render a tool_result block's `content` (string OR block array OR arbitrary
77
+ * value) back to text — what the caller reported when it executed the tool. */
78
+ function toolResultText(block) {
79
+ const c = block && block.content;
80
+ if (typeof c === "string") return c;
81
+ if (Array.isArray(c)) return textOfContent(c);
82
+ if (c == null) return "";
83
+ try { return JSON.stringify(c); } catch { return String(c); }
84
+ }
85
+
86
+ /**
87
+ * Decide whether a user turn maps to a DECLARED, dispatch-backed graph-query
88
+ * tool, and bind its arguments. Deterministic, in-ethos (no NL guessing beyond
89
+ * the chat surface's own command routing):
90
+ *
91
+ * 1. A slash/bare command that names a tmct tool ("describe X", "/callers X",
92
+ * "untested") → that tool with its argument bound from the exact arg key the
93
+ * dispatchTool switch reads (COMMANDS in chat.mjs). Only when the tool is
94
+ * declared by the caller.
95
+ * 2. Otherwise, a non-conversational structural question → tmct_ask{query:…},
96
+ * when tmct_ask is declared. Small-talk (isConversational) never emits a
97
+ * call — it falls through to a text answer.
98
+ *
99
+ * Returns { name, input } or null (→ answer as text).
100
+ */
101
+ export function selectTool(text, declaredNames) {
102
+ const t = String(text || "").trim();
103
+ if (!t) return null;
104
+
105
+ // 1. explicit command form → a specific tool, argument bound
106
+ const cmdLine = t.startsWith("/") ? t : asBareCommand(t);
107
+ if (cmdLine) {
108
+ const [first, ...restTok] = cmdLine.replace(/^\//, "").split(/\s+/);
109
+ const spec = COMMANDS[String(first).toLowerCase()];
110
+ if (spec && declaredNames.has(spec.tool) && BACKED_TOOLS.has(spec.tool)) {
111
+ const input = {};
112
+ if (spec.arg) {
113
+ const val = restTok.join(" ").trim();
114
+ if (val) input[spec.arg] = val;
115
+ // an entity command with no argument can't bind a call — fall through
116
+ else if (!spec.optional) return askFallback(t, declaredNames);
117
+ }
118
+ return { name: spec.tool, input };
119
+ }
120
+ }
121
+
122
+ // 2. structural question → tmct_ask, unless it's small-talk
123
+ return askFallback(t, declaredNames);
124
+ }
125
+
126
+ /** The tmct_ask fallback: emit tmct_ask{query} for a non-conversational line when
127
+ * the caller declared tmct_ask; otherwise null (→ text answer). */
128
+ function askFallback(text, declaredNames) {
129
+ if (declaredNames.has("tmct_ask") && BACKED_TOOLS.has("tmct_ask") && !isConversational(text)) {
130
+ return { name: "tmct_ask", input: { query: text } };
131
+ }
132
+ return null;
133
+ }
134
+
135
+ /** Build the assistant message envelope shared by every branch. */
136
+ function assistantMessage(model, content, stopReason) {
137
+ return {
138
+ id: `msg_${uuidv7().replace(/-/g, "")}`,
139
+ type: "message",
140
+ role: "assistant",
141
+ model: model || "tmct",
142
+ content,
143
+ stop_reason: stopReason,
144
+ stop_sequence: null,
145
+ usage: { ...ZERO_USAGE },
146
+ };
147
+ }
148
+
149
+ /**
150
+ * Produce the Messages-API response for one request body. Pure over its inputs
151
+ * (the loaded graph + config), so it is unit-testable without a socket.
152
+ * - a returned tool_result → end_turn text (relay the tool's output)
153
+ * - a mapped, declared graph tool → tool_use
154
+ * - otherwise → end_turn text via runTurn
155
+ */
156
+ export async function respondToMessages(body, { config, graph, source = defaultSource } = {}) {
157
+ const { model, messages, tools } = body || {};
158
+ const declaredNames = new Set(
159
+ (Array.isArray(tools) ? tools : []).map((t) => t && t.name).filter(Boolean),
160
+ );
161
+
162
+ // Closing the loop: the caller executed our tool_use and returned a
163
+ // tool_result. Relay it as the final, cited answer with end_turn.
164
+ const lastUser = lastMessageOfRole(messages, "user");
165
+ const tr = firstToolResult(lastUser);
166
+ if (tr) {
167
+ const text = toolResultText(tr) || "(the tool returned no output)";
168
+ return assistantMessage(model, [{ type: "text", text }], "end_turn");
169
+ }
170
+
171
+ const userText = textOfContent(lastUser && lastUser.content);
172
+
173
+ // tool_use emission: a declared, dispatch-backed graph tool the request maps to.
174
+ if (declaredNames.size) {
175
+ const sel = selectTool(userText, declaredNames);
176
+ if (sel) {
177
+ const block = {
178
+ type: "tool_use",
179
+ id: `toolu_${uuidv7().replace(/-/g, "")}`,
180
+ name: sel.name,
181
+ input: sel.input,
182
+ };
183
+ return assistantMessage(model, [block], "tool_use");
184
+ }
185
+ }
186
+
187
+ // text answer: the cited, read-only answer the chat surface gives. memoryDir is
188
+ // null so the endpoint is PURE — no session artifacts, no writes, deterministic.
189
+ const { answer } = await runTurn(userText, { config, graph, source, memoryDir: null });
190
+ return assistantMessage(model, [{ type: "text", text: answer }], "end_turn");
191
+ }
192
+
193
+ /** Self-description payload (GET /) — lets a routing target discover the endpoint
194
+ * and the tools tmct can back with just an HTTP GET. */
195
+ function describe(config) {
196
+ return {
197
+ service: "tmct",
198
+ description: "Anthropic Messages API-compatible, deterministic, no-LLM graph router (the $0 floor).",
199
+ endpoint: { method: "POST", path: "/v1/messages" },
200
+ graph: config && config.graphFile,
201
+ tools: TOOLS.map((t) => ({ name: t.name, description: t.description, input_schema: t.inputSchema })),
202
+ usage_pricing: ZERO_USAGE,
203
+ };
204
+ }
205
+
206
+ async function readBody(req, limit = 5 * 1024 * 1024) {
207
+ return new Promise((resolve, reject) => {
208
+ const chunks = [];
209
+ let size = 0;
210
+ req.on("data", (c) => {
211
+ size += c.length;
212
+ if (size > limit) { reject(new Error("request body too large")); req.destroy(); return; }
213
+ chunks.push(c);
214
+ });
215
+ req.on("end", () => resolve(Buffer.concat(chunks).toString("utf8")));
216
+ req.on("error", reject);
217
+ });
218
+ }
219
+
220
+ function sendJson(res, status, obj) {
221
+ const payload = JSON.stringify(obj);
222
+ res.writeHead(status, { "content-type": "application/json; charset=utf-8" });
223
+ res.end(payload);
224
+ }
225
+
226
+ /** An Anthropic-style error envelope. */
227
+ function sendError(res, status, type, message) {
228
+ sendJson(res, status, { type: "error", error: { type, message } });
229
+ }
230
+
231
+ /**
232
+ * Start the HTTP server. Loads the graph once (tolerant: a missing artifact is
233
+ * the empty bootstrap graph, never an error). Returns { server, url, host, port,
234
+ * config, close } — `close()` shuts the socket cleanly (no hanging handles).
235
+ *
236
+ * config — { graphFile } (build via configFor(repoPath) in bin/tmct.mjs)
237
+ * host — bind address (default 127.0.0.1)
238
+ * port — TCP port; 0 picks an ephemeral port (tests)
239
+ */
240
+ export async function startServer({ config, host = "127.0.0.1", port = 0, source = defaultSource } = {}) {
241
+ if (!config || !config.graphFile) throw new Error("startServer requires config.graphFile");
242
+ // Load the graph once, up front. A missing artifact loads as the empty
243
+ // bootstrap graph — runTurn tolerates it (an honest empty/orienting answer).
244
+ const graph = parseEntities(await source.fetchEntities(config));
245
+
246
+ const server = createServer(async (req, res) => {
247
+ try {
248
+ const url = new URL(req.url, "http://localhost");
249
+ if (req.method === "GET" && (url.pathname === "/" || url.pathname === "/v1/models")) {
250
+ sendJson(res, 200, describe(config));
251
+ return;
252
+ }
253
+ if (url.pathname !== "/v1/messages") {
254
+ sendError(res, 404, "not_found_error", `no route ${req.method} ${url.pathname}`);
255
+ return;
256
+ }
257
+ if (req.method !== "POST") {
258
+ sendError(res, 405, "invalid_request_error", "POST /v1/messages");
259
+ return;
260
+ }
261
+ let body;
262
+ try {
263
+ body = JSON.parse((await readBody(req)) || "{}");
264
+ } catch {
265
+ sendError(res, 400, "invalid_request_error", "request body is not valid JSON");
266
+ return;
267
+ }
268
+ if (!body || !Array.isArray(body.messages)) {
269
+ sendError(res, 400, "invalid_request_error", "`messages` array is required");
270
+ return;
271
+ }
272
+ const out = await respondToMessages(body, { config, graph, source });
273
+ sendJson(res, 200, out);
274
+ } catch (e) {
275
+ sendError(res, 500, "api_error", e && e.message ? e.message : String(e));
276
+ }
277
+ });
278
+
279
+ await new Promise((resolve, reject) => {
280
+ server.once("error", reject);
281
+ server.listen(port, host, () => { server.removeListener("error", reject); resolve(); });
282
+ });
283
+
284
+ const addr = server.address();
285
+ const boundPort = typeof addr === "object" && addr ? addr.port : port;
286
+ const url = `http://${host}:${boundPort}`;
287
+
288
+ return {
289
+ server,
290
+ host,
291
+ port: boundPort,
292
+ url,
293
+ config,
294
+ close: () => new Promise((resolve) => server.close(() => resolve())),
295
+ };
296
+ }
package/src/syllogise.mjs CHANGED
Binary file