local kernel = require("knl")
local adapter = require("knl_adapter")
local policy = require("policy")
local supervisor = require("supervisor")
local Outcome = kernel.Outcome
local llm = adapter.anthropic:open({
model = "claude-haiku-4-5-20251001",
max_tokens = 1024,
})
local tools = adapter.tools({
{
name = "add",
description = "Add two numbers and return their sum.",
input_schema = {
type = "object",
properties = {
a = { type = "number" },
b = { type = "number" },
},
required = { "a", "b" },
},
handler = function(args)
return tostring(args.a + args.b)
end,
},
})
local device = kernel.device({
llm = llm,
tools = tools,
system = "You are a terse assistant. Use the add tool for any arithmetic.",
})
local MAX_BEATS = 4
local function has_tool_use(out)
for _, block in ipairs(out.content or {}) do
if block.type == "tool_use" then
return true
end
end
return false
end
kernel.session({
owner = "beat-e2e",
budget = { amount = 8, tag = "beats", desc = "one unit per beat" },
}, function(s)
s:append({
kind = "msg_user",
meta = { label = "seed" },
data = { content = "What is 20250904 + 42? Use the add tool, then answer with just the number." },
})
local beats = 0
local last
while beats < MAX_BEATS do
last = kernel.beat(s, device)
beats = beats + 1
print(string.format("[BEAT %d] status=%s", beats, tostring(last.status)))
if not Outcome.is_ok(last) then
break
end
if not has_tool_use(last.out) then
break
end
end
Outcome.match(last, {
ok = function(o)
local text = {}
for _, block in ipairs(o.out.content or {}) do
if block.type == "text" then
text[#text + 1] = block.text
end
end
print("[E2E] final answer: " .. table.concat(text, " "))
end,
refused = function(o)
print("[E2E] refused: " .. tostring(o.reason))
end,
error = function(o)
local detail = o.detail
if type(detail) == "table" then
detail = tostring(detail.message)
if o.detail.kind ~= nil then
detail = o.detail.kind .. ": " .. detail
end
end
print("[E2E] error(" .. tostring(o.kind) .. "): " .. tostring(detail))
end,
stopped = function(o)
print("[E2E] stopped(" .. tostring(o.reason) .. "): grant " .. tostring(o.tag))
end,
})
local grouped = kernel.views.beats(s)
local kinds = {}
for _, ev in ipairs(s:events()) do
kinds[#kinds + 1] = ev.kind
end
local usage = kernel.views.usage(s)[1] or { calls = 0, input_tokens = 0, output_tokens = 0 }
print(
string.format(
"[E2E] beats=%d declared=%d usage: calls=%s in=%s out=%s remaining=%s",
beats,
#grouped,
tostring(usage.calls),
tostring(usage.input_tokens),
tostring(usage.output_tokens),
tostring(s:remaining())
)
)
print("[E2E] history: " .. table.concat(kinds, ","))
for i, row in ipairs(grouped) do
print(
string.format(
"[E2E] beat %d: %s seq %s..%s kinds=%s",
i,
tostring(row.beat),
tostring(row.seq_from),
tostring(row.seq_to),
tostring(row.kinds)
)
)
end
end)
local strong = adapter.anthropic:open({
model = "claude-sonnet-4-5-20250929",
max_tokens = 1024,
})
local stalled = policy.stagnation({ same = 3, no_progress = 2 })
local escalate = policy.escalate({ strong = strong })
kernel.session({
owner = "beat-e2e-policy",
budget = { amount = 8, tag = "beats", desc = "one unit per beat" },
}, function(s)
s:append({
kind = "msg_user",
meta = { label = "seed" },
data = { content = "What is 1918 + 77, and then that plus 5? Use the add tool for each step." },
})
local policied = kernel.device({
llm = llm,
tools = tools,
system = "You are a terse assistant. Use the add tool for any arithmetic.",
fold = policy.window({ tail = 3 }),
filters = { policy.carry({ max_bytes = 400 })(s) },
})
local current = policied
local beats, last, why = 0, nil, nil
while beats < MAX_BEATS do
last = kernel.beat(s, current)
beats = beats + 1
print(string.format("[POLICY BEAT %d] status=%s", beats, tostring(last.status)))
current = escalate(last, current)
if current ~= policied then
print("[POLICY] escalated: the next beat runs on the stronger model")
end
if not Outcome.is_ok(last) then
break
end
if not has_tool_use(last.out) then
break
end
why = stalled(s)
if why ~= nil then
print("[POLICY] stagnation: " .. why)
break
end
end
Outcome.match(last, {
ok = function(o)
local text = {}
for _, block in ipairs(o.out.content or {}) do
if block.type == "text" then
text[#text + 1] = block.text
end
end
print("[POLICY] final answer: " .. table.concat(text, " "))
end,
refused = function(o)
print("[POLICY] refused: " .. tostring(o.reason))
end,
error = function(o)
local detail = o.detail
if type(detail) == "table" then
detail = tostring(detail.message)
end
print("[POLICY] error(" .. tostring(o.kind) .. "): " .. tostring(detail))
end,
stopped = function(o)
print("[POLICY] stopped(" .. tostring(o.reason) .. "): grant " .. tostring(o.tag))
end,
})
local requests = {}
for _, ev in ipairs(s:events()) do
if ev.kind == "llm_request" then
requests[#requests + 1] = ev.data.request
end
end
local last_request = requests[#requests]
print(
string.format(
"[POLICY] beats=%d declared=%d messages_sent_last=%d stagnation=%s escalated=%s",
beats,
#kernel.views.beats(s),
last_request and #last_request.messages or 0,
tostring(why),
tostring(current ~= policied)
)
)
end)
local function settle(session, beat_device, cap)
local last
for _ = 1, cap do
last = kernel.beat(session, beat_device)
if not Outcome.is_ok(last) or not has_tool_use(last.out) then
break
end
end
return last
end
local shared_db = os.tmpname()
kernel.session({
owner = "beat-e2e-supervisor",
budget = { amount = 12, tag = "beats", desc = "one unit per beat, children included" },
store = { sqlite = shared_db },
}, function(s)
local questions = {
"What is 1918 + 77? Use the add tool, then answer with just the number.",
"What is 250 + 6? Use the add tool, then answer with just the number.",
}
local children = {}
for i, question in ipairs(questions) do
children[i] = {
opts = { budget = { amount = 4 } },
fn = function(child)
child:append({
kind = "msg_user",
meta = { label = "seed" },
data = { content = question },
})
local out = settle(child, device, 3)
return child:id(), out.status
end,
}
end
local results = supervisor.parallel(s, children, { timeout_ms = 120000 })
local read = {}
for i, slot in ipairs(results) do
if slot.ok then
read[#read + 1] = slot.values[1]
print(string.format("[SUPERVISOR] child %d: %s", i, tostring(slot.values[2])))
else
local err = slot.err
print(
string.format(
"[SUPERVISOR] child %d failed: %s",
i,
tostring(type(err) == "table" and err.message or err)
)
)
end
end
print(
string.format(
"[SUPERVISOR] children=%d read=%d remaining=%s tree=%d",
#results,
#read,
tostring(s:remaining()),
#kernel.views.tree(s)
)
)
if #read == 0 then
print("[SUPERVISOR] nothing to merge")
return
end
local merged = kernel.device({
llm = llm,
tools = tools,
system = "You are a terse assistant. Use the add tool for any arithmetic.",
fold = supervisor.merge(s, read),
})
s:append({
kind = "msg_user",
data = { content = "Add the two numbers the workers reported, and answer with just the sum." },
})
local final = settle(s, merged, 3)
Outcome.match(final, {
ok = function(o)
local text = {}
for _, block in ipairs(o.out.content or {}) do
if block.type == "text" then
text[#text + 1] = block.text
end
end
print("[SUPERVISOR] final answer: " .. table.concat(text, " "))
end,
refused = function(o)
print("[SUPERVISOR] refused: " .. tostring(o.reason))
end,
error = function(o)
local detail = o.detail
if type(detail) == "table" then
detail = tostring(detail.message)
end
print("[SUPERVISOR] error(" .. tostring(o.kind) .. "): " .. tostring(detail))
end,
stopped = function(o)
print("[SUPERVISOR] stopped(" .. tostring(o.reason) .. "): grant " .. tostring(o.tag))
end,
})
local requests = {}
for _, ev in ipairs(s:events()) do
if ev.kind == "llm_request" then
requests[#requests + 1] = ev.data.request
end
end
local last_request = requests[#requests]
print(
string.format(
"[SUPERVISOR] merged messages=%d usage_rows=%d",
last_request and #last_request.messages or 0,
#kernel.views.usage(s, { sessions = read })
)
)
end)
os.remove(shared_db)
print("[E2E] all_ok")