mirror of
https://github.com/bitbaum/loki.git
synced 2026-12-05 16:07:06 +01:00
Watch mode forgot its own remarks the moment the tab closed: the next visit
noticed the same missing heading or the same tiny text, offered "Fix this"
again, and pressing it commissioned the same work twice. The content hash
could not catch it — every filing carries the watched steps with "(Ns ago)"
on each line, so no two filings of one finding ever hashed the same.
- A remark carries its signature as a key (widget/watch-trail.ts:
checksSignature / noticeSignature / the read's `${path}|read`). "Fix
this" files it with the note (`noticeKey`), and so does a failure Watch
files itself. Ingest honours the key only with a valid owner pass and
dedupes by it BEFORE the content hash (bumpDuplicateFeedbackByNotice):
a second press lands on the open row and is counted, not built again.
- Migration 0090: site_feedback.notice_key + index.
- /api/widget/owner/changes answers `known`: every keyed row, open or
settled, with the owner's own status words — beyond the twelve the
strip shows.
- widget/known-fixes.ts (pure): the row that answers for a key (the open
one, else the newest — an old closed row cannot silence a live fix);
the verdict: in flight or closed → silent, live → "again", unknown →
say; the card's chip ("Being fixed · Building", "Needs you", "Fixed 3
Oct"). Watch looks before it speaks (awaits the first read of the
owner's fixes, 4 s at most) and skips the model read for a page whose
read-fix is in flight.
- The noticed card shows where its fix is instead of a second button; a
live fix that is back reads "Loki noticed — again … It is back" with
"Fix again". Cards follow the fix as it moves while the panel is open.
- Tests: scripts/test/known-fixes.ts (6), widget-thread (key survives a
reload), widget-changes (route shape), and two browser cases in
widget-watch-browser (a fix in flight is not noticed again; a live one
that recurs says so and files its key). Changelog.
Claude-Session: https://claude.ai/code/session_01YKx4TPejLvFR9Uj4xrTf6Y
Co-authored-by: Claude <noreply@anthropic.com>
298 lines
10 KiB
TypeScript
298 lines
10 KiB
TypeScript
/**
|
||
* "Your changes" — the loop closing on the owner's own site.
|
||
*
|
||
* Until now the panel's story ended at "On it — Loki tells you when", and on
|
||
* the site Loki never did: the telling went to a push notification and a
|
||
* Telegram chat, and the page the owner came back to said nothing. This strip
|
||
* is the other half of that sentence. It lists what the owner asked for and
|
||
* where each thing is (Building · On its way · Live · Needs you), asks Loki
|
||
* again every half minute while something is moving, and when a change goes
|
||
* live since the last look, Loki says so — in the thread when the panel is
|
||
* open, beside the launcher when it is not — with one tap to see it.
|
||
*
|
||
* Words come from the server (owner-view.ts). This side renders, remembers
|
||
* what it has already seen (localStorage, per site), and notices the one
|
||
* transition that matters.
|
||
*/
|
||
import { h } from "./dom";
|
||
import { knownFor, parseKnown, type KnownFix } from "./known-fixes";
|
||
|
||
export type OwnerChange = {
|
||
id: string;
|
||
text: string;
|
||
at: string;
|
||
label: string;
|
||
tone: "neutral" | "accent" | "warning" | "positive";
|
||
detail: string;
|
||
live: boolean;
|
||
settled: boolean;
|
||
href: string | null;
|
||
action: string | null;
|
||
};
|
||
|
||
/** Mirrors OWNER_CHANGES_MAX in src/lib/feedback/owner-view.ts. */
|
||
export const CHANGES_MAX = 12;
|
||
/** Rows shown before "all N in Loki". The strip sits above the thread. */
|
||
export const CHANGES_SHOWN = 3;
|
||
export const CHANGES_POLL_MS = 30_000;
|
||
/** Stop asking after this long with nothing moving — a tab left open all day
|
||
* must not poll Loki all day. */
|
||
export const CHANGES_POLL_FOR_MS = 2 * 60 * 60_000;
|
||
|
||
const seenKey = (token: string) => `loki-changes-seen:${token}`;
|
||
|
||
/** What this browser last saw of each change: live or not. */
|
||
export function readSeen(token: string): Record<string, boolean> {
|
||
try {
|
||
const raw = JSON.parse(localStorage.getItem(seenKey(token)) ?? "{}") as unknown;
|
||
if (!raw || typeof raw !== "object") return {};
|
||
const out: Record<string, boolean> = {};
|
||
for (const [k, v] of Object.entries(raw)) if (typeof v === "boolean") out[k] = v;
|
||
return out;
|
||
} catch {
|
||
return {};
|
||
}
|
||
}
|
||
|
||
export function writeSeen(token: string, seen: Record<string, boolean>): void {
|
||
try {
|
||
localStorage.setItem(seenKey(token), JSON.stringify(seen));
|
||
} catch {
|
||
/* storage blocked: the announcement may repeat next visit */
|
||
}
|
||
}
|
||
|
||
/**
|
||
* The changes that went live since the last look. A change never seen before
|
||
* is not announced — the first open after a long absence should not shout
|
||
* about everything that ever shipped — only one this browser saw building.
|
||
*/
|
||
export function newlyLive(
|
||
prev: Record<string, boolean>,
|
||
next: readonly OwnerChange[],
|
||
): OwnerChange[] {
|
||
return next.filter((c) => c.live && prev[c.id] === false);
|
||
}
|
||
|
||
export function toSeen(next: readonly OwnerChange[]): Record<string, boolean> {
|
||
return Object.fromEntries(next.map((c) => [c.id, c.live]));
|
||
}
|
||
|
||
/**
|
||
* The collapsed line: what is moving and what landed, in the order the owner
|
||
* cares — what needs them, what is being built, what is waiting its turn,
|
||
* what is live. "Your changes · 1 needs you · 1 building · 2 live".
|
||
*/
|
||
export function changesSummary(changes: readonly OwnerChange[], visitors = 0): string {
|
||
const count = (label: string) => changes.filter((c) => c.label === label).length;
|
||
const parts = [
|
||
["needs you", count("Needs you")],
|
||
["building", count("Building") + count("Starting")],
|
||
["in line", count("In line")],
|
||
["on its way", count("On its way")],
|
||
["live", changes.filter((c) => c.live).length],
|
||
]
|
||
.filter(([, n]) => (n as number) > 0)
|
||
.map(([w, n]) => `${n} ${w}`);
|
||
if (visitors) parts.push(`${visitors} from visitors`);
|
||
return parts.length ? `Your changes · ${parts.join(" · ")}` : `Your changes · ${changes.length}`;
|
||
}
|
||
|
||
export function parseChanges(body: unknown): OwnerChange[] {
|
||
const raw = (body as { changes?: unknown })?.changes;
|
||
if (!Array.isArray(raw)) return [];
|
||
return raw
|
||
.map((c) => (c ?? {}) as Partial<OwnerChange>)
|
||
.filter(
|
||
(c) => typeof c.id === "string" && typeof c.text === "string" && typeof c.label === "string",
|
||
)
|
||
.map((c) => ({
|
||
id: c.id as string,
|
||
text: String(c.text).slice(0, 200),
|
||
at: typeof c.at === "string" ? c.at : "",
|
||
label: String(c.label).slice(0, 40),
|
||
tone: (["neutral", "accent", "warning", "positive"] as const).includes(
|
||
c.tone as OwnerChange["tone"],
|
||
)
|
||
? (c.tone as OwnerChange["tone"])
|
||
: "neutral",
|
||
detail: typeof c.detail === "string" ? c.detail.slice(0, 300) : "",
|
||
live: c.live === true,
|
||
settled: c.settled === true,
|
||
href: typeof c.href === "string" && /^https?:\/\//.test(c.href) ? c.href : null,
|
||
action: typeof c.action === "string" ? c.action.slice(0, 40) : null,
|
||
}))
|
||
.slice(0, CHANGES_MAX);
|
||
}
|
||
|
||
export function createChanges(opts: {
|
||
apiBase: string;
|
||
token: string;
|
||
pass: () => string | null;
|
||
/** A change went live since this browser last looked. */
|
||
onLive: (change: OwnerChange) => void;
|
||
/** The server no longer honours the pass. */
|
||
onPassRefused: () => void;
|
||
/** The list was re-read: cards keyed to a fix follow its status. */
|
||
onUpdate?: () => void;
|
||
}) {
|
||
// One line until opened, like the notes above it: "Your changes · 1
|
||
// building · 2 live ›". It was a header, three rows and a "See it →" line
|
||
// under each — the busiest block on the panel, open all the time.
|
||
const el = h("div", "yours");
|
||
el.style.display = "none";
|
||
const head = h("button", "yours-head");
|
||
head.type = "button";
|
||
head.setAttribute("aria-expanded", "false");
|
||
const summary = h("span", "yours-sum");
|
||
const chev = h("span", "thoughts-chev", "›");
|
||
chev.setAttribute("aria-hidden", "true");
|
||
head.append(summary, chev);
|
||
const body = h("div", "yours-body");
|
||
body.style.display = "none";
|
||
const list = h("div", "yours-list");
|
||
const all = h("a", "yours-all");
|
||
all.target = "_blank";
|
||
all.rel = "noopener";
|
||
// Visitors' notes are not the owner's changes; they are counted, and read
|
||
// in Loki, where the inbox has the room to show who said what.
|
||
const visitorsLine = h("a", "yours-visitors");
|
||
visitorsLine.target = "_blank";
|
||
visitorsLine.rel = "noopener";
|
||
visitorsLine.style.display = "none";
|
||
body.append(list, all, visitorsLine);
|
||
el.append(head, body);
|
||
let open = false;
|
||
head.addEventListener("click", () => {
|
||
open = !open;
|
||
head.setAttribute("aria-expanded", String(open));
|
||
el.classList.toggle("open", open);
|
||
body.style.display = open ? "" : "none";
|
||
});
|
||
|
||
let changes: OwnerChange[] = [];
|
||
/** Every keyed fix the server knows, open or settled (known-fixes.ts). */
|
||
let known: KnownFix[] = [];
|
||
let firstRead: (() => void) | null = null;
|
||
/** Settles after the first answer (or refusal), so Watch can look before it speaks. */
|
||
const ready = new Promise<void>((resolve) => {
|
||
firstRead = resolve;
|
||
});
|
||
let inbox: string | null = null;
|
||
let visitors = 0;
|
||
let timer = 0;
|
||
let startedAt = 0;
|
||
let inflight = false;
|
||
|
||
function render() {
|
||
list.textContent = "";
|
||
el.style.display = changes.length || visitors ? "" : "none";
|
||
summary.textContent = changesSummary(changes, visitors);
|
||
el.classList.toggle(
|
||
"needs-you",
|
||
changes.some((c) => c.tone === "warning"),
|
||
);
|
||
visitorsLine.style.display = visitors && inbox ? "" : "none";
|
||
if (visitors && inbox) {
|
||
visitorsLine.textContent = `${visitors} note${visitors === 1 ? "" : "s"} from visitors, in Loki →`;
|
||
visitorsLine.href = inbox;
|
||
}
|
||
for (const c of changes.slice(0, CHANGES_SHOWN)) {
|
||
// The row IS the link when there is somewhere to go — no second line.
|
||
const row = c.href
|
||
? h("a", `yours-row tone-${c.tone}`)
|
||
: h("div", `yours-row tone-${c.tone}`);
|
||
if (row instanceof HTMLAnchorElement && c.href) {
|
||
row.href = c.href;
|
||
row.rel = "noopener";
|
||
row.target = "_blank";
|
||
}
|
||
row.title = c.detail;
|
||
const text = h("span", "yours-text", c.text);
|
||
const status = h("span", "yours-status", c.label);
|
||
row.append(h("span", "yours-dot"), text, status);
|
||
list.appendChild(row);
|
||
}
|
||
const more = changes.length - CHANGES_SHOWN;
|
||
all.textContent = more > 0 ? `All ${changes.length} in Loki →` : "Open in Loki →";
|
||
all.style.display = inbox ? "" : "none";
|
||
if (inbox) all.href = inbox;
|
||
}
|
||
|
||
async function refresh(): Promise<void> {
|
||
const pass = opts.pass();
|
||
if (!pass) {
|
||
firstRead?.();
|
||
firstRead = null;
|
||
return;
|
||
}
|
||
if (inflight) return;
|
||
inflight = true;
|
||
try {
|
||
const res = await fetch(`${opts.apiBase}/api/widget/owner/changes`, {
|
||
method: "POST",
|
||
headers: { "Content-Type": "application/json" },
|
||
body: JSON.stringify({ token: opts.token, ownerPass: pass }),
|
||
});
|
||
const body = (await res.json().catch(() => null)) as {
|
||
owner?: boolean;
|
||
inbox?: unknown;
|
||
visitors?: unknown;
|
||
} | null;
|
||
if (!res.ok || !body) return;
|
||
if (body.owner === false) {
|
||
opts.onPassRefused();
|
||
return;
|
||
}
|
||
const next = parseChanges(body);
|
||
known = parseKnown(body);
|
||
opts.onUpdate?.();
|
||
inbox = typeof body.inbox === "string" && /^https?:\/\//.test(body.inbox) ? body.inbox : null;
|
||
visitors =
|
||
typeof body.visitors === "number" && body.visitors > 0 ? Math.floor(body.visitors) : 0;
|
||
const prev = readSeen(opts.token);
|
||
for (const c of newlyLive(prev, next)) opts.onLive(c);
|
||
writeSeen(opts.token, { ...prev, ...toSeen(next) });
|
||
changes = next;
|
||
render();
|
||
// Keep asking only while something can still change.
|
||
if (!next.some((c) => !c.settled)) stop();
|
||
} catch {
|
||
/* Loki unreachable: the strip keeps what it had */
|
||
} finally {
|
||
inflight = false;
|
||
firstRead?.();
|
||
firstRead = null;
|
||
}
|
||
}
|
||
|
||
function stop() {
|
||
window.clearInterval(timer);
|
||
timer = 0;
|
||
}
|
||
|
||
/** Ask now, then every half minute while something is moving. */
|
||
function start() {
|
||
if (!opts.pass()) return;
|
||
void refresh();
|
||
if (timer) return;
|
||
startedAt = Date.now();
|
||
timer = window.setInterval(() => {
|
||
if (Date.now() - startedAt > CHANGES_POLL_FOR_MS) return stop();
|
||
void refresh();
|
||
}, CHANGES_POLL_MS);
|
||
}
|
||
|
||
return {
|
||
el,
|
||
refresh,
|
||
start,
|
||
stop,
|
||
count: () => changes.length,
|
||
/** The fix a remark's key already has, if any — see known-fixes.ts. */
|
||
known: (key: string) => knownFor(known, key),
|
||
/** Resolves once Loki has answered (or could not), never later than the caller's own wait. */
|
||
ready: () => ready,
|
||
};
|
||
}
|