mirror of
https://github.com/bitbaum/loki.git
synced 2026-12-05 11:48:31 +01:00
Track 0 of docs/master-plan-2026-10.md: stop the leak and the routing lies,
app only, nothing on the box changes.
Stop the leak
- A request on a shared project from a Builder, a client or a viewer is
filed as a suggestion in the owner's tenant (source `client` or the new
`member`), announced like a visitor's note, and waits for the owner's
tap. Only the owner's own word starts a build on the owner's builder
(ask-in-loki.ts, implement.ts, pass-holder.ts). Until now a Builder's
ask built at once on the owner's subscription and a client's built on
a monthly allowance; both spent one person's engine on another's word.
- The per-site allowance is retired: config/client-allowance.ts, the
members PATCH, the People-panel picker, the dossier count, the invite
copy. The `monthly_changes` columns stay in the schema unread (a DROP
aborts the deploy; dropped by hand later). `on_allowance` is kept and
read as `previewFirst`: a change built for the person who asked still
opens as a draft and goes live on their "Make it live".
- The owner pass is the owner's alone; everyone with a seat carries a
member pass. The 40-builds-a-day cap is keyed by the owner, not the
project: a guard on one engine, never a price.
Routing honesty
- inject-core requests only what was chosen (a lock, a named builder, a
stored preference) and lets the decision apply the floor, so a fresh
account with Fleet Runner connected runs there instead of a 403
(execution-access.ts requestedDispatchChannel).
- Kickoff writes a path under the cloud builder's root only for accounts
the cloud serves; for anyone else the path stays empty so the project
is not locked to a builder they do not have.
- "Hosted runner" is offered, accepted and dispatched only for the one
account it serves (lib/hosted-runner/serves.ts).
Records: CHANGELOG entry {#seat-suggests}, ROADMAP milestone ticked,
CLAUDE.md and feedback-widget.md say what a seat's ask is now. Tests:
client-seat rewritten, execution-access extended, the pinned shapes in
client-seat-browser, seat-on-site-and-tracking, feedback-trust,
project-capabilities and owner-note-builds updated.
Claude-Session: https://claude.ai/code/session_01NZqZujmXpupVDC1Hi5Jbsx
Co-authored-by: Claude <noreply@anthropic.com>
193 lines
8.3 KiB
TypeScript
193 lines
8.3 KiB
TypeScript
/**
|
|
* The owner says what to change on their own site, and it gets built.
|
|
*
|
|
* Before this, the widget could not tell the owner from a stranger: every note
|
|
* waited in /feedback for the owner to return to Loki and press Implement, then
|
|
* merge, on their own words. The owner's link now carries a signed pass; a note
|
|
* sent with it starts the fix at ingest and merges itself once green.
|
|
*/
|
|
import assert from "node:assert/strict";
|
|
import { readFileSync } from "node:fs";
|
|
|
|
import {
|
|
createOwnerPass,
|
|
verifyOwnerPass,
|
|
ownerSiteUrl,
|
|
OWNER_PASS_HASH_KEY,
|
|
ownerReturnUrl,
|
|
} from "../../src/lib/feedback/owner-pass";
|
|
import { effectiveAutoShip } from "../../src/lib/feedback/auto-ship";
|
|
import { composeFeedbackFixPrompt } from "../../src/lib/feedback/compose-dispatch";
|
|
import { hashWithoutPass, ownerSignInUrl, passFromHash } from "../../widget/owner-pass";
|
|
import { FEEDBACK_SELF_ASSERTED_SOURCES, FEEDBACK_SOURCE } from "../../src/lib/constants/statuses";
|
|
|
|
// Read when a pass is signed, not at import.
|
|
process.env.AUTH_SECRET ??= "test-secret-for-owner-pass";
|
|
|
|
const project = "8d96d258-36f2-4b47-a897-9a5baeb6c296";
|
|
const owner = "11111111-2222-4333-8444-555555555555";
|
|
const now = Date.parse("2026-09-26T08:00:00Z");
|
|
|
|
// The pass proves exactly one project and one owner, and it expires.
|
|
const pass = createOwnerPass(project, owner, now);
|
|
assert.deepEqual(verifyOwnerPass(pass, now), { projectId: project, userId: owner });
|
|
assert.equal(verifyOwnerPass(pass, now + 91 * 24 * 60 * 60 * 1000), null, "expired pass");
|
|
const [p, u, exp, sig] = pass.split(".");
|
|
assert.equal(verifyOwnerPass(`${p}.${u}.${Number(exp) + 1}.${sig}`, now), null, "tampered expiry");
|
|
assert.equal(
|
|
verifyOwnerPass(`${p}.99999999-2222-4333-8444-555555555555.${exp}.${sig}`, now),
|
|
null,
|
|
"a pass cannot be moved to another person",
|
|
);
|
|
assert.equal(verifyOwnerPass("garbage", now), null);
|
|
|
|
// The link puts the pass in the FRAGMENT, which browsers never send to a server.
|
|
const url = ownerSiteUrl("https://farmhouse.orangecat.ch/#old", pass);
|
|
assert.ok(url.startsWith("https://farmhouse.orangecat.ch/#"), url);
|
|
assert.ok(url.includes(`${OWNER_PASS_HASH_KEY}=`), url);
|
|
assert.ok(!url.includes("?"), "never a query string");
|
|
|
|
// Owner is earned, never asserted: the widget body may not claim it.
|
|
assert.ok(!(FEEDBACK_SELF_ASSERTED_SOURCES as readonly string[]).includes(FEEDBACK_SOURCE.OWNER));
|
|
const ingest = readFileSync("src/app/api/feedback/route.ts", "utf8");
|
|
assert.match(
|
|
ingest,
|
|
/z\.enum\(FEEDBACK_SELF_ASSERTED_SOURCES\)/,
|
|
"body source uses the restricted list",
|
|
);
|
|
assert.match(
|
|
ingest,
|
|
/const holder = passHolder\(data\.ownerPass, token\);\s*const fromOwner = holder !== null;/,
|
|
"the owner, verified — and only the owner (whoever asks pays, 2026-10-20)",
|
|
);
|
|
// One way an owner's words start a fix, shared by Report and the walkthrough's "Not quite".
|
|
const ownerBuild = readFileSync("src/lib/feedback/owner-build.ts", "utf8");
|
|
assert.match(
|
|
ownerBuild,
|
|
/implementFeedback\(ownerUserId, feedbackId\)/,
|
|
"owner notes start the fix",
|
|
);
|
|
assert.match(
|
|
readFileSync("src/app/api/widget/tour/route.ts", "utf8"),
|
|
/startOwnerBuild\(f\.userId, f\.projectId, id\)/,
|
|
"a walkthrough's 'Not quite' starts the fix the same way",
|
|
);
|
|
|
|
// The owner repeating a note retries it; it must not answer as a visitor, or
|
|
// the widget drops a good pass. So the pass is checked BEFORE dedupe.
|
|
assert.ok(
|
|
ingest.indexOf("const fromOwner") < ingest.indexOf("bumpDuplicateFeedback("),
|
|
"owner check precedes dedupe",
|
|
);
|
|
assert.match(ingest, /startOwnerBuild\(token\.userId, token\.projectId, bumped\)/);
|
|
assert.match(
|
|
ownerBuild,
|
|
/status === 409 && body\.alreadyRunning === true\) return \{ building: true \}/,
|
|
);
|
|
|
|
// Owner notes merge themselves unless the project explicitly turned that off.
|
|
assert.equal(effectiveAutoShip(null, FEEDBACK_SOURCE.OWNER), true);
|
|
assert.equal(effectiveAutoShip(undefined, FEEDBACK_SOURCE.OWNER), true);
|
|
assert.equal(effectiveAutoShip(false, FEEDBACK_SOURCE.OWNER), false, "explicit off wins");
|
|
assert.equal(effectiveAutoShip(null, FEEDBACK_SOURCE.VISITOR), null, "strangers still wait");
|
|
assert.equal(effectiveAutoShip(true, FEEDBACK_SOURCE.VISITOR), true);
|
|
const attach = readFileSync("src/lib/feedback/attach-work.ts", "utf8");
|
|
assert.match(attach, /autoShip: effectiveAutoShip\(project\?\.autoShip, item\.source, item\)/);
|
|
|
|
// The Implement button and the owner note share one path.
|
|
const route = readFileSync("src/app/api/feedback/[id]/dispatch/route.ts", "utf8");
|
|
assert.match(route, /implementFeedback\(userId, idOrResp/);
|
|
assert.ok(!route.includes("injectPrompt"), "the route must not keep its own copy");
|
|
|
|
// The widget lifts the pass out of the fragment and leaves the rest intact, so
|
|
// a link copied from the address bar afterwards does not carry it.
|
|
assert.equal(passFromHash(new URL(url).hash), pass);
|
|
assert.equal(passFromHash("#section-2"), null);
|
|
assert.equal(hashWithoutPass(`#${OWNER_PASS_HASH_KEY}=abc`), "");
|
|
assert.equal(hashWithoutPass(`#tab=plan&${OWNER_PASS_HASH_KEY}=abc`), "#tab=plan");
|
|
const widget = readFileSync("widget/main.ts", "utf8");
|
|
assert.match(
|
|
readFileSync("widget/conversation-model.ts", "utf8"),
|
|
/ownerPass: pass \?\? undefined/,
|
|
"the widget sends the pass",
|
|
);
|
|
assert.match(widget, /if \(ownerState\.arrived\) pendingReport = \{\}/, "arriving opens the note");
|
|
|
|
// Loki hands the pass on the Live link to whoever may hold one:
|
|
const page = readFileSync("src/app/(app)/projects/[id]/page.tsx", "utf8");
|
|
// The owner and every Builder (lib/feedback/pass-rule.ts, mayHoldPass) —
|
|
// a viewer's or a client's "Open site" is the plain link.
|
|
assert.match(
|
|
page,
|
|
/mayHoldPass\(dossier\.viewerRole\) \? createOwnerPass\(id, session\.user\.id\) : null/,
|
|
);
|
|
|
|
// The agent is told the owner asked for it — a request to build, not a report.
|
|
const ownerFields = {
|
|
suggestion: "Add a Back to top link",
|
|
duplicateCount: 1,
|
|
url: "https://farmhouse.orangecat.ch/",
|
|
page: "/",
|
|
scope: null,
|
|
selectedElements: null,
|
|
};
|
|
assert.match(
|
|
composeFeedbackFixPrompt({ ...ownerFields, source: "owner" }, "Farmhouse"),
|
|
/^The owner of Farmhouse asked for this change/,
|
|
);
|
|
assert.match(composeFeedbackFixPrompt(ownerFields, "Farmhouse"), /^Fix this visitor feedback/);
|
|
|
|
// "This is my site — let Loki watch": the pass goes back ONLY to the project's
|
|
// own site. Anywhere else would hand a working pass to whoever wrote the link.
|
|
{
|
|
const site = ["https://farm.example"];
|
|
assert.equal(
|
|
ownerReturnUrl("https://farm.example/menu?x=1", site),
|
|
"https://farm.example/menu?x=1",
|
|
);
|
|
assert.equal(ownerReturnUrl("https://evil.example/", site), null, "another site");
|
|
assert.equal(
|
|
ownerReturnUrl("https://farm.example.evil.example/", site),
|
|
null,
|
|
"a lookalike host",
|
|
);
|
|
assert.equal(
|
|
ownerReturnUrl("http://farm.example/", site),
|
|
null,
|
|
"another scheme is another origin",
|
|
);
|
|
assert.equal(
|
|
ownerReturnUrl("https://user:pw@farm.example/", site),
|
|
null,
|
|
"credentials in the URL",
|
|
);
|
|
assert.equal(ownerReturnUrl("javascript:alert(1)", site), null);
|
|
assert.equal(ownerReturnUrl("/relative", site), null);
|
|
assert.equal(ownerReturnUrl(null, site), null);
|
|
assert.equal(ownerReturnUrl("https://farm.example/", []), null, "no known site, no redirect");
|
|
// The widget builds the link; the hash it is on never travels.
|
|
const link = new URL(
|
|
ownerSignInUrl("https://loki.test", "fcw_x", "https://farm.example/p#loki-owner=old"),
|
|
);
|
|
assert.equal(link.pathname, "/api/widget/owner");
|
|
assert.equal(link.searchParams.get("return"), "https://farm.example/p");
|
|
|
|
// Signed out, the link must lead through sign-in and BACK — shipped once as a
|
|
// bare 401 (the proxy gates /api/*) and with a param sign-in ignores.
|
|
const ownerRoute = readFileSync("src/app/api/widget/owner/route.ts", "utf8");
|
|
const signInForm = readFileSync("src/components/auth/SignInForm.tsx", "utf8");
|
|
const param = /searchParams\.get\("(\w+)"\)/.exec(signInForm)?.[1];
|
|
assert.ok(param, "the sign-in form reads a return parameter");
|
|
assert.ok(
|
|
ownerRoute.includes(`searchParams.set("${param}"`),
|
|
`the owner route returns through sign-in's own parameter (${param})`,
|
|
);
|
|
assert.match(ownerRoute, /if \(!userId\)/, "the handler itself requires a session");
|
|
assert.ok(
|
|
readFileSync("src/proxy.ts", "utf8").includes("|api/widget/owner|"),
|
|
"the proxy lets the route redirect a signed-out owner instead of answering 401",
|
|
);
|
|
}
|
|
|
|
console.log("owner-note-builds: ok");
|