mirror of
https://github.com/bitbaum/loki.git
synced 2026-12-04 08:58:25 +01:00
* feat(approvals): standing approvals + one-tap decisions from Telegram
Asking Loki in Telegram to put an appointment in the calendar did not put an
appointment in the calendar. It sent you to a website.
Three separate causes, all fixed here:
1. THE CHAT HAD NO ROAD TO THE CALENDAR AT ALL. `gog calendar create` is
blocked on the box by the safety shim, and the fleetcrown skill could only
decide drafts that already existed — it had no way to create one. So the
agent hit the shim, read "route it through the approval queue", and passed
that on as advice. Adds `fc.sh book`, which proposes a structured event
through /api/actions/propose, and rewrites the shim's refusal to name it.
The shim still blocks every external Google write; unattended agents have
gained nothing.
2. THE GATE WAS ON THE WRONG SIDE OF THE REVERSIBILITY LINE. The queue enforced
"nothing runs that the operator did not TAP", which is stricter than the rule
it stands for ("nothing runs that the operator did not APPROVE"). For an
event in your own calendar — private, self-only, undone by deleting it — the
difference was pure latency (2026-08-04: proposed 14:15, booked 21:02).
Standing approvals let that yes be given once, in advance, with three rails
in code rather than in a docblock: eligibility is hard-coded (calendar events
and commitments only — never anything that reaches another person), the
payload is re-checked at decision time (guests mean invitations, so those go
back to the queue), and unknown is never yes (no resolvable time, or a slot
already past, queues). Default on for calendar events; toggle at /approvals.
3. THE NOTIFICATION HANDED BACK A CHORE. "2 actions are waiting… <url>" states
a problem and leaves the answer behind a login. Decisions now arrive as a
card carrying what/when/where plus one-tap Approve / Reject / Edit buttons,
backed by HMAC links scoped to one action and one verb — so a decision needs
no session and works when the gateway is down. Still two messages and only
two: one when a decision is owed, one when the effect really happened.
Also fixes a real data bug: propose_action had no event date fields, so the WHEN
of an appointment had nowhere to go and arrived as prose in `body`, which a Groq
pass then had to guess back — a model re-deriving a value the model one step
earlier knew exactly.
Behaviour that did NOT change: emails, messages, dispatches, and person-book
writes still require a per-item yes, and always will — they are ineligible in
code, and a preference row naming one authorises nothing. Every auto-approval is
audited with `via: standing-rule` and announced, so nothing is done in the
operator's name silently.
Tests pin the parts that must not be trusted: 37 cases on the standing-approval
rails (asserted from the side that hurts — a false `auto: true`), 21 on token
forgery and tampering.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
* fix(chat-skill): the chat's whole Loki skill was dead, and unversioned
Found while testing the previous commit end-to-end, and it is the largest of
the reasons appointments never got added.
On 2026-09-14 the product was renamed to Loki and `calendar-drain.env`'s keys
were renamed with it. The chat agent's skill script, which reads that file, was
not — because it lived only on the box, where no gate in this repo could see
it. Since then every command it offers has aborted on its first line:
line 14: <OLD>_AGENT_TOKEN: <OLD>_AGENT_TOKEN missing in calendar-drain.env
Verified live on bitbaum just now. So from Telegram the operator could not list
the approval queue, could not approve or reject anything, and could not
dispatch — silently, because a skill that errors just makes the agent talk
about something else instead. The only surviving route to a decision was the
website. That is the complaint, and it had a cause older than the UX.
Fixes:
- Renames the skill to `loki` (the retired product name is forbidden in this
repo by scripts/hetzner/test-no-retired-name.sh — which caught this commit,
correctly, when the box files were first imported verbatim) and reads the
env keys the file actually has.
- The installer REMOVES the superseded skill directory rather than leaving it.
Two skills claiming the same job, one of which cannot run, is how the agent
kept choosing the broken one.
- The installer now runs `loki.sh pending` as the openclaw user and fails if it
cannot authenticate. That is the check which would have caught the rename on
the day it happened: run the thing, as the user who runs it, and require real
output. Every other verification in that script only asserted that files
exist, and files existing is exactly what was true throughout the outage.
Also closes a gap in the previous commit: an auto-approved COMMITMENT executed
without telling anyone, while the comments claimed every auto-approval is
announced. Calendar events are announced from where gog succeeds (asynchronous,
on the box); everything else finishes inside enqueueAction, so that is the only
place that knows it finished at all. A standing approval nobody is told about
is not a standing rule, it is a silent one.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
* fix(install): keep the gog shim parseable, and prove the block still fires
The replacement text I substituted into the shim carried double quotes. It
lands inside `block(){ echo "…" >&2; }` — a double-quoted bash string — so a
quote in the middle of it closes that string early and leaves the shim a
syntax error. Every gog call on the box would then fail, reads included, and
the calendar drain's real bookings with them: an install step meant to improve
an error message would have taken out the calendar entirely.
Caught by tracing the sed against the shim's actual text rather than running
it. Two checks added so the next one is caught by the machine:
- `bash -n` on the shim after the edit — it is a shell script we just modified
in place, so prove it still parses before trusting anything else about it.
- Actually attempt a calendar write as the openclaw user and require it to be
REFUSED. The old check grepped for the rule's text, and a rule you can read
in a file is not a rule you have seen refuse anything.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
* fix(ui): use tokens this design system actually defines
The new approvals control and decision page reached for `text-brand`,
`accent-brand`, `bg-surface-hover` and `rounded-card`. None of those exist in
globals.css — the real names are `accent-text`, `surface-raised` and the
`radius-*` scale. Tailwind emits nothing for an unknown utility and reports no
error, so the checkbox would have rendered browser-default blue and the hover
state simply would not have happened, on a page whose entire job is to be
tapped once and trusted.
The design check passes either way: it forbids arbitrary values (`bg-[#fff]`),
which is a different failure from naming a token that was never defined. Caught
by grepping whether any other component uses these names — none did, which is
the tell.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
126 lines
5.4 KiB
TypeScript
126 lines
5.4 KiB
TypeScript
// Pins one-tap approval tokens — the thing standing between a Telegram button
|
|
// and someone else's approval queue.
|
|
//
|
|
// Why it earns a test: this token is a bearer authorisation that travels
|
|
// outside the app and needs no session on the other end. Everything that makes
|
|
// that acceptable is in the verify function — scope to one action, scope to one
|
|
// verb, an expiry, and a signature that actually covers all of it. Each of those
|
|
// is a line of code that a refactor could quietly drop while every happy-path
|
|
// test kept passing, because a token that verifies too easily still verifies.
|
|
//
|
|
// So the assertions are mostly forgeries. The one that matters most is the
|
|
// tampering pair: a token whose actionId or verb was edited must fail, which is
|
|
// the difference between "signed" and "signed over the right bytes".
|
|
process.env.AUTH_SECRET ||= "test-secret-for-action-links";
|
|
|
|
import {
|
|
ACTION_LINK_VERBS,
|
|
actionEditUrl,
|
|
actionLinkUrl,
|
|
createActionLinkToken,
|
|
verifyActionLinkToken,
|
|
LINK_TTL_MS,
|
|
} from "@/lib/actions/action-link";
|
|
|
|
const NOW = Date.parse("2026-09-17T10:00:00Z");
|
|
const ACTION = "11111111-2222-3333-4444-555555555555";
|
|
const USER = "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee";
|
|
const OTHER_ACTION = "99999999-8888-7777-6666-555555555555";
|
|
|
|
let pass = 0;
|
|
const cases: Array<[string, boolean]> = [];
|
|
const check = (name: string, cond: boolean) => {
|
|
cases.push([name, cond]);
|
|
if (cond) pass++;
|
|
};
|
|
|
|
const mint = (verb: "approve" | "reject" = "approve", actionId = ACTION) =>
|
|
createActionLinkToken({ actionId, userId: USER, verb }, NOW);
|
|
|
|
// ── Round trip ──────────────────────────────────────────────────────────────
|
|
const approveToken = mint("approve");
|
|
const claim = verifyActionLinkToken(approveToken, NOW);
|
|
check("a fresh token verifies", claim !== null);
|
|
check("it carries the action id back", claim?.actionId === ACTION);
|
|
check("it carries the user id back", claim?.userId === USER);
|
|
check("it carries the verb back", claim?.verb === "approve");
|
|
check("reject round-trips too", verifyActionLinkToken(mint("reject"), NOW)?.verb === "reject");
|
|
check("only approve and reject exist as verbs", ACTION_LINK_VERBS.length === 2);
|
|
|
|
// ── Expiry ──────────────────────────────────────────────────────────────────
|
|
check(
|
|
"live one millisecond before expiry",
|
|
verifyActionLinkToken(approveToken, NOW + LINK_TTL_MS - 1) !== null,
|
|
);
|
|
check(
|
|
"dead one millisecond after expiry",
|
|
verifyActionLinkToken(approveToken, NOW + LINK_TTL_MS + 1) === null,
|
|
);
|
|
|
|
// ── Forgery and tampering — the reason this file exists ─────────────────────
|
|
check("garbage is refused", verifyActionLinkToken("nonsense", NOW) === null);
|
|
check("empty string is refused", verifyActionLinkToken("", NOW) === null);
|
|
check(
|
|
"an unsigned but well-shaped token is refused",
|
|
verifyActionLinkToken(`${ACTION}.${USER}.approve.${NOW + LINK_TTL_MS}.deadbeef`, NOW) === null,
|
|
);
|
|
check(
|
|
"a token with no signature segment is refused",
|
|
verifyActionLinkToken(`${ACTION}.${USER}.approve.${NOW + LINK_TTL_MS}`, NOW) === null,
|
|
);
|
|
|
|
// Swap the action id but keep the valid signature: the classic "approve a
|
|
// DIFFERENT row with a link you were legitimately given" attack.
|
|
const parts = approveToken.split(".");
|
|
check(
|
|
"swapping the action id invalidates the token",
|
|
verifyActionLinkToken([OTHER_ACTION, parts[1], parts[2], parts[3], parts[4]].join("."), NOW) ===
|
|
null,
|
|
);
|
|
check(
|
|
"swapping the user id invalidates the token",
|
|
verifyActionLinkToken([parts[0], OTHER_ACTION, parts[2], parts[3], parts[4]].join("."), NOW) ===
|
|
null,
|
|
);
|
|
// Verb escalation: a reject link must never be editable into an approve link.
|
|
const rejectParts = mint("reject").split(".");
|
|
check(
|
|
"editing reject → approve invalidates the token",
|
|
verifyActionLinkToken(
|
|
[rejectParts[0], rejectParts[1], "approve", rejectParts[3], rejectParts[4]].join("."),
|
|
NOW,
|
|
) === null,
|
|
);
|
|
check(
|
|
"extending the expiry invalidates the token",
|
|
verifyActionLinkToken(
|
|
[parts[0], parts[1], parts[2], String(NOW + LINK_TTL_MS * 10), parts[4]].join("."),
|
|
NOW,
|
|
) === null,
|
|
);
|
|
check(
|
|
"an unknown verb is refused even if it were signed",
|
|
verifyActionLinkToken(`${ACTION}.${USER}.delete.${NOW + LINK_TTL_MS}.x`, NOW) === null,
|
|
);
|
|
|
|
// Two different actions must not produce the same token — a signature that
|
|
// ignored its payload would still pass every test above but this one.
|
|
check("different actions produce different tokens", mint("approve", OTHER_ACTION) !== approveToken);
|
|
check("different verbs produce different tokens", mint("reject") !== approveToken);
|
|
|
|
// ── URLs are absolute (they are read inside Telegram) ───────────────────────
|
|
check(
|
|
"the decision URL is absolute and points at /a/",
|
|
/^https:\/\/[^/]+\/a\/.+/.test(
|
|
actionLinkUrl({ actionId: ACTION, userId: USER, verb: "approve" }),
|
|
),
|
|
);
|
|
check(
|
|
"the edit URL is absolute and focuses the row",
|
|
actionEditUrl(ACTION).startsWith("https://") && actionEditUrl(ACTION).includes(`focus=${ACTION}`),
|
|
);
|
|
|
|
for (const [name, ok] of cases) console.log(`${ok ? "✓" : "✗"} ${name}`);
|
|
console.log(`\n${pass}/${cases.length} passed`);
|
|
if (pass !== cases.length) process.exit(1);
|