Files
loki/scripts/test/widget-changes.ts
T
CatoandClaude adfbdcfe07 fix(session): the builder updates, the sheet changes things, one box, a preview is the site (#1305)
The owner used Loki from a phone (2026-10-18) and could not tell whether
anything worked. Four separate faults, each passing every check:

The cloud builder ran code days behind the app. Its restart waits for
every agent to be idle, and autopilot refilled the queue the instant one
finished: ten consecutive deploys (23:38 → 09:12) found "2 agent(s)
working — deferring restart". Hence "Model: not known yet" of an answer
the screen was showing. Now, while the cloud runner's heartbeat version is
behind the deploy's, the commands route hands it no work-starting
commands (dispatch, launch_agent, auto_continue, browser_task) — a
person's own words into a session still go through — so the idle moment
comes, the restart lands, the queue resumes on the new code. Server-side
on purpose: a runner-side rule would have waited for the very restart it
was meant to allow. Control's builder line says "a newer version waits
for its agents to finish" meanwhile.

An agent renamed a preview site, committed, and reported "the work lives
only on this machine — tell Loki where this preview's remote should
live". The box contract says push or fail loudly; a preview folder is
served from disk and has no remote by design, so the change had been live
since the save and the owner read a failure and a chore. Every dispatch
to a preview folder now carries "Where this goes" (lib/previews/
delivery-note), and the contract has the same section.

The session sheet's Change and Set typed /model and /effort into the
terminal and closed; Claude Code drew its picker behind the conversation
and nothing on screen moved. Each row now lists its choices; a tap sends
the command with the choice named (/model sonnet, /effort high), the
sheet stays open and says what is changing, and settles when the log
names it. The sheet is built like the Settings sheet.

The session box had a 26px library button beside 44px round ones, a 36px
chip and a 34px Keys pill, the microphone second from the left in one
view and beside Send in the other, and "claude" on the tab beside
"Message Claude…". One row, one size, one order in both views; the
agent's name everywhere (terminal-strip-tabs badgeNames).

Widget: the owner's change list reads what needs them first (the line
counted nine, the list showed none); "9 needs you" → "9 need you"; a
failed call is named ("my call to Loki to save your note failed… Your
words are still here — press it again") instead of "one of my calls to
Loki failed — nothing for you to do".

Tests: runner-update-hold, preview-delivery-note, owner-changes-order,
session-sheet-browser (new); builder-presence, widget-changes,
widget-self-report, inject-context-boundaries extended.


Claude-Session: https://claude.ai/code/session_01NZqZujmXpupVDC1Hi5Jbsx

Co-authored-by: Claude <noreply@anthropic.com>
2026-10-18 13:37:05 +02:00

273 lines
10 KiB
TypeScript

// "Your changes": the loop closing on the owner's own site. The panel's story
// used to end at "On it — Loki tells you when", and on the site Loki never
// did. Pinned here: the owner's words per phase come from the same honest
// phase the inbox uses and say nothing an engineer wrote; the widget
// announces exactly the transition to live, once, and never on first sight;
// the route answers only to a pass for this project and this owner; and the
// receipt's "Follow the build" no longer tours a page that has not changed.
// Run: npx tsx scripts/test/widget-changes.ts
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import { ownerStatusFor, OWNER_CHANGES_MAX } from "@/lib/feedback/owner-view";
import { FEEDBACK_WORK_PHASE, WAITING_ON, type FeedbackWorkView } from "@/lib/feedback/work-phase";
import { FIX_SHIP_STATE, type FixShipping } from "@/lib/feedback/fix-shipping";
import {
CHANGES_MAX,
builtSay,
changesHeadline,
SAVED_NOTE_LABEL,
changesSummary,
newlyLive,
parseChanges,
toSeen,
type OwnerChange,
} from "../../widget/changes";
const view = (
phase: FeedbackWorkView["phase"],
extra: Partial<FeedbackWorkView> = {},
): FeedbackWorkView => ({
phase,
waitingOn: WAITING_ON.NOBODY,
label: "x",
detail: "Open Fleet Runner on This computer",
diagnostic: "TypeError: cannot read properties of undefined",
runId: "run-1",
...extra,
});
const ship = (state: string): FixShipping => ({ state }) as FixShipping;
// ---- the owner's words, per phase ----
{
const waiting = ownerStatusFor(view(FEEDBACK_WORK_PHASE.NOT_STARTED));
assert.equal(waiting.label, "Waiting");
assert.ok(waiting.needsYou && !waiting.live && !waiting.settled);
assert.equal(ownerStatusFor(view(FEEDBACK_WORK_PHASE.QUEUED)).label, "Starting");
// Behind another change in the project's one lane: the order is the news.
const behind = ownerStatusFor(
view(FEEDBACK_WORK_PHASE.QUEUED, {
detail: "Behind “Add alt text” on this project — starts when that run finishes",
}),
);
assert.equal(behind.label, "In line");
assert.match(behind.detail, /^Behind “Add alt text”/);
assert.equal(ownerStatusFor(view(FEEDBACK_WORK_PHASE.WORKING)).label, "Building");
for (const p of [FEEDBACK_WORK_PHASE.STUCK, FEEDBACK_WORK_PHASE.FAILED]) {
const s = ownerStatusFor(view(p));
assert.equal(s.label, "Needs you");
assert.equal(s.tone, "warning");
assert.ok(s.needsYou, "the owner is sent into Loki, where the why is");
}
for (const st of [
FIX_SHIP_STATE.PUSHED,
FIX_SHIP_STATE.PR_OPEN,
FIX_SHIP_STATE.MERGED,
FIX_SHIP_STATE.DEPLOYING,
]) {
const s = ownerStatusFor(view(FEEDBACK_WORK_PHASE.NEEDS_VERIFY, { ship: ship(st) }));
assert.equal(s.label, "On its way", st);
assert.ok(!s.live && !s.settled);
}
const live = ownerStatusFor(
view(FEEDBACK_WORK_PHASE.NEEDS_VERIFY, { ship: ship(FIX_SHIP_STATE.DEPLOYED) }),
);
assert.equal(live.label, "Live");
assert.ok(live.live && live.settled, "live is the end of the road for the widget");
const checkLive = ownerStatusFor(view(FEEDBACK_WORK_PHASE.NEEDS_VERIFY, { checkLive: true }));
assert.ok(checkLive.live, "no deploy we can see, but the reader has to look: counts as live");
const closedPr = ownerStatusFor(
view(FEEDBACK_WORK_PHASE.NEEDS_VERIFY, { ship: ship(FIX_SHIP_STATE.PR_CLOSED) }),
);
assert.equal(closedPr.label, "Checking");
assert.ok(!closedPr.live, "a PR closed without merging is not a fix that shipped");
assert.ok(ownerStatusFor(view(FEEDBACK_WORK_PHASE.DONE)).live);
const closed = ownerStatusFor(view(FEEDBACK_WORK_PHASE.ARCHIVED));
assert.ok(closed.settled && !closed.live);
// Nothing the operator's view carries reaches the site.
for (const p of Object.values(FEEDBACK_WORK_PHASE)) {
const s = ownerStatusFor(view(p));
for (const leak of ["Fleet Runner", "TypeError", "run-1", "dispatched"]) {
assert.ok(!s.detail.includes(leak) && !s.label.includes(leak), `${p}: "${leak}" leaked`);
}
}
}
// ---- the widget notices exactly the transition to live ----
{
const c = (id: string, live: boolean): OwnerChange => ({
id,
text: id,
at: "",
label: live ? "Live" : "Building",
tone: "accent",
detail: "",
live,
settled: live,
href: null,
action: null,
});
assert.deepEqual(
newlyLive({}, [c("a", true)]),
[],
"first sight of a live change is not an announcement",
);
assert.deepEqual(
newlyLive({ a: false }, [c("a", true)]).map((x) => x.id),
["a"],
"seen building, now live: announce",
);
assert.deepEqual(newlyLive({ a: true }, [c("a", true)]), [], "already announced");
assert.deepEqual(newlyLive({ a: false }, [c("a", false)]), [], "still building");
assert.deepEqual(toSeen([c("a", true), c("b", false)]), { a: true, b: false });
}
// ---- what the widget accepts from the server ----
{
const parsed = parseChanges({
changes: [
{
id: "1",
text: "Add a scene",
label: "Live",
tone: "positive",
live: true,
settled: true,
href: "https://heidi.test/x#loki-tour=t",
action: "See it",
at: "2026-10-09T10:00:00Z",
detail: "d",
},
{ id: "2", text: "x", label: "Building", tone: "weird", href: "javascript:alert(1)" },
{ nope: true },
"junk",
],
});
assert.equal(parsed.length, 2);
assert.equal(parsed[0].action, "See it");
assert.equal(parsed[1].tone, "neutral", "an unknown tone falls back");
assert.equal(parsed[1].href, null, "only http(s) links become links");
assert.deepEqual(parseChanges(null), []);
assert.equal(CHANGES_MAX, OWNER_CHANGES_MAX, "the widget's cap mirrors the server's");
}
// ---- the route and the receipt link ----
const route = readFileSync("src/app/api/widget/owner/changes/route.ts", "utf8");
const ownerCheck = readFileSync("src/lib/widget/owner-request.ts", "utf8");
assert.match(route, /ownerRequest\(req, data\)/, "a pass, verified");
assert.match(ownerCheck, /passHolder\(data\.ownerPass, token\)/, "…by passHolder");
assert.match(
readFileSync("src/lib/feedback/pass-rule.ts", "utf8"),
/pass\.projectId !== site\.projectId\) return false/,
"for this project, its owner or an editor (scripts/test/widget-pass-holder.ts)",
);
assert.match(
route,
/owner: false, changes: \[\]/,
"no pass, nothing — a stranger with the public token learns nothing",
);
assert.match(
route,
/attachFeedbackWork\(token\.userId, \[\s*\.\.\.mine/,
"the same phase the inbox derives",
);
assert.match(
route,
/i\.source === FEEDBACK_SOURCE\.OWNER \|\| i\.source === FEEDBACK_SOURCE\.AI_REVIEW/,
"yours means yours — a visitor's note is not the owner's change",
);
assert.match(route, /visitors/, "and visitors' notes are counted, not listed");
assert.match(ownerCheck, /isWidgetOriginAllowed/, "origin allowlist like every widget route");
assert.match(
route,
/createTourToken\(item\.id\)/,
"See it is the walkthrough of this change, for the owner",
);
const watchFix = readFileSync("src/app/(app)/feedback/[id]/watch-fix/route.ts", "utf8");
assert.match(
watchFix,
/if \(!status\.live\) \{(?:(?!tourSiteUrl)[\s\S])*?\/watch\?run=(?:(?!tourSiteUrl)[\s\S])*?return projectInbox;\s*\}/,
"no walkthrough of a change that is not there yet — its live run, or the inbox",
);
const ingest = readFileSync("src/app/api/feedback/route.ts", "utf8");
assert.match(
ingest,
/duplicateOf: bumped, \.\.\.build, followUrl/,
"the owner's repeated note keeps its follow link",
);
const main = readFileSync("widget/main.ts", "utf8");
assert.match(main, /is live on this site\./, "Loki says it in the thread");
assert.match(main, /launcher\.say\(/, "and beside the launcher when the panel is closed");
assert.match(main, /changes\.start\(\)/, "asked for on arrival");
// ---- the collapsed line says what moves and what landed, in that order ----
{
const c = (label: string, live = false) =>
({
id: label,
text: label,
at: "",
label,
tone: "neutral",
detail: "",
live,
settled: live,
href: null,
action: null,
}) as OwnerChange;
assert.equal(
changesSummary([c("Building"), c("In line"), c("In line"), c("Live", true), c("Done", true)]),
"Your changes · 1 building · 2 in line · 2 live",
);
assert.equal(
changesSummary([c("Needs you"), c("Live", true)]),
"Your changes · 1 needs you · 1 live",
);
assert.equal(
changesSummary([c("Needs you"), c("Needs you"), c("On its way")]),
"Your changes · 2 need you · 1 on its way",
"the open list agrees with the line above it (it said '9 needs you', 2026-10-18)",
);
assert.equal(changesSummary([c("Closed")]), "Your changes · 1");
// The status line carries the most urgent fragment only.
assert.equal(
changesHeadline([c("Live", true), c("Needs you"), c("Building")]),
"1 change needs you",
);
assert.equal(
changesHeadline([c("Needs you"), c("Needs you")]),
"2 changes need you",
"says what is counted, in agreement",
);
assert.equal(changesHeadline([c("Live", true), c("Starting"), c("Building")]), "2 being built");
assert.equal(changesHeadline([c("Live", true), c("Live", true)]), "2 changes live");
assert.equal(changesHeadline([c("On its way")]), "1 on its way");
assert.equal(changesHeadline([c("Closed")]), "", "nothing to say: nothing on the line");
// Notes saved while Loki watched lead the line until they are built.
assert.equal(
changesHeadline([c("Live", true), c(SAVED_NOTE_LABEL), c(SAVED_NOTE_LABEL)]),
"2 notes saved",
);
assert.equal(changesHeadline([c(SAVED_NOTE_LABEL), c("Needs you")]), "1 change needs you");
assert.match(builtSay({ started: 3, total: 3 }), /Started your 3 saved notes/);
assert.match(
builtSay({ started: 5, total: 5, builds: 2 }),
/5 saved notes as 2 builds, one per page/,
);
assert.match(
builtSay({ started: 3, total: 3, builds: 1 }),
/as one build — they are all about the same page/,
);
assert.match(
builtSay({ started: 1, total: 3, note: "Limit reached." }),
/Started 1 of 3.*Limit reached/,
);
assert.equal(builtSay({ started: 0, total: 0 }), "There are no saved notes waiting.");
// The widget's word for a saved note is the server's.
assert.ok(
readFileSync("src/lib/feedback/owner-view.ts", "utf8").includes(`label: "${SAVED_NOTE_LABEL}"`),
"owner-view labels a saved note exactly as the widget counts it",
);
}
console.log("widget-changes: ok");