Skip to content
10 changes: 10 additions & 0 deletions .github/fork/carried-prs.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@
"number": 19273,
"takenSha": "b8d5c37cf59f7989edaf14dea2822f28ae841fdb",
"reason": "Local-model composer dictation"
},
{
"number": 28719,
"takenSha": "8dc3e6ff446dc1f1a714288091249ee86c91735b",
"reason": "Desktop cache miss lets the server hydrate PATH before provider checks"
},
{
"number": 31169,
"takenSha": "afede16a7a38b21be21ee696a1c3027f6f6b73e7",
"reason": "Providers re-check when a shell PATH arrives after startup; fork also re-checks failed checks"
}
]
}
6 changes: 5 additions & 1 deletion apps/desktop/src/shell/DesktopShellEnvironment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ describe("DesktopShellEnvironment", () => {
const startup = yield* service.installIntoProcess.pipe(Effect.forkChild);
yield* Deferred.await(started);
assert.isDefined(startup.pollUnsafe());
assert.isFalse(
yield* Fiber.join(startup),
"nothing was installed yet, so the server must hydrate PATH itself",
);
yield* Deferred.succeed(release, undefined);
return yield* service.awaitDiscovery;
}),
Expand Down Expand Up @@ -249,7 +253,7 @@ describe("DesktopShellEnvironment", () => {
}),
use: (service) =>
Effect.gen(function* () {
yield* service.installIntoProcess;
assert.isTrue(yield* service.installIntoProcess);
yield* service.installIntoProcess;
assert.equal(env.PATH, "/old/bin:/usr/bin:/launcher/bin");
assert.equal(env.SSH_AUTH_SOCK, "/current-session.sock");
Expand Down
8 changes: 5 additions & 3 deletions apps/desktop/src/shell/DesktopShellEnvironment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ export class DesktopShellEnvironmentCommandTimeoutError extends Schema.TaggedErr
export class DesktopShellEnvironment extends Context.Service<
DesktopShellEnvironment,
{
// Desktop owns discovery, including on cache misses; the backend receives updates over fd4.
// Yields true only when a cached environment was applied; on a miss the backend hydrates
// PATH itself until desktop discovery arrives over fd4.
readonly installIntoProcess: Effect.Effect<boolean>;
readonly awaitDiscovery: Effect.Effect<boolean>;
readonly updates: Stream.Stream<DesktopShellEnvironmentSnapshot>;
Expand Down Expand Up @@ -628,7 +629,8 @@ export const make = Effect.gen(function* () {
const cache = yield* fileSystem
.readFileString(cachePath)
.pipe(Effect.flatMap(decodeCache), Effect.option);
if (Option.isSome(cache) && cache.value.key === key) {
const hit = Option.isSome(cache) && cache.value.key === key;
if (hit) {
const cached = Object.fromEntries(
CACHED_ENV_NAMES.flatMap((name) => {
const value = cache.value.environment[name];
Expand All @@ -654,7 +656,7 @@ export const make = Effect.gen(function* () {
);
}
yield* Effect.forkIn(refresh, scope);
return true;
return hit;
}).pipe(Effect.withSpan("desktop.shellEnvironment.installIntoProcess")),
);
const updates = Stream.unwrap(
Expand Down
10 changes: 9 additions & 1 deletion apps/server/src/policy/ManagedPolicy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,15 @@ it.layer(NodeServices.layer)("ManagedPolicy.load", (it) => {
]);
// The host variable that replaces Codex's launch arguments is ignored too.
const host = { PATH: "/usr/bin", T3CODE_CODEX_LAUNCH_ARGS: "-c mcp_servers.x.command=sh" };
assert.deepEqual(providerHostEnvironment(policy.summary, host), { PATH: "/usr/bin" });
const view = providerHostEnvironment(policy.summary, host);
assert.deepEqual({ ...view }, { PATH: "/usr/bin" });
host.PATH = "/late/bin";
Object.assign(host, { NEW_VARIABLE: "late", t3code_codex_launch_args: "hidden" });
assert.deepEqual({ ...view }, { PATH: "/late/bin", NEW_VARIABLE: "late" });
assert.isUndefined(view.T3CODE_CODEX_LAUNCH_ARGS);
assert.isFalse("t3code_codex_launch_args" in view);
assert.isFalse(Reflect.set(view, "PATH", "/replaced"));
assert.equal(host.PATH, "/late/bin");
assert.strictEqual(providerHostEnvironment(undefined, host), host);
}),
);
Expand Down
30 changes: 23 additions & 7 deletions apps/server/src/policy/managedProviders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,29 @@ export const providerHostEnvironment = (
environment: NodeJS.ProcessEnv,
): NodeJS.ProcessEnv => {
if (summary === undefined) return environment;
const next = { ...environment };
for (const name of Object.keys(next)) {
if (LAUNCH_OVERRIDE_VARIABLES.some((override) => override === name.toUpperCase())) {
delete next[name];
}
}
return next;
const allowed = (name: string | symbol) =>
typeof name !== "string" ||
!LAUNCH_OVERRIDE_VARIABLES.some((override) => override === name.toUpperCase());
return new Proxy(
{},
{
get: (_, name) => (allowed(name) ? Reflect.get(environment, name) : undefined),
has: (_, name) => allowed(name) && Reflect.has(environment, name),
ownKeys: () => Reflect.ownKeys(environment).filter(allowed),
getOwnPropertyDescriptor: (_, name) =>
allowed(name) && Reflect.has(environment, name)
? {
configurable: true,
enumerable: true,
value: Reflect.get(environment, name),
writable: false,
}
: undefined,
set: () => false,
deleteProperty: () => false,
defineProperty: () => false,
},
);
};

/** Each driver's home variable, and the settings that rewrite it at launch. */
Expand Down
18 changes: 18 additions & 0 deletions apps/server/src/provider/ProviderHostPathWatcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as Stream from "effect/Stream";

import * as DesktopTelemetryReceiver from "../resourceTelemetry/DesktopTelemetryReceiver.ts";
import * as ProviderRegistry from "./ProviderRegistry.ts";

/** The desktop's shell PATH can arrive after drivers were built and checked against a bare one. */
export const layer = Layer.effectDiscard(
Effect.gen(function* () {
const receiver = yield* DesktopTelemetryReceiver.DesktopTelemetryReceiver;
const providers = yield* ProviderRegistry.ProviderRegistry;
yield* receiver.pathChanges.pipe(
Stream.runForEach(() => providers.rebuildMissingAndRefresh),
Effect.forkScoped,
);
}),
);
68 changes: 68 additions & 0 deletions apps/server/src/provider/ProviderInstanceRegistry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import * as FileSystem from "effect/FileSystem";
import * as Layer from "effect/Layer";
import * as Path from "effect/Path";
import * as PubSub from "effect/PubSub";
import * as Queue from "effect/Queue";
import * as Schema from "effect/Schema";
import * as TestClock from "effect/testing/TestClock";
import { HttpClient, HttpClientResponse } from "effect/http";
Expand Down Expand Up @@ -945,6 +946,73 @@ describe("ProviderInstanceRegistry secret environment", () => {
}).pipe(Effect.scoped);
});

it.effect("does not rebuild an instance replaced after selection", () => {
const fixture = setup({ [secret.reference]: "sk-live" });
return Effect.gen(function* () {
const { registry, mutator } = yield* fixture.make;
const selected = yield* registry.listInstances;
fixture.vault.set(secret.reference, "sk-rotated");
yield* mutator.reloadSecrets();
const replacement = yield* registry.getInstance(instanceId);
expect(replacement).not.toBe(selected[0]);
yield* mutator.rebuild(selected);
expect(yield* registry.getInstance(instanceId)).toBe(replacement);
expect(fixture.created).toHaveLength(2);
yield* mutator.rebuild([replacement!]);
expect(fixture.created).toHaveLength(3);
}).pipe(Effect.scoped);
});

it.effect("keeps a rebuilt instance live while another instance's secret read waits", () => {
const plainId = ProviderInstanceId.make("fake_plain");
const created: Array<ProviderInstanceId> = [];
return Effect.gen(function* () {
const gate = yield* Deferred.make<void>();
const readStarts = yield* Queue.unbounded<void>();
const driver: AnyProviderDriver = {
driverKind,
metadata: { displayName: "Fake" },
configSchema: Schema.Unknown,
defaultConfig: () => ({}),
create: (input) =>
Effect.sync(() => {
created.push(input.instanceId);
return { instanceId: input.instanceId, enabled: input.enabled } as ProviderInstance;
}),
};
const resolver = Layer.succeed(SecretResolver.SecretResolver, {
read: (references) =>
Queue.offer(readStarts, undefined).pipe(
Effect.andThen(Deferred.await(gate)),
Effect.as(references.map(() => "sk-live")),
),
invalidate: () => Effect.void,
});
const { registry, mutator } = yield* makeProviderInstanceRegistry({
drivers: [driver],
configMap: {
[instanceId]: {
driver: driverKind,
config: {},
environment: [{ name: "OPENAI_API_KEY", value: secret, sensitive: true }],
},
[plainId]: { driver: driverKind, config: {} },
},
}).pipe(Effect.provide(resolver));
const plain = yield* registry.getInstance(plainId);
// Startup's read and the rebuild's read both wait on the vault.
const rebuilding = yield* mutator.rebuild([plain!]).pipe(Effect.forkScoped);
yield* Queue.take(readStarts);
yield* Queue.take(readStarts);
expect(yield* registry.getInstance(plainId)).toBe(plain);

yield* Deferred.succeed(gate, undefined);
yield* Fiber.join(rebuilding);
expect(yield* registry.getInstance(plainId)).not.toBe(plain);
expect(created.filter((id) => id === plainId)).toHaveLength(2);
}).pipe(Effect.scoped);
});

it.effect("reads a config field typed SettingsSecretValue and rebuilds when it rotates", () => {
const apiKey: SecretReference = { ...secret, reference: "op://Private/Gateway/key" };
const fixture = setup(
Expand Down
38 changes: 38 additions & 0 deletions apps/server/src/provider/ProviderInstanceRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,43 @@ export const makeProviderInstanceRegistry = Effect.fn(
Effect.provideContext(driverContext),
);

const rebuild: ProviderInstanceRegistryMutator.ProviderInstanceRegistryMutatorShape["rebuild"] =
Effect.fn("ProviderInstanceRegistry.rebuild")(function* (instances) {
// Reconcile also builds every pending instance, so read the secrets it needs first and
// outside the lock, as startup does; a vault prompt then holds neither the lock nor the
// selected instances, which stay live until the cached read lets them rebuild at once.
const configMap = yield* Ref.get(configMapRef);
const live = yield* Ref.get(entries);
const selected = new Set(instances.map((instance) => instance.instanceId));
const references = yield* Effect.filter(
Object.entries(configMap),
([rawInstanceId, entry]) => {
const id = ProviderInstanceId.make(rawInstanceId);
return (selected.has(id) || !live.has(id)) &&
secretReferencesOf(driversById, entry).length > 0
? isEntryEnabled(driversById, entry)
: Effect.succeed(false);
},
).pipe(
Effect.map((targets) =>
targets.flatMap(([, entry]) => secretReferencesOf(driversById, entry)),
),
);
if (references.length > 0) yield* (yield* SecretResolver.SecretResolver).read(references);
yield* semaphore.withPermits(1)(
Effect.gen(function* () {
const current = yield* Ref.get(entries);
const ids = new Set(
instances
.filter((instance) => current.get(instance.instanceId)?.instance === instance)
.map((instance) => instance.instanceId),
);
if (ids.size === 0) return;
yield* reconcileWithR(yield* Ref.get(configMapRef), ids);
}),
);
}, Effect.provideContext(driverContext));

// Reading secrets can wait on a vault unlock, so the server does not wait for it to start.
yield* semaphore.withPermits(1)(
Ref.set(configMapRef, input.configMap).pipe(
Expand Down Expand Up @@ -554,6 +591,7 @@ export const makeProviderInstanceRegistry = Effect.fn(
const mutator: ProviderInstanceRegistryMutator.ProviderInstanceRegistryMutatorShape = {
reconcile,
reloadSecrets,
rebuild,
};

return { registry, mutator };
Expand Down
3 changes: 3 additions & 0 deletions apps/server/src/provider/ProviderInstanceRegistryMutator.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
/** Reconcile closes removed/replaced scopes before creating replacements: at most one live instance per id. */
import type { ProviderInstanceConfigMap, ProviderInstanceId } from "@t3tools/contracts";
import type { ProviderInstance } from "@t3tools/provider-core/server/driver";
import * as Context from "effect/Context";
import type * as Effect from "effect/Effect";

export interface ProviderInstanceRegistryMutatorShape {
readonly reconcile: (configMap: ProviderInstanceConfigMap) => Effect.Effect<void>;
/** Re-reads the secrets of every instance, or one, and rebuilds those whose secrets changed. */
readonly reloadSecrets: (instanceId?: ProviderInstanceId) => Effect.Effect<void>;
/** Rebuilds these instances from their current config, so their drivers read the environment again. */
readonly rebuild: (instances: ReadonlyArray<ProviderInstance>) => Effect.Effect<void>;
}

export class ProviderInstanceRegistryMutator extends Context.Service<
Expand Down
Loading
Loading