Trine for tools and AI agents (roadmap #44)
Three things make Trine easy to drive from a program:
- **
--jsondiagnostics** with a stablecodeand machine-applicablefixes. - **
trine fix**, which shows or applies those fixes. - The language card (CARD.md): the whole language on one page (~2k tokens), with every example checked by the test suite. Give it to a model before it writes Trine.
Plus what earlier steps built: input and output only through capabilities, so a program's reach shows in its types (trine caps, CAPABILITIES.md); review by meaning (trine semdiff); and fast trine run (cached native builds).
--json
Any command accepts --json. Errors are printed to stderr, one JSON object per line (stdout stays the program's own output). trine check f.tr --json prints, on success:
{"ok":true,"functions":[{"name":"fib","type":"(Int) -> Int"}, ...]}
An error:
{"file":"typo.tr","line":5,"col":9,"severity":"error","kind":"type error",
"code":"unknown-name","message":"unknown name `compte` โ did you mean `compute`?",
"source":" print(compte(4))",
"fixes":[{"title":"rename `compte` to `compute`","confidence":"likely",
"edits":[{"line":5,"col":9,"end_line":5,"end_col":15,"text":"compute"}]}]}
line/colare 1-based;colcounts bytes.- A runtime error also has
stack:[{"function","file","line","col","times"}, ...], innermost first (docs/TESTING.md); native release builds don't record it. kindissyntax error,type error,errororruntime error. Runtime errors from native code are converted to the same form.- Edits replace the text from (
line,col) up to but not including (end_line,end_col); an insertion has equal start and end. A fix's edits don't overlap: apply them last-first. - Several fixes for one error are alternatives (say
ris anA, or aB): pick one. - **
confidence:** -certain: the intent is unambiguous (letโvarfor a binding that is assigned to). -likely: the usual intent (a typo's nearest name; removing a?from a non-Option). -placeholder: makes the program check by insertingtodo("..."), which has any type and stops withnot implemented: ...if reached (a missing match case or method). Fill it in. - No fixes: the right change depends on what the program should do (
1 + "two"). Readmessage; most messages say what would work.
Codes
| code | meaning | fix |
|---|---|---|
unknown-name | name not defined | likely: the nearest defined name |
immutable | assignment to a let | certain: let โ var |
non-exhaustive | match misses cases | placeholder: the missing cases with todo |
unknown-type-variable | type variable not declared on the type | certain: add the parameter |
type-arity | wrong number of type arguments | likely: drop the extra ones |
missing-where | generic impl needs a trait on its parameter | certain: add the where clause |
missing-impl | type doesn't implement a trait | placeholder: an impl with todo methods |
missing-method | impl lacks a method | placeholder: the method with todo |
missing-field | constructor call lacks named fields | placeholder: field: todo(...) |
named-args-on-function | f(a: 1) on a function | certain: positional, in parameter order |
ambiguous-field | several types have this field | likely (alternatives): annotate the parameter |
ambiguous-collection | Map or Set? | likely (alternatives): annotate the parameter |
no-such-field | type has no such field | likely: the nearest field name |
try-not-option | ? on a value that isn't an Option/Result | likely: remove the ? |
try-error-type | ? would return the wrong error type | likely: change the function's error type |
try-return-type | ? in a function that doesn't return Option/Result | likely: return Option[...], wrap the result in Some |
type-mismatch | expected one type, found another | needs a decision |
field-type | .with(f: v) with the wrong type | needs a decision |
partial-field | only some variants have the field | needs a decision (use match) |
impl-not-general | method works only for one instantiation | needs a decision |
not-a-key | Float or function as a map key or set element | needs a decision |
not-comparable | < on lists or records | needs a decision |
bad-in | in on a non-collection | needs a decision |
outside-loop | break / continue not inside a loop of the same function (a lambda's body is its own) | needs a decision |
task-authority | a task (spawn, an async fn call) given a Sys, a Dir, Python or a function value | needs a decision (pass net(sys) or data) |
task-moved | a connection or line reader used after it moved into a task, or taken by tasks in a loop | needs a decision |
async-as-value | an async fn used as a value instead of called | needs a decision |
yield-outside-generator | yield in a function that isn't a generator | needs a decision (declare yields T) |
generator-returns | return with a value in a generator | needs a decision |
limit | a --max-* resource limit was reached (runtime) | needs a decision |
index-out-of-bounds, overflow, missing-key, todo, permission, no-main | runtime | needs a decision |
syntax, error, runtime | everything else | needs a decision |
var-never-reassigned (warning, trine lint) | a var nobody assigns | certain: let |
unused-variable (warning) | a let/var never read | likely: rename to _name |
unused-function (warning) | nothing calls it | needs a decision |
Codes are stable; messages may be reworded.
trine fix
trine fix f.tr # the first error and its fixes
trine fix f.tr --apply # apply the most confident fix, re-check, repeat (at most 20)
trine fix f.tr --json # the first error as JSON
Measured: the error corpus
tools/agent_loop.py is a scripted agent that uses nothing but the JSON: it applies the most confident fix until the program checks, then runs it. On the 31 programs in tests/errors/:
| outcome | count |
|---|---|
| fixed: checks and runs | 14 |
placeholder: checks; running reaches the inserted todo | 3 |
| needs a decision (no mechanical fix) | 10 |
| runtime error (out of bounds, overflow, missing key: the program checks) | 4 |
So every one of the 17 static errors that has a mechanical repair is repaired from the JSON alone, and the test suite holds that line (json_diagnostics_have_codes_and_mechanical_fixes_repair_programs). The other 14 need a decision about intent, which is a model's or a person's job; for them the JSON gives the code, the position, the source line and the message.
Formatter, linter, docs (roadmap #34)
trine fmt f.trrewrites the file in the canonical layout (2-space blocks, one space around operators, none inside brackets, one blank line between definitions, comments kept, aligned trailing comments kept aligned).--checkexits non-zero if the file isn't formatted;--stdoutprints instead of writing. Format agent output before committing it: diffs then show only real changes. The formatter refuses (and leaves the file alone) unless the result lexes to exactly the same tokens and formatting it again changes nothing, so it can't change what a program means.trine lint f.trprints warnings (--jsonas above, severitywarning;--strictexits non-zero).trine fix --applyapplies their fixes once the program has no errors.trine testrunstest "name":blocks (exit status 1 on a failure);trine hints f.trsays where native code borrows, shares, may copy and still checks bounds (docs/TESTING.md, docs/EDITOR.md).trine doc f.trprints a Markdown reference: the file's opening comment, then each type, trait, impl and function with the comment above it and the function's inferred type.- All three cover the modules the file imports from the same project (not packages): see MODULES.md.