Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions apps/server/src/lazurio/holders.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ import {
processAlive,
readBootClock,
readSessionBindings,
readUnverifiedSessions,
replaceOwnerFile,
stopSessionDaemon,
unverifiedBindingDirectory,
watchDirectories,
} from "./holders.ts";

Expand Down Expand Up @@ -174,6 +176,47 @@ describe("the Platform's vectors", () => {
expect(await readSessionBindings(NodePath.join(sockets, "none"))).toEqual([]);
});

// LazurioPlatform `src/browser/contract/vectors/v1/unverified-binding.json` (C19).
it("a mark of an unverified binding counts whatever its text; a directory that cannot be listed is thrown", async () => {
expect(unverifiedBindingDirectory({ XDG_RUNTIME_DIR: "/run/user/1000" })).toBe(
"/run/user/1000/lazurio-browser/unverified",
);
expect(unverifiedBindingDirectory({ XDG_RUNTIME_DIR: "run/user" })).toBeUndefined();
expect(unverifiedBindingDirectory({})).toBeUndefined();
const runtime = await temporary("holders-unverified-");
const marks = NodePath.join(runtime, "lazurio-browser", "unverified");
expect(await readUnverifiedSessions({ XDG_RUNTIME_DIR: runtime })).toEqual([]);
expect(await readUnverifiedSessions({})).toEqual([]);
await NodeFSP.mkdir(marks, { recursive: true });
const write = (name: string, text: string) =>
NodeFSP.writeFile(NodePath.join(marks, name), text);
await write(
"t3-thread.json",
JSON.stringify({
schema: "lazurio.browser-unverified-binding.v1",
session: "t3-thread",
targetId: id(14),
before: null,
}),
);
await write("codex-2.json", "not a mark");
await write("claude-1.json", "");
await NodeFSP.chmod(NodePath.join(marks, "claude-1.json"), 0o000);
await write(".codex-3.json.tmp", "");
await write("codex 4.json", "");
await write("codex-5.txt", "");
expect(await readUnverifiedSessions({ XDG_RUNTIME_DIR: runtime })).toEqual([
"claude-1",
"codex-2",
"t3-thread",
]);
if (process.getuid?.() !== 0) {
await NodeFSP.chmod(marks, 0o000);
await expect(readUnverifiedSessions({ XDG_RUNTIME_DIR: runtime })).rejects.toThrow();
await NodeFSP.chmod(marks, 0o700);
}
});

it("a process is alive while kill -0 reaches it", () => {
expect(processAlive(process.pid)).toBe(true);
for (const pid of [0, 1, -5, Number.NaN, 1.5]) expect(processAlive(pid)).toBe(false);
Expand Down
32 changes: 29 additions & 3 deletions apps/server/src/lazurio/holders.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// @effect-diagnostics nodeBuiltinImport:off globalDate:off globalTimers:off - The holder files and the daemon's socket are the Platform's, read, written and asked outside the Effect runtime as it does.

/**
* Lazurio overlay (LazurioPlatform F47 points 4 and 5): the hub keeps a remote tab while a holder
* file lists it or a bound agent-browser daemon runs. Copied from LazurioPlatform
* `src/browser/people/holds.ts` at 49d54c9; holders.test.ts runs the Platform's vectors.
* Lazurio overlay (LazurioPlatform F47 points 4 and 5): copied from LazurioPlatform
* `src/browser/people/holds.ts` at 49d54c9, and the two readers of C19's marks from
* `src/browser/contract/node.ts`; holders.test.ts runs the Platform's vectors.
*/

import * as NodeCrypto from "node:crypto";
Expand Down Expand Up @@ -52,6 +52,32 @@ export function liveTabsPath(
: undefined;
}

/** `$XDG_RUNTIME_DIR/lazurio-browser/unverified`, where the Platform marks a session whose
* agent-browser binding it has not verified (C19); undefined without an absolute runtime
* directory. */
export function unverifiedBindingDirectory(
env: Readonly<Record<string, string | undefined>>,
): string | undefined {
const runtime = env.XDG_RUNTIME_DIR;
return runtime !== undefined && NodePath.isAbsolute(runtime)
? NodePath.join(runtime, "lazurio-browser", "unverified")
: undefined;
}

/** The sessions marked unverified now (C19), sorted: each `<session>.json` of the directory
* whose name is a session's, whatever its text. Empty without the directory; a directory that
* cannot be listed is thrown. */
export async function readUnverifiedSessions(
env: Readonly<Record<string, string | undefined>>,
): Promise<string[]> {
const directory = unverifiedBindingDirectory(env);
if (directory === undefined) return [];
return (await directoryNames(directory)).flatMap((name) => {
const session = name.endsWith(".json") ? name.slice(0, -".json".length) : "";
return isStrip(session) ? [session] : [];
});
}

/** Where agent-browser keeps its sockets and a session's tab binding
* (`AGENT_BROWSER_SOCKET_DIR` when absolute, else `$XDG_RUNTIME_DIR/agent-browser`), as the
* Platform's `agentBrowserSocketDirectory` (`src/browser/window.ts`). */
Expand Down
72 changes: 70 additions & 2 deletions apps/server/src/lazurio/threadTabs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ async function world(options: { readonly declared?: boolean; readonly ready?: bo
/** Reads of a live thread wait here while the test holds them back (`holdLiveReads`). */
let liveGate: { readonly open: Promise<void>; readonly reached: () => void } | null = null;
let markReady = () => {};
let afterRead: ((path: string) => Promise<void>) | null = null;
const ready =
options.ready === false
? new Promise<void>((resolve) => {
Expand All @@ -90,7 +91,10 @@ async function world(options: { readonly declared?: boolean; readonly ready?: bo
ready,
readFile: async (path) => {
if (path === listPath) listReads.push(listReads.length);
return NodeFSP.readFile(path, "utf8");
const text = await NodeFSP.readFile(path, "utf8");
const hook = afterRead;
if (hook !== null) await hook(path);
return text;
},
daemon: {
alive: (pid) => processes.has(pid),
Expand Down Expand Up @@ -189,6 +193,10 @@ async function world(options: { readonly declared?: boolean; readonly ready?: bo
await NodeFSP.mkdir(holders, { recursive: true, mode: 0o700 });
await NodeFSP.writeFile(fileOf(strip), holderFileText(strip, label, tabs), { mode: 0o600 });
},
/** Runs once a file has been read, before the reader gets its text. */
afterRead: (hook: ((path: string) => Promise<void>) | null) => {
afterRead = hook;
},
bind: async (session: string, targetId: string, pid?: number) => {
await NodeFSP.mkdir(sockets, { recursive: true });
await NodeFSP.writeFile(
Expand Down Expand Up @@ -415,7 +423,12 @@ describe("writes re-read the file and fail closed", () => {
expect(w.listReads.length - before).toBe(1);
// The watch covers the hub's directory, the holder files and the agents' bindings.
expect(w.watched).toEqual([
[NodePath.join(w.runtime, "lazurio-browser"), w.holders, w.sockets],
[
NodePath.join(w.runtime, "lazurio-browser"),
w.holders,
w.sockets,
NodePath.join(w.runtime, "lazurio-browser", "unverified"),
],
]);
});

Expand Down Expand Up @@ -684,6 +697,61 @@ describe("tabs an agent opens", () => {
expect(tabIds(await w.core.read(threadA))).toEqual([id(9), id(10)]);
});

it("a new binding of a session the Platform has not verified is taken in only once its mark is gone (LazurioPlatform#989)", async () => {
const w = await world();
const marks = NodePath.join(w.runtime, "lazurio-browser", "unverified");
await NodeFSP.mkdir(marks, { recursive: true });
const mark = NodePath.join(marks, `${stripA}.json`);
// `lazurio browser window` is binding the session: its binding is no evidence yet.
await NodeFSP.writeFile(mark, "{}");
await w.bind(stripA, id(11));
await w.core.refresh();
expect(await w.stored()).toBeNull();
// Verified: the mark goes, and the window joins the strip.
await NodeFSP.rm(mark);
await w.core.refresh();
expect(tabIds(await w.core.read(threadA))).toEqual([id(11)]);
// Marks that cannot be listed: no new binding is taken in meanwhile.
if (process.getuid?.() !== 0) {
await NodeFSP.chmod(marks, 0o000);
await w.bind(stripA, id(12));
await w.core.refresh();
expect(tabIds(await w.core.read(threadA))).toEqual([id(11)]);
expect(w.logs).toContain("thread-tabs-unverified-unreadable");
await NodeFSP.chmod(marks, 0o700);
await w.core.refresh();
expect(tabIds(await w.core.read(threadA))).toEqual([id(11), id(12)]);
}
});

it("a binding read while the Platform still verified it is not taken in, though the binding changes and its mark goes before the marks are listed (review of #208)", async () => {
const w = await world();
const marks = NodePath.join(w.runtime, "lazurio-browser", "unverified");
await NodeFSP.mkdir(marks, { recursive: true });
const mark = NodePath.join(marks, `${stripA}.json`);
const target = NodePath.join(w.sockets, `${stripA}.target`);
await NodeFSP.writeFile(mark, "{}");
await w.bind(stripA, id(13));
w.afterRead(async (path) => {
if (path !== target) return;
w.afterRead(null);
await w.bind(stripA, id(14));
await NodeFSP.rm(mark);
});
await w.core.refresh();
expect(await w.stored()).toBeNull();
// A binding that moves between two reads, with no mark in sight, waits for a read that holds.
w.afterRead(async (path) => {
if (path !== target) return;
w.afterRead(null);
await w.bind(stripA, id(15));
});
await w.core.refresh();
expect(await w.stored()).toBeNull();
await w.core.refresh();
expect(tabIds(await w.core.read(threadA))).toEqual([id(15)]);
});

it("a binding unchanged after a release is not recorded again; a retired thread's is ignored", async () => {
const w = await world();
await w.bind(stripA, id(3));
Expand Down
34 changes: 33 additions & 1 deletion apps/server/src/lazurio/threadTabs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import {
processAlive,
readBootClock,
readSessionBindings,
readUnverifiedSessions,
replaceOwnerFile,
type SessionBinding,
stopSessionDaemon,
Expand Down Expand Up @@ -388,6 +389,17 @@ export function createThreadTabs(seams: ThreadTabsCoreSeams): ThreadTabsCore {
holdsOf = holds;
}

/** The sessions whose binding the Platform has not verified (C19); every session when the
* marks cannot be listed, so no binding is taken in meanwhile. */
async function unverifiedSessions(): Promise<ReadonlySet<string> | "all"> {
try {
return new Set(await readUnverifiedSessions({ XDG_RUNTIME_DIR: seams.runtimeDirectory }));
} catch {
log("thread-tabs-unverified-unreadable");
return "all";
}
}

/** The agents' bindings: which tabs an agent works in, and the `t3-` sessions bound to another
* tab since they were last seen (none on the first read, the baseline). */
async function readBindings(baseline: boolean): Promise<SessionBinding[]> {
Expand All @@ -396,6 +408,9 @@ export function createThreadTabs(seams: ThreadTabsCoreSeams): ThreadTabsCore {
boundBy = new Map();
return [];
}
// Marks listed before and after the read, and a second read that must agree: a binding read
// mid-verification is never taken in, whatever the Platform changes in between (C19).
const markedBefore = await unverifiedSessions();
let bindings: SessionBinding[];
try {
bindings = await readSessionBindings(seams.socketDirectory, alive, seams.readFile);
Expand All @@ -411,9 +426,21 @@ export function createThreadTabs(seams: ThreadTabsCoreSeams): ThreadTabsCore {
if (sessions === undefined) boundBy.set(binding.targetId, [binding.session]);
else sessions.push(binding.session);
}
const markedAfter = await unverifiedSessions();
const readAgain = await readSessionBindings(seams.socketDirectory, alive, seams.readFile).then(
(again) => new Map(again.map((binding) => [binding.session, binding.targetId])),
() => null,
);
const marked = (session: string) =>
markedBefore === "all" ||
markedAfter === "all" ||
markedBefore.has(session) ||
markedAfter.has(session);
const fresh: SessionBinding[] = [];
for (const binding of bindings) {
if (!isThreadStrip(binding.session)) continue;
// Taken in, or not, once the Platform has verified it and its mark is gone (LazurioPlatform#989).
if (marked(binding.session) || readAgain?.get(binding.session) !== binding.targetId) continue;
const last = lastBound.get(binding.session);
lastBound.set(binding.session, binding.targetId);
if (!baseline && last !== binding.targetId) fresh.push(binding);
Expand Down Expand Up @@ -660,7 +687,12 @@ export function createThreadTabs(seams: ThreadTabsCoreSeams): ThreadTabsCore {
started = true;
if (stopped) return;
watcher = (seams.watch ?? watchDirectories)(
[lazurioDirectory, holdersDirectory, seams.socketDirectory],
[
lazurioDirectory,
holdersDirectory,
seams.socketDirectory,
NodePath.join(lazurioDirectory, "unverified"),
],
refreshSoon,
);
beat = setInterval(() => {
Expand Down
8 changes: 5 additions & 3 deletions docs/operations/lazurio-fork-release.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ zachovává. Licence má jít s každou kopií: image ji nese v
porovná se souborem v repozitáři.

Funkce, které overlay zkopíroval z LazurioPlatform `src/browser/people/holds.ts`
ve stavu 49d54c9 do `apps/server/src/lazurio/holders.ts` (vyjmenovává je
hlavička toho souboru), jsou podle grantu Organization Admina z 2026-10-12 pod
licencí MIT (LazurioPlatform, rozhodnutí F50 bod 6). Ostatní soubory Platformy
ve stavu 49d54c9 a z modulu kontraktu `src/browser/contract/node.ts` (čtení
značky neověřené vazby, C19) do `apps/server/src/lazurio/holders.ts`
(vyjmenovává je hlavička toho souboru), jsou podle grantu Organization Admina
z 2026-10-12 pod licencí MIT (LazurioPlatform, rozhodnutí F50 bod 6; modul
kontraktu je pod MIT celý). Ostatní soubory Platformy
zůstávají pod Elastic-2.0. Záznam platí, dokud kopie nenahradí soubory kontraktu
prohlížeče, které fork převezme z artefaktu Platformy (F50 bod 5).

Expand Down
10 changes: 9 additions & 1 deletion docs/operations/lazurio-overlay.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,15 @@ k přestavbě na v0.1.0, Lazurio/t3code#70), a tak overlay dělá tohle:
kterou drží jiný pás nebo kterou jmenuje vazba jiného sezení, server do pásu
nepřevezme, pokud ji pás vlákna už nemá (LazurioPlatform#974): agent-browser
vazbu zapíše i po neúspěšném `tab <id>`, na záložku, kterou si čerstvý daemon
při připojení vybral sám, třeba na záložku jiného agenta.
při připojení vybral sám, třeba na záložku jiného agenta. Vazbu sezení, které
Platforma označila jako neověřené (`$XDG_RUNTIME_DIR/lazurio-browser/unverified/<sezení>.json`,
kontrakt C19), server do pásu nepřevezme vůbec, dokud značka nezmizí; pak ji
posoudí jako každou novou vazbu (LazurioPlatform#989). Značky čte před
vazbami i po nich a vazby přečte dvakrát: převezme jen vazbu, kterou žádné
z obou čtení značek nezná a která se mezi oběma čteními nezměnila, takže ani
vazbu přečtenou uprostřed ověřování nevezme za ověřenou. Adresář značek
sleduje spolu s ostatními, a když ho nejde vypsat, nepřevezme žádnou novou
vazbu.
- **Cesta pro web.** `GET /api/lazurio/thread-tabs?threadId=<id>` vrátí snímek
nebo `{"available": false}`, `GET …/events?threadId=<id>` je SSE se snímkem
hned a při každé změně (a s událostmi `foreign`), `POST …/hold` a
Expand Down
8 changes: 8 additions & 0 deletions scripts/lazurio-release-contract.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2027,6 +2027,14 @@ NodeTest.test("the panel opens on an agent's tab through its seams", async () =>
NodeAssert.match(host, /Effect\.andThen\(threadTabs\.record\(threadId, targetId, revealed\)\)/);
NodeAssert.match(tabsService, /if \(anothersTab\(stripOf\(threadId\), binding\)\) \{/);
NodeAssert.match(tabsService, /await record\(threadId, binding\.targetId, true\);/);
// Nor while the Platform has not verified the session's binding (C19, LazurioPlatform#989):
// marks listed before and after the read, and a second read that must agree.
NodeAssert.match(tabsService, /const markedBefore = await unverifiedSessions\(\);/);
NodeAssert.match(tabsService, /const markedAfter = await unverifiedSessions\(\);/);
NodeAssert.match(
tabsService,
/if \(marked\(binding\.session\) \|\| readAgain\?\.get\(binding\.session\) !== binding\.targetId\) continue;/,
);
});

// A plain click on a thread's server in upstream's Servers panel (pingdotgg/t3code#22542) opens it in
Expand Down
Loading