triflux 10.17.4 → 10.18.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. package/CLAUDE.md +170 -0
  2. package/LICENSE +21 -21
  3. package/bin/triflux.mjs +137 -0
  4. package/hub/account-broker.mjs +32 -0
  5. package/hub/cli-adapter-base.mjs +20 -3
  6. package/hub/codex-adapter.mjs +38 -2
  7. package/hub/gemini-adapter.mjs +8 -1
  8. package/hub/lib/trace-recorder.mjs +153 -0
  9. package/hub/server.mjs +145 -29
  10. package/hub/team/conductor.mjs +68 -6
  11. package/hub/team/swarm-hypervisor.mjs +88 -2
  12. package/hub/workers/codex-mcp.mjs +29 -0
  13. package/hub/workers/factory.mjs +11 -9
  14. package/hud/providers/claude.mjs +105 -9
  15. package/package.json +23 -67
  16. package/references/cli-parameter-reference.md +240 -0
  17. package/references/codex-plugin-cc-analysis.md +706 -0
  18. package/references/codex-plugin-cc-code-patterns.md +468 -0
  19. package/scripts/__tests__/mcp-guard-engine-http-headers.test.mjs +177 -0
  20. package/scripts/__tests__/mcp-guard-engine-proactive-sync.test.mjs +237 -0
  21. package/scripts/__tests__/mcp-guard-engine-sync-http-headers.test.mjs +226 -0
  22. package/scripts/__tests__/mcp-guard-engine-watch-http-headers.test.mjs +138 -0
  23. package/scripts/__tests__/release-governance.test.mjs +6 -1
  24. package/scripts/completions/tfx.bash +47 -47
  25. package/scripts/completions/tfx.fish +44 -44
  26. package/scripts/completions/tfx.zsh +83 -83
  27. package/scripts/lib/codex-recovery.sh +50 -0
  28. package/scripts/lib/doctor-env-checks.mjs +121 -0
  29. package/scripts/lib/mcp-guard-engine.mjs +560 -36
  30. package/scripts/mcp-safety-guard.mjs +41 -23
  31. package/scripts/release/prepare.mjs +21 -12
  32. package/scripts/setup.mjs +10 -3
  33. package/scripts/tfx-route-worker.mjs +1 -1
  34. package/scripts/tfx-route.sh +174 -47
  35. package/scripts/token-snapshot.mjs +4 -2
  36. package/.claude-plugin/marketplace.json +0 -34
  37. package/.claude-plugin/plugin.json +0 -22
  38. package/config/mcp-registry.json +0 -30
  39. package/skills/.omc/state/agent-replay-8f0e10a9-9693-4410-96f5-a6b07e8ed995.jsonl +0 -1
  40. package/skills/.omc/state/idle-notif-cooldown.json +0 -3
  41. package/skills/.omc/state/last-tool-error.json +0 -7
  42. package/skills/.omc/state/subagent-tracking.json +0 -7
  43. package/skills/tfx-remote-spawn/references/hosts.json +0 -41
  44. package/skills/tfx-remote-spawn/references/hosts.json.bak.20260425_040814 +0 -16
  45. package/tui/codex-profile.mjs +0 -459
  46. package/tui/core.mjs +0 -266
  47. package/tui/doctor.mjs +0 -375
  48. package/tui/gemini-profile.mjs +0 -299
  49. package/tui/monitor-data.mjs +0 -152
  50. package/tui/monitor.mjs +0 -333
  51. package/tui/setup.mjs +0 -599
package/CLAUDE.md ADDED
@@ -0,0 +1,170 @@
1
+ # triflux — Claude Code 운영 가이드
2
+
3
+ <core-systems>
4
+ ## 핵심 스킬 시스템 (항상 인지)
5
+
6
+ 이 프로젝트는 3개의 스킬 시스템을 동시에 사용한다. 어떤 작업이든 해당 시스템의 스킬이 있는지 먼저 확인한다.
7
+
8
+ | 시스템 | 접두사 | 용도 | 스킬 수 |
9
+ |--------|--------|------|---------|
10
+ | **triflux** | `/tfx-*` | CLI 라우팅, 멀티모델 오케스트레이션, 스웜, 원격 실행 | ~40개 |
11
+ | **gstack** | `/` (접두사 없음) | QA, ship, investigate, design, review, checkpoint | ~35개 |
12
+ | **omc** | `/oh-my-claudecode:*` | autopilot, ralph, team, ultrawork, ccg | ~25개 |
13
+
14
+ 스킬을 모르면 자연어 라우팅(`.claude/rules/tfx-routing.md`)으로 자동 매핑된다.
15
+ 세션 종료 전 메모리 파일이 3개+ 변경됐으면 `/memory-hygiene` 제안을 검토한다.
16
+ </core-systems>
17
+
18
+ <psmux-wt>
19
+ ## psmux/WT 규칙
20
+
21
+ psmux 세션·WT 패인을 생성/조작/정리할 때 `tfx-psmux-rules` 스킬을 참조한다.
22
+ WT 프리징 방지: exit → sleep 2 → kill 순서. 바로 kill하지 않는다.
23
+
24
+ ### wt.exe → wt-manager 경유
25
+
26
+ safety-guard가 `wt.exe`, `wt new-tab`, `wt split-pane`, `Start-Process wt`를 차단한다.
27
+ `hub/team/wt-manager.mjs`의 API를 사용한다.
28
+
29
+ | 용도 | API |
30
+ |------|-----|
31
+ | 새 탭 | `createTab({ title, command, profile, cwd })` |
32
+ | 패인 분할 | `splitPane({ direction: 'H'\|'V', title, command })` |
33
+ | 다중 배치 | `applySplitLayout([{ title, command, direction }])` |
34
+ | 탭 정리 | `closeTab(title)` / `closeStale({ olderThanMs, titlePattern })` |
35
+
36
+ 차단과 대안은 항상 쌍으로 존재해야 한다. 차단만 추가하고 대안을 안 만들면 데드락.
37
+
38
+ ### raw `psmux kill-session` → psmux wrapper 경유
39
+
40
+ safety-guard가 raw `psmux kill-session`을 차단한다.
41
+ 세션 정리는 `hub/team/psmux.mjs` 공개 API 또는 internal wrapper로 우회한다.
42
+
43
+ | 용도 | API / 래퍼 |
44
+ |------|------------|
45
+ | 세션 조회 | `listSessions({ filterTitle?, olderThanMs? })` |
46
+ | title prefix / regex kill | `killSessionByTitle(titlePattern)` |
47
+ | stale idle 세션 정리 | `pruneStale({ olderThanMs, dryRun })` |
48
+ | Bash 훅 우회용 래퍼 | `node hub/team/psmux.mjs --internal kill-by-title <prefix\|/regex/>` |
49
+
50
+ ### psmux에서 Codex 실행
51
+
52
+ | 방식 | 동작 | 이유 |
53
+ |------|------|------|
54
+ | `codex` (interactive) | 불가 | psmux에서 TTY를 못 잡음 |
55
+ | `codex < prompt.md` | 불가 | "stdin is not a terminal" |
56
+ | `codex exec "$(cat prompt.md)" -s danger-full-access --dangerously-bypass-approvals-and-sandbox` | 사용 | 유일한 안전 경로 |
57
+
58
+ `codex exec`는 config.toml `approval_mode`를 무시하므로 `--dangerously-bypass-approvals-and-sandbox` 필수.
59
+ `-s` 유효값: read-only, workspace-write, danger-full-access.
60
+ </psmux-wt>
61
+
62
+ <codex-config>
63
+ ## Codex config.toml
64
+
65
+ config.toml에 이미 설정된 값은 CLI 플래그로 중복 지정하지 않는다.
66
+
67
+ | config.toml에 있으면 | CLI에서 생략 |
68
+ |---------------------|-------------|
69
+ | `approval_mode = "auto"` | `-a`, `--full-auto` |
70
+ | `sandbox = "workspace-write"` | `-s`, `--full-auto` |
71
+
72
+ 안전 패턴: config.toml에 기본값을 두고, CLI에서는 `--profile` 선택만 한다.
73
+ </codex-config>
74
+
75
+ <account-broker>
76
+ ## AccountBroker (계정 브로커)
77
+
78
+ conductor, headless, swarm-hypervisor가 하나의 AccountBroker 싱글턴을 공유한다.
79
+
80
+ | 항목 | 설명 |
81
+ |------|------|
82
+ | 계정별 CircuitBreaker | 장애 격리 — 한 계정 오류가 다른 계정에 전파되지 않음 |
83
+ | busy 플래그 | 동일 계정 이중 임대(double-lease) 방지 |
84
+ | `/broker/reload` | 장시간 세션 중 accounts.json 핫리로드 |
85
+ | EventEmitter 이벤트 | `lease`, `release`, `cooldown`, `tierFallback`, `circuitOpen`, `circuitClose`, `noAvailableAccounts` — HUD 연동용 |
86
+ </account-broker>
87
+
88
+ <remote>
89
+ ## 원격 실행
90
+
91
+ ### 스킬 구분
92
+
93
+ | 스킬 | 대상 | 방식 |
94
+ |------|------|------|
95
+ | tfx-codex-swarm | 로컬 전용 | 로컬 worktree + psmux |
96
+ | tfx-remote-spawn | Claude Code 원격 | SSH → Claude Code 세션 → 내부 tfx 라우팅 |
97
+
98
+ codex를 SSH 너머로 직접 실행하지 않는다. config.toml 충돌 + TTY 문제.
99
+ 원격에서 codex가 필요하면: remote-spawn → Claude Code → Claude가 내부에서 codex 호출.
100
+
101
+ ### SSH 패턴
102
+
103
+ hosts.json `os` 필드로 대상 셸을 판단한다. safety-guard도 이 필드를 참조.
104
+
105
+ | 대상 OS | 셸 | 패턴 |
106
+ |---------|-----|------|
107
+ | windows | PowerShell | scp + `pwsh -File` 필수. `$var` → `$env:VAR`, `2>/dev/null` → `2>$null` |
108
+ | darwin | zsh | 인라인 가능. brew PATH 주의 (`/opt/homebrew/bin`) |
109
+ | linux | bash | 인라인 가능. 표준 POSIX |
110
+
111
+ - `~` → `$HOME` 변환은 모든 OS 공통
112
+ </remote>
113
+
114
+ <headless-retrieval>
115
+ ## Headless 결과 회수
116
+
117
+ background로 실행한 headless 결과는 **반드시 task-notification 완료 후** 읽는다.
118
+
119
+ | 패턴 | 올바름 | 이유 |
120
+ |------|--------|------|
121
+ | task-notification 후 output 파일 읽기 | YES | 프로세스 종료 = 워커 전부 완료 |
122
+ | task-notification 전 output 파일 tail | NO | 시작 메시지만 보이고 "실패"로 오진 |
123
+ | psmux capture-pane으로 중간 체크 | NO | 워커 진행 중이면 빈 화면일 수 있음 |
124
+
125
+ 완료 마커: `=== HEADLESS_COMPLETE succeeded=N failed=N total=N ===`
126
+ 워커 상세: `$TMPDIR/tfx-headless/{sessionName}-worker-N.txt`
127
+ </headless-retrieval>
128
+
129
+ <cross-review>
130
+ ## 교차 검증
131
+
132
+ - Claude 작성 코드 → Codex 리뷰
133
+ - Codex 작성 코드 → Claude 리뷰
134
+ - 동일 모델 self-approve 하지 않는다
135
+ - git commit 전 미검증 파일 감지 시 nudge
136
+ </cross-review>
137
+
138
+ <session-context>
139
+ ## 맥락 이탈 판단
140
+
141
+ 현재 세션 맥락과 무관한 요청이 감지되면 psmux 격리를 제안한다.
142
+
143
+ | 확신도 | 신호 | 행동 |
144
+ |--------|------|------|
145
+ | 확실 | "새 탭", "별도로", "새 세션" | 바로 psmux spawn |
146
+ | 높음 | 다른 프로젝트/스택 언급 | 분리 제안 |
147
+ | 중간 | 작업 유형 전환 | 분리 제안 + 현재 세션 옵션 |
148
+ | 낮음 | 현재 작업 연장 | 세션 유지 |
149
+ </session-context>
150
+
151
+ ## 세부 규칙은 `.claude/rules/` 참조
152
+
153
+ | 파일 | 내용 |
154
+ |------|------|
155
+ | `.claude/rules/tfx-routing.md` | 자연어 → 스킬 라우팅, CLI 라우팅 Layer 1~3, 충돌 해소 |
156
+ | `.claude/rules/tfx-execution-skill-map.md` | tfx-auto / multi / swarm 실행 엔진 매핑, 격리 기준, 안티패턴 |
157
+ | `.claude/rules/tfx-autoplan-principles.md` | gstack autoplan의 6 decision principles, phase 우선순위, 충돌 해소 규칙 추출본 |
158
+ | `.claude/rules/tfx-update-logic.md` | triflux / OMC / gstack / Codex / Gemini 업데이트 로직 |
159
+ | `.claude/rules/tfx-stack-coexistence.md` | gstack / superpowers / triflux 공존 원칙, 레이어 분리, 의존 방향, 충돌 해소 |
160
+ | `.claude/rules/tfx-mirror-policy.md` | packages/ 3-layer mirror 정책 (core 단순 cp / remote import 변환 / triflux byte-identical), tests 제외 룰, drift 차단 |
161
+
162
+ Claude Code는 `.claude/rules/*.md` 를 자동 로드한다. Codex CLI는 `@import` 미지원이므로 필요 시 `AGENTS.md` 를 독립 유지한다.
163
+
164
+ ## GBrain Configuration (configured by /setup-gbrain)
165
+ - Engine: pglite
166
+ - Config file: ~/.gbrain/config.json (mode 0600)
167
+ - Setup date: 2026-04-25
168
+ - MCP registered: yes (user scope, absolute path)
169
+ - Memory sync: artifacts-only (repo: github.com/tellang/gstack-brain-tellang)
170
+ - Current repo policy: read-write (github.com/tellang/triflux)
package/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2025 tellang
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2025 tellang
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/bin/triflux.mjs CHANGED
@@ -43,6 +43,12 @@ import {
43
43
  ensureTfxSection,
44
44
  getLatestRoutingTable,
45
45
  } from "../scripts/claudemd-sync.mjs";
46
+ import {
47
+ applyPluginRootHookFallbacks,
48
+ commandExists,
49
+ findPluginRootHookIssues,
50
+ inspectMacTimeoutDependency,
51
+ } from "../scripts/lib/doctor-env-checks.mjs";
46
52
  import { ensureGeminiProfiles } from "../scripts/lib/gemini-profiles.mjs";
47
53
  import { serializeHandoff } from "../scripts/lib/handoff.mjs";
48
54
  import {
@@ -2041,6 +2047,60 @@ async function cmdDoctor(options = {}) {
2041
2047
  for (const target of SYNC_MAP) {
2042
2048
  syncFile(target.src, target.dst, target.label);
2043
2049
  }
2050
+ const macTimeoutForFix = inspectMacTimeoutDependency();
2051
+ if (!macTimeoutForFix.ok) {
2052
+ if (commandExists("brew")) {
2053
+ if (process.stdin.isTTY && process.stdout.isTTY) {
2054
+ info("macOS GNU timeout 부재: coreutils 설치 가능");
2055
+ process.stdout.write(
2056
+ " brew install coreutils 실행할까요? [y/N] ",
2057
+ );
2058
+ let answer = "";
2059
+ try {
2060
+ const buf = Buffer.alloc(128);
2061
+ const n = readSync(0, buf, 0, 128);
2062
+ answer = buf.toString("utf8", 0, n).trim().toLowerCase();
2063
+ } catch {
2064
+ answer = "";
2065
+ }
2066
+ if (answer.startsWith("y")) {
2067
+ try {
2068
+ execFileSync("brew", ["install", "coreutils"], {
2069
+ stdio: "inherit",
2070
+ timeout: 600000,
2071
+ windowsHide: true,
2072
+ });
2073
+ report.actions.push({
2074
+ type: "install",
2075
+ name: "coreutils",
2076
+ status: "ok",
2077
+ });
2078
+ ok("coreutils 설치 완료");
2079
+ } catch (error) {
2080
+ report.actions.push({
2081
+ type: "install",
2082
+ name: "coreutils",
2083
+ status: "failed",
2084
+ message: error.message,
2085
+ });
2086
+ warn(
2087
+ `coreutils 설치 실패: ${renderErrorMessage(error.message)}`,
2088
+ );
2089
+ }
2090
+ } else {
2091
+ info("건너뜀: brew install coreutils");
2092
+ }
2093
+ } else {
2094
+ warn(
2095
+ "macOS GNU timeout 부재 — 비대화형 모드에서는 자동 설치하지 않습니다.",
2096
+ );
2097
+ info("수동 설치: brew install coreutils");
2098
+ }
2099
+ } else {
2100
+ warn("macOS GNU timeout 부재 — Homebrew를 찾지 못했습니다.");
2101
+ info("Homebrew 설치 후: brew install coreutils");
2102
+ }
2103
+ }
2044
2104
  {
2045
2105
  const claudeGuide = ensureGlobalClaudeRoutingSection(CLAUDE_DIR);
2046
2106
  if (
@@ -2197,6 +2257,34 @@ async function cmdDoctor(options = {}) {
2197
2257
  issues++;
2198
2258
  }
2199
2259
 
2260
+ // macOS GNU timeout/coreutils
2261
+ section("macOS timeout");
2262
+ const macTimeout = inspectMacTimeoutDependency();
2263
+ if (macTimeout.status === "skipped") {
2264
+ addDoctorCheck(report, {
2265
+ name: "macos-timeout",
2266
+ status: "skipped",
2267
+ platform: process.platform,
2268
+ });
2269
+ info("macOS 아님 — 건너뜀");
2270
+ } else if (macTimeout.ok) {
2271
+ addDoctorCheck(report, {
2272
+ name: "macos-timeout",
2273
+ status: "ok",
2274
+ provider: macTimeout.provider,
2275
+ });
2276
+ ok(`timeout provider: ${macTimeout.provider}`);
2277
+ } else {
2278
+ addDoctorCheck(report, {
2279
+ name: "macos-timeout",
2280
+ status: "missing",
2281
+ fix: macTimeout.fix,
2282
+ });
2283
+ warn("GNU timeout/gtimeout 미설치");
2284
+ info("수정: brew install coreutils");
2285
+ issues++;
2286
+ }
2287
+
2200
2288
  // 2. HUD
2201
2289
  section("HUD");
2202
2290
  const hud = join(CLAUDE_DIR, "hud", "hud-qos-status.mjs");
@@ -3813,6 +3901,55 @@ async function cmdDoctor(options = {}) {
3813
3901
  }
3814
3902
 
3815
3903
  if (settings) {
3904
+ let pluginRootHookIssues = findPluginRootHookIssues(settings);
3905
+ if (pluginRootHookIssues.length > 0 && fix) {
3906
+ const fallbackResult = applyPluginRootHookFallbacks(settings, {
3907
+ pluginRoot: PKG_ROOT,
3908
+ });
3909
+ if (fallbackResult.changed) {
3910
+ writeFileSync(
3911
+ settingsPath,
3912
+ JSON.stringify(settings, null, 2) + "\n",
3913
+ "utf8",
3914
+ );
3915
+ ok(
3916
+ `PLUGIN_ROOT fallback ${fallbackResult.count}개 hook command에 적용됨`,
3917
+ );
3918
+ try {
3919
+ settings = JSON.parse(readFileSync(settingsPath, "utf8"));
3920
+ pluginRootHookIssues = findPluginRootHookIssues(settings);
3921
+ } catch (error) {
3922
+ warn(`PLUGIN_ROOT fallback 재검증 실패: ${error.message}`);
3923
+ }
3924
+ }
3925
+ }
3926
+
3927
+ addDoctorCheck(report, {
3928
+ name: "hook-plugin-root",
3929
+ status:
3930
+ pluginRootHookIssues.length === 0 ? "ok" : "missing-fallback",
3931
+ count: pluginRootHookIssues.length,
3932
+ examples: pluginRootHookIssues.slice(0, 3).map((issue) => ({
3933
+ event: issue.event,
3934
+ reason: issue.reason,
3935
+ command: issue.command,
3936
+ })),
3937
+ ...(pluginRootHookIssues.length > 0
3938
+ ? { fix: "tfx doctor --fix 또는 tfx setup" }
3939
+ : {}),
3940
+ });
3941
+ if (pluginRootHookIssues.length === 0) {
3942
+ ok("PLUGIN_ROOT hook fallback 확인됨");
3943
+ } else {
3944
+ warn(
3945
+ `PLUGIN_ROOT fallback 없는 hook ${pluginRootHookIssues.length}개 감지`,
3946
+ );
3947
+ info(
3948
+ "영향: npm 단독 설치에서 /hooks/... 경로로 붕괴할 수 있습니다.",
3949
+ );
3950
+ issues += pluginRootHookIssues.length;
3951
+ }
3952
+
3816
3953
  let coverage = computeHookCoverage(settings, managedHooks);
3817
3954
 
3818
3955
  if (coverage.missing.length > 0 && fix) {
@@ -796,6 +796,19 @@ class AccountBroker extends EventEmitter {
796
796
  }));
797
797
  }
798
798
 
799
+ // ── disabled marker ───────────────────────────────────────────
800
+
801
+ /**
802
+ * True when the broker holds zero accounts. Used by adapters to distinguish
803
+ * "broker exists but is empty (disable env / no accounts)" from "broker has
804
+ * accounts but lease() returned null (all busy/cooldown/circuit)" — the
805
+ * former should fall back to default ~/.codex/auth.json instead of returning
806
+ * circuit_open.
807
+ */
808
+ get isDisabled() {
809
+ return this.#state.size === 0;
810
+ }
811
+
799
812
  // ── nextAvailableEta ──────────────────────────────────────────
800
813
 
801
814
  nextAvailableEta(provider) {
@@ -849,7 +862,22 @@ function loadConfig() {
849
862
 
850
863
  // ── Singleton ────────────────────────────────────────────────────
851
864
 
865
+ function isBrokerDisabledByEnv() {
866
+ const flag = process.env.TFX_DISABLE_ACCOUNT_BROKER;
867
+ return flag === "1" || flag === "true";
868
+ }
869
+
870
+ function createEmptyBroker() {
871
+ return new AccountBroker({ codex: [], gemini: [] });
872
+ }
873
+
852
874
  function createBroker() {
875
+ if (isBrokerDisabledByEnv()) {
876
+ // Empty broker: lease() always returns null → callers fall back to default
877
+ // single-account path (~/.codex/auth.json). Avoids per-account multiplexing
878
+ // race conditions while keeping the AccountBroker API surface intact.
879
+ return createEmptyBroker();
880
+ }
853
881
  const config = loadConfig();
854
882
  if (!config) return null;
855
883
  try {
@@ -862,6 +890,10 @@ function createBroker() {
862
890
 
863
891
  /** Re-read config and replace the module-level singleton. ESM live binding propagates to all importers. */
864
892
  function reloadBroker() {
893
+ if (isBrokerDisabledByEnv()) {
894
+ broker = createEmptyBroker();
895
+ return { ok: true, broker, disabled: true };
896
+ }
865
897
  const config = loadConfig();
866
898
  if (!config) return { ok: false, error: "Config not found or invalid" };
867
899
  try {
@@ -241,8 +241,14 @@ export async function executeWithCircuitBroker({
241
241
 
242
242
  // access broker as live binding property (not destructured) so reloadBroker() propagates
243
243
  const hasBroker = brokerMod.broker != null;
244
- const lease = hasBroker ? brokerMod.broker.lease({ provider }) : null;
245
- if (hasBroker && !lease) {
244
+ // Empty broker (TFX_DISABLE_ACCOUNT_BROKER=1 or zero accounts) must not
245
+ // gate execution. lease() is null because there are no accounts to lease,
246
+ // not because all accounts are busy/cooldown/circuit. Treat it like
247
+ // hasBroker=false and fall through to the default auth path.
248
+ const brokerDisabled = hasBroker && brokerMod.broker.isDisabled === true;
249
+ const effectiveBroker = hasBroker && !brokerDisabled;
250
+ const lease = effectiveBroker ? brokerMod.broker.lease({ provider }) : null;
251
+ if (effectiveBroker && !lease) {
246
252
  return createResult(false, { fellBack: true, failureMode: "circuit_open" });
247
253
  }
248
254
 
@@ -263,11 +269,15 @@ export async function executeWithCircuitBroker({
263
269
  try {
264
270
  lastResult = await withRetry(
265
271
  async () => {
272
+ // PRD A1: lease 메타데이터를 runFn 에 전달해서 adapter 가 실제 spawn 시
273
+ // 해당 account 의 authFile/env/profile 을 적용할 수 있게 한다. lease=null
274
+ // 이면 adapter 는 default 경로 (단일 ~/.codex/auth.json) 로 동작한다.
266
275
  const result = await runFn(
267
276
  opts.prompt || "",
268
277
  opts.workdir || process.cwd(),
269
278
  preflight,
270
279
  attempts[attemptIndex],
280
+ lease,
271
281
  );
272
282
  const current = {
273
283
  ...result,
@@ -366,7 +376,14 @@ export async function runProcess(command, workdir, timeout, opts = {}) {
366
376
  let child;
367
377
 
368
378
  try {
369
- child = spawn(command, { cwd: workdir, shell: true, windowsHide: true });
379
+ // PRD A1: opts.spawnEnv 있으면 env spawn (lease 의 authFile/env 적용).
380
+ // undefined 면 spawn 의 default 동작 (부모 process env inherit) 유지.
381
+ child = spawn(command, {
382
+ cwd: workdir,
383
+ shell: true,
384
+ windowsHide: true,
385
+ env: opts.spawnEnv,
386
+ });
370
387
  } catch (error) {
371
388
  return createResult(false, {
372
389
  stderr: String(error?.message || error),
@@ -133,7 +133,7 @@ export function buildExecArgs(opts = {}) {
133
133
 
134
134
  // ── Codex execution ─────────────────────────────────────────────
135
135
 
136
- async function runCodex(prompt, workdir, preflight, attempt) {
136
+ async function runCodex(prompt, workdir, preflight, attempt, lease) {
137
137
  const dir = join(tmpdir(), "triflux-codex-exec");
138
138
  mkdirSync(dir, { recursive: true });
139
139
  const resultFile = join(
@@ -142,7 +142,7 @@ async function runCodex(prompt, workdir, preflight, attempt) {
142
142
  );
143
143
  const command = commandWithOverrides(
144
144
  buildExecCommand(prompt, resultFile, {
145
- profile: attempt.profile,
145
+ profile: lease?.profile ?? attempt.profile,
146
146
  skipGitRepoCheck: true,
147
147
  sandboxBypass: attempt.forceBypass,
148
148
  }),
@@ -150,12 +150,48 @@ async function runCodex(prompt, workdir, preflight, attempt) {
150
150
  preflight.codexPath,
151
151
  buildOverrides(attempt.requested, attempt.excluded),
152
152
  );
153
+ // PRD A1 — lease 메타데이터를 spawn env 에 적용한다. lease.authFile 이 있으면
154
+ // 해당 파일이 위치한 디렉토리를 CODEX_HOME 으로 export 해서 codex CLI 가 그
155
+ // account 의 auth.json 을 사용하게 한다. lease 가 null 이면 default ~/.codex
156
+ // 동작 유지 (회귀 없음). lease.env 는 추가 환경변수 (provider 고정 등) 주입용.
157
+ const spawnEnv = lease ? buildLeaseSpawnEnv(lease) : undefined;
153
158
  return runProcess(command, workdir, attempt.timeout, {
154
159
  resultFile,
155
160
  inferStallMode,
161
+ spawnEnv,
156
162
  });
157
163
  }
158
164
 
165
+ function buildLeaseSpawnEnv(lease) {
166
+ const extra = {};
167
+ if (lease.authFile) {
168
+ // lease.authFile 은 보통 ~/.claude/cache/tfx-hub/codex-auth-<account>.json 형식.
169
+ // codex CLI 는 CODEX_HOME 을 통해 auth.json 위치를 결정하므로, 이 cache 파일을
170
+ // 직접 CODEX_HOME 후보 디렉토리로 사용한다. cache 파일 자체가 auth.json 이름이
171
+ // 아니라면 후속 PR 에서 isolated dir + symlink/복사 처리한다 (PRD Open Question).
172
+ // 현재는 lease.authFile 의 dirname 을 export 하되, 그 dirname 안에 auth.json 이
173
+ // 실제로 있을 때만 적용한다 (false-positive 회피).
174
+ try {
175
+ const dir = dirnameOf(lease.authFile);
176
+ if (dir) extra.CODEX_HOME = dir;
177
+ } catch {}
178
+ }
179
+ if (lease.env && typeof lease.env === "object") {
180
+ Object.assign(extra, lease.env);
181
+ }
182
+ return Object.keys(extra).length ? { ...process.env, ...extra } : undefined;
183
+ }
184
+
185
+ function dirnameOf(filePath) {
186
+ if (typeof filePath !== "string" || !filePath) return null;
187
+ const lastSep = Math.max(
188
+ filePath.lastIndexOf("/"),
189
+ filePath.lastIndexOf("\\"),
190
+ );
191
+ if (lastSep < 0) return null;
192
+ return filePath.slice(0, lastSep);
193
+ }
194
+
159
195
  // ── Public API ──────────────────────────────────────────────────
160
196
 
161
197
  export async function getCircuitState() {
@@ -113,7 +113,7 @@ export function buildExecArgs(opts = {}) {
113
113
 
114
114
  // ── Execution ───────────────────────────────────────────────────
115
115
 
116
- async function runGemini(prompt, workdir, preflight, attempt) {
116
+ async function runGemini(prompt, workdir, preflight, attempt, lease) {
117
117
  const dir = join(tmpdir(), "triflux-gemini-exec");
118
118
  mkdirSync(dir, { recursive: true });
119
119
  const resultFile = join(
@@ -125,9 +125,16 @@ async function runGemini(prompt, workdir, preflight, attempt) {
125
125
  allowedMcpServers: attempt.allowedMcpServers,
126
126
  excludeMcpServers: attempt.excludeMcpServers,
127
127
  });
128
+ // PRD A1: lease.env 가 있으면 spawn env 에 적용 (예: GOOGLE_API_KEY 등 account-specific
129
+ // 환경변수). lease 가 null 이면 default 동작 유지 (부모 env inherit).
130
+ const spawnEnv =
131
+ lease?.env && typeof lease.env === "object"
132
+ ? { ...process.env, ...lease.env }
133
+ : undefined;
128
134
  return runProcess(command, workdir, attempt.timeout, {
129
135
  resultFile,
130
136
  inferStallMode,
137
+ spawnEnv,
131
138
  });
132
139
  }
133
140