@opengeni/worker-bundle 0.7.5 → 0.8.3

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 (41) hide show
  1. package/dist/chunk-BJUW5LCM.js +325 -0
  2. package/dist/chunk-BJUW5LCM.js.map +1 -0
  3. package/dist/host-export.d.ts +70 -0
  4. package/dist/host-export.js +7 -0
  5. package/dist/host-export.js.map +1 -0
  6. package/dist/index.d.ts +98 -6
  7. package/dist/index.js +2812 -813
  8. package/dist/index.js.map +1 -1
  9. package/dist/workflow-bundle.js +12358 -0
  10. package/package.json +18 -12
  11. package/src/activities/agent-turn.ts +2045 -545
  12. package/src/activities/capabilities.ts +9 -2
  13. package/src/activities/codex-capacity.ts +6 -11
  14. package/src/activities/codex-rotation.ts +75 -86
  15. package/src/activities/context-compaction.ts +104 -24
  16. package/src/activities/environment.ts +367 -92
  17. package/src/activities/expiring-material-renewal.ts +143 -0
  18. package/src/activities/goals.ts +4 -0
  19. package/src/activities/mcp-credentials.ts +38 -0
  20. package/src/activities/packs.ts +115 -2
  21. package/src/activities/run-credential-renewal.ts +70 -0
  22. package/src/activities/run-credentials.ts +132 -0
  23. package/src/activities/run-input.ts +23 -0
  24. package/src/activities/sandbox-lease.ts +1 -1
  25. package/src/activities/scheduled-tasks.ts +21 -0
  26. package/src/activities/session-state.ts +72 -0
  27. package/src/activities/streaming.ts +1 -0
  28. package/src/activities/toolspace-token-renewal.ts +57 -0
  29. package/src/activities/types.ts +44 -6
  30. package/src/activities/workspace-capture.ts +113 -93
  31. package/src/activities.ts +10 -2
  32. package/src/host-export-pump.ts +416 -0
  33. package/src/host-export.ts +22 -0
  34. package/src/http.ts +77 -28
  35. package/src/index.ts +360 -156
  36. package/src/sandbox-resume.ts +47 -1
  37. package/src/sandbox-routing.ts +1 -1
  38. package/src/worker-service-lifecycle.ts +104 -0
  39. package/src/workflows/activities.ts +13 -7
  40. package/src/workflows/session.ts +219 -23
  41. package/src/workflows.ts +1 -0
@@ -14,6 +14,7 @@ import {
14
14
  CODEX_MODEL_CONTEXT_WINDOW_TOKENS,
15
15
  CODEX_MODEL_EFFECTIVE_CONTEXT_WINDOW_TOKENS,
16
16
  CODEX_MODEL_AUTO_COMPACT_TOKEN_LIMIT,
17
+ CODEX_MODEL_TOOL_OUTPUT_TRUNCATION_TOKENS,
17
18
  CODEX_MODEL_ID_PREFIX,
18
19
  CODEX_PROVIDER_BASE_URL,
19
20
  CODEX_PROVIDER_ID,
@@ -48,11 +49,15 @@ export async function settingsWithSessionMcpServersForRun(
48
49
  if (metadata.length === 0) {
49
50
  return settings;
50
51
  }
51
- throw new Error("session MCP server credentials require OPENGENI_ENVIRONMENTS_ENCRYPTION_KEY");
52
+ if (metadata.some((server) => server.headerNames.length > 0)) {
53
+ throw new Error(
54
+ "session MCP server credentials require OPENGENI_ENVIRONMENTS_ENCRYPTION_KEY",
55
+ );
56
+ }
52
57
  }
53
58
  return settingsWithSessionMcpServers(
54
59
  settings,
55
- await listSessionMcpServersForRun(db, workspaceId, sessionId, encryptionKey),
60
+ await listSessionMcpServersForRun(db, workspaceId, sessionId, encryptionKey ?? null),
56
61
  );
57
62
  }
58
63
 
@@ -78,6 +83,7 @@ export function settingsWithSessionMcpServers(
78
83
  ...(server.requireApproval !== undefined
79
84
  ? { requireApproval: server.requireApproval }
80
85
  : {}),
86
+ ...(server.connectionRef ? { connectionRef: server.connectionRef } : {}),
81
87
  headers: server.headers,
82
88
  })),
83
89
  ],
@@ -174,6 +180,7 @@ export function withCodexProvider(settings: Settings): Settings {
174
180
  contextWindowTokens: CODEX_MODEL_CONTEXT_WINDOW_TOKENS,
175
181
  effectiveContextWindowTokens: CODEX_MODEL_EFFECTIVE_CONTEXT_WINDOW_TOKENS,
176
182
  autoCompactTokenLimit: CODEX_MODEL_AUTO_COMPACT_TOKEN_LIMIT,
183
+ toolOutputTruncationTokens: CODEX_MODEL_TOOL_OUTPUT_TRUNCATION_TOKENS,
177
184
  })),
178
185
  };
179
186
  return { ...settings, modelProvidersJson: JSON.stringify([...providers, codexProvider]) };
@@ -9,6 +9,7 @@ import {
9
9
  } from "@opengeni/db";
10
10
  import type { Settings } from "@opengeni/config";
11
11
  import {
12
+ CODEX_USAGE_EXHAUSTED_PCT,
12
13
  authoritativeCodexCapacityResetAt,
13
14
  isCodexCredentialEligible,
14
15
  selectCodexCredentialLeaseForTurn,
@@ -94,7 +95,6 @@ export function codexCapacityDecision(
94
95
  sessionPinSource: context.sessionPinSource,
95
96
  sessionLastCredentialId: context.sessionLastCredentialId,
96
97
  continuationCredentialId: null,
97
- nearExhaustionPct: settings.codexRotationNearExhaustionPct,
98
98
  now,
99
99
  });
100
100
  if (selected.credentialId) {
@@ -103,17 +103,12 @@ export function codexCapacityDecision(
103
103
  credentialId: selected.credentialId,
104
104
  diagnostic: {
105
105
  connectedCount: context.accounts.length,
106
- eligibleCount: context.accounts.filter((account) =>
107
- isCodexCredentialEligible(account, settings.codexRotationNearExhaustionPct, now),
108
- ).length,
106
+ eligibleCount: context.accounts.filter((account) => isCodexCredentialEligible(account, now))
107
+ .length,
109
108
  },
110
109
  };
111
110
  }
112
- const authoritativeReset = authoritativeCodexCapacityResetAt(
113
- context.accounts,
114
- settings.codexRotationNearExhaustionPct,
115
- now,
116
- );
111
+ const authoritativeReset = authoritativeCodexCapacityResetAt(context.accounts, now);
117
112
  return {
118
113
  kind: "unavailable",
119
114
  earliestResetAt: authoritativeReset,
@@ -135,8 +130,8 @@ async function refreshCapacityMetadata(
135
130
  (account) =>
136
131
  account.allocatorEnabled &&
137
132
  account.status === "active" &&
138
- ((account.primaryUsedPercent ?? 0) >= services.settings.codexRotationNearExhaustionPct ||
139
- (account.secondaryUsedPercent ?? 0) >= services.settings.codexRotationNearExhaustionPct ||
133
+ ((account.primaryUsedPercent ?? 0) >= CODEX_USAGE_EXHAUSTED_PCT ||
134
+ (account.secondaryUsedPercent ?? 0) >= CODEX_USAGE_EXHAUSTED_PCT ||
140
135
  account.usageCheckedAt === null),
141
136
  );
142
137
  await refreshCodexUsageAndRepairCapacityWaiters(
@@ -21,6 +21,25 @@ export type CodexRotationStrategy =
21
21
  | "drain_then_next"
22
22
  | "sharded";
23
23
 
24
+ /**
25
+ * The ONE strategy rotation-enabled workspaces run: sticky-sharded. The legacy
26
+ * strategies are all strictly dominated post-cache-affinity — session
27
+ * stickiness is worth ~half the prompt-cache hit rate, and round_robin /
28
+ * most_remaining churn accounts per turn while drain_then_next concentrates
29
+ * every concurrent session on ONE account (the measured concurrency-eviction
30
+ * failure sharding replaced). Stored `rotation_strategy` values are therefore
31
+ * NORMALIZED here at every read site: legacy values (and any future unknown
32
+ * string) behave as "sharded". The column and the legacy branch code are kept
33
+ * intact for old-binary rollback safety — they are simply unreachable through
34
+ * this normalization. The API accepts-but-ignores strategy writes for the same
35
+ * reason (no SDK caller breaks). User-facing choice is intentionally gone: the
36
+ * remaining real intents are rotation on/off, per-account allocator
37
+ * include/exclude, and manual pins — not algorithm selection.
38
+ */
39
+ export function effectiveRotationStrategy(_stored: string): CodexRotationStrategy {
40
+ return "sharded";
41
+ }
42
+
24
43
  /**
25
44
  * How a session's codex pin governs THIS turn's account selection (pure). Encodes the
26
45
  * policy-pin LIFECYCLE rule: a 'policy' pin is meaningful ONLY while the sharded policy
@@ -55,7 +74,7 @@ export function classifyCodexPin(args: {
55
74
  if (pinned && pinSource !== "policy") {
56
75
  return "manual";
57
76
  }
58
- const shardedActive = rotationEnabled && strategy === "sharded";
77
+ const shardedActive = rotationEnabled && effectiveRotationStrategy(strategy) === "sharded";
59
78
  if (shardedActive) {
60
79
  return "sharded";
61
80
  }
@@ -95,12 +114,14 @@ export type CodexTurnLeaseSelection = {
95
114
  /** The minimum all-capped idle: continueDelayMs is clamped to at least this (never 0). */
96
115
  export const MIN_IDLE_MS = 60_000; // 60s
97
116
  /**
98
- * Cooldown applied to an over-threshold account whose cached reset is null / unknown
117
+ * Cooldown applied to an exhausted account whose cached reset is null / unknown
99
118
  * / already-elapsed (the turn hot path never refreshes usage, so a capped window's
100
119
  * reset reads stale). Treating it as "available after a default cooldown" keeps
101
120
  * availableAt — and therefore earliestReset — ALWAYS in the future.
102
121
  */
103
122
  export const DEFAULT_RESET_COOLDOWN_MS = 60_000; // 60s
123
+ /** Cached provider usage becomes unavailable only at actual exhaustion. */
124
+ export const CODEX_USAGE_EXHAUSTED_PCT = 100;
104
125
 
105
126
  function effectiveWindowUsed(usedPercent: number | null, resetAt: Date | null, now: Date): number {
106
127
  // A completed provider window is eligible immediately even if its last cache
@@ -173,42 +194,32 @@ function droppedConnectorsFor(acct: CodexRotationAccount, usedConnectors: string
173
194
 
174
195
  /**
175
196
  * Healthy for an already-frozen/in-flight turn = connected/usable (status
176
- * "active", excludes needs_relogin/error), not cooling, and under the
177
- * near-exhaustion threshold on BOTH windows. Allocator eligibility is
197
+ * "active", excludes needs_relogin/error), not cooling, and not exhausted in
198
+ * either provider window. Allocator eligibility is
178
199
  * deliberately separate so disabling NEW work cannot break refresh/resume.
179
200
  */
180
- export function isCodexCredentialHealthy(
181
- acct: CodexRotationAccount,
182
- nearExhaustionPct: number,
183
- now: Date,
184
- ): boolean {
201
+ export function isCodexCredentialHealthy(acct: CodexRotationAccount, now: Date): boolean {
185
202
  return (
186
- acct.status === "active" && !cooling(acct, now) && bindingUsedPct(acct, now) < nearExhaustionPct
203
+ acct.status === "active" &&
204
+ !cooling(acct, now) &&
205
+ bindingUsedPct(acct, now) < CODEX_USAGE_EXHAUSTED_PCT
187
206
  );
188
207
  }
189
208
 
190
209
  /** New automatic allocation requires both health and explicit allocator eligibility. */
191
- export function isCodexCredentialEligible(
192
- acct: CodexRotationAccount,
193
- nearExhaustionPct: number,
194
- now: Date,
195
- ): boolean {
196
- return acct.allocatorEnabled && isCodexCredentialHealthy(acct, nearExhaustionPct, now);
210
+ export function isCodexCredentialEligible(acct: CodexRotationAccount, now: Date): boolean {
211
+ return acct.allocatorEnabled && isCodexCredentialHealthy(acct, now);
197
212
  }
198
213
 
199
214
  /**
200
215
  * Public eligibility predicate (same definition as the private `eligible`): an
201
- * account is usable this turn iff it is connected/active, not cooling, and under
202
- * the near-exhaustion threshold on BOTH windows. The worker's sharded home
216
+ * account is usable this turn iff it is connected/active, not cooling, and below
217
+ * actual exhaustion on both windows. The worker's sharded home
203
218
  * health-check uses this to decide whether a session's existing policy pin is still
204
219
  * a valid home or must be re-sharded.
205
220
  */
206
- export function isCodexAccountEligible(
207
- acct: CodexRotationAccount,
208
- nearExhaustionPct: number,
209
- now: Date,
210
- ): boolean {
211
- return isCodexCredentialEligible(acct, nearExhaustionPct, now);
221
+ export function isCodexAccountEligible(acct: CodexRotationAccount, now: Date): boolean {
222
+ return isCodexCredentialEligible(acct, now);
212
223
  }
213
224
 
214
225
  /** Deterministic 32-bit FNV-1a over a UTF-16 code-unit stream. Pure, allocation-free. */
@@ -225,7 +236,7 @@ function fnv1a32(input: string): number {
225
236
  /**
226
237
  * Session-sharded HOME account (AM-6): the deterministic account a session runs on
227
238
  * under the "sharded" strategy — `stableAccountList[ hash(sessionId) % N ]` over the
228
- * ELIGIBLE (connected, not cooling, under-threshold) accounts in stable created_at
239
+ * ELIGIBLE (connected, not cooling, not exhausted) accounts in stable created_at
229
240
  * order (`listCodexAccountStatuses`).
230
241
  *
231
242
  * Why deterministic hash over "least-loaded by session count" (AM-6): it needs ZERO
@@ -244,13 +255,10 @@ function fnv1a32(input: string): number {
244
255
  export function shardCredentialForSession(args: {
245
256
  sessionId: string;
246
257
  accounts: CodexRotationAccount[];
247
- nearExhaustionPct: number;
248
258
  now: Date;
249
259
  }): string | null {
250
- const { sessionId, accounts, nearExhaustionPct, now } = args;
251
- const eligibles = accounts.filter((acct) =>
252
- isCodexCredentialEligible(acct, nearExhaustionPct, now),
253
- );
260
+ const { sessionId, accounts, now } = args;
261
+ const eligibles = accounts.filter((acct) => isCodexCredentialEligible(acct, now));
254
262
  if (eligibles.length === 0) {
255
263
  return null;
256
264
  }
@@ -259,12 +267,8 @@ export function shardCredentialForSession(args: {
259
267
  }
260
268
 
261
269
  /** earliestResetAt across ALL connected accounts — exported for the sharded all-capped idle. */
262
- export function earliestCodexReset(
263
- accounts: CodexRotationAccount[],
264
- nearExhaustionPct: number,
265
- now: Date,
266
- ): Date {
267
- return earliestReset(accounts, nearExhaustionPct, now);
270
+ export function earliestCodexReset(accounts: CodexRotationAccount[], now: Date): Date {
271
+ return earliestReset(accounts, now);
268
272
  }
269
273
 
270
274
  /**
@@ -288,21 +292,20 @@ export function chooseShardedHome(args: {
288
292
  sessionId: string;
289
293
  currentPolicyPin: string | null;
290
294
  accounts: CodexRotationAccount[];
291
- nearExhaustionPct: number;
292
295
  now: Date;
293
296
  }):
294
297
  | { kind: "home"; credentialId: string; rewritePin: boolean }
295
298
  | { kind: "allCapped"; earliestResetAt: Date } {
296
- const { sessionId, currentPolicyPin, accounts, nearExhaustionPct, now } = args;
299
+ const { sessionId, currentPolicyPin, accounts, now } = args;
297
300
  const pinRow = currentPolicyPin
298
301
  ? (accounts.find((acct) => acct.id === currentPolicyPin) ?? null)
299
302
  : null;
300
- if (pinRow && isCodexCredentialEligible(pinRow, nearExhaustionPct, now)) {
303
+ if (pinRow && isCodexCredentialEligible(pinRow, now)) {
301
304
  return { kind: "home", credentialId: currentPolicyPin!, rewritePin: false };
302
305
  }
303
- const home = shardCredentialForSession({ sessionId, accounts, nearExhaustionPct, now });
306
+ const home = shardCredentialForSession({ sessionId, accounts, now });
304
307
  if (home == null) {
305
- return { kind: "allCapped", earliestResetAt: earliestReset(accounts, nearExhaustionPct, now) };
308
+ return { kind: "allCapped", earliestResetAt: earliestReset(accounts, now) };
306
309
  }
307
310
  return { kind: "home", credentialId: home, rewritePin: true };
308
311
  }
@@ -319,11 +322,7 @@ export function chooseShardedHome(args: {
319
322
  * default cooldown (now + DEFAULT_RESET_COOLDOWN_MS), so the caller always idles a
320
323
  * bounded positive time and re-checks (which refreshes usage and self-heals).
321
324
  */
322
- export function availableAt(
323
- acct: CodexRotationAccount,
324
- nearExhaustionPct: number,
325
- now: Date,
326
- ): Date {
325
+ export function availableAt(acct: CodexRotationAccount, now: Date): Date {
327
326
  const nowMs = now.getTime();
328
327
  // An unknown/elapsed block clears after a default cooldown, never in the past.
329
328
  const defaultClear = new Date(nowMs + DEFAULT_RESET_COOLDOWN_MS);
@@ -340,11 +339,13 @@ export function availableAt(
340
339
  candidates.push(resetAt != null && resetAt.getTime() > nowMs ? resetAt : defaultClear);
341
340
  };
342
341
  windowClear(
343
- effectiveWindowUsed(acct.primaryUsedPercent, acct.primaryResetAt, now) >= nearExhaustionPct,
342
+ effectiveWindowUsed(acct.primaryUsedPercent, acct.primaryResetAt, now) >=
343
+ CODEX_USAGE_EXHAUSTED_PCT,
344
344
  acct.primaryResetAt,
345
345
  );
346
346
  windowClear(
347
- effectiveWindowUsed(acct.secondaryUsedPercent, acct.secondaryResetAt, now) >= nearExhaustionPct,
347
+ effectiveWindowUsed(acct.secondaryUsedPercent, acct.secondaryResetAt, now) >=
348
+ CODEX_USAGE_EXHAUSTED_PCT,
348
349
  acct.secondaryResetAt,
349
350
  );
350
351
  // Ineligible for a non-quota reason (needs_relogin / error) with no known block, or
@@ -357,13 +358,9 @@ export function availableAt(
357
358
  }
358
359
 
359
360
  /** earliestResetAt across ALL connected accounts (min of each account's availableAt). */
360
- function earliestReset(
361
- accounts: CodexRotationAccount[],
362
- nearExhaustionPct: number,
363
- now: Date,
364
- ): Date {
361
+ function earliestReset(accounts: CodexRotationAccount[], now: Date): Date {
365
362
  return accounts
366
- .map((acct) => availableAt(acct, nearExhaustionPct, now))
363
+ .map((acct) => availableAt(acct, now))
367
364
  .reduce((a, b) => (b.getTime() < a.getTime() ? b : a));
368
365
  }
369
366
 
@@ -375,7 +372,6 @@ function earliestReset(
375
372
  */
376
373
  export function authoritativeCodexCapacityResetAt(
377
374
  accounts: CodexRotationAccount[],
378
- nearExhaustionPct: number,
379
375
  now: Date,
380
376
  ): Date | null {
381
377
  const future: Date[] = [];
@@ -385,14 +381,14 @@ export function authoritativeCodexCapacityResetAt(
385
381
  future.push(account.exhaustedUntil);
386
382
  }
387
383
  if (
388
- (account.primaryUsedPercent ?? 0) >= nearExhaustionPct &&
384
+ (account.primaryUsedPercent ?? 0) >= CODEX_USAGE_EXHAUSTED_PCT &&
389
385
  account.primaryResetAt &&
390
386
  account.primaryResetAt.getTime() > now.getTime()
391
387
  ) {
392
388
  future.push(account.primaryResetAt);
393
389
  }
394
390
  if (
395
- (account.secondaryUsedPercent ?? 0) >= nearExhaustionPct &&
391
+ (account.secondaryUsedPercent ?? 0) >= CODEX_USAGE_EXHAUSTED_PCT &&
396
392
  account.secondaryResetAt &&
397
393
  account.secondaryResetAt.getTime() > now.getTime()
398
394
  ) {
@@ -494,7 +490,6 @@ export function chooseRotationActive(args: {
494
490
  activeCredentialId: string | null;
495
491
  priorCredentialId: string | null;
496
492
  accounts: CodexRotationAccount[];
497
- nearExhaustionPct: number;
498
493
  now: Date;
499
494
  // P4 (Part B): the connectors the leaving session has access to (the leaving
500
495
  // account's cached connector set, used as the "session needs these" proxy).
@@ -502,14 +497,11 @@ export function chooseRotationActive(args: {
502
497
  // P3 (every account trivially covers → Tier 1 == all eligibles). Self-gating.
503
498
  usedConnectors?: string[];
504
499
  }): RotationDecision {
505
- const {
506
- rotationStrategy,
507
- activeCredentialId,
508
- priorCredentialId,
509
- accounts,
510
- nearExhaustionPct,
511
- now,
512
- } = args;
500
+ const { activeCredentialId, priorCredentialId, accounts, now } = args;
501
+ // Normalize at the entry point: rotation-enabled ALWAYS behaves as sharded
502
+ // (see effectiveRotationStrategy). args keeps the field so call sites and
503
+ // tests can still express stored legacy values.
504
+ const rotationStrategy = effectiveRotationStrategy(args.rotationStrategy);
513
505
  const allocatableAccounts = accounts.filter((account) => account.allocatorEnabled);
514
506
  const usedConnectors = args.usedConnectors ?? [];
515
507
 
@@ -517,20 +509,18 @@ export function chooseRotationActive(args: {
517
509
  return { kind: "none" };
518
510
  }
519
511
 
520
- const eligibles = allocatableAccounts.filter((acct) =>
521
- isCodexCredentialEligible(acct, nearExhaustionPct, now),
522
- );
512
+ const eligibles = allocatableAccounts.filter((acct) => isCodexCredentialEligible(acct, now));
523
513
 
524
514
  // The active pointer is a cursor/manual preference, NOT a sticky lease. In
525
515
  // particular, most_remaining must rank the whole eligible pool every turn;
526
- // keeping a healthy active account until 90% was the production monopolization
527
- // defect fixed by OPE-21. drain_then_next remains the one explicitly sticky
516
+ // keeping a healthy active account sticky was the production monopolization
517
+ // defect fixed by credential allocator. drain_then_next remains the one explicitly sticky
528
518
  // strategy, and a manual pin is handled by the caller as explicit policy.
529
519
  const decide = (chosen: CodexRotationAccount | undefined): RotationDecision => {
530
520
  if (!chosen) {
531
521
  return {
532
522
  kind: "allCapped",
533
- earliestResetAt: earliestReset(allocatableAccounts, nearExhaustionPct, now),
523
+ earliestResetAt: earliestReset(allocatableAccounts, now),
534
524
  };
535
525
  }
536
526
  // P4: surface the dropped-connector note when the chosen account doesn't cover
@@ -550,7 +540,7 @@ export function chooseRotationActive(args: {
550
540
  if (eligibles.length === 0) {
551
541
  return {
552
542
  kind: "allCapped",
553
- earliestResetAt: earliestReset(allocatableAccounts, nearExhaustionPct, now),
543
+ earliestResetAt: earliestReset(allocatableAccounts, now),
554
544
  };
555
545
  }
556
546
  const priorIdx = priorCredentialId
@@ -563,7 +553,7 @@ export function chooseRotationActive(args: {
563
553
  ...allocatableAccounts.slice(0, priorIdx + 1),
564
554
  ]
565
555
  : allocatableAccounts;
566
- const chosen = ordered.find((acct) => isCodexCredentialEligible(acct, nearExhaustionPct, now));
556
+ const chosen = ordered.find((acct) => isCodexCredentialEligible(acct, now));
567
557
  return decide(chosen);
568
558
  }
569
559
 
@@ -572,7 +562,7 @@ export function chooseRotationActive(args: {
572
562
  const priorRow = priorCredentialId
573
563
  ? accounts.find((acct) => acct.id === priorCredentialId)
574
564
  : undefined;
575
- if (priorRow && isCodexCredentialEligible(priorRow, nearExhaustionPct, now)) {
565
+ if (priorRow && isCodexCredentialEligible(priorRow, now)) {
576
566
  return decide(priorRow);
577
567
  }
578
568
  return decide(eligibles[0]);
@@ -632,7 +622,7 @@ export function chooseLegacyRotationActive(
632
622
  const active = args.activeCredentialId
633
623
  ? args.accounts.find((account) => account.id === args.activeCredentialId)
634
624
  : undefined;
635
- if (active && isCodexCredentialEligible(active, args.nearExhaustionPct, args.now)) {
625
+ if (active && isCodexCredentialEligible(active, args.now)) {
636
626
  return { kind: "active", credentialId: active.id, moved: false };
637
627
  }
638
628
  }
@@ -662,7 +652,6 @@ export function selectCodexCredentialLeaseForTurn(args: {
662
652
  sessionLastCredentialId: string | null;
663
653
  /** Same-turn checkpoint/frozen account; temporary disable cannot move it. */
664
654
  continuationCredentialId?: string | null;
665
- nearExhaustionPct: number;
666
655
  now: Date;
667
656
  }): CodexTurnLeaseSelection {
668
657
  const {
@@ -674,11 +663,13 @@ export function selectCodexCredentialLeaseForTurn(args: {
674
663
  existingCredentialId,
675
664
  } = args.context;
676
665
  const leasingEnabled = args.leasingEnabled && leaseRotationEnabled;
677
- const strategy = rotationStrategy as CodexRotationStrategy;
666
+ // Normalized: rotation-enabled always behaves as sharded (see
667
+ // effectiveRotationStrategy); the stored value is only ever legacy residue.
668
+ const strategy = effectiveRotationStrategy(rotationStrategy);
678
669
  const existing = existingCredentialId
679
670
  ? accounts.find((account) => account.id === existingCredentialId)
680
671
  : undefined;
681
- if (existing && isCodexCredentialHealthy(existing, args.nearExhaustionPct, args.now)) {
672
+ if (existing && isCodexCredentialHealthy(existing, args.now)) {
682
673
  return {
683
674
  credentialId: existing.id,
684
675
  advanceActivePointer: false,
@@ -693,7 +684,7 @@ export function selectCodexCredentialLeaseForTurn(args: {
693
684
  const continuation = args.continuationCredentialId
694
685
  ? accounts.find((account) => account.id === args.continuationCredentialId)
695
686
  : undefined;
696
- if (continuation && isCodexCredentialHealthy(continuation, args.nearExhaustionPct, args.now)) {
687
+ if (continuation && isCodexCredentialHealthy(continuation, args.now)) {
697
688
  return {
698
689
  credentialId: continuation.id,
699
690
  advanceActivePointer: false,
@@ -724,7 +715,7 @@ export function selectCodexCredentialLeaseForTurn(args: {
724
715
  if (!pinned?.allocatorEnabled) {
725
716
  return { credentialId: null, decision: { kind: "none" }, advanceActivePointer: false };
726
717
  }
727
- if (isCodexCredentialHealthy(pinned, args.nearExhaustionPct, args.now)) {
718
+ if (isCodexCredentialHealthy(pinned, args.now)) {
728
719
  return {
729
720
  credentialId: pinned.id,
730
721
  decision: { kind: "active", credentialId: pinned.id, moved: false },
@@ -735,14 +726,14 @@ export function selectCodexCredentialLeaseForTurn(args: {
735
726
  credentialId: null,
736
727
  decision: {
737
728
  kind: "allCapped",
738
- earliestResetAt: availableAt(pinned, args.nearExhaustionPct, args.now),
729
+ earliestResetAt: availableAt(pinned, args.now),
739
730
  },
740
731
  advanceActivePointer: false,
741
732
  };
742
733
  }
743
734
 
744
- // OPE-31 policy homes compose with OPE-21 by sharding only the candidate list
745
- // handed to this selector. A future OPE-32 filter may therefore choose one
735
+ // Pin-policy homes compose with the credential allocator by sharding only the candidate list
736
+ // handed to this selector. A future policy filter may therefore choose one
746
737
  // primary/fallback pool before this ranker runs; accounts from different pools
747
738
  // are never union-ranked here.
748
739
  if (pinDisposition === "sharded") {
@@ -753,7 +744,6 @@ export function selectCodexCredentialLeaseForTurn(args: {
753
744
  sessionId: args.sessionId,
754
745
  currentPolicyPin: args.sessionPinSource === "policy" ? args.sessionPinnedCredentialId : null,
755
746
  accounts,
756
- nearExhaustionPct: args.nearExhaustionPct,
757
747
  now: args.now,
758
748
  });
759
749
  if (shard.kind === "allCapped") {
@@ -811,7 +801,6 @@ export function selectCodexCredentialLeaseForTurn(args: {
811
801
  // the workspace pointer is only correct when this session has never run.
812
802
  priorCredentialId: priorId,
813
803
  accounts,
814
- nearExhaustionPct: args.nearExhaustionPct,
815
804
  now: args.now,
816
805
  usedConnectors: accounts.find((account) => account.id === priorId)?.connectorNamespaces ?? [],
817
806
  });