Trine for tools and AI agents (roadmap #44)

Three things make Trine easy to drive from a program:

  1. **--json diagnostics** with a stable code and machine-applicable fixes.
  2. **trine fix**, which shows or applies those fixes.
  3. 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"}]}]}

Codes

codemeaningfix
unknown-namename not definedlikely: the nearest defined name
immutableassignment to a letcertain: let โ†’ var
non-exhaustivematch misses casesplaceholder: the missing cases with todo
unknown-type-variabletype variable not declared on the typecertain: add the parameter
type-aritywrong number of type argumentslikely: drop the extra ones
missing-wheregeneric impl needs a trait on its parametercertain: add the where clause
missing-impltype doesn't implement a traitplaceholder: an impl with todo methods
missing-methodimpl lacks a methodplaceholder: the method with todo
missing-fieldconstructor call lacks named fieldsplaceholder: field: todo(...)
named-args-on-functionf(a: 1) on a functioncertain: positional, in parameter order
ambiguous-fieldseveral types have this fieldlikely (alternatives): annotate the parameter
ambiguous-collectionMap or Set?likely (alternatives): annotate the parameter
no-such-fieldtype has no such fieldlikely: the nearest field name
try-not-option? on a value that isn't an Option/Resultlikely: remove the ?
try-error-type? would return the wrong error typelikely: change the function's error type
try-return-type? in a function that doesn't return Option/Resultlikely: return Option[...], wrap the result in Some
type-mismatchexpected one type, found anotherneeds a decision
field-type.with(f: v) with the wrong typeneeds a decision
partial-fieldonly some variants have the fieldneeds a decision (use match)
impl-not-generalmethod works only for one instantiationneeds a decision
not-a-keyFloat or function as a map key or set elementneeds a decision
not-comparable< on lists or recordsneeds a decision
bad-inin on a non-collectionneeds a decision
outside-loopbreak / continue not inside a loop of the same function (a lambda's body is its own)needs a decision
task-authoritya task (spawn, an async fn call) given a Sys, a Dir, Python or a function valueneeds a decision (pass net(sys) or data)
task-moveda connection or line reader used after it moved into a task, or taken by tasks in a loopneeds a decision
async-as-valuean async fn used as a value instead of calledneeds a decision
yield-outside-generatoryield in a function that isn't a generatorneeds a decision (declare yields T)
generator-returnsreturn with a value in a generatorneeds a decision
limita --max-* resource limit was reached (runtime)needs a decision
index-out-of-bounds, overflow, missing-key, todo, permission, no-mainruntimeneeds a decision
syntax, error, runtimeeverything elseneeds a decision
var-never-reassigned (warning, trine lint)a var nobody assignscertain: let
unused-variable (warning)a let/var never readlikely: rename to _name
unused-function (warning)nothing calls itneeds 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/:

outcomecount
fixed: checks and runs14
placeholder: checks; running reaches the inserted todo3
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)