-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcodex.ts
More file actions
630 lines (572 loc) · 20.5 KB
/
codex.ts
File metadata and controls
630 lines (572 loc) · 20.5 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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
import { Effect } from "effect"
import * as Data from "effect/Data"
import type { PlatformError } from "@effect/platform/Error"
import * as FileSystem from "@effect/platform/FileSystem"
import * as Path from "@effect/platform/Path"
import { parseEnvEntries } from "./core/env.js"
import { resolveCodexAuthPath } from "./core/domain.js"
export interface CodexAuthStatus {
readonly path: string
readonly connected: boolean
readonly entries: number
}
export interface CodexAccountEntry {
readonly label: string
readonly path: string
readonly connected: boolean
readonly entries: number
readonly legacy: boolean
}
export class CodexAuthError extends Data.TaggedError("CodexAuthError")<{
readonly message: string
}> {}
const normalizeInput = (input: string | undefined): string => input?.trim() ?? ""
const expandHome = (input: string, home: string | undefined): string => {
if (input === "~") {
return home ?? input
}
if (input.startsWith("~/")) {
return home ? `${home}/${input.slice(2)}` : input
}
return input
}
const normalizeLabel = (value: string): string => {
const trimmed = value.trim()
if (trimmed.length === 0) {
return ""
}
const normalized = trimmed
.toLowerCase()
.replace(/[^a-z0-9]+/g, "-")
.replace(/^-+/, "")
.replace(/-+$/, "")
return normalized.length > 0 ? normalized : ""
}
const normalizeCodexLabel = (value: string | undefined): string => {
const normalized = normalizeLabel(value ?? "")
return normalized.length === 0 ? "default" : normalized
}
const authMarker = "auth.json"
const codexConfigYamlName = "config.yml"
const authFiles: ReadonlyArray<string> = [
"auth.json",
"internal_storage.json",
"version.json",
codexConfigYamlName
]
const codexConfigKey = "cli_auth_credentials_store"
const codexConfigLine = `${codexConfigKey} = "file"`
const codexApprovalPolicyKey = "approval_policy"
const codexApprovalPolicyLine = `${codexApprovalPolicyKey} = "never"`
const codexSandboxModeKey = "sandbox_mode"
const codexSandboxModeLine = `${codexSandboxModeKey} = "danger-full-access"`
const codexConfigTomlContents = `model = "gpt-5.3-codex"
model_reasoning_effort = "xhigh"
personality = "pragmatic"
${codexApprovalPolicyLine}
${codexSandboxModeLine}
web_search = "live"
${codexConfigLine}
[features]
shell_snapshot = true
collab = true
apps = true
[projects."/home/dev"]
trust_level = "trusted"
[projects."/home/dev/app"]
trust_level = "trusted"
[projects."/home/dev/.codex"]
trust_level = "trusted"
`
const codexConfigYamlContents = `model = "gpt-5.3-codex"
model_reasoning_effort = "xhigh"
personality = "pragmatic"
approval_policy = "never"
sandbox_mode = "danger-full-access"
web_search = "live"
[features]
shell_snapshot = true
collab = true
apps = true
`
const trimTrailingSlash = (value: string): string => value.replace(/\/+$/, "")
// CHANGE: derive per-project Codex auth path under a root
// WHY: avoid shared root collisions between multiple projects
// QUOTE(ТЗ): "Почему он не видит авторизацию тогда?"
// REF: user-request-2026-01-14
// SOURCE: n/a
// FORMAT THEOREM: forall r,p: path(r,p) = r + "/" + p
// PURITY: CORE
// EFFECT: Effect<string, never, never>
// INVARIANT: no trailing slash in root
// COMPLEXITY: O(1)
export const resolveProjectCodexAuthPath = (root: string, projectId: string): string =>
`${trimTrailingSlash(root)}/${projectId}`
const hasAuthMarker = (
fs: FileSystem.FileSystem,
path: Path.Path,
dirPath: string
): Effect.Effect<boolean, PlatformError> =>
fs.exists(path.join(dirPath, authMarker))
// CHANGE: resolve Codex auth source path from input and environment
// WHY: provide a deterministic import path for Codex credentials
// QUOTE(ТЗ): "Добавь подключение Codex в интеграции"
// REF: user-request-2026-01-09
// SOURCE: n/a
// FORMAT THEOREM: forall s: resolve(s) -> path | null
// PURITY: CORE
// EFFECT: Effect<string | null, never, never>
// INVARIANT: empty input falls back to CODEX_HOME or HOME/.codex
// COMPLEXITY: O(1)
export const resolveCodexSourcePath = (
input: string | undefined,
home: string | undefined,
codexHome: string | undefined
): string | null => {
const trimmed = normalizeInput(input)
if (trimmed.length > 0) {
return expandHome(trimmed, home)
}
const fallback = normalizeInput(codexHome)
if (fallback.length > 0) {
return expandHome(fallback, home)
}
return home && home.length > 0 ? `${home}/.codex` : null
}
// CHANGE: read Codex auth status from a directory
// WHY: show whether Codex credentials are available in integrations
// QUOTE(ТЗ): "Добавь подключение Codex в интеграции"
// REF: user-request-2026-01-09
// SOURCE: n/a
// FORMAT THEOREM: forall p: status(p) -> connected(p)
// PURITY: SHELL
// EFFECT: Effect<CodexAuthStatus, PlatformError, FileSystem | Path>
// INVARIANT: connected iff directory exists and has entries
// COMPLEXITY: O(n) where n = |entries|
export const readCodexAuthStatus = (
authPath: string
): Effect.Effect<CodexAuthStatus, PlatformError, FileSystem.FileSystem | Path.Path> =>
Effect.gen(function* (_) {
const fs = yield* _(FileSystem.FileSystem)
const exists = yield* _(fs.exists(authPath))
if (!exists) {
return { path: authPath, connected: false, entries: 0 }
}
const info = yield* _(fs.stat(authPath))
if (info.type !== "Directory") {
return { path: authPath, connected: false, entries: 0 }
}
const entries = yield* _(fs.readDirectory(authPath))
return {
path: authPath,
connected: entries.length > 0,
entries: entries.length
}
})
// CHANGE: list Codex auth accounts in the shared secrets root
// WHY: allow multiple Codex integrations to coexist
// QUOTE(ТЗ): "Добавь подключение Codex в интеграции"
// REF: user-request-2026-01-09
// SOURCE: n/a
// FORMAT THEOREM: forall r: list(r) = accounts(r)
// PURITY: SHELL
// EFFECT: Effect<ReadonlyArray<CodexAccountEntry>, PlatformError, FileSystem | Path>
// INVARIANT: legacy root files map to label "default"
// COMPLEXITY: O(n) where n = |entries|
export const listCodexAccounts = (
rootPath: string
): Effect.Effect<ReadonlyArray<CodexAccountEntry>, PlatformError, FileSystem.FileSystem | Path.Path> =>
Effect.gen(function* (_) {
const fs = yield* _(FileSystem.FileSystem)
const path = yield* _(Path.Path)
const exists = yield* _(fs.exists(rootPath))
if (!exists) {
return []
}
const info = yield* _(fs.stat(rootPath))
if (info.type !== "Directory") {
return []
}
const entries = yield* _(fs.readDirectory(rootPath))
let hasDefaultDir = false
const legacyAuth = yield* _(hasAuthMarker(fs, path, rootPath))
const accounts: Array<CodexAccountEntry> = []
for (const entry of entries) {
const entryPath = path.join(rootPath, entry)
const entryInfo = yield* _(fs.stat(entryPath))
if (entryInfo.type === "Directory") {
if (entry === "default") {
hasDefaultDir = true
}
const isAccount = yield* _(hasAuthMarker(fs, path, entryPath))
if (isAccount) {
const status = yield* _(readCodexAuthStatus(entryPath))
accounts.push({
label: entry,
path: entryPath,
connected: status.connected,
entries: status.entries,
legacy: false
})
}
}
}
if (legacyAuth && !hasDefaultDir) {
const status = yield* _(readCodexAuthStatus(rootPath))
accounts.unshift({
label: "default",
path: rootPath,
connected: status.connected,
entries: status.entries,
legacy: true
})
}
return accounts
})
const resolveAccountPath = (rootPath: string, label: string): string => {
const normalized = normalizeCodexLabel(label)
return `${rootPath}/${normalized}`
}
const ensureCodexConfig = (
fs: FileSystem.FileSystem,
path: Path.Path,
accountPath: string
): Effect.Effect<void, PlatformError> =>
Effect.gen(function* (_) {
const configPath = path.join(accountPath, "config.toml")
// Always overwrite to avoid copying host project trust entries.
yield* _(fs.writeFileString(configPath, codexConfigTomlContents))
})
// CHANGE: ensure Codex config.yml exists with required settings
// WHY: propagate model settings into the mounted Codex auth directory
// QUOTE(ТЗ): "Добавь конфиг config.yml"
// REF: user-request-2026-01-15
// SOURCE: n/a
// FORMAT THEOREM: forall p: ensure(p) -> exists(config.yml, p)
// PURITY: SHELL
// EFFECT: Effect<void, PlatformError, FileSystem | Path>
// INVARIANT: file is overwritten with requested contents
// COMPLEXITY: O(1)
const ensureCodexConfigYaml = (
fs: FileSystem.FileSystem,
path: Path.Path,
accountPath: string
): Effect.Effect<void, PlatformError> =>
Effect.gen(function* (_) {
const configPath = path.join(accountPath, codexConfigYamlName)
yield* _(fs.writeFileString(configPath, codexConfigYamlContents))
})
const ensureWritableDirectory = (
fs: FileSystem.FileSystem,
path: Path.Path,
dirPath: string
): Effect.Effect<boolean, PlatformError> =>
Effect.gen(function* (_) {
const ensureDir = yield* _(Effect.either(fs.makeDirectory(dirPath, { recursive: true })))
if (ensureDir._tag === "Left") {
return false
}
const probePath = path.join(dirPath, ".dg-write-test")
const writeResult = yield* _(Effect.either(fs.writeFileString(probePath, "ok")))
if (writeResult._tag === "Left") {
return false
}
yield* _(fs.remove(probePath, { force: true }))
return true
})
// CHANGE: resolve a writable Codex auth root directory
// WHY: allow Codex auth to be persisted by the host orchestrator
// QUOTE(ТЗ): "Я не могу нормально пока прокинуть авторизацию Codex"
// REF: user-request-2026-01-14
// SOURCE: n/a
// FORMAT THEOREM: forall root: writable(root) -> select(root)
// PURITY: SHELL
// EFFECT: Effect<string, CodexAuthError | PlatformError, FileSystem | Path>
// INVARIANT: returns a directory writable by the orchestrator
// COMPLEXITY: O(1)
export const resolveWritableCodexRoot = (
projectsRoot: string
): Effect.Effect<string, CodexAuthError | PlatformError, FileSystem.FileSystem | Path.Path> =>
Effect.gen(function* (_) {
const fs = yield* _(FileSystem.FileSystem)
const path = yield* _(Path.Path)
const baseRoot = path.resolve(resolveCodexAuthPath(projectsRoot))
const baseWritable = yield* _(ensureWritableDirectory(fs, path, baseRoot))
if (baseWritable) {
return baseRoot
}
const fallbackRoot = `${baseRoot}-host`
const fallbackWritable = yield* _(ensureWritableDirectory(fs, path, fallbackRoot))
if (fallbackWritable) {
return fallbackRoot
}
const envRoot = resolveCodexSourcePath(
undefined,
process.env["HOME"],
process.env["CODEX_HOME"]
)
if (envRoot) {
const resolvedEnvRoot = path.resolve(envRoot)
const envWritable = yield* _(ensureWritableDirectory(fs, path, resolvedEnvRoot))
if (envWritable) {
return resolvedEnvRoot
}
}
return yield* _(
Effect.fail(
new CodexAuthError({
message: `Codex auth root is not writable: ${baseRoot}`
})
)
)
})
// CHANGE: prepare a Codex CLI auth directory for a labeled account
// WHY: ensure device auth writes credentials into a dedicated CODEX_HOME
// QUOTE(ТЗ): "Мне нужна прямо нативная интеграция с Codex"
// REF: user-request-2026-01-10
// SOURCE: n/a
// FORMAT THEOREM: forall r,l: prepare(r,l) -> exists(dir(r,l))
// PURITY: SHELL
// EFFECT: Effect<string, PlatformError, FileSystem | Path>
// INVARIANT: config.toml contains cli_auth_credentials_store = "file"
// COMPLEXITY: O(n) where n = |config|
export const prepareCodexAccountDir = (
rootPath: string,
label: string
): Effect.Effect<string, PlatformError, FileSystem.FileSystem | Path.Path> =>
Effect.gen(function* (_) {
const fs = yield* _(FileSystem.FileSystem)
const path = yield* _(Path.Path)
const accountPath = resolveAccountPath(rootPath, label)
yield* _(fs.makeDirectory(accountPath, { recursive: true }))
yield* _(ensureCodexConfig(fs, path, accountPath))
yield* _(ensureCodexConfigYaml(fs, path, accountPath))
return accountPath
})
// CHANGE: import Codex auth cache into a labeled integration directory
// WHY: allow multiple Codex integrations in the orchestrator
// QUOTE(ТЗ): "Добавь подключение Codex в интеграции"
// REF: user-request-2026-01-09
// SOURCE: n/a
// FORMAT THEOREM: forall s,l: import(s,l) -> exists(account(l))
// PURITY: SHELL
// EFFECT: Effect<void, CodexAuthError | PlatformError, FileSystem | Path>
// INVARIANT: target directory is replaced
// COMPLEXITY: O(n) where n = |files|
export const importCodexAuthDir = (
sourcePath: string,
rootPath: string,
label: string
): Effect.Effect<void, CodexAuthError | PlatformError, FileSystem.FileSystem | Path.Path> =>
Effect.gen(function* (_) {
const fs = yield* _(FileSystem.FileSystem)
const path = yield* _(Path.Path)
const exists = yield* _(fs.exists(sourcePath))
if (!exists) {
yield* _(
Effect.fail(
new CodexAuthError({ message: `Source not found: ${sourcePath}` })
)
)
return
}
const info = yield* _(fs.stat(sourcePath))
if (info.type !== "Directory") {
yield* _(
Effect.fail(
new CodexAuthError({ message: `Source is not a directory: ${sourcePath}` })
)
)
return
}
const target = resolveAccountPath(rootPath, label)
yield* _(fs.makeDirectory(path.dirname(target), { recursive: true }))
yield* _(fs.remove(target, { recursive: true, force: true }))
yield* _(fs.makeDirectory(target, { recursive: true }))
for (const file of authFiles) {
const src = path.join(sourcePath, file)
const dst = path.join(target, file)
const exists = yield* _(fs.exists(src))
if (exists) {
yield* _(fs.copyFile(src, dst))
}
}
yield* _(ensureCodexConfig(fs, path, target))
yield* _(ensureCodexConfigYaml(fs, path, target))
})
// CHANGE: copy Codex auth cache to a destination directory
// WHY: attach a selected Codex integration to a project
// QUOTE(ТЗ): "хочу подключить 10 Codex интеграций"
// REF: user-request-2026-01-09
// SOURCE: n/a
// FORMAT THEOREM: forall s,d: copy(s,d) -> exists(d)
// PURITY: SHELL
// EFFECT: Effect<void, CodexAuthError | PlatformError, FileSystem | Path>
// INVARIANT: destination directory is replaced
// COMPLEXITY: O(n) where n = |files|
export const copyCodexAuthDir = (
sourcePath: string,
destPath: string
): Effect.Effect<void, CodexAuthError | PlatformError, FileSystem.FileSystem | Path.Path> =>
Effect.gen(function* (_) {
const fs = yield* _(FileSystem.FileSystem)
const path = yield* _(Path.Path)
const exists = yield* _(fs.exists(sourcePath))
if (!exists) {
yield* _(
Effect.fail(
new CodexAuthError({ message: `Source not found: ${sourcePath}` })
)
)
return
}
const info = yield* _(fs.stat(sourcePath))
if (info.type !== "Directory") {
yield* _(
Effect.fail(
new CodexAuthError({ message: `Source is not a directory: ${sourcePath}` })
)
)
return
}
yield* _(fs.remove(destPath, { recursive: true, force: true }))
yield* _(fs.makeDirectory(path.dirname(destPath), { recursive: true }))
yield* _(fs.makeDirectory(destPath, { recursive: true }))
for (const file of authFiles) {
const src = path.join(sourcePath, file)
const dst = path.join(destPath, file)
const exists = yield* _(fs.exists(src))
if (exists) {
yield* _(fs.copyFile(src, dst))
}
}
yield* _(ensureCodexConfig(fs, path, destPath))
yield* _(ensureCodexConfigYaml(fs, path, destPath))
})
// CHANGE: remove a Codex integration by label
// WHY: allow deleting an individual Codex account
// QUOTE(ТЗ): "хочу подключить 10 Codex интеграций"
// REF: user-request-2026-01-09
// SOURCE: n/a
// FORMAT THEOREM: forall r,l: remove(r,l) -> not exists(account(l))
// PURITY: SHELL
// EFFECT: Effect<void, PlatformError, FileSystem | Path>
// INVARIANT: only the labeled account is removed
// COMPLEXITY: O(n) where n = |entries|
export const removeCodexAccount = (
rootPath: string,
label: string
): Effect.Effect<void, PlatformError, FileSystem.FileSystem | Path.Path> =>
Effect.gen(function* (_) {
const fs = yield* _(FileSystem.FileSystem)
const path = yield* _(Path.Path)
const normalized = normalizeCodexLabel(label)
const target = resolveAccountPath(rootPath, normalized)
const targetExists = yield* _(fs.exists(target))
if (targetExists) {
yield* _(fs.remove(target, { recursive: true, force: true }))
return
}
if (normalized !== "default") {
return
}
const rootExists = yield* _(fs.exists(rootPath))
if (!rootExists) {
return
}
const rootInfo = yield* _(fs.stat(rootPath))
if (rootInfo.type !== "Directory") {
return
}
const hasLegacy = yield* _(hasAuthMarker(fs, path, rootPath))
if (!hasLegacy) {
return
}
const entries = yield* _(fs.readDirectory(rootPath))
for (const entry of entries) {
const entryPath = path.join(rootPath, entry)
const entryInfo = yield* _(fs.stat(entryPath))
if (entryInfo.type === "Directory") {
const isAccount = yield* _(hasAuthMarker(fs, path, entryPath))
if (!isAccount) {
yield* _(fs.remove(entryPath, { recursive: true, force: true }))
}
} else {
yield* _(fs.remove(entryPath, { recursive: true, force: true }))
}
}
})
// CHANGE: clear Codex auth cache for a path
// WHY: allow disconnecting Codex from a project
// QUOTE(ТЗ): "хочу подключить 10 Codex интеграций"
// REF: user-request-2026-01-09
// SOURCE: n/a
// FORMAT THEOREM: forall p: clear(p) -> empty(p)
// PURITY: SHELL
// EFFECT: Effect<void, PlatformError, FileSystem | Path>
// INVARIANT: directory exists after clear
// COMPLEXITY: O(n) where n = |files|
export const clearCodexAuthDir = (
destPath: string
): Effect.Effect<void, PlatformError, FileSystem.FileSystem | Path.Path> =>
Effect.gen(function* (_) {
const fs = yield* _(FileSystem.FileSystem)
yield* _(fs.remove(destPath, { recursive: true, force: true }))
yield* _(fs.makeDirectory(destPath, { recursive: true }))
})
// CHANGE: locate a Codex integration path by label
// WHY: map project selection to a concrete auth directory
// QUOTE(ТЗ): "хочу подключить 10 Codex интеграций"
// REF: user-request-2026-01-09
// SOURCE: n/a
// FORMAT THEOREM: forall r,l: find(r,l) -> path | null
// PURITY: SHELL
// EFFECT: Effect<string | null, PlatformError, FileSystem | Path>
// INVARIANT: legacy default is resolved to root when present
// COMPLEXITY: O(n) where n = |entries|
export const findCodexAccountPath = (
rootPath: string,
label: string
): Effect.Effect<string | null, PlatformError, FileSystem.FileSystem | Path.Path> =>
Effect.gen(function* (_) {
const fs = yield* _(FileSystem.FileSystem)
const path = yield* _(Path.Path)
const normalized = normalizeCodexLabel(label)
const target = resolveAccountPath(rootPath, normalized)
const targetExists = yield* _(fs.exists(target))
if (targetExists) {
const isAccount = yield* _(hasAuthMarker(fs, path, target))
return isAccount ? target : null
}
if (normalized !== "default") {
return null
}
const rootExists = yield* _(fs.exists(rootPath))
if (!rootExists) {
return null
}
const info = yield* _(fs.stat(rootPath))
if (info.type !== "Directory") {
return null
}
const legacy = yield* _(hasAuthMarker(fs, path, rootPath))
return legacy ? rootPath : null
})
// CHANGE: resolve Codex label stored in a project env file
// WHY: show which Codex account is active for a project
// QUOTE(ТЗ): "хочу подключить 10 Codex интеграций"
// REF: user-request-2026-01-09
// SOURCE: n/a
// FORMAT THEOREM: forall env: label(env) -> string | null
// PURITY: CORE
// EFFECT: Effect<string | null, never, never>
// INVARIANT: empty values map to null
// COMPLEXITY: O(n) where n = |lines|
export const resolveProjectCodexLabel = (envText: string): string | null => {
const entry = parseEnvEntries(envText).find((item) => item.key === "CODEX_AUTH_LABEL")
if (!entry) {
return null
}
const trimmed = entry.value.trim()
return trimmed.length > 0 ? trimmed : null
}