@matatbread/matbot-tool-types 0.4.4 → 0.4.5

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/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "matbotRuntime": [
4
4
  "node"
5
5
  ],
6
- "version": "0.4.4",
6
+ "version": "0.4.5",
7
7
  "engines": {
8
8
  "node": ">=24.0.0"
9
9
  },
@@ -36,14 +36,14 @@
36
36
  "typescript": "^6.0.3"
37
37
  },
38
38
  "peerDependencies": {
39
- "@matatbread/matbot-plugin-api": "^0.4.4",
40
- "@matatbread/matbot-core": "^0.4.4"
39
+ "@matatbread/matbot-plugin-api": "^0.4.5",
40
+ "@matatbread/matbot-core": "^0.4.5"
41
41
  },
42
42
  "devDependencies": {
43
43
  "@types/node": "^22.0.0",
44
44
  "typescript": "^6.0.3",
45
- "@matatbread/matbot-core": "^0.4.4",
46
- "@matatbread/matbot-plugin-api": "^0.4.4"
45
+ "@matatbread/matbot-plugin-api": "^0.4.5",
46
+ "@matatbread/matbot-core": "^0.4.5"
47
47
  },
48
48
  "scripts": {
49
49
  "typecheck": "tsc --noEmit"
package/src/build-dts.ts CHANGED
@@ -92,11 +92,22 @@ export async function buildMatbotToolsDts(
92
92
  const { fileURLToPath } = await import('node:url');
93
93
  const { createRequire } = await import('node:module');
94
94
 
95
- // Anchor plugin-api at the monorepo source if present, else the installed package.
95
+ // Anchor plugin-api: the monorepo source if present, then the project's own install, and finally THIS
96
+ // module's own resolution — which always works, because tool-types peer-depends on plugin-api, so
97
+ // whatever copy the host loaded is reachable from here.
98
+ //
99
+ // The last step is not belt-and-braces. Without it, a deployment where matbot is not resolvable from the
100
+ // config dir (installed globally, or a config dir outside the project) returned null, and the *silence*
101
+ // was the damage: ToolTypeIndex has no dts, skills_compiler falls back to a three-arm hardcoded stub, and
102
+ // the model then generates against a view of the registry that omits most live tools. Measured: asked to
103
+ // call `whoami`, it declared a `ToolContracts` arm for it *itself* — inventing `{ id: string; type: string }`
104
+ // — which compiled clean and only happened to resemble the real `Principal`. A wrong guess compiles too,
105
+ // and the cast gate cannot see it: nothing was cast, a contract was asserted. Same failure the scaffold's
106
+ // dangling plugin-api link used to cause, and the same fix.
96
107
  const monorepoApi = join(projectRoot, 'plugin-api', 'src', 'index.ts');
97
108
  let pluginApiIndex: string | undefined = existsSync(monorepoApi) ? monorepoApi : undefined;
98
- if (!pluginApiIndex) {
99
- try { pluginApiIndex = createRequire(join(projectRoot, '_')).resolve('@matatbread/matbot-plugin-api'); } catch { /* unresolved */ }
109
+ for (const from of pluginApiIndex === undefined ? [join(projectRoot, '_'), fileURLToPath(import.meta.url)] : []) {
110
+ try { pluginApiIndex = createRequire(from).resolve('@matatbread/matbot-plugin-api'); break; } catch { /* try the next anchor */ }
100
111
  }
101
112
  if (!pluginApiIndex) return null;
102
113
 
package/src/checker.ts CHANGED
@@ -116,10 +116,22 @@ async function runWorker(data: Record<string, unknown>): Promise<DiagnosticRecor
116
116
  return result.ok ? null : (result.diagnostics ?? []);
117
117
  }
118
118
 
119
- /** Typecheck a compiled plugin's build dir against its own tsconfig. `output` is the full annotated
120
- * report (anchored frames, related locations, hints, cascade-capped). */
121
- export async function checkProjectDir(buildDir: string): Promise<CheckResult> {
122
- const diags = await runWorker({ mode: 'project', buildDir });
119
+ /**
120
+ * Typecheck a compiled plugin's build dir against its own tsconfig. `output` is the full annotated
121
+ * report (anchored frames, related locations, hints, cascade-capped).
122
+ *
123
+ * `ownContracts` names the tools this compilation is entitled to declare a `ToolContracts` arm for — its
124
+ * own. Any other arm in the checked source is rejected like a cast: the dts is an input to generation,
125
+ * derived from the live registry, so an arm authored here asserts a shape nothing can contradict. Omit
126
+ * the option and the rule does not run (there is then no way to tell a self-declaration from an invention).
127
+ */
128
+ export async function checkProjectDir(
129
+ buildDir: string, opts?: { ownContracts?: readonly string[] },
130
+ ): Promise<CheckResult> {
131
+ const diags = await runWorker({
132
+ mode: 'project', buildDir,
133
+ ...(opts?.ownContracts !== undefined ? { ownContracts: [...opts.ownContracts] } : {}),
134
+ });
123
135
  if (diags === null) return { ok: true, output: '' };
124
136
  const parts: string[] = [];
125
137
  if (diags.length > 1) parts.push(`${diags.length} errors. Fix the FIRST error first — later errors often cascade from it.\n`);
@@ -237,6 +249,35 @@ try {
237
249
  return { file: sf, start: node.getStart(sf), length: node.getWidth(sf), code: code,
238
250
  category: ts.DiagnosticCategory.Error, messageText: msg, __syn: true };
239
251
  };
252
+ // ── contract gate: a generated tool declares its OWN ToolContracts arm and nobody else's ──
253
+ // The dts is an INPUT to code generation, derived from the live registry. A generated file that adds an
254
+ // arm for another tool is not describing anything — it is asserting a shape tsc then has no way to
255
+ // contradict, so a wrong guess compiles and fails at runtime. Measured, when the derived dts was
256
+ // unavailable and the model wanted \`whoami\`: it declared \`ToolContract<{ id: string; type: string }, {}>\`
257
+ // for it, which compiled clean and only resembled the real \`Principal\`. The cast gate cannot see this —
258
+ // nothing is cast. Only enforced when the caller says which names are the compilation's own; absent that
259
+ // list the rule cannot tell an invention from a legitimate self-declaration, so it does not run.
260
+ const own = Array.isArray(workerData.ownContracts) ? workerData.ownContracts : null;
261
+ const contractVisit = function (node, sf) {
262
+ if (own && ts.isModuleDeclaration(node) && ts.isStringLiteral(node.name)
263
+ && node.name.text === '@matatbread/matbot-plugin-api' && node.body && node.getStart(sf) >= minStart) {
264
+ node.body.forEachChild(function (decl) {
265
+ if (!ts.isInterfaceDeclaration(decl) || decl.name.text !== 'ToolContracts') return;
266
+ for (const member of decl.members) {
267
+ const name = member.name && member.name.getText ? member.name.getText(sf) : '';
268
+ if (name === '' || own.indexOf(name) !== -1) continue;
269
+ synth.push(mk(sf, member, 90004,
270
+ "the contract for '" + name + "' is not yours to declare — tool contracts come from the ambient " +
271
+ "tool dts, which is derived from the live registry. Declaring one here asserts a shape nothing " +
272
+ "can check, so a wrong guess compiles and fails at runtime. Delete this member: call the tool as " +
273
+ "'await tool." + name + "(...)' and use the value it returns, whose type is already precise. If '" +
274
+ name + "' is absent from the ambient dts then no such tool is loaded and it cannot be called at all."));
275
+ }
276
+ });
277
+ }
278
+ ts.forEachChild(node, function (c) { contractVisit(c, sf); });
279
+ };
280
+
240
281
  const skip = new Set();
241
282
  const visit = function (node, sf) {
242
283
  const isAssertion = ts.isAsExpression(node) || (ts.isTypeAssertionExpression && ts.isTypeAssertionExpression(node));
@@ -261,7 +302,7 @@ try {
261
302
  }
262
303
  ts.forEachChild(node, function (c) { visit(c, sf); });
263
304
  };
264
- for (const gf of gateFiles) visit(gf, gf);
305
+ for (const gf of gateFiles) { visit(gf, gf); contractVisit(gf, gf); }
265
306
 
266
307
  const errors = targets.filter(function (d) { return d.category === ts.DiagnosticCategory.Error; }).concat(synth);
267
308
  errors.sort(function (a, b) {