Files
loki/scripts/test/watch-shared-access.ts
CatoandClaude Fable 5.1 38a5b5654d feat(previews): before and after, photographed — the tour of what Loki changed on a live site (#1271)
* feat(previews): before and after, photographed — the tour of what Loki changed on a live site

A preview made from a live site (/change "Improve this site") used to end in
improvements.html, a page the build agent styled freehand that Loki could
neither check nor illustrate, and the owner still compared two sites from
memory. Now the build leaves improvements.json (one entry per change naming
the live page and the new file, plus the engineer's account under the
pull-request walkthrough's keys), and Loki shows it:

- `/p/<slug>/_loki/before-after`, behind the preview's own code: each change
  as the live page and the new page photographed on a phone, side by side,
  with the build's words, what was not brought over, the questions for the
  owner, and the account. Pictures are taken by lib/feedback/page-picture on
  first ask and kept in the folder's `.loki/pictures/`; "before" pictures are
  taken the moment the preview starts, while they still are the before; an
  "after" is Loki opening its own preview with the access cookie
  (`x-loki-picture: 1` keeps the widget out of the shot), keyed by content.
- Watch it work links the tour and shows the Walkthrough from the folder.
- The brief says a preview is a first impression, not a migration: the
  homepage and the pages a visitor uses, at the live site's own paths.

The consultation's tour now reads the pages a buyer reads: navigation before
footer, the biggest section (a shop's catalogue) first, one page per section
before the second, accounts and small print left out. center-com.ru's tour had
been the login page, the privacy policy and the bank details.

From the review of this flow: the consultation cache is keyed by the
normalized address (every "Build" click had re-read the whole site inside the
request); a create saves the access code and attaches a host only after every
check has passed; name and folder agree (a leftover folder no longer serves
<name> at <name>-2); the gate no longer names the client; the chat's
read_website reuses the crawl; one in-flight helper; the shared escaper; the
preview route memoizes its project lookup for a page load; stale comments.

Dogfood without a browser: the Previews workflow gets `website` (the exact
/change path, as the operator, on the deployed app) and `check` (open a
preview as its owner and report the site, the tour and its pictures — the
cookie is minted on the box and never printed).

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Y9KbVEpxKLC9nhKhpWncU1

* fix(watch): the preview walkthrough reads from a lib the shared-access test can bind

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Y9KbVEpxKLC9nhKhpWncU1

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-10-17 19:30:21 +02:00

171 lines
6.3 KiB
TypeScript

import assert from "node:assert/strict";
import fs from "node:fs";
import ts from "typescript";
import { NextRequest, NextResponse } from "next/server";
import { isValidUuid } from "../../src/lib/utils";
import { dispatchMessageDisplay } from "../../src/lib/terminal-conversation";
import { buildWatchTimeline } from "../../src/lib/project-watch";
import { fleetSurfaceHref } from "../../src/lib/fleet-context";
import { buildRunTrack } from "../../src/lib/run-track";
async function main() {
const file = "src/app/api/projects/[id]/watch/route.ts";
const parsed = ts.createSourceFile(
file,
fs.readFileSync(file, "utf8"),
ts.ScriptTarget.Latest,
true,
);
const fn = parsed.statements.find((s) => ts.isFunctionDeclaration(s) && s.name?.text === "GET")!;
const js = ts.transpileModule(fn.getText(parsed).replace(/^export /, ""), {
compilerOptions: { target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.CommonJS },
}).outputText;
const project = "aaaaaaaa-bbbb-4ccc-8ddd-eeeeeeeeeeee",
runId = "11111111-2222-4333-8444-555555555555";
let actor = "owner";
let memberRole: "viewer" | "editor" | null = null;
const readers: string[] = [];
const run = {
id: runId,
projectId: project,
projectKey: "loki",
payload: {},
adapter: "claude",
startedAt: new Date(),
finishedAt: new Date(),
outcome: "failed",
summary: null,
};
const packet =
"# Loki operator dispatch\nInternal framing.\n\n## The operator's goals & deadlines\nPrivate life goal.\n\n## Your task (direct operator instruction)\nFix the project menu.\n\n## Exit contract\nInternal handoff.";
const query = {
from: () => query,
where: (condition: unknown) => {
assert(JSON.stringify(condition).includes('"owner"'));
return query;
},
orderBy: () => query,
limit: async () => [{ customPrompt: packet, resolvedPrompt: packet, dispatchedAt: new Date() }],
};
const ownerOnly = async (owner: string) => {
readers.push(owner);
assert.equal(owner, "owner");
};
const bindings = {
NextResponse,
isValidUuid,
dispatchMessageDisplay,
buildWatchTimeline,
fleetSurfaceHref,
FEEDBACK_STATUS: { DISPATCHED: "dispatched" },
getSessionUserId: async () => actor,
readIdParam: async () => project,
getProjectAccess: async () =>
actor === "owner"
? { ownerUserId: "owner", role: "owner", canEdit: true }
: memberRole
? { ownerUserId: "owner", role: memberRole, canEdit: memberRole === "editor" }
: null,
getProjectCore: async (owner: string) =>
owner === "owner" ? { id: project, name: "loki" } : null,
getServerKickoff: () => null,
previewWalkthrough: async () => null,
getOrchestrationRunById: async (owner: string) => {
await ownerOnly(owner);
return run;
},
// One loader gathers the run (lib/run-track-load); it reaches GitHub and
// the owner's budget, so it is called with the OWNER's id and told whose
// view this is — never asked for a member as if they were the owner.
loadRunTrack: async (owner: string, _run: unknown, opts: { ownerView: boolean }) => {
await ownerOnly(owner);
assert.equal(opts.ownerView, actor === "owner", "members get the owner-safe view");
const view = { phase: "failed", terminalReady: true, label: "Failed", stalled: false };
return {
track: buildRunTrack({
eventKinds: ["dispatched"],
eventTimes: {},
run: { startedAt: null, finishedAt: run.finishedAt.toISOString(), outcome: "failed" },
askedAt: null,
phase: "failed",
stalled: false,
lastProgressAt: null,
needsYou: null,
builderLabel: null,
evidence: null,
fix: null,
liveUrl: null,
providerLabel: "Claude",
failure: "It stopped.",
}),
follow: null,
fix: null,
following: false,
events: [{ kind: "dispatched", detail: null, createdAt: new Date() }],
snap: null,
view,
prompt: { text: packet, at: new Date() },
project: { id: "registered", gitUrl: null, liveUrl: null },
provider: "Claude",
error: null,
failed: true,
};
},
getProjectAutopilotOverride: async (owner: string) => {
await ownerOnly(owner);
return null;
},
getBeaconSettings: async (owner: string) => {
await ownerOnly(owner);
return { auto_inject_mode: "on" };
},
providerLabel: () => "Claude",
};
const get = new Function(...Object.keys(bindings), js + ";return GET;")(
...Object.values(bindings),
);
const call = () =>
get(new NextRequest(`https://loki.example/api/projects/${project}/watch?run=${runId}`), {
params: Promise.resolve({ id: project }),
});
const owned = await (await call()).json();
assert(
owned.items.find((i: { type: string }) => i.type === "you").text.includes("Private life goal"),
);
assert(owned.canRetry);
assert(owned.terminalHref);
assert(owned.autopilot);
assert.equal(owned.track.stages.length, 6);
assert.equal(owned.following, false, "a failed run has nothing left to follow");
actor = "member";
for (const role of ["viewer", "editor"] as const) {
memberRole = role;
const response = await call();
assert.equal(response.status, 200);
const shared = await response.json();
assert.equal(
shared.items.find((i: { type: string }) => i.type === "you").text,
"Fix the project menu.",
);
assert(!JSON.stringify(shared).includes("Private life goal"));
assert.equal(shared.canRetry, false);
assert.equal(shared.status.terminalReady, false);
assert.equal(shared.terminalHref, undefined);
assert.equal(shared.autopilot, undefined);
assert.equal(shared.tab, undefined);
assert.equal(shared.track.stages.length, 6, "members see where the change is");
assert.equal(shared.follow, null);
assert.equal(shared.following, false);
}
memberRole = null;
const before = readers.length;
assert.equal((await call()).status, 404);
assert.equal(readers.length, before);
console.log(
"✓ invited members read project-scoped owner-tenant progress without personal dispatch context or owner-only terminal/actions; strangers cannot read it",
);
}
main().catch((e) => {
console.error(e);
process.exitCode = 1;
});