The debugger
The debugger steps through the evaluation of a policy in the browser, as an IDE steps through Kotlin.
At each step it shows:
- the line in the source;
- the bindings of the vars;
- the part of the input that the step reads;
- the output document as the rules make it.
It is an http4k HttpHandler in rego4k-server-debugger. The page uses
Datastar, a small library that updates the page from the server.


Run it alone
Give the debugger an engine with a RegoCache. Each step compiles the modules again, and the cache makes that
free.
val rego = Rego(
Builtins.Pure.stdlib(Re2Engine) + Builtins.Provided.stdlib,
cache = RegoCache(maxBytes = 64L * 1024 * 1024, maxEntries = 256),
)
val server = Debugger(rego).asServer(SunHttp(port)).start()Alone.kt, lines 17 to 21, on GitHubThe keys are the keys of IDE debuggers:
- F11 steps into.
- F10 steps over.
- Shift+F11 steps out.
- F8 continues.
Step back goes back one step. A click on the number of a line sets a breakpoint.
The page has the layout of an IDE debugger. The panel on the left has a tab for the bundles and a tab for the files, and it closes. The query and the step buttons are above the code.
On the right, there are three sections:
- Variables shows the locals, the input, the data, the output document and the result as trees that you expand.
- Call stack has a row for each step, with its operation, its expression and its file and line. Click a row to go to its step.
- Breakpoints lists the breakpoints, and removes one.
On the pages of your application
Give the debugger three things:
- a
BundleCatalogwith your bundles; - a
PageFramewith your page around the debugger; - a path for
Mount.
val catalog = object : BundleCatalog {
override fun bundles() =
listOf(CatalogBundle("authz", revision = "42", hash = "6c1f"))
override fun files(name: String) =
if (name != "authz") null else listOf(
SourceFile("authz/policy.rego", authzPolicy),
SourceFile(
"authz/examples/viewer-reads.input.json",
"""{"method": "GET", "user": {"roles": ["viewer"]}}""",
),
)
}
val frame = PageFrame { debugger ->
head {
title("Admin: policies")
link(rel = "stylesheet", href = "/assets/admin.css")
debugger.head(this)
}
body {
nav { a("/admin") { +"Admin" }; a("/admin/policies") { +"Policies" } }
ol("breadcrumbs") { li { a("/admin") { +"Admin" } }; li { +"Debug" } }
main { debugger.content(this) }
}
}
val debugger = Debugger(rego, catalog = catalog, frame = frame)
val application = routes(
"/admin" bind GET to adminHome,
adminLogin.then(Mount("/admin/policies/debug", debugger).route),
)AdminPages.kt, lines 32 to 62, on GitHubThe frame must call debugger.head in the head and debugger.content in the body. Each action of the page
patches only elements inside the content, so your navigation and breadcrumbs stay.
The page uses only relative URLs, so it works below any path. The URL of the page must end with /.
GET <path>/?bundle=authz&query=data.authz.allow opens a bundle with a query. The example inputs of a bundle show
as buttons above the input editor: the constant inputs of its test rules, and its files examples/<name>.input.json.
On the decision server
DebuggerModule.module(bundles) puts the debugger on the decision server, with the hosted bundles as its catalog.
The authentication filter of the server checks each request to the debugger too.
val bundles = HostedBundles(rego, { Bundle.read(policies) })
val server = DecisionServer(
authentication,
listOf(
OpaApi.module(bundles),
DebuggerModule.module(bundles, "/debugger", frame)
)
)ServerDebugger.kt, lines 16 to 23, on GitHubYour own look
The stylesheets of the debugger, the tree shaking page and the page of the OpenAPI descriptions take each colour, font
and radius from a design token: a custom property of tokens.css in rego4k-server-core (Tokens of the package rego4k.server.styles).
The tokens have a light and a dark theme. The dark theme applies when the system prefers it and <html> has no
data-theme="light", or when <html> has data-theme="dark", as on this site.
| Token | What it colours or sets |
|---|---|
--bg, --panel |
The page, and a panel on it: the explorer, an editor, the code view |
--text, --muted, --border |
The text, the text of less importance, and the lines |
--accent, --accent-text |
The accent, for example the Debug button, and the text on it |
--current, --current-text |
The line of the current step, and the box around the source text of the step |
--read, --read-border |
The part of the input or the data that the last read examined |
--changed, --added, --deleted |
A value that a rule gave, an added line, a deleted line |
--breakpoint, --error |
A breakpoint, and an error |
--tok-keyword, --tok-string, --tok-number, --tok-comment, --tok-var, --tok-operator, --tok-punctuation |
The tokens of Rego and JSON |
--selection, --shadow |
The selected text of an editor, and the shadow of a list over the page |
--sans, --mono |
The font of the text and the font of the code |
--radius-mark, --radius-control, --radius, --radius-group |
The radius of a mark, a small control, a panel, a group of controls |
Give the pages your own stylesheet of tokens. The pages load it after the default tokens, so a rule in it changes
any token. DebuggerModule.module, TreeShaking.module and ApiDocs.module take the same tokens.
// /brand/tokens.css has :root { --accent: #7b1fa2; --radius: 2px; }
val tokens = Tokens(listOf("/brand/tokens.css"))
val debugger = Debugger(rego, DefaultComponents(tokens))OwnLook.kt, lines 66 to 68, on GitHubYou can also set a token on an element around the debugger, as the frame below does.
DebuggerComponents changes the HTML of the parts of the debugger. It has one kotlinx.html function for each part,
and DefaultComponents is the look of the screenshot. A subclass can draw one part again, for example the toolbar
above the code:
/**
* The look of the debugger, with plain buttons in the toolbar.
* The other parts keep the look of [DefaultComponents].
*/
class PlainButtons : DefaultComponents() {
override fun FlowContent.toolbar(toolbar: Toolbar) {
textEditor(this@PlainButtons, toolbar.query)
for (command in toolbar.buttons) {
button(classes = "my-button") {
id = command.id
disabled = !command.enabled
on(command.action)
+command.label
}
}
}
}OwnLook.kt, lines 22 to 38, on GitHubPageFrame puts your page around the debugger. This frame adds a header, and sets the colours and the fonts on the
main element:
val frame = PageFrame { debugger ->
head {
title("Policies - My application")
debugger.head(this)
}
body {
header { +"My application" }
main {
// the colours and the fonts of the debugger
style = "--accent: #0b7a53; --mono: ui-monospace, monospace"
debugger.content(this)
}
}
}
val debugger = Debugger(rego, PlainButtons(), frame = frame)OwnLook.kt, lines 44 to 58, on GitHubThe parts can also show the files of a bundle on your own pages, without the debugger. The demo does this on the page of each policy.
The web components
The pages of the debugger have no JavaScript outside a web component of Rocket, the components of Datastar. The server writes the element of each component and its children with kotlinx.html. The script of the component defines its tag in the browser.
| Tag | Script | Attributes and signals | Kotlin |
|---|---|---|---|
<rego-editor> |
rego-editor.js |
language (rego, json or text) and errors (the id of the element with the parse errors). The textarea binds the signal of its field. |
textEditor |
<scroll-into-view> |
scroll-into-view.js |
None. The element is hidden, and the box around its parent scrolls to it. | scrollIntoView |
<theme-switch> |
theme-switch.js |
The action @choose(choice), and the local signals $$choice (light, dark or system) and $$dark. Your page writes the buttons. |
Themes |
The debugger and the tree shaking page load each script, and serve it next to datastar.js (Script in
rego4k.debugger.scripts). Thus your PageFrame can use each component. To use a component on a page of your own,
do these steps:
- Write its element with the Kotlin function of the table, or with
webComponent(tag) { ... }. - Load
datastar.jsand the script of the component as modules in theheadof the page. - Serve the scripts with
Script.route, or load them from the path of the debugger.
<theme-switch> sets data-theme on <html>. Before the first paint, the small inline script Themes.FIRST_PAINT
sets the stored theme. Write it with firstPaint() at the start of the head. If your Content-Security-Policy allows
no inline script, add its hash Themes.FIRST_PAINT_SOURCE to script-src. Datastar needs 'unsafe-eval'.
The routes and the signals
The debugger keeps no state on the server. The state of a debug session is in the Datastar signals of the page: the files, the query, the input, the breakpoints, the current step and the recorded values of the replay.
Each action is a Datastar @post to a route relative to the page, for example api/commands/step_into,
api/breakpoints or api/edit (Action in rego4k.debugger.models). The request sends all the signals, and the
response patches the signals and the page. Thus any instance of the server can answer any request.
To drive a different UI from the same routes, do these steps:
- Implement
DebuggerComponents, or make a subclass ofDefaultComponents. Each function draws one part from its typed model, andlayoutplaces the parts. - Start each action with
on(action), and bind each text field withbind(field). The models give the actions and the fields, so each action goes to a route of the server. - Give the components to
Debugger(rego, components). Put the page in your own layout withPageFrame.
Each response replaces the element with the id debugger, so the components cannot change the ids that a response
patches. ComponentsTest steps through a policy with other components.
The README of rego4k-server-debugger lists the parts, and explains how the
debugger works.