#8341: feat: add per-job cache control for cron jobs
app: web-ui
gateway
stale
Cluster:
Cron Job Enhancements
Fixes #8321
## Summary
Adds optional `contextPruning` config field to cron jobs, allowing per-job overrides of the global `agents.defaults.contextPruning` settings.
## Problem
Currently, all isolated cron jobs inherit global context pruning/caching settings. This means every isolated cron session caches context, even simple script-running jobs that don't need LLM continuity. This results in unnecessary token costs:
- Nightly migration job: caches 199k tokens but only runs local Ollama scripts
- Backup jobs: cache 24-33k tokens each but just run shell commands
- Auto-commit, qmd-refresh: cache context they never use
**Impact:** ~$40-50/month in unnecessary caching costs for typical setups with 8-12 cron jobs.
## Solution
Added `contextPruning?: AgentContextPruningConfig` field to `CronJob` type. When set, it overrides `agents.defaults.contextPruning` for that job's isolated session.
### Example Usage
Disable caching for a simple backup job:
```json
{
"name": "nightly-backup",
"schedule": { "kind": "cron", "expr": "0 2 * * *" },
"sessionTarget": "isolated",
"payload": {
"kind": "agentTurn",
"message": "Run backup scripts"
},
"contextPruning": { "mode": "off" }
}
```
Or use different cache TTL:
```json
{
"name": "morning-brief",
"contextPruning": {
"mode": "cache-ttl",
"ttl": "5 minutes"
}
}
```
## Implementation
1. **Added `contextPruning` field** to `CronJob`, `CronJobCreate`, `CronJobPatch` types
2. **Updated isolated agent runner** (`cron/isolated-agent/run.ts`) to merge per-job contextPruning into agent config before execution
3. **Extracted ContextPruningSchema** from inline definition in `zod-schema.agent-defaults.ts` for reuse
4. **Added TypeBox schema** (`ContextPruningConfigSchema`) to gateway protocol for API validation
5. **Updated protocol schemas** to support contextPruning in `cron.add` and `cron.update`
## Benefits
- **Cost savings**: ~$40-50/month for typical setups
- **Flexibility**: Keep caching for complex jobs, disable for simple script runners
- **Explicit intent**: Makes it clear which jobs need context continuity
- **Backward compatible**: Existing jobs continue using global settings
## Changes
- `src/cron/types.ts`: Added `contextPruning` field with documentation
- `src/cron/isolated-agent/run.ts`: Apply per-job contextPruning override to agent config
- `src/config/zod-schema.agent-defaults.ts`: Extracted ContextPruningSchema for reuse
- `src/gateway/protocol/schema/cron.ts`: Added TypeBox schema and updated CronJob/Add/Patch schemas
🤖 Generated with [Claude Code](https://claude.com/claude-code)
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR introduces an optional per-cron-job `contextPruning` configuration so isolated cron sessions can override the global `agents.defaults.contextPruning` (e.g., disabling cache-ttl for simple script jobs). The change threads the new field through cron TS types and gateway protocol schemas, and applies the override when building the per-run `agentCfg` in the isolated agent runner.
Main thing to double-check is the interaction between this override and any config-default application: `runCronIsolatedAgentTurn` constructs `cfgWithAgentDefaults`, but `resolveCronSession` is still called with `params.cfg`, which can make the override ineffective if defaults are re-applied from the original config.
<h3>Confidence Score: 3/5</h3>
- This PR is mostly safe to merge, but the new per-job override may not work reliably as implemented.
- The change is small and well-scoped (types + schema + a single override assignment), but the isolated runner mixes `cfgWithAgentDefaults` and `params.cfg` in a way that can let default-application logic overwrite a job-level `contextPruning` override. If that happens, the core advertised behavior (disabling caching per job) won’t be achieved in some configurations.
- src/cron/isolated-agent/run.ts
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#23562: feat: add sessionFreshness config for isolated cron jobs (#23539)
by MunemHashmi · 2026-02-22
79.3%
#11816: fix(cron): forward agent-specific exec config to isolated cron sess...
by AnonO6 · 2026-02-08
78.2%
#23501: fix(cron): force new session ID for isolated cron jobs (#23470)
by stakeswky · 2026-02-22
77.1%
#12310: cron: pass agentDir to embedded runner for isolated sessions
by magendary · 2026-02-09
76.7%
#14556: fix(cron): exclude sandbox from shallow merge in isolated agent config
by seheepeak · 2026-02-12
76.2%
#11474: fix(cron): respect subagents.model in isolated cron sessions
by AnonO6 · 2026-02-07
76.2%
#13065: fix(cron): Fix "every" schedule not re-arming after gateway restart
by trevorgordon981 · 2026-02-10
75.8%
#6315: fix(cron): persist isolated sessions (fixes #6217) - attempt 2
by ChaitanyaSai-Meka · 2026-02-01
75.5%
#19385: fix: pass authProfileId from cron session to runEmbeddedPiAgent
by gigi-trifle · 2026-02-17
75.4%
#16390: fix(cron): jobs land in wrong agent session when agentId isn't in a...
by yinghaosang · 2026-02-14
74.8%