Skip to content

Sessions ​

A session is a long-lived binary connection between an execution and a plugin. It exists for the cases a request cannot serve: a conversation that goes back and forth many times, carries data that only makes sense in sequence, or has to outlive a single call without paying to re-establish itself each time.

The platform owns the connection. It does not own what travels on it.

No bundled plugin declares a session yet

Sessions are implemented end to end — declaration, connect endpoint, token, endpoint, pooling and client. Their first protocol is the script language's script-functions, which answers external function implementations.

What the platform owns, and what it does not ​

The platform ownsThe plugin owns
The address a session is reached atThe messages that travel on it
The token that authorizes it, and its lifetimeTheir encoding
Version negotiation at the handshakeCorrelation between a question and its answer
Keepalives, and deciding a connection is deadWhat an error looks like
Taking a Langium instance out of the pool and putting it backWhat the session is for

There is deliberately no envelope inside a session. Nothing the platform writes wraps what you send, nothing it reads interprets it, and there is no platform-defined error message. If two conversations are not the same conversation, declare two session types rather than one type with a prefixed vocabulary — the name is free, and a second type costs nothing.

Addressing a target ​

Sessions are declared on things that already have ids: language plugins and server contribution plugins. Both id spaces are flat and they overlap — config-mdeo is a language id and a contribution id — so every address spells out which it means:

lang:script
contrib:script-functions

That same string is used everywhere one is written down: the URL segments of both endpoints, the target claim of a session token, the keys of ServiceConfig.sessions, log lines and error messages. @mdeo/plugin exports formatPluginTarget and parsePluginTarget; com.mdeo.common.model.PluginTarget is their Kotlin twin.

Contribution ids are unique within a project. Adding a plugin whose contribution id is already taken is refused with PluginContributionIdConflict, because an ambiguous address would otherwise be resolved by whichever row came back first.

Declaring a session ​

A session type is declared in the plugin manifest, beside whatever declares the target. The protocols below are made up for illustration; the one real protocol so far, script-functions, is declared by script contributions for you.

ts
// On a language plugin
const modelLanguagePlugin: LanguagePlugin = {
    id: "model",
    // …
    sessions: {
        validation: { protocol: "model-validation", versions: [1] }
    }
};

// On a server contribution plugin
const contribution: ServerContributionPlugin = {
    id: "geo",
    // …
    sessions: {
        calls: { protocol: "geo-queries", versions: [2, 1] }
    }
};
FieldMeaning
protocolName of the protocol, owned by whoever defines the contract — script-functions belongs to the script language, not to the platform
versionsVersions this side can speak, most preferred first

The manifest is the only place a session type is written down. The connect endpoint hands a caller the protocol and versions from there, and the session endpoint negotiates against the same values, so the two cannot drift.

Serving a session ​

Writing the service in Kotlin

A Kotlin plugin service serves sessions with :plugin-service: a SessionHandler with the same open/onMessage/onClose shape, registered on its contribution. For script-functions, scriptContribution registers the handler for you.

In a TypeScript service, register a handler under the target address and the session name:

ts
const config: ServiceConfig<ScriptServices> = {
    // …
    sessions: {
        "contrib:geo": {
            calls: {
                open(ctx) {
                    return {
                        onMessage(data) {
                            ctx.send(answer(data, ctx.version));
                        },
                        onClose(reason) {
                            releaseWhateverWasHeld(reason);
                        }
                    };
                }
            }
        }
    }
};

open is called once, when the connection has been authorized and a version agreed. What it returns handles that one connection until it ends.

The session context ​

In TypeScript, open receives:

FieldMeaning
projectId, executionIdWho the session belongs to. A session never outlives its execution
target, sessionNameWhat was addressed
versionThe version both sides agreed on
jwtThe token the session was opened with
serverApiBackend access, already pointed at the project
instanceFor a lang: target, the Langium instance this session owns, with the project's contribution plugins loaded
send(data)Writes one message
close(reason)Ends the session

The Kotlin SessionContext has projectId, executionId, target, sessionName and version as above, token for the token the session was opened with, and suspending send and close. It has no serverApi or instance: a Kotlin service holds no Langium instances.

A handler registered without a matching declaration — or a declaration without a handler — is reported as a warning at service start. Both are wiring mistakes that would otherwise only show up when an execution tries to connect.

Opening a session ​

An execution node never dials a plugin directly. It asks the backend, with the token it holds for the run:

POST /api/projects/{projectId}/sessions/{kind}/{targetId}/{name}/connect

Scope: session:open, on a token bound to an execution.

json
{
  "url": "ws://workbench:80/plugin/geo/ws/sessions/contrib/geo/calls",
  "protocol": "geo-queries",
  "versions": [2, 1],
  "token": "eyJ…",
  "expiresAt": 1789000000
}

The backend decides whether the project has that target at all, so a run cannot reach a plugin the project has not enabled. The token it returns carries the plugin:session:connect scope and names the one target and session it may be spent on; it is bound to the execution, so it stops being accepted the moment the run ends.

From Kotlin, SessionResolver makes that call and caches the answer for the run, refetching the token as it nears its expiry. SessionClient dials the endpoint, keeps it alive, and reconnects when it drops, up to three times with a growing delay. A reconnect restores the transport, not the conversation: connectionNumber changes with every reconnect, which tells a protocol to re-establish whatever state the new connection starts without. When the plugin ends or refuses a session, onClosed receives its close code and reason.

The session endpoint ​

GET /ws/sessions/:kind/:targetId/:name        (WebSocket upgrade)

The token goes in Authorization: Bearer; a token in the query string is not read, so it never lands in a proxy's access log. A service implementing this endpoint itself verifies the token as an RS256 JWT against the keys at <BACKEND_API_URL>/.well-known/jwks.json, requires the configured issuer, and accepts it only when its scope contains plugin:session:connect, its target and session claims name exactly the addressed target and session, and it carries projectId and executionId. The caller lists the versions it can speak in ?v=2,1, most preferred first, and the first one the plugin also declares wins. The service does not report which one that was, so SessionClient picks the version from what the connect endpoint declares and offers only that one; a plugin redeployed without it refuses with 4409 instead of silently speaking another.

A connection that never becomes a session is closed with a code and a reason:

CodeMeaning
4401Token missing, invalid, or issued for a different target or session
4404This service serves no such target or session
4409No version both sides can speak
4503No capacity to hold another session: MAX_SESSIONS (default 64) are open, or no Langium instance is free for a lang: session
4408The peer stopped answering keepalives (TypeScript services; Kotlin services close an unresponsive connection through Ktor's own timeout)

Everything a plugin itself closes a session with is its own business.

Lifetime and liveness ​

Sessions are exempt from request timeouts. A call can legitimately take as long as the work takes, so there is no deadline on a message — liveness comes from keepalives instead. Both sides ping every 30 seconds and give up on a peer that has not answered for 90.

This is not optional. The reverse proxy in front of a plugin service drops connections that have been idle for an hour, and a session doing long stretches of local work would otherwise be collected mid-conversation.

Messages larger than 1 KiB are compressed with permessage-deflate when both sides offer it, which every platform client and server does. It is negotiated per connection, so a peer without it still works.

Sessions and the Langium pool ​

A lang: session takes its Langium instance out of the pool for the lifetime of the connection and resets it back in when the session closes. That is the only way to hold parser and scope state across messages, but it means the instance is unavailable to the request path for as long as the run lasts.

Two settings keep that from starving the service:

VariableDefaultMeaning
MAX_SESSION_INSTANCES2How many instances open sessions may hold at once
LANGIUM_ACQUIRE_TIMEOUT_MS30000How long an ordinary request waits for a free instance before failing

The session budget is refused when exhausted rather than queued: a caller waiting for a slot held by a session that has not finished would be waiting for the rest of a run. The request path's wait, by contrast, is bounded — without a bound, one instance that is never released stalls every request behind it with nothing to show for it.

The instance is picked exactly like one for a request: it has the contribution plugins the project registers for that language loaded. Requests carry that list in their body because the backend relays them, but a session is dialed directly. So while it opens the connection, the language service fetches the list itself, with the session token:

GET /api/projects/{projectId}/sessions/lang/{languageId}/{name}/contribution-plugins

Scope: session:contributions:read, which only the token issued for exactly that lang: session carries. The caller never sends the list, so it cannot choose what gets loaded. When the fetch fails, the connection is closed with 4503.

contrib: sessions hold no Langium instance and draw on neither budget.

Deployment ​

A session is an ordinary WebSocket upgrade on the plugin service's own port, so it needs what the execution WebSocket already needs. The execution node dials the URL the backend resolved for the plugin. For a plugin registered with a relative URL such as /plugin/geo/, that is the URL behind INTERNAL_PLUGIN_BASE_URL, which is the workbench's nginx in both the compose and the Kubernetes deployment; the Gateway API route in Kubernetes only carries browser traffic. Each location ^~ /plugin/…/ block already forwards upgrades with a 3600-second read timeout, so a new plugin service needs such a block, or has to be registered with an absolute URL the execution nodes can reach.

A message may be at most 512 MiB once inflated, on both stacks. A session token is valid for JWT_EXPIRATION_SECONDS, at most five minutes, and only while its execution runs. A plugin service cannot tell that a run ended, so the short lifetime is what keeps a leaked token from opening sessions later. It is only needed to open a connection; SessionResolver fetches a new one before a reconnect needs it.

See also ​

Released under the terms of the repository licence.