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
24 changes: 21 additions & 3 deletions apps/server/src/provider/Drivers/ClaudeDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ import {
mergeClaudeProcessEnvironment,
} from "./ClaudeHome.ts";
import * as HostProcess from "@t3tools/shared/HostProcess";
import { mergeProviderInstanceEnvironment } from "@t3tools/provider-core/server/instanceEnvironment";
import { makeClaudeMaintenanceResolution } from "./ClaudeMaintenance.ts";
const decodeClaudeSettings = Schema.decodeSync(ClaudeSettings);

Expand Down Expand Up @@ -132,7 +133,19 @@ export const ClaudeDriver: ProviderDriver<
enabled,
binaryPath: expandHomePath(config.binaryPath, yield* HostProcess.HomeDirectory),
},
environment: yield* mergeClaudeProcessEnvironment(config, environment, process.env),
environment: yield* mergeClaudeProcessEnvironment(
config,
environment,
yield* HostProcess.Environment,
),
...(config.setupMode === "inference"
? {
nativeEnvironment: yield* mergeProviderInstanceEnvironment(
environment,
yield* HostProcess.Environment,
),
}
: {}),
stateDir,
});
const { config: effectiveConfig, environment: processEnv, configDir } = runtime;
Expand Down Expand Up @@ -519,7 +532,8 @@ export const ClaudeDriver: ProviderDriver<
return {
instanceId,
driverKind: DRIVER_KIND,
...(runtime.overlay?.active === false
...(runtime.overlay?.active === false ||
(config.setupMode === "inference" && !config.shareProfile)
? {}
: { sharedSkillsDirectory: path.join(runtime.overlay?.sharedRoot ?? configDir, "skills") }),
continuationIdentity: {
Expand All @@ -535,7 +549,11 @@ export const ClaudeDriver: ProviderDriver<
invalidateCaches: Cache.invalidateAll(capabilitiesProbeCache),
workspaceRescans: Stream.fromQueue(workspaceRescans),
snapshotForCwd: (cwd: string) =>
snapshot.getSnapshot.pipe(
(runtime.nativeSkillsHome === undefined
? Effect.void
: materializeClaudeHome(runtime).pipe(Effect.ignoreCause({ log: true }))
).pipe(
Effect.andThen(snapshot.getSnapshot),
Effect.flatMap((machineSnapshot) =>
withAccess(
probeClaudeWorkspaceSnapshot(effectiveConfig, machineSnapshot, cwd, processEnv),
Expand Down
80 changes: 80 additions & 0 deletions apps/server/src/provider/Drivers/ClaudeHome.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import * as Path from "effect/Path";
import * as Schema from "effect/Schema";
import { ClaudeSettings, ProviderInstanceId } from "@t3tools/contracts";
import { symlinksSupported } from "@t3tools/shared/testing/symlinks";
import * as HostProcess from "@t3tools/shared/HostProcess";

import {
makeClaudeCapabilitiesCacheKey,
Expand All @@ -19,6 +20,7 @@ import {
mergeClaudeProcessEnvironment,
resolveClaudeHomePath,
} from "./ClaudeHome.ts";
import { discoverClaudeSkills } from "./ClaudeSkills.ts";

const decodeClaudeSettings = Schema.decodeSync(ClaudeSettings);

Expand Down Expand Up @@ -156,6 +158,84 @@ it.layer(NodeServices.layer)("ClaudeHome", (it) => {
);
}

it.effect.skipIf(!symlinksSupported)("retains personal skills in an inference home", () =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-claude-inference-" });
const personal = path.join(root, "personal");
const routed = path.join(root, "routed");
const skill = (home: string, name: string) => path.join(home, "skills", name, "SKILL.md");
for (const [home, name, body] of [
[personal, "review", "Personal review"],
[personal, "existing", "Personal copy"],
[routed, "existing", "Routed copy"],
] as const) {
yield* fs.makeDirectory(path.dirname(skill(home, name)), { recursive: true });
yield* fs.writeFileString(skill(home, name), body);
}
yield* fs.writeFileString(path.join(personal, "settings.json"), "{}");

const runtime = yield* resolveClaudeRuntime({
instanceId: ProviderInstanceId.make("claudeAgent"),
stateDir: root,
config: decodeClaudeSettings({ setupMode: "inference", homePath: routed }),
environment: { CLAUDE_CONFIG_DIR: routed },
nativeEnvironment: { CLAUDE_CONFIG_DIR: personal },
}).pipe(Effect.provideService(HostProcess.Environment, {}));
yield* materializeClaudeHome(runtime);
expect(yield* fs.readFileString(skill(routed, "existing"))).toBe("Routed copy");
expect(yield* fs.exists(path.join(routed, "settings.json"))).toBe(false);
expect(
(yield* discoverClaudeSkills(runtime.config, undefined, runtime.environment)).map(
({ name }) => name,
),
).toEqual(["existing", "review"]);
expect(yield* fs.readFileString(skill(routed, "review"))).toBe("Personal review");
yield* fs.symlink(
path.dirname(skill(personal, "existing")),
path.dirname(skill(routed, "manual")),
);
const alternate = path.join(root, "alternate");
yield* fs.makeDirectory(path.dirname(skill(alternate, "review")), { recursive: true });
yield* fs.writeFileString(skill(alternate, "review"), "Alternate review");
const switched = yield* resolveClaudeRuntime({
instanceId: ProviderInstanceId.make("claudeAgent"),
stateDir: root,
config: decodeClaudeSettings({
setupMode: "inference",
homePath: routed,
inferenceSkillsHomePath: alternate,
}),
environment: { CLAUDE_CONFIG_DIR: routed },
});
yield* materializeClaudeHome(switched);
expect(yield* fs.readFileString(skill(routed, "review"))).toBe("Alternate review");
yield* fs.remove(path.dirname(skill(alternate, "review")), { recursive: true });
yield* materializeClaudeHome(switched);
expect((yield* fs.readDirectory(path.join(routed, "skills"))).sort()).toEqual([
"existing",
"manual",
]);
expect(yield* fs.readFileString(skill(routed, "manual"))).toBe("Personal copy");
yield* materializeClaudeHome(runtime);
const privateRuntime = yield* resolveClaudeRuntime({
instanceId: ProviderInstanceId.make("claudeAgent"),
stateDir: root,
config: decodeClaudeSettings({
setupMode: "inference",
homePath: routed,
shareProfile: false,
}),
environment: { CLAUDE_CONFIG_DIR: routed },
nativeEnvironment: { CLAUDE_CONFIG_DIR: personal },
});
yield* materializeClaudeHome(privateRuntime);
expect(yield* fs.exists(skill(routed, "review"))).toBe(false);
expect(yield* fs.readFileString(skill(routed, "manual"))).toBe("Personal copy");
}),
);

it.effect("separates capability probes by cwd", () =>
Effect.gen(function* () {
const config = { binaryPath: "claude", homePath: "" };
Expand Down
17 changes: 17 additions & 0 deletions apps/server/src/provider/Drivers/ClaudeHome.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { expandHomePath } from "@t3tools/provider-core/server/pathExpansion";
import * as HostProcess from "@t3tools/shared/HostProcess";

import { readClaudeSettingsEnv } from "./ClaudeSkills.ts";
import { linkInferenceSkills } from "./inferenceSkills.ts";

const quotePath = Schema.encodeSync(Schema.fromJsonString(Schema.String));

Expand Down Expand Up @@ -154,12 +155,14 @@ export type ClaudeRuntime = {
readonly config: ClaudeSettings;
readonly configDir: string;
readonly overlay: undefined;
readonly nativeSkillsHome?: string;
}
| {
readonly mode: "cli";
readonly config: ClaudeSettings & { readonly homePath: ClaudeAccountDir };
readonly configDir: ClaudeAccountDir;
readonly overlay: HomeOverlay;
readonly nativeSkillsHome?: never;
}
);

Expand All @@ -168,6 +171,7 @@ export const resolveClaudeRuntime = Effect.fn("resolveClaudeRuntime")(function*
readonly config: ClaudeSettings;
readonly environment: NodeJS.ProcessEnv;
readonly stateDir: string;
readonly nativeEnvironment?: NodeJS.ProcessEnv;
}): Effect.fn.Return<ClaudeRuntime, never, Path.Path> {
if (input.config.setupMode !== "cli") {
return {
Expand All @@ -176,6 +180,14 @@ export const resolveClaudeRuntime = Effect.fn("resolveClaudeRuntime")(function*
configDir: yield* resolveClaudeHomePath(input.config, input.environment),
environment: yield* makeClaudeEnvironment(input.config, input.environment),
overlay: undefined,
...(input.config.setupMode === "inference" && input.config.shareProfile
? {
nativeSkillsHome: yield* resolveClaudeHomePath(
{ homePath: input.config.inferenceSkillsHomePath ?? "" },
input.nativeEnvironment ?? (yield* HostProcess.Environment),
),
}
: {}),
};
}
const path = yield* Path.Path;
Expand All @@ -199,6 +211,11 @@ export const resolveClaudeRuntime = Effect.fn("resolveClaudeRuntime")(function*
export const materializeClaudeHome = Effect.fn("materializeClaudeHome")(function* (
runtime: ClaudeRuntime,
) {
if (runtime.mode === "existing" && runtime.config.setupMode === "inference") {
yield* linkInferenceSkills(runtime.nativeSkillsHome, runtime.configDir, "synced").pipe(
Effect.ignoreCause({ log: true }),
);
}
if (!runtime.overlay) return;
const fileSystem = yield* FileSystem.FileSystem;
yield* fileSystem.makeDirectory(runtime.configDir, { recursive: true, mode: 0o700 });
Expand Down
29 changes: 29 additions & 0 deletions apps/server/src/provider/Drivers/CodexDriver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,35 @@ it.layer(layerTest)("CodexDriver", (it) => {
}).pipe(Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, noSpawn)),
);

it.effect.skipIf(windowsHost)(
"keeps personal skills in an inference home without sharing auth",
() =>
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-codex-inference-" });
const native = NodePath.join(root, "native");
const routed = NodePath.join(root, "routed");
const skill = (home: string, name: string) =>
NodePath.join(home, "skills", name, "SKILL.md");
for (const name of ["review", ".system"]) {
yield* fs.makeDirectory(NodePath.dirname(skill(native, name)), { recursive: true });
yield* fs.writeFileString(skill(native, name), name);
}
yield* fs.writeFileString(NodePath.join(native, "auth.json"), "{}");
const instance = yield* CodexDriver.create({
instanceId: ProviderInstanceId.make("codex-inference"),
displayName: "Codex Inference",
enabled: false,
environment: [{ name: "CODEX_HOME", value: native, sensitive: false }],
config: { ...CodexDriver.defaultConfig(), homePath: routed, setupMode: "inference" },
});
expect(instance.continuationIdentity.continuationKey).toBe(`codex:home:${routed}`);
expect(yield* fs.readFileString(skill(routed, "review"))).toBe("review");
expect(yield* fs.exists(skill(routed, ".system"))).toBe(false);
expect(yield* fs.exists(NodePath.join(routed, "auth.json"))).toBe(false);
}).pipe(Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, noSpawn)),
);

it.effect.each(["inference", "existing"] as const)(
"publishes models with the right provenance in %s mode",
(setupMode) =>
Expand Down
25 changes: 24 additions & 1 deletion apps/server/src/provider/Drivers/CodexDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,12 @@ import {
codexContinuationIdentity,
codexHomeLaunchArgs,
materializeCodexShadowHome,
resolveCodexHomeLayout,
} from "./CodexHomeLayout.ts";
import { resolveCodexRuntime } from "./CodexRuntime.ts";
import { makeManagedCodexProvider } from "./CodexManagedProvider.ts";
import { codexUsageReader } from "./codexUsage.ts";
import { linkInferenceSkills } from "./inferenceSkills.ts";
import * as CodexInstallation from "../CodexInstallation.ts";
import * as CodexManagedTokenBroker from "../CodexManagedTokenBroker.ts";
import * as ServerSecretStore from "../../auth/ServerSecretStore.ts";
Expand Down Expand Up @@ -225,6 +227,26 @@ export const CodexDriver: ProviderDriver<
});
const { homeLayout, environment: processEnv } = runtime;
const accountKey = homeLayout.effectiveHomePath ?? homeLayout.sharedHomePath;
const nativeHome =
config.setupMode === "inference"
? (yield* resolveCodexHomeLayout(
{
...config,
homePath: config.inferenceSkillsHomePath ?? "",
shadowHomePath: "",
},
instanceEnvironment,
)).sharedHomePath
: undefined;
const syncInferenceSkills =
nativeHome === undefined
? Effect.void
: linkInferenceSkills(nativeHome, accountKey, ".system").pipe(
Effect.provideService(FileSystem.FileSystem, fileSystem),
Effect.provideService(Path.Path, pathService),
Effect.ignoreCause({ log: true }),
);
yield* syncInferenceSkills;
const runtimePaths = {
homePath: homeLayout.sharedHomePath,
shadowHomePath: accountKey,
Expand Down Expand Up @@ -506,7 +528,8 @@ export const CodexDriver: ProviderDriver<
: Effect.all([
snapshot.getSnapshot,
withAccess(
resolveRuntime.pipe(
syncInferenceSkills.pipe(
Effect.andThen(resolveRuntime),
Effect.flatMap((runtime) =>
probeSkills({
binaryPath: runtime.config.binaryPath,
Expand Down
72 changes: 72 additions & 0 deletions apps/server/src/provider/Drivers/inferenceSkills.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Path from "effect/Path";

export const linkInferenceSkills = Effect.fn("linkInferenceSkills")(function* (
nativeHome: string | undefined,
inferenceHome: string,
privateEntry: string,
) {
if (nativeHome === inferenceHome) return;
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const source = nativeHome === undefined ? undefined : path.join(nativeHome, "skills");
const destination = path.join(inferenceHome, "skills");
const anchor = path.join(inferenceHome, ".t3-native-skills");
const linkTarget = (entry: string) =>
fs.readLink(entry).pipe(Effect.orElseSucceed(() => undefined));
if ((yield* linkTarget(destination)) !== undefined) return;
const readNames = (directory: string) =>
fs
.readDirectory(directory)
.pipe(Effect.catchReason("PlatformError", "NotFound", () => Effect.succeed([])));
const names = new Set(
source === undefined
? []
: yield* Effect.filter(
(yield* readNames(source)).filter(
(name) => !name.startsWith(".") && name !== privateEntry,
),
(name) => fs.exists(path.join(source, name, "SKILL.md")),
),
);
const previous = yield* linkTarget(anchor);
if (!names.size && previous === undefined) return;
const existing = new Set(yield* readNames(destination));
for (const name of existing) {
if (names.has(name)) continue;
const entry = path.join(destination, name);
if ((yield* linkTarget(entry)) === path.join(anchor, name)) {
yield* fs.remove(entry, { force: true });
}
}
if (!names.size || source === undefined) {
if (previous !== undefined) yield* fs.remove(anchor, { force: true });
return;
}
yield* fs.makeDirectory(destination, { recursive: true, mode: 0o700 });
if (previous === undefined) {
yield* fs
.symlink(source, anchor)
.pipe(Effect.catchReason("PlatformError", "AlreadyExists", () => Effect.void));
if ((yield* linkTarget(anchor)) !== source) return;
} else if (previous !== source) {
yield* Effect.scoped(
Effect.gen(function* () {
const stage = yield* fs.makeTempDirectoryScoped({
directory: inferenceHome,
prefix: ".t3-skills-",
});
const replacement = path.join(stage, "source");
yield* fs.symlink(source, replacement);
yield* fs.rename(replacement, anchor);
}),
);
}
for (const name of names) {
if (existing.has(name)) continue;
yield* fs
.symlink(path.join(anchor, name), path.join(destination, name))
.pipe(Effect.catchReason("PlatformError", "AlreadyExists", () => Effect.void));
}
});
Loading
Loading