Skip to content

Commit 8e69659

Browse files
committed
fix(provider): resolve skills and slash commands against the workspace root
Both composer pickers are empty or incomplete in a packaged desktop build. `DesktopEnvironment` sets the server's cwd to the user's home directory when packaged (`isPackaged ? homeDirectory : appRoot`). `ServerProvider.skills` and `ServerProvider.slashCommands` are produced once per provider instance against that single cwd, but both are project-scoped: - `ClaudeSkills` scans `<configDir>/skills` and `<cwd>/.claude/skills`, so the "project" root resolves to `~/.claude/skills` for every project. A user who keeps no skills there sees none at all, and `$` reports "No skills found". - the CLI's init handshake reports project-scoped slash commands, so `/` lists only the built-ins. Measured on one real workspace: the CLI reports 140 commands from the project root versus 48 from a neutral directory. A provider is a machine-level installation in this model — version, auth and models genuinely are per-machine — so rather than making the snapshot project-aware this adds a workspace-scoped surface beside it: - `ProviderInstance` gains optional `discoverSkillsForCwd` and `discoverSlashCommandsForCwd`. Optional, so the other drivers are untouched. - `ProviderRegistry` gains matching `*ForInstance` methods that follow the existing `getProviderMaintenanceCapabilitiesForInstance` delegation pattern and resolve empty when a driver cannot enumerate per directory. - new `providers.workspaceSkills` RPC resolves `thread.worktreePath ?? project.workspaceRoot` — the same idiom `assets.createUrl` already uses — and is mapped to orchestration:read. - the composer prefers the workspace result for `$` and `/`, falling back to the snapshot, so an older server behaves exactly as before. The RPC is keyed on the project with an optional thread, because the composer is frequently on a draft thread that has no persisted row yet, and that is precisely when the pickers matter. `ClaudeDriver`'s capabilities cache is now keyed by cwd (capacity 16) instead of holding a single entry probed against the server's cwd. `makeClaudeCapabilitiesCacheKey` already included cwd and `ClaudeHome.test.ts` already asserted that two cwds produce two keys, so this completes what the key was designed for. Binary path and resolved HOME are constant within one instance's scope, so keying on cwd preserves the isolation the composite key provided. Cost is one CLI probe per workspace per existing 5-minute TTL: measured ~1.6s cold, ~20ms warm. Skills are deliberately still read from the filesystem rather than the init handshake: `init.skills` carries bare names, while `ServerProviderSkill` requires `path` and displays `description`, which only the scan can supply.
1 parent 11f0513 commit 8e69659

14 files changed

Lines changed: 383 additions & 33 deletions

File tree

‎apps/server/src/auth/RpcAuthorization.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ export const RPC_REQUIRED_SCOPES = {
8383
[WS_METHODS.projectsWriteFile]: AuthOrchestrationOperateScope,
8484
[WS_METHODS.shellOpenInEditor]: AuthOrchestrationOperateScope,
8585
[WS_METHODS.filesystemBrowse]: AuthOrchestrationReadScope,
86+
// Reads a thread, its project's workspace root, and that directory's
87+
// skill definitions. Read-only, so it sits with the other read methods.
88+
[WS_METHODS.providersWorkspaceSkills]: AuthOrchestrationReadScope,
8689
[WS_METHODS.assetsCreateUrl]: AuthOrchestrationReadScope,
8790
[WS_METHODS.subscribeVcsStatus]: AuthOrchestrationReadScope,
8891
[WS_METHODS.subscribeResourceTelemetry]: AuthOrchestrationReadScope,

‎apps/server/src/provider/Drivers/ClaudeDriver.ts‎

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,15 @@ import {
5454
makeProviderSnapshotSettingsSource,
5555
type ProviderSnapshotSettings,
5656
} from "../providerUpdateSettings.ts";
57-
import { makeClaudeCapabilitiesCacheKey, makeClaudeContinuationGroupKey } from "./ClaudeHome.ts";
57+
import { makeClaudeContinuationGroupKey } from "./ClaudeHome.ts";
58+
import { discoverClaudeSkills } from "./ClaudeSkills.ts";
5859
const decodeClaudeSettings = Schema.decodeSync(ClaudeSettings);
5960

6061
const DRIVER_KIND = ProviderDriverKind.make("claudeAgent");
6162
const CAPABILITIES_PROBE_TTL = Duration.minutes(5);
63+
// One entry per workspace the user has open. Small enough to bound CLI
64+
// spawns, large enough that switching between projects does not re-probe.
65+
const CAPABILITIES_PROBE_CACHE_CAPACITY = 16;
6266

6367
function isClaudeNativeCommandPath(commandPath: string): boolean {
6468
const normalized = normalizeCommandPath(commandPath);
@@ -151,21 +155,27 @@ export const ClaudeDriver: ProviderDriver<ClaudeSettings, ClaudeDriverEnv> = {
151155
const adapter = yield* makeClaudeAdapter(effectiveConfig, adapterOptions);
152156
const textGeneration = yield* makeClaudeTextGeneration(effectiveConfig, processEnv);
153157

154-
// Per-instance capabilities cache: keyed on binary + resolved HOME so
155-
// account-specific probes never share auth metadata across instances.
158+
// Per-instance capabilities cache, keyed by working directory.
159+
//
160+
// This cache lives in one instance's scope, so binary path and resolved
161+
// HOME are already constant here — they cannot leak account metadata
162+
// across instances. The only thing that varies is the cwd, and it
163+
// genuinely does vary: the CLI's init handshake reports PROJECT-scoped
164+
// slash commands, so a probe run from the server's own cwd sees only the
165+
// built-ins. Keying on cwd lets each workspace get its own answer while
166+
// still costing one CLI spawn per workspace per TTL.
156167
const capabilitiesProbeCache = yield* Cache.make({
157-
capacity: 1,
168+
capacity: CAPABILITIES_PROBE_CACHE_CAPACITY,
158169
timeToLive: CAPABILITIES_PROBE_TTL,
159-
lookup: () =>
160-
probeClaudeCapabilities(effectiveConfig, processEnv, cwd).pipe(
170+
lookup: (probeCwd: string) =>
171+
probeClaudeCapabilities(effectiveConfig, processEnv, probeCwd).pipe(
161172
Effect.provideService(Path.Path, path),
162173
),
163174
});
164-
const capabilitiesCacheKey = yield* makeClaudeCapabilitiesCacheKey(effectiveConfig, cwd);
165175

166176
const checkProvider = checkClaudeProviderStatus(
167177
effectiveConfig,
168-
() => Cache.get(capabilitiesProbeCache, capabilitiesCacheKey),
178+
() => Cache.get(capabilitiesProbeCache, cwd),
169179
processEnv,
170180
cwd,
171181
).pipe(
@@ -216,6 +226,26 @@ export const ClaudeDriver: ProviderDriver<ClaudeSettings, ClaudeDriverEnv> = {
216226
snapshot,
217227
adapter,
218228
textGeneration,
229+
// Project-scoped skills. The snapshot's own `skills` are scanned once
230+
// against `ServerConfig.cwd`, which a packaged build sets to the home
231+
// directory, so it can never see a project's `.claude/skills`. This is
232+
// a filesystem scan, not a CLI probe, so it is cheap enough to run per
233+
// request and needs no cache.
234+
discoverSkillsForCwd: (skillsCwd: string) =>
235+
discoverClaudeSkills(effectiveConfig, skillsCwd, processEnv).pipe(
236+
Effect.provideService(FileSystem.FileSystem, fileSystem),
237+
Effect.provideService(Path.Path, path),
238+
),
239+
// Slash commands come from the CLI's init handshake, so this spawns a
240+
// process. It goes through the same cwd-keyed cache the snapshot probe
241+
// uses, which bounds it to one spawn per workspace per TTL. A failed
242+
// probe resolves empty so the caller falls back to the snapshot rather
243+
// than surfacing an error in a picker.
244+
discoverSlashCommandsForCwd: (commandsCwd: string) =>
245+
Cache.get(capabilitiesProbeCache, commandsCwd).pipe(
246+
Effect.map((capabilities) => capabilities?.slashCommands ?? []),
247+
Effect.orElseSucceed(() => []),
248+
),
219249
} satisfies ProviderInstance;
220250
}),
221251
};

‎apps/server/src/provider/Layers/ProviderRegistry.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ import {
2727
ProviderDriverKind,
2828
type ProviderInstanceId,
2929
type ServerProvider,
30+
type ServerProviderSkill,
31+
type ServerProviderSlashCommand,
3032
type ServerProviderUpdateState,
3133
} from "@t3tools/contracts";
3234
import * as Cause from "effect/Cause";
@@ -508,6 +510,33 @@ export const ProviderRegistryLive = Layer.effect(
508510
);
509511
});
510512

513+
const discoverSkillsForInstance = Effect.fn("discoverSkillsForInstance")(function* (
514+
instanceId: ProviderInstanceId,
515+
cwd: string,
516+
) {
517+
const instance = Array.from((yield* Ref.get(liveSubsRef)).values()).find(
518+
(candidate) => candidate.instanceId === instanceId,
519+
);
520+
// Not live, or a driver with no directory-scoped skill concept: resolve
521+
// empty so the caller falls back to the snapshot instead of failing.
522+
if (!instance?.discoverSkillsForCwd) {
523+
return [] as ReadonlyArray<ServerProviderSkill>;
524+
}
525+
return yield* instance.discoverSkillsForCwd(cwd);
526+
});
527+
528+
const discoverSlashCommandsForInstance = Effect.fn("discoverSlashCommandsForInstance")(
529+
function* (instanceId: ProviderInstanceId, cwd: string) {
530+
const instance = Array.from((yield* Ref.get(liveSubsRef)).values()).find(
531+
(candidate) => candidate.instanceId === instanceId,
532+
);
533+
if (!instance?.discoverSlashCommandsForCwd) {
534+
return [] as ReadonlyArray<ServerProviderSlashCommand>;
535+
}
536+
return yield* instance.discoverSlashCommandsForCwd(cwd);
537+
},
538+
);
539+
511540
/**
512541
* Diff the aggregator's live-source set against the current
513542
* `ProviderInstanceRegistry` and:
@@ -711,6 +740,8 @@ export const ProviderRegistryLive = Layer.effect(
711740
refreshInstance: (instanceId: ProviderInstanceId) =>
712741
refreshInstance(instanceId).pipe(Effect.catchCause(recoverRefreshFailure)),
713742
getProviderMaintenanceCapabilitiesForInstance,
743+
discoverSkillsForInstance,
744+
discoverSlashCommandsForInstance,
714745
setProviderMaintenanceActionState,
715746
get streamChanges() {
716747
return Stream.fromPubSub(changesPubSub);

‎apps/server/src/provider/ProviderDriver.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import type {
2525
ProviderDriverKind,
2626
ProviderInstanceEnvironment,
2727
ProviderInstanceId,
28+
ServerProviderSkill,
29+
ServerProviderSlashCommand,
2830
} from "@t3tools/contracts";
2931
import type * as Effect from "effect/Effect";
3032
import type * as Schema from "effect/Schema";
@@ -71,6 +73,37 @@ export interface ProviderInstance {
7173
readonly snapshot: ServerProviderShape;
7274
readonly adapter: ProviderAdapterShape<ProviderAdapterError>;
7375
readonly textGeneration: TextGeneration.TextGeneration["Service"];
76+
/**
77+
* Skills visible from a specific workspace root.
78+
*
79+
* `snapshot.skills` is machine-scoped: it is produced once per instance
80+
* against the server's own cwd, which for a packaged desktop build is the
81+
* user's home directory. Skills are project-scoped, so that snapshot can
82+
* only ever report the user-scope ones. Callers that know which project
83+
* they are asking about (a thread's worktree or its project's workspace
84+
* root) use this instead, and drivers that can enumerate skills per
85+
* directory implement it.
86+
*
87+
* Optional: a driver that has no directory-scoped skill concept simply
88+
* omits it and callers fall back to the snapshot.
89+
*/
90+
readonly discoverSkillsForCwd?: (
91+
cwd: string,
92+
) => Effect.Effect<ReadonlyArray<ServerProviderSkill>>;
93+
/**
94+
* Slash commands visible from a specific workspace root.
95+
*
96+
* Same scoping problem as `discoverSkillsForCwd`, different source: these
97+
* come from the agent CLI's own init handshake, which reports PROJECT-scoped
98+
* commands. `snapshot.slashCommands` is probed once against the server's cwd
99+
* and therefore lists only the CLI's built-ins.
100+
*
101+
* Unlike the skills scan this spawns the CLI, so implementations should
102+
* cache per directory rather than probe per call.
103+
*/
104+
readonly discoverSlashCommandsForCwd?: (
105+
cwd: string,
106+
) => Effect.Effect<ReadonlyArray<ServerProviderSlashCommand>>;
74107
}
75108

76109
export interface ProviderContinuationIdentity {

‎apps/server/src/provider/Services/ProviderRegistry.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import type {
1010
ProviderInstanceId,
1111
ProviderDriverKind,
1212
ServerProvider,
13+
ServerProviderSkill,
14+
ServerProviderSlashCommand,
1315
ServerProviderUpdateState,
1416
} from "@t3tools/contracts";
1517
import * as Context from "effect/Context";
@@ -57,6 +59,36 @@ export interface ProviderRegistryShape {
5759
provider: ProviderDriverKind,
5860
) => Effect.Effect<ProviderMaintenanceCapabilities>;
5961

62+
/**
63+
* Skills one live instance can see from a specific workspace root.
64+
*
65+
* `ServerProvider.skills` is machine-scoped — scanned once against the
66+
* server's own cwd, which a packaged desktop build sets to the home
67+
* directory — so it reports user-scope skills only. Callers holding a
68+
* project context ask here instead.
69+
*
70+
* Resolves to an empty array when the instance is not live or its driver
71+
* has no directory-scoped skill concept; callers then fall back to the
72+
* snapshot rather than treating it as an error.
73+
*/
74+
readonly discoverSkillsForInstance: (
75+
instanceId: ProviderInstanceId,
76+
cwd: string,
77+
) => Effect.Effect<ReadonlyArray<ServerProviderSkill>>;
78+
79+
/**
80+
* Slash commands one live instance can see from a specific workspace root.
81+
*
82+
* Same scoping story as `discoverSkillsForInstance`, but sourced from the
83+
* agent CLI's init handshake rather than the filesystem, so it may spawn a
84+
* process. Resolves empty when the instance is not live or its driver cannot
85+
* enumerate commands per directory.
86+
*/
87+
readonly discoverSlashCommandsForInstance: (
88+
instanceId: ProviderInstanceId,
89+
cwd: string,
90+
) => Effect.Effect<ReadonlyArray<ServerProviderSlashCommand>>;
91+
6092
/**
6193
* Apply volatile maintenance-action state to one configured instance.
6294
* This state is never persisted to disk. Today only update actions are

‎apps/server/src/provider/providerMaintenanceRunner.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,8 @@ function makeRegistry(
191191
refreshInstance: () => Ref.get(providersRef),
192192
getProviderMaintenanceCapabilitiesForInstance: (_instanceId, provider) =>
193193
Effect.succeed(lifecycleFor(provider)),
194+
discoverSkillsForInstance: () => Effect.succeed([]),
195+
discoverSlashCommandsForInstance: () => Effect.succeed([]),
194196
setProviderMaintenanceActionState,
195197
streamChanges: Stream.empty,
196198
};

‎apps/server/src/provider/testUtils/providerRegistryMock.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ export const makeProviderRegistryMock = (
1313
refreshInstance: () => Effect.succeed(providers),
1414
getProviderMaintenanceCapabilitiesForInstance: (_instanceId, provider) =>
1515
Effect.succeed(makeManualOnlyProviderMaintenanceCapabilities({ provider, packageName: null })),
16+
discoverSkillsForInstance: () => Effect.succeed([]),
17+
discoverSlashCommandsForInstance: () => Effect.succeed([]),
1618
setProviderMaintenanceActionState: () => Effect.succeed(providers),
1719
streamChanges: Stream.empty,
1820
});

‎apps/server/src/ws.ts‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1934,6 +1934,57 @@ const makeWsRpcLayer = (
19341934
),
19351935
{ "rpc.aggregate": "workspace" },
19361936
),
1937+
[WS_METHODS.providersWorkspaceSkills]: (input) =>
1938+
observeRpcEffect(
1939+
WS_METHODS.providersWorkspaceSkills,
1940+
Effect.gen(function* () {
1941+
// The project root is the base; a persisted thread's worktree
1942+
// overrides it. Keyed on the project rather than the thread so a
1943+
// DRAFT thread still resolves — it has no server-side row yet,
1944+
// and that is exactly when the skill picker matters most.
1945+
const project = yield* projectionSnapshotQuery
1946+
.getProjectShellById(input.projectId)
1947+
.pipe(
1948+
Effect.mapError(
1949+
(cause) =>
1950+
new OrchestrationGetSnapshotError({
1951+
message: "Failed to load project for skill discovery",
1952+
cause,
1953+
}),
1954+
),
1955+
);
1956+
if (Option.isNone(project)) {
1957+
return yield* new OrchestrationGetSnapshotError({
1958+
message: `Unknown project ${input.projectId}`,
1959+
});
1960+
}
1961+
// An unknown thread id is not an error: a draft thread has no row.
1962+
const worktreePath = input.threadId
1963+
? yield* projectionSnapshotQuery.getThreadShellById(input.threadId).pipe(
1964+
Effect.map((thread) =>
1965+
Option.isSome(thread) ? thread.value.worktreePath : null,
1966+
),
1967+
Effect.orElseSucceed(() => null),
1968+
)
1969+
: null;
1970+
const workspaceRoot = worktreePath ?? project.value.workspaceRoot;
1971+
// Run both against the same resolved root. The skills scan is
1972+
// filesystem-only; the slash-command probe may spawn the CLI but
1973+
// is cached per directory by the driver.
1974+
const [skills, slashCommands] = yield* Effect.all(
1975+
[
1976+
providerRegistry.discoverSkillsForInstance(input.instanceId, workspaceRoot),
1977+
providerRegistry.discoverSlashCommandsForInstance(
1978+
input.instanceId,
1979+
workspaceRoot,
1980+
),
1981+
],
1982+
{ concurrency: 2 },
1983+
);
1984+
return { workspaceRoot, skills, slashCommands };
1985+
}),
1986+
{ "rpc.aggregate": "provider" },
1987+
),
19371988
[WS_METHODS.assetsCreateUrl]: (input) =>
19381989
observeRpcEffect(
19391990
WS_METHODS.assetsCreateUrl,

‎apps/web/src/components/ChatView.tsx‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ import { environmentCatalog } from "../connection/catalog";
239239
import { selectThreadTerminalUiState, useTerminalUiStateStore } from "../terminalUiStateStore";
240240
import { useKnownTerminalSessions, useThreadRunningTerminalIds } from "../state/terminalSessions";
241241
import { projectEnvironment } from "../state/projects";
242+
import { providerSkillsEnvironment } from "../state/providerSkills";
242243
import { useEnvironmentQuery } from "../state/query";
243244
import {
244245
primaryServerAvailableEditorsAtom,
@@ -2720,6 +2721,38 @@ function ChatViewContent(props: ChatViewProps) {
27202721
const defaultInstanceId = defaultInstanceIdForDriver(selectedProvider);
27212722
return providerStatuses.find((status) => status.instanceId === defaultInstanceId) ?? null;
27222723
}, [activeProviderInstanceId, providerStatuses, selectedProvider]);
2724+
// Skills for the `$` picker, resolved against this thread's workspace root.
2725+
// `activeProviderStatus.skills` is machine-scoped: the server scans it once
2726+
// per provider instance against its own cwd, which a packaged desktop build
2727+
// sets to the user's home directory, so it reports user-scope skills only and
2728+
// is empty on a machine that keeps none there. Falling back to it keeps the
2729+
// picker working against a server that predates this RPC.
2730+
const workspaceCapabilitiesQuery = useEnvironmentQuery(
2731+
activeThread && activeProject && activeProviderInstanceId
2732+
? providerSkillsEnvironment.workspaceSkills({
2733+
environmentId: activeThread.environmentId,
2734+
input: {
2735+
projectId: activeProject.id,
2736+
instanceId: activeProviderInstanceId,
2737+
// Only a persisted thread has a server-side row; a draft has none,
2738+
// and the project root is the right answer for it anyway.
2739+
...(activeServerThread ? { threadId: activeServerThread.id } : {}),
2740+
},
2741+
})
2742+
: null,
2743+
);
2744+
const activeSkills =
2745+
workspaceCapabilitiesQuery.data?.skills ??
2746+
activeProviderStatus?.skills ??
2747+
EMPTY_PROVIDER_SKILLS;
2748+
// Same story for `/`: the snapshot's slashCommands come from a probe run
2749+
// against the server's own cwd, so they are the CLI's built-ins only. The
2750+
// workspace-scoped result adds the project's own commands.
2751+
const activeSlashCommands =
2752+
workspaceCapabilitiesQuery.data?.slashCommands &&
2753+
workspaceCapabilitiesQuery.data.slashCommands.length > 0
2754+
? workspaceCapabilitiesQuery.data.slashCommands
2755+
: undefined;
27232756
const providerStatusBannerKey = getProviderStatusBannerKey(activeProviderStatus);
27242757
const [dismissedProviderStatusBannerKey, setDismissedProviderStatusBannerKey] = useState<
27252758
string | null
@@ -6534,7 +6567,7 @@ function ChatViewContent(props: ChatViewProps) {
65346567
resolvedTheme={resolvedTheme}
65356568
timestampFormat={timestampFormat}
65366569
workspaceRoot={activeWorkspaceRoot}
6537-
skills={activeProviderStatus?.skills ?? EMPTY_PROVIDER_SKILLS}
6570+
skills={activeSkills}
65386571
anchorMessageId={timelineAnchorMessageId}
65396572
onAnchorReady={onTimelineAnchorReady}
65406573
contentInsetEndAdjustment={composerOverlayHeight}
@@ -6661,6 +6694,8 @@ function ChatViewContent(props: ChatViewProps) {
66616694
interactionMode={interactionMode}
66626695
lockedProvider={lockedProvider}
66636696
providerStatuses={providerStatuses as ServerProvider[]}
6697+
workspaceSkills={activeSkills}
6698+
workspaceSlashCommands={activeSlashCommands}
66646699
activeProjectDefaultModelSelection={
66656700
activeProject?.defaultModelSelection
66666701
}

0 commit comments

Comments
 (0)