Files
loki/scripts/test/execution-access.ts
CatoandClaude 83316f063a feat(feedback): whoever asks pays — a seat's request is a suggestion, the allowance is retired, routing tells the truth (#1344)
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>
2026-10-20 11:43:36 +02:00

153 lines
5.7 KiB
TypeScript

/**
* Inline tests for multitenant execution routing.
* Run: npx tsx scripts/test/execution-access.ts
*/
import {
decideQueuedExecution,
offlineFallbackChannel,
requestedDispatchChannel,
type ExecutionAccess,
} from "@/lib/execution-access";
function access(input: {
cloudBuilderAllowed: boolean;
cloud?: boolean;
local?: boolean;
}): ExecutionAccess {
return {
userId: "user-test",
cloudBuilderAllowed: input.cloudBuilderAllowed,
presence: {
cloud: input.cloud ?? false,
local: input.local ?? false,
any: Boolean(input.cloud || input.local),
},
};
}
const founderCloud = decideQueuedExecution(access({ cloudBuilderAllowed: true, cloud: true }), {
defaultChannel: "cloud",
});
if (!founderCloud.ok || founderCloud.channel !== "cloud" || founderCloud.runnerConnected !== true) {
throw new Error("founder cloud routing");
}
const founderCloudOffline = decideQueuedExecution(access({ cloudBuilderAllowed: true }), {
defaultChannel: "cloud",
});
if (
!founderCloudOffline.ok ||
founderCloudOffline.channel !== "cloud" ||
founderCloudOffline.runnerConnected !== false
) {
throw new Error("founder cloud offline queue");
}
const tenantLocal = decideQueuedExecution(access({ cloudBuilderAllowed: false, local: true }), {
defaultChannel: "cloud",
});
if (!tenantLocal.ok || tenantLocal.channel !== "local" || tenantLocal.runnerConnected !== true) {
throw new Error("tenant reroutes to local builder");
}
const tenantNoBuilder = decideQueuedExecution(access({ cloudBuilderAllowed: false }), {
defaultChannel: "cloud",
});
if (
tenantNoBuilder.ok ||
tenantNoBuilder.status !== 409 ||
tenantNoBuilder.code !== "builder-required"
) {
throw new Error("tenant without builder must not queue into the void");
}
const tenantExplicitCloud = decideQueuedExecution(
access({ cloudBuilderAllowed: false, local: true }),
{ requestedChannel: "cloud" },
);
if (
tenantExplicitCloud.ok ||
tenantExplicitCloud.status !== 403 ||
tenantExplicitCloud.code !== "cloud-builder-private"
) {
throw new Error("tenant explicit cloud must be blocked");
}
// What inject-core REQUESTS: a lock or a stored choice, never the cloud
// floor. Passing the floor as the request turned every fresh account's first
// dispatch into a 403 with Fleet Runner connected (2026-10-20).
if (requestedDispatchChannel({ builderPref: null, gitUrl: "https://github.com/x/y" }) !== null)
throw new Error("nobody chose: nothing is requested");
if (requestedDispatchChannel({ builderPref: "local" }) !== "local")
throw new Error("a stored choice is requested");
if (requestedDispatchChannel({ builderPref: "hosted" }) !== null)
throw new Error("hosted is not a channel anyone claims");
const tenantFloor = decideQueuedExecution(access({ cloudBuilderAllowed: false, local: true }), {
requestedChannel: requestedDispatchChannel({ builderPref: null }),
defaultChannel: "cloud",
});
if (!tenantFloor.ok || tenantFloor.channel !== "local")
throw new Error("an unchosen project on a tenant with a computer runs on the computer");
const founderFloor = decideQueuedExecution(access({ cloudBuilderAllowed: true, cloud: true }), {
requestedChannel: requestedDispatchChannel({ builderPref: null }),
defaultChannel: "cloud",
});
if (!founderFloor.ok || founderFloor.channel !== "cloud")
throw new Error("the floor still routes the founder to the cloud");
console.log("✓ execution-access tests passed");
// ── Offline fallback: a preference for a shut laptop does not park the work ─
{
const CLONEABLE = "https://github.com/bitbaum/loki.git";
// Preference for this computer, cloneable repo, no lock: may fall through.
if (offlineFallbackChannel({ gitUrl: CLONEABLE, builderPref: "local" }) !== "cloud") {
throw new Error("a local preference on a cloneable repo falls through to the cloud");
}
// Locked to the laptop (a tree with no repo): never.
if (offlineFallbackChannel({ dirPath: "/home/g/dev/scratch", builderPref: "local" }) !== null) {
throw new Error("a locus lock never falls through");
}
// Cloud preference: the desktop does not clone on demand, so never.
if (offlineFallbackChannel({ gitUrl: CLONEABLE }) !== null) {
throw new Error("cloud work does not fall through to a laptop");
}
// Laptop off, cloud on → the cloud takes it and says where it came from.
const fell = decideQueuedExecution(access({ cloudBuilderAllowed: true, cloud: true }), {
requestedChannel: "local",
offlineFallback: "cloud",
});
if (
!fell.ok ||
fell.channel !== "cloud" ||
fell.reroutedFrom !== "local" ||
!fell.runnerConnected
) {
throw new Error("offline local preference falls through to the online cloud");
}
// Laptop on → stays put, nothing to report.
const stays = decideQueuedExecution(
access({ cloudBuilderAllowed: true, cloud: true, local: true }),
{ requestedChannel: "local", offlineFallback: "cloud" },
);
if (!stays.ok || stays.channel !== "local" || stays.reroutedFrom !== undefined) {
throw new Error("an online preference is honoured");
}
// Both off → queues for the chosen one, visibly.
const both = decideQueuedExecution(access({ cloudBuilderAllowed: true }), {
requestedChannel: "local",
offlineFallback: "cloud",
});
if (!both.ok || both.channel !== "local" || both.runnerConnected !== false || both.reroutedFrom) {
throw new Error("no online sibling → queue for the chosen builder");
}
// A tenant without cloud access never falls through to the shared box.
const tenant = decideQueuedExecution(access({ cloudBuilderAllowed: false, cloud: true }), {
requestedChannel: "local",
offlineFallback: "cloud",
});
if (tenant.ok) throw new Error("tenant without cloud access must not fall through to it");
}
console.log("✓ execution-access offline fallback");