Files
loki/scripts/test/feedback-trust.ts
CatoandClaude Opus 5.5 b912cb4d91 feat(notify): every person chooses what reaches their own Telegram (#1543)
* feat(notify): every person chooses what reaches their own Telegram

Loki is multi-tenant, but Telegram was one hard-wired chat (the operator's)
on a bot whose incoming messages belong to OpenClaw. Now each account sets
it up in Settings → Notifications → Telegram:

- Connect Telegram opens Loki's notification bot with a one-time code;
  pressing Start links that chat (POST /api/telegram/webhook, verified by
  Telegram's secret-token header). No chat ids, nothing to type. /stop or
  blocking the bot unlinks. The webhook registers itself on first use.
- Six kinds of message (config/notify-categories.ts), each on/off per
  person; "servers" is shown to the operator only.
- Every app sender now goes through the gateway as that person
  (lib/notify/person.ts → deliverNotice): approvals, feedback, fixes, runs,
  studio, build requests, sign-ups, autopilot, site live, gateway down. The
  gateway applies the person's choices, sends to their own linked chat, and
  falls back to the operator's old chat for the operator only.
  scripts/test/one-telegram-door.ts fails the build on a direct send.
- Buttons Loki composes from its own records (a customer's own site) are
  trusted; links from outside still meet the host allowlist.
- Migration 0132 adds the link and choices to notification_preferences.
- New env: TELEGRAM_NOTIFY_BOT_TOKEN, TELEGRAM_NOTIFY_BOT_USERNAME (checked as
  a pair at boot). Until set, Settings says Telegram is not set up and the
  operator's existing chat keeps working.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmtzUbLhg3oqt4X1NaN

* fix(openclaw): the heartbeat follows the same one-tap contract

The box audit (openclaw-day-plan run 3) found the sender of "Gateway was
not running. Restarted automatically. All good now.": the OpenClaw
workspace's tools/health-guardian.sh, not in any repo, whose status lines the
agent relays. The contract now goes on HEARTBEAT.md too (between markers,
backed up, idempotent), and the installer prints how health-guardian is
started and its speaking lines, numbers and secrets redacted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmtzUbLhg3oqt4X1NaN

* style(drizzle): format the generated 0132 snapshot and journal

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019MDtmtzUbLhg3oqt4X1NaN

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-11-02 09:05:10 +01:00

117 lines
5.4 KiB
TypeScript

/**
* A stranger's words reach a builder only by a person's tap.
*
* Four holes, found by tracing a visitor's note end to end (2026-10-19):
* a visitor could label their note as Loki's own finding; an overnight
* allowance built strangers' notes with nobody looking; opening a stranger's
* tracking link started their build; and every customer's feedback went to
* the operator's Telegram. Each is pinned here.
*/
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import { createAgentPass, verifyAgentPass } from "../../src/lib/feedback/agent-pass";
import { composeReviewPrompt } from "../../src/lib/feedback/ai-review-prompt";
import { FEEDBACK_SOURCE, waitsForATap } from "../../src/lib/constants/statuses";
process.env.AUTH_SECRET ??= "test-secret-for-agent-pass";
process.env.NEXT_PUBLIC_APP_URL ??= "https://loki.example";
const read = (p: string) => readFileSync(p, "utf8");
const project = "8d96d258-36f2-4b47-a897-9a5baeb6c296";
const other = "9e96d258-36f2-4b47-a897-9a5baeb6c296";
const now = Date.parse("2026-10-19T08:00:00Z");
// ─── 1. Who filed a note is the server's call ────────────────────────────────
const pass = createAgentPass(project, FEEDBACK_SOURCE.AI_REVIEW, now);
assert.equal(verifyAgentPass(pass, project, now), FEEDBACK_SOURCE.AI_REVIEW);
assert.equal(verifyAgentPass(pass, other, now), null, "a pass proves one project");
assert.equal(verifyAgentPass(pass, project, now + 49 * 3600_000), null, "it expires");
assert.equal(verifyAgentPass(undefined, project, now), null);
assert.equal(verifyAgentPass("", project, now), null);
const [p, , exp, sig] = pass.split(".");
assert.equal(
verifyAgentPass(`${p}.synthesizer.${exp}.${sig}`, project, now),
null,
"a reviewer's pass cannot be relabelled as the synthesizer's",
);
assert.equal(
verifyAgentPass(`${p}.owner.${exp}.${sig}`, project, now),
null,
"never the owner — that is the owner pass's job",
);
const ingest = read("src/app/api/feedback/route.ts");
assert.match(
ingest,
/verifyAgentPass\(data\.agentPass, token\.projectId\) \?\? FEEDBACK_SOURCE\.VISITOR/,
"the source comes from a verified pass, else visitor",
);
assert.doesNotMatch(ingest, /data\.source/, "the claimed source is never read");
const prompt = composeReviewPrompt("https://site.example/", { id: project, name: "Site" }, "fcw_x");
const promptPass = /"agentPass":"([^"]+)"/.exec(prompt)?.[1];
assert.ok(promptPass, "the reviewer's prompt carries its pass");
assert.equal(verifyAgentPass(promptPass, project), FEEDBACK_SOURCE.AI_REVIEW);
assert.doesNotMatch(prompt, /"source":/, "and no longer asserts a source");
const synth = read("src/app/api/projects/[id]/feedback/synthesize/route.ts");
assert.match(synth, /createAgentPass\(project\.id, FEEDBACK_SOURCE\.SYNTHESIZER\)/);
assert.doesNotMatch(synth, /"source":"/);
// ─── 2. An allowance never builds a stranger's note ──────────────────────────
assert.equal(waitsForATap(FEEDBACK_SOURCE.VISITOR), true);
assert.equal(waitsForATap(null), true, "a row older than sources is nobody's word");
assert.equal(waitsForATap(FEEDBACK_SOURCE.OWNER), false);
assert.equal(waitsForATap(FEEDBACK_SOURCE.AI_REVIEW), false);
assert.equal(waitsForATap(FEEDBACK_SOURCE.CLIENT), true, "a client's ask waits for the owner");
assert.equal(waitsForATap(FEEDBACK_SOURCE.MEMBER), true, "so does any other seat's");
const night = read("src/lib/autopilot-night.ts");
assert.match(
night,
/if \(planned\.verdict\.auto && planned\.f\.open\.some\(\(r\) => waitsForATap\(r\.source\)\)\) \{\s*planned = await planFor\(false\);/,
"a plan nobody will tap is re-made without strangers' notes",
);
assert.match(night, /\(strangers \|\| !waitsForATap\(f\.source\)\)/);
// ─── 3. Opening a tracking link starts nothing ───────────────────────────────
const claim = read("src/app/api/feedback/claim/route.ts");
assert.doesNotMatch(claim, /implementFeedback/, "a link never builds");
assert.ok(
claim.indexOf("access?.canEdit") < claim.indexOf("claimFeedbackForReporter("),
"someone who builds the project does not take the reporter's claim",
);
// ─── 4. Each account's news reaches its own channels ────────────────────────
for (const f of [
"src/lib/feedback/notify-new.ts",
"src/lib/feedback/notify-shipped.ts",
"src/lib/feedback/notify-needs-you.ts",
"src/lib/orchestration/notify-close.ts",
"src/lib/autopilot-night.ts",
]) {
const src = read(f);
assert.doesNotMatch(
src,
/selfTelegramTarget\(/,
`${f}: per-account news never goes to the box-wide chat`,
);
// Per-account news goes to THAT account's own Telegram: through the gateway
// as that person (lib/notify/person.ts), never to a chat chosen here.
assert.match(
src,
/notifyPerson\(/,
`${f}: Telegram only through the gateway, as the account's person`,
);
assert.doesNotMatch(src, /sendTelegramMessage\(/, `${f}: no direct Telegram send`);
}
// And the gateway falls back to the operator's old chat for the operator only.
const deliver = read("src/lib/notify/deliver.ts");
assert.match(
deliver,
/telegramTargetFor\(userId\)/,
"legacy chat only via telegramTargetFor (operator-only)",
);
assert.match(deliver, /getTelegramTarget\(userId\)/, "a linked chat is the person's own");
console.log("✓ feedback trust: source, night, claim link, Telegram");