Skip to main content
Version: 0.16.0

More than a question

A question is one of four things a Plugin may declare. It may also be told to do something, with a command; say that something happened, with an occurrence; and publish live data, with a stream. Each is declared, each is governed by a capability, and each reaches the Plugin only if the operator granted it (the families).

A PluginDeclaresDoesGoverned by a capability such as
is askeda questionanswers it, with bytes of its ownquery.hello.answer
is tolda commandacknowledges it: accepted, done or failed, with a linecommand.set.accept
reportsan occurrencereports it with a severity it states, a line and bytes of its ownevent.changed.report
publishesa streamsends data on it, once the Core has activated itstream.<name>.publish

Told, and saying so​

The greeter now also accepts the command greeter.set, whose payload is the greeting to use, and reports the occurrence greeter.changed when the greeting changes. The question answers with the greeting in force. The whole program, in each language:

plugin/main.go
// Command greeter is a Plugin that answers who it is greeting, and is told how to greet.
package main

import (
"context"
"fmt"
"os"
"os/signal"
"syscall"

"github.com/yoke-project/yoke-sdk-go/plugin"
)

func declaration() plugin.Declaration {
return plugin.Declaration{
ID: "com.example.greeter",
Commands: []string{"greeter.set"},
Queries: []string{"greeter.hello"},
Occurrences: []string{"greeter.changed"},
Capabilities: []plugin.Capability{
{Name: "command.set.accept", Governs: plugin.Object{Command: "greeter.set"}},
{Name: "query.hello.answer", Governs: plugin.Object{Query: "greeter.hello"}},
{Name: "event.changed.report", Governs: plugin.Object{Occurrence: "greeter.changed"}},
},
}
}

// A change of greeting is routine: the author states its severity, since the library chooses none.
var changed = plugin.SeverityOf(10)

// greeter is the Plugin's own state: the word it greets with.
type greeter struct{ greeting string }

func (g *greeter) handle(u *plugin.Unit, event any) error {
switch e := event.(type) {
case plugin.Question:
if e.Type == "greeter.hello" {
return u.Answer(e, []byte(g.greeting+", "+string(e.Payload)))
}
case plugin.Command:
if e.Type == "greeter.set" {
if len(e.Payload) == 0 {
return u.Ack(e, plugin.Failed, "a greeting cannot be empty")
}
g.greeting = string(e.Payload)
line := "greeting with " + g.greeting
if err := u.Ack(e, plugin.Done, line); err != nil {
return err
}
return u.Report("greeter.changed", changed, line, nil)
}
}
return nil
}

func main() {
if len(os.Args) == 2 && os.Args[1] == "--manifest" {
os.Stdout.Write(declaration().Manifest())
return
}
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGTERM, os.Interrupt)
defer stop()
u, err := plugin.Start(ctx, declaration())
if err != nil {
fmt.Fprintln(os.Stderr, "greeter:", err)
os.Exit(1)
}
u.Health(0, "ready to greet")
g := &greeter{greeting: "hello"}
for {
select {
case event, open := <-u.Events():
if !open {
return
}
if err := g.handle(u, event); err != nil {
fmt.Fprintln(os.Stderr, "greeter:", err)
}
case <-ctx.Done():
u.Close()
<-u.Done()
return
}
}
}

What is new is the declaration and the handling of each event:

  • A command is acknowledged once, at least. done when it is carried out, failed when it cannot be, each with a line for a person; accepted first when the work outlasts the wait a caller is given, followed by one done or failed. An acknowledgement carries no result: a result is an answer.
  • An occurrence carries the severity its author states, from 0 to 99, higher being more serious. The library chooses none, and an occurrence reported without one is refused (severity).
  • The state is the Plugin's own. The greeting lives in the program; the Core neither reads nor keeps it, and a Plugin that is restarted starts from hello again.

Build it and print its Manifest again, as on the program. It now declares three capabilities:

plugin/com.example.greeter.yaml
manifest: 1
id: com.example.greeter
protocol: 1
commands:
- id: greeter.set
queries:
- id: greeter.hello
occurrences:
- id: greeter.changed
capabilities:
- name: command.set.accept
governs:
command: greeter.set
- name: query.hello.answer
governs:
query: greeter.hello
- name: event.changed.report
governs:
occurrence: greeter.changed

Try it​

Write the Manifest again and take the Plugin into try/ again, as in after a change, and declare a channel there: a command reaches a Plugin from an interface, and an interface attaches to a channel. From greeter/:

build/x86_64/greeter --manifest > plugin/com.example.greeter.yaml
yoke-compose add channel cli --dir try --transport local --clients multiple
yoke app run --from try

By hand, the same is done to greeter-try/, with the digest written again (after a change).

Grant the three capabilities, and restart the unit so that it is admitted with them:

yoke plugin grant com.example.greeter command.set.accept
yoke plugin grant com.example.greeter query.hello.answer
yoke plugin grant com.example.greeter event.changed.report
yoke unit restart greeter

An operator sends a command with yoke, and an interface client attached to the channel with its library's Command call: in Go, a.Command(ctx, "greeter", "greeter.set", []byte("ciao")) (an interface client).

echo -n ciao | yoke unit command greeter greeter.set --input -
done greeting with ciao

Told an empty greeting, it acknowledges failed with a greeting cannot be empty, and yoke exits as a refusal. The question then answers with the new greeting:

echo -n world | yoke unit ask greeter greeter.hello --input -
ciao, world

And the occurrence is in the instance's log, under the unit, its incarnation and the severity the greeter stated:

yoke log query --unit greeter
…
2026-11-02T20:37:10.786896563Z greeter/2 [10] greeting with ciao

A report the Plugin was not granted is refused by the Core, and the Session goes on. The library hands the refusal to the Plugin as an event, correlated to the message it refused; the greeter above ignores it. Granted everything but event.changed.report, a greeter that wrote each refusal to its standard error would leave this in the log:

2026-11-02T20:48:35.562801791Z greeter/2 [10] greeter: refused: u-5 scope.withheld: the unit's grant does not cover the occurrence greeter.changed

What a Plugin does about what it was not granted is its own to decide: carry on without it, run reduced, or exit (admission).

Streams​

A stream is live data a Plugin publishes: samples, frames, a measurement as it happens. It is declared with what its data tolerates — loss, reordering — and the Plugin sends on it only once the Core has activated it, which the library hands over as an event naming the stream, its transport and its address. Before that there is nowhere to send, and the library refuses rather than make a transport of its own. An operator starts and stops a stream with yoke stream start and yoke stream stop, and an interface subscribes to it through a channel (streams and transports, streams to clients).

Needs​

What a Plugin must reach on the host is declared as a need, by class and never by a particular thing: device:instrument, storage:datasets, secret:token, display. That this program opens a serial device is true of it wherever it runs; which device a copy of it opens is a fact about one deployment, supplied by the application that runs it. A Plugin declares the class, and the application binds the thing (the Manifest, how a unit gets a secret).