Files
loki/scripts/test/build-suggestions.ts
CatoandClaude Opus 5.5 e47011e270 Security: the server's files belong to its owner; Loki answers general questions and recaps chats (#1260)
* fix(loki): answer general questions, plain words for who can build, one reminder at a time

- General questions get general answers. "What year was the Eiffel Tower
  built?" answered "Not in your data." live: the closed-world check flagged
  every name and year, and the repair pass deleted them. When the question
  names no surface of the person's records, only the sentences about their
  records are checked (cited, or naming a person/project/goal they have).
  Contract rule 11 says the same to the model.
- A bold "<project>: …" line that only the person can carry out (install the
  desktop app, sign in, add a key, approve, pay) is no longer offered to a
  "yes", and is rewritten as a plain sentence.
- The fleet facts and brief say "can new work start right now" in plain words
  (Loki's own computers / the desktop app) instead of runner/builder, and the
  two no longer disagree. Field names stop leaking ("the query returned none").
- One account reminder at a time: the OrangeCat link line first, then the
  verify-email line, each one row on a phone.
- Onboarding: no CLI/tenant wording; the folder field is asked only of someone
  whose code is already on this computer.

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

* fix(security): the server's files and telemetry are the operator's, not every account's

- /api/people/sync/openclaw read the OpenClaw contact book from the server's
  home directory — the site operator's WhatsApp/Telegram contacts — and
  wrote it into the CALLER's book. Any signed-in account with its private
  zone open (a new one has no PIN) could copy it. Now operator-only, and the
  People page no longer explains OpenClaw and Hermes to people who have
  neither.
- /system showed every account the box's RAM, disk, uptime, Hetzner capacity
  and "Recent telemetry" from debug_logs, which has no user column: every
  account's errors, sign-in failures and cron counts. Those cards and
  /api/system, /api/system/hetzner are now the operator's; a tenant keeps
  their own alarms, jobs and records.
- Suggestions: OrangeCat gift wishlists ("My Birthday Wishlist") are no
  longer offered as things to build, and each starter says what it is
  instead of one sentence repeated four times.

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

* fix(loki): name threads from the server; phone-sized targets and clamps

- A thread is named the moment its first answer is saved, server-side. The
  browser was the only caller, 3s after the answer, so a person who locked
  their phone or closed the tab left the thread named by its opening line.
  The browser's ask now also takes a title the server already set.
- /suggestions: the "Quiet today" links were 18px tall; now 44px targets.
- /packages: descriptions declared two lines and rendered four (a `block`
  utility beat line-clamp).
- /loki start screen: an untitled thread shows two lines of the question
  instead of one cut mid-word.

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

* fix(security): free-text folder paths and server files are the machine owner's

An audit of every route that touches the server's own filesystem or
processes, for an ordinary signed-in account on the hosted server:

- DELETE /api/projects/[id]?deleteLocal=1 removed any folder under the dev
  root that the account's free-text dirPath named — the operator's checkout,
  another tenant's preview. Now only an account allowed on the fleet machine.
- GET /api/projects/[id] returned the operator's OpenClaw cron jobs matched by
  a substring of the project name. Operator only.
- POST /api/control/agent rewrote the server's global agent preferences and
  Claude settings. Now the machine owner's: own desktop app, or the operator.
- GET /api/sessions read any project's handoff by name. Now only a project the
  account owns, where the server's files can be its own.
- merge-prompts fell back to the operator's OpenClaw agent with user text
  through a shell. Machine owner only, argv not shell, input bounded.
- /api/control ran git and a /proc scan on account-chosen folders. Machine
  owner only.
- Previews (public <slug>.orangecat.ch sites with the fleet's deploy key) are
  fleet infrastructure; preview owner lookup no longer lets a copied dirPath
  decide whose access code opens a preview.
- The feedback page picture no longer opens private-network addresses on the
  server (checked on every request the page makes).
- New cron jobs are addressed to the operator's Telegram only for the operator.

Also: "what did I ask you today?" never worked live — the thread source threw
on a timestamp the driver returned as a string, and returned nothing.

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

* fix(models): the model table no longer pushes a tablet page sideways

At 768px the table needed ~910px and overflowed the page by 164px
(responsive audit against production, 2026-10-17). Rows stay cards up to the
laptop breakpoint, and the table scrolls inside its own box as a backstop.

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

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-10-17 17:51:39 +02:00

240 lines
7.9 KiB
TypeScript

// The /loki suggestion row is ranked from a person's own records. Pinned here:
// report fixes outrank everything; project work dispatches (carries `project`)
// while new builds open the create flow (carries `href`); visitor-written text
// never reaches a prompt; one busy project cannot fill the row; demand that
// matches what the person builds outranks demand that does not; a brand-new
// account gets starters instead of an empty row; refresh rotates the window.
// Run: npx tsx scripts/test/build-suggestions.ts
import assert from "node:assert/strict";
import {
rankBuildSuggestions,
newProjectHref,
type SuggestionInputs,
type SuggestionProject,
} from "@/lib/build-suggestions/rank";
import { BUILD_STARTERS } from "@/config/build-starters";
import { isBuildableNeed } from "@/lib/integrations/demand-buildable";
import { planRetrieval } from "@/lib/agent/plan";
import { parseCreateProjectRequest } from "@/lib/loki-fleet-commands";
function project(over: Partial<SuggestionProject> & { name: string }): SuggestionProject {
return {
description: null,
stack: null,
liveUrl: "https://example.org",
runnable: true,
nextStep: null,
nextStepDate: new Date().toISOString(),
...over,
};
}
const empty: SuggestionInputs = {
projects: [],
openFeedback: {},
stuckGoals: [],
commitmentsDue: [],
captures: [],
demand: [],
};
// ---- a brand-new account gets starters, each opening the create flow ----
{
const { suggestions } = rankBuildSuggestions(empty);
assert.equal(suggestions.length, BUILD_STARTERS.length);
for (const s of suggestions) {
assert.equal(s.kind, "starter");
assert.ok(s.href?.startsWith("/projects?new=1&"), "a starter opens the create dialog");
assert.equal(s.prompt, null);
}
// Each says what it is — not one sentence repeated down the page.
assert.equal(new Set(suggestions.map((s) => s.why)).size, suggestions.length);
}
// ---- a gift wishlist is not a build; a request for software is ----
{
const gift = {
title: "My Birthday Wishlist",
text: "Help me celebrate! Here are some things I would love.",
url: "/wishlists/7e834489",
};
assert.equal(isBuildableNeed(gift), false);
assert.equal(
isBuildableNeed({ ...gift, title: "Wishlist", text: "I need a booking website" }),
true,
);
assert.equal(isBuildableNeed({ ...gift, url: "/needs/42" }), true);
}
// ---- report fixes lead, and dispatch into their project ----
{
const { suggestions } = rankBuildSuggestions({
...empty,
projects: [
project({ name: "sink", nextStep: "add search" }),
project({ name: "heidi", liveUrl: null }),
],
openFeedback: { sink: 3 },
});
assert.equal(suggestions[0].kind, "fix");
assert.equal(suggestions[0].project, "sink");
assert.match(suggestions[0].label, /3 reports/);
assert.ok(suggestions.some((s) => s.kind === "ship" && s.project === "heidi"));
assert.ok(suggestions.some((s) => s.kind === "continue" && /add search/.test(s.prompt ?? "")));
assert.ok(
!suggestions.some((s) => s.kind === "starter"),
"starters are only for an account with nothing to work on",
);
for (const s of suggestions) {
assert.ok(s.why.length > 0, `${s.id} names the fact it came from`);
assert.ok(s.prompt || s.href, `${s.id} leads somewhere`);
}
}
// ---- a project without code cannot be dispatched to ----
{
const { suggestions } = rankBuildSuggestions({
...empty,
projects: [project({ name: "idea-only", runnable: false, liveUrl: null })],
openFeedback: { "idea-only": 2 },
});
assert.ok(!suggestions.some((s) => s.project === "idea-only"));
}
// ---- one busy project cannot fill the row ----
{
const { suggestions } = rankBuildSuggestions({
...empty,
projects: [
project({ name: "busy", liveUrl: null, nextStep: "x" }),
project({ name: "quiet", liveUrl: null }),
],
openFeedback: { busy: 9 },
});
assert.equal(
suggestions.slice(0, 3).filter((s) => s.project === "busy").length,
2,
"variety before repetition: the quiet project gets a seat before a third busy one",
);
}
// ---- relevant demand outranks unrelated demand ----
{
const { suggestions } = rankBuildSuggestions({
...empty,
projects: [project({ name: "bakery-site", description: "website for a bakery in Zurich" })],
demand: [
{ id: "a", title: "Tax advice", text: "need an accountant", url: "https://oc/a" },
{ id: "b", title: "Bakery website", text: "our bakery needs a website", url: "https://oc/b" },
],
});
const demand = suggestions.filter((s) => s.kind === "demand");
assert.equal(demand[0].id, "demand:b");
assert.match(demand[0].prompt ?? "", /external text/, "external text is labelled as such");
}
// ---- a sticky note that reads like a build becomes a new project; a reminder does not ----
{
const { suggestions } = rankBuildSuggestions({
...empty,
projects: [project({ name: "p" })],
captures: [
{ body: "app that tracks my plants", ageDays: 2 },
{ body: "call mum", ageDays: 1 },
{ body: "build a recipe site", ageDays: 400 },
],
});
const ideas = suggestions.filter((s) => s.kind === "idea");
assert.equal(ideas.length, 1);
assert.ok(ideas[0].href?.includes("description=app+that+tracks+my+plants"));
}
// ---- refresh rotates the window through everything that qualified ----
{
const inputs: SuggestionInputs = {
...empty,
projects: Array.from({ length: 6 }, (_, i) =>
project({ name: `p${i}`, liveUrl: null, nextStep: "next" }),
),
};
const first = rankBuildSuggestions(inputs, { limit: 4, offset: 0 });
const second = rankBuildSuggestions(inputs, { limit: 4, offset: 1 });
assert.equal(first.total, 12);
assert.notDeepEqual(
first.suggestions.map((s) => s.id),
second.suggestions.map((s) => s.id),
);
}
// ---- labels fit a phone row ----
{
const { suggestions } = rankBuildSuggestions({
...empty,
projects: [project({ name: "p", nextStep: "a".repeat(300) })],
});
for (const s of suggestions) assert.ok(s.label.length <= 44, s.label);
}
assert.equal(
newProjectHref("x", "y z"),
"/projects?new=1&name=x&description=y+z",
"the create dialog's deep link",
);
// ---- asking for direction leads with the ranked suggestions; asking about a
// visitor's "suggestion" (the feedback column) does not ----
for (const q of [
"what should I build next",
"any ideas?",
"what should we do next",
"what do you suggest",
]) {
assert.equal(planRetrieval(q).sources[0], "builds", q);
}
for (const q of ["what was the latest suggestion from visitors on sink", "what's next today"]) {
assert.notEqual(planRetrieval(q).sources[0], "builds", q);
}
// ---- the line Loki hands back for a new build carries its scope to the agent ----
{
const req = parseCreateProjectRequest(
"create project plant-log and build it: a page that logs when I water each plant",
);
assert.equal(req?.name, "plant-log");
assert.equal(req?.dispatchAfter, true);
assert.equal(req?.task, "a page that logs when I water each plant");
assert.equal(parseCreateProjectRequest("create project plant-log and build it")?.task, null);
assert.equal(parseCreateProjectRequest("create project plant-log")?.dispatchAfter, false);
}
console.log("build-suggestions: ok");
const fixedNow = Date.parse("2026-10-14T08:00:00Z");
for (const date of [null, "invalid", "2026-08-29", "2026-10-15"]) {
const stale = rankBuildSuggestions({
...empty,
now: fixedNow,
projects: [project({ name: "old", nextStep: "448 old test failures", nextStepDate: date })],
});
assert(
!stale.suggestions.some((s) => s.kind === "continue"),
"undated, old and future handoffs cannot become current work",
);
}
const fresh = rankBuildSuggestions({
...empty,
now: fixedNow,
projects: [
project({
name: "current",
nextStep: "Improve the homepage",
nextStepDate: "2026-10-13T08:00:00Z",
}),
],
});
assert(
fresh.suggestions.some(
(s) => s.kind === "continue" && s.why.includes("2026-10-13") && !s.why.includes("T08"),
),
);