Skip to content

Commit f6fb4f9

Browse files
committed
fix(server): use Effect services for workspace operations
1 parent 4f45e23 commit f6fb4f9

11 files changed

Lines changed: 116 additions & 42 deletions

‎apps/server/src/checkpointing/CheckpointStore.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* @module CheckpointStore
1515
*/
1616
import { VcsUnsupportedOperationError, type CheckpointRef } from "@t3tools/contracts";
17-
import * as NodePath from "node:path";
17+
import * as Path from "effect/Path";
1818
import * as Context from "effect/Context";
1919
import * as Effect from "effect/Effect";
2020
import * as Layer from "effect/Layer";
@@ -105,6 +105,7 @@ export class CheckpointStore extends Context.Service<
105105

106106
/** @public Service construction is part of the canonical Effect module API. */
107107
export const make = Effect.gen(function* () {
108+
const path = yield* Path.Path;
108109
const vcsRegistry = yield* VcsDriverRegistry.VcsDriverRegistry;
109110

110111
const resolveCheckpoints = Effect.fn("CheckpointStore.resolveCheckpoints")(function* (
@@ -129,8 +130,7 @@ export const make = Effect.gen(function* () {
129130
Effect.map(
130131
(handle) =>
131132
handle !== null &&
132-
(!requireRoot ||
133-
NodePath.resolve(handle.repository.rootPath) === NodePath.resolve(cwd)),
133+
(!requireRoot || path.resolve(handle.repository.rootPath) === path.resolve(cwd)),
134134
),
135135
);
136136

‎apps/server/src/orchestration-v2/Adapters/GrokAdapterV2.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as NodePath from "@effect/platform-node/NodePath";
12
import * as NodeServices from "@effect/platform-node/NodeServices";
23
import {
34
ProjectId,
@@ -420,6 +421,7 @@ describe("Grok launch permission mode", () => {
420421
}).pipe(
421422
Effect.provide(
422423
RuntimePolicy.layerFromProjectStore.pipe(
424+
Layer.provide(NodePath.layerPosix),
423425
Layer.provide(
424426
Layer.mock(ProjectStore.ProjectStoreV2)({
425427
get: () => Effect.die("the thread has a worktree"),

‎apps/server/src/orchestration-v2/CheckpointCaptureService.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as NodePath from "@effect/platform-node/NodePath";
12
import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
23
import { assert, it } from "@effect/vitest";
34
import {
@@ -276,6 +277,7 @@ it.layer(layerProjectionStoreTest)("CheckpointCaptureServiceV2", (it) => {
276277
Layer.mergeAll(
277278
IdAllocator.layer,
278279
NodeCrypto.layer,
280+
NodePath.layer,
279281
Layer.mock(CheckpointStore.CheckpointStore)({
280282
isGitRepository: () => Effect.succeed(true),
281283
captureCheckpoint: () => Effect.void,

‎apps/server/src/orchestration-v2/CheckpointService.test.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as NodePath from "@effect/platform-node/NodePath";
12
import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
23
import { assert, it, vi } from "@effect/vitest";
34
import {
@@ -59,7 +60,7 @@ it.effect.each([false, true, "interrupt"] as const)(
5960
}),
6061
),
6162
),
62-
Layer.provideMerge(NodeCrypto.layer),
63+
Layer.provideMerge(Layer.mergeAll(NodeCrypto.layer, NodePath.layer)),
6364
);
6465

6566
return Effect.gen(function* () {
@@ -169,7 +170,7 @@ it.effect("keeps workspace captures independent, idempotent, and repository-qual
169170
}),
170171
),
171172
),
172-
Layer.provideMerge(NodeCrypto.layer),
173+
Layer.provideMerge(Layer.mergeAll(NodeCrypto.layer, NodePath.layer)),
173174
);
174175
return Effect.gen(function* () {
175176
const service = yield* CheckpointService.CheckpointServiceV2;

‎apps/server/src/orchestration-v2/CheckpointService.ts‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import * as Hex from "effect/encoding/Hex";
2020
import * as KeyedLock from "@t3tools/shared/KeyedLock";
2121
import * as Layer from "effect/Layer";
2222
import * as Schema from "effect/Schema";
23-
import * as NodePath from "node:path";
23+
import * as Path from "effect/Path";
2424

2525
import { parseTurnDiffFilesFromNumstat } from "../checkpointing/Diffs.ts";
2626
import * as CheckpointStore from "../checkpointing/CheckpointStore.ts";
@@ -256,10 +256,11 @@ function makeCheckpoint(input: {
256256
export const layer: Layer.Layer<
257257
CheckpointServiceV2,
258258
never,
259-
CheckpointStore.CheckpointStore | Crypto.Crypto | IdAllocator.IdAllocatorV2
259+
CheckpointStore.CheckpointStore | Crypto.Crypto | IdAllocator.IdAllocatorV2 | Path.Path
260260
> = Layer.effect(
261261
CheckpointServiceV2,
262262
Effect.gen(function* () {
263+
const path = yield* Path.Path;
263264
const checkpointStore = yield* CheckpointStore.CheckpointStore;
264265
const crypto = yield* Crypto.Crypto;
265266
const checkpointRefFor = (input: Parameters<typeof checkpointRefForScopeOrdinal>[0]) =>
@@ -287,11 +288,11 @@ export const layer: Layer.Layer<
287288
repository: WorkspaceRepository,
288289
) =>
289290
(scope.workspaceRepositories ?? []).flatMap((other) => {
290-
const relative = NodePath.relative(repository.checkoutPath, other.checkoutPath);
291+
const relative = path.relative(repository.checkoutPath, other.checkoutPath);
291292
return relative &&
292-
!NodePath.isAbsolute(relative) &&
293+
!path.isAbsolute(relative) &&
293294
relative !== ".." &&
294-
!relative.startsWith(`..${NodePath.sep}`)
295+
!relative.startsWith(`..${path.sep}`)
295296
? [relative]
296297
: [];
297298
});

‎apps/server/src/orchestration-v2/Orchestrator.ts‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4677,11 +4677,14 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
46774677
.get(modelSelection.instanceId)
46784678
.pipe(mapDispatchError(command));
46794679
const capabilities = yield* adapter.getCapabilities().pipe(mapDispatchError(command));
4680-
yield* validateWorkspaceProviderRoots({
4681-
workspace: projection.thread.workspace,
4682-
providerInstanceId: modelSelection.instanceId,
4683-
capabilities,
4684-
});
4680+
yield* validateWorkspaceProviderRoots(
4681+
{
4682+
workspace: projection.thread.workspace,
4683+
providerInstanceId: modelSelection.instanceId,
4684+
capabilities,
4685+
},
4686+
path,
4687+
);
46854688
}
46864689
let dispatchMode = resolveMessageDispatchIntent(
46874690
projection,

‎apps/server/src/orchestration-v2/ProviderTurnStartService.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ export class ProviderTurnStartError extends Schema.TaggedError<ProviderTurnStart
5858
},
5959
) {}
6060

61+
const encodeWorkspaceFolders = Schema.encodeSync(
62+
Schema.fromJsonString(Schema.Array(Schema.Struct({ name: Schema.String, path: Schema.String }))),
63+
);
64+
6165
const isProviderTurnStartError = Schema.is(ProviderTurnStartError);
6266

6367
/** Claude refuses to replace a process running background work before it reads the prompt. */
@@ -1167,7 +1171,7 @@ export const layer: Layer.Layer<
11671171
if (!(yield* isCurrentAttemptInStatus("running"))) return;
11681172
const start = compact ? session.compactThread! : session.startTurn;
11691173
const workspaceContext = projection.thread.workspace
1170-
? `This thread uses a multi-folder workspace. Its folders on this environment are:\n${JSON.stringify(projection.thread.workspace.folders.map(({ name, path }) => ({ name, path })))}\nUse these existing checkouts. Workspace worktrees and filesystem restore are not supported.`
1174+
? `This thread uses a multi-folder workspace. Its folders on this environment are:\n${encodeWorkspaceFolders(projection.thread.workspace.folders.map(({ name, path }) => ({ name, path })))}\nUse these existing checkouts. Workspace worktrees and filesystem restore are not supported.`
11711175
: "";
11721176
const context = [workspaceContext, delivery.context, restartNote]
11731177
.filter((part) => part !== "")

‎apps/server/src/orchestration-v2/RuntimePolicy.test.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as NodePath from "@effect/platform-node/NodePath";
12
import { assert, it } from "@effect/vitest";
23
import {
34
type ModelSelection,
@@ -11,6 +12,7 @@ import {
1112
import * as DateTime from "effect/DateTime";
1213
import * as Effect from "effect/Effect";
1314
import * as Layer from "effect/Layer";
15+
import * as Path from "effect/Path";
1416
import * as Option from "effect/Option";
1517
import * as Stream from "effect/Stream";
1618
import { CodexProviderCapabilitiesV2 } from "./Adapters/CodexAdapterV2.ts";
@@ -88,6 +90,7 @@ const providerInstanceFor = (instanceId: ProviderInstanceId) =>
8890
}) as unknown as ProviderInstance;
8991

9092
const layerTest = RuntimePolicy.layerFromProjectStore.pipe(
93+
Layer.provide(NodePath.layerPosix),
9194
Layer.provide(
9295
Layer.succeed(ProviderInstanceRegistry.ProviderInstanceRegistry, {
9396
getInstance: (instanceId) => Effect.succeed(providerInstanceFor(instanceId)),
@@ -207,3 +210,32 @@ it.layer(layerTest)("RuntimePolicyV2", (it) => {
207210
}),
208211
);
209212
});
213+
214+
it.effect("validates Windows descendants, external siblings and distinct drives", () =>
215+
Effect.gen(function* () {
216+
const path = yield* Path.Path;
217+
const workspace = {
218+
filePath: "C:\\meta\\all.code-workspace",
219+
workspaceRoot: "C:\\meta",
220+
folders: [{ id: "nested", name: "Nested", path: "C:\\meta\\child" }],
221+
repositories: [],
222+
};
223+
yield* RuntimePolicy.validateWorkspaceProviderRoots(
224+
{ workspace, providerInstanceId, capabilities: undefined },
225+
path,
226+
);
227+
const external = ["C:\\outside", "D:\\child", "D:\\child"];
228+
const error = yield* RuntimePolicy.validateWorkspaceProviderRoots(
229+
{
230+
workspace: {
231+
...workspace,
232+
folders: external.map((path, index) => ({ id: String(index), name: "External", path })),
233+
},
234+
providerInstanceId,
235+
capabilities: undefined,
236+
},
237+
path,
238+
).pipe(Effect.flip);
239+
assert.equal(error.externalRootCount, 2);
240+
}).pipe(Effect.provide(NodePath.layerWin32)),
241+
);

‎apps/server/src/orchestration-v2/RuntimePolicy.ts‎

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import * as Schema from "effect/Schema";
1616
import * as ProviderInstanceRegistry from "../provider/ProviderInstanceRegistry.ts";
1717
import * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";
1818
import * as ProjectStore from "./ProjectStore.ts";
19-
import * as NodePath from "node:path";
19+
import * as Path from "effect/Path";
2020

2121
/**
2222
* ERRORS
@@ -43,18 +43,19 @@ export class WorkspaceProviderRootsUnsupportedError extends Schema.TaggedError<W
4343
}
4444
}
4545

46-
export const validateWorkspaceProviderRoots = (input: {
47-
readonly workspace: CodeWorkspace;
48-
readonly providerInstanceId: ProviderInstanceId;
49-
readonly capabilities: OrchestrationV2ProviderCapabilities | undefined;
50-
}) => {
46+
export const validateWorkspaceProviderRoots = (
47+
input: {
48+
readonly workspace: CodeWorkspace;
49+
readonly providerInstanceId: ProviderInstanceId;
50+
readonly capabilities: OrchestrationV2ProviderCapabilities | undefined;
51+
},
52+
path: Path.Path,
53+
) => {
5154
const externalRootCount = [
5255
...new Set(input.workspace.folders.map((folder) => folder.path)),
5356
].filter((directory) => {
54-
const relative = NodePath.relative(input.workspace.workspaceRoot, directory);
55-
return (
56-
relative === ".." || relative.startsWith(`..${NodePath.sep}`) || NodePath.isAbsolute(relative)
57-
);
57+
const relative = path.relative(input.workspace.workspaceRoot, directory);
58+
return relative === ".." || relative.startsWith(`..${path.sep}`) || path.isAbsolute(relative);
5859
}).length;
5960
return externalRootCount > 0 &&
6061
input.capabilities?.runtimePolicy.supportsAdditionalWorkspaceRoots !== true
@@ -134,10 +135,11 @@ function providerRuntimeMode(
134135
export const layerFromProjectStore: Layer.Layer<
135136
RuntimePolicyV2,
136137
never,
137-
ProjectStore.ProjectStoreV2 | ProviderInstanceRegistry.ProviderInstanceRegistry
138+
ProjectStore.ProjectStoreV2 | ProviderInstanceRegistry.ProviderInstanceRegistry | Path.Path
138139
> = Layer.effect(
139140
RuntimePolicyV2,
140141
Effect.gen(function* () {
142+
const path = yield* Path.Path;
141143
const projects = yield* ProjectStore.ProjectStoreV2;
142144
const providerInstances = yield* ProviderInstanceRegistry.ProviderInstanceRegistry;
143145
return RuntimePolicyV2.of({
@@ -190,11 +192,14 @@ export const layerFromProjectStore: Layer.Layer<
190192
}),
191193
),
192194
);
193-
yield* validateWorkspaceProviderRoots({
194-
workspace: input.thread.workspace,
195-
capabilities,
196-
providerInstanceId: input.modelSelection.instanceId,
197-
});
195+
yield* validateWorkspaceProviderRoots(
196+
{
197+
workspace: input.thread.workspace,
198+
capabilities,
199+
providerInstanceId: input.modelSelection.instanceId,
200+
},
201+
path,
202+
);
198203
}
199204
return ProviderAdapter.ProviderAdapterV2RuntimePolicy.make({
200205
runtimeMode: providerRuntimeMode(input.thread.runtimeMode, supportedRuntimeModes),

‎apps/server/src/project/CodeWorkspaceLoader.test.ts‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const fixture = Effect.fnUntraced(function* () {
2323
const meta = path.join(base, "meta");
2424
const children = ["one", "two"].map((name) => path.join(meta, name));
2525
const external = path.join(base, "external");
26-
const plain = path.join(base, "plain");
26+
const plain = path.join(base, "plain-你好-é-🚀");
2727
for (const dir of [meta, ...children, external, plain]) {
2828
yield* fs.makeDirectory(dir, { recursive: true });
2929
}
@@ -54,7 +54,7 @@ it.layer(layer)("CodeWorkspaceLoader", (it) => {
5454
{ "path": "alias", "name": "Alias" },
5555
{ "path": "two" },
5656
{ "path": ${JSON.stringify(f.external)} },
57-
{ "path": "../plain" },
57+
{ "path": "../plain-你好-é-🚀" },
5858
],
5959
}`,
6060
);
@@ -68,8 +68,27 @@ it.layer(layer)("CodeWorkspaceLoader", (it) => {
6868
"Alias",
6969
"two",
7070
"external",
71-
"plain",
71+
"plain-你好-é-🚀",
7272
]);
73+
const runner = yield* ProcessRunner.ProcessRunner;
74+
const legacyId = Effect.fnUntraced(function* (kind: string, canonicalPath: string) {
75+
const result = yield* runner.run({
76+
command: process.execPath,
77+
args: [
78+
"-e",
79+
'process.stdout.write(require("node:crypto").createHash("sha256").update(process.argv[1]).digest("hex"))',
80+
canonicalPath,
81+
],
82+
});
83+
expect(result.code).toBe(0);
84+
return `${kind}-${result.stdout}`;
85+
});
86+
for (const folder of loaded.workspace.folders) {
87+
expect(folder.id).toBe(yield* legacyId("folder", folder.path));
88+
}
89+
for (const repository of loaded.workspace.repositories) {
90+
expect(repository.id).toBe(yield* legacyId("repository", repository.checkoutPath));
91+
}
7392
expect(loaded.workspace.folders[0]?.id).toBe(loaded.workspace.folders[2]?.id);
7493
expect(loaded.workspace.repositories.map((repo) => repo.checkoutPath)).toEqual([
7594
f.meta,

0 commit comments

Comments
 (0)