@metta-ts/debug 1.3.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 MesTTo
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,25 @@
1
+ <!--
2
+ SPDX-FileCopyrightText: 2026 MesTTo
3
+ SPDX-License-Identifier: MIT
4
+ -->
5
+
6
+ # @metta-ts/debug
7
+
8
+ `@metta-ts/debug` contains the host-free debugger engine used by `metta-debug`
9
+ and embedders such as language servers.
10
+
11
+ The package does not read files, register global output sinks, or import Node
12
+ APIs. Callers provide the exact runner they already use.
13
+
14
+ ```ts
15
+ import { explainCall } from "@metta-ts/debug";
16
+ import { runProgram } from "@metta-ts/core";
17
+
18
+ const report = explainCall(runProgram, "(= (double $x) (* $x 2))", "(double 21)");
19
+
20
+ console.log(report.result); // ["42"]
21
+ ```
22
+
23
+ Use `collectTrace` when the caller already has an assembled program and only
24
+ needs the raw trace event stream. Use `summarize` when the caller already
25
+ collected events and needs the grouped `metta-debug why` counters.
@@ -0,0 +1,26 @@
1
+ import { TraceEvent, Atom, RunOptions, QueryResult } from '@metta-ts/core';
2
+ export { TraceEvent } from '@metta-ts/core';
3
+
4
+ interface TraceSummary {
5
+ readonly grounded: Record<string, number>;
6
+ readonly specialized: string[];
7
+ readonly overflow: string[];
8
+ readonly reductions: number;
9
+ }
10
+ type TraceRunner = (program: string, fuel: number | undefined, imports: Map<string, Atom[]>, opts?: RunOptions) => QueryResult[];
11
+ interface DebugRunOptions {
12
+ readonly fuel?: number | undefined;
13
+ readonly imports?: Map<string, Atom[]> | undefined;
14
+ readonly runOptions?: Omit<RunOptions, "trace"> | undefined;
15
+ }
16
+ interface CallExplanation {
17
+ readonly result: string[];
18
+ readonly trace: TraceEvent[];
19
+ readonly summary: TraceSummary;
20
+ }
21
+ declare function summarize(events: readonly TraceEvent[]): TraceSummary;
22
+ declare function assembleQuery(source: string, call: string): string;
23
+ declare function collectTrace(runner: TraceRunner, program: string, opts?: DebugRunOptions): TraceEvent[];
24
+ declare function explainCall(runner: TraceRunner, source: string, call: string, opts?: DebugRunOptions): CallExplanation;
25
+
26
+ export { type CallExplanation, type DebugRunOptions, type TraceRunner, type TraceSummary, assembleQuery, collectTrace, explainCall, summarize };
package/dist/index.js ADDED
@@ -0,0 +1,49 @@
1
+ // src/index.ts
2
+ import {
3
+ format
4
+ } from "@metta-ts/core";
5
+ function summarize(events) {
6
+ const grounded = {};
7
+ const specialized = /* @__PURE__ */ new Set();
8
+ const overflow = [];
9
+ let reductions = 0;
10
+ for (const e of events) {
11
+ if (e.kind === "grounded") grounded[e.op] = (grounded[e.op] ?? 0) + 1;
12
+ else if (e.kind === "specialize") specialized.add(`${e.from} -> ${e.to}`);
13
+ else if (e.kind === "overflow") overflow.push(e.atom);
14
+ else reductions++;
15
+ }
16
+ return { grounded, specialized: [...specialized], overflow, reductions };
17
+ }
18
+ function assembleQuery(source, call) {
19
+ const trimmed = call.trim();
20
+ const q = trimmed.startsWith("!") ? trimmed : `!${trimmed}`;
21
+ return `${source}
22
+ ${q}`;
23
+ }
24
+ function runWithTrace(runner, program, opts = {}) {
25
+ const trace = [];
26
+ const runOptions = {
27
+ ...opts.runOptions ?? {},
28
+ trace: (e) => trace.push(e)
29
+ };
30
+ const groups = runner(program, opts.fuel, opts.imports ?? /* @__PURE__ */ new Map(), runOptions);
31
+ return { groups, trace };
32
+ }
33
+ function collectTrace(runner, program, opts) {
34
+ return runWithTrace(runner, program, opts).trace;
35
+ }
36
+ function explainCall(runner, source, call, opts) {
37
+ const { groups, trace } = runWithTrace(runner, assembleQuery(source, call), opts);
38
+ return {
39
+ result: groups.at(-1)?.results.map(format) ?? [],
40
+ trace,
41
+ summary: summarize(trace)
42
+ };
43
+ }
44
+ export {
45
+ assembleQuery,
46
+ collectTrace,
47
+ explainCall,
48
+ summarize
49
+ };
package/package.json ADDED
@@ -0,0 +1,50 @@
1
+ {
2
+ "name": "@metta-ts/debug",
3
+ "version": "1.3.0",
4
+ "license": "MIT",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "default": "./dist/index.js"
12
+ },
13
+ "./package.json": "./package.json"
14
+ },
15
+ "files": [
16
+ "dist"
17
+ ],
18
+ "dependencies": {
19
+ "@metta-ts/core": "1.3.0"
20
+ },
21
+ "author": "MesTTo",
22
+ "engines": {
23
+ "node": ">=20"
24
+ },
25
+ "sideEffects": false,
26
+ "module": "./dist/index.js",
27
+ "publishConfig": {
28
+ "access": "public"
29
+ },
30
+ "description": "Host-free trace collection and explanation helpers for MeTTa TS debuggers.",
31
+ "keywords": [
32
+ "metta",
33
+ "hyperon",
34
+ "debug",
35
+ "trace",
36
+ "typescript"
37
+ ],
38
+ "repository": {
39
+ "type": "git",
40
+ "url": "git+https://github.com/MesTTo/MeTTa-TS.git",
41
+ "directory": "packages/debug"
42
+ },
43
+ "homepage": "https://github.com/MesTTo/MeTTa-TS#readme",
44
+ "bugs": {
45
+ "url": "https://github.com/MesTTo/MeTTa-TS/issues"
46
+ },
47
+ "scripts": {
48
+ "build": "tsup src/index.ts --format esm --dts --clean"
49
+ }
50
+ }