mirror of
https://github.com/bitbaum/loki.git
synced 2026-12-03 20:37:06 +01:00
* fix(builder): a sealed session that has been silent for half an hour no longer holds a runner update The drain restarts the box runner only when no agent is working. A sealed session has no status file the drain can read: its agent lives inside gVisor and the unit's cgroup holds only the `podman run` that relays its terminal, so it counted as working for as long as it was open, and sessions stay open for days. Measured on the box, 2026-11-12 07:56 UTC (Sealed builds run 43178274733, mode=activity): three open sealed sessions, one of them eighteen hours old, relayed 0, 40 and 96 bytes in a minute while the drain logged "3 agent(s) working — deferring restart" on every look. The runner stayed two releases behind for hours, so the runner fixes merged that morning were not running, and the commands route held new work for the restart. - agent_is_busy: a sealed session that has relayed under 512 bytes a look for 30 minutes is not working. Shorter than that, or a quiet that cannot be told, still is: unknown is not idle. - quiet_for reads the relay's cumulative byte counters and keeps its note under /run/loki-drain/<unit>, outside the drain's own process: every deploy starts a new drain, and a clock that restarted with it would never reach thirty minutes on a box that deploys several times an hour. - The rule is the sealed session's alone. An unsealed agent that is not Claude still always holds the restart: its output was not measured. What it can cost: a sealed agent stopped at a permission prompt in the middle of a task prints nothing, so after thirty minutes of that the restart goes ahead; the session reopens and its open run is closed. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019MDtmt4p1jMfJnbJtpeNhi * fix(composer): the project composer falls back to the cloud runner when the project's own builder is off The chat reroutes a project pinned to an offline computer to the cloud runner and says so (inject-core). The project composer for an idle project (/api/control/tab-inject) asked for the routing decision without that fallback: a project registered from a laptop queued for the laptop while it was shut, with the laptop's folder as the place to start, and the answer named no builder. The row sat until the reaper closed it an hour later. - tab-inject passes the project's offline fallback, starts a rerouted dispatch in the folder the cloud runner clones into, and answers with the builder that took it and the reroute sentence. - execution-access test pins the three. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019MDtmt4p1jMfJnbJtpeNhi --------- Co-authored-by: Claude <noreply@anthropic.com>
171 lines
6.7 KiB
TypeScript
171 lines
6.7 KiB
TypeScript
/**
|
|
* Inline tests for multitenant execution routing.
|
|
* Run: npx tsx scripts/test/execution-access.ts
|
|
*/
|
|
import { readFileSync } from "node:fs";
|
|
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");
|
|
}
|
|
// The project composer falls back the same way. It asked for the decision
|
|
// without the fallback, so a project registered from a laptop queued for that
|
|
// laptop while it was shut, and the answer named no builder at all
|
|
// (2026-11-12: the owner's computer had been offline for a day).
|
|
{
|
|
const route = readFileSync("src/app/api/control/tab-inject/route.ts", "utf8");
|
|
if (!/offlineFallback: project \? offlineFallbackChannel\(project\) : null/.test(route))
|
|
throw new Error("the project composer must pass the project's offline fallback");
|
|
if (
|
|
!/dir: reroutedFrom\s*\?\s*\(coldStartWorkspaceDir\(project\.name, project\.gitUrl\) \?\? project\.dirPath\)/.test(
|
|
route,
|
|
)
|
|
)
|
|
throw new Error("a rerouted dispatch must not carry the other machine's folder");
|
|
if (!/\.\.\.\(rerouted \? \{ message: rerouted \} : \{\}\)/.test(route))
|
|
throw new Error("the answer must say in a sentence that another builder took the work");
|
|
}
|
|
console.log("✓ execution-access offline fallback");
|