#19385: fix: pass authProfileId from cron session to runEmbeddedPiAgent
size: S
Cluster:
Cron Session Enhancements
## Summary
- Cron isolated-agent jobs were not passing `authProfileId` to `runEmbeddedPiAgent`, causing the runner to fall back to default auth profile ordering (OAuth > Token > API Key)
- CLI auth (API key type) was deprioritized, so billing-eligible profiles were tried first, producing repeated "payment verification failed" messages on every cron run
- This applies the same fix as #16303 — passes `authProfileId`/`authProfileIdSource` from the session entry and applies `applyModelOverrideToSessionEntry` when the resolved model differs from the default
## Changes (2 files)
- **`src/cron/isolated-agent/run.ts`** — import `applyModelOverrideToSessionEntry`, apply it after `resolveCronSession` when model override is active, and pass `authProfileId`/`authProfileIdSource` to `runEmbeddedPiAgent`
- **`src/cron/isolated-agent/session.test.ts`** — add test verifying `authProfileOverride` fields are preserved across session resets
Fixes #14401
Ref: #16303, #14376
## Test plan
- [x] All 145 cron tests pass (`npx vitest run src/cron/`)
- [x] New test verifies `authProfileOverride`/`authProfileOverrideSource` preservation
- [ ] Manual: configure a cron job with Claude CLI auth, verify it uses CLI profile instead of billing-eligible profiles
🤖 Generated with [Claude Code](https://claude.com/claude-code)
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
This PR fixes a bug where cron isolated-agent jobs were not passing `authProfileId` to `runEmbeddedPiAgent`, causing the runner to fall back to the default auth profile ordering (OAuth > Token > API Key) and repeatedly attempt billing-eligible profiles before CLI API key profiles, resulting in "payment verification failed" spam on every cron run.
**Key changes:**
- In `run.ts`: imports `applyModelOverrideToSessionEntry` and calls it after `resolveCronSession` when a model override is active, mirroring existing sub-agent behavior. Also computes `cronAuthProfileId` (only when `providerOverride === provider` to guard against cross-provider profile leakage during fallback) and passes `authProfileId`/`authProfileIdSource` to `runEmbeddedPiAgent`.
- In `session.test.ts`: adds a regression test verifying `authProfileOverride`/`authProfileOverrideSource` are preserved across session loads.
The implementation correctly mirrors the pattern in `src/commands/agent.ts` (the reference PR #16303) where `authProfileId` is set to `undefined` when the active provider differs from the primary, preventing an auth profile for one provider from being mistakenly applied to a fallback provider. The logic flows cleanly and doesn't introduce any correctness issues.
<h3>Confidence Score: 5/5</h3>
- This PR is safe to merge; the fix is a targeted, well-scoped bug fix that mirrors an existing pattern in the codebase.
- The logic correctly mirrors the reference pattern from `commands/agent.ts`, the provider-guard check prevents cross-provider auth profile leakage during model fallback, `applyModelOverrideToSessionEntry` is called with the session entry's own override fields (so it either preserves them or correctly clears them when unset), and the code doesn't introduce any new execution paths. The one style note is about incomplete test coverage for the stale-session case, but the underlying implementation is correct (the `...entry` spread in `resolveCronSession` already preserves the fields unconditionally).
- No files require special attention
<sub>Last reviewed commit: e627be4</sub>
<!-- greptile_other_comments_section -->
<sub>(3/5) Reply to the agent's comments like "Can you suggest a fix for this @greptileai?" or ask follow-up questions!</sub>
<!-- /greptile_comment -->
Most Similar PRs
#16303: fix: apply cron payload.model override to session entry and pass au...
by superlowburn · 2026-02-14
91.6%
#12310: cron: pass agentDir to embedded runner for isolated sessions
by magendary · 2026-02-09
86.9%
#22707: fix: pass agentDir to runEmbeddedPiAgent in cron isolated sessions
by mrlerner · 2026-02-21
86.4%
#22845: Pass agentDir through cron and followup embedded runs
by seilk · 2026-02-21
85.6%
#21646: fix(cron): pass agentDir to runEmbeddedPiAgent for correct auth res...
by zhangjunmengyang · 2026-02-20
84.9%
#21279: Fix/sessions list cron model override
by altaywtf · 2026-02-19
83.9%
#16390: fix(cron): jobs land in wrong agent session when agentId isn't in a...
by yinghaosang · 2026-02-14
83.6%
#23501: fix(cron): force new session ID for isolated cron jobs (#23470)
by stakeswky · 2026-02-22
82.4%
#11474: fix(cron): respect subagents.model in isolated cron sessions
by AnonO6 · 2026-02-07
82.2%
#11816: fix(cron): forward agent-specific exec config to isolated cron sess...
by AnonO6 · 2026-02-08
82.1%