Skip to content

External functions, records, & minor improvements - #43

Merged
47 commits merged into
mainfrom
feature/external-functions
Nov 3, 2026
Merged

47 commits merged into
mainfrom
feature/external-functions

Conversation

@nk-coding

Copy link
Copy Markdown
Collaborator

External functions for script contributions

A contribution plugin can implement a script function in its own service instead of shipping a
typed-AST body. Arguments travel over a session, the implementation runs there, and its result
comes back as values the script can use.

New features - script

  • Scripts can call contributed functions implemented in a plugin's service, not just as typed-AST
    bodies. Arguments are in-only and readonly on the service, and so is the model.
  • Records: named fields, structural equality, with copies, usable as parameters, returns and
    fields.
  • val, named arguments and default arguments. No parameter-count limit.

New features - platform and plugins

  • Plugin targets: one address for everything a capability can be offered on, lang:<id> or
    contrib:<id>. The kind is spelled out because the two id spaces overlap — config-mdeo is both
    a language and a contribution.
  • Sessions as a plugin primitive: a named, versioned WebSocket to a target. Platform owns address,
    token, lifetime, liveness, encoding; the protocol inside is the plugin's.
  • One-shot requests addressed at a target too, so a contribution serves the plain request/response
    channel a language plugin already did.
  • script-functions: the protocol language-script defines on top. CBOR, collections keep their
    identity and are sent again only when changed, results are checked whole before they are used.
  • Contributions can declare record and opaque classes, and external implementations per
    signature.
  • The model and its instances are passed to external functions, readonly, uploaded once per round.
  • Three Kotlin modules for writing plugin services: script-functions-protocol, plugin-service,
    script-plugin-service (scriptContribution { function { ... } } DSL). Consumed with
    includeBuild. Keeps number types exactly, unlike the old TS helper.
  • Manifest fingerprints: backend refreshes a plugin in the background when its manifest changes, so
    no more refreshing plugins by hand after a redeploy.
  • Caller deadlines (X-Mdeo-Timeout-Ms), forwarded to plugins, handlers get an AbortSignal.
  • One TimeoutConfig for all backend service timeouts, defaults unchanged.
  • Session and token limits: max open sessions, bounded session token lifetime.
  • Docs: develop/sessions.md, kotlin-plugin-service.md, script-functions-protocol.md.
  • tmux dev scripts for running the whole stack on the host.

Breaking changes

Older services do not work against this backend, so deploy everything together.

  • One error shape everywhere, HTTP and WebSocket: {"error": {"code", "message"}}.
    ExecutionWsErrorCodes merged into ErrorCodes.
  • One scope per capability, the same scope at every hop. New: plugin:file-data:compute,
    plugin:request:send, plugin:execution:start, session:open, session:contributions:read,
    plugin:session:connect. Gone: execution:read.
  • One-shot requests moved from /request/<languageId>/<key> to /request/<kind>/<targetId>/<key>,
    on the backend and on plugin services alike.
  • New tables contribution_targets, plugin_sessions, plugin_manifest_fingerprints, created at
    startup like the rest.
  • A manifest declaring something the platform cannot address is refused, where parts of it used to
    be stored and the rest dropped silently.
  • val and record are keywords now; scripts using either as a name need backticks.
  • Deadline and unavailable failures answer 504 and 503 instead of 400.

Bug fixes

  • Contributed signature types were added to the global table and remapped again through the
    contribution's own array, losing them whenever the arrays did not line up.
  • A contributed function called by name did not get the program pushed as receiver.
  • A return inside a branch that always jumps was no longer type-checked (regression on this
    branch); two more bugs fell out of unifying the control flow analysis.
  • Plugin requests that were busy, late or abandoned answered with an internal error.
  • One caller's deadline could fail a computation shared with others.
  • A session reconnect broke in-flight external calls.
  • Compressed bodies and messages had no bound on what they inflate to.
  • A session refusal could crash a plugin service.
  • Background work and batch entries could fail each other.
  • A narrowed plugin request token lost the computation it belonged to.
  • A caller's token was forwarded to plugins that do not delegate.
  • Internal error detail leaked into what callers are told.
  • A request or file data key could step out of the plugin URL it was put in.
  • A config execution that failed before it was forwarded could not be deleted.
  • The main script in a typed AST closure was not found by the path the execution was given.
  • The script service loaded its closure handler before the plugin context was initialized.
  • A manifest whose contribution declared sessions without an id had them dropped silently; now
    refused, like language plugins already were.
  • A session's owner was not told why the plugin closed it.

Performance

  • Compression on every hop (Ktor, fastify, permessage-deflate, nginx gzip). Largest AST
    97.6 kB → 2.9 kB, config contributions 49 kB → 3.6 kB per request.
  • POST /file-data-batch, same-tick coalescing, in-flight dedupe; a script fetched with its
    imports in one request.
  • Concurrent requests for the same file data share one computation, with cycle detection.
  • Contributions sent by hash to services that cache them.
  • File data validity check as one recursive CTE; file_data.data served as stored, no
    parse/serialize round trip.
  • Metamodel sent to external function services once per session; Model.nameOf memoized.

Adds sessions to plugin targets, external implementations of contributed functions, the script-functions protocol, and Kotlin modules for writing contribution plugin services.
Parameter and return types were added to the global table and then remapped again through the contribution's own types array, which lost them for any contribution whose array did not line up.
Builds once up front and rebuilds the JVM services on request, since
parallel Gradle builds conflict.
One control flow analysis now answers whether a body returns, whether a variable
is initialized and whether a val may be assigned. Contributed parameters, results
and record fields follow one type rule; enums and default values work across the
boundary, varargs contributions are refused. Default arguments use Kotlin's mask
convention, so the parameter limit is gone, and records can be renamed on import.
Composite keys, class identity and plugin targets are structured values instead of
strings, and sessions are no longer part of the plugin API the frontend reads.
Language plugins were already refused for this, contributions still had their
sessions dropped silently. The rule is now one function with a test of its own.
Also corrects the close code KDoc: only the TypeScript session server closes
with 4408, here Ktor's pong timeout does it with a code of its own.
…them too

Requests move from /request/<languageId>/<key> to /request/<kind>/<targetId>/<key>,
the shape sessions already use, and a contribution answers them through its own
handlers. A language request still gets a Langium instance; a contribution, which
has none, is served without one.
A refresh can no longer make a contribution id ambiguous, request and file data keys must be one URL segment, plain and inflated bodies are bounded, malformed session declarations are refused, upstream timeouts answer 504/503, unexpected exception text stays in the log, and file data requests fail after shutdown instead of spinning.
Results are conformed to their declared types at every level, an operation's own collections map to an id only within one result, calls never wait on a closed session, optimizer threads get their own sessions, and the client offers only the agreed protocol version.
Early session messages are held until the handler is ready, config executions get the resolved contribution set, one-shot requests check project and body, a failed Langium build fails its waiter, and large caller deadlines no longer fire at once.
Control flow analysis tolerates blocks not typed yet, class names that would make a type ambiguous are refused, and the formatter keeps arguments in source order.
Copilot AI lite review requested due to automatic review settings September 28, 2026 20:10

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@nk-coding
nk-coding requested a review from szschaler October 1, 2026 01:35
@nk-coding nk-coding closed this pull request by merging all changes into main in e581935 Nov 3, 2026
@nk-coding
nk-coding deleted the feature/external-functions branch November 3, 2026 09:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants