Files
loki/scripts/test/project-mention.ts
CatoandClaude Opus 5.5 a1b9d212b2 fix: no silent standing approvals for Loki's own ideas, and no dead-end stalls (#1168)
* fix(approvals): standing approvals cover what you ask for, never Loki's own ideas

The owner saw "✅ Done — Check in with Rajif Rao" on Telegram and feared Loki
had contacted someone without permission. Nothing was sent: a commitment is a
row in the operator's own list. But the path was still wrong. The check-in
producer writes its suggestions as create_commitment rows, so the "Record
commitments I mention" standing approval silently approved one nobody
mentioned, and the confirmation read as if the check-in had happened.

- standing-approval: RAIL 0. Every proposal now carries a required origin
  ("operator" | "loki"); only operator proposals may ride a standing rule.
- enqueueAction takes the origin from every caller and stamps it on the
  payload so the calendar drain's confirmation can say the same thing.
- Check-ins, the night plan and propose calls marked "not asked for" are
  origin "loki"; chat, the agent's propose tool and MCP are "operator".
- A commitment's confirmation now reads "📌 Added to your commitments —
  “Reach out to …”. A reminder for you. Nothing was sent to anyone."
- The Approvals setting says Loki's own suggestions still wait for a yes.

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

* fix(control): a stalled queue always has a button, and names who holds it

On a phone the owner tapped "Run it in the cloud" under "Waiting for this
computer". The next refresh said "Stalled — 2 dispatches queued for 118m, the
builder is connected but not executing them. Restart the desktop app or check
the cloud builder if this persists", with no button, under a foot line that
read "This computer offline". Three things were wrong:

- The stall clock ran from created_at, so work the cloud had held for seconds
  was reported as 118 minutes stuck. A reroute now stamps reroutedAt and the
  detector measures from it.
- The sentence blamed a desktop app for work the cloud was holding. The stall
  query now counts cloud-held rows too, and the headline names the builder
  that actually has the work and whether it is connected.
- A genuine stall had no way forward. It now offers "Stop waiting"
  (POST /api/control/stop-waiting): the stuck rows close as failed dispatches,
  where Retry sends them again verbatim and Dismiss clears them, and their
  runs close so the project's lane is free. The consequence is stated in the
  headline before the tap.

Both hero queue buttons now report what they did ("Handed 2 dispatches to the
cloud builder.", or why a project stayed put) instead of firing and forgetting.

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

* fix(loki): a screenshot sent with "fix this" finds the project it shows

On a phone the owner opened a new chat, attached a screenshot of Loki's own
Control page and wrote "Fix this so it works properly". The thread read
"No project — answers only": the text named no project, so the screenshot
fast path could not dispatch, and the picture was only ever described,
never used to decide where the fix should go.

- The vision preflight now opens with one line, "Shown: <app or site, and
  any URL visible>".
- projectShownInScreenshot picks a project only when the evidence points at
  exactly one: a project's live host visible in the analysis (compared whole,
  so loki.orangecat.ch is Loki and not OrangeCat), else a single project
  named on the Shown line. Projects merely listed on the screen do not count,
  and two candidates stay a question for the person.
- When Loki chose the project this way the reply says so and how to pick a
  different one, so a guess never reads as the person's own choice.

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

* fix(notify): a failed run's Telegram message links to where Retry is

"❌ loki: run error … choose another AI provider and Retry" arrived with
nothing to tap. Failed, partial and timed-out runs now carry one button,
"Open <project> in Control", which lands on the project card where Retry and
the provider switch live. Successes stay buttonless.

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

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-10-12 06:40:51 -07:00

228 lines
7.9 KiB
TypeScript

/**
* Regression coverage for the phone session of 2026-08-18.
*
* The operator typed, on /loki:
*
* "I want you to tell me what is left to do with Orange Cat for it to work
* properly."
*
* and got "Which project should I run that on?" over a grid of nine project
* chips — none of them the project they had just named. Two independent bugs
* had to line up to produce that, and both are covered here:
*
* 1. Project matching was `text.includes(name)` on the raw slug, so the
* registered `orangecat` never matched the typed "Orange Cat".
* 2. looksLikeDispatchTask treated any sentence over 16 characters containing
* " for "/" on "/" in " as work to dispatch. "for it to work" qualified, so
* a question was routed to an agent instead of being answered.
*
* Run: npx tsx scripts/test/project-mention.ts (or npm run test:unit)
*/
import { resolveFleetCommandProjectKey } from "@/lib/loki-fleet-commands";
import {
projectShownInScreenshot,
projectMentionedIn,
resolveProjectFromContext,
textMentionsProject,
unknownProjectMention,
} from "@/lib/project-mention";
import { looksLikeDispatchTask } from "@/lib/command-resolve";
let passed = 0;
function check(label: string, condition: boolean): void {
if (!condition) throw new Error(`FAILED: ${label}`);
passed += 1;
console.log(` ✓ ${label}`);
}
function eq<T>(actual: T, expected: T, label: string): void {
check(`${label} (got ${JSON.stringify(actual)})`, Object.is(actual, expected));
}
// The fleet as it stood on the day, in the order the picker rendered it.
const FLEET = [
"Bitbaum",
"BiasLens",
"Zeitkastli",
"Prime tower",
"lifeops",
"aoz-begleitung",
"botsmann",
"datacat",
"loki",
"orangecat",
];
console.log("project-mention — spacing/case tolerance");
eq(
projectMentionedIn("what is left to do with Orange Cat", FLEET),
"orangecat",
"the reported case",
);
eq(projectMentionedIn("push orangecat please", FLEET), "orangecat", "exact slug still matches");
eq(projectMentionedIn("check OrangeCat's build", FLEET), "orangecat", "camel case");
eq(projectMentionedIn("look at orange-cat", FLEET), "orangecat", "hyphenated");
eq(
projectMentionedIn("the AOZ Begleitung rebuild", FLEET),
"aoz-begleitung",
"slug with a hyphen, typed with a space",
);
eq(
projectMentionedIn("deploy prime tower", FLEET),
"Prime tower",
"registered name keeps its own casing in the answer",
);
console.log("\nproject-mention — no invented matches");
eq(projectMentionedIn("nothing relevant here", FLEET), null, "unrelated prose matches nothing");
check(
"a project name is not found inside a longer word",
!textMentionsProject("going somewhere", "go"),
);
check("partial token runs do not match", !textMentionsProject("orange", "orangecat"));
eq(
projectMentionedIn("compare aoz to the aoz-begleitung plan", ["aoz", "aoz-begleitung"]),
"aoz-begleitung",
"the longer, more specific name wins",
);
console.log("\nproject-mention — selection outranks prose");
eq(
resolveProjectFromContext("work on Orange Cat", "datacat", FLEET),
"datacat",
"an explicit pick wins",
);
eq(
resolveProjectFromContext("work on Orange Cat", undefined, FLEET),
"orangecat",
"falls back to the name in the text",
);
console.log("\nproject-mention — a fast path follows the project you name");
eq(
resolveFleetCommandProjectKey("write the business plan for Orange Cat", "loki", FLEET),
"orangecat",
"named in the message beats the selection the last answer left behind",
);
eq(
resolveFleetCommandProjectKey("write the business plan", "loki", FLEET),
"loki",
"no name: the selection",
);
eq(resolveFleetCommandProjectKey("write the business plan", undefined, FLEET), null, "neither");
console.log("\nproject-mention — naming something we don't have");
const noOrangeCat = FLEET.filter((p) => p !== "orangecat");
eq(
unknownProjectMention("what is left to do with Orange Cat for it to work", noOrangeCat),
"Orange Cat",
"reports the operator's own words back",
);
eq(
unknownProjectMention("code review for “Red Dog”", noOrangeCat),
"Red Dog",
"a quoted phrase counts as a name",
);
eq(
unknownProjectMention("what is left to do with Orange Cat", FLEET),
null,
"silent when the project IS registered",
);
eq(unknownProjectMention("fix the tests", FLEET), null, "silent on ordinary prose");
eq(unknownProjectMention("remind me on Monday", FLEET), null, "a weekday is not a project");
console.log("\ncommand-resolve — asking is not dispatching");
check(
"the reported sentence is a question, not work",
!looksLikeDispatchTask(
"I want you to tell me what is left to do with Orange Cat for it to work properly.",
),
);
check("tell me …", !looksLikeDispatchTask("tell me how the deploy pipeline works for loki"));
check(
"can you explain …",
!looksLikeDispatchTask("Can you explain what changed in datacat on Friday"),
);
check(
"give me a rundown …",
!looksLikeDispatchTask("give me a rundown of what is blocked on botsmann"),
);
check(
"I need to know …",
!looksLikeDispatchTask("I need to know whether the migration ran in lifeops"),
);
check(
"a pronoun-led sentence is not an instruction",
!looksLikeDispatchTask("we should probably think about the pricing page in a while"),
);
check(
"a capability question is a question, even without its question mark",
!looksLikeDispatchTask("Are you able to work on improving Loki in this field."),
);
check(
"is it possible …",
!looksLikeDispatchTask("is it possible to run the deploy on lifeops from here"),
);
check("do you know …", !looksLikeDispatchTask("do you know what is running on the cloud builder"));
console.log("\ncommand-resolve — work is still work");
check("imperative with a verb", looksLikeDispatchTask("fix the failing tests in loki"));
check("build handoff", looksLikeDispatchTask("ok let's build it"));
check("explicit implement", looksLikeDispatchTask("implement the invite flow for botsmann"));
check(
"verb-led with no keyword verb",
looksLikeDispatchTask("ship the parser rewrite for datacat"),
);
check(
"an action question still dispatches",
looksLikeDispatchTask("can you fix the failing types in lifeops?"),
);
console.log("\nproject-mention — a screenshot names the project it is of");
// 2026-10-12: "Fix this so it works properly" + a screenshot of Loki's Control
// page, sent with no project picked, could not be acted on.
const SITES = [
{ name: "loki", liveUrl: "https://loki.orangecat.ch" },
{ name: "orangecat", liveUrl: "https://orangecat.ch" },
{ name: "aoz", liveUrl: null },
{ name: "aoz-begleitung", liveUrl: "https://aoz.orangecat.ch" },
];
const analysis = (shown: string, body = "") =>
`\n\n--- Attached image analysis (s.png, via m) ---\nShown: ${shown}\n${body}`;
eq(
projectShownInScreenshot(analysis("Loki — the Control screen"), SITES),
"loki",
"the Shown line names the project",
);
eq(
projectShownInScreenshot(analysis("a web app at loki.orangecat.ch"), SITES),
"loki",
"a project's own host wins, even though it also contains 'orangecat'",
);
eq(
projectShownInScreenshot(analysis("unknown", "The page loads https://orangecat.ch/about"), SITES),
"orangecat",
"a host anywhere in the analysis counts",
);
eq(
projectShownInScreenshot(
analysis("Loki — Control", "Cards for orangecat, aoz-begleitung and substrata are stalled."),
SITES,
),
"loki",
"projects listed ON the screen do not compete with the app it is",
);
eq(
projectShownInScreenshot(analysis("Loki and OrangeCat side by side"), SITES),
null,
"two projects shown is a question, not a guess",
);
eq(
projectShownInScreenshot(analysis("the AOZ Begleitung portal"), SITES),
"aoz-begleitung",
"a name containing another project's name is still one project",
);
eq(projectShownInScreenshot(analysis("unknown"), SITES), null, "unknown stays unknown");
eq(projectShownInScreenshot("", SITES), null, "no analysis, no guess");
console.log(`\n${passed}/${passed} project-mention cases passed`);