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
15 changes: 15 additions & 0 deletions .github/workflows/lazurio-fork-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,21 @@ jobs:
# admits, a copy of APPLICATION_PUBLIC_PATHS in HumanAndMachine-ai/Machines (row 34,
# root decision 0157).
packages/client-runtime/src/lazurio/workspaceGateway.test.ts
# Buddy's project beside the managed Folder (LZD-0033, row 15): the web leaves it and its
# threads out of lists, the archive and pull request rows, not out of point reads, so a
# link to its thread opens. The logic is in the overlay module; the project, thread and
# archive atoms and their web wiring carry one hook each.
packages/client-runtime/src/lazurio/unlistedProjects.ts
packages/client-runtime/src/lazurio/unlistedProjects.test.ts
packages/client-runtime/src/state/projectEntities.ts
packages/client-runtime/src/state/threadShell.ts
packages/client-runtime/src/state/archivedThreads.ts
apps/web/src/state/threads.ts
apps/web/src/lib/archivedThreadsState.ts
apps/web/src/components/pullRequest/PullRequestThreadLinks.tsx
apps/web/src/components/ThreadNotificationCoordinator.tsx
apps/web/src/state/entities.ts
apps/web/src/hooks/useActiveProjectTarget.ts
)
unexpected="$(git diff --name-only "$UPSTREAM_SHA..HEAD" |
grep -E '^(apps|packages|scripts)/' |
Expand Down
2 changes: 2 additions & 0 deletions apps/server/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ export class ServerConfig extends Context.Service<
readonly cwd: string;
/** Opt-in hosted Folder entry contract, declared by Machines. */
readonly lazurioFolder?: string;
/** The Buddy folder beside the Folder, declared with it (`T3CODE_LAZURIO_BUDDY_FOLDER`). */
readonly lazurioBuddyFolder?: string;
readonly lazurioToolsUrl?: string;
readonly baseDir: string;
readonly staticDir: string | undefined;
Expand Down
29 changes: 24 additions & 5 deletions apps/server/src/lazurio/managedFolder.testkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import * as WorkspacePaths from "../workspace/WorkspacePaths.ts";

/**
* A managed Environment on disk: its Folder, a repository inside it, one beside it (an earlier
* project), a symlink to the Folder, and where its database goes.
* project), the Buddy folder beside it and a symlink to it, a symlink to the Folder, and where its
* database goes.
*/
export const makeFolderDirectories = () => {
const parent = NodeFS.realpathSync(
Expand All @@ -29,19 +30,36 @@ export const makeFolderDirectories = () => {
const inside = NodePath.join(folder, "organizations", "example");
const repository = NodePath.join(parent, "repository");
const link = NodePath.join(parent, "link-to-Lazurio");
const buddy = NodePath.join(parent, "Buddy");
const buddyLink = NodePath.join(parent, "link-to-Buddy");
NodeFS.mkdirSync(inside, { recursive: true });
NodeFS.mkdirSync(repository);
NodeFS.mkdirSync(buddy);
NodeFS.symlinkSync(folder, link);
return { parent, folder, inside, repository, link, database: NodePath.join(parent, "db.sqlite") };
NodeFS.symlinkSync(buddy, buddyLink);
return {
parent,
folder,
inside,
repository,
link,
buddy,
buddyLink,
database: NodePath.join(parent, "db.sqlite"),
};
};

/** The test server config, with `lazurioFolder` as `T3CODE_LAZURIO_FOLDER` would set it. */
export const layerManagedConfig = (lazurioFolder?: string) =>
/**
* The test server config, with `lazurioFolder` and `lazurioBuddyFolder` as `T3CODE_LAZURIO_FOLDER`
* and `T3CODE_LAZURIO_BUDDY_FOLDER` would set them.
*/
export const layerManagedConfig = (lazurioFolder?: string, lazurioBuddyFolder?: string) =>
Layer.effect(
ServerConfig.ServerConfig,
Effect.map(ServerConfig.ServerConfig, (config) => ({
...config,
...(lazurioFolder === undefined ? {} : { lazurioFolder }),
...(lazurioBuddyFolder === undefined ? {} : { lazurioBuddyFolder }),
})),
).pipe(Layer.provide(ServerConfig.layerTest(process.cwd(), { prefix: "t3-lazurio-folder-" })));

Expand All @@ -65,13 +83,14 @@ const layerMetadata = Layer.merge(
export const layerProjects = <E, R>(input: {
readonly persistence: Layer.Layer<SqlClient.SqlClient, E, R>;
readonly lazurioFolder?: string;
readonly lazurioBuddyFolder?: string;
}) =>
RuntimeLayer.layerProjectService.pipe(
Layer.provideMerge(ProjectEnrichmentService.layer),
Layer.provideMerge(layerWorkspacePaths),
Layer.provideMerge(layerMetadata),
Layer.provideMerge(input.persistence),
Layer.provideMerge(ServerSettings.layerTest()),
Layer.provideMerge(layerManagedConfig(input.lazurioFolder)),
Layer.provideMerge(layerManagedConfig(input.lazurioFolder, input.lazurioBuddyFolder)),
Layer.provideMerge(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)),
);
36 changes: 27 additions & 9 deletions apps/server/src/lazurio/managedFolder.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
/**
* Lazurio overlay (LazurioPlatform F51): with `T3CODE_LAZURIO_FOLDER` a hosted Environment's chats
* start in its Lazurio Folder. This is an entry-context contract, not a filesystem sandbox: agents
* start at the Folder and can then work in its repositories with their normal tools. Without the
* variable nothing here refuses anything.
* Lazurio overlay (LazurioPlatform F51): with `T3CODE_LAZURIO_FOLDER` chats start in the Lazurio
* Folder, an entry context, not a sandbox; `T3CODE_LAZURIO_BUDDY_FOLDER` adds Buddy's folder beside it
* (LZD-0033). Without the Folder nothing here refuses anything.
*/
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";

import * as ServerConfig from "../config.ts";

/** The name the server's startup gives Buddy's project (managedFolderStartup.ts). */
export const BUDDY_PROJECT_TITLE = "Buddy";

const turnOutsideFolder =
"Agents in this Environment work in its Lazurio Folder. Start a new chat in the Lazurio project.";
const repositoryAtFolder =
Expand All @@ -17,23 +19,30 @@ const repositoryAtFolder =
/**
* The guard of the session manager, the VCS service and the model recommendation route, read once
* from the server config: the Folder, why an agent turn or a new repository is refused in a
* managed Environment, or null, and whether a path names the Folder. A path counts as the Folder
* when it resolves to it, symlinks followed.
* managed Environment, or null, and whether a path names the Folder or the Buddy folder. A path
* counts as one when it resolves to it, symlinks followed.
*/
export const makeFolderGuard = Effect.gen(function* () {
const { lazurioFolder: folder } = yield* ServerConfig.ServerConfig;
const { lazurioFolder: folder, lazurioBuddyFolder } = yield* ServerConfig.ServerConfig;
// Only beside a Folder: the config never sets one alone, and a test's mock answers with a function.
const buddyFolder =
folder !== undefined && typeof lazurioBuddyFolder === "string" ? lazurioBuddyFolder : undefined;
const fs = yield* FileSystem.FileSystem;
/** `path` with its symlinks resolved, or null when it does not resolve. */
const canonical = (path: string) => fs.realPath(path).pipe(Effect.orElseSucceed(() => null));
return {
/** The Folder's canonical path; undefined when the Environment is not managed. */
folder,
/** An agent turn runs only in the Folder itself; anywhere else, or nowhere, is refused. */
/** The Buddy folder's canonical path; undefined unless declared beside the Folder. */
buddyFolder,
/** An agent turn runs only in the Folder or the Buddy folder itself; anywhere else, or nowhere, is refused. */
turnRefusal: (cwd: string | null | undefined): Effect.Effect<string | null> =>
folder === undefined
? Effect.succeed(null)
: (cwd == null ? Effect.succeed(null) : canonical(cwd)).pipe(
Effect.map((path) => (path === folder ? null : turnOutsideFolder)),
Effect.map((path) =>
path === folder || (path !== null && path === buddyFolder) ? null : turnOutsideFolder,
),
),
/** The Folder holds repositories, so it never becomes one; a path that does not resolve is compared as it is. */
repositoryRefusal: (cwd: string): Effect.Effect<string | null> =>
Expand All @@ -47,14 +56,23 @@ export const makeFolderGuard = Effect.gen(function* () {
folder === undefined
? Effect.succeed(false)
: canonical(path).pipe(Effect.map((resolved) => resolved === folder)),
/** Whether `path` is the Buddy folder itself; never without one. */
isBuddyFolder: (path: string): Effect.Effect<boolean> =>
buddyFolder === undefined
? Effect.succeed(false)
: canonical(path).pipe(Effect.map((resolved) => resolved === buddyFolder)),
};
});

/** The managed Folder in the config the server gives its clients (`server.getConfig`), if any. */
export const managedFolderClientConfig = (config: {
readonly lazurioFolder?: string;
readonly lazurioBuddyFolder?: string;
readonly lazurioToolsUrl?: string;
}) => ({
...(config.lazurioFolder === undefined ? {} : { lazurioFolder: config.lazurioFolder }),
...(config.lazurioFolder === undefined || config.lazurioBuddyFolder === undefined
? {}
: { lazurioBuddyFolder: config.lazurioBuddyFolder }),
...(config.lazurioToolsUrl === undefined ? {} : { lazurioToolsUrl: config.lazurioToolsUrl }),
});
71 changes: 69 additions & 2 deletions apps/server/src/lazurio/managedFolderConfig.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ const makeFolder = () => {
NodeFS.mkdirSync(folder);
const link = NodePath.join(parent, "link-to-Lazurio");
NodeFS.symlinkSync(folder, link);
return { parent, folder, link };
const buddy = NodePath.join(parent, "Buddy");
NodeFS.mkdirSync(buddy);
const buddyLink = NodePath.join(parent, "link-to-Buddy");
NodeFS.symlinkSync(buddy, buddyLink);
return { parent, folder, link, buddy, buddyLink };
};

it.effect(
Expand Down Expand Up @@ -62,14 +66,77 @@ it.effect("makes the Folder's canonical path the cwd, without a cwd bootstrap",
}),
);

it.effect("declares the Buddy folder beside the Folder by its canonical path", () =>
Effect.gen(function* () {
const { folder, buddy, buddyLink } = makeFolder();
assert.deepStrictEqual(
yield* resolveManagedFolderConfig.pipe(
withEnv({ T3CODE_LAZURIO_FOLDER: folder, T3CODE_LAZURIO_BUDDY_FOLDER: buddyLink }),
),
{
cwd: folder,
lazurioFolder: folder,
autoBootstrapProjectFromCwd: false,
lazurioBuddyFolder: buddy,
},
);
// Missing (not made yet, or removed): Chat still starts and the path stays, so an earlier
// Buddy project stays unlisted. A sibling whose name starts with two dots is still beside.
// A link whose target was removed still names the target, the root its project was made with.
const target = NodePath.join(NodePath.dirname(folder), "Buddy-target");
const dangling = NodePath.join(NodePath.dirname(folder), "Buddy-link");
NodeFS.symlinkSync(target, dangling);
assert.strictEqual(
(yield* resolveManagedFolderConfig.pipe(
withEnv({ T3CODE_LAZURIO_FOLDER: folder, T3CODE_LAZURIO_BUDDY_FOLDER: dangling }),
)).lazurioBuddyFolder,
target,
);
const dotsSibling = NodePath.join(NodePath.dirname(folder), "..Buddy");
NodeFS.mkdirSync(dotsSibling);
for (const declared of [`${buddy}-missing`, dotsSibling]) {
assert.deepStrictEqual(
yield* resolveManagedFolderConfig.pipe(
withEnv({ T3CODE_LAZURIO_FOLDER: folder, T3CODE_LAZURIO_BUDDY_FOLDER: declared }),
),
{
cwd: folder,
lazurioFolder: folder,
autoBootstrapProjectFromCwd: false,
lazurioBuddyFolder: declared,
},
);
}
}),
);

it.effect("refuses a relative path, a file, an insecure or credentialed tools URL", () =>
Effect.gen(function* () {
const { parent, folder } = makeFolder();
const { parent, folder, buddy } = makeFolder();
const file = NodePath.join(parent, "not-a-folder");
NodeFS.writeFileSync(file, "");
const insideFolder = NodePath.join(folder, ".agents", "buddy");
NodeFS.mkdirSync(insideFolder, { recursive: true });
const dotsInsideFolder = NodePath.join(folder, "..buddy");
NodeFS.mkdirSync(dotsInsideFolder);
NodeFS.mkdirSync(NodePath.join(folder, "..x"));
const beside = "T3CODE_LAZURIO_BUDDY_FOLDER must be beside the Lazurio Folder";
for (const [env, expected] of [
[{ T3CODE_LAZURIO_FOLDER: "Lazurio" }, "T3CODE_LAZURIO_FOLDER must be an absolute"],
[{ T3CODE_LAZURIO_FOLDER: file }, "T3CODE_LAZURIO_FOLDER must be a directory"],
[
{ T3CODE_LAZURIO_BUDDY_FOLDER: buddy },
"T3CODE_LAZURIO_BUDDY_FOLDER needs T3CODE_LAZURIO_FOLDER",
],
[
{ T3CODE_LAZURIO_FOLDER: folder, T3CODE_LAZURIO_BUDDY_FOLDER: "Buddy" },
"T3CODE_LAZURIO_BUDDY_FOLDER must be an absolute",
],
[{ T3CODE_LAZURIO_FOLDER: folder, T3CODE_LAZURIO_BUDDY_FOLDER: folder }, beside],
[{ T3CODE_LAZURIO_FOLDER: folder, T3CODE_LAZURIO_BUDDY_FOLDER: insideFolder }, beside],
[{ T3CODE_LAZURIO_FOLDER: folder, T3CODE_LAZURIO_BUDDY_FOLDER: dotsInsideFolder }, beside],
[{ T3CODE_LAZURIO_FOLDER: `${folder}/..x`, T3CODE_LAZURIO_BUDDY_FOLDER: folder }, beside],
[{ T3CODE_LAZURIO_FOLDER: folder, T3CODE_LAZURIO_BUDDY_FOLDER: parent }, beside],
[
{ T3CODE_LAZURIO_FOLDER: folder, T3CODE_LAZURIO_TOOLS_URL: "http://launchpad.test/" },
"T3CODE_LAZURIO_TOOLS_URL must be an HTTPS URL",
Expand Down
88 changes: 80 additions & 8 deletions apps/server/src/lazurio/managedFolderConfig.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/**
* Lazurio overlay (LazurioPlatform F51): `T3CODE_LAZURIO_FOLDER`, an absolute existing directory
* whose canonical path becomes the cwd and `lazurioFolder` (no project bootstraps from the cwd), and
* `T3CODE_LAZURIO_TOOLS_URL`, an HTTPS page without credentials for connecting agent accounts.
* Unset, both leave upstream's config as it is.
* Lazurio overlay (LazurioPlatform F51): `T3CODE_LAZURIO_FOLDER` (its canonical path becomes the cwd
* and `lazurioFolder`), `T3CODE_LAZURIO_BUDDY_FOLDER` (Buddy's folder beside it, LZD-0033) and
* `T3CODE_LAZURIO_TOOLS_URL` (HTTPS, no credentials). Unset, each leaves upstream's config alone.
*/
import * as Config from "effect/Config";
import * as Effect from "effect/Effect";
Expand All @@ -22,6 +21,10 @@ const ManagedFolderEnv = Config.all({
Config.option,
Config.map(Option.getOrUndefined),
),
buddyFolder: Config.String("T3CODE_LAZURIO_BUDDY_FOLDER").pipe(
Config.option,
Config.map(Option.getOrUndefined),
),
toolsUrl: Config.URL("T3CODE_LAZURIO_TOOLS_URL").pipe(
Config.option,
Config.map(Option.getOrUndefined),
Expand All @@ -30,22 +33,90 @@ const ManagedFolderEnv = Config.all({

/** The declared Folder's canonical path: absolute, existing, a directory, symlinks resolved. */
const canonicalFolder = Effect.fn("LazurioManagedFolder.canonicalFolder")(function* (
variable: "T3CODE_LAZURIO_FOLDER" | "T3CODE_LAZURIO_BUDDY_FOLDER",
declared: string,
) {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
if (!path.isAbsolute(declared))
return yield* invalid("T3CODE_LAZURIO_FOLDER must be an absolute existing directory.");
return yield* invalid(`${variable} must be an absolute existing directory.`);
const root = yield* fs.realPath(declared);
const info = yield* fs.stat(root);
if (info.type !== "Directory")
return yield* invalid("T3CODE_LAZURIO_FOLDER must be a directory.");
if (info.type !== "Directory") return yield* invalid(`${variable} must be a directory.`);
return root;
});

/**
* Where a missing path would be once made: links followed, also dangling ones, and the deepest
* existing ancestor resolved, so a project created through a link keeps its root after a restart.
*/
const canonicalMissing = Effect.fn("LazurioManagedFolder.canonicalMissing")(function* (
declared: string,
) {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
let current = path.resolve(declared);
const rest: Array<string> = [];
for (let hop = 0; hop < 40; hop++) {
const target = yield* fs.readLink(current).pipe(Effect.option);
if (Option.isSome(target)) {
current = path.resolve(path.dirname(current), target.value);
continue;
}
if (yield* fs.exists(current).pipe(Effect.orElseSucceed(() => false)))
return path.join(yield* fs.realPath(current), ...rest);
const parent = path.dirname(current);
if (parent === current) break;
rest.unshift(path.basename(current));
current = parent;
}
return path.resolve(declared);
});

/** Whether `inner` is `outer` or lies under it, compared by whole path components. */
const within = (path: Path.Path, outer: string, inner: string) => {
const relative = path.relative(outer, inner);
return (
relative === "" ||
(relative !== ".." && !relative.startsWith(`..${path.sep}`) && !path.isAbsolute(relative))
);
};

/**
* The Buddy folder's canonical path, beside the Folder (not inside or around it), so its agents
* load its own instructions. A missing folder keeps its declared path, so Chat runs and an earlier
* Buddy project stays unlisted; nothing resolves to it, so no turn runs there.
*/
const canonicalBuddyFolder = Effect.fn("LazurioManagedFolder.canonicalBuddyFolder")(function* (
folder: string | undefined,
declared: string,
) {
if (folder === undefined)
return yield* invalid("T3CODE_LAZURIO_BUDDY_FOLDER needs T3CODE_LAZURIO_FOLDER.");
const path = yield* Path.Path;
const fs = yield* FileSystem.FileSystem;
const root =
path.isAbsolute(declared) &&
!(yield* fs.exists(declared).pipe(Effect.orElseSucceed(() => false)))
? yield* canonicalMissing(declared)
: yield* canonicalFolder("T3CODE_LAZURIO_BUDDY_FOLDER", declared);
if (within(path, folder, root) || within(path, root, folder))
return yield* invalid(
"T3CODE_LAZURIO_BUDDY_FOLDER must be beside the Lazurio Folder, not inside it or around it.",
);
return root;
});

export const resolveManagedFolderConfig = Effect.gen(function* () {
const env = yield* ManagedFolderEnv;
const folder = env.folder === undefined ? undefined : yield* canonicalFolder(env.folder);
const folder =
env.folder === undefined
? undefined
: yield* canonicalFolder("T3CODE_LAZURIO_FOLDER", env.folder);
const buddyFolder =
env.buddyFolder === undefined
? undefined
: yield* canonicalBuddyFolder(folder, env.buddyFolder);
if (
env.toolsUrl !== undefined &&
(env.toolsUrl.protocol !== "https:" || env.toolsUrl.username || env.toolsUrl.password)
Expand All @@ -55,6 +126,7 @@ export const resolveManagedFolderConfig = Effect.gen(function* () {
...(folder === undefined
? {}
: { cwd: folder, lazurioFolder: folder, autoBootstrapProjectFromCwd: false }),
...(buddyFolder === undefined ? {} : { lazurioBuddyFolder: buddyFolder }),
...(env.toolsUrl === undefined ? {} : { lazurioToolsUrl: env.toolsUrl.toString() }),
};
});
Loading
Loading