Repository navigation
Expand file tree
/
Copy pathCodexHomeLayout.ts
More file actions
422 lines (388 loc) · 12.7 KB
/
Copy pathCodexHomeLayout.ts
File metadata and controls
422 lines (388 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
import * as NodeOS from "node:os";
import { ProviderDriverKind, type CodexSettings } from "@t3tools/contracts";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Path from "effect/Path";
import * as Schema from "effect/Schema";
import * as PlatformError from "effect/PlatformError";
import { expandHomePath } from "@t3tools/provider-core/server/pathExpansion";
export interface CodexHomeLayout {
readonly mode: "direct" | "authOverlay";
readonly sharedHomePath: string;
readonly effectiveHomePath: string | undefined;
readonly continuationKey: string;
}
const KNOWN_SHARED_DIRECTORIES = [
"sessions",
"archived_sessions",
"sqlite",
"shell_snapshots",
"worktrees",
"skills",
"plugins",
"cache",
"logs",
"mcp-oauth-locks",
] as const;
const PRIVATE_ENTRY_NAMES = new Set(["auth.json", "models_cache.json"]);
const SHADOW_LOCAL_ENTRY_NAMES = new Set(["log", "memories", "tmp"]);
const REPLACEABLE_SHARED_RUNTIME_DIRECTORIES = new Set(["mcp-oauth-locks"]);
function resolveHomePath(path: Path.Path, value: string | undefined): string {
const expanded =
value && value.trim().length > 0
? expandHomePath(value)
: path.join(NodeOS.homedir(), ".codex");
return path.resolve(expanded);
}
export const resolveCodexHomeLayout = Effect.fn("resolveCodexHomeLayout")(function* (
config: CodexSettings,
): Effect.fn.Return<CodexHomeLayout, never, Path.Path> {
const path = yield* Path.Path;
const sharedHomePath = resolveHomePath(path, config.homePath);
const shadowHomePath = config.shadowHomePath.trim();
if (shadowHomePath.length === 0) {
return {
mode: "direct",
sharedHomePath,
effectiveHomePath: config.homePath.trim().length > 0 ? sharedHomePath : undefined,
continuationKey: `codex:home:${sharedHomePath}`,
};
}
const effectiveHomePath = path.resolve(expandHomePath(shadowHomePath));
return {
mode: "authOverlay",
sharedHomePath,
effectiveHomePath,
continuationKey: `codex:home:${sharedHomePath}`,
};
});
const CodexShadowHomeContext = {
sharedHomePath: Schema.String,
effectiveHomePath: Schema.String,
};
export class CodexShadowHomeFileSystemError extends Schema.TaggedError<CodexShadowHomeFileSystemError>()(
"CodexShadowHomeFileSystemError",
{
...CodexShadowHomeContext,
operation: Schema.Literals(["readLink", "makeDirectory", "readDirectory", "remove", "symlink"]),
path: Schema.String,
targetPath: Schema.optional(Schema.String),
entryName: Schema.optional(Schema.String),
cause: Schema.Defect(),
},
) {
override get message(): string {
const target = this.targetPath === undefined ? "" : ` to '${this.targetPath}'`;
return `Codex shadow home filesystem operation '${this.operation}' failed for '${this.path}'${target}.`;
}
}
export class CodexShadowHomePathConflictError extends Schema.TaggedError<CodexShadowHomePathConflictError>()(
"CodexShadowHomePathConflictError",
CodexShadowHomeContext,
) {
override get message(): string {
return `Codex shadow home path '${this.effectiveHomePath}' must be different from the shared home path '${this.sharedHomePath}'.`;
}
}
export class CodexShadowHomeEntryConflictError extends Schema.TaggedError<CodexShadowHomeEntryConflictError>()(
"CodexShadowHomeEntryConflictError",
{
...CodexShadowHomeContext,
entryName: Schema.String,
linkPath: Schema.String,
targetPath: Schema.String,
},
) {
override get message(): string {
return `Cannot create Codex shadow home entry '${this.entryName}' because '${this.linkPath}' already exists and is not a symlink.`;
}
}
export class CodexShadowHomePrivateEntrySymlinkError extends Schema.TaggedError<CodexShadowHomePrivateEntrySymlinkError>()(
"CodexShadowHomePrivateEntrySymlinkError",
{
...CodexShadowHomeContext,
entryName: Schema.String,
path: Schema.String,
},
) {
override get message(): string {
return `Codex shadow home private entry '${this.entryName}' at '${this.path}' must be a real file, not a symlink.`;
}
}
export const CodexShadowHomeError = Schema.Union([
CodexShadowHomeFileSystemError,
CodexShadowHomePathConflictError,
CodexShadowHomeEntryConflictError,
CodexShadowHomePrivateEntrySymlinkError,
]);
export type CodexShadowHomeError = typeof CodexShadowHomeError.Type;
type LinkState =
| {
readonly _tag: "Missing";
}
| {
readonly _tag: "NotSymlink";
}
| {
readonly _tag: "Symlink";
readonly target: string;
};
function isNotSymlinkError(error: PlatformError.PlatformError): boolean {
const cause = error.reason.cause;
return (
error.reason._tag === "Unknown" &&
typeof cause === "object" &&
cause !== null &&
"code" in cause &&
cause.code === "EINVAL"
);
}
const readLinkState = Effect.fn("CodexHomeLayout.readLinkState")(function* (input: {
readonly fileSystem: FileSystem.FileSystem;
readonly sharedHomePath: string;
readonly effectiveHomePath: string;
readonly entryName: string;
readonly linkPath: string;
}): Effect.fn.Return<LinkState, CodexShadowHomeError> {
return yield* input.fileSystem.readLink(input.linkPath).pipe(
Effect.map((target): LinkState => ({ _tag: "Symlink", target })),
Effect.catchTags({
PlatformError: (cause) => {
if (cause.reason._tag === "NotFound") {
return Effect.succeed<LinkState>({ _tag: "Missing" });
}
if (isNotSymlinkError(cause)) {
return Effect.succeed<LinkState>({ _tag: "NotSymlink" });
}
return new CodexShadowHomeFileSystemError({
sharedHomePath: input.sharedHomePath,
effectiveHomePath: input.effectiveHomePath,
operation: "readLink",
path: input.linkPath,
entryName: input.entryName,
cause,
});
},
}),
);
});
const removePrivateSymlink = Effect.fn("CodexHomeLayout.removePrivateSymlink")(function* (input: {
readonly fileSystem: FileSystem.FileSystem;
readonly sharedHomePath: string;
readonly effectiveHomePath: string;
readonly entryName: string;
}): Effect.fn.Return<void, CodexShadowHomeError, Path.Path> {
const path = yield* Path.Path;
const privatePath = path.join(input.effectiveHomePath, input.entryName);
const state = yield* readLinkState({
...input,
linkPath: privatePath,
});
if (state._tag === "Symlink") {
yield* input.fileSystem.remove(privatePath).pipe(
Effect.catchTags({
PlatformError: (cause) =>
new CodexShadowHomeFileSystemError({
sharedHomePath: input.sharedHomePath,
effectiveHomePath: input.effectiveHomePath,
operation: "remove",
path: privatePath,
entryName: input.entryName,
cause,
}),
}),
);
}
});
const ensureSymlink = Effect.fn("CodexHomeLayout.ensureSymlink")(function* (input: {
readonly fileSystem: FileSystem.FileSystem;
readonly sharedHomePath: string;
readonly effectiveHomePath: string;
readonly entryName: string;
}): Effect.fn.Return<void, CodexShadowHomeError, Path.Path> {
const path = yield* Path.Path;
const target = path.join(input.sharedHomePath, input.entryName);
const link = path.join(input.effectiveHomePath, input.entryName);
const state = yield* readLinkState({
...input,
linkPath: link,
});
const createLink = input.fileSystem.symlink(target, link).pipe(
Effect.catchTags({
PlatformError: (cause) =>
new CodexShadowHomeFileSystemError({
sharedHomePath: input.sharedHomePath,
effectiveHomePath: input.effectiveHomePath,
operation: "symlink",
path: link,
targetPath: target,
entryName: input.entryName,
cause,
}),
}),
);
if (state._tag === "NotSymlink") {
if (!REPLACEABLE_SHARED_RUNTIME_DIRECTORIES.has(input.entryName)) {
return yield* new CodexShadowHomeEntryConflictError({
sharedHomePath: input.sharedHomePath,
effectiveHomePath: input.effectiveHomePath,
entryName: input.entryName,
linkPath: link,
targetPath: target,
});
}
yield* input.fileSystem.remove(link, { recursive: true }).pipe(
Effect.catchTags({
PlatformError: (cause) =>
new CodexShadowHomeFileSystemError({
sharedHomePath: input.sharedHomePath,
effectiveHomePath: input.effectiveHomePath,
operation: "remove",
path: link,
entryName: input.entryName,
cause,
}),
}),
);
return yield* createLink;
}
if (state._tag === "Missing") {
return yield* createLink;
}
const resolvedExisting = path.resolve(path.dirname(link), state.target);
if (resolvedExisting !== target) {
yield* input.fileSystem.remove(link).pipe(
Effect.catchTags({
PlatformError: (cause) =>
new CodexShadowHomeFileSystemError({
sharedHomePath: input.sharedHomePath,
effectiveHomePath: input.effectiveHomePath,
operation: "remove",
path: link,
entryName: input.entryName,
cause,
}),
}),
);
yield* createLink;
}
});
const ensureShadowAuthIsPrivate = Effect.fn("CodexHomeLayout.ensureShadowAuthIsPrivate")(
function* (input: {
readonly fileSystem: FileSystem.FileSystem;
readonly sharedHomePath: string;
readonly effectiveHomePath: string;
}): Effect.fn.Return<void, CodexShadowHomeError, Path.Path> {
const path = yield* Path.Path;
const entryName = "auth.json";
const authPath = path.join(input.effectiveHomePath, entryName);
const state = yield* readLinkState({
...input,
entryName,
linkPath: authPath,
});
if (state._tag === "Symlink") {
return yield* new CodexShadowHomePrivateEntrySymlinkError({
sharedHomePath: input.sharedHomePath,
effectiveHomePath: input.effectiveHomePath,
entryName,
path: authPath,
});
}
},
);
export const materializeCodexShadowHome = Effect.fn("materializeCodexShadowHome")(function* (
layout: CodexHomeLayout,
) {
if (layout.mode !== "authOverlay") return;
const effectiveHomePath = layout.effectiveHomePath;
if (!effectiveHomePath) return;
if (layout.sharedHomePath === effectiveHomePath) {
return yield* new CodexShadowHomePathConflictError({
sharedHomePath: layout.sharedHomePath,
effectiveHomePath,
});
}
const fileSystem = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const makeDirectory = (directoryPath: string) =>
fileSystem.makeDirectory(directoryPath, { recursive: true }).pipe(
Effect.catchTags({
PlatformError: (cause) =>
new CodexShadowHomeFileSystemError({
sharedHomePath: layout.sharedHomePath,
effectiveHomePath,
operation: "makeDirectory",
path: directoryPath,
cause,
}),
}),
);
yield* Effect.all(
[
makeDirectory(layout.sharedHomePath),
makeDirectory(effectiveHomePath),
...KNOWN_SHARED_DIRECTORIES.map((directory) =>
makeDirectory(path.join(layout.sharedHomePath, directory)),
),
],
{ concurrency: "unbounded" },
);
const sharedEntryNames = yield* fileSystem.readDirectory(layout.sharedHomePath).pipe(
Effect.catchTags({
PlatformError: (cause) =>
new CodexShadowHomeFileSystemError({
sharedHomePath: layout.sharedHomePath,
effectiveHomePath,
operation: "readDirectory",
path: layout.sharedHomePath,
cause,
}),
}),
);
const entries = new Set<string>(KNOWN_SHARED_DIRECTORIES);
for (const entryName of sharedEntryNames) {
if (!PRIVATE_ENTRY_NAMES.has(entryName) && !SHADOW_LOCAL_ENTRY_NAMES.has(entryName)) {
entries.add(entryName);
}
}
yield* Effect.forEach(
PRIVATE_ENTRY_NAMES,
(entryName) =>
entryName === "auth.json"
? Effect.void
: removePrivateSymlink({
fileSystem,
sharedHomePath: layout.sharedHomePath,
effectiveHomePath,
entryName,
}),
{ discard: true },
);
yield* Effect.forEach(
entries,
(entryName) => {
if (PRIVATE_ENTRY_NAMES.has(entryName)) {
return Effect.void;
}
return ensureSymlink({
fileSystem,
sharedHomePath: layout.sharedHomePath,
effectiveHomePath,
entryName,
});
},
{ discard: true },
);
yield* ensureShadowAuthIsPrivate({
fileSystem,
sharedHomePath: layout.sharedHomePath,
effectiveHomePath,
});
});
export function codexContinuationIdentity(layout: CodexHomeLayout) {
return {
driverKind: ProviderDriverKind.make("codex"),
continuationKey: layout.continuationKey,
};
}