@triflux/core 10.46.0 → 10.47.0

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.
@@ -13,6 +13,7 @@
13
13
  import { execFile, execFileSync } from "node:child_process";
14
14
  import { dirname, join } from "node:path";
15
15
  import { fileURLToPath, pathToFileURL } from "node:url";
16
+ import { resolveRoleControlSnapshot } from "../hub/lib/cto-env.mjs";
16
17
  import {
17
18
  buildSynapseTaskSummary,
18
19
  drainPendingSynapse,
@@ -95,6 +96,11 @@ async function defaultResolveParticipantLakeRoot(cwd) {
95
96
  */
96
97
  export async function emitParticipantSessionStarted(stdinData, seams = {}) {
97
98
  try {
99
+ if (
100
+ !resolveRoleControlSnapshot(seams.env || process.env).auto_collect_enabled
101
+ ) {
102
+ return null;
103
+ }
98
104
  const payload = parseStartPayload(stdinData);
99
105
  const sessionId = String(payload?.session_id || "").trim();
100
106
  if (!sessionId) return null;
@@ -38,10 +38,9 @@ export function resolveRoleControlSnapshot(env = process.env, options = {}) {
38
38
  const ctoManagerEnabled = masterEnabled && isEnvOn(env, "TFX_CTO_MANAGER");
39
39
  const leadManagerEnabled =
40
40
  ctoManagerEnabled && isEnvOn(env, "TFX_LEAD_MANAGER");
41
- const northStarEnabled =
42
- masterEnabled && !isEnvOff(env, "TFX_CTO_NORTH_STAR");
41
+ const northStarEnabled = masterEnabled && isEnvOn(env, "TFX_CTO_NORTH_STAR");
43
42
  const autoCollectEnabled =
44
- masterEnabled && !isEnvOff(env, "TFX_CTO_AUTO_COLLECT");
43
+ masterEnabled && isEnvOn(env, "TFX_CTO_AUTO_COLLECT");
45
44
  const values = {
46
45
  master_enabled: masterEnabled,
47
46
  cto_manager_enabled: ctoManagerEnabled,
@@ -38,6 +38,8 @@ export function classifyBucket(bucket) {
38
38
 
39
39
  // primary/secondary를 window_minutes 기준으로 정규화
40
40
  // HUD 규약: primary=5h, secondary=1w
41
+ // window_minutes 가 비어 있으면 길이를 모르므로 보고된 칸에 둔다(app-server 스키마에서
42
+ // windowDurationMins 는 선택 필드). 길이를 알지만 5h/1w 가 아닌 버킷은 버린다.
41
43
  export function normalizeBuckets(rl) {
42
44
  let primary = null;
43
45
  let secondary = null;
@@ -46,6 +48,12 @@ export function normalizeBuckets(rl) {
46
48
  if (kind === "five_hour") primary = bucket;
47
49
  else if (kind === "weekly") secondary = bucket;
48
50
  }
51
+ if (!primary && rl.primary && rl.primary.window_minutes == null) {
52
+ primary = rl.primary;
53
+ }
54
+ if (!secondary && rl.secondary && rl.secondary.window_minutes == null) {
55
+ secondary = rl.secondary;
56
+ }
49
57
  return { primary, secondary };
50
58
  }
51
59
 
@@ -104,7 +112,35 @@ function isNewerSnapshot(candidate, current) {
104
112
  );
105
113
  }
106
114
 
107
- function selectCodexWindowSnapshot(snapshots, nowSec, windowKey) {
115
+ function positiveWindowMinutes(bucket) {
116
+ const minutes = Number(bucket?.window_minutes);
117
+ return minutes > 0 ? minutes : null;
118
+ }
119
+
120
+ // 계정 하나에는 같은 종류의 창이 한 번에 하나만 열린다. 브로커 계정이 없으면
121
+ // ~/.codex/sessions 와 HUD 라벨이 모두 ~/.codex/auth.json 계정 하나의 것이다. 그래서
122
+ // 세션 로그에 더 늦게 리셋되는 창이 나타났고 옛 창이 그 창이 열리기 전에만 관측됐다면,
123
+ // 옛 창은 조기 리셋(2026-09-23 실측)이나 계정 전환으로 끝난 창이다. 프로브로만 본 창은
124
+ // 다른 계정일 수 있어 근거로 쓰지 않는다. 새 창이 열린 뒤에도 관측된 창은 함께 도는
125
+ // 계정이므로 max-used 후보로 남긴다.
126
+ function isReplacedWindowGroup(group, other) {
127
+ if (other === group || !other.seenInSessions) return false;
128
+ if (other.minResetAt <= group.maxResetAt || !other.windowMinutes) {
129
+ return false;
130
+ }
131
+ const lastSeenMs = Date.parse(group.latest.timestamp);
132
+ if (!Number.isFinite(lastSeenMs)) return false;
133
+ // resets_at 은 실측으로 아래로 약 30초, 위로 1초 흔들리므로 가장 늦은 값으로 개시 시각을 잡는다.
134
+ const otherOpenedAtMs = (other.maxResetAt - other.windowMinutes * 60) * 1000;
135
+ return lastSeenMs <= otherOpenedAtMs;
136
+ }
137
+
138
+ function selectCodexWindowSnapshot(
139
+ snapshots,
140
+ nowSec,
141
+ windowKey,
142
+ replaceEndedWindows,
143
+ ) {
108
144
  const groups = [];
109
145
  const ungrouped = [];
110
146
 
@@ -126,19 +162,29 @@ function selectCodexWindowSnapshot(snapshots, nowSec, windowKey) {
126
162
  minResetAt: resetAt,
127
163
  maxResetAt: resetAt,
128
164
  latest: snapshot,
165
+ seenInSessions: !snapshot.probe,
166
+ windowMinutes: positiveWindowMinutes(snapshot[windowKey]),
129
167
  };
130
168
  groups.push(group);
131
169
  continue;
132
170
  }
133
171
 
172
+ if (!snapshot.probe) group.seenInSessions = true;
173
+ group.windowMinutes ??= positiveWindowMinutes(snapshot[windowKey]);
134
174
  group.minResetAt = Math.min(group.minResetAt, resetAt);
135
175
  group.maxResetAt = Math.max(group.maxResetAt, resetAt);
136
176
  if (isNewerSnapshot(snapshot, group.latest)) group.latest = snapshot;
137
177
  }
138
178
 
139
- const activeGroups = groups.filter(
179
+ const openGroups = groups.filter(
140
180
  (group) => Number(group.latest[windowKey]?.resets_at) > nowSec,
141
181
  );
182
+ const activeGroups = replaceEndedWindows
183
+ ? openGroups.filter(
184
+ (group) =>
185
+ !openGroups.some((other) => isReplacedWindowGroup(group, other)),
186
+ )
187
+ : openGroups;
142
188
  let selected = null;
143
189
  if (activeGroups.length > 0) {
144
190
  selected = activeGroups
@@ -168,16 +214,18 @@ function selectCodexWindowSnapshot(snapshots, nowSec, windowKey) {
168
214
  return { snapshot: selected, activeGroupCount: activeGroups.length };
169
215
  }
170
216
 
171
- function selectCodexSnapshot(snapshots, nowSec) {
217
+ function selectCodexSnapshot(snapshots, nowSec, replaceEndedWindows) {
172
218
  const primarySelection = selectCodexWindowSnapshot(
173
219
  snapshots,
174
220
  nowSec,
175
221
  "primary",
222
+ replaceEndedWindows,
176
223
  );
177
224
  const secondarySelection = selectCodexWindowSnapshot(
178
225
  snapshots,
179
226
  nowSec,
180
227
  "secondary",
228
+ replaceEndedWindows,
181
229
  );
182
230
  const primarySnapshot = primarySelection.snapshot;
183
231
  if (!primarySnapshot) return null;
@@ -198,7 +246,7 @@ function selectCodexSnapshot(snapshots, nowSec) {
198
246
  return selected;
199
247
  }
200
248
 
201
- function mergeRateLimitSnapshots(snapshots, nowSec) {
249
+ function mergeRateLimitSnapshots(snapshots, nowSec, replaceEndedWindows) {
202
250
  const merged = {};
203
251
  const codexSnapshots = [];
204
252
  for (const snapshot of snapshots) {
@@ -211,7 +259,11 @@ function mergeRateLimitSnapshots(snapshots, nowSec) {
211
259
  }
212
260
  }
213
261
 
214
- const codex = selectCodexSnapshot(codexSnapshots, nowSec);
262
+ const codex = selectCodexSnapshot(
263
+ codexSnapshots,
264
+ nowSec,
265
+ replaceEndedWindows,
266
+ );
215
267
  if (codex) merged.codex = codex;
216
268
  return merged;
217
269
  }
@@ -227,9 +279,16 @@ export function getCodexRateLimits({
227
279
  now = new Date(),
228
280
  maxLinesPerFile = 800,
229
281
  extraSnapshots = [],
282
+ // 교체된 창 판정(isReplacedWindowGroup). 브로커 Codex 계정이 있으면 끈다.
283
+ replaceEndedWindows = true,
230
284
  } = {}) {
231
285
  let syntheticBucket = null; // 최근 token_count에서 합성 (행 활성화 + 토큰 데이터용)
232
- const recentSnapshots = [...extraSnapshots];
286
+ // 프로브도 세션 로그와 같은 window_minutes 기준으로 5h/1w 슬롯을 정한다.
287
+ // app-server 는 5h 창이 없으면 주간 창을 primary 로 보낸다(2026-09-23 실측).
288
+ const recentSnapshots = extraSnapshots.map((snapshot) => ({
289
+ ...snapshot,
290
+ ...normalizeBuckets(snapshot),
291
+ }));
233
292
  const nowSec = Math.floor(now.getTime() / 1000);
234
293
 
235
294
  // 7일간 스캔: 실제 rate_limits 우선, 합성 버킷은 폴백
@@ -313,7 +372,11 @@ export function getCodexRateLimits({
313
372
  }
314
373
 
315
374
  const snapshots = dayOffset <= 1 ? recentSnapshots : daySnapshots;
316
- const mergedBuckets = mergeRateLimitSnapshots(snapshots, nowSec);
375
+ const mergedBuckets = mergeRateLimitSnapshots(
376
+ snapshots,
377
+ nowSec,
378
+ replaceEndedWindows,
379
+ );
317
380
  if (Object.keys(mergedBuckets).length > 0) {
318
381
  if (syntheticBucket) {
319
382
  const main =
@@ -369,10 +432,30 @@ export async function collectProbeSnapshots(now = new Date()) {
369
432
  );
370
433
  }
371
434
 
435
+ // 브로커 계정은 자기 CODEX_HOME 에서 돌기 때문에, 옛 창이 전환 전 계정의 창이어도 그
436
+ // 계정이 아직 쓰이고 있을 수 있다. accounts.json 의 codex 항목과 프로브용 인증 캐시로 판정한다.
437
+ export function hasBrokerCodexAccounts({
438
+ brokerDir = CODEX_BROKER_AUTH_CACHE_DIR,
439
+ } = {}) {
440
+ const accounts = readJson(join(brokerDir, "accounts.json"), null);
441
+ if (Array.isArray(accounts?.codex) && accounts.codex.length > 0) return true;
442
+ try {
443
+ return readdirSync(brokerDir).some((file) =>
444
+ /^codex-auth-.+\.json$/.test(file),
445
+ );
446
+ } catch {
447
+ return false;
448
+ }
449
+ }
450
+
372
451
  export async function refreshCodexRateLimitsCache() {
373
452
  const now = new Date();
374
453
  const extraSnapshots = await collectProbeSnapshots(now);
375
- const buckets = getCodexRateLimits({ now, extraSnapshots });
454
+ const buckets = getCodexRateLimits({
455
+ now,
456
+ extraSnapshots,
457
+ replaceEndedWindows: !hasBrokerCodexAccounts(),
458
+ });
376
459
  // buckets가 null이어도 캐시 갱신 (stale 데이터 제거)
377
460
  writeJsonSafe(CODEX_QUOTA_CACHE_PATH, { timestamp: Date.now(), buckets });
378
461
  return buckets;
@@ -1,5 +1,6 @@
1
1
  import { readFileSync, statSync } from "node:fs";
2
2
  import { join } from "node:path";
3
+ import { resolveRoleControlSnapshot } from "../../hub/lib/cto-env.mjs";
3
4
  import {
4
5
  CTO_LAKE_CURRENT_RELATIVE_PATH,
5
6
  CTO_STATUS_LINE_MAX_CHARS,
@@ -53,6 +54,8 @@ function extractStatusLine(lines) {
53
54
  }
54
55
 
55
56
  export function readCtoStatus(options = {}) {
57
+ if (!resolveRoleControlSnapshot(options.env).auto_collect_enabled)
58
+ return null;
56
59
  try {
57
60
  const currentPath = resolveCurrentPath(options);
58
61
  const stat = statSync(currentPath);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@triflux/core",
3
- "version": "10.46.0",
3
+ "version": "10.47.0",
4
4
  "description": "triflux core — CLI routing, pipeline, adapters. Zero native dependencies.",
5
5
  "type": "module",
6
6
  "main": "hub/index.mjs",